re PR target/63947 (Wrong fcmov suffix)
[official-gcc.git] / gcc / gimplify.c
blob8e3dd834a01fb053d6e4bef876536ab9becae74e
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,
93 GOVD_MAP_TO_ONLY = 8192,
94 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
95 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
96 | GOVD_LOCAL)
100 enum omp_region_type
102 ORT_WORKSHARE = 0,
103 ORT_SIMD = 1,
104 ORT_PARALLEL = 2,
105 ORT_COMBINED_PARALLEL = 3,
106 ORT_TASK = 4,
107 ORT_UNTIED_TASK = 5,
108 ORT_TEAMS = 8,
109 ORT_TARGET_DATA = 16,
110 ORT_TARGET = 32
113 /* Gimplify hashtable helper. */
115 struct gimplify_hasher : typed_free_remove <elt_t>
117 typedef elt_t value_type;
118 typedef elt_t compare_type;
119 static inline hashval_t hash (const value_type *);
120 static inline bool equal (const value_type *, const compare_type *);
123 struct gimplify_ctx
125 struct gimplify_ctx *prev_context;
127 vec<gbind *> bind_expr_stack;
128 tree temps;
129 gimple_seq conditional_cleanups;
130 tree exit_label;
131 tree return_temp;
133 vec<tree> case_labels;
134 /* The formal temporary table. Should this be persistent? */
135 hash_table<gimplify_hasher> *temp_htab;
137 int conditions;
138 bool save_stack;
139 bool into_ssa;
140 bool allow_rhs_cond_expr;
141 bool in_cleanup_point_expr;
144 struct gimplify_omp_ctx
146 struct gimplify_omp_ctx *outer_context;
147 splay_tree variables;
148 hash_set<tree> *privatized_types;
149 location_t location;
150 enum omp_clause_default_kind default_kind;
151 enum omp_region_type region_type;
152 bool combined_loop;
153 bool distribute;
156 static struct gimplify_ctx *gimplify_ctxp;
157 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
159 /* Forward declaration. */
160 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
162 /* Shorter alias name for the above function for use in gimplify.c
163 only. */
165 static inline void
166 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
168 gimple_seq_add_stmt_without_update (seq_p, gs);
171 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
172 NULL, a new sequence is allocated. This function is
173 similar to gimple_seq_add_seq, but does not scan the operands.
174 During gimplification, we need to manipulate statement sequences
175 before the def/use vectors have been constructed. */
177 static void
178 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
180 gimple_stmt_iterator si;
182 if (src == NULL)
183 return;
185 si = gsi_last (*dst_p);
186 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
190 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
191 and popping gimplify contexts. */
193 static struct gimplify_ctx *ctx_pool = NULL;
195 /* Return a gimplify context struct from the pool. */
197 static inline struct gimplify_ctx *
198 ctx_alloc (void)
200 struct gimplify_ctx * c = ctx_pool;
202 if (c)
203 ctx_pool = c->prev_context;
204 else
205 c = XNEW (struct gimplify_ctx);
207 memset (c, '\0', sizeof (*c));
208 return c;
211 /* Put gimplify context C back into the pool. */
213 static inline void
214 ctx_free (struct gimplify_ctx *c)
216 c->prev_context = ctx_pool;
217 ctx_pool = c;
220 /* Free allocated ctx stack memory. */
222 void
223 free_gimplify_stack (void)
225 struct gimplify_ctx *c;
227 while ((c = ctx_pool))
229 ctx_pool = c->prev_context;
230 free (c);
235 /* Set up a context for the gimplifier. */
237 void
238 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
240 struct gimplify_ctx *c = ctx_alloc ();
242 c->prev_context = gimplify_ctxp;
243 gimplify_ctxp = c;
244 gimplify_ctxp->into_ssa = in_ssa;
245 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
248 /* Tear down a context for the gimplifier. If BODY is non-null, then
249 put the temporaries into the outer BIND_EXPR. Otherwise, put them
250 in the local_decls.
252 BODY is not a sequence, but the first tuple in a sequence. */
254 void
255 pop_gimplify_context (gimple body)
257 struct gimplify_ctx *c = gimplify_ctxp;
259 gcc_assert (c
260 && (!c->bind_expr_stack.exists ()
261 || c->bind_expr_stack.is_empty ()));
262 c->bind_expr_stack.release ();
263 gimplify_ctxp = c->prev_context;
265 if (body)
266 declare_vars (c->temps, body, false);
267 else
268 record_vars (c->temps);
270 delete c->temp_htab;
271 c->temp_htab = NULL;
272 ctx_free (c);
275 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
277 static void
278 gimple_push_bind_expr (gbind *bind_stmt)
280 gimplify_ctxp->bind_expr_stack.reserve (8);
281 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
284 /* Pop the first element off the stack of bindings. */
286 static void
287 gimple_pop_bind_expr (void)
289 gimplify_ctxp->bind_expr_stack.pop ();
292 /* Return the first element of the stack of bindings. */
294 gbind *
295 gimple_current_bind_expr (void)
297 return gimplify_ctxp->bind_expr_stack.last ();
300 /* Return the stack of bindings created during gimplification. */
302 vec<gbind *>
303 gimple_bind_expr_stack (void)
305 return gimplify_ctxp->bind_expr_stack;
308 /* Return true iff there is a COND_EXPR between us and the innermost
309 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
311 static bool
312 gimple_conditional_context (void)
314 return gimplify_ctxp->conditions > 0;
317 /* Note that we've entered a COND_EXPR. */
319 static void
320 gimple_push_condition (void)
322 #ifdef ENABLE_GIMPLE_CHECKING
323 if (gimplify_ctxp->conditions == 0)
324 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
325 #endif
326 ++(gimplify_ctxp->conditions);
329 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
330 now, add any conditional cleanups we've seen to the prequeue. */
332 static void
333 gimple_pop_condition (gimple_seq *pre_p)
335 int conds = --(gimplify_ctxp->conditions);
337 gcc_assert (conds >= 0);
338 if (conds == 0)
340 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
341 gimplify_ctxp->conditional_cleanups = NULL;
345 /* A stable comparison routine for use with splay trees and DECLs. */
347 static int
348 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
350 tree a = (tree) xa;
351 tree b = (tree) xb;
353 return DECL_UID (a) - DECL_UID (b);
356 /* Create a new omp construct that deals with variable remapping. */
358 static struct gimplify_omp_ctx *
359 new_omp_context (enum omp_region_type region_type)
361 struct gimplify_omp_ctx *c;
363 c = XCNEW (struct gimplify_omp_ctx);
364 c->outer_context = gimplify_omp_ctxp;
365 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
366 c->privatized_types = new hash_set<tree>;
367 c->location = input_location;
368 c->region_type = region_type;
369 if ((region_type & ORT_TASK) == 0)
370 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
371 else
372 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
374 return c;
377 /* Destroy an omp construct that deals with variable remapping. */
379 static void
380 delete_omp_context (struct gimplify_omp_ctx *c)
382 splay_tree_delete (c->variables);
383 delete c->privatized_types;
384 XDELETE (c);
387 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
388 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
390 /* Both gimplify the statement T and append it to *SEQ_P. This function
391 behaves exactly as gimplify_stmt, but you don't have to pass T as a
392 reference. */
394 void
395 gimplify_and_add (tree t, gimple_seq *seq_p)
397 gimplify_stmt (&t, seq_p);
400 /* Gimplify statement T into sequence *SEQ_P, and return the first
401 tuple in the sequence of generated tuples for this statement.
402 Return NULL if gimplifying T produced no tuples. */
404 static gimple
405 gimplify_and_return_first (tree t, gimple_seq *seq_p)
407 gimple_stmt_iterator last = gsi_last (*seq_p);
409 gimplify_and_add (t, seq_p);
411 if (!gsi_end_p (last))
413 gsi_next (&last);
414 return gsi_stmt (last);
416 else
417 return gimple_seq_first_stmt (*seq_p);
420 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
421 LHS, or for a call argument. */
423 static bool
424 is_gimple_mem_rhs (tree t)
426 /* If we're dealing with a renamable type, either source or dest must be
427 a renamed variable. */
428 if (is_gimple_reg_type (TREE_TYPE (t)))
429 return is_gimple_val (t);
430 else
431 return is_gimple_val (t) || is_gimple_lvalue (t);
434 /* Return true if T is a CALL_EXPR or an expression that can be
435 assigned to a temporary. Note that this predicate should only be
436 used during gimplification. See the rationale for this in
437 gimplify_modify_expr. */
439 static bool
440 is_gimple_reg_rhs_or_call (tree t)
442 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
443 || TREE_CODE (t) == CALL_EXPR);
446 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
447 this predicate should only be used during gimplification. See the
448 rationale for this in gimplify_modify_expr. */
450 static bool
451 is_gimple_mem_rhs_or_call (tree t)
453 /* If we're dealing with a renamable type, either source or dest must be
454 a renamed variable. */
455 if (is_gimple_reg_type (TREE_TYPE (t)))
456 return is_gimple_val (t);
457 else
458 return (is_gimple_val (t) || is_gimple_lvalue (t)
459 || TREE_CODE (t) == CALL_EXPR);
462 /* Create a temporary with a name derived from VAL. Subroutine of
463 lookup_tmp_var; nobody else should call this function. */
465 static inline tree
466 create_tmp_from_val (tree val)
468 /* Drop all qualifiers and address-space information from the value type. */
469 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
470 tree var = create_tmp_var (type, get_name (val));
471 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
472 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
473 DECL_GIMPLE_REG_P (var) = 1;
474 return var;
477 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
478 an existing expression temporary. */
480 static tree
481 lookup_tmp_var (tree val, bool is_formal)
483 tree ret;
485 /* If not optimizing, never really reuse a temporary. local-alloc
486 won't allocate any variable that is used in more than one basic
487 block, which means it will go into memory, causing much extra
488 work in reload and final and poorer code generation, outweighing
489 the extra memory allocation here. */
490 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
491 ret = create_tmp_from_val (val);
492 else
494 elt_t elt, *elt_p;
495 elt_t **slot;
497 elt.val = val;
498 if (!gimplify_ctxp->temp_htab)
499 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
500 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
501 if (*slot == NULL)
503 elt_p = XNEW (elt_t);
504 elt_p->val = val;
505 elt_p->temp = ret = create_tmp_from_val (val);
506 *slot = elt_p;
508 else
510 elt_p = *slot;
511 ret = elt_p->temp;
515 return ret;
518 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
520 static tree
521 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
522 bool is_formal)
524 tree t, mod;
526 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
527 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
528 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
529 fb_rvalue);
531 if (gimplify_ctxp->into_ssa
532 && is_gimple_reg_type (TREE_TYPE (val)))
533 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
534 else
535 t = lookup_tmp_var (val, is_formal);
537 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
539 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
541 /* gimplify_modify_expr might want to reduce this further. */
542 gimplify_and_add (mod, pre_p);
543 ggc_free (mod);
545 return t;
548 /* Return a formal temporary variable initialized with VAL. PRE_P is as
549 in gimplify_expr. Only use this function if:
551 1) The value of the unfactored expression represented by VAL will not
552 change between the initialization and use of the temporary, and
553 2) The temporary will not be otherwise modified.
555 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
556 and #2 means it is inappropriate for && temps.
558 For other cases, use get_initialized_tmp_var instead. */
560 tree
561 get_formal_tmp_var (tree val, gimple_seq *pre_p)
563 return internal_get_tmp_var (val, pre_p, NULL, true);
566 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
567 are as in gimplify_expr. */
569 tree
570 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
572 return internal_get_tmp_var (val, pre_p, post_p, false);
575 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
576 generate debug info for them; otherwise don't. */
578 void
579 declare_vars (tree vars, gimple gs, bool debug_info)
581 tree last = vars;
582 if (last)
584 tree temps, block;
586 gbind *scope = as_a <gbind *> (gs);
588 temps = nreverse (last);
590 block = gimple_bind_block (scope);
591 gcc_assert (!block || TREE_CODE (block) == BLOCK);
592 if (!block || !debug_info)
594 DECL_CHAIN (last) = gimple_bind_vars (scope);
595 gimple_bind_set_vars (scope, temps);
597 else
599 /* We need to attach the nodes both to the BIND_EXPR and to its
600 associated BLOCK for debugging purposes. The key point here
601 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
602 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
603 if (BLOCK_VARS (block))
604 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
605 else
607 gimple_bind_set_vars (scope,
608 chainon (gimple_bind_vars (scope), temps));
609 BLOCK_VARS (block) = temps;
615 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
616 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
617 no such upper bound can be obtained. */
619 static void
620 force_constant_size (tree var)
622 /* The only attempt we make is by querying the maximum size of objects
623 of the variable's type. */
625 HOST_WIDE_INT max_size;
627 gcc_assert (TREE_CODE (var) == VAR_DECL);
629 max_size = max_int_size_in_bytes (TREE_TYPE (var));
631 gcc_assert (max_size >= 0);
633 DECL_SIZE_UNIT (var)
634 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
635 DECL_SIZE (var)
636 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
639 /* Push the temporary variable TMP into the current binding. */
641 void
642 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
644 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
646 /* Later processing assumes that the object size is constant, which might
647 not be true at this point. Force the use of a constant upper bound in
648 this case. */
649 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
650 force_constant_size (tmp);
652 DECL_CONTEXT (tmp) = fn->decl;
653 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
655 record_vars_into (tmp, fn->decl);
658 /* Push the temporary variable TMP into the current binding. */
660 void
661 gimple_add_tmp_var (tree tmp)
663 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
665 /* Later processing assumes that the object size is constant, which might
666 not be true at this point. Force the use of a constant upper bound in
667 this case. */
668 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
669 force_constant_size (tmp);
671 DECL_CONTEXT (tmp) = current_function_decl;
672 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
674 if (gimplify_ctxp)
676 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
677 gimplify_ctxp->temps = tmp;
679 /* Mark temporaries local within the nearest enclosing parallel. */
680 if (gimplify_omp_ctxp)
682 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
683 while (ctx
684 && (ctx->region_type == ORT_WORKSHARE
685 || ctx->region_type == ORT_SIMD))
686 ctx = ctx->outer_context;
687 if (ctx)
688 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
691 else if (cfun)
692 record_vars (tmp);
693 else
695 gimple_seq body_seq;
697 /* This case is for nested functions. We need to expose the locals
698 they create. */
699 body_seq = gimple_body (current_function_decl);
700 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
706 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
707 nodes that are referenced more than once in GENERIC functions. This is
708 necessary because gimplification (translation into GIMPLE) is performed
709 by modifying tree nodes in-place, so gimplication of a shared node in a
710 first context could generate an invalid GIMPLE form in a second context.
712 This is achieved with a simple mark/copy/unmark algorithm that walks the
713 GENERIC representation top-down, marks nodes with TREE_VISITED the first
714 time it encounters them, duplicates them if they already have TREE_VISITED
715 set, and finally removes the TREE_VISITED marks it has set.
717 The algorithm works only at the function level, i.e. it generates a GENERIC
718 representation of a function with no nodes shared within the function when
719 passed a GENERIC function (except for nodes that are allowed to be shared).
721 At the global level, it is also necessary to unshare tree nodes that are
722 referenced in more than one function, for the same aforementioned reason.
723 This requires some cooperation from the front-end. There are 2 strategies:
725 1. Manual unsharing. The front-end needs to call unshare_expr on every
726 expression that might end up being shared across functions.
728 2. Deep unsharing. This is an extension of regular unsharing. Instead
729 of calling unshare_expr on expressions that might be shared across
730 functions, the front-end pre-marks them with TREE_VISITED. This will
731 ensure that they are unshared on the first reference within functions
732 when the regular unsharing algorithm runs. The counterpart is that
733 this algorithm must look deeper than for manual unsharing, which is
734 specified by LANG_HOOKS_DEEP_UNSHARING.
736 If there are only few specific cases of node sharing across functions, it is
737 probably easier for a front-end to unshare the expressions manually. On the
738 contrary, if the expressions generated at the global level are as widespread
739 as expressions generated within functions, deep unsharing is very likely the
740 way to go. */
742 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
743 These nodes model computations that must be done once. If we were to
744 unshare something like SAVE_EXPR(i++), the gimplification process would
745 create wrong code. However, if DATA is non-null, it must hold a pointer
746 set that is used to unshare the subtrees of these nodes. */
748 static tree
749 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
751 tree t = *tp;
752 enum tree_code code = TREE_CODE (t);
754 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
755 copy their subtrees if we can make sure to do it only once. */
756 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
758 if (data && !((hash_set<tree> *)data)->add (t))
760 else
761 *walk_subtrees = 0;
764 /* Stop at types, decls, constants like copy_tree_r. */
765 else if (TREE_CODE_CLASS (code) == tcc_type
766 || TREE_CODE_CLASS (code) == tcc_declaration
767 || TREE_CODE_CLASS (code) == tcc_constant
768 /* We can't do anything sensible with a BLOCK used as an
769 expression, but we also can't just die when we see it
770 because of non-expression uses. So we avert our eyes
771 and cross our fingers. Silly Java. */
772 || code == BLOCK)
773 *walk_subtrees = 0;
775 /* Cope with the statement expression extension. */
776 else if (code == STATEMENT_LIST)
779 /* Leave the bulk of the work to copy_tree_r itself. */
780 else
781 copy_tree_r (tp, walk_subtrees, NULL);
783 return NULL_TREE;
786 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
787 If *TP has been visited already, then *TP is deeply copied by calling
788 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
790 static tree
791 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
793 tree t = *tp;
794 enum tree_code code = TREE_CODE (t);
796 /* Skip types, decls, and constants. But we do want to look at their
797 types and the bounds of types. Mark them as visited so we properly
798 unmark their subtrees on the unmark pass. If we've already seen them,
799 don't look down further. */
800 if (TREE_CODE_CLASS (code) == tcc_type
801 || TREE_CODE_CLASS (code) == tcc_declaration
802 || TREE_CODE_CLASS (code) == tcc_constant)
804 if (TREE_VISITED (t))
805 *walk_subtrees = 0;
806 else
807 TREE_VISITED (t) = 1;
810 /* If this node has been visited already, unshare it and don't look
811 any deeper. */
812 else if (TREE_VISITED (t))
814 walk_tree (tp, mostly_copy_tree_r, data, NULL);
815 *walk_subtrees = 0;
818 /* Otherwise, mark the node as visited and keep looking. */
819 else
820 TREE_VISITED (t) = 1;
822 return NULL_TREE;
825 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
826 copy_if_shared_r callback unmodified. */
828 static inline void
829 copy_if_shared (tree *tp, void *data)
831 walk_tree (tp, copy_if_shared_r, data, NULL);
834 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
835 any nested functions. */
837 static void
838 unshare_body (tree fndecl)
840 struct cgraph_node *cgn = cgraph_node::get (fndecl);
841 /* If the language requires deep unsharing, we need a pointer set to make
842 sure we don't repeatedly unshare subtrees of unshareable nodes. */
843 hash_set<tree> *visited
844 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
846 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
847 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
848 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
850 delete visited;
852 if (cgn)
853 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
854 unshare_body (cgn->decl);
857 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
858 Subtrees are walked until the first unvisited node is encountered. */
860 static tree
861 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
863 tree t = *tp;
865 /* If this node has been visited, unmark it and keep looking. */
866 if (TREE_VISITED (t))
867 TREE_VISITED (t) = 0;
869 /* Otherwise, don't look any deeper. */
870 else
871 *walk_subtrees = 0;
873 return NULL_TREE;
876 /* Unmark the visited trees rooted at *TP. */
878 static inline void
879 unmark_visited (tree *tp)
881 walk_tree (tp, unmark_visited_r, NULL, NULL);
884 /* Likewise, but mark all trees as not visited. */
886 static void
887 unvisit_body (tree fndecl)
889 struct cgraph_node *cgn = cgraph_node::get (fndecl);
891 unmark_visited (&DECL_SAVED_TREE (fndecl));
892 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
893 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
895 if (cgn)
896 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
897 unvisit_body (cgn->decl);
900 /* Unconditionally make an unshared copy of EXPR. This is used when using
901 stored expressions which span multiple functions, such as BINFO_VTABLE,
902 as the normal unsharing process can't tell that they're shared. */
904 tree
905 unshare_expr (tree expr)
907 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
908 return expr;
911 /* Worker for unshare_expr_without_location. */
913 static tree
914 prune_expr_location (tree *tp, int *walk_subtrees, void *)
916 if (EXPR_P (*tp))
917 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
918 else
919 *walk_subtrees = 0;
920 return NULL_TREE;
923 /* Similar to unshare_expr but also prune all expression locations
924 from EXPR. */
926 tree
927 unshare_expr_without_location (tree expr)
929 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
930 if (EXPR_P (expr))
931 walk_tree (&expr, prune_expr_location, NULL, NULL);
932 return expr;
935 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
936 contain statements and have a value. Assign its value to a temporary
937 and give it void_type_node. Return the temporary, or NULL_TREE if
938 WRAPPER was already void. */
940 tree
941 voidify_wrapper_expr (tree wrapper, tree temp)
943 tree type = TREE_TYPE (wrapper);
944 if (type && !VOID_TYPE_P (type))
946 tree *p;
948 /* Set p to point to the body of the wrapper. Loop until we find
949 something that isn't a wrapper. */
950 for (p = &wrapper; p && *p; )
952 switch (TREE_CODE (*p))
954 case BIND_EXPR:
955 TREE_SIDE_EFFECTS (*p) = 1;
956 TREE_TYPE (*p) = void_type_node;
957 /* For a BIND_EXPR, the body is operand 1. */
958 p = &BIND_EXPR_BODY (*p);
959 break;
961 case CLEANUP_POINT_EXPR:
962 case TRY_FINALLY_EXPR:
963 case TRY_CATCH_EXPR:
964 TREE_SIDE_EFFECTS (*p) = 1;
965 TREE_TYPE (*p) = void_type_node;
966 p = &TREE_OPERAND (*p, 0);
967 break;
969 case STATEMENT_LIST:
971 tree_stmt_iterator i = tsi_last (*p);
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
974 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
976 break;
978 case COMPOUND_EXPR:
979 /* Advance to the last statement. Set all container types to
980 void. */
981 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
983 TREE_SIDE_EFFECTS (*p) = 1;
984 TREE_TYPE (*p) = void_type_node;
986 break;
988 case TRANSACTION_EXPR:
989 TREE_SIDE_EFFECTS (*p) = 1;
990 TREE_TYPE (*p) = void_type_node;
991 p = &TRANSACTION_EXPR_BODY (*p);
992 break;
994 default:
995 /* Assume that any tree upon which voidify_wrapper_expr is
996 directly called is a wrapper, and that its body is op0. */
997 if (p == &wrapper)
999 TREE_SIDE_EFFECTS (*p) = 1;
1000 TREE_TYPE (*p) = void_type_node;
1001 p = &TREE_OPERAND (*p, 0);
1002 break;
1004 goto out;
1008 out:
1009 if (p == NULL || IS_EMPTY_STMT (*p))
1010 temp = NULL_TREE;
1011 else if (temp)
1013 /* The wrapper is on the RHS of an assignment that we're pushing
1014 down. */
1015 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1016 || TREE_CODE (temp) == MODIFY_EXPR);
1017 TREE_OPERAND (temp, 1) = *p;
1018 *p = temp;
1020 else
1022 temp = create_tmp_var (type, "retval");
1023 *p = build2 (INIT_EXPR, type, temp, *p);
1026 return temp;
1029 return NULL_TREE;
1032 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1033 a temporary through which they communicate. */
1035 static void
1036 build_stack_save_restore (gcall **save, gcall **restore)
1038 tree tmp_var;
1040 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1041 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1042 gimple_call_set_lhs (*save, tmp_var);
1044 *restore
1045 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1046 1, tmp_var);
1049 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1051 static enum gimplify_status
1052 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1054 tree bind_expr = *expr_p;
1055 bool old_save_stack = gimplify_ctxp->save_stack;
1056 tree t;
1057 gbind *bind_stmt;
1058 gimple_seq body, cleanup;
1059 gcall *stack_save;
1060 location_t start_locus = 0, end_locus = 0;
1062 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1064 /* Mark variables seen in this bind expr. */
1065 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1067 if (TREE_CODE (t) == VAR_DECL)
1069 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1071 /* Mark variable as local. */
1072 if (ctx && !DECL_EXTERNAL (t)
1073 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1074 || splay_tree_lookup (ctx->variables,
1075 (splay_tree_key) t) == NULL))
1077 if (ctx->region_type == ORT_SIMD
1078 && TREE_ADDRESSABLE (t)
1079 && !TREE_STATIC (t))
1080 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1081 else
1082 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1085 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1087 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1088 cfun->has_local_explicit_reg_vars = true;
1091 /* Preliminarily mark non-addressed complex variables as eligible
1092 for promotion to gimple registers. We'll transform their uses
1093 as we find them. */
1094 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1095 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1096 && !TREE_THIS_VOLATILE (t)
1097 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1098 && !needs_to_live_in_memory (t))
1099 DECL_GIMPLE_REG_P (t) = 1;
1102 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1103 BIND_EXPR_BLOCK (bind_expr));
1104 gimple_push_bind_expr (bind_stmt);
1106 gimplify_ctxp->save_stack = false;
1108 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1109 body = NULL;
1110 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1111 gimple_bind_set_body (bind_stmt, body);
1113 /* Source location wise, the cleanup code (stack_restore and clobbers)
1114 belongs to the end of the block, so propagate what we have. The
1115 stack_save operation belongs to the beginning of block, which we can
1116 infer from the bind_expr directly if the block has no explicit
1117 assignment. */
1118 if (BIND_EXPR_BLOCK (bind_expr))
1120 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1121 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1123 if (start_locus == 0)
1124 start_locus = EXPR_LOCATION (bind_expr);
1126 cleanup = NULL;
1127 stack_save = NULL;
1128 if (gimplify_ctxp->save_stack)
1130 gcall *stack_restore;
1132 /* Save stack on entry and restore it on exit. Add a try_finally
1133 block to achieve this. */
1134 build_stack_save_restore (&stack_save, &stack_restore);
1136 gimple_set_location (stack_save, start_locus);
1137 gimple_set_location (stack_restore, end_locus);
1139 gimplify_seq_add_stmt (&cleanup, stack_restore);
1142 /* Add clobbers for all variables that go out of scope. */
1143 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1145 if (TREE_CODE (t) == VAR_DECL
1146 && !is_global_var (t)
1147 && DECL_CONTEXT (t) == current_function_decl
1148 && !DECL_HARD_REGISTER (t)
1149 && !TREE_THIS_VOLATILE (t)
1150 && !DECL_HAS_VALUE_EXPR_P (t)
1151 /* Only care for variables that have to be in memory. Others
1152 will be rewritten into SSA names, hence moved to the top-level. */
1153 && !is_gimple_reg (t)
1154 && flag_stack_reuse != SR_NONE)
1156 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1157 gimple clobber_stmt;
1158 TREE_THIS_VOLATILE (clobber) = 1;
1159 clobber_stmt = gimple_build_assign (t, clobber);
1160 gimple_set_location (clobber_stmt, end_locus);
1161 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1165 if (cleanup)
1167 gtry *gs;
1168 gimple_seq new_body;
1170 new_body = NULL;
1171 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1172 GIMPLE_TRY_FINALLY);
1174 if (stack_save)
1175 gimplify_seq_add_stmt (&new_body, stack_save);
1176 gimplify_seq_add_stmt (&new_body, gs);
1177 gimple_bind_set_body (bind_stmt, new_body);
1180 gimplify_ctxp->save_stack = old_save_stack;
1181 gimple_pop_bind_expr ();
1183 gimplify_seq_add_stmt (pre_p, bind_stmt);
1185 if (temp)
1187 *expr_p = temp;
1188 return GS_OK;
1191 *expr_p = NULL_TREE;
1192 return GS_ALL_DONE;
1195 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1196 GIMPLE value, it is assigned to a new temporary and the statement is
1197 re-written to return the temporary.
1199 PRE_P points to the sequence where side effects that must happen before
1200 STMT should be stored. */
1202 static enum gimplify_status
1203 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1205 greturn *ret;
1206 tree ret_expr = TREE_OPERAND (stmt, 0);
1207 tree result_decl, result;
1209 if (ret_expr == error_mark_node)
1210 return GS_ERROR;
1212 /* Implicit _Cilk_sync must be inserted right before any return statement
1213 if there is a _Cilk_spawn in the function. If the user has provided a
1214 _Cilk_sync, the optimizer should remove this duplicate one. */
1215 if (fn_contains_cilk_spawn_p (cfun))
1217 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1218 gimplify_and_add (impl_sync, pre_p);
1221 if (!ret_expr
1222 || TREE_CODE (ret_expr) == RESULT_DECL
1223 || ret_expr == error_mark_node)
1225 greturn *ret = gimple_build_return (ret_expr);
1226 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1227 gimplify_seq_add_stmt (pre_p, ret);
1228 return GS_ALL_DONE;
1231 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1232 result_decl = NULL_TREE;
1233 else
1235 result_decl = TREE_OPERAND (ret_expr, 0);
1237 /* See through a return by reference. */
1238 if (TREE_CODE (result_decl) == INDIRECT_REF)
1239 result_decl = TREE_OPERAND (result_decl, 0);
1241 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1242 || TREE_CODE (ret_expr) == INIT_EXPR)
1243 && TREE_CODE (result_decl) == RESULT_DECL);
1246 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1247 Recall that aggregate_value_p is FALSE for any aggregate type that is
1248 returned in registers. If we're returning values in registers, then
1249 we don't want to extend the lifetime of the RESULT_DECL, particularly
1250 across another call. In addition, for those aggregates for which
1251 hard_function_value generates a PARALLEL, we'll die during normal
1252 expansion of structure assignments; there's special code in expand_return
1253 to handle this case that does not exist in expand_expr. */
1254 if (!result_decl)
1255 result = NULL_TREE;
1256 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1258 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1260 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1261 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1262 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1263 should be effectively allocated by the caller, i.e. all calls to
1264 this function must be subject to the Return Slot Optimization. */
1265 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1266 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1268 result = result_decl;
1270 else if (gimplify_ctxp->return_temp)
1271 result = gimplify_ctxp->return_temp;
1272 else
1274 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1276 /* ??? With complex control flow (usually involving abnormal edges),
1277 we can wind up warning about an uninitialized value for this. Due
1278 to how this variable is constructed and initialized, this is never
1279 true. Give up and never warn. */
1280 TREE_NO_WARNING (result) = 1;
1282 gimplify_ctxp->return_temp = result;
1285 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1286 Then gimplify the whole thing. */
1287 if (result != result_decl)
1288 TREE_OPERAND (ret_expr, 0) = result;
1290 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1292 ret = gimple_build_return (result);
1293 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1294 gimplify_seq_add_stmt (pre_p, ret);
1296 return GS_ALL_DONE;
1299 /* Gimplify a variable-length array DECL. */
1301 static void
1302 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1304 /* This is a variable-sized decl. Simplify its size and mark it
1305 for deferred expansion. */
1306 tree t, addr, ptr_type;
1308 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1309 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1311 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1312 if (DECL_HAS_VALUE_EXPR_P (decl))
1313 return;
1315 /* All occurrences of this decl in final gimplified code will be
1316 replaced by indirection. Setting DECL_VALUE_EXPR does two
1317 things: First, it lets the rest of the gimplifier know what
1318 replacement to use. Second, it lets the debug info know
1319 where to find the value. */
1320 ptr_type = build_pointer_type (TREE_TYPE (decl));
1321 addr = create_tmp_var (ptr_type, get_name (decl));
1322 DECL_IGNORED_P (addr) = 0;
1323 t = build_fold_indirect_ref (addr);
1324 TREE_THIS_NOTRAP (t) = 1;
1325 SET_DECL_VALUE_EXPR (decl, t);
1326 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1328 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1329 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1330 size_int (DECL_ALIGN (decl)));
1331 /* The call has been built for a variable-sized object. */
1332 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1333 t = fold_convert (ptr_type, t);
1334 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1336 gimplify_and_add (t, seq_p);
1338 /* Indicate that we need to restore the stack level when the
1339 enclosing BIND_EXPR is exited. */
1340 gimplify_ctxp->save_stack = true;
1343 /* A helper function to be called via walk_tree. Mark all labels under *TP
1344 as being forced. To be called for DECL_INITIAL of static variables. */
1346 static tree
1347 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1349 if (TYPE_P (*tp))
1350 *walk_subtrees = 0;
1351 if (TREE_CODE (*tp) == LABEL_DECL)
1352 FORCED_LABEL (*tp) = 1;
1354 return NULL_TREE;
1357 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1358 and initialization explicit. */
1360 static enum gimplify_status
1361 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1363 tree stmt = *stmt_p;
1364 tree decl = DECL_EXPR_DECL (stmt);
1366 *stmt_p = NULL_TREE;
1368 if (TREE_TYPE (decl) == error_mark_node)
1369 return GS_ERROR;
1371 if ((TREE_CODE (decl) == TYPE_DECL
1372 || TREE_CODE (decl) == VAR_DECL)
1373 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1374 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1376 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1377 in case its size expressions contain problematic nodes like CALL_EXPR. */
1378 if (TREE_CODE (decl) == TYPE_DECL
1379 && DECL_ORIGINAL_TYPE (decl)
1380 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1381 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1383 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1385 tree init = DECL_INITIAL (decl);
1387 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1388 || (!TREE_STATIC (decl)
1389 && flag_stack_check == GENERIC_STACK_CHECK
1390 && compare_tree_int (DECL_SIZE_UNIT (decl),
1391 STACK_CHECK_MAX_VAR_SIZE) > 0))
1392 gimplify_vla_decl (decl, seq_p);
1394 /* Some front ends do not explicitly declare all anonymous
1395 artificial variables. We compensate here by declaring the
1396 variables, though it would be better if the front ends would
1397 explicitly declare them. */
1398 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1399 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1400 gimple_add_tmp_var (decl);
1402 if (init && init != error_mark_node)
1404 if (!TREE_STATIC (decl))
1406 DECL_INITIAL (decl) = NULL_TREE;
1407 init = build2 (INIT_EXPR, void_type_node, decl, init);
1408 gimplify_and_add (init, seq_p);
1409 ggc_free (init);
1411 else
1412 /* We must still examine initializers for static variables
1413 as they may contain a label address. */
1414 walk_tree (&init, force_labels_r, NULL, NULL);
1418 return GS_ALL_DONE;
1421 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1422 and replacing the LOOP_EXPR with goto, but if the loop contains an
1423 EXIT_EXPR, we need to append a label for it to jump to. */
1425 static enum gimplify_status
1426 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1428 tree saved_label = gimplify_ctxp->exit_label;
1429 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1431 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1433 gimplify_ctxp->exit_label = NULL_TREE;
1435 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1437 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1439 if (gimplify_ctxp->exit_label)
1440 gimplify_seq_add_stmt (pre_p,
1441 gimple_build_label (gimplify_ctxp->exit_label));
1443 gimplify_ctxp->exit_label = saved_label;
1445 *expr_p = NULL;
1446 return GS_ALL_DONE;
1449 /* Gimplify a statement list onto a sequence. These may be created either
1450 by an enlightened front-end, or by shortcut_cond_expr. */
1452 static enum gimplify_status
1453 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1455 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1457 tree_stmt_iterator i = tsi_start (*expr_p);
1459 while (!tsi_end_p (i))
1461 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1462 tsi_delink (&i);
1465 if (temp)
1467 *expr_p = temp;
1468 return GS_OK;
1471 return GS_ALL_DONE;
1475 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1476 branch to. */
1478 static enum gimplify_status
1479 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1481 tree switch_expr = *expr_p;
1482 gimple_seq switch_body_seq = NULL;
1483 enum gimplify_status ret;
1484 tree index_type = TREE_TYPE (switch_expr);
1485 if (index_type == NULL_TREE)
1486 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1488 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1489 fb_rvalue);
1490 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1491 return ret;
1493 if (SWITCH_BODY (switch_expr))
1495 vec<tree> labels;
1496 vec<tree> saved_labels;
1497 tree default_case = NULL_TREE;
1498 gswitch *switch_stmt;
1500 /* If someone can be bothered to fill in the labels, they can
1501 be bothered to null out the body too. */
1502 gcc_assert (!SWITCH_LABELS (switch_expr));
1504 /* Save old labels, get new ones from body, then restore the old
1505 labels. Save all the things from the switch body to append after. */
1506 saved_labels = gimplify_ctxp->case_labels;
1507 gimplify_ctxp->case_labels.create (8);
1509 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1510 labels = gimplify_ctxp->case_labels;
1511 gimplify_ctxp->case_labels = saved_labels;
1513 preprocess_case_label_vec_for_gimple (labels, index_type,
1514 &default_case);
1516 if (!default_case)
1518 glabel *new_default;
1520 default_case
1521 = build_case_label (NULL_TREE, NULL_TREE,
1522 create_artificial_label (UNKNOWN_LOCATION));
1523 new_default = gimple_build_label (CASE_LABEL (default_case));
1524 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1527 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1528 default_case, labels);
1529 gimplify_seq_add_stmt (pre_p, switch_stmt);
1530 gimplify_seq_add_seq (pre_p, switch_body_seq);
1531 labels.release ();
1533 else
1534 gcc_assert (SWITCH_LABELS (switch_expr));
1536 return GS_ALL_DONE;
1539 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1541 static enum gimplify_status
1542 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1544 struct gimplify_ctx *ctxp;
1545 glabel *label_stmt;
1547 /* Invalid OpenMP programs can play Duff's Device type games with
1548 #pragma omp parallel. At least in the C front end, we don't
1549 detect such invalid branches until after gimplification. */
1550 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1551 if (ctxp->case_labels.exists ())
1552 break;
1554 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1555 ctxp->case_labels.safe_push (*expr_p);
1556 gimplify_seq_add_stmt (pre_p, label_stmt);
1558 return GS_ALL_DONE;
1561 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1562 if necessary. */
1564 tree
1565 build_and_jump (tree *label_p)
1567 if (label_p == NULL)
1568 /* If there's nowhere to jump, just fall through. */
1569 return NULL_TREE;
1571 if (*label_p == NULL_TREE)
1573 tree label = create_artificial_label (UNKNOWN_LOCATION);
1574 *label_p = label;
1577 return build1 (GOTO_EXPR, void_type_node, *label_p);
1580 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1581 This also involves building a label to jump to and communicating it to
1582 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1584 static enum gimplify_status
1585 gimplify_exit_expr (tree *expr_p)
1587 tree cond = TREE_OPERAND (*expr_p, 0);
1588 tree expr;
1590 expr = build_and_jump (&gimplify_ctxp->exit_label);
1591 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1592 *expr_p = expr;
1594 return GS_OK;
1597 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1598 different from its canonical type, wrap the whole thing inside a
1599 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1600 type.
1602 The canonical type of a COMPONENT_REF is the type of the field being
1603 referenced--unless the field is a bit-field which can be read directly
1604 in a smaller mode, in which case the canonical type is the
1605 sign-appropriate type corresponding to that mode. */
1607 static void
1608 canonicalize_component_ref (tree *expr_p)
1610 tree expr = *expr_p;
1611 tree type;
1613 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1615 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1616 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1617 else
1618 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1620 /* One could argue that all the stuff below is not necessary for
1621 the non-bitfield case and declare it a FE error if type
1622 adjustment would be needed. */
1623 if (TREE_TYPE (expr) != type)
1625 #ifdef ENABLE_TYPES_CHECKING
1626 tree old_type = TREE_TYPE (expr);
1627 #endif
1628 int type_quals;
1630 /* We need to preserve qualifiers and propagate them from
1631 operand 0. */
1632 type_quals = TYPE_QUALS (type)
1633 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1634 if (TYPE_QUALS (type) != type_quals)
1635 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1637 /* Set the type of the COMPONENT_REF to the underlying type. */
1638 TREE_TYPE (expr) = type;
1640 #ifdef ENABLE_TYPES_CHECKING
1641 /* It is now a FE error, if the conversion from the canonical
1642 type to the original expression type is not useless. */
1643 gcc_assert (useless_type_conversion_p (old_type, type));
1644 #endif
1648 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1649 to foo, embed that change in the ADDR_EXPR by converting
1650 T array[U];
1651 (T *)&array
1653 &array[L]
1654 where L is the lower bound. For simplicity, only do this for constant
1655 lower bound.
1656 The constraint is that the type of &array[L] is trivially convertible
1657 to T *. */
1659 static void
1660 canonicalize_addr_expr (tree *expr_p)
1662 tree expr = *expr_p;
1663 tree addr_expr = TREE_OPERAND (expr, 0);
1664 tree datype, ddatype, pddatype;
1666 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1667 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1668 || TREE_CODE (addr_expr) != ADDR_EXPR)
1669 return;
1671 /* The addr_expr type should be a pointer to an array. */
1672 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1673 if (TREE_CODE (datype) != ARRAY_TYPE)
1674 return;
1676 /* The pointer to element type shall be trivially convertible to
1677 the expression pointer type. */
1678 ddatype = TREE_TYPE (datype);
1679 pddatype = build_pointer_type (ddatype);
1680 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1681 pddatype))
1682 return;
1684 /* The lower bound and element sizes must be constant. */
1685 if (!TYPE_SIZE_UNIT (ddatype)
1686 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1687 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1688 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1689 return;
1691 /* All checks succeeded. Build a new node to merge the cast. */
1692 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1693 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1694 NULL_TREE, NULL_TREE);
1695 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1697 /* We can have stripped a required restrict qualifier above. */
1698 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1699 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1702 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1703 underneath as appropriate. */
1705 static enum gimplify_status
1706 gimplify_conversion (tree *expr_p)
1708 location_t loc = EXPR_LOCATION (*expr_p);
1709 gcc_assert (CONVERT_EXPR_P (*expr_p));
1711 /* Then strip away all but the outermost conversion. */
1712 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1714 /* And remove the outermost conversion if it's useless. */
1715 if (tree_ssa_useless_type_conversion (*expr_p))
1716 *expr_p = TREE_OPERAND (*expr_p, 0);
1718 /* If we still have a conversion at the toplevel,
1719 then canonicalize some constructs. */
1720 if (CONVERT_EXPR_P (*expr_p))
1722 tree sub = TREE_OPERAND (*expr_p, 0);
1724 /* If a NOP conversion is changing the type of a COMPONENT_REF
1725 expression, then canonicalize its type now in order to expose more
1726 redundant conversions. */
1727 if (TREE_CODE (sub) == COMPONENT_REF)
1728 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1730 /* If a NOP conversion is changing a pointer to array of foo
1731 to a pointer to foo, embed that change in the ADDR_EXPR. */
1732 else if (TREE_CODE (sub) == ADDR_EXPR)
1733 canonicalize_addr_expr (expr_p);
1736 /* If we have a conversion to a non-register type force the
1737 use of a VIEW_CONVERT_EXPR instead. */
1738 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1739 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1740 TREE_OPERAND (*expr_p, 0));
1742 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1743 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1744 TREE_SET_CODE (*expr_p, NOP_EXPR);
1746 return GS_OK;
1749 /* Nonlocal VLAs seen in the current function. */
1750 static hash_set<tree> *nonlocal_vlas;
1752 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1753 static tree nonlocal_vla_vars;
1755 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1756 DECL_VALUE_EXPR, and it's worth re-examining things. */
1758 static enum gimplify_status
1759 gimplify_var_or_parm_decl (tree *expr_p)
1761 tree decl = *expr_p;
1763 /* ??? If this is a local variable, and it has not been seen in any
1764 outer BIND_EXPR, then it's probably the result of a duplicate
1765 declaration, for which we've already issued an error. It would
1766 be really nice if the front end wouldn't leak these at all.
1767 Currently the only known culprit is C++ destructors, as seen
1768 in g++.old-deja/g++.jason/binding.C. */
1769 if (TREE_CODE (decl) == VAR_DECL
1770 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1771 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1772 && decl_function_context (decl) == current_function_decl)
1774 gcc_assert (seen_error ());
1775 return GS_ERROR;
1778 /* When within an OpenMP context, notice uses of variables. */
1779 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1780 return GS_ALL_DONE;
1782 /* If the decl is an alias for another expression, substitute it now. */
1783 if (DECL_HAS_VALUE_EXPR_P (decl))
1785 tree value_expr = DECL_VALUE_EXPR (decl);
1787 /* For referenced nonlocal VLAs add a decl for debugging purposes
1788 to the current function. */
1789 if (TREE_CODE (decl) == VAR_DECL
1790 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1791 && nonlocal_vlas != NULL
1792 && TREE_CODE (value_expr) == INDIRECT_REF
1793 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1794 && decl_function_context (decl) != current_function_decl)
1796 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1797 while (ctx
1798 && (ctx->region_type == ORT_WORKSHARE
1799 || ctx->region_type == ORT_SIMD))
1800 ctx = ctx->outer_context;
1801 if (!ctx && !nonlocal_vlas->add (decl))
1803 tree copy = copy_node (decl);
1805 lang_hooks.dup_lang_specific_decl (copy);
1806 SET_DECL_RTL (copy, 0);
1807 TREE_USED (copy) = 1;
1808 DECL_CHAIN (copy) = nonlocal_vla_vars;
1809 nonlocal_vla_vars = copy;
1810 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1811 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1815 *expr_p = unshare_expr (value_expr);
1816 return GS_OK;
1819 return GS_ALL_DONE;
1822 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1824 static void
1825 recalculate_side_effects (tree t)
1827 enum tree_code code = TREE_CODE (t);
1828 int len = TREE_OPERAND_LENGTH (t);
1829 int i;
1831 switch (TREE_CODE_CLASS (code))
1833 case tcc_expression:
1834 switch (code)
1836 case INIT_EXPR:
1837 case MODIFY_EXPR:
1838 case VA_ARG_EXPR:
1839 case PREDECREMENT_EXPR:
1840 case PREINCREMENT_EXPR:
1841 case POSTDECREMENT_EXPR:
1842 case POSTINCREMENT_EXPR:
1843 /* All of these have side-effects, no matter what their
1844 operands are. */
1845 return;
1847 default:
1848 break;
1850 /* Fall through. */
1852 case tcc_comparison: /* a comparison expression */
1853 case tcc_unary: /* a unary arithmetic expression */
1854 case tcc_binary: /* a binary arithmetic expression */
1855 case tcc_reference: /* a reference */
1856 case tcc_vl_exp: /* a function call */
1857 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1858 for (i = 0; i < len; ++i)
1860 tree op = TREE_OPERAND (t, i);
1861 if (op && TREE_SIDE_EFFECTS (op))
1862 TREE_SIDE_EFFECTS (t) = 1;
1864 break;
1866 case tcc_constant:
1867 /* No side-effects. */
1868 return;
1870 default:
1871 gcc_unreachable ();
1875 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1876 node *EXPR_P.
1878 compound_lval
1879 : min_lval '[' val ']'
1880 | min_lval '.' ID
1881 | compound_lval '[' val ']'
1882 | compound_lval '.' ID
1884 This is not part of the original SIMPLE definition, which separates
1885 array and member references, but it seems reasonable to handle them
1886 together. Also, this way we don't run into problems with union
1887 aliasing; gcc requires that for accesses through a union to alias, the
1888 union reference must be explicit, which was not always the case when we
1889 were splitting up array and member refs.
1891 PRE_P points to the sequence where side effects that must happen before
1892 *EXPR_P should be stored.
1894 POST_P points to the sequence where side effects that must happen after
1895 *EXPR_P should be stored. */
1897 static enum gimplify_status
1898 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1899 fallback_t fallback)
1901 tree *p;
1902 enum gimplify_status ret = GS_ALL_DONE, tret;
1903 int i;
1904 location_t loc = EXPR_LOCATION (*expr_p);
1905 tree expr = *expr_p;
1907 /* Create a stack of the subexpressions so later we can walk them in
1908 order from inner to outer. */
1909 auto_vec<tree, 10> expr_stack;
1911 /* We can handle anything that get_inner_reference can deal with. */
1912 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1914 restart:
1915 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1916 if (TREE_CODE (*p) == INDIRECT_REF)
1917 *p = fold_indirect_ref_loc (loc, *p);
1919 if (handled_component_p (*p))
1921 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1922 additional COMPONENT_REFs. */
1923 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1924 && gimplify_var_or_parm_decl (p) == GS_OK)
1925 goto restart;
1926 else
1927 break;
1929 expr_stack.safe_push (*p);
1932 gcc_assert (expr_stack.length ());
1934 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1935 walked through and P points to the innermost expression.
1937 Java requires that we elaborated nodes in source order. That
1938 means we must gimplify the inner expression followed by each of
1939 the indices, in order. But we can't gimplify the inner
1940 expression until we deal with any variable bounds, sizes, or
1941 positions in order to deal with PLACEHOLDER_EXPRs.
1943 So we do this in three steps. First we deal with the annotations
1944 for any variables in the components, then we gimplify the base,
1945 then we gimplify any indices, from left to right. */
1946 for (i = expr_stack.length () - 1; i >= 0; i--)
1948 tree t = expr_stack[i];
1950 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1952 /* Gimplify the low bound and element type size and put them into
1953 the ARRAY_REF. If these values are set, they have already been
1954 gimplified. */
1955 if (TREE_OPERAND (t, 2) == NULL_TREE)
1957 tree low = unshare_expr (array_ref_low_bound (t));
1958 if (!is_gimple_min_invariant (low))
1960 TREE_OPERAND (t, 2) = low;
1961 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1962 post_p, is_gimple_reg,
1963 fb_rvalue);
1964 ret = MIN (ret, tret);
1967 else
1969 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1970 is_gimple_reg, fb_rvalue);
1971 ret = MIN (ret, tret);
1974 if (TREE_OPERAND (t, 3) == NULL_TREE)
1976 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1977 tree elmt_size = unshare_expr (array_ref_element_size (t));
1978 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1980 /* Divide the element size by the alignment of the element
1981 type (above). */
1982 elmt_size
1983 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1985 if (!is_gimple_min_invariant (elmt_size))
1987 TREE_OPERAND (t, 3) = elmt_size;
1988 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1989 post_p, is_gimple_reg,
1990 fb_rvalue);
1991 ret = MIN (ret, tret);
1994 else
1996 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1997 is_gimple_reg, fb_rvalue);
1998 ret = MIN (ret, tret);
2001 else if (TREE_CODE (t) == COMPONENT_REF)
2003 /* Set the field offset into T and gimplify it. */
2004 if (TREE_OPERAND (t, 2) == NULL_TREE)
2006 tree offset = unshare_expr (component_ref_field_offset (t));
2007 tree field = TREE_OPERAND (t, 1);
2008 tree factor
2009 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2011 /* Divide the offset by its alignment. */
2012 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2014 if (!is_gimple_min_invariant (offset))
2016 TREE_OPERAND (t, 2) = offset;
2017 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2018 post_p, is_gimple_reg,
2019 fb_rvalue);
2020 ret = MIN (ret, tret);
2023 else
2025 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2026 is_gimple_reg, fb_rvalue);
2027 ret = MIN (ret, tret);
2032 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2033 so as to match the min_lval predicate. Failure to do so may result
2034 in the creation of large aggregate temporaries. */
2035 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2036 fallback | fb_lvalue);
2037 ret = MIN (ret, tret);
2039 /* And finally, the indices and operands of ARRAY_REF. During this
2040 loop we also remove any useless conversions. */
2041 for (; expr_stack.length () > 0; )
2043 tree t = expr_stack.pop ();
2045 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2047 /* Gimplify the dimension. */
2048 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2050 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2051 is_gimple_val, fb_rvalue);
2052 ret = MIN (ret, tret);
2056 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2058 /* The innermost expression P may have originally had
2059 TREE_SIDE_EFFECTS set which would have caused all the outer
2060 expressions in *EXPR_P leading to P to also have had
2061 TREE_SIDE_EFFECTS set. */
2062 recalculate_side_effects (t);
2065 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2066 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2068 canonicalize_component_ref (expr_p);
2071 expr_stack.release ();
2073 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2075 return ret;
2078 /* Gimplify the self modifying expression pointed to by EXPR_P
2079 (++, --, +=, -=).
2081 PRE_P points to the list where side effects that must happen before
2082 *EXPR_P should be stored.
2084 POST_P points to the list where side effects that must happen after
2085 *EXPR_P should be stored.
2087 WANT_VALUE is nonzero iff we want to use the value of this expression
2088 in another expression.
2090 ARITH_TYPE is the type the computation should be performed in. */
2092 enum gimplify_status
2093 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2094 bool want_value, tree arith_type)
2096 enum tree_code code;
2097 tree lhs, lvalue, rhs, t1;
2098 gimple_seq post = NULL, *orig_post_p = post_p;
2099 bool postfix;
2100 enum tree_code arith_code;
2101 enum gimplify_status ret;
2102 location_t loc = EXPR_LOCATION (*expr_p);
2104 code = TREE_CODE (*expr_p);
2106 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2107 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2109 /* Prefix or postfix? */
2110 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2111 /* Faster to treat as prefix if result is not used. */
2112 postfix = want_value;
2113 else
2114 postfix = false;
2116 /* For postfix, make sure the inner expression's post side effects
2117 are executed after side effects from this expression. */
2118 if (postfix)
2119 post_p = &post;
2121 /* Add or subtract? */
2122 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2123 arith_code = PLUS_EXPR;
2124 else
2125 arith_code = MINUS_EXPR;
2127 /* Gimplify the LHS into a GIMPLE lvalue. */
2128 lvalue = TREE_OPERAND (*expr_p, 0);
2129 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2130 if (ret == GS_ERROR)
2131 return ret;
2133 /* Extract the operands to the arithmetic operation. */
2134 lhs = lvalue;
2135 rhs = TREE_OPERAND (*expr_p, 1);
2137 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2138 that as the result value and in the postqueue operation. */
2139 if (postfix)
2141 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2142 if (ret == GS_ERROR)
2143 return ret;
2145 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2148 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2149 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2151 rhs = convert_to_ptrofftype_loc (loc, rhs);
2152 if (arith_code == MINUS_EXPR)
2153 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2154 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2156 else
2157 t1 = fold_convert (TREE_TYPE (*expr_p),
2158 fold_build2 (arith_code, arith_type,
2159 fold_convert (arith_type, lhs),
2160 fold_convert (arith_type, rhs)));
2162 if (postfix)
2164 gimplify_assign (lvalue, t1, pre_p);
2165 gimplify_seq_add_seq (orig_post_p, post);
2166 *expr_p = lhs;
2167 return GS_ALL_DONE;
2169 else
2171 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2172 return GS_OK;
2176 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2178 static void
2179 maybe_with_size_expr (tree *expr_p)
2181 tree expr = *expr_p;
2182 tree type = TREE_TYPE (expr);
2183 tree size;
2185 /* If we've already wrapped this or the type is error_mark_node, we can't do
2186 anything. */
2187 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2188 || type == error_mark_node)
2189 return;
2191 /* If the size isn't known or is a constant, we have nothing to do. */
2192 size = TYPE_SIZE_UNIT (type);
2193 if (!size || TREE_CODE (size) == INTEGER_CST)
2194 return;
2196 /* Otherwise, make a WITH_SIZE_EXPR. */
2197 size = unshare_expr (size);
2198 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2199 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2202 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2203 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2204 the CALL_EXPR. */
2206 enum gimplify_status
2207 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2209 bool (*test) (tree);
2210 fallback_t fb;
2212 /* In general, we allow lvalues for function arguments to avoid
2213 extra overhead of copying large aggregates out of even larger
2214 aggregates into temporaries only to copy the temporaries to
2215 the argument list. Make optimizers happy by pulling out to
2216 temporaries those types that fit in registers. */
2217 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2218 test = is_gimple_val, fb = fb_rvalue;
2219 else
2221 test = is_gimple_lvalue, fb = fb_either;
2222 /* Also strip a TARGET_EXPR that would force an extra copy. */
2223 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2225 tree init = TARGET_EXPR_INITIAL (*arg_p);
2226 if (init
2227 && !VOID_TYPE_P (TREE_TYPE (init)))
2228 *arg_p = init;
2232 /* If this is a variable sized type, we must remember the size. */
2233 maybe_with_size_expr (arg_p);
2235 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2236 /* Make sure arguments have the same location as the function call
2237 itself. */
2238 protected_set_expr_location (*arg_p, call_location);
2240 /* There is a sequence point before a function call. Side effects in
2241 the argument list must occur before the actual call. So, when
2242 gimplifying arguments, force gimplify_expr to use an internal
2243 post queue which is then appended to the end of PRE_P. */
2244 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2247 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2248 references that weren't in the source. We'll do it during omplower pass
2249 instead. */
2251 static bool
2252 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2254 struct gimplify_omp_ctx *ctx;
2255 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2256 if (ctx->region_type == ORT_TARGET)
2257 return false;
2258 return fold_stmt (gsi);
2261 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2262 WANT_VALUE is true if the result of the call is desired. */
2264 static enum gimplify_status
2265 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2267 tree fndecl, parms, p, fnptrtype;
2268 enum gimplify_status ret;
2269 int i, nargs;
2270 gcall *call;
2271 bool builtin_va_start_p = false;
2272 location_t loc = EXPR_LOCATION (*expr_p);
2274 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2276 /* For reliable diagnostics during inlining, it is necessary that
2277 every call_expr be annotated with file and line. */
2278 if (! EXPR_HAS_LOCATION (*expr_p))
2279 SET_EXPR_LOCATION (*expr_p, input_location);
2281 /* Gimplify internal functions created in the FEs. */
2282 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2284 if (want_value)
2285 return GS_ALL_DONE;
2287 nargs = call_expr_nargs (*expr_p);
2288 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2289 auto_vec<tree> vargs (nargs);
2291 for (i = 0; i < nargs; i++)
2293 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2294 EXPR_LOCATION (*expr_p));
2295 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2297 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2298 gimplify_seq_add_stmt (pre_p, call);
2299 return GS_ALL_DONE;
2302 /* This may be a call to a builtin function.
2304 Builtin function calls may be transformed into different
2305 (and more efficient) builtin function calls under certain
2306 circumstances. Unfortunately, gimplification can muck things
2307 up enough that the builtin expanders are not aware that certain
2308 transformations are still valid.
2310 So we attempt transformation/gimplification of the call before
2311 we gimplify the CALL_EXPR. At this time we do not manage to
2312 transform all calls in the same manner as the expanders do, but
2313 we do transform most of them. */
2314 fndecl = get_callee_fndecl (*expr_p);
2315 if (fndecl
2316 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2317 switch (DECL_FUNCTION_CODE (fndecl))
2319 case BUILT_IN_VA_START:
2321 builtin_va_start_p = TRUE;
2322 if (call_expr_nargs (*expr_p) < 2)
2324 error ("too few arguments to function %<va_start%>");
2325 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2326 return GS_OK;
2329 if (fold_builtin_next_arg (*expr_p, true))
2331 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2332 return GS_OK;
2334 break;
2336 case BUILT_IN_LINE:
2338 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2339 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2340 return GS_OK;
2342 case BUILT_IN_FILE:
2344 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2345 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2346 return GS_OK;
2348 case BUILT_IN_FUNCTION:
2350 const char *function;
2351 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2352 *expr_p = build_string_literal (strlen (function) + 1, function);
2353 return GS_OK;
2355 default:
2358 if (fndecl && DECL_BUILT_IN (fndecl))
2360 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2361 if (new_tree && new_tree != *expr_p)
2363 /* There was a transformation of this call which computes the
2364 same value, but in a more efficient way. Return and try
2365 again. */
2366 *expr_p = new_tree;
2367 return GS_OK;
2371 /* Remember the original function pointer type. */
2372 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2374 /* There is a sequence point before the call, so any side effects in
2375 the calling expression must occur before the actual call. Force
2376 gimplify_expr to use an internal post queue. */
2377 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2378 is_gimple_call_addr, fb_rvalue);
2380 nargs = call_expr_nargs (*expr_p);
2382 /* Get argument types for verification. */
2383 fndecl = get_callee_fndecl (*expr_p);
2384 parms = NULL_TREE;
2385 if (fndecl)
2386 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2387 else
2388 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2390 if (fndecl && DECL_ARGUMENTS (fndecl))
2391 p = DECL_ARGUMENTS (fndecl);
2392 else if (parms)
2393 p = parms;
2394 else
2395 p = NULL_TREE;
2396 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2399 /* If the last argument is __builtin_va_arg_pack () and it is not
2400 passed as a named argument, decrease the number of CALL_EXPR
2401 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2402 if (!p
2403 && i < nargs
2404 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2406 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2407 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2409 if (last_arg_fndecl
2410 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2411 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2412 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2414 tree call = *expr_p;
2416 --nargs;
2417 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2418 CALL_EXPR_FN (call),
2419 nargs, CALL_EXPR_ARGP (call));
2421 /* Copy all CALL_EXPR flags, location and block, except
2422 CALL_EXPR_VA_ARG_PACK flag. */
2423 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2424 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2425 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2426 = CALL_EXPR_RETURN_SLOT_OPT (call);
2427 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2428 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2430 /* Set CALL_EXPR_VA_ARG_PACK. */
2431 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2435 /* Gimplify the function arguments. */
2436 if (nargs > 0)
2438 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2439 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2440 PUSH_ARGS_REVERSED ? i-- : i++)
2442 enum gimplify_status t;
2444 /* Avoid gimplifying the second argument to va_start, which needs to
2445 be the plain PARM_DECL. */
2446 if ((i != 1) || !builtin_va_start_p)
2448 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2449 EXPR_LOCATION (*expr_p));
2451 if (t == GS_ERROR)
2452 ret = GS_ERROR;
2457 /* Gimplify the static chain. */
2458 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2460 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2461 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2462 else
2464 enum gimplify_status t;
2465 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2466 EXPR_LOCATION (*expr_p));
2467 if (t == GS_ERROR)
2468 ret = GS_ERROR;
2472 /* Verify the function result. */
2473 if (want_value && fndecl
2474 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2476 error_at (loc, "using result of function returning %<void%>");
2477 ret = GS_ERROR;
2480 /* Try this again in case gimplification exposed something. */
2481 if (ret != GS_ERROR)
2483 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2485 if (new_tree && new_tree != *expr_p)
2487 /* There was a transformation of this call which computes the
2488 same value, but in a more efficient way. Return and try
2489 again. */
2490 *expr_p = new_tree;
2491 return GS_OK;
2494 else
2496 *expr_p = error_mark_node;
2497 return GS_ERROR;
2500 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2501 decl. This allows us to eliminate redundant or useless
2502 calls to "const" functions. */
2503 if (TREE_CODE (*expr_p) == CALL_EXPR)
2505 int flags = call_expr_flags (*expr_p);
2506 if (flags & (ECF_CONST | ECF_PURE)
2507 /* An infinite loop is considered a side effect. */
2508 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2509 TREE_SIDE_EFFECTS (*expr_p) = 0;
2512 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2513 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2514 form and delegate the creation of a GIMPLE_CALL to
2515 gimplify_modify_expr. This is always possible because when
2516 WANT_VALUE is true, the caller wants the result of this call into
2517 a temporary, which means that we will emit an INIT_EXPR in
2518 internal_get_tmp_var which will then be handled by
2519 gimplify_modify_expr. */
2520 if (!want_value)
2522 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2523 have to do is replicate it as a GIMPLE_CALL tuple. */
2524 gimple_stmt_iterator gsi;
2525 call = gimple_build_call_from_tree (*expr_p);
2526 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2527 notice_special_calls (call);
2528 gimplify_seq_add_stmt (pre_p, call);
2529 gsi = gsi_last (*pre_p);
2530 maybe_fold_stmt (&gsi);
2531 *expr_p = NULL_TREE;
2533 else
2534 /* Remember the original function type. */
2535 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2536 CALL_EXPR_FN (*expr_p));
2538 return ret;
2541 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2542 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2544 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2545 condition is true or false, respectively. If null, we should generate
2546 our own to skip over the evaluation of this specific expression.
2548 LOCUS is the source location of the COND_EXPR.
2550 This function is the tree equivalent of do_jump.
2552 shortcut_cond_r should only be called by shortcut_cond_expr. */
2554 static tree
2555 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2556 location_t locus)
2558 tree local_label = NULL_TREE;
2559 tree t, expr = NULL;
2561 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2562 retain the shortcut semantics. Just insert the gotos here;
2563 shortcut_cond_expr will append the real blocks later. */
2564 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2566 location_t new_locus;
2568 /* Turn if (a && b) into
2570 if (a); else goto no;
2571 if (b) goto yes; else goto no;
2572 (no:) */
2574 if (false_label_p == NULL)
2575 false_label_p = &local_label;
2577 /* Keep the original source location on the first 'if'. */
2578 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2579 append_to_statement_list (t, &expr);
2581 /* Set the source location of the && on the second 'if'. */
2582 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2583 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2584 new_locus);
2585 append_to_statement_list (t, &expr);
2587 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2589 location_t new_locus;
2591 /* Turn if (a || b) into
2593 if (a) goto yes;
2594 if (b) goto yes; else goto no;
2595 (yes:) */
2597 if (true_label_p == NULL)
2598 true_label_p = &local_label;
2600 /* Keep the original source location on the first 'if'. */
2601 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2602 append_to_statement_list (t, &expr);
2604 /* Set the source location of the || on the second 'if'. */
2605 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2606 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2607 new_locus);
2608 append_to_statement_list (t, &expr);
2610 else if (TREE_CODE (pred) == COND_EXPR
2611 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2612 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2614 location_t new_locus;
2616 /* As long as we're messing with gotos, turn if (a ? b : c) into
2617 if (a)
2618 if (b) goto yes; else goto no;
2619 else
2620 if (c) goto yes; else goto no;
2622 Don't do this if one of the arms has void type, which can happen
2623 in C++ when the arm is throw. */
2625 /* Keep the original source location on the first 'if'. Set the source
2626 location of the ? on the second 'if'. */
2627 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2628 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2629 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2630 false_label_p, locus),
2631 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2632 false_label_p, new_locus));
2634 else
2636 expr = build3 (COND_EXPR, void_type_node, pred,
2637 build_and_jump (true_label_p),
2638 build_and_jump (false_label_p));
2639 SET_EXPR_LOCATION (expr, locus);
2642 if (local_label)
2644 t = build1 (LABEL_EXPR, void_type_node, local_label);
2645 append_to_statement_list (t, &expr);
2648 return expr;
2651 /* Given a conditional expression EXPR with short-circuit boolean
2652 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2653 predicate apart into the equivalent sequence of conditionals. */
2655 static tree
2656 shortcut_cond_expr (tree expr)
2658 tree pred = TREE_OPERAND (expr, 0);
2659 tree then_ = TREE_OPERAND (expr, 1);
2660 tree else_ = TREE_OPERAND (expr, 2);
2661 tree true_label, false_label, end_label, t;
2662 tree *true_label_p;
2663 tree *false_label_p;
2664 bool emit_end, emit_false, jump_over_else;
2665 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2666 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2668 /* First do simple transformations. */
2669 if (!else_se)
2671 /* If there is no 'else', turn
2672 if (a && b) then c
2673 into
2674 if (a) if (b) then c. */
2675 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2677 /* Keep the original source location on the first 'if'. */
2678 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2679 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2680 /* Set the source location of the && on the second 'if'. */
2681 if (EXPR_HAS_LOCATION (pred))
2682 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2683 then_ = shortcut_cond_expr (expr);
2684 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2685 pred = TREE_OPERAND (pred, 0);
2686 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2687 SET_EXPR_LOCATION (expr, locus);
2691 if (!then_se)
2693 /* If there is no 'then', turn
2694 if (a || b); else d
2695 into
2696 if (a); else if (b); else d. */
2697 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2699 /* Keep the original source location on the first 'if'. */
2700 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2701 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2702 /* Set the source location of the || on the second 'if'. */
2703 if (EXPR_HAS_LOCATION (pred))
2704 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2705 else_ = shortcut_cond_expr (expr);
2706 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2707 pred = TREE_OPERAND (pred, 0);
2708 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2709 SET_EXPR_LOCATION (expr, locus);
2713 /* If we're done, great. */
2714 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2715 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2716 return expr;
2718 /* Otherwise we need to mess with gotos. Change
2719 if (a) c; else d;
2721 if (a); else goto no;
2722 c; goto end;
2723 no: d; end:
2724 and recursively gimplify the condition. */
2726 true_label = false_label = end_label = NULL_TREE;
2728 /* If our arms just jump somewhere, hijack those labels so we don't
2729 generate jumps to jumps. */
2731 if (then_
2732 && TREE_CODE (then_) == GOTO_EXPR
2733 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2735 true_label = GOTO_DESTINATION (then_);
2736 then_ = NULL;
2737 then_se = false;
2740 if (else_
2741 && TREE_CODE (else_) == GOTO_EXPR
2742 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2744 false_label = GOTO_DESTINATION (else_);
2745 else_ = NULL;
2746 else_se = false;
2749 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2750 if (true_label)
2751 true_label_p = &true_label;
2752 else
2753 true_label_p = NULL;
2755 /* The 'else' branch also needs a label if it contains interesting code. */
2756 if (false_label || else_se)
2757 false_label_p = &false_label;
2758 else
2759 false_label_p = NULL;
2761 /* If there was nothing else in our arms, just forward the label(s). */
2762 if (!then_se && !else_se)
2763 return shortcut_cond_r (pred, true_label_p, false_label_p,
2764 EXPR_LOC_OR_LOC (expr, input_location));
2766 /* If our last subexpression already has a terminal label, reuse it. */
2767 if (else_se)
2768 t = expr_last (else_);
2769 else if (then_se)
2770 t = expr_last (then_);
2771 else
2772 t = NULL;
2773 if (t && TREE_CODE (t) == LABEL_EXPR)
2774 end_label = LABEL_EXPR_LABEL (t);
2776 /* If we don't care about jumping to the 'else' branch, jump to the end
2777 if the condition is false. */
2778 if (!false_label_p)
2779 false_label_p = &end_label;
2781 /* We only want to emit these labels if we aren't hijacking them. */
2782 emit_end = (end_label == NULL_TREE);
2783 emit_false = (false_label == NULL_TREE);
2785 /* We only emit the jump over the else clause if we have to--if the
2786 then clause may fall through. Otherwise we can wind up with a
2787 useless jump and a useless label at the end of gimplified code,
2788 which will cause us to think that this conditional as a whole
2789 falls through even if it doesn't. If we then inline a function
2790 which ends with such a condition, that can cause us to issue an
2791 inappropriate warning about control reaching the end of a
2792 non-void function. */
2793 jump_over_else = block_may_fallthru (then_);
2795 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2796 EXPR_LOC_OR_LOC (expr, input_location));
2798 expr = NULL;
2799 append_to_statement_list (pred, &expr);
2801 append_to_statement_list (then_, &expr);
2802 if (else_se)
2804 if (jump_over_else)
2806 tree last = expr_last (expr);
2807 t = build_and_jump (&end_label);
2808 if (EXPR_HAS_LOCATION (last))
2809 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2810 append_to_statement_list (t, &expr);
2812 if (emit_false)
2814 t = build1 (LABEL_EXPR, void_type_node, false_label);
2815 append_to_statement_list (t, &expr);
2817 append_to_statement_list (else_, &expr);
2819 if (emit_end && end_label)
2821 t = build1 (LABEL_EXPR, void_type_node, end_label);
2822 append_to_statement_list (t, &expr);
2825 return expr;
2828 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2830 tree
2831 gimple_boolify (tree expr)
2833 tree type = TREE_TYPE (expr);
2834 location_t loc = EXPR_LOCATION (expr);
2836 if (TREE_CODE (expr) == NE_EXPR
2837 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2838 && integer_zerop (TREE_OPERAND (expr, 1)))
2840 tree call = TREE_OPERAND (expr, 0);
2841 tree fn = get_callee_fndecl (call);
2843 /* For __builtin_expect ((long) (x), y) recurse into x as well
2844 if x is truth_value_p. */
2845 if (fn
2846 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2847 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2848 && call_expr_nargs (call) == 2)
2850 tree arg = CALL_EXPR_ARG (call, 0);
2851 if (arg)
2853 if (TREE_CODE (arg) == NOP_EXPR
2854 && TREE_TYPE (arg) == TREE_TYPE (call))
2855 arg = TREE_OPERAND (arg, 0);
2856 if (truth_value_p (TREE_CODE (arg)))
2858 arg = gimple_boolify (arg);
2859 CALL_EXPR_ARG (call, 0)
2860 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2866 switch (TREE_CODE (expr))
2868 case TRUTH_AND_EXPR:
2869 case TRUTH_OR_EXPR:
2870 case TRUTH_XOR_EXPR:
2871 case TRUTH_ANDIF_EXPR:
2872 case TRUTH_ORIF_EXPR:
2873 /* Also boolify the arguments of truth exprs. */
2874 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2875 /* FALLTHRU */
2877 case TRUTH_NOT_EXPR:
2878 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2880 /* These expressions always produce boolean results. */
2881 if (TREE_CODE (type) != BOOLEAN_TYPE)
2882 TREE_TYPE (expr) = boolean_type_node;
2883 return expr;
2885 case ANNOTATE_EXPR:
2886 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2888 case annot_expr_ivdep_kind:
2889 case annot_expr_no_vector_kind:
2890 case annot_expr_vector_kind:
2891 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2892 if (TREE_CODE (type) != BOOLEAN_TYPE)
2893 TREE_TYPE (expr) = boolean_type_node;
2894 return expr;
2895 default:
2896 gcc_unreachable ();
2899 default:
2900 if (COMPARISON_CLASS_P (expr))
2902 /* There expressions always prduce boolean results. */
2903 if (TREE_CODE (type) != BOOLEAN_TYPE)
2904 TREE_TYPE (expr) = boolean_type_node;
2905 return expr;
2907 /* Other expressions that get here must have boolean values, but
2908 might need to be converted to the appropriate mode. */
2909 if (TREE_CODE (type) == BOOLEAN_TYPE)
2910 return expr;
2911 return fold_convert_loc (loc, boolean_type_node, expr);
2915 /* Given a conditional expression *EXPR_P without side effects, gimplify
2916 its operands. New statements are inserted to PRE_P. */
2918 static enum gimplify_status
2919 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2921 tree expr = *expr_p, cond;
2922 enum gimplify_status ret, tret;
2923 enum tree_code code;
2925 cond = gimple_boolify (COND_EXPR_COND (expr));
2927 /* We need to handle && and || specially, as their gimplification
2928 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2929 code = TREE_CODE (cond);
2930 if (code == TRUTH_ANDIF_EXPR)
2931 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2932 else if (code == TRUTH_ORIF_EXPR)
2933 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2934 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2935 COND_EXPR_COND (*expr_p) = cond;
2937 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2938 is_gimple_val, fb_rvalue);
2939 ret = MIN (ret, tret);
2940 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2941 is_gimple_val, fb_rvalue);
2943 return MIN (ret, tret);
2946 /* Return true if evaluating EXPR could trap.
2947 EXPR is GENERIC, while tree_could_trap_p can be called
2948 only on GIMPLE. */
2950 static bool
2951 generic_expr_could_trap_p (tree expr)
2953 unsigned i, n;
2955 if (!expr || is_gimple_val (expr))
2956 return false;
2958 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2959 return true;
2961 n = TREE_OPERAND_LENGTH (expr);
2962 for (i = 0; i < n; i++)
2963 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2964 return true;
2966 return false;
2969 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2970 into
2972 if (p) if (p)
2973 t1 = a; a;
2974 else or else
2975 t1 = b; b;
2978 The second form is used when *EXPR_P is of type void.
2980 PRE_P points to the list where side effects that must happen before
2981 *EXPR_P should be stored. */
2983 static enum gimplify_status
2984 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2986 tree expr = *expr_p;
2987 tree type = TREE_TYPE (expr);
2988 location_t loc = EXPR_LOCATION (expr);
2989 tree tmp, arm1, arm2;
2990 enum gimplify_status ret;
2991 tree label_true, label_false, label_cont;
2992 bool have_then_clause_p, have_else_clause_p;
2993 gcond *cond_stmt;
2994 enum tree_code pred_code;
2995 gimple_seq seq = NULL;
2997 /* If this COND_EXPR has a value, copy the values into a temporary within
2998 the arms. */
2999 if (!VOID_TYPE_P (type))
3001 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3002 tree result;
3004 /* If either an rvalue is ok or we do not require an lvalue, create the
3005 temporary. But we cannot do that if the type is addressable. */
3006 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3007 && !TREE_ADDRESSABLE (type))
3009 if (gimplify_ctxp->allow_rhs_cond_expr
3010 /* If either branch has side effects or could trap, it can't be
3011 evaluated unconditionally. */
3012 && !TREE_SIDE_EFFECTS (then_)
3013 && !generic_expr_could_trap_p (then_)
3014 && !TREE_SIDE_EFFECTS (else_)
3015 && !generic_expr_could_trap_p (else_))
3016 return gimplify_pure_cond_expr (expr_p, pre_p);
3018 tmp = create_tmp_var (type, "iftmp");
3019 result = tmp;
3022 /* Otherwise, only create and copy references to the values. */
3023 else
3025 type = build_pointer_type (type);
3027 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3028 then_ = build_fold_addr_expr_loc (loc, then_);
3030 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3031 else_ = build_fold_addr_expr_loc (loc, else_);
3033 expr
3034 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3036 tmp = create_tmp_var (type, "iftmp");
3037 result = build_simple_mem_ref_loc (loc, tmp);
3040 /* Build the new then clause, `tmp = then_;'. But don't build the
3041 assignment if the value is void; in C++ it can be if it's a throw. */
3042 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3043 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3045 /* Similarly, build the new else clause, `tmp = else_;'. */
3046 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3047 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3049 TREE_TYPE (expr) = void_type_node;
3050 recalculate_side_effects (expr);
3052 /* Move the COND_EXPR to the prequeue. */
3053 gimplify_stmt (&expr, pre_p);
3055 *expr_p = result;
3056 return GS_ALL_DONE;
3059 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3060 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3061 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3062 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3064 /* Make sure the condition has BOOLEAN_TYPE. */
3065 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3067 /* Break apart && and || conditions. */
3068 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3069 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3071 expr = shortcut_cond_expr (expr);
3073 if (expr != *expr_p)
3075 *expr_p = expr;
3077 /* We can't rely on gimplify_expr to re-gimplify the expanded
3078 form properly, as cleanups might cause the target labels to be
3079 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3080 set up a conditional context. */
3081 gimple_push_condition ();
3082 gimplify_stmt (expr_p, &seq);
3083 gimple_pop_condition (pre_p);
3084 gimple_seq_add_seq (pre_p, seq);
3086 return GS_ALL_DONE;
3090 /* Now do the normal gimplification. */
3092 /* Gimplify condition. */
3093 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3094 fb_rvalue);
3095 if (ret == GS_ERROR)
3096 return GS_ERROR;
3097 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3099 gimple_push_condition ();
3101 have_then_clause_p = have_else_clause_p = false;
3102 if (TREE_OPERAND (expr, 1) != NULL
3103 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3104 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3105 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3106 == current_function_decl)
3107 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3108 have different locations, otherwise we end up with incorrect
3109 location information on the branches. */
3110 && (optimize
3111 || !EXPR_HAS_LOCATION (expr)
3112 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3113 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3115 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3116 have_then_clause_p = true;
3118 else
3119 label_true = create_artificial_label (UNKNOWN_LOCATION);
3120 if (TREE_OPERAND (expr, 2) != NULL
3121 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3122 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3123 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3124 == current_function_decl)
3125 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3126 have different locations, otherwise we end up with incorrect
3127 location information on the branches. */
3128 && (optimize
3129 || !EXPR_HAS_LOCATION (expr)
3130 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3131 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3133 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3134 have_else_clause_p = true;
3136 else
3137 label_false = create_artificial_label (UNKNOWN_LOCATION);
3139 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3140 &arm2);
3142 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3143 label_false);
3145 gimplify_seq_add_stmt (&seq, cond_stmt);
3146 label_cont = NULL_TREE;
3147 if (!have_then_clause_p)
3149 /* For if (...) {} else { code; } put label_true after
3150 the else block. */
3151 if (TREE_OPERAND (expr, 1) == NULL_TREE
3152 && !have_else_clause_p
3153 && TREE_OPERAND (expr, 2) != NULL_TREE)
3154 label_cont = label_true;
3155 else
3157 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3158 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3159 /* For if (...) { code; } else {} or
3160 if (...) { code; } else goto label; or
3161 if (...) { code; return; } else { ... }
3162 label_cont isn't needed. */
3163 if (!have_else_clause_p
3164 && TREE_OPERAND (expr, 2) != NULL_TREE
3165 && gimple_seq_may_fallthru (seq))
3167 gimple g;
3168 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3170 g = gimple_build_goto (label_cont);
3172 /* GIMPLE_COND's are very low level; they have embedded
3173 gotos. This particular embedded goto should not be marked
3174 with the location of the original COND_EXPR, as it would
3175 correspond to the COND_EXPR's condition, not the ELSE or the
3176 THEN arms. To avoid marking it with the wrong location, flag
3177 it as "no location". */
3178 gimple_set_do_not_emit_location (g);
3180 gimplify_seq_add_stmt (&seq, g);
3184 if (!have_else_clause_p)
3186 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3187 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3189 if (label_cont)
3190 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3192 gimple_pop_condition (pre_p);
3193 gimple_seq_add_seq (pre_p, seq);
3195 if (ret == GS_ERROR)
3196 ; /* Do nothing. */
3197 else if (have_then_clause_p || have_else_clause_p)
3198 ret = GS_ALL_DONE;
3199 else
3201 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3202 expr = TREE_OPERAND (expr, 0);
3203 gimplify_stmt (&expr, pre_p);
3206 *expr_p = NULL;
3207 return ret;
3210 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3211 to be marked addressable.
3213 We cannot rely on such an expression being directly markable if a temporary
3214 has been created by the gimplification. In this case, we create another
3215 temporary and initialize it with a copy, which will become a store after we
3216 mark it addressable. This can happen if the front-end passed us something
3217 that it could not mark addressable yet, like a Fortran pass-by-reference
3218 parameter (int) floatvar. */
3220 static void
3221 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3223 while (handled_component_p (*expr_p))
3224 expr_p = &TREE_OPERAND (*expr_p, 0);
3225 if (is_gimple_reg (*expr_p))
3227 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3228 DECL_GIMPLE_REG_P (var) = 0;
3229 *expr_p = var;
3233 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3234 a call to __builtin_memcpy. */
3236 static enum gimplify_status
3237 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3238 gimple_seq *seq_p)
3240 tree t, to, to_ptr, from, from_ptr;
3241 gcall *gs;
3242 location_t loc = EXPR_LOCATION (*expr_p);
3244 to = TREE_OPERAND (*expr_p, 0);
3245 from = TREE_OPERAND (*expr_p, 1);
3247 /* Mark the RHS addressable. Beware that it may not be possible to do so
3248 directly if a temporary has been created by the gimplification. */
3249 prepare_gimple_addressable (&from, seq_p);
3251 mark_addressable (from);
3252 from_ptr = build_fold_addr_expr_loc (loc, from);
3253 gimplify_arg (&from_ptr, seq_p, loc);
3255 mark_addressable (to);
3256 to_ptr = build_fold_addr_expr_loc (loc, to);
3257 gimplify_arg (&to_ptr, seq_p, loc);
3259 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3261 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3263 if (want_value)
3265 /* tmp = memcpy() */
3266 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3267 gimple_call_set_lhs (gs, t);
3268 gimplify_seq_add_stmt (seq_p, gs);
3270 *expr_p = build_simple_mem_ref (t);
3271 return GS_ALL_DONE;
3274 gimplify_seq_add_stmt (seq_p, gs);
3275 *expr_p = NULL;
3276 return GS_ALL_DONE;
3279 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3280 a call to __builtin_memset. In this case we know that the RHS is
3281 a CONSTRUCTOR with an empty element list. */
3283 static enum gimplify_status
3284 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3285 gimple_seq *seq_p)
3287 tree t, from, to, to_ptr;
3288 gcall *gs;
3289 location_t loc = EXPR_LOCATION (*expr_p);
3291 /* Assert our assumptions, to abort instead of producing wrong code
3292 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3293 not be immediately exposed. */
3294 from = TREE_OPERAND (*expr_p, 1);
3295 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3296 from = TREE_OPERAND (from, 0);
3298 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3299 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3301 /* Now proceed. */
3302 to = TREE_OPERAND (*expr_p, 0);
3304 to_ptr = build_fold_addr_expr_loc (loc, to);
3305 gimplify_arg (&to_ptr, seq_p, loc);
3306 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3308 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3310 if (want_value)
3312 /* tmp = memset() */
3313 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3314 gimple_call_set_lhs (gs, t);
3315 gimplify_seq_add_stmt (seq_p, gs);
3317 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3318 return GS_ALL_DONE;
3321 gimplify_seq_add_stmt (seq_p, gs);
3322 *expr_p = NULL;
3323 return GS_ALL_DONE;
3326 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3327 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3328 assignment. Return non-null if we detect a potential overlap. */
3330 struct gimplify_init_ctor_preeval_data
3332 /* The base decl of the lhs object. May be NULL, in which case we
3333 have to assume the lhs is indirect. */
3334 tree lhs_base_decl;
3336 /* The alias set of the lhs object. */
3337 alias_set_type lhs_alias_set;
3340 static tree
3341 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3343 struct gimplify_init_ctor_preeval_data *data
3344 = (struct gimplify_init_ctor_preeval_data *) xdata;
3345 tree t = *tp;
3347 /* If we find the base object, obviously we have overlap. */
3348 if (data->lhs_base_decl == t)
3349 return t;
3351 /* If the constructor component is indirect, determine if we have a
3352 potential overlap with the lhs. The only bits of information we
3353 have to go on at this point are addressability and alias sets. */
3354 if ((INDIRECT_REF_P (t)
3355 || TREE_CODE (t) == MEM_REF)
3356 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3357 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3358 return t;
3360 /* If the constructor component is a call, determine if it can hide a
3361 potential overlap with the lhs through an INDIRECT_REF like above.
3362 ??? Ugh - this is completely broken. In fact this whole analysis
3363 doesn't look conservative. */
3364 if (TREE_CODE (t) == CALL_EXPR)
3366 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3368 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3369 if (POINTER_TYPE_P (TREE_VALUE (type))
3370 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3371 && alias_sets_conflict_p (data->lhs_alias_set,
3372 get_alias_set
3373 (TREE_TYPE (TREE_VALUE (type)))))
3374 return t;
3377 if (IS_TYPE_OR_DECL_P (t))
3378 *walk_subtrees = 0;
3379 return NULL;
3382 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3383 force values that overlap with the lhs (as described by *DATA)
3384 into temporaries. */
3386 static void
3387 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3388 struct gimplify_init_ctor_preeval_data *data)
3390 enum gimplify_status one;
3392 /* If the value is constant, then there's nothing to pre-evaluate. */
3393 if (TREE_CONSTANT (*expr_p))
3395 /* Ensure it does not have side effects, it might contain a reference to
3396 the object we're initializing. */
3397 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3398 return;
3401 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3402 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3403 return;
3405 /* Recurse for nested constructors. */
3406 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3408 unsigned HOST_WIDE_INT ix;
3409 constructor_elt *ce;
3410 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3412 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3413 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3415 return;
3418 /* If this is a variable sized type, we must remember the size. */
3419 maybe_with_size_expr (expr_p);
3421 /* Gimplify the constructor element to something appropriate for the rhs
3422 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3423 the gimplifier will consider this a store to memory. Doing this
3424 gimplification now means that we won't have to deal with complicated
3425 language-specific trees, nor trees like SAVE_EXPR that can induce
3426 exponential search behavior. */
3427 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3428 if (one == GS_ERROR)
3430 *expr_p = NULL;
3431 return;
3434 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3435 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3436 always be true for all scalars, since is_gimple_mem_rhs insists on a
3437 temporary variable for them. */
3438 if (DECL_P (*expr_p))
3439 return;
3441 /* If this is of variable size, we have no choice but to assume it doesn't
3442 overlap since we can't make a temporary for it. */
3443 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3444 return;
3446 /* Otherwise, we must search for overlap ... */
3447 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3448 return;
3450 /* ... and if found, force the value into a temporary. */
3451 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3454 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3455 a RANGE_EXPR in a CONSTRUCTOR for an array.
3457 var = lower;
3458 loop_entry:
3459 object[var] = value;
3460 if (var == upper)
3461 goto loop_exit;
3462 var = var + 1;
3463 goto loop_entry;
3464 loop_exit:
3466 We increment var _after_ the loop exit check because we might otherwise
3467 fail if upper == TYPE_MAX_VALUE (type for upper).
3469 Note that we never have to deal with SAVE_EXPRs here, because this has
3470 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3472 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3473 gimple_seq *, bool);
3475 static void
3476 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3477 tree value, tree array_elt_type,
3478 gimple_seq *pre_p, bool cleared)
3480 tree loop_entry_label, loop_exit_label, fall_thru_label;
3481 tree var, var_type, cref, tmp;
3483 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3484 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3485 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3487 /* Create and initialize the index variable. */
3488 var_type = TREE_TYPE (upper);
3489 var = create_tmp_var (var_type, NULL);
3490 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3492 /* Add the loop entry label. */
3493 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3495 /* Build the reference. */
3496 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3497 var, NULL_TREE, NULL_TREE);
3499 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3500 the store. Otherwise just assign value to the reference. */
3502 if (TREE_CODE (value) == CONSTRUCTOR)
3503 /* NB we might have to call ourself recursively through
3504 gimplify_init_ctor_eval if the value is a constructor. */
3505 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3506 pre_p, cleared);
3507 else
3508 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3510 /* We exit the loop when the index var is equal to the upper bound. */
3511 gimplify_seq_add_stmt (pre_p,
3512 gimple_build_cond (EQ_EXPR, var, upper,
3513 loop_exit_label, fall_thru_label));
3515 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3517 /* Otherwise, increment the index var... */
3518 tmp = build2 (PLUS_EXPR, var_type, var,
3519 fold_convert (var_type, integer_one_node));
3520 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3522 /* ...and jump back to the loop entry. */
3523 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3525 /* Add the loop exit label. */
3526 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3529 /* Return true if FDECL is accessing a field that is zero sized. */
3531 static bool
3532 zero_sized_field_decl (const_tree fdecl)
3534 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3535 && integer_zerop (DECL_SIZE (fdecl)))
3536 return true;
3537 return false;
3540 /* Return true if TYPE is zero sized. */
3542 static bool
3543 zero_sized_type (const_tree type)
3545 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3546 && integer_zerop (TYPE_SIZE (type)))
3547 return true;
3548 return false;
3551 /* A subroutine of gimplify_init_constructor. Generate individual
3552 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3553 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3554 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3555 zeroed first. */
3557 static void
3558 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3559 gimple_seq *pre_p, bool cleared)
3561 tree array_elt_type = NULL;
3562 unsigned HOST_WIDE_INT ix;
3563 tree purpose, value;
3565 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3566 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3568 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3570 tree cref;
3572 /* NULL values are created above for gimplification errors. */
3573 if (value == NULL)
3574 continue;
3576 if (cleared && initializer_zerop (value))
3577 continue;
3579 /* ??? Here's to hoping the front end fills in all of the indices,
3580 so we don't have to figure out what's missing ourselves. */
3581 gcc_assert (purpose);
3583 /* Skip zero-sized fields, unless value has side-effects. This can
3584 happen with calls to functions returning a zero-sized type, which
3585 we shouldn't discard. As a number of downstream passes don't
3586 expect sets of zero-sized fields, we rely on the gimplification of
3587 the MODIFY_EXPR we make below to drop the assignment statement. */
3588 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3589 continue;
3591 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3592 whole range. */
3593 if (TREE_CODE (purpose) == RANGE_EXPR)
3595 tree lower = TREE_OPERAND (purpose, 0);
3596 tree upper = TREE_OPERAND (purpose, 1);
3598 /* If the lower bound is equal to upper, just treat it as if
3599 upper was the index. */
3600 if (simple_cst_equal (lower, upper))
3601 purpose = upper;
3602 else
3604 gimplify_init_ctor_eval_range (object, lower, upper, value,
3605 array_elt_type, pre_p, cleared);
3606 continue;
3610 if (array_elt_type)
3612 /* Do not use bitsizetype for ARRAY_REF indices. */
3613 if (TYPE_DOMAIN (TREE_TYPE (object)))
3614 purpose
3615 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3616 purpose);
3617 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3618 purpose, NULL_TREE, NULL_TREE);
3620 else
3622 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3623 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3624 unshare_expr (object), purpose, NULL_TREE);
3627 if (TREE_CODE (value) == CONSTRUCTOR
3628 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3629 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3630 pre_p, cleared);
3631 else
3633 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3634 gimplify_and_add (init, pre_p);
3635 ggc_free (init);
3640 /* Return the appropriate RHS predicate for this LHS. */
3642 gimple_predicate
3643 rhs_predicate_for (tree lhs)
3645 if (is_gimple_reg (lhs))
3646 return is_gimple_reg_rhs_or_call;
3647 else
3648 return is_gimple_mem_rhs_or_call;
3651 /* Gimplify a C99 compound literal expression. This just means adding
3652 the DECL_EXPR before the current statement and using its anonymous
3653 decl instead. */
3655 static enum gimplify_status
3656 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3657 bool (*gimple_test_f) (tree),
3658 fallback_t fallback)
3660 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3661 tree decl = DECL_EXPR_DECL (decl_s);
3662 tree init = DECL_INITIAL (decl);
3663 /* Mark the decl as addressable if the compound literal
3664 expression is addressable now, otherwise it is marked too late
3665 after we gimplify the initialization expression. */
3666 if (TREE_ADDRESSABLE (*expr_p))
3667 TREE_ADDRESSABLE (decl) = 1;
3668 /* Otherwise, if we don't need an lvalue and have a literal directly
3669 substitute it. Check if it matches the gimple predicate, as
3670 otherwise we'd generate a new temporary, and we can as well just
3671 use the decl we already have. */
3672 else if (!TREE_ADDRESSABLE (decl)
3673 && init
3674 && (fallback & fb_lvalue) == 0
3675 && gimple_test_f (init))
3677 *expr_p = init;
3678 return GS_OK;
3681 /* Preliminarily mark non-addressed complex variables as eligible
3682 for promotion to gimple registers. We'll transform their uses
3683 as we find them. */
3684 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3685 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3686 && !TREE_THIS_VOLATILE (decl)
3687 && !needs_to_live_in_memory (decl))
3688 DECL_GIMPLE_REG_P (decl) = 1;
3690 /* If the decl is not addressable, then it is being used in some
3691 expression or on the right hand side of a statement, and it can
3692 be put into a readonly data section. */
3693 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3694 TREE_READONLY (decl) = 1;
3696 /* This decl isn't mentioned in the enclosing block, so add it to the
3697 list of temps. FIXME it seems a bit of a kludge to say that
3698 anonymous artificial vars aren't pushed, but everything else is. */
3699 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3700 gimple_add_tmp_var (decl);
3702 gimplify_and_add (decl_s, pre_p);
3703 *expr_p = decl;
3704 return GS_OK;
3707 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3708 return a new CONSTRUCTOR if something changed. */
3710 static tree
3711 optimize_compound_literals_in_ctor (tree orig_ctor)
3713 tree ctor = orig_ctor;
3714 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3715 unsigned int idx, num = vec_safe_length (elts);
3717 for (idx = 0; idx < num; idx++)
3719 tree value = (*elts)[idx].value;
3720 tree newval = value;
3721 if (TREE_CODE (value) == CONSTRUCTOR)
3722 newval = optimize_compound_literals_in_ctor (value);
3723 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3725 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3726 tree decl = DECL_EXPR_DECL (decl_s);
3727 tree init = DECL_INITIAL (decl);
3729 if (!TREE_ADDRESSABLE (value)
3730 && !TREE_ADDRESSABLE (decl)
3731 && init
3732 && TREE_CODE (init) == CONSTRUCTOR)
3733 newval = optimize_compound_literals_in_ctor (init);
3735 if (newval == value)
3736 continue;
3738 if (ctor == orig_ctor)
3740 ctor = copy_node (orig_ctor);
3741 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3742 elts = CONSTRUCTOR_ELTS (ctor);
3744 (*elts)[idx].value = newval;
3746 return ctor;
3749 /* A subroutine of gimplify_modify_expr. Break out elements of a
3750 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3752 Note that we still need to clear any elements that don't have explicit
3753 initializers, so if not all elements are initialized we keep the
3754 original MODIFY_EXPR, we just remove all of the constructor elements.
3756 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3757 GS_ERROR if we would have to create a temporary when gimplifying
3758 this constructor. Otherwise, return GS_OK.
3760 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3762 static enum gimplify_status
3763 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3764 bool want_value, bool notify_temp_creation)
3766 tree object, ctor, type;
3767 enum gimplify_status ret;
3768 vec<constructor_elt, va_gc> *elts;
3770 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3772 if (!notify_temp_creation)
3774 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3775 is_gimple_lvalue, fb_lvalue);
3776 if (ret == GS_ERROR)
3777 return ret;
3780 object = TREE_OPERAND (*expr_p, 0);
3781 ctor = TREE_OPERAND (*expr_p, 1) =
3782 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3783 type = TREE_TYPE (ctor);
3784 elts = CONSTRUCTOR_ELTS (ctor);
3785 ret = GS_ALL_DONE;
3787 switch (TREE_CODE (type))
3789 case RECORD_TYPE:
3790 case UNION_TYPE:
3791 case QUAL_UNION_TYPE:
3792 case ARRAY_TYPE:
3794 struct gimplify_init_ctor_preeval_data preeval_data;
3795 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3796 bool cleared, complete_p, valid_const_initializer;
3798 /* Aggregate types must lower constructors to initialization of
3799 individual elements. The exception is that a CONSTRUCTOR node
3800 with no elements indicates zero-initialization of the whole. */
3801 if (vec_safe_is_empty (elts))
3803 if (notify_temp_creation)
3804 return GS_OK;
3805 break;
3808 /* Fetch information about the constructor to direct later processing.
3809 We might want to make static versions of it in various cases, and
3810 can only do so if it known to be a valid constant initializer. */
3811 valid_const_initializer
3812 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3813 &num_ctor_elements, &complete_p);
3815 /* If a const aggregate variable is being initialized, then it
3816 should never be a lose to promote the variable to be static. */
3817 if (valid_const_initializer
3818 && num_nonzero_elements > 1
3819 && TREE_READONLY (object)
3820 && TREE_CODE (object) == VAR_DECL
3821 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3823 if (notify_temp_creation)
3824 return GS_ERROR;
3825 DECL_INITIAL (object) = ctor;
3826 TREE_STATIC (object) = 1;
3827 if (!DECL_NAME (object))
3828 DECL_NAME (object) = create_tmp_var_name ("C");
3829 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3831 /* ??? C++ doesn't automatically append a .<number> to the
3832 assembler name, and even when it does, it looks at FE private
3833 data structures to figure out what that number should be,
3834 which are not set for this variable. I suppose this is
3835 important for local statics for inline functions, which aren't
3836 "local" in the object file sense. So in order to get a unique
3837 TU-local symbol, we must invoke the lhd version now. */
3838 lhd_set_decl_assembler_name (object);
3840 *expr_p = NULL_TREE;
3841 break;
3844 /* If there are "lots" of initialized elements, even discounting
3845 those that are not address constants (and thus *must* be
3846 computed at runtime), then partition the constructor into
3847 constant and non-constant parts. Block copy the constant
3848 parts in, then generate code for the non-constant parts. */
3849 /* TODO. There's code in cp/typeck.c to do this. */
3851 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3852 /* store_constructor will ignore the clearing of variable-sized
3853 objects. Initializers for such objects must explicitly set
3854 every field that needs to be set. */
3855 cleared = false;
3856 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3857 /* If the constructor isn't complete, clear the whole object
3858 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3860 ??? This ought not to be needed. For any element not present
3861 in the initializer, we should simply set them to zero. Except
3862 we'd need to *find* the elements that are not present, and that
3863 requires trickery to avoid quadratic compile-time behavior in
3864 large cases or excessive memory use in small cases. */
3865 cleared = true;
3866 else if (num_ctor_elements - num_nonzero_elements
3867 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3868 && num_nonzero_elements < num_ctor_elements / 4)
3869 /* If there are "lots" of zeros, it's more efficient to clear
3870 the memory and then set the nonzero elements. */
3871 cleared = true;
3872 else
3873 cleared = false;
3875 /* If there are "lots" of initialized elements, and all of them
3876 are valid address constants, then the entire initializer can
3877 be dropped to memory, and then memcpy'd out. Don't do this
3878 for sparse arrays, though, as it's more efficient to follow
3879 the standard CONSTRUCTOR behavior of memset followed by
3880 individual element initialization. Also don't do this for small
3881 all-zero initializers (which aren't big enough to merit
3882 clearing), and don't try to make bitwise copies of
3883 TREE_ADDRESSABLE types.
3885 We cannot apply such transformation when compiling chkp static
3886 initializer because creation of initializer image in the memory
3887 will require static initialization of bounds for it. It should
3888 result in another gimplification of similar initializer and we
3889 may fall into infinite loop. */
3890 if (valid_const_initializer
3891 && !(cleared || num_nonzero_elements == 0)
3892 && !TREE_ADDRESSABLE (type)
3893 && (!current_function_decl
3894 || !lookup_attribute ("chkp ctor",
3895 DECL_ATTRIBUTES (current_function_decl))))
3897 HOST_WIDE_INT size = int_size_in_bytes (type);
3898 unsigned int align;
3900 /* ??? We can still get unbounded array types, at least
3901 from the C++ front end. This seems wrong, but attempt
3902 to work around it for now. */
3903 if (size < 0)
3905 size = int_size_in_bytes (TREE_TYPE (object));
3906 if (size >= 0)
3907 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3910 /* Find the maximum alignment we can assume for the object. */
3911 /* ??? Make use of DECL_OFFSET_ALIGN. */
3912 if (DECL_P (object))
3913 align = DECL_ALIGN (object);
3914 else
3915 align = TYPE_ALIGN (type);
3917 /* Do a block move either if the size is so small as to make
3918 each individual move a sub-unit move on average, or if it
3919 is so large as to make individual moves inefficient. */
3920 if (size > 0
3921 && num_nonzero_elements > 1
3922 && (size < num_nonzero_elements
3923 || !can_move_by_pieces (size, align)))
3925 if (notify_temp_creation)
3926 return GS_ERROR;
3928 walk_tree (&ctor, force_labels_r, NULL, NULL);
3929 ctor = tree_output_constant_def (ctor);
3930 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3931 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3932 TREE_OPERAND (*expr_p, 1) = ctor;
3934 /* This is no longer an assignment of a CONSTRUCTOR, but
3935 we still may have processing to do on the LHS. So
3936 pretend we didn't do anything here to let that happen. */
3937 return GS_UNHANDLED;
3941 /* If the target is volatile, we have non-zero elements and more than
3942 one field to assign, initialize the target from a temporary. */
3943 if (TREE_THIS_VOLATILE (object)
3944 && !TREE_ADDRESSABLE (type)
3945 && num_nonzero_elements > 0
3946 && vec_safe_length (elts) > 1)
3948 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3949 TREE_OPERAND (*expr_p, 0) = temp;
3950 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3951 *expr_p,
3952 build2 (MODIFY_EXPR, void_type_node,
3953 object, temp));
3954 return GS_OK;
3957 if (notify_temp_creation)
3958 return GS_OK;
3960 /* If there are nonzero elements and if needed, pre-evaluate to capture
3961 elements overlapping with the lhs into temporaries. We must do this
3962 before clearing to fetch the values before they are zeroed-out. */
3963 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3965 preeval_data.lhs_base_decl = get_base_address (object);
3966 if (!DECL_P (preeval_data.lhs_base_decl))
3967 preeval_data.lhs_base_decl = NULL;
3968 preeval_data.lhs_alias_set = get_alias_set (object);
3970 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3971 pre_p, post_p, &preeval_data);
3974 if (cleared)
3976 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3977 Note that we still have to gimplify, in order to handle the
3978 case of variable sized types. Avoid shared tree structures. */
3979 CONSTRUCTOR_ELTS (ctor) = NULL;
3980 TREE_SIDE_EFFECTS (ctor) = 0;
3981 object = unshare_expr (object);
3982 gimplify_stmt (expr_p, pre_p);
3985 /* If we have not block cleared the object, or if there are nonzero
3986 elements in the constructor, add assignments to the individual
3987 scalar fields of the object. */
3988 if (!cleared || num_nonzero_elements > 0)
3989 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3991 *expr_p = NULL_TREE;
3993 break;
3995 case COMPLEX_TYPE:
3997 tree r, i;
3999 if (notify_temp_creation)
4000 return GS_OK;
4002 /* Extract the real and imaginary parts out of the ctor. */
4003 gcc_assert (elts->length () == 2);
4004 r = (*elts)[0].value;
4005 i = (*elts)[1].value;
4006 if (r == NULL || i == NULL)
4008 tree zero = build_zero_cst (TREE_TYPE (type));
4009 if (r == NULL)
4010 r = zero;
4011 if (i == NULL)
4012 i = zero;
4015 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4016 represent creation of a complex value. */
4017 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4019 ctor = build_complex (type, r, i);
4020 TREE_OPERAND (*expr_p, 1) = ctor;
4022 else
4024 ctor = build2 (COMPLEX_EXPR, type, r, i);
4025 TREE_OPERAND (*expr_p, 1) = ctor;
4026 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4027 pre_p,
4028 post_p,
4029 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4030 fb_rvalue);
4033 break;
4035 case VECTOR_TYPE:
4037 unsigned HOST_WIDE_INT ix;
4038 constructor_elt *ce;
4040 if (notify_temp_creation)
4041 return GS_OK;
4043 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4044 if (TREE_CONSTANT (ctor))
4046 bool constant_p = true;
4047 tree value;
4049 /* Even when ctor is constant, it might contain non-*_CST
4050 elements, such as addresses or trapping values like
4051 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4052 in VECTOR_CST nodes. */
4053 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4054 if (!CONSTANT_CLASS_P (value))
4056 constant_p = false;
4057 break;
4060 if (constant_p)
4062 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4063 break;
4066 TREE_CONSTANT (ctor) = 0;
4069 /* Vector types use CONSTRUCTOR all the way through gimple
4070 compilation as a general initializer. */
4071 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4073 enum gimplify_status tret;
4074 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4075 fb_rvalue);
4076 if (tret == GS_ERROR)
4077 ret = GS_ERROR;
4079 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4080 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4082 break;
4084 default:
4085 /* So how did we get a CONSTRUCTOR for a scalar type? */
4086 gcc_unreachable ();
4089 if (ret == GS_ERROR)
4090 return GS_ERROR;
4091 else if (want_value)
4093 *expr_p = object;
4094 return GS_OK;
4096 else
4098 /* If we have gimplified both sides of the initializer but have
4099 not emitted an assignment, do so now. */
4100 if (*expr_p)
4102 tree lhs = TREE_OPERAND (*expr_p, 0);
4103 tree rhs = TREE_OPERAND (*expr_p, 1);
4104 gassign *init = gimple_build_assign (lhs, rhs);
4105 gimplify_seq_add_stmt (pre_p, init);
4106 *expr_p = NULL;
4109 return GS_ALL_DONE;
4113 /* Given a pointer value OP0, return a simplified version of an
4114 indirection through OP0, or NULL_TREE if no simplification is
4115 possible. This may only be applied to a rhs of an expression.
4116 Note that the resulting type may be different from the type pointed
4117 to in the sense that it is still compatible from the langhooks
4118 point of view. */
4120 static tree
4121 gimple_fold_indirect_ref_rhs (tree t)
4123 return gimple_fold_indirect_ref (t);
4126 /* Subroutine of gimplify_modify_expr to do simplifications of
4127 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4128 something changes. */
4130 static enum gimplify_status
4131 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4132 gimple_seq *pre_p, gimple_seq *post_p,
4133 bool want_value)
4135 enum gimplify_status ret = GS_UNHANDLED;
4136 bool changed;
4140 changed = false;
4141 switch (TREE_CODE (*from_p))
4143 case VAR_DECL:
4144 /* If we're assigning from a read-only variable initialized with
4145 a constructor, do the direct assignment from the constructor,
4146 but only if neither source nor target are volatile since this
4147 latter assignment might end up being done on a per-field basis. */
4148 if (DECL_INITIAL (*from_p)
4149 && TREE_READONLY (*from_p)
4150 && !TREE_THIS_VOLATILE (*from_p)
4151 && !TREE_THIS_VOLATILE (*to_p)
4152 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4154 tree old_from = *from_p;
4155 enum gimplify_status subret;
4157 /* Move the constructor into the RHS. */
4158 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4160 /* Let's see if gimplify_init_constructor will need to put
4161 it in memory. */
4162 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4163 false, true);
4164 if (subret == GS_ERROR)
4166 /* If so, revert the change. */
4167 *from_p = old_from;
4169 else
4171 ret = GS_OK;
4172 changed = true;
4175 break;
4176 case INDIRECT_REF:
4178 /* If we have code like
4180 *(const A*)(A*)&x
4182 where the type of "x" is a (possibly cv-qualified variant
4183 of "A"), treat the entire expression as identical to "x".
4184 This kind of code arises in C++ when an object is bound
4185 to a const reference, and if "x" is a TARGET_EXPR we want
4186 to take advantage of the optimization below. */
4187 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4188 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4189 if (t)
4191 if (TREE_THIS_VOLATILE (t) != volatile_p)
4193 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4194 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4195 build_fold_addr_expr (t));
4196 if (REFERENCE_CLASS_P (t))
4197 TREE_THIS_VOLATILE (t) = volatile_p;
4199 *from_p = t;
4200 ret = GS_OK;
4201 changed = true;
4203 break;
4206 case TARGET_EXPR:
4208 /* If we are initializing something from a TARGET_EXPR, strip the
4209 TARGET_EXPR and initialize it directly, if possible. This can't
4210 be done if the initializer is void, since that implies that the
4211 temporary is set in some non-trivial way.
4213 ??? What about code that pulls out the temp and uses it
4214 elsewhere? I think that such code never uses the TARGET_EXPR as
4215 an initializer. If I'm wrong, we'll die because the temp won't
4216 have any RTL. In that case, I guess we'll need to replace
4217 references somehow. */
4218 tree init = TARGET_EXPR_INITIAL (*from_p);
4220 if (init
4221 && !VOID_TYPE_P (TREE_TYPE (init)))
4223 *from_p = init;
4224 ret = GS_OK;
4225 changed = true;
4228 break;
4230 case COMPOUND_EXPR:
4231 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4232 caught. */
4233 gimplify_compound_expr (from_p, pre_p, true);
4234 ret = GS_OK;
4235 changed = true;
4236 break;
4238 case CONSTRUCTOR:
4239 /* If we already made some changes, let the front end have a
4240 crack at this before we break it down. */
4241 if (ret != GS_UNHANDLED)
4242 break;
4243 /* If we're initializing from a CONSTRUCTOR, break this into
4244 individual MODIFY_EXPRs. */
4245 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4246 false);
4248 case COND_EXPR:
4249 /* If we're assigning to a non-register type, push the assignment
4250 down into the branches. This is mandatory for ADDRESSABLE types,
4251 since we cannot generate temporaries for such, but it saves a
4252 copy in other cases as well. */
4253 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4255 /* This code should mirror the code in gimplify_cond_expr. */
4256 enum tree_code code = TREE_CODE (*expr_p);
4257 tree cond = *from_p;
4258 tree result = *to_p;
4260 ret = gimplify_expr (&result, pre_p, post_p,
4261 is_gimple_lvalue, fb_lvalue);
4262 if (ret != GS_ERROR)
4263 ret = GS_OK;
4265 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4266 TREE_OPERAND (cond, 1)
4267 = build2 (code, void_type_node, result,
4268 TREE_OPERAND (cond, 1));
4269 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4270 TREE_OPERAND (cond, 2)
4271 = build2 (code, void_type_node, unshare_expr (result),
4272 TREE_OPERAND (cond, 2));
4274 TREE_TYPE (cond) = void_type_node;
4275 recalculate_side_effects (cond);
4277 if (want_value)
4279 gimplify_and_add (cond, pre_p);
4280 *expr_p = unshare_expr (result);
4282 else
4283 *expr_p = cond;
4284 return ret;
4286 break;
4288 case CALL_EXPR:
4289 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4290 return slot so that we don't generate a temporary. */
4291 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4292 && aggregate_value_p (*from_p, *from_p))
4294 bool use_target;
4296 if (!(rhs_predicate_for (*to_p))(*from_p))
4297 /* If we need a temporary, *to_p isn't accurate. */
4298 use_target = false;
4299 /* It's OK to use the return slot directly unless it's an NRV. */
4300 else if (TREE_CODE (*to_p) == RESULT_DECL
4301 && DECL_NAME (*to_p) == NULL_TREE
4302 && needs_to_live_in_memory (*to_p))
4303 use_target = true;
4304 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4305 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4306 /* Don't force regs into memory. */
4307 use_target = false;
4308 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4309 /* It's OK to use the target directly if it's being
4310 initialized. */
4311 use_target = true;
4312 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4313 /* Always use the target and thus RSO for variable-sized types.
4314 GIMPLE cannot deal with a variable-sized assignment
4315 embedded in a call statement. */
4316 use_target = true;
4317 else if (TREE_CODE (*to_p) != SSA_NAME
4318 && (!is_gimple_variable (*to_p)
4319 || needs_to_live_in_memory (*to_p)))
4320 /* Don't use the original target if it's already addressable;
4321 if its address escapes, and the called function uses the
4322 NRV optimization, a conforming program could see *to_p
4323 change before the called function returns; see c++/19317.
4324 When optimizing, the return_slot pass marks more functions
4325 as safe after we have escape info. */
4326 use_target = false;
4327 else
4328 use_target = true;
4330 if (use_target)
4332 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4333 mark_addressable (*to_p);
4336 break;
4338 case WITH_SIZE_EXPR:
4339 /* Likewise for calls that return an aggregate of non-constant size,
4340 since we would not be able to generate a temporary at all. */
4341 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4343 *from_p = TREE_OPERAND (*from_p, 0);
4344 /* We don't change ret in this case because the
4345 WITH_SIZE_EXPR might have been added in
4346 gimplify_modify_expr, so returning GS_OK would lead to an
4347 infinite loop. */
4348 changed = true;
4350 break;
4352 /* If we're initializing from a container, push the initialization
4353 inside it. */
4354 case CLEANUP_POINT_EXPR:
4355 case BIND_EXPR:
4356 case STATEMENT_LIST:
4358 tree wrap = *from_p;
4359 tree t;
4361 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4362 fb_lvalue);
4363 if (ret != GS_ERROR)
4364 ret = GS_OK;
4366 t = voidify_wrapper_expr (wrap, *expr_p);
4367 gcc_assert (t == *expr_p);
4369 if (want_value)
4371 gimplify_and_add (wrap, pre_p);
4372 *expr_p = unshare_expr (*to_p);
4374 else
4375 *expr_p = wrap;
4376 return GS_OK;
4379 case COMPOUND_LITERAL_EXPR:
4381 tree complit = TREE_OPERAND (*expr_p, 1);
4382 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4383 tree decl = DECL_EXPR_DECL (decl_s);
4384 tree init = DECL_INITIAL (decl);
4386 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4387 into struct T x = { 0, 1, 2 } if the address of the
4388 compound literal has never been taken. */
4389 if (!TREE_ADDRESSABLE (complit)
4390 && !TREE_ADDRESSABLE (decl)
4391 && init)
4393 *expr_p = copy_node (*expr_p);
4394 TREE_OPERAND (*expr_p, 1) = init;
4395 return GS_OK;
4399 default:
4400 break;
4403 while (changed);
4405 return ret;
4409 /* Return true if T looks like a valid GIMPLE statement. */
4411 static bool
4412 is_gimple_stmt (tree t)
4414 const enum tree_code code = TREE_CODE (t);
4416 switch (code)
4418 case NOP_EXPR:
4419 /* The only valid NOP_EXPR is the empty statement. */
4420 return IS_EMPTY_STMT (t);
4422 case BIND_EXPR:
4423 case COND_EXPR:
4424 /* These are only valid if they're void. */
4425 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4427 case SWITCH_EXPR:
4428 case GOTO_EXPR:
4429 case RETURN_EXPR:
4430 case LABEL_EXPR:
4431 case CASE_LABEL_EXPR:
4432 case TRY_CATCH_EXPR:
4433 case TRY_FINALLY_EXPR:
4434 case EH_FILTER_EXPR:
4435 case CATCH_EXPR:
4436 case ASM_EXPR:
4437 case STATEMENT_LIST:
4438 case OMP_PARALLEL:
4439 case OMP_FOR:
4440 case OMP_SIMD:
4441 case CILK_SIMD:
4442 case OMP_DISTRIBUTE:
4443 case OMP_SECTIONS:
4444 case OMP_SECTION:
4445 case OMP_SINGLE:
4446 case OMP_MASTER:
4447 case OMP_TASKGROUP:
4448 case OMP_ORDERED:
4449 case OMP_CRITICAL:
4450 case OMP_TASK:
4451 /* These are always void. */
4452 return true;
4454 case CALL_EXPR:
4455 case MODIFY_EXPR:
4456 case PREDICT_EXPR:
4457 /* These are valid regardless of their type. */
4458 return true;
4460 default:
4461 return false;
4466 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4467 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4468 DECL_GIMPLE_REG_P set.
4470 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4471 other, unmodified part of the complex object just before the total store.
4472 As a consequence, if the object is still uninitialized, an undefined value
4473 will be loaded into a register, which may result in a spurious exception
4474 if the register is floating-point and the value happens to be a signaling
4475 NaN for example. Then the fully-fledged complex operations lowering pass
4476 followed by a DCE pass are necessary in order to fix things up. */
4478 static enum gimplify_status
4479 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4480 bool want_value)
4482 enum tree_code code, ocode;
4483 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4485 lhs = TREE_OPERAND (*expr_p, 0);
4486 rhs = TREE_OPERAND (*expr_p, 1);
4487 code = TREE_CODE (lhs);
4488 lhs = TREE_OPERAND (lhs, 0);
4490 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4491 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4492 TREE_NO_WARNING (other) = 1;
4493 other = get_formal_tmp_var (other, pre_p);
4495 realpart = code == REALPART_EXPR ? rhs : other;
4496 imagpart = code == REALPART_EXPR ? other : rhs;
4498 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4499 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4500 else
4501 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4503 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4504 *expr_p = (want_value) ? rhs : NULL_TREE;
4506 return GS_ALL_DONE;
4509 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4511 modify_expr
4512 : varname '=' rhs
4513 | '*' ID '=' rhs
4515 PRE_P points to the list where side effects that must happen before
4516 *EXPR_P should be stored.
4518 POST_P points to the list where side effects that must happen after
4519 *EXPR_P should be stored.
4521 WANT_VALUE is nonzero iff we want to use the value of this expression
4522 in another expression. */
4524 static enum gimplify_status
4525 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4526 bool want_value)
4528 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4529 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4530 enum gimplify_status ret = GS_UNHANDLED;
4531 gimple assign;
4532 location_t loc = EXPR_LOCATION (*expr_p);
4533 gimple_stmt_iterator gsi;
4535 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4536 || TREE_CODE (*expr_p) == INIT_EXPR);
4538 /* Trying to simplify a clobber using normal logic doesn't work,
4539 so handle it here. */
4540 if (TREE_CLOBBER_P (*from_p))
4542 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4543 if (ret == GS_ERROR)
4544 return ret;
4545 gcc_assert (!want_value
4546 && (TREE_CODE (*to_p) == VAR_DECL
4547 || TREE_CODE (*to_p) == MEM_REF));
4548 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4549 *expr_p = NULL;
4550 return GS_ALL_DONE;
4553 /* Insert pointer conversions required by the middle-end that are not
4554 required by the frontend. This fixes middle-end type checking for
4555 for example gcc.dg/redecl-6.c. */
4556 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4558 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4559 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4560 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4563 /* See if any simplifications can be done based on what the RHS is. */
4564 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4565 want_value);
4566 if (ret != GS_UNHANDLED)
4567 return ret;
4569 /* For zero sized types only gimplify the left hand side and right hand
4570 side as statements and throw away the assignment. Do this after
4571 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4572 types properly. */
4573 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4575 gimplify_stmt (from_p, pre_p);
4576 gimplify_stmt (to_p, pre_p);
4577 *expr_p = NULL_TREE;
4578 return GS_ALL_DONE;
4581 /* If the value being copied is of variable width, compute the length
4582 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4583 before gimplifying any of the operands so that we can resolve any
4584 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4585 the size of the expression to be copied, not of the destination, so
4586 that is what we must do here. */
4587 maybe_with_size_expr (from_p);
4589 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4590 if (ret == GS_ERROR)
4591 return ret;
4593 /* As a special case, we have to temporarily allow for assignments
4594 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4595 a toplevel statement, when gimplifying the GENERIC expression
4596 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4597 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4599 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4600 prevent gimplify_expr from trying to create a new temporary for
4601 foo's LHS, we tell it that it should only gimplify until it
4602 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4603 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4604 and all we need to do here is set 'a' to be its LHS. */
4605 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4606 fb_rvalue);
4607 if (ret == GS_ERROR)
4608 return ret;
4610 /* Now see if the above changed *from_p to something we handle specially. */
4611 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4612 want_value);
4613 if (ret != GS_UNHANDLED)
4614 return ret;
4616 /* If we've got a variable sized assignment between two lvalues (i.e. does
4617 not involve a call), then we can make things a bit more straightforward
4618 by converting the assignment to memcpy or memset. */
4619 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4621 tree from = TREE_OPERAND (*from_p, 0);
4622 tree size = TREE_OPERAND (*from_p, 1);
4624 if (TREE_CODE (from) == CONSTRUCTOR)
4625 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4627 if (is_gimple_addressable (from))
4629 *from_p = from;
4630 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4631 pre_p);
4635 /* Transform partial stores to non-addressable complex variables into
4636 total stores. This allows us to use real instead of virtual operands
4637 for these variables, which improves optimization. */
4638 if ((TREE_CODE (*to_p) == REALPART_EXPR
4639 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4640 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4641 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4643 /* Try to alleviate the effects of the gimplification creating artificial
4644 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4645 if (!gimplify_ctxp->into_ssa
4646 && TREE_CODE (*from_p) == VAR_DECL
4647 && DECL_IGNORED_P (*from_p)
4648 && DECL_P (*to_p)
4649 && !DECL_IGNORED_P (*to_p))
4651 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4652 DECL_NAME (*from_p)
4653 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4654 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4655 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4658 if (want_value && TREE_THIS_VOLATILE (*to_p))
4659 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4661 if (TREE_CODE (*from_p) == CALL_EXPR)
4663 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4664 instead of a GIMPLE_ASSIGN. */
4665 gcall *call_stmt;
4666 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4668 /* Gimplify internal functions created in the FEs. */
4669 int nargs = call_expr_nargs (*from_p), i;
4670 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4671 auto_vec<tree> vargs (nargs);
4673 for (i = 0; i < nargs; i++)
4675 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4676 EXPR_LOCATION (*from_p));
4677 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4679 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4680 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4682 else
4684 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4685 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4686 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4687 tree fndecl = get_callee_fndecl (*from_p);
4688 if (fndecl
4689 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4690 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4691 && call_expr_nargs (*from_p) == 3)
4692 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4693 CALL_EXPR_ARG (*from_p, 0),
4694 CALL_EXPR_ARG (*from_p, 1),
4695 CALL_EXPR_ARG (*from_p, 2));
4696 else
4698 call_stmt = gimple_build_call_from_tree (*from_p);
4699 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4702 notice_special_calls (call_stmt);
4703 if (!gimple_call_noreturn_p (call_stmt))
4704 gimple_call_set_lhs (call_stmt, *to_p);
4705 assign = call_stmt;
4707 else
4709 assign = gimple_build_assign (*to_p, *from_p);
4710 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4713 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4715 /* We should have got an SSA name from the start. */
4716 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4719 gimplify_seq_add_stmt (pre_p, assign);
4720 gsi = gsi_last (*pre_p);
4721 maybe_fold_stmt (&gsi);
4723 if (want_value)
4725 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4726 return GS_OK;
4728 else
4729 *expr_p = NULL;
4731 return GS_ALL_DONE;
4734 /* Gimplify a comparison between two variable-sized objects. Do this
4735 with a call to BUILT_IN_MEMCMP. */
4737 static enum gimplify_status
4738 gimplify_variable_sized_compare (tree *expr_p)
4740 location_t loc = EXPR_LOCATION (*expr_p);
4741 tree op0 = TREE_OPERAND (*expr_p, 0);
4742 tree op1 = TREE_OPERAND (*expr_p, 1);
4743 tree t, arg, dest, src, expr;
4745 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4746 arg = unshare_expr (arg);
4747 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4748 src = build_fold_addr_expr_loc (loc, op1);
4749 dest = build_fold_addr_expr_loc (loc, op0);
4750 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4751 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4753 expr
4754 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4755 SET_EXPR_LOCATION (expr, loc);
4756 *expr_p = expr;
4758 return GS_OK;
4761 /* Gimplify a comparison between two aggregate objects of integral scalar
4762 mode as a comparison between the bitwise equivalent scalar values. */
4764 static enum gimplify_status
4765 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4767 location_t loc = EXPR_LOCATION (*expr_p);
4768 tree op0 = TREE_OPERAND (*expr_p, 0);
4769 tree op1 = TREE_OPERAND (*expr_p, 1);
4771 tree type = TREE_TYPE (op0);
4772 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4774 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4775 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4777 *expr_p
4778 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4780 return GS_OK;
4783 /* Gimplify an expression sequence. This function gimplifies each
4784 expression and rewrites the original expression with the last
4785 expression of the sequence in GIMPLE form.
4787 PRE_P points to the list where the side effects for all the
4788 expressions in the sequence will be emitted.
4790 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4792 static enum gimplify_status
4793 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4795 tree t = *expr_p;
4799 tree *sub_p = &TREE_OPERAND (t, 0);
4801 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4802 gimplify_compound_expr (sub_p, pre_p, false);
4803 else
4804 gimplify_stmt (sub_p, pre_p);
4806 t = TREE_OPERAND (t, 1);
4808 while (TREE_CODE (t) == COMPOUND_EXPR);
4810 *expr_p = t;
4811 if (want_value)
4812 return GS_OK;
4813 else
4815 gimplify_stmt (expr_p, pre_p);
4816 return GS_ALL_DONE;
4820 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4821 gimplify. After gimplification, EXPR_P will point to a new temporary
4822 that holds the original value of the SAVE_EXPR node.
4824 PRE_P points to the list where side effects that must happen before
4825 *EXPR_P should be stored. */
4827 static enum gimplify_status
4828 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4830 enum gimplify_status ret = GS_ALL_DONE;
4831 tree val;
4833 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4834 val = TREE_OPERAND (*expr_p, 0);
4836 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4837 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4839 /* The operand may be a void-valued expression such as SAVE_EXPRs
4840 generated by the Java frontend for class initialization. It is
4841 being executed only for its side-effects. */
4842 if (TREE_TYPE (val) == void_type_node)
4844 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4845 is_gimple_stmt, fb_none);
4846 val = NULL;
4848 else
4849 val = get_initialized_tmp_var (val, pre_p, post_p);
4851 TREE_OPERAND (*expr_p, 0) = val;
4852 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4855 *expr_p = val;
4857 return ret;
4860 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4862 unary_expr
4863 : ...
4864 | '&' varname
4867 PRE_P points to the list where side effects that must happen before
4868 *EXPR_P should be stored.
4870 POST_P points to the list where side effects that must happen after
4871 *EXPR_P should be stored. */
4873 static enum gimplify_status
4874 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4876 tree expr = *expr_p;
4877 tree op0 = TREE_OPERAND (expr, 0);
4878 enum gimplify_status ret;
4879 location_t loc = EXPR_LOCATION (*expr_p);
4881 switch (TREE_CODE (op0))
4883 case INDIRECT_REF:
4884 do_indirect_ref:
4885 /* Check if we are dealing with an expression of the form '&*ptr'.
4886 While the front end folds away '&*ptr' into 'ptr', these
4887 expressions may be generated internally by the compiler (e.g.,
4888 builtins like __builtin_va_end). */
4889 /* Caution: the silent array decomposition semantics we allow for
4890 ADDR_EXPR means we can't always discard the pair. */
4891 /* Gimplification of the ADDR_EXPR operand may drop
4892 cv-qualification conversions, so make sure we add them if
4893 needed. */
4895 tree op00 = TREE_OPERAND (op0, 0);
4896 tree t_expr = TREE_TYPE (expr);
4897 tree t_op00 = TREE_TYPE (op00);
4899 if (!useless_type_conversion_p (t_expr, t_op00))
4900 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4901 *expr_p = op00;
4902 ret = GS_OK;
4904 break;
4906 case VIEW_CONVERT_EXPR:
4907 /* Take the address of our operand and then convert it to the type of
4908 this ADDR_EXPR.
4910 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4911 all clear. The impact of this transformation is even less clear. */
4913 /* If the operand is a useless conversion, look through it. Doing so
4914 guarantees that the ADDR_EXPR and its operand will remain of the
4915 same type. */
4916 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4917 op0 = TREE_OPERAND (op0, 0);
4919 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4920 build_fold_addr_expr_loc (loc,
4921 TREE_OPERAND (op0, 0)));
4922 ret = GS_OK;
4923 break;
4925 default:
4926 /* We use fb_either here because the C frontend sometimes takes
4927 the address of a call that returns a struct; see
4928 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4929 the implied temporary explicit. */
4931 /* Make the operand addressable. */
4932 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4933 is_gimple_addressable, fb_either);
4934 if (ret == GS_ERROR)
4935 break;
4937 /* Then mark it. Beware that it may not be possible to do so directly
4938 if a temporary has been created by the gimplification. */
4939 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4941 op0 = TREE_OPERAND (expr, 0);
4943 /* For various reasons, the gimplification of the expression
4944 may have made a new INDIRECT_REF. */
4945 if (TREE_CODE (op0) == INDIRECT_REF)
4946 goto do_indirect_ref;
4948 mark_addressable (TREE_OPERAND (expr, 0));
4950 /* The FEs may end up building ADDR_EXPRs early on a decl with
4951 an incomplete type. Re-build ADDR_EXPRs in canonical form
4952 here. */
4953 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4954 *expr_p = build_fold_addr_expr (op0);
4956 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4957 recompute_tree_invariant_for_addr_expr (*expr_p);
4959 /* If we re-built the ADDR_EXPR add a conversion to the original type
4960 if required. */
4961 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4962 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4964 break;
4967 return ret;
4970 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4971 value; output operands should be a gimple lvalue. */
4973 static enum gimplify_status
4974 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4976 tree expr;
4977 int noutputs;
4978 const char **oconstraints;
4979 int i;
4980 tree link;
4981 const char *constraint;
4982 bool allows_mem, allows_reg, is_inout;
4983 enum gimplify_status ret, tret;
4984 gasm *stmt;
4985 vec<tree, va_gc> *inputs;
4986 vec<tree, va_gc> *outputs;
4987 vec<tree, va_gc> *clobbers;
4988 vec<tree, va_gc> *labels;
4989 tree link_next;
4991 expr = *expr_p;
4992 noutputs = list_length (ASM_OUTPUTS (expr));
4993 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4995 inputs = NULL;
4996 outputs = NULL;
4997 clobbers = NULL;
4998 labels = NULL;
5000 ret = GS_ALL_DONE;
5001 link_next = NULL_TREE;
5002 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5004 bool ok;
5005 size_t constraint_len;
5007 link_next = TREE_CHAIN (link);
5009 oconstraints[i]
5010 = constraint
5011 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5012 constraint_len = strlen (constraint);
5013 if (constraint_len == 0)
5014 continue;
5016 ok = parse_output_constraint (&constraint, i, 0, 0,
5017 &allows_mem, &allows_reg, &is_inout);
5018 if (!ok)
5020 ret = GS_ERROR;
5021 is_inout = false;
5024 if (!allows_reg && allows_mem)
5025 mark_addressable (TREE_VALUE (link));
5027 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5028 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5029 fb_lvalue | fb_mayfail);
5030 if (tret == GS_ERROR)
5032 error ("invalid lvalue in asm output %d", i);
5033 ret = tret;
5036 vec_safe_push (outputs, link);
5037 TREE_CHAIN (link) = NULL_TREE;
5039 if (is_inout)
5041 /* An input/output operand. To give the optimizers more
5042 flexibility, split it into separate input and output
5043 operands. */
5044 tree input;
5045 char buf[10];
5047 /* Turn the in/out constraint into an output constraint. */
5048 char *p = xstrdup (constraint);
5049 p[0] = '=';
5050 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5052 /* And add a matching input constraint. */
5053 if (allows_reg)
5055 sprintf (buf, "%d", i);
5057 /* If there are multiple alternatives in the constraint,
5058 handle each of them individually. Those that allow register
5059 will be replaced with operand number, the others will stay
5060 unchanged. */
5061 if (strchr (p, ',') != NULL)
5063 size_t len = 0, buflen = strlen (buf);
5064 char *beg, *end, *str, *dst;
5066 for (beg = p + 1;;)
5068 end = strchr (beg, ',');
5069 if (end == NULL)
5070 end = strchr (beg, '\0');
5071 if ((size_t) (end - beg) < buflen)
5072 len += buflen + 1;
5073 else
5074 len += end - beg + 1;
5075 if (*end)
5076 beg = end + 1;
5077 else
5078 break;
5081 str = (char *) alloca (len);
5082 for (beg = p + 1, dst = str;;)
5084 const char *tem;
5085 bool mem_p, reg_p, inout_p;
5087 end = strchr (beg, ',');
5088 if (end)
5089 *end = '\0';
5090 beg[-1] = '=';
5091 tem = beg - 1;
5092 parse_output_constraint (&tem, i, 0, 0,
5093 &mem_p, &reg_p, &inout_p);
5094 if (dst != str)
5095 *dst++ = ',';
5096 if (reg_p)
5098 memcpy (dst, buf, buflen);
5099 dst += buflen;
5101 else
5103 if (end)
5104 len = end - beg;
5105 else
5106 len = strlen (beg);
5107 memcpy (dst, beg, len);
5108 dst += len;
5110 if (end)
5111 beg = end + 1;
5112 else
5113 break;
5115 *dst = '\0';
5116 input = build_string (dst - str, str);
5118 else
5119 input = build_string (strlen (buf), buf);
5121 else
5122 input = build_string (constraint_len - 1, constraint + 1);
5124 free (p);
5126 input = build_tree_list (build_tree_list (NULL_TREE, input),
5127 unshare_expr (TREE_VALUE (link)));
5128 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5132 link_next = NULL_TREE;
5133 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5135 link_next = TREE_CHAIN (link);
5136 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5137 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5138 oconstraints, &allows_mem, &allows_reg);
5140 /* If we can't make copies, we can only accept memory. */
5141 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5143 if (allows_mem)
5144 allows_reg = 0;
5145 else
5147 error ("impossible constraint in %<asm%>");
5148 error ("non-memory input %d must stay in memory", i);
5149 return GS_ERROR;
5153 /* If the operand is a memory input, it should be an lvalue. */
5154 if (!allows_reg && allows_mem)
5156 tree inputv = TREE_VALUE (link);
5157 STRIP_NOPS (inputv);
5158 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5159 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5160 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5161 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5162 TREE_VALUE (link) = error_mark_node;
5163 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5164 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5165 mark_addressable (TREE_VALUE (link));
5166 if (tret == GS_ERROR)
5168 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5169 input_location = EXPR_LOCATION (TREE_VALUE (link));
5170 error ("memory input %d is not directly addressable", i);
5171 ret = tret;
5174 else
5176 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5177 is_gimple_asm_val, fb_rvalue);
5178 if (tret == GS_ERROR)
5179 ret = tret;
5182 TREE_CHAIN (link) = NULL_TREE;
5183 vec_safe_push (inputs, link);
5186 link_next = NULL_TREE;
5187 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5189 link_next = TREE_CHAIN (link);
5190 TREE_CHAIN (link) = NULL_TREE;
5191 vec_safe_push (clobbers, link);
5194 link_next = NULL_TREE;
5195 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5197 link_next = TREE_CHAIN (link);
5198 TREE_CHAIN (link) = NULL_TREE;
5199 vec_safe_push (labels, link);
5202 /* Do not add ASMs with errors to the gimple IL stream. */
5203 if (ret != GS_ERROR)
5205 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5206 inputs, outputs, clobbers, labels);
5208 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5209 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5211 gimplify_seq_add_stmt (pre_p, stmt);
5214 return ret;
5217 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5218 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5219 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5220 return to this function.
5222 FIXME should we complexify the prequeue handling instead? Or use flags
5223 for all the cleanups and let the optimizer tighten them up? The current
5224 code seems pretty fragile; it will break on a cleanup within any
5225 non-conditional nesting. But any such nesting would be broken, anyway;
5226 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5227 and continues out of it. We can do that at the RTL level, though, so
5228 having an optimizer to tighten up try/finally regions would be a Good
5229 Thing. */
5231 static enum gimplify_status
5232 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5234 gimple_stmt_iterator iter;
5235 gimple_seq body_sequence = NULL;
5237 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5239 /* We only care about the number of conditions between the innermost
5240 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5241 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5242 int old_conds = gimplify_ctxp->conditions;
5243 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5244 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5245 gimplify_ctxp->conditions = 0;
5246 gimplify_ctxp->conditional_cleanups = NULL;
5247 gimplify_ctxp->in_cleanup_point_expr = true;
5249 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5251 gimplify_ctxp->conditions = old_conds;
5252 gimplify_ctxp->conditional_cleanups = old_cleanups;
5253 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5255 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5257 gimple wce = gsi_stmt (iter);
5259 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5261 if (gsi_one_before_end_p (iter))
5263 /* Note that gsi_insert_seq_before and gsi_remove do not
5264 scan operands, unlike some other sequence mutators. */
5265 if (!gimple_wce_cleanup_eh_only (wce))
5266 gsi_insert_seq_before_without_update (&iter,
5267 gimple_wce_cleanup (wce),
5268 GSI_SAME_STMT);
5269 gsi_remove (&iter, true);
5270 break;
5272 else
5274 gtry *gtry;
5275 gimple_seq seq;
5276 enum gimple_try_flags kind;
5278 if (gimple_wce_cleanup_eh_only (wce))
5279 kind = GIMPLE_TRY_CATCH;
5280 else
5281 kind = GIMPLE_TRY_FINALLY;
5282 seq = gsi_split_seq_after (iter);
5284 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5285 /* Do not use gsi_replace here, as it may scan operands.
5286 We want to do a simple structural modification only. */
5287 gsi_set_stmt (&iter, gtry);
5288 iter = gsi_start (gtry->eval);
5291 else
5292 gsi_next (&iter);
5295 gimplify_seq_add_seq (pre_p, body_sequence);
5296 if (temp)
5298 *expr_p = temp;
5299 return GS_OK;
5301 else
5303 *expr_p = NULL;
5304 return GS_ALL_DONE;
5308 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5309 is the cleanup action required. EH_ONLY is true if the cleanup should
5310 only be executed if an exception is thrown, not on normal exit. */
5312 static void
5313 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5315 gimple wce;
5316 gimple_seq cleanup_stmts = NULL;
5318 /* Errors can result in improperly nested cleanups. Which results in
5319 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5320 if (seen_error ())
5321 return;
5323 if (gimple_conditional_context ())
5325 /* If we're in a conditional context, this is more complex. We only
5326 want to run the cleanup if we actually ran the initialization that
5327 necessitates it, but we want to run it after the end of the
5328 conditional context. So we wrap the try/finally around the
5329 condition and use a flag to determine whether or not to actually
5330 run the destructor. Thus
5332 test ? f(A()) : 0
5334 becomes (approximately)
5336 flag = 0;
5337 try {
5338 if (test) { A::A(temp); flag = 1; val = f(temp); }
5339 else { val = 0; }
5340 } finally {
5341 if (flag) A::~A(temp);
5345 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5346 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5347 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5349 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5350 gimplify_stmt (&cleanup, &cleanup_stmts);
5351 wce = gimple_build_wce (cleanup_stmts);
5353 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5354 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5355 gimplify_seq_add_stmt (pre_p, ftrue);
5357 /* Because of this manipulation, and the EH edges that jump
5358 threading cannot redirect, the temporary (VAR) will appear
5359 to be used uninitialized. Don't warn. */
5360 TREE_NO_WARNING (var) = 1;
5362 else
5364 gimplify_stmt (&cleanup, &cleanup_stmts);
5365 wce = gimple_build_wce (cleanup_stmts);
5366 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5367 gimplify_seq_add_stmt (pre_p, wce);
5371 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5373 static enum gimplify_status
5374 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5376 tree targ = *expr_p;
5377 tree temp = TARGET_EXPR_SLOT (targ);
5378 tree init = TARGET_EXPR_INITIAL (targ);
5379 enum gimplify_status ret;
5381 if (init)
5383 tree cleanup = NULL_TREE;
5385 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5386 to the temps list. Handle also variable length TARGET_EXPRs. */
5387 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5389 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5390 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5391 gimplify_vla_decl (temp, pre_p);
5393 else
5394 gimple_add_tmp_var (temp);
5396 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5397 expression is supposed to initialize the slot. */
5398 if (VOID_TYPE_P (TREE_TYPE (init)))
5399 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5400 else
5402 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5403 init = init_expr;
5404 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5405 init = NULL;
5406 ggc_free (init_expr);
5408 if (ret == GS_ERROR)
5410 /* PR c++/28266 Make sure this is expanded only once. */
5411 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5412 return GS_ERROR;
5414 if (init)
5415 gimplify_and_add (init, pre_p);
5417 /* If needed, push the cleanup for the temp. */
5418 if (TARGET_EXPR_CLEANUP (targ))
5420 if (CLEANUP_EH_ONLY (targ))
5421 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5422 CLEANUP_EH_ONLY (targ), pre_p);
5423 else
5424 cleanup = TARGET_EXPR_CLEANUP (targ);
5427 /* Add a clobber for the temporary going out of scope, like
5428 gimplify_bind_expr. */
5429 if (gimplify_ctxp->in_cleanup_point_expr
5430 && needs_to_live_in_memory (temp)
5431 && flag_stack_reuse == SR_ALL)
5433 tree clobber = build_constructor (TREE_TYPE (temp),
5434 NULL);
5435 TREE_THIS_VOLATILE (clobber) = true;
5436 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5437 if (cleanup)
5438 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5439 clobber);
5440 else
5441 cleanup = clobber;
5444 if (cleanup)
5445 gimple_push_cleanup (temp, cleanup, false, pre_p);
5447 /* Only expand this once. */
5448 TREE_OPERAND (targ, 3) = init;
5449 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5451 else
5452 /* We should have expanded this before. */
5453 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5455 *expr_p = temp;
5456 return GS_OK;
5459 /* Gimplification of expression trees. */
5461 /* Gimplify an expression which appears at statement context. The
5462 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5463 NULL, a new sequence is allocated.
5465 Return true if we actually added a statement to the queue. */
5467 bool
5468 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5470 gimple_seq_node last;
5472 last = gimple_seq_last (*seq_p);
5473 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5474 return last != gimple_seq_last (*seq_p);
5477 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5478 to CTX. If entries already exist, force them to be some flavor of private.
5479 If there is no enclosing parallel, do nothing. */
5481 void
5482 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5484 splay_tree_node n;
5486 if (decl == NULL || !DECL_P (decl))
5487 return;
5491 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5492 if (n != NULL)
5494 if (n->value & GOVD_SHARED)
5495 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5496 else if (n->value & GOVD_MAP)
5497 n->value |= GOVD_MAP_TO_ONLY;
5498 else
5499 return;
5501 else if (ctx->region_type == ORT_TARGET)
5502 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5503 else if (ctx->region_type != ORT_WORKSHARE
5504 && ctx->region_type != ORT_SIMD
5505 && ctx->region_type != ORT_TARGET_DATA)
5506 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5508 ctx = ctx->outer_context;
5510 while (ctx);
5513 /* Similarly for each of the type sizes of TYPE. */
5515 static void
5516 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5518 if (type == NULL || type == error_mark_node)
5519 return;
5520 type = TYPE_MAIN_VARIANT (type);
5522 if (ctx->privatized_types->add (type))
5523 return;
5525 switch (TREE_CODE (type))
5527 case INTEGER_TYPE:
5528 case ENUMERAL_TYPE:
5529 case BOOLEAN_TYPE:
5530 case REAL_TYPE:
5531 case FIXED_POINT_TYPE:
5532 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5533 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5534 break;
5536 case ARRAY_TYPE:
5537 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5538 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5539 break;
5541 case RECORD_TYPE:
5542 case UNION_TYPE:
5543 case QUAL_UNION_TYPE:
5545 tree field;
5546 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5547 if (TREE_CODE (field) == FIELD_DECL)
5549 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5550 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5553 break;
5555 case POINTER_TYPE:
5556 case REFERENCE_TYPE:
5557 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5558 break;
5560 default:
5561 break;
5564 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5565 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5566 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5569 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5571 static void
5572 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5574 splay_tree_node n;
5575 unsigned int nflags;
5576 tree t;
5578 if (error_operand_p (decl))
5579 return;
5581 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5582 there are constructors involved somewhere. */
5583 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5584 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5585 flags |= GOVD_SEEN;
5587 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5588 if (n != NULL && n->value != GOVD_ALIGNED)
5590 /* We shouldn't be re-adding the decl with the same data
5591 sharing class. */
5592 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5593 /* The only combination of data sharing classes we should see is
5594 FIRSTPRIVATE and LASTPRIVATE. */
5595 nflags = n->value | flags;
5596 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5597 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5598 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5599 n->value = nflags;
5600 return;
5603 /* When adding a variable-sized variable, we have to handle all sorts
5604 of additional bits of data: the pointer replacement variable, and
5605 the parameters of the type. */
5606 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5608 /* Add the pointer replacement variable as PRIVATE if the variable
5609 replacement is private, else FIRSTPRIVATE since we'll need the
5610 address of the original variable either for SHARED, or for the
5611 copy into or out of the context. */
5612 if (!(flags & GOVD_LOCAL))
5614 nflags = flags & GOVD_MAP
5615 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5616 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5617 nflags |= flags & GOVD_SEEN;
5618 t = DECL_VALUE_EXPR (decl);
5619 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5620 t = TREE_OPERAND (t, 0);
5621 gcc_assert (DECL_P (t));
5622 omp_add_variable (ctx, t, nflags);
5625 /* Add all of the variable and type parameters (which should have
5626 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5627 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5628 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5629 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5631 /* The variable-sized variable itself is never SHARED, only some form
5632 of PRIVATE. The sharing would take place via the pointer variable
5633 which we remapped above. */
5634 if (flags & GOVD_SHARED)
5635 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5636 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5638 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5639 alloca statement we generate for the variable, so make sure it
5640 is available. This isn't automatically needed for the SHARED
5641 case, since we won't be allocating local storage then.
5642 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5643 in this case omp_notice_variable will be called later
5644 on when it is gimplified. */
5645 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5646 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5647 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5649 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5650 && lang_hooks.decls.omp_privatize_by_reference (decl))
5652 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5654 /* Similar to the direct variable sized case above, we'll need the
5655 size of references being privatized. */
5656 if ((flags & GOVD_SHARED) == 0)
5658 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5659 if (TREE_CODE (t) != INTEGER_CST)
5660 omp_notice_variable (ctx, t, true);
5664 if (n != NULL)
5665 n->value |= flags;
5666 else
5667 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5670 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5671 This just prints out diagnostics about threadprivate variable uses
5672 in untied tasks. If DECL2 is non-NULL, prevent this warning
5673 on that variable. */
5675 static bool
5676 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5677 tree decl2)
5679 splay_tree_node n;
5680 struct gimplify_omp_ctx *octx;
5682 for (octx = ctx; octx; octx = octx->outer_context)
5683 if (octx->region_type == ORT_TARGET)
5685 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5686 if (n == NULL)
5688 error ("threadprivate variable %qE used in target region",
5689 DECL_NAME (decl));
5690 error_at (octx->location, "enclosing target region");
5691 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5693 if (decl2)
5694 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5697 if (ctx->region_type != ORT_UNTIED_TASK)
5698 return false;
5699 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5700 if (n == NULL)
5702 error ("threadprivate variable %qE used in untied task",
5703 DECL_NAME (decl));
5704 error_at (ctx->location, "enclosing task");
5705 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5707 if (decl2)
5708 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5709 return false;
5712 /* Record the fact that DECL was used within the OpenMP context CTX.
5713 IN_CODE is true when real code uses DECL, and false when we should
5714 merely emit default(none) errors. Return true if DECL is going to
5715 be remapped and thus DECL shouldn't be gimplified into its
5716 DECL_VALUE_EXPR (if any). */
5718 static bool
5719 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5721 splay_tree_node n;
5722 unsigned flags = in_code ? GOVD_SEEN : 0;
5723 bool ret = false, shared;
5725 if (error_operand_p (decl))
5726 return false;
5728 /* Threadprivate variables are predetermined. */
5729 if (is_global_var (decl))
5731 if (DECL_THREAD_LOCAL_P (decl))
5732 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5734 if (DECL_HAS_VALUE_EXPR_P (decl))
5736 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5738 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5739 return omp_notice_threadprivate_variable (ctx, decl, value);
5743 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5744 if (ctx->region_type == ORT_TARGET)
5746 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5747 if (n == NULL)
5749 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5751 error ("%qD referenced in target region does not have "
5752 "a mappable type", decl);
5753 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5755 else
5756 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5758 else
5760 /* If nothing changed, there's nothing left to do. */
5761 if ((n->value & flags) == flags)
5762 return ret;
5763 n->value |= flags;
5765 goto do_outer;
5768 if (n == NULL)
5770 enum omp_clause_default_kind default_kind, kind;
5771 struct gimplify_omp_ctx *octx;
5773 if (ctx->region_type == ORT_WORKSHARE
5774 || ctx->region_type == ORT_SIMD
5775 || ctx->region_type == ORT_TARGET_DATA)
5776 goto do_outer;
5778 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5779 remapped firstprivate instead of shared. To some extent this is
5780 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5781 default_kind = ctx->default_kind;
5782 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5783 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5784 default_kind = kind;
5786 switch (default_kind)
5788 case OMP_CLAUSE_DEFAULT_NONE:
5789 if ((ctx->region_type & ORT_PARALLEL) != 0)
5791 error ("%qE not specified in enclosing parallel",
5792 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5793 error_at (ctx->location, "enclosing parallel");
5795 else if ((ctx->region_type & ORT_TASK) != 0)
5797 error ("%qE not specified in enclosing task",
5798 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5799 error_at (ctx->location, "enclosing task");
5801 else if (ctx->region_type == ORT_TEAMS)
5803 error ("%qE not specified in enclosing teams construct",
5804 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5805 error_at (ctx->location, "enclosing teams construct");
5807 else
5808 gcc_unreachable ();
5809 /* FALLTHRU */
5810 case OMP_CLAUSE_DEFAULT_SHARED:
5811 flags |= GOVD_SHARED;
5812 break;
5813 case OMP_CLAUSE_DEFAULT_PRIVATE:
5814 flags |= GOVD_PRIVATE;
5815 break;
5816 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5817 flags |= GOVD_FIRSTPRIVATE;
5818 break;
5819 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5820 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5821 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5822 if (ctx->outer_context)
5823 omp_notice_variable (ctx->outer_context, decl, in_code);
5824 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5826 splay_tree_node n2;
5828 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5829 continue;
5830 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5831 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5833 flags |= GOVD_FIRSTPRIVATE;
5834 break;
5836 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5837 break;
5839 if (flags & GOVD_FIRSTPRIVATE)
5840 break;
5841 if (octx == NULL
5842 && (TREE_CODE (decl) == PARM_DECL
5843 || (!is_global_var (decl)
5844 && DECL_CONTEXT (decl) == current_function_decl)))
5846 flags |= GOVD_FIRSTPRIVATE;
5847 break;
5849 flags |= GOVD_SHARED;
5850 break;
5851 default:
5852 gcc_unreachable ();
5855 if ((flags & GOVD_PRIVATE)
5856 && lang_hooks.decls.omp_private_outer_ref (decl))
5857 flags |= GOVD_PRIVATE_OUTER_REF;
5859 omp_add_variable (ctx, decl, flags);
5861 shared = (flags & GOVD_SHARED) != 0;
5862 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5863 goto do_outer;
5866 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5867 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5868 && DECL_SIZE (decl)
5869 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5871 splay_tree_node n2;
5872 tree t = DECL_VALUE_EXPR (decl);
5873 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5874 t = TREE_OPERAND (t, 0);
5875 gcc_assert (DECL_P (t));
5876 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5877 n2->value |= GOVD_SEEN;
5880 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5881 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5883 /* If nothing changed, there's nothing left to do. */
5884 if ((n->value & flags) == flags)
5885 return ret;
5886 flags |= n->value;
5887 n->value = flags;
5889 do_outer:
5890 /* If the variable is private in the current context, then we don't
5891 need to propagate anything to an outer context. */
5892 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5893 return ret;
5894 if (ctx->outer_context
5895 && omp_notice_variable (ctx->outer_context, decl, in_code))
5896 return true;
5897 return ret;
5900 /* Verify that DECL is private within CTX. If there's specific information
5901 to the contrary in the innermost scope, generate an error. */
5903 static bool
5904 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5906 splay_tree_node n;
5908 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5909 if (n != NULL)
5911 if (n->value & GOVD_SHARED)
5913 if (ctx == gimplify_omp_ctxp)
5915 if (simd)
5916 error ("iteration variable %qE is predetermined linear",
5917 DECL_NAME (decl));
5918 else
5919 error ("iteration variable %qE should be private",
5920 DECL_NAME (decl));
5921 n->value = GOVD_PRIVATE;
5922 return true;
5924 else
5925 return false;
5927 else if ((n->value & GOVD_EXPLICIT) != 0
5928 && (ctx == gimplify_omp_ctxp
5929 || (ctx->region_type == ORT_COMBINED_PARALLEL
5930 && gimplify_omp_ctxp->outer_context == ctx)))
5932 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5933 error ("iteration variable %qE should not be firstprivate",
5934 DECL_NAME (decl));
5935 else if ((n->value & GOVD_REDUCTION) != 0)
5936 error ("iteration variable %qE should not be reduction",
5937 DECL_NAME (decl));
5938 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5939 error ("iteration variable %qE should not be lastprivate",
5940 DECL_NAME (decl));
5941 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5942 error ("iteration variable %qE should not be private",
5943 DECL_NAME (decl));
5944 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5945 error ("iteration variable %qE is predetermined linear",
5946 DECL_NAME (decl));
5948 return (ctx == gimplify_omp_ctxp
5949 || (ctx->region_type == ORT_COMBINED_PARALLEL
5950 && gimplify_omp_ctxp->outer_context == ctx));
5953 if (ctx->region_type != ORT_WORKSHARE
5954 && ctx->region_type != ORT_SIMD)
5955 return false;
5956 else if (ctx->outer_context)
5957 return omp_is_private (ctx->outer_context, decl, simd);
5958 return false;
5961 /* Return true if DECL is private within a parallel region
5962 that binds to the current construct's context or in parallel
5963 region's REDUCTION clause. */
5965 static bool
5966 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5968 splay_tree_node n;
5972 ctx = ctx->outer_context;
5973 if (ctx == NULL)
5974 return !(is_global_var (decl)
5975 /* References might be private, but might be shared too,
5976 when checking for copyprivate, assume they might be
5977 private, otherwise assume they might be shared. */
5978 || (!copyprivate
5979 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5981 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5982 continue;
5984 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5985 if (n != NULL)
5986 return (n->value & GOVD_SHARED) == 0;
5988 while (ctx->region_type == ORT_WORKSHARE
5989 || ctx->region_type == ORT_SIMD);
5990 return false;
5993 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5994 and previous omp contexts. */
5996 static void
5997 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5998 enum omp_region_type region_type)
6000 struct gimplify_omp_ctx *ctx, *outer_ctx;
6001 tree c;
6003 ctx = new_omp_context (region_type);
6004 outer_ctx = ctx->outer_context;
6006 while ((c = *list_p) != NULL)
6008 bool remove = false;
6009 bool notice_outer = true;
6010 const char *check_non_private = NULL;
6011 unsigned int flags;
6012 tree decl;
6014 switch (OMP_CLAUSE_CODE (c))
6016 case OMP_CLAUSE_PRIVATE:
6017 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6018 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6020 flags |= GOVD_PRIVATE_OUTER_REF;
6021 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6023 else
6024 notice_outer = false;
6025 goto do_add;
6026 case OMP_CLAUSE_SHARED:
6027 flags = GOVD_SHARED | GOVD_EXPLICIT;
6028 goto do_add;
6029 case OMP_CLAUSE_FIRSTPRIVATE:
6030 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6031 check_non_private = "firstprivate";
6032 goto do_add;
6033 case OMP_CLAUSE_LASTPRIVATE:
6034 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6035 check_non_private = "lastprivate";
6036 goto do_add;
6037 case OMP_CLAUSE_REDUCTION:
6038 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6039 check_non_private = "reduction";
6040 goto do_add;
6041 case OMP_CLAUSE_LINEAR:
6042 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6043 is_gimple_val, fb_rvalue) == GS_ERROR)
6045 remove = true;
6046 break;
6048 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6049 goto do_add;
6051 case OMP_CLAUSE_MAP:
6052 decl = OMP_CLAUSE_DECL (c);
6053 if (error_operand_p (decl))
6055 remove = true;
6056 break;
6058 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6059 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6060 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6061 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6062 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6064 remove = true;
6065 break;
6067 if (!DECL_P (decl))
6069 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6070 NULL, is_gimple_lvalue, fb_lvalue)
6071 == GS_ERROR)
6073 remove = true;
6074 break;
6076 break;
6078 flags = GOVD_MAP | GOVD_EXPLICIT;
6079 goto do_add;
6081 case OMP_CLAUSE_DEPEND:
6082 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6084 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6085 NULL, is_gimple_val, fb_rvalue);
6086 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6088 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6090 remove = true;
6091 break;
6093 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6094 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6095 is_gimple_val, fb_rvalue) == GS_ERROR)
6097 remove = true;
6098 break;
6100 break;
6102 case OMP_CLAUSE_TO:
6103 case OMP_CLAUSE_FROM:
6104 decl = OMP_CLAUSE_DECL (c);
6105 if (error_operand_p (decl))
6107 remove = true;
6108 break;
6110 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6111 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6112 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6113 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6114 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6116 remove = true;
6117 break;
6119 if (!DECL_P (decl))
6121 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6122 NULL, is_gimple_lvalue, fb_lvalue)
6123 == GS_ERROR)
6125 remove = true;
6126 break;
6128 break;
6130 goto do_notice;
6132 do_add:
6133 decl = OMP_CLAUSE_DECL (c);
6134 if (error_operand_p (decl))
6136 remove = true;
6137 break;
6139 omp_add_variable (ctx, decl, flags);
6140 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6141 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6143 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6144 GOVD_LOCAL | GOVD_SEEN);
6145 gimplify_omp_ctxp = ctx;
6146 push_gimplify_context ();
6148 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6149 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6151 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6152 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6153 pop_gimplify_context
6154 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6155 push_gimplify_context ();
6156 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6157 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6158 pop_gimplify_context
6159 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6160 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6161 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6163 gimplify_omp_ctxp = outer_ctx;
6165 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6166 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6168 gimplify_omp_ctxp = ctx;
6169 push_gimplify_context ();
6170 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6172 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6173 NULL, NULL);
6174 TREE_SIDE_EFFECTS (bind) = 1;
6175 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6176 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6178 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6179 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6180 pop_gimplify_context
6181 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6182 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6184 gimplify_omp_ctxp = outer_ctx;
6186 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6187 && OMP_CLAUSE_LINEAR_STMT (c))
6189 gimplify_omp_ctxp = ctx;
6190 push_gimplify_context ();
6191 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6193 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6194 NULL, NULL);
6195 TREE_SIDE_EFFECTS (bind) = 1;
6196 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6197 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6199 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6200 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6201 pop_gimplify_context
6202 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6203 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6205 gimplify_omp_ctxp = outer_ctx;
6207 if (notice_outer)
6208 goto do_notice;
6209 break;
6211 case OMP_CLAUSE_COPYIN:
6212 case OMP_CLAUSE_COPYPRIVATE:
6213 decl = OMP_CLAUSE_DECL (c);
6214 if (error_operand_p (decl))
6216 remove = true;
6217 break;
6219 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6220 && !remove
6221 && !omp_check_private (ctx, decl, true))
6223 remove = true;
6224 if (is_global_var (decl))
6226 if (DECL_THREAD_LOCAL_P (decl))
6227 remove = false;
6228 else if (DECL_HAS_VALUE_EXPR_P (decl))
6230 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6232 if (value
6233 && DECL_P (value)
6234 && DECL_THREAD_LOCAL_P (value))
6235 remove = false;
6238 if (remove)
6239 error_at (OMP_CLAUSE_LOCATION (c),
6240 "copyprivate variable %qE is not threadprivate"
6241 " or private in outer context", DECL_NAME (decl));
6243 do_notice:
6244 if (outer_ctx)
6245 omp_notice_variable (outer_ctx, decl, true);
6246 if (check_non_private
6247 && region_type == ORT_WORKSHARE
6248 && omp_check_private (ctx, decl, false))
6250 error ("%s variable %qE is private in outer context",
6251 check_non_private, DECL_NAME (decl));
6252 remove = true;
6254 break;
6256 case OMP_CLAUSE_FINAL:
6257 case OMP_CLAUSE_IF:
6258 OMP_CLAUSE_OPERAND (c, 0)
6259 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6260 /* Fall through. */
6262 case OMP_CLAUSE_SCHEDULE:
6263 case OMP_CLAUSE_NUM_THREADS:
6264 case OMP_CLAUSE_NUM_TEAMS:
6265 case OMP_CLAUSE_THREAD_LIMIT:
6266 case OMP_CLAUSE_DIST_SCHEDULE:
6267 case OMP_CLAUSE_DEVICE:
6268 case OMP_CLAUSE__CILK_FOR_COUNT_:
6269 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6270 is_gimple_val, fb_rvalue) == GS_ERROR)
6271 remove = true;
6272 break;
6274 case OMP_CLAUSE_NOWAIT:
6275 case OMP_CLAUSE_ORDERED:
6276 case OMP_CLAUSE_UNTIED:
6277 case OMP_CLAUSE_COLLAPSE:
6278 case OMP_CLAUSE_MERGEABLE:
6279 case OMP_CLAUSE_PROC_BIND:
6280 case OMP_CLAUSE_SAFELEN:
6281 break;
6283 case OMP_CLAUSE_ALIGNED:
6284 decl = OMP_CLAUSE_DECL (c);
6285 if (error_operand_p (decl))
6287 remove = true;
6288 break;
6290 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6291 is_gimple_val, fb_rvalue) == GS_ERROR)
6293 remove = true;
6294 break;
6296 if (!is_global_var (decl)
6297 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6298 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6299 break;
6301 case OMP_CLAUSE_DEFAULT:
6302 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6303 break;
6305 default:
6306 gcc_unreachable ();
6309 if (remove)
6310 *list_p = OMP_CLAUSE_CHAIN (c);
6311 else
6312 list_p = &OMP_CLAUSE_CHAIN (c);
6315 gimplify_omp_ctxp = ctx;
6318 struct gimplify_adjust_omp_clauses_data
6320 tree *list_p;
6321 gimple_seq *pre_p;
6324 /* For all variables that were not actually used within the context,
6325 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6327 static int
6328 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6330 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6331 gimple_seq *pre_p
6332 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6333 tree decl = (tree) n->key;
6334 unsigned flags = n->value;
6335 enum omp_clause_code code;
6336 tree clause;
6337 bool private_debug;
6339 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6340 return 0;
6341 if ((flags & GOVD_SEEN) == 0)
6342 return 0;
6343 if (flags & GOVD_DEBUG_PRIVATE)
6345 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6346 private_debug = true;
6348 else if (flags & GOVD_MAP)
6349 private_debug = false;
6350 else
6351 private_debug
6352 = lang_hooks.decls.omp_private_debug_clause (decl,
6353 !!(flags & GOVD_SHARED));
6354 if (private_debug)
6355 code = OMP_CLAUSE_PRIVATE;
6356 else if (flags & GOVD_MAP)
6357 code = OMP_CLAUSE_MAP;
6358 else if (flags & GOVD_SHARED)
6360 if (is_global_var (decl))
6362 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6363 while (ctx != NULL)
6365 splay_tree_node on
6366 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6367 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6368 | GOVD_PRIVATE | GOVD_REDUCTION
6369 | GOVD_LINEAR | GOVD_MAP)) != 0)
6370 break;
6371 ctx = ctx->outer_context;
6373 if (ctx == NULL)
6374 return 0;
6376 code = OMP_CLAUSE_SHARED;
6378 else if (flags & GOVD_PRIVATE)
6379 code = OMP_CLAUSE_PRIVATE;
6380 else if (flags & GOVD_FIRSTPRIVATE)
6381 code = OMP_CLAUSE_FIRSTPRIVATE;
6382 else if (flags & GOVD_LASTPRIVATE)
6383 code = OMP_CLAUSE_LASTPRIVATE;
6384 else if (flags & GOVD_ALIGNED)
6385 return 0;
6386 else
6387 gcc_unreachable ();
6389 clause = build_omp_clause (input_location, code);
6390 OMP_CLAUSE_DECL (clause) = decl;
6391 OMP_CLAUSE_CHAIN (clause) = *list_p;
6392 if (private_debug)
6393 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6394 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6395 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6396 else if (code == OMP_CLAUSE_MAP)
6398 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6399 ? OMP_CLAUSE_MAP_TO
6400 : OMP_CLAUSE_MAP_TOFROM;
6401 if (DECL_SIZE (decl)
6402 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6404 tree decl2 = DECL_VALUE_EXPR (decl);
6405 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6406 decl2 = TREE_OPERAND (decl2, 0);
6407 gcc_assert (DECL_P (decl2));
6408 tree mem = build_simple_mem_ref (decl2);
6409 OMP_CLAUSE_DECL (clause) = mem;
6410 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6411 if (gimplify_omp_ctxp->outer_context)
6413 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6414 omp_notice_variable (ctx, decl2, true);
6415 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6417 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6418 OMP_CLAUSE_MAP);
6419 OMP_CLAUSE_DECL (nc) = decl;
6420 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6421 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6422 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6423 OMP_CLAUSE_CHAIN (clause) = nc;
6425 else
6426 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6428 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6430 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6431 OMP_CLAUSE_DECL (nc) = decl;
6432 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6433 OMP_CLAUSE_CHAIN (nc) = *list_p;
6434 OMP_CLAUSE_CHAIN (clause) = nc;
6435 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6436 gimplify_omp_ctxp = ctx->outer_context;
6437 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6438 gimplify_omp_ctxp = ctx;
6440 *list_p = clause;
6441 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6442 gimplify_omp_ctxp = ctx->outer_context;
6443 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6444 gimplify_omp_ctxp = ctx;
6445 return 0;
6448 static void
6449 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6451 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6452 tree c, decl;
6454 while ((c = *list_p) != NULL)
6456 splay_tree_node n;
6457 bool remove = false;
6459 switch (OMP_CLAUSE_CODE (c))
6461 case OMP_CLAUSE_PRIVATE:
6462 case OMP_CLAUSE_SHARED:
6463 case OMP_CLAUSE_FIRSTPRIVATE:
6464 case OMP_CLAUSE_LINEAR:
6465 decl = OMP_CLAUSE_DECL (c);
6466 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6467 remove = !(n->value & GOVD_SEEN);
6468 if (! remove)
6470 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6471 if ((n->value & GOVD_DEBUG_PRIVATE)
6472 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6474 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6475 || ((n->value & GOVD_DATA_SHARE_CLASS)
6476 == GOVD_PRIVATE));
6477 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6478 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6480 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6481 && ctx->outer_context
6482 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6483 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6485 if (ctx->outer_context->combined_loop
6486 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6488 n = splay_tree_lookup (ctx->outer_context->variables,
6489 (splay_tree_key) decl);
6490 if (n == NULL
6491 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6493 int flags = GOVD_FIRSTPRIVATE;
6494 /* #pragma omp distribute does not allow
6495 lastprivate clause. */
6496 if (!ctx->outer_context->distribute)
6497 flags |= GOVD_LASTPRIVATE;
6498 if (n == NULL)
6499 omp_add_variable (ctx->outer_context, decl,
6500 flags | GOVD_SEEN);
6501 else
6502 n->value |= flags | GOVD_SEEN;
6505 else if (!is_global_var (decl))
6506 omp_notice_variable (ctx->outer_context, decl, true);
6509 break;
6511 case OMP_CLAUSE_LASTPRIVATE:
6512 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6513 accurately reflect the presence of a FIRSTPRIVATE clause. */
6514 decl = OMP_CLAUSE_DECL (c);
6515 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6516 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6517 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6518 break;
6520 case OMP_CLAUSE_ALIGNED:
6521 decl = OMP_CLAUSE_DECL (c);
6522 if (!is_global_var (decl))
6524 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6525 remove = n == NULL || !(n->value & GOVD_SEEN);
6526 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6528 struct gimplify_omp_ctx *octx;
6529 if (n != NULL
6530 && (n->value & (GOVD_DATA_SHARE_CLASS
6531 & ~GOVD_FIRSTPRIVATE)))
6532 remove = true;
6533 else
6534 for (octx = ctx->outer_context; octx;
6535 octx = octx->outer_context)
6537 n = splay_tree_lookup (octx->variables,
6538 (splay_tree_key) decl);
6539 if (n == NULL)
6540 continue;
6541 if (n->value & GOVD_LOCAL)
6542 break;
6543 /* We have to avoid assigning a shared variable
6544 to itself when trying to add
6545 __builtin_assume_aligned. */
6546 if (n->value & GOVD_SHARED)
6548 remove = true;
6549 break;
6554 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6556 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6557 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6558 remove = true;
6560 break;
6562 case OMP_CLAUSE_MAP:
6563 decl = OMP_CLAUSE_DECL (c);
6564 if (!DECL_P (decl))
6565 break;
6566 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6567 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6568 remove = true;
6569 else if (DECL_SIZE (decl)
6570 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6571 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6573 tree decl2 = DECL_VALUE_EXPR (decl);
6574 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6575 decl2 = TREE_OPERAND (decl2, 0);
6576 gcc_assert (DECL_P (decl2));
6577 tree mem = build_simple_mem_ref (decl2);
6578 OMP_CLAUSE_DECL (c) = mem;
6579 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6580 if (ctx->outer_context)
6582 omp_notice_variable (ctx->outer_context, decl2, true);
6583 omp_notice_variable (ctx->outer_context,
6584 OMP_CLAUSE_SIZE (c), true);
6586 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6587 OMP_CLAUSE_MAP);
6588 OMP_CLAUSE_DECL (nc) = decl;
6589 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6590 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6591 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6592 OMP_CLAUSE_CHAIN (c) = nc;
6593 c = nc;
6595 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6596 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6597 break;
6599 case OMP_CLAUSE_TO:
6600 case OMP_CLAUSE_FROM:
6601 decl = OMP_CLAUSE_DECL (c);
6602 if (!DECL_P (decl))
6603 break;
6604 if (DECL_SIZE (decl)
6605 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6607 tree decl2 = DECL_VALUE_EXPR (decl);
6608 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6609 decl2 = TREE_OPERAND (decl2, 0);
6610 gcc_assert (DECL_P (decl2));
6611 tree mem = build_simple_mem_ref (decl2);
6612 OMP_CLAUSE_DECL (c) = mem;
6613 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6614 if (ctx->outer_context)
6616 omp_notice_variable (ctx->outer_context, decl2, true);
6617 omp_notice_variable (ctx->outer_context,
6618 OMP_CLAUSE_SIZE (c), true);
6621 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6622 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6623 break;
6625 case OMP_CLAUSE_REDUCTION:
6626 case OMP_CLAUSE_COPYIN:
6627 case OMP_CLAUSE_COPYPRIVATE:
6628 case OMP_CLAUSE_IF:
6629 case OMP_CLAUSE_NUM_THREADS:
6630 case OMP_CLAUSE_NUM_TEAMS:
6631 case OMP_CLAUSE_THREAD_LIMIT:
6632 case OMP_CLAUSE_DIST_SCHEDULE:
6633 case OMP_CLAUSE_DEVICE:
6634 case OMP_CLAUSE_SCHEDULE:
6635 case OMP_CLAUSE_NOWAIT:
6636 case OMP_CLAUSE_ORDERED:
6637 case OMP_CLAUSE_DEFAULT:
6638 case OMP_CLAUSE_UNTIED:
6639 case OMP_CLAUSE_COLLAPSE:
6640 case OMP_CLAUSE_FINAL:
6641 case OMP_CLAUSE_MERGEABLE:
6642 case OMP_CLAUSE_PROC_BIND:
6643 case OMP_CLAUSE_SAFELEN:
6644 case OMP_CLAUSE_DEPEND:
6645 case OMP_CLAUSE__CILK_FOR_COUNT_:
6646 break;
6648 default:
6649 gcc_unreachable ();
6652 if (remove)
6653 *list_p = OMP_CLAUSE_CHAIN (c);
6654 else
6655 list_p = &OMP_CLAUSE_CHAIN (c);
6658 /* Add in any implicit data sharing. */
6659 struct gimplify_adjust_omp_clauses_data data;
6660 data.list_p = list_p;
6661 data.pre_p = pre_p;
6662 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6664 gimplify_omp_ctxp = ctx->outer_context;
6665 delete_omp_context (ctx);
6668 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6669 gimplification of the body, as well as scanning the body for used
6670 variables. We need to do this scan now, because variable-sized
6671 decls will be decomposed during gimplification. */
6673 static void
6674 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6676 tree expr = *expr_p;
6677 gimple g;
6678 gimple_seq body = NULL;
6680 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6681 OMP_PARALLEL_COMBINED (expr)
6682 ? ORT_COMBINED_PARALLEL
6683 : ORT_PARALLEL);
6685 push_gimplify_context ();
6687 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6688 if (gimple_code (g) == GIMPLE_BIND)
6689 pop_gimplify_context (g);
6690 else
6691 pop_gimplify_context (NULL);
6693 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6695 g = gimple_build_omp_parallel (body,
6696 OMP_PARALLEL_CLAUSES (expr),
6697 NULL_TREE, NULL_TREE);
6698 if (OMP_PARALLEL_COMBINED (expr))
6699 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6700 gimplify_seq_add_stmt (pre_p, g);
6701 *expr_p = NULL_TREE;
6704 /* Gimplify the contents of an OMP_TASK statement. This involves
6705 gimplification of the body, as well as scanning the body for used
6706 variables. We need to do this scan now, because variable-sized
6707 decls will be decomposed during gimplification. */
6709 static void
6710 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6712 tree expr = *expr_p;
6713 gimple g;
6714 gimple_seq body = NULL;
6716 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6717 find_omp_clause (OMP_TASK_CLAUSES (expr),
6718 OMP_CLAUSE_UNTIED)
6719 ? ORT_UNTIED_TASK : ORT_TASK);
6721 push_gimplify_context ();
6723 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6724 if (gimple_code (g) == GIMPLE_BIND)
6725 pop_gimplify_context (g);
6726 else
6727 pop_gimplify_context (NULL);
6729 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6731 g = gimple_build_omp_task (body,
6732 OMP_TASK_CLAUSES (expr),
6733 NULL_TREE, NULL_TREE,
6734 NULL_TREE, NULL_TREE, NULL_TREE);
6735 gimplify_seq_add_stmt (pre_p, g);
6736 *expr_p = NULL_TREE;
6739 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6740 with non-NULL OMP_FOR_INIT. */
6742 static tree
6743 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6745 *walk_subtrees = 0;
6746 switch (TREE_CODE (*tp))
6748 case OMP_FOR:
6749 *walk_subtrees = 1;
6750 /* FALLTHRU */
6751 case OMP_SIMD:
6752 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6753 return *tp;
6754 break;
6755 case BIND_EXPR:
6756 case STATEMENT_LIST:
6757 case OMP_PARALLEL:
6758 *walk_subtrees = 1;
6759 break;
6760 default:
6761 break;
6763 return NULL_TREE;
6766 /* Gimplify the gross structure of an OMP_FOR statement. */
6768 static enum gimplify_status
6769 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6771 tree for_stmt, orig_for_stmt, decl, var, t;
6772 enum gimplify_status ret = GS_ALL_DONE;
6773 enum gimplify_status tret;
6774 gomp_for *gfor;
6775 gimple_seq for_body, for_pre_body;
6776 int i;
6777 bool simd;
6778 bitmap has_decl_expr = NULL;
6780 orig_for_stmt = for_stmt = *expr_p;
6782 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6783 || TREE_CODE (for_stmt) == CILK_SIMD);
6784 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6785 simd ? ORT_SIMD : ORT_WORKSHARE);
6786 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6787 gimplify_omp_ctxp->distribute = true;
6789 /* Handle OMP_FOR_INIT. */
6790 for_pre_body = NULL;
6791 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6793 has_decl_expr = BITMAP_ALLOC (NULL);
6794 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6795 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6796 == VAR_DECL)
6798 t = OMP_FOR_PRE_BODY (for_stmt);
6799 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6801 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6803 tree_stmt_iterator si;
6804 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6805 tsi_next (&si))
6807 t = tsi_stmt (si);
6808 if (TREE_CODE (t) == DECL_EXPR
6809 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6810 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6814 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6815 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6817 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6819 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6820 NULL, NULL);
6821 gcc_assert (for_stmt != NULL_TREE);
6822 gimplify_omp_ctxp->combined_loop = true;
6825 for_body = NULL;
6826 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6827 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6828 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6829 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6830 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6832 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6833 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6834 decl = TREE_OPERAND (t, 0);
6835 gcc_assert (DECL_P (decl));
6836 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6837 || POINTER_TYPE_P (TREE_TYPE (decl)));
6839 /* Make sure the iteration variable is private. */
6840 tree c = NULL_TREE;
6841 tree c2 = NULL_TREE;
6842 if (orig_for_stmt != for_stmt)
6843 /* Do this only on innermost construct for combined ones. */;
6844 else if (simd)
6846 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6847 (splay_tree_key)decl);
6848 omp_is_private (gimplify_omp_ctxp, decl,
6849 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6850 != 1));
6851 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6852 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6853 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6855 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6856 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6857 if (has_decl_expr
6858 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6859 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6860 OMP_CLAUSE_DECL (c) = decl;
6861 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6862 OMP_FOR_CLAUSES (for_stmt) = c;
6863 omp_add_variable (gimplify_omp_ctxp, decl,
6864 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6866 else
6868 bool lastprivate
6869 = (!has_decl_expr
6870 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6871 if (lastprivate
6872 && gimplify_omp_ctxp->outer_context
6873 && gimplify_omp_ctxp->outer_context->region_type
6874 == ORT_WORKSHARE
6875 && gimplify_omp_ctxp->outer_context->combined_loop
6876 && !gimplify_omp_ctxp->outer_context->distribute)
6878 struct gimplify_omp_ctx *outer
6879 = gimplify_omp_ctxp->outer_context;
6880 n = splay_tree_lookup (outer->variables,
6881 (splay_tree_key) decl);
6882 if (n != NULL
6883 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6884 lastprivate = false;
6885 else if (omp_check_private (outer, decl, false))
6886 error ("lastprivate variable %qE is private in outer "
6887 "context", DECL_NAME (decl));
6888 else
6890 omp_add_variable (outer, decl,
6891 GOVD_LASTPRIVATE | GOVD_SEEN);
6892 if (outer->outer_context)
6893 omp_notice_variable (outer->outer_context, decl, true);
6896 c = build_omp_clause (input_location,
6897 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6898 : OMP_CLAUSE_PRIVATE);
6899 OMP_CLAUSE_DECL (c) = decl;
6900 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6901 OMP_FOR_CLAUSES (for_stmt) = c;
6902 omp_add_variable (gimplify_omp_ctxp, decl,
6903 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6904 | GOVD_EXPLICIT | GOVD_SEEN);
6905 c = NULL_TREE;
6908 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6909 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6910 else
6911 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6913 /* If DECL is not a gimple register, create a temporary variable to act
6914 as an iteration counter. This is valid, since DECL cannot be
6915 modified in the body of the loop. Similarly for any iteration vars
6916 in simd with collapse > 1 where the iterator vars must be
6917 lastprivate. */
6918 if (orig_for_stmt != for_stmt)
6919 var = decl;
6920 else if (!is_gimple_reg (decl)
6921 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
6923 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6924 TREE_OPERAND (t, 0) = var;
6926 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6928 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6930 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6931 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6932 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6933 OMP_CLAUSE_DECL (c2) = var;
6934 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6935 OMP_FOR_CLAUSES (for_stmt) = c2;
6936 omp_add_variable (gimplify_omp_ctxp, var,
6937 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6938 if (c == NULL_TREE)
6940 c = c2;
6941 c2 = NULL_TREE;
6944 else
6945 omp_add_variable (gimplify_omp_ctxp, var,
6946 GOVD_PRIVATE | GOVD_SEEN);
6948 else
6949 var = decl;
6951 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6952 is_gimple_val, fb_rvalue);
6953 ret = MIN (ret, tret);
6954 if (ret == GS_ERROR)
6955 return ret;
6957 /* Handle OMP_FOR_COND. */
6958 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6959 gcc_assert (COMPARISON_CLASS_P (t));
6960 gcc_assert (TREE_OPERAND (t, 0) == decl);
6962 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6963 is_gimple_val, fb_rvalue);
6964 ret = MIN (ret, tret);
6966 /* Handle OMP_FOR_INCR. */
6967 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6968 switch (TREE_CODE (t))
6970 case PREINCREMENT_EXPR:
6971 case POSTINCREMENT_EXPR:
6973 tree decl = TREE_OPERAND (t, 0);
6974 /* c_omp_for_incr_canonicalize_ptr() should have been
6975 called to massage things appropriately. */
6976 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6978 if (orig_for_stmt != for_stmt)
6979 break;
6980 t = build_int_cst (TREE_TYPE (decl), 1);
6981 if (c)
6982 OMP_CLAUSE_LINEAR_STEP (c) = t;
6983 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6984 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6985 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6986 break;
6989 case PREDECREMENT_EXPR:
6990 case POSTDECREMENT_EXPR:
6991 /* c_omp_for_incr_canonicalize_ptr() should have been
6992 called to massage things appropriately. */
6993 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6994 if (orig_for_stmt != for_stmt)
6995 break;
6996 t = build_int_cst (TREE_TYPE (decl), -1);
6997 if (c)
6998 OMP_CLAUSE_LINEAR_STEP (c) = t;
6999 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7000 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7001 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7002 break;
7004 case MODIFY_EXPR:
7005 gcc_assert (TREE_OPERAND (t, 0) == decl);
7006 TREE_OPERAND (t, 0) = var;
7008 t = TREE_OPERAND (t, 1);
7009 switch (TREE_CODE (t))
7011 case PLUS_EXPR:
7012 if (TREE_OPERAND (t, 1) == decl)
7014 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7015 TREE_OPERAND (t, 0) = var;
7016 break;
7019 /* Fallthru. */
7020 case MINUS_EXPR:
7021 case POINTER_PLUS_EXPR:
7022 gcc_assert (TREE_OPERAND (t, 0) == decl);
7023 TREE_OPERAND (t, 0) = var;
7024 break;
7025 default:
7026 gcc_unreachable ();
7029 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7030 is_gimple_val, fb_rvalue);
7031 ret = MIN (ret, tret);
7032 if (c)
7034 tree step = TREE_OPERAND (t, 1);
7035 tree stept = TREE_TYPE (decl);
7036 if (POINTER_TYPE_P (stept))
7037 stept = sizetype;
7038 step = fold_convert (stept, step);
7039 if (TREE_CODE (t) == MINUS_EXPR)
7040 step = fold_build1 (NEGATE_EXPR, stept, step);
7041 OMP_CLAUSE_LINEAR_STEP (c) = step;
7042 if (step != TREE_OPERAND (t, 1))
7044 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7045 &for_pre_body, NULL,
7046 is_gimple_val, fb_rvalue);
7047 ret = MIN (ret, tret);
7050 break;
7052 default:
7053 gcc_unreachable ();
7056 if (c2)
7058 gcc_assert (c);
7059 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7062 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7063 && orig_for_stmt == for_stmt)
7065 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7066 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7067 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7068 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7069 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7070 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7071 && OMP_CLAUSE_DECL (c) == decl)
7073 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7074 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7075 gcc_assert (TREE_OPERAND (t, 0) == var);
7076 t = TREE_OPERAND (t, 1);
7077 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7078 || TREE_CODE (t) == MINUS_EXPR
7079 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7080 gcc_assert (TREE_OPERAND (t, 0) == var);
7081 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7082 TREE_OPERAND (t, 1));
7083 gimple_seq *seq;
7084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7085 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7086 else
7087 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7088 gimplify_assign (decl, t, seq);
7093 BITMAP_FREE (has_decl_expr);
7095 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7097 if (orig_for_stmt != for_stmt)
7098 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7100 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7101 decl = TREE_OPERAND (t, 0);
7102 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7103 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7104 TREE_OPERAND (t, 0) = var;
7105 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7106 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7107 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7110 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7112 int kind;
7113 switch (TREE_CODE (orig_for_stmt))
7115 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7116 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7117 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7118 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7119 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7120 default:
7121 gcc_unreachable ();
7123 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7124 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7125 for_pre_body);
7126 if (orig_for_stmt != for_stmt)
7127 gimple_omp_for_set_combined_p (gfor, true);
7128 if (gimplify_omp_ctxp
7129 && (gimplify_omp_ctxp->combined_loop
7130 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7131 && gimplify_omp_ctxp->outer_context
7132 && gimplify_omp_ctxp->outer_context->combined_loop)))
7134 gimple_omp_for_set_combined_into_p (gfor, true);
7135 if (gimplify_omp_ctxp->combined_loop)
7136 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7137 else
7138 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7141 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7143 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7144 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7145 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7146 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7147 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7148 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7149 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7150 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7153 gimplify_seq_add_stmt (pre_p, gfor);
7154 if (ret != GS_ALL_DONE)
7155 return GS_ERROR;
7156 *expr_p = NULL_TREE;
7157 return GS_ALL_DONE;
7160 /* Gimplify the gross structure of other OpenMP constructs.
7161 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7162 and OMP_TEAMS. */
7164 static void
7165 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7167 tree expr = *expr_p;
7168 gimple stmt;
7169 gimple_seq body = NULL;
7170 enum omp_region_type ort = ORT_WORKSHARE;
7172 switch (TREE_CODE (expr))
7174 case OMP_SECTIONS:
7175 case OMP_SINGLE:
7176 break;
7177 case OMP_TARGET:
7178 ort = ORT_TARGET;
7179 break;
7180 case OMP_TARGET_DATA:
7181 ort = ORT_TARGET_DATA;
7182 break;
7183 case OMP_TEAMS:
7184 ort = ORT_TEAMS;
7185 break;
7186 default:
7187 gcc_unreachable ();
7189 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7190 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7192 push_gimplify_context ();
7193 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7194 if (gimple_code (g) == GIMPLE_BIND)
7195 pop_gimplify_context (g);
7196 else
7197 pop_gimplify_context (NULL);
7198 if (ort == ORT_TARGET_DATA)
7200 gimple_seq cleanup = NULL;
7201 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7202 g = gimple_build_call (fn, 0);
7203 gimple_seq_add_stmt (&cleanup, g);
7204 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7205 body = NULL;
7206 gimple_seq_add_stmt (&body, g);
7209 else
7210 gimplify_and_add (OMP_BODY (expr), &body);
7211 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7213 switch (TREE_CODE (expr))
7215 case OMP_SECTIONS:
7216 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7217 break;
7218 case OMP_SINGLE:
7219 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7220 break;
7221 case OMP_TARGET:
7222 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7223 OMP_CLAUSES (expr));
7224 break;
7225 case OMP_TARGET_DATA:
7226 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7227 OMP_CLAUSES (expr));
7228 break;
7229 case OMP_TEAMS:
7230 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7231 break;
7232 default:
7233 gcc_unreachable ();
7236 gimplify_seq_add_stmt (pre_p, stmt);
7237 *expr_p = NULL_TREE;
7240 /* Gimplify the gross structure of OpenMP target update construct. */
7242 static void
7243 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7245 tree expr = *expr_p;
7246 gomp_target *stmt;
7248 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7249 ORT_WORKSHARE);
7250 gimplify_adjust_omp_clauses (pre_p, &OMP_TARGET_UPDATE_CLAUSES (expr));
7251 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7252 OMP_TARGET_UPDATE_CLAUSES (expr));
7254 gimplify_seq_add_stmt (pre_p, stmt);
7255 *expr_p = NULL_TREE;
7258 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7259 stabilized the lhs of the atomic operation as *ADDR. Return true if
7260 EXPR is this stabilized form. */
7262 static bool
7263 goa_lhs_expr_p (tree expr, tree addr)
7265 /* Also include casts to other type variants. The C front end is fond
7266 of adding these for e.g. volatile variables. This is like
7267 STRIP_TYPE_NOPS but includes the main variant lookup. */
7268 STRIP_USELESS_TYPE_CONVERSION (expr);
7270 if (TREE_CODE (expr) == INDIRECT_REF)
7272 expr = TREE_OPERAND (expr, 0);
7273 while (expr != addr
7274 && (CONVERT_EXPR_P (expr)
7275 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7276 && TREE_CODE (expr) == TREE_CODE (addr)
7277 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7279 expr = TREE_OPERAND (expr, 0);
7280 addr = TREE_OPERAND (addr, 0);
7282 if (expr == addr)
7283 return true;
7284 return (TREE_CODE (addr) == ADDR_EXPR
7285 && TREE_CODE (expr) == ADDR_EXPR
7286 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7288 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7289 return true;
7290 return false;
7293 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7294 expression does not involve the lhs, evaluate it into a temporary.
7295 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7296 or -1 if an error was encountered. */
7298 static int
7299 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7300 tree lhs_var)
7302 tree expr = *expr_p;
7303 int saw_lhs;
7305 if (goa_lhs_expr_p (expr, lhs_addr))
7307 *expr_p = lhs_var;
7308 return 1;
7310 if (is_gimple_val (expr))
7311 return 0;
7313 saw_lhs = 0;
7314 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7316 case tcc_binary:
7317 case tcc_comparison:
7318 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7319 lhs_var);
7320 case tcc_unary:
7321 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7322 lhs_var);
7323 break;
7324 case tcc_expression:
7325 switch (TREE_CODE (expr))
7327 case TRUTH_ANDIF_EXPR:
7328 case TRUTH_ORIF_EXPR:
7329 case TRUTH_AND_EXPR:
7330 case TRUTH_OR_EXPR:
7331 case TRUTH_XOR_EXPR:
7332 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7333 lhs_addr, lhs_var);
7334 case TRUTH_NOT_EXPR:
7335 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7336 lhs_addr, lhs_var);
7337 break;
7338 case COMPOUND_EXPR:
7339 /* Break out any preevaluations from cp_build_modify_expr. */
7340 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7341 expr = TREE_OPERAND (expr, 1))
7342 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7343 *expr_p = expr;
7344 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7345 default:
7346 break;
7348 break;
7349 default:
7350 break;
7353 if (saw_lhs == 0)
7355 enum gimplify_status gs;
7356 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7357 if (gs != GS_ALL_DONE)
7358 saw_lhs = -1;
7361 return saw_lhs;
7364 /* Gimplify an OMP_ATOMIC statement. */
7366 static enum gimplify_status
7367 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7369 tree addr = TREE_OPERAND (*expr_p, 0);
7370 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7371 ? NULL : TREE_OPERAND (*expr_p, 1);
7372 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7373 tree tmp_load;
7374 gomp_atomic_load *loadstmt;
7375 gomp_atomic_store *storestmt;
7377 tmp_load = create_tmp_reg (type, NULL);
7378 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7379 return GS_ERROR;
7381 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7382 != GS_ALL_DONE)
7383 return GS_ERROR;
7385 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7386 gimplify_seq_add_stmt (pre_p, loadstmt);
7387 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7388 != GS_ALL_DONE)
7389 return GS_ERROR;
7391 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7392 rhs = tmp_load;
7393 storestmt = gimple_build_omp_atomic_store (rhs);
7394 gimplify_seq_add_stmt (pre_p, storestmt);
7395 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7397 gimple_omp_atomic_set_seq_cst (loadstmt);
7398 gimple_omp_atomic_set_seq_cst (storestmt);
7400 switch (TREE_CODE (*expr_p))
7402 case OMP_ATOMIC_READ:
7403 case OMP_ATOMIC_CAPTURE_OLD:
7404 *expr_p = tmp_load;
7405 gimple_omp_atomic_set_need_value (loadstmt);
7406 break;
7407 case OMP_ATOMIC_CAPTURE_NEW:
7408 *expr_p = rhs;
7409 gimple_omp_atomic_set_need_value (storestmt);
7410 break;
7411 default:
7412 *expr_p = NULL;
7413 break;
7416 return GS_ALL_DONE;
7419 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7420 body, and adding some EH bits. */
7422 static enum gimplify_status
7423 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7425 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7426 gimple body_stmt;
7427 gtransaction *trans_stmt;
7428 gimple_seq body = NULL;
7429 int subcode = 0;
7431 /* Wrap the transaction body in a BIND_EXPR so we have a context
7432 where to put decls for OpenMP. */
7433 if (TREE_CODE (tbody) != BIND_EXPR)
7435 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7436 TREE_SIDE_EFFECTS (bind) = 1;
7437 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7438 TRANSACTION_EXPR_BODY (expr) = bind;
7441 push_gimplify_context ();
7442 temp = voidify_wrapper_expr (*expr_p, NULL);
7444 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7445 pop_gimplify_context (body_stmt);
7447 trans_stmt = gimple_build_transaction (body, NULL);
7448 if (TRANSACTION_EXPR_OUTER (expr))
7449 subcode = GTMA_IS_OUTER;
7450 else if (TRANSACTION_EXPR_RELAXED (expr))
7451 subcode = GTMA_IS_RELAXED;
7452 gimple_transaction_set_subcode (trans_stmt, subcode);
7454 gimplify_seq_add_stmt (pre_p, trans_stmt);
7456 if (temp)
7458 *expr_p = temp;
7459 return GS_OK;
7462 *expr_p = NULL_TREE;
7463 return GS_ALL_DONE;
7466 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7467 expression produces a value to be used as an operand inside a GIMPLE
7468 statement, the value will be stored back in *EXPR_P. This value will
7469 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7470 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7471 emitted in PRE_P and POST_P.
7473 Additionally, this process may overwrite parts of the input
7474 expression during gimplification. Ideally, it should be
7475 possible to do non-destructive gimplification.
7477 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7478 the expression needs to evaluate to a value to be used as
7479 an operand in a GIMPLE statement, this value will be stored in
7480 *EXPR_P on exit. This happens when the caller specifies one
7481 of fb_lvalue or fb_rvalue fallback flags.
7483 PRE_P will contain the sequence of GIMPLE statements corresponding
7484 to the evaluation of EXPR and all the side-effects that must
7485 be executed before the main expression. On exit, the last
7486 statement of PRE_P is the core statement being gimplified. For
7487 instance, when gimplifying 'if (++a)' the last statement in
7488 PRE_P will be 'if (t.1)' where t.1 is the result of
7489 pre-incrementing 'a'.
7491 POST_P will contain the sequence of GIMPLE statements corresponding
7492 to the evaluation of all the side-effects that must be executed
7493 after the main expression. If this is NULL, the post
7494 side-effects are stored at the end of PRE_P.
7496 The reason why the output is split in two is to handle post
7497 side-effects explicitly. In some cases, an expression may have
7498 inner and outer post side-effects which need to be emitted in
7499 an order different from the one given by the recursive
7500 traversal. For instance, for the expression (*p--)++ the post
7501 side-effects of '--' must actually occur *after* the post
7502 side-effects of '++'. However, gimplification will first visit
7503 the inner expression, so if a separate POST sequence was not
7504 used, the resulting sequence would be:
7506 1 t.1 = *p
7507 2 p = p - 1
7508 3 t.2 = t.1 + 1
7509 4 *p = t.2
7511 However, the post-decrement operation in line #2 must not be
7512 evaluated until after the store to *p at line #4, so the
7513 correct sequence should be:
7515 1 t.1 = *p
7516 2 t.2 = t.1 + 1
7517 3 *p = t.2
7518 4 p = p - 1
7520 So, by specifying a separate post queue, it is possible
7521 to emit the post side-effects in the correct order.
7522 If POST_P is NULL, an internal queue will be used. Before
7523 returning to the caller, the sequence POST_P is appended to
7524 the main output sequence PRE_P.
7526 GIMPLE_TEST_F points to a function that takes a tree T and
7527 returns nonzero if T is in the GIMPLE form requested by the
7528 caller. The GIMPLE predicates are in gimple.c.
7530 FALLBACK tells the function what sort of a temporary we want if
7531 gimplification cannot produce an expression that complies with
7532 GIMPLE_TEST_F.
7534 fb_none means that no temporary should be generated
7535 fb_rvalue means that an rvalue is OK to generate
7536 fb_lvalue means that an lvalue is OK to generate
7537 fb_either means that either is OK, but an lvalue is preferable.
7538 fb_mayfail means that gimplification may fail (in which case
7539 GS_ERROR will be returned)
7541 The return value is either GS_ERROR or GS_ALL_DONE, since this
7542 function iterates until EXPR is completely gimplified or an error
7543 occurs. */
7545 enum gimplify_status
7546 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7547 bool (*gimple_test_f) (tree), fallback_t fallback)
7549 tree tmp;
7550 gimple_seq internal_pre = NULL;
7551 gimple_seq internal_post = NULL;
7552 tree save_expr;
7553 bool is_statement;
7554 location_t saved_location;
7555 enum gimplify_status ret;
7556 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7558 save_expr = *expr_p;
7559 if (save_expr == NULL_TREE)
7560 return GS_ALL_DONE;
7562 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7563 is_statement = gimple_test_f == is_gimple_stmt;
7564 if (is_statement)
7565 gcc_assert (pre_p);
7567 /* Consistency checks. */
7568 if (gimple_test_f == is_gimple_reg)
7569 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7570 else if (gimple_test_f == is_gimple_val
7571 || gimple_test_f == is_gimple_call_addr
7572 || gimple_test_f == is_gimple_condexpr
7573 || gimple_test_f == is_gimple_mem_rhs
7574 || gimple_test_f == is_gimple_mem_rhs_or_call
7575 || gimple_test_f == is_gimple_reg_rhs
7576 || gimple_test_f == is_gimple_reg_rhs_or_call
7577 || gimple_test_f == is_gimple_asm_val
7578 || gimple_test_f == is_gimple_mem_ref_addr)
7579 gcc_assert (fallback & fb_rvalue);
7580 else if (gimple_test_f == is_gimple_min_lval
7581 || gimple_test_f == is_gimple_lvalue)
7582 gcc_assert (fallback & fb_lvalue);
7583 else if (gimple_test_f == is_gimple_addressable)
7584 gcc_assert (fallback & fb_either);
7585 else if (gimple_test_f == is_gimple_stmt)
7586 gcc_assert (fallback == fb_none);
7587 else
7589 /* We should have recognized the GIMPLE_TEST_F predicate to
7590 know what kind of fallback to use in case a temporary is
7591 needed to hold the value or address of *EXPR_P. */
7592 gcc_unreachable ();
7595 /* We used to check the predicate here and return immediately if it
7596 succeeds. This is wrong; the design is for gimplification to be
7597 idempotent, and for the predicates to only test for valid forms, not
7598 whether they are fully simplified. */
7599 if (pre_p == NULL)
7600 pre_p = &internal_pre;
7602 if (post_p == NULL)
7603 post_p = &internal_post;
7605 /* Remember the last statements added to PRE_P and POST_P. Every
7606 new statement added by the gimplification helpers needs to be
7607 annotated with location information. To centralize the
7608 responsibility, we remember the last statement that had been
7609 added to both queues before gimplifying *EXPR_P. If
7610 gimplification produces new statements in PRE_P and POST_P, those
7611 statements will be annotated with the same location information
7612 as *EXPR_P. */
7613 pre_last_gsi = gsi_last (*pre_p);
7614 post_last_gsi = gsi_last (*post_p);
7616 saved_location = input_location;
7617 if (save_expr != error_mark_node
7618 && EXPR_HAS_LOCATION (*expr_p))
7619 input_location = EXPR_LOCATION (*expr_p);
7621 /* Loop over the specific gimplifiers until the toplevel node
7622 remains the same. */
7625 /* Strip away as many useless type conversions as possible
7626 at the toplevel. */
7627 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7629 /* Remember the expr. */
7630 save_expr = *expr_p;
7632 /* Die, die, die, my darling. */
7633 if (save_expr == error_mark_node
7634 || (TREE_TYPE (save_expr)
7635 && TREE_TYPE (save_expr) == error_mark_node))
7637 ret = GS_ERROR;
7638 break;
7641 /* Do any language-specific gimplification. */
7642 ret = ((enum gimplify_status)
7643 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7644 if (ret == GS_OK)
7646 if (*expr_p == NULL_TREE)
7647 break;
7648 if (*expr_p != save_expr)
7649 continue;
7651 else if (ret != GS_UNHANDLED)
7652 break;
7654 /* Make sure that all the cases set 'ret' appropriately. */
7655 ret = GS_UNHANDLED;
7656 switch (TREE_CODE (*expr_p))
7658 /* First deal with the special cases. */
7660 case POSTINCREMENT_EXPR:
7661 case POSTDECREMENT_EXPR:
7662 case PREINCREMENT_EXPR:
7663 case PREDECREMENT_EXPR:
7664 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7665 fallback != fb_none,
7666 TREE_TYPE (*expr_p));
7667 break;
7669 case VIEW_CONVERT_EXPR:
7670 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7671 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7673 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7674 post_p, is_gimple_val, fb_rvalue);
7675 recalculate_side_effects (*expr_p);
7676 break;
7678 /* Fallthru. */
7680 case ARRAY_REF:
7681 case ARRAY_RANGE_REF:
7682 case REALPART_EXPR:
7683 case IMAGPART_EXPR:
7684 case COMPONENT_REF:
7685 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7686 fallback ? fallback : fb_rvalue);
7687 break;
7689 case COND_EXPR:
7690 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7692 /* C99 code may assign to an array in a structure value of a
7693 conditional expression, and this has undefined behavior
7694 only on execution, so create a temporary if an lvalue is
7695 required. */
7696 if (fallback == fb_lvalue)
7698 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7699 mark_addressable (*expr_p);
7700 ret = GS_OK;
7702 break;
7704 case CALL_EXPR:
7705 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7707 /* C99 code may assign to an array in a structure returned
7708 from a function, and this has undefined behavior only on
7709 execution, so create a temporary if an lvalue is
7710 required. */
7711 if (fallback == fb_lvalue)
7713 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7714 mark_addressable (*expr_p);
7715 ret = GS_OK;
7717 break;
7719 case TREE_LIST:
7720 gcc_unreachable ();
7722 case COMPOUND_EXPR:
7723 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7724 break;
7726 case COMPOUND_LITERAL_EXPR:
7727 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7728 gimple_test_f, fallback);
7729 break;
7731 case MODIFY_EXPR:
7732 case INIT_EXPR:
7733 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7734 fallback != fb_none);
7735 break;
7737 case TRUTH_ANDIF_EXPR:
7738 case TRUTH_ORIF_EXPR:
7740 /* Preserve the original type of the expression and the
7741 source location of the outer expression. */
7742 tree org_type = TREE_TYPE (*expr_p);
7743 *expr_p = gimple_boolify (*expr_p);
7744 *expr_p = build3_loc (input_location, COND_EXPR,
7745 org_type, *expr_p,
7746 fold_convert_loc
7747 (input_location,
7748 org_type, boolean_true_node),
7749 fold_convert_loc
7750 (input_location,
7751 org_type, boolean_false_node));
7752 ret = GS_OK;
7753 break;
7756 case TRUTH_NOT_EXPR:
7758 tree type = TREE_TYPE (*expr_p);
7759 /* The parsers are careful to generate TRUTH_NOT_EXPR
7760 only with operands that are always zero or one.
7761 We do not fold here but handle the only interesting case
7762 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7763 *expr_p = gimple_boolify (*expr_p);
7764 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7765 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7766 TREE_TYPE (*expr_p),
7767 TREE_OPERAND (*expr_p, 0));
7768 else
7769 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7770 TREE_TYPE (*expr_p),
7771 TREE_OPERAND (*expr_p, 0),
7772 build_int_cst (TREE_TYPE (*expr_p), 1));
7773 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7774 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7775 ret = GS_OK;
7776 break;
7779 case ADDR_EXPR:
7780 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7781 break;
7783 case ANNOTATE_EXPR:
7785 tree cond = TREE_OPERAND (*expr_p, 0);
7786 tree kind = TREE_OPERAND (*expr_p, 1);
7787 tree type = TREE_TYPE (cond);
7788 if (!INTEGRAL_TYPE_P (type))
7790 *expr_p = cond;
7791 ret = GS_OK;
7792 break;
7794 tree tmp = create_tmp_var (type, NULL);
7795 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7796 gcall *call
7797 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7798 gimple_call_set_lhs (call, tmp);
7799 gimplify_seq_add_stmt (pre_p, call);
7800 *expr_p = tmp;
7801 ret = GS_ALL_DONE;
7802 break;
7805 case VA_ARG_EXPR:
7806 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7807 break;
7809 CASE_CONVERT:
7810 if (IS_EMPTY_STMT (*expr_p))
7812 ret = GS_ALL_DONE;
7813 break;
7816 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7817 || fallback == fb_none)
7819 /* Just strip a conversion to void (or in void context) and
7820 try again. */
7821 *expr_p = TREE_OPERAND (*expr_p, 0);
7822 ret = GS_OK;
7823 break;
7826 ret = gimplify_conversion (expr_p);
7827 if (ret == GS_ERROR)
7828 break;
7829 if (*expr_p != save_expr)
7830 break;
7831 /* FALLTHRU */
7833 case FIX_TRUNC_EXPR:
7834 /* unary_expr: ... | '(' cast ')' val | ... */
7835 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7836 is_gimple_val, fb_rvalue);
7837 recalculate_side_effects (*expr_p);
7838 break;
7840 case INDIRECT_REF:
7842 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7843 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7844 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7846 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7847 if (*expr_p != save_expr)
7849 ret = GS_OK;
7850 break;
7853 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7854 is_gimple_reg, fb_rvalue);
7855 if (ret == GS_ERROR)
7856 break;
7858 recalculate_side_effects (*expr_p);
7859 *expr_p = fold_build2_loc (input_location, MEM_REF,
7860 TREE_TYPE (*expr_p),
7861 TREE_OPERAND (*expr_p, 0),
7862 build_int_cst (saved_ptr_type, 0));
7863 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7864 TREE_THIS_NOTRAP (*expr_p) = notrap;
7865 ret = GS_OK;
7866 break;
7869 /* We arrive here through the various re-gimplifcation paths. */
7870 case MEM_REF:
7871 /* First try re-folding the whole thing. */
7872 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7873 TREE_OPERAND (*expr_p, 0),
7874 TREE_OPERAND (*expr_p, 1));
7875 if (tmp)
7877 *expr_p = tmp;
7878 recalculate_side_effects (*expr_p);
7879 ret = GS_OK;
7880 break;
7882 /* Avoid re-gimplifying the address operand if it is already
7883 in suitable form. Re-gimplifying would mark the address
7884 operand addressable. Always gimplify when not in SSA form
7885 as we still may have to gimplify decls with value-exprs. */
7886 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7887 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7889 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7890 is_gimple_mem_ref_addr, fb_rvalue);
7891 if (ret == GS_ERROR)
7892 break;
7894 recalculate_side_effects (*expr_p);
7895 ret = GS_ALL_DONE;
7896 break;
7898 /* Constants need not be gimplified. */
7899 case INTEGER_CST:
7900 case REAL_CST:
7901 case FIXED_CST:
7902 case STRING_CST:
7903 case COMPLEX_CST:
7904 case VECTOR_CST:
7905 /* Drop the overflow flag on constants, we do not want
7906 that in the GIMPLE IL. */
7907 if (TREE_OVERFLOW_P (*expr_p))
7908 *expr_p = drop_tree_overflow (*expr_p);
7909 ret = GS_ALL_DONE;
7910 break;
7912 case CONST_DECL:
7913 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7914 CONST_DECL node. Otherwise the decl is replaceable by its
7915 value. */
7916 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7917 if (fallback & fb_lvalue)
7918 ret = GS_ALL_DONE;
7919 else
7921 *expr_p = DECL_INITIAL (*expr_p);
7922 ret = GS_OK;
7924 break;
7926 case DECL_EXPR:
7927 ret = gimplify_decl_expr (expr_p, pre_p);
7928 break;
7930 case BIND_EXPR:
7931 ret = gimplify_bind_expr (expr_p, pre_p);
7932 break;
7934 case LOOP_EXPR:
7935 ret = gimplify_loop_expr (expr_p, pre_p);
7936 break;
7938 case SWITCH_EXPR:
7939 ret = gimplify_switch_expr (expr_p, pre_p);
7940 break;
7942 case EXIT_EXPR:
7943 ret = gimplify_exit_expr (expr_p);
7944 break;
7946 case GOTO_EXPR:
7947 /* If the target is not LABEL, then it is a computed jump
7948 and the target needs to be gimplified. */
7949 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7951 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7952 NULL, is_gimple_val, fb_rvalue);
7953 if (ret == GS_ERROR)
7954 break;
7956 gimplify_seq_add_stmt (pre_p,
7957 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7958 ret = GS_ALL_DONE;
7959 break;
7961 case PREDICT_EXPR:
7962 gimplify_seq_add_stmt (pre_p,
7963 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7964 PREDICT_EXPR_OUTCOME (*expr_p)));
7965 ret = GS_ALL_DONE;
7966 break;
7968 case LABEL_EXPR:
7969 ret = GS_ALL_DONE;
7970 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7971 == current_function_decl);
7972 gimplify_seq_add_stmt (pre_p,
7973 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7974 break;
7976 case CASE_LABEL_EXPR:
7977 ret = gimplify_case_label_expr (expr_p, pre_p);
7978 break;
7980 case RETURN_EXPR:
7981 ret = gimplify_return_expr (*expr_p, pre_p);
7982 break;
7984 case CONSTRUCTOR:
7985 /* Don't reduce this in place; let gimplify_init_constructor work its
7986 magic. Buf if we're just elaborating this for side effects, just
7987 gimplify any element that has side-effects. */
7988 if (fallback == fb_none)
7990 unsigned HOST_WIDE_INT ix;
7991 tree val;
7992 tree temp = NULL_TREE;
7993 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7994 if (TREE_SIDE_EFFECTS (val))
7995 append_to_statement_list (val, &temp);
7997 *expr_p = temp;
7998 ret = temp ? GS_OK : GS_ALL_DONE;
8000 /* C99 code may assign to an array in a constructed
8001 structure or union, and this has undefined behavior only
8002 on execution, so create a temporary if an lvalue is
8003 required. */
8004 else if (fallback == fb_lvalue)
8006 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8007 mark_addressable (*expr_p);
8008 ret = GS_OK;
8010 else
8011 ret = GS_ALL_DONE;
8012 break;
8014 /* The following are special cases that are not handled by the
8015 original GIMPLE grammar. */
8017 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8018 eliminated. */
8019 case SAVE_EXPR:
8020 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8021 break;
8023 case BIT_FIELD_REF:
8024 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8025 post_p, is_gimple_lvalue, fb_either);
8026 recalculate_side_effects (*expr_p);
8027 break;
8029 case TARGET_MEM_REF:
8031 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8033 if (TMR_BASE (*expr_p))
8034 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8035 post_p, is_gimple_mem_ref_addr, fb_either);
8036 if (TMR_INDEX (*expr_p))
8037 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8038 post_p, is_gimple_val, fb_rvalue);
8039 if (TMR_INDEX2 (*expr_p))
8040 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8041 post_p, is_gimple_val, fb_rvalue);
8042 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8043 ret = MIN (r0, r1);
8045 break;
8047 case NON_LVALUE_EXPR:
8048 /* This should have been stripped above. */
8049 gcc_unreachable ();
8051 case ASM_EXPR:
8052 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8053 break;
8055 case TRY_FINALLY_EXPR:
8056 case TRY_CATCH_EXPR:
8058 gimple_seq eval, cleanup;
8059 gtry *try_;
8061 /* Calls to destructors are generated automatically in FINALLY/CATCH
8062 block. They should have location as UNKNOWN_LOCATION. However,
8063 gimplify_call_expr will reset these call stmts to input_location
8064 if it finds stmt's location is unknown. To prevent resetting for
8065 destructors, we set the input_location to unknown.
8066 Note that this only affects the destructor calls in FINALLY/CATCH
8067 block, and will automatically reset to its original value by the
8068 end of gimplify_expr. */
8069 input_location = UNKNOWN_LOCATION;
8070 eval = cleanup = NULL;
8071 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8072 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8073 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8074 if (gimple_seq_empty_p (cleanup))
8076 gimple_seq_add_seq (pre_p, eval);
8077 ret = GS_ALL_DONE;
8078 break;
8080 try_ = gimple_build_try (eval, cleanup,
8081 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8082 ? GIMPLE_TRY_FINALLY
8083 : GIMPLE_TRY_CATCH);
8084 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8085 gimple_set_location (try_, saved_location);
8086 else
8087 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8088 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8089 gimple_try_set_catch_is_cleanup (try_,
8090 TRY_CATCH_IS_CLEANUP (*expr_p));
8091 gimplify_seq_add_stmt (pre_p, try_);
8092 ret = GS_ALL_DONE;
8093 break;
8096 case CLEANUP_POINT_EXPR:
8097 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8098 break;
8100 case TARGET_EXPR:
8101 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8102 break;
8104 case CATCH_EXPR:
8106 gimple c;
8107 gimple_seq handler = NULL;
8108 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8109 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8110 gimplify_seq_add_stmt (pre_p, c);
8111 ret = GS_ALL_DONE;
8112 break;
8115 case EH_FILTER_EXPR:
8117 gimple ehf;
8118 gimple_seq failure = NULL;
8120 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8121 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8122 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8123 gimplify_seq_add_stmt (pre_p, ehf);
8124 ret = GS_ALL_DONE;
8125 break;
8128 case OBJ_TYPE_REF:
8130 enum gimplify_status r0, r1;
8131 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8132 post_p, is_gimple_val, fb_rvalue);
8133 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8134 post_p, is_gimple_val, fb_rvalue);
8135 TREE_SIDE_EFFECTS (*expr_p) = 0;
8136 ret = MIN (r0, r1);
8138 break;
8140 case LABEL_DECL:
8141 /* We get here when taking the address of a label. We mark
8142 the label as "forced"; meaning it can never be removed and
8143 it is a potential target for any computed goto. */
8144 FORCED_LABEL (*expr_p) = 1;
8145 ret = GS_ALL_DONE;
8146 break;
8148 case STATEMENT_LIST:
8149 ret = gimplify_statement_list (expr_p, pre_p);
8150 break;
8152 case WITH_SIZE_EXPR:
8154 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8155 post_p == &internal_post ? NULL : post_p,
8156 gimple_test_f, fallback);
8157 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8158 is_gimple_val, fb_rvalue);
8159 ret = GS_ALL_DONE;
8161 break;
8163 case VAR_DECL:
8164 case PARM_DECL:
8165 ret = gimplify_var_or_parm_decl (expr_p);
8166 break;
8168 case RESULT_DECL:
8169 /* When within an OpenMP context, notice uses of variables. */
8170 if (gimplify_omp_ctxp)
8171 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8172 ret = GS_ALL_DONE;
8173 break;
8175 case SSA_NAME:
8176 /* Allow callbacks into the gimplifier during optimization. */
8177 ret = GS_ALL_DONE;
8178 break;
8180 case OMP_PARALLEL:
8181 gimplify_omp_parallel (expr_p, pre_p);
8182 ret = GS_ALL_DONE;
8183 break;
8185 case OMP_TASK:
8186 gimplify_omp_task (expr_p, pre_p);
8187 ret = GS_ALL_DONE;
8188 break;
8190 case OMP_FOR:
8191 case OMP_SIMD:
8192 case CILK_SIMD:
8193 case CILK_FOR:
8194 case OMP_DISTRIBUTE:
8195 ret = gimplify_omp_for (expr_p, pre_p);
8196 break;
8198 case OMP_SECTIONS:
8199 case OMP_SINGLE:
8200 case OMP_TARGET:
8201 case OMP_TARGET_DATA:
8202 case OMP_TEAMS:
8203 gimplify_omp_workshare (expr_p, pre_p);
8204 ret = GS_ALL_DONE;
8205 break;
8207 case OMP_TARGET_UPDATE:
8208 gimplify_omp_target_update (expr_p, pre_p);
8209 ret = GS_ALL_DONE;
8210 break;
8212 case OMP_SECTION:
8213 case OMP_MASTER:
8214 case OMP_TASKGROUP:
8215 case OMP_ORDERED:
8216 case OMP_CRITICAL:
8218 gimple_seq body = NULL;
8219 gimple g;
8221 gimplify_and_add (OMP_BODY (*expr_p), &body);
8222 switch (TREE_CODE (*expr_p))
8224 case OMP_SECTION:
8225 g = gimple_build_omp_section (body);
8226 break;
8227 case OMP_MASTER:
8228 g = gimple_build_omp_master (body);
8229 break;
8230 case OMP_TASKGROUP:
8232 gimple_seq cleanup = NULL;
8233 tree fn
8234 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8235 g = gimple_build_call (fn, 0);
8236 gimple_seq_add_stmt (&cleanup, g);
8237 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8238 body = NULL;
8239 gimple_seq_add_stmt (&body, g);
8240 g = gimple_build_omp_taskgroup (body);
8242 break;
8243 case OMP_ORDERED:
8244 g = gimple_build_omp_ordered (body);
8245 break;
8246 case OMP_CRITICAL:
8247 g = gimple_build_omp_critical (body,
8248 OMP_CRITICAL_NAME (*expr_p));
8249 break;
8250 default:
8251 gcc_unreachable ();
8253 gimplify_seq_add_stmt (pre_p, g);
8254 ret = GS_ALL_DONE;
8255 break;
8258 case OMP_ATOMIC:
8259 case OMP_ATOMIC_READ:
8260 case OMP_ATOMIC_CAPTURE_OLD:
8261 case OMP_ATOMIC_CAPTURE_NEW:
8262 ret = gimplify_omp_atomic (expr_p, pre_p);
8263 break;
8265 case TRANSACTION_EXPR:
8266 ret = gimplify_transaction (expr_p, pre_p);
8267 break;
8269 case TRUTH_AND_EXPR:
8270 case TRUTH_OR_EXPR:
8271 case TRUTH_XOR_EXPR:
8273 tree orig_type = TREE_TYPE (*expr_p);
8274 tree new_type, xop0, xop1;
8275 *expr_p = gimple_boolify (*expr_p);
8276 new_type = TREE_TYPE (*expr_p);
8277 if (!useless_type_conversion_p (orig_type, new_type))
8279 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8280 ret = GS_OK;
8281 break;
8284 /* Boolified binary truth expressions are semantically equivalent
8285 to bitwise binary expressions. Canonicalize them to the
8286 bitwise variant. */
8287 switch (TREE_CODE (*expr_p))
8289 case TRUTH_AND_EXPR:
8290 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8291 break;
8292 case TRUTH_OR_EXPR:
8293 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8294 break;
8295 case TRUTH_XOR_EXPR:
8296 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8297 break;
8298 default:
8299 break;
8301 /* Now make sure that operands have compatible type to
8302 expression's new_type. */
8303 xop0 = TREE_OPERAND (*expr_p, 0);
8304 xop1 = TREE_OPERAND (*expr_p, 1);
8305 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8306 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8307 new_type,
8308 xop0);
8309 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8310 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8311 new_type,
8312 xop1);
8313 /* Continue classified as tcc_binary. */
8314 goto expr_2;
8317 case FMA_EXPR:
8318 case VEC_COND_EXPR:
8319 case VEC_PERM_EXPR:
8320 /* Classified as tcc_expression. */
8321 goto expr_3;
8323 case POINTER_PLUS_EXPR:
8325 enum gimplify_status r0, r1;
8326 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8327 post_p, is_gimple_val, fb_rvalue);
8328 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8329 post_p, is_gimple_val, fb_rvalue);
8330 recalculate_side_effects (*expr_p);
8331 ret = MIN (r0, r1);
8332 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8333 after gimplifying operands - this is similar to how
8334 it would be folding all gimplified stmts on creation
8335 to have them canonicalized, which is what we eventually
8336 should do anyway. */
8337 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8338 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8340 *expr_p = build_fold_addr_expr_with_type_loc
8341 (input_location,
8342 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8343 TREE_OPERAND (*expr_p, 0),
8344 fold_convert (ptr_type_node,
8345 TREE_OPERAND (*expr_p, 1))),
8346 TREE_TYPE (*expr_p));
8347 ret = MIN (ret, GS_OK);
8349 break;
8352 case CILK_SYNC_STMT:
8354 if (!fn_contains_cilk_spawn_p (cfun))
8356 error_at (EXPR_LOCATION (*expr_p),
8357 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8358 ret = GS_ERROR;
8360 else
8362 gimplify_cilk_sync (expr_p, pre_p);
8363 ret = GS_ALL_DONE;
8365 break;
8368 default:
8369 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8371 case tcc_comparison:
8372 /* Handle comparison of objects of non scalar mode aggregates
8373 with a call to memcmp. It would be nice to only have to do
8374 this for variable-sized objects, but then we'd have to allow
8375 the same nest of reference nodes we allow for MODIFY_EXPR and
8376 that's too complex.
8378 Compare scalar mode aggregates as scalar mode values. Using
8379 memcmp for them would be very inefficient at best, and is
8380 plain wrong if bitfields are involved. */
8382 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8384 /* Vector comparisons need no boolification. */
8385 if (TREE_CODE (type) == VECTOR_TYPE)
8386 goto expr_2;
8387 else if (!AGGREGATE_TYPE_P (type))
8389 tree org_type = TREE_TYPE (*expr_p);
8390 *expr_p = gimple_boolify (*expr_p);
8391 if (!useless_type_conversion_p (org_type,
8392 TREE_TYPE (*expr_p)))
8394 *expr_p = fold_convert_loc (input_location,
8395 org_type, *expr_p);
8396 ret = GS_OK;
8398 else
8399 goto expr_2;
8401 else if (TYPE_MODE (type) != BLKmode)
8402 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8403 else
8404 ret = gimplify_variable_sized_compare (expr_p);
8406 break;
8409 /* If *EXPR_P does not need to be special-cased, handle it
8410 according to its class. */
8411 case tcc_unary:
8412 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8413 post_p, is_gimple_val, fb_rvalue);
8414 break;
8416 case tcc_binary:
8417 expr_2:
8419 enum gimplify_status r0, r1;
8421 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8422 post_p, is_gimple_val, fb_rvalue);
8423 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8424 post_p, is_gimple_val, fb_rvalue);
8426 ret = MIN (r0, r1);
8427 break;
8430 expr_3:
8432 enum gimplify_status r0, r1, r2;
8434 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8435 post_p, is_gimple_val, fb_rvalue);
8436 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8437 post_p, is_gimple_val, fb_rvalue);
8438 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8439 post_p, is_gimple_val, fb_rvalue);
8441 ret = MIN (MIN (r0, r1), r2);
8442 break;
8445 case tcc_declaration:
8446 case tcc_constant:
8447 ret = GS_ALL_DONE;
8448 goto dont_recalculate;
8450 default:
8451 gcc_unreachable ();
8454 recalculate_side_effects (*expr_p);
8456 dont_recalculate:
8457 break;
8460 gcc_assert (*expr_p || ret != GS_OK);
8462 while (ret == GS_OK);
8464 /* If we encountered an error_mark somewhere nested inside, either
8465 stub out the statement or propagate the error back out. */
8466 if (ret == GS_ERROR)
8468 if (is_statement)
8469 *expr_p = NULL;
8470 goto out;
8473 /* This was only valid as a return value from the langhook, which
8474 we handled. Make sure it doesn't escape from any other context. */
8475 gcc_assert (ret != GS_UNHANDLED);
8477 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8479 /* We aren't looking for a value, and we don't have a valid
8480 statement. If it doesn't have side-effects, throw it away. */
8481 if (!TREE_SIDE_EFFECTS (*expr_p))
8482 *expr_p = NULL;
8483 else if (!TREE_THIS_VOLATILE (*expr_p))
8485 /* This is probably a _REF that contains something nested that
8486 has side effects. Recurse through the operands to find it. */
8487 enum tree_code code = TREE_CODE (*expr_p);
8489 switch (code)
8491 case COMPONENT_REF:
8492 case REALPART_EXPR:
8493 case IMAGPART_EXPR:
8494 case VIEW_CONVERT_EXPR:
8495 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8496 gimple_test_f, fallback);
8497 break;
8499 case ARRAY_REF:
8500 case ARRAY_RANGE_REF:
8501 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8502 gimple_test_f, fallback);
8503 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8504 gimple_test_f, fallback);
8505 break;
8507 default:
8508 /* Anything else with side-effects must be converted to
8509 a valid statement before we get here. */
8510 gcc_unreachable ();
8513 *expr_p = NULL;
8515 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8516 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8518 /* Historically, the compiler has treated a bare reference
8519 to a non-BLKmode volatile lvalue as forcing a load. */
8520 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8522 /* Normally, we do not want to create a temporary for a
8523 TREE_ADDRESSABLE type because such a type should not be
8524 copied by bitwise-assignment. However, we make an
8525 exception here, as all we are doing here is ensuring that
8526 we read the bytes that make up the type. We use
8527 create_tmp_var_raw because create_tmp_var will abort when
8528 given a TREE_ADDRESSABLE type. */
8529 tree tmp = create_tmp_var_raw (type, "vol");
8530 gimple_add_tmp_var (tmp);
8531 gimplify_assign (tmp, *expr_p, pre_p);
8532 *expr_p = NULL;
8534 else
8535 /* We can't do anything useful with a volatile reference to
8536 an incomplete type, so just throw it away. Likewise for
8537 a BLKmode type, since any implicit inner load should
8538 already have been turned into an explicit one by the
8539 gimplification process. */
8540 *expr_p = NULL;
8543 /* If we are gimplifying at the statement level, we're done. Tack
8544 everything together and return. */
8545 if (fallback == fb_none || is_statement)
8547 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8548 it out for GC to reclaim it. */
8549 *expr_p = NULL_TREE;
8551 if (!gimple_seq_empty_p (internal_pre)
8552 || !gimple_seq_empty_p (internal_post))
8554 gimplify_seq_add_seq (&internal_pre, internal_post);
8555 gimplify_seq_add_seq (pre_p, internal_pre);
8558 /* The result of gimplifying *EXPR_P is going to be the last few
8559 statements in *PRE_P and *POST_P. Add location information
8560 to all the statements that were added by the gimplification
8561 helpers. */
8562 if (!gimple_seq_empty_p (*pre_p))
8563 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8565 if (!gimple_seq_empty_p (*post_p))
8566 annotate_all_with_location_after (*post_p, post_last_gsi,
8567 input_location);
8569 goto out;
8572 #ifdef ENABLE_GIMPLE_CHECKING
8573 if (*expr_p)
8575 enum tree_code code = TREE_CODE (*expr_p);
8576 /* These expressions should already be in gimple IR form. */
8577 gcc_assert (code != MODIFY_EXPR
8578 && code != ASM_EXPR
8579 && code != BIND_EXPR
8580 && code != CATCH_EXPR
8581 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8582 && code != EH_FILTER_EXPR
8583 && code != GOTO_EXPR
8584 && code != LABEL_EXPR
8585 && code != LOOP_EXPR
8586 && code != SWITCH_EXPR
8587 && code != TRY_FINALLY_EXPR
8588 && code != OMP_CRITICAL
8589 && code != OMP_FOR
8590 && code != OMP_MASTER
8591 && code != OMP_TASKGROUP
8592 && code != OMP_ORDERED
8593 && code != OMP_PARALLEL
8594 && code != OMP_SECTIONS
8595 && code != OMP_SECTION
8596 && code != OMP_SINGLE);
8598 #endif
8600 /* Otherwise we're gimplifying a subexpression, so the resulting
8601 value is interesting. If it's a valid operand that matches
8602 GIMPLE_TEST_F, we're done. Unless we are handling some
8603 post-effects internally; if that's the case, we need to copy into
8604 a temporary before adding the post-effects to POST_P. */
8605 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8606 goto out;
8608 /* Otherwise, we need to create a new temporary for the gimplified
8609 expression. */
8611 /* We can't return an lvalue if we have an internal postqueue. The
8612 object the lvalue refers to would (probably) be modified by the
8613 postqueue; we need to copy the value out first, which means an
8614 rvalue. */
8615 if ((fallback & fb_lvalue)
8616 && gimple_seq_empty_p (internal_post)
8617 && is_gimple_addressable (*expr_p))
8619 /* An lvalue will do. Take the address of the expression, store it
8620 in a temporary, and replace the expression with an INDIRECT_REF of
8621 that temporary. */
8622 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8623 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8624 *expr_p = build_simple_mem_ref (tmp);
8626 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8628 /* An rvalue will do. Assign the gimplified expression into a
8629 new temporary TMP and replace the original expression with
8630 TMP. First, make sure that the expression has a type so that
8631 it can be assigned into a temporary. */
8632 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8633 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8635 else
8637 #ifdef ENABLE_GIMPLE_CHECKING
8638 if (!(fallback & fb_mayfail))
8640 fprintf (stderr, "gimplification failed:\n");
8641 print_generic_expr (stderr, *expr_p, 0);
8642 debug_tree (*expr_p);
8643 internal_error ("gimplification failed");
8645 #endif
8646 gcc_assert (fallback & fb_mayfail);
8648 /* If this is an asm statement, and the user asked for the
8649 impossible, don't die. Fail and let gimplify_asm_expr
8650 issue an error. */
8651 ret = GS_ERROR;
8652 goto out;
8655 /* Make sure the temporary matches our predicate. */
8656 gcc_assert ((*gimple_test_f) (*expr_p));
8658 if (!gimple_seq_empty_p (internal_post))
8660 annotate_all_with_location (internal_post, input_location);
8661 gimplify_seq_add_seq (pre_p, internal_post);
8664 out:
8665 input_location = saved_location;
8666 return ret;
8669 /* Look through TYPE for variable-sized objects and gimplify each such
8670 size that we find. Add to LIST_P any statements generated. */
8672 void
8673 gimplify_type_sizes (tree type, gimple_seq *list_p)
8675 tree field, t;
8677 if (type == NULL || type == error_mark_node)
8678 return;
8680 /* We first do the main variant, then copy into any other variants. */
8681 type = TYPE_MAIN_VARIANT (type);
8683 /* Avoid infinite recursion. */
8684 if (TYPE_SIZES_GIMPLIFIED (type))
8685 return;
8687 TYPE_SIZES_GIMPLIFIED (type) = 1;
8689 switch (TREE_CODE (type))
8691 case INTEGER_TYPE:
8692 case ENUMERAL_TYPE:
8693 case BOOLEAN_TYPE:
8694 case REAL_TYPE:
8695 case FIXED_POINT_TYPE:
8696 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8697 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8699 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8701 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8702 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8704 break;
8706 case ARRAY_TYPE:
8707 /* These types may not have declarations, so handle them here. */
8708 gimplify_type_sizes (TREE_TYPE (type), list_p);
8709 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8710 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8711 with assigned stack slots, for -O1+ -g they should be tracked
8712 by VTA. */
8713 if (!(TYPE_NAME (type)
8714 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8715 && DECL_IGNORED_P (TYPE_NAME (type)))
8716 && TYPE_DOMAIN (type)
8717 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8719 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8720 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8721 DECL_IGNORED_P (t) = 0;
8722 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8723 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8724 DECL_IGNORED_P (t) = 0;
8726 break;
8728 case RECORD_TYPE:
8729 case UNION_TYPE:
8730 case QUAL_UNION_TYPE:
8731 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8732 if (TREE_CODE (field) == FIELD_DECL)
8734 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8735 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8736 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8737 gimplify_type_sizes (TREE_TYPE (field), list_p);
8739 break;
8741 case POINTER_TYPE:
8742 case REFERENCE_TYPE:
8743 /* We used to recurse on the pointed-to type here, which turned out to
8744 be incorrect because its definition might refer to variables not
8745 yet initialized at this point if a forward declaration is involved.
8747 It was actually useful for anonymous pointed-to types to ensure
8748 that the sizes evaluation dominates every possible later use of the
8749 values. Restricting to such types here would be safe since there
8750 is no possible forward declaration around, but would introduce an
8751 undesirable middle-end semantic to anonymity. We then defer to
8752 front-ends the responsibility of ensuring that the sizes are
8753 evaluated both early and late enough, e.g. by attaching artificial
8754 type declarations to the tree. */
8755 break;
8757 default:
8758 break;
8761 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8762 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8764 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8766 TYPE_SIZE (t) = TYPE_SIZE (type);
8767 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8768 TYPE_SIZES_GIMPLIFIED (t) = 1;
8772 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8773 a size or position, has had all of its SAVE_EXPRs evaluated.
8774 We add any required statements to *STMT_P. */
8776 void
8777 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8779 tree expr = *expr_p;
8781 /* We don't do anything if the value isn't there, is constant, or contains
8782 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8783 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8784 will want to replace it with a new variable, but that will cause problems
8785 if this type is from outside the function. It's OK to have that here. */
8786 if (is_gimple_sizepos (expr))
8787 return;
8789 *expr_p = unshare_expr (expr);
8791 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8794 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8795 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8796 is true, also gimplify the parameters. */
8798 gbind *
8799 gimplify_body (tree fndecl, bool do_parms)
8801 location_t saved_location = input_location;
8802 gimple_seq parm_stmts, seq;
8803 gimple outer_stmt;
8804 gbind *outer_bind;
8805 struct cgraph_node *cgn;
8807 timevar_push (TV_TREE_GIMPLIFY);
8809 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8810 gimplification. */
8811 default_rtl_profile ();
8813 gcc_assert (gimplify_ctxp == NULL);
8814 push_gimplify_context ();
8816 if (flag_openmp)
8818 gcc_assert (gimplify_omp_ctxp == NULL);
8819 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8820 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8823 /* Unshare most shared trees in the body and in that of any nested functions.
8824 It would seem we don't have to do this for nested functions because
8825 they are supposed to be output and then the outer function gimplified
8826 first, but the g++ front end doesn't always do it that way. */
8827 unshare_body (fndecl);
8828 unvisit_body (fndecl);
8830 cgn = cgraph_node::get (fndecl);
8831 if (cgn && cgn->origin)
8832 nonlocal_vlas = new hash_set<tree>;
8834 /* Make sure input_location isn't set to something weird. */
8835 input_location = DECL_SOURCE_LOCATION (fndecl);
8837 /* Resolve callee-copies. This has to be done before processing
8838 the body so that DECL_VALUE_EXPR gets processed correctly. */
8839 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8841 /* Gimplify the function's body. */
8842 seq = NULL;
8843 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8844 outer_stmt = gimple_seq_first_stmt (seq);
8845 if (!outer_stmt)
8847 outer_stmt = gimple_build_nop ();
8848 gimplify_seq_add_stmt (&seq, outer_stmt);
8851 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8852 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8853 if (gimple_code (outer_stmt) == GIMPLE_BIND
8854 && gimple_seq_first (seq) == gimple_seq_last (seq))
8855 outer_bind = as_a <gbind *> (outer_stmt);
8856 else
8857 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8859 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8861 /* If we had callee-copies statements, insert them at the beginning
8862 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8863 if (!gimple_seq_empty_p (parm_stmts))
8865 tree parm;
8867 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8868 gimple_bind_set_body (outer_bind, parm_stmts);
8870 for (parm = DECL_ARGUMENTS (current_function_decl);
8871 parm; parm = DECL_CHAIN (parm))
8872 if (DECL_HAS_VALUE_EXPR_P (parm))
8874 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8875 DECL_IGNORED_P (parm) = 0;
8879 if (nonlocal_vlas)
8881 if (nonlocal_vla_vars)
8883 /* tree-nested.c may later on call declare_vars (..., true);
8884 which relies on BLOCK_VARS chain to be the tail of the
8885 gimple_bind_vars chain. Ensure we don't violate that
8886 assumption. */
8887 if (gimple_bind_block (outer_bind)
8888 == DECL_INITIAL (current_function_decl))
8889 declare_vars (nonlocal_vla_vars, outer_bind, true);
8890 else
8891 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8892 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8893 nonlocal_vla_vars);
8894 nonlocal_vla_vars = NULL_TREE;
8896 delete nonlocal_vlas;
8897 nonlocal_vlas = NULL;
8900 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8902 delete_omp_context (gimplify_omp_ctxp);
8903 gimplify_omp_ctxp = NULL;
8906 pop_gimplify_context (outer_bind);
8907 gcc_assert (gimplify_ctxp == NULL);
8909 #ifdef ENABLE_CHECKING
8910 if (!seen_error ())
8911 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8912 #endif
8914 timevar_pop (TV_TREE_GIMPLIFY);
8915 input_location = saved_location;
8917 return outer_bind;
8920 typedef char *char_p; /* For DEF_VEC_P. */
8922 /* Return whether we should exclude FNDECL from instrumentation. */
8924 static bool
8925 flag_instrument_functions_exclude_p (tree fndecl)
8927 vec<char_p> *v;
8929 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8930 if (v && v->length () > 0)
8932 const char *name;
8933 int i;
8934 char *s;
8936 name = lang_hooks.decl_printable_name (fndecl, 0);
8937 FOR_EACH_VEC_ELT (*v, i, s)
8938 if (strstr (name, s) != NULL)
8939 return true;
8942 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8943 if (v && v->length () > 0)
8945 const char *name;
8946 int i;
8947 char *s;
8949 name = DECL_SOURCE_FILE (fndecl);
8950 FOR_EACH_VEC_ELT (*v, i, s)
8951 if (strstr (name, s) != NULL)
8952 return true;
8955 return false;
8958 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8959 node for the function we want to gimplify.
8961 Return the sequence of GIMPLE statements corresponding to the body
8962 of FNDECL. */
8964 void
8965 gimplify_function_tree (tree fndecl)
8967 tree parm, ret;
8968 gimple_seq seq;
8969 gbind *bind;
8971 gcc_assert (!gimple_body (fndecl));
8973 if (DECL_STRUCT_FUNCTION (fndecl))
8974 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8975 else
8976 push_struct_function (fndecl);
8978 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8980 /* Preliminarily mark non-addressed complex variables as eligible
8981 for promotion to gimple registers. We'll transform their uses
8982 as we find them. */
8983 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8984 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8985 && !TREE_THIS_VOLATILE (parm)
8986 && !needs_to_live_in_memory (parm))
8987 DECL_GIMPLE_REG_P (parm) = 1;
8990 ret = DECL_RESULT (fndecl);
8991 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8992 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8993 && !needs_to_live_in_memory (ret))
8994 DECL_GIMPLE_REG_P (ret) = 1;
8996 bind = gimplify_body (fndecl, true);
8998 /* The tree body of the function is no longer needed, replace it
8999 with the new GIMPLE body. */
9000 seq = NULL;
9001 gimple_seq_add_stmt (&seq, bind);
9002 gimple_set_body (fndecl, seq);
9004 /* If we're instrumenting function entry/exit, then prepend the call to
9005 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9006 catch the exit hook. */
9007 /* ??? Add some way to ignore exceptions for this TFE. */
9008 if (flag_instrument_function_entry_exit
9009 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9010 && !flag_instrument_functions_exclude_p (fndecl))
9012 tree x;
9013 gbind *new_bind;
9014 gimple tf;
9015 gimple_seq cleanup = NULL, body = NULL;
9016 tree tmp_var;
9017 gcall *call;
9019 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9020 call = gimple_build_call (x, 1, integer_zero_node);
9021 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9022 gimple_call_set_lhs (call, tmp_var);
9023 gimplify_seq_add_stmt (&cleanup, call);
9024 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9025 call = gimple_build_call (x, 2,
9026 build_fold_addr_expr (current_function_decl),
9027 tmp_var);
9028 gimplify_seq_add_stmt (&cleanup, call);
9029 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9031 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9032 call = gimple_build_call (x, 1, integer_zero_node);
9033 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9034 gimple_call_set_lhs (call, tmp_var);
9035 gimplify_seq_add_stmt (&body, call);
9036 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9037 call = gimple_build_call (x, 2,
9038 build_fold_addr_expr (current_function_decl),
9039 tmp_var);
9040 gimplify_seq_add_stmt (&body, call);
9041 gimplify_seq_add_stmt (&body, tf);
9042 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9043 /* Clear the block for BIND, since it is no longer directly inside
9044 the function, but within a try block. */
9045 gimple_bind_set_block (bind, NULL);
9047 /* Replace the current function body with the body
9048 wrapped in the try/finally TF. */
9049 seq = NULL;
9050 gimple_seq_add_stmt (&seq, new_bind);
9051 gimple_set_body (fndecl, seq);
9054 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9055 cfun->curr_properties = PROP_gimple_any;
9057 pop_cfun ();
9060 /* Return a dummy expression of type TYPE in order to keep going after an
9061 error. */
9063 static tree
9064 dummy_object (tree type)
9066 tree t = build_int_cst (build_pointer_type (type), 0);
9067 return build2 (MEM_REF, type, t, t);
9070 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9071 builtin function, but a very special sort of operator. */
9073 enum gimplify_status
9074 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9076 tree promoted_type, have_va_type;
9077 tree valist = TREE_OPERAND (*expr_p, 0);
9078 tree type = TREE_TYPE (*expr_p);
9079 tree t;
9080 location_t loc = EXPR_LOCATION (*expr_p);
9082 /* Verify that valist is of the proper type. */
9083 have_va_type = TREE_TYPE (valist);
9084 if (have_va_type == error_mark_node)
9085 return GS_ERROR;
9086 have_va_type = targetm.canonical_va_list_type (have_va_type);
9088 if (have_va_type == NULL_TREE)
9090 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9091 return GS_ERROR;
9094 /* Generate a diagnostic for requesting data of a type that cannot
9095 be passed through `...' due to type promotion at the call site. */
9096 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9097 != type)
9099 static bool gave_help;
9100 bool warned;
9102 /* Unfortunately, this is merely undefined, rather than a constraint
9103 violation, so we cannot make this an error. If this call is never
9104 executed, the program is still strictly conforming. */
9105 warned = warning_at (loc, 0,
9106 "%qT is promoted to %qT when passed through %<...%>",
9107 type, promoted_type);
9108 if (!gave_help && warned)
9110 gave_help = true;
9111 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9112 promoted_type, type);
9115 /* We can, however, treat "undefined" any way we please.
9116 Call abort to encourage the user to fix the program. */
9117 if (warned)
9118 inform (loc, "if this code is reached, the program will abort");
9119 /* Before the abort, allow the evaluation of the va_list
9120 expression to exit or longjmp. */
9121 gimplify_and_add (valist, pre_p);
9122 t = build_call_expr_loc (loc,
9123 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9124 gimplify_and_add (t, pre_p);
9126 /* This is dead code, but go ahead and finish so that the
9127 mode of the result comes out right. */
9128 *expr_p = dummy_object (type);
9129 return GS_ALL_DONE;
9131 else
9133 /* Make it easier for the backends by protecting the valist argument
9134 from multiple evaluations. */
9135 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9137 /* For this case, the backends will be expecting a pointer to
9138 TREE_TYPE (abi), but it's possible we've
9139 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9140 So fix it. */
9141 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9143 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9144 valist = fold_convert_loc (loc, p1,
9145 build_fold_addr_expr_loc (loc, valist));
9148 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9150 else
9151 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9153 if (!targetm.gimplify_va_arg_expr)
9154 /* FIXME: Once most targets are converted we should merely
9155 assert this is non-null. */
9156 return GS_ALL_DONE;
9158 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9159 return GS_OK;
9163 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9165 DST/SRC are the destination and source respectively. You can pass
9166 ungimplified trees in DST or SRC, in which case they will be
9167 converted to a gimple operand if necessary.
9169 This function returns the newly created GIMPLE_ASSIGN tuple. */
9171 gimple
9172 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9174 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9175 gimplify_and_add (t, seq_p);
9176 ggc_free (t);
9177 return gimple_seq_last_stmt (*seq_p);
9180 inline hashval_t
9181 gimplify_hasher::hash (const value_type *p)
9183 tree t = p->val;
9184 return iterative_hash_expr (t, 0);
9187 inline bool
9188 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9190 tree t1 = p1->val;
9191 tree t2 = p2->val;
9192 enum tree_code code = TREE_CODE (t1);
9194 if (TREE_CODE (t2) != code
9195 || TREE_TYPE (t1) != TREE_TYPE (t2))
9196 return false;
9198 if (!operand_equal_p (t1, t2, 0))
9199 return false;
9201 #ifdef ENABLE_CHECKING
9202 /* Only allow them to compare equal if they also hash equal; otherwise
9203 results are nondeterminate, and we fail bootstrap comparison. */
9204 gcc_assert (hash (p1) == hash (p2));
9205 #endif
9207 return true;