* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / gimplify.c
blob0ebc24c45240f6e84957590aff9c4ebb75ba4259
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 "basic-block.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-fold.h"
34 #include "tree-eh.h"
35 #include "gimple-expr.h"
36 #include "is-a.h"
37 #include "gimple.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "stringpool.h"
41 #include "calls.h"
42 #include "varasm.h"
43 #include "stor-layout.h"
44 #include "stmt.h"
45 #include "print-tree.h"
46 #include "tree-iterator.h"
47 #include "tree-inline.h"
48 #include "tree-pretty-print.h"
49 #include "langhooks.h"
50 #include "bitmap.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssanames.h"
55 #include "tree-ssa.h"
56 #include "diagnostic-core.h"
57 #include "target.h"
58 #include "splay-tree.h"
59 #include "omp-low.h"
60 #include "gimple-low.h"
61 #include "cilk.h"
63 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
64 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
65 #include "builtins.h"
67 enum gimplify_omp_var_data
69 GOVD_SEEN = 1,
70 GOVD_EXPLICIT = 2,
71 GOVD_SHARED = 4,
72 GOVD_PRIVATE = 8,
73 GOVD_FIRSTPRIVATE = 16,
74 GOVD_LASTPRIVATE = 32,
75 GOVD_REDUCTION = 64,
76 GOVD_LOCAL = 128,
77 GOVD_MAP = 256,
78 GOVD_DEBUG_PRIVATE = 512,
79 GOVD_PRIVATE_OUTER_REF = 1024,
80 GOVD_LINEAR = 2048,
81 GOVD_ALIGNED = 4096,
82 GOVD_MAP_TO_ONLY = 8192,
83 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
84 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
85 | GOVD_LOCAL)
89 enum omp_region_type
91 ORT_WORKSHARE = 0,
92 ORT_SIMD = 1,
93 ORT_PARALLEL = 2,
94 ORT_COMBINED_PARALLEL = 3,
95 ORT_TASK = 4,
96 ORT_UNTIED_TASK = 5,
97 ORT_TEAMS = 8,
98 ORT_TARGET_DATA = 16,
99 ORT_TARGET = 32
102 /* Gimplify hashtable helper. */
104 struct gimplify_hasher : typed_free_remove <elt_t>
106 typedef elt_t value_type;
107 typedef elt_t compare_type;
108 static inline hashval_t hash (const value_type *);
109 static inline bool equal (const value_type *, const compare_type *);
112 struct gimplify_ctx
114 struct gimplify_ctx *prev_context;
116 vec<gimple> bind_expr_stack;
117 tree temps;
118 gimple_seq conditional_cleanups;
119 tree exit_label;
120 tree return_temp;
122 vec<tree> case_labels;
123 /* The formal temporary table. Should this be persistent? */
124 hash_table<gimplify_hasher> *temp_htab;
126 int conditions;
127 bool save_stack;
128 bool into_ssa;
129 bool allow_rhs_cond_expr;
130 bool in_cleanup_point_expr;
133 struct gimplify_omp_ctx
135 struct gimplify_omp_ctx *outer_context;
136 splay_tree variables;
137 hash_set<tree> *privatized_types;
138 location_t location;
139 enum omp_clause_default_kind default_kind;
140 enum omp_region_type region_type;
141 bool combined_loop;
142 bool distribute;
145 static struct gimplify_ctx *gimplify_ctxp;
146 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
148 /* Forward declaration. */
149 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
151 /* Shorter alias name for the above function for use in gimplify.c
152 only. */
154 static inline void
155 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
157 gimple_seq_add_stmt_without_update (seq_p, gs);
160 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
161 NULL, a new sequence is allocated. This function is
162 similar to gimple_seq_add_seq, but does not scan the operands.
163 During gimplification, we need to manipulate statement sequences
164 before the def/use vectors have been constructed. */
166 static void
167 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
169 gimple_stmt_iterator si;
171 if (src == NULL)
172 return;
174 si = gsi_last (*dst_p);
175 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
179 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
180 and popping gimplify contexts. */
182 static struct gimplify_ctx *ctx_pool = NULL;
184 /* Return a gimplify context struct from the pool. */
186 static inline struct gimplify_ctx *
187 ctx_alloc (void)
189 struct gimplify_ctx * c = ctx_pool;
191 if (c)
192 ctx_pool = c->prev_context;
193 else
194 c = XNEW (struct gimplify_ctx);
196 memset (c, '\0', sizeof (*c));
197 return c;
200 /* Put gimplify context C back into the pool. */
202 static inline void
203 ctx_free (struct gimplify_ctx *c)
205 c->prev_context = ctx_pool;
206 ctx_pool = c;
209 /* Free allocated ctx stack memory. */
211 void
212 free_gimplify_stack (void)
214 struct gimplify_ctx *c;
216 while ((c = ctx_pool))
218 ctx_pool = c->prev_context;
219 free (c);
224 /* Set up a context for the gimplifier. */
226 void
227 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
229 struct gimplify_ctx *c = ctx_alloc ();
231 c->prev_context = gimplify_ctxp;
232 gimplify_ctxp = c;
233 gimplify_ctxp->into_ssa = in_ssa;
234 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
237 /* Tear down a context for the gimplifier. If BODY is non-null, then
238 put the temporaries into the outer BIND_EXPR. Otherwise, put them
239 in the local_decls.
241 BODY is not a sequence, but the first tuple in a sequence. */
243 void
244 pop_gimplify_context (gimple body)
246 struct gimplify_ctx *c = gimplify_ctxp;
248 gcc_assert (c
249 && (!c->bind_expr_stack.exists ()
250 || c->bind_expr_stack.is_empty ()));
251 c->bind_expr_stack.release ();
252 gimplify_ctxp = c->prev_context;
254 if (body)
255 declare_vars (c->temps, body, false);
256 else
257 record_vars (c->temps);
259 delete c->temp_htab;
260 c->temp_htab = NULL;
261 ctx_free (c);
264 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
266 static void
267 gimple_push_bind_expr (gimple gimple_bind)
269 gimplify_ctxp->bind_expr_stack.reserve (8);
270 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
273 /* Pop the first element off the stack of bindings. */
275 static void
276 gimple_pop_bind_expr (void)
278 gimplify_ctxp->bind_expr_stack.pop ();
281 /* Return the first element of the stack of bindings. */
283 gimple
284 gimple_current_bind_expr (void)
286 return gimplify_ctxp->bind_expr_stack.last ();
289 /* Return the stack of bindings created during gimplification. */
291 vec<gimple>
292 gimple_bind_expr_stack (void)
294 return gimplify_ctxp->bind_expr_stack;
297 /* Return true iff there is a COND_EXPR between us and the innermost
298 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
300 static bool
301 gimple_conditional_context (void)
303 return gimplify_ctxp->conditions > 0;
306 /* Note that we've entered a COND_EXPR. */
308 static void
309 gimple_push_condition (void)
311 #ifdef ENABLE_GIMPLE_CHECKING
312 if (gimplify_ctxp->conditions == 0)
313 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
314 #endif
315 ++(gimplify_ctxp->conditions);
318 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
319 now, add any conditional cleanups we've seen to the prequeue. */
321 static void
322 gimple_pop_condition (gimple_seq *pre_p)
324 int conds = --(gimplify_ctxp->conditions);
326 gcc_assert (conds >= 0);
327 if (conds == 0)
329 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
330 gimplify_ctxp->conditional_cleanups = NULL;
334 /* A stable comparison routine for use with splay trees and DECLs. */
336 static int
337 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
339 tree a = (tree) xa;
340 tree b = (tree) xb;
342 return DECL_UID (a) - DECL_UID (b);
345 /* Create a new omp construct that deals with variable remapping. */
347 static struct gimplify_omp_ctx *
348 new_omp_context (enum omp_region_type region_type)
350 struct gimplify_omp_ctx *c;
352 c = XCNEW (struct gimplify_omp_ctx);
353 c->outer_context = gimplify_omp_ctxp;
354 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
355 c->privatized_types = new hash_set<tree>;
356 c->location = input_location;
357 c->region_type = region_type;
358 if ((region_type & ORT_TASK) == 0)
359 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
360 else
361 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
363 return c;
366 /* Destroy an omp construct that deals with variable remapping. */
368 static void
369 delete_omp_context (struct gimplify_omp_ctx *c)
371 splay_tree_delete (c->variables);
372 delete c->privatized_types;
373 XDELETE (c);
376 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
377 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
379 /* Both gimplify the statement T and append it to *SEQ_P. This function
380 behaves exactly as gimplify_stmt, but you don't have to pass T as a
381 reference. */
383 void
384 gimplify_and_add (tree t, gimple_seq *seq_p)
386 gimplify_stmt (&t, seq_p);
389 /* Gimplify statement T into sequence *SEQ_P, and return the first
390 tuple in the sequence of generated tuples for this statement.
391 Return NULL if gimplifying T produced no tuples. */
393 static gimple
394 gimplify_and_return_first (tree t, gimple_seq *seq_p)
396 gimple_stmt_iterator last = gsi_last (*seq_p);
398 gimplify_and_add (t, seq_p);
400 if (!gsi_end_p (last))
402 gsi_next (&last);
403 return gsi_stmt (last);
405 else
406 return gimple_seq_first_stmt (*seq_p);
409 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
410 LHS, or for a call argument. */
412 static bool
413 is_gimple_mem_rhs (tree t)
415 /* If we're dealing with a renamable type, either source or dest must be
416 a renamed variable. */
417 if (is_gimple_reg_type (TREE_TYPE (t)))
418 return is_gimple_val (t);
419 else
420 return is_gimple_val (t) || is_gimple_lvalue (t);
423 /* Return true if T is a CALL_EXPR or an expression that can be
424 assigned to a temporary. Note that this predicate should only be
425 used during gimplification. See the rationale for this in
426 gimplify_modify_expr. */
428 static bool
429 is_gimple_reg_rhs_or_call (tree t)
431 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
432 || TREE_CODE (t) == CALL_EXPR);
435 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
436 this predicate should only be used during gimplification. See the
437 rationale for this in gimplify_modify_expr. */
439 static bool
440 is_gimple_mem_rhs_or_call (tree t)
442 /* If we're dealing with a renamable type, either source or dest must be
443 a renamed variable. */
444 if (is_gimple_reg_type (TREE_TYPE (t)))
445 return is_gimple_val (t);
446 else
447 return (is_gimple_val (t) || is_gimple_lvalue (t)
448 || TREE_CODE (t) == CALL_EXPR);
451 /* Create a temporary with a name derived from VAL. Subroutine of
452 lookup_tmp_var; nobody else should call this function. */
454 static inline tree
455 create_tmp_from_val (tree val)
457 /* Drop all qualifiers and address-space information from the value type. */
458 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
459 tree var = create_tmp_var (type, get_name (val));
460 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
461 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
462 DECL_GIMPLE_REG_P (var) = 1;
463 return var;
466 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
467 an existing expression temporary. */
469 static tree
470 lookup_tmp_var (tree val, bool is_formal)
472 tree ret;
474 /* If not optimizing, never really reuse a temporary. local-alloc
475 won't allocate any variable that is used in more than one basic
476 block, which means it will go into memory, causing much extra
477 work in reload and final and poorer code generation, outweighing
478 the extra memory allocation here. */
479 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
480 ret = create_tmp_from_val (val);
481 else
483 elt_t elt, *elt_p;
484 elt_t **slot;
486 elt.val = val;
487 if (!gimplify_ctxp->temp_htab)
488 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
489 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
490 if (*slot == NULL)
492 elt_p = XNEW (elt_t);
493 elt_p->val = val;
494 elt_p->temp = ret = create_tmp_from_val (val);
495 *slot = elt_p;
497 else
499 elt_p = *slot;
500 ret = elt_p->temp;
504 return ret;
507 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
509 static tree
510 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
511 bool is_formal)
513 tree t, mod;
515 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
516 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
517 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
518 fb_rvalue);
520 if (gimplify_ctxp->into_ssa
521 && is_gimple_reg_type (TREE_TYPE (val)))
522 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
523 else
524 t = lookup_tmp_var (val, is_formal);
526 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
528 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
530 /* gimplify_modify_expr might want to reduce this further. */
531 gimplify_and_add (mod, pre_p);
532 ggc_free (mod);
534 return t;
537 /* Return a formal temporary variable initialized with VAL. PRE_P is as
538 in gimplify_expr. Only use this function if:
540 1) The value of the unfactored expression represented by VAL will not
541 change between the initialization and use of the temporary, and
542 2) The temporary will not be otherwise modified.
544 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
545 and #2 means it is inappropriate for && temps.
547 For other cases, use get_initialized_tmp_var instead. */
549 tree
550 get_formal_tmp_var (tree val, gimple_seq *pre_p)
552 return internal_get_tmp_var (val, pre_p, NULL, true);
555 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
556 are as in gimplify_expr. */
558 tree
559 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
561 return internal_get_tmp_var (val, pre_p, post_p, false);
564 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
565 generate debug info for them; otherwise don't. */
567 void
568 declare_vars (tree vars, gimple scope, bool debug_info)
570 tree last = vars;
571 if (last)
573 tree temps, block;
575 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
577 temps = nreverse (last);
579 block = gimple_bind_block (scope);
580 gcc_assert (!block || TREE_CODE (block) == BLOCK);
581 if (!block || !debug_info)
583 DECL_CHAIN (last) = gimple_bind_vars (scope);
584 gimple_bind_set_vars (scope, temps);
586 else
588 /* We need to attach the nodes both to the BIND_EXPR and to its
589 associated BLOCK for debugging purposes. The key point here
590 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
591 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
592 if (BLOCK_VARS (block))
593 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
594 else
596 gimple_bind_set_vars (scope,
597 chainon (gimple_bind_vars (scope), temps));
598 BLOCK_VARS (block) = temps;
604 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
605 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
606 no such upper bound can be obtained. */
608 static void
609 force_constant_size (tree var)
611 /* The only attempt we make is by querying the maximum size of objects
612 of the variable's type. */
614 HOST_WIDE_INT max_size;
616 gcc_assert (TREE_CODE (var) == VAR_DECL);
618 max_size = max_int_size_in_bytes (TREE_TYPE (var));
620 gcc_assert (max_size >= 0);
622 DECL_SIZE_UNIT (var)
623 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
624 DECL_SIZE (var)
625 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
628 /* Push the temporary variable TMP into the current binding. */
630 void
631 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
633 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
635 /* Later processing assumes that the object size is constant, which might
636 not be true at this point. Force the use of a constant upper bound in
637 this case. */
638 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
639 force_constant_size (tmp);
641 DECL_CONTEXT (tmp) = fn->decl;
642 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
644 record_vars_into (tmp, fn->decl);
647 /* Push the temporary variable TMP into the current binding. */
649 void
650 gimple_add_tmp_var (tree tmp)
652 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
654 /* Later processing assumes that the object size is constant, which might
655 not be true at this point. Force the use of a constant upper bound in
656 this case. */
657 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
658 force_constant_size (tmp);
660 DECL_CONTEXT (tmp) = current_function_decl;
661 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
663 if (gimplify_ctxp)
665 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
666 gimplify_ctxp->temps = tmp;
668 /* Mark temporaries local within the nearest enclosing parallel. */
669 if (gimplify_omp_ctxp)
671 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
672 while (ctx
673 && (ctx->region_type == ORT_WORKSHARE
674 || ctx->region_type == ORT_SIMD))
675 ctx = ctx->outer_context;
676 if (ctx)
677 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
680 else if (cfun)
681 record_vars (tmp);
682 else
684 gimple_seq body_seq;
686 /* This case is for nested functions. We need to expose the locals
687 they create. */
688 body_seq = gimple_body (current_function_decl);
689 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
695 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
696 nodes that are referenced more than once in GENERIC functions. This is
697 necessary because gimplification (translation into GIMPLE) is performed
698 by modifying tree nodes in-place, so gimplication of a shared node in a
699 first context could generate an invalid GIMPLE form in a second context.
701 This is achieved with a simple mark/copy/unmark algorithm that walks the
702 GENERIC representation top-down, marks nodes with TREE_VISITED the first
703 time it encounters them, duplicates them if they already have TREE_VISITED
704 set, and finally removes the TREE_VISITED marks it has set.
706 The algorithm works only at the function level, i.e. it generates a GENERIC
707 representation of a function with no nodes shared within the function when
708 passed a GENERIC function (except for nodes that are allowed to be shared).
710 At the global level, it is also necessary to unshare tree nodes that are
711 referenced in more than one function, for the same aforementioned reason.
712 This requires some cooperation from the front-end. There are 2 strategies:
714 1. Manual unsharing. The front-end needs to call unshare_expr on every
715 expression that might end up being shared across functions.
717 2. Deep unsharing. This is an extension of regular unsharing. Instead
718 of calling unshare_expr on expressions that might be shared across
719 functions, the front-end pre-marks them with TREE_VISITED. This will
720 ensure that they are unshared on the first reference within functions
721 when the regular unsharing algorithm runs. The counterpart is that
722 this algorithm must look deeper than for manual unsharing, which is
723 specified by LANG_HOOKS_DEEP_UNSHARING.
725 If there are only few specific cases of node sharing across functions, it is
726 probably easier for a front-end to unshare the expressions manually. On the
727 contrary, if the expressions generated at the global level are as widespread
728 as expressions generated within functions, deep unsharing is very likely the
729 way to go. */
731 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
732 These nodes model computations that must be done once. If we were to
733 unshare something like SAVE_EXPR(i++), the gimplification process would
734 create wrong code. However, if DATA is non-null, it must hold a pointer
735 set that is used to unshare the subtrees of these nodes. */
737 static tree
738 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
740 tree t = *tp;
741 enum tree_code code = TREE_CODE (t);
743 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
744 copy their subtrees if we can make sure to do it only once. */
745 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
747 if (data && !((hash_set<tree> *)data)->add (t))
749 else
750 *walk_subtrees = 0;
753 /* Stop at types, decls, constants like copy_tree_r. */
754 else if (TREE_CODE_CLASS (code) == tcc_type
755 || TREE_CODE_CLASS (code) == tcc_declaration
756 || TREE_CODE_CLASS (code) == tcc_constant
757 /* We can't do anything sensible with a BLOCK used as an
758 expression, but we also can't just die when we see it
759 because of non-expression uses. So we avert our eyes
760 and cross our fingers. Silly Java. */
761 || code == BLOCK)
762 *walk_subtrees = 0;
764 /* Cope with the statement expression extension. */
765 else if (code == STATEMENT_LIST)
768 /* Leave the bulk of the work to copy_tree_r itself. */
769 else
770 copy_tree_r (tp, walk_subtrees, NULL);
772 return NULL_TREE;
775 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
776 If *TP has been visited already, then *TP is deeply copied by calling
777 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
779 static tree
780 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
782 tree t = *tp;
783 enum tree_code code = TREE_CODE (t);
785 /* Skip types, decls, and constants. But we do want to look at their
786 types and the bounds of types. Mark them as visited so we properly
787 unmark their subtrees on the unmark pass. If we've already seen them,
788 don't look down further. */
789 if (TREE_CODE_CLASS (code) == tcc_type
790 || TREE_CODE_CLASS (code) == tcc_declaration
791 || TREE_CODE_CLASS (code) == tcc_constant)
793 if (TREE_VISITED (t))
794 *walk_subtrees = 0;
795 else
796 TREE_VISITED (t) = 1;
799 /* If this node has been visited already, unshare it and don't look
800 any deeper. */
801 else if (TREE_VISITED (t))
803 walk_tree (tp, mostly_copy_tree_r, data, NULL);
804 *walk_subtrees = 0;
807 /* Otherwise, mark the node as visited and keep looking. */
808 else
809 TREE_VISITED (t) = 1;
811 return NULL_TREE;
814 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
815 copy_if_shared_r callback unmodified. */
817 static inline void
818 copy_if_shared (tree *tp, void *data)
820 walk_tree (tp, copy_if_shared_r, data, NULL);
823 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
824 any nested functions. */
826 static void
827 unshare_body (tree fndecl)
829 struct cgraph_node *cgn = cgraph_node::get (fndecl);
830 /* If the language requires deep unsharing, we need a pointer set to make
831 sure we don't repeatedly unshare subtrees of unshareable nodes. */
832 hash_set<tree> *visited
833 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
835 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
836 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
837 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
839 delete visited;
841 if (cgn)
842 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
843 unshare_body (cgn->decl);
846 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
847 Subtrees are walked until the first unvisited node is encountered. */
849 static tree
850 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
852 tree t = *tp;
854 /* If this node has been visited, unmark it and keep looking. */
855 if (TREE_VISITED (t))
856 TREE_VISITED (t) = 0;
858 /* Otherwise, don't look any deeper. */
859 else
860 *walk_subtrees = 0;
862 return NULL_TREE;
865 /* Unmark the visited trees rooted at *TP. */
867 static inline void
868 unmark_visited (tree *tp)
870 walk_tree (tp, unmark_visited_r, NULL, NULL);
873 /* Likewise, but mark all trees as not visited. */
875 static void
876 unvisit_body (tree fndecl)
878 struct cgraph_node *cgn = cgraph_node::get (fndecl);
880 unmark_visited (&DECL_SAVED_TREE (fndecl));
881 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
882 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
884 if (cgn)
885 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
886 unvisit_body (cgn->decl);
889 /* Unconditionally make an unshared copy of EXPR. This is used when using
890 stored expressions which span multiple functions, such as BINFO_VTABLE,
891 as the normal unsharing process can't tell that they're shared. */
893 tree
894 unshare_expr (tree expr)
896 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
897 return expr;
900 /* Worker for unshare_expr_without_location. */
902 static tree
903 prune_expr_location (tree *tp, int *walk_subtrees, void *)
905 if (EXPR_P (*tp))
906 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
907 else
908 *walk_subtrees = 0;
909 return NULL_TREE;
912 /* Similar to unshare_expr but also prune all expression locations
913 from EXPR. */
915 tree
916 unshare_expr_without_location (tree expr)
918 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
919 if (EXPR_P (expr))
920 walk_tree (&expr, prune_expr_location, NULL, NULL);
921 return expr;
924 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
925 contain statements and have a value. Assign its value to a temporary
926 and give it void_type_node. Return the temporary, or NULL_TREE if
927 WRAPPER was already void. */
929 tree
930 voidify_wrapper_expr (tree wrapper, tree temp)
932 tree type = TREE_TYPE (wrapper);
933 if (type && !VOID_TYPE_P (type))
935 tree *p;
937 /* Set p to point to the body of the wrapper. Loop until we find
938 something that isn't a wrapper. */
939 for (p = &wrapper; p && *p; )
941 switch (TREE_CODE (*p))
943 case BIND_EXPR:
944 TREE_SIDE_EFFECTS (*p) = 1;
945 TREE_TYPE (*p) = void_type_node;
946 /* For a BIND_EXPR, the body is operand 1. */
947 p = &BIND_EXPR_BODY (*p);
948 break;
950 case CLEANUP_POINT_EXPR:
951 case TRY_FINALLY_EXPR:
952 case TRY_CATCH_EXPR:
953 TREE_SIDE_EFFECTS (*p) = 1;
954 TREE_TYPE (*p) = void_type_node;
955 p = &TREE_OPERAND (*p, 0);
956 break;
958 case STATEMENT_LIST:
960 tree_stmt_iterator i = tsi_last (*p);
961 TREE_SIDE_EFFECTS (*p) = 1;
962 TREE_TYPE (*p) = void_type_node;
963 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
965 break;
967 case COMPOUND_EXPR:
968 /* Advance to the last statement. Set all container types to
969 void. */
970 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
972 TREE_SIDE_EFFECTS (*p) = 1;
973 TREE_TYPE (*p) = void_type_node;
975 break;
977 case TRANSACTION_EXPR:
978 TREE_SIDE_EFFECTS (*p) = 1;
979 TREE_TYPE (*p) = void_type_node;
980 p = &TRANSACTION_EXPR_BODY (*p);
981 break;
983 default:
984 /* Assume that any tree upon which voidify_wrapper_expr is
985 directly called is a wrapper, and that its body is op0. */
986 if (p == &wrapper)
988 TREE_SIDE_EFFECTS (*p) = 1;
989 TREE_TYPE (*p) = void_type_node;
990 p = &TREE_OPERAND (*p, 0);
991 break;
993 goto out;
997 out:
998 if (p == NULL || IS_EMPTY_STMT (*p))
999 temp = NULL_TREE;
1000 else if (temp)
1002 /* The wrapper is on the RHS of an assignment that we're pushing
1003 down. */
1004 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1005 || TREE_CODE (temp) == MODIFY_EXPR);
1006 TREE_OPERAND (temp, 1) = *p;
1007 *p = temp;
1009 else
1011 temp = create_tmp_var (type, "retval");
1012 *p = build2 (INIT_EXPR, type, temp, *p);
1015 return temp;
1018 return NULL_TREE;
1021 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1022 a temporary through which they communicate. */
1024 static void
1025 build_stack_save_restore (gimple *save, gimple *restore)
1027 tree tmp_var;
1029 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1030 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1031 gimple_call_set_lhs (*save, tmp_var);
1033 *restore
1034 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1035 1, tmp_var);
1038 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1040 static enum gimplify_status
1041 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1043 tree bind_expr = *expr_p;
1044 bool old_save_stack = gimplify_ctxp->save_stack;
1045 tree t;
1046 gimple gimple_bind;
1047 gimple_seq body, cleanup;
1048 gimple stack_save;
1049 location_t start_locus = 0, end_locus = 0;
1051 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1053 /* Mark variables seen in this bind expr. */
1054 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1056 if (TREE_CODE (t) == VAR_DECL)
1058 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1060 /* Mark variable as local. */
1061 if (ctx && !DECL_EXTERNAL (t)
1062 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1063 || splay_tree_lookup (ctx->variables,
1064 (splay_tree_key) t) == NULL))
1066 if (ctx->region_type == ORT_SIMD
1067 && TREE_ADDRESSABLE (t)
1068 && !TREE_STATIC (t))
1069 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1070 else
1071 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1074 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1076 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1077 cfun->has_local_explicit_reg_vars = true;
1080 /* Preliminarily mark non-addressed complex variables as eligible
1081 for promotion to gimple registers. We'll transform their uses
1082 as we find them. */
1083 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1084 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1085 && !TREE_THIS_VOLATILE (t)
1086 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1087 && !needs_to_live_in_memory (t))
1088 DECL_GIMPLE_REG_P (t) = 1;
1091 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1092 BIND_EXPR_BLOCK (bind_expr));
1093 gimple_push_bind_expr (gimple_bind);
1095 gimplify_ctxp->save_stack = false;
1097 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1098 body = NULL;
1099 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1100 gimple_bind_set_body (gimple_bind, body);
1102 /* Source location wise, the cleanup code (stack_restore and clobbers)
1103 belongs to the end of the block, so propagate what we have. The
1104 stack_save operation belongs to the beginning of block, which we can
1105 infer from the bind_expr directly if the block has no explicit
1106 assignment. */
1107 if (BIND_EXPR_BLOCK (bind_expr))
1109 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1110 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1112 if (start_locus == 0)
1113 start_locus = EXPR_LOCATION (bind_expr);
1115 cleanup = NULL;
1116 stack_save = NULL;
1117 if (gimplify_ctxp->save_stack)
1119 gimple stack_restore;
1121 /* Save stack on entry and restore it on exit. Add a try_finally
1122 block to achieve this. */
1123 build_stack_save_restore (&stack_save, &stack_restore);
1125 gimple_set_location (stack_save, start_locus);
1126 gimple_set_location (stack_restore, end_locus);
1128 gimplify_seq_add_stmt (&cleanup, stack_restore);
1131 /* Add clobbers for all variables that go out of scope. */
1132 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1134 if (TREE_CODE (t) == VAR_DECL
1135 && !is_global_var (t)
1136 && DECL_CONTEXT (t) == current_function_decl
1137 && !DECL_HARD_REGISTER (t)
1138 && !TREE_THIS_VOLATILE (t)
1139 && !DECL_HAS_VALUE_EXPR_P (t)
1140 /* Only care for variables that have to be in memory. Others
1141 will be rewritten into SSA names, hence moved to the top-level. */
1142 && !is_gimple_reg (t)
1143 && flag_stack_reuse != SR_NONE)
1145 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1146 gimple clobber_stmt;
1147 TREE_THIS_VOLATILE (clobber) = 1;
1148 clobber_stmt = gimple_build_assign (t, clobber);
1149 gimple_set_location (clobber_stmt, end_locus);
1150 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1154 if (cleanup)
1156 gimple gs;
1157 gimple_seq new_body;
1159 new_body = NULL;
1160 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1161 GIMPLE_TRY_FINALLY);
1163 if (stack_save)
1164 gimplify_seq_add_stmt (&new_body, stack_save);
1165 gimplify_seq_add_stmt (&new_body, gs);
1166 gimple_bind_set_body (gimple_bind, new_body);
1169 gimplify_ctxp->save_stack = old_save_stack;
1170 gimple_pop_bind_expr ();
1172 gimplify_seq_add_stmt (pre_p, gimple_bind);
1174 if (temp)
1176 *expr_p = temp;
1177 return GS_OK;
1180 *expr_p = NULL_TREE;
1181 return GS_ALL_DONE;
1184 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1185 GIMPLE value, it is assigned to a new temporary and the statement is
1186 re-written to return the temporary.
1188 PRE_P points to the sequence where side effects that must happen before
1189 STMT should be stored. */
1191 static enum gimplify_status
1192 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1194 gimple ret;
1195 tree ret_expr = TREE_OPERAND (stmt, 0);
1196 tree result_decl, result;
1198 if (ret_expr == error_mark_node)
1199 return GS_ERROR;
1201 /* Implicit _Cilk_sync must be inserted right before any return statement
1202 if there is a _Cilk_spawn in the function. If the user has provided a
1203 _Cilk_sync, the optimizer should remove this duplicate one. */
1204 if (fn_contains_cilk_spawn_p (cfun))
1206 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1207 gimplify_and_add (impl_sync, pre_p);
1210 if (!ret_expr
1211 || TREE_CODE (ret_expr) == RESULT_DECL
1212 || ret_expr == error_mark_node)
1214 gimple ret = gimple_build_return (ret_expr);
1215 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1216 gimplify_seq_add_stmt (pre_p, ret);
1217 return GS_ALL_DONE;
1220 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1221 result_decl = NULL_TREE;
1222 else
1224 result_decl = TREE_OPERAND (ret_expr, 0);
1226 /* See through a return by reference. */
1227 if (TREE_CODE (result_decl) == INDIRECT_REF)
1228 result_decl = TREE_OPERAND (result_decl, 0);
1230 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1231 || TREE_CODE (ret_expr) == INIT_EXPR)
1232 && TREE_CODE (result_decl) == RESULT_DECL);
1235 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1236 Recall that aggregate_value_p is FALSE for any aggregate type that is
1237 returned in registers. If we're returning values in registers, then
1238 we don't want to extend the lifetime of the RESULT_DECL, particularly
1239 across another call. In addition, for those aggregates for which
1240 hard_function_value generates a PARALLEL, we'll die during normal
1241 expansion of structure assignments; there's special code in expand_return
1242 to handle this case that does not exist in expand_expr. */
1243 if (!result_decl)
1244 result = NULL_TREE;
1245 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1247 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1249 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1250 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1251 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1252 should be effectively allocated by the caller, i.e. all calls to
1253 this function must be subject to the Return Slot Optimization. */
1254 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1255 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1257 result = result_decl;
1259 else if (gimplify_ctxp->return_temp)
1260 result = gimplify_ctxp->return_temp;
1261 else
1263 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1265 /* ??? With complex control flow (usually involving abnormal edges),
1266 we can wind up warning about an uninitialized value for this. Due
1267 to how this variable is constructed and initialized, this is never
1268 true. Give up and never warn. */
1269 TREE_NO_WARNING (result) = 1;
1271 gimplify_ctxp->return_temp = result;
1274 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1275 Then gimplify the whole thing. */
1276 if (result != result_decl)
1277 TREE_OPERAND (ret_expr, 0) = result;
1279 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1281 ret = gimple_build_return (result);
1282 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1283 gimplify_seq_add_stmt (pre_p, ret);
1285 return GS_ALL_DONE;
1288 /* Gimplify a variable-length array DECL. */
1290 static void
1291 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1293 /* This is a variable-sized decl. Simplify its size and mark it
1294 for deferred expansion. */
1295 tree t, addr, ptr_type;
1297 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1298 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1300 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1301 if (DECL_HAS_VALUE_EXPR_P (decl))
1302 return;
1304 /* All occurrences of this decl in final gimplified code will be
1305 replaced by indirection. Setting DECL_VALUE_EXPR does two
1306 things: First, it lets the rest of the gimplifier know what
1307 replacement to use. Second, it lets the debug info know
1308 where to find the value. */
1309 ptr_type = build_pointer_type (TREE_TYPE (decl));
1310 addr = create_tmp_var (ptr_type, get_name (decl));
1311 DECL_IGNORED_P (addr) = 0;
1312 t = build_fold_indirect_ref (addr);
1313 TREE_THIS_NOTRAP (t) = 1;
1314 SET_DECL_VALUE_EXPR (decl, t);
1315 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1317 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1318 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1319 size_int (DECL_ALIGN (decl)));
1320 /* The call has been built for a variable-sized object. */
1321 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1322 t = fold_convert (ptr_type, t);
1323 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1325 gimplify_and_add (t, seq_p);
1327 /* Indicate that we need to restore the stack level when the
1328 enclosing BIND_EXPR is exited. */
1329 gimplify_ctxp->save_stack = true;
1332 /* A helper function to be called via walk_tree. Mark all labels under *TP
1333 as being forced. To be called for DECL_INITIAL of static variables. */
1335 static tree
1336 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1338 if (TYPE_P (*tp))
1339 *walk_subtrees = 0;
1340 if (TREE_CODE (*tp) == LABEL_DECL)
1341 FORCED_LABEL (*tp) = 1;
1343 return NULL_TREE;
1346 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1347 and initialization explicit. */
1349 static enum gimplify_status
1350 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1352 tree stmt = *stmt_p;
1353 tree decl = DECL_EXPR_DECL (stmt);
1355 *stmt_p = NULL_TREE;
1357 if (TREE_TYPE (decl) == error_mark_node)
1358 return GS_ERROR;
1360 if ((TREE_CODE (decl) == TYPE_DECL
1361 || TREE_CODE (decl) == VAR_DECL)
1362 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1363 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1365 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1366 in case its size expressions contain problematic nodes like CALL_EXPR. */
1367 if (TREE_CODE (decl) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (decl)
1369 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1370 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1372 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1374 tree init = DECL_INITIAL (decl);
1376 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1377 || (!TREE_STATIC (decl)
1378 && flag_stack_check == GENERIC_STACK_CHECK
1379 && compare_tree_int (DECL_SIZE_UNIT (decl),
1380 STACK_CHECK_MAX_VAR_SIZE) > 0))
1381 gimplify_vla_decl (decl, seq_p);
1383 /* Some front ends do not explicitly declare all anonymous
1384 artificial variables. We compensate here by declaring the
1385 variables, though it would be better if the front ends would
1386 explicitly declare them. */
1387 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1388 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1389 gimple_add_tmp_var (decl);
1391 if (init && init != error_mark_node)
1393 if (!TREE_STATIC (decl))
1395 DECL_INITIAL (decl) = NULL_TREE;
1396 init = build2 (INIT_EXPR, void_type_node, decl, init);
1397 gimplify_and_add (init, seq_p);
1398 ggc_free (init);
1400 else
1401 /* We must still examine initializers for static variables
1402 as they may contain a label address. */
1403 walk_tree (&init, force_labels_r, NULL, NULL);
1407 return GS_ALL_DONE;
1410 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1411 and replacing the LOOP_EXPR with goto, but if the loop contains an
1412 EXIT_EXPR, we need to append a label for it to jump to. */
1414 static enum gimplify_status
1415 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1417 tree saved_label = gimplify_ctxp->exit_label;
1418 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1420 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1422 gimplify_ctxp->exit_label = NULL_TREE;
1424 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1426 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1428 if (gimplify_ctxp->exit_label)
1429 gimplify_seq_add_stmt (pre_p,
1430 gimple_build_label (gimplify_ctxp->exit_label));
1432 gimplify_ctxp->exit_label = saved_label;
1434 *expr_p = NULL;
1435 return GS_ALL_DONE;
1438 /* Gimplify a statement list onto a sequence. These may be created either
1439 by an enlightened front-end, or by shortcut_cond_expr. */
1441 static enum gimplify_status
1442 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1444 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1446 tree_stmt_iterator i = tsi_start (*expr_p);
1448 while (!tsi_end_p (i))
1450 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1451 tsi_delink (&i);
1454 if (temp)
1456 *expr_p = temp;
1457 return GS_OK;
1460 return GS_ALL_DONE;
1464 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1465 branch to. */
1467 static enum gimplify_status
1468 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1470 tree switch_expr = *expr_p;
1471 gimple_seq switch_body_seq = NULL;
1472 enum gimplify_status ret;
1473 tree index_type = TREE_TYPE (switch_expr);
1474 if (index_type == NULL_TREE)
1475 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1477 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1478 fb_rvalue);
1479 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1480 return ret;
1482 if (SWITCH_BODY (switch_expr))
1484 vec<tree> labels;
1485 vec<tree> saved_labels;
1486 tree default_case = NULL_TREE;
1487 gimple gimple_switch;
1489 /* If someone can be bothered to fill in the labels, they can
1490 be bothered to null out the body too. */
1491 gcc_assert (!SWITCH_LABELS (switch_expr));
1493 /* Save old labels, get new ones from body, then restore the old
1494 labels. Save all the things from the switch body to append after. */
1495 saved_labels = gimplify_ctxp->case_labels;
1496 gimplify_ctxp->case_labels.create (8);
1498 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1499 labels = gimplify_ctxp->case_labels;
1500 gimplify_ctxp->case_labels = saved_labels;
1502 preprocess_case_label_vec_for_gimple (labels, index_type,
1503 &default_case);
1505 if (!default_case)
1507 gimple new_default;
1509 default_case
1510 = build_case_label (NULL_TREE, NULL_TREE,
1511 create_artificial_label (UNKNOWN_LOCATION));
1512 new_default = gimple_build_label (CASE_LABEL (default_case));
1513 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1516 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1517 default_case, labels);
1518 gimplify_seq_add_stmt (pre_p, gimple_switch);
1519 gimplify_seq_add_seq (pre_p, switch_body_seq);
1520 labels.release ();
1522 else
1523 gcc_assert (SWITCH_LABELS (switch_expr));
1525 return GS_ALL_DONE;
1528 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1530 static enum gimplify_status
1531 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1533 struct gimplify_ctx *ctxp;
1534 gimple gimple_label;
1536 /* Invalid OpenMP programs can play Duff's Device type games with
1537 #pragma omp parallel. At least in the C front end, we don't
1538 detect such invalid branches until after gimplification. */
1539 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1540 if (ctxp->case_labels.exists ())
1541 break;
1543 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1544 ctxp->case_labels.safe_push (*expr_p);
1545 gimplify_seq_add_stmt (pre_p, gimple_label);
1547 return GS_ALL_DONE;
1550 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1551 if necessary. */
1553 tree
1554 build_and_jump (tree *label_p)
1556 if (label_p == NULL)
1557 /* If there's nowhere to jump, just fall through. */
1558 return NULL_TREE;
1560 if (*label_p == NULL_TREE)
1562 tree label = create_artificial_label (UNKNOWN_LOCATION);
1563 *label_p = label;
1566 return build1 (GOTO_EXPR, void_type_node, *label_p);
1569 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1570 This also involves building a label to jump to and communicating it to
1571 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1573 static enum gimplify_status
1574 gimplify_exit_expr (tree *expr_p)
1576 tree cond = TREE_OPERAND (*expr_p, 0);
1577 tree expr;
1579 expr = build_and_jump (&gimplify_ctxp->exit_label);
1580 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1581 *expr_p = expr;
1583 return GS_OK;
1586 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1587 different from its canonical type, wrap the whole thing inside a
1588 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1589 type.
1591 The canonical type of a COMPONENT_REF is the type of the field being
1592 referenced--unless the field is a bit-field which can be read directly
1593 in a smaller mode, in which case the canonical type is the
1594 sign-appropriate type corresponding to that mode. */
1596 static void
1597 canonicalize_component_ref (tree *expr_p)
1599 tree expr = *expr_p;
1600 tree type;
1602 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1604 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1605 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1606 else
1607 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1609 /* One could argue that all the stuff below is not necessary for
1610 the non-bitfield case and declare it a FE error if type
1611 adjustment would be needed. */
1612 if (TREE_TYPE (expr) != type)
1614 #ifdef ENABLE_TYPES_CHECKING
1615 tree old_type = TREE_TYPE (expr);
1616 #endif
1617 int type_quals;
1619 /* We need to preserve qualifiers and propagate them from
1620 operand 0. */
1621 type_quals = TYPE_QUALS (type)
1622 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1623 if (TYPE_QUALS (type) != type_quals)
1624 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1626 /* Set the type of the COMPONENT_REF to the underlying type. */
1627 TREE_TYPE (expr) = type;
1629 #ifdef ENABLE_TYPES_CHECKING
1630 /* It is now a FE error, if the conversion from the canonical
1631 type to the original expression type is not useless. */
1632 gcc_assert (useless_type_conversion_p (old_type, type));
1633 #endif
1637 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1638 to foo, embed that change in the ADDR_EXPR by converting
1639 T array[U];
1640 (T *)&array
1642 &array[L]
1643 where L is the lower bound. For simplicity, only do this for constant
1644 lower bound.
1645 The constraint is that the type of &array[L] is trivially convertible
1646 to T *. */
1648 static void
1649 canonicalize_addr_expr (tree *expr_p)
1651 tree expr = *expr_p;
1652 tree addr_expr = TREE_OPERAND (expr, 0);
1653 tree datype, ddatype, pddatype;
1655 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1656 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1657 || TREE_CODE (addr_expr) != ADDR_EXPR)
1658 return;
1660 /* The addr_expr type should be a pointer to an array. */
1661 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1662 if (TREE_CODE (datype) != ARRAY_TYPE)
1663 return;
1665 /* The pointer to element type shall be trivially convertible to
1666 the expression pointer type. */
1667 ddatype = TREE_TYPE (datype);
1668 pddatype = build_pointer_type (ddatype);
1669 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1670 pddatype))
1671 return;
1673 /* The lower bound and element sizes must be constant. */
1674 if (!TYPE_SIZE_UNIT (ddatype)
1675 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1676 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1677 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1678 return;
1680 /* All checks succeeded. Build a new node to merge the cast. */
1681 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1682 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1683 NULL_TREE, NULL_TREE);
1684 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1686 /* We can have stripped a required restrict qualifier above. */
1687 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1688 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1691 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1692 underneath as appropriate. */
1694 static enum gimplify_status
1695 gimplify_conversion (tree *expr_p)
1697 location_t loc = EXPR_LOCATION (*expr_p);
1698 gcc_assert (CONVERT_EXPR_P (*expr_p));
1700 /* Then strip away all but the outermost conversion. */
1701 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1703 /* And remove the outermost conversion if it's useless. */
1704 if (tree_ssa_useless_type_conversion (*expr_p))
1705 *expr_p = TREE_OPERAND (*expr_p, 0);
1707 /* If we still have a conversion at the toplevel,
1708 then canonicalize some constructs. */
1709 if (CONVERT_EXPR_P (*expr_p))
1711 tree sub = TREE_OPERAND (*expr_p, 0);
1713 /* If a NOP conversion is changing the type of a COMPONENT_REF
1714 expression, then canonicalize its type now in order to expose more
1715 redundant conversions. */
1716 if (TREE_CODE (sub) == COMPONENT_REF)
1717 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1719 /* If a NOP conversion is changing a pointer to array of foo
1720 to a pointer to foo, embed that change in the ADDR_EXPR. */
1721 else if (TREE_CODE (sub) == ADDR_EXPR)
1722 canonicalize_addr_expr (expr_p);
1725 /* If we have a conversion to a non-register type force the
1726 use of a VIEW_CONVERT_EXPR instead. */
1727 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1728 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1729 TREE_OPERAND (*expr_p, 0));
1731 return GS_OK;
1734 /* Nonlocal VLAs seen in the current function. */
1735 static hash_set<tree> *nonlocal_vlas;
1737 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1738 static tree nonlocal_vla_vars;
1740 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1741 DECL_VALUE_EXPR, and it's worth re-examining things. */
1743 static enum gimplify_status
1744 gimplify_var_or_parm_decl (tree *expr_p)
1746 tree decl = *expr_p;
1748 /* ??? If this is a local variable, and it has not been seen in any
1749 outer BIND_EXPR, then it's probably the result of a duplicate
1750 declaration, for which we've already issued an error. It would
1751 be really nice if the front end wouldn't leak these at all.
1752 Currently the only known culprit is C++ destructors, as seen
1753 in g++.old-deja/g++.jason/binding.C. */
1754 if (TREE_CODE (decl) == VAR_DECL
1755 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1756 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1757 && decl_function_context (decl) == current_function_decl)
1759 gcc_assert (seen_error ());
1760 return GS_ERROR;
1763 /* When within an OpenMP context, notice uses of variables. */
1764 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1765 return GS_ALL_DONE;
1767 /* If the decl is an alias for another expression, substitute it now. */
1768 if (DECL_HAS_VALUE_EXPR_P (decl))
1770 tree value_expr = DECL_VALUE_EXPR (decl);
1772 /* For referenced nonlocal VLAs add a decl for debugging purposes
1773 to the current function. */
1774 if (TREE_CODE (decl) == VAR_DECL
1775 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1776 && nonlocal_vlas != NULL
1777 && TREE_CODE (value_expr) == INDIRECT_REF
1778 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1779 && decl_function_context (decl) != current_function_decl)
1781 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1782 while (ctx
1783 && (ctx->region_type == ORT_WORKSHARE
1784 || ctx->region_type == ORT_SIMD))
1785 ctx = ctx->outer_context;
1786 if (!ctx && !nonlocal_vlas->add (decl))
1788 tree copy = copy_node (decl);
1790 lang_hooks.dup_lang_specific_decl (copy);
1791 SET_DECL_RTL (copy, 0);
1792 TREE_USED (copy) = 1;
1793 DECL_CHAIN (copy) = nonlocal_vla_vars;
1794 nonlocal_vla_vars = copy;
1795 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1796 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1800 *expr_p = unshare_expr (value_expr);
1801 return GS_OK;
1804 return GS_ALL_DONE;
1807 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1809 static void
1810 recalculate_side_effects (tree t)
1812 enum tree_code code = TREE_CODE (t);
1813 int len = TREE_OPERAND_LENGTH (t);
1814 int i;
1816 switch (TREE_CODE_CLASS (code))
1818 case tcc_expression:
1819 switch (code)
1821 case INIT_EXPR:
1822 case MODIFY_EXPR:
1823 case VA_ARG_EXPR:
1824 case PREDECREMENT_EXPR:
1825 case PREINCREMENT_EXPR:
1826 case POSTDECREMENT_EXPR:
1827 case POSTINCREMENT_EXPR:
1828 /* All of these have side-effects, no matter what their
1829 operands are. */
1830 return;
1832 default:
1833 break;
1835 /* Fall through. */
1837 case tcc_comparison: /* a comparison expression */
1838 case tcc_unary: /* a unary arithmetic expression */
1839 case tcc_binary: /* a binary arithmetic expression */
1840 case tcc_reference: /* a reference */
1841 case tcc_vl_exp: /* a function call */
1842 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1843 for (i = 0; i < len; ++i)
1845 tree op = TREE_OPERAND (t, i);
1846 if (op && TREE_SIDE_EFFECTS (op))
1847 TREE_SIDE_EFFECTS (t) = 1;
1849 break;
1851 case tcc_constant:
1852 /* No side-effects. */
1853 return;
1855 default:
1856 gcc_unreachable ();
1860 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1861 node *EXPR_P.
1863 compound_lval
1864 : min_lval '[' val ']'
1865 | min_lval '.' ID
1866 | compound_lval '[' val ']'
1867 | compound_lval '.' ID
1869 This is not part of the original SIMPLE definition, which separates
1870 array and member references, but it seems reasonable to handle them
1871 together. Also, this way we don't run into problems with union
1872 aliasing; gcc requires that for accesses through a union to alias, the
1873 union reference must be explicit, which was not always the case when we
1874 were splitting up array and member refs.
1876 PRE_P points to the sequence where side effects that must happen before
1877 *EXPR_P should be stored.
1879 POST_P points to the sequence where side effects that must happen after
1880 *EXPR_P should be stored. */
1882 static enum gimplify_status
1883 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1884 fallback_t fallback)
1886 tree *p;
1887 enum gimplify_status ret = GS_ALL_DONE, tret;
1888 int i;
1889 location_t loc = EXPR_LOCATION (*expr_p);
1890 tree expr = *expr_p;
1892 /* Create a stack of the subexpressions so later we can walk them in
1893 order from inner to outer. */
1894 auto_vec<tree, 10> expr_stack;
1896 /* We can handle anything that get_inner_reference can deal with. */
1897 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1899 restart:
1900 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1901 if (TREE_CODE (*p) == INDIRECT_REF)
1902 *p = fold_indirect_ref_loc (loc, *p);
1904 if (handled_component_p (*p))
1906 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1907 additional COMPONENT_REFs. */
1908 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1909 && gimplify_var_or_parm_decl (p) == GS_OK)
1910 goto restart;
1911 else
1912 break;
1914 expr_stack.safe_push (*p);
1917 gcc_assert (expr_stack.length ());
1919 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1920 walked through and P points to the innermost expression.
1922 Java requires that we elaborated nodes in source order. That
1923 means we must gimplify the inner expression followed by each of
1924 the indices, in order. But we can't gimplify the inner
1925 expression until we deal with any variable bounds, sizes, or
1926 positions in order to deal with PLACEHOLDER_EXPRs.
1928 So we do this in three steps. First we deal with the annotations
1929 for any variables in the components, then we gimplify the base,
1930 then we gimplify any indices, from left to right. */
1931 for (i = expr_stack.length () - 1; i >= 0; i--)
1933 tree t = expr_stack[i];
1935 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1937 /* Gimplify the low bound and element type size and put them into
1938 the ARRAY_REF. If these values are set, they have already been
1939 gimplified. */
1940 if (TREE_OPERAND (t, 2) == NULL_TREE)
1942 tree low = unshare_expr (array_ref_low_bound (t));
1943 if (!is_gimple_min_invariant (low))
1945 TREE_OPERAND (t, 2) = low;
1946 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1947 post_p, is_gimple_reg,
1948 fb_rvalue);
1949 ret = MIN (ret, tret);
1952 else
1954 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1955 is_gimple_reg, fb_rvalue);
1956 ret = MIN (ret, tret);
1959 if (TREE_OPERAND (t, 3) == NULL_TREE)
1961 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1962 tree elmt_size = unshare_expr (array_ref_element_size (t));
1963 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1965 /* Divide the element size by the alignment of the element
1966 type (above). */
1967 elmt_size
1968 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1970 if (!is_gimple_min_invariant (elmt_size))
1972 TREE_OPERAND (t, 3) = elmt_size;
1973 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1974 post_p, is_gimple_reg,
1975 fb_rvalue);
1976 ret = MIN (ret, tret);
1979 else
1981 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1982 is_gimple_reg, fb_rvalue);
1983 ret = MIN (ret, tret);
1986 else if (TREE_CODE (t) == COMPONENT_REF)
1988 /* Set the field offset into T and gimplify it. */
1989 if (TREE_OPERAND (t, 2) == NULL_TREE)
1991 tree offset = unshare_expr (component_ref_field_offset (t));
1992 tree field = TREE_OPERAND (t, 1);
1993 tree factor
1994 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1996 /* Divide the offset by its alignment. */
1997 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1999 if (!is_gimple_min_invariant (offset))
2001 TREE_OPERAND (t, 2) = offset;
2002 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2003 post_p, is_gimple_reg,
2004 fb_rvalue);
2005 ret = MIN (ret, tret);
2008 else
2010 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2011 is_gimple_reg, fb_rvalue);
2012 ret = MIN (ret, tret);
2017 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2018 so as to match the min_lval predicate. Failure to do so may result
2019 in the creation of large aggregate temporaries. */
2020 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2021 fallback | fb_lvalue);
2022 ret = MIN (ret, tret);
2024 /* And finally, the indices and operands of ARRAY_REF. During this
2025 loop we also remove any useless conversions. */
2026 for (; expr_stack.length () > 0; )
2028 tree t = expr_stack.pop ();
2030 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2032 /* Gimplify the dimension. */
2033 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2035 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2036 is_gimple_val, fb_rvalue);
2037 ret = MIN (ret, tret);
2041 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2043 /* The innermost expression P may have originally had
2044 TREE_SIDE_EFFECTS set which would have caused all the outer
2045 expressions in *EXPR_P leading to P to also have had
2046 TREE_SIDE_EFFECTS set. */
2047 recalculate_side_effects (t);
2050 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2051 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2053 canonicalize_component_ref (expr_p);
2056 expr_stack.release ();
2058 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2060 return ret;
2063 /* Gimplify the self modifying expression pointed to by EXPR_P
2064 (++, --, +=, -=).
2066 PRE_P points to the list where side effects that must happen before
2067 *EXPR_P should be stored.
2069 POST_P points to the list where side effects that must happen after
2070 *EXPR_P should be stored.
2072 WANT_VALUE is nonzero iff we want to use the value of this expression
2073 in another expression.
2075 ARITH_TYPE is the type the computation should be performed in. */
2077 enum gimplify_status
2078 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2079 bool want_value, tree arith_type)
2081 enum tree_code code;
2082 tree lhs, lvalue, rhs, t1;
2083 gimple_seq post = NULL, *orig_post_p = post_p;
2084 bool postfix;
2085 enum tree_code arith_code;
2086 enum gimplify_status ret;
2087 location_t loc = EXPR_LOCATION (*expr_p);
2089 code = TREE_CODE (*expr_p);
2091 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2092 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2094 /* Prefix or postfix? */
2095 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2096 /* Faster to treat as prefix if result is not used. */
2097 postfix = want_value;
2098 else
2099 postfix = false;
2101 /* For postfix, make sure the inner expression's post side effects
2102 are executed after side effects from this expression. */
2103 if (postfix)
2104 post_p = &post;
2106 /* Add or subtract? */
2107 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2108 arith_code = PLUS_EXPR;
2109 else
2110 arith_code = MINUS_EXPR;
2112 /* Gimplify the LHS into a GIMPLE lvalue. */
2113 lvalue = TREE_OPERAND (*expr_p, 0);
2114 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2115 if (ret == GS_ERROR)
2116 return ret;
2118 /* Extract the operands to the arithmetic operation. */
2119 lhs = lvalue;
2120 rhs = TREE_OPERAND (*expr_p, 1);
2122 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2123 that as the result value and in the postqueue operation. */
2124 if (postfix)
2126 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2127 if (ret == GS_ERROR)
2128 return ret;
2130 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2133 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2134 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2136 rhs = convert_to_ptrofftype_loc (loc, rhs);
2137 if (arith_code == MINUS_EXPR)
2138 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2139 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2141 else
2142 t1 = fold_convert (TREE_TYPE (*expr_p),
2143 fold_build2 (arith_code, arith_type,
2144 fold_convert (arith_type, lhs),
2145 fold_convert (arith_type, rhs)));
2147 if (postfix)
2149 gimplify_assign (lvalue, t1, pre_p);
2150 gimplify_seq_add_seq (orig_post_p, post);
2151 *expr_p = lhs;
2152 return GS_ALL_DONE;
2154 else
2156 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2157 return GS_OK;
2161 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2163 static void
2164 maybe_with_size_expr (tree *expr_p)
2166 tree expr = *expr_p;
2167 tree type = TREE_TYPE (expr);
2168 tree size;
2170 /* If we've already wrapped this or the type is error_mark_node, we can't do
2171 anything. */
2172 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2173 || type == error_mark_node)
2174 return;
2176 /* If the size isn't known or is a constant, we have nothing to do. */
2177 size = TYPE_SIZE_UNIT (type);
2178 if (!size || TREE_CODE (size) == INTEGER_CST)
2179 return;
2181 /* Otherwise, make a WITH_SIZE_EXPR. */
2182 size = unshare_expr (size);
2183 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2184 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2187 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2188 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2189 the CALL_EXPR. */
2191 enum gimplify_status
2192 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2194 bool (*test) (tree);
2195 fallback_t fb;
2197 /* In general, we allow lvalues for function arguments to avoid
2198 extra overhead of copying large aggregates out of even larger
2199 aggregates into temporaries only to copy the temporaries to
2200 the argument list. Make optimizers happy by pulling out to
2201 temporaries those types that fit in registers. */
2202 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2203 test = is_gimple_val, fb = fb_rvalue;
2204 else
2206 test = is_gimple_lvalue, fb = fb_either;
2207 /* Also strip a TARGET_EXPR that would force an extra copy. */
2208 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2210 tree init = TARGET_EXPR_INITIAL (*arg_p);
2211 if (init
2212 && !VOID_TYPE_P (TREE_TYPE (init)))
2213 *arg_p = init;
2217 /* If this is a variable sized type, we must remember the size. */
2218 maybe_with_size_expr (arg_p);
2220 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2221 /* Make sure arguments have the same location as the function call
2222 itself. */
2223 protected_set_expr_location (*arg_p, call_location);
2225 /* There is a sequence point before a function call. Side effects in
2226 the argument list must occur before the actual call. So, when
2227 gimplifying arguments, force gimplify_expr to use an internal
2228 post queue which is then appended to the end of PRE_P. */
2229 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2232 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2233 references that weren't in the source. We'll do it during omplower pass
2234 instead. */
2236 static bool
2237 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2239 struct gimplify_omp_ctx *ctx;
2240 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2241 if (ctx->region_type == ORT_TARGET)
2242 return false;
2243 return fold_stmt (gsi);
2246 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2247 WANT_VALUE is true if the result of the call is desired. */
2249 static enum gimplify_status
2250 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2252 tree fndecl, parms, p, fnptrtype;
2253 enum gimplify_status ret;
2254 int i, nargs;
2255 gimple call;
2256 bool builtin_va_start_p = false;
2257 location_t loc = EXPR_LOCATION (*expr_p);
2259 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2261 /* For reliable diagnostics during inlining, it is necessary that
2262 every call_expr be annotated with file and line. */
2263 if (! EXPR_HAS_LOCATION (*expr_p))
2264 SET_EXPR_LOCATION (*expr_p, input_location);
2266 /* Gimplify internal functions created in the FEs. */
2267 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2269 nargs = call_expr_nargs (*expr_p);
2270 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2271 auto_vec<tree> vargs (nargs);
2273 for (i = 0; i < nargs; i++)
2275 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2276 EXPR_LOCATION (*expr_p));
2277 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2279 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2280 gimplify_seq_add_stmt (pre_p, call);
2281 return GS_ALL_DONE;
2284 /* This may be a call to a builtin function.
2286 Builtin function calls may be transformed into different
2287 (and more efficient) builtin function calls under certain
2288 circumstances. Unfortunately, gimplification can muck things
2289 up enough that the builtin expanders are not aware that certain
2290 transformations are still valid.
2292 So we attempt transformation/gimplification of the call before
2293 we gimplify the CALL_EXPR. At this time we do not manage to
2294 transform all calls in the same manner as the expanders do, but
2295 we do transform most of them. */
2296 fndecl = get_callee_fndecl (*expr_p);
2297 if (fndecl
2298 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2299 switch (DECL_FUNCTION_CODE (fndecl))
2301 case BUILT_IN_VA_START:
2303 builtin_va_start_p = TRUE;
2304 if (call_expr_nargs (*expr_p) < 2)
2306 error ("too few arguments to function %<va_start%>");
2307 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2308 return GS_OK;
2311 if (fold_builtin_next_arg (*expr_p, true))
2313 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2314 return GS_OK;
2316 break;
2318 case BUILT_IN_LINE:
2320 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2321 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2322 return GS_OK;
2324 case BUILT_IN_FILE:
2326 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2327 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2328 return GS_OK;
2330 case BUILT_IN_FUNCTION:
2332 const char *function;
2333 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2334 *expr_p = build_string_literal (strlen (function) + 1, function);
2335 return GS_OK;
2337 default:
2340 if (fndecl && DECL_BUILT_IN (fndecl))
2342 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2343 if (new_tree && new_tree != *expr_p)
2345 /* There was a transformation of this call which computes the
2346 same value, but in a more efficient way. Return and try
2347 again. */
2348 *expr_p = new_tree;
2349 return GS_OK;
2353 /* Remember the original function pointer type. */
2354 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2356 /* There is a sequence point before the call, so any side effects in
2357 the calling expression must occur before the actual call. Force
2358 gimplify_expr to use an internal post queue. */
2359 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2360 is_gimple_call_addr, fb_rvalue);
2362 nargs = call_expr_nargs (*expr_p);
2364 /* Get argument types for verification. */
2365 fndecl = get_callee_fndecl (*expr_p);
2366 parms = NULL_TREE;
2367 if (fndecl)
2368 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2369 else
2370 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2372 if (fndecl && DECL_ARGUMENTS (fndecl))
2373 p = DECL_ARGUMENTS (fndecl);
2374 else if (parms)
2375 p = parms;
2376 else
2377 p = NULL_TREE;
2378 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2381 /* If the last argument is __builtin_va_arg_pack () and it is not
2382 passed as a named argument, decrease the number of CALL_EXPR
2383 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2384 if (!p
2385 && i < nargs
2386 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2388 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2389 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2391 if (last_arg_fndecl
2392 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2393 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2394 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2396 tree call = *expr_p;
2398 --nargs;
2399 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2400 CALL_EXPR_FN (call),
2401 nargs, CALL_EXPR_ARGP (call));
2403 /* Copy all CALL_EXPR flags, location and block, except
2404 CALL_EXPR_VA_ARG_PACK flag. */
2405 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2406 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2407 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2408 = CALL_EXPR_RETURN_SLOT_OPT (call);
2409 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2410 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2412 /* Set CALL_EXPR_VA_ARG_PACK. */
2413 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2417 /* Finally, gimplify the function arguments. */
2418 if (nargs > 0)
2420 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2421 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2422 PUSH_ARGS_REVERSED ? i-- : i++)
2424 enum gimplify_status t;
2426 /* Avoid gimplifying the second argument to va_start, which needs to
2427 be the plain PARM_DECL. */
2428 if ((i != 1) || !builtin_va_start_p)
2430 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2431 EXPR_LOCATION (*expr_p));
2433 if (t == GS_ERROR)
2434 ret = GS_ERROR;
2439 /* Verify the function result. */
2440 if (want_value && fndecl
2441 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2443 error_at (loc, "using result of function returning %<void%>");
2444 ret = GS_ERROR;
2447 /* Try this again in case gimplification exposed something. */
2448 if (ret != GS_ERROR)
2450 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2452 if (new_tree && new_tree != *expr_p)
2454 /* There was a transformation of this call which computes the
2455 same value, but in a more efficient way. Return and try
2456 again. */
2457 *expr_p = new_tree;
2458 return GS_OK;
2461 else
2463 *expr_p = error_mark_node;
2464 return GS_ERROR;
2467 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2468 decl. This allows us to eliminate redundant or useless
2469 calls to "const" functions. */
2470 if (TREE_CODE (*expr_p) == CALL_EXPR)
2472 int flags = call_expr_flags (*expr_p);
2473 if (flags & (ECF_CONST | ECF_PURE)
2474 /* An infinite loop is considered a side effect. */
2475 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2476 TREE_SIDE_EFFECTS (*expr_p) = 0;
2479 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2480 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2481 form and delegate the creation of a GIMPLE_CALL to
2482 gimplify_modify_expr. This is always possible because when
2483 WANT_VALUE is true, the caller wants the result of this call into
2484 a temporary, which means that we will emit an INIT_EXPR in
2485 internal_get_tmp_var which will then be handled by
2486 gimplify_modify_expr. */
2487 if (!want_value)
2489 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2490 have to do is replicate it as a GIMPLE_CALL tuple. */
2491 gimple_stmt_iterator gsi;
2492 call = gimple_build_call_from_tree (*expr_p);
2493 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2494 notice_special_calls (call);
2495 gimplify_seq_add_stmt (pre_p, call);
2496 gsi = gsi_last (*pre_p);
2497 maybe_fold_stmt (&gsi);
2498 *expr_p = NULL_TREE;
2500 else
2501 /* Remember the original function type. */
2502 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2503 CALL_EXPR_FN (*expr_p));
2505 return ret;
2508 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2509 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2511 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2512 condition is true or false, respectively. If null, we should generate
2513 our own to skip over the evaluation of this specific expression.
2515 LOCUS is the source location of the COND_EXPR.
2517 This function is the tree equivalent of do_jump.
2519 shortcut_cond_r should only be called by shortcut_cond_expr. */
2521 static tree
2522 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2523 location_t locus)
2525 tree local_label = NULL_TREE;
2526 tree t, expr = NULL;
2528 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2529 retain the shortcut semantics. Just insert the gotos here;
2530 shortcut_cond_expr will append the real blocks later. */
2531 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2533 location_t new_locus;
2535 /* Turn if (a && b) into
2537 if (a); else goto no;
2538 if (b) goto yes; else goto no;
2539 (no:) */
2541 if (false_label_p == NULL)
2542 false_label_p = &local_label;
2544 /* Keep the original source location on the first 'if'. */
2545 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2546 append_to_statement_list (t, &expr);
2548 /* Set the source location of the && on the second 'if'. */
2549 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2550 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2551 new_locus);
2552 append_to_statement_list (t, &expr);
2554 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2556 location_t new_locus;
2558 /* Turn if (a || b) into
2560 if (a) goto yes;
2561 if (b) goto yes; else goto no;
2562 (yes:) */
2564 if (true_label_p == NULL)
2565 true_label_p = &local_label;
2567 /* Keep the original source location on the first 'if'. */
2568 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2569 append_to_statement_list (t, &expr);
2571 /* Set the source location of the || on the second 'if'. */
2572 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2573 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2574 new_locus);
2575 append_to_statement_list (t, &expr);
2577 else if (TREE_CODE (pred) == COND_EXPR
2578 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2579 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2581 location_t new_locus;
2583 /* As long as we're messing with gotos, turn if (a ? b : c) into
2584 if (a)
2585 if (b) goto yes; else goto no;
2586 else
2587 if (c) goto yes; else goto no;
2589 Don't do this if one of the arms has void type, which can happen
2590 in C++ when the arm is throw. */
2592 /* Keep the original source location on the first 'if'. Set the source
2593 location of the ? on the second 'if'. */
2594 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2595 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2596 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2597 false_label_p, locus),
2598 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2599 false_label_p, new_locus));
2601 else
2603 expr = build3 (COND_EXPR, void_type_node, pred,
2604 build_and_jump (true_label_p),
2605 build_and_jump (false_label_p));
2606 SET_EXPR_LOCATION (expr, locus);
2609 if (local_label)
2611 t = build1 (LABEL_EXPR, void_type_node, local_label);
2612 append_to_statement_list (t, &expr);
2615 return expr;
2618 /* Given a conditional expression EXPR with short-circuit boolean
2619 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2620 predicate apart into the equivalent sequence of conditionals. */
2622 static tree
2623 shortcut_cond_expr (tree expr)
2625 tree pred = TREE_OPERAND (expr, 0);
2626 tree then_ = TREE_OPERAND (expr, 1);
2627 tree else_ = TREE_OPERAND (expr, 2);
2628 tree true_label, false_label, end_label, t;
2629 tree *true_label_p;
2630 tree *false_label_p;
2631 bool emit_end, emit_false, jump_over_else;
2632 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2633 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2635 /* First do simple transformations. */
2636 if (!else_se)
2638 /* If there is no 'else', turn
2639 if (a && b) then c
2640 into
2641 if (a) if (b) then c. */
2642 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2644 /* Keep the original source location on the first 'if'. */
2645 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2646 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2647 /* Set the source location of the && on the second 'if'. */
2648 if (EXPR_HAS_LOCATION (pred))
2649 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2650 then_ = shortcut_cond_expr (expr);
2651 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2652 pred = TREE_OPERAND (pred, 0);
2653 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2654 SET_EXPR_LOCATION (expr, locus);
2658 if (!then_se)
2660 /* If there is no 'then', turn
2661 if (a || b); else d
2662 into
2663 if (a); else if (b); else d. */
2664 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2666 /* Keep the original source location on the first 'if'. */
2667 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2668 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2669 /* Set the source location of the || on the second 'if'. */
2670 if (EXPR_HAS_LOCATION (pred))
2671 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2672 else_ = shortcut_cond_expr (expr);
2673 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2674 pred = TREE_OPERAND (pred, 0);
2675 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2676 SET_EXPR_LOCATION (expr, locus);
2680 /* If we're done, great. */
2681 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2682 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2683 return expr;
2685 /* Otherwise we need to mess with gotos. Change
2686 if (a) c; else d;
2688 if (a); else goto no;
2689 c; goto end;
2690 no: d; end:
2691 and recursively gimplify the condition. */
2693 true_label = false_label = end_label = NULL_TREE;
2695 /* If our arms just jump somewhere, hijack those labels so we don't
2696 generate jumps to jumps. */
2698 if (then_
2699 && TREE_CODE (then_) == GOTO_EXPR
2700 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2702 true_label = GOTO_DESTINATION (then_);
2703 then_ = NULL;
2704 then_se = false;
2707 if (else_
2708 && TREE_CODE (else_) == GOTO_EXPR
2709 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2711 false_label = GOTO_DESTINATION (else_);
2712 else_ = NULL;
2713 else_se = false;
2716 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2717 if (true_label)
2718 true_label_p = &true_label;
2719 else
2720 true_label_p = NULL;
2722 /* The 'else' branch also needs a label if it contains interesting code. */
2723 if (false_label || else_se)
2724 false_label_p = &false_label;
2725 else
2726 false_label_p = NULL;
2728 /* If there was nothing else in our arms, just forward the label(s). */
2729 if (!then_se && !else_se)
2730 return shortcut_cond_r (pred, true_label_p, false_label_p,
2731 EXPR_LOC_OR_LOC (expr, input_location));
2733 /* If our last subexpression already has a terminal label, reuse it. */
2734 if (else_se)
2735 t = expr_last (else_);
2736 else if (then_se)
2737 t = expr_last (then_);
2738 else
2739 t = NULL;
2740 if (t && TREE_CODE (t) == LABEL_EXPR)
2741 end_label = LABEL_EXPR_LABEL (t);
2743 /* If we don't care about jumping to the 'else' branch, jump to the end
2744 if the condition is false. */
2745 if (!false_label_p)
2746 false_label_p = &end_label;
2748 /* We only want to emit these labels if we aren't hijacking them. */
2749 emit_end = (end_label == NULL_TREE);
2750 emit_false = (false_label == NULL_TREE);
2752 /* We only emit the jump over the else clause if we have to--if the
2753 then clause may fall through. Otherwise we can wind up with a
2754 useless jump and a useless label at the end of gimplified code,
2755 which will cause us to think that this conditional as a whole
2756 falls through even if it doesn't. If we then inline a function
2757 which ends with such a condition, that can cause us to issue an
2758 inappropriate warning about control reaching the end of a
2759 non-void function. */
2760 jump_over_else = block_may_fallthru (then_);
2762 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2763 EXPR_LOC_OR_LOC (expr, input_location));
2765 expr = NULL;
2766 append_to_statement_list (pred, &expr);
2768 append_to_statement_list (then_, &expr);
2769 if (else_se)
2771 if (jump_over_else)
2773 tree last = expr_last (expr);
2774 t = build_and_jump (&end_label);
2775 if (EXPR_HAS_LOCATION (last))
2776 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2777 append_to_statement_list (t, &expr);
2779 if (emit_false)
2781 t = build1 (LABEL_EXPR, void_type_node, false_label);
2782 append_to_statement_list (t, &expr);
2784 append_to_statement_list (else_, &expr);
2786 if (emit_end && end_label)
2788 t = build1 (LABEL_EXPR, void_type_node, end_label);
2789 append_to_statement_list (t, &expr);
2792 return expr;
2795 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2797 tree
2798 gimple_boolify (tree expr)
2800 tree type = TREE_TYPE (expr);
2801 location_t loc = EXPR_LOCATION (expr);
2803 if (TREE_CODE (expr) == NE_EXPR
2804 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2805 && integer_zerop (TREE_OPERAND (expr, 1)))
2807 tree call = TREE_OPERAND (expr, 0);
2808 tree fn = get_callee_fndecl (call);
2810 /* For __builtin_expect ((long) (x), y) recurse into x as well
2811 if x is truth_value_p. */
2812 if (fn
2813 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2814 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2815 && call_expr_nargs (call) == 2)
2817 tree arg = CALL_EXPR_ARG (call, 0);
2818 if (arg)
2820 if (TREE_CODE (arg) == NOP_EXPR
2821 && TREE_TYPE (arg) == TREE_TYPE (call))
2822 arg = TREE_OPERAND (arg, 0);
2823 if (truth_value_p (TREE_CODE (arg)))
2825 arg = gimple_boolify (arg);
2826 CALL_EXPR_ARG (call, 0)
2827 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2833 switch (TREE_CODE (expr))
2835 case TRUTH_AND_EXPR:
2836 case TRUTH_OR_EXPR:
2837 case TRUTH_XOR_EXPR:
2838 case TRUTH_ANDIF_EXPR:
2839 case TRUTH_ORIF_EXPR:
2840 /* Also boolify the arguments of truth exprs. */
2841 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2842 /* FALLTHRU */
2844 case TRUTH_NOT_EXPR:
2845 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2847 /* These expressions always produce boolean results. */
2848 if (TREE_CODE (type) != BOOLEAN_TYPE)
2849 TREE_TYPE (expr) = boolean_type_node;
2850 return expr;
2852 case ANNOTATE_EXPR:
2853 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2855 case annot_expr_ivdep_kind:
2856 case annot_expr_no_vector_kind:
2857 case annot_expr_vector_kind:
2858 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2859 if (TREE_CODE (type) != BOOLEAN_TYPE)
2860 TREE_TYPE (expr) = boolean_type_node;
2861 return expr;
2862 default:
2863 gcc_unreachable ();
2866 default:
2867 if (COMPARISON_CLASS_P (expr))
2869 /* There expressions always prduce boolean results. */
2870 if (TREE_CODE (type) != BOOLEAN_TYPE)
2871 TREE_TYPE (expr) = boolean_type_node;
2872 return expr;
2874 /* Other expressions that get here must have boolean values, but
2875 might need to be converted to the appropriate mode. */
2876 if (TREE_CODE (type) == BOOLEAN_TYPE)
2877 return expr;
2878 return fold_convert_loc (loc, boolean_type_node, expr);
2882 /* Given a conditional expression *EXPR_P without side effects, gimplify
2883 its operands. New statements are inserted to PRE_P. */
2885 static enum gimplify_status
2886 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2888 tree expr = *expr_p, cond;
2889 enum gimplify_status ret, tret;
2890 enum tree_code code;
2892 cond = gimple_boolify (COND_EXPR_COND (expr));
2894 /* We need to handle && and || specially, as their gimplification
2895 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2896 code = TREE_CODE (cond);
2897 if (code == TRUTH_ANDIF_EXPR)
2898 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2899 else if (code == TRUTH_ORIF_EXPR)
2900 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2901 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2902 COND_EXPR_COND (*expr_p) = cond;
2904 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2905 is_gimple_val, fb_rvalue);
2906 ret = MIN (ret, tret);
2907 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2908 is_gimple_val, fb_rvalue);
2910 return MIN (ret, tret);
2913 /* Return true if evaluating EXPR could trap.
2914 EXPR is GENERIC, while tree_could_trap_p can be called
2915 only on GIMPLE. */
2917 static bool
2918 generic_expr_could_trap_p (tree expr)
2920 unsigned i, n;
2922 if (!expr || is_gimple_val (expr))
2923 return false;
2925 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2926 return true;
2928 n = TREE_OPERAND_LENGTH (expr);
2929 for (i = 0; i < n; i++)
2930 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2931 return true;
2933 return false;
2936 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2937 into
2939 if (p) if (p)
2940 t1 = a; a;
2941 else or else
2942 t1 = b; b;
2945 The second form is used when *EXPR_P is of type void.
2947 PRE_P points to the list where side effects that must happen before
2948 *EXPR_P should be stored. */
2950 static enum gimplify_status
2951 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2953 tree expr = *expr_p;
2954 tree type = TREE_TYPE (expr);
2955 location_t loc = EXPR_LOCATION (expr);
2956 tree tmp, arm1, arm2;
2957 enum gimplify_status ret;
2958 tree label_true, label_false, label_cont;
2959 bool have_then_clause_p, have_else_clause_p;
2960 gimple gimple_cond;
2961 enum tree_code pred_code;
2962 gimple_seq seq = NULL;
2964 /* If this COND_EXPR has a value, copy the values into a temporary within
2965 the arms. */
2966 if (!VOID_TYPE_P (type))
2968 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2969 tree result;
2971 /* If either an rvalue is ok or we do not require an lvalue, create the
2972 temporary. But we cannot do that if the type is addressable. */
2973 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2974 && !TREE_ADDRESSABLE (type))
2976 if (gimplify_ctxp->allow_rhs_cond_expr
2977 /* If either branch has side effects or could trap, it can't be
2978 evaluated unconditionally. */
2979 && !TREE_SIDE_EFFECTS (then_)
2980 && !generic_expr_could_trap_p (then_)
2981 && !TREE_SIDE_EFFECTS (else_)
2982 && !generic_expr_could_trap_p (else_))
2983 return gimplify_pure_cond_expr (expr_p, pre_p);
2985 tmp = create_tmp_var (type, "iftmp");
2986 result = tmp;
2989 /* Otherwise, only create and copy references to the values. */
2990 else
2992 type = build_pointer_type (type);
2994 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2995 then_ = build_fold_addr_expr_loc (loc, then_);
2997 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2998 else_ = build_fold_addr_expr_loc (loc, else_);
3000 expr
3001 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3003 tmp = create_tmp_var (type, "iftmp");
3004 result = build_simple_mem_ref_loc (loc, tmp);
3007 /* Build the new then clause, `tmp = then_;'. But don't build the
3008 assignment if the value is void; in C++ it can be if it's a throw. */
3009 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3010 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3012 /* Similarly, build the new else clause, `tmp = else_;'. */
3013 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3014 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3016 TREE_TYPE (expr) = void_type_node;
3017 recalculate_side_effects (expr);
3019 /* Move the COND_EXPR to the prequeue. */
3020 gimplify_stmt (&expr, pre_p);
3022 *expr_p = result;
3023 return GS_ALL_DONE;
3026 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3027 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3028 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3029 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3031 /* Make sure the condition has BOOLEAN_TYPE. */
3032 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3034 /* Break apart && and || conditions. */
3035 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3036 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3038 expr = shortcut_cond_expr (expr);
3040 if (expr != *expr_p)
3042 *expr_p = expr;
3044 /* We can't rely on gimplify_expr to re-gimplify the expanded
3045 form properly, as cleanups might cause the target labels to be
3046 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3047 set up a conditional context. */
3048 gimple_push_condition ();
3049 gimplify_stmt (expr_p, &seq);
3050 gimple_pop_condition (pre_p);
3051 gimple_seq_add_seq (pre_p, seq);
3053 return GS_ALL_DONE;
3057 /* Now do the normal gimplification. */
3059 /* Gimplify condition. */
3060 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3061 fb_rvalue);
3062 if (ret == GS_ERROR)
3063 return GS_ERROR;
3064 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3066 gimple_push_condition ();
3068 have_then_clause_p = have_else_clause_p = false;
3069 if (TREE_OPERAND (expr, 1) != NULL
3070 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3071 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3072 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3073 == current_function_decl)
3074 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3075 have different locations, otherwise we end up with incorrect
3076 location information on the branches. */
3077 && (optimize
3078 || !EXPR_HAS_LOCATION (expr)
3079 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3080 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3082 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3083 have_then_clause_p = true;
3085 else
3086 label_true = create_artificial_label (UNKNOWN_LOCATION);
3087 if (TREE_OPERAND (expr, 2) != NULL
3088 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3089 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3090 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3091 == current_function_decl)
3092 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3093 have different locations, otherwise we end up with incorrect
3094 location information on the branches. */
3095 && (optimize
3096 || !EXPR_HAS_LOCATION (expr)
3097 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3098 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3100 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3101 have_else_clause_p = true;
3103 else
3104 label_false = create_artificial_label (UNKNOWN_LOCATION);
3106 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3107 &arm2);
3109 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3110 label_false);
3112 gimplify_seq_add_stmt (&seq, gimple_cond);
3113 label_cont = NULL_TREE;
3114 if (!have_then_clause_p)
3116 /* For if (...) {} else { code; } put label_true after
3117 the else block. */
3118 if (TREE_OPERAND (expr, 1) == NULL_TREE
3119 && !have_else_clause_p
3120 && TREE_OPERAND (expr, 2) != NULL_TREE)
3121 label_cont = label_true;
3122 else
3124 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3125 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3126 /* For if (...) { code; } else {} or
3127 if (...) { code; } else goto label; or
3128 if (...) { code; return; } else { ... }
3129 label_cont isn't needed. */
3130 if (!have_else_clause_p
3131 && TREE_OPERAND (expr, 2) != NULL_TREE
3132 && gimple_seq_may_fallthru (seq))
3134 gimple g;
3135 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3137 g = gimple_build_goto (label_cont);
3139 /* GIMPLE_COND's are very low level; they have embedded
3140 gotos. This particular embedded goto should not be marked
3141 with the location of the original COND_EXPR, as it would
3142 correspond to the COND_EXPR's condition, not the ELSE or the
3143 THEN arms. To avoid marking it with the wrong location, flag
3144 it as "no location". */
3145 gimple_set_do_not_emit_location (g);
3147 gimplify_seq_add_stmt (&seq, g);
3151 if (!have_else_clause_p)
3153 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3154 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3156 if (label_cont)
3157 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3159 gimple_pop_condition (pre_p);
3160 gimple_seq_add_seq (pre_p, seq);
3162 if (ret == GS_ERROR)
3163 ; /* Do nothing. */
3164 else if (have_then_clause_p || have_else_clause_p)
3165 ret = GS_ALL_DONE;
3166 else
3168 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3169 expr = TREE_OPERAND (expr, 0);
3170 gimplify_stmt (&expr, pre_p);
3173 *expr_p = NULL;
3174 return ret;
3177 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3178 to be marked addressable.
3180 We cannot rely on such an expression being directly markable if a temporary
3181 has been created by the gimplification. In this case, we create another
3182 temporary and initialize it with a copy, which will become a store after we
3183 mark it addressable. This can happen if the front-end passed us something
3184 that it could not mark addressable yet, like a Fortran pass-by-reference
3185 parameter (int) floatvar. */
3187 static void
3188 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3190 while (handled_component_p (*expr_p))
3191 expr_p = &TREE_OPERAND (*expr_p, 0);
3192 if (is_gimple_reg (*expr_p))
3194 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3195 DECL_GIMPLE_REG_P (var) = 0;
3196 *expr_p = var;
3200 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3201 a call to __builtin_memcpy. */
3203 static enum gimplify_status
3204 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3205 gimple_seq *seq_p)
3207 tree t, to, to_ptr, from, from_ptr;
3208 gimple gs;
3209 location_t loc = EXPR_LOCATION (*expr_p);
3211 to = TREE_OPERAND (*expr_p, 0);
3212 from = TREE_OPERAND (*expr_p, 1);
3214 /* Mark the RHS addressable. Beware that it may not be possible to do so
3215 directly if a temporary has been created by the gimplification. */
3216 prepare_gimple_addressable (&from, seq_p);
3218 mark_addressable (from);
3219 from_ptr = build_fold_addr_expr_loc (loc, from);
3220 gimplify_arg (&from_ptr, seq_p, loc);
3222 mark_addressable (to);
3223 to_ptr = build_fold_addr_expr_loc (loc, to);
3224 gimplify_arg (&to_ptr, seq_p, loc);
3226 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3228 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3230 if (want_value)
3232 /* tmp = memcpy() */
3233 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3234 gimple_call_set_lhs (gs, t);
3235 gimplify_seq_add_stmt (seq_p, gs);
3237 *expr_p = build_simple_mem_ref (t);
3238 return GS_ALL_DONE;
3241 gimplify_seq_add_stmt (seq_p, gs);
3242 *expr_p = NULL;
3243 return GS_ALL_DONE;
3246 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3247 a call to __builtin_memset. In this case we know that the RHS is
3248 a CONSTRUCTOR with an empty element list. */
3250 static enum gimplify_status
3251 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3252 gimple_seq *seq_p)
3254 tree t, from, to, to_ptr;
3255 gimple gs;
3256 location_t loc = EXPR_LOCATION (*expr_p);
3258 /* Assert our assumptions, to abort instead of producing wrong code
3259 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3260 not be immediately exposed. */
3261 from = TREE_OPERAND (*expr_p, 1);
3262 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3263 from = TREE_OPERAND (from, 0);
3265 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3266 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3268 /* Now proceed. */
3269 to = TREE_OPERAND (*expr_p, 0);
3271 to_ptr = build_fold_addr_expr_loc (loc, to);
3272 gimplify_arg (&to_ptr, seq_p, loc);
3273 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3275 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3277 if (want_value)
3279 /* tmp = memset() */
3280 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3281 gimple_call_set_lhs (gs, t);
3282 gimplify_seq_add_stmt (seq_p, gs);
3284 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3285 return GS_ALL_DONE;
3288 gimplify_seq_add_stmt (seq_p, gs);
3289 *expr_p = NULL;
3290 return GS_ALL_DONE;
3293 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3294 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3295 assignment. Return non-null if we detect a potential overlap. */
3297 struct gimplify_init_ctor_preeval_data
3299 /* The base decl of the lhs object. May be NULL, in which case we
3300 have to assume the lhs is indirect. */
3301 tree lhs_base_decl;
3303 /* The alias set of the lhs object. */
3304 alias_set_type lhs_alias_set;
3307 static tree
3308 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3310 struct gimplify_init_ctor_preeval_data *data
3311 = (struct gimplify_init_ctor_preeval_data *) xdata;
3312 tree t = *tp;
3314 /* If we find the base object, obviously we have overlap. */
3315 if (data->lhs_base_decl == t)
3316 return t;
3318 /* If the constructor component is indirect, determine if we have a
3319 potential overlap with the lhs. The only bits of information we
3320 have to go on at this point are addressability and alias sets. */
3321 if ((INDIRECT_REF_P (t)
3322 || TREE_CODE (t) == MEM_REF)
3323 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3324 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3325 return t;
3327 /* If the constructor component is a call, determine if it can hide a
3328 potential overlap with the lhs through an INDIRECT_REF like above.
3329 ??? Ugh - this is completely broken. In fact this whole analysis
3330 doesn't look conservative. */
3331 if (TREE_CODE (t) == CALL_EXPR)
3333 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3335 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3336 if (POINTER_TYPE_P (TREE_VALUE (type))
3337 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3338 && alias_sets_conflict_p (data->lhs_alias_set,
3339 get_alias_set
3340 (TREE_TYPE (TREE_VALUE (type)))))
3341 return t;
3344 if (IS_TYPE_OR_DECL_P (t))
3345 *walk_subtrees = 0;
3346 return NULL;
3349 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3350 force values that overlap with the lhs (as described by *DATA)
3351 into temporaries. */
3353 static void
3354 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3355 struct gimplify_init_ctor_preeval_data *data)
3357 enum gimplify_status one;
3359 /* If the value is constant, then there's nothing to pre-evaluate. */
3360 if (TREE_CONSTANT (*expr_p))
3362 /* Ensure it does not have side effects, it might contain a reference to
3363 the object we're initializing. */
3364 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3365 return;
3368 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3369 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3370 return;
3372 /* Recurse for nested constructors. */
3373 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3375 unsigned HOST_WIDE_INT ix;
3376 constructor_elt *ce;
3377 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3379 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3380 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3382 return;
3385 /* If this is a variable sized type, we must remember the size. */
3386 maybe_with_size_expr (expr_p);
3388 /* Gimplify the constructor element to something appropriate for the rhs
3389 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3390 the gimplifier will consider this a store to memory. Doing this
3391 gimplification now means that we won't have to deal with complicated
3392 language-specific trees, nor trees like SAVE_EXPR that can induce
3393 exponential search behavior. */
3394 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3395 if (one == GS_ERROR)
3397 *expr_p = NULL;
3398 return;
3401 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3402 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3403 always be true for all scalars, since is_gimple_mem_rhs insists on a
3404 temporary variable for them. */
3405 if (DECL_P (*expr_p))
3406 return;
3408 /* If this is of variable size, we have no choice but to assume it doesn't
3409 overlap since we can't make a temporary for it. */
3410 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3411 return;
3413 /* Otherwise, we must search for overlap ... */
3414 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3415 return;
3417 /* ... and if found, force the value into a temporary. */
3418 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3421 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3422 a RANGE_EXPR in a CONSTRUCTOR for an array.
3424 var = lower;
3425 loop_entry:
3426 object[var] = value;
3427 if (var == upper)
3428 goto loop_exit;
3429 var = var + 1;
3430 goto loop_entry;
3431 loop_exit:
3433 We increment var _after_ the loop exit check because we might otherwise
3434 fail if upper == TYPE_MAX_VALUE (type for upper).
3436 Note that we never have to deal with SAVE_EXPRs here, because this has
3437 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3439 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3440 gimple_seq *, bool);
3442 static void
3443 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3444 tree value, tree array_elt_type,
3445 gimple_seq *pre_p, bool cleared)
3447 tree loop_entry_label, loop_exit_label, fall_thru_label;
3448 tree var, var_type, cref, tmp;
3450 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3451 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3452 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3454 /* Create and initialize the index variable. */
3455 var_type = TREE_TYPE (upper);
3456 var = create_tmp_var (var_type, NULL);
3457 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3459 /* Add the loop entry label. */
3460 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3462 /* Build the reference. */
3463 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3464 var, NULL_TREE, NULL_TREE);
3466 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3467 the store. Otherwise just assign value to the reference. */
3469 if (TREE_CODE (value) == CONSTRUCTOR)
3470 /* NB we might have to call ourself recursively through
3471 gimplify_init_ctor_eval if the value is a constructor. */
3472 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3473 pre_p, cleared);
3474 else
3475 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3477 /* We exit the loop when the index var is equal to the upper bound. */
3478 gimplify_seq_add_stmt (pre_p,
3479 gimple_build_cond (EQ_EXPR, var, upper,
3480 loop_exit_label, fall_thru_label));
3482 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3484 /* Otherwise, increment the index var... */
3485 tmp = build2 (PLUS_EXPR, var_type, var,
3486 fold_convert (var_type, integer_one_node));
3487 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3489 /* ...and jump back to the loop entry. */
3490 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3492 /* Add the loop exit label. */
3493 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3496 /* Return true if FDECL is accessing a field that is zero sized. */
3498 static bool
3499 zero_sized_field_decl (const_tree fdecl)
3501 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3502 && integer_zerop (DECL_SIZE (fdecl)))
3503 return true;
3504 return false;
3507 /* Return true if TYPE is zero sized. */
3509 static bool
3510 zero_sized_type (const_tree type)
3512 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3513 && integer_zerop (TYPE_SIZE (type)))
3514 return true;
3515 return false;
3518 /* A subroutine of gimplify_init_constructor. Generate individual
3519 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3520 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3521 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3522 zeroed first. */
3524 static void
3525 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3526 gimple_seq *pre_p, bool cleared)
3528 tree array_elt_type = NULL;
3529 unsigned HOST_WIDE_INT ix;
3530 tree purpose, value;
3532 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3533 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3535 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3537 tree cref;
3539 /* NULL values are created above for gimplification errors. */
3540 if (value == NULL)
3541 continue;
3543 if (cleared && initializer_zerop (value))
3544 continue;
3546 /* ??? Here's to hoping the front end fills in all of the indices,
3547 so we don't have to figure out what's missing ourselves. */
3548 gcc_assert (purpose);
3550 /* Skip zero-sized fields, unless value has side-effects. This can
3551 happen with calls to functions returning a zero-sized type, which
3552 we shouldn't discard. As a number of downstream passes don't
3553 expect sets of zero-sized fields, we rely on the gimplification of
3554 the MODIFY_EXPR we make below to drop the assignment statement. */
3555 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3556 continue;
3558 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3559 whole range. */
3560 if (TREE_CODE (purpose) == RANGE_EXPR)
3562 tree lower = TREE_OPERAND (purpose, 0);
3563 tree upper = TREE_OPERAND (purpose, 1);
3565 /* If the lower bound is equal to upper, just treat it as if
3566 upper was the index. */
3567 if (simple_cst_equal (lower, upper))
3568 purpose = upper;
3569 else
3571 gimplify_init_ctor_eval_range (object, lower, upper, value,
3572 array_elt_type, pre_p, cleared);
3573 continue;
3577 if (array_elt_type)
3579 /* Do not use bitsizetype for ARRAY_REF indices. */
3580 if (TYPE_DOMAIN (TREE_TYPE (object)))
3581 purpose
3582 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3583 purpose);
3584 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3585 purpose, NULL_TREE, NULL_TREE);
3587 else
3589 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3590 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3591 unshare_expr (object), purpose, NULL_TREE);
3594 if (TREE_CODE (value) == CONSTRUCTOR
3595 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3596 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3597 pre_p, cleared);
3598 else
3600 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3601 gimplify_and_add (init, pre_p);
3602 ggc_free (init);
3607 /* Return the appropriate RHS predicate for this LHS. */
3609 gimple_predicate
3610 rhs_predicate_for (tree lhs)
3612 if (is_gimple_reg (lhs))
3613 return is_gimple_reg_rhs_or_call;
3614 else
3615 return is_gimple_mem_rhs_or_call;
3618 /* Gimplify a C99 compound literal expression. This just means adding
3619 the DECL_EXPR before the current statement and using its anonymous
3620 decl instead. */
3622 static enum gimplify_status
3623 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3624 bool (*gimple_test_f) (tree),
3625 fallback_t fallback)
3627 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3628 tree decl = DECL_EXPR_DECL (decl_s);
3629 tree init = DECL_INITIAL (decl);
3630 /* Mark the decl as addressable if the compound literal
3631 expression is addressable now, otherwise it is marked too late
3632 after we gimplify the initialization expression. */
3633 if (TREE_ADDRESSABLE (*expr_p))
3634 TREE_ADDRESSABLE (decl) = 1;
3635 /* Otherwise, if we don't need an lvalue and have a literal directly
3636 substitute it. Check if it matches the gimple predicate, as
3637 otherwise we'd generate a new temporary, and we can as well just
3638 use the decl we already have. */
3639 else if (!TREE_ADDRESSABLE (decl)
3640 && init
3641 && (fallback & fb_lvalue) == 0
3642 && gimple_test_f (init))
3644 *expr_p = init;
3645 return GS_OK;
3648 /* Preliminarily mark non-addressed complex variables as eligible
3649 for promotion to gimple registers. We'll transform their uses
3650 as we find them. */
3651 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3652 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3653 && !TREE_THIS_VOLATILE (decl)
3654 && !needs_to_live_in_memory (decl))
3655 DECL_GIMPLE_REG_P (decl) = 1;
3657 /* If the decl is not addressable, then it is being used in some
3658 expression or on the right hand side of a statement, and it can
3659 be put into a readonly data section. */
3660 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3661 TREE_READONLY (decl) = 1;
3663 /* This decl isn't mentioned in the enclosing block, so add it to the
3664 list of temps. FIXME it seems a bit of a kludge to say that
3665 anonymous artificial vars aren't pushed, but everything else is. */
3666 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3667 gimple_add_tmp_var (decl);
3669 gimplify_and_add (decl_s, pre_p);
3670 *expr_p = decl;
3671 return GS_OK;
3674 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3675 return a new CONSTRUCTOR if something changed. */
3677 static tree
3678 optimize_compound_literals_in_ctor (tree orig_ctor)
3680 tree ctor = orig_ctor;
3681 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3682 unsigned int idx, num = vec_safe_length (elts);
3684 for (idx = 0; idx < num; idx++)
3686 tree value = (*elts)[idx].value;
3687 tree newval = value;
3688 if (TREE_CODE (value) == CONSTRUCTOR)
3689 newval = optimize_compound_literals_in_ctor (value);
3690 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3692 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3693 tree decl = DECL_EXPR_DECL (decl_s);
3694 tree init = DECL_INITIAL (decl);
3696 if (!TREE_ADDRESSABLE (value)
3697 && !TREE_ADDRESSABLE (decl)
3698 && init
3699 && TREE_CODE (init) == CONSTRUCTOR)
3700 newval = optimize_compound_literals_in_ctor (init);
3702 if (newval == value)
3703 continue;
3705 if (ctor == orig_ctor)
3707 ctor = copy_node (orig_ctor);
3708 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3709 elts = CONSTRUCTOR_ELTS (ctor);
3711 (*elts)[idx].value = newval;
3713 return ctor;
3716 /* A subroutine of gimplify_modify_expr. Break out elements of a
3717 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3719 Note that we still need to clear any elements that don't have explicit
3720 initializers, so if not all elements are initialized we keep the
3721 original MODIFY_EXPR, we just remove all of the constructor elements.
3723 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3724 GS_ERROR if we would have to create a temporary when gimplifying
3725 this constructor. Otherwise, return GS_OK.
3727 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3729 static enum gimplify_status
3730 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3731 bool want_value, bool notify_temp_creation)
3733 tree object, ctor, type;
3734 enum gimplify_status ret;
3735 vec<constructor_elt, va_gc> *elts;
3737 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3739 if (!notify_temp_creation)
3741 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3742 is_gimple_lvalue, fb_lvalue);
3743 if (ret == GS_ERROR)
3744 return ret;
3747 object = TREE_OPERAND (*expr_p, 0);
3748 ctor = TREE_OPERAND (*expr_p, 1) =
3749 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3750 type = TREE_TYPE (ctor);
3751 elts = CONSTRUCTOR_ELTS (ctor);
3752 ret = GS_ALL_DONE;
3754 switch (TREE_CODE (type))
3756 case RECORD_TYPE:
3757 case UNION_TYPE:
3758 case QUAL_UNION_TYPE:
3759 case ARRAY_TYPE:
3761 struct gimplify_init_ctor_preeval_data preeval_data;
3762 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3763 bool cleared, complete_p, valid_const_initializer;
3765 /* Aggregate types must lower constructors to initialization of
3766 individual elements. The exception is that a CONSTRUCTOR node
3767 with no elements indicates zero-initialization of the whole. */
3768 if (vec_safe_is_empty (elts))
3770 if (notify_temp_creation)
3771 return GS_OK;
3772 break;
3775 /* Fetch information about the constructor to direct later processing.
3776 We might want to make static versions of it in various cases, and
3777 can only do so if it known to be a valid constant initializer. */
3778 valid_const_initializer
3779 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3780 &num_ctor_elements, &complete_p);
3782 /* If a const aggregate variable is being initialized, then it
3783 should never be a lose to promote the variable to be static. */
3784 if (valid_const_initializer
3785 && num_nonzero_elements > 1
3786 && TREE_READONLY (object)
3787 && TREE_CODE (object) == VAR_DECL
3788 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3790 if (notify_temp_creation)
3791 return GS_ERROR;
3792 DECL_INITIAL (object) = ctor;
3793 TREE_STATIC (object) = 1;
3794 if (!DECL_NAME (object))
3795 DECL_NAME (object) = create_tmp_var_name ("C");
3796 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3798 /* ??? C++ doesn't automatically append a .<number> to the
3799 assembler name, and even when it does, it looks at FE private
3800 data structures to figure out what that number should be,
3801 which are not set for this variable. I suppose this is
3802 important for local statics for inline functions, which aren't
3803 "local" in the object file sense. So in order to get a unique
3804 TU-local symbol, we must invoke the lhd version now. */
3805 lhd_set_decl_assembler_name (object);
3807 *expr_p = NULL_TREE;
3808 break;
3811 /* If there are "lots" of initialized elements, even discounting
3812 those that are not address constants (and thus *must* be
3813 computed at runtime), then partition the constructor into
3814 constant and non-constant parts. Block copy the constant
3815 parts in, then generate code for the non-constant parts. */
3816 /* TODO. There's code in cp/typeck.c to do this. */
3818 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3819 /* store_constructor will ignore the clearing of variable-sized
3820 objects. Initializers for such objects must explicitly set
3821 every field that needs to be set. */
3822 cleared = false;
3823 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3824 /* If the constructor isn't complete, clear the whole object
3825 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3827 ??? This ought not to be needed. For any element not present
3828 in the initializer, we should simply set them to zero. Except
3829 we'd need to *find* the elements that are not present, and that
3830 requires trickery to avoid quadratic compile-time behavior in
3831 large cases or excessive memory use in small cases. */
3832 cleared = true;
3833 else if (num_ctor_elements - num_nonzero_elements
3834 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3835 && num_nonzero_elements < num_ctor_elements / 4)
3836 /* If there are "lots" of zeros, it's more efficient to clear
3837 the memory and then set the nonzero elements. */
3838 cleared = true;
3839 else
3840 cleared = false;
3842 /* If there are "lots" of initialized elements, and all of them
3843 are valid address constants, then the entire initializer can
3844 be dropped to memory, and then memcpy'd out. Don't do this
3845 for sparse arrays, though, as it's more efficient to follow
3846 the standard CONSTRUCTOR behavior of memset followed by
3847 individual element initialization. Also don't do this for small
3848 all-zero initializers (which aren't big enough to merit
3849 clearing), and don't try to make bitwise copies of
3850 TREE_ADDRESSABLE types. */
3851 if (valid_const_initializer
3852 && !(cleared || num_nonzero_elements == 0)
3853 && !TREE_ADDRESSABLE (type))
3855 HOST_WIDE_INT size = int_size_in_bytes (type);
3856 unsigned int align;
3858 /* ??? We can still get unbounded array types, at least
3859 from the C++ front end. This seems wrong, but attempt
3860 to work around it for now. */
3861 if (size < 0)
3863 size = int_size_in_bytes (TREE_TYPE (object));
3864 if (size >= 0)
3865 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3868 /* Find the maximum alignment we can assume for the object. */
3869 /* ??? Make use of DECL_OFFSET_ALIGN. */
3870 if (DECL_P (object))
3871 align = DECL_ALIGN (object);
3872 else
3873 align = TYPE_ALIGN (type);
3875 /* Do a block move either if the size is so small as to make
3876 each individual move a sub-unit move on average, or if it
3877 is so large as to make individual moves inefficient. */
3878 if (size > 0
3879 && num_nonzero_elements > 1
3880 && (size < num_nonzero_elements
3881 || !can_move_by_pieces (size, align)))
3883 if (notify_temp_creation)
3884 return GS_ERROR;
3886 walk_tree (&ctor, force_labels_r, NULL, NULL);
3887 ctor = tree_output_constant_def (ctor);
3888 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3889 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3890 TREE_OPERAND (*expr_p, 1) = ctor;
3892 /* This is no longer an assignment of a CONSTRUCTOR, but
3893 we still may have processing to do on the LHS. So
3894 pretend we didn't do anything here to let that happen. */
3895 return GS_UNHANDLED;
3899 /* If the target is volatile, we have non-zero elements and more than
3900 one field to assign, initialize the target from a temporary. */
3901 if (TREE_THIS_VOLATILE (object)
3902 && !TREE_ADDRESSABLE (type)
3903 && num_nonzero_elements > 0
3904 && vec_safe_length (elts) > 1)
3906 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3907 TREE_OPERAND (*expr_p, 0) = temp;
3908 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3909 *expr_p,
3910 build2 (MODIFY_EXPR, void_type_node,
3911 object, temp));
3912 return GS_OK;
3915 if (notify_temp_creation)
3916 return GS_OK;
3918 /* If there are nonzero elements and if needed, pre-evaluate to capture
3919 elements overlapping with the lhs into temporaries. We must do this
3920 before clearing to fetch the values before they are zeroed-out. */
3921 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3923 preeval_data.lhs_base_decl = get_base_address (object);
3924 if (!DECL_P (preeval_data.lhs_base_decl))
3925 preeval_data.lhs_base_decl = NULL;
3926 preeval_data.lhs_alias_set = get_alias_set (object);
3928 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3929 pre_p, post_p, &preeval_data);
3932 if (cleared)
3934 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3935 Note that we still have to gimplify, in order to handle the
3936 case of variable sized types. Avoid shared tree structures. */
3937 CONSTRUCTOR_ELTS (ctor) = NULL;
3938 TREE_SIDE_EFFECTS (ctor) = 0;
3939 object = unshare_expr (object);
3940 gimplify_stmt (expr_p, pre_p);
3943 /* If we have not block cleared the object, or if there are nonzero
3944 elements in the constructor, add assignments to the individual
3945 scalar fields of the object. */
3946 if (!cleared || num_nonzero_elements > 0)
3947 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3949 *expr_p = NULL_TREE;
3951 break;
3953 case COMPLEX_TYPE:
3955 tree r, i;
3957 if (notify_temp_creation)
3958 return GS_OK;
3960 /* Extract the real and imaginary parts out of the ctor. */
3961 gcc_assert (elts->length () == 2);
3962 r = (*elts)[0].value;
3963 i = (*elts)[1].value;
3964 if (r == NULL || i == NULL)
3966 tree zero = build_zero_cst (TREE_TYPE (type));
3967 if (r == NULL)
3968 r = zero;
3969 if (i == NULL)
3970 i = zero;
3973 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3974 represent creation of a complex value. */
3975 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3977 ctor = build_complex (type, r, i);
3978 TREE_OPERAND (*expr_p, 1) = ctor;
3980 else
3982 ctor = build2 (COMPLEX_EXPR, type, r, i);
3983 TREE_OPERAND (*expr_p, 1) = ctor;
3984 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3985 pre_p,
3986 post_p,
3987 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3988 fb_rvalue);
3991 break;
3993 case VECTOR_TYPE:
3995 unsigned HOST_WIDE_INT ix;
3996 constructor_elt *ce;
3998 if (notify_temp_creation)
3999 return GS_OK;
4001 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4002 if (TREE_CONSTANT (ctor))
4004 bool constant_p = true;
4005 tree value;
4007 /* Even when ctor is constant, it might contain non-*_CST
4008 elements, such as addresses or trapping values like
4009 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4010 in VECTOR_CST nodes. */
4011 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4012 if (!CONSTANT_CLASS_P (value))
4014 constant_p = false;
4015 break;
4018 if (constant_p)
4020 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4021 break;
4024 /* Don't reduce an initializer constant even if we can't
4025 make a VECTOR_CST. It won't do anything for us, and it'll
4026 prevent us from representing it as a single constant. */
4027 if (initializer_constant_valid_p (ctor, type))
4028 break;
4030 TREE_CONSTANT (ctor) = 0;
4033 /* Vector types use CONSTRUCTOR all the way through gimple
4034 compilation as a general initializer. */
4035 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4037 enum gimplify_status tret;
4038 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4039 fb_rvalue);
4040 if (tret == GS_ERROR)
4041 ret = GS_ERROR;
4043 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4044 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4046 break;
4048 default:
4049 /* So how did we get a CONSTRUCTOR for a scalar type? */
4050 gcc_unreachable ();
4053 if (ret == GS_ERROR)
4054 return GS_ERROR;
4055 else if (want_value)
4057 *expr_p = object;
4058 return GS_OK;
4060 else
4062 /* If we have gimplified both sides of the initializer but have
4063 not emitted an assignment, do so now. */
4064 if (*expr_p)
4066 tree lhs = TREE_OPERAND (*expr_p, 0);
4067 tree rhs = TREE_OPERAND (*expr_p, 1);
4068 gimple init = gimple_build_assign (lhs, rhs);
4069 gimplify_seq_add_stmt (pre_p, init);
4070 *expr_p = NULL;
4073 return GS_ALL_DONE;
4077 /* Given a pointer value OP0, return a simplified version of an
4078 indirection through OP0, or NULL_TREE if no simplification is
4079 possible. This may only be applied to a rhs of an expression.
4080 Note that the resulting type may be different from the type pointed
4081 to in the sense that it is still compatible from the langhooks
4082 point of view. */
4084 static tree
4085 gimple_fold_indirect_ref_rhs (tree t)
4087 return gimple_fold_indirect_ref (t);
4090 /* Subroutine of gimplify_modify_expr to do simplifications of
4091 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4092 something changes. */
4094 static enum gimplify_status
4095 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4096 gimple_seq *pre_p, gimple_seq *post_p,
4097 bool want_value)
4099 enum gimplify_status ret = GS_UNHANDLED;
4100 bool changed;
4104 changed = false;
4105 switch (TREE_CODE (*from_p))
4107 case VAR_DECL:
4108 /* If we're assigning from a read-only variable initialized with
4109 a constructor, do the direct assignment from the constructor,
4110 but only if neither source nor target are volatile since this
4111 latter assignment might end up being done on a per-field basis. */
4112 if (DECL_INITIAL (*from_p)
4113 && TREE_READONLY (*from_p)
4114 && !TREE_THIS_VOLATILE (*from_p)
4115 && !TREE_THIS_VOLATILE (*to_p)
4116 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4118 tree old_from = *from_p;
4119 enum gimplify_status subret;
4121 /* Move the constructor into the RHS. */
4122 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4124 /* Let's see if gimplify_init_constructor will need to put
4125 it in memory. */
4126 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4127 false, true);
4128 if (subret == GS_ERROR)
4130 /* If so, revert the change. */
4131 *from_p = old_from;
4133 else
4135 ret = GS_OK;
4136 changed = true;
4139 break;
4140 case INDIRECT_REF:
4142 /* If we have code like
4144 *(const A*)(A*)&x
4146 where the type of "x" is a (possibly cv-qualified variant
4147 of "A"), treat the entire expression as identical to "x".
4148 This kind of code arises in C++ when an object is bound
4149 to a const reference, and if "x" is a TARGET_EXPR we want
4150 to take advantage of the optimization below. */
4151 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4152 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4153 if (t)
4155 if (TREE_THIS_VOLATILE (t) != volatile_p)
4157 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4158 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4159 build_fold_addr_expr (t));
4160 if (REFERENCE_CLASS_P (t))
4161 TREE_THIS_VOLATILE (t) = volatile_p;
4163 *from_p = t;
4164 ret = GS_OK;
4165 changed = true;
4167 break;
4170 case TARGET_EXPR:
4172 /* If we are initializing something from a TARGET_EXPR, strip the
4173 TARGET_EXPR and initialize it directly, if possible. This can't
4174 be done if the initializer is void, since that implies that the
4175 temporary is set in some non-trivial way.
4177 ??? What about code that pulls out the temp and uses it
4178 elsewhere? I think that such code never uses the TARGET_EXPR as
4179 an initializer. If I'm wrong, we'll die because the temp won't
4180 have any RTL. In that case, I guess we'll need to replace
4181 references somehow. */
4182 tree init = TARGET_EXPR_INITIAL (*from_p);
4184 if (init
4185 && !VOID_TYPE_P (TREE_TYPE (init)))
4187 *from_p = init;
4188 ret = GS_OK;
4189 changed = true;
4192 break;
4194 case COMPOUND_EXPR:
4195 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4196 caught. */
4197 gimplify_compound_expr (from_p, pre_p, true);
4198 ret = GS_OK;
4199 changed = true;
4200 break;
4202 case CONSTRUCTOR:
4203 /* If we already made some changes, let the front end have a
4204 crack at this before we break it down. */
4205 if (ret != GS_UNHANDLED)
4206 break;
4207 /* If we're initializing from a CONSTRUCTOR, break this into
4208 individual MODIFY_EXPRs. */
4209 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4210 false);
4212 case COND_EXPR:
4213 /* If we're assigning to a non-register type, push the assignment
4214 down into the branches. This is mandatory for ADDRESSABLE types,
4215 since we cannot generate temporaries for such, but it saves a
4216 copy in other cases as well. */
4217 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4219 /* This code should mirror the code in gimplify_cond_expr. */
4220 enum tree_code code = TREE_CODE (*expr_p);
4221 tree cond = *from_p;
4222 tree result = *to_p;
4224 ret = gimplify_expr (&result, pre_p, post_p,
4225 is_gimple_lvalue, fb_lvalue);
4226 if (ret != GS_ERROR)
4227 ret = GS_OK;
4229 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4230 TREE_OPERAND (cond, 1)
4231 = build2 (code, void_type_node, result,
4232 TREE_OPERAND (cond, 1));
4233 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4234 TREE_OPERAND (cond, 2)
4235 = build2 (code, void_type_node, unshare_expr (result),
4236 TREE_OPERAND (cond, 2));
4238 TREE_TYPE (cond) = void_type_node;
4239 recalculate_side_effects (cond);
4241 if (want_value)
4243 gimplify_and_add (cond, pre_p);
4244 *expr_p = unshare_expr (result);
4246 else
4247 *expr_p = cond;
4248 return ret;
4250 break;
4252 case CALL_EXPR:
4253 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4254 return slot so that we don't generate a temporary. */
4255 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4256 && aggregate_value_p (*from_p, *from_p))
4258 bool use_target;
4260 if (!(rhs_predicate_for (*to_p))(*from_p))
4261 /* If we need a temporary, *to_p isn't accurate. */
4262 use_target = false;
4263 /* It's OK to use the return slot directly unless it's an NRV. */
4264 else if (TREE_CODE (*to_p) == RESULT_DECL
4265 && DECL_NAME (*to_p) == NULL_TREE
4266 && needs_to_live_in_memory (*to_p))
4267 use_target = true;
4268 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4269 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4270 /* Don't force regs into memory. */
4271 use_target = false;
4272 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4273 /* It's OK to use the target directly if it's being
4274 initialized. */
4275 use_target = true;
4276 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4277 /* Always use the target and thus RSO for variable-sized types.
4278 GIMPLE cannot deal with a variable-sized assignment
4279 embedded in a call statement. */
4280 use_target = true;
4281 else if (TREE_CODE (*to_p) != SSA_NAME
4282 && (!is_gimple_variable (*to_p)
4283 || needs_to_live_in_memory (*to_p)))
4284 /* Don't use the original target if it's already addressable;
4285 if its address escapes, and the called function uses the
4286 NRV optimization, a conforming program could see *to_p
4287 change before the called function returns; see c++/19317.
4288 When optimizing, the return_slot pass marks more functions
4289 as safe after we have escape info. */
4290 use_target = false;
4291 else
4292 use_target = true;
4294 if (use_target)
4296 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4297 mark_addressable (*to_p);
4300 break;
4302 case WITH_SIZE_EXPR:
4303 /* Likewise for calls that return an aggregate of non-constant size,
4304 since we would not be able to generate a temporary at all. */
4305 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4307 *from_p = TREE_OPERAND (*from_p, 0);
4308 /* We don't change ret in this case because the
4309 WITH_SIZE_EXPR might have been added in
4310 gimplify_modify_expr, so returning GS_OK would lead to an
4311 infinite loop. */
4312 changed = true;
4314 break;
4316 /* If we're initializing from a container, push the initialization
4317 inside it. */
4318 case CLEANUP_POINT_EXPR:
4319 case BIND_EXPR:
4320 case STATEMENT_LIST:
4322 tree wrap = *from_p;
4323 tree t;
4325 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4326 fb_lvalue);
4327 if (ret != GS_ERROR)
4328 ret = GS_OK;
4330 t = voidify_wrapper_expr (wrap, *expr_p);
4331 gcc_assert (t == *expr_p);
4333 if (want_value)
4335 gimplify_and_add (wrap, pre_p);
4336 *expr_p = unshare_expr (*to_p);
4338 else
4339 *expr_p = wrap;
4340 return GS_OK;
4343 case COMPOUND_LITERAL_EXPR:
4345 tree complit = TREE_OPERAND (*expr_p, 1);
4346 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4347 tree decl = DECL_EXPR_DECL (decl_s);
4348 tree init = DECL_INITIAL (decl);
4350 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4351 into struct T x = { 0, 1, 2 } if the address of the
4352 compound literal has never been taken. */
4353 if (!TREE_ADDRESSABLE (complit)
4354 && !TREE_ADDRESSABLE (decl)
4355 && init)
4357 *expr_p = copy_node (*expr_p);
4358 TREE_OPERAND (*expr_p, 1) = init;
4359 return GS_OK;
4363 default:
4364 break;
4367 while (changed);
4369 return ret;
4373 /* Return true if T looks like a valid GIMPLE statement. */
4375 static bool
4376 is_gimple_stmt (tree t)
4378 const enum tree_code code = TREE_CODE (t);
4380 switch (code)
4382 case NOP_EXPR:
4383 /* The only valid NOP_EXPR is the empty statement. */
4384 return IS_EMPTY_STMT (t);
4386 case BIND_EXPR:
4387 case COND_EXPR:
4388 /* These are only valid if they're void. */
4389 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4391 case SWITCH_EXPR:
4392 case GOTO_EXPR:
4393 case RETURN_EXPR:
4394 case LABEL_EXPR:
4395 case CASE_LABEL_EXPR:
4396 case TRY_CATCH_EXPR:
4397 case TRY_FINALLY_EXPR:
4398 case EH_FILTER_EXPR:
4399 case CATCH_EXPR:
4400 case ASM_EXPR:
4401 case STATEMENT_LIST:
4402 case OMP_PARALLEL:
4403 case OMP_FOR:
4404 case OMP_SIMD:
4405 case CILK_SIMD:
4406 case OMP_DISTRIBUTE:
4407 case OMP_SECTIONS:
4408 case OMP_SECTION:
4409 case OMP_SINGLE:
4410 case OMP_MASTER:
4411 case OMP_TASKGROUP:
4412 case OMP_ORDERED:
4413 case OMP_CRITICAL:
4414 case OMP_TASK:
4415 /* These are always void. */
4416 return true;
4418 case CALL_EXPR:
4419 case MODIFY_EXPR:
4420 case PREDICT_EXPR:
4421 /* These are valid regardless of their type. */
4422 return true;
4424 default:
4425 return false;
4430 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4431 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4432 DECL_GIMPLE_REG_P set.
4434 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4435 other, unmodified part of the complex object just before the total store.
4436 As a consequence, if the object is still uninitialized, an undefined value
4437 will be loaded into a register, which may result in a spurious exception
4438 if the register is floating-point and the value happens to be a signaling
4439 NaN for example. Then the fully-fledged complex operations lowering pass
4440 followed by a DCE pass are necessary in order to fix things up. */
4442 static enum gimplify_status
4443 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4444 bool want_value)
4446 enum tree_code code, ocode;
4447 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4449 lhs = TREE_OPERAND (*expr_p, 0);
4450 rhs = TREE_OPERAND (*expr_p, 1);
4451 code = TREE_CODE (lhs);
4452 lhs = TREE_OPERAND (lhs, 0);
4454 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4455 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4456 TREE_NO_WARNING (other) = 1;
4457 other = get_formal_tmp_var (other, pre_p);
4459 realpart = code == REALPART_EXPR ? rhs : other;
4460 imagpart = code == REALPART_EXPR ? other : rhs;
4462 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4463 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4464 else
4465 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4467 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4468 *expr_p = (want_value) ? rhs : NULL_TREE;
4470 return GS_ALL_DONE;
4473 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4475 modify_expr
4476 : varname '=' rhs
4477 | '*' ID '=' rhs
4479 PRE_P points to the list where side effects that must happen before
4480 *EXPR_P should be stored.
4482 POST_P points to the list where side effects that must happen after
4483 *EXPR_P should be stored.
4485 WANT_VALUE is nonzero iff we want to use the value of this expression
4486 in another expression. */
4488 static enum gimplify_status
4489 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4490 bool want_value)
4492 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4493 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4494 enum gimplify_status ret = GS_UNHANDLED;
4495 gimple assign;
4496 location_t loc = EXPR_LOCATION (*expr_p);
4497 gimple_stmt_iterator gsi;
4499 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4500 || TREE_CODE (*expr_p) == INIT_EXPR);
4502 /* Trying to simplify a clobber using normal logic doesn't work,
4503 so handle it here. */
4504 if (TREE_CLOBBER_P (*from_p))
4506 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4507 if (ret == GS_ERROR)
4508 return ret;
4509 gcc_assert (!want_value
4510 && (TREE_CODE (*to_p) == VAR_DECL
4511 || TREE_CODE (*to_p) == MEM_REF));
4512 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4513 *expr_p = NULL;
4514 return GS_ALL_DONE;
4517 /* Insert pointer conversions required by the middle-end that are not
4518 required by the frontend. This fixes middle-end type checking for
4519 for example gcc.dg/redecl-6.c. */
4520 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4522 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4523 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4524 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4527 /* See if any simplifications can be done based on what the RHS is. */
4528 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4529 want_value);
4530 if (ret != GS_UNHANDLED)
4531 return ret;
4533 /* For zero sized types only gimplify the left hand side and right hand
4534 side as statements and throw away the assignment. Do this after
4535 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4536 types properly. */
4537 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4539 gimplify_stmt (from_p, pre_p);
4540 gimplify_stmt (to_p, pre_p);
4541 *expr_p = NULL_TREE;
4542 return GS_ALL_DONE;
4545 /* If the value being copied is of variable width, compute the length
4546 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4547 before gimplifying any of the operands so that we can resolve any
4548 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4549 the size of the expression to be copied, not of the destination, so
4550 that is what we must do here. */
4551 maybe_with_size_expr (from_p);
4553 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4554 if (ret == GS_ERROR)
4555 return ret;
4557 /* As a special case, we have to temporarily allow for assignments
4558 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4559 a toplevel statement, when gimplifying the GENERIC expression
4560 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4561 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4563 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4564 prevent gimplify_expr from trying to create a new temporary for
4565 foo's LHS, we tell it that it should only gimplify until it
4566 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4567 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4568 and all we need to do here is set 'a' to be its LHS. */
4569 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4570 fb_rvalue);
4571 if (ret == GS_ERROR)
4572 return ret;
4574 /* Now see if the above changed *from_p to something we handle specially. */
4575 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4576 want_value);
4577 if (ret != GS_UNHANDLED)
4578 return ret;
4580 /* If we've got a variable sized assignment between two lvalues (i.e. does
4581 not involve a call), then we can make things a bit more straightforward
4582 by converting the assignment to memcpy or memset. */
4583 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4585 tree from = TREE_OPERAND (*from_p, 0);
4586 tree size = TREE_OPERAND (*from_p, 1);
4588 if (TREE_CODE (from) == CONSTRUCTOR)
4589 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4591 if (is_gimple_addressable (from))
4593 *from_p = from;
4594 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4595 pre_p);
4599 /* Transform partial stores to non-addressable complex variables into
4600 total stores. This allows us to use real instead of virtual operands
4601 for these variables, which improves optimization. */
4602 if ((TREE_CODE (*to_p) == REALPART_EXPR
4603 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4604 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4605 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4607 /* Try to alleviate the effects of the gimplification creating artificial
4608 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4609 if (!gimplify_ctxp->into_ssa
4610 && TREE_CODE (*from_p) == VAR_DECL
4611 && DECL_IGNORED_P (*from_p)
4612 && DECL_P (*to_p)
4613 && !DECL_IGNORED_P (*to_p))
4615 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4616 DECL_NAME (*from_p)
4617 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4618 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4619 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4622 if (want_value && TREE_THIS_VOLATILE (*to_p))
4623 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4625 if (TREE_CODE (*from_p) == CALL_EXPR)
4627 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4628 instead of a GIMPLE_ASSIGN. */
4629 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4630 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4631 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4632 tree fndecl = get_callee_fndecl (*from_p);
4633 if (fndecl
4634 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4635 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4636 && call_expr_nargs (*from_p) == 3)
4637 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4638 CALL_EXPR_ARG (*from_p, 0),
4639 CALL_EXPR_ARG (*from_p, 1),
4640 CALL_EXPR_ARG (*from_p, 2));
4641 else
4643 assign = gimple_build_call_from_tree (*from_p);
4644 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4646 notice_special_calls (assign);
4647 if (!gimple_call_noreturn_p (assign))
4648 gimple_call_set_lhs (assign, *to_p);
4650 else
4652 assign = gimple_build_assign (*to_p, *from_p);
4653 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4656 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4658 /* We should have got an SSA name from the start. */
4659 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4662 gimplify_seq_add_stmt (pre_p, assign);
4663 gsi = gsi_last (*pre_p);
4664 maybe_fold_stmt (&gsi);
4666 if (want_value)
4668 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4669 return GS_OK;
4671 else
4672 *expr_p = NULL;
4674 return GS_ALL_DONE;
4677 /* Gimplify a comparison between two variable-sized objects. Do this
4678 with a call to BUILT_IN_MEMCMP. */
4680 static enum gimplify_status
4681 gimplify_variable_sized_compare (tree *expr_p)
4683 location_t loc = EXPR_LOCATION (*expr_p);
4684 tree op0 = TREE_OPERAND (*expr_p, 0);
4685 tree op1 = TREE_OPERAND (*expr_p, 1);
4686 tree t, arg, dest, src, expr;
4688 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4689 arg = unshare_expr (arg);
4690 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4691 src = build_fold_addr_expr_loc (loc, op1);
4692 dest = build_fold_addr_expr_loc (loc, op0);
4693 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4694 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4696 expr
4697 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4698 SET_EXPR_LOCATION (expr, loc);
4699 *expr_p = expr;
4701 return GS_OK;
4704 /* Gimplify a comparison between two aggregate objects of integral scalar
4705 mode as a comparison between the bitwise equivalent scalar values. */
4707 static enum gimplify_status
4708 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4710 location_t loc = EXPR_LOCATION (*expr_p);
4711 tree op0 = TREE_OPERAND (*expr_p, 0);
4712 tree op1 = TREE_OPERAND (*expr_p, 1);
4714 tree type = TREE_TYPE (op0);
4715 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4717 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4718 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4720 *expr_p
4721 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4723 return GS_OK;
4726 /* Gimplify an expression sequence. This function gimplifies each
4727 expression and rewrites the original expression with the last
4728 expression of the sequence in GIMPLE form.
4730 PRE_P points to the list where the side effects for all the
4731 expressions in the sequence will be emitted.
4733 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4735 static enum gimplify_status
4736 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4738 tree t = *expr_p;
4742 tree *sub_p = &TREE_OPERAND (t, 0);
4744 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4745 gimplify_compound_expr (sub_p, pre_p, false);
4746 else
4747 gimplify_stmt (sub_p, pre_p);
4749 t = TREE_OPERAND (t, 1);
4751 while (TREE_CODE (t) == COMPOUND_EXPR);
4753 *expr_p = t;
4754 if (want_value)
4755 return GS_OK;
4756 else
4758 gimplify_stmt (expr_p, pre_p);
4759 return GS_ALL_DONE;
4763 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4764 gimplify. After gimplification, EXPR_P will point to a new temporary
4765 that holds the original value of the SAVE_EXPR node.
4767 PRE_P points to the list where side effects that must happen before
4768 *EXPR_P should be stored. */
4770 static enum gimplify_status
4771 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4773 enum gimplify_status ret = GS_ALL_DONE;
4774 tree val;
4776 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4777 val = TREE_OPERAND (*expr_p, 0);
4779 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4780 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4782 /* The operand may be a void-valued expression such as SAVE_EXPRs
4783 generated by the Java frontend for class initialization. It is
4784 being executed only for its side-effects. */
4785 if (TREE_TYPE (val) == void_type_node)
4787 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4788 is_gimple_stmt, fb_none);
4789 val = NULL;
4791 else
4792 val = get_initialized_tmp_var (val, pre_p, post_p);
4794 TREE_OPERAND (*expr_p, 0) = val;
4795 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4798 *expr_p = val;
4800 return ret;
4803 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4805 unary_expr
4806 : ...
4807 | '&' varname
4810 PRE_P points to the list where side effects that must happen before
4811 *EXPR_P should be stored.
4813 POST_P points to the list where side effects that must happen after
4814 *EXPR_P should be stored. */
4816 static enum gimplify_status
4817 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4819 tree expr = *expr_p;
4820 tree op0 = TREE_OPERAND (expr, 0);
4821 enum gimplify_status ret;
4822 location_t loc = EXPR_LOCATION (*expr_p);
4824 switch (TREE_CODE (op0))
4826 case INDIRECT_REF:
4827 do_indirect_ref:
4828 /* Check if we are dealing with an expression of the form '&*ptr'.
4829 While the front end folds away '&*ptr' into 'ptr', these
4830 expressions may be generated internally by the compiler (e.g.,
4831 builtins like __builtin_va_end). */
4832 /* Caution: the silent array decomposition semantics we allow for
4833 ADDR_EXPR means we can't always discard the pair. */
4834 /* Gimplification of the ADDR_EXPR operand may drop
4835 cv-qualification conversions, so make sure we add them if
4836 needed. */
4838 tree op00 = TREE_OPERAND (op0, 0);
4839 tree t_expr = TREE_TYPE (expr);
4840 tree t_op00 = TREE_TYPE (op00);
4842 if (!useless_type_conversion_p (t_expr, t_op00))
4843 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4844 *expr_p = op00;
4845 ret = GS_OK;
4847 break;
4849 case VIEW_CONVERT_EXPR:
4850 /* Take the address of our operand and then convert it to the type of
4851 this ADDR_EXPR.
4853 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4854 all clear. The impact of this transformation is even less clear. */
4856 /* If the operand is a useless conversion, look through it. Doing so
4857 guarantees that the ADDR_EXPR and its operand will remain of the
4858 same type. */
4859 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4860 op0 = TREE_OPERAND (op0, 0);
4862 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4863 build_fold_addr_expr_loc (loc,
4864 TREE_OPERAND (op0, 0)));
4865 ret = GS_OK;
4866 break;
4868 default:
4869 /* We use fb_either here because the C frontend sometimes takes
4870 the address of a call that returns a struct; see
4871 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4872 the implied temporary explicit. */
4874 /* Make the operand addressable. */
4875 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4876 is_gimple_addressable, fb_either);
4877 if (ret == GS_ERROR)
4878 break;
4880 /* Then mark it. Beware that it may not be possible to do so directly
4881 if a temporary has been created by the gimplification. */
4882 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4884 op0 = TREE_OPERAND (expr, 0);
4886 /* For various reasons, the gimplification of the expression
4887 may have made a new INDIRECT_REF. */
4888 if (TREE_CODE (op0) == INDIRECT_REF)
4889 goto do_indirect_ref;
4891 mark_addressable (TREE_OPERAND (expr, 0));
4893 /* The FEs may end up building ADDR_EXPRs early on a decl with
4894 an incomplete type. Re-build ADDR_EXPRs in canonical form
4895 here. */
4896 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4897 *expr_p = build_fold_addr_expr (op0);
4899 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4900 recompute_tree_invariant_for_addr_expr (*expr_p);
4902 /* If we re-built the ADDR_EXPR add a conversion to the original type
4903 if required. */
4904 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4905 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4907 break;
4910 return ret;
4913 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4914 value; output operands should be a gimple lvalue. */
4916 static enum gimplify_status
4917 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4919 tree expr;
4920 int noutputs;
4921 const char **oconstraints;
4922 int i;
4923 tree link;
4924 const char *constraint;
4925 bool allows_mem, allows_reg, is_inout;
4926 enum gimplify_status ret, tret;
4927 gimple stmt;
4928 vec<tree, va_gc> *inputs;
4929 vec<tree, va_gc> *outputs;
4930 vec<tree, va_gc> *clobbers;
4931 vec<tree, va_gc> *labels;
4932 tree link_next;
4934 expr = *expr_p;
4935 noutputs = list_length (ASM_OUTPUTS (expr));
4936 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4938 inputs = NULL;
4939 outputs = NULL;
4940 clobbers = NULL;
4941 labels = NULL;
4943 ret = GS_ALL_DONE;
4944 link_next = NULL_TREE;
4945 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4947 bool ok;
4948 size_t constraint_len;
4950 link_next = TREE_CHAIN (link);
4952 oconstraints[i]
4953 = constraint
4954 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4955 constraint_len = strlen (constraint);
4956 if (constraint_len == 0)
4957 continue;
4959 ok = parse_output_constraint (&constraint, i, 0, 0,
4960 &allows_mem, &allows_reg, &is_inout);
4961 if (!ok)
4963 ret = GS_ERROR;
4964 is_inout = false;
4967 if (!allows_reg && allows_mem)
4968 mark_addressable (TREE_VALUE (link));
4970 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4971 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4972 fb_lvalue | fb_mayfail);
4973 if (tret == GS_ERROR)
4975 error ("invalid lvalue in asm output %d", i);
4976 ret = tret;
4979 vec_safe_push (outputs, link);
4980 TREE_CHAIN (link) = NULL_TREE;
4982 if (is_inout)
4984 /* An input/output operand. To give the optimizers more
4985 flexibility, split it into separate input and output
4986 operands. */
4987 tree input;
4988 char buf[10];
4990 /* Turn the in/out constraint into an output constraint. */
4991 char *p = xstrdup (constraint);
4992 p[0] = '=';
4993 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4995 /* And add a matching input constraint. */
4996 if (allows_reg)
4998 sprintf (buf, "%d", i);
5000 /* If there are multiple alternatives in the constraint,
5001 handle each of them individually. Those that allow register
5002 will be replaced with operand number, the others will stay
5003 unchanged. */
5004 if (strchr (p, ',') != NULL)
5006 size_t len = 0, buflen = strlen (buf);
5007 char *beg, *end, *str, *dst;
5009 for (beg = p + 1;;)
5011 end = strchr (beg, ',');
5012 if (end == NULL)
5013 end = strchr (beg, '\0');
5014 if ((size_t) (end - beg) < buflen)
5015 len += buflen + 1;
5016 else
5017 len += end - beg + 1;
5018 if (*end)
5019 beg = end + 1;
5020 else
5021 break;
5024 str = (char *) alloca (len);
5025 for (beg = p + 1, dst = str;;)
5027 const char *tem;
5028 bool mem_p, reg_p, inout_p;
5030 end = strchr (beg, ',');
5031 if (end)
5032 *end = '\0';
5033 beg[-1] = '=';
5034 tem = beg - 1;
5035 parse_output_constraint (&tem, i, 0, 0,
5036 &mem_p, &reg_p, &inout_p);
5037 if (dst != str)
5038 *dst++ = ',';
5039 if (reg_p)
5041 memcpy (dst, buf, buflen);
5042 dst += buflen;
5044 else
5046 if (end)
5047 len = end - beg;
5048 else
5049 len = strlen (beg);
5050 memcpy (dst, beg, len);
5051 dst += len;
5053 if (end)
5054 beg = end + 1;
5055 else
5056 break;
5058 *dst = '\0';
5059 input = build_string (dst - str, str);
5061 else
5062 input = build_string (strlen (buf), buf);
5064 else
5065 input = build_string (constraint_len - 1, constraint + 1);
5067 free (p);
5069 input = build_tree_list (build_tree_list (NULL_TREE, input),
5070 unshare_expr (TREE_VALUE (link)));
5071 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5075 link_next = NULL_TREE;
5076 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5078 link_next = TREE_CHAIN (link);
5079 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5080 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5081 oconstraints, &allows_mem, &allows_reg);
5083 /* If we can't make copies, we can only accept memory. */
5084 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5086 if (allows_mem)
5087 allows_reg = 0;
5088 else
5090 error ("impossible constraint in %<asm%>");
5091 error ("non-memory input %d must stay in memory", i);
5092 return GS_ERROR;
5096 /* If the operand is a memory input, it should be an lvalue. */
5097 if (!allows_reg && allows_mem)
5099 tree inputv = TREE_VALUE (link);
5100 STRIP_NOPS (inputv);
5101 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5102 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5103 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5104 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5105 TREE_VALUE (link) = error_mark_node;
5106 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5107 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5108 mark_addressable (TREE_VALUE (link));
5109 if (tret == GS_ERROR)
5111 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5112 input_location = EXPR_LOCATION (TREE_VALUE (link));
5113 error ("memory input %d is not directly addressable", i);
5114 ret = tret;
5117 else
5119 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5120 is_gimple_asm_val, fb_rvalue);
5121 if (tret == GS_ERROR)
5122 ret = tret;
5125 TREE_CHAIN (link) = NULL_TREE;
5126 vec_safe_push (inputs, link);
5129 link_next = NULL_TREE;
5130 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5132 link_next = TREE_CHAIN (link);
5133 TREE_CHAIN (link) = NULL_TREE;
5134 vec_safe_push (clobbers, link);
5137 link_next = NULL_TREE;
5138 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5140 link_next = TREE_CHAIN (link);
5141 TREE_CHAIN (link) = NULL_TREE;
5142 vec_safe_push (labels, link);
5145 /* Do not add ASMs with errors to the gimple IL stream. */
5146 if (ret != GS_ERROR)
5148 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5149 inputs, outputs, clobbers, labels);
5151 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5152 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5154 gimplify_seq_add_stmt (pre_p, stmt);
5157 return ret;
5160 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5161 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5162 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5163 return to this function.
5165 FIXME should we complexify the prequeue handling instead? Or use flags
5166 for all the cleanups and let the optimizer tighten them up? The current
5167 code seems pretty fragile; it will break on a cleanup within any
5168 non-conditional nesting. But any such nesting would be broken, anyway;
5169 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5170 and continues out of it. We can do that at the RTL level, though, so
5171 having an optimizer to tighten up try/finally regions would be a Good
5172 Thing. */
5174 static enum gimplify_status
5175 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5177 gimple_stmt_iterator iter;
5178 gimple_seq body_sequence = NULL;
5180 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5182 /* We only care about the number of conditions between the innermost
5183 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5184 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5185 int old_conds = gimplify_ctxp->conditions;
5186 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5187 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5188 gimplify_ctxp->conditions = 0;
5189 gimplify_ctxp->conditional_cleanups = NULL;
5190 gimplify_ctxp->in_cleanup_point_expr = true;
5192 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5194 gimplify_ctxp->conditions = old_conds;
5195 gimplify_ctxp->conditional_cleanups = old_cleanups;
5196 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5198 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5200 gimple wce = gsi_stmt (iter);
5202 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5204 if (gsi_one_before_end_p (iter))
5206 /* Note that gsi_insert_seq_before and gsi_remove do not
5207 scan operands, unlike some other sequence mutators. */
5208 if (!gimple_wce_cleanup_eh_only (wce))
5209 gsi_insert_seq_before_without_update (&iter,
5210 gimple_wce_cleanup (wce),
5211 GSI_SAME_STMT);
5212 gsi_remove (&iter, true);
5213 break;
5215 else
5217 gimple_statement_try *gtry;
5218 gimple_seq seq;
5219 enum gimple_try_flags kind;
5221 if (gimple_wce_cleanup_eh_only (wce))
5222 kind = GIMPLE_TRY_CATCH;
5223 else
5224 kind = GIMPLE_TRY_FINALLY;
5225 seq = gsi_split_seq_after (iter);
5227 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5228 /* Do not use gsi_replace here, as it may scan operands.
5229 We want to do a simple structural modification only. */
5230 gsi_set_stmt (&iter, gtry);
5231 iter = gsi_start (gtry->eval);
5234 else
5235 gsi_next (&iter);
5238 gimplify_seq_add_seq (pre_p, body_sequence);
5239 if (temp)
5241 *expr_p = temp;
5242 return GS_OK;
5244 else
5246 *expr_p = NULL;
5247 return GS_ALL_DONE;
5251 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5252 is the cleanup action required. EH_ONLY is true if the cleanup should
5253 only be executed if an exception is thrown, not on normal exit. */
5255 static void
5256 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5258 gimple wce;
5259 gimple_seq cleanup_stmts = NULL;
5261 /* Errors can result in improperly nested cleanups. Which results in
5262 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5263 if (seen_error ())
5264 return;
5266 if (gimple_conditional_context ())
5268 /* If we're in a conditional context, this is more complex. We only
5269 want to run the cleanup if we actually ran the initialization that
5270 necessitates it, but we want to run it after the end of the
5271 conditional context. So we wrap the try/finally around the
5272 condition and use a flag to determine whether or not to actually
5273 run the destructor. Thus
5275 test ? f(A()) : 0
5277 becomes (approximately)
5279 flag = 0;
5280 try {
5281 if (test) { A::A(temp); flag = 1; val = f(temp); }
5282 else { val = 0; }
5283 } finally {
5284 if (flag) A::~A(temp);
5288 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5289 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5290 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5292 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5293 gimplify_stmt (&cleanup, &cleanup_stmts);
5294 wce = gimple_build_wce (cleanup_stmts);
5296 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5297 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5298 gimplify_seq_add_stmt (pre_p, ftrue);
5300 /* Because of this manipulation, and the EH edges that jump
5301 threading cannot redirect, the temporary (VAR) will appear
5302 to be used uninitialized. Don't warn. */
5303 TREE_NO_WARNING (var) = 1;
5305 else
5307 gimplify_stmt (&cleanup, &cleanup_stmts);
5308 wce = gimple_build_wce (cleanup_stmts);
5309 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5310 gimplify_seq_add_stmt (pre_p, wce);
5314 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5316 static enum gimplify_status
5317 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5319 tree targ = *expr_p;
5320 tree temp = TARGET_EXPR_SLOT (targ);
5321 tree init = TARGET_EXPR_INITIAL (targ);
5322 enum gimplify_status ret;
5324 if (init)
5326 tree cleanup = NULL_TREE;
5328 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5329 to the temps list. Handle also variable length TARGET_EXPRs. */
5330 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5332 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5333 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5334 gimplify_vla_decl (temp, pre_p);
5336 else
5337 gimple_add_tmp_var (temp);
5339 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5340 expression is supposed to initialize the slot. */
5341 if (VOID_TYPE_P (TREE_TYPE (init)))
5342 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5343 else
5345 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5346 init = init_expr;
5347 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5348 init = NULL;
5349 ggc_free (init_expr);
5351 if (ret == GS_ERROR)
5353 /* PR c++/28266 Make sure this is expanded only once. */
5354 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5355 return GS_ERROR;
5357 if (init)
5358 gimplify_and_add (init, pre_p);
5360 /* If needed, push the cleanup for the temp. */
5361 if (TARGET_EXPR_CLEANUP (targ))
5363 if (CLEANUP_EH_ONLY (targ))
5364 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5365 CLEANUP_EH_ONLY (targ), pre_p);
5366 else
5367 cleanup = TARGET_EXPR_CLEANUP (targ);
5370 /* Add a clobber for the temporary going out of scope, like
5371 gimplify_bind_expr. */
5372 if (gimplify_ctxp->in_cleanup_point_expr
5373 && needs_to_live_in_memory (temp)
5374 && flag_stack_reuse == SR_ALL)
5376 tree clobber = build_constructor (TREE_TYPE (temp),
5377 NULL);
5378 TREE_THIS_VOLATILE (clobber) = true;
5379 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5380 if (cleanup)
5381 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5382 clobber);
5383 else
5384 cleanup = clobber;
5387 if (cleanup)
5388 gimple_push_cleanup (temp, cleanup, false, pre_p);
5390 /* Only expand this once. */
5391 TREE_OPERAND (targ, 3) = init;
5392 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5394 else
5395 /* We should have expanded this before. */
5396 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5398 *expr_p = temp;
5399 return GS_OK;
5402 /* Gimplification of expression trees. */
5404 /* Gimplify an expression which appears at statement context. The
5405 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5406 NULL, a new sequence is allocated.
5408 Return true if we actually added a statement to the queue. */
5410 bool
5411 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5413 gimple_seq_node last;
5415 last = gimple_seq_last (*seq_p);
5416 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5417 return last != gimple_seq_last (*seq_p);
5420 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5421 to CTX. If entries already exist, force them to be some flavor of private.
5422 If there is no enclosing parallel, do nothing. */
5424 void
5425 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5427 splay_tree_node n;
5429 if (decl == NULL || !DECL_P (decl))
5430 return;
5434 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5435 if (n != NULL)
5437 if (n->value & GOVD_SHARED)
5438 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5439 else if (n->value & GOVD_MAP)
5440 n->value |= GOVD_MAP_TO_ONLY;
5441 else
5442 return;
5444 else if (ctx->region_type == ORT_TARGET)
5445 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5446 else if (ctx->region_type != ORT_WORKSHARE
5447 && ctx->region_type != ORT_SIMD
5448 && ctx->region_type != ORT_TARGET_DATA)
5449 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5451 ctx = ctx->outer_context;
5453 while (ctx);
5456 /* Similarly for each of the type sizes of TYPE. */
5458 static void
5459 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5461 if (type == NULL || type == error_mark_node)
5462 return;
5463 type = TYPE_MAIN_VARIANT (type);
5465 if (ctx->privatized_types->add (type))
5466 return;
5468 switch (TREE_CODE (type))
5470 case INTEGER_TYPE:
5471 case ENUMERAL_TYPE:
5472 case BOOLEAN_TYPE:
5473 case REAL_TYPE:
5474 case FIXED_POINT_TYPE:
5475 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5476 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5477 break;
5479 case ARRAY_TYPE:
5480 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5481 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5482 break;
5484 case RECORD_TYPE:
5485 case UNION_TYPE:
5486 case QUAL_UNION_TYPE:
5488 tree field;
5489 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5490 if (TREE_CODE (field) == FIELD_DECL)
5492 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5493 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5496 break;
5498 case POINTER_TYPE:
5499 case REFERENCE_TYPE:
5500 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5501 break;
5503 default:
5504 break;
5507 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5508 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5509 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5512 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5514 static void
5515 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5517 splay_tree_node n;
5518 unsigned int nflags;
5519 tree t;
5521 if (error_operand_p (decl))
5522 return;
5524 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5525 there are constructors involved somewhere. */
5526 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5527 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5528 flags |= GOVD_SEEN;
5530 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5531 if (n != NULL && n->value != GOVD_ALIGNED)
5533 /* We shouldn't be re-adding the decl with the same data
5534 sharing class. */
5535 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5536 /* The only combination of data sharing classes we should see is
5537 FIRSTPRIVATE and LASTPRIVATE. */
5538 nflags = n->value | flags;
5539 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5540 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5541 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5542 n->value = nflags;
5543 return;
5546 /* When adding a variable-sized variable, we have to handle all sorts
5547 of additional bits of data: the pointer replacement variable, and
5548 the parameters of the type. */
5549 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5551 /* Add the pointer replacement variable as PRIVATE if the variable
5552 replacement is private, else FIRSTPRIVATE since we'll need the
5553 address of the original variable either for SHARED, or for the
5554 copy into or out of the context. */
5555 if (!(flags & GOVD_LOCAL))
5557 nflags = flags & GOVD_MAP
5558 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5559 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5560 nflags |= flags & GOVD_SEEN;
5561 t = DECL_VALUE_EXPR (decl);
5562 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5563 t = TREE_OPERAND (t, 0);
5564 gcc_assert (DECL_P (t));
5565 omp_add_variable (ctx, t, nflags);
5568 /* Add all of the variable and type parameters (which should have
5569 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5570 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5571 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5572 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5574 /* The variable-sized variable itself is never SHARED, only some form
5575 of PRIVATE. The sharing would take place via the pointer variable
5576 which we remapped above. */
5577 if (flags & GOVD_SHARED)
5578 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5579 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5581 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5582 alloca statement we generate for the variable, so make sure it
5583 is available. This isn't automatically needed for the SHARED
5584 case, since we won't be allocating local storage then.
5585 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5586 in this case omp_notice_variable will be called later
5587 on when it is gimplified. */
5588 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5589 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5590 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5592 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5593 && lang_hooks.decls.omp_privatize_by_reference (decl))
5595 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5597 /* Similar to the direct variable sized case above, we'll need the
5598 size of references being privatized. */
5599 if ((flags & GOVD_SHARED) == 0)
5601 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5602 if (TREE_CODE (t) != INTEGER_CST)
5603 omp_notice_variable (ctx, t, true);
5607 if (n != NULL)
5608 n->value |= flags;
5609 else
5610 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5613 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5614 This just prints out diagnostics about threadprivate variable uses
5615 in untied tasks. If DECL2 is non-NULL, prevent this warning
5616 on that variable. */
5618 static bool
5619 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5620 tree decl2)
5622 splay_tree_node n;
5623 struct gimplify_omp_ctx *octx;
5625 for (octx = ctx; octx; octx = octx->outer_context)
5626 if (octx->region_type == ORT_TARGET)
5628 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5629 if (n == NULL)
5631 error ("threadprivate variable %qE used in target region",
5632 DECL_NAME (decl));
5633 error_at (octx->location, "enclosing target region");
5634 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5636 if (decl2)
5637 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5640 if (ctx->region_type != ORT_UNTIED_TASK)
5641 return false;
5642 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5643 if (n == NULL)
5645 error ("threadprivate variable %qE used in untied task",
5646 DECL_NAME (decl));
5647 error_at (ctx->location, "enclosing task");
5648 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5650 if (decl2)
5651 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5652 return false;
5655 /* Record the fact that DECL was used within the OpenMP context CTX.
5656 IN_CODE is true when real code uses DECL, and false when we should
5657 merely emit default(none) errors. Return true if DECL is going to
5658 be remapped and thus DECL shouldn't be gimplified into its
5659 DECL_VALUE_EXPR (if any). */
5661 static bool
5662 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5664 splay_tree_node n;
5665 unsigned flags = in_code ? GOVD_SEEN : 0;
5666 bool ret = false, shared;
5668 if (error_operand_p (decl))
5669 return false;
5671 /* Threadprivate variables are predetermined. */
5672 if (is_global_var (decl))
5674 if (DECL_THREAD_LOCAL_P (decl))
5675 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5677 if (DECL_HAS_VALUE_EXPR_P (decl))
5679 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5681 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5682 return omp_notice_threadprivate_variable (ctx, decl, value);
5686 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5687 if (ctx->region_type == ORT_TARGET)
5689 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5690 if (n == NULL)
5692 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5694 error ("%qD referenced in target region does not have "
5695 "a mappable type", decl);
5696 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5698 else
5699 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5701 else
5703 /* If nothing changed, there's nothing left to do. */
5704 if ((n->value & flags) == flags)
5705 return ret;
5706 n->value |= flags;
5708 goto do_outer;
5711 if (n == NULL)
5713 enum omp_clause_default_kind default_kind, kind;
5714 struct gimplify_omp_ctx *octx;
5716 if (ctx->region_type == ORT_WORKSHARE
5717 || ctx->region_type == ORT_SIMD
5718 || ctx->region_type == ORT_TARGET_DATA)
5719 goto do_outer;
5721 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5722 remapped firstprivate instead of shared. To some extent this is
5723 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5724 default_kind = ctx->default_kind;
5725 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5726 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5727 default_kind = kind;
5729 switch (default_kind)
5731 case OMP_CLAUSE_DEFAULT_NONE:
5732 if ((ctx->region_type & ORT_PARALLEL) != 0)
5734 error ("%qE not specified in enclosing parallel",
5735 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5736 error_at (ctx->location, "enclosing parallel");
5738 else if ((ctx->region_type & ORT_TASK) != 0)
5740 error ("%qE not specified in enclosing task",
5741 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5742 error_at (ctx->location, "enclosing task");
5744 else if (ctx->region_type == ORT_TEAMS)
5746 error ("%qE not specified in enclosing teams construct",
5747 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5748 error_at (ctx->location, "enclosing teams construct");
5750 else
5751 gcc_unreachable ();
5752 /* FALLTHRU */
5753 case OMP_CLAUSE_DEFAULT_SHARED:
5754 flags |= GOVD_SHARED;
5755 break;
5756 case OMP_CLAUSE_DEFAULT_PRIVATE:
5757 flags |= GOVD_PRIVATE;
5758 break;
5759 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5760 flags |= GOVD_FIRSTPRIVATE;
5761 break;
5762 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5763 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5764 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5765 if (ctx->outer_context)
5766 omp_notice_variable (ctx->outer_context, decl, in_code);
5767 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5769 splay_tree_node n2;
5771 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5772 continue;
5773 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5774 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5776 flags |= GOVD_FIRSTPRIVATE;
5777 break;
5779 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5780 break;
5782 if (flags & GOVD_FIRSTPRIVATE)
5783 break;
5784 if (octx == NULL
5785 && (TREE_CODE (decl) == PARM_DECL
5786 || (!is_global_var (decl)
5787 && DECL_CONTEXT (decl) == current_function_decl)))
5789 flags |= GOVD_FIRSTPRIVATE;
5790 break;
5792 flags |= GOVD_SHARED;
5793 break;
5794 default:
5795 gcc_unreachable ();
5798 if ((flags & GOVD_PRIVATE)
5799 && lang_hooks.decls.omp_private_outer_ref (decl))
5800 flags |= GOVD_PRIVATE_OUTER_REF;
5802 omp_add_variable (ctx, decl, flags);
5804 shared = (flags & GOVD_SHARED) != 0;
5805 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5806 goto do_outer;
5809 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5810 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5811 && DECL_SIZE (decl)
5812 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5814 splay_tree_node n2;
5815 tree t = DECL_VALUE_EXPR (decl);
5816 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5817 t = TREE_OPERAND (t, 0);
5818 gcc_assert (DECL_P (t));
5819 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5820 n2->value |= GOVD_SEEN;
5823 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5824 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5826 /* If nothing changed, there's nothing left to do. */
5827 if ((n->value & flags) == flags)
5828 return ret;
5829 flags |= n->value;
5830 n->value = flags;
5832 do_outer:
5833 /* If the variable is private in the current context, then we don't
5834 need to propagate anything to an outer context. */
5835 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5836 return ret;
5837 if (ctx->outer_context
5838 && omp_notice_variable (ctx->outer_context, decl, in_code))
5839 return true;
5840 return ret;
5843 /* Verify that DECL is private within CTX. If there's specific information
5844 to the contrary in the innermost scope, generate an error. */
5846 static bool
5847 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5849 splay_tree_node n;
5851 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5852 if (n != NULL)
5854 if (n->value & GOVD_SHARED)
5856 if (ctx == gimplify_omp_ctxp)
5858 if (simd)
5859 error ("iteration variable %qE is predetermined linear",
5860 DECL_NAME (decl));
5861 else
5862 error ("iteration variable %qE should be private",
5863 DECL_NAME (decl));
5864 n->value = GOVD_PRIVATE;
5865 return true;
5867 else
5868 return false;
5870 else if ((n->value & GOVD_EXPLICIT) != 0
5871 && (ctx == gimplify_omp_ctxp
5872 || (ctx->region_type == ORT_COMBINED_PARALLEL
5873 && gimplify_omp_ctxp->outer_context == ctx)))
5875 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5876 error ("iteration variable %qE should not be firstprivate",
5877 DECL_NAME (decl));
5878 else if ((n->value & GOVD_REDUCTION) != 0)
5879 error ("iteration variable %qE should not be reduction",
5880 DECL_NAME (decl));
5881 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
5882 error ("iteration variable %qE should not be lastprivate",
5883 DECL_NAME (decl));
5884 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5885 error ("iteration variable %qE should not be private",
5886 DECL_NAME (decl));
5887 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
5888 error ("iteration variable %qE is predetermined linear",
5889 DECL_NAME (decl));
5891 return (ctx == gimplify_omp_ctxp
5892 || (ctx->region_type == ORT_COMBINED_PARALLEL
5893 && gimplify_omp_ctxp->outer_context == ctx));
5896 if (ctx->region_type != ORT_WORKSHARE
5897 && ctx->region_type != ORT_SIMD)
5898 return false;
5899 else if (ctx->outer_context)
5900 return omp_is_private (ctx->outer_context, decl, simd);
5901 return false;
5904 /* Return true if DECL is private within a parallel region
5905 that binds to the current construct's context or in parallel
5906 region's REDUCTION clause. */
5908 static bool
5909 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5911 splay_tree_node n;
5915 ctx = ctx->outer_context;
5916 if (ctx == NULL)
5917 return !(is_global_var (decl)
5918 /* References might be private, but might be shared too,
5919 when checking for copyprivate, assume they might be
5920 private, otherwise assume they might be shared. */
5921 || (!copyprivate
5922 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5924 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5925 continue;
5927 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5928 if (n != NULL)
5929 return (n->value & GOVD_SHARED) == 0;
5931 while (ctx->region_type == ORT_WORKSHARE
5932 || ctx->region_type == ORT_SIMD);
5933 return false;
5936 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5937 and previous omp contexts. */
5939 static void
5940 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5941 enum omp_region_type region_type)
5943 struct gimplify_omp_ctx *ctx, *outer_ctx;
5944 tree c;
5946 ctx = new_omp_context (region_type);
5947 outer_ctx = ctx->outer_context;
5949 while ((c = *list_p) != NULL)
5951 bool remove = false;
5952 bool notice_outer = true;
5953 const char *check_non_private = NULL;
5954 unsigned int flags;
5955 tree decl;
5957 switch (OMP_CLAUSE_CODE (c))
5959 case OMP_CLAUSE_PRIVATE:
5960 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5961 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5963 flags |= GOVD_PRIVATE_OUTER_REF;
5964 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5966 else
5967 notice_outer = false;
5968 goto do_add;
5969 case OMP_CLAUSE_SHARED:
5970 flags = GOVD_SHARED | GOVD_EXPLICIT;
5971 goto do_add;
5972 case OMP_CLAUSE_FIRSTPRIVATE:
5973 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5974 check_non_private = "firstprivate";
5975 goto do_add;
5976 case OMP_CLAUSE_LASTPRIVATE:
5977 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5978 check_non_private = "lastprivate";
5979 goto do_add;
5980 case OMP_CLAUSE_REDUCTION:
5981 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5982 check_non_private = "reduction";
5983 goto do_add;
5984 case OMP_CLAUSE_LINEAR:
5985 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5986 is_gimple_val, fb_rvalue) == GS_ERROR)
5988 remove = true;
5989 break;
5991 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5992 goto do_add;
5994 case OMP_CLAUSE_MAP:
5995 decl = OMP_CLAUSE_DECL (c);
5996 if (error_operand_p (decl))
5998 remove = true;
5999 break;
6001 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6002 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6003 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6004 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6005 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6007 remove = true;
6008 break;
6010 if (!DECL_P (decl))
6012 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6013 NULL, is_gimple_lvalue, fb_lvalue)
6014 == GS_ERROR)
6016 remove = true;
6017 break;
6019 break;
6021 flags = GOVD_MAP | GOVD_EXPLICIT;
6022 goto do_add;
6024 case OMP_CLAUSE_DEPEND:
6025 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6027 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6028 NULL, is_gimple_val, fb_rvalue);
6029 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6031 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6033 remove = true;
6034 break;
6036 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6037 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6038 is_gimple_val, fb_rvalue) == GS_ERROR)
6040 remove = true;
6041 break;
6043 break;
6045 case OMP_CLAUSE_TO:
6046 case OMP_CLAUSE_FROM:
6047 decl = OMP_CLAUSE_DECL (c);
6048 if (error_operand_p (decl))
6050 remove = true;
6051 break;
6053 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6054 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6055 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6056 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6057 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6059 remove = true;
6060 break;
6062 if (!DECL_P (decl))
6064 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6065 NULL, is_gimple_lvalue, fb_lvalue)
6066 == GS_ERROR)
6068 remove = true;
6069 break;
6071 break;
6073 goto do_notice;
6075 do_add:
6076 decl = OMP_CLAUSE_DECL (c);
6077 if (error_operand_p (decl))
6079 remove = true;
6080 break;
6082 omp_add_variable (ctx, decl, flags);
6083 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6084 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6086 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6087 GOVD_LOCAL | GOVD_SEEN);
6088 gimplify_omp_ctxp = ctx;
6089 push_gimplify_context ();
6091 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6092 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6094 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6095 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6096 pop_gimplify_context
6097 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6098 push_gimplify_context ();
6099 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6100 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6101 pop_gimplify_context
6102 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6103 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6104 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6106 gimplify_omp_ctxp = outer_ctx;
6108 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6109 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6111 gimplify_omp_ctxp = ctx;
6112 push_gimplify_context ();
6113 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6115 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6116 NULL, NULL);
6117 TREE_SIDE_EFFECTS (bind) = 1;
6118 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6119 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6121 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6122 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6123 pop_gimplify_context
6124 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6125 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6127 gimplify_omp_ctxp = outer_ctx;
6129 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6130 && OMP_CLAUSE_LINEAR_STMT (c))
6132 gimplify_omp_ctxp = ctx;
6133 push_gimplify_context ();
6134 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6136 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6137 NULL, NULL);
6138 TREE_SIDE_EFFECTS (bind) = 1;
6139 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6140 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6142 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6143 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6144 pop_gimplify_context
6145 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6146 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6148 gimplify_omp_ctxp = outer_ctx;
6150 if (notice_outer)
6151 goto do_notice;
6152 break;
6154 case OMP_CLAUSE_COPYIN:
6155 case OMP_CLAUSE_COPYPRIVATE:
6156 decl = OMP_CLAUSE_DECL (c);
6157 if (error_operand_p (decl))
6159 remove = true;
6160 break;
6162 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6163 && !remove
6164 && !omp_check_private (ctx, decl, true))
6166 remove = true;
6167 if (is_global_var (decl))
6169 if (DECL_THREAD_LOCAL_P (decl))
6170 remove = false;
6171 else if (DECL_HAS_VALUE_EXPR_P (decl))
6173 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6175 if (value
6176 && DECL_P (value)
6177 && DECL_THREAD_LOCAL_P (value))
6178 remove = false;
6181 if (remove)
6182 error_at (OMP_CLAUSE_LOCATION (c),
6183 "copyprivate variable %qE is not threadprivate"
6184 " or private in outer context", DECL_NAME (decl));
6186 do_notice:
6187 if (outer_ctx)
6188 omp_notice_variable (outer_ctx, decl, true);
6189 if (check_non_private
6190 && region_type == ORT_WORKSHARE
6191 && omp_check_private (ctx, decl, false))
6193 error ("%s variable %qE is private in outer context",
6194 check_non_private, DECL_NAME (decl));
6195 remove = true;
6197 break;
6199 case OMP_CLAUSE_FINAL:
6200 case OMP_CLAUSE_IF:
6201 OMP_CLAUSE_OPERAND (c, 0)
6202 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6203 /* Fall through. */
6205 case OMP_CLAUSE_SCHEDULE:
6206 case OMP_CLAUSE_NUM_THREADS:
6207 case OMP_CLAUSE_NUM_TEAMS:
6208 case OMP_CLAUSE_THREAD_LIMIT:
6209 case OMP_CLAUSE_DIST_SCHEDULE:
6210 case OMP_CLAUSE_DEVICE:
6211 case OMP_CLAUSE__CILK_FOR_COUNT_:
6212 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6213 is_gimple_val, fb_rvalue) == GS_ERROR)
6214 remove = true;
6215 break;
6217 case OMP_CLAUSE_NOWAIT:
6218 case OMP_CLAUSE_ORDERED:
6219 case OMP_CLAUSE_UNTIED:
6220 case OMP_CLAUSE_COLLAPSE:
6221 case OMP_CLAUSE_MERGEABLE:
6222 case OMP_CLAUSE_PROC_BIND:
6223 case OMP_CLAUSE_SAFELEN:
6224 break;
6226 case OMP_CLAUSE_ALIGNED:
6227 decl = OMP_CLAUSE_DECL (c);
6228 if (error_operand_p (decl))
6230 remove = true;
6231 break;
6233 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6234 is_gimple_val, fb_rvalue) == GS_ERROR)
6236 remove = true;
6237 break;
6239 if (!is_global_var (decl)
6240 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6241 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6242 break;
6244 case OMP_CLAUSE_DEFAULT:
6245 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6246 break;
6248 default:
6249 gcc_unreachable ();
6252 if (remove)
6253 *list_p = OMP_CLAUSE_CHAIN (c);
6254 else
6255 list_p = &OMP_CLAUSE_CHAIN (c);
6258 gimplify_omp_ctxp = ctx;
6261 struct gimplify_adjust_omp_clauses_data
6263 tree *list_p;
6264 gimple_seq *pre_p;
6267 /* For all variables that were not actually used within the context,
6268 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6270 static int
6271 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6273 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6274 gimple_seq *pre_p
6275 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6276 tree decl = (tree) n->key;
6277 unsigned flags = n->value;
6278 enum omp_clause_code code;
6279 tree clause;
6280 bool private_debug;
6282 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6283 return 0;
6284 if ((flags & GOVD_SEEN) == 0)
6285 return 0;
6286 if (flags & GOVD_DEBUG_PRIVATE)
6288 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6289 private_debug = true;
6291 else if (flags & GOVD_MAP)
6292 private_debug = false;
6293 else
6294 private_debug
6295 = lang_hooks.decls.omp_private_debug_clause (decl,
6296 !!(flags & GOVD_SHARED));
6297 if (private_debug)
6298 code = OMP_CLAUSE_PRIVATE;
6299 else if (flags & GOVD_MAP)
6300 code = OMP_CLAUSE_MAP;
6301 else if (flags & GOVD_SHARED)
6303 if (is_global_var (decl))
6305 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6306 while (ctx != NULL)
6308 splay_tree_node on
6309 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6310 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6311 | GOVD_PRIVATE | GOVD_REDUCTION
6312 | GOVD_LINEAR | GOVD_MAP)) != 0)
6313 break;
6314 ctx = ctx->outer_context;
6316 if (ctx == NULL)
6317 return 0;
6319 code = OMP_CLAUSE_SHARED;
6321 else if (flags & GOVD_PRIVATE)
6322 code = OMP_CLAUSE_PRIVATE;
6323 else if (flags & GOVD_FIRSTPRIVATE)
6324 code = OMP_CLAUSE_FIRSTPRIVATE;
6325 else if (flags & GOVD_LASTPRIVATE)
6326 code = OMP_CLAUSE_LASTPRIVATE;
6327 else if (flags & GOVD_ALIGNED)
6328 return 0;
6329 else
6330 gcc_unreachable ();
6332 clause = build_omp_clause (input_location, code);
6333 OMP_CLAUSE_DECL (clause) = decl;
6334 OMP_CLAUSE_CHAIN (clause) = *list_p;
6335 if (private_debug)
6336 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6337 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6338 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6339 else if (code == OMP_CLAUSE_MAP)
6341 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6342 ? OMP_CLAUSE_MAP_TO
6343 : OMP_CLAUSE_MAP_TOFROM;
6344 if (DECL_SIZE (decl)
6345 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6347 tree decl2 = DECL_VALUE_EXPR (decl);
6348 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6349 decl2 = TREE_OPERAND (decl2, 0);
6350 gcc_assert (DECL_P (decl2));
6351 tree mem = build_simple_mem_ref (decl2);
6352 OMP_CLAUSE_DECL (clause) = mem;
6353 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6354 if (gimplify_omp_ctxp->outer_context)
6356 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6357 omp_notice_variable (ctx, decl2, true);
6358 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6360 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6361 OMP_CLAUSE_MAP);
6362 OMP_CLAUSE_DECL (nc) = decl;
6363 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6364 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6365 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6366 OMP_CLAUSE_CHAIN (clause) = nc;
6368 else
6369 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6371 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6373 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6374 OMP_CLAUSE_DECL (nc) = decl;
6375 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6376 OMP_CLAUSE_CHAIN (nc) = *list_p;
6377 OMP_CLAUSE_CHAIN (clause) = nc;
6378 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6379 gimplify_omp_ctxp = ctx->outer_context;
6380 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6381 gimplify_omp_ctxp = ctx;
6383 *list_p = clause;
6384 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6385 gimplify_omp_ctxp = ctx->outer_context;
6386 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6387 gimplify_omp_ctxp = ctx;
6388 return 0;
6391 static void
6392 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6394 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6395 tree c, decl;
6397 while ((c = *list_p) != NULL)
6399 splay_tree_node n;
6400 bool remove = false;
6402 switch (OMP_CLAUSE_CODE (c))
6404 case OMP_CLAUSE_PRIVATE:
6405 case OMP_CLAUSE_SHARED:
6406 case OMP_CLAUSE_FIRSTPRIVATE:
6407 case OMP_CLAUSE_LINEAR:
6408 decl = OMP_CLAUSE_DECL (c);
6409 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6410 remove = !(n->value & GOVD_SEEN);
6411 if (! remove)
6413 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6414 if ((n->value & GOVD_DEBUG_PRIVATE)
6415 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6417 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6418 || ((n->value & GOVD_DATA_SHARE_CLASS)
6419 == GOVD_PRIVATE));
6420 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6421 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6423 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6424 && ctx->outer_context
6425 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6426 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6428 if (ctx->outer_context->combined_loop
6429 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6431 n = splay_tree_lookup (ctx->outer_context->variables,
6432 (splay_tree_key) decl);
6433 if (n == NULL
6434 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6436 int flags = GOVD_FIRSTPRIVATE;
6437 /* #pragma omp distribute does not allow
6438 lastprivate clause. */
6439 if (!ctx->outer_context->distribute)
6440 flags |= GOVD_LASTPRIVATE;
6441 if (n == NULL)
6442 omp_add_variable (ctx->outer_context, decl,
6443 flags | GOVD_SEEN);
6444 else
6445 n->value |= flags | GOVD_SEEN;
6448 else if (!is_global_var (decl))
6449 omp_notice_variable (ctx->outer_context, decl, true);
6452 break;
6454 case OMP_CLAUSE_LASTPRIVATE:
6455 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6456 accurately reflect the presence of a FIRSTPRIVATE clause. */
6457 decl = OMP_CLAUSE_DECL (c);
6458 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6459 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6460 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6461 break;
6463 case OMP_CLAUSE_ALIGNED:
6464 decl = OMP_CLAUSE_DECL (c);
6465 if (!is_global_var (decl))
6467 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6468 remove = n == NULL || !(n->value & GOVD_SEEN);
6469 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6471 struct gimplify_omp_ctx *octx;
6472 if (n != NULL
6473 && (n->value & (GOVD_DATA_SHARE_CLASS
6474 & ~GOVD_FIRSTPRIVATE)))
6475 remove = true;
6476 else
6477 for (octx = ctx->outer_context; octx;
6478 octx = octx->outer_context)
6480 n = splay_tree_lookup (octx->variables,
6481 (splay_tree_key) decl);
6482 if (n == NULL)
6483 continue;
6484 if (n->value & GOVD_LOCAL)
6485 break;
6486 /* We have to avoid assigning a shared variable
6487 to itself when trying to add
6488 __builtin_assume_aligned. */
6489 if (n->value & GOVD_SHARED)
6491 remove = true;
6492 break;
6497 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6499 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6500 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6501 remove = true;
6503 break;
6505 case OMP_CLAUSE_MAP:
6506 decl = OMP_CLAUSE_DECL (c);
6507 if (!DECL_P (decl))
6508 break;
6509 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6510 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6511 remove = true;
6512 else if (DECL_SIZE (decl)
6513 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6514 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6516 tree decl2 = DECL_VALUE_EXPR (decl);
6517 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6518 decl2 = TREE_OPERAND (decl2, 0);
6519 gcc_assert (DECL_P (decl2));
6520 tree mem = build_simple_mem_ref (decl2);
6521 OMP_CLAUSE_DECL (c) = mem;
6522 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6523 if (ctx->outer_context)
6525 omp_notice_variable (ctx->outer_context, decl2, true);
6526 omp_notice_variable (ctx->outer_context,
6527 OMP_CLAUSE_SIZE (c), true);
6529 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6530 OMP_CLAUSE_MAP);
6531 OMP_CLAUSE_DECL (nc) = decl;
6532 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6533 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6534 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6535 OMP_CLAUSE_CHAIN (c) = nc;
6536 c = nc;
6538 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6539 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6540 break;
6542 case OMP_CLAUSE_TO:
6543 case OMP_CLAUSE_FROM:
6544 decl = OMP_CLAUSE_DECL (c);
6545 if (!DECL_P (decl))
6546 break;
6547 if (DECL_SIZE (decl)
6548 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6550 tree decl2 = DECL_VALUE_EXPR (decl);
6551 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6552 decl2 = TREE_OPERAND (decl2, 0);
6553 gcc_assert (DECL_P (decl2));
6554 tree mem = build_simple_mem_ref (decl2);
6555 OMP_CLAUSE_DECL (c) = mem;
6556 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6557 if (ctx->outer_context)
6559 omp_notice_variable (ctx->outer_context, decl2, true);
6560 omp_notice_variable (ctx->outer_context,
6561 OMP_CLAUSE_SIZE (c), true);
6564 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6565 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6566 break;
6568 case OMP_CLAUSE_REDUCTION:
6569 case OMP_CLAUSE_COPYIN:
6570 case OMP_CLAUSE_COPYPRIVATE:
6571 case OMP_CLAUSE_IF:
6572 case OMP_CLAUSE_NUM_THREADS:
6573 case OMP_CLAUSE_NUM_TEAMS:
6574 case OMP_CLAUSE_THREAD_LIMIT:
6575 case OMP_CLAUSE_DIST_SCHEDULE:
6576 case OMP_CLAUSE_DEVICE:
6577 case OMP_CLAUSE_SCHEDULE:
6578 case OMP_CLAUSE_NOWAIT:
6579 case OMP_CLAUSE_ORDERED:
6580 case OMP_CLAUSE_DEFAULT:
6581 case OMP_CLAUSE_UNTIED:
6582 case OMP_CLAUSE_COLLAPSE:
6583 case OMP_CLAUSE_FINAL:
6584 case OMP_CLAUSE_MERGEABLE:
6585 case OMP_CLAUSE_PROC_BIND:
6586 case OMP_CLAUSE_SAFELEN:
6587 case OMP_CLAUSE_DEPEND:
6588 case OMP_CLAUSE__CILK_FOR_COUNT_:
6589 break;
6591 default:
6592 gcc_unreachable ();
6595 if (remove)
6596 *list_p = OMP_CLAUSE_CHAIN (c);
6597 else
6598 list_p = &OMP_CLAUSE_CHAIN (c);
6601 /* Add in any implicit data sharing. */
6602 struct gimplify_adjust_omp_clauses_data data;
6603 data.list_p = list_p;
6604 data.pre_p = pre_p;
6605 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6607 gimplify_omp_ctxp = ctx->outer_context;
6608 delete_omp_context (ctx);
6611 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6612 gimplification of the body, as well as scanning the body for used
6613 variables. We need to do this scan now, because variable-sized
6614 decls will be decomposed during gimplification. */
6616 static void
6617 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6619 tree expr = *expr_p;
6620 gimple g;
6621 gimple_seq body = NULL;
6623 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6624 OMP_PARALLEL_COMBINED (expr)
6625 ? ORT_COMBINED_PARALLEL
6626 : ORT_PARALLEL);
6628 push_gimplify_context ();
6630 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6631 if (gimple_code (g) == GIMPLE_BIND)
6632 pop_gimplify_context (g);
6633 else
6634 pop_gimplify_context (NULL);
6636 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6638 g = gimple_build_omp_parallel (body,
6639 OMP_PARALLEL_CLAUSES (expr),
6640 NULL_TREE, NULL_TREE);
6641 if (OMP_PARALLEL_COMBINED (expr))
6642 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6643 gimplify_seq_add_stmt (pre_p, g);
6644 *expr_p = NULL_TREE;
6647 /* Gimplify the contents of an OMP_TASK statement. This involves
6648 gimplification of the body, as well as scanning the body for used
6649 variables. We need to do this scan now, because variable-sized
6650 decls will be decomposed during gimplification. */
6652 static void
6653 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6655 tree expr = *expr_p;
6656 gimple g;
6657 gimple_seq body = NULL;
6659 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6660 find_omp_clause (OMP_TASK_CLAUSES (expr),
6661 OMP_CLAUSE_UNTIED)
6662 ? ORT_UNTIED_TASK : ORT_TASK);
6664 push_gimplify_context ();
6666 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6667 if (gimple_code (g) == GIMPLE_BIND)
6668 pop_gimplify_context (g);
6669 else
6670 pop_gimplify_context (NULL);
6672 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6674 g = gimple_build_omp_task (body,
6675 OMP_TASK_CLAUSES (expr),
6676 NULL_TREE, NULL_TREE,
6677 NULL_TREE, NULL_TREE, NULL_TREE);
6678 gimplify_seq_add_stmt (pre_p, g);
6679 *expr_p = NULL_TREE;
6682 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6683 with non-NULL OMP_FOR_INIT. */
6685 static tree
6686 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6688 *walk_subtrees = 0;
6689 switch (TREE_CODE (*tp))
6691 case OMP_FOR:
6692 *walk_subtrees = 1;
6693 /* FALLTHRU */
6694 case OMP_SIMD:
6695 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6696 return *tp;
6697 break;
6698 case BIND_EXPR:
6699 case STATEMENT_LIST:
6700 case OMP_PARALLEL:
6701 *walk_subtrees = 1;
6702 break;
6703 default:
6704 break;
6706 return NULL_TREE;
6709 /* Gimplify the gross structure of an OMP_FOR statement. */
6711 static enum gimplify_status
6712 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6714 tree for_stmt, orig_for_stmt, decl, var, t;
6715 enum gimplify_status ret = GS_ALL_DONE;
6716 enum gimplify_status tret;
6717 gimple gfor;
6718 gimple_seq for_body, for_pre_body;
6719 int i;
6720 bool simd;
6721 bitmap has_decl_expr = NULL;
6723 orig_for_stmt = for_stmt = *expr_p;
6725 simd = (TREE_CODE (for_stmt) == OMP_SIMD
6726 || TREE_CODE (for_stmt) == CILK_SIMD);
6727 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6728 simd ? ORT_SIMD : ORT_WORKSHARE);
6729 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
6730 gimplify_omp_ctxp->distribute = true;
6732 /* Handle OMP_FOR_INIT. */
6733 for_pre_body = NULL;
6734 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6736 has_decl_expr = BITMAP_ALLOC (NULL);
6737 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6738 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6739 == VAR_DECL)
6741 t = OMP_FOR_PRE_BODY (for_stmt);
6742 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6744 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6746 tree_stmt_iterator si;
6747 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6748 tsi_next (&si))
6750 t = tsi_stmt (si);
6751 if (TREE_CODE (t) == DECL_EXPR
6752 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6753 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6757 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6758 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6760 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6762 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6763 NULL, NULL);
6764 gcc_assert (for_stmt != NULL_TREE);
6765 gimplify_omp_ctxp->combined_loop = true;
6768 for_body = NULL;
6769 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6770 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6771 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6772 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6773 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6775 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6776 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6777 decl = TREE_OPERAND (t, 0);
6778 gcc_assert (DECL_P (decl));
6779 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6780 || POINTER_TYPE_P (TREE_TYPE (decl)));
6782 /* Make sure the iteration variable is private. */
6783 tree c = NULL_TREE;
6784 tree c2 = NULL_TREE;
6785 if (orig_for_stmt != for_stmt)
6786 /* Do this only on innermost construct for combined ones. */;
6787 else if (simd)
6789 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6790 (splay_tree_key)decl);
6791 omp_is_private (gimplify_omp_ctxp, decl,
6792 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6793 != 1));
6794 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6795 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6796 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6798 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6799 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6800 if (has_decl_expr
6801 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6802 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6803 OMP_CLAUSE_DECL (c) = decl;
6804 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6805 OMP_FOR_CLAUSES (for_stmt) = c;
6806 omp_add_variable (gimplify_omp_ctxp, decl,
6807 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6809 else
6811 bool lastprivate
6812 = (!has_decl_expr
6813 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6814 if (lastprivate
6815 && gimplify_omp_ctxp->outer_context
6816 && gimplify_omp_ctxp->outer_context->region_type
6817 == ORT_WORKSHARE
6818 && gimplify_omp_ctxp->outer_context->combined_loop
6819 && !gimplify_omp_ctxp->outer_context->distribute)
6821 struct gimplify_omp_ctx *outer
6822 = gimplify_omp_ctxp->outer_context;
6823 n = splay_tree_lookup (outer->variables,
6824 (splay_tree_key) decl);
6825 if (n != NULL
6826 && (n->value & GOVD_DATA_SHARE_CLASS) == GOVD_LOCAL)
6827 lastprivate = false;
6828 else if (omp_check_private (outer, decl, false))
6829 error ("lastprivate variable %qE is private in outer "
6830 "context", DECL_NAME (decl));
6831 else
6833 omp_add_variable (outer, decl,
6834 GOVD_LASTPRIVATE | GOVD_SEEN);
6835 if (outer->outer_context)
6836 omp_notice_variable (outer->outer_context, decl, true);
6839 c = build_omp_clause (input_location,
6840 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6841 : OMP_CLAUSE_PRIVATE);
6842 OMP_CLAUSE_DECL (c) = decl;
6843 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6844 OMP_FOR_CLAUSES (for_stmt) = c;
6845 omp_add_variable (gimplify_omp_ctxp, decl,
6846 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6847 | GOVD_EXPLICIT | GOVD_SEEN);
6848 c = NULL_TREE;
6851 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
6852 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6853 else
6854 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6856 /* If DECL is not a gimple register, create a temporary variable to act
6857 as an iteration counter. This is valid, since DECL cannot be
6858 modified in the body of the loop. Similarly for any iteration vars
6859 in simd with collapse > 1 where the iterator vars must be
6860 lastprivate. */
6861 if (orig_for_stmt != for_stmt)
6862 var = decl;
6863 else if (!is_gimple_reg (decl)
6864 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
6866 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6867 TREE_OPERAND (t, 0) = var;
6869 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6871 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6873 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6874 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
6875 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
6876 OMP_CLAUSE_DECL (c2) = var;
6877 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
6878 OMP_FOR_CLAUSES (for_stmt) = c2;
6879 omp_add_variable (gimplify_omp_ctxp, var,
6880 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6881 if (c == NULL_TREE)
6883 c = c2;
6884 c2 = NULL_TREE;
6887 else
6888 omp_add_variable (gimplify_omp_ctxp, var,
6889 GOVD_PRIVATE | GOVD_SEEN);
6891 else
6892 var = decl;
6894 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6895 is_gimple_val, fb_rvalue);
6896 ret = MIN (ret, tret);
6897 if (ret == GS_ERROR)
6898 return ret;
6900 /* Handle OMP_FOR_COND. */
6901 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6902 gcc_assert (COMPARISON_CLASS_P (t));
6903 gcc_assert (TREE_OPERAND (t, 0) == decl);
6905 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6906 is_gimple_val, fb_rvalue);
6907 ret = MIN (ret, tret);
6909 /* Handle OMP_FOR_INCR. */
6910 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6911 switch (TREE_CODE (t))
6913 case PREINCREMENT_EXPR:
6914 case POSTINCREMENT_EXPR:
6916 tree decl = TREE_OPERAND (t, 0);
6917 /* c_omp_for_incr_canonicalize_ptr() should have been
6918 called to massage things appropriately. */
6919 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6921 if (orig_for_stmt != for_stmt)
6922 break;
6923 t = build_int_cst (TREE_TYPE (decl), 1);
6924 if (c)
6925 OMP_CLAUSE_LINEAR_STEP (c) = t;
6926 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6927 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6928 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6929 break;
6932 case PREDECREMENT_EXPR:
6933 case POSTDECREMENT_EXPR:
6934 /* c_omp_for_incr_canonicalize_ptr() should have been
6935 called to massage things appropriately. */
6936 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6937 if (orig_for_stmt != for_stmt)
6938 break;
6939 t = build_int_cst (TREE_TYPE (decl), -1);
6940 if (c)
6941 OMP_CLAUSE_LINEAR_STEP (c) = t;
6942 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6943 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6944 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6945 break;
6947 case MODIFY_EXPR:
6948 gcc_assert (TREE_OPERAND (t, 0) == decl);
6949 TREE_OPERAND (t, 0) = var;
6951 t = TREE_OPERAND (t, 1);
6952 switch (TREE_CODE (t))
6954 case PLUS_EXPR:
6955 if (TREE_OPERAND (t, 1) == decl)
6957 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6958 TREE_OPERAND (t, 0) = var;
6959 break;
6962 /* Fallthru. */
6963 case MINUS_EXPR:
6964 case POINTER_PLUS_EXPR:
6965 gcc_assert (TREE_OPERAND (t, 0) == decl);
6966 TREE_OPERAND (t, 0) = var;
6967 break;
6968 default:
6969 gcc_unreachable ();
6972 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6973 is_gimple_val, fb_rvalue);
6974 ret = MIN (ret, tret);
6975 if (c)
6977 tree step = TREE_OPERAND (t, 1);
6978 tree stept = TREE_TYPE (decl);
6979 if (POINTER_TYPE_P (stept))
6980 stept = sizetype;
6981 step = fold_convert (stept, step);
6982 if (TREE_CODE (t) == MINUS_EXPR)
6983 step = fold_build1 (NEGATE_EXPR, stept, step);
6984 OMP_CLAUSE_LINEAR_STEP (c) = step;
6985 if (step != TREE_OPERAND (t, 1))
6987 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6988 &for_pre_body, NULL,
6989 is_gimple_val, fb_rvalue);
6990 ret = MIN (ret, tret);
6993 break;
6995 default:
6996 gcc_unreachable ();
6999 if (c2)
7001 gcc_assert (c);
7002 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7005 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7006 && orig_for_stmt == for_stmt)
7008 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7009 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7010 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7011 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7012 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7013 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7014 && OMP_CLAUSE_DECL (c) == decl)
7016 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7017 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7018 gcc_assert (TREE_OPERAND (t, 0) == var);
7019 t = TREE_OPERAND (t, 1);
7020 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7021 || TREE_CODE (t) == MINUS_EXPR
7022 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7023 gcc_assert (TREE_OPERAND (t, 0) == var);
7024 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7025 TREE_OPERAND (t, 1));
7026 gimple_seq *seq;
7027 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7028 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7029 else
7030 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7031 gimplify_assign (decl, t, seq);
7036 BITMAP_FREE (has_decl_expr);
7038 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7040 if (orig_for_stmt != for_stmt)
7041 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7043 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7044 decl = TREE_OPERAND (t, 0);
7045 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7046 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7047 TREE_OPERAND (t, 0) = var;
7048 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7049 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7050 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7053 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7055 int kind;
7056 switch (TREE_CODE (orig_for_stmt))
7058 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7059 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7060 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7061 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7062 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7063 default:
7064 gcc_unreachable ();
7066 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7067 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7068 for_pre_body);
7069 if (orig_for_stmt != for_stmt)
7070 gimple_omp_for_set_combined_p (gfor, true);
7071 if (gimplify_omp_ctxp
7072 && (gimplify_omp_ctxp->combined_loop
7073 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7074 && gimplify_omp_ctxp->outer_context
7075 && gimplify_omp_ctxp->outer_context->combined_loop)))
7077 gimple_omp_for_set_combined_into_p (gfor, true);
7078 if (gimplify_omp_ctxp->combined_loop)
7079 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7080 else
7081 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7084 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7086 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7087 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7088 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7089 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7090 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7091 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7092 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7093 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7096 gimplify_seq_add_stmt (pre_p, gfor);
7097 if (ret != GS_ALL_DONE)
7098 return GS_ERROR;
7099 *expr_p = NULL_TREE;
7100 return GS_ALL_DONE;
7103 /* Gimplify the gross structure of other OpenMP constructs.
7104 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7105 and OMP_TEAMS. */
7107 static void
7108 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7110 tree expr = *expr_p;
7111 gimple stmt;
7112 gimple_seq body = NULL;
7113 enum omp_region_type ort = ORT_WORKSHARE;
7115 switch (TREE_CODE (expr))
7117 case OMP_SECTIONS:
7118 case OMP_SINGLE:
7119 break;
7120 case OMP_TARGET:
7121 ort = ORT_TARGET;
7122 break;
7123 case OMP_TARGET_DATA:
7124 ort = ORT_TARGET_DATA;
7125 break;
7126 case OMP_TEAMS:
7127 ort = ORT_TEAMS;
7128 break;
7129 default:
7130 gcc_unreachable ();
7132 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7133 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7135 push_gimplify_context ();
7136 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7137 if (gimple_code (g) == GIMPLE_BIND)
7138 pop_gimplify_context (g);
7139 else
7140 pop_gimplify_context (NULL);
7141 if (ort == ORT_TARGET_DATA)
7143 gimple_seq cleanup = NULL;
7144 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7145 g = gimple_build_call (fn, 0);
7146 gimple_seq_add_stmt (&cleanup, g);
7147 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7148 body = NULL;
7149 gimple_seq_add_stmt (&body, g);
7152 else
7153 gimplify_and_add (OMP_BODY (expr), &body);
7154 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7156 switch (TREE_CODE (expr))
7158 case OMP_SECTIONS:
7159 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7160 break;
7161 case OMP_SINGLE:
7162 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7163 break;
7164 case OMP_TARGET:
7165 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7166 OMP_CLAUSES (expr));
7167 break;
7168 case OMP_TARGET_DATA:
7169 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7170 OMP_CLAUSES (expr));
7171 break;
7172 case OMP_TEAMS:
7173 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7174 break;
7175 default:
7176 gcc_unreachable ();
7179 gimplify_seq_add_stmt (pre_p, stmt);
7180 *expr_p = NULL_TREE;
7183 /* Gimplify the gross structure of OpenMP target update construct. */
7185 static void
7186 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7188 tree expr = *expr_p;
7189 gimple stmt;
7191 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7192 ORT_WORKSHARE);
7193 gimplify_adjust_omp_clauses (pre_p, &OMP_TARGET_UPDATE_CLAUSES (expr));
7194 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7195 OMP_TARGET_UPDATE_CLAUSES (expr));
7197 gimplify_seq_add_stmt (pre_p, stmt);
7198 *expr_p = NULL_TREE;
7201 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7202 stabilized the lhs of the atomic operation as *ADDR. Return true if
7203 EXPR is this stabilized form. */
7205 static bool
7206 goa_lhs_expr_p (tree expr, tree addr)
7208 /* Also include casts to other type variants. The C front end is fond
7209 of adding these for e.g. volatile variables. This is like
7210 STRIP_TYPE_NOPS but includes the main variant lookup. */
7211 STRIP_USELESS_TYPE_CONVERSION (expr);
7213 if (TREE_CODE (expr) == INDIRECT_REF)
7215 expr = TREE_OPERAND (expr, 0);
7216 while (expr != addr
7217 && (CONVERT_EXPR_P (expr)
7218 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7219 && TREE_CODE (expr) == TREE_CODE (addr)
7220 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7222 expr = TREE_OPERAND (expr, 0);
7223 addr = TREE_OPERAND (addr, 0);
7225 if (expr == addr)
7226 return true;
7227 return (TREE_CODE (addr) == ADDR_EXPR
7228 && TREE_CODE (expr) == ADDR_EXPR
7229 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7231 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7232 return true;
7233 return false;
7236 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7237 expression does not involve the lhs, evaluate it into a temporary.
7238 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7239 or -1 if an error was encountered. */
7241 static int
7242 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7243 tree lhs_var)
7245 tree expr = *expr_p;
7246 int saw_lhs;
7248 if (goa_lhs_expr_p (expr, lhs_addr))
7250 *expr_p = lhs_var;
7251 return 1;
7253 if (is_gimple_val (expr))
7254 return 0;
7256 saw_lhs = 0;
7257 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7259 case tcc_binary:
7260 case tcc_comparison:
7261 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7262 lhs_var);
7263 case tcc_unary:
7264 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7265 lhs_var);
7266 break;
7267 case tcc_expression:
7268 switch (TREE_CODE (expr))
7270 case TRUTH_ANDIF_EXPR:
7271 case TRUTH_ORIF_EXPR:
7272 case TRUTH_AND_EXPR:
7273 case TRUTH_OR_EXPR:
7274 case TRUTH_XOR_EXPR:
7275 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7276 lhs_addr, lhs_var);
7277 case TRUTH_NOT_EXPR:
7278 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7279 lhs_addr, lhs_var);
7280 break;
7281 case COMPOUND_EXPR:
7282 /* Break out any preevaluations from cp_build_modify_expr. */
7283 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7284 expr = TREE_OPERAND (expr, 1))
7285 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7286 *expr_p = expr;
7287 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7288 default:
7289 break;
7291 break;
7292 default:
7293 break;
7296 if (saw_lhs == 0)
7298 enum gimplify_status gs;
7299 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7300 if (gs != GS_ALL_DONE)
7301 saw_lhs = -1;
7304 return saw_lhs;
7307 /* Gimplify an OMP_ATOMIC statement. */
7309 static enum gimplify_status
7310 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7312 tree addr = TREE_OPERAND (*expr_p, 0);
7313 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7314 ? NULL : TREE_OPERAND (*expr_p, 1);
7315 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7316 tree tmp_load;
7317 gimple loadstmt, storestmt;
7319 tmp_load = create_tmp_reg (type, NULL);
7320 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7321 return GS_ERROR;
7323 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7324 != GS_ALL_DONE)
7325 return GS_ERROR;
7327 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7328 gimplify_seq_add_stmt (pre_p, loadstmt);
7329 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7330 != GS_ALL_DONE)
7331 return GS_ERROR;
7333 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7334 rhs = tmp_load;
7335 storestmt = gimple_build_omp_atomic_store (rhs);
7336 gimplify_seq_add_stmt (pre_p, storestmt);
7337 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7339 gimple_omp_atomic_set_seq_cst (loadstmt);
7340 gimple_omp_atomic_set_seq_cst (storestmt);
7342 switch (TREE_CODE (*expr_p))
7344 case OMP_ATOMIC_READ:
7345 case OMP_ATOMIC_CAPTURE_OLD:
7346 *expr_p = tmp_load;
7347 gimple_omp_atomic_set_need_value (loadstmt);
7348 break;
7349 case OMP_ATOMIC_CAPTURE_NEW:
7350 *expr_p = rhs;
7351 gimple_omp_atomic_set_need_value (storestmt);
7352 break;
7353 default:
7354 *expr_p = NULL;
7355 break;
7358 return GS_ALL_DONE;
7361 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7362 body, and adding some EH bits. */
7364 static enum gimplify_status
7365 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7367 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7368 gimple g;
7369 gimple_seq body = NULL;
7370 int subcode = 0;
7372 /* Wrap the transaction body in a BIND_EXPR so we have a context
7373 where to put decls for OpenMP. */
7374 if (TREE_CODE (tbody) != BIND_EXPR)
7376 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7377 TREE_SIDE_EFFECTS (bind) = 1;
7378 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7379 TRANSACTION_EXPR_BODY (expr) = bind;
7382 push_gimplify_context ();
7383 temp = voidify_wrapper_expr (*expr_p, NULL);
7385 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7386 pop_gimplify_context (g);
7388 g = gimple_build_transaction (body, NULL);
7389 if (TRANSACTION_EXPR_OUTER (expr))
7390 subcode = GTMA_IS_OUTER;
7391 else if (TRANSACTION_EXPR_RELAXED (expr))
7392 subcode = GTMA_IS_RELAXED;
7393 gimple_transaction_set_subcode (g, subcode);
7395 gimplify_seq_add_stmt (pre_p, g);
7397 if (temp)
7399 *expr_p = temp;
7400 return GS_OK;
7403 *expr_p = NULL_TREE;
7404 return GS_ALL_DONE;
7407 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7408 expression produces a value to be used as an operand inside a GIMPLE
7409 statement, the value will be stored back in *EXPR_P. This value will
7410 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7411 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7412 emitted in PRE_P and POST_P.
7414 Additionally, this process may overwrite parts of the input
7415 expression during gimplification. Ideally, it should be
7416 possible to do non-destructive gimplification.
7418 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7419 the expression needs to evaluate to a value to be used as
7420 an operand in a GIMPLE statement, this value will be stored in
7421 *EXPR_P on exit. This happens when the caller specifies one
7422 of fb_lvalue or fb_rvalue fallback flags.
7424 PRE_P will contain the sequence of GIMPLE statements corresponding
7425 to the evaluation of EXPR and all the side-effects that must
7426 be executed before the main expression. On exit, the last
7427 statement of PRE_P is the core statement being gimplified. For
7428 instance, when gimplifying 'if (++a)' the last statement in
7429 PRE_P will be 'if (t.1)' where t.1 is the result of
7430 pre-incrementing 'a'.
7432 POST_P will contain the sequence of GIMPLE statements corresponding
7433 to the evaluation of all the side-effects that must be executed
7434 after the main expression. If this is NULL, the post
7435 side-effects are stored at the end of PRE_P.
7437 The reason why the output is split in two is to handle post
7438 side-effects explicitly. In some cases, an expression may have
7439 inner and outer post side-effects which need to be emitted in
7440 an order different from the one given by the recursive
7441 traversal. For instance, for the expression (*p--)++ the post
7442 side-effects of '--' must actually occur *after* the post
7443 side-effects of '++'. However, gimplification will first visit
7444 the inner expression, so if a separate POST sequence was not
7445 used, the resulting sequence would be:
7447 1 t.1 = *p
7448 2 p = p - 1
7449 3 t.2 = t.1 + 1
7450 4 *p = t.2
7452 However, the post-decrement operation in line #2 must not be
7453 evaluated until after the store to *p at line #4, so the
7454 correct sequence should be:
7456 1 t.1 = *p
7457 2 t.2 = t.1 + 1
7458 3 *p = t.2
7459 4 p = p - 1
7461 So, by specifying a separate post queue, it is possible
7462 to emit the post side-effects in the correct order.
7463 If POST_P is NULL, an internal queue will be used. Before
7464 returning to the caller, the sequence POST_P is appended to
7465 the main output sequence PRE_P.
7467 GIMPLE_TEST_F points to a function that takes a tree T and
7468 returns nonzero if T is in the GIMPLE form requested by the
7469 caller. The GIMPLE predicates are in gimple.c.
7471 FALLBACK tells the function what sort of a temporary we want if
7472 gimplification cannot produce an expression that complies with
7473 GIMPLE_TEST_F.
7475 fb_none means that no temporary should be generated
7476 fb_rvalue means that an rvalue is OK to generate
7477 fb_lvalue means that an lvalue is OK to generate
7478 fb_either means that either is OK, but an lvalue is preferable.
7479 fb_mayfail means that gimplification may fail (in which case
7480 GS_ERROR will be returned)
7482 The return value is either GS_ERROR or GS_ALL_DONE, since this
7483 function iterates until EXPR is completely gimplified or an error
7484 occurs. */
7486 enum gimplify_status
7487 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7488 bool (*gimple_test_f) (tree), fallback_t fallback)
7490 tree tmp;
7491 gimple_seq internal_pre = NULL;
7492 gimple_seq internal_post = NULL;
7493 tree save_expr;
7494 bool is_statement;
7495 location_t saved_location;
7496 enum gimplify_status ret;
7497 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7499 save_expr = *expr_p;
7500 if (save_expr == NULL_TREE)
7501 return GS_ALL_DONE;
7503 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7504 is_statement = gimple_test_f == is_gimple_stmt;
7505 if (is_statement)
7506 gcc_assert (pre_p);
7508 /* Consistency checks. */
7509 if (gimple_test_f == is_gimple_reg)
7510 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7511 else if (gimple_test_f == is_gimple_val
7512 || gimple_test_f == is_gimple_call_addr
7513 || gimple_test_f == is_gimple_condexpr
7514 || gimple_test_f == is_gimple_mem_rhs
7515 || gimple_test_f == is_gimple_mem_rhs_or_call
7516 || gimple_test_f == is_gimple_reg_rhs
7517 || gimple_test_f == is_gimple_reg_rhs_or_call
7518 || gimple_test_f == is_gimple_asm_val
7519 || gimple_test_f == is_gimple_mem_ref_addr)
7520 gcc_assert (fallback & fb_rvalue);
7521 else if (gimple_test_f == is_gimple_min_lval
7522 || gimple_test_f == is_gimple_lvalue)
7523 gcc_assert (fallback & fb_lvalue);
7524 else if (gimple_test_f == is_gimple_addressable)
7525 gcc_assert (fallback & fb_either);
7526 else if (gimple_test_f == is_gimple_stmt)
7527 gcc_assert (fallback == fb_none);
7528 else
7530 /* We should have recognized the GIMPLE_TEST_F predicate to
7531 know what kind of fallback to use in case a temporary is
7532 needed to hold the value or address of *EXPR_P. */
7533 gcc_unreachable ();
7536 /* We used to check the predicate here and return immediately if it
7537 succeeds. This is wrong; the design is for gimplification to be
7538 idempotent, and for the predicates to only test for valid forms, not
7539 whether they are fully simplified. */
7540 if (pre_p == NULL)
7541 pre_p = &internal_pre;
7543 if (post_p == NULL)
7544 post_p = &internal_post;
7546 /* Remember the last statements added to PRE_P and POST_P. Every
7547 new statement added by the gimplification helpers needs to be
7548 annotated with location information. To centralize the
7549 responsibility, we remember the last statement that had been
7550 added to both queues before gimplifying *EXPR_P. If
7551 gimplification produces new statements in PRE_P and POST_P, those
7552 statements will be annotated with the same location information
7553 as *EXPR_P. */
7554 pre_last_gsi = gsi_last (*pre_p);
7555 post_last_gsi = gsi_last (*post_p);
7557 saved_location = input_location;
7558 if (save_expr != error_mark_node
7559 && EXPR_HAS_LOCATION (*expr_p))
7560 input_location = EXPR_LOCATION (*expr_p);
7562 /* Loop over the specific gimplifiers until the toplevel node
7563 remains the same. */
7566 /* Strip away as many useless type conversions as possible
7567 at the toplevel. */
7568 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7570 /* Remember the expr. */
7571 save_expr = *expr_p;
7573 /* Die, die, die, my darling. */
7574 if (save_expr == error_mark_node
7575 || (TREE_TYPE (save_expr)
7576 && TREE_TYPE (save_expr) == error_mark_node))
7578 ret = GS_ERROR;
7579 break;
7582 /* Do any language-specific gimplification. */
7583 ret = ((enum gimplify_status)
7584 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7585 if (ret == GS_OK)
7587 if (*expr_p == NULL_TREE)
7588 break;
7589 if (*expr_p != save_expr)
7590 continue;
7592 else if (ret != GS_UNHANDLED)
7593 break;
7595 /* Make sure that all the cases set 'ret' appropriately. */
7596 ret = GS_UNHANDLED;
7597 switch (TREE_CODE (*expr_p))
7599 /* First deal with the special cases. */
7601 case POSTINCREMENT_EXPR:
7602 case POSTDECREMENT_EXPR:
7603 case PREINCREMENT_EXPR:
7604 case PREDECREMENT_EXPR:
7605 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7606 fallback != fb_none,
7607 TREE_TYPE (*expr_p));
7608 break;
7610 case VIEW_CONVERT_EXPR:
7611 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7612 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7614 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7615 post_p, is_gimple_val, fb_rvalue);
7616 recalculate_side_effects (*expr_p);
7617 break;
7619 /* Fallthru. */
7621 case ARRAY_REF:
7622 case ARRAY_RANGE_REF:
7623 case REALPART_EXPR:
7624 case IMAGPART_EXPR:
7625 case COMPONENT_REF:
7626 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7627 fallback ? fallback : fb_rvalue);
7628 break;
7630 case COND_EXPR:
7631 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7633 /* C99 code may assign to an array in a structure value of a
7634 conditional expression, and this has undefined behavior
7635 only on execution, so create a temporary if an lvalue is
7636 required. */
7637 if (fallback == fb_lvalue)
7639 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7640 mark_addressable (*expr_p);
7641 ret = GS_OK;
7643 break;
7645 case CALL_EXPR:
7646 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7648 /* C99 code may assign to an array in a structure returned
7649 from a function, and this has undefined behavior only on
7650 execution, so create a temporary if an lvalue is
7651 required. */
7652 if (fallback == fb_lvalue)
7654 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7655 mark_addressable (*expr_p);
7656 ret = GS_OK;
7658 break;
7660 case TREE_LIST:
7661 gcc_unreachable ();
7663 case COMPOUND_EXPR:
7664 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7665 break;
7667 case COMPOUND_LITERAL_EXPR:
7668 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7669 gimple_test_f, fallback);
7670 break;
7672 case MODIFY_EXPR:
7673 case INIT_EXPR:
7674 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7675 fallback != fb_none);
7676 break;
7678 case TRUTH_ANDIF_EXPR:
7679 case TRUTH_ORIF_EXPR:
7681 /* Preserve the original type of the expression and the
7682 source location of the outer expression. */
7683 tree org_type = TREE_TYPE (*expr_p);
7684 *expr_p = gimple_boolify (*expr_p);
7685 *expr_p = build3_loc (input_location, COND_EXPR,
7686 org_type, *expr_p,
7687 fold_convert_loc
7688 (input_location,
7689 org_type, boolean_true_node),
7690 fold_convert_loc
7691 (input_location,
7692 org_type, boolean_false_node));
7693 ret = GS_OK;
7694 break;
7697 case TRUTH_NOT_EXPR:
7699 tree type = TREE_TYPE (*expr_p);
7700 /* The parsers are careful to generate TRUTH_NOT_EXPR
7701 only with operands that are always zero or one.
7702 We do not fold here but handle the only interesting case
7703 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7704 *expr_p = gimple_boolify (*expr_p);
7705 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7706 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7707 TREE_TYPE (*expr_p),
7708 TREE_OPERAND (*expr_p, 0));
7709 else
7710 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7711 TREE_TYPE (*expr_p),
7712 TREE_OPERAND (*expr_p, 0),
7713 build_int_cst (TREE_TYPE (*expr_p), 1));
7714 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7715 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7716 ret = GS_OK;
7717 break;
7720 case ADDR_EXPR:
7721 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7722 break;
7724 case ANNOTATE_EXPR:
7726 tree cond = TREE_OPERAND (*expr_p, 0);
7727 tree kind = TREE_OPERAND (*expr_p, 1);
7728 tree type = TREE_TYPE (cond);
7729 if (!INTEGRAL_TYPE_P (type))
7731 *expr_p = cond;
7732 ret = GS_OK;
7733 break;
7735 tree tmp = create_tmp_var (type, NULL);
7736 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7737 gimple call
7738 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
7739 gimple_call_set_lhs (call, tmp);
7740 gimplify_seq_add_stmt (pre_p, call);
7741 *expr_p = tmp;
7742 ret = GS_ALL_DONE;
7743 break;
7746 case VA_ARG_EXPR:
7747 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7748 break;
7750 CASE_CONVERT:
7751 if (IS_EMPTY_STMT (*expr_p))
7753 ret = GS_ALL_DONE;
7754 break;
7757 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7758 || fallback == fb_none)
7760 /* Just strip a conversion to void (or in void context) and
7761 try again. */
7762 *expr_p = TREE_OPERAND (*expr_p, 0);
7763 ret = GS_OK;
7764 break;
7767 ret = gimplify_conversion (expr_p);
7768 if (ret == GS_ERROR)
7769 break;
7770 if (*expr_p != save_expr)
7771 break;
7772 /* FALLTHRU */
7774 case FIX_TRUNC_EXPR:
7775 /* unary_expr: ... | '(' cast ')' val | ... */
7776 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7777 is_gimple_val, fb_rvalue);
7778 recalculate_side_effects (*expr_p);
7779 break;
7781 case INDIRECT_REF:
7783 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7784 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7785 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7787 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7788 if (*expr_p != save_expr)
7790 ret = GS_OK;
7791 break;
7794 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7795 is_gimple_reg, fb_rvalue);
7796 if (ret == GS_ERROR)
7797 break;
7799 recalculate_side_effects (*expr_p);
7800 *expr_p = fold_build2_loc (input_location, MEM_REF,
7801 TREE_TYPE (*expr_p),
7802 TREE_OPERAND (*expr_p, 0),
7803 build_int_cst (saved_ptr_type, 0));
7804 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7805 TREE_THIS_NOTRAP (*expr_p) = notrap;
7806 ret = GS_OK;
7807 break;
7810 /* We arrive here through the various re-gimplifcation paths. */
7811 case MEM_REF:
7812 /* First try re-folding the whole thing. */
7813 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7814 TREE_OPERAND (*expr_p, 0),
7815 TREE_OPERAND (*expr_p, 1));
7816 if (tmp)
7818 *expr_p = tmp;
7819 recalculate_side_effects (*expr_p);
7820 ret = GS_OK;
7821 break;
7823 /* Avoid re-gimplifying the address operand if it is already
7824 in suitable form. Re-gimplifying would mark the address
7825 operand addressable. Always gimplify when not in SSA form
7826 as we still may have to gimplify decls with value-exprs. */
7827 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7828 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7830 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7831 is_gimple_mem_ref_addr, fb_rvalue);
7832 if (ret == GS_ERROR)
7833 break;
7835 recalculate_side_effects (*expr_p);
7836 ret = GS_ALL_DONE;
7837 break;
7839 /* Constants need not be gimplified. */
7840 case INTEGER_CST:
7841 case REAL_CST:
7842 case FIXED_CST:
7843 case STRING_CST:
7844 case COMPLEX_CST:
7845 case VECTOR_CST:
7846 /* Drop the overflow flag on constants, we do not want
7847 that in the GIMPLE IL. */
7848 if (TREE_OVERFLOW_P (*expr_p))
7849 *expr_p = drop_tree_overflow (*expr_p);
7850 ret = GS_ALL_DONE;
7851 break;
7853 case CONST_DECL:
7854 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7855 CONST_DECL node. Otherwise the decl is replaceable by its
7856 value. */
7857 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7858 if (fallback & fb_lvalue)
7859 ret = GS_ALL_DONE;
7860 else
7862 *expr_p = DECL_INITIAL (*expr_p);
7863 ret = GS_OK;
7865 break;
7867 case DECL_EXPR:
7868 ret = gimplify_decl_expr (expr_p, pre_p);
7869 break;
7871 case BIND_EXPR:
7872 ret = gimplify_bind_expr (expr_p, pre_p);
7873 break;
7875 case LOOP_EXPR:
7876 ret = gimplify_loop_expr (expr_p, pre_p);
7877 break;
7879 case SWITCH_EXPR:
7880 ret = gimplify_switch_expr (expr_p, pre_p);
7881 break;
7883 case EXIT_EXPR:
7884 ret = gimplify_exit_expr (expr_p);
7885 break;
7887 case GOTO_EXPR:
7888 /* If the target is not LABEL, then it is a computed jump
7889 and the target needs to be gimplified. */
7890 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7892 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7893 NULL, is_gimple_val, fb_rvalue);
7894 if (ret == GS_ERROR)
7895 break;
7897 gimplify_seq_add_stmt (pre_p,
7898 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7899 ret = GS_ALL_DONE;
7900 break;
7902 case PREDICT_EXPR:
7903 gimplify_seq_add_stmt (pre_p,
7904 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7905 PREDICT_EXPR_OUTCOME (*expr_p)));
7906 ret = GS_ALL_DONE;
7907 break;
7909 case LABEL_EXPR:
7910 ret = GS_ALL_DONE;
7911 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7912 == current_function_decl);
7913 gimplify_seq_add_stmt (pre_p,
7914 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7915 break;
7917 case CASE_LABEL_EXPR:
7918 ret = gimplify_case_label_expr (expr_p, pre_p);
7919 break;
7921 case RETURN_EXPR:
7922 ret = gimplify_return_expr (*expr_p, pre_p);
7923 break;
7925 case CONSTRUCTOR:
7926 /* Don't reduce this in place; let gimplify_init_constructor work its
7927 magic. Buf if we're just elaborating this for side effects, just
7928 gimplify any element that has side-effects. */
7929 if (fallback == fb_none)
7931 unsigned HOST_WIDE_INT ix;
7932 tree val;
7933 tree temp = NULL_TREE;
7934 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7935 if (TREE_SIDE_EFFECTS (val))
7936 append_to_statement_list (val, &temp);
7938 *expr_p = temp;
7939 ret = temp ? GS_OK : GS_ALL_DONE;
7941 /* C99 code may assign to an array in a constructed
7942 structure or union, and this has undefined behavior only
7943 on execution, so create a temporary if an lvalue is
7944 required. */
7945 else if (fallback == fb_lvalue)
7947 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7948 mark_addressable (*expr_p);
7949 ret = GS_OK;
7951 else
7952 ret = GS_ALL_DONE;
7953 break;
7955 /* The following are special cases that are not handled by the
7956 original GIMPLE grammar. */
7958 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7959 eliminated. */
7960 case SAVE_EXPR:
7961 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7962 break;
7964 case BIT_FIELD_REF:
7965 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7966 post_p, is_gimple_lvalue, fb_either);
7967 recalculate_side_effects (*expr_p);
7968 break;
7970 case TARGET_MEM_REF:
7972 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7974 if (TMR_BASE (*expr_p))
7975 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7976 post_p, is_gimple_mem_ref_addr, fb_either);
7977 if (TMR_INDEX (*expr_p))
7978 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7979 post_p, is_gimple_val, fb_rvalue);
7980 if (TMR_INDEX2 (*expr_p))
7981 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7982 post_p, is_gimple_val, fb_rvalue);
7983 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7984 ret = MIN (r0, r1);
7986 break;
7988 case NON_LVALUE_EXPR:
7989 /* This should have been stripped above. */
7990 gcc_unreachable ();
7992 case ASM_EXPR:
7993 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7994 break;
7996 case TRY_FINALLY_EXPR:
7997 case TRY_CATCH_EXPR:
7999 gimple_seq eval, cleanup;
8000 gimple try_;
8002 /* Calls to destructors are generated automatically in FINALLY/CATCH
8003 block. They should have location as UNKNOWN_LOCATION. However,
8004 gimplify_call_expr will reset these call stmts to input_location
8005 if it finds stmt's location is unknown. To prevent resetting for
8006 destructors, we set the input_location to unknown.
8007 Note that this only affects the destructor calls in FINALLY/CATCH
8008 block, and will automatically reset to its original value by the
8009 end of gimplify_expr. */
8010 input_location = UNKNOWN_LOCATION;
8011 eval = cleanup = NULL;
8012 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8013 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8014 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8015 if (gimple_seq_empty_p (cleanup))
8017 gimple_seq_add_seq (pre_p, eval);
8018 ret = GS_ALL_DONE;
8019 break;
8021 try_ = gimple_build_try (eval, cleanup,
8022 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8023 ? GIMPLE_TRY_FINALLY
8024 : GIMPLE_TRY_CATCH);
8025 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8026 gimple_set_location (try_, saved_location);
8027 else
8028 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8029 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8030 gimple_try_set_catch_is_cleanup (try_,
8031 TRY_CATCH_IS_CLEANUP (*expr_p));
8032 gimplify_seq_add_stmt (pre_p, try_);
8033 ret = GS_ALL_DONE;
8034 break;
8037 case CLEANUP_POINT_EXPR:
8038 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8039 break;
8041 case TARGET_EXPR:
8042 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8043 break;
8045 case CATCH_EXPR:
8047 gimple c;
8048 gimple_seq handler = NULL;
8049 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8050 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8051 gimplify_seq_add_stmt (pre_p, c);
8052 ret = GS_ALL_DONE;
8053 break;
8056 case EH_FILTER_EXPR:
8058 gimple ehf;
8059 gimple_seq failure = NULL;
8061 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8062 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8063 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8064 gimplify_seq_add_stmt (pre_p, ehf);
8065 ret = GS_ALL_DONE;
8066 break;
8069 case OBJ_TYPE_REF:
8071 enum gimplify_status r0, r1;
8072 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8073 post_p, is_gimple_val, fb_rvalue);
8074 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8075 post_p, is_gimple_val, fb_rvalue);
8076 TREE_SIDE_EFFECTS (*expr_p) = 0;
8077 ret = MIN (r0, r1);
8079 break;
8081 case LABEL_DECL:
8082 /* We get here when taking the address of a label. We mark
8083 the label as "forced"; meaning it can never be removed and
8084 it is a potential target for any computed goto. */
8085 FORCED_LABEL (*expr_p) = 1;
8086 ret = GS_ALL_DONE;
8087 break;
8089 case STATEMENT_LIST:
8090 ret = gimplify_statement_list (expr_p, pre_p);
8091 break;
8093 case WITH_SIZE_EXPR:
8095 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8096 post_p == &internal_post ? NULL : post_p,
8097 gimple_test_f, fallback);
8098 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8099 is_gimple_val, fb_rvalue);
8100 ret = GS_ALL_DONE;
8102 break;
8104 case VAR_DECL:
8105 case PARM_DECL:
8106 ret = gimplify_var_or_parm_decl (expr_p);
8107 break;
8109 case RESULT_DECL:
8110 /* When within an OpenMP context, notice uses of variables. */
8111 if (gimplify_omp_ctxp)
8112 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8113 ret = GS_ALL_DONE;
8114 break;
8116 case SSA_NAME:
8117 /* Allow callbacks into the gimplifier during optimization. */
8118 ret = GS_ALL_DONE;
8119 break;
8121 case OMP_PARALLEL:
8122 gimplify_omp_parallel (expr_p, pre_p);
8123 ret = GS_ALL_DONE;
8124 break;
8126 case OMP_TASK:
8127 gimplify_omp_task (expr_p, pre_p);
8128 ret = GS_ALL_DONE;
8129 break;
8131 case OMP_FOR:
8132 case OMP_SIMD:
8133 case CILK_SIMD:
8134 case CILK_FOR:
8135 case OMP_DISTRIBUTE:
8136 ret = gimplify_omp_for (expr_p, pre_p);
8137 break;
8139 case OMP_SECTIONS:
8140 case OMP_SINGLE:
8141 case OMP_TARGET:
8142 case OMP_TARGET_DATA:
8143 case OMP_TEAMS:
8144 gimplify_omp_workshare (expr_p, pre_p);
8145 ret = GS_ALL_DONE;
8146 break;
8148 case OMP_TARGET_UPDATE:
8149 gimplify_omp_target_update (expr_p, pre_p);
8150 ret = GS_ALL_DONE;
8151 break;
8153 case OMP_SECTION:
8154 case OMP_MASTER:
8155 case OMP_TASKGROUP:
8156 case OMP_ORDERED:
8157 case OMP_CRITICAL:
8159 gimple_seq body = NULL;
8160 gimple g;
8162 gimplify_and_add (OMP_BODY (*expr_p), &body);
8163 switch (TREE_CODE (*expr_p))
8165 case OMP_SECTION:
8166 g = gimple_build_omp_section (body);
8167 break;
8168 case OMP_MASTER:
8169 g = gimple_build_omp_master (body);
8170 break;
8171 case OMP_TASKGROUP:
8173 gimple_seq cleanup = NULL;
8174 tree fn
8175 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8176 g = gimple_build_call (fn, 0);
8177 gimple_seq_add_stmt (&cleanup, g);
8178 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8179 body = NULL;
8180 gimple_seq_add_stmt (&body, g);
8181 g = gimple_build_omp_taskgroup (body);
8183 break;
8184 case OMP_ORDERED:
8185 g = gimple_build_omp_ordered (body);
8186 break;
8187 case OMP_CRITICAL:
8188 g = gimple_build_omp_critical (body,
8189 OMP_CRITICAL_NAME (*expr_p));
8190 break;
8191 default:
8192 gcc_unreachable ();
8194 gimplify_seq_add_stmt (pre_p, g);
8195 ret = GS_ALL_DONE;
8196 break;
8199 case OMP_ATOMIC:
8200 case OMP_ATOMIC_READ:
8201 case OMP_ATOMIC_CAPTURE_OLD:
8202 case OMP_ATOMIC_CAPTURE_NEW:
8203 ret = gimplify_omp_atomic (expr_p, pre_p);
8204 break;
8206 case TRANSACTION_EXPR:
8207 ret = gimplify_transaction (expr_p, pre_p);
8208 break;
8210 case TRUTH_AND_EXPR:
8211 case TRUTH_OR_EXPR:
8212 case TRUTH_XOR_EXPR:
8214 tree orig_type = TREE_TYPE (*expr_p);
8215 tree new_type, xop0, xop1;
8216 *expr_p = gimple_boolify (*expr_p);
8217 new_type = TREE_TYPE (*expr_p);
8218 if (!useless_type_conversion_p (orig_type, new_type))
8220 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8221 ret = GS_OK;
8222 break;
8225 /* Boolified binary truth expressions are semantically equivalent
8226 to bitwise binary expressions. Canonicalize them to the
8227 bitwise variant. */
8228 switch (TREE_CODE (*expr_p))
8230 case TRUTH_AND_EXPR:
8231 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8232 break;
8233 case TRUTH_OR_EXPR:
8234 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8235 break;
8236 case TRUTH_XOR_EXPR:
8237 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8238 break;
8239 default:
8240 break;
8242 /* Now make sure that operands have compatible type to
8243 expression's new_type. */
8244 xop0 = TREE_OPERAND (*expr_p, 0);
8245 xop1 = TREE_OPERAND (*expr_p, 1);
8246 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8247 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8248 new_type,
8249 xop0);
8250 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8251 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8252 new_type,
8253 xop1);
8254 /* Continue classified as tcc_binary. */
8255 goto expr_2;
8258 case FMA_EXPR:
8259 case VEC_COND_EXPR:
8260 case VEC_PERM_EXPR:
8261 /* Classified as tcc_expression. */
8262 goto expr_3;
8264 case POINTER_PLUS_EXPR:
8266 enum gimplify_status r0, r1;
8267 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8268 post_p, is_gimple_val, fb_rvalue);
8269 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8270 post_p, is_gimple_val, fb_rvalue);
8271 recalculate_side_effects (*expr_p);
8272 ret = MIN (r0, r1);
8273 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8274 after gimplifying operands - this is similar to how
8275 it would be folding all gimplified stmts on creation
8276 to have them canonicalized, which is what we eventually
8277 should do anyway. */
8278 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8279 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8281 *expr_p = build_fold_addr_expr_with_type_loc
8282 (input_location,
8283 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8284 TREE_OPERAND (*expr_p, 0),
8285 fold_convert (ptr_type_node,
8286 TREE_OPERAND (*expr_p, 1))),
8287 TREE_TYPE (*expr_p));
8288 ret = MIN (ret, GS_OK);
8290 break;
8293 case CILK_SYNC_STMT:
8295 if (!fn_contains_cilk_spawn_p (cfun))
8297 error_at (EXPR_LOCATION (*expr_p),
8298 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8299 ret = GS_ERROR;
8301 else
8303 gimplify_cilk_sync (expr_p, pre_p);
8304 ret = GS_ALL_DONE;
8306 break;
8309 default:
8310 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8312 case tcc_comparison:
8313 /* Handle comparison of objects of non scalar mode aggregates
8314 with a call to memcmp. It would be nice to only have to do
8315 this for variable-sized objects, but then we'd have to allow
8316 the same nest of reference nodes we allow for MODIFY_EXPR and
8317 that's too complex.
8319 Compare scalar mode aggregates as scalar mode values. Using
8320 memcmp for them would be very inefficient at best, and is
8321 plain wrong if bitfields are involved. */
8323 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8325 /* Vector comparisons need no boolification. */
8326 if (TREE_CODE (type) == VECTOR_TYPE)
8327 goto expr_2;
8328 else if (!AGGREGATE_TYPE_P (type))
8330 tree org_type = TREE_TYPE (*expr_p);
8331 *expr_p = gimple_boolify (*expr_p);
8332 if (!useless_type_conversion_p (org_type,
8333 TREE_TYPE (*expr_p)))
8335 *expr_p = fold_convert_loc (input_location,
8336 org_type, *expr_p);
8337 ret = GS_OK;
8339 else
8340 goto expr_2;
8342 else if (TYPE_MODE (type) != BLKmode)
8343 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8344 else
8345 ret = gimplify_variable_sized_compare (expr_p);
8347 break;
8350 /* If *EXPR_P does not need to be special-cased, handle it
8351 according to its class. */
8352 case tcc_unary:
8353 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8354 post_p, is_gimple_val, fb_rvalue);
8355 break;
8357 case tcc_binary:
8358 expr_2:
8360 enum gimplify_status r0, r1;
8362 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8363 post_p, is_gimple_val, fb_rvalue);
8364 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8365 post_p, is_gimple_val, fb_rvalue);
8367 ret = MIN (r0, r1);
8368 break;
8371 expr_3:
8373 enum gimplify_status r0, r1, r2;
8375 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8376 post_p, is_gimple_val, fb_rvalue);
8377 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8378 post_p, is_gimple_val, fb_rvalue);
8379 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8380 post_p, is_gimple_val, fb_rvalue);
8382 ret = MIN (MIN (r0, r1), r2);
8383 break;
8386 case tcc_declaration:
8387 case tcc_constant:
8388 ret = GS_ALL_DONE;
8389 goto dont_recalculate;
8391 default:
8392 gcc_unreachable ();
8395 recalculate_side_effects (*expr_p);
8397 dont_recalculate:
8398 break;
8401 gcc_assert (*expr_p || ret != GS_OK);
8403 while (ret == GS_OK);
8405 /* If we encountered an error_mark somewhere nested inside, either
8406 stub out the statement or propagate the error back out. */
8407 if (ret == GS_ERROR)
8409 if (is_statement)
8410 *expr_p = NULL;
8411 goto out;
8414 /* This was only valid as a return value from the langhook, which
8415 we handled. Make sure it doesn't escape from any other context. */
8416 gcc_assert (ret != GS_UNHANDLED);
8418 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8420 /* We aren't looking for a value, and we don't have a valid
8421 statement. If it doesn't have side-effects, throw it away. */
8422 if (!TREE_SIDE_EFFECTS (*expr_p))
8423 *expr_p = NULL;
8424 else if (!TREE_THIS_VOLATILE (*expr_p))
8426 /* This is probably a _REF that contains something nested that
8427 has side effects. Recurse through the operands to find it. */
8428 enum tree_code code = TREE_CODE (*expr_p);
8430 switch (code)
8432 case COMPONENT_REF:
8433 case REALPART_EXPR:
8434 case IMAGPART_EXPR:
8435 case VIEW_CONVERT_EXPR:
8436 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8437 gimple_test_f, fallback);
8438 break;
8440 case ARRAY_REF:
8441 case ARRAY_RANGE_REF:
8442 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8443 gimple_test_f, fallback);
8444 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8445 gimple_test_f, fallback);
8446 break;
8448 default:
8449 /* Anything else with side-effects must be converted to
8450 a valid statement before we get here. */
8451 gcc_unreachable ();
8454 *expr_p = NULL;
8456 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8457 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8459 /* Historically, the compiler has treated a bare reference
8460 to a non-BLKmode volatile lvalue as forcing a load. */
8461 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8463 /* Normally, we do not want to create a temporary for a
8464 TREE_ADDRESSABLE type because such a type should not be
8465 copied by bitwise-assignment. However, we make an
8466 exception here, as all we are doing here is ensuring that
8467 we read the bytes that make up the type. We use
8468 create_tmp_var_raw because create_tmp_var will abort when
8469 given a TREE_ADDRESSABLE type. */
8470 tree tmp = create_tmp_var_raw (type, "vol");
8471 gimple_add_tmp_var (tmp);
8472 gimplify_assign (tmp, *expr_p, pre_p);
8473 *expr_p = NULL;
8475 else
8476 /* We can't do anything useful with a volatile reference to
8477 an incomplete type, so just throw it away. Likewise for
8478 a BLKmode type, since any implicit inner load should
8479 already have been turned into an explicit one by the
8480 gimplification process. */
8481 *expr_p = NULL;
8484 /* If we are gimplifying at the statement level, we're done. Tack
8485 everything together and return. */
8486 if (fallback == fb_none || is_statement)
8488 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8489 it out for GC to reclaim it. */
8490 *expr_p = NULL_TREE;
8492 if (!gimple_seq_empty_p (internal_pre)
8493 || !gimple_seq_empty_p (internal_post))
8495 gimplify_seq_add_seq (&internal_pre, internal_post);
8496 gimplify_seq_add_seq (pre_p, internal_pre);
8499 /* The result of gimplifying *EXPR_P is going to be the last few
8500 statements in *PRE_P and *POST_P. Add location information
8501 to all the statements that were added by the gimplification
8502 helpers. */
8503 if (!gimple_seq_empty_p (*pre_p))
8504 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8506 if (!gimple_seq_empty_p (*post_p))
8507 annotate_all_with_location_after (*post_p, post_last_gsi,
8508 input_location);
8510 goto out;
8513 #ifdef ENABLE_GIMPLE_CHECKING
8514 if (*expr_p)
8516 enum tree_code code = TREE_CODE (*expr_p);
8517 /* These expressions should already be in gimple IR form. */
8518 gcc_assert (code != MODIFY_EXPR
8519 && code != ASM_EXPR
8520 && code != BIND_EXPR
8521 && code != CATCH_EXPR
8522 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8523 && code != EH_FILTER_EXPR
8524 && code != GOTO_EXPR
8525 && code != LABEL_EXPR
8526 && code != LOOP_EXPR
8527 && code != SWITCH_EXPR
8528 && code != TRY_FINALLY_EXPR
8529 && code != OMP_CRITICAL
8530 && code != OMP_FOR
8531 && code != OMP_MASTER
8532 && code != OMP_TASKGROUP
8533 && code != OMP_ORDERED
8534 && code != OMP_PARALLEL
8535 && code != OMP_SECTIONS
8536 && code != OMP_SECTION
8537 && code != OMP_SINGLE);
8539 #endif
8541 /* Otherwise we're gimplifying a subexpression, so the resulting
8542 value is interesting. If it's a valid operand that matches
8543 GIMPLE_TEST_F, we're done. Unless we are handling some
8544 post-effects internally; if that's the case, we need to copy into
8545 a temporary before adding the post-effects to POST_P. */
8546 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8547 goto out;
8549 /* Otherwise, we need to create a new temporary for the gimplified
8550 expression. */
8552 /* We can't return an lvalue if we have an internal postqueue. The
8553 object the lvalue refers to would (probably) be modified by the
8554 postqueue; we need to copy the value out first, which means an
8555 rvalue. */
8556 if ((fallback & fb_lvalue)
8557 && gimple_seq_empty_p (internal_post)
8558 && is_gimple_addressable (*expr_p))
8560 /* An lvalue will do. Take the address of the expression, store it
8561 in a temporary, and replace the expression with an INDIRECT_REF of
8562 that temporary. */
8563 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8564 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8565 *expr_p = build_simple_mem_ref (tmp);
8567 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8569 /* An rvalue will do. Assign the gimplified expression into a
8570 new temporary TMP and replace the original expression with
8571 TMP. First, make sure that the expression has a type so that
8572 it can be assigned into a temporary. */
8573 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8574 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8576 else
8578 #ifdef ENABLE_GIMPLE_CHECKING
8579 if (!(fallback & fb_mayfail))
8581 fprintf (stderr, "gimplification failed:\n");
8582 print_generic_expr (stderr, *expr_p, 0);
8583 debug_tree (*expr_p);
8584 internal_error ("gimplification failed");
8586 #endif
8587 gcc_assert (fallback & fb_mayfail);
8589 /* If this is an asm statement, and the user asked for the
8590 impossible, don't die. Fail and let gimplify_asm_expr
8591 issue an error. */
8592 ret = GS_ERROR;
8593 goto out;
8596 /* Make sure the temporary matches our predicate. */
8597 gcc_assert ((*gimple_test_f) (*expr_p));
8599 if (!gimple_seq_empty_p (internal_post))
8601 annotate_all_with_location (internal_post, input_location);
8602 gimplify_seq_add_seq (pre_p, internal_post);
8605 out:
8606 input_location = saved_location;
8607 return ret;
8610 /* Look through TYPE for variable-sized objects and gimplify each such
8611 size that we find. Add to LIST_P any statements generated. */
8613 void
8614 gimplify_type_sizes (tree type, gimple_seq *list_p)
8616 tree field, t;
8618 if (type == NULL || type == error_mark_node)
8619 return;
8621 /* We first do the main variant, then copy into any other variants. */
8622 type = TYPE_MAIN_VARIANT (type);
8624 /* Avoid infinite recursion. */
8625 if (TYPE_SIZES_GIMPLIFIED (type))
8626 return;
8628 TYPE_SIZES_GIMPLIFIED (type) = 1;
8630 switch (TREE_CODE (type))
8632 case INTEGER_TYPE:
8633 case ENUMERAL_TYPE:
8634 case BOOLEAN_TYPE:
8635 case REAL_TYPE:
8636 case FIXED_POINT_TYPE:
8637 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8638 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8640 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8642 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8643 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8645 break;
8647 case ARRAY_TYPE:
8648 /* These types may not have declarations, so handle them here. */
8649 gimplify_type_sizes (TREE_TYPE (type), list_p);
8650 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8651 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8652 with assigned stack slots, for -O1+ -g they should be tracked
8653 by VTA. */
8654 if (!(TYPE_NAME (type)
8655 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8656 && DECL_IGNORED_P (TYPE_NAME (type)))
8657 && TYPE_DOMAIN (type)
8658 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8660 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8661 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8662 DECL_IGNORED_P (t) = 0;
8663 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8664 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8665 DECL_IGNORED_P (t) = 0;
8667 break;
8669 case RECORD_TYPE:
8670 case UNION_TYPE:
8671 case QUAL_UNION_TYPE:
8672 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8673 if (TREE_CODE (field) == FIELD_DECL)
8675 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8676 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8677 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8678 gimplify_type_sizes (TREE_TYPE (field), list_p);
8680 break;
8682 case POINTER_TYPE:
8683 case REFERENCE_TYPE:
8684 /* We used to recurse on the pointed-to type here, which turned out to
8685 be incorrect because its definition might refer to variables not
8686 yet initialized at this point if a forward declaration is involved.
8688 It was actually useful for anonymous pointed-to types to ensure
8689 that the sizes evaluation dominates every possible later use of the
8690 values. Restricting to such types here would be safe since there
8691 is no possible forward declaration around, but would introduce an
8692 undesirable middle-end semantic to anonymity. We then defer to
8693 front-ends the responsibility of ensuring that the sizes are
8694 evaluated both early and late enough, e.g. by attaching artificial
8695 type declarations to the tree. */
8696 break;
8698 default:
8699 break;
8702 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8703 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8705 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8707 TYPE_SIZE (t) = TYPE_SIZE (type);
8708 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8709 TYPE_SIZES_GIMPLIFIED (t) = 1;
8713 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8714 a size or position, has had all of its SAVE_EXPRs evaluated.
8715 We add any required statements to *STMT_P. */
8717 void
8718 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8720 tree expr = *expr_p;
8722 /* We don't do anything if the value isn't there, is constant, or contains
8723 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8724 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8725 will want to replace it with a new variable, but that will cause problems
8726 if this type is from outside the function. It's OK to have that here. */
8727 if (is_gimple_sizepos (expr))
8728 return;
8730 *expr_p = unshare_expr (expr);
8732 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8735 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8736 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8737 is true, also gimplify the parameters. */
8739 gimple
8740 gimplify_body (tree fndecl, bool do_parms)
8742 location_t saved_location = input_location;
8743 gimple_seq parm_stmts, seq;
8744 gimple outer_bind;
8745 struct cgraph_node *cgn;
8747 timevar_push (TV_TREE_GIMPLIFY);
8749 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8750 gimplification. */
8751 default_rtl_profile ();
8753 gcc_assert (gimplify_ctxp == NULL);
8754 push_gimplify_context ();
8756 if (flag_openmp)
8758 gcc_assert (gimplify_omp_ctxp == NULL);
8759 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8760 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8763 /* Unshare most shared trees in the body and in that of any nested functions.
8764 It would seem we don't have to do this for nested functions because
8765 they are supposed to be output and then the outer function gimplified
8766 first, but the g++ front end doesn't always do it that way. */
8767 unshare_body (fndecl);
8768 unvisit_body (fndecl);
8770 cgn = cgraph_node::get (fndecl);
8771 if (cgn && cgn->origin)
8772 nonlocal_vlas = new hash_set<tree>;
8774 /* Make sure input_location isn't set to something weird. */
8775 input_location = DECL_SOURCE_LOCATION (fndecl);
8777 /* Resolve callee-copies. This has to be done before processing
8778 the body so that DECL_VALUE_EXPR gets processed correctly. */
8779 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8781 /* Gimplify the function's body. */
8782 seq = NULL;
8783 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8784 outer_bind = gimple_seq_first_stmt (seq);
8785 if (!outer_bind)
8787 outer_bind = gimple_build_nop ();
8788 gimplify_seq_add_stmt (&seq, outer_bind);
8791 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8792 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8793 if (gimple_code (outer_bind) == GIMPLE_BIND
8794 && gimple_seq_first (seq) == gimple_seq_last (seq))
8796 else
8797 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8799 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8801 /* If we had callee-copies statements, insert them at the beginning
8802 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8803 if (!gimple_seq_empty_p (parm_stmts))
8805 tree parm;
8807 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8808 gimple_bind_set_body (outer_bind, parm_stmts);
8810 for (parm = DECL_ARGUMENTS (current_function_decl);
8811 parm; parm = DECL_CHAIN (parm))
8812 if (DECL_HAS_VALUE_EXPR_P (parm))
8814 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8815 DECL_IGNORED_P (parm) = 0;
8819 if (nonlocal_vlas)
8821 if (nonlocal_vla_vars)
8823 /* tree-nested.c may later on call declare_vars (..., true);
8824 which relies on BLOCK_VARS chain to be the tail of the
8825 gimple_bind_vars chain. Ensure we don't violate that
8826 assumption. */
8827 if (gimple_bind_block (outer_bind)
8828 == DECL_INITIAL (current_function_decl))
8829 declare_vars (nonlocal_vla_vars, outer_bind, true);
8830 else
8831 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8832 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8833 nonlocal_vla_vars);
8834 nonlocal_vla_vars = NULL_TREE;
8836 delete nonlocal_vlas;
8837 nonlocal_vlas = NULL;
8840 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8842 delete_omp_context (gimplify_omp_ctxp);
8843 gimplify_omp_ctxp = NULL;
8846 pop_gimplify_context (outer_bind);
8847 gcc_assert (gimplify_ctxp == NULL);
8849 #ifdef ENABLE_CHECKING
8850 if (!seen_error ())
8851 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8852 #endif
8854 timevar_pop (TV_TREE_GIMPLIFY);
8855 input_location = saved_location;
8857 return outer_bind;
8860 typedef char *char_p; /* For DEF_VEC_P. */
8862 /* Return whether we should exclude FNDECL from instrumentation. */
8864 static bool
8865 flag_instrument_functions_exclude_p (tree fndecl)
8867 vec<char_p> *v;
8869 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8870 if (v && v->length () > 0)
8872 const char *name;
8873 int i;
8874 char *s;
8876 name = lang_hooks.decl_printable_name (fndecl, 0);
8877 FOR_EACH_VEC_ELT (*v, i, s)
8878 if (strstr (name, s) != NULL)
8879 return true;
8882 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8883 if (v && v->length () > 0)
8885 const char *name;
8886 int i;
8887 char *s;
8889 name = DECL_SOURCE_FILE (fndecl);
8890 FOR_EACH_VEC_ELT (*v, i, s)
8891 if (strstr (name, s) != NULL)
8892 return true;
8895 return false;
8898 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8899 node for the function we want to gimplify.
8901 Return the sequence of GIMPLE statements corresponding to the body
8902 of FNDECL. */
8904 void
8905 gimplify_function_tree (tree fndecl)
8907 tree parm, ret;
8908 gimple_seq seq;
8909 gimple bind;
8911 gcc_assert (!gimple_body (fndecl));
8913 if (DECL_STRUCT_FUNCTION (fndecl))
8914 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8915 else
8916 push_struct_function (fndecl);
8918 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8920 /* Preliminarily mark non-addressed complex variables as eligible
8921 for promotion to gimple registers. We'll transform their uses
8922 as we find them. */
8923 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8924 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8925 && !TREE_THIS_VOLATILE (parm)
8926 && !needs_to_live_in_memory (parm))
8927 DECL_GIMPLE_REG_P (parm) = 1;
8930 ret = DECL_RESULT (fndecl);
8931 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8932 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8933 && !needs_to_live_in_memory (ret))
8934 DECL_GIMPLE_REG_P (ret) = 1;
8936 bind = gimplify_body (fndecl, true);
8938 /* The tree body of the function is no longer needed, replace it
8939 with the new GIMPLE body. */
8940 seq = NULL;
8941 gimple_seq_add_stmt (&seq, bind);
8942 gimple_set_body (fndecl, seq);
8944 /* If we're instrumenting function entry/exit, then prepend the call to
8945 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8946 catch the exit hook. */
8947 /* ??? Add some way to ignore exceptions for this TFE. */
8948 if (flag_instrument_function_entry_exit
8949 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8950 && !flag_instrument_functions_exclude_p (fndecl))
8952 tree x;
8953 gimple new_bind;
8954 gimple tf;
8955 gimple_seq cleanup = NULL, body = NULL;
8956 tree tmp_var;
8957 gimple call;
8959 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8960 call = gimple_build_call (x, 1, integer_zero_node);
8961 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8962 gimple_call_set_lhs (call, tmp_var);
8963 gimplify_seq_add_stmt (&cleanup, call);
8964 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8965 call = gimple_build_call (x, 2,
8966 build_fold_addr_expr (current_function_decl),
8967 tmp_var);
8968 gimplify_seq_add_stmt (&cleanup, call);
8969 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8971 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8972 call = gimple_build_call (x, 1, integer_zero_node);
8973 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8974 gimple_call_set_lhs (call, tmp_var);
8975 gimplify_seq_add_stmt (&body, call);
8976 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8977 call = gimple_build_call (x, 2,
8978 build_fold_addr_expr (current_function_decl),
8979 tmp_var);
8980 gimplify_seq_add_stmt (&body, call);
8981 gimplify_seq_add_stmt (&body, tf);
8982 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8983 /* Clear the block for BIND, since it is no longer directly inside
8984 the function, but within a try block. */
8985 gimple_bind_set_block (bind, NULL);
8987 /* Replace the current function body with the body
8988 wrapped in the try/finally TF. */
8989 seq = NULL;
8990 gimple_seq_add_stmt (&seq, new_bind);
8991 gimple_set_body (fndecl, seq);
8994 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8995 cfun->curr_properties = PROP_gimple_any;
8997 pop_cfun ();
9000 /* Return a dummy expression of type TYPE in order to keep going after an
9001 error. */
9003 static tree
9004 dummy_object (tree type)
9006 tree t = build_int_cst (build_pointer_type (type), 0);
9007 return build2 (MEM_REF, type, t, t);
9010 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9011 builtin function, but a very special sort of operator. */
9013 enum gimplify_status
9014 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9016 tree promoted_type, have_va_type;
9017 tree valist = TREE_OPERAND (*expr_p, 0);
9018 tree type = TREE_TYPE (*expr_p);
9019 tree t;
9020 location_t loc = EXPR_LOCATION (*expr_p);
9022 /* Verify that valist is of the proper type. */
9023 have_va_type = TREE_TYPE (valist);
9024 if (have_va_type == error_mark_node)
9025 return GS_ERROR;
9026 have_va_type = targetm.canonical_va_list_type (have_va_type);
9028 if (have_va_type == NULL_TREE)
9030 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9031 return GS_ERROR;
9034 /* Generate a diagnostic for requesting data of a type that cannot
9035 be passed through `...' due to type promotion at the call site. */
9036 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9037 != type)
9039 static bool gave_help;
9040 bool warned;
9042 /* Unfortunately, this is merely undefined, rather than a constraint
9043 violation, so we cannot make this an error. If this call is never
9044 executed, the program is still strictly conforming. */
9045 warned = warning_at (loc, 0,
9046 "%qT is promoted to %qT when passed through %<...%>",
9047 type, promoted_type);
9048 if (!gave_help && warned)
9050 gave_help = true;
9051 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9052 promoted_type, type);
9055 /* We can, however, treat "undefined" any way we please.
9056 Call abort to encourage the user to fix the program. */
9057 if (warned)
9058 inform (loc, "if this code is reached, the program will abort");
9059 /* Before the abort, allow the evaluation of the va_list
9060 expression to exit or longjmp. */
9061 gimplify_and_add (valist, pre_p);
9062 t = build_call_expr_loc (loc,
9063 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9064 gimplify_and_add (t, pre_p);
9066 /* This is dead code, but go ahead and finish so that the
9067 mode of the result comes out right. */
9068 *expr_p = dummy_object (type);
9069 return GS_ALL_DONE;
9071 else
9073 /* Make it easier for the backends by protecting the valist argument
9074 from multiple evaluations. */
9075 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9077 /* For this case, the backends will be expecting a pointer to
9078 TREE_TYPE (abi), but it's possible we've
9079 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9080 So fix it. */
9081 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9083 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9084 valist = fold_convert_loc (loc, p1,
9085 build_fold_addr_expr_loc (loc, valist));
9088 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9090 else
9091 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9093 if (!targetm.gimplify_va_arg_expr)
9094 /* FIXME: Once most targets are converted we should merely
9095 assert this is non-null. */
9096 return GS_ALL_DONE;
9098 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9099 return GS_OK;
9103 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9105 DST/SRC are the destination and source respectively. You can pass
9106 ungimplified trees in DST or SRC, in which case they will be
9107 converted to a gimple operand if necessary.
9109 This function returns the newly created GIMPLE_ASSIGN tuple. */
9111 gimple
9112 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9114 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9115 gimplify_and_add (t, seq_p);
9116 ggc_free (t);
9117 return gimple_seq_last_stmt (*seq_p);
9120 inline hashval_t
9121 gimplify_hasher::hash (const value_type *p)
9123 tree t = p->val;
9124 return iterative_hash_expr (t, 0);
9127 inline bool
9128 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
9130 tree t1 = p1->val;
9131 tree t2 = p2->val;
9132 enum tree_code code = TREE_CODE (t1);
9134 if (TREE_CODE (t2) != code
9135 || TREE_TYPE (t1) != TREE_TYPE (t2))
9136 return false;
9138 if (!operand_equal_p (t1, t2, 0))
9139 return false;
9141 #ifdef ENABLE_CHECKING
9142 /* Only allow them to compare equal if they also hash equal; otherwise
9143 results are nondeterminate, and we fail bootstrap comparison. */
9144 gcc_assert (hash (p1) == hash (p2));
9145 #endif
9147 return true;