* c-c++-common/ubsan/float-cast-overflow-6.c: Add i?86-*-* target.
[official-gcc.git] / gcc / gimplify.c
blob38be0ce9f540398dba07b159e990b9848d501ad5
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<gimple> bind_expr_stack;
128 tree temps;
129 gimple_seq conditional_cleanups;
130 tree exit_label;
131 tree return_temp;
133 vec<tree> case_labels;
134 /* The formal temporary table. Should this be persistent? */
135 hash_table<gimplify_hasher> *temp_htab;
137 int conditions;
138 bool save_stack;
139 bool into_ssa;
140 bool allow_rhs_cond_expr;
141 bool in_cleanup_point_expr;
144 struct gimplify_omp_ctx
146 struct gimplify_omp_ctx *outer_context;
147 splay_tree variables;
148 hash_set<tree> *privatized_types;
149 location_t location;
150 enum omp_clause_default_kind default_kind;
151 enum omp_region_type region_type;
152 bool combined_loop;
153 bool distribute;
156 static struct gimplify_ctx *gimplify_ctxp;
157 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
159 /* Forward declaration. */
160 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
162 /* Shorter alias name for the above function for use in gimplify.c
163 only. */
165 static inline void
166 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
168 gimple_seq_add_stmt_without_update (seq_p, gs);
171 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
172 NULL, a new sequence is allocated. This function is
173 similar to gimple_seq_add_seq, but does not scan the operands.
174 During gimplification, we need to manipulate statement sequences
175 before the def/use vectors have been constructed. */
177 static void
178 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
180 gimple_stmt_iterator si;
182 if (src == NULL)
183 return;
185 si = gsi_last (*dst_p);
186 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
190 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
191 and popping gimplify contexts. */
193 static struct gimplify_ctx *ctx_pool = NULL;
195 /* Return a gimplify context struct from the pool. */
197 static inline struct gimplify_ctx *
198 ctx_alloc (void)
200 struct gimplify_ctx * c = ctx_pool;
202 if (c)
203 ctx_pool = c->prev_context;
204 else
205 c = XNEW (struct gimplify_ctx);
207 memset (c, '\0', sizeof (*c));
208 return c;
211 /* Put gimplify context C back into the pool. */
213 static inline void
214 ctx_free (struct gimplify_ctx *c)
216 c->prev_context = ctx_pool;
217 ctx_pool = c;
220 /* Free allocated ctx stack memory. */
222 void
223 free_gimplify_stack (void)
225 struct gimplify_ctx *c;
227 while ((c = ctx_pool))
229 ctx_pool = c->prev_context;
230 free (c);
235 /* Set up a context for the gimplifier. */
237 void
238 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
240 struct gimplify_ctx *c = ctx_alloc ();
242 c->prev_context = gimplify_ctxp;
243 gimplify_ctxp = c;
244 gimplify_ctxp->into_ssa = in_ssa;
245 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
248 /* Tear down a context for the gimplifier. If BODY is non-null, then
249 put the temporaries into the outer BIND_EXPR. Otherwise, put them
250 in the local_decls.
252 BODY is not a sequence, but the first tuple in a sequence. */
254 void
255 pop_gimplify_context (gimple body)
257 struct gimplify_ctx *c = gimplify_ctxp;
259 gcc_assert (c
260 && (!c->bind_expr_stack.exists ()
261 || c->bind_expr_stack.is_empty ()));
262 c->bind_expr_stack.release ();
263 gimplify_ctxp = c->prev_context;
265 if (body)
266 declare_vars (c->temps, body, false);
267 else
268 record_vars (c->temps);
270 delete c->temp_htab;
271 c->temp_htab = NULL;
272 ctx_free (c);
275 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
277 static void
278 gimple_push_bind_expr (gimple gimple_bind)
280 gimplify_ctxp->bind_expr_stack.reserve (8);
281 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
284 /* Pop the first element off the stack of bindings. */
286 static void
287 gimple_pop_bind_expr (void)
289 gimplify_ctxp->bind_expr_stack.pop ();
292 /* Return the first element of the stack of bindings. */
294 gimple
295 gimple_current_bind_expr (void)
297 return gimplify_ctxp->bind_expr_stack.last ();
300 /* Return the stack of bindings created during gimplification. */
302 vec<gimple>
303 gimple_bind_expr_stack (void)
305 return gimplify_ctxp->bind_expr_stack;
308 /* Return true iff there is a COND_EXPR between us and the innermost
309 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
311 static bool
312 gimple_conditional_context (void)
314 return gimplify_ctxp->conditions > 0;
317 /* Note that we've entered a COND_EXPR. */
319 static void
320 gimple_push_condition (void)
322 #ifdef ENABLE_GIMPLE_CHECKING
323 if (gimplify_ctxp->conditions == 0)
324 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
325 #endif
326 ++(gimplify_ctxp->conditions);
329 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
330 now, add any conditional cleanups we've seen to the prequeue. */
332 static void
333 gimple_pop_condition (gimple_seq *pre_p)
335 int conds = --(gimplify_ctxp->conditions);
337 gcc_assert (conds >= 0);
338 if (conds == 0)
340 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
341 gimplify_ctxp->conditional_cleanups = NULL;
345 /* A stable comparison routine for use with splay trees and DECLs. */
347 static int
348 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
350 tree a = (tree) xa;
351 tree b = (tree) xb;
353 return DECL_UID (a) - DECL_UID (b);
356 /* Create a new omp construct that deals with variable remapping. */
358 static struct gimplify_omp_ctx *
359 new_omp_context (enum omp_region_type region_type)
361 struct gimplify_omp_ctx *c;
363 c = XCNEW (struct gimplify_omp_ctx);
364 c->outer_context = gimplify_omp_ctxp;
365 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
366 c->privatized_types = new hash_set<tree>;
367 c->location = input_location;
368 c->region_type = region_type;
369 if ((region_type & ORT_TASK) == 0)
370 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
371 else
372 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
374 return c;
377 /* Destroy an omp construct that deals with variable remapping. */
379 static void
380 delete_omp_context (struct gimplify_omp_ctx *c)
382 splay_tree_delete (c->variables);
383 delete c->privatized_types;
384 XDELETE (c);
387 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
388 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
390 /* Both gimplify the statement T and append it to *SEQ_P. This function
391 behaves exactly as gimplify_stmt, but you don't have to pass T as a
392 reference. */
394 void
395 gimplify_and_add (tree t, gimple_seq *seq_p)
397 gimplify_stmt (&t, seq_p);
400 /* Gimplify statement T into sequence *SEQ_P, and return the first
401 tuple in the sequence of generated tuples for this statement.
402 Return NULL if gimplifying T produced no tuples. */
404 static gimple
405 gimplify_and_return_first (tree t, gimple_seq *seq_p)
407 gimple_stmt_iterator last = gsi_last (*seq_p);
409 gimplify_and_add (t, seq_p);
411 if (!gsi_end_p (last))
413 gsi_next (&last);
414 return gsi_stmt (last);
416 else
417 return gimple_seq_first_stmt (*seq_p);
420 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
421 LHS, or for a call argument. */
423 static bool
424 is_gimple_mem_rhs (tree t)
426 /* If we're dealing with a renamable type, either source or dest must be
427 a renamed variable. */
428 if (is_gimple_reg_type (TREE_TYPE (t)))
429 return is_gimple_val (t);
430 else
431 return is_gimple_val (t) || is_gimple_lvalue (t);
434 /* Return true if T is a CALL_EXPR or an expression that can be
435 assigned to a temporary. Note that this predicate should only be
436 used during gimplification. See the rationale for this in
437 gimplify_modify_expr. */
439 static bool
440 is_gimple_reg_rhs_or_call (tree t)
442 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
443 || TREE_CODE (t) == CALL_EXPR);
446 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
447 this predicate should only be used during gimplification. See the
448 rationale for this in gimplify_modify_expr. */
450 static bool
451 is_gimple_mem_rhs_or_call (tree t)
453 /* If we're dealing with a renamable type, either source or dest must be
454 a renamed variable. */
455 if (is_gimple_reg_type (TREE_TYPE (t)))
456 return is_gimple_val (t);
457 else
458 return (is_gimple_val (t) || is_gimple_lvalue (t)
459 || TREE_CODE (t) == CALL_EXPR);
462 /* Create a temporary with a name derived from VAL. Subroutine of
463 lookup_tmp_var; nobody else should call this function. */
465 static inline tree
466 create_tmp_from_val (tree val)
468 /* Drop all qualifiers and address-space information from the value type. */
469 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
470 tree var = create_tmp_var (type, get_name (val));
471 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
472 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
473 DECL_GIMPLE_REG_P (var) = 1;
474 return var;
477 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
478 an existing expression temporary. */
480 static tree
481 lookup_tmp_var (tree val, bool is_formal)
483 tree ret;
485 /* If not optimizing, never really reuse a temporary. local-alloc
486 won't allocate any variable that is used in more than one basic
487 block, which means it will go into memory, causing much extra
488 work in reload and final and poorer code generation, outweighing
489 the extra memory allocation here. */
490 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
491 ret = create_tmp_from_val (val);
492 else
494 elt_t elt, *elt_p;
495 elt_t **slot;
497 elt.val = val;
498 if (!gimplify_ctxp->temp_htab)
499 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
500 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
501 if (*slot == NULL)
503 elt_p = XNEW (elt_t);
504 elt_p->val = val;
505 elt_p->temp = ret = create_tmp_from_val (val);
506 *slot = elt_p;
508 else
510 elt_p = *slot;
511 ret = elt_p->temp;
515 return ret;
518 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
520 static tree
521 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
522 bool is_formal)
524 tree t, mod;
526 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
527 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
528 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
529 fb_rvalue);
531 if (gimplify_ctxp->into_ssa
532 && is_gimple_reg_type (TREE_TYPE (val)))
533 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
534 else
535 t = lookup_tmp_var (val, is_formal);
537 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
539 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
541 /* gimplify_modify_expr might want to reduce this further. */
542 gimplify_and_add (mod, pre_p);
543 ggc_free (mod);
545 return t;
548 /* Return a formal temporary variable initialized with VAL. PRE_P is as
549 in gimplify_expr. Only use this function if:
551 1) The value of the unfactored expression represented by VAL will not
552 change between the initialization and use of the temporary, and
553 2) The temporary will not be otherwise modified.
555 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
556 and #2 means it is inappropriate for && temps.
558 For other cases, use get_initialized_tmp_var instead. */
560 tree
561 get_formal_tmp_var (tree val, gimple_seq *pre_p)
563 return internal_get_tmp_var (val, pre_p, NULL, true);
566 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
567 are as in gimplify_expr. */
569 tree
570 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
572 return internal_get_tmp_var (val, pre_p, post_p, false);
575 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
576 generate debug info for them; otherwise don't. */
578 void
579 declare_vars (tree vars, gimple scope, bool debug_info)
581 tree last = vars;
582 if (last)
584 tree temps, block;
586 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
588 temps = nreverse (last);
590 block = gimple_bind_block (scope);
591 gcc_assert (!block || TREE_CODE (block) == BLOCK);
592 if (!block || !debug_info)
594 DECL_CHAIN (last) = gimple_bind_vars (scope);
595 gimple_bind_set_vars (scope, temps);
597 else
599 /* We need to attach the nodes both to the BIND_EXPR and to its
600 associated BLOCK for debugging purposes. The key point here
601 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
602 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
603 if (BLOCK_VARS (block))
604 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
605 else
607 gimple_bind_set_vars (scope,
608 chainon (gimple_bind_vars (scope), temps));
609 BLOCK_VARS (block) = temps;
615 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
616 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
617 no such upper bound can be obtained. */
619 static void
620 force_constant_size (tree var)
622 /* The only attempt we make is by querying the maximum size of objects
623 of the variable's type. */
625 HOST_WIDE_INT max_size;
627 gcc_assert (TREE_CODE (var) == VAR_DECL);
629 max_size = max_int_size_in_bytes (TREE_TYPE (var));
631 gcc_assert (max_size >= 0);
633 DECL_SIZE_UNIT (var)
634 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
635 DECL_SIZE (var)
636 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
639 /* Push the temporary variable TMP into the current binding. */
641 void
642 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
644 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
646 /* Later processing assumes that the object size is constant, which might
647 not be true at this point. Force the use of a constant upper bound in
648 this case. */
649 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
650 force_constant_size (tmp);
652 DECL_CONTEXT (tmp) = fn->decl;
653 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
655 record_vars_into (tmp, fn->decl);
658 /* Push the temporary variable TMP into the current binding. */
660 void
661 gimple_add_tmp_var (tree tmp)
663 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
665 /* Later processing assumes that the object size is constant, which might
666 not be true at this point. Force the use of a constant upper bound in
667 this case. */
668 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
669 force_constant_size (tmp);
671 DECL_CONTEXT (tmp) = current_function_decl;
672 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
674 if (gimplify_ctxp)
676 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
677 gimplify_ctxp->temps = tmp;
679 /* Mark temporaries local within the nearest enclosing parallel. */
680 if (gimplify_omp_ctxp)
682 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
683 while (ctx
684 && (ctx->region_type == ORT_WORKSHARE
685 || ctx->region_type == ORT_SIMD))
686 ctx = ctx->outer_context;
687 if (ctx)
688 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
691 else if (cfun)
692 record_vars (tmp);
693 else
695 gimple_seq body_seq;
697 /* This case is for nested functions. We need to expose the locals
698 they create. */
699 body_seq = gimple_body (current_function_decl);
700 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
706 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
707 nodes that are referenced more than once in GENERIC functions. This is
708 necessary because gimplification (translation into GIMPLE) is performed
709 by modifying tree nodes in-place, so gimplication of a shared node in a
710 first context could generate an invalid GIMPLE form in a second context.
712 This is achieved with a simple mark/copy/unmark algorithm that walks the
713 GENERIC representation top-down, marks nodes with TREE_VISITED the first
714 time it encounters them, duplicates them if they already have TREE_VISITED
715 set, and finally removes the TREE_VISITED marks it has set.
717 The algorithm works only at the function level, i.e. it generates a GENERIC
718 representation of a function with no nodes shared within the function when
719 passed a GENERIC function (except for nodes that are allowed to be shared).
721 At the global level, it is also necessary to unshare tree nodes that are
722 referenced in more than one function, for the same aforementioned reason.
723 This requires some cooperation from the front-end. There are 2 strategies:
725 1. Manual unsharing. The front-end needs to call unshare_expr on every
726 expression that might end up being shared across functions.
728 2. Deep unsharing. This is an extension of regular unsharing. Instead
729 of calling unshare_expr on expressions that might be shared across
730 functions, the front-end pre-marks them with TREE_VISITED. This will
731 ensure that they are unshared on the first reference within functions
732 when the regular unsharing algorithm runs. The counterpart is that
733 this algorithm must look deeper than for manual unsharing, which is
734 specified by LANG_HOOKS_DEEP_UNSHARING.
736 If there are only few specific cases of node sharing across functions, it is
737 probably easier for a front-end to unshare the expressions manually. On the
738 contrary, if the expressions generated at the global level are as widespread
739 as expressions generated within functions, deep unsharing is very likely the
740 way to go. */
742 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
743 These nodes model computations that must be done once. If we were to
744 unshare something like SAVE_EXPR(i++), the gimplification process would
745 create wrong code. However, if DATA is non-null, it must hold a pointer
746 set that is used to unshare the subtrees of these nodes. */
748 static tree
749 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
751 tree t = *tp;
752 enum tree_code code = TREE_CODE (t);
754 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
755 copy their subtrees if we can make sure to do it only once. */
756 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
758 if (data && !((hash_set<tree> *)data)->add (t))
760 else
761 *walk_subtrees = 0;
764 /* Stop at types, decls, constants like copy_tree_r. */
765 else if (TREE_CODE_CLASS (code) == tcc_type
766 || TREE_CODE_CLASS (code) == tcc_declaration
767 || TREE_CODE_CLASS (code) == tcc_constant
768 /* We can't do anything sensible with a BLOCK used as an
769 expression, but we also can't just die when we see it
770 because of non-expression uses. So we avert our eyes
771 and cross our fingers. Silly Java. */
772 || code == BLOCK)
773 *walk_subtrees = 0;
775 /* Cope with the statement expression extension. */
776 else if (code == STATEMENT_LIST)
779 /* Leave the bulk of the work to copy_tree_r itself. */
780 else
781 copy_tree_r (tp, walk_subtrees, NULL);
783 return NULL_TREE;
786 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
787 If *TP has been visited already, then *TP is deeply copied by calling
788 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
790 static tree
791 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
793 tree t = *tp;
794 enum tree_code code = TREE_CODE (t);
796 /* Skip types, decls, and constants. But we do want to look at their
797 types and the bounds of types. Mark them as visited so we properly
798 unmark their subtrees on the unmark pass. If we've already seen them,
799 don't look down further. */
800 if (TREE_CODE_CLASS (code) == tcc_type
801 || TREE_CODE_CLASS (code) == tcc_declaration
802 || TREE_CODE_CLASS (code) == tcc_constant)
804 if (TREE_VISITED (t))
805 *walk_subtrees = 0;
806 else
807 TREE_VISITED (t) = 1;
810 /* If this node has been visited already, unshare it and don't look
811 any deeper. */
812 else if (TREE_VISITED (t))
814 walk_tree (tp, mostly_copy_tree_r, data, NULL);
815 *walk_subtrees = 0;
818 /* Otherwise, mark the node as visited and keep looking. */
819 else
820 TREE_VISITED (t) = 1;
822 return NULL_TREE;
825 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
826 copy_if_shared_r callback unmodified. */
828 static inline void
829 copy_if_shared (tree *tp, void *data)
831 walk_tree (tp, copy_if_shared_r, data, NULL);
834 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
835 any nested functions. */
837 static void
838 unshare_body (tree fndecl)
840 struct cgraph_node *cgn = cgraph_node::get (fndecl);
841 /* If the language requires deep unsharing, we need a pointer set to make
842 sure we don't repeatedly unshare subtrees of unshareable nodes. */
843 hash_set<tree> *visited
844 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
846 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
847 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
848 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
850 delete visited;
852 if (cgn)
853 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
854 unshare_body (cgn->decl);
857 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
858 Subtrees are walked until the first unvisited node is encountered. */
860 static tree
861 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
863 tree t = *tp;
865 /* If this node has been visited, unmark it and keep looking. */
866 if (TREE_VISITED (t))
867 TREE_VISITED (t) = 0;
869 /* Otherwise, don't look any deeper. */
870 else
871 *walk_subtrees = 0;
873 return NULL_TREE;
876 /* Unmark the visited trees rooted at *TP. */
878 static inline void
879 unmark_visited (tree *tp)
881 walk_tree (tp, unmark_visited_r, NULL, NULL);
884 /* Likewise, but mark all trees as not visited. */
886 static void
887 unvisit_body (tree fndecl)
889 struct cgraph_node *cgn = cgraph_node::get (fndecl);
891 unmark_visited (&DECL_SAVED_TREE (fndecl));
892 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
893 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
895 if (cgn)
896 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
897 unvisit_body (cgn->decl);
900 /* Unconditionally make an unshared copy of EXPR. This is used when using
901 stored expressions which span multiple functions, such as BINFO_VTABLE,
902 as the normal unsharing process can't tell that they're shared. */
904 tree
905 unshare_expr (tree expr)
907 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
908 return expr;
911 /* Worker for unshare_expr_without_location. */
913 static tree
914 prune_expr_location (tree *tp, int *walk_subtrees, void *)
916 if (EXPR_P (*tp))
917 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
918 else
919 *walk_subtrees = 0;
920 return NULL_TREE;
923 /* Similar to unshare_expr but also prune all expression locations
924 from EXPR. */
926 tree
927 unshare_expr_without_location (tree expr)
929 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
930 if (EXPR_P (expr))
931 walk_tree (&expr, prune_expr_location, NULL, NULL);
932 return expr;
935 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
936 contain statements and have a value. Assign its value to a temporary
937 and give it void_type_node. Return the temporary, or NULL_TREE if
938 WRAPPER was already void. */
940 tree
941 voidify_wrapper_expr (tree wrapper, tree temp)
943 tree type = TREE_TYPE (wrapper);
944 if (type && !VOID_TYPE_P (type))
946 tree *p;
948 /* Set p to point to the body of the wrapper. Loop until we find
949 something that isn't a wrapper. */
950 for (p = &wrapper; p && *p; )
952 switch (TREE_CODE (*p))
954 case BIND_EXPR:
955 TREE_SIDE_EFFECTS (*p) = 1;
956 TREE_TYPE (*p) = void_type_node;
957 /* For a BIND_EXPR, the body is operand 1. */
958 p = &BIND_EXPR_BODY (*p);
959 break;
961 case CLEANUP_POINT_EXPR:
962 case TRY_FINALLY_EXPR:
963 case TRY_CATCH_EXPR:
964 TREE_SIDE_EFFECTS (*p) = 1;
965 TREE_TYPE (*p) = void_type_node;
966 p = &TREE_OPERAND (*p, 0);
967 break;
969 case STATEMENT_LIST:
971 tree_stmt_iterator i = tsi_last (*p);
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
974 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
976 break;
978 case COMPOUND_EXPR:
979 /* Advance to the last statement. Set all container types to
980 void. */
981 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
983 TREE_SIDE_EFFECTS (*p) = 1;
984 TREE_TYPE (*p) = void_type_node;
986 break;
988 case TRANSACTION_EXPR:
989 TREE_SIDE_EFFECTS (*p) = 1;
990 TREE_TYPE (*p) = void_type_node;
991 p = &TRANSACTION_EXPR_BODY (*p);
992 break;
994 default:
995 /* Assume that any tree upon which voidify_wrapper_expr is
996 directly called is a wrapper, and that its body is op0. */
997 if (p == &wrapper)
999 TREE_SIDE_EFFECTS (*p) = 1;
1000 TREE_TYPE (*p) = void_type_node;
1001 p = &TREE_OPERAND (*p, 0);
1002 break;
1004 goto out;
1008 out:
1009 if (p == NULL || IS_EMPTY_STMT (*p))
1010 temp = NULL_TREE;
1011 else if (temp)
1013 /* The wrapper is on the RHS of an assignment that we're pushing
1014 down. */
1015 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1016 || TREE_CODE (temp) == MODIFY_EXPR);
1017 TREE_OPERAND (temp, 1) = *p;
1018 *p = temp;
1020 else
1022 temp = create_tmp_var (type, "retval");
1023 *p = build2 (INIT_EXPR, type, temp, *p);
1026 return temp;
1029 return NULL_TREE;
1032 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1033 a temporary through which they communicate. */
1035 static void
1036 build_stack_save_restore (gimple *save, gimple *restore)
1038 tree tmp_var;
1040 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1041 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1042 gimple_call_set_lhs (*save, tmp_var);
1044 *restore
1045 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1046 1, tmp_var);
1049 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1051 static enum gimplify_status
1052 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1054 tree bind_expr = *expr_p;
1055 bool old_save_stack = gimplify_ctxp->save_stack;
1056 tree t;
1057 gimple gimple_bind;
1058 gimple_seq body, cleanup;
1059 gimple stack_save;
1060 location_t start_locus = 0, end_locus = 0;
1062 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1064 /* Mark variables seen in this bind expr. */
1065 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1067 if (TREE_CODE (t) == VAR_DECL)
1069 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1071 /* Mark variable as local. */
1072 if (ctx && !DECL_EXTERNAL (t)
1073 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1074 || splay_tree_lookup (ctx->variables,
1075 (splay_tree_key) t) == NULL))
1077 if (ctx->region_type == ORT_SIMD
1078 && TREE_ADDRESSABLE (t)
1079 && !TREE_STATIC (t))
1080 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1081 else
1082 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1085 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1087 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1088 cfun->has_local_explicit_reg_vars = true;
1091 /* Preliminarily mark non-addressed complex variables as eligible
1092 for promotion to gimple registers. We'll transform their uses
1093 as we find them. */
1094 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1095 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1096 && !TREE_THIS_VOLATILE (t)
1097 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1098 && !needs_to_live_in_memory (t))
1099 DECL_GIMPLE_REG_P (t) = 1;
1102 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1103 BIND_EXPR_BLOCK (bind_expr));
1104 gimple_push_bind_expr (gimple_bind);
1106 gimplify_ctxp->save_stack = false;
1108 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1109 body = NULL;
1110 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1111 gimple_bind_set_body (gimple_bind, body);
1113 /* Source location wise, the cleanup code (stack_restore and clobbers)
1114 belongs to the end of the block, so propagate what we have. The
1115 stack_save operation belongs to the beginning of block, which we can
1116 infer from the bind_expr directly if the block has no explicit
1117 assignment. */
1118 if (BIND_EXPR_BLOCK (bind_expr))
1120 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1121 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1123 if (start_locus == 0)
1124 start_locus = EXPR_LOCATION (bind_expr);
1126 cleanup = NULL;
1127 stack_save = NULL;
1128 if (gimplify_ctxp->save_stack)
1130 gimple stack_restore;
1132 /* Save stack on entry and restore it on exit. Add a try_finally
1133 block to achieve this. */
1134 build_stack_save_restore (&stack_save, &stack_restore);
1136 gimple_set_location (stack_save, start_locus);
1137 gimple_set_location (stack_restore, end_locus);
1139 gimplify_seq_add_stmt (&cleanup, stack_restore);
1142 /* Add clobbers for all variables that go out of scope. */
1143 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1145 if (TREE_CODE (t) == VAR_DECL
1146 && !is_global_var (t)
1147 && DECL_CONTEXT (t) == current_function_decl
1148 && !DECL_HARD_REGISTER (t)
1149 && !TREE_THIS_VOLATILE (t)
1150 && !DECL_HAS_VALUE_EXPR_P (t)
1151 /* Only care for variables that have to be in memory. Others
1152 will be rewritten into SSA names, hence moved to the top-level. */
1153 && !is_gimple_reg (t)
1154 && flag_stack_reuse != SR_NONE)
1156 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1157 gimple clobber_stmt;
1158 TREE_THIS_VOLATILE (clobber) = 1;
1159 clobber_stmt = gimple_build_assign (t, clobber);
1160 gimple_set_location (clobber_stmt, end_locus);
1161 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1165 if (cleanup)
1167 gimple gs;
1168 gimple_seq new_body;
1170 new_body = NULL;
1171 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1172 GIMPLE_TRY_FINALLY);
1174 if (stack_save)
1175 gimplify_seq_add_stmt (&new_body, stack_save);
1176 gimplify_seq_add_stmt (&new_body, gs);
1177 gimple_bind_set_body (gimple_bind, new_body);
1180 gimplify_ctxp->save_stack = old_save_stack;
1181 gimple_pop_bind_expr ();
1183 gimplify_seq_add_stmt (pre_p, gimple_bind);
1185 if (temp)
1187 *expr_p = temp;
1188 return GS_OK;
1191 *expr_p = NULL_TREE;
1192 return GS_ALL_DONE;
1195 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1196 GIMPLE value, it is assigned to a new temporary and the statement is
1197 re-written to return the temporary.
1199 PRE_P points to the sequence where side effects that must happen before
1200 STMT should be stored. */
1202 static enum gimplify_status
1203 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1205 gimple ret;
1206 tree ret_expr = TREE_OPERAND (stmt, 0);
1207 tree result_decl, result;
1209 if (ret_expr == error_mark_node)
1210 return GS_ERROR;
1212 /* Implicit _Cilk_sync must be inserted right before any return statement
1213 if there is a _Cilk_spawn in the function. If the user has provided a
1214 _Cilk_sync, the optimizer should remove this duplicate one. */
1215 if (fn_contains_cilk_spawn_p (cfun))
1217 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1218 gimplify_and_add (impl_sync, pre_p);
1221 if (!ret_expr
1222 || TREE_CODE (ret_expr) == RESULT_DECL
1223 || ret_expr == error_mark_node)
1225 gimple ret = gimple_build_return (ret_expr);
1226 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1227 gimplify_seq_add_stmt (pre_p, ret);
1228 return GS_ALL_DONE;
1231 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1232 result_decl = NULL_TREE;
1233 else
1235 result_decl = TREE_OPERAND (ret_expr, 0);
1237 /* See through a return by reference. */
1238 if (TREE_CODE (result_decl) == INDIRECT_REF)
1239 result_decl = TREE_OPERAND (result_decl, 0);
1241 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1242 || TREE_CODE (ret_expr) == INIT_EXPR)
1243 && TREE_CODE (result_decl) == RESULT_DECL);
1246 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1247 Recall that aggregate_value_p is FALSE for any aggregate type that is
1248 returned in registers. If we're returning values in registers, then
1249 we don't want to extend the lifetime of the RESULT_DECL, particularly
1250 across another call. In addition, for those aggregates for which
1251 hard_function_value generates a PARALLEL, we'll die during normal
1252 expansion of structure assignments; there's special code in expand_return
1253 to handle this case that does not exist in expand_expr. */
1254 if (!result_decl)
1255 result = NULL_TREE;
1256 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1258 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1260 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1261 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1262 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1263 should be effectively allocated by the caller, i.e. all calls to
1264 this function must be subject to the Return Slot Optimization. */
1265 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1266 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1268 result = result_decl;
1270 else if (gimplify_ctxp->return_temp)
1271 result = gimplify_ctxp->return_temp;
1272 else
1274 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1276 /* ??? With complex control flow (usually involving abnormal edges),
1277 we can wind up warning about an uninitialized value for this. Due
1278 to how this variable is constructed and initialized, this is never
1279 true. Give up and never warn. */
1280 TREE_NO_WARNING (result) = 1;
1282 gimplify_ctxp->return_temp = result;
1285 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1286 Then gimplify the whole thing. */
1287 if (result != result_decl)
1288 TREE_OPERAND (ret_expr, 0) = result;
1290 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1292 ret = gimple_build_return (result);
1293 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1294 gimplify_seq_add_stmt (pre_p, ret);
1296 return GS_ALL_DONE;
1299 /* Gimplify a variable-length array DECL. */
1301 static void
1302 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1304 /* This is a variable-sized decl. Simplify its size and mark it
1305 for deferred expansion. */
1306 tree t, addr, ptr_type;
1308 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1309 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1311 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1312 if (DECL_HAS_VALUE_EXPR_P (decl))
1313 return;
1315 /* All occurrences of this decl in final gimplified code will be
1316 replaced by indirection. Setting DECL_VALUE_EXPR does two
1317 things: First, it lets the rest of the gimplifier know what
1318 replacement to use. Second, it lets the debug info know
1319 where to find the value. */
1320 ptr_type = build_pointer_type (TREE_TYPE (decl));
1321 addr = create_tmp_var (ptr_type, get_name (decl));
1322 DECL_IGNORED_P (addr) = 0;
1323 t = build_fold_indirect_ref (addr);
1324 TREE_THIS_NOTRAP (t) = 1;
1325 SET_DECL_VALUE_EXPR (decl, t);
1326 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1328 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1329 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1330 size_int (DECL_ALIGN (decl)));
1331 /* The call has been built for a variable-sized object. */
1332 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1333 t = fold_convert (ptr_type, t);
1334 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1336 gimplify_and_add (t, seq_p);
1338 /* Indicate that we need to restore the stack level when the
1339 enclosing BIND_EXPR is exited. */
1340 gimplify_ctxp->save_stack = true;
1343 /* A helper function to be called via walk_tree. Mark all labels under *TP
1344 as being forced. To be called for DECL_INITIAL of static variables. */
1346 static tree
1347 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1349 if (TYPE_P (*tp))
1350 *walk_subtrees = 0;
1351 if (TREE_CODE (*tp) == LABEL_DECL)
1352 FORCED_LABEL (*tp) = 1;
1354 return NULL_TREE;
1357 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1358 and initialization explicit. */
1360 static enum gimplify_status
1361 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1363 tree stmt = *stmt_p;
1364 tree decl = DECL_EXPR_DECL (stmt);
1366 *stmt_p = NULL_TREE;
1368 if (TREE_TYPE (decl) == error_mark_node)
1369 return GS_ERROR;
1371 if ((TREE_CODE (decl) == TYPE_DECL
1372 || TREE_CODE (decl) == VAR_DECL)
1373 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1374 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1376 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1377 in case its size expressions contain problematic nodes like CALL_EXPR. */
1378 if (TREE_CODE (decl) == TYPE_DECL
1379 && DECL_ORIGINAL_TYPE (decl)
1380 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1381 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1383 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1385 tree init = DECL_INITIAL (decl);
1387 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1388 || (!TREE_STATIC (decl)
1389 && flag_stack_check == GENERIC_STACK_CHECK
1390 && compare_tree_int (DECL_SIZE_UNIT (decl),
1391 STACK_CHECK_MAX_VAR_SIZE) > 0))
1392 gimplify_vla_decl (decl, seq_p);
1394 /* Some front ends do not explicitly declare all anonymous
1395 artificial variables. We compensate here by declaring the
1396 variables, though it would be better if the front ends would
1397 explicitly declare them. */
1398 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1399 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1400 gimple_add_tmp_var (decl);
1402 if (init && init != error_mark_node)
1404 if (!TREE_STATIC (decl))
1406 DECL_INITIAL (decl) = NULL_TREE;
1407 init = build2 (INIT_EXPR, void_type_node, decl, init);
1408 gimplify_and_add (init, seq_p);
1409 ggc_free (init);
1411 else
1412 /* We must still examine initializers for static variables
1413 as they may contain a label address. */
1414 walk_tree (&init, force_labels_r, NULL, NULL);
1418 return GS_ALL_DONE;
1421 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1422 and replacing the LOOP_EXPR with goto, but if the loop contains an
1423 EXIT_EXPR, we need to append a label for it to jump to. */
1425 static enum gimplify_status
1426 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1428 tree saved_label = gimplify_ctxp->exit_label;
1429 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1431 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1433 gimplify_ctxp->exit_label = NULL_TREE;
1435 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1437 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1439 if (gimplify_ctxp->exit_label)
1440 gimplify_seq_add_stmt (pre_p,
1441 gimple_build_label (gimplify_ctxp->exit_label));
1443 gimplify_ctxp->exit_label = saved_label;
1445 *expr_p = NULL;
1446 return GS_ALL_DONE;
1449 /* Gimplify a statement list onto a sequence. These may be created either
1450 by an enlightened front-end, or by shortcut_cond_expr. */
1452 static enum gimplify_status
1453 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1455 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1457 tree_stmt_iterator i = tsi_start (*expr_p);
1459 while (!tsi_end_p (i))
1461 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1462 tsi_delink (&i);
1465 if (temp)
1467 *expr_p = temp;
1468 return GS_OK;
1471 return GS_ALL_DONE;
1475 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1476 branch to. */
1478 static enum gimplify_status
1479 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1481 tree switch_expr = *expr_p;
1482 gimple_seq switch_body_seq = NULL;
1483 enum gimplify_status ret;
1484 tree index_type = TREE_TYPE (switch_expr);
1485 if (index_type == NULL_TREE)
1486 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1488 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1489 fb_rvalue);
1490 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1491 return ret;
1493 if (SWITCH_BODY (switch_expr))
1495 vec<tree> labels;
1496 vec<tree> saved_labels;
1497 tree default_case = NULL_TREE;
1498 gimple gimple_switch;
1500 /* If someone can be bothered to fill in the labels, they can
1501 be bothered to null out the body too. */
1502 gcc_assert (!SWITCH_LABELS (switch_expr));
1504 /* Save old labels, get new ones from body, then restore the old
1505 labels. Save all the things from the switch body to append after. */
1506 saved_labels = gimplify_ctxp->case_labels;
1507 gimplify_ctxp->case_labels.create (8);
1509 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1510 labels = gimplify_ctxp->case_labels;
1511 gimplify_ctxp->case_labels = saved_labels;
1513 preprocess_case_label_vec_for_gimple (labels, index_type,
1514 &default_case);
1516 if (!default_case)
1518 gimple new_default;
1520 default_case
1521 = build_case_label (NULL_TREE, NULL_TREE,
1522 create_artificial_label (UNKNOWN_LOCATION));
1523 new_default = gimple_build_label (CASE_LABEL (default_case));
1524 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1527 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1528 default_case, labels);
1529 gimplify_seq_add_stmt (pre_p, gimple_switch);
1530 gimplify_seq_add_seq (pre_p, switch_body_seq);
1531 labels.release ();
1533 else
1534 gcc_assert (SWITCH_LABELS (switch_expr));
1536 return GS_ALL_DONE;
1539 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1541 static enum gimplify_status
1542 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1544 struct gimplify_ctx *ctxp;
1545 gimple gimple_label;
1547 /* Invalid 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 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1555 ctxp->case_labels.safe_push (*expr_p);
1556 gimplify_seq_add_stmt (pre_p, gimple_label);
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 return GS_OK;
1745 /* Nonlocal VLAs seen in the current function. */
1746 static hash_set<tree> *nonlocal_vlas;
1748 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1749 static tree nonlocal_vla_vars;
1751 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1752 DECL_VALUE_EXPR, and it's worth re-examining things. */
1754 static enum gimplify_status
1755 gimplify_var_or_parm_decl (tree *expr_p)
1757 tree decl = *expr_p;
1759 /* ??? If this is a local variable, and it has not been seen in any
1760 outer BIND_EXPR, then it's probably the result of a duplicate
1761 declaration, for which we've already issued an error. It would
1762 be really nice if the front end wouldn't leak these at all.
1763 Currently the only known culprit is C++ destructors, as seen
1764 in g++.old-deja/g++.jason/binding.C. */
1765 if (TREE_CODE (decl) == VAR_DECL
1766 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1767 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1768 && decl_function_context (decl) == current_function_decl)
1770 gcc_assert (seen_error ());
1771 return GS_ERROR;
1774 /* When within an OpenMP context, notice uses of variables. */
1775 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1776 return GS_ALL_DONE;
1778 /* If the decl is an alias for another expression, substitute it now. */
1779 if (DECL_HAS_VALUE_EXPR_P (decl))
1781 tree value_expr = DECL_VALUE_EXPR (decl);
1783 /* For referenced nonlocal VLAs add a decl for debugging purposes
1784 to the current function. */
1785 if (TREE_CODE (decl) == VAR_DECL
1786 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1787 && nonlocal_vlas != NULL
1788 && TREE_CODE (value_expr) == INDIRECT_REF
1789 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1790 && decl_function_context (decl) != current_function_decl)
1792 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1793 while (ctx
1794 && (ctx->region_type == ORT_WORKSHARE
1795 || ctx->region_type == ORT_SIMD))
1796 ctx = ctx->outer_context;
1797 if (!ctx && !nonlocal_vlas->add (decl))
1799 tree copy = copy_node (decl);
1801 lang_hooks.dup_lang_specific_decl (copy);
1802 SET_DECL_RTL (copy, 0);
1803 TREE_USED (copy) = 1;
1804 DECL_CHAIN (copy) = nonlocal_vla_vars;
1805 nonlocal_vla_vars = copy;
1806 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1807 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1811 *expr_p = unshare_expr (value_expr);
1812 return GS_OK;
1815 return GS_ALL_DONE;
1818 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1820 static void
1821 recalculate_side_effects (tree t)
1823 enum tree_code code = TREE_CODE (t);
1824 int len = TREE_OPERAND_LENGTH (t);
1825 int i;
1827 switch (TREE_CODE_CLASS (code))
1829 case tcc_expression:
1830 switch (code)
1832 case INIT_EXPR:
1833 case MODIFY_EXPR:
1834 case VA_ARG_EXPR:
1835 case PREDECREMENT_EXPR:
1836 case PREINCREMENT_EXPR:
1837 case POSTDECREMENT_EXPR:
1838 case POSTINCREMENT_EXPR:
1839 /* All of these have side-effects, no matter what their
1840 operands are. */
1841 return;
1843 default:
1844 break;
1846 /* Fall through. */
1848 case tcc_comparison: /* a comparison expression */
1849 case tcc_unary: /* a unary arithmetic expression */
1850 case tcc_binary: /* a binary arithmetic expression */
1851 case tcc_reference: /* a reference */
1852 case tcc_vl_exp: /* a function call */
1853 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1854 for (i = 0; i < len; ++i)
1856 tree op = TREE_OPERAND (t, i);
1857 if (op && TREE_SIDE_EFFECTS (op))
1858 TREE_SIDE_EFFECTS (t) = 1;
1860 break;
1862 case tcc_constant:
1863 /* No side-effects. */
1864 return;
1866 default:
1867 gcc_unreachable ();
1871 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1872 node *EXPR_P.
1874 compound_lval
1875 : min_lval '[' val ']'
1876 | min_lval '.' ID
1877 | compound_lval '[' val ']'
1878 | compound_lval '.' ID
1880 This is not part of the original SIMPLE definition, which separates
1881 array and member references, but it seems reasonable to handle them
1882 together. Also, this way we don't run into problems with union
1883 aliasing; gcc requires that for accesses through a union to alias, the
1884 union reference must be explicit, which was not always the case when we
1885 were splitting up array and member refs.
1887 PRE_P points to the sequence where side effects that must happen before
1888 *EXPR_P should be stored.
1890 POST_P points to the sequence where side effects that must happen after
1891 *EXPR_P should be stored. */
1893 static enum gimplify_status
1894 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1895 fallback_t fallback)
1897 tree *p;
1898 enum gimplify_status ret = GS_ALL_DONE, tret;
1899 int i;
1900 location_t loc = EXPR_LOCATION (*expr_p);
1901 tree expr = *expr_p;
1903 /* Create a stack of the subexpressions so later we can walk them in
1904 order from inner to outer. */
1905 auto_vec<tree, 10> expr_stack;
1907 /* We can handle anything that get_inner_reference can deal with. */
1908 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1910 restart:
1911 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1912 if (TREE_CODE (*p) == INDIRECT_REF)
1913 *p = fold_indirect_ref_loc (loc, *p);
1915 if (handled_component_p (*p))
1917 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1918 additional COMPONENT_REFs. */
1919 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1920 && gimplify_var_or_parm_decl (p) == GS_OK)
1921 goto restart;
1922 else
1923 break;
1925 expr_stack.safe_push (*p);
1928 gcc_assert (expr_stack.length ());
1930 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1931 walked through and P points to the innermost expression.
1933 Java requires that we elaborated nodes in source order. That
1934 means we must gimplify the inner expression followed by each of
1935 the indices, in order. But we can't gimplify the inner
1936 expression until we deal with any variable bounds, sizes, or
1937 positions in order to deal with PLACEHOLDER_EXPRs.
1939 So we do this in three steps. First we deal with the annotations
1940 for any variables in the components, then we gimplify the base,
1941 then we gimplify any indices, from left to right. */
1942 for (i = expr_stack.length () - 1; i >= 0; i--)
1944 tree t = expr_stack[i];
1946 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1948 /* Gimplify the low bound and element type size and put them into
1949 the ARRAY_REF. If these values are set, they have already been
1950 gimplified. */
1951 if (TREE_OPERAND (t, 2) == NULL_TREE)
1953 tree low = unshare_expr (array_ref_low_bound (t));
1954 if (!is_gimple_min_invariant (low))
1956 TREE_OPERAND (t, 2) = low;
1957 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1958 post_p, is_gimple_reg,
1959 fb_rvalue);
1960 ret = MIN (ret, tret);
1963 else
1965 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1966 is_gimple_reg, fb_rvalue);
1967 ret = MIN (ret, tret);
1970 if (TREE_OPERAND (t, 3) == NULL_TREE)
1972 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1973 tree elmt_size = unshare_expr (array_ref_element_size (t));
1974 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1976 /* Divide the element size by the alignment of the element
1977 type (above). */
1978 elmt_size
1979 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1981 if (!is_gimple_min_invariant (elmt_size))
1983 TREE_OPERAND (t, 3) = elmt_size;
1984 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1985 post_p, is_gimple_reg,
1986 fb_rvalue);
1987 ret = MIN (ret, tret);
1990 else
1992 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1993 is_gimple_reg, fb_rvalue);
1994 ret = MIN (ret, tret);
1997 else if (TREE_CODE (t) == COMPONENT_REF)
1999 /* Set the field offset into T and gimplify it. */
2000 if (TREE_OPERAND (t, 2) == NULL_TREE)
2002 tree offset = unshare_expr (component_ref_field_offset (t));
2003 tree field = TREE_OPERAND (t, 1);
2004 tree factor
2005 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2007 /* Divide the offset by its alignment. */
2008 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2010 if (!is_gimple_min_invariant (offset))
2012 TREE_OPERAND (t, 2) = offset;
2013 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2014 post_p, is_gimple_reg,
2015 fb_rvalue);
2016 ret = MIN (ret, tret);
2019 else
2021 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2022 is_gimple_reg, fb_rvalue);
2023 ret = MIN (ret, tret);
2028 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2029 so as to match the min_lval predicate. Failure to do so may result
2030 in the creation of large aggregate temporaries. */
2031 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2032 fallback | fb_lvalue);
2033 ret = MIN (ret, tret);
2035 /* And finally, the indices and operands of ARRAY_REF. During this
2036 loop we also remove any useless conversions. */
2037 for (; expr_stack.length () > 0; )
2039 tree t = expr_stack.pop ();
2041 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2043 /* Gimplify the dimension. */
2044 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2046 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2047 is_gimple_val, fb_rvalue);
2048 ret = MIN (ret, tret);
2052 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2054 /* The innermost expression P may have originally had
2055 TREE_SIDE_EFFECTS set which would have caused all the outer
2056 expressions in *EXPR_P leading to P to also have had
2057 TREE_SIDE_EFFECTS set. */
2058 recalculate_side_effects (t);
2061 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2062 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2064 canonicalize_component_ref (expr_p);
2067 expr_stack.release ();
2069 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2071 return ret;
2074 /* Gimplify the self modifying expression pointed to by EXPR_P
2075 (++, --, +=, -=).
2077 PRE_P points to the list where side effects that must happen before
2078 *EXPR_P should be stored.
2080 POST_P points to the list where side effects that must happen after
2081 *EXPR_P should be stored.
2083 WANT_VALUE is nonzero iff we want to use the value of this expression
2084 in another expression.
2086 ARITH_TYPE is the type the computation should be performed in. */
2088 enum gimplify_status
2089 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2090 bool want_value, tree arith_type)
2092 enum tree_code code;
2093 tree lhs, lvalue, rhs, t1;
2094 gimple_seq post = NULL, *orig_post_p = post_p;
2095 bool postfix;
2096 enum tree_code arith_code;
2097 enum gimplify_status ret;
2098 location_t loc = EXPR_LOCATION (*expr_p);
2100 code = TREE_CODE (*expr_p);
2102 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2103 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2105 /* Prefix or postfix? */
2106 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2107 /* Faster to treat as prefix if result is not used. */
2108 postfix = want_value;
2109 else
2110 postfix = false;
2112 /* For postfix, make sure the inner expression's post side effects
2113 are executed after side effects from this expression. */
2114 if (postfix)
2115 post_p = &post;
2117 /* Add or subtract? */
2118 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2119 arith_code = PLUS_EXPR;
2120 else
2121 arith_code = MINUS_EXPR;
2123 /* Gimplify the LHS into a GIMPLE lvalue. */
2124 lvalue = TREE_OPERAND (*expr_p, 0);
2125 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2126 if (ret == GS_ERROR)
2127 return ret;
2129 /* Extract the operands to the arithmetic operation. */
2130 lhs = lvalue;
2131 rhs = TREE_OPERAND (*expr_p, 1);
2133 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2134 that as the result value and in the postqueue operation. */
2135 if (postfix)
2137 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2138 if (ret == GS_ERROR)
2139 return ret;
2141 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2144 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2145 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2147 rhs = convert_to_ptrofftype_loc (loc, rhs);
2148 if (arith_code == MINUS_EXPR)
2149 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2150 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2152 else
2153 t1 = fold_convert (TREE_TYPE (*expr_p),
2154 fold_build2 (arith_code, arith_type,
2155 fold_convert (arith_type, lhs),
2156 fold_convert (arith_type, rhs)));
2158 if (postfix)
2160 gimplify_assign (lvalue, t1, pre_p);
2161 gimplify_seq_add_seq (orig_post_p, post);
2162 *expr_p = lhs;
2163 return GS_ALL_DONE;
2165 else
2167 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2168 return GS_OK;
2172 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2174 static void
2175 maybe_with_size_expr (tree *expr_p)
2177 tree expr = *expr_p;
2178 tree type = TREE_TYPE (expr);
2179 tree size;
2181 /* If we've already wrapped this or the type is error_mark_node, we can't do
2182 anything. */
2183 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2184 || type == error_mark_node)
2185 return;
2187 /* If the size isn't known or is a constant, we have nothing to do. */
2188 size = TYPE_SIZE_UNIT (type);
2189 if (!size || TREE_CODE (size) == INTEGER_CST)
2190 return;
2192 /* Otherwise, make a WITH_SIZE_EXPR. */
2193 size = unshare_expr (size);
2194 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2195 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2198 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2199 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2200 the CALL_EXPR. */
2202 enum gimplify_status
2203 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2205 bool (*test) (tree);
2206 fallback_t fb;
2208 /* In general, we allow lvalues for function arguments to avoid
2209 extra overhead of copying large aggregates out of even larger
2210 aggregates into temporaries only to copy the temporaries to
2211 the argument list. Make optimizers happy by pulling out to
2212 temporaries those types that fit in registers. */
2213 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2214 test = is_gimple_val, fb = fb_rvalue;
2215 else
2217 test = is_gimple_lvalue, fb = fb_either;
2218 /* Also strip a TARGET_EXPR that would force an extra copy. */
2219 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2221 tree init = TARGET_EXPR_INITIAL (*arg_p);
2222 if (init
2223 && !VOID_TYPE_P (TREE_TYPE (init)))
2224 *arg_p = init;
2228 /* If this is a variable sized type, we must remember the size. */
2229 maybe_with_size_expr (arg_p);
2231 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2232 /* Make sure arguments have the same location as the function call
2233 itself. */
2234 protected_set_expr_location (*arg_p, call_location);
2236 /* There is a sequence point before a function call. Side effects in
2237 the argument list must occur before the actual call. So, when
2238 gimplifying arguments, force gimplify_expr to use an internal
2239 post queue which is then appended to the end of PRE_P. */
2240 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2243 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2244 references that weren't in the source. We'll do it during omplower pass
2245 instead. */
2247 static bool
2248 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2250 struct gimplify_omp_ctx *ctx;
2251 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2252 if (ctx->region_type == ORT_TARGET)
2253 return false;
2254 return fold_stmt (gsi);
2257 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2258 WANT_VALUE is true if the result of the call is desired. */
2260 static enum gimplify_status
2261 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2263 tree fndecl, parms, p, fnptrtype;
2264 enum gimplify_status ret;
2265 int i, nargs;
2266 gimple call;
2267 bool builtin_va_start_p = false;
2268 location_t loc = EXPR_LOCATION (*expr_p);
2270 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2272 /* For reliable diagnostics during inlining, it is necessary that
2273 every call_expr be annotated with file and line. */
2274 if (! EXPR_HAS_LOCATION (*expr_p))
2275 SET_EXPR_LOCATION (*expr_p, input_location);
2277 /* Gimplify internal functions created in the FEs. */
2278 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2280 nargs = call_expr_nargs (*expr_p);
2281 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2282 auto_vec<tree> vargs (nargs);
2284 for (i = 0; i < nargs; i++)
2286 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2287 EXPR_LOCATION (*expr_p));
2288 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2290 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2291 gimplify_seq_add_stmt (pre_p, call);
2292 return GS_ALL_DONE;
2295 /* This may be a call to a builtin function.
2297 Builtin function calls may be transformed into different
2298 (and more efficient) builtin function calls under certain
2299 circumstances. Unfortunately, gimplification can muck things
2300 up enough that the builtin expanders are not aware that certain
2301 transformations are still valid.
2303 So we attempt transformation/gimplification of the call before
2304 we gimplify the CALL_EXPR. At this time we do not manage to
2305 transform all calls in the same manner as the expanders do, but
2306 we do transform most of them. */
2307 fndecl = get_callee_fndecl (*expr_p);
2308 if (fndecl
2309 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2310 switch (DECL_FUNCTION_CODE (fndecl))
2312 case BUILT_IN_VA_START:
2314 builtin_va_start_p = TRUE;
2315 if (call_expr_nargs (*expr_p) < 2)
2317 error ("too few arguments to function %<va_start%>");
2318 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2319 return GS_OK;
2322 if (fold_builtin_next_arg (*expr_p, true))
2324 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2325 return GS_OK;
2327 break;
2329 case BUILT_IN_LINE:
2331 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2332 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2333 return GS_OK;
2335 case BUILT_IN_FILE:
2337 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2338 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2339 return GS_OK;
2341 case BUILT_IN_FUNCTION:
2343 const char *function;
2344 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2345 *expr_p = build_string_literal (strlen (function) + 1, function);
2346 return GS_OK;
2348 default:
2351 if (fndecl && DECL_BUILT_IN (fndecl))
2353 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2354 if (new_tree && new_tree != *expr_p)
2356 /* There was a transformation of this call which computes the
2357 same value, but in a more efficient way. Return and try
2358 again. */
2359 *expr_p = new_tree;
2360 return GS_OK;
2364 /* Remember the original function pointer type. */
2365 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2367 /* There is a sequence point before the call, so any side effects in
2368 the calling expression must occur before the actual call. Force
2369 gimplify_expr to use an internal post queue. */
2370 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2371 is_gimple_call_addr, fb_rvalue);
2373 nargs = call_expr_nargs (*expr_p);
2375 /* Get argument types for verification. */
2376 fndecl = get_callee_fndecl (*expr_p);
2377 parms = NULL_TREE;
2378 if (fndecl)
2379 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2380 else
2381 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2383 if (fndecl && DECL_ARGUMENTS (fndecl))
2384 p = DECL_ARGUMENTS (fndecl);
2385 else if (parms)
2386 p = parms;
2387 else
2388 p = NULL_TREE;
2389 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2392 /* If the last argument is __builtin_va_arg_pack () and it is not
2393 passed as a named argument, decrease the number of CALL_EXPR
2394 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2395 if (!p
2396 && i < nargs
2397 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2399 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2400 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2402 if (last_arg_fndecl
2403 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2404 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2405 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2407 tree call = *expr_p;
2409 --nargs;
2410 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2411 CALL_EXPR_FN (call),
2412 nargs, CALL_EXPR_ARGP (call));
2414 /* Copy all CALL_EXPR flags, location and block, except
2415 CALL_EXPR_VA_ARG_PACK flag. */
2416 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2417 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2418 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2419 = CALL_EXPR_RETURN_SLOT_OPT (call);
2420 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2421 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2423 /* Set CALL_EXPR_VA_ARG_PACK. */
2424 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2428 /* Finally, gimplify the function arguments. */
2429 if (nargs > 0)
2431 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2432 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2433 PUSH_ARGS_REVERSED ? i-- : i++)
2435 enum gimplify_status t;
2437 /* Avoid gimplifying the second argument to va_start, which needs to
2438 be the plain PARM_DECL. */
2439 if ((i != 1) || !builtin_va_start_p)
2441 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2442 EXPR_LOCATION (*expr_p));
2444 if (t == GS_ERROR)
2445 ret = GS_ERROR;
2450 /* Verify the function result. */
2451 if (want_value && fndecl
2452 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2454 error_at (loc, "using result of function returning %<void%>");
2455 ret = GS_ERROR;
2458 /* Try this again in case gimplification exposed something. */
2459 if (ret != GS_ERROR)
2461 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2463 if (new_tree && new_tree != *expr_p)
2465 /* There was a transformation of this call which computes the
2466 same value, but in a more efficient way. Return and try
2467 again. */
2468 *expr_p = new_tree;
2469 return GS_OK;
2472 else
2474 *expr_p = error_mark_node;
2475 return GS_ERROR;
2478 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2479 decl. This allows us to eliminate redundant or useless
2480 calls to "const" functions. */
2481 if (TREE_CODE (*expr_p) == CALL_EXPR)
2483 int flags = call_expr_flags (*expr_p);
2484 if (flags & (ECF_CONST | ECF_PURE)
2485 /* An infinite loop is considered a side effect. */
2486 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2487 TREE_SIDE_EFFECTS (*expr_p) = 0;
2490 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2491 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2492 form and delegate the creation of a GIMPLE_CALL to
2493 gimplify_modify_expr. This is always possible because when
2494 WANT_VALUE is true, the caller wants the result of this call into
2495 a temporary, which means that we will emit an INIT_EXPR in
2496 internal_get_tmp_var which will then be handled by
2497 gimplify_modify_expr. */
2498 if (!want_value)
2500 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2501 have to do is replicate it as a GIMPLE_CALL tuple. */
2502 gimple_stmt_iterator gsi;
2503 call = gimple_build_call_from_tree (*expr_p);
2504 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2505 notice_special_calls (call);
2506 gimplify_seq_add_stmt (pre_p, call);
2507 gsi = gsi_last (*pre_p);
2508 maybe_fold_stmt (&gsi);
2509 *expr_p = NULL_TREE;
2511 else
2512 /* Remember the original function type. */
2513 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2514 CALL_EXPR_FN (*expr_p));
2516 return ret;
2519 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2520 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2522 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2523 condition is true or false, respectively. If null, we should generate
2524 our own to skip over the evaluation of this specific expression.
2526 LOCUS is the source location of the COND_EXPR.
2528 This function is the tree equivalent of do_jump.
2530 shortcut_cond_r should only be called by shortcut_cond_expr. */
2532 static tree
2533 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2534 location_t locus)
2536 tree local_label = NULL_TREE;
2537 tree t, expr = NULL;
2539 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2540 retain the shortcut semantics. Just insert the gotos here;
2541 shortcut_cond_expr will append the real blocks later. */
2542 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2544 location_t new_locus;
2546 /* Turn if (a && b) into
2548 if (a); else goto no;
2549 if (b) goto yes; else goto no;
2550 (no:) */
2552 if (false_label_p == NULL)
2553 false_label_p = &local_label;
2555 /* Keep the original source location on the first 'if'. */
2556 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2557 append_to_statement_list (t, &expr);
2559 /* Set the source location of the && on the second 'if'. */
2560 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2561 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2562 new_locus);
2563 append_to_statement_list (t, &expr);
2565 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2567 location_t new_locus;
2569 /* Turn if (a || b) into
2571 if (a) goto yes;
2572 if (b) goto yes; else goto no;
2573 (yes:) */
2575 if (true_label_p == NULL)
2576 true_label_p = &local_label;
2578 /* Keep the original source location on the first 'if'. */
2579 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2580 append_to_statement_list (t, &expr);
2582 /* Set the source location of the || on the second 'if'. */
2583 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2584 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2585 new_locus);
2586 append_to_statement_list (t, &expr);
2588 else if (TREE_CODE (pred) == COND_EXPR
2589 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2590 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2592 location_t new_locus;
2594 /* As long as we're messing with gotos, turn if (a ? b : c) into
2595 if (a)
2596 if (b) goto yes; else goto no;
2597 else
2598 if (c) goto yes; else goto no;
2600 Don't do this if one of the arms has void type, which can happen
2601 in C++ when the arm is throw. */
2603 /* Keep the original source location on the first 'if'. Set the source
2604 location of the ? on the second 'if'. */
2605 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2606 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2607 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2608 false_label_p, locus),
2609 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2610 false_label_p, new_locus));
2612 else
2614 expr = build3 (COND_EXPR, void_type_node, pred,
2615 build_and_jump (true_label_p),
2616 build_and_jump (false_label_p));
2617 SET_EXPR_LOCATION (expr, locus);
2620 if (local_label)
2622 t = build1 (LABEL_EXPR, void_type_node, local_label);
2623 append_to_statement_list (t, &expr);
2626 return expr;
2629 /* Given a conditional expression EXPR with short-circuit boolean
2630 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2631 predicate apart into the equivalent sequence of conditionals. */
2633 static tree
2634 shortcut_cond_expr (tree expr)
2636 tree pred = TREE_OPERAND (expr, 0);
2637 tree then_ = TREE_OPERAND (expr, 1);
2638 tree else_ = TREE_OPERAND (expr, 2);
2639 tree true_label, false_label, end_label, t;
2640 tree *true_label_p;
2641 tree *false_label_p;
2642 bool emit_end, emit_false, jump_over_else;
2643 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2644 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2646 /* First do simple transformations. */
2647 if (!else_se)
2649 /* If there is no 'else', turn
2650 if (a && b) then c
2651 into
2652 if (a) if (b) then c. */
2653 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2655 /* Keep the original source location on the first 'if'. */
2656 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2657 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2658 /* Set the source location of the && on the second 'if'. */
2659 if (EXPR_HAS_LOCATION (pred))
2660 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2661 then_ = shortcut_cond_expr (expr);
2662 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2663 pred = TREE_OPERAND (pred, 0);
2664 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2665 SET_EXPR_LOCATION (expr, locus);
2669 if (!then_se)
2671 /* If there is no 'then', turn
2672 if (a || b); else d
2673 into
2674 if (a); else if (b); else d. */
2675 while (TREE_CODE (pred) == TRUTH_ORIF_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 else_ = shortcut_cond_expr (expr);
2684 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2685 pred = TREE_OPERAND (pred, 0);
2686 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2687 SET_EXPR_LOCATION (expr, locus);
2691 /* If we're done, great. */
2692 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2693 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2694 return expr;
2696 /* Otherwise we need to mess with gotos. Change
2697 if (a) c; else d;
2699 if (a); else goto no;
2700 c; goto end;
2701 no: d; end:
2702 and recursively gimplify the condition. */
2704 true_label = false_label = end_label = NULL_TREE;
2706 /* If our arms just jump somewhere, hijack those labels so we don't
2707 generate jumps to jumps. */
2709 if (then_
2710 && TREE_CODE (then_) == GOTO_EXPR
2711 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2713 true_label = GOTO_DESTINATION (then_);
2714 then_ = NULL;
2715 then_se = false;
2718 if (else_
2719 && TREE_CODE (else_) == GOTO_EXPR
2720 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2722 false_label = GOTO_DESTINATION (else_);
2723 else_ = NULL;
2724 else_se = false;
2727 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2728 if (true_label)
2729 true_label_p = &true_label;
2730 else
2731 true_label_p = NULL;
2733 /* The 'else' branch also needs a label if it contains interesting code. */
2734 if (false_label || else_se)
2735 false_label_p = &false_label;
2736 else
2737 false_label_p = NULL;
2739 /* If there was nothing else in our arms, just forward the label(s). */
2740 if (!then_se && !else_se)
2741 return shortcut_cond_r (pred, true_label_p, false_label_p,
2742 EXPR_LOC_OR_LOC (expr, input_location));
2744 /* If our last subexpression already has a terminal label, reuse it. */
2745 if (else_se)
2746 t = expr_last (else_);
2747 else if (then_se)
2748 t = expr_last (then_);
2749 else
2750 t = NULL;
2751 if (t && TREE_CODE (t) == LABEL_EXPR)
2752 end_label = LABEL_EXPR_LABEL (t);
2754 /* If we don't care about jumping to the 'else' branch, jump to the end
2755 if the condition is false. */
2756 if (!false_label_p)
2757 false_label_p = &end_label;
2759 /* We only want to emit these labels if we aren't hijacking them. */
2760 emit_end = (end_label == NULL_TREE);
2761 emit_false = (false_label == NULL_TREE);
2763 /* We only emit the jump over the else clause if we have to--if the
2764 then clause may fall through. Otherwise we can wind up with a
2765 useless jump and a useless label at the end of gimplified code,
2766 which will cause us to think that this conditional as a whole
2767 falls through even if it doesn't. If we then inline a function
2768 which ends with such a condition, that can cause us to issue an
2769 inappropriate warning about control reaching the end of a
2770 non-void function. */
2771 jump_over_else = block_may_fallthru (then_);
2773 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2774 EXPR_LOC_OR_LOC (expr, input_location));
2776 expr = NULL;
2777 append_to_statement_list (pred, &expr);
2779 append_to_statement_list (then_, &expr);
2780 if (else_se)
2782 if (jump_over_else)
2784 tree last = expr_last (expr);
2785 t = build_and_jump (&end_label);
2786 if (EXPR_HAS_LOCATION (last))
2787 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2788 append_to_statement_list (t, &expr);
2790 if (emit_false)
2792 t = build1 (LABEL_EXPR, void_type_node, false_label);
2793 append_to_statement_list (t, &expr);
2795 append_to_statement_list (else_, &expr);
2797 if (emit_end && end_label)
2799 t = build1 (LABEL_EXPR, void_type_node, end_label);
2800 append_to_statement_list (t, &expr);
2803 return expr;
2806 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2808 tree
2809 gimple_boolify (tree expr)
2811 tree type = TREE_TYPE (expr);
2812 location_t loc = EXPR_LOCATION (expr);
2814 if (TREE_CODE (expr) == NE_EXPR
2815 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2816 && integer_zerop (TREE_OPERAND (expr, 1)))
2818 tree call = TREE_OPERAND (expr, 0);
2819 tree fn = get_callee_fndecl (call);
2821 /* For __builtin_expect ((long) (x), y) recurse into x as well
2822 if x is truth_value_p. */
2823 if (fn
2824 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2825 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2826 && call_expr_nargs (call) == 2)
2828 tree arg = CALL_EXPR_ARG (call, 0);
2829 if (arg)
2831 if (TREE_CODE (arg) == NOP_EXPR
2832 && TREE_TYPE (arg) == TREE_TYPE (call))
2833 arg = TREE_OPERAND (arg, 0);
2834 if (truth_value_p (TREE_CODE (arg)))
2836 arg = gimple_boolify (arg);
2837 CALL_EXPR_ARG (call, 0)
2838 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2844 switch (TREE_CODE (expr))
2846 case TRUTH_AND_EXPR:
2847 case TRUTH_OR_EXPR:
2848 case TRUTH_XOR_EXPR:
2849 case TRUTH_ANDIF_EXPR:
2850 case TRUTH_ORIF_EXPR:
2851 /* Also boolify the arguments of truth exprs. */
2852 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2853 /* FALLTHRU */
2855 case TRUTH_NOT_EXPR:
2856 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2858 /* These expressions always produce boolean results. */
2859 if (TREE_CODE (type) != BOOLEAN_TYPE)
2860 TREE_TYPE (expr) = boolean_type_node;
2861 return expr;
2863 case ANNOTATE_EXPR:
2864 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2866 case annot_expr_ivdep_kind:
2867 case annot_expr_no_vector_kind:
2868 case annot_expr_vector_kind:
2869 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2870 if (TREE_CODE (type) != BOOLEAN_TYPE)
2871 TREE_TYPE (expr) = boolean_type_node;
2872 return expr;
2873 default:
2874 gcc_unreachable ();
2877 default:
2878 if (COMPARISON_CLASS_P (expr))
2880 /* There expressions always prduce boolean results. */
2881 if (TREE_CODE (type) != BOOLEAN_TYPE)
2882 TREE_TYPE (expr) = boolean_type_node;
2883 return expr;
2885 /* Other expressions that get here must have boolean values, but
2886 might need to be converted to the appropriate mode. */
2887 if (TREE_CODE (type) == BOOLEAN_TYPE)
2888 return expr;
2889 return fold_convert_loc (loc, boolean_type_node, expr);
2893 /* Given a conditional expression *EXPR_P without side effects, gimplify
2894 its operands. New statements are inserted to PRE_P. */
2896 static enum gimplify_status
2897 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2899 tree expr = *expr_p, cond;
2900 enum gimplify_status ret, tret;
2901 enum tree_code code;
2903 cond = gimple_boolify (COND_EXPR_COND (expr));
2905 /* We need to handle && and || specially, as their gimplification
2906 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2907 code = TREE_CODE (cond);
2908 if (code == TRUTH_ANDIF_EXPR)
2909 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2910 else if (code == TRUTH_ORIF_EXPR)
2911 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2912 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2913 COND_EXPR_COND (*expr_p) = cond;
2915 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2916 is_gimple_val, fb_rvalue);
2917 ret = MIN (ret, tret);
2918 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2919 is_gimple_val, fb_rvalue);
2921 return MIN (ret, tret);
2924 /* Return true if evaluating EXPR could trap.
2925 EXPR is GENERIC, while tree_could_trap_p can be called
2926 only on GIMPLE. */
2928 static bool
2929 generic_expr_could_trap_p (tree expr)
2931 unsigned i, n;
2933 if (!expr || is_gimple_val (expr))
2934 return false;
2936 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2937 return true;
2939 n = TREE_OPERAND_LENGTH (expr);
2940 for (i = 0; i < n; i++)
2941 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2942 return true;
2944 return false;
2947 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2948 into
2950 if (p) if (p)
2951 t1 = a; a;
2952 else or else
2953 t1 = b; b;
2956 The second form is used when *EXPR_P is of type void.
2958 PRE_P points to the list where side effects that must happen before
2959 *EXPR_P should be stored. */
2961 static enum gimplify_status
2962 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2964 tree expr = *expr_p;
2965 tree type = TREE_TYPE (expr);
2966 location_t loc = EXPR_LOCATION (expr);
2967 tree tmp, arm1, arm2;
2968 enum gimplify_status ret;
2969 tree label_true, label_false, label_cont;
2970 bool have_then_clause_p, have_else_clause_p;
2971 gimple gimple_cond;
2972 enum tree_code pred_code;
2973 gimple_seq seq = NULL;
2975 /* If this COND_EXPR has a value, copy the values into a temporary within
2976 the arms. */
2977 if (!VOID_TYPE_P (type))
2979 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2980 tree result;
2982 /* If either an rvalue is ok or we do not require an lvalue, create the
2983 temporary. But we cannot do that if the type is addressable. */
2984 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2985 && !TREE_ADDRESSABLE (type))
2987 if (gimplify_ctxp->allow_rhs_cond_expr
2988 /* If either branch has side effects or could trap, it can't be
2989 evaluated unconditionally. */
2990 && !TREE_SIDE_EFFECTS (then_)
2991 && !generic_expr_could_trap_p (then_)
2992 && !TREE_SIDE_EFFECTS (else_)
2993 && !generic_expr_could_trap_p (else_))
2994 return gimplify_pure_cond_expr (expr_p, pre_p);
2996 tmp = create_tmp_var (type, "iftmp");
2997 result = tmp;
3000 /* Otherwise, only create and copy references to the values. */
3001 else
3003 type = build_pointer_type (type);
3005 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3006 then_ = build_fold_addr_expr_loc (loc, then_);
3008 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3009 else_ = build_fold_addr_expr_loc (loc, else_);
3011 expr
3012 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3014 tmp = create_tmp_var (type, "iftmp");
3015 result = build_simple_mem_ref_loc (loc, tmp);
3018 /* Build the new then clause, `tmp = then_;'. But don't build the
3019 assignment if the value is void; in C++ it can be if it's a throw. */
3020 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3021 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3023 /* Similarly, build the new else clause, `tmp = else_;'. */
3024 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3025 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3027 TREE_TYPE (expr) = void_type_node;
3028 recalculate_side_effects (expr);
3030 /* Move the COND_EXPR to the prequeue. */
3031 gimplify_stmt (&expr, pre_p);
3033 *expr_p = result;
3034 return GS_ALL_DONE;
3037 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3038 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3039 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3040 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3042 /* Make sure the condition has BOOLEAN_TYPE. */
3043 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3045 /* Break apart && and || conditions. */
3046 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3047 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3049 expr = shortcut_cond_expr (expr);
3051 if (expr != *expr_p)
3053 *expr_p = expr;
3055 /* We can't rely on gimplify_expr to re-gimplify the expanded
3056 form properly, as cleanups might cause the target labels to be
3057 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3058 set up a conditional context. */
3059 gimple_push_condition ();
3060 gimplify_stmt (expr_p, &seq);
3061 gimple_pop_condition (pre_p);
3062 gimple_seq_add_seq (pre_p, seq);
3064 return GS_ALL_DONE;
3068 /* Now do the normal gimplification. */
3070 /* Gimplify condition. */
3071 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3072 fb_rvalue);
3073 if (ret == GS_ERROR)
3074 return GS_ERROR;
3075 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3077 gimple_push_condition ();
3079 have_then_clause_p = have_else_clause_p = false;
3080 if (TREE_OPERAND (expr, 1) != NULL
3081 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3082 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3083 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3084 == current_function_decl)
3085 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3086 have different locations, otherwise we end up with incorrect
3087 location information on the branches. */
3088 && (optimize
3089 || !EXPR_HAS_LOCATION (expr)
3090 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3091 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3093 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3094 have_then_clause_p = true;
3096 else
3097 label_true = create_artificial_label (UNKNOWN_LOCATION);
3098 if (TREE_OPERAND (expr, 2) != NULL
3099 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3100 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3101 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3102 == current_function_decl)
3103 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3104 have different locations, otherwise we end up with incorrect
3105 location information on the branches. */
3106 && (optimize
3107 || !EXPR_HAS_LOCATION (expr)
3108 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3109 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3111 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3112 have_else_clause_p = true;
3114 else
3115 label_false = create_artificial_label (UNKNOWN_LOCATION);
3117 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3118 &arm2);
3120 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3121 label_false);
3123 gimplify_seq_add_stmt (&seq, gimple_cond);
3124 label_cont = NULL_TREE;
3125 if (!have_then_clause_p)
3127 /* For if (...) {} else { code; } put label_true after
3128 the else block. */
3129 if (TREE_OPERAND (expr, 1) == NULL_TREE
3130 && !have_else_clause_p
3131 && TREE_OPERAND (expr, 2) != NULL_TREE)
3132 label_cont = label_true;
3133 else
3135 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3136 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3137 /* For if (...) { code; } else {} or
3138 if (...) { code; } else goto label; or
3139 if (...) { code; return; } else { ... }
3140 label_cont isn't needed. */
3141 if (!have_else_clause_p
3142 && TREE_OPERAND (expr, 2) != NULL_TREE
3143 && gimple_seq_may_fallthru (seq))
3145 gimple g;
3146 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3148 g = gimple_build_goto (label_cont);
3150 /* GIMPLE_COND's are very low level; they have embedded
3151 gotos. This particular embedded goto should not be marked
3152 with the location of the original COND_EXPR, as it would
3153 correspond to the COND_EXPR's condition, not the ELSE or the
3154 THEN arms. To avoid marking it with the wrong location, flag
3155 it as "no location". */
3156 gimple_set_do_not_emit_location (g);
3158 gimplify_seq_add_stmt (&seq, g);
3162 if (!have_else_clause_p)
3164 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3165 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3167 if (label_cont)
3168 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3170 gimple_pop_condition (pre_p);
3171 gimple_seq_add_seq (pre_p, seq);
3173 if (ret == GS_ERROR)
3174 ; /* Do nothing. */
3175 else if (have_then_clause_p || have_else_clause_p)
3176 ret = GS_ALL_DONE;
3177 else
3179 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3180 expr = TREE_OPERAND (expr, 0);
3181 gimplify_stmt (&expr, pre_p);
3184 *expr_p = NULL;
3185 return ret;
3188 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3189 to be marked addressable.
3191 We cannot rely on such an expression being directly markable if a temporary
3192 has been created by the gimplification. In this case, we create another
3193 temporary and initialize it with a copy, which will become a store after we
3194 mark it addressable. This can happen if the front-end passed us something
3195 that it could not mark addressable yet, like a Fortran pass-by-reference
3196 parameter (int) floatvar. */
3198 static void
3199 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3201 while (handled_component_p (*expr_p))
3202 expr_p = &TREE_OPERAND (*expr_p, 0);
3203 if (is_gimple_reg (*expr_p))
3205 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3206 DECL_GIMPLE_REG_P (var) = 0;
3207 *expr_p = var;
3211 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3212 a call to __builtin_memcpy. */
3214 static enum gimplify_status
3215 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3216 gimple_seq *seq_p)
3218 tree t, to, to_ptr, from, from_ptr;
3219 gimple gs;
3220 location_t loc = EXPR_LOCATION (*expr_p);
3222 to = TREE_OPERAND (*expr_p, 0);
3223 from = TREE_OPERAND (*expr_p, 1);
3225 /* Mark the RHS addressable. Beware that it may not be possible to do so
3226 directly if a temporary has been created by the gimplification. */
3227 prepare_gimple_addressable (&from, seq_p);
3229 mark_addressable (from);
3230 from_ptr = build_fold_addr_expr_loc (loc, from);
3231 gimplify_arg (&from_ptr, seq_p, loc);
3233 mark_addressable (to);
3234 to_ptr = build_fold_addr_expr_loc (loc, to);
3235 gimplify_arg (&to_ptr, seq_p, loc);
3237 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3239 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3241 if (want_value)
3243 /* tmp = memcpy() */
3244 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3245 gimple_call_set_lhs (gs, t);
3246 gimplify_seq_add_stmt (seq_p, gs);
3248 *expr_p = build_simple_mem_ref (t);
3249 return GS_ALL_DONE;
3252 gimplify_seq_add_stmt (seq_p, gs);
3253 *expr_p = NULL;
3254 return GS_ALL_DONE;
3257 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3258 a call to __builtin_memset. In this case we know that the RHS is
3259 a CONSTRUCTOR with an empty element list. */
3261 static enum gimplify_status
3262 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3263 gimple_seq *seq_p)
3265 tree t, from, to, to_ptr;
3266 gimple gs;
3267 location_t loc = EXPR_LOCATION (*expr_p);
3269 /* Assert our assumptions, to abort instead of producing wrong code
3270 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3271 not be immediately exposed. */
3272 from = TREE_OPERAND (*expr_p, 1);
3273 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3274 from = TREE_OPERAND (from, 0);
3276 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3277 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3279 /* Now proceed. */
3280 to = TREE_OPERAND (*expr_p, 0);
3282 to_ptr = build_fold_addr_expr_loc (loc, to);
3283 gimplify_arg (&to_ptr, seq_p, loc);
3284 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3286 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3288 if (want_value)
3290 /* tmp = memset() */
3291 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3292 gimple_call_set_lhs (gs, t);
3293 gimplify_seq_add_stmt (seq_p, gs);
3295 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3296 return GS_ALL_DONE;
3299 gimplify_seq_add_stmt (seq_p, gs);
3300 *expr_p = NULL;
3301 return GS_ALL_DONE;
3304 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3305 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3306 assignment. Return non-null if we detect a potential overlap. */
3308 struct gimplify_init_ctor_preeval_data
3310 /* The base decl of the lhs object. May be NULL, in which case we
3311 have to assume the lhs is indirect. */
3312 tree lhs_base_decl;
3314 /* The alias set of the lhs object. */
3315 alias_set_type lhs_alias_set;
3318 static tree
3319 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3321 struct gimplify_init_ctor_preeval_data *data
3322 = (struct gimplify_init_ctor_preeval_data *) xdata;
3323 tree t = *tp;
3325 /* If we find the base object, obviously we have overlap. */
3326 if (data->lhs_base_decl == t)
3327 return t;
3329 /* If the constructor component is indirect, determine if we have a
3330 potential overlap with the lhs. The only bits of information we
3331 have to go on at this point are addressability and alias sets. */
3332 if ((INDIRECT_REF_P (t)
3333 || TREE_CODE (t) == MEM_REF)
3334 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3335 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3336 return t;
3338 /* If the constructor component is a call, determine if it can hide a
3339 potential overlap with the lhs through an INDIRECT_REF like above.
3340 ??? Ugh - this is completely broken. In fact this whole analysis
3341 doesn't look conservative. */
3342 if (TREE_CODE (t) == CALL_EXPR)
3344 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3346 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3347 if (POINTER_TYPE_P (TREE_VALUE (type))
3348 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3349 && alias_sets_conflict_p (data->lhs_alias_set,
3350 get_alias_set
3351 (TREE_TYPE (TREE_VALUE (type)))))
3352 return t;
3355 if (IS_TYPE_OR_DECL_P (t))
3356 *walk_subtrees = 0;
3357 return NULL;
3360 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3361 force values that overlap with the lhs (as described by *DATA)
3362 into temporaries. */
3364 static void
3365 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3366 struct gimplify_init_ctor_preeval_data *data)
3368 enum gimplify_status one;
3370 /* If the value is constant, then there's nothing to pre-evaluate. */
3371 if (TREE_CONSTANT (*expr_p))
3373 /* Ensure it does not have side effects, it might contain a reference to
3374 the object we're initializing. */
3375 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3376 return;
3379 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3380 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3381 return;
3383 /* Recurse for nested constructors. */
3384 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3386 unsigned HOST_WIDE_INT ix;
3387 constructor_elt *ce;
3388 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3390 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3391 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3393 return;
3396 /* If this is a variable sized type, we must remember the size. */
3397 maybe_with_size_expr (expr_p);
3399 /* Gimplify the constructor element to something appropriate for the rhs
3400 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3401 the gimplifier will consider this a store to memory. Doing this
3402 gimplification now means that we won't have to deal with complicated
3403 language-specific trees, nor trees like SAVE_EXPR that can induce
3404 exponential search behavior. */
3405 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3406 if (one == GS_ERROR)
3408 *expr_p = NULL;
3409 return;
3412 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3413 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3414 always be true for all scalars, since is_gimple_mem_rhs insists on a
3415 temporary variable for them. */
3416 if (DECL_P (*expr_p))
3417 return;
3419 /* If this is of variable size, we have no choice but to assume it doesn't
3420 overlap since we can't make a temporary for it. */
3421 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3422 return;
3424 /* Otherwise, we must search for overlap ... */
3425 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3426 return;
3428 /* ... and if found, force the value into a temporary. */
3429 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3432 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3433 a RANGE_EXPR in a CONSTRUCTOR for an array.
3435 var = lower;
3436 loop_entry:
3437 object[var] = value;
3438 if (var == upper)
3439 goto loop_exit;
3440 var = var + 1;
3441 goto loop_entry;
3442 loop_exit:
3444 We increment var _after_ the loop exit check because we might otherwise
3445 fail if upper == TYPE_MAX_VALUE (type for upper).
3447 Note that we never have to deal with SAVE_EXPRs here, because this has
3448 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3450 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3451 gimple_seq *, bool);
3453 static void
3454 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3455 tree value, tree array_elt_type,
3456 gimple_seq *pre_p, bool cleared)
3458 tree loop_entry_label, loop_exit_label, fall_thru_label;
3459 tree var, var_type, cref, tmp;
3461 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3462 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3463 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3465 /* Create and initialize the index variable. */
3466 var_type = TREE_TYPE (upper);
3467 var = create_tmp_var (var_type, NULL);
3468 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3470 /* Add the loop entry label. */
3471 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3473 /* Build the reference. */
3474 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3475 var, NULL_TREE, NULL_TREE);
3477 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3478 the store. Otherwise just assign value to the reference. */
3480 if (TREE_CODE (value) == CONSTRUCTOR)
3481 /* NB we might have to call ourself recursively through
3482 gimplify_init_ctor_eval if the value is a constructor. */
3483 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3484 pre_p, cleared);
3485 else
3486 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3488 /* We exit the loop when the index var is equal to the upper bound. */
3489 gimplify_seq_add_stmt (pre_p,
3490 gimple_build_cond (EQ_EXPR, var, upper,
3491 loop_exit_label, fall_thru_label));
3493 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3495 /* Otherwise, increment the index var... */
3496 tmp = build2 (PLUS_EXPR, var_type, var,
3497 fold_convert (var_type, integer_one_node));
3498 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3500 /* ...and jump back to the loop entry. */
3501 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3503 /* Add the loop exit label. */
3504 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3507 /* Return true if FDECL is accessing a field that is zero sized. */
3509 static bool
3510 zero_sized_field_decl (const_tree fdecl)
3512 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3513 && integer_zerop (DECL_SIZE (fdecl)))
3514 return true;
3515 return false;
3518 /* Return true if TYPE is zero sized. */
3520 static bool
3521 zero_sized_type (const_tree type)
3523 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3524 && integer_zerop (TYPE_SIZE (type)))
3525 return true;
3526 return false;
3529 /* A subroutine of gimplify_init_constructor. Generate individual
3530 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3531 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3532 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3533 zeroed first. */
3535 static void
3536 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3537 gimple_seq *pre_p, bool cleared)
3539 tree array_elt_type = NULL;
3540 unsigned HOST_WIDE_INT ix;
3541 tree purpose, value;
3543 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3544 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3546 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3548 tree cref;
3550 /* NULL values are created above for gimplification errors. */
3551 if (value == NULL)
3552 continue;
3554 if (cleared && initializer_zerop (value))
3555 continue;
3557 /* ??? Here's to hoping the front end fills in all of the indices,
3558 so we don't have to figure out what's missing ourselves. */
3559 gcc_assert (purpose);
3561 /* Skip zero-sized fields, unless value has side-effects. This can
3562 happen with calls to functions returning a zero-sized type, which
3563 we shouldn't discard. As a number of downstream passes don't
3564 expect sets of zero-sized fields, we rely on the gimplification of
3565 the MODIFY_EXPR we make below to drop the assignment statement. */
3566 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3567 continue;
3569 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3570 whole range. */
3571 if (TREE_CODE (purpose) == RANGE_EXPR)
3573 tree lower = TREE_OPERAND (purpose, 0);
3574 tree upper = TREE_OPERAND (purpose, 1);
3576 /* If the lower bound is equal to upper, just treat it as if
3577 upper was the index. */
3578 if (simple_cst_equal (lower, upper))
3579 purpose = upper;
3580 else
3582 gimplify_init_ctor_eval_range (object, lower, upper, value,
3583 array_elt_type, pre_p, cleared);
3584 continue;
3588 if (array_elt_type)
3590 /* Do not use bitsizetype for ARRAY_REF indices. */
3591 if (TYPE_DOMAIN (TREE_TYPE (object)))
3592 purpose
3593 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3594 purpose);
3595 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3596 purpose, NULL_TREE, NULL_TREE);
3598 else
3600 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3601 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3602 unshare_expr (object), purpose, NULL_TREE);
3605 if (TREE_CODE (value) == CONSTRUCTOR
3606 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3607 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3608 pre_p, cleared);
3609 else
3611 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3612 gimplify_and_add (init, pre_p);
3613 ggc_free (init);
3618 /* Return the appropriate RHS predicate for this LHS. */
3620 gimple_predicate
3621 rhs_predicate_for (tree lhs)
3623 if (is_gimple_reg (lhs))
3624 return is_gimple_reg_rhs_or_call;
3625 else
3626 return is_gimple_mem_rhs_or_call;
3629 /* Gimplify a C99 compound literal expression. This just means adding
3630 the DECL_EXPR before the current statement and using its anonymous
3631 decl instead. */
3633 static enum gimplify_status
3634 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3635 bool (*gimple_test_f) (tree),
3636 fallback_t fallback)
3638 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3639 tree decl = DECL_EXPR_DECL (decl_s);
3640 tree init = DECL_INITIAL (decl);
3641 /* Mark the decl as addressable if the compound literal
3642 expression is addressable now, otherwise it is marked too late
3643 after we gimplify the initialization expression. */
3644 if (TREE_ADDRESSABLE (*expr_p))
3645 TREE_ADDRESSABLE (decl) = 1;
3646 /* Otherwise, if we don't need an lvalue and have a literal directly
3647 substitute it. Check if it matches the gimple predicate, as
3648 otherwise we'd generate a new temporary, and we can as well just
3649 use the decl we already have. */
3650 else if (!TREE_ADDRESSABLE (decl)
3651 && init
3652 && (fallback & fb_lvalue) == 0
3653 && gimple_test_f (init))
3655 *expr_p = init;
3656 return GS_OK;
3659 /* Preliminarily mark non-addressed complex variables as eligible
3660 for promotion to gimple registers. We'll transform their uses
3661 as we find them. */
3662 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3663 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3664 && !TREE_THIS_VOLATILE (decl)
3665 && !needs_to_live_in_memory (decl))
3666 DECL_GIMPLE_REG_P (decl) = 1;
3668 /* If the decl is not addressable, then it is being used in some
3669 expression or on the right hand side of a statement, and it can
3670 be put into a readonly data section. */
3671 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3672 TREE_READONLY (decl) = 1;
3674 /* This decl isn't mentioned in the enclosing block, so add it to the
3675 list of temps. FIXME it seems a bit of a kludge to say that
3676 anonymous artificial vars aren't pushed, but everything else is. */
3677 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3678 gimple_add_tmp_var (decl);
3680 gimplify_and_add (decl_s, pre_p);
3681 *expr_p = decl;
3682 return GS_OK;
3685 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3686 return a new CONSTRUCTOR if something changed. */
3688 static tree
3689 optimize_compound_literals_in_ctor (tree orig_ctor)
3691 tree ctor = orig_ctor;
3692 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3693 unsigned int idx, num = vec_safe_length (elts);
3695 for (idx = 0; idx < num; idx++)
3697 tree value = (*elts)[idx].value;
3698 tree newval = value;
3699 if (TREE_CODE (value) == CONSTRUCTOR)
3700 newval = optimize_compound_literals_in_ctor (value);
3701 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3703 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3704 tree decl = DECL_EXPR_DECL (decl_s);
3705 tree init = DECL_INITIAL (decl);
3707 if (!TREE_ADDRESSABLE (value)
3708 && !TREE_ADDRESSABLE (decl)
3709 && init
3710 && TREE_CODE (init) == CONSTRUCTOR)
3711 newval = optimize_compound_literals_in_ctor (init);
3713 if (newval == value)
3714 continue;
3716 if (ctor == orig_ctor)
3718 ctor = copy_node (orig_ctor);
3719 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3720 elts = CONSTRUCTOR_ELTS (ctor);
3722 (*elts)[idx].value = newval;
3724 return ctor;
3727 /* A subroutine of gimplify_modify_expr. Break out elements of a
3728 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3730 Note that we still need to clear any elements that don't have explicit
3731 initializers, so if not all elements are initialized we keep the
3732 original MODIFY_EXPR, we just remove all of the constructor elements.
3734 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3735 GS_ERROR if we would have to create a temporary when gimplifying
3736 this constructor. Otherwise, return GS_OK.
3738 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3740 static enum gimplify_status
3741 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3742 bool want_value, bool notify_temp_creation)
3744 tree object, ctor, type;
3745 enum gimplify_status ret;
3746 vec<constructor_elt, va_gc> *elts;
3748 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3750 if (!notify_temp_creation)
3752 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3753 is_gimple_lvalue, fb_lvalue);
3754 if (ret == GS_ERROR)
3755 return ret;
3758 object = TREE_OPERAND (*expr_p, 0);
3759 ctor = TREE_OPERAND (*expr_p, 1) =
3760 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3761 type = TREE_TYPE (ctor);
3762 elts = CONSTRUCTOR_ELTS (ctor);
3763 ret = GS_ALL_DONE;
3765 switch (TREE_CODE (type))
3767 case RECORD_TYPE:
3768 case UNION_TYPE:
3769 case QUAL_UNION_TYPE:
3770 case ARRAY_TYPE:
3772 struct gimplify_init_ctor_preeval_data preeval_data;
3773 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3774 bool cleared, complete_p, valid_const_initializer;
3776 /* Aggregate types must lower constructors to initialization of
3777 individual elements. The exception is that a CONSTRUCTOR node
3778 with no elements indicates zero-initialization of the whole. */
3779 if (vec_safe_is_empty (elts))
3781 if (notify_temp_creation)
3782 return GS_OK;
3783 break;
3786 /* Fetch information about the constructor to direct later processing.
3787 We might want to make static versions of it in various cases, and
3788 can only do so if it known to be a valid constant initializer. */
3789 valid_const_initializer
3790 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3791 &num_ctor_elements, &complete_p);
3793 /* If a const aggregate variable is being initialized, then it
3794 should never be a lose to promote the variable to be static. */
3795 if (valid_const_initializer
3796 && num_nonzero_elements > 1
3797 && TREE_READONLY (object)
3798 && TREE_CODE (object) == VAR_DECL
3799 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3801 if (notify_temp_creation)
3802 return GS_ERROR;
3803 DECL_INITIAL (object) = ctor;
3804 TREE_STATIC (object) = 1;
3805 if (!DECL_NAME (object))
3806 DECL_NAME (object) = create_tmp_var_name ("C");
3807 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3809 /* ??? C++ doesn't automatically append a .<number> to the
3810 assembler name, and even when it does, it looks at FE private
3811 data structures to figure out what that number should be,
3812 which are not set for this variable. I suppose this is
3813 important for local statics for inline functions, which aren't
3814 "local" in the object file sense. So in order to get a unique
3815 TU-local symbol, we must invoke the lhd version now. */
3816 lhd_set_decl_assembler_name (object);
3818 *expr_p = NULL_TREE;
3819 break;
3822 /* If there are "lots" of initialized elements, even discounting
3823 those that are not address constants (and thus *must* be
3824 computed at runtime), then partition the constructor into
3825 constant and non-constant parts. Block copy the constant
3826 parts in, then generate code for the non-constant parts. */
3827 /* TODO. There's code in cp/typeck.c to do this. */
3829 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3830 /* store_constructor will ignore the clearing of variable-sized
3831 objects. Initializers for such objects must explicitly set
3832 every field that needs to be set. */
3833 cleared = false;
3834 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3835 /* If the constructor isn't complete, clear the whole object
3836 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3838 ??? This ought not to be needed. For any element not present
3839 in the initializer, we should simply set them to zero. Except
3840 we'd need to *find* the elements that are not present, and that
3841 requires trickery to avoid quadratic compile-time behavior in
3842 large cases or excessive memory use in small cases. */
3843 cleared = true;
3844 else if (num_ctor_elements - num_nonzero_elements
3845 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3846 && num_nonzero_elements < num_ctor_elements / 4)
3847 /* If there are "lots" of zeros, it's more efficient to clear
3848 the memory and then set the nonzero elements. */
3849 cleared = true;
3850 else
3851 cleared = false;
3853 /* If there are "lots" of initialized elements, and all of them
3854 are valid address constants, then the entire initializer can
3855 be dropped to memory, and then memcpy'd out. Don't do this
3856 for sparse arrays, though, as it's more efficient to follow
3857 the standard CONSTRUCTOR behavior of memset followed by
3858 individual element initialization. Also don't do this for small
3859 all-zero initializers (which aren't big enough to merit
3860 clearing), and don't try to make bitwise copies of
3861 TREE_ADDRESSABLE types. */
3862 if (valid_const_initializer
3863 && !(cleared || num_nonzero_elements == 0)
3864 && !TREE_ADDRESSABLE (type))
3866 HOST_WIDE_INT size = int_size_in_bytes (type);
3867 unsigned int align;
3869 /* ??? We can still get unbounded array types, at least
3870 from the C++ front end. This seems wrong, but attempt
3871 to work around it for now. */
3872 if (size < 0)
3874 size = int_size_in_bytes (TREE_TYPE (object));
3875 if (size >= 0)
3876 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3879 /* Find the maximum alignment we can assume for the object. */
3880 /* ??? Make use of DECL_OFFSET_ALIGN. */
3881 if (DECL_P (object))
3882 align = DECL_ALIGN (object);
3883 else
3884 align = TYPE_ALIGN (type);
3886 /* Do a block move either if the size is so small as to make
3887 each individual move a sub-unit move on average, or if it
3888 is so large as to make individual moves inefficient. */
3889 if (size > 0
3890 && num_nonzero_elements > 1
3891 && (size < num_nonzero_elements
3892 || !can_move_by_pieces (size, align)))
3894 if (notify_temp_creation)
3895 return GS_ERROR;
3897 walk_tree (&ctor, force_labels_r, NULL, NULL);
3898 ctor = tree_output_constant_def (ctor);
3899 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3900 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3901 TREE_OPERAND (*expr_p, 1) = ctor;
3903 /* This is no longer an assignment of a CONSTRUCTOR, but
3904 we still may have processing to do on the LHS. So
3905 pretend we didn't do anything here to let that happen. */
3906 return GS_UNHANDLED;
3910 /* If the target is volatile, we have non-zero elements and more than
3911 one field to assign, initialize the target from a temporary. */
3912 if (TREE_THIS_VOLATILE (object)
3913 && !TREE_ADDRESSABLE (type)
3914 && num_nonzero_elements > 0
3915 && vec_safe_length (elts) > 1)
3917 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3918 TREE_OPERAND (*expr_p, 0) = temp;
3919 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3920 *expr_p,
3921 build2 (MODIFY_EXPR, void_type_node,
3922 object, temp));
3923 return GS_OK;
3926 if (notify_temp_creation)
3927 return GS_OK;
3929 /* If there are nonzero elements and if needed, pre-evaluate to capture
3930 elements overlapping with the lhs into temporaries. We must do this
3931 before clearing to fetch the values before they are zeroed-out. */
3932 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3934 preeval_data.lhs_base_decl = get_base_address (object);
3935 if (!DECL_P (preeval_data.lhs_base_decl))
3936 preeval_data.lhs_base_decl = NULL;
3937 preeval_data.lhs_alias_set = get_alias_set (object);
3939 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3940 pre_p, post_p, &preeval_data);
3943 if (cleared)
3945 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3946 Note that we still have to gimplify, in order to handle the
3947 case of variable sized types. Avoid shared tree structures. */
3948 CONSTRUCTOR_ELTS (ctor) = NULL;
3949 TREE_SIDE_EFFECTS (ctor) = 0;
3950 object = unshare_expr (object);
3951 gimplify_stmt (expr_p, pre_p);
3954 /* If we have not block cleared the object, or if there are nonzero
3955 elements in the constructor, add assignments to the individual
3956 scalar fields of the object. */
3957 if (!cleared || num_nonzero_elements > 0)
3958 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3960 *expr_p = NULL_TREE;
3962 break;
3964 case COMPLEX_TYPE:
3966 tree r, i;
3968 if (notify_temp_creation)
3969 return GS_OK;
3971 /* Extract the real and imaginary parts out of the ctor. */
3972 gcc_assert (elts->length () == 2);
3973 r = (*elts)[0].value;
3974 i = (*elts)[1].value;
3975 if (r == NULL || i == NULL)
3977 tree zero = build_zero_cst (TREE_TYPE (type));
3978 if (r == NULL)
3979 r = zero;
3980 if (i == NULL)
3981 i = zero;
3984 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3985 represent creation of a complex value. */
3986 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3988 ctor = build_complex (type, r, i);
3989 TREE_OPERAND (*expr_p, 1) = ctor;
3991 else
3993 ctor = build2 (COMPLEX_EXPR, type, r, i);
3994 TREE_OPERAND (*expr_p, 1) = ctor;
3995 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3996 pre_p,
3997 post_p,
3998 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3999 fb_rvalue);
4002 break;
4004 case VECTOR_TYPE:
4006 unsigned HOST_WIDE_INT ix;
4007 constructor_elt *ce;
4009 if (notify_temp_creation)
4010 return GS_OK;
4012 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4013 if (TREE_CONSTANT (ctor))
4015 bool constant_p = true;
4016 tree value;
4018 /* Even when ctor is constant, it might contain non-*_CST
4019 elements, such as addresses or trapping values like
4020 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4021 in VECTOR_CST nodes. */
4022 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4023 if (!CONSTANT_CLASS_P (value))
4025 constant_p = false;
4026 break;
4029 if (constant_p)
4031 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4032 break;
4035 TREE_CONSTANT (ctor) = 0;
4038 /* Vector types use CONSTRUCTOR all the way through gimple
4039 compilation as a general initializer. */
4040 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4042 enum gimplify_status tret;
4043 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4044 fb_rvalue);
4045 if (tret == GS_ERROR)
4046 ret = GS_ERROR;
4048 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4049 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4051 break;
4053 default:
4054 /* So how did we get a CONSTRUCTOR for a scalar type? */
4055 gcc_unreachable ();
4058 if (ret == GS_ERROR)
4059 return GS_ERROR;
4060 else if (want_value)
4062 *expr_p = object;
4063 return GS_OK;
4065 else
4067 /* If we have gimplified both sides of the initializer but have
4068 not emitted an assignment, do so now. */
4069 if (*expr_p)
4071 tree lhs = TREE_OPERAND (*expr_p, 0);
4072 tree rhs = TREE_OPERAND (*expr_p, 1);
4073 gimple init = gimple_build_assign (lhs, rhs);
4074 gimplify_seq_add_stmt (pre_p, init);
4075 *expr_p = NULL;
4078 return GS_ALL_DONE;
4082 /* Given a pointer value OP0, return a simplified version of an
4083 indirection through OP0, or NULL_TREE if no simplification is
4084 possible. This may only be applied to a rhs of an expression.
4085 Note that the resulting type may be different from the type pointed
4086 to in the sense that it is still compatible from the langhooks
4087 point of view. */
4089 static tree
4090 gimple_fold_indirect_ref_rhs (tree t)
4092 return gimple_fold_indirect_ref (t);
4095 /* Subroutine of gimplify_modify_expr to do simplifications of
4096 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4097 something changes. */
4099 static enum gimplify_status
4100 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4101 gimple_seq *pre_p, gimple_seq *post_p,
4102 bool want_value)
4104 enum gimplify_status ret = GS_UNHANDLED;
4105 bool changed;
4109 changed = false;
4110 switch (TREE_CODE (*from_p))
4112 case VAR_DECL:
4113 /* If we're assigning from a read-only variable initialized with
4114 a constructor, do the direct assignment from the constructor,
4115 but only if neither source nor target are volatile since this
4116 latter assignment might end up being done on a per-field basis. */
4117 if (DECL_INITIAL (*from_p)
4118 && TREE_READONLY (*from_p)
4119 && !TREE_THIS_VOLATILE (*from_p)
4120 && !TREE_THIS_VOLATILE (*to_p)
4121 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4123 tree old_from = *from_p;
4124 enum gimplify_status subret;
4126 /* Move the constructor into the RHS. */
4127 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4129 /* Let's see if gimplify_init_constructor will need to put
4130 it in memory. */
4131 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4132 false, true);
4133 if (subret == GS_ERROR)
4135 /* If so, revert the change. */
4136 *from_p = old_from;
4138 else
4140 ret = GS_OK;
4141 changed = true;
4144 break;
4145 case INDIRECT_REF:
4147 /* If we have code like
4149 *(const A*)(A*)&x
4151 where the type of "x" is a (possibly cv-qualified variant
4152 of "A"), treat the entire expression as identical to "x".
4153 This kind of code arises in C++ when an object is bound
4154 to a const reference, and if "x" is a TARGET_EXPR we want
4155 to take advantage of the optimization below. */
4156 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4157 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4158 if (t)
4160 if (TREE_THIS_VOLATILE (t) != volatile_p)
4162 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4163 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4164 build_fold_addr_expr (t));
4165 if (REFERENCE_CLASS_P (t))
4166 TREE_THIS_VOLATILE (t) = volatile_p;
4168 *from_p = t;
4169 ret = GS_OK;
4170 changed = true;
4172 break;
4175 case TARGET_EXPR:
4177 /* If we are initializing something from a TARGET_EXPR, strip the
4178 TARGET_EXPR and initialize it directly, if possible. This can't
4179 be done if the initializer is void, since that implies that the
4180 temporary is set in some non-trivial way.
4182 ??? What about code that pulls out the temp and uses it
4183 elsewhere? I think that such code never uses the TARGET_EXPR as
4184 an initializer. If I'm wrong, we'll die because the temp won't
4185 have any RTL. In that case, I guess we'll need to replace
4186 references somehow. */
4187 tree init = TARGET_EXPR_INITIAL (*from_p);
4189 if (init
4190 && !VOID_TYPE_P (TREE_TYPE (init)))
4192 *from_p = init;
4193 ret = GS_OK;
4194 changed = true;
4197 break;
4199 case COMPOUND_EXPR:
4200 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4201 caught. */
4202 gimplify_compound_expr (from_p, pre_p, true);
4203 ret = GS_OK;
4204 changed = true;
4205 break;
4207 case CONSTRUCTOR:
4208 /* If we already made some changes, let the front end have a
4209 crack at this before we break it down. */
4210 if (ret != GS_UNHANDLED)
4211 break;
4212 /* If we're initializing from a CONSTRUCTOR, break this into
4213 individual MODIFY_EXPRs. */
4214 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4215 false);
4217 case COND_EXPR:
4218 /* If we're assigning to a non-register type, push the assignment
4219 down into the branches. This is mandatory for ADDRESSABLE types,
4220 since we cannot generate temporaries for such, but it saves a
4221 copy in other cases as well. */
4222 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4224 /* This code should mirror the code in gimplify_cond_expr. */
4225 enum tree_code code = TREE_CODE (*expr_p);
4226 tree cond = *from_p;
4227 tree result = *to_p;
4229 ret = gimplify_expr (&result, pre_p, post_p,
4230 is_gimple_lvalue, fb_lvalue);
4231 if (ret != GS_ERROR)
4232 ret = GS_OK;
4234 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4235 TREE_OPERAND (cond, 1)
4236 = build2 (code, void_type_node, result,
4237 TREE_OPERAND (cond, 1));
4238 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4239 TREE_OPERAND (cond, 2)
4240 = build2 (code, void_type_node, unshare_expr (result),
4241 TREE_OPERAND (cond, 2));
4243 TREE_TYPE (cond) = void_type_node;
4244 recalculate_side_effects (cond);
4246 if (want_value)
4248 gimplify_and_add (cond, pre_p);
4249 *expr_p = unshare_expr (result);
4251 else
4252 *expr_p = cond;
4253 return ret;
4255 break;
4257 case CALL_EXPR:
4258 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4259 return slot so that we don't generate a temporary. */
4260 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4261 && aggregate_value_p (*from_p, *from_p))
4263 bool use_target;
4265 if (!(rhs_predicate_for (*to_p))(*from_p))
4266 /* If we need a temporary, *to_p isn't accurate. */
4267 use_target = false;
4268 /* It's OK to use the return slot directly unless it's an NRV. */
4269 else if (TREE_CODE (*to_p) == RESULT_DECL
4270 && DECL_NAME (*to_p) == NULL_TREE
4271 && needs_to_live_in_memory (*to_p))
4272 use_target = true;
4273 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4274 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4275 /* Don't force regs into memory. */
4276 use_target = false;
4277 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4278 /* It's OK to use the target directly if it's being
4279 initialized. */
4280 use_target = true;
4281 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4282 /* Always use the target and thus RSO for variable-sized types.
4283 GIMPLE cannot deal with a variable-sized assignment
4284 embedded in a call statement. */
4285 use_target = true;
4286 else if (TREE_CODE (*to_p) != SSA_NAME
4287 && (!is_gimple_variable (*to_p)
4288 || needs_to_live_in_memory (*to_p)))
4289 /* Don't use the original target if it's already addressable;
4290 if its address escapes, and the called function uses the
4291 NRV optimization, a conforming program could see *to_p
4292 change before the called function returns; see c++/19317.
4293 When optimizing, the return_slot pass marks more functions
4294 as safe after we have escape info. */
4295 use_target = false;
4296 else
4297 use_target = true;
4299 if (use_target)
4301 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4302 mark_addressable (*to_p);
4305 break;
4307 case WITH_SIZE_EXPR:
4308 /* Likewise for calls that return an aggregate of non-constant size,
4309 since we would not be able to generate a temporary at all. */
4310 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4312 *from_p = TREE_OPERAND (*from_p, 0);
4313 /* We don't change ret in this case because the
4314 WITH_SIZE_EXPR might have been added in
4315 gimplify_modify_expr, so returning GS_OK would lead to an
4316 infinite loop. */
4317 changed = true;
4319 break;
4321 /* If we're initializing from a container, push the initialization
4322 inside it. */
4323 case CLEANUP_POINT_EXPR:
4324 case BIND_EXPR:
4325 case STATEMENT_LIST:
4327 tree wrap = *from_p;
4328 tree t;
4330 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4331 fb_lvalue);
4332 if (ret != GS_ERROR)
4333 ret = GS_OK;
4335 t = voidify_wrapper_expr (wrap, *expr_p);
4336 gcc_assert (t == *expr_p);
4338 if (want_value)
4340 gimplify_and_add (wrap, pre_p);
4341 *expr_p = unshare_expr (*to_p);
4343 else
4344 *expr_p = wrap;
4345 return GS_OK;
4348 case COMPOUND_LITERAL_EXPR:
4350 tree complit = TREE_OPERAND (*expr_p, 1);
4351 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4352 tree decl = DECL_EXPR_DECL (decl_s);
4353 tree init = DECL_INITIAL (decl);
4355 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4356 into struct T x = { 0, 1, 2 } if the address of the
4357 compound literal has never been taken. */
4358 if (!TREE_ADDRESSABLE (complit)
4359 && !TREE_ADDRESSABLE (decl)
4360 && init)
4362 *expr_p = copy_node (*expr_p);
4363 TREE_OPERAND (*expr_p, 1) = init;
4364 return GS_OK;
4368 default:
4369 break;
4372 while (changed);
4374 return ret;
4378 /* Return true if T looks like a valid GIMPLE statement. */
4380 static bool
4381 is_gimple_stmt (tree t)
4383 const enum tree_code code = TREE_CODE (t);
4385 switch (code)
4387 case NOP_EXPR:
4388 /* The only valid NOP_EXPR is the empty statement. */
4389 return IS_EMPTY_STMT (t);
4391 case BIND_EXPR:
4392 case COND_EXPR:
4393 /* These are only valid if they're void. */
4394 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4396 case SWITCH_EXPR:
4397 case GOTO_EXPR:
4398 case RETURN_EXPR:
4399 case LABEL_EXPR:
4400 case CASE_LABEL_EXPR:
4401 case TRY_CATCH_EXPR:
4402 case TRY_FINALLY_EXPR:
4403 case EH_FILTER_EXPR:
4404 case CATCH_EXPR:
4405 case ASM_EXPR:
4406 case STATEMENT_LIST:
4407 case OMP_PARALLEL:
4408 case OMP_FOR:
4409 case OMP_SIMD:
4410 case CILK_SIMD:
4411 case OMP_DISTRIBUTE:
4412 case OMP_SECTIONS:
4413 case OMP_SECTION:
4414 case OMP_SINGLE:
4415 case OMP_MASTER:
4416 case OMP_TASKGROUP:
4417 case OMP_ORDERED:
4418 case OMP_CRITICAL:
4419 case OMP_TASK:
4420 /* These are always void. */
4421 return true;
4423 case CALL_EXPR:
4424 case MODIFY_EXPR:
4425 case PREDICT_EXPR:
4426 /* These are valid regardless of their type. */
4427 return true;
4429 default:
4430 return false;
4435 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4436 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4437 DECL_GIMPLE_REG_P set.
4439 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4440 other, unmodified part of the complex object just before the total store.
4441 As a consequence, if the object is still uninitialized, an undefined value
4442 will be loaded into a register, which may result in a spurious exception
4443 if the register is floating-point and the value happens to be a signaling
4444 NaN for example. Then the fully-fledged complex operations lowering pass
4445 followed by a DCE pass are necessary in order to fix things up. */
4447 static enum gimplify_status
4448 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4449 bool want_value)
4451 enum tree_code code, ocode;
4452 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4454 lhs = TREE_OPERAND (*expr_p, 0);
4455 rhs = TREE_OPERAND (*expr_p, 1);
4456 code = TREE_CODE (lhs);
4457 lhs = TREE_OPERAND (lhs, 0);
4459 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4460 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4461 TREE_NO_WARNING (other) = 1;
4462 other = get_formal_tmp_var (other, pre_p);
4464 realpart = code == REALPART_EXPR ? rhs : other;
4465 imagpart = code == REALPART_EXPR ? other : rhs;
4467 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4468 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4469 else
4470 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4472 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4473 *expr_p = (want_value) ? rhs : NULL_TREE;
4475 return GS_ALL_DONE;
4478 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4480 modify_expr
4481 : varname '=' rhs
4482 | '*' ID '=' rhs
4484 PRE_P points to the list where side effects that must happen before
4485 *EXPR_P should be stored.
4487 POST_P points to the list where side effects that must happen after
4488 *EXPR_P should be stored.
4490 WANT_VALUE is nonzero iff we want to use the value of this expression
4491 in another expression. */
4493 static enum gimplify_status
4494 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4495 bool want_value)
4497 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4498 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4499 enum gimplify_status ret = GS_UNHANDLED;
4500 gimple assign;
4501 location_t loc = EXPR_LOCATION (*expr_p);
4502 gimple_stmt_iterator gsi;
4504 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4505 || TREE_CODE (*expr_p) == INIT_EXPR);
4507 /* Trying to simplify a clobber using normal logic doesn't work,
4508 so handle it here. */
4509 if (TREE_CLOBBER_P (*from_p))
4511 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4512 if (ret == GS_ERROR)
4513 return ret;
4514 gcc_assert (!want_value
4515 && (TREE_CODE (*to_p) == VAR_DECL
4516 || TREE_CODE (*to_p) == MEM_REF));
4517 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4518 *expr_p = NULL;
4519 return GS_ALL_DONE;
4522 /* Insert pointer conversions required by the middle-end that are not
4523 required by the frontend. This fixes middle-end type checking for
4524 for example gcc.dg/redecl-6.c. */
4525 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4527 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4528 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4529 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4532 /* See if any simplifications can be done based on what the RHS is. */
4533 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4534 want_value);
4535 if (ret != GS_UNHANDLED)
4536 return ret;
4538 /* For zero sized types only gimplify the left hand side and right hand
4539 side as statements and throw away the assignment. Do this after
4540 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4541 types properly. */
4542 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4544 gimplify_stmt (from_p, pre_p);
4545 gimplify_stmt (to_p, pre_p);
4546 *expr_p = NULL_TREE;
4547 return GS_ALL_DONE;
4550 /* If the value being copied is of variable width, compute the length
4551 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4552 before gimplifying any of the operands so that we can resolve any
4553 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4554 the size of the expression to be copied, not of the destination, so
4555 that is what we must do here. */
4556 maybe_with_size_expr (from_p);
4558 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4559 if (ret == GS_ERROR)
4560 return ret;
4562 /* As a special case, we have to temporarily allow for assignments
4563 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4564 a toplevel statement, when gimplifying the GENERIC expression
4565 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4566 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4568 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4569 prevent gimplify_expr from trying to create a new temporary for
4570 foo's LHS, we tell it that it should only gimplify until it
4571 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4572 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4573 and all we need to do here is set 'a' to be its LHS. */
4574 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4575 fb_rvalue);
4576 if (ret == GS_ERROR)
4577 return ret;
4579 /* Now see if the above changed *from_p to something we handle specially. */
4580 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4581 want_value);
4582 if (ret != GS_UNHANDLED)
4583 return ret;
4585 /* If we've got a variable sized assignment between two lvalues (i.e. does
4586 not involve a call), then we can make things a bit more straightforward
4587 by converting the assignment to memcpy or memset. */
4588 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4590 tree from = TREE_OPERAND (*from_p, 0);
4591 tree size = TREE_OPERAND (*from_p, 1);
4593 if (TREE_CODE (from) == CONSTRUCTOR)
4594 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4596 if (is_gimple_addressable (from))
4598 *from_p = from;
4599 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4600 pre_p);
4604 /* Transform partial stores to non-addressable complex variables into
4605 total stores. This allows us to use real instead of virtual operands
4606 for these variables, which improves optimization. */
4607 if ((TREE_CODE (*to_p) == REALPART_EXPR
4608 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4609 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4610 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4612 /* Try to alleviate the effects of the gimplification creating artificial
4613 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4614 if (!gimplify_ctxp->into_ssa
4615 && TREE_CODE (*from_p) == VAR_DECL
4616 && DECL_IGNORED_P (*from_p)
4617 && DECL_P (*to_p)
4618 && !DECL_IGNORED_P (*to_p))
4620 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4621 DECL_NAME (*from_p)
4622 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4623 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4624 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4627 if (want_value && TREE_THIS_VOLATILE (*to_p))
4628 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4630 if (TREE_CODE (*from_p) == CALL_EXPR)
4632 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4633 instead of a GIMPLE_ASSIGN. */
4634 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4635 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4636 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4637 tree fndecl = get_callee_fndecl (*from_p);
4638 if (fndecl
4639 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4640 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4641 && call_expr_nargs (*from_p) == 3)
4642 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4643 CALL_EXPR_ARG (*from_p, 0),
4644 CALL_EXPR_ARG (*from_p, 1),
4645 CALL_EXPR_ARG (*from_p, 2));
4646 else
4648 assign = gimple_build_call_from_tree (*from_p);
4649 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4651 notice_special_calls (assign);
4652 if (!gimple_call_noreturn_p (assign))
4653 gimple_call_set_lhs (assign, *to_p);
4655 else
4657 assign = gimple_build_assign (*to_p, *from_p);
4658 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4661 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4663 /* We should have got an SSA name from the start. */
4664 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4667 gimplify_seq_add_stmt (pre_p, assign);
4668 gsi = gsi_last (*pre_p);
4669 maybe_fold_stmt (&gsi);
4671 if (want_value)
4673 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4674 return GS_OK;
4676 else
4677 *expr_p = NULL;
4679 return GS_ALL_DONE;
4682 /* Gimplify a comparison between two variable-sized objects. Do this
4683 with a call to BUILT_IN_MEMCMP. */
4685 static enum gimplify_status
4686 gimplify_variable_sized_compare (tree *expr_p)
4688 location_t loc = EXPR_LOCATION (*expr_p);
4689 tree op0 = TREE_OPERAND (*expr_p, 0);
4690 tree op1 = TREE_OPERAND (*expr_p, 1);
4691 tree t, arg, dest, src, expr;
4693 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4694 arg = unshare_expr (arg);
4695 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4696 src = build_fold_addr_expr_loc (loc, op1);
4697 dest = build_fold_addr_expr_loc (loc, op0);
4698 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4699 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4701 expr
4702 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4703 SET_EXPR_LOCATION (expr, loc);
4704 *expr_p = expr;
4706 return GS_OK;
4709 /* Gimplify a comparison between two aggregate objects of integral scalar
4710 mode as a comparison between the bitwise equivalent scalar values. */
4712 static enum gimplify_status
4713 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4715 location_t loc = EXPR_LOCATION (*expr_p);
4716 tree op0 = TREE_OPERAND (*expr_p, 0);
4717 tree op1 = TREE_OPERAND (*expr_p, 1);
4719 tree type = TREE_TYPE (op0);
4720 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4722 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4723 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4725 *expr_p
4726 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4728 return GS_OK;
4731 /* Gimplify an expression sequence. This function gimplifies each
4732 expression and rewrites the original expression with the last
4733 expression of the sequence in GIMPLE form.
4735 PRE_P points to the list where the side effects for all the
4736 expressions in the sequence will be emitted.
4738 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4740 static enum gimplify_status
4741 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4743 tree t = *expr_p;
4747 tree *sub_p = &TREE_OPERAND (t, 0);
4749 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4750 gimplify_compound_expr (sub_p, pre_p, false);
4751 else
4752 gimplify_stmt (sub_p, pre_p);
4754 t = TREE_OPERAND (t, 1);
4756 while (TREE_CODE (t) == COMPOUND_EXPR);
4758 *expr_p = t;
4759 if (want_value)
4760 return GS_OK;
4761 else
4763 gimplify_stmt (expr_p, pre_p);
4764 return GS_ALL_DONE;
4768 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4769 gimplify. After gimplification, EXPR_P will point to a new temporary
4770 that holds the original value of the SAVE_EXPR node.
4772 PRE_P points to the list where side effects that must happen before
4773 *EXPR_P should be stored. */
4775 static enum gimplify_status
4776 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4778 enum gimplify_status ret = GS_ALL_DONE;
4779 tree val;
4781 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4782 val = TREE_OPERAND (*expr_p, 0);
4784 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4785 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4787 /* The operand may be a void-valued expression such as SAVE_EXPRs
4788 generated by the Java frontend for class initialization. It is
4789 being executed only for its side-effects. */
4790 if (TREE_TYPE (val) == void_type_node)
4792 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4793 is_gimple_stmt, fb_none);
4794 val = NULL;
4796 else
4797 val = get_initialized_tmp_var (val, pre_p, post_p);
4799 TREE_OPERAND (*expr_p, 0) = val;
4800 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4803 *expr_p = val;
4805 return ret;
4808 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4810 unary_expr
4811 : ...
4812 | '&' varname
4815 PRE_P points to the list where side effects that must happen before
4816 *EXPR_P should be stored.
4818 POST_P points to the list where side effects that must happen after
4819 *EXPR_P should be stored. */
4821 static enum gimplify_status
4822 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4824 tree expr = *expr_p;
4825 tree op0 = TREE_OPERAND (expr, 0);
4826 enum gimplify_status ret;
4827 location_t loc = EXPR_LOCATION (*expr_p);
4829 switch (TREE_CODE (op0))
4831 case INDIRECT_REF:
4832 do_indirect_ref:
4833 /* Check if we are dealing with an expression of the form '&*ptr'.
4834 While the front end folds away '&*ptr' into 'ptr', these
4835 expressions may be generated internally by the compiler (e.g.,
4836 builtins like __builtin_va_end). */
4837 /* Caution: the silent array decomposition semantics we allow for
4838 ADDR_EXPR means we can't always discard the pair. */
4839 /* Gimplification of the ADDR_EXPR operand may drop
4840 cv-qualification conversions, so make sure we add them if
4841 needed. */
4843 tree op00 = TREE_OPERAND (op0, 0);
4844 tree t_expr = TREE_TYPE (expr);
4845 tree t_op00 = TREE_TYPE (op00);
4847 if (!useless_type_conversion_p (t_expr, t_op00))
4848 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4849 *expr_p = op00;
4850 ret = GS_OK;
4852 break;
4854 case VIEW_CONVERT_EXPR:
4855 /* Take the address of our operand and then convert it to the type of
4856 this ADDR_EXPR.
4858 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4859 all clear. The impact of this transformation is even less clear. */
4861 /* If the operand is a useless conversion, look through it. Doing so
4862 guarantees that the ADDR_EXPR and its operand will remain of the
4863 same type. */
4864 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4865 op0 = TREE_OPERAND (op0, 0);
4867 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4868 build_fold_addr_expr_loc (loc,
4869 TREE_OPERAND (op0, 0)));
4870 ret = GS_OK;
4871 break;
4873 default:
4874 /* We use fb_either here because the C frontend sometimes takes
4875 the address of a call that returns a struct; see
4876 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4877 the implied temporary explicit. */
4879 /* Make the operand addressable. */
4880 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4881 is_gimple_addressable, fb_either);
4882 if (ret == GS_ERROR)
4883 break;
4885 /* Then mark it. Beware that it may not be possible to do so directly
4886 if a temporary has been created by the gimplification. */
4887 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4889 op0 = TREE_OPERAND (expr, 0);
4891 /* For various reasons, the gimplification of the expression
4892 may have made a new INDIRECT_REF. */
4893 if (TREE_CODE (op0) == INDIRECT_REF)
4894 goto do_indirect_ref;
4896 mark_addressable (TREE_OPERAND (expr, 0));
4898 /* The FEs may end up building ADDR_EXPRs early on a decl with
4899 an incomplete type. Re-build ADDR_EXPRs in canonical form
4900 here. */
4901 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4902 *expr_p = build_fold_addr_expr (op0);
4904 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4905 recompute_tree_invariant_for_addr_expr (*expr_p);
4907 /* If we re-built the ADDR_EXPR add a conversion to the original type
4908 if required. */
4909 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4910 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4912 break;
4915 return ret;
4918 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4919 value; output operands should be a gimple lvalue. */
4921 static enum gimplify_status
4922 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4924 tree expr;
4925 int noutputs;
4926 const char **oconstraints;
4927 int i;
4928 tree link;
4929 const char *constraint;
4930 bool allows_mem, allows_reg, is_inout;
4931 enum gimplify_status ret, tret;
4932 gimple stmt;
4933 vec<tree, va_gc> *inputs;
4934 vec<tree, va_gc> *outputs;
4935 vec<tree, va_gc> *clobbers;
4936 vec<tree, va_gc> *labels;
4937 tree link_next;
4939 expr = *expr_p;
4940 noutputs = list_length (ASM_OUTPUTS (expr));
4941 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4943 inputs = NULL;
4944 outputs = NULL;
4945 clobbers = NULL;
4946 labels = NULL;
4948 ret = GS_ALL_DONE;
4949 link_next = NULL_TREE;
4950 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4952 bool ok;
4953 size_t constraint_len;
4955 link_next = TREE_CHAIN (link);
4957 oconstraints[i]
4958 = constraint
4959 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4960 constraint_len = strlen (constraint);
4961 if (constraint_len == 0)
4962 continue;
4964 ok = parse_output_constraint (&constraint, i, 0, 0,
4965 &allows_mem, &allows_reg, &is_inout);
4966 if (!ok)
4968 ret = GS_ERROR;
4969 is_inout = false;
4972 if (!allows_reg && allows_mem)
4973 mark_addressable (TREE_VALUE (link));
4975 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4976 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4977 fb_lvalue | fb_mayfail);
4978 if (tret == GS_ERROR)
4980 error ("invalid lvalue in asm output %d", i);
4981 ret = tret;
4984 vec_safe_push (outputs, link);
4985 TREE_CHAIN (link) = NULL_TREE;
4987 if (is_inout)
4989 /* An input/output operand. To give the optimizers more
4990 flexibility, split it into separate input and output
4991 operands. */
4992 tree input;
4993 char buf[10];
4995 /* Turn the in/out constraint into an output constraint. */
4996 char *p = xstrdup (constraint);
4997 p[0] = '=';
4998 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5000 /* And add a matching input constraint. */
5001 if (allows_reg)
5003 sprintf (buf, "%d", i);
5005 /* If there are multiple alternatives in the constraint,
5006 handle each of them individually. Those that allow register
5007 will be replaced with operand number, the others will stay
5008 unchanged. */
5009 if (strchr (p, ',') != NULL)
5011 size_t len = 0, buflen = strlen (buf);
5012 char *beg, *end, *str, *dst;
5014 for (beg = p + 1;;)
5016 end = strchr (beg, ',');
5017 if (end == NULL)
5018 end = strchr (beg, '\0');
5019 if ((size_t) (end - beg) < buflen)
5020 len += buflen + 1;
5021 else
5022 len += end - beg + 1;
5023 if (*end)
5024 beg = end + 1;
5025 else
5026 break;
5029 str = (char *) alloca (len);
5030 for (beg = p + 1, dst = str;;)
5032 const char *tem;
5033 bool mem_p, reg_p, inout_p;
5035 end = strchr (beg, ',');
5036 if (end)
5037 *end = '\0';
5038 beg[-1] = '=';
5039 tem = beg - 1;
5040 parse_output_constraint (&tem, i, 0, 0,
5041 &mem_p, &reg_p, &inout_p);
5042 if (dst != str)
5043 *dst++ = ',';
5044 if (reg_p)
5046 memcpy (dst, buf, buflen);
5047 dst += buflen;
5049 else
5051 if (end)
5052 len = end - beg;
5053 else
5054 len = strlen (beg);
5055 memcpy (dst, beg, len);
5056 dst += len;
5058 if (end)
5059 beg = end + 1;
5060 else
5061 break;
5063 *dst = '\0';
5064 input = build_string (dst - str, str);
5066 else
5067 input = build_string (strlen (buf), buf);
5069 else
5070 input = build_string (constraint_len - 1, constraint + 1);
5072 free (p);
5074 input = build_tree_list (build_tree_list (NULL_TREE, input),
5075 unshare_expr (TREE_VALUE (link)));
5076 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5080 link_next = NULL_TREE;
5081 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5083 link_next = TREE_CHAIN (link);
5084 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5085 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5086 oconstraints, &allows_mem, &allows_reg);
5088 /* If we can't make copies, we can only accept memory. */
5089 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5091 if (allows_mem)
5092 allows_reg = 0;
5093 else
5095 error ("impossible constraint in %<asm%>");
5096 error ("non-memory input %d must stay in memory", i);
5097 return GS_ERROR;
5101 /* If the operand is a memory input, it should be an lvalue. */
5102 if (!allows_reg && allows_mem)
5104 tree inputv = TREE_VALUE (link);
5105 STRIP_NOPS (inputv);
5106 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5107 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5108 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5109 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5110 TREE_VALUE (link) = error_mark_node;
5111 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5112 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5113 mark_addressable (TREE_VALUE (link));
5114 if (tret == GS_ERROR)
5116 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5117 input_location = EXPR_LOCATION (TREE_VALUE (link));
5118 error ("memory input %d is not directly addressable", i);
5119 ret = tret;
5122 else
5124 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5125 is_gimple_asm_val, fb_rvalue);
5126 if (tret == GS_ERROR)
5127 ret = tret;
5130 TREE_CHAIN (link) = NULL_TREE;
5131 vec_safe_push (inputs, link);
5134 link_next = NULL_TREE;
5135 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5137 link_next = TREE_CHAIN (link);
5138 TREE_CHAIN (link) = NULL_TREE;
5139 vec_safe_push (clobbers, link);
5142 link_next = NULL_TREE;
5143 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5145 link_next = TREE_CHAIN (link);
5146 TREE_CHAIN (link) = NULL_TREE;
5147 vec_safe_push (labels, link);
5150 /* Do not add ASMs with errors to the gimple IL stream. */
5151 if (ret != GS_ERROR)
5153 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5154 inputs, outputs, clobbers, labels);
5156 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5157 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5159 gimplify_seq_add_stmt (pre_p, stmt);
5162 return ret;
5165 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5166 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5167 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5168 return to this function.
5170 FIXME should we complexify the prequeue handling instead? Or use flags
5171 for all the cleanups and let the optimizer tighten them up? The current
5172 code seems pretty fragile; it will break on a cleanup within any
5173 non-conditional nesting. But any such nesting would be broken, anyway;
5174 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5175 and continues out of it. We can do that at the RTL level, though, so
5176 having an optimizer to tighten up try/finally regions would be a Good
5177 Thing. */
5179 static enum gimplify_status
5180 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5182 gimple_stmt_iterator iter;
5183 gimple_seq body_sequence = NULL;
5185 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5187 /* We only care about the number of conditions between the innermost
5188 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5189 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5190 int old_conds = gimplify_ctxp->conditions;
5191 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5192 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5193 gimplify_ctxp->conditions = 0;
5194 gimplify_ctxp->conditional_cleanups = NULL;
5195 gimplify_ctxp->in_cleanup_point_expr = true;
5197 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5199 gimplify_ctxp->conditions = old_conds;
5200 gimplify_ctxp->conditional_cleanups = old_cleanups;
5201 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5203 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5205 gimple wce = gsi_stmt (iter);
5207 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5209 if (gsi_one_before_end_p (iter))
5211 /* Note that gsi_insert_seq_before and gsi_remove do not
5212 scan operands, unlike some other sequence mutators. */
5213 if (!gimple_wce_cleanup_eh_only (wce))
5214 gsi_insert_seq_before_without_update (&iter,
5215 gimple_wce_cleanup (wce),
5216 GSI_SAME_STMT);
5217 gsi_remove (&iter, true);
5218 break;
5220 else
5222 gimple_statement_try *gtry;
5223 gimple_seq seq;
5224 enum gimple_try_flags kind;
5226 if (gimple_wce_cleanup_eh_only (wce))
5227 kind = GIMPLE_TRY_CATCH;
5228 else
5229 kind = GIMPLE_TRY_FINALLY;
5230 seq = gsi_split_seq_after (iter);
5232 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5233 /* Do not use gsi_replace here, as it may scan operands.
5234 We want to do a simple structural modification only. */
5235 gsi_set_stmt (&iter, gtry);
5236 iter = gsi_start (gtry->eval);
5239 else
5240 gsi_next (&iter);
5243 gimplify_seq_add_seq (pre_p, body_sequence);
5244 if (temp)
5246 *expr_p = temp;
5247 return GS_OK;
5249 else
5251 *expr_p = NULL;
5252 return GS_ALL_DONE;
5256 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5257 is the cleanup action required. EH_ONLY is true if the cleanup should
5258 only be executed if an exception is thrown, not on normal exit. */
5260 static void
5261 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5263 gimple wce;
5264 gimple_seq cleanup_stmts = NULL;
5266 /* Errors can result in improperly nested cleanups. Which results in
5267 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5268 if (seen_error ())
5269 return;
5271 if (gimple_conditional_context ())
5273 /* If we're in a conditional context, this is more complex. We only
5274 want to run the cleanup if we actually ran the initialization that
5275 necessitates it, but we want to run it after the end of the
5276 conditional context. So we wrap the try/finally around the
5277 condition and use a flag to determine whether or not to actually
5278 run the destructor. Thus
5280 test ? f(A()) : 0
5282 becomes (approximately)
5284 flag = 0;
5285 try {
5286 if (test) { A::A(temp); flag = 1; val = f(temp); }
5287 else { val = 0; }
5288 } finally {
5289 if (flag) A::~A(temp);
5293 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5294 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5295 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5297 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5298 gimplify_stmt (&cleanup, &cleanup_stmts);
5299 wce = gimple_build_wce (cleanup_stmts);
5301 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5302 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5303 gimplify_seq_add_stmt (pre_p, ftrue);
5305 /* Because of this manipulation, and the EH edges that jump
5306 threading cannot redirect, the temporary (VAR) will appear
5307 to be used uninitialized. Don't warn. */
5308 TREE_NO_WARNING (var) = 1;
5310 else
5312 gimplify_stmt (&cleanup, &cleanup_stmts);
5313 wce = gimple_build_wce (cleanup_stmts);
5314 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5315 gimplify_seq_add_stmt (pre_p, wce);
5319 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5321 static enum gimplify_status
5322 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5324 tree targ = *expr_p;
5325 tree temp = TARGET_EXPR_SLOT (targ);
5326 tree init = TARGET_EXPR_INITIAL (targ);
5327 enum gimplify_status ret;
5329 if (init)
5331 tree cleanup = NULL_TREE;
5333 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5334 to the temps list. Handle also variable length TARGET_EXPRs. */
5335 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5337 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5338 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5339 gimplify_vla_decl (temp, pre_p);
5341 else
5342 gimple_add_tmp_var (temp);
5344 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5345 expression is supposed to initialize the slot. */
5346 if (VOID_TYPE_P (TREE_TYPE (init)))
5347 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5348 else
5350 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5351 init = init_expr;
5352 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5353 init = NULL;
5354 ggc_free (init_expr);
5356 if (ret == GS_ERROR)
5358 /* PR c++/28266 Make sure this is expanded only once. */
5359 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5360 return GS_ERROR;
5362 if (init)
5363 gimplify_and_add (init, pre_p);
5365 /* If needed, push the cleanup for the temp. */
5366 if (TARGET_EXPR_CLEANUP (targ))
5368 if (CLEANUP_EH_ONLY (targ))
5369 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5370 CLEANUP_EH_ONLY (targ), pre_p);
5371 else
5372 cleanup = TARGET_EXPR_CLEANUP (targ);
5375 /* Add a clobber for the temporary going out of scope, like
5376 gimplify_bind_expr. */
5377 if (gimplify_ctxp->in_cleanup_point_expr
5378 && needs_to_live_in_memory (temp)
5379 && flag_stack_reuse == SR_ALL)
5381 tree clobber = build_constructor (TREE_TYPE (temp),
5382 NULL);
5383 TREE_THIS_VOLATILE (clobber) = true;
5384 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5385 if (cleanup)
5386 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5387 clobber);
5388 else
5389 cleanup = clobber;
5392 if (cleanup)
5393 gimple_push_cleanup (temp, cleanup, false, pre_p);
5395 /* Only expand this once. */
5396 TREE_OPERAND (targ, 3) = init;
5397 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5399 else
5400 /* We should have expanded this before. */
5401 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5403 *expr_p = temp;
5404 return GS_OK;
5407 /* Gimplification of expression trees. */
5409 /* Gimplify an expression which appears at statement context. The
5410 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5411 NULL, a new sequence is allocated.
5413 Return true if we actually added a statement to the queue. */
5415 bool
5416 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5418 gimple_seq_node last;
5420 last = gimple_seq_last (*seq_p);
5421 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5422 return last != gimple_seq_last (*seq_p);
5425 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5426 to CTX. If entries already exist, force them to be some flavor of private.
5427 If there is no enclosing parallel, do nothing. */
5429 void
5430 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5432 splay_tree_node n;
5434 if (decl == NULL || !DECL_P (decl))
5435 return;
5439 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5440 if (n != NULL)
5442 if (n->value & GOVD_SHARED)
5443 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5444 else if (n->value & GOVD_MAP)
5445 n->value |= GOVD_MAP_TO_ONLY;
5446 else
5447 return;
5449 else if (ctx->region_type == ORT_TARGET)
5450 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5451 else if (ctx->region_type != ORT_WORKSHARE
5452 && ctx->region_type != ORT_SIMD
5453 && ctx->region_type != ORT_TARGET_DATA)
5454 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5456 ctx = ctx->outer_context;
5458 while (ctx);
5461 /* Similarly for each of the type sizes of TYPE. */
5463 static void
5464 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5466 if (type == NULL || type == error_mark_node)
5467 return;
5468 type = TYPE_MAIN_VARIANT (type);
5470 if (ctx->privatized_types->add (type))
5471 return;
5473 switch (TREE_CODE (type))
5475 case INTEGER_TYPE:
5476 case ENUMERAL_TYPE:
5477 case BOOLEAN_TYPE:
5478 case REAL_TYPE:
5479 case FIXED_POINT_TYPE:
5480 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5481 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5482 break;
5484 case ARRAY_TYPE:
5485 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5486 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5487 break;
5489 case RECORD_TYPE:
5490 case UNION_TYPE:
5491 case QUAL_UNION_TYPE:
5493 tree field;
5494 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5495 if (TREE_CODE (field) == FIELD_DECL)
5497 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5498 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5501 break;
5503 case POINTER_TYPE:
5504 case REFERENCE_TYPE:
5505 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5506 break;
5508 default:
5509 break;
5512 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5513 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5514 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5517 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5519 static void
5520 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5522 splay_tree_node n;
5523 unsigned int nflags;
5524 tree t;
5526 if (error_operand_p (decl))
5527 return;
5529 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5530 there are constructors involved somewhere. */
5531 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5532 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5533 flags |= GOVD_SEEN;
5535 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5536 if (n != NULL && n->value != GOVD_ALIGNED)
5538 /* We shouldn't be re-adding the decl with the same data
5539 sharing class. */
5540 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5541 /* The only combination of data sharing classes we should see is
5542 FIRSTPRIVATE and LASTPRIVATE. */
5543 nflags = n->value | flags;
5544 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5545 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5546 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5547 n->value = nflags;
5548 return;
5551 /* When adding a variable-sized variable, we have to handle all sorts
5552 of additional bits of data: the pointer replacement variable, and
5553 the parameters of the type. */
5554 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5556 /* Add the pointer replacement variable as PRIVATE if the variable
5557 replacement is private, else FIRSTPRIVATE since we'll need the
5558 address of the original variable either for SHARED, or for the
5559 copy into or out of the context. */
5560 if (!(flags & GOVD_LOCAL))
5562 nflags = flags & GOVD_MAP
5563 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5564 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5565 nflags |= flags & GOVD_SEEN;
5566 t = DECL_VALUE_EXPR (decl);
5567 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5568 t = TREE_OPERAND (t, 0);
5569 gcc_assert (DECL_P (t));
5570 omp_add_variable (ctx, t, nflags);
5573 /* Add all of the variable and type parameters (which should have
5574 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5575 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5576 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5577 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5579 /* The variable-sized variable itself is never SHARED, only some form
5580 of PRIVATE. The sharing would take place via the pointer variable
5581 which we remapped above. */
5582 if (flags & GOVD_SHARED)
5583 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5584 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5586 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5587 alloca statement we generate for the variable, so make sure it
5588 is available. This isn't automatically needed for the SHARED
5589 case, since we won't be allocating local storage then.
5590 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5591 in this case omp_notice_variable will be called later
5592 on when it is gimplified. */
5593 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5594 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5595 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5597 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5598 && lang_hooks.decls.omp_privatize_by_reference (decl))
5600 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5602 /* Similar to the direct variable sized case above, we'll need the
5603 size of references being privatized. */
5604 if ((flags & GOVD_SHARED) == 0)
5606 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5607 if (TREE_CODE (t) != INTEGER_CST)
5608 omp_notice_variable (ctx, t, true);
5612 if (n != NULL)
5613 n->value |= flags;
5614 else
5615 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5618 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5619 This just prints out diagnostics about threadprivate variable uses
5620 in untied tasks. If DECL2 is non-NULL, prevent this warning
5621 on that variable. */
5623 static bool
5624 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5625 tree decl2)
5627 splay_tree_node n;
5628 struct gimplify_omp_ctx *octx;
5630 for (octx = ctx; octx; octx = octx->outer_context)
5631 if (octx->region_type == ORT_TARGET)
5633 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5634 if (n == NULL)
5636 error ("threadprivate variable %qE used in target region",
5637 DECL_NAME (decl));
5638 error_at (octx->location, "enclosing target region");
5639 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5641 if (decl2)
5642 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5645 if (ctx->region_type != ORT_UNTIED_TASK)
5646 return false;
5647 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5648 if (n == NULL)
5650 error ("threadprivate variable %qE used in untied task",
5651 DECL_NAME (decl));
5652 error_at (ctx->location, "enclosing task");
5653 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5655 if (decl2)
5656 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5657 return false;
5660 /* Record the fact that DECL was used within the OpenMP context CTX.
5661 IN_CODE is true when real code uses DECL, and false when we should
5662 merely emit default(none) errors. Return true if DECL is going to
5663 be remapped and thus DECL shouldn't be gimplified into its
5664 DECL_VALUE_EXPR (if any). */
5666 static bool
5667 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5669 splay_tree_node n;
5670 unsigned flags = in_code ? GOVD_SEEN : 0;
5671 bool ret = false, shared;
5673 if (error_operand_p (decl))
5674 return false;
5676 /* Threadprivate variables are predetermined. */
5677 if (is_global_var (decl))
5679 if (DECL_THREAD_LOCAL_P (decl))
5680 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5682 if (DECL_HAS_VALUE_EXPR_P (decl))
5684 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5686 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5687 return omp_notice_threadprivate_variable (ctx, decl, value);
5691 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5692 if (ctx->region_type == ORT_TARGET)
5694 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5695 if (n == NULL)
5697 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5699 error ("%qD referenced in target region does not have "
5700 "a mappable type", decl);
5701 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5703 else
5704 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5706 else
5708 /* If nothing changed, there's nothing left to do. */
5709 if ((n->value & flags) == flags)
5710 return ret;
5711 n->value |= flags;
5713 goto do_outer;
5716 if (n == NULL)
5718 enum omp_clause_default_kind default_kind, kind;
5719 struct gimplify_omp_ctx *octx;
5721 if (ctx->region_type == ORT_WORKSHARE
5722 || ctx->region_type == ORT_SIMD
5723 || ctx->region_type == ORT_TARGET_DATA)
5724 goto do_outer;
5726 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5727 remapped firstprivate instead of shared. To some extent this is
5728 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5729 default_kind = ctx->default_kind;
5730 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5731 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5732 default_kind = kind;
5734 switch (default_kind)
5736 case OMP_CLAUSE_DEFAULT_NONE:
5737 if ((ctx->region_type & ORT_PARALLEL) != 0)
5739 error ("%qE not specified in enclosing parallel",
5740 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5741 error_at (ctx->location, "enclosing parallel");
5743 else if ((ctx->region_type & ORT_TASK) != 0)
5745 error ("%qE not specified in enclosing task",
5746 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5747 error_at (ctx->location, "enclosing task");
5749 else if (ctx->region_type == ORT_TEAMS)
5751 error ("%qE not specified in enclosing teams construct",
5752 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5753 error_at (ctx->location, "enclosing teams construct");
5755 else
5756 gcc_unreachable ();
5757 /* FALLTHRU */
5758 case OMP_CLAUSE_DEFAULT_SHARED:
5759 flags |= GOVD_SHARED;
5760 break;
5761 case OMP_CLAUSE_DEFAULT_PRIVATE:
5762 flags |= GOVD_PRIVATE;
5763 break;
5764 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5765 flags |= GOVD_FIRSTPRIVATE;
5766 break;
5767 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5768 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5769 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5770 if (ctx->outer_context)
5771 omp_notice_variable (ctx->outer_context, decl, in_code);
5772 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5774 splay_tree_node n2;
5776 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5777 continue;
5778 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5779 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5781 flags |= GOVD_FIRSTPRIVATE;
5782 break;
5784 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5785 break;
5787 if (flags & GOVD_FIRSTPRIVATE)
5788 break;
5789 if (octx == NULL
5790 && (TREE_CODE (decl) == PARM_DECL
5791 || (!is_global_var (decl)
5792 && DECL_CONTEXT (decl) == current_function_decl)))
5794 flags |= GOVD_FIRSTPRIVATE;
5795 break;
5797 flags |= GOVD_SHARED;
5798 break;
5799 default:
5800 gcc_unreachable ();
5803 if ((flags & GOVD_PRIVATE)
5804 && lang_hooks.decls.omp_private_outer_ref (decl))
5805 flags |= GOVD_PRIVATE_OUTER_REF;
5807 omp_add_variable (ctx, decl, flags);
5809 shared = (flags & GOVD_SHARED) != 0;
5810 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5811 goto do_outer;
5814 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5815 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5816 && DECL_SIZE (decl)
5817 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5819 splay_tree_node n2;
5820 tree t = DECL_VALUE_EXPR (decl);
5821 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5822 t = TREE_OPERAND (t, 0);
5823 gcc_assert (DECL_P (t));
5824 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5825 n2->value |= GOVD_SEEN;
5828 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5829 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5831 /* If nothing changed, there's nothing left to do. */
5832 if ((n->value & flags) == flags)
5833 return ret;
5834 flags |= n->value;
5835 n->value = flags;
5837 do_outer:
5838 /* If the variable is private in the current context, then we don't
5839 need to propagate anything to an outer context. */
5840 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5841 return ret;
5842 if (ctx->outer_context
5843 && omp_notice_variable (ctx->outer_context, decl, in_code))
5844 return true;
5845 return ret;
5848 /* Verify that DECL is private within CTX. If there's specific information
5849 to the contrary in the innermost scope, generate an error. */
5851 static bool
5852 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5854 splay_tree_node n;
5856 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5857 if (n != NULL)
5859 if (n->value & GOVD_SHARED)
5861 if (ctx == gimplify_omp_ctxp)
5863 if (simd)
5864 error ("iteration variable %qE is predetermined linear",
5865 DECL_NAME (decl));
5866 else
5867 error ("iteration variable %qE should be private",
5868 DECL_NAME (decl));
5869 n->value = GOVD_PRIVATE;
5870 return true;
5872 else
5873 return false;
5875 else if ((n->value & GOVD_EXPLICIT) != 0
5876 && (ctx == gimplify_omp_ctxp
5877 || (ctx->region_type == ORT_COMBINED_PARALLEL
5878 && gimplify_omp_ctxp->outer_context == ctx)))
5880 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5881 error ("iteration variable %qE should not be firstprivate",
5882 DECL_NAME (decl));
5883 else if ((n->value & GOVD_REDUCTION) != 0)
5884 error ("iteration variable %qE should not be reduction",
5885 DECL_NAME (decl));
5886 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5887 error ("iteration variable %qE should not be lastprivate",
5888 DECL_NAME (decl));
5889 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5890 error ("iteration variable %qE should not be private",
5891 DECL_NAME (decl));
5892 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5893 error ("iteration variable %qE is predetermined linear",
5894 DECL_NAME (decl));
5896 return (ctx == gimplify_omp_ctxp
5897 || (ctx->region_type == ORT_COMBINED_PARALLEL
5898 && gimplify_omp_ctxp->outer_context == ctx));
5901 if (ctx->region_type != ORT_WORKSHARE
5902 && ctx->region_type != ORT_SIMD)
5903 return false;
5904 else if (ctx->outer_context)
5905 return omp_is_private (ctx->outer_context, decl, simd);
5906 return false;
5909 /* Return true if DECL is private within a parallel region
5910 that binds to the current construct's context or in parallel
5911 region's REDUCTION clause. */
5913 static bool
5914 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5916 splay_tree_node n;
5920 ctx = ctx->outer_context;
5921 if (ctx == NULL)
5922 return !(is_global_var (decl)
5923 /* References might be private, but might be shared too,
5924 when checking for copyprivate, assume they might be
5925 private, otherwise assume they might be shared. */
5926 || (!copyprivate
5927 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5929 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5930 continue;
5932 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5933 if (n != NULL)
5934 return (n->value & GOVD_SHARED) == 0;
5936 while (ctx->region_type == ORT_WORKSHARE
5937 || ctx->region_type == ORT_SIMD);
5938 return false;
5941 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5942 and previous omp contexts. */
5944 static void
5945 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5946 enum omp_region_type region_type)
5948 struct gimplify_omp_ctx *ctx, *outer_ctx;
5949 tree c;
5951 ctx = new_omp_context (region_type);
5952 outer_ctx = ctx->outer_context;
5954 while ((c = *list_p) != NULL)
5956 bool remove = false;
5957 bool notice_outer = true;
5958 const char *check_non_private = NULL;
5959 unsigned int flags;
5960 tree decl;
5962 switch (OMP_CLAUSE_CODE (c))
5964 case OMP_CLAUSE_PRIVATE:
5965 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5966 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5968 flags |= GOVD_PRIVATE_OUTER_REF;
5969 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5971 else
5972 notice_outer = false;
5973 goto do_add;
5974 case OMP_CLAUSE_SHARED:
5975 flags = GOVD_SHARED | GOVD_EXPLICIT;
5976 goto do_add;
5977 case OMP_CLAUSE_FIRSTPRIVATE:
5978 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5979 check_non_private = "firstprivate";
5980 goto do_add;
5981 case OMP_CLAUSE_LASTPRIVATE:
5982 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5983 check_non_private = "lastprivate";
5984 goto do_add;
5985 case OMP_CLAUSE_REDUCTION:
5986 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5987 check_non_private = "reduction";
5988 goto do_add;
5989 case OMP_CLAUSE_LINEAR:
5990 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5991 is_gimple_val, fb_rvalue) == GS_ERROR)
5993 remove = true;
5994 break;
5996 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5997 goto do_add;
5999 case OMP_CLAUSE_MAP:
6000 decl = OMP_CLAUSE_DECL (c);
6001 if (error_operand_p (decl))
6003 remove = true;
6004 break;
6006 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6007 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6008 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6009 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6010 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6012 remove = true;
6013 break;
6015 if (!DECL_P (decl))
6017 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6018 NULL, is_gimple_lvalue, fb_lvalue)
6019 == GS_ERROR)
6021 remove = true;
6022 break;
6024 break;
6026 flags = GOVD_MAP | GOVD_EXPLICIT;
6027 goto do_add;
6029 case OMP_CLAUSE_DEPEND:
6030 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6032 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6033 NULL, is_gimple_val, fb_rvalue);
6034 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6036 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6038 remove = true;
6039 break;
6041 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6042 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6043 is_gimple_val, fb_rvalue) == GS_ERROR)
6045 remove = true;
6046 break;
6048 break;
6050 case OMP_CLAUSE_TO:
6051 case OMP_CLAUSE_FROM:
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 goto do_notice;
6080 do_add:
6081 decl = OMP_CLAUSE_DECL (c);
6082 if (error_operand_p (decl))
6084 remove = true;
6085 break;
6087 omp_add_variable (ctx, decl, flags);
6088 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6089 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6091 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6092 GOVD_LOCAL | GOVD_SEEN);
6093 gimplify_omp_ctxp = ctx;
6094 push_gimplify_context ();
6096 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6097 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6099 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6100 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6101 pop_gimplify_context
6102 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6103 push_gimplify_context ();
6104 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6105 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6106 pop_gimplify_context
6107 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6108 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6109 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6111 gimplify_omp_ctxp = outer_ctx;
6113 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6114 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6116 gimplify_omp_ctxp = ctx;
6117 push_gimplify_context ();
6118 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6120 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6121 NULL, NULL);
6122 TREE_SIDE_EFFECTS (bind) = 1;
6123 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6124 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6126 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6127 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6128 pop_gimplify_context
6129 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6130 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6132 gimplify_omp_ctxp = outer_ctx;
6134 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6135 && OMP_CLAUSE_LINEAR_STMT (c))
6137 gimplify_omp_ctxp = ctx;
6138 push_gimplify_context ();
6139 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6141 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6142 NULL, NULL);
6143 TREE_SIDE_EFFECTS (bind) = 1;
6144 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6145 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6147 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6148 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6149 pop_gimplify_context
6150 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6151 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6153 gimplify_omp_ctxp = outer_ctx;
6155 if (notice_outer)
6156 goto do_notice;
6157 break;
6159 case OMP_CLAUSE_COPYIN:
6160 case OMP_CLAUSE_COPYPRIVATE:
6161 decl = OMP_CLAUSE_DECL (c);
6162 if (error_operand_p (decl))
6164 remove = true;
6165 break;
6167 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6168 && !remove
6169 && !omp_check_private (ctx, decl, true))
6171 remove = true;
6172 if (is_global_var (decl))
6174 if (DECL_THREAD_LOCAL_P (decl))
6175 remove = false;
6176 else if (DECL_HAS_VALUE_EXPR_P (decl))
6178 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6180 if (value
6181 && DECL_P (value)
6182 && DECL_THREAD_LOCAL_P (value))
6183 remove = false;
6186 if (remove)
6187 error_at (OMP_CLAUSE_LOCATION (c),
6188 "copyprivate variable %qE is not threadprivate"
6189 " or private in outer context", DECL_NAME (decl));
6191 do_notice:
6192 if (outer_ctx)
6193 omp_notice_variable (outer_ctx, decl, true);
6194 if (check_non_private
6195 && region_type == ORT_WORKSHARE
6196 && omp_check_private (ctx, decl, false))
6198 error ("%s variable %qE is private in outer context",
6199 check_non_private, DECL_NAME (decl));
6200 remove = true;
6202 break;
6204 case OMP_CLAUSE_FINAL:
6205 case OMP_CLAUSE_IF:
6206 OMP_CLAUSE_OPERAND (c, 0)
6207 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6208 /* Fall through. */
6210 case OMP_CLAUSE_SCHEDULE:
6211 case OMP_CLAUSE_NUM_THREADS:
6212 case OMP_CLAUSE_NUM_TEAMS:
6213 case OMP_CLAUSE_THREAD_LIMIT:
6214 case OMP_CLAUSE_DIST_SCHEDULE:
6215 case OMP_CLAUSE_DEVICE:
6216 case OMP_CLAUSE__CILK_FOR_COUNT_:
6217 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6218 is_gimple_val, fb_rvalue) == GS_ERROR)
6219 remove = true;
6220 break;
6222 case OMP_CLAUSE_NOWAIT:
6223 case OMP_CLAUSE_ORDERED:
6224 case OMP_CLAUSE_UNTIED:
6225 case OMP_CLAUSE_COLLAPSE:
6226 case OMP_CLAUSE_MERGEABLE:
6227 case OMP_CLAUSE_PROC_BIND:
6228 case OMP_CLAUSE_SAFELEN:
6229 break;
6231 case OMP_CLAUSE_ALIGNED:
6232 decl = OMP_CLAUSE_DECL (c);
6233 if (error_operand_p (decl))
6235 remove = true;
6236 break;
6238 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6239 is_gimple_val, fb_rvalue) == GS_ERROR)
6241 remove = true;
6242 break;
6244 if (!is_global_var (decl)
6245 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6246 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6247 break;
6249 case OMP_CLAUSE_DEFAULT:
6250 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6251 break;
6253 default:
6254 gcc_unreachable ();
6257 if (remove)
6258 *list_p = OMP_CLAUSE_CHAIN (c);
6259 else
6260 list_p = &OMP_CLAUSE_CHAIN (c);
6263 gimplify_omp_ctxp = ctx;
6266 struct gimplify_adjust_omp_clauses_data
6268 tree *list_p;
6269 gimple_seq *pre_p;
6272 /* For all variables that were not actually used within the context,
6273 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6275 static int
6276 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6278 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6279 gimple_seq *pre_p
6280 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6281 tree decl = (tree) n->key;
6282 unsigned flags = n->value;
6283 enum omp_clause_code code;
6284 tree clause;
6285 bool private_debug;
6287 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6288 return 0;
6289 if ((flags & GOVD_SEEN) == 0)
6290 return 0;
6291 if (flags & GOVD_DEBUG_PRIVATE)
6293 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6294 private_debug = true;
6296 else if (flags & GOVD_MAP)
6297 private_debug = false;
6298 else
6299 private_debug
6300 = lang_hooks.decls.omp_private_debug_clause (decl,
6301 !!(flags & GOVD_SHARED));
6302 if (private_debug)
6303 code = OMP_CLAUSE_PRIVATE;
6304 else if (flags & GOVD_MAP)
6305 code = OMP_CLAUSE_MAP;
6306 else if (flags & GOVD_SHARED)
6308 if (is_global_var (decl))
6310 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6311 while (ctx != NULL)
6313 splay_tree_node on
6314 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6315 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6316 | GOVD_PRIVATE | GOVD_REDUCTION
6317 | GOVD_LINEAR | GOVD_MAP)) != 0)
6318 break;
6319 ctx = ctx->outer_context;
6321 if (ctx == NULL)
6322 return 0;
6324 code = OMP_CLAUSE_SHARED;
6326 else if (flags & GOVD_PRIVATE)
6327 code = OMP_CLAUSE_PRIVATE;
6328 else if (flags & GOVD_FIRSTPRIVATE)
6329 code = OMP_CLAUSE_FIRSTPRIVATE;
6330 else if (flags & GOVD_LASTPRIVATE)
6331 code = OMP_CLAUSE_LASTPRIVATE;
6332 else if (flags & GOVD_ALIGNED)
6333 return 0;
6334 else
6335 gcc_unreachable ();
6337 clause = build_omp_clause (input_location, code);
6338 OMP_CLAUSE_DECL (clause) = decl;
6339 OMP_CLAUSE_CHAIN (clause) = *list_p;
6340 if (private_debug)
6341 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6342 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6343 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6344 else if (code == OMP_CLAUSE_MAP)
6346 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6347 ? OMP_CLAUSE_MAP_TO
6348 : OMP_CLAUSE_MAP_TOFROM;
6349 if (DECL_SIZE (decl)
6350 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6352 tree decl2 = DECL_VALUE_EXPR (decl);
6353 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6354 decl2 = TREE_OPERAND (decl2, 0);
6355 gcc_assert (DECL_P (decl2));
6356 tree mem = build_simple_mem_ref (decl2);
6357 OMP_CLAUSE_DECL (clause) = mem;
6358 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6359 if (gimplify_omp_ctxp->outer_context)
6361 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6362 omp_notice_variable (ctx, decl2, true);
6363 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6365 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6366 OMP_CLAUSE_MAP);
6367 OMP_CLAUSE_DECL (nc) = decl;
6368 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6369 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6370 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6371 OMP_CLAUSE_CHAIN (clause) = nc;
6373 else
6374 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6376 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6378 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6379 OMP_CLAUSE_DECL (nc) = decl;
6380 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6381 OMP_CLAUSE_CHAIN (nc) = *list_p;
6382 OMP_CLAUSE_CHAIN (clause) = nc;
6383 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6384 gimplify_omp_ctxp = ctx->outer_context;
6385 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6386 gimplify_omp_ctxp = ctx;
6388 *list_p = clause;
6389 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6390 gimplify_omp_ctxp = ctx->outer_context;
6391 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6392 gimplify_omp_ctxp = ctx;
6393 return 0;
6396 static void
6397 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6399 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6400 tree c, decl;
6402 while ((c = *list_p) != NULL)
6404 splay_tree_node n;
6405 bool remove = false;
6407 switch (OMP_CLAUSE_CODE (c))
6409 case OMP_CLAUSE_PRIVATE:
6410 case OMP_CLAUSE_SHARED:
6411 case OMP_CLAUSE_FIRSTPRIVATE:
6412 case OMP_CLAUSE_LINEAR:
6413 decl = OMP_CLAUSE_DECL (c);
6414 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6415 remove = !(n->value & GOVD_SEEN);
6416 if (! remove)
6418 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6419 if ((n->value & GOVD_DEBUG_PRIVATE)
6420 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6422 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6423 || ((n->value & GOVD_DATA_SHARE_CLASS)
6424 == GOVD_PRIVATE));
6425 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6426 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6429 && ctx->outer_context
6430 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6431 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6433 if (ctx->outer_context->combined_loop
6434 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6436 n = splay_tree_lookup (ctx->outer_context->variables,
6437 (splay_tree_key) decl);
6438 if (n == NULL
6439 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6441 int flags = GOVD_FIRSTPRIVATE;
6442 /* #pragma omp distribute does not allow
6443 lastprivate clause. */
6444 if (!ctx->outer_context->distribute)
6445 flags |= GOVD_LASTPRIVATE;
6446 if (n == NULL)
6447 omp_add_variable (ctx->outer_context, decl,
6448 flags | GOVD_SEEN);
6449 else
6450 n->value |= flags | GOVD_SEEN;
6453 else if (!is_global_var (decl))
6454 omp_notice_variable (ctx->outer_context, decl, true);
6457 break;
6459 case OMP_CLAUSE_LASTPRIVATE:
6460 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6461 accurately reflect the presence of a FIRSTPRIVATE clause. */
6462 decl = OMP_CLAUSE_DECL (c);
6463 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6464 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6465 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6466 break;
6468 case OMP_CLAUSE_ALIGNED:
6469 decl = OMP_CLAUSE_DECL (c);
6470 if (!is_global_var (decl))
6472 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6473 remove = n == NULL || !(n->value & GOVD_SEEN);
6474 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6476 struct gimplify_omp_ctx *octx;
6477 if (n != NULL
6478 && (n->value & (GOVD_DATA_SHARE_CLASS
6479 & ~GOVD_FIRSTPRIVATE)))
6480 remove = true;
6481 else
6482 for (octx = ctx->outer_context; octx;
6483 octx = octx->outer_context)
6485 n = splay_tree_lookup (octx->variables,
6486 (splay_tree_key) decl);
6487 if (n == NULL)
6488 continue;
6489 if (n->value & GOVD_LOCAL)
6490 break;
6491 /* We have to avoid assigning a shared variable
6492 to itself when trying to add
6493 __builtin_assume_aligned. */
6494 if (n->value & GOVD_SHARED)
6496 remove = true;
6497 break;
6502 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6504 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6505 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6506 remove = true;
6508 break;
6510 case OMP_CLAUSE_MAP:
6511 decl = OMP_CLAUSE_DECL (c);
6512 if (!DECL_P (decl))
6513 break;
6514 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6515 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6516 remove = true;
6517 else if (DECL_SIZE (decl)
6518 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6519 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6521 tree decl2 = DECL_VALUE_EXPR (decl);
6522 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6523 decl2 = TREE_OPERAND (decl2, 0);
6524 gcc_assert (DECL_P (decl2));
6525 tree mem = build_simple_mem_ref (decl2);
6526 OMP_CLAUSE_DECL (c) = mem;
6527 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6528 if (ctx->outer_context)
6530 omp_notice_variable (ctx->outer_context, decl2, true);
6531 omp_notice_variable (ctx->outer_context,
6532 OMP_CLAUSE_SIZE (c), true);
6534 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6535 OMP_CLAUSE_MAP);
6536 OMP_CLAUSE_DECL (nc) = decl;
6537 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6538 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6539 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6540 OMP_CLAUSE_CHAIN (c) = nc;
6541 c = nc;
6543 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6544 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6545 break;
6547 case OMP_CLAUSE_TO:
6548 case OMP_CLAUSE_FROM:
6549 decl = OMP_CLAUSE_DECL (c);
6550 if (!DECL_P (decl))
6551 break;
6552 if (DECL_SIZE (decl)
6553 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6555 tree decl2 = DECL_VALUE_EXPR (decl);
6556 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6557 decl2 = TREE_OPERAND (decl2, 0);
6558 gcc_assert (DECL_P (decl2));
6559 tree mem = build_simple_mem_ref (decl2);
6560 OMP_CLAUSE_DECL (c) = mem;
6561 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6562 if (ctx->outer_context)
6564 omp_notice_variable (ctx->outer_context, decl2, true);
6565 omp_notice_variable (ctx->outer_context,
6566 OMP_CLAUSE_SIZE (c), true);
6569 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6570 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6571 break;
6573 case OMP_CLAUSE_REDUCTION:
6574 case OMP_CLAUSE_COPYIN:
6575 case OMP_CLAUSE_COPYPRIVATE:
6576 case OMP_CLAUSE_IF:
6577 case OMP_CLAUSE_NUM_THREADS:
6578 case OMP_CLAUSE_NUM_TEAMS:
6579 case OMP_CLAUSE_THREAD_LIMIT:
6580 case OMP_CLAUSE_DIST_SCHEDULE:
6581 case OMP_CLAUSE_DEVICE:
6582 case OMP_CLAUSE_SCHEDULE:
6583 case OMP_CLAUSE_NOWAIT:
6584 case OMP_CLAUSE_ORDERED:
6585 case OMP_CLAUSE_DEFAULT:
6586 case OMP_CLAUSE_UNTIED:
6587 case OMP_CLAUSE_COLLAPSE:
6588 case OMP_CLAUSE_FINAL:
6589 case OMP_CLAUSE_MERGEABLE:
6590 case OMP_CLAUSE_PROC_BIND:
6591 case OMP_CLAUSE_SAFELEN:
6592 case OMP_CLAUSE_DEPEND:
6593 case OMP_CLAUSE__CILK_FOR_COUNT_:
6594 break;
6596 default:
6597 gcc_unreachable ();
6600 if (remove)
6601 *list_p = OMP_CLAUSE_CHAIN (c);
6602 else
6603 list_p = &OMP_CLAUSE_CHAIN (c);
6606 /* Add in any implicit data sharing. */
6607 struct gimplify_adjust_omp_clauses_data data;
6608 data.list_p = list_p;
6609 data.pre_p = pre_p;
6610 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6612 gimplify_omp_ctxp = ctx->outer_context;
6613 delete_omp_context (ctx);
6616 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6617 gimplification of the body, as well as scanning the body for used
6618 variables. We need to do this scan now, because variable-sized
6619 decls will be decomposed during gimplification. */
6621 static void
6622 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6624 tree expr = *expr_p;
6625 gimple g;
6626 gimple_seq body = NULL;
6628 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6629 OMP_PARALLEL_COMBINED (expr)
6630 ? ORT_COMBINED_PARALLEL
6631 : ORT_PARALLEL);
6633 push_gimplify_context ();
6635 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6636 if (gimple_code (g) == GIMPLE_BIND)
6637 pop_gimplify_context (g);
6638 else
6639 pop_gimplify_context (NULL);
6641 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6643 g = gimple_build_omp_parallel (body,
6644 OMP_PARALLEL_CLAUSES (expr),
6645 NULL_TREE, NULL_TREE);
6646 if (OMP_PARALLEL_COMBINED (expr))
6647 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6648 gimplify_seq_add_stmt (pre_p, g);
6649 *expr_p = NULL_TREE;
6652 /* Gimplify the contents of an OMP_TASK statement. This involves
6653 gimplification of the body, as well as scanning the body for used
6654 variables. We need to do this scan now, because variable-sized
6655 decls will be decomposed during gimplification. */
6657 static void
6658 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6660 tree expr = *expr_p;
6661 gimple g;
6662 gimple_seq body = NULL;
6664 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6665 find_omp_clause (OMP_TASK_CLAUSES (expr),
6666 OMP_CLAUSE_UNTIED)
6667 ? ORT_UNTIED_TASK : ORT_TASK);
6669 push_gimplify_context ();
6671 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6672 if (gimple_code (g) == GIMPLE_BIND)
6673 pop_gimplify_context (g);
6674 else
6675 pop_gimplify_context (NULL);
6677 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6679 g = gimple_build_omp_task (body,
6680 OMP_TASK_CLAUSES (expr),
6681 NULL_TREE, NULL_TREE,
6682 NULL_TREE, NULL_TREE, NULL_TREE);
6683 gimplify_seq_add_stmt (pre_p, g);
6684 *expr_p = NULL_TREE;
6687 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6688 with non-NULL OMP_FOR_INIT. */
6690 static tree
6691 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6693 *walk_subtrees = 0;
6694 switch (TREE_CODE (*tp))
6696 case OMP_FOR:
6697 *walk_subtrees = 1;
6698 /* FALLTHRU */
6699 case OMP_SIMD:
6700 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6701 return *tp;
6702 break;
6703 case BIND_EXPR:
6704 case STATEMENT_LIST:
6705 case OMP_PARALLEL:
6706 *walk_subtrees = 1;
6707 break;
6708 default:
6709 break;
6711 return NULL_TREE;
6714 /* Gimplify the gross structure of an OMP_FOR statement. */
6716 static enum gimplify_status
6717 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6719 tree for_stmt, orig_for_stmt, decl, var, t;
6720 enum gimplify_status ret = GS_ALL_DONE;
6721 enum gimplify_status tret;
6722 gimple gfor;
6723 gimple_seq for_body, for_pre_body;
6724 int i;
6725 bool simd;
6726 bitmap has_decl_expr = NULL;
6728 orig_for_stmt = for_stmt = *expr_p;
6730 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6731 || TREE_CODE (for_stmt) == CILK_SIMD);
6732 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6733 simd ? ORT_SIMD : ORT_WORKSHARE);
6734 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6735 gimplify_omp_ctxp->distribute = true;
6737 /* Handle OMP_FOR_INIT. */
6738 for_pre_body = NULL;
6739 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6741 has_decl_expr = BITMAP_ALLOC (NULL);
6742 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6743 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6744 == VAR_DECL)
6746 t = OMP_FOR_PRE_BODY (for_stmt);
6747 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6749 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6751 tree_stmt_iterator si;
6752 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6753 tsi_next (&si))
6755 t = tsi_stmt (si);
6756 if (TREE_CODE (t) == DECL_EXPR
6757 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6758 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6762 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6763 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6765 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6767 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6768 NULL, NULL);
6769 gcc_assert (for_stmt != NULL_TREE);
6770 gimplify_omp_ctxp->combined_loop = true;
6773 for_body = NULL;
6774 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6775 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6776 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6777 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6778 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6780 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6781 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6782 decl = TREE_OPERAND (t, 0);
6783 gcc_assert (DECL_P (decl));
6784 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6785 || POINTER_TYPE_P (TREE_TYPE (decl)));
6787 /* Make sure the iteration variable is private. */
6788 tree c = NULL_TREE;
6789 tree c2 = NULL_TREE;
6790 if (orig_for_stmt != for_stmt)
6791 /* Do this only on innermost construct for combined ones. */;
6792 else if (simd)
6794 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6795 (splay_tree_key)decl);
6796 omp_is_private (gimplify_omp_ctxp, decl,
6797 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6798 != 1));
6799 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6800 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6801 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6803 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6804 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6805 if (has_decl_expr
6806 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6807 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6808 OMP_CLAUSE_DECL (c) = decl;
6809 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6810 OMP_FOR_CLAUSES (for_stmt) = c;
6811 omp_add_variable (gimplify_omp_ctxp, decl,
6812 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6814 else
6816 bool lastprivate
6817 = (!has_decl_expr
6818 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6819 if (lastprivate
6820 && gimplify_omp_ctxp->outer_context
6821 && gimplify_omp_ctxp->outer_context->region_type
6822 == ORT_WORKSHARE
6823 && gimplify_omp_ctxp->outer_context->combined_loop
6824 && !gimplify_omp_ctxp->outer_context->distribute)
6826 struct gimplify_omp_ctx *outer
6827 = gimplify_omp_ctxp->outer_context;
6828 n = splay_tree_lookup (outer->variables,
6829 (splay_tree_key) decl);
6830 if (n != NULL
6831 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6832 lastprivate = false;
6833 else if (omp_check_private (outer, decl, false))
6834 error ("lastprivate variable %qE is private in outer "
6835 "context", DECL_NAME (decl));
6836 else
6838 omp_add_variable (outer, decl,
6839 GOVD_LASTPRIVATE | GOVD_SEEN);
6840 if (outer->outer_context)
6841 omp_notice_variable (outer->outer_context, decl, true);
6844 c = build_omp_clause (input_location,
6845 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6846 : OMP_CLAUSE_PRIVATE);
6847 OMP_CLAUSE_DECL (c) = decl;
6848 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6849 OMP_FOR_CLAUSES (for_stmt) = c;
6850 omp_add_variable (gimplify_omp_ctxp, decl,
6851 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6852 | GOVD_EXPLICIT | GOVD_SEEN);
6853 c = NULL_TREE;
6856 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6857 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6858 else
6859 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6861 /* If DECL is not a gimple register, create a temporary variable to act
6862 as an iteration counter. This is valid, since DECL cannot be
6863 modified in the body of the loop. Similarly for any iteration vars
6864 in simd with collapse > 1 where the iterator vars must be
6865 lastprivate. */
6866 if (orig_for_stmt != for_stmt)
6867 var = decl;
6868 else if (!is_gimple_reg (decl)
6869 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
6871 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6872 TREE_OPERAND (t, 0) = var;
6874 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6876 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6878 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6879 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6880 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6881 OMP_CLAUSE_DECL (c2) = var;
6882 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6883 OMP_FOR_CLAUSES (for_stmt) = c2;
6884 omp_add_variable (gimplify_omp_ctxp, var,
6885 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6886 if (c == NULL_TREE)
6888 c = c2;
6889 c2 = NULL_TREE;
6892 else
6893 omp_add_variable (gimplify_omp_ctxp, var,
6894 GOVD_PRIVATE | GOVD_SEEN);
6896 else
6897 var = decl;
6899 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6900 is_gimple_val, fb_rvalue);
6901 ret = MIN (ret, tret);
6902 if (ret == GS_ERROR)
6903 return ret;
6905 /* Handle OMP_FOR_COND. */
6906 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6907 gcc_assert (COMPARISON_CLASS_P (t));
6908 gcc_assert (TREE_OPERAND (t, 0) == decl);
6910 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6911 is_gimple_val, fb_rvalue);
6912 ret = MIN (ret, tret);
6914 /* Handle OMP_FOR_INCR. */
6915 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6916 switch (TREE_CODE (t))
6918 case PREINCREMENT_EXPR:
6919 case POSTINCREMENT_EXPR:
6921 tree decl = TREE_OPERAND (t, 0);
6922 /* c_omp_for_incr_canonicalize_ptr() should have been
6923 called to massage things appropriately. */
6924 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6926 if (orig_for_stmt != for_stmt)
6927 break;
6928 t = build_int_cst (TREE_TYPE (decl), 1);
6929 if (c)
6930 OMP_CLAUSE_LINEAR_STEP (c) = t;
6931 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6932 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6933 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6934 break;
6937 case PREDECREMENT_EXPR:
6938 case POSTDECREMENT_EXPR:
6939 /* c_omp_for_incr_canonicalize_ptr() should have been
6940 called to massage things appropriately. */
6941 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6942 if (orig_for_stmt != for_stmt)
6943 break;
6944 t = build_int_cst (TREE_TYPE (decl), -1);
6945 if (c)
6946 OMP_CLAUSE_LINEAR_STEP (c) = t;
6947 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6948 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6949 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6950 break;
6952 case MODIFY_EXPR:
6953 gcc_assert (TREE_OPERAND (t, 0) == decl);
6954 TREE_OPERAND (t, 0) = var;
6956 t = TREE_OPERAND (t, 1);
6957 switch (TREE_CODE (t))
6959 case PLUS_EXPR:
6960 if (TREE_OPERAND (t, 1) == decl)
6962 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6963 TREE_OPERAND (t, 0) = var;
6964 break;
6967 /* Fallthru. */
6968 case MINUS_EXPR:
6969 case POINTER_PLUS_EXPR:
6970 gcc_assert (TREE_OPERAND (t, 0) == decl);
6971 TREE_OPERAND (t, 0) = var;
6972 break;
6973 default:
6974 gcc_unreachable ();
6977 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6978 is_gimple_val, fb_rvalue);
6979 ret = MIN (ret, tret);
6980 if (c)
6982 tree step = TREE_OPERAND (t, 1);
6983 tree stept = TREE_TYPE (decl);
6984 if (POINTER_TYPE_P (stept))
6985 stept = sizetype;
6986 step = fold_convert (stept, step);
6987 if (TREE_CODE (t) == MINUS_EXPR)
6988 step = fold_build1 (NEGATE_EXPR, stept, step);
6989 OMP_CLAUSE_LINEAR_STEP (c) = step;
6990 if (step != TREE_OPERAND (t, 1))
6992 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6993 &for_pre_body, NULL,
6994 is_gimple_val, fb_rvalue);
6995 ret = MIN (ret, tret);
6998 break;
7000 default:
7001 gcc_unreachable ();
7004 if (c2)
7006 gcc_assert (c);
7007 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7010 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7011 && orig_for_stmt == for_stmt)
7013 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7014 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7015 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7016 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7017 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7018 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7019 && OMP_CLAUSE_DECL (c) == decl)
7021 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7022 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7023 gcc_assert (TREE_OPERAND (t, 0) == var);
7024 t = TREE_OPERAND (t, 1);
7025 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7026 || TREE_CODE (t) == MINUS_EXPR
7027 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7028 gcc_assert (TREE_OPERAND (t, 0) == var);
7029 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7030 TREE_OPERAND (t, 1));
7031 gimple_seq *seq;
7032 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7033 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7034 else
7035 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7036 gimplify_assign (decl, t, seq);
7041 BITMAP_FREE (has_decl_expr);
7043 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7045 if (orig_for_stmt != for_stmt)
7046 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7048 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7049 decl = TREE_OPERAND (t, 0);
7050 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7051 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7052 TREE_OPERAND (t, 0) = var;
7053 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7054 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7055 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7058 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7060 int kind;
7061 switch (TREE_CODE (orig_for_stmt))
7063 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7064 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7065 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7066 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7067 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7068 default:
7069 gcc_unreachable ();
7071 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7072 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7073 for_pre_body);
7074 if (orig_for_stmt != for_stmt)
7075 gimple_omp_for_set_combined_p (gfor, true);
7076 if (gimplify_omp_ctxp
7077 && (gimplify_omp_ctxp->combined_loop
7078 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7079 && gimplify_omp_ctxp->outer_context
7080 && gimplify_omp_ctxp->outer_context->combined_loop)))
7082 gimple_omp_for_set_combined_into_p (gfor, true);
7083 if (gimplify_omp_ctxp->combined_loop)
7084 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7085 else
7086 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7089 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7091 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7092 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7093 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7094 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7095 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7096 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7097 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7098 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7101 gimplify_seq_add_stmt (pre_p, gfor);
7102 if (ret != GS_ALL_DONE)
7103 return GS_ERROR;
7104 *expr_p = NULL_TREE;
7105 return GS_ALL_DONE;
7108 /* Gimplify the gross structure of other OpenMP constructs.
7109 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7110 and OMP_TEAMS. */
7112 static void
7113 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7115 tree expr = *expr_p;
7116 gimple stmt;
7117 gimple_seq body = NULL;
7118 enum omp_region_type ort = ORT_WORKSHARE;
7120 switch (TREE_CODE (expr))
7122 case OMP_SECTIONS:
7123 case OMP_SINGLE:
7124 break;
7125 case OMP_TARGET:
7126 ort = ORT_TARGET;
7127 break;
7128 case OMP_TARGET_DATA:
7129 ort = ORT_TARGET_DATA;
7130 break;
7131 case OMP_TEAMS:
7132 ort = ORT_TEAMS;
7133 break;
7134 default:
7135 gcc_unreachable ();
7137 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7138 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7140 push_gimplify_context ();
7141 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7142 if (gimple_code (g) == GIMPLE_BIND)
7143 pop_gimplify_context (g);
7144 else
7145 pop_gimplify_context (NULL);
7146 if (ort == ORT_TARGET_DATA)
7148 gimple_seq cleanup = NULL;
7149 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7150 g = gimple_build_call (fn, 0);
7151 gimple_seq_add_stmt (&cleanup, g);
7152 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7153 body = NULL;
7154 gimple_seq_add_stmt (&body, g);
7157 else
7158 gimplify_and_add (OMP_BODY (expr), &body);
7159 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7161 switch (TREE_CODE (expr))
7163 case OMP_SECTIONS:
7164 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7165 break;
7166 case OMP_SINGLE:
7167 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7168 break;
7169 case OMP_TARGET:
7170 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7171 OMP_CLAUSES (expr));
7172 break;
7173 case OMP_TARGET_DATA:
7174 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7175 OMP_CLAUSES (expr));
7176 break;
7177 case OMP_TEAMS:
7178 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7179 break;
7180 default:
7181 gcc_unreachable ();
7184 gimplify_seq_add_stmt (pre_p, stmt);
7185 *expr_p = NULL_TREE;
7188 /* Gimplify the gross structure of OpenMP target update construct. */
7190 static void
7191 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7193 tree expr = *expr_p;
7194 gimple stmt;
7196 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7197 ORT_WORKSHARE);
7198 gimplify_adjust_omp_clauses (pre_p, &OMP_TARGET_UPDATE_CLAUSES (expr));
7199 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7200 OMP_TARGET_UPDATE_CLAUSES (expr));
7202 gimplify_seq_add_stmt (pre_p, stmt);
7203 *expr_p = NULL_TREE;
7206 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7207 stabilized the lhs of the atomic operation as *ADDR. Return true if
7208 EXPR is this stabilized form. */
7210 static bool
7211 goa_lhs_expr_p (tree expr, tree addr)
7213 /* Also include casts to other type variants. The C front end is fond
7214 of adding these for e.g. volatile variables. This is like
7215 STRIP_TYPE_NOPS but includes the main variant lookup. */
7216 STRIP_USELESS_TYPE_CONVERSION (expr);
7218 if (TREE_CODE (expr) == INDIRECT_REF)
7220 expr = TREE_OPERAND (expr, 0);
7221 while (expr != addr
7222 && (CONVERT_EXPR_P (expr)
7223 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7224 && TREE_CODE (expr) == TREE_CODE (addr)
7225 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7227 expr = TREE_OPERAND (expr, 0);
7228 addr = TREE_OPERAND (addr, 0);
7230 if (expr == addr)
7231 return true;
7232 return (TREE_CODE (addr) == ADDR_EXPR
7233 && TREE_CODE (expr) == ADDR_EXPR
7234 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7236 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7237 return true;
7238 return false;
7241 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7242 expression does not involve the lhs, evaluate it into a temporary.
7243 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7244 or -1 if an error was encountered. */
7246 static int
7247 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7248 tree lhs_var)
7250 tree expr = *expr_p;
7251 int saw_lhs;
7253 if (goa_lhs_expr_p (expr, lhs_addr))
7255 *expr_p = lhs_var;
7256 return 1;
7258 if (is_gimple_val (expr))
7259 return 0;
7261 saw_lhs = 0;
7262 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7264 case tcc_binary:
7265 case tcc_comparison:
7266 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7267 lhs_var);
7268 case tcc_unary:
7269 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7270 lhs_var);
7271 break;
7272 case tcc_expression:
7273 switch (TREE_CODE (expr))
7275 case TRUTH_ANDIF_EXPR:
7276 case TRUTH_ORIF_EXPR:
7277 case TRUTH_AND_EXPR:
7278 case TRUTH_OR_EXPR:
7279 case TRUTH_XOR_EXPR:
7280 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7281 lhs_addr, lhs_var);
7282 case TRUTH_NOT_EXPR:
7283 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7284 lhs_addr, lhs_var);
7285 break;
7286 case COMPOUND_EXPR:
7287 /* Break out any preevaluations from cp_build_modify_expr. */
7288 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7289 expr = TREE_OPERAND (expr, 1))
7290 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7291 *expr_p = expr;
7292 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7293 default:
7294 break;
7296 break;
7297 default:
7298 break;
7301 if (saw_lhs == 0)
7303 enum gimplify_status gs;
7304 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7305 if (gs != GS_ALL_DONE)
7306 saw_lhs = -1;
7309 return saw_lhs;
7312 /* Gimplify an OMP_ATOMIC statement. */
7314 static enum gimplify_status
7315 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7317 tree addr = TREE_OPERAND (*expr_p, 0);
7318 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7319 ? NULL : TREE_OPERAND (*expr_p, 1);
7320 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7321 tree tmp_load;
7322 gimple loadstmt, storestmt;
7324 tmp_load = create_tmp_reg (type, NULL);
7325 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7326 return GS_ERROR;
7328 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7329 != GS_ALL_DONE)
7330 return GS_ERROR;
7332 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7333 gimplify_seq_add_stmt (pre_p, loadstmt);
7334 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7335 != GS_ALL_DONE)
7336 return GS_ERROR;
7338 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7339 rhs = tmp_load;
7340 storestmt = gimple_build_omp_atomic_store (rhs);
7341 gimplify_seq_add_stmt (pre_p, storestmt);
7342 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7344 gimple_omp_atomic_set_seq_cst (loadstmt);
7345 gimple_omp_atomic_set_seq_cst (storestmt);
7347 switch (TREE_CODE (*expr_p))
7349 case OMP_ATOMIC_READ:
7350 case OMP_ATOMIC_CAPTURE_OLD:
7351 *expr_p = tmp_load;
7352 gimple_omp_atomic_set_need_value (loadstmt);
7353 break;
7354 case OMP_ATOMIC_CAPTURE_NEW:
7355 *expr_p = rhs;
7356 gimple_omp_atomic_set_need_value (storestmt);
7357 break;
7358 default:
7359 *expr_p = NULL;
7360 break;
7363 return GS_ALL_DONE;
7366 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7367 body, and adding some EH bits. */
7369 static enum gimplify_status
7370 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7372 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7373 gimple g;
7374 gimple_seq body = NULL;
7375 int subcode = 0;
7377 /* Wrap the transaction body in a BIND_EXPR so we have a context
7378 where to put decls for OpenMP. */
7379 if (TREE_CODE (tbody) != BIND_EXPR)
7381 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7382 TREE_SIDE_EFFECTS (bind) = 1;
7383 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7384 TRANSACTION_EXPR_BODY (expr) = bind;
7387 push_gimplify_context ();
7388 temp = voidify_wrapper_expr (*expr_p, NULL);
7390 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7391 pop_gimplify_context (g);
7393 g = gimple_build_transaction (body, NULL);
7394 if (TRANSACTION_EXPR_OUTER (expr))
7395 subcode = GTMA_IS_OUTER;
7396 else if (TRANSACTION_EXPR_RELAXED (expr))
7397 subcode = GTMA_IS_RELAXED;
7398 gimple_transaction_set_subcode (g, subcode);
7400 gimplify_seq_add_stmt (pre_p, g);
7402 if (temp)
7404 *expr_p = temp;
7405 return GS_OK;
7408 *expr_p = NULL_TREE;
7409 return GS_ALL_DONE;
7412 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7413 expression produces a value to be used as an operand inside a GIMPLE
7414 statement, the value will be stored back in *EXPR_P. This value will
7415 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7416 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7417 emitted in PRE_P and POST_P.
7419 Additionally, this process may overwrite parts of the input
7420 expression during gimplification. Ideally, it should be
7421 possible to do non-destructive gimplification.
7423 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7424 the expression needs to evaluate to a value to be used as
7425 an operand in a GIMPLE statement, this value will be stored in
7426 *EXPR_P on exit. This happens when the caller specifies one
7427 of fb_lvalue or fb_rvalue fallback flags.
7429 PRE_P will contain the sequence of GIMPLE statements corresponding
7430 to the evaluation of EXPR and all the side-effects that must
7431 be executed before the main expression. On exit, the last
7432 statement of PRE_P is the core statement being gimplified. For
7433 instance, when gimplifying 'if (++a)' the last statement in
7434 PRE_P will be 'if (t.1)' where t.1 is the result of
7435 pre-incrementing 'a'.
7437 POST_P will contain the sequence of GIMPLE statements corresponding
7438 to the evaluation of all the side-effects that must be executed
7439 after the main expression. If this is NULL, the post
7440 side-effects are stored at the end of PRE_P.
7442 The reason why the output is split in two is to handle post
7443 side-effects explicitly. In some cases, an expression may have
7444 inner and outer post side-effects which need to be emitted in
7445 an order different from the one given by the recursive
7446 traversal. For instance, for the expression (*p--)++ the post
7447 side-effects of '--' must actually occur *after* the post
7448 side-effects of '++'. However, gimplification will first visit
7449 the inner expression, so if a separate POST sequence was not
7450 used, the resulting sequence would be:
7452 1 t.1 = *p
7453 2 p = p - 1
7454 3 t.2 = t.1 + 1
7455 4 *p = t.2
7457 However, the post-decrement operation in line #2 must not be
7458 evaluated until after the store to *p at line #4, so the
7459 correct sequence should be:
7461 1 t.1 = *p
7462 2 t.2 = t.1 + 1
7463 3 *p = t.2
7464 4 p = p - 1
7466 So, by specifying a separate post queue, it is possible
7467 to emit the post side-effects in the correct order.
7468 If POST_P is NULL, an internal queue will be used. Before
7469 returning to the caller, the sequence POST_P is appended to
7470 the main output sequence PRE_P.
7472 GIMPLE_TEST_F points to a function that takes a tree T and
7473 returns nonzero if T is in the GIMPLE form requested by the
7474 caller. The GIMPLE predicates are in gimple.c.
7476 FALLBACK tells the function what sort of a temporary we want if
7477 gimplification cannot produce an expression that complies with
7478 GIMPLE_TEST_F.
7480 fb_none means that no temporary should be generated
7481 fb_rvalue means that an rvalue is OK to generate
7482 fb_lvalue means that an lvalue is OK to generate
7483 fb_either means that either is OK, but an lvalue is preferable.
7484 fb_mayfail means that gimplification may fail (in which case
7485 GS_ERROR will be returned)
7487 The return value is either GS_ERROR or GS_ALL_DONE, since this
7488 function iterates until EXPR is completely gimplified or an error
7489 occurs. */
7491 enum gimplify_status
7492 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7493 bool (*gimple_test_f) (tree), fallback_t fallback)
7495 tree tmp;
7496 gimple_seq internal_pre = NULL;
7497 gimple_seq internal_post = NULL;
7498 tree save_expr;
7499 bool is_statement;
7500 location_t saved_location;
7501 enum gimplify_status ret;
7502 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7504 save_expr = *expr_p;
7505 if (save_expr == NULL_TREE)
7506 return GS_ALL_DONE;
7508 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7509 is_statement = gimple_test_f == is_gimple_stmt;
7510 if (is_statement)
7511 gcc_assert (pre_p);
7513 /* Consistency checks. */
7514 if (gimple_test_f == is_gimple_reg)
7515 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7516 else if (gimple_test_f == is_gimple_val
7517 || gimple_test_f == is_gimple_call_addr
7518 || gimple_test_f == is_gimple_condexpr
7519 || gimple_test_f == is_gimple_mem_rhs
7520 || gimple_test_f == is_gimple_mem_rhs_or_call
7521 || gimple_test_f == is_gimple_reg_rhs
7522 || gimple_test_f == is_gimple_reg_rhs_or_call
7523 || gimple_test_f == is_gimple_asm_val
7524 || gimple_test_f == is_gimple_mem_ref_addr)
7525 gcc_assert (fallback & fb_rvalue);
7526 else if (gimple_test_f == is_gimple_min_lval
7527 || gimple_test_f == is_gimple_lvalue)
7528 gcc_assert (fallback & fb_lvalue);
7529 else if (gimple_test_f == is_gimple_addressable)
7530 gcc_assert (fallback & fb_either);
7531 else if (gimple_test_f == is_gimple_stmt)
7532 gcc_assert (fallback == fb_none);
7533 else
7535 /* We should have recognized the GIMPLE_TEST_F predicate to
7536 know what kind of fallback to use in case a temporary is
7537 needed to hold the value or address of *EXPR_P. */
7538 gcc_unreachable ();
7541 /* We used to check the predicate here and return immediately if it
7542 succeeds. This is wrong; the design is for gimplification to be
7543 idempotent, and for the predicates to only test for valid forms, not
7544 whether they are fully simplified. */
7545 if (pre_p == NULL)
7546 pre_p = &internal_pre;
7548 if (post_p == NULL)
7549 post_p = &internal_post;
7551 /* Remember the last statements added to PRE_P and POST_P. Every
7552 new statement added by the gimplification helpers needs to be
7553 annotated with location information. To centralize the
7554 responsibility, we remember the last statement that had been
7555 added to both queues before gimplifying *EXPR_P. If
7556 gimplification produces new statements in PRE_P and POST_P, those
7557 statements will be annotated with the same location information
7558 as *EXPR_P. */
7559 pre_last_gsi = gsi_last (*pre_p);
7560 post_last_gsi = gsi_last (*post_p);
7562 saved_location = input_location;
7563 if (save_expr != error_mark_node
7564 && EXPR_HAS_LOCATION (*expr_p))
7565 input_location = EXPR_LOCATION (*expr_p);
7567 /* Loop over the specific gimplifiers until the toplevel node
7568 remains the same. */
7571 /* Strip away as many useless type conversions as possible
7572 at the toplevel. */
7573 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7575 /* Remember the expr. */
7576 save_expr = *expr_p;
7578 /* Die, die, die, my darling. */
7579 if (save_expr == error_mark_node
7580 || (TREE_TYPE (save_expr)
7581 && TREE_TYPE (save_expr) == error_mark_node))
7583 ret = GS_ERROR;
7584 break;
7587 /* Do any language-specific gimplification. */
7588 ret = ((enum gimplify_status)
7589 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7590 if (ret == GS_OK)
7592 if (*expr_p == NULL_TREE)
7593 break;
7594 if (*expr_p != save_expr)
7595 continue;
7597 else if (ret != GS_UNHANDLED)
7598 break;
7600 /* Make sure that all the cases set 'ret' appropriately. */
7601 ret = GS_UNHANDLED;
7602 switch (TREE_CODE (*expr_p))
7604 /* First deal with the special cases. */
7606 case POSTINCREMENT_EXPR:
7607 case POSTDECREMENT_EXPR:
7608 case PREINCREMENT_EXPR:
7609 case PREDECREMENT_EXPR:
7610 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7611 fallback != fb_none,
7612 TREE_TYPE (*expr_p));
7613 break;
7615 case VIEW_CONVERT_EXPR:
7616 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7617 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7619 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7620 post_p, is_gimple_val, fb_rvalue);
7621 recalculate_side_effects (*expr_p);
7622 break;
7624 /* Fallthru. */
7626 case ARRAY_REF:
7627 case ARRAY_RANGE_REF:
7628 case REALPART_EXPR:
7629 case IMAGPART_EXPR:
7630 case COMPONENT_REF:
7631 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7632 fallback ? fallback : fb_rvalue);
7633 break;
7635 case COND_EXPR:
7636 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7638 /* C99 code may assign to an array in a structure value of a
7639 conditional expression, and this has undefined behavior
7640 only on execution, so create a temporary if an lvalue is
7641 required. */
7642 if (fallback == fb_lvalue)
7644 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7645 mark_addressable (*expr_p);
7646 ret = GS_OK;
7648 break;
7650 case CALL_EXPR:
7651 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7653 /* C99 code may assign to an array in a structure returned
7654 from a function, and this has undefined behavior only on
7655 execution, so create a temporary if an lvalue is
7656 required. */
7657 if (fallback == fb_lvalue)
7659 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7660 mark_addressable (*expr_p);
7661 ret = GS_OK;
7663 break;
7665 case TREE_LIST:
7666 gcc_unreachable ();
7668 case COMPOUND_EXPR:
7669 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7670 break;
7672 case COMPOUND_LITERAL_EXPR:
7673 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7674 gimple_test_f, fallback);
7675 break;
7677 case MODIFY_EXPR:
7678 case INIT_EXPR:
7679 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7680 fallback != fb_none);
7681 break;
7683 case TRUTH_ANDIF_EXPR:
7684 case TRUTH_ORIF_EXPR:
7686 /* Preserve the original type of the expression and the
7687 source location of the outer expression. */
7688 tree org_type = TREE_TYPE (*expr_p);
7689 *expr_p = gimple_boolify (*expr_p);
7690 *expr_p = build3_loc (input_location, COND_EXPR,
7691 org_type, *expr_p,
7692 fold_convert_loc
7693 (input_location,
7694 org_type, boolean_true_node),
7695 fold_convert_loc
7696 (input_location,
7697 org_type, boolean_false_node));
7698 ret = GS_OK;
7699 break;
7702 case TRUTH_NOT_EXPR:
7704 tree type = TREE_TYPE (*expr_p);
7705 /* The parsers are careful to generate TRUTH_NOT_EXPR
7706 only with operands that are always zero or one.
7707 We do not fold here but handle the only interesting case
7708 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7709 *expr_p = gimple_boolify (*expr_p);
7710 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7711 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7712 TREE_TYPE (*expr_p),
7713 TREE_OPERAND (*expr_p, 0));
7714 else
7715 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7716 TREE_TYPE (*expr_p),
7717 TREE_OPERAND (*expr_p, 0),
7718 build_int_cst (TREE_TYPE (*expr_p), 1));
7719 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7720 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7721 ret = GS_OK;
7722 break;
7725 case ADDR_EXPR:
7726 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7727 break;
7729 case ANNOTATE_EXPR:
7731 tree cond = TREE_OPERAND (*expr_p, 0);
7732 tree kind = TREE_OPERAND (*expr_p, 1);
7733 tree type = TREE_TYPE (cond);
7734 if (!INTEGRAL_TYPE_P (type))
7736 *expr_p = cond;
7737 ret = GS_OK;
7738 break;
7740 tree tmp = create_tmp_var (type, NULL);
7741 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7742 gimple call
7743 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7744 gimple_call_set_lhs (call, tmp);
7745 gimplify_seq_add_stmt (pre_p, call);
7746 *expr_p = tmp;
7747 ret = GS_ALL_DONE;
7748 break;
7751 case VA_ARG_EXPR:
7752 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7753 break;
7755 CASE_CONVERT:
7756 if (IS_EMPTY_STMT (*expr_p))
7758 ret = GS_ALL_DONE;
7759 break;
7762 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7763 || fallback == fb_none)
7765 /* Just strip a conversion to void (or in void context) and
7766 try again. */
7767 *expr_p = TREE_OPERAND (*expr_p, 0);
7768 ret = GS_OK;
7769 break;
7772 ret = gimplify_conversion (expr_p);
7773 if (ret == GS_ERROR)
7774 break;
7775 if (*expr_p != save_expr)
7776 break;
7777 /* FALLTHRU */
7779 case FIX_TRUNC_EXPR:
7780 /* unary_expr: ... | '(' cast ')' val | ... */
7781 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7782 is_gimple_val, fb_rvalue);
7783 recalculate_side_effects (*expr_p);
7784 break;
7786 case INDIRECT_REF:
7788 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7789 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7790 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7792 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7793 if (*expr_p != save_expr)
7795 ret = GS_OK;
7796 break;
7799 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7800 is_gimple_reg, fb_rvalue);
7801 if (ret == GS_ERROR)
7802 break;
7804 recalculate_side_effects (*expr_p);
7805 *expr_p = fold_build2_loc (input_location, MEM_REF,
7806 TREE_TYPE (*expr_p),
7807 TREE_OPERAND (*expr_p, 0),
7808 build_int_cst (saved_ptr_type, 0));
7809 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7810 TREE_THIS_NOTRAP (*expr_p) = notrap;
7811 ret = GS_OK;
7812 break;
7815 /* We arrive here through the various re-gimplifcation paths. */
7816 case MEM_REF:
7817 /* First try re-folding the whole thing. */
7818 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7819 TREE_OPERAND (*expr_p, 0),
7820 TREE_OPERAND (*expr_p, 1));
7821 if (tmp)
7823 *expr_p = tmp;
7824 recalculate_side_effects (*expr_p);
7825 ret = GS_OK;
7826 break;
7828 /* Avoid re-gimplifying the address operand if it is already
7829 in suitable form. Re-gimplifying would mark the address
7830 operand addressable. Always gimplify when not in SSA form
7831 as we still may have to gimplify decls with value-exprs. */
7832 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7833 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7835 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7836 is_gimple_mem_ref_addr, fb_rvalue);
7837 if (ret == GS_ERROR)
7838 break;
7840 recalculate_side_effects (*expr_p);
7841 ret = GS_ALL_DONE;
7842 break;
7844 /* Constants need not be gimplified. */
7845 case INTEGER_CST:
7846 case REAL_CST:
7847 case FIXED_CST:
7848 case STRING_CST:
7849 case COMPLEX_CST:
7850 case VECTOR_CST:
7851 /* Drop the overflow flag on constants, we do not want
7852 that in the GIMPLE IL. */
7853 if (TREE_OVERFLOW_P (*expr_p))
7854 *expr_p = drop_tree_overflow (*expr_p);
7855 ret = GS_ALL_DONE;
7856 break;
7858 case CONST_DECL:
7859 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7860 CONST_DECL node. Otherwise the decl is replaceable by its
7861 value. */
7862 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7863 if (fallback & fb_lvalue)
7864 ret = GS_ALL_DONE;
7865 else
7867 *expr_p = DECL_INITIAL (*expr_p);
7868 ret = GS_OK;
7870 break;
7872 case DECL_EXPR:
7873 ret = gimplify_decl_expr (expr_p, pre_p);
7874 break;
7876 case BIND_EXPR:
7877 ret = gimplify_bind_expr (expr_p, pre_p);
7878 break;
7880 case LOOP_EXPR:
7881 ret = gimplify_loop_expr (expr_p, pre_p);
7882 break;
7884 case SWITCH_EXPR:
7885 ret = gimplify_switch_expr (expr_p, pre_p);
7886 break;
7888 case EXIT_EXPR:
7889 ret = gimplify_exit_expr (expr_p);
7890 break;
7892 case GOTO_EXPR:
7893 /* If the target is not LABEL, then it is a computed jump
7894 and the target needs to be gimplified. */
7895 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7897 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7898 NULL, is_gimple_val, fb_rvalue);
7899 if (ret == GS_ERROR)
7900 break;
7902 gimplify_seq_add_stmt (pre_p,
7903 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7904 ret = GS_ALL_DONE;
7905 break;
7907 case PREDICT_EXPR:
7908 gimplify_seq_add_stmt (pre_p,
7909 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7910 PREDICT_EXPR_OUTCOME (*expr_p)));
7911 ret = GS_ALL_DONE;
7912 break;
7914 case LABEL_EXPR:
7915 ret = GS_ALL_DONE;
7916 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7917 == current_function_decl);
7918 gimplify_seq_add_stmt (pre_p,
7919 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7920 break;
7922 case CASE_LABEL_EXPR:
7923 ret = gimplify_case_label_expr (expr_p, pre_p);
7924 break;
7926 case RETURN_EXPR:
7927 ret = gimplify_return_expr (*expr_p, pre_p);
7928 break;
7930 case CONSTRUCTOR:
7931 /* Don't reduce this in place; let gimplify_init_constructor work its
7932 magic. Buf if we're just elaborating this for side effects, just
7933 gimplify any element that has side-effects. */
7934 if (fallback == fb_none)
7936 unsigned HOST_WIDE_INT ix;
7937 tree val;
7938 tree temp = NULL_TREE;
7939 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7940 if (TREE_SIDE_EFFECTS (val))
7941 append_to_statement_list (val, &temp);
7943 *expr_p = temp;
7944 ret = temp ? GS_OK : GS_ALL_DONE;
7946 /* C99 code may assign to an array in a constructed
7947 structure or union, and this has undefined behavior only
7948 on execution, so create a temporary if an lvalue is
7949 required. */
7950 else if (fallback == fb_lvalue)
7952 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7953 mark_addressable (*expr_p);
7954 ret = GS_OK;
7956 else
7957 ret = GS_ALL_DONE;
7958 break;
7960 /* The following are special cases that are not handled by the
7961 original GIMPLE grammar. */
7963 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7964 eliminated. */
7965 case SAVE_EXPR:
7966 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7967 break;
7969 case BIT_FIELD_REF:
7970 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7971 post_p, is_gimple_lvalue, fb_either);
7972 recalculate_side_effects (*expr_p);
7973 break;
7975 case TARGET_MEM_REF:
7977 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7979 if (TMR_BASE (*expr_p))
7980 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7981 post_p, is_gimple_mem_ref_addr, fb_either);
7982 if (TMR_INDEX (*expr_p))
7983 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7984 post_p, is_gimple_val, fb_rvalue);
7985 if (TMR_INDEX2 (*expr_p))
7986 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7987 post_p, is_gimple_val, fb_rvalue);
7988 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7989 ret = MIN (r0, r1);
7991 break;
7993 case NON_LVALUE_EXPR:
7994 /* This should have been stripped above. */
7995 gcc_unreachable ();
7997 case ASM_EXPR:
7998 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7999 break;
8001 case TRY_FINALLY_EXPR:
8002 case TRY_CATCH_EXPR:
8004 gimple_seq eval, cleanup;
8005 gimple try_;
8007 /* Calls to destructors are generated automatically in FINALLY/CATCH
8008 block. They should have location as UNKNOWN_LOCATION. However,
8009 gimplify_call_expr will reset these call stmts to input_location
8010 if it finds stmt's location is unknown. To prevent resetting for
8011 destructors, we set the input_location to unknown.
8012 Note that this only affects the destructor calls in FINALLY/CATCH
8013 block, and will automatically reset to its original value by the
8014 end of gimplify_expr. */
8015 input_location = UNKNOWN_LOCATION;
8016 eval = cleanup = NULL;
8017 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8018 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8019 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8020 if (gimple_seq_empty_p (cleanup))
8022 gimple_seq_add_seq (pre_p, eval);
8023 ret = GS_ALL_DONE;
8024 break;
8026 try_ = gimple_build_try (eval, cleanup,
8027 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8028 ? GIMPLE_TRY_FINALLY
8029 : GIMPLE_TRY_CATCH);
8030 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8031 gimple_set_location (try_, saved_location);
8032 else
8033 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8034 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8035 gimple_try_set_catch_is_cleanup (try_,
8036 TRY_CATCH_IS_CLEANUP (*expr_p));
8037 gimplify_seq_add_stmt (pre_p, try_);
8038 ret = GS_ALL_DONE;
8039 break;
8042 case CLEANUP_POINT_EXPR:
8043 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8044 break;
8046 case TARGET_EXPR:
8047 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8048 break;
8050 case CATCH_EXPR:
8052 gimple c;
8053 gimple_seq handler = NULL;
8054 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8055 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8056 gimplify_seq_add_stmt (pre_p, c);
8057 ret = GS_ALL_DONE;
8058 break;
8061 case EH_FILTER_EXPR:
8063 gimple ehf;
8064 gimple_seq failure = NULL;
8066 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8067 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8068 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8069 gimplify_seq_add_stmt (pre_p, ehf);
8070 ret = GS_ALL_DONE;
8071 break;
8074 case OBJ_TYPE_REF:
8076 enum gimplify_status r0, r1;
8077 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8078 post_p, is_gimple_val, fb_rvalue);
8079 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8080 post_p, is_gimple_val, fb_rvalue);
8081 TREE_SIDE_EFFECTS (*expr_p) = 0;
8082 ret = MIN (r0, r1);
8084 break;
8086 case LABEL_DECL:
8087 /* We get here when taking the address of a label. We mark
8088 the label as "forced"; meaning it can never be removed and
8089 it is a potential target for any computed goto. */
8090 FORCED_LABEL (*expr_p) = 1;
8091 ret = GS_ALL_DONE;
8092 break;
8094 case STATEMENT_LIST:
8095 ret = gimplify_statement_list (expr_p, pre_p);
8096 break;
8098 case WITH_SIZE_EXPR:
8100 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8101 post_p == &internal_post ? NULL : post_p,
8102 gimple_test_f, fallback);
8103 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8104 is_gimple_val, fb_rvalue);
8105 ret = GS_ALL_DONE;
8107 break;
8109 case VAR_DECL:
8110 case PARM_DECL:
8111 ret = gimplify_var_or_parm_decl (expr_p);
8112 break;
8114 case RESULT_DECL:
8115 /* When within an OpenMP context, notice uses of variables. */
8116 if (gimplify_omp_ctxp)
8117 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8118 ret = GS_ALL_DONE;
8119 break;
8121 case SSA_NAME:
8122 /* Allow callbacks into the gimplifier during optimization. */
8123 ret = GS_ALL_DONE;
8124 break;
8126 case OMP_PARALLEL:
8127 gimplify_omp_parallel (expr_p, pre_p);
8128 ret = GS_ALL_DONE;
8129 break;
8131 case OMP_TASK:
8132 gimplify_omp_task (expr_p, pre_p);
8133 ret = GS_ALL_DONE;
8134 break;
8136 case OMP_FOR:
8137 case OMP_SIMD:
8138 case CILK_SIMD:
8139 case CILK_FOR:
8140 case OMP_DISTRIBUTE:
8141 ret = gimplify_omp_for (expr_p, pre_p);
8142 break;
8144 case OMP_SECTIONS:
8145 case OMP_SINGLE:
8146 case OMP_TARGET:
8147 case OMP_TARGET_DATA:
8148 case OMP_TEAMS:
8149 gimplify_omp_workshare (expr_p, pre_p);
8150 ret = GS_ALL_DONE;
8151 break;
8153 case OMP_TARGET_UPDATE:
8154 gimplify_omp_target_update (expr_p, pre_p);
8155 ret = GS_ALL_DONE;
8156 break;
8158 case OMP_SECTION:
8159 case OMP_MASTER:
8160 case OMP_TASKGROUP:
8161 case OMP_ORDERED:
8162 case OMP_CRITICAL:
8164 gimple_seq body = NULL;
8165 gimple g;
8167 gimplify_and_add (OMP_BODY (*expr_p), &body);
8168 switch (TREE_CODE (*expr_p))
8170 case OMP_SECTION:
8171 g = gimple_build_omp_section (body);
8172 break;
8173 case OMP_MASTER:
8174 g = gimple_build_omp_master (body);
8175 break;
8176 case OMP_TASKGROUP:
8178 gimple_seq cleanup = NULL;
8179 tree fn
8180 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8181 g = gimple_build_call (fn, 0);
8182 gimple_seq_add_stmt (&cleanup, g);
8183 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8184 body = NULL;
8185 gimple_seq_add_stmt (&body, g);
8186 g = gimple_build_omp_taskgroup (body);
8188 break;
8189 case OMP_ORDERED:
8190 g = gimple_build_omp_ordered (body);
8191 break;
8192 case OMP_CRITICAL:
8193 g = gimple_build_omp_critical (body,
8194 OMP_CRITICAL_NAME (*expr_p));
8195 break;
8196 default:
8197 gcc_unreachable ();
8199 gimplify_seq_add_stmt (pre_p, g);
8200 ret = GS_ALL_DONE;
8201 break;
8204 case OMP_ATOMIC:
8205 case OMP_ATOMIC_READ:
8206 case OMP_ATOMIC_CAPTURE_OLD:
8207 case OMP_ATOMIC_CAPTURE_NEW:
8208 ret = gimplify_omp_atomic (expr_p, pre_p);
8209 break;
8211 case TRANSACTION_EXPR:
8212 ret = gimplify_transaction (expr_p, pre_p);
8213 break;
8215 case TRUTH_AND_EXPR:
8216 case TRUTH_OR_EXPR:
8217 case TRUTH_XOR_EXPR:
8219 tree orig_type = TREE_TYPE (*expr_p);
8220 tree new_type, xop0, xop1;
8221 *expr_p = gimple_boolify (*expr_p);
8222 new_type = TREE_TYPE (*expr_p);
8223 if (!useless_type_conversion_p (orig_type, new_type))
8225 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8226 ret = GS_OK;
8227 break;
8230 /* Boolified binary truth expressions are semantically equivalent
8231 to bitwise binary expressions. Canonicalize them to the
8232 bitwise variant. */
8233 switch (TREE_CODE (*expr_p))
8235 case TRUTH_AND_EXPR:
8236 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8237 break;
8238 case TRUTH_OR_EXPR:
8239 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8240 break;
8241 case TRUTH_XOR_EXPR:
8242 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8243 break;
8244 default:
8245 break;
8247 /* Now make sure that operands have compatible type to
8248 expression's new_type. */
8249 xop0 = TREE_OPERAND (*expr_p, 0);
8250 xop1 = TREE_OPERAND (*expr_p, 1);
8251 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8252 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8253 new_type,
8254 xop0);
8255 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8256 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8257 new_type,
8258 xop1);
8259 /* Continue classified as tcc_binary. */
8260 goto expr_2;
8263 case FMA_EXPR:
8264 case VEC_COND_EXPR:
8265 case VEC_PERM_EXPR:
8266 /* Classified as tcc_expression. */
8267 goto expr_3;
8269 case POINTER_PLUS_EXPR:
8271 enum gimplify_status r0, r1;
8272 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8273 post_p, is_gimple_val, fb_rvalue);
8274 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8275 post_p, is_gimple_val, fb_rvalue);
8276 recalculate_side_effects (*expr_p);
8277 ret = MIN (r0, r1);
8278 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8279 after gimplifying operands - this is similar to how
8280 it would be folding all gimplified stmts on creation
8281 to have them canonicalized, which is what we eventually
8282 should do anyway. */
8283 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8284 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8286 *expr_p = build_fold_addr_expr_with_type_loc
8287 (input_location,
8288 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8289 TREE_OPERAND (*expr_p, 0),
8290 fold_convert (ptr_type_node,
8291 TREE_OPERAND (*expr_p, 1))),
8292 TREE_TYPE (*expr_p));
8293 ret = MIN (ret, GS_OK);
8295 break;
8298 case CILK_SYNC_STMT:
8300 if (!fn_contains_cilk_spawn_p (cfun))
8302 error_at (EXPR_LOCATION (*expr_p),
8303 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8304 ret = GS_ERROR;
8306 else
8308 gimplify_cilk_sync (expr_p, pre_p);
8309 ret = GS_ALL_DONE;
8311 break;
8314 default:
8315 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8317 case tcc_comparison:
8318 /* Handle comparison of objects of non scalar mode aggregates
8319 with a call to memcmp. It would be nice to only have to do
8320 this for variable-sized objects, but then we'd have to allow
8321 the same nest of reference nodes we allow for MODIFY_EXPR and
8322 that's too complex.
8324 Compare scalar mode aggregates as scalar mode values. Using
8325 memcmp for them would be very inefficient at best, and is
8326 plain wrong if bitfields are involved. */
8328 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8330 /* Vector comparisons need no boolification. */
8331 if (TREE_CODE (type) == VECTOR_TYPE)
8332 goto expr_2;
8333 else if (!AGGREGATE_TYPE_P (type))
8335 tree org_type = TREE_TYPE (*expr_p);
8336 *expr_p = gimple_boolify (*expr_p);
8337 if (!useless_type_conversion_p (org_type,
8338 TREE_TYPE (*expr_p)))
8340 *expr_p = fold_convert_loc (input_location,
8341 org_type, *expr_p);
8342 ret = GS_OK;
8344 else
8345 goto expr_2;
8347 else if (TYPE_MODE (type) != BLKmode)
8348 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8349 else
8350 ret = gimplify_variable_sized_compare (expr_p);
8352 break;
8355 /* If *EXPR_P does not need to be special-cased, handle it
8356 according to its class. */
8357 case tcc_unary:
8358 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8359 post_p, is_gimple_val, fb_rvalue);
8360 break;
8362 case tcc_binary:
8363 expr_2:
8365 enum gimplify_status r0, r1;
8367 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8368 post_p, is_gimple_val, fb_rvalue);
8369 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8370 post_p, is_gimple_val, fb_rvalue);
8372 ret = MIN (r0, r1);
8373 break;
8376 expr_3:
8378 enum gimplify_status r0, r1, r2;
8380 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8381 post_p, is_gimple_val, fb_rvalue);
8382 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8383 post_p, is_gimple_val, fb_rvalue);
8384 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8385 post_p, is_gimple_val, fb_rvalue);
8387 ret = MIN (MIN (r0, r1), r2);
8388 break;
8391 case tcc_declaration:
8392 case tcc_constant:
8393 ret = GS_ALL_DONE;
8394 goto dont_recalculate;
8396 default:
8397 gcc_unreachable ();
8400 recalculate_side_effects (*expr_p);
8402 dont_recalculate:
8403 break;
8406 gcc_assert (*expr_p || ret != GS_OK);
8408 while (ret == GS_OK);
8410 /* If we encountered an error_mark somewhere nested inside, either
8411 stub out the statement or propagate the error back out. */
8412 if (ret == GS_ERROR)
8414 if (is_statement)
8415 *expr_p = NULL;
8416 goto out;
8419 /* This was only valid as a return value from the langhook, which
8420 we handled. Make sure it doesn't escape from any other context. */
8421 gcc_assert (ret != GS_UNHANDLED);
8423 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8425 /* We aren't looking for a value, and we don't have a valid
8426 statement. If it doesn't have side-effects, throw it away. */
8427 if (!TREE_SIDE_EFFECTS (*expr_p))
8428 *expr_p = NULL;
8429 else if (!TREE_THIS_VOLATILE (*expr_p))
8431 /* This is probably a _REF that contains something nested that
8432 has side effects. Recurse through the operands to find it. */
8433 enum tree_code code = TREE_CODE (*expr_p);
8435 switch (code)
8437 case COMPONENT_REF:
8438 case REALPART_EXPR:
8439 case IMAGPART_EXPR:
8440 case VIEW_CONVERT_EXPR:
8441 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8442 gimple_test_f, fallback);
8443 break;
8445 case ARRAY_REF:
8446 case ARRAY_RANGE_REF:
8447 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8448 gimple_test_f, fallback);
8449 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8450 gimple_test_f, fallback);
8451 break;
8453 default:
8454 /* Anything else with side-effects must be converted to
8455 a valid statement before we get here. */
8456 gcc_unreachable ();
8459 *expr_p = NULL;
8461 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8462 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8464 /* Historically, the compiler has treated a bare reference
8465 to a non-BLKmode volatile lvalue as forcing a load. */
8466 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8468 /* Normally, we do not want to create a temporary for a
8469 TREE_ADDRESSABLE type because such a type should not be
8470 copied by bitwise-assignment. However, we make an
8471 exception here, as all we are doing here is ensuring that
8472 we read the bytes that make up the type. We use
8473 create_tmp_var_raw because create_tmp_var will abort when
8474 given a TREE_ADDRESSABLE type. */
8475 tree tmp = create_tmp_var_raw (type, "vol");
8476 gimple_add_tmp_var (tmp);
8477 gimplify_assign (tmp, *expr_p, pre_p);
8478 *expr_p = NULL;
8480 else
8481 /* We can't do anything useful with a volatile reference to
8482 an incomplete type, so just throw it away. Likewise for
8483 a BLKmode type, since any implicit inner load should
8484 already have been turned into an explicit one by the
8485 gimplification process. */
8486 *expr_p = NULL;
8489 /* If we are gimplifying at the statement level, we're done. Tack
8490 everything together and return. */
8491 if (fallback == fb_none || is_statement)
8493 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8494 it out for GC to reclaim it. */
8495 *expr_p = NULL_TREE;
8497 if (!gimple_seq_empty_p (internal_pre)
8498 || !gimple_seq_empty_p (internal_post))
8500 gimplify_seq_add_seq (&internal_pre, internal_post);
8501 gimplify_seq_add_seq (pre_p, internal_pre);
8504 /* The result of gimplifying *EXPR_P is going to be the last few
8505 statements in *PRE_P and *POST_P. Add location information
8506 to all the statements that were added by the gimplification
8507 helpers. */
8508 if (!gimple_seq_empty_p (*pre_p))
8509 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8511 if (!gimple_seq_empty_p (*post_p))
8512 annotate_all_with_location_after (*post_p, post_last_gsi,
8513 input_location);
8515 goto out;
8518 #ifdef ENABLE_GIMPLE_CHECKING
8519 if (*expr_p)
8521 enum tree_code code = TREE_CODE (*expr_p);
8522 /* These expressions should already be in gimple IR form. */
8523 gcc_assert (code != MODIFY_EXPR
8524 && code != ASM_EXPR
8525 && code != BIND_EXPR
8526 && code != CATCH_EXPR
8527 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8528 && code != EH_FILTER_EXPR
8529 && code != GOTO_EXPR
8530 && code != LABEL_EXPR
8531 && code != LOOP_EXPR
8532 && code != SWITCH_EXPR
8533 && code != TRY_FINALLY_EXPR
8534 && code != OMP_CRITICAL
8535 && code != OMP_FOR
8536 && code != OMP_MASTER
8537 && code != OMP_TASKGROUP
8538 && code != OMP_ORDERED
8539 && code != OMP_PARALLEL
8540 && code != OMP_SECTIONS
8541 && code != OMP_SECTION
8542 && code != OMP_SINGLE);
8544 #endif
8546 /* Otherwise we're gimplifying a subexpression, so the resulting
8547 value is interesting. If it's a valid operand that matches
8548 GIMPLE_TEST_F, we're done. Unless we are handling some
8549 post-effects internally; if that's the case, we need to copy into
8550 a temporary before adding the post-effects to POST_P. */
8551 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8552 goto out;
8554 /* Otherwise, we need to create a new temporary for the gimplified
8555 expression. */
8557 /* We can't return an lvalue if we have an internal postqueue. The
8558 object the lvalue refers to would (probably) be modified by the
8559 postqueue; we need to copy the value out first, which means an
8560 rvalue. */
8561 if ((fallback & fb_lvalue)
8562 && gimple_seq_empty_p (internal_post)
8563 && is_gimple_addressable (*expr_p))
8565 /* An lvalue will do. Take the address of the expression, store it
8566 in a temporary, and replace the expression with an INDIRECT_REF of
8567 that temporary. */
8568 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8569 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8570 *expr_p = build_simple_mem_ref (tmp);
8572 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8574 /* An rvalue will do. Assign the gimplified expression into a
8575 new temporary TMP and replace the original expression with
8576 TMP. First, make sure that the expression has a type so that
8577 it can be assigned into a temporary. */
8578 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8579 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8581 else
8583 #ifdef ENABLE_GIMPLE_CHECKING
8584 if (!(fallback & fb_mayfail))
8586 fprintf (stderr, "gimplification failed:\n");
8587 print_generic_expr (stderr, *expr_p, 0);
8588 debug_tree (*expr_p);
8589 internal_error ("gimplification failed");
8591 #endif
8592 gcc_assert (fallback & fb_mayfail);
8594 /* If this is an asm statement, and the user asked for the
8595 impossible, don't die. Fail and let gimplify_asm_expr
8596 issue an error. */
8597 ret = GS_ERROR;
8598 goto out;
8601 /* Make sure the temporary matches our predicate. */
8602 gcc_assert ((*gimple_test_f) (*expr_p));
8604 if (!gimple_seq_empty_p (internal_post))
8606 annotate_all_with_location (internal_post, input_location);
8607 gimplify_seq_add_seq (pre_p, internal_post);
8610 out:
8611 input_location = saved_location;
8612 return ret;
8615 /* Look through TYPE for variable-sized objects and gimplify each such
8616 size that we find. Add to LIST_P any statements generated. */
8618 void
8619 gimplify_type_sizes (tree type, gimple_seq *list_p)
8621 tree field, t;
8623 if (type == NULL || type == error_mark_node)
8624 return;
8626 /* We first do the main variant, then copy into any other variants. */
8627 type = TYPE_MAIN_VARIANT (type);
8629 /* Avoid infinite recursion. */
8630 if (TYPE_SIZES_GIMPLIFIED (type))
8631 return;
8633 TYPE_SIZES_GIMPLIFIED (type) = 1;
8635 switch (TREE_CODE (type))
8637 case INTEGER_TYPE:
8638 case ENUMERAL_TYPE:
8639 case BOOLEAN_TYPE:
8640 case REAL_TYPE:
8641 case FIXED_POINT_TYPE:
8642 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8643 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8645 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8647 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8648 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8650 break;
8652 case ARRAY_TYPE:
8653 /* These types may not have declarations, so handle them here. */
8654 gimplify_type_sizes (TREE_TYPE (type), list_p);
8655 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8656 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8657 with assigned stack slots, for -O1+ -g they should be tracked
8658 by VTA. */
8659 if (!(TYPE_NAME (type)
8660 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8661 && DECL_IGNORED_P (TYPE_NAME (type)))
8662 && TYPE_DOMAIN (type)
8663 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8665 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8666 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8667 DECL_IGNORED_P (t) = 0;
8668 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8669 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8670 DECL_IGNORED_P (t) = 0;
8672 break;
8674 case RECORD_TYPE:
8675 case UNION_TYPE:
8676 case QUAL_UNION_TYPE:
8677 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8678 if (TREE_CODE (field) == FIELD_DECL)
8680 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8681 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8682 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8683 gimplify_type_sizes (TREE_TYPE (field), list_p);
8685 break;
8687 case POINTER_TYPE:
8688 case REFERENCE_TYPE:
8689 /* We used to recurse on the pointed-to type here, which turned out to
8690 be incorrect because its definition might refer to variables not
8691 yet initialized at this point if a forward declaration is involved.
8693 It was actually useful for anonymous pointed-to types to ensure
8694 that the sizes evaluation dominates every possible later use of the
8695 values. Restricting to such types here would be safe since there
8696 is no possible forward declaration around, but would introduce an
8697 undesirable middle-end semantic to anonymity. We then defer to
8698 front-ends the responsibility of ensuring that the sizes are
8699 evaluated both early and late enough, e.g. by attaching artificial
8700 type declarations to the tree. */
8701 break;
8703 default:
8704 break;
8707 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8708 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8710 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8712 TYPE_SIZE (t) = TYPE_SIZE (type);
8713 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8714 TYPE_SIZES_GIMPLIFIED (t) = 1;
8718 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8719 a size or position, has had all of its SAVE_EXPRs evaluated.
8720 We add any required statements to *STMT_P. */
8722 void
8723 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8725 tree expr = *expr_p;
8727 /* We don't do anything if the value isn't there, is constant, or contains
8728 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8729 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8730 will want to replace it with a new variable, but that will cause problems
8731 if this type is from outside the function. It's OK to have that here. */
8732 if (is_gimple_sizepos (expr))
8733 return;
8735 *expr_p = unshare_expr (expr);
8737 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8740 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8741 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8742 is true, also gimplify the parameters. */
8744 gimple
8745 gimplify_body (tree fndecl, bool do_parms)
8747 location_t saved_location = input_location;
8748 gimple_seq parm_stmts, seq;
8749 gimple outer_bind;
8750 struct cgraph_node *cgn;
8752 timevar_push (TV_TREE_GIMPLIFY);
8754 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8755 gimplification. */
8756 default_rtl_profile ();
8758 gcc_assert (gimplify_ctxp == NULL);
8759 push_gimplify_context ();
8761 if (flag_openmp)
8763 gcc_assert (gimplify_omp_ctxp == NULL);
8764 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8765 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8768 /* Unshare most shared trees in the body and in that of any nested functions.
8769 It would seem we don't have to do this for nested functions because
8770 they are supposed to be output and then the outer function gimplified
8771 first, but the g++ front end doesn't always do it that way. */
8772 unshare_body (fndecl);
8773 unvisit_body (fndecl);
8775 cgn = cgraph_node::get (fndecl);
8776 if (cgn && cgn->origin)
8777 nonlocal_vlas = new hash_set<tree>;
8779 /* Make sure input_location isn't set to something weird. */
8780 input_location = DECL_SOURCE_LOCATION (fndecl);
8782 /* Resolve callee-copies. This has to be done before processing
8783 the body so that DECL_VALUE_EXPR gets processed correctly. */
8784 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8786 /* Gimplify the function's body. */
8787 seq = NULL;
8788 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8789 outer_bind = gimple_seq_first_stmt (seq);
8790 if (!outer_bind)
8792 outer_bind = gimple_build_nop ();
8793 gimplify_seq_add_stmt (&seq, outer_bind);
8796 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8797 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8798 if (gimple_code (outer_bind) == GIMPLE_BIND
8799 && gimple_seq_first (seq) == gimple_seq_last (seq))
8801 else
8802 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8804 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8806 /* If we had callee-copies statements, insert them at the beginning
8807 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8808 if (!gimple_seq_empty_p (parm_stmts))
8810 tree parm;
8812 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8813 gimple_bind_set_body (outer_bind, parm_stmts);
8815 for (parm = DECL_ARGUMENTS (current_function_decl);
8816 parm; parm = DECL_CHAIN (parm))
8817 if (DECL_HAS_VALUE_EXPR_P (parm))
8819 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8820 DECL_IGNORED_P (parm) = 0;
8824 if (nonlocal_vlas)
8826 if (nonlocal_vla_vars)
8828 /* tree-nested.c may later on call declare_vars (..., true);
8829 which relies on BLOCK_VARS chain to be the tail of the
8830 gimple_bind_vars chain. Ensure we don't violate that
8831 assumption. */
8832 if (gimple_bind_block (outer_bind)
8833 == DECL_INITIAL (current_function_decl))
8834 declare_vars (nonlocal_vla_vars, outer_bind, true);
8835 else
8836 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8837 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8838 nonlocal_vla_vars);
8839 nonlocal_vla_vars = NULL_TREE;
8841 delete nonlocal_vlas;
8842 nonlocal_vlas = NULL;
8845 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8847 delete_omp_context (gimplify_omp_ctxp);
8848 gimplify_omp_ctxp = NULL;
8851 pop_gimplify_context (outer_bind);
8852 gcc_assert (gimplify_ctxp == NULL);
8854 #ifdef ENABLE_CHECKING
8855 if (!seen_error ())
8856 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8857 #endif
8859 timevar_pop (TV_TREE_GIMPLIFY);
8860 input_location = saved_location;
8862 return outer_bind;
8865 typedef char *char_p; /* For DEF_VEC_P. */
8867 /* Return whether we should exclude FNDECL from instrumentation. */
8869 static bool
8870 flag_instrument_functions_exclude_p (tree fndecl)
8872 vec<char_p> *v;
8874 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8875 if (v && v->length () > 0)
8877 const char *name;
8878 int i;
8879 char *s;
8881 name = lang_hooks.decl_printable_name (fndecl, 0);
8882 FOR_EACH_VEC_ELT (*v, i, s)
8883 if (strstr (name, s) != NULL)
8884 return true;
8887 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8888 if (v && v->length () > 0)
8890 const char *name;
8891 int i;
8892 char *s;
8894 name = DECL_SOURCE_FILE (fndecl);
8895 FOR_EACH_VEC_ELT (*v, i, s)
8896 if (strstr (name, s) != NULL)
8897 return true;
8900 return false;
8903 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8904 node for the function we want to gimplify.
8906 Return the sequence of GIMPLE statements corresponding to the body
8907 of FNDECL. */
8909 void
8910 gimplify_function_tree (tree fndecl)
8912 tree parm, ret;
8913 gimple_seq seq;
8914 gimple bind;
8916 gcc_assert (!gimple_body (fndecl));
8918 if (DECL_STRUCT_FUNCTION (fndecl))
8919 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8920 else
8921 push_struct_function (fndecl);
8923 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8925 /* Preliminarily mark non-addressed complex variables as eligible
8926 for promotion to gimple registers. We'll transform their uses
8927 as we find them. */
8928 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8929 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8930 && !TREE_THIS_VOLATILE (parm)
8931 && !needs_to_live_in_memory (parm))
8932 DECL_GIMPLE_REG_P (parm) = 1;
8935 ret = DECL_RESULT (fndecl);
8936 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8937 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8938 && !needs_to_live_in_memory (ret))
8939 DECL_GIMPLE_REG_P (ret) = 1;
8941 bind = gimplify_body (fndecl, true);
8943 /* The tree body of the function is no longer needed, replace it
8944 with the new GIMPLE body. */
8945 seq = NULL;
8946 gimple_seq_add_stmt (&seq, bind);
8947 gimple_set_body (fndecl, seq);
8949 /* If we're instrumenting function entry/exit, then prepend the call to
8950 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8951 catch the exit hook. */
8952 /* ??? Add some way to ignore exceptions for this TFE. */
8953 if (flag_instrument_function_entry_exit
8954 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8955 && !flag_instrument_functions_exclude_p (fndecl))
8957 tree x;
8958 gimple new_bind;
8959 gimple tf;
8960 gimple_seq cleanup = NULL, body = NULL;
8961 tree tmp_var;
8962 gimple call;
8964 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8965 call = gimple_build_call (x, 1, integer_zero_node);
8966 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8967 gimple_call_set_lhs (call, tmp_var);
8968 gimplify_seq_add_stmt (&cleanup, call);
8969 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8970 call = gimple_build_call (x, 2,
8971 build_fold_addr_expr (current_function_decl),
8972 tmp_var);
8973 gimplify_seq_add_stmt (&cleanup, call);
8974 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8976 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8977 call = gimple_build_call (x, 1, integer_zero_node);
8978 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8979 gimple_call_set_lhs (call, tmp_var);
8980 gimplify_seq_add_stmt (&body, call);
8981 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8982 call = gimple_build_call (x, 2,
8983 build_fold_addr_expr (current_function_decl),
8984 tmp_var);
8985 gimplify_seq_add_stmt (&body, call);
8986 gimplify_seq_add_stmt (&body, tf);
8987 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8988 /* Clear the block for BIND, since it is no longer directly inside
8989 the function, but within a try block. */
8990 gimple_bind_set_block (bind, NULL);
8992 /* Replace the current function body with the body
8993 wrapped in the try/finally TF. */
8994 seq = NULL;
8995 gimple_seq_add_stmt (&seq, new_bind);
8996 gimple_set_body (fndecl, seq);
8999 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9000 cfun->curr_properties = PROP_gimple_any;
9002 pop_cfun ();
9005 /* Return a dummy expression of type TYPE in order to keep going after an
9006 error. */
9008 static tree
9009 dummy_object (tree type)
9011 tree t = build_int_cst (build_pointer_type (type), 0);
9012 return build2 (MEM_REF, type, t, t);
9015 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9016 builtin function, but a very special sort of operator. */
9018 enum gimplify_status
9019 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9021 tree promoted_type, have_va_type;
9022 tree valist = TREE_OPERAND (*expr_p, 0);
9023 tree type = TREE_TYPE (*expr_p);
9024 tree t;
9025 location_t loc = EXPR_LOCATION (*expr_p);
9027 /* Verify that valist is of the proper type. */
9028 have_va_type = TREE_TYPE (valist);
9029 if (have_va_type == error_mark_node)
9030 return GS_ERROR;
9031 have_va_type = targetm.canonical_va_list_type (have_va_type);
9033 if (have_va_type == NULL_TREE)
9035 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9036 return GS_ERROR;
9039 /* Generate a diagnostic for requesting data of a type that cannot
9040 be passed through `...' due to type promotion at the call site. */
9041 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9042 != type)
9044 static bool gave_help;
9045 bool warned;
9047 /* Unfortunately, this is merely undefined, rather than a constraint
9048 violation, so we cannot make this an error. If this call is never
9049 executed, the program is still strictly conforming. */
9050 warned = warning_at (loc, 0,
9051 "%qT is promoted to %qT when passed through %<...%>",
9052 type, promoted_type);
9053 if (!gave_help && warned)
9055 gave_help = true;
9056 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9057 promoted_type, type);
9060 /* We can, however, treat "undefined" any way we please.
9061 Call abort to encourage the user to fix the program. */
9062 if (warned)
9063 inform (loc, "if this code is reached, the program will abort");
9064 /* Before the abort, allow the evaluation of the va_list
9065 expression to exit or longjmp. */
9066 gimplify_and_add (valist, pre_p);
9067 t = build_call_expr_loc (loc,
9068 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9069 gimplify_and_add (t, pre_p);
9071 /* This is dead code, but go ahead and finish so that the
9072 mode of the result comes out right. */
9073 *expr_p = dummy_object (type);
9074 return GS_ALL_DONE;
9076 else
9078 /* Make it easier for the backends by protecting the valist argument
9079 from multiple evaluations. */
9080 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9082 /* For this case, the backends will be expecting a pointer to
9083 TREE_TYPE (abi), but it's possible we've
9084 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9085 So fix it. */
9086 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9088 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9089 valist = fold_convert_loc (loc, p1,
9090 build_fold_addr_expr_loc (loc, valist));
9093 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9095 else
9096 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9098 if (!targetm.gimplify_va_arg_expr)
9099 /* FIXME: Once most targets are converted we should merely
9100 assert this is non-null. */
9101 return GS_ALL_DONE;
9103 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9104 return GS_OK;
9108 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9110 DST/SRC are the destination and source respectively. You can pass
9111 ungimplified trees in DST or SRC, in which case they will be
9112 converted to a gimple operand if necessary.
9114 This function returns the newly created GIMPLE_ASSIGN tuple. */
9116 gimple
9117 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9119 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9120 gimplify_and_add (t, seq_p);
9121 ggc_free (t);
9122 return gimple_seq_last_stmt (*seq_p);
9125 inline hashval_t
9126 gimplify_hasher::hash (const value_type *p)
9128 tree t = p->val;
9129 return iterative_hash_expr (t, 0);
9132 inline bool
9133 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9135 tree t1 = p1->val;
9136 tree t2 = p2->val;
9137 enum tree_code code = TREE_CODE (t1);
9139 if (TREE_CODE (t2) != code
9140 || TREE_TYPE (t1) != TREE_TYPE (t2))
9141 return false;
9143 if (!operand_equal_p (t1, t2, 0))
9144 return false;
9146 #ifdef ENABLE_CHECKING
9147 /* Only allow them to compare equal if they also hash equal; otherwise
9148 results are nondeterminate, and we fail bootstrap comparison. */
9149 gcc_assert (hash (p1) == hash (p2));
9150 #endif
9152 return true;