Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / gimplify.c
blobb4dd4d491f2b0228d259ccfb2480f518bf61080e
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 "pointer-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 */
66 enum gimplify_omp_var_data
68 GOVD_SEEN = 1,
69 GOVD_EXPLICIT = 2,
70 GOVD_SHARED = 4,
71 GOVD_PRIVATE = 8,
72 GOVD_FIRSTPRIVATE = 16,
73 GOVD_LASTPRIVATE = 32,
74 GOVD_REDUCTION = 64,
75 GOVD_LOCAL = 128,
76 GOVD_MAP = 256,
77 GOVD_DEBUG_PRIVATE = 512,
78 GOVD_PRIVATE_OUTER_REF = 1024,
79 GOVD_LINEAR = 2048,
80 GOVD_ALIGNED = 4096,
81 GOVD_MAP_TO_ONLY = 8192,
82 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
83 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
84 | GOVD_LOCAL)
88 enum omp_region_type
90 ORT_WORKSHARE = 0,
91 ORT_SIMD = 1,
92 ORT_PARALLEL = 2,
93 ORT_COMBINED_PARALLEL = 3,
94 ORT_TASK = 4,
95 ORT_UNTIED_TASK = 5,
96 ORT_TEAMS = 8,
97 ORT_TARGET_DATA = 16,
98 ORT_TARGET = 32
101 /* Gimplify hashtable helper. */
103 struct gimplify_hasher : typed_free_remove <elt_t>
105 typedef elt_t value_type;
106 typedef elt_t compare_type;
107 static inline hashval_t hash (const value_type *);
108 static inline bool equal (const value_type *, const compare_type *);
111 struct gimplify_ctx
113 struct gimplify_ctx *prev_context;
115 vec<gimple> bind_expr_stack;
116 tree temps;
117 gimple_seq conditional_cleanups;
118 tree exit_label;
119 tree return_temp;
121 vec<tree> case_labels;
122 /* The formal temporary table. Should this be persistent? */
123 hash_table <gimplify_hasher> temp_htab;
125 int conditions;
126 bool save_stack;
127 bool into_ssa;
128 bool allow_rhs_cond_expr;
129 bool in_cleanup_point_expr;
132 struct gimplify_omp_ctx
134 struct gimplify_omp_ctx *outer_context;
135 splay_tree variables;
136 struct pointer_set_t *privatized_types;
137 location_t location;
138 enum omp_clause_default_kind default_kind;
139 enum omp_region_type region_type;
140 bool combined_loop;
143 static struct gimplify_ctx *gimplify_ctxp;
144 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
146 /* Forward declaration. */
147 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
149 /* Shorter alias name for the above function for use in gimplify.c
150 only. */
152 static inline void
153 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
155 gimple_seq_add_stmt_without_update (seq_p, gs);
158 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
159 NULL, a new sequence is allocated. This function is
160 similar to gimple_seq_add_seq, but does not scan the operands.
161 During gimplification, we need to manipulate statement sequences
162 before the def/use vectors have been constructed. */
164 static void
165 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
167 gimple_stmt_iterator si;
169 if (src == NULL)
170 return;
172 si = gsi_last (*dst_p);
173 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
177 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
178 and popping gimplify contexts. */
180 static struct gimplify_ctx *ctx_pool = NULL;
182 /* Return a gimplify context struct from the pool. */
184 static inline struct gimplify_ctx *
185 ctx_alloc (void)
187 struct gimplify_ctx * c = ctx_pool;
189 if (c)
190 ctx_pool = c->prev_context;
191 else
192 c = XNEW (struct gimplify_ctx);
194 memset (c, '\0', sizeof (*c));
195 return c;
198 /* Put gimplify context C back into the pool. */
200 static inline void
201 ctx_free (struct gimplify_ctx *c)
203 c->prev_context = ctx_pool;
204 ctx_pool = c;
207 /* Free allocated ctx stack memory. */
209 void
210 free_gimplify_stack (void)
212 struct gimplify_ctx *c;
214 while ((c = ctx_pool))
216 ctx_pool = c->prev_context;
217 free (c);
222 /* Set up a context for the gimplifier. */
224 void
225 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
227 struct gimplify_ctx *c = ctx_alloc ();
229 c->prev_context = gimplify_ctxp;
230 gimplify_ctxp = c;
231 gimplify_ctxp->into_ssa = in_ssa;
232 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
235 /* Tear down a context for the gimplifier. If BODY is non-null, then
236 put the temporaries into the outer BIND_EXPR. Otherwise, put them
237 in the local_decls.
239 BODY is not a sequence, but the first tuple in a sequence. */
241 void
242 pop_gimplify_context (gimple body)
244 struct gimplify_ctx *c = gimplify_ctxp;
246 gcc_assert (c
247 && (!c->bind_expr_stack.exists ()
248 || c->bind_expr_stack.is_empty ()));
249 c->bind_expr_stack.release ();
250 gimplify_ctxp = c->prev_context;
252 if (body)
253 declare_vars (c->temps, body, false);
254 else
255 record_vars (c->temps);
257 if (c->temp_htab.is_created ())
258 c->temp_htab.dispose ();
259 ctx_free (c);
262 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
264 static void
265 gimple_push_bind_expr (gimple gimple_bind)
267 gimplify_ctxp->bind_expr_stack.reserve (8);
268 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
271 /* Pop the first element off the stack of bindings. */
273 static void
274 gimple_pop_bind_expr (void)
276 gimplify_ctxp->bind_expr_stack.pop ();
279 /* Return the first element of the stack of bindings. */
281 gimple
282 gimple_current_bind_expr (void)
284 return gimplify_ctxp->bind_expr_stack.last ();
287 /* Return the stack of bindings created during gimplification. */
289 vec<gimple>
290 gimple_bind_expr_stack (void)
292 return gimplify_ctxp->bind_expr_stack;
295 /* Return true iff there is a COND_EXPR between us and the innermost
296 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
298 static bool
299 gimple_conditional_context (void)
301 return gimplify_ctxp->conditions > 0;
304 /* Note that we've entered a COND_EXPR. */
306 static void
307 gimple_push_condition (void)
309 #ifdef ENABLE_GIMPLE_CHECKING
310 if (gimplify_ctxp->conditions == 0)
311 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
312 #endif
313 ++(gimplify_ctxp->conditions);
316 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
317 now, add any conditional cleanups we've seen to the prequeue. */
319 static void
320 gimple_pop_condition (gimple_seq *pre_p)
322 int conds = --(gimplify_ctxp->conditions);
324 gcc_assert (conds >= 0);
325 if (conds == 0)
327 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
328 gimplify_ctxp->conditional_cleanups = NULL;
332 /* A stable comparison routine for use with splay trees and DECLs. */
334 static int
335 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
337 tree a = (tree) xa;
338 tree b = (tree) xb;
340 return DECL_UID (a) - DECL_UID (b);
343 /* Create a new omp construct that deals with variable remapping. */
345 static struct gimplify_omp_ctx *
346 new_omp_context (enum omp_region_type region_type)
348 struct gimplify_omp_ctx *c;
350 c = XCNEW (struct gimplify_omp_ctx);
351 c->outer_context = gimplify_omp_ctxp;
352 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
353 c->privatized_types = pointer_set_create ();
354 c->location = input_location;
355 c->region_type = region_type;
356 if ((region_type & ORT_TASK) == 0)
357 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
358 else
359 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
361 return c;
364 /* Destroy an omp construct that deals with variable remapping. */
366 static void
367 delete_omp_context (struct gimplify_omp_ctx *c)
369 splay_tree_delete (c->variables);
370 pointer_set_destroy (c->privatized_types);
371 XDELETE (c);
374 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
375 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
377 /* Both gimplify the statement T and append it to *SEQ_P. This function
378 behaves exactly as gimplify_stmt, but you don't have to pass T as a
379 reference. */
381 void
382 gimplify_and_add (tree t, gimple_seq *seq_p)
384 gimplify_stmt (&t, seq_p);
387 /* Gimplify statement T into sequence *SEQ_P, and return the first
388 tuple in the sequence of generated tuples for this statement.
389 Return NULL if gimplifying T produced no tuples. */
391 static gimple
392 gimplify_and_return_first (tree t, gimple_seq *seq_p)
394 gimple_stmt_iterator last = gsi_last (*seq_p);
396 gimplify_and_add (t, seq_p);
398 if (!gsi_end_p (last))
400 gsi_next (&last);
401 return gsi_stmt (last);
403 else
404 return gimple_seq_first_stmt (*seq_p);
407 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
408 LHS, or for a call argument. */
410 static bool
411 is_gimple_mem_rhs (tree t)
413 /* If we're dealing with a renamable type, either source or dest must be
414 a renamed variable. */
415 if (is_gimple_reg_type (TREE_TYPE (t)))
416 return is_gimple_val (t);
417 else
418 return is_gimple_val (t) || is_gimple_lvalue (t);
421 /* Return true if T is a CALL_EXPR or an expression that can be
422 assigned to a temporary. Note that this predicate should only be
423 used during gimplification. See the rationale for this in
424 gimplify_modify_expr. */
426 static bool
427 is_gimple_reg_rhs_or_call (tree t)
429 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
430 || TREE_CODE (t) == CALL_EXPR);
433 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
434 this predicate should only be used during gimplification. See the
435 rationale for this in gimplify_modify_expr. */
437 static bool
438 is_gimple_mem_rhs_or_call (tree t)
440 /* If we're dealing with a renamable type, either source or dest must be
441 a renamed variable. */
442 if (is_gimple_reg_type (TREE_TYPE (t)))
443 return is_gimple_val (t);
444 else
445 return (is_gimple_val (t) || is_gimple_lvalue (t)
446 || TREE_CODE (t) == CALL_EXPR);
449 /* Create a temporary with a name derived from VAL. Subroutine of
450 lookup_tmp_var; nobody else should call this function. */
452 static inline tree
453 create_tmp_from_val (tree val, bool is_formal)
455 /* Drop all qualifiers and address-space information from the value type. */
456 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
457 tree var = create_tmp_var (type, get_name (val));
458 if (is_formal
459 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
460 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
461 DECL_GIMPLE_REG_P (var) = 1;
462 return var;
465 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
466 an existing expression temporary. */
468 static tree
469 lookup_tmp_var (tree val, bool is_formal)
471 tree ret;
473 /* If not optimizing, never really reuse a temporary. local-alloc
474 won't allocate any variable that is used in more than one basic
475 block, which means it will go into memory, causing much extra
476 work in reload and final and poorer code generation, outweighing
477 the extra memory allocation here. */
478 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
479 ret = create_tmp_from_val (val, is_formal);
480 else
482 elt_t elt, *elt_p;
483 elt_t **slot;
485 elt.val = val;
486 if (!gimplify_ctxp->temp_htab.is_created ())
487 gimplify_ctxp->temp_htab.create (1000);
488 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
489 if (*slot == NULL)
491 elt_p = XNEW (elt_t);
492 elt_p->val = val;
493 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
494 *slot = elt_p;
496 else
498 elt_p = *slot;
499 ret = elt_p->temp;
503 return ret;
506 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
508 static tree
509 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
510 bool is_formal)
512 tree t, mod;
514 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
515 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
516 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
517 fb_rvalue);
519 if (gimplify_ctxp->into_ssa
520 && is_gimple_reg_type (TREE_TYPE (val)))
521 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
522 else
523 t = lookup_tmp_var (val, is_formal);
525 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
527 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
529 /* gimplify_modify_expr might want to reduce this further. */
530 gimplify_and_add (mod, pre_p);
531 ggc_free (mod);
533 return t;
536 /* Return a formal temporary variable initialized with VAL. PRE_P is as
537 in gimplify_expr. Only use this function if:
539 1) The value of the unfactored expression represented by VAL will not
540 change between the initialization and use of the temporary, and
541 2) The temporary will not be otherwise modified.
543 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
544 and #2 means it is inappropriate for && temps.
546 For other cases, use get_initialized_tmp_var instead. */
548 tree
549 get_formal_tmp_var (tree val, gimple_seq *pre_p)
551 return internal_get_tmp_var (val, pre_p, NULL, true);
554 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
555 are as in gimplify_expr. */
557 tree
558 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
560 return internal_get_tmp_var (val, pre_p, post_p, false);
563 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
564 generate debug info for them; otherwise don't. */
566 void
567 declare_vars (tree vars, gimple scope, bool debug_info)
569 tree last = vars;
570 if (last)
572 tree temps, block;
574 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
576 temps = nreverse (last);
578 block = gimple_bind_block (scope);
579 gcc_assert (!block || TREE_CODE (block) == BLOCK);
580 if (!block || !debug_info)
582 DECL_CHAIN (last) = gimple_bind_vars (scope);
583 gimple_bind_set_vars (scope, temps);
585 else
587 /* We need to attach the nodes both to the BIND_EXPR and to its
588 associated BLOCK for debugging purposes. The key point here
589 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
590 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
591 if (BLOCK_VARS (block))
592 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
593 else
595 gimple_bind_set_vars (scope,
596 chainon (gimple_bind_vars (scope), temps));
597 BLOCK_VARS (block) = temps;
603 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
604 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
605 no such upper bound can be obtained. */
607 static void
608 force_constant_size (tree var)
610 /* The only attempt we make is by querying the maximum size of objects
611 of the variable's type. */
613 HOST_WIDE_INT max_size;
615 gcc_assert (TREE_CODE (var) == VAR_DECL);
617 max_size = max_int_size_in_bytes (TREE_TYPE (var));
619 gcc_assert (max_size >= 0);
621 DECL_SIZE_UNIT (var)
622 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
623 DECL_SIZE (var)
624 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
627 /* Push the temporary variable TMP into the current binding. */
629 void
630 gimple_add_tmp_var (tree tmp)
632 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
634 /* Later processing assumes that the object size is constant, which might
635 not be true at this point. Force the use of a constant upper bound in
636 this case. */
637 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
638 force_constant_size (tmp);
640 DECL_CONTEXT (tmp) = current_function_decl;
641 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
643 if (gimplify_ctxp)
645 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
646 gimplify_ctxp->temps = tmp;
648 /* Mark temporaries local within the nearest enclosing parallel. */
649 if (gimplify_omp_ctxp)
651 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
652 while (ctx
653 && (ctx->region_type == ORT_WORKSHARE
654 || ctx->region_type == ORT_SIMD))
655 ctx = ctx->outer_context;
656 if (ctx)
657 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
660 else if (cfun)
661 record_vars (tmp);
662 else
664 gimple_seq body_seq;
666 /* This case is for nested functions. We need to expose the locals
667 they create. */
668 body_seq = gimple_body (current_function_decl);
669 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
675 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
676 nodes that are referenced more than once in GENERIC functions. This is
677 necessary because gimplification (translation into GIMPLE) is performed
678 by modifying tree nodes in-place, so gimplication of a shared node in a
679 first context could generate an invalid GIMPLE form in a second context.
681 This is achieved with a simple mark/copy/unmark algorithm that walks the
682 GENERIC representation top-down, marks nodes with TREE_VISITED the first
683 time it encounters them, duplicates them if they already have TREE_VISITED
684 set, and finally removes the TREE_VISITED marks it has set.
686 The algorithm works only at the function level, i.e. it generates a GENERIC
687 representation of a function with no nodes shared within the function when
688 passed a GENERIC function (except for nodes that are allowed to be shared).
690 At the global level, it is also necessary to unshare tree nodes that are
691 referenced in more than one function, for the same aforementioned reason.
692 This requires some cooperation from the front-end. There are 2 strategies:
694 1. Manual unsharing. The front-end needs to call unshare_expr on every
695 expression that might end up being shared across functions.
697 2. Deep unsharing. This is an extension of regular unsharing. Instead
698 of calling unshare_expr on expressions that might be shared across
699 functions, the front-end pre-marks them with TREE_VISITED. This will
700 ensure that they are unshared on the first reference within functions
701 when the regular unsharing algorithm runs. The counterpart is that
702 this algorithm must look deeper than for manual unsharing, which is
703 specified by LANG_HOOKS_DEEP_UNSHARING.
705 If there are only few specific cases of node sharing across functions, it is
706 probably easier for a front-end to unshare the expressions manually. On the
707 contrary, if the expressions generated at the global level are as widespread
708 as expressions generated within functions, deep unsharing is very likely the
709 way to go. */
711 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
712 These nodes model computations that must be done once. If we were to
713 unshare something like SAVE_EXPR(i++), the gimplification process would
714 create wrong code. However, if DATA is non-null, it must hold a pointer
715 set that is used to unshare the subtrees of these nodes. */
717 static tree
718 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
720 tree t = *tp;
721 enum tree_code code = TREE_CODE (t);
723 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
724 copy their subtrees if we can make sure to do it only once. */
725 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
727 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
729 else
730 *walk_subtrees = 0;
733 /* Stop at types, decls, constants like copy_tree_r. */
734 else if (TREE_CODE_CLASS (code) == tcc_type
735 || TREE_CODE_CLASS (code) == tcc_declaration
736 || TREE_CODE_CLASS (code) == tcc_constant
737 /* We can't do anything sensible with a BLOCK used as an
738 expression, but we also can't just die when we see it
739 because of non-expression uses. So we avert our eyes
740 and cross our fingers. Silly Java. */
741 || code == BLOCK)
742 *walk_subtrees = 0;
744 /* Cope with the statement expression extension. */
745 else if (code == STATEMENT_LIST)
748 /* Leave the bulk of the work to copy_tree_r itself. */
749 else
750 copy_tree_r (tp, walk_subtrees, NULL);
752 return NULL_TREE;
755 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
756 If *TP has been visited already, then *TP is deeply copied by calling
757 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
759 static tree
760 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
762 tree t = *tp;
763 enum tree_code code = TREE_CODE (t);
765 /* Skip types, decls, and constants. But we do want to look at their
766 types and the bounds of types. Mark them as visited so we properly
767 unmark their subtrees on the unmark pass. If we've already seen them,
768 don't look down further. */
769 if (TREE_CODE_CLASS (code) == tcc_type
770 || TREE_CODE_CLASS (code) == tcc_declaration
771 || TREE_CODE_CLASS (code) == tcc_constant)
773 if (TREE_VISITED (t))
774 *walk_subtrees = 0;
775 else
776 TREE_VISITED (t) = 1;
779 /* If this node has been visited already, unshare it and don't look
780 any deeper. */
781 else if (TREE_VISITED (t))
783 walk_tree (tp, mostly_copy_tree_r, data, NULL);
784 *walk_subtrees = 0;
787 /* Otherwise, mark the node as visited and keep looking. */
788 else
789 TREE_VISITED (t) = 1;
791 return NULL_TREE;
794 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
795 copy_if_shared_r callback unmodified. */
797 static inline void
798 copy_if_shared (tree *tp, void *data)
800 walk_tree (tp, copy_if_shared_r, data, NULL);
803 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
804 any nested functions. */
806 static void
807 unshare_body (tree fndecl)
809 struct cgraph_node *cgn = cgraph_get_node (fndecl);
810 /* If the language requires deep unsharing, we need a pointer set to make
811 sure we don't repeatedly unshare subtrees of unshareable nodes. */
812 struct pointer_set_t *visited
813 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
815 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
816 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
817 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
819 if (visited)
820 pointer_set_destroy (visited);
822 if (cgn)
823 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
824 unshare_body (cgn->decl);
827 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
828 Subtrees are walked until the first unvisited node is encountered. */
830 static tree
831 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
833 tree t = *tp;
835 /* If this node has been visited, unmark it and keep looking. */
836 if (TREE_VISITED (t))
837 TREE_VISITED (t) = 0;
839 /* Otherwise, don't look any deeper. */
840 else
841 *walk_subtrees = 0;
843 return NULL_TREE;
846 /* Unmark the visited trees rooted at *TP. */
848 static inline void
849 unmark_visited (tree *tp)
851 walk_tree (tp, unmark_visited_r, NULL, NULL);
854 /* Likewise, but mark all trees as not visited. */
856 static void
857 unvisit_body (tree fndecl)
859 struct cgraph_node *cgn = cgraph_get_node (fndecl);
861 unmark_visited (&DECL_SAVED_TREE (fndecl));
862 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
863 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
865 if (cgn)
866 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
867 unvisit_body (cgn->decl);
870 /* Unconditionally make an unshared copy of EXPR. This is used when using
871 stored expressions which span multiple functions, such as BINFO_VTABLE,
872 as the normal unsharing process can't tell that they're shared. */
874 tree
875 unshare_expr (tree expr)
877 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
878 return expr;
881 /* Worker for unshare_expr_without_location. */
883 static tree
884 prune_expr_location (tree *tp, int *walk_subtrees, void *)
886 if (EXPR_P (*tp))
887 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
888 else
889 *walk_subtrees = 0;
890 return NULL_TREE;
893 /* Similar to unshare_expr but also prune all expression locations
894 from EXPR. */
896 tree
897 unshare_expr_without_location (tree expr)
899 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
900 if (EXPR_P (expr))
901 walk_tree (&expr, prune_expr_location, NULL, NULL);
902 return expr;
905 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
906 contain statements and have a value. Assign its value to a temporary
907 and give it void_type_node. Return the temporary, or NULL_TREE if
908 WRAPPER was already void. */
910 tree
911 voidify_wrapper_expr (tree wrapper, tree temp)
913 tree type = TREE_TYPE (wrapper);
914 if (type && !VOID_TYPE_P (type))
916 tree *p;
918 /* Set p to point to the body of the wrapper. Loop until we find
919 something that isn't a wrapper. */
920 for (p = &wrapper; p && *p; )
922 switch (TREE_CODE (*p))
924 case BIND_EXPR:
925 TREE_SIDE_EFFECTS (*p) = 1;
926 TREE_TYPE (*p) = void_type_node;
927 /* For a BIND_EXPR, the body is operand 1. */
928 p = &BIND_EXPR_BODY (*p);
929 break;
931 case CLEANUP_POINT_EXPR:
932 case TRY_FINALLY_EXPR:
933 case TRY_CATCH_EXPR:
934 TREE_SIDE_EFFECTS (*p) = 1;
935 TREE_TYPE (*p) = void_type_node;
936 p = &TREE_OPERAND (*p, 0);
937 break;
939 case STATEMENT_LIST:
941 tree_stmt_iterator i = tsi_last (*p);
942 TREE_SIDE_EFFECTS (*p) = 1;
943 TREE_TYPE (*p) = void_type_node;
944 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
946 break;
948 case COMPOUND_EXPR:
949 /* Advance to the last statement. Set all container types to
950 void. */
951 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
953 TREE_SIDE_EFFECTS (*p) = 1;
954 TREE_TYPE (*p) = void_type_node;
956 break;
958 case TRANSACTION_EXPR:
959 TREE_SIDE_EFFECTS (*p) = 1;
960 TREE_TYPE (*p) = void_type_node;
961 p = &TRANSACTION_EXPR_BODY (*p);
962 break;
964 default:
965 /* Assume that any tree upon which voidify_wrapper_expr is
966 directly called is a wrapper, and that its body is op0. */
967 if (p == &wrapper)
969 TREE_SIDE_EFFECTS (*p) = 1;
970 TREE_TYPE (*p) = void_type_node;
971 p = &TREE_OPERAND (*p, 0);
972 break;
974 goto out;
978 out:
979 if (p == NULL || IS_EMPTY_STMT (*p))
980 temp = NULL_TREE;
981 else if (temp)
983 /* The wrapper is on the RHS of an assignment that we're pushing
984 down. */
985 gcc_assert (TREE_CODE (temp) == INIT_EXPR
986 || TREE_CODE (temp) == MODIFY_EXPR);
987 TREE_OPERAND (temp, 1) = *p;
988 *p = temp;
990 else
992 temp = create_tmp_var (type, "retval");
993 *p = build2 (INIT_EXPR, type, temp, *p);
996 return temp;
999 return NULL_TREE;
1002 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1003 a temporary through which they communicate. */
1005 static void
1006 build_stack_save_restore (gimple *save, gimple *restore)
1008 tree tmp_var;
1010 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1011 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1012 gimple_call_set_lhs (*save, tmp_var);
1014 *restore
1015 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1016 1, tmp_var);
1019 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1021 static enum gimplify_status
1022 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1024 tree bind_expr = *expr_p;
1025 bool old_save_stack = gimplify_ctxp->save_stack;
1026 tree t;
1027 gimple gimple_bind;
1028 gimple_seq body, cleanup;
1029 gimple stack_save;
1031 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1033 /* Mark variables seen in this bind expr. */
1034 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1036 if (TREE_CODE (t) == VAR_DECL)
1038 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1040 /* Mark variable as local. */
1041 if (ctx && !DECL_EXTERNAL (t)
1042 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1043 || splay_tree_lookup (ctx->variables,
1044 (splay_tree_key) t) == NULL))
1046 if (ctx->region_type == ORT_SIMD
1047 && TREE_ADDRESSABLE (t)
1048 && !TREE_STATIC (t))
1049 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1050 else
1051 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1054 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1056 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1057 cfun->has_local_explicit_reg_vars = true;
1060 /* Preliminarily mark non-addressed complex variables as eligible
1061 for promotion to gimple registers. We'll transform their uses
1062 as we find them. */
1063 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1064 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1065 && !TREE_THIS_VOLATILE (t)
1066 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1067 && !needs_to_live_in_memory (t))
1068 DECL_GIMPLE_REG_P (t) = 1;
1071 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1072 BIND_EXPR_BLOCK (bind_expr));
1073 gimple_push_bind_expr (gimple_bind);
1075 gimplify_ctxp->save_stack = false;
1077 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1078 body = NULL;
1079 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1080 gimple_bind_set_body (gimple_bind, body);
1082 cleanup = NULL;
1083 stack_save = NULL;
1084 if (gimplify_ctxp->save_stack)
1086 gimple stack_restore;
1088 /* Save stack on entry and restore it on exit. Add a try_finally
1089 block to achieve this. */
1090 build_stack_save_restore (&stack_save, &stack_restore);
1092 gimplify_seq_add_stmt (&cleanup, stack_restore);
1095 /* Add clobbers for all variables that go out of scope. */
1096 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1098 if (TREE_CODE (t) == VAR_DECL
1099 && !is_global_var (t)
1100 && DECL_CONTEXT (t) == current_function_decl
1101 && !DECL_HARD_REGISTER (t)
1102 && !TREE_THIS_VOLATILE (t)
1103 && !DECL_HAS_VALUE_EXPR_P (t)
1104 /* Only care for variables that have to be in memory. Others
1105 will be rewritten into SSA names, hence moved to the top-level. */
1106 && !is_gimple_reg (t)
1107 && flag_stack_reuse != SR_NONE)
1109 tree clobber = build_constructor (TREE_TYPE (t),
1110 NULL);
1111 TREE_THIS_VOLATILE (clobber) = 1;
1112 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1116 if (cleanup)
1118 gimple gs;
1119 gimple_seq new_body;
1121 new_body = NULL;
1122 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1123 GIMPLE_TRY_FINALLY);
1125 if (stack_save)
1126 gimplify_seq_add_stmt (&new_body, stack_save);
1127 gimplify_seq_add_stmt (&new_body, gs);
1128 gimple_bind_set_body (gimple_bind, new_body);
1131 gimplify_ctxp->save_stack = old_save_stack;
1132 gimple_pop_bind_expr ();
1134 gimplify_seq_add_stmt (pre_p, gimple_bind);
1136 if (temp)
1138 *expr_p = temp;
1139 return GS_OK;
1142 *expr_p = NULL_TREE;
1143 return GS_ALL_DONE;
1146 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1147 GIMPLE value, it is assigned to a new temporary and the statement is
1148 re-written to return the temporary.
1150 PRE_P points to the sequence where side effects that must happen before
1151 STMT should be stored. */
1153 static enum gimplify_status
1154 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1156 gimple ret;
1157 tree ret_expr = TREE_OPERAND (stmt, 0);
1158 tree result_decl, result;
1160 if (ret_expr == error_mark_node)
1161 return GS_ERROR;
1163 /* Implicit _Cilk_sync must be inserted right before any return statement
1164 if there is a _Cilk_spawn in the function. If the user has provided a
1165 _Cilk_sync, the optimizer should remove this duplicate one. */
1166 if (fn_contains_cilk_spawn_p (cfun))
1168 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1169 gimplify_and_add (impl_sync, pre_p);
1172 if (!ret_expr
1173 || TREE_CODE (ret_expr) == RESULT_DECL
1174 || ret_expr == error_mark_node)
1176 gimple ret = gimple_build_return (ret_expr);
1177 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1178 gimplify_seq_add_stmt (pre_p, ret);
1179 return GS_ALL_DONE;
1182 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1183 result_decl = NULL_TREE;
1184 else
1186 result_decl = TREE_OPERAND (ret_expr, 0);
1188 /* See through a return by reference. */
1189 if (TREE_CODE (result_decl) == INDIRECT_REF)
1190 result_decl = TREE_OPERAND (result_decl, 0);
1192 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1193 || TREE_CODE (ret_expr) == INIT_EXPR)
1194 && TREE_CODE (result_decl) == RESULT_DECL);
1197 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1198 Recall that aggregate_value_p is FALSE for any aggregate type that is
1199 returned in registers. If we're returning values in registers, then
1200 we don't want to extend the lifetime of the RESULT_DECL, particularly
1201 across another call. In addition, for those aggregates for which
1202 hard_function_value generates a PARALLEL, we'll die during normal
1203 expansion of structure assignments; there's special code in expand_return
1204 to handle this case that does not exist in expand_expr. */
1205 if (!result_decl)
1206 result = NULL_TREE;
1207 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1209 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1211 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1212 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1213 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1214 should be effectively allocated by the caller, i.e. all calls to
1215 this function must be subject to the Return Slot Optimization. */
1216 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1217 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1219 result = result_decl;
1221 else if (gimplify_ctxp->return_temp)
1222 result = gimplify_ctxp->return_temp;
1223 else
1225 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1227 /* ??? With complex control flow (usually involving abnormal edges),
1228 we can wind up warning about an uninitialized value for this. Due
1229 to how this variable is constructed and initialized, this is never
1230 true. Give up and never warn. */
1231 TREE_NO_WARNING (result) = 1;
1233 gimplify_ctxp->return_temp = result;
1236 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1237 Then gimplify the whole thing. */
1238 if (result != result_decl)
1239 TREE_OPERAND (ret_expr, 0) = result;
1241 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1243 ret = gimple_build_return (result);
1244 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1245 gimplify_seq_add_stmt (pre_p, ret);
1247 return GS_ALL_DONE;
1250 /* Gimplify a variable-length array DECL. */
1252 static void
1253 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1255 /* This is a variable-sized decl. Simplify its size and mark it
1256 for deferred expansion. */
1257 tree t, addr, ptr_type;
1259 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1260 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1262 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1263 if (DECL_HAS_VALUE_EXPR_P (decl))
1264 return;
1266 /* All occurrences of this decl in final gimplified code will be
1267 replaced by indirection. Setting DECL_VALUE_EXPR does two
1268 things: First, it lets the rest of the gimplifier know what
1269 replacement to use. Second, it lets the debug info know
1270 where to find the value. */
1271 ptr_type = build_pointer_type (TREE_TYPE (decl));
1272 addr = create_tmp_var (ptr_type, get_name (decl));
1273 DECL_IGNORED_P (addr) = 0;
1274 t = build_fold_indirect_ref (addr);
1275 TREE_THIS_NOTRAP (t) = 1;
1276 SET_DECL_VALUE_EXPR (decl, t);
1277 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1279 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1280 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1281 size_int (DECL_ALIGN (decl)));
1282 /* The call has been built for a variable-sized object. */
1283 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1284 t = fold_convert (ptr_type, t);
1285 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1287 gimplify_and_add (t, seq_p);
1289 /* Indicate that we need to restore the stack level when the
1290 enclosing BIND_EXPR is exited. */
1291 gimplify_ctxp->save_stack = true;
1294 /* A helper function to be called via walk_tree. Mark all labels under *TP
1295 as being forced. To be called for DECL_INITIAL of static variables. */
1297 static tree
1298 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1300 if (TYPE_P (*tp))
1301 *walk_subtrees = 0;
1302 if (TREE_CODE (*tp) == LABEL_DECL)
1303 FORCED_LABEL (*tp) = 1;
1305 return NULL_TREE;
1308 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1309 and initialization explicit. */
1311 static enum gimplify_status
1312 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1314 tree stmt = *stmt_p;
1315 tree decl = DECL_EXPR_DECL (stmt);
1317 *stmt_p = NULL_TREE;
1319 if (TREE_TYPE (decl) == error_mark_node)
1320 return GS_ERROR;
1322 if ((TREE_CODE (decl) == TYPE_DECL
1323 || TREE_CODE (decl) == VAR_DECL)
1324 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1325 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1327 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1328 in case its size expressions contain problematic nodes like CALL_EXPR. */
1329 if (TREE_CODE (decl) == TYPE_DECL
1330 && DECL_ORIGINAL_TYPE (decl)
1331 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1332 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1334 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1336 tree init = DECL_INITIAL (decl);
1338 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1339 || (!TREE_STATIC (decl)
1340 && flag_stack_check == GENERIC_STACK_CHECK
1341 && compare_tree_int (DECL_SIZE_UNIT (decl),
1342 STACK_CHECK_MAX_VAR_SIZE) > 0))
1343 gimplify_vla_decl (decl, seq_p);
1345 /* Some front ends do not explicitly declare all anonymous
1346 artificial variables. We compensate here by declaring the
1347 variables, though it would be better if the front ends would
1348 explicitly declare them. */
1349 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1350 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1351 gimple_add_tmp_var (decl);
1353 if (init && init != error_mark_node)
1355 if (!TREE_STATIC (decl))
1357 DECL_INITIAL (decl) = NULL_TREE;
1358 init = build2 (INIT_EXPR, void_type_node, decl, init);
1359 gimplify_and_add (init, seq_p);
1360 ggc_free (init);
1362 else
1363 /* We must still examine initializers for static variables
1364 as they may contain a label address. */
1365 walk_tree (&init, force_labels_r, NULL, NULL);
1369 return GS_ALL_DONE;
1372 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1373 and replacing the LOOP_EXPR with goto, but if the loop contains an
1374 EXIT_EXPR, we need to append a label for it to jump to. */
1376 static enum gimplify_status
1377 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1379 tree saved_label = gimplify_ctxp->exit_label;
1380 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1382 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1384 gimplify_ctxp->exit_label = NULL_TREE;
1386 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1388 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1390 if (gimplify_ctxp->exit_label)
1391 gimplify_seq_add_stmt (pre_p,
1392 gimple_build_label (gimplify_ctxp->exit_label));
1394 gimplify_ctxp->exit_label = saved_label;
1396 *expr_p = NULL;
1397 return GS_ALL_DONE;
1400 /* Gimplify a statement list onto a sequence. These may be created either
1401 by an enlightened front-end, or by shortcut_cond_expr. */
1403 static enum gimplify_status
1404 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1406 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1408 tree_stmt_iterator i = tsi_start (*expr_p);
1410 while (!tsi_end_p (i))
1412 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1413 tsi_delink (&i);
1416 if (temp)
1418 *expr_p = temp;
1419 return GS_OK;
1422 return GS_ALL_DONE;
1426 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1427 branch to. */
1429 static enum gimplify_status
1430 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1432 tree switch_expr = *expr_p;
1433 gimple_seq switch_body_seq = NULL;
1434 enum gimplify_status ret;
1435 tree index_type = TREE_TYPE (switch_expr);
1436 if (index_type == NULL_TREE)
1437 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1439 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1440 fb_rvalue);
1441 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1442 return ret;
1444 if (SWITCH_BODY (switch_expr))
1446 vec<tree> labels;
1447 vec<tree> saved_labels;
1448 tree default_case = NULL_TREE;
1449 gimple gimple_switch;
1451 /* If someone can be bothered to fill in the labels, they can
1452 be bothered to null out the body too. */
1453 gcc_assert (!SWITCH_LABELS (switch_expr));
1455 /* Save old labels, get new ones from body, then restore the old
1456 labels. Save all the things from the switch body to append after. */
1457 saved_labels = gimplify_ctxp->case_labels;
1458 gimplify_ctxp->case_labels.create (8);
1460 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1461 labels = gimplify_ctxp->case_labels;
1462 gimplify_ctxp->case_labels = saved_labels;
1464 preprocess_case_label_vec_for_gimple (labels, index_type,
1465 &default_case);
1467 if (!default_case)
1469 gimple new_default;
1471 default_case
1472 = build_case_label (NULL_TREE, NULL_TREE,
1473 create_artificial_label (UNKNOWN_LOCATION));
1474 new_default = gimple_build_label (CASE_LABEL (default_case));
1475 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1478 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1479 default_case, labels);
1480 gimplify_seq_add_stmt (pre_p, gimple_switch);
1481 gimplify_seq_add_seq (pre_p, switch_body_seq);
1482 labels.release ();
1484 else
1485 gcc_assert (SWITCH_LABELS (switch_expr));
1487 return GS_ALL_DONE;
1490 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1492 static enum gimplify_status
1493 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1495 struct gimplify_ctx *ctxp;
1496 gimple gimple_label;
1498 /* Invalid OpenMP programs can play Duff's Device type games with
1499 #pragma omp parallel. At least in the C front end, we don't
1500 detect such invalid branches until after gimplification. */
1501 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1502 if (ctxp->case_labels.exists ())
1503 break;
1505 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1506 ctxp->case_labels.safe_push (*expr_p);
1507 gimplify_seq_add_stmt (pre_p, gimple_label);
1509 return GS_ALL_DONE;
1512 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1513 if necessary. */
1515 tree
1516 build_and_jump (tree *label_p)
1518 if (label_p == NULL)
1519 /* If there's nowhere to jump, just fall through. */
1520 return NULL_TREE;
1522 if (*label_p == NULL_TREE)
1524 tree label = create_artificial_label (UNKNOWN_LOCATION);
1525 *label_p = label;
1528 return build1 (GOTO_EXPR, void_type_node, *label_p);
1531 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1532 This also involves building a label to jump to and communicating it to
1533 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1535 static enum gimplify_status
1536 gimplify_exit_expr (tree *expr_p)
1538 tree cond = TREE_OPERAND (*expr_p, 0);
1539 tree expr;
1541 expr = build_and_jump (&gimplify_ctxp->exit_label);
1542 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1543 *expr_p = expr;
1545 return GS_OK;
1548 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1549 different from its canonical type, wrap the whole thing inside a
1550 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1551 type.
1553 The canonical type of a COMPONENT_REF is the type of the field being
1554 referenced--unless the field is a bit-field which can be read directly
1555 in a smaller mode, in which case the canonical type is the
1556 sign-appropriate type corresponding to that mode. */
1558 static void
1559 canonicalize_component_ref (tree *expr_p)
1561 tree expr = *expr_p;
1562 tree type;
1564 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1566 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1567 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1568 else
1569 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1571 /* One could argue that all the stuff below is not necessary for
1572 the non-bitfield case and declare it a FE error if type
1573 adjustment would be needed. */
1574 if (TREE_TYPE (expr) != type)
1576 #ifdef ENABLE_TYPES_CHECKING
1577 tree old_type = TREE_TYPE (expr);
1578 #endif
1579 int type_quals;
1581 /* We need to preserve qualifiers and propagate them from
1582 operand 0. */
1583 type_quals = TYPE_QUALS (type)
1584 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1585 if (TYPE_QUALS (type) != type_quals)
1586 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1588 /* Set the type of the COMPONENT_REF to the underlying type. */
1589 TREE_TYPE (expr) = type;
1591 #ifdef ENABLE_TYPES_CHECKING
1592 /* It is now a FE error, if the conversion from the canonical
1593 type to the original expression type is not useless. */
1594 gcc_assert (useless_type_conversion_p (old_type, type));
1595 #endif
1599 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1600 to foo, embed that change in the ADDR_EXPR by converting
1601 T array[U];
1602 (T *)&array
1604 &array[L]
1605 where L is the lower bound. For simplicity, only do this for constant
1606 lower bound.
1607 The constraint is that the type of &array[L] is trivially convertible
1608 to T *. */
1610 static void
1611 canonicalize_addr_expr (tree *expr_p)
1613 tree expr = *expr_p;
1614 tree addr_expr = TREE_OPERAND (expr, 0);
1615 tree datype, ddatype, pddatype;
1617 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1618 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1619 || TREE_CODE (addr_expr) != ADDR_EXPR)
1620 return;
1622 /* The addr_expr type should be a pointer to an array. */
1623 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1624 if (TREE_CODE (datype) != ARRAY_TYPE)
1625 return;
1627 /* The pointer to element type shall be trivially convertible to
1628 the expression pointer type. */
1629 ddatype = TREE_TYPE (datype);
1630 pddatype = build_pointer_type (ddatype);
1631 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1632 pddatype))
1633 return;
1635 /* The lower bound and element sizes must be constant. */
1636 if (!TYPE_SIZE_UNIT (ddatype)
1637 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1638 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1639 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1640 return;
1642 /* All checks succeeded. Build a new node to merge the cast. */
1643 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1644 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1645 NULL_TREE, NULL_TREE);
1646 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1648 /* We can have stripped a required restrict qualifier above. */
1649 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1650 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1653 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1654 underneath as appropriate. */
1656 static enum gimplify_status
1657 gimplify_conversion (tree *expr_p)
1659 location_t loc = EXPR_LOCATION (*expr_p);
1660 gcc_assert (CONVERT_EXPR_P (*expr_p));
1662 /* Then strip away all but the outermost conversion. */
1663 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1665 /* And remove the outermost conversion if it's useless. */
1666 if (tree_ssa_useless_type_conversion (*expr_p))
1667 *expr_p = TREE_OPERAND (*expr_p, 0);
1669 /* If we still have a conversion at the toplevel,
1670 then canonicalize some constructs. */
1671 if (CONVERT_EXPR_P (*expr_p))
1673 tree sub = TREE_OPERAND (*expr_p, 0);
1675 /* If a NOP conversion is changing the type of a COMPONENT_REF
1676 expression, then canonicalize its type now in order to expose more
1677 redundant conversions. */
1678 if (TREE_CODE (sub) == COMPONENT_REF)
1679 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1681 /* If a NOP conversion is changing a pointer to array of foo
1682 to a pointer to foo, embed that change in the ADDR_EXPR. */
1683 else if (TREE_CODE (sub) == ADDR_EXPR)
1684 canonicalize_addr_expr (expr_p);
1687 /* If we have a conversion to a non-register type force the
1688 use of a VIEW_CONVERT_EXPR instead. */
1689 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1690 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1691 TREE_OPERAND (*expr_p, 0));
1693 return GS_OK;
1696 /* Nonlocal VLAs seen in the current function. */
1697 static struct pointer_set_t *nonlocal_vlas;
1699 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1700 static tree nonlocal_vla_vars;
1702 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1703 DECL_VALUE_EXPR, and it's worth re-examining things. */
1705 static enum gimplify_status
1706 gimplify_var_or_parm_decl (tree *expr_p)
1708 tree decl = *expr_p;
1710 /* ??? If this is a local variable, and it has not been seen in any
1711 outer BIND_EXPR, then it's probably the result of a duplicate
1712 declaration, for which we've already issued an error. It would
1713 be really nice if the front end wouldn't leak these at all.
1714 Currently the only known culprit is C++ destructors, as seen
1715 in g++.old-deja/g++.jason/binding.C. */
1716 if (TREE_CODE (decl) == VAR_DECL
1717 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1718 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1719 && decl_function_context (decl) == current_function_decl)
1721 gcc_assert (seen_error ());
1722 return GS_ERROR;
1725 /* When within an OpenMP context, notice uses of variables. */
1726 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1727 return GS_ALL_DONE;
1729 /* If the decl is an alias for another expression, substitute it now. */
1730 if (DECL_HAS_VALUE_EXPR_P (decl))
1732 tree value_expr = DECL_VALUE_EXPR (decl);
1734 /* For referenced nonlocal VLAs add a decl for debugging purposes
1735 to the current function. */
1736 if (TREE_CODE (decl) == VAR_DECL
1737 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1738 && nonlocal_vlas != NULL
1739 && TREE_CODE (value_expr) == INDIRECT_REF
1740 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1741 && decl_function_context (decl) != current_function_decl)
1743 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1744 while (ctx
1745 && (ctx->region_type == ORT_WORKSHARE
1746 || ctx->region_type == ORT_SIMD))
1747 ctx = ctx->outer_context;
1748 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
1750 tree copy = copy_node (decl);
1752 lang_hooks.dup_lang_specific_decl (copy);
1753 SET_DECL_RTL (copy, 0);
1754 TREE_USED (copy) = 1;
1755 DECL_CHAIN (copy) = nonlocal_vla_vars;
1756 nonlocal_vla_vars = copy;
1757 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1758 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1762 *expr_p = unshare_expr (value_expr);
1763 return GS_OK;
1766 return GS_ALL_DONE;
1769 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1771 static void
1772 recalculate_side_effects (tree t)
1774 enum tree_code code = TREE_CODE (t);
1775 int len = TREE_OPERAND_LENGTH (t);
1776 int i;
1778 switch (TREE_CODE_CLASS (code))
1780 case tcc_expression:
1781 switch (code)
1783 case INIT_EXPR:
1784 case MODIFY_EXPR:
1785 case VA_ARG_EXPR:
1786 case PREDECREMENT_EXPR:
1787 case PREINCREMENT_EXPR:
1788 case POSTDECREMENT_EXPR:
1789 case POSTINCREMENT_EXPR:
1790 /* All of these have side-effects, no matter what their
1791 operands are. */
1792 return;
1794 default:
1795 break;
1797 /* Fall through. */
1799 case tcc_comparison: /* a comparison expression */
1800 case tcc_unary: /* a unary arithmetic expression */
1801 case tcc_binary: /* a binary arithmetic expression */
1802 case tcc_reference: /* a reference */
1803 case tcc_vl_exp: /* a function call */
1804 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1805 for (i = 0; i < len; ++i)
1807 tree op = TREE_OPERAND (t, i);
1808 if (op && TREE_SIDE_EFFECTS (op))
1809 TREE_SIDE_EFFECTS (t) = 1;
1811 break;
1813 case tcc_constant:
1814 /* No side-effects. */
1815 return;
1817 default:
1818 gcc_unreachable ();
1822 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1823 node *EXPR_P.
1825 compound_lval
1826 : min_lval '[' val ']'
1827 | min_lval '.' ID
1828 | compound_lval '[' val ']'
1829 | compound_lval '.' ID
1831 This is not part of the original SIMPLE definition, which separates
1832 array and member references, but it seems reasonable to handle them
1833 together. Also, this way we don't run into problems with union
1834 aliasing; gcc requires that for accesses through a union to alias, the
1835 union reference must be explicit, which was not always the case when we
1836 were splitting up array and member refs.
1838 PRE_P points to the sequence where side effects that must happen before
1839 *EXPR_P should be stored.
1841 POST_P points to the sequence where side effects that must happen after
1842 *EXPR_P should be stored. */
1844 static enum gimplify_status
1845 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1846 fallback_t fallback)
1848 tree *p;
1849 enum gimplify_status ret = GS_ALL_DONE, tret;
1850 int i;
1851 location_t loc = EXPR_LOCATION (*expr_p);
1852 tree expr = *expr_p;
1854 /* Create a stack of the subexpressions so later we can walk them in
1855 order from inner to outer. */
1856 auto_vec<tree, 10> expr_stack;
1858 /* We can handle anything that get_inner_reference can deal with. */
1859 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1861 restart:
1862 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1863 if (TREE_CODE (*p) == INDIRECT_REF)
1864 *p = fold_indirect_ref_loc (loc, *p);
1866 if (handled_component_p (*p))
1868 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1869 additional COMPONENT_REFs. */
1870 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1871 && gimplify_var_or_parm_decl (p) == GS_OK)
1872 goto restart;
1873 else
1874 break;
1876 expr_stack.safe_push (*p);
1879 gcc_assert (expr_stack.length ());
1881 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1882 walked through and P points to the innermost expression.
1884 Java requires that we elaborated nodes in source order. That
1885 means we must gimplify the inner expression followed by each of
1886 the indices, in order. But we can't gimplify the inner
1887 expression until we deal with any variable bounds, sizes, or
1888 positions in order to deal with PLACEHOLDER_EXPRs.
1890 So we do this in three steps. First we deal with the annotations
1891 for any variables in the components, then we gimplify the base,
1892 then we gimplify any indices, from left to right. */
1893 for (i = expr_stack.length () - 1; i >= 0; i--)
1895 tree t = expr_stack[i];
1897 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1899 /* Gimplify the low bound and element type size and put them into
1900 the ARRAY_REF. If these values are set, they have already been
1901 gimplified. */
1902 if (TREE_OPERAND (t, 2) == NULL_TREE)
1904 tree low = unshare_expr (array_ref_low_bound (t));
1905 if (!is_gimple_min_invariant (low))
1907 TREE_OPERAND (t, 2) = low;
1908 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1909 post_p, is_gimple_reg,
1910 fb_rvalue);
1911 ret = MIN (ret, tret);
1914 else
1916 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1917 is_gimple_reg, fb_rvalue);
1918 ret = MIN (ret, tret);
1921 if (TREE_OPERAND (t, 3) == NULL_TREE)
1923 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1924 tree elmt_size = unshare_expr (array_ref_element_size (t));
1925 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1927 /* Divide the element size by the alignment of the element
1928 type (above). */
1929 elmt_size
1930 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1932 if (!is_gimple_min_invariant (elmt_size))
1934 TREE_OPERAND (t, 3) = elmt_size;
1935 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1936 post_p, is_gimple_reg,
1937 fb_rvalue);
1938 ret = MIN (ret, tret);
1941 else
1943 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1944 is_gimple_reg, fb_rvalue);
1945 ret = MIN (ret, tret);
1948 else if (TREE_CODE (t) == COMPONENT_REF)
1950 /* Set the field offset into T and gimplify it. */
1951 if (TREE_OPERAND (t, 2) == NULL_TREE)
1953 tree offset = unshare_expr (component_ref_field_offset (t));
1954 tree field = TREE_OPERAND (t, 1);
1955 tree factor
1956 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1958 /* Divide the offset by its alignment. */
1959 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
1961 if (!is_gimple_min_invariant (offset))
1963 TREE_OPERAND (t, 2) = offset;
1964 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1965 post_p, is_gimple_reg,
1966 fb_rvalue);
1967 ret = MIN (ret, tret);
1970 else
1972 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1973 is_gimple_reg, fb_rvalue);
1974 ret = MIN (ret, tret);
1979 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1980 so as to match the min_lval predicate. Failure to do so may result
1981 in the creation of large aggregate temporaries. */
1982 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1983 fallback | fb_lvalue);
1984 ret = MIN (ret, tret);
1986 /* And finally, the indices and operands of ARRAY_REF. During this
1987 loop we also remove any useless conversions. */
1988 for (; expr_stack.length () > 0; )
1990 tree t = expr_stack.pop ();
1992 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1994 /* Gimplify the dimension. */
1995 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1997 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1998 is_gimple_val, fb_rvalue);
1999 ret = MIN (ret, tret);
2003 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2005 /* The innermost expression P may have originally had
2006 TREE_SIDE_EFFECTS set which would have caused all the outer
2007 expressions in *EXPR_P leading to P to also have had
2008 TREE_SIDE_EFFECTS set. */
2009 recalculate_side_effects (t);
2012 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2013 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2015 canonicalize_component_ref (expr_p);
2018 expr_stack.release ();
2020 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2022 return ret;
2025 /* Gimplify the self modifying expression pointed to by EXPR_P
2026 (++, --, +=, -=).
2028 PRE_P points to the list where side effects that must happen before
2029 *EXPR_P should be stored.
2031 POST_P points to the list where side effects that must happen after
2032 *EXPR_P should be stored.
2034 WANT_VALUE is nonzero iff we want to use the value of this expression
2035 in another expression.
2037 ARITH_TYPE is the type the computation should be performed in. */
2039 enum gimplify_status
2040 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2041 bool want_value, tree arith_type)
2043 enum tree_code code;
2044 tree lhs, lvalue, rhs, t1;
2045 gimple_seq post = NULL, *orig_post_p = post_p;
2046 bool postfix;
2047 enum tree_code arith_code;
2048 enum gimplify_status ret;
2049 location_t loc = EXPR_LOCATION (*expr_p);
2051 code = TREE_CODE (*expr_p);
2053 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2054 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2056 /* Prefix or postfix? */
2057 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2058 /* Faster to treat as prefix if result is not used. */
2059 postfix = want_value;
2060 else
2061 postfix = false;
2063 /* For postfix, make sure the inner expression's post side effects
2064 are executed after side effects from this expression. */
2065 if (postfix)
2066 post_p = &post;
2068 /* Add or subtract? */
2069 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2070 arith_code = PLUS_EXPR;
2071 else
2072 arith_code = MINUS_EXPR;
2074 /* Gimplify the LHS into a GIMPLE lvalue. */
2075 lvalue = TREE_OPERAND (*expr_p, 0);
2076 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2077 if (ret == GS_ERROR)
2078 return ret;
2080 /* Extract the operands to the arithmetic operation. */
2081 lhs = lvalue;
2082 rhs = TREE_OPERAND (*expr_p, 1);
2084 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2085 that as the result value and in the postqueue operation. */
2086 if (postfix)
2088 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2089 if (ret == GS_ERROR)
2090 return ret;
2092 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2095 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2096 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2098 rhs = convert_to_ptrofftype_loc (loc, rhs);
2099 if (arith_code == MINUS_EXPR)
2100 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2101 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2103 else
2104 t1 = fold_convert (TREE_TYPE (*expr_p),
2105 fold_build2 (arith_code, arith_type,
2106 fold_convert (arith_type, lhs),
2107 fold_convert (arith_type, rhs)));
2109 if (postfix)
2111 gimplify_assign (lvalue, t1, pre_p);
2112 gimplify_seq_add_seq (orig_post_p, post);
2113 *expr_p = lhs;
2114 return GS_ALL_DONE;
2116 else
2118 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2119 return GS_OK;
2123 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2125 static void
2126 maybe_with_size_expr (tree *expr_p)
2128 tree expr = *expr_p;
2129 tree type = TREE_TYPE (expr);
2130 tree size;
2132 /* If we've already wrapped this or the type is error_mark_node, we can't do
2133 anything. */
2134 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2135 || type == error_mark_node)
2136 return;
2138 /* If the size isn't known or is a constant, we have nothing to do. */
2139 size = TYPE_SIZE_UNIT (type);
2140 if (!size || TREE_CODE (size) == INTEGER_CST)
2141 return;
2143 /* Otherwise, make a WITH_SIZE_EXPR. */
2144 size = unshare_expr (size);
2145 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2146 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2149 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2150 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2151 the CALL_EXPR. */
2153 static enum gimplify_status
2154 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2156 bool (*test) (tree);
2157 fallback_t fb;
2159 /* In general, we allow lvalues for function arguments to avoid
2160 extra overhead of copying large aggregates out of even larger
2161 aggregates into temporaries only to copy the temporaries to
2162 the argument list. Make optimizers happy by pulling out to
2163 temporaries those types that fit in registers. */
2164 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2165 test = is_gimple_val, fb = fb_rvalue;
2166 else
2168 test = is_gimple_lvalue, fb = fb_either;
2169 /* Also strip a TARGET_EXPR that would force an extra copy. */
2170 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2172 tree init = TARGET_EXPR_INITIAL (*arg_p);
2173 if (init
2174 && !VOID_TYPE_P (TREE_TYPE (init)))
2175 *arg_p = init;
2179 /* If this is a variable sized type, we must remember the size. */
2180 maybe_with_size_expr (arg_p);
2182 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2183 /* Make sure arguments have the same location as the function call
2184 itself. */
2185 protected_set_expr_location (*arg_p, call_location);
2187 /* There is a sequence point before a function call. Side effects in
2188 the argument list must occur before the actual call. So, when
2189 gimplifying arguments, force gimplify_expr to use an internal
2190 post queue which is then appended to the end of PRE_P. */
2191 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2194 /* Don't fold STMT inside ORT_TARGET, because it can break code by adding decl
2195 references that weren't in the source. We'll do it during omplower pass
2196 instead. */
2198 static bool
2199 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2201 struct gimplify_omp_ctx *ctx;
2202 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2203 if (ctx->region_type == ORT_TARGET)
2204 return false;
2205 return fold_stmt (gsi);
2208 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2209 WANT_VALUE is true if the result of the call is desired. */
2211 static enum gimplify_status
2212 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2214 tree fndecl, parms, p, fnptrtype;
2215 enum gimplify_status ret;
2216 int i, nargs;
2217 gimple call;
2218 bool builtin_va_start_p = false;
2219 location_t loc = EXPR_LOCATION (*expr_p);
2221 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2223 /* For reliable diagnostics during inlining, it is necessary that
2224 every call_expr be annotated with file and line. */
2225 if (! EXPR_HAS_LOCATION (*expr_p))
2226 SET_EXPR_LOCATION (*expr_p, input_location);
2228 /* This may be a call to a builtin function.
2230 Builtin function calls may be transformed into different
2231 (and more efficient) builtin function calls under certain
2232 circumstances. Unfortunately, gimplification can muck things
2233 up enough that the builtin expanders are not aware that certain
2234 transformations are still valid.
2236 So we attempt transformation/gimplification of the call before
2237 we gimplify the CALL_EXPR. At this time we do not manage to
2238 transform all calls in the same manner as the expanders do, but
2239 we do transform most of them. */
2240 fndecl = get_callee_fndecl (*expr_p);
2241 if (fndecl
2242 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2243 switch (DECL_FUNCTION_CODE (fndecl))
2245 case BUILT_IN_VA_START:
2247 builtin_va_start_p = TRUE;
2248 if (call_expr_nargs (*expr_p) < 2)
2250 error ("too few arguments to function %<va_start%>");
2251 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2252 return GS_OK;
2255 if (fold_builtin_next_arg (*expr_p, true))
2257 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2258 return GS_OK;
2260 break;
2262 case BUILT_IN_LINE:
2264 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2265 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2266 return GS_OK;
2268 case BUILT_IN_FILE:
2270 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2271 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2272 return GS_OK;
2274 case BUILT_IN_FUNCTION:
2276 const char *function;
2277 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2278 *expr_p = build_string_literal (strlen (function) + 1, function);
2279 return GS_OK;
2281 default:
2284 if (fndecl && DECL_BUILT_IN (fndecl))
2286 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2287 if (new_tree && new_tree != *expr_p)
2289 /* There was a transformation of this call which computes the
2290 same value, but in a more efficient way. Return and try
2291 again. */
2292 *expr_p = new_tree;
2293 return GS_OK;
2297 /* Remember the original function pointer type. */
2298 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2300 /* There is a sequence point before the call, so any side effects in
2301 the calling expression must occur before the actual call. Force
2302 gimplify_expr to use an internal post queue. */
2303 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2304 is_gimple_call_addr, fb_rvalue);
2306 nargs = call_expr_nargs (*expr_p);
2308 /* Get argument types for verification. */
2309 fndecl = get_callee_fndecl (*expr_p);
2310 parms = NULL_TREE;
2311 if (fndecl)
2312 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2313 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2314 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2316 if (fndecl && DECL_ARGUMENTS (fndecl))
2317 p = DECL_ARGUMENTS (fndecl);
2318 else if (parms)
2319 p = parms;
2320 else
2321 p = NULL_TREE;
2322 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2325 /* If the last argument is __builtin_va_arg_pack () and it is not
2326 passed as a named argument, decrease the number of CALL_EXPR
2327 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2328 if (!p
2329 && i < nargs
2330 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2332 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2333 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2335 if (last_arg_fndecl
2336 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2337 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2338 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2340 tree call = *expr_p;
2342 --nargs;
2343 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2344 CALL_EXPR_FN (call),
2345 nargs, CALL_EXPR_ARGP (call));
2347 /* Copy all CALL_EXPR flags, location and block, except
2348 CALL_EXPR_VA_ARG_PACK flag. */
2349 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2350 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2351 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2352 = CALL_EXPR_RETURN_SLOT_OPT (call);
2353 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2354 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2356 /* Set CALL_EXPR_VA_ARG_PACK. */
2357 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2361 /* Finally, gimplify the function arguments. */
2362 if (nargs > 0)
2364 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2365 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2366 PUSH_ARGS_REVERSED ? i-- : i++)
2368 enum gimplify_status t;
2370 /* Avoid gimplifying the second argument to va_start, which needs to
2371 be the plain PARM_DECL. */
2372 if ((i != 1) || !builtin_va_start_p)
2374 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2375 EXPR_LOCATION (*expr_p));
2377 if (t == GS_ERROR)
2378 ret = GS_ERROR;
2383 /* Verify the function result. */
2384 if (want_value && fndecl
2385 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2387 error_at (loc, "using result of function returning %<void%>");
2388 ret = GS_ERROR;
2391 /* Try this again in case gimplification exposed something. */
2392 if (ret != GS_ERROR)
2394 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2396 if (new_tree && new_tree != *expr_p)
2398 /* There was a transformation of this call which computes the
2399 same value, but in a more efficient way. Return and try
2400 again. */
2401 *expr_p = new_tree;
2402 return GS_OK;
2405 else
2407 *expr_p = error_mark_node;
2408 return GS_ERROR;
2411 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2412 decl. This allows us to eliminate redundant or useless
2413 calls to "const" functions. */
2414 if (TREE_CODE (*expr_p) == CALL_EXPR)
2416 int flags = call_expr_flags (*expr_p);
2417 if (flags & (ECF_CONST | ECF_PURE)
2418 /* An infinite loop is considered a side effect. */
2419 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2420 TREE_SIDE_EFFECTS (*expr_p) = 0;
2423 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2424 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2425 form and delegate the creation of a GIMPLE_CALL to
2426 gimplify_modify_expr. This is always possible because when
2427 WANT_VALUE is true, the caller wants the result of this call into
2428 a temporary, which means that we will emit an INIT_EXPR in
2429 internal_get_tmp_var which will then be handled by
2430 gimplify_modify_expr. */
2431 if (!want_value)
2433 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2434 have to do is replicate it as a GIMPLE_CALL tuple. */
2435 gimple_stmt_iterator gsi;
2436 call = gimple_build_call_from_tree (*expr_p);
2437 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2438 notice_special_calls (call);
2439 gimplify_seq_add_stmt (pre_p, call);
2440 gsi = gsi_last (*pre_p);
2441 maybe_fold_stmt (&gsi);
2442 *expr_p = NULL_TREE;
2444 else
2445 /* Remember the original function type. */
2446 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2447 CALL_EXPR_FN (*expr_p));
2449 return ret;
2452 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2453 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2455 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2456 condition is true or false, respectively. If null, we should generate
2457 our own to skip over the evaluation of this specific expression.
2459 LOCUS is the source location of the COND_EXPR.
2461 This function is the tree equivalent of do_jump.
2463 shortcut_cond_r should only be called by shortcut_cond_expr. */
2465 static tree
2466 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2467 location_t locus)
2469 tree local_label = NULL_TREE;
2470 tree t, expr = NULL;
2472 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2473 retain the shortcut semantics. Just insert the gotos here;
2474 shortcut_cond_expr will append the real blocks later. */
2475 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2477 location_t new_locus;
2479 /* Turn if (a && b) into
2481 if (a); else goto no;
2482 if (b) goto yes; else goto no;
2483 (no:) */
2485 if (false_label_p == NULL)
2486 false_label_p = &local_label;
2488 /* Keep the original source location on the first 'if'. */
2489 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2490 append_to_statement_list (t, &expr);
2492 /* Set the source location of the && on the second 'if'. */
2493 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2494 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2495 new_locus);
2496 append_to_statement_list (t, &expr);
2498 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2500 location_t new_locus;
2502 /* Turn if (a || b) into
2504 if (a) goto yes;
2505 if (b) goto yes; else goto no;
2506 (yes:) */
2508 if (true_label_p == NULL)
2509 true_label_p = &local_label;
2511 /* Keep the original source location on the first 'if'. */
2512 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2513 append_to_statement_list (t, &expr);
2515 /* Set the source location of the || on the second 'if'. */
2516 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2517 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2518 new_locus);
2519 append_to_statement_list (t, &expr);
2521 else if (TREE_CODE (pred) == COND_EXPR
2522 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2523 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2525 location_t new_locus;
2527 /* As long as we're messing with gotos, turn if (a ? b : c) into
2528 if (a)
2529 if (b) goto yes; else goto no;
2530 else
2531 if (c) goto yes; else goto no;
2533 Don't do this if one of the arms has void type, which can happen
2534 in C++ when the arm is throw. */
2536 /* Keep the original source location on the first 'if'. Set the source
2537 location of the ? on the second 'if'. */
2538 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2539 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2540 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2541 false_label_p, locus),
2542 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2543 false_label_p, new_locus));
2545 else
2547 expr = build3 (COND_EXPR, void_type_node, pred,
2548 build_and_jump (true_label_p),
2549 build_and_jump (false_label_p));
2550 SET_EXPR_LOCATION (expr, locus);
2553 if (local_label)
2555 t = build1 (LABEL_EXPR, void_type_node, local_label);
2556 append_to_statement_list (t, &expr);
2559 return expr;
2562 /* Given a conditional expression EXPR with short-circuit boolean
2563 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2564 predicate apart into the equivalent sequence of conditionals. */
2566 static tree
2567 shortcut_cond_expr (tree expr)
2569 tree pred = TREE_OPERAND (expr, 0);
2570 tree then_ = TREE_OPERAND (expr, 1);
2571 tree else_ = TREE_OPERAND (expr, 2);
2572 tree true_label, false_label, end_label, t;
2573 tree *true_label_p;
2574 tree *false_label_p;
2575 bool emit_end, emit_false, jump_over_else;
2576 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2577 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2579 /* First do simple transformations. */
2580 if (!else_se)
2582 /* If there is no 'else', turn
2583 if (a && b) then c
2584 into
2585 if (a) if (b) then c. */
2586 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2588 /* Keep the original source location on the first 'if'. */
2589 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2590 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2591 /* Set the source location of the && on the second 'if'. */
2592 if (EXPR_HAS_LOCATION (pred))
2593 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2594 then_ = shortcut_cond_expr (expr);
2595 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2596 pred = TREE_OPERAND (pred, 0);
2597 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2598 SET_EXPR_LOCATION (expr, locus);
2602 if (!then_se)
2604 /* If there is no 'then', turn
2605 if (a || b); else d
2606 into
2607 if (a); else if (b); else d. */
2608 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2610 /* Keep the original source location on the first 'if'. */
2611 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2612 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2613 /* Set the source location of the || on the second 'if'. */
2614 if (EXPR_HAS_LOCATION (pred))
2615 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2616 else_ = shortcut_cond_expr (expr);
2617 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2618 pred = TREE_OPERAND (pred, 0);
2619 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2620 SET_EXPR_LOCATION (expr, locus);
2624 /* If we're done, great. */
2625 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2626 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2627 return expr;
2629 /* Otherwise we need to mess with gotos. Change
2630 if (a) c; else d;
2632 if (a); else goto no;
2633 c; goto end;
2634 no: d; end:
2635 and recursively gimplify the condition. */
2637 true_label = false_label = end_label = NULL_TREE;
2639 /* If our arms just jump somewhere, hijack those labels so we don't
2640 generate jumps to jumps. */
2642 if (then_
2643 && TREE_CODE (then_) == GOTO_EXPR
2644 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2646 true_label = GOTO_DESTINATION (then_);
2647 then_ = NULL;
2648 then_se = false;
2651 if (else_
2652 && TREE_CODE (else_) == GOTO_EXPR
2653 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2655 false_label = GOTO_DESTINATION (else_);
2656 else_ = NULL;
2657 else_se = false;
2660 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2661 if (true_label)
2662 true_label_p = &true_label;
2663 else
2664 true_label_p = NULL;
2666 /* The 'else' branch also needs a label if it contains interesting code. */
2667 if (false_label || else_se)
2668 false_label_p = &false_label;
2669 else
2670 false_label_p = NULL;
2672 /* If there was nothing else in our arms, just forward the label(s). */
2673 if (!then_se && !else_se)
2674 return shortcut_cond_r (pred, true_label_p, false_label_p,
2675 EXPR_LOC_OR_LOC (expr, input_location));
2677 /* If our last subexpression already has a terminal label, reuse it. */
2678 if (else_se)
2679 t = expr_last (else_);
2680 else if (then_se)
2681 t = expr_last (then_);
2682 else
2683 t = NULL;
2684 if (t && TREE_CODE (t) == LABEL_EXPR)
2685 end_label = LABEL_EXPR_LABEL (t);
2687 /* If we don't care about jumping to the 'else' branch, jump to the end
2688 if the condition is false. */
2689 if (!false_label_p)
2690 false_label_p = &end_label;
2692 /* We only want to emit these labels if we aren't hijacking them. */
2693 emit_end = (end_label == NULL_TREE);
2694 emit_false = (false_label == NULL_TREE);
2696 /* We only emit the jump over the else clause if we have to--if the
2697 then clause may fall through. Otherwise we can wind up with a
2698 useless jump and a useless label at the end of gimplified code,
2699 which will cause us to think that this conditional as a whole
2700 falls through even if it doesn't. If we then inline a function
2701 which ends with such a condition, that can cause us to issue an
2702 inappropriate warning about control reaching the end of a
2703 non-void function. */
2704 jump_over_else = block_may_fallthru (then_);
2706 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2707 EXPR_LOC_OR_LOC (expr, input_location));
2709 expr = NULL;
2710 append_to_statement_list (pred, &expr);
2712 append_to_statement_list (then_, &expr);
2713 if (else_se)
2715 if (jump_over_else)
2717 tree last = expr_last (expr);
2718 t = build_and_jump (&end_label);
2719 if (EXPR_HAS_LOCATION (last))
2720 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2721 append_to_statement_list (t, &expr);
2723 if (emit_false)
2725 t = build1 (LABEL_EXPR, void_type_node, false_label);
2726 append_to_statement_list (t, &expr);
2728 append_to_statement_list (else_, &expr);
2730 if (emit_end && end_label)
2732 t = build1 (LABEL_EXPR, void_type_node, end_label);
2733 append_to_statement_list (t, &expr);
2736 return expr;
2739 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2741 tree
2742 gimple_boolify (tree expr)
2744 tree type = TREE_TYPE (expr);
2745 location_t loc = EXPR_LOCATION (expr);
2747 if (TREE_CODE (expr) == NE_EXPR
2748 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2749 && integer_zerop (TREE_OPERAND (expr, 1)))
2751 tree call = TREE_OPERAND (expr, 0);
2752 tree fn = get_callee_fndecl (call);
2754 /* For __builtin_expect ((long) (x), y) recurse into x as well
2755 if x is truth_value_p. */
2756 if (fn
2757 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2758 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2759 && call_expr_nargs (call) == 2)
2761 tree arg = CALL_EXPR_ARG (call, 0);
2762 if (arg)
2764 if (TREE_CODE (arg) == NOP_EXPR
2765 && TREE_TYPE (arg) == TREE_TYPE (call))
2766 arg = TREE_OPERAND (arg, 0);
2767 if (truth_value_p (TREE_CODE (arg)))
2769 arg = gimple_boolify (arg);
2770 CALL_EXPR_ARG (call, 0)
2771 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2777 switch (TREE_CODE (expr))
2779 case TRUTH_AND_EXPR:
2780 case TRUTH_OR_EXPR:
2781 case TRUTH_XOR_EXPR:
2782 case TRUTH_ANDIF_EXPR:
2783 case TRUTH_ORIF_EXPR:
2784 /* Also boolify the arguments of truth exprs. */
2785 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2786 /* FALLTHRU */
2788 case TRUTH_NOT_EXPR:
2789 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2791 /* These expressions always produce boolean results. */
2792 if (TREE_CODE (type) != BOOLEAN_TYPE)
2793 TREE_TYPE (expr) = boolean_type_node;
2794 return expr;
2796 case ANNOTATE_EXPR:
2797 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
2798 == annot_expr_ivdep_kind)
2800 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2801 if (TREE_CODE (type) != BOOLEAN_TYPE)
2802 TREE_TYPE (expr) = boolean_type_node;
2803 return expr;
2805 /* FALLTHRU */
2807 default:
2808 if (COMPARISON_CLASS_P (expr))
2810 /* There expressions always prduce boolean results. */
2811 if (TREE_CODE (type) != BOOLEAN_TYPE)
2812 TREE_TYPE (expr) = boolean_type_node;
2813 return expr;
2815 /* Other expressions that get here must have boolean values, but
2816 might need to be converted to the appropriate mode. */
2817 if (TREE_CODE (type) == BOOLEAN_TYPE)
2818 return expr;
2819 return fold_convert_loc (loc, boolean_type_node, expr);
2823 /* Given a conditional expression *EXPR_P without side effects, gimplify
2824 its operands. New statements are inserted to PRE_P. */
2826 static enum gimplify_status
2827 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2829 tree expr = *expr_p, cond;
2830 enum gimplify_status ret, tret;
2831 enum tree_code code;
2833 cond = gimple_boolify (COND_EXPR_COND (expr));
2835 /* We need to handle && and || specially, as their gimplification
2836 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2837 code = TREE_CODE (cond);
2838 if (code == TRUTH_ANDIF_EXPR)
2839 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2840 else if (code == TRUTH_ORIF_EXPR)
2841 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2842 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2843 COND_EXPR_COND (*expr_p) = cond;
2845 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2846 is_gimple_val, fb_rvalue);
2847 ret = MIN (ret, tret);
2848 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2849 is_gimple_val, fb_rvalue);
2851 return MIN (ret, tret);
2854 /* Return true if evaluating EXPR could trap.
2855 EXPR is GENERIC, while tree_could_trap_p can be called
2856 only on GIMPLE. */
2858 static bool
2859 generic_expr_could_trap_p (tree expr)
2861 unsigned i, n;
2863 if (!expr || is_gimple_val (expr))
2864 return false;
2866 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2867 return true;
2869 n = TREE_OPERAND_LENGTH (expr);
2870 for (i = 0; i < n; i++)
2871 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2872 return true;
2874 return false;
2877 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2878 into
2880 if (p) if (p)
2881 t1 = a; a;
2882 else or else
2883 t1 = b; b;
2886 The second form is used when *EXPR_P is of type void.
2888 PRE_P points to the list where side effects that must happen before
2889 *EXPR_P should be stored. */
2891 static enum gimplify_status
2892 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2894 tree expr = *expr_p;
2895 tree type = TREE_TYPE (expr);
2896 location_t loc = EXPR_LOCATION (expr);
2897 tree tmp, arm1, arm2;
2898 enum gimplify_status ret;
2899 tree label_true, label_false, label_cont;
2900 bool have_then_clause_p, have_else_clause_p;
2901 gimple gimple_cond;
2902 enum tree_code pred_code;
2903 gimple_seq seq = NULL;
2905 /* If this COND_EXPR has a value, copy the values into a temporary within
2906 the arms. */
2907 if (!VOID_TYPE_P (type))
2909 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
2910 tree result;
2912 /* If either an rvalue is ok or we do not require an lvalue, create the
2913 temporary. But we cannot do that if the type is addressable. */
2914 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
2915 && !TREE_ADDRESSABLE (type))
2917 if (gimplify_ctxp->allow_rhs_cond_expr
2918 /* If either branch has side effects or could trap, it can't be
2919 evaluated unconditionally. */
2920 && !TREE_SIDE_EFFECTS (then_)
2921 && !generic_expr_could_trap_p (then_)
2922 && !TREE_SIDE_EFFECTS (else_)
2923 && !generic_expr_could_trap_p (else_))
2924 return gimplify_pure_cond_expr (expr_p, pre_p);
2926 tmp = create_tmp_var (type, "iftmp");
2927 result = tmp;
2930 /* Otherwise, only create and copy references to the values. */
2931 else
2933 type = build_pointer_type (type);
2935 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2936 then_ = build_fold_addr_expr_loc (loc, then_);
2938 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2939 else_ = build_fold_addr_expr_loc (loc, else_);
2941 expr
2942 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
2944 tmp = create_tmp_var (type, "iftmp");
2945 result = build_simple_mem_ref_loc (loc, tmp);
2948 /* Build the new then clause, `tmp = then_;'. But don't build the
2949 assignment if the value is void; in C++ it can be if it's a throw. */
2950 if (!VOID_TYPE_P (TREE_TYPE (then_)))
2951 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
2953 /* Similarly, build the new else clause, `tmp = else_;'. */
2954 if (!VOID_TYPE_P (TREE_TYPE (else_)))
2955 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
2957 TREE_TYPE (expr) = void_type_node;
2958 recalculate_side_effects (expr);
2960 /* Move the COND_EXPR to the prequeue. */
2961 gimplify_stmt (&expr, pre_p);
2963 *expr_p = result;
2964 return GS_ALL_DONE;
2967 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
2968 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
2969 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
2970 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
2972 /* Make sure the condition has BOOLEAN_TYPE. */
2973 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2975 /* Break apart && and || conditions. */
2976 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2977 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2979 expr = shortcut_cond_expr (expr);
2981 if (expr != *expr_p)
2983 *expr_p = expr;
2985 /* We can't rely on gimplify_expr to re-gimplify the expanded
2986 form properly, as cleanups might cause the target labels to be
2987 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2988 set up a conditional context. */
2989 gimple_push_condition ();
2990 gimplify_stmt (expr_p, &seq);
2991 gimple_pop_condition (pre_p);
2992 gimple_seq_add_seq (pre_p, seq);
2994 return GS_ALL_DONE;
2998 /* Now do the normal gimplification. */
3000 /* Gimplify condition. */
3001 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3002 fb_rvalue);
3003 if (ret == GS_ERROR)
3004 return GS_ERROR;
3005 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3007 gimple_push_condition ();
3009 have_then_clause_p = have_else_clause_p = false;
3010 if (TREE_OPERAND (expr, 1) != NULL
3011 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3012 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3013 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3014 == current_function_decl)
3015 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3016 have different locations, otherwise we end up with incorrect
3017 location information on the branches. */
3018 && (optimize
3019 || !EXPR_HAS_LOCATION (expr)
3020 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3021 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3023 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3024 have_then_clause_p = true;
3026 else
3027 label_true = create_artificial_label (UNKNOWN_LOCATION);
3028 if (TREE_OPERAND (expr, 2) != NULL
3029 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3030 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3031 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3032 == current_function_decl)
3033 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3034 have different locations, otherwise we end up with incorrect
3035 location information on the branches. */
3036 && (optimize
3037 || !EXPR_HAS_LOCATION (expr)
3038 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3039 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3041 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3042 have_else_clause_p = true;
3044 else
3045 label_false = create_artificial_label (UNKNOWN_LOCATION);
3047 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3048 &arm2);
3050 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3051 label_false);
3053 gimplify_seq_add_stmt (&seq, gimple_cond);
3054 label_cont = NULL_TREE;
3055 if (!have_then_clause_p)
3057 /* For if (...) {} else { code; } put label_true after
3058 the else block. */
3059 if (TREE_OPERAND (expr, 1) == NULL_TREE
3060 && !have_else_clause_p
3061 && TREE_OPERAND (expr, 2) != NULL_TREE)
3062 label_cont = label_true;
3063 else
3065 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3066 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3067 /* For if (...) { code; } else {} or
3068 if (...) { code; } else goto label; or
3069 if (...) { code; return; } else { ... }
3070 label_cont isn't needed. */
3071 if (!have_else_clause_p
3072 && TREE_OPERAND (expr, 2) != NULL_TREE
3073 && gimple_seq_may_fallthru (seq))
3075 gimple g;
3076 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3078 g = gimple_build_goto (label_cont);
3080 /* GIMPLE_COND's are very low level; they have embedded
3081 gotos. This particular embedded goto should not be marked
3082 with the location of the original COND_EXPR, as it would
3083 correspond to the COND_EXPR's condition, not the ELSE or the
3084 THEN arms. To avoid marking it with the wrong location, flag
3085 it as "no location". */
3086 gimple_set_do_not_emit_location (g);
3088 gimplify_seq_add_stmt (&seq, g);
3092 if (!have_else_clause_p)
3094 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3095 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3097 if (label_cont)
3098 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3100 gimple_pop_condition (pre_p);
3101 gimple_seq_add_seq (pre_p, seq);
3103 if (ret == GS_ERROR)
3104 ; /* Do nothing. */
3105 else if (have_then_clause_p || have_else_clause_p)
3106 ret = GS_ALL_DONE;
3107 else
3109 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3110 expr = TREE_OPERAND (expr, 0);
3111 gimplify_stmt (&expr, pre_p);
3114 *expr_p = NULL;
3115 return ret;
3118 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3119 to be marked addressable.
3121 We cannot rely on such an expression being directly markable if a temporary
3122 has been created by the gimplification. In this case, we create another
3123 temporary and initialize it with a copy, which will become a store after we
3124 mark it addressable. This can happen if the front-end passed us something
3125 that it could not mark addressable yet, like a Fortran pass-by-reference
3126 parameter (int) floatvar. */
3128 static void
3129 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3131 while (handled_component_p (*expr_p))
3132 expr_p = &TREE_OPERAND (*expr_p, 0);
3133 if (is_gimple_reg (*expr_p))
3134 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3137 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3138 a call to __builtin_memcpy. */
3140 static enum gimplify_status
3141 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3142 gimple_seq *seq_p)
3144 tree t, to, to_ptr, from, from_ptr;
3145 gimple gs;
3146 location_t loc = EXPR_LOCATION (*expr_p);
3148 to = TREE_OPERAND (*expr_p, 0);
3149 from = TREE_OPERAND (*expr_p, 1);
3151 /* Mark the RHS addressable. Beware that it may not be possible to do so
3152 directly if a temporary has been created by the gimplification. */
3153 prepare_gimple_addressable (&from, seq_p);
3155 mark_addressable (from);
3156 from_ptr = build_fold_addr_expr_loc (loc, from);
3157 gimplify_arg (&from_ptr, seq_p, loc);
3159 mark_addressable (to);
3160 to_ptr = build_fold_addr_expr_loc (loc, to);
3161 gimplify_arg (&to_ptr, seq_p, loc);
3163 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3165 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3167 if (want_value)
3169 /* tmp = memcpy() */
3170 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3171 gimple_call_set_lhs (gs, t);
3172 gimplify_seq_add_stmt (seq_p, gs);
3174 *expr_p = build_simple_mem_ref (t);
3175 return GS_ALL_DONE;
3178 gimplify_seq_add_stmt (seq_p, gs);
3179 *expr_p = NULL;
3180 return GS_ALL_DONE;
3183 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3184 a call to __builtin_memset. In this case we know that the RHS is
3185 a CONSTRUCTOR with an empty element list. */
3187 static enum gimplify_status
3188 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3189 gimple_seq *seq_p)
3191 tree t, from, to, to_ptr;
3192 gimple gs;
3193 location_t loc = EXPR_LOCATION (*expr_p);
3195 /* Assert our assumptions, to abort instead of producing wrong code
3196 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3197 not be immediately exposed. */
3198 from = TREE_OPERAND (*expr_p, 1);
3199 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3200 from = TREE_OPERAND (from, 0);
3202 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3203 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3205 /* Now proceed. */
3206 to = TREE_OPERAND (*expr_p, 0);
3208 to_ptr = build_fold_addr_expr_loc (loc, to);
3209 gimplify_arg (&to_ptr, seq_p, loc);
3210 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3212 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3214 if (want_value)
3216 /* tmp = memset() */
3217 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3218 gimple_call_set_lhs (gs, t);
3219 gimplify_seq_add_stmt (seq_p, gs);
3221 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3222 return GS_ALL_DONE;
3225 gimplify_seq_add_stmt (seq_p, gs);
3226 *expr_p = NULL;
3227 return GS_ALL_DONE;
3230 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3231 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3232 assignment. Return non-null if we detect a potential overlap. */
3234 struct gimplify_init_ctor_preeval_data
3236 /* The base decl of the lhs object. May be NULL, in which case we
3237 have to assume the lhs is indirect. */
3238 tree lhs_base_decl;
3240 /* The alias set of the lhs object. */
3241 alias_set_type lhs_alias_set;
3244 static tree
3245 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3247 struct gimplify_init_ctor_preeval_data *data
3248 = (struct gimplify_init_ctor_preeval_data *) xdata;
3249 tree t = *tp;
3251 /* If we find the base object, obviously we have overlap. */
3252 if (data->lhs_base_decl == t)
3253 return t;
3255 /* If the constructor component is indirect, determine if we have a
3256 potential overlap with the lhs. The only bits of information we
3257 have to go on at this point are addressability and alias sets. */
3258 if ((INDIRECT_REF_P (t)
3259 || TREE_CODE (t) == MEM_REF)
3260 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3261 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3262 return t;
3264 /* If the constructor component is a call, determine if it can hide a
3265 potential overlap with the lhs through an INDIRECT_REF like above.
3266 ??? Ugh - this is completely broken. In fact this whole analysis
3267 doesn't look conservative. */
3268 if (TREE_CODE (t) == CALL_EXPR)
3270 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3272 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3273 if (POINTER_TYPE_P (TREE_VALUE (type))
3274 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3275 && alias_sets_conflict_p (data->lhs_alias_set,
3276 get_alias_set
3277 (TREE_TYPE (TREE_VALUE (type)))))
3278 return t;
3281 if (IS_TYPE_OR_DECL_P (t))
3282 *walk_subtrees = 0;
3283 return NULL;
3286 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3287 force values that overlap with the lhs (as described by *DATA)
3288 into temporaries. */
3290 static void
3291 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3292 struct gimplify_init_ctor_preeval_data *data)
3294 enum gimplify_status one;
3296 /* If the value is constant, then there's nothing to pre-evaluate. */
3297 if (TREE_CONSTANT (*expr_p))
3299 /* Ensure it does not have side effects, it might contain a reference to
3300 the object we're initializing. */
3301 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3302 return;
3305 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3306 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3307 return;
3309 /* Recurse for nested constructors. */
3310 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3312 unsigned HOST_WIDE_INT ix;
3313 constructor_elt *ce;
3314 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3316 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3317 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3319 return;
3322 /* If this is a variable sized type, we must remember the size. */
3323 maybe_with_size_expr (expr_p);
3325 /* Gimplify the constructor element to something appropriate for the rhs
3326 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3327 the gimplifier will consider this a store to memory. Doing this
3328 gimplification now means that we won't have to deal with complicated
3329 language-specific trees, nor trees like SAVE_EXPR that can induce
3330 exponential search behavior. */
3331 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3332 if (one == GS_ERROR)
3334 *expr_p = NULL;
3335 return;
3338 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3339 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3340 always be true for all scalars, since is_gimple_mem_rhs insists on a
3341 temporary variable for them. */
3342 if (DECL_P (*expr_p))
3343 return;
3345 /* If this is of variable size, we have no choice but to assume it doesn't
3346 overlap since we can't make a temporary for it. */
3347 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3348 return;
3350 /* Otherwise, we must search for overlap ... */
3351 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3352 return;
3354 /* ... and if found, force the value into a temporary. */
3355 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3358 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3359 a RANGE_EXPR in a CONSTRUCTOR for an array.
3361 var = lower;
3362 loop_entry:
3363 object[var] = value;
3364 if (var == upper)
3365 goto loop_exit;
3366 var = var + 1;
3367 goto loop_entry;
3368 loop_exit:
3370 We increment var _after_ the loop exit check because we might otherwise
3371 fail if upper == TYPE_MAX_VALUE (type for upper).
3373 Note that we never have to deal with SAVE_EXPRs here, because this has
3374 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3376 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3377 gimple_seq *, bool);
3379 static void
3380 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3381 tree value, tree array_elt_type,
3382 gimple_seq *pre_p, bool cleared)
3384 tree loop_entry_label, loop_exit_label, fall_thru_label;
3385 tree var, var_type, cref, tmp;
3387 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3388 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3389 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3391 /* Create and initialize the index variable. */
3392 var_type = TREE_TYPE (upper);
3393 var = create_tmp_var (var_type, NULL);
3394 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3396 /* Add the loop entry label. */
3397 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3399 /* Build the reference. */
3400 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3401 var, NULL_TREE, NULL_TREE);
3403 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3404 the store. Otherwise just assign value to the reference. */
3406 if (TREE_CODE (value) == CONSTRUCTOR)
3407 /* NB we might have to call ourself recursively through
3408 gimplify_init_ctor_eval if the value is a constructor. */
3409 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3410 pre_p, cleared);
3411 else
3412 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3414 /* We exit the loop when the index var is equal to the upper bound. */
3415 gimplify_seq_add_stmt (pre_p,
3416 gimple_build_cond (EQ_EXPR, var, upper,
3417 loop_exit_label, fall_thru_label));
3419 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3421 /* Otherwise, increment the index var... */
3422 tmp = build2 (PLUS_EXPR, var_type, var,
3423 fold_convert (var_type, integer_one_node));
3424 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3426 /* ...and jump back to the loop entry. */
3427 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3429 /* Add the loop exit label. */
3430 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3433 /* Return true if FDECL is accessing a field that is zero sized. */
3435 static bool
3436 zero_sized_field_decl (const_tree fdecl)
3438 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3439 && integer_zerop (DECL_SIZE (fdecl)))
3440 return true;
3441 return false;
3444 /* Return true if TYPE is zero sized. */
3446 static bool
3447 zero_sized_type (const_tree type)
3449 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3450 && integer_zerop (TYPE_SIZE (type)))
3451 return true;
3452 return false;
3455 /* A subroutine of gimplify_init_constructor. Generate individual
3456 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3457 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3458 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3459 zeroed first. */
3461 static void
3462 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3463 gimple_seq *pre_p, bool cleared)
3465 tree array_elt_type = NULL;
3466 unsigned HOST_WIDE_INT ix;
3467 tree purpose, value;
3469 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3470 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3472 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3474 tree cref;
3476 /* NULL values are created above for gimplification errors. */
3477 if (value == NULL)
3478 continue;
3480 if (cleared && initializer_zerop (value))
3481 continue;
3483 /* ??? Here's to hoping the front end fills in all of the indices,
3484 so we don't have to figure out what's missing ourselves. */
3485 gcc_assert (purpose);
3487 /* Skip zero-sized fields, unless value has side-effects. This can
3488 happen with calls to functions returning a zero-sized type, which
3489 we shouldn't discard. As a number of downstream passes don't
3490 expect sets of zero-sized fields, we rely on the gimplification of
3491 the MODIFY_EXPR we make below to drop the assignment statement. */
3492 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3493 continue;
3495 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3496 whole range. */
3497 if (TREE_CODE (purpose) == RANGE_EXPR)
3499 tree lower = TREE_OPERAND (purpose, 0);
3500 tree upper = TREE_OPERAND (purpose, 1);
3502 /* If the lower bound is equal to upper, just treat it as if
3503 upper was the index. */
3504 if (simple_cst_equal (lower, upper))
3505 purpose = upper;
3506 else
3508 gimplify_init_ctor_eval_range (object, lower, upper, value,
3509 array_elt_type, pre_p, cleared);
3510 continue;
3514 if (array_elt_type)
3516 /* Do not use bitsizetype for ARRAY_REF indices. */
3517 if (TYPE_DOMAIN (TREE_TYPE (object)))
3518 purpose
3519 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3520 purpose);
3521 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3522 purpose, NULL_TREE, NULL_TREE);
3524 else
3526 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3527 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3528 unshare_expr (object), purpose, NULL_TREE);
3531 if (TREE_CODE (value) == CONSTRUCTOR
3532 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3533 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3534 pre_p, cleared);
3535 else
3537 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3538 gimplify_and_add (init, pre_p);
3539 ggc_free (init);
3544 /* Return the appropriate RHS predicate for this LHS. */
3546 gimple_predicate
3547 rhs_predicate_for (tree lhs)
3549 if (is_gimple_reg (lhs))
3550 return is_gimple_reg_rhs_or_call;
3551 else
3552 return is_gimple_mem_rhs_or_call;
3555 /* Gimplify a C99 compound literal expression. This just means adding
3556 the DECL_EXPR before the current statement and using its anonymous
3557 decl instead. */
3559 static enum gimplify_status
3560 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3561 bool (*gimple_test_f) (tree),
3562 fallback_t fallback)
3564 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3565 tree decl = DECL_EXPR_DECL (decl_s);
3566 tree init = DECL_INITIAL (decl);
3567 /* Mark the decl as addressable if the compound literal
3568 expression is addressable now, otherwise it is marked too late
3569 after we gimplify the initialization expression. */
3570 if (TREE_ADDRESSABLE (*expr_p))
3571 TREE_ADDRESSABLE (decl) = 1;
3572 /* Otherwise, if we don't need an lvalue and have a literal directly
3573 substitute it. Check if it matches the gimple predicate, as
3574 otherwise we'd generate a new temporary, and we can as well just
3575 use the decl we already have. */
3576 else if (!TREE_ADDRESSABLE (decl)
3577 && init
3578 && (fallback & fb_lvalue) == 0
3579 && gimple_test_f (init))
3581 *expr_p = init;
3582 return GS_OK;
3585 /* Preliminarily mark non-addressed complex variables as eligible
3586 for promotion to gimple registers. We'll transform their uses
3587 as we find them. */
3588 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3589 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3590 && !TREE_THIS_VOLATILE (decl)
3591 && !needs_to_live_in_memory (decl))
3592 DECL_GIMPLE_REG_P (decl) = 1;
3594 /* If the decl is not addressable, then it is being used in some
3595 expression or on the right hand side of a statement, and it can
3596 be put into a readonly data section. */
3597 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3598 TREE_READONLY (decl) = 1;
3600 /* This decl isn't mentioned in the enclosing block, so add it to the
3601 list of temps. FIXME it seems a bit of a kludge to say that
3602 anonymous artificial vars aren't pushed, but everything else is. */
3603 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3604 gimple_add_tmp_var (decl);
3606 gimplify_and_add (decl_s, pre_p);
3607 *expr_p = decl;
3608 return GS_OK;
3611 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3612 return a new CONSTRUCTOR if something changed. */
3614 static tree
3615 optimize_compound_literals_in_ctor (tree orig_ctor)
3617 tree ctor = orig_ctor;
3618 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3619 unsigned int idx, num = vec_safe_length (elts);
3621 for (idx = 0; idx < num; idx++)
3623 tree value = (*elts)[idx].value;
3624 tree newval = value;
3625 if (TREE_CODE (value) == CONSTRUCTOR)
3626 newval = optimize_compound_literals_in_ctor (value);
3627 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3629 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3630 tree decl = DECL_EXPR_DECL (decl_s);
3631 tree init = DECL_INITIAL (decl);
3633 if (!TREE_ADDRESSABLE (value)
3634 && !TREE_ADDRESSABLE (decl)
3635 && init
3636 && TREE_CODE (init) == CONSTRUCTOR)
3637 newval = optimize_compound_literals_in_ctor (init);
3639 if (newval == value)
3640 continue;
3642 if (ctor == orig_ctor)
3644 ctor = copy_node (orig_ctor);
3645 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3646 elts = CONSTRUCTOR_ELTS (ctor);
3648 (*elts)[idx].value = newval;
3650 return ctor;
3653 /* A subroutine of gimplify_modify_expr. Break out elements of a
3654 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3656 Note that we still need to clear any elements that don't have explicit
3657 initializers, so if not all elements are initialized we keep the
3658 original MODIFY_EXPR, we just remove all of the constructor elements.
3660 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3661 GS_ERROR if we would have to create a temporary when gimplifying
3662 this constructor. Otherwise, return GS_OK.
3664 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3666 static enum gimplify_status
3667 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3668 bool want_value, bool notify_temp_creation)
3670 tree object, ctor, type;
3671 enum gimplify_status ret;
3672 vec<constructor_elt, va_gc> *elts;
3674 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3676 if (!notify_temp_creation)
3678 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3679 is_gimple_lvalue, fb_lvalue);
3680 if (ret == GS_ERROR)
3681 return ret;
3684 object = TREE_OPERAND (*expr_p, 0);
3685 ctor = TREE_OPERAND (*expr_p, 1) =
3686 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3687 type = TREE_TYPE (ctor);
3688 elts = CONSTRUCTOR_ELTS (ctor);
3689 ret = GS_ALL_DONE;
3691 switch (TREE_CODE (type))
3693 case RECORD_TYPE:
3694 case UNION_TYPE:
3695 case QUAL_UNION_TYPE:
3696 case ARRAY_TYPE:
3698 struct gimplify_init_ctor_preeval_data preeval_data;
3699 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3700 bool cleared, complete_p, valid_const_initializer;
3702 /* Aggregate types must lower constructors to initialization of
3703 individual elements. The exception is that a CONSTRUCTOR node
3704 with no elements indicates zero-initialization of the whole. */
3705 if (vec_safe_is_empty (elts))
3707 if (notify_temp_creation)
3708 return GS_OK;
3709 break;
3712 /* Fetch information about the constructor to direct later processing.
3713 We might want to make static versions of it in various cases, and
3714 can only do so if it known to be a valid constant initializer. */
3715 valid_const_initializer
3716 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3717 &num_ctor_elements, &complete_p);
3719 /* If a const aggregate variable is being initialized, then it
3720 should never be a lose to promote the variable to be static. */
3721 if (valid_const_initializer
3722 && num_nonzero_elements > 1
3723 && TREE_READONLY (object)
3724 && TREE_CODE (object) == VAR_DECL
3725 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3727 if (notify_temp_creation)
3728 return GS_ERROR;
3729 DECL_INITIAL (object) = ctor;
3730 TREE_STATIC (object) = 1;
3731 if (!DECL_NAME (object))
3732 DECL_NAME (object) = create_tmp_var_name ("C");
3733 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3735 /* ??? C++ doesn't automatically append a .<number> to the
3736 assembler name, and even when it does, it looks at FE private
3737 data structures to figure out what that number should be,
3738 which are not set for this variable. I suppose this is
3739 important for local statics for inline functions, which aren't
3740 "local" in the object file sense. So in order to get a unique
3741 TU-local symbol, we must invoke the lhd version now. */
3742 lhd_set_decl_assembler_name (object);
3744 *expr_p = NULL_TREE;
3745 break;
3748 /* If there are "lots" of initialized elements, even discounting
3749 those that are not address constants (and thus *must* be
3750 computed at runtime), then partition the constructor into
3751 constant and non-constant parts. Block copy the constant
3752 parts in, then generate code for the non-constant parts. */
3753 /* TODO. There's code in cp/typeck.c to do this. */
3755 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3756 /* store_constructor will ignore the clearing of variable-sized
3757 objects. Initializers for such objects must explicitly set
3758 every field that needs to be set. */
3759 cleared = false;
3760 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3761 /* If the constructor isn't complete, clear the whole object
3762 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3764 ??? This ought not to be needed. For any element not present
3765 in the initializer, we should simply set them to zero. Except
3766 we'd need to *find* the elements that are not present, and that
3767 requires trickery to avoid quadratic compile-time behavior in
3768 large cases or excessive memory use in small cases. */
3769 cleared = true;
3770 else if (num_ctor_elements - num_nonzero_elements
3771 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3772 && num_nonzero_elements < num_ctor_elements / 4)
3773 /* If there are "lots" of zeros, it's more efficient to clear
3774 the memory and then set the nonzero elements. */
3775 cleared = true;
3776 else
3777 cleared = false;
3779 /* If there are "lots" of initialized elements, and all of them
3780 are valid address constants, then the entire initializer can
3781 be dropped to memory, and then memcpy'd out. Don't do this
3782 for sparse arrays, though, as it's more efficient to follow
3783 the standard CONSTRUCTOR behavior of memset followed by
3784 individual element initialization. Also don't do this for small
3785 all-zero initializers (which aren't big enough to merit
3786 clearing), and don't try to make bitwise copies of
3787 TREE_ADDRESSABLE types. */
3788 if (valid_const_initializer
3789 && !(cleared || num_nonzero_elements == 0)
3790 && !TREE_ADDRESSABLE (type))
3792 HOST_WIDE_INT size = int_size_in_bytes (type);
3793 unsigned int align;
3795 /* ??? We can still get unbounded array types, at least
3796 from the C++ front end. This seems wrong, but attempt
3797 to work around it for now. */
3798 if (size < 0)
3800 size = int_size_in_bytes (TREE_TYPE (object));
3801 if (size >= 0)
3802 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3805 /* Find the maximum alignment we can assume for the object. */
3806 /* ??? Make use of DECL_OFFSET_ALIGN. */
3807 if (DECL_P (object))
3808 align = DECL_ALIGN (object);
3809 else
3810 align = TYPE_ALIGN (type);
3812 /* Do a block move either if the size is so small as to make
3813 each individual move a sub-unit move on average, or if it
3814 is so large as to make individual moves inefficient. */
3815 if (size > 0
3816 && num_nonzero_elements > 1
3817 && (size < num_nonzero_elements
3818 || !can_move_by_pieces (size, align)))
3820 if (notify_temp_creation)
3821 return GS_ERROR;
3823 walk_tree (&ctor, force_labels_r, NULL, NULL);
3824 ctor = tree_output_constant_def (ctor);
3825 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3826 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3827 TREE_OPERAND (*expr_p, 1) = ctor;
3829 /* This is no longer an assignment of a CONSTRUCTOR, but
3830 we still may have processing to do on the LHS. So
3831 pretend we didn't do anything here to let that happen. */
3832 return GS_UNHANDLED;
3836 /* If the target is volatile, we have non-zero elements and more than
3837 one field to assign, initialize the target from a temporary. */
3838 if (TREE_THIS_VOLATILE (object)
3839 && !TREE_ADDRESSABLE (type)
3840 && num_nonzero_elements > 0
3841 && vec_safe_length (elts) > 1)
3843 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3844 TREE_OPERAND (*expr_p, 0) = temp;
3845 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3846 *expr_p,
3847 build2 (MODIFY_EXPR, void_type_node,
3848 object, temp));
3849 return GS_OK;
3852 if (notify_temp_creation)
3853 return GS_OK;
3855 /* If there are nonzero elements and if needed, pre-evaluate to capture
3856 elements overlapping with the lhs into temporaries. We must do this
3857 before clearing to fetch the values before they are zeroed-out. */
3858 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3860 preeval_data.lhs_base_decl = get_base_address (object);
3861 if (!DECL_P (preeval_data.lhs_base_decl))
3862 preeval_data.lhs_base_decl = NULL;
3863 preeval_data.lhs_alias_set = get_alias_set (object);
3865 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3866 pre_p, post_p, &preeval_data);
3869 if (cleared)
3871 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3872 Note that we still have to gimplify, in order to handle the
3873 case of variable sized types. Avoid shared tree structures. */
3874 CONSTRUCTOR_ELTS (ctor) = NULL;
3875 TREE_SIDE_EFFECTS (ctor) = 0;
3876 object = unshare_expr (object);
3877 gimplify_stmt (expr_p, pre_p);
3880 /* If we have not block cleared the object, or if there are nonzero
3881 elements in the constructor, add assignments to the individual
3882 scalar fields of the object. */
3883 if (!cleared || num_nonzero_elements > 0)
3884 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3886 *expr_p = NULL_TREE;
3888 break;
3890 case COMPLEX_TYPE:
3892 tree r, i;
3894 if (notify_temp_creation)
3895 return GS_OK;
3897 /* Extract the real and imaginary parts out of the ctor. */
3898 gcc_assert (elts->length () == 2);
3899 r = (*elts)[0].value;
3900 i = (*elts)[1].value;
3901 if (r == NULL || i == NULL)
3903 tree zero = build_zero_cst (TREE_TYPE (type));
3904 if (r == NULL)
3905 r = zero;
3906 if (i == NULL)
3907 i = zero;
3910 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3911 represent creation of a complex value. */
3912 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3914 ctor = build_complex (type, r, i);
3915 TREE_OPERAND (*expr_p, 1) = ctor;
3917 else
3919 ctor = build2 (COMPLEX_EXPR, type, r, i);
3920 TREE_OPERAND (*expr_p, 1) = ctor;
3921 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3922 pre_p,
3923 post_p,
3924 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3925 fb_rvalue);
3928 break;
3930 case VECTOR_TYPE:
3932 unsigned HOST_WIDE_INT ix;
3933 constructor_elt *ce;
3935 if (notify_temp_creation)
3936 return GS_OK;
3938 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3939 if (TREE_CONSTANT (ctor))
3941 bool constant_p = true;
3942 tree value;
3944 /* Even when ctor is constant, it might contain non-*_CST
3945 elements, such as addresses or trapping values like
3946 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3947 in VECTOR_CST nodes. */
3948 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3949 if (!CONSTANT_CLASS_P (value))
3951 constant_p = false;
3952 break;
3955 if (constant_p)
3957 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3958 break;
3961 /* Don't reduce an initializer constant even if we can't
3962 make a VECTOR_CST. It won't do anything for us, and it'll
3963 prevent us from representing it as a single constant. */
3964 if (initializer_constant_valid_p (ctor, type))
3965 break;
3967 TREE_CONSTANT (ctor) = 0;
3970 /* Vector types use CONSTRUCTOR all the way through gimple
3971 compilation as a general initializer. */
3972 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
3974 enum gimplify_status tret;
3975 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3976 fb_rvalue);
3977 if (tret == GS_ERROR)
3978 ret = GS_ERROR;
3980 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
3981 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3983 break;
3985 default:
3986 /* So how did we get a CONSTRUCTOR for a scalar type? */
3987 gcc_unreachable ();
3990 if (ret == GS_ERROR)
3991 return GS_ERROR;
3992 else if (want_value)
3994 *expr_p = object;
3995 return GS_OK;
3997 else
3999 /* If we have gimplified both sides of the initializer but have
4000 not emitted an assignment, do so now. */
4001 if (*expr_p)
4003 tree lhs = TREE_OPERAND (*expr_p, 0);
4004 tree rhs = TREE_OPERAND (*expr_p, 1);
4005 gimple init = gimple_build_assign (lhs, rhs);
4006 gimplify_seq_add_stmt (pre_p, init);
4007 *expr_p = NULL;
4010 return GS_ALL_DONE;
4014 /* Given a pointer value OP0, return a simplified version of an
4015 indirection through OP0, or NULL_TREE if no simplification is
4016 possible. This may only be applied to a rhs of an expression.
4017 Note that the resulting type may be different from the type pointed
4018 to in the sense that it is still compatible from the langhooks
4019 point of view. */
4021 static tree
4022 gimple_fold_indirect_ref_rhs (tree t)
4024 return gimple_fold_indirect_ref (t);
4027 /* Subroutine of gimplify_modify_expr to do simplifications of
4028 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4029 something changes. */
4031 static enum gimplify_status
4032 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4033 gimple_seq *pre_p, gimple_seq *post_p,
4034 bool want_value)
4036 enum gimplify_status ret = GS_UNHANDLED;
4037 bool changed;
4041 changed = false;
4042 switch (TREE_CODE (*from_p))
4044 case VAR_DECL:
4045 /* If we're assigning from a read-only variable initialized with
4046 a constructor, do the direct assignment from the constructor,
4047 but only if neither source nor target are volatile since this
4048 latter assignment might end up being done on a per-field basis. */
4049 if (DECL_INITIAL (*from_p)
4050 && TREE_READONLY (*from_p)
4051 && !TREE_THIS_VOLATILE (*from_p)
4052 && !TREE_THIS_VOLATILE (*to_p)
4053 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4055 tree old_from = *from_p;
4056 enum gimplify_status subret;
4058 /* Move the constructor into the RHS. */
4059 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4061 /* Let's see if gimplify_init_constructor will need to put
4062 it in memory. */
4063 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4064 false, true);
4065 if (subret == GS_ERROR)
4067 /* If so, revert the change. */
4068 *from_p = old_from;
4070 else
4072 ret = GS_OK;
4073 changed = true;
4076 break;
4077 case INDIRECT_REF:
4079 /* If we have code like
4081 *(const A*)(A*)&x
4083 where the type of "x" is a (possibly cv-qualified variant
4084 of "A"), treat the entire expression as identical to "x".
4085 This kind of code arises in C++ when an object is bound
4086 to a const reference, and if "x" is a TARGET_EXPR we want
4087 to take advantage of the optimization below. */
4088 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4089 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4090 if (t)
4092 if (TREE_THIS_VOLATILE (t) != volatile_p)
4094 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4095 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4096 build_fold_addr_expr (t));
4097 if (REFERENCE_CLASS_P (t))
4098 TREE_THIS_VOLATILE (t) = volatile_p;
4100 *from_p = t;
4101 ret = GS_OK;
4102 changed = true;
4104 break;
4107 case TARGET_EXPR:
4109 /* If we are initializing something from a TARGET_EXPR, strip the
4110 TARGET_EXPR and initialize it directly, if possible. This can't
4111 be done if the initializer is void, since that implies that the
4112 temporary is set in some non-trivial way.
4114 ??? What about code that pulls out the temp and uses it
4115 elsewhere? I think that such code never uses the TARGET_EXPR as
4116 an initializer. If I'm wrong, we'll die because the temp won't
4117 have any RTL. In that case, I guess we'll need to replace
4118 references somehow. */
4119 tree init = TARGET_EXPR_INITIAL (*from_p);
4121 if (init
4122 && !VOID_TYPE_P (TREE_TYPE (init)))
4124 *from_p = init;
4125 ret = GS_OK;
4126 changed = true;
4129 break;
4131 case COMPOUND_EXPR:
4132 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4133 caught. */
4134 gimplify_compound_expr (from_p, pre_p, true);
4135 ret = GS_OK;
4136 changed = true;
4137 break;
4139 case CONSTRUCTOR:
4140 /* If we already made some changes, let the front end have a
4141 crack at this before we break it down. */
4142 if (ret != GS_UNHANDLED)
4143 break;
4144 /* If we're initializing from a CONSTRUCTOR, break this into
4145 individual MODIFY_EXPRs. */
4146 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4147 false);
4149 case COND_EXPR:
4150 /* If we're assigning to a non-register type, push the assignment
4151 down into the branches. This is mandatory for ADDRESSABLE types,
4152 since we cannot generate temporaries for such, but it saves a
4153 copy in other cases as well. */
4154 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4156 /* This code should mirror the code in gimplify_cond_expr. */
4157 enum tree_code code = TREE_CODE (*expr_p);
4158 tree cond = *from_p;
4159 tree result = *to_p;
4161 ret = gimplify_expr (&result, pre_p, post_p,
4162 is_gimple_lvalue, fb_lvalue);
4163 if (ret != GS_ERROR)
4164 ret = GS_OK;
4166 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4167 TREE_OPERAND (cond, 1)
4168 = build2 (code, void_type_node, result,
4169 TREE_OPERAND (cond, 1));
4170 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4171 TREE_OPERAND (cond, 2)
4172 = build2 (code, void_type_node, unshare_expr (result),
4173 TREE_OPERAND (cond, 2));
4175 TREE_TYPE (cond) = void_type_node;
4176 recalculate_side_effects (cond);
4178 if (want_value)
4180 gimplify_and_add (cond, pre_p);
4181 *expr_p = unshare_expr (result);
4183 else
4184 *expr_p = cond;
4185 return ret;
4187 break;
4189 case CALL_EXPR:
4190 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4191 return slot so that we don't generate a temporary. */
4192 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4193 && aggregate_value_p (*from_p, *from_p))
4195 bool use_target;
4197 if (!(rhs_predicate_for (*to_p))(*from_p))
4198 /* If we need a temporary, *to_p isn't accurate. */
4199 use_target = false;
4200 /* It's OK to use the return slot directly unless it's an NRV. */
4201 else if (TREE_CODE (*to_p) == RESULT_DECL
4202 && DECL_NAME (*to_p) == NULL_TREE
4203 && needs_to_live_in_memory (*to_p))
4204 use_target = true;
4205 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4206 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4207 /* Don't force regs into memory. */
4208 use_target = false;
4209 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4210 /* It's OK to use the target directly if it's being
4211 initialized. */
4212 use_target = true;
4213 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4214 /* Always use the target and thus RSO for variable-sized types.
4215 GIMPLE cannot deal with a variable-sized assignment
4216 embedded in a call statement. */
4217 use_target = true;
4218 else if (TREE_CODE (*to_p) != SSA_NAME
4219 && (!is_gimple_variable (*to_p)
4220 || needs_to_live_in_memory (*to_p)))
4221 /* Don't use the original target if it's already addressable;
4222 if its address escapes, and the called function uses the
4223 NRV optimization, a conforming program could see *to_p
4224 change before the called function returns; see c++/19317.
4225 When optimizing, the return_slot pass marks more functions
4226 as safe after we have escape info. */
4227 use_target = false;
4228 else
4229 use_target = true;
4231 if (use_target)
4233 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4234 mark_addressable (*to_p);
4237 break;
4239 case WITH_SIZE_EXPR:
4240 /* Likewise for calls that return an aggregate of non-constant size,
4241 since we would not be able to generate a temporary at all. */
4242 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4244 *from_p = TREE_OPERAND (*from_p, 0);
4245 /* We don't change ret in this case because the
4246 WITH_SIZE_EXPR might have been added in
4247 gimplify_modify_expr, so returning GS_OK would lead to an
4248 infinite loop. */
4249 changed = true;
4251 break;
4253 /* If we're initializing from a container, push the initialization
4254 inside it. */
4255 case CLEANUP_POINT_EXPR:
4256 case BIND_EXPR:
4257 case STATEMENT_LIST:
4259 tree wrap = *from_p;
4260 tree t;
4262 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4263 fb_lvalue);
4264 if (ret != GS_ERROR)
4265 ret = GS_OK;
4267 t = voidify_wrapper_expr (wrap, *expr_p);
4268 gcc_assert (t == *expr_p);
4270 if (want_value)
4272 gimplify_and_add (wrap, pre_p);
4273 *expr_p = unshare_expr (*to_p);
4275 else
4276 *expr_p = wrap;
4277 return GS_OK;
4280 case COMPOUND_LITERAL_EXPR:
4282 tree complit = TREE_OPERAND (*expr_p, 1);
4283 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4284 tree decl = DECL_EXPR_DECL (decl_s);
4285 tree init = DECL_INITIAL (decl);
4287 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4288 into struct T x = { 0, 1, 2 } if the address of the
4289 compound literal has never been taken. */
4290 if (!TREE_ADDRESSABLE (complit)
4291 && !TREE_ADDRESSABLE (decl)
4292 && init)
4294 *expr_p = copy_node (*expr_p);
4295 TREE_OPERAND (*expr_p, 1) = init;
4296 return GS_OK;
4300 default:
4301 break;
4304 while (changed);
4306 return ret;
4310 /* Return true if T looks like a valid GIMPLE statement. */
4312 static bool
4313 is_gimple_stmt (tree t)
4315 const enum tree_code code = TREE_CODE (t);
4317 switch (code)
4319 case NOP_EXPR:
4320 /* The only valid NOP_EXPR is the empty statement. */
4321 return IS_EMPTY_STMT (t);
4323 case BIND_EXPR:
4324 case COND_EXPR:
4325 /* These are only valid if they're void. */
4326 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4328 case SWITCH_EXPR:
4329 case GOTO_EXPR:
4330 case RETURN_EXPR:
4331 case LABEL_EXPR:
4332 case CASE_LABEL_EXPR:
4333 case TRY_CATCH_EXPR:
4334 case TRY_FINALLY_EXPR:
4335 case EH_FILTER_EXPR:
4336 case CATCH_EXPR:
4337 case ASM_EXPR:
4338 case STATEMENT_LIST:
4339 case OMP_PARALLEL:
4340 case OMP_FOR:
4341 case OMP_SIMD:
4342 case CILK_SIMD:
4343 case OMP_DISTRIBUTE:
4344 case OMP_SECTIONS:
4345 case OMP_SECTION:
4346 case OMP_SINGLE:
4347 case OMP_MASTER:
4348 case OMP_TASKGROUP:
4349 case OMP_ORDERED:
4350 case OMP_CRITICAL:
4351 case OMP_TASK:
4352 /* These are always void. */
4353 return true;
4355 case CALL_EXPR:
4356 case MODIFY_EXPR:
4357 case PREDICT_EXPR:
4358 /* These are valid regardless of their type. */
4359 return true;
4361 default:
4362 return false;
4367 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4368 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4369 DECL_GIMPLE_REG_P set.
4371 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4372 other, unmodified part of the complex object just before the total store.
4373 As a consequence, if the object is still uninitialized, an undefined value
4374 will be loaded into a register, which may result in a spurious exception
4375 if the register is floating-point and the value happens to be a signaling
4376 NaN for example. Then the fully-fledged complex operations lowering pass
4377 followed by a DCE pass are necessary in order to fix things up. */
4379 static enum gimplify_status
4380 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4381 bool want_value)
4383 enum tree_code code, ocode;
4384 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4386 lhs = TREE_OPERAND (*expr_p, 0);
4387 rhs = TREE_OPERAND (*expr_p, 1);
4388 code = TREE_CODE (lhs);
4389 lhs = TREE_OPERAND (lhs, 0);
4391 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4392 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4393 TREE_NO_WARNING (other) = 1;
4394 other = get_formal_tmp_var (other, pre_p);
4396 realpart = code == REALPART_EXPR ? rhs : other;
4397 imagpart = code == REALPART_EXPR ? other : rhs;
4399 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4400 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4401 else
4402 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4404 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4405 *expr_p = (want_value) ? rhs : NULL_TREE;
4407 return GS_ALL_DONE;
4410 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4412 modify_expr
4413 : varname '=' rhs
4414 | '*' ID '=' rhs
4416 PRE_P points to the list where side effects that must happen before
4417 *EXPR_P should be stored.
4419 POST_P points to the list where side effects that must happen after
4420 *EXPR_P should be stored.
4422 WANT_VALUE is nonzero iff we want to use the value of this expression
4423 in another expression. */
4425 static enum gimplify_status
4426 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4427 bool want_value)
4429 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4430 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4431 enum gimplify_status ret = GS_UNHANDLED;
4432 gimple assign;
4433 location_t loc = EXPR_LOCATION (*expr_p);
4434 gimple_stmt_iterator gsi;
4436 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4437 || TREE_CODE (*expr_p) == INIT_EXPR);
4439 /* Trying to simplify a clobber using normal logic doesn't work,
4440 so handle it here. */
4441 if (TREE_CLOBBER_P (*from_p))
4443 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4444 if (ret == GS_ERROR)
4445 return ret;
4446 gcc_assert (!want_value
4447 && (TREE_CODE (*to_p) == VAR_DECL
4448 || TREE_CODE (*to_p) == MEM_REF));
4449 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4450 *expr_p = NULL;
4451 return GS_ALL_DONE;
4454 /* Insert pointer conversions required by the middle-end that are not
4455 required by the frontend. This fixes middle-end type checking for
4456 for example gcc.dg/redecl-6.c. */
4457 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4459 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4460 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4461 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4464 /* See if any simplifications can be done based on what the RHS is. */
4465 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4466 want_value);
4467 if (ret != GS_UNHANDLED)
4468 return ret;
4470 /* For zero sized types only gimplify the left hand side and right hand
4471 side as statements and throw away the assignment. Do this after
4472 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4473 types properly. */
4474 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4476 gimplify_stmt (from_p, pre_p);
4477 gimplify_stmt (to_p, pre_p);
4478 *expr_p = NULL_TREE;
4479 return GS_ALL_DONE;
4482 /* If the value being copied is of variable width, compute the length
4483 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4484 before gimplifying any of the operands so that we can resolve any
4485 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4486 the size of the expression to be copied, not of the destination, so
4487 that is what we must do here. */
4488 maybe_with_size_expr (from_p);
4490 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4491 if (ret == GS_ERROR)
4492 return ret;
4494 /* As a special case, we have to temporarily allow for assignments
4495 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4496 a toplevel statement, when gimplifying the GENERIC expression
4497 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4498 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4500 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4501 prevent gimplify_expr from trying to create a new temporary for
4502 foo's LHS, we tell it that it should only gimplify until it
4503 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4504 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4505 and all we need to do here is set 'a' to be its LHS. */
4506 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4507 fb_rvalue);
4508 if (ret == GS_ERROR)
4509 return ret;
4511 /* Now see if the above changed *from_p to something we handle specially. */
4512 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4513 want_value);
4514 if (ret != GS_UNHANDLED)
4515 return ret;
4517 /* If we've got a variable sized assignment between two lvalues (i.e. does
4518 not involve a call), then we can make things a bit more straightforward
4519 by converting the assignment to memcpy or memset. */
4520 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4522 tree from = TREE_OPERAND (*from_p, 0);
4523 tree size = TREE_OPERAND (*from_p, 1);
4525 if (TREE_CODE (from) == CONSTRUCTOR)
4526 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4528 if (is_gimple_addressable (from))
4530 *from_p = from;
4531 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4532 pre_p);
4536 /* Transform partial stores to non-addressable complex variables into
4537 total stores. This allows us to use real instead of virtual operands
4538 for these variables, which improves optimization. */
4539 if ((TREE_CODE (*to_p) == REALPART_EXPR
4540 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4541 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4542 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4544 /* Try to alleviate the effects of the gimplification creating artificial
4545 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4546 if (!gimplify_ctxp->into_ssa
4547 && TREE_CODE (*from_p) == VAR_DECL
4548 && DECL_IGNORED_P (*from_p)
4549 && DECL_P (*to_p)
4550 && !DECL_IGNORED_P (*to_p))
4552 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4553 DECL_NAME (*from_p)
4554 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4555 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4556 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4559 if (want_value && TREE_THIS_VOLATILE (*to_p))
4560 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4562 if (TREE_CODE (*from_p) == CALL_EXPR)
4564 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4565 instead of a GIMPLE_ASSIGN. */
4566 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4567 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4568 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4569 tree fndecl = get_callee_fndecl (*from_p);
4570 if (fndecl
4571 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4572 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4573 && call_expr_nargs (*from_p) == 3)
4574 assign = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4575 CALL_EXPR_ARG (*from_p, 0),
4576 CALL_EXPR_ARG (*from_p, 1),
4577 CALL_EXPR_ARG (*from_p, 2));
4578 else
4580 assign = gimple_build_call_from_tree (*from_p);
4581 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4583 notice_special_calls (assign);
4584 if (!gimple_call_noreturn_p (assign))
4585 gimple_call_set_lhs (assign, *to_p);
4587 else
4589 assign = gimple_build_assign (*to_p, *from_p);
4590 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4593 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4595 /* We should have got an SSA name from the start. */
4596 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4599 gimplify_seq_add_stmt (pre_p, assign);
4600 gsi = gsi_last (*pre_p);
4601 maybe_fold_stmt (&gsi);
4603 if (want_value)
4605 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4606 return GS_OK;
4608 else
4609 *expr_p = NULL;
4611 return GS_ALL_DONE;
4614 /* Gimplify a comparison between two variable-sized objects. Do this
4615 with a call to BUILT_IN_MEMCMP. */
4617 static enum gimplify_status
4618 gimplify_variable_sized_compare (tree *expr_p)
4620 location_t loc = EXPR_LOCATION (*expr_p);
4621 tree op0 = TREE_OPERAND (*expr_p, 0);
4622 tree op1 = TREE_OPERAND (*expr_p, 1);
4623 tree t, arg, dest, src, expr;
4625 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4626 arg = unshare_expr (arg);
4627 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4628 src = build_fold_addr_expr_loc (loc, op1);
4629 dest = build_fold_addr_expr_loc (loc, op0);
4630 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4631 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4633 expr
4634 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4635 SET_EXPR_LOCATION (expr, loc);
4636 *expr_p = expr;
4638 return GS_OK;
4641 /* Gimplify a comparison between two aggregate objects of integral scalar
4642 mode as a comparison between the bitwise equivalent scalar values. */
4644 static enum gimplify_status
4645 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4647 location_t loc = EXPR_LOCATION (*expr_p);
4648 tree op0 = TREE_OPERAND (*expr_p, 0);
4649 tree op1 = TREE_OPERAND (*expr_p, 1);
4651 tree type = TREE_TYPE (op0);
4652 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4654 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4655 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4657 *expr_p
4658 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4660 return GS_OK;
4663 /* Gimplify an expression sequence. This function gimplifies each
4664 expression and rewrites the original expression with the last
4665 expression of the sequence in GIMPLE form.
4667 PRE_P points to the list where the side effects for all the
4668 expressions in the sequence will be emitted.
4670 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4672 static enum gimplify_status
4673 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4675 tree t = *expr_p;
4679 tree *sub_p = &TREE_OPERAND (t, 0);
4681 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4682 gimplify_compound_expr (sub_p, pre_p, false);
4683 else
4684 gimplify_stmt (sub_p, pre_p);
4686 t = TREE_OPERAND (t, 1);
4688 while (TREE_CODE (t) == COMPOUND_EXPR);
4690 *expr_p = t;
4691 if (want_value)
4692 return GS_OK;
4693 else
4695 gimplify_stmt (expr_p, pre_p);
4696 return GS_ALL_DONE;
4700 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4701 gimplify. After gimplification, EXPR_P will point to a new temporary
4702 that holds the original value of the SAVE_EXPR node.
4704 PRE_P points to the list where side effects that must happen before
4705 *EXPR_P should be stored. */
4707 static enum gimplify_status
4708 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4710 enum gimplify_status ret = GS_ALL_DONE;
4711 tree val;
4713 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4714 val = TREE_OPERAND (*expr_p, 0);
4716 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4717 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4719 /* The operand may be a void-valued expression such as SAVE_EXPRs
4720 generated by the Java frontend for class initialization. It is
4721 being executed only for its side-effects. */
4722 if (TREE_TYPE (val) == void_type_node)
4724 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4725 is_gimple_stmt, fb_none);
4726 val = NULL;
4728 else
4729 val = get_initialized_tmp_var (val, pre_p, post_p);
4731 TREE_OPERAND (*expr_p, 0) = val;
4732 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4735 *expr_p = val;
4737 return ret;
4740 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4742 unary_expr
4743 : ...
4744 | '&' varname
4747 PRE_P points to the list where side effects that must happen before
4748 *EXPR_P should be stored.
4750 POST_P points to the list where side effects that must happen after
4751 *EXPR_P should be stored. */
4753 static enum gimplify_status
4754 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4756 tree expr = *expr_p;
4757 tree op0 = TREE_OPERAND (expr, 0);
4758 enum gimplify_status ret;
4759 location_t loc = EXPR_LOCATION (*expr_p);
4761 switch (TREE_CODE (op0))
4763 case INDIRECT_REF:
4764 do_indirect_ref:
4765 /* Check if we are dealing with an expression of the form '&*ptr'.
4766 While the front end folds away '&*ptr' into 'ptr', these
4767 expressions may be generated internally by the compiler (e.g.,
4768 builtins like __builtin_va_end). */
4769 /* Caution: the silent array decomposition semantics we allow for
4770 ADDR_EXPR means we can't always discard the pair. */
4771 /* Gimplification of the ADDR_EXPR operand may drop
4772 cv-qualification conversions, so make sure we add them if
4773 needed. */
4775 tree op00 = TREE_OPERAND (op0, 0);
4776 tree t_expr = TREE_TYPE (expr);
4777 tree t_op00 = TREE_TYPE (op00);
4779 if (!useless_type_conversion_p (t_expr, t_op00))
4780 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4781 *expr_p = op00;
4782 ret = GS_OK;
4784 break;
4786 case VIEW_CONVERT_EXPR:
4787 /* Take the address of our operand and then convert it to the type of
4788 this ADDR_EXPR.
4790 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4791 all clear. The impact of this transformation is even less clear. */
4793 /* If the operand is a useless conversion, look through it. Doing so
4794 guarantees that the ADDR_EXPR and its operand will remain of the
4795 same type. */
4796 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4797 op0 = TREE_OPERAND (op0, 0);
4799 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4800 build_fold_addr_expr_loc (loc,
4801 TREE_OPERAND (op0, 0)));
4802 ret = GS_OK;
4803 break;
4805 default:
4806 /* We use fb_either here because the C frontend sometimes takes
4807 the address of a call that returns a struct; see
4808 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4809 the implied temporary explicit. */
4811 /* Make the operand addressable. */
4812 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4813 is_gimple_addressable, fb_either);
4814 if (ret == GS_ERROR)
4815 break;
4817 /* Then mark it. Beware that it may not be possible to do so directly
4818 if a temporary has been created by the gimplification. */
4819 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4821 op0 = TREE_OPERAND (expr, 0);
4823 /* For various reasons, the gimplification of the expression
4824 may have made a new INDIRECT_REF. */
4825 if (TREE_CODE (op0) == INDIRECT_REF)
4826 goto do_indirect_ref;
4828 mark_addressable (TREE_OPERAND (expr, 0));
4830 /* Fix to PR/41163 (r151122) broke LIPO. Calls to builtin functions
4831 were 'canonicized' in profile-use pass, but not in profile-gen. */
4832 if (!flag_dyn_ipa)
4834 /* The FEs may end up building ADDR_EXPRs early on a decl with
4835 an incomplete type. Re-build ADDR_EXPRs in canonical form
4836 here. */
4837 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4838 *expr_p = build_fold_addr_expr (op0);
4841 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4842 recompute_tree_invariant_for_addr_expr (*expr_p);
4844 /* Fix to PR/41163 (r151122) broke LIPO. Calls to builtin functions
4845 were 'canonicized' in profile-use pass, but not in profile-gen. */
4846 if (!flag_dyn_ipa)
4848 /* If we re-built the ADDR_EXPR add a conversion to the original type
4849 if required. */
4850 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4851 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4854 break;
4857 return ret;
4860 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4861 value; output operands should be a gimple lvalue. */
4863 static enum gimplify_status
4864 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4866 tree expr;
4867 int noutputs;
4868 const char **oconstraints;
4869 int i;
4870 tree link;
4871 const char *constraint;
4872 bool allows_mem, allows_reg, is_inout;
4873 enum gimplify_status ret, tret;
4874 gimple stmt;
4875 vec<tree, va_gc> *inputs;
4876 vec<tree, va_gc> *outputs;
4877 vec<tree, va_gc> *clobbers;
4878 vec<tree, va_gc> *labels;
4879 tree link_next;
4881 expr = *expr_p;
4882 noutputs = list_length (ASM_OUTPUTS (expr));
4883 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4885 inputs = NULL;
4886 outputs = NULL;
4887 clobbers = NULL;
4888 labels = NULL;
4890 ret = GS_ALL_DONE;
4891 link_next = NULL_TREE;
4892 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4894 bool ok;
4895 size_t constraint_len;
4897 link_next = TREE_CHAIN (link);
4899 oconstraints[i]
4900 = constraint
4901 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4902 constraint_len = strlen (constraint);
4903 if (constraint_len == 0)
4904 continue;
4906 ok = parse_output_constraint (&constraint, i, 0, 0,
4907 &allows_mem, &allows_reg, &is_inout);
4908 if (!ok)
4910 ret = GS_ERROR;
4911 is_inout = false;
4914 if (!allows_reg && allows_mem)
4915 mark_addressable (TREE_VALUE (link));
4917 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4918 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4919 fb_lvalue | fb_mayfail);
4920 if (tret == GS_ERROR)
4922 error ("invalid lvalue in asm output %d", i);
4923 ret = tret;
4926 vec_safe_push (outputs, link);
4927 TREE_CHAIN (link) = NULL_TREE;
4929 if (is_inout)
4931 /* An input/output operand. To give the optimizers more
4932 flexibility, split it into separate input and output
4933 operands. */
4934 tree input;
4935 char buf[10];
4937 /* Turn the in/out constraint into an output constraint. */
4938 char *p = xstrdup (constraint);
4939 p[0] = '=';
4940 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4942 /* And add a matching input constraint. */
4943 if (allows_reg)
4945 sprintf (buf, "%d", i);
4947 /* If there are multiple alternatives in the constraint,
4948 handle each of them individually. Those that allow register
4949 will be replaced with operand number, the others will stay
4950 unchanged. */
4951 if (strchr (p, ',') != NULL)
4953 size_t len = 0, buflen = strlen (buf);
4954 char *beg, *end, *str, *dst;
4956 for (beg = p + 1;;)
4958 end = strchr (beg, ',');
4959 if (end == NULL)
4960 end = strchr (beg, '\0');
4961 if ((size_t) (end - beg) < buflen)
4962 len += buflen + 1;
4963 else
4964 len += end - beg + 1;
4965 if (*end)
4966 beg = end + 1;
4967 else
4968 break;
4971 str = (char *) alloca (len);
4972 for (beg = p + 1, dst = str;;)
4974 const char *tem;
4975 bool mem_p, reg_p, inout_p;
4977 end = strchr (beg, ',');
4978 if (end)
4979 *end = '\0';
4980 beg[-1] = '=';
4981 tem = beg - 1;
4982 parse_output_constraint (&tem, i, 0, 0,
4983 &mem_p, &reg_p, &inout_p);
4984 if (dst != str)
4985 *dst++ = ',';
4986 if (reg_p)
4988 memcpy (dst, buf, buflen);
4989 dst += buflen;
4991 else
4993 if (end)
4994 len = end - beg;
4995 else
4996 len = strlen (beg);
4997 memcpy (dst, beg, len);
4998 dst += len;
5000 if (end)
5001 beg = end + 1;
5002 else
5003 break;
5005 *dst = '\0';
5006 input = build_string (dst - str, str);
5008 else
5009 input = build_string (strlen (buf), buf);
5011 else
5012 input = build_string (constraint_len - 1, constraint + 1);
5014 free (p);
5016 input = build_tree_list (build_tree_list (NULL_TREE, input),
5017 unshare_expr (TREE_VALUE (link)));
5018 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5022 link_next = NULL_TREE;
5023 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5025 link_next = TREE_CHAIN (link);
5026 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5027 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5028 oconstraints, &allows_mem, &allows_reg);
5030 /* If we can't make copies, we can only accept memory. */
5031 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5033 if (allows_mem)
5034 allows_reg = 0;
5035 else
5037 error ("impossible constraint in %<asm%>");
5038 error ("non-memory input %d must stay in memory", i);
5039 return GS_ERROR;
5043 /* If the operand is a memory input, it should be an lvalue. */
5044 if (!allows_reg && allows_mem)
5046 tree inputv = TREE_VALUE (link);
5047 STRIP_NOPS (inputv);
5048 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5049 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5050 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5051 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5052 TREE_VALUE (link) = error_mark_node;
5053 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5054 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5055 mark_addressable (TREE_VALUE (link));
5056 if (tret == GS_ERROR)
5058 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5059 input_location = EXPR_LOCATION (TREE_VALUE (link));
5060 error ("memory input %d is not directly addressable", i);
5061 ret = tret;
5064 else
5066 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5067 is_gimple_asm_val, fb_rvalue);
5068 if (tret == GS_ERROR)
5069 ret = tret;
5072 TREE_CHAIN (link) = NULL_TREE;
5073 vec_safe_push (inputs, link);
5076 link_next = NULL_TREE;
5077 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5079 link_next = TREE_CHAIN (link);
5080 TREE_CHAIN (link) = NULL_TREE;
5081 vec_safe_push (clobbers, link);
5084 link_next = NULL_TREE;
5085 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5087 link_next = TREE_CHAIN (link);
5088 TREE_CHAIN (link) = NULL_TREE;
5089 vec_safe_push (labels, link);
5092 /* Do not add ASMs with errors to the gimple IL stream. */
5093 if (ret != GS_ERROR)
5095 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5096 inputs, outputs, clobbers, labels);
5098 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5099 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5101 gimplify_seq_add_stmt (pre_p, stmt);
5104 return ret;
5107 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5108 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5109 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5110 return to this function.
5112 FIXME should we complexify the prequeue handling instead? Or use flags
5113 for all the cleanups and let the optimizer tighten them up? The current
5114 code seems pretty fragile; it will break on a cleanup within any
5115 non-conditional nesting. But any such nesting would be broken, anyway;
5116 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5117 and continues out of it. We can do that at the RTL level, though, so
5118 having an optimizer to tighten up try/finally regions would be a Good
5119 Thing. */
5121 static enum gimplify_status
5122 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5124 gimple_stmt_iterator iter;
5125 gimple_seq body_sequence = NULL;
5127 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5129 /* We only care about the number of conditions between the innermost
5130 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5131 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5132 int old_conds = gimplify_ctxp->conditions;
5133 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5134 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5135 gimplify_ctxp->conditions = 0;
5136 gimplify_ctxp->conditional_cleanups = NULL;
5137 gimplify_ctxp->in_cleanup_point_expr = true;
5139 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5141 gimplify_ctxp->conditions = old_conds;
5142 gimplify_ctxp->conditional_cleanups = old_cleanups;
5143 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5145 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5147 gimple wce = gsi_stmt (iter);
5149 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5151 if (gsi_one_before_end_p (iter))
5153 /* Note that gsi_insert_seq_before and gsi_remove do not
5154 scan operands, unlike some other sequence mutators. */
5155 if (!gimple_wce_cleanup_eh_only (wce))
5156 gsi_insert_seq_before_without_update (&iter,
5157 gimple_wce_cleanup (wce),
5158 GSI_SAME_STMT);
5159 gsi_remove (&iter, true);
5160 break;
5162 else
5164 gimple_statement_try *gtry;
5165 gimple_seq seq;
5166 enum gimple_try_flags kind;
5168 if (gimple_wce_cleanup_eh_only (wce))
5169 kind = GIMPLE_TRY_CATCH;
5170 else
5171 kind = GIMPLE_TRY_FINALLY;
5172 seq = gsi_split_seq_after (iter);
5174 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5175 /* Do not use gsi_replace here, as it may scan operands.
5176 We want to do a simple structural modification only. */
5177 gsi_set_stmt (&iter, gtry);
5178 iter = gsi_start (gtry->eval);
5181 else
5182 gsi_next (&iter);
5185 gimplify_seq_add_seq (pre_p, body_sequence);
5186 if (temp)
5188 *expr_p = temp;
5189 return GS_OK;
5191 else
5193 *expr_p = NULL;
5194 return GS_ALL_DONE;
5198 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5199 is the cleanup action required. EH_ONLY is true if the cleanup should
5200 only be executed if an exception is thrown, not on normal exit. */
5202 static void
5203 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5205 gimple wce;
5206 gimple_seq cleanup_stmts = NULL;
5208 /* Errors can result in improperly nested cleanups. Which results in
5209 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5210 if (seen_error ())
5211 return;
5213 if (gimple_conditional_context ())
5215 /* If we're in a conditional context, this is more complex. We only
5216 want to run the cleanup if we actually ran the initialization that
5217 necessitates it, but we want to run it after the end of the
5218 conditional context. So we wrap the try/finally around the
5219 condition and use a flag to determine whether or not to actually
5220 run the destructor. Thus
5222 test ? f(A()) : 0
5224 becomes (approximately)
5226 flag = 0;
5227 try {
5228 if (test) { A::A(temp); flag = 1; val = f(temp); }
5229 else { val = 0; }
5230 } finally {
5231 if (flag) A::~A(temp);
5235 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5236 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5237 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5239 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5240 gimplify_stmt (&cleanup, &cleanup_stmts);
5241 wce = gimple_build_wce (cleanup_stmts);
5243 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5244 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5245 gimplify_seq_add_stmt (pre_p, ftrue);
5247 /* Because of this manipulation, and the EH edges that jump
5248 threading cannot redirect, the temporary (VAR) will appear
5249 to be used uninitialized. Don't warn. */
5250 TREE_NO_WARNING (var) = 1;
5252 else
5254 gimplify_stmt (&cleanup, &cleanup_stmts);
5255 wce = gimple_build_wce (cleanup_stmts);
5256 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5257 gimplify_seq_add_stmt (pre_p, wce);
5261 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5263 static enum gimplify_status
5264 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5266 tree targ = *expr_p;
5267 tree temp = TARGET_EXPR_SLOT (targ);
5268 tree init = TARGET_EXPR_INITIAL (targ);
5269 enum gimplify_status ret;
5271 if (init)
5273 tree cleanup = NULL_TREE;
5275 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5276 to the temps list. Handle also variable length TARGET_EXPRs. */
5277 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5279 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5280 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5281 gimplify_vla_decl (temp, pre_p);
5283 else
5284 gimple_add_tmp_var (temp);
5286 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5287 expression is supposed to initialize the slot. */
5288 if (VOID_TYPE_P (TREE_TYPE (init)))
5289 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5290 else
5292 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5293 init = init_expr;
5294 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5295 init = NULL;
5296 ggc_free (init_expr);
5298 if (ret == GS_ERROR)
5300 /* PR c++/28266 Make sure this is expanded only once. */
5301 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5302 return GS_ERROR;
5304 if (init)
5305 gimplify_and_add (init, pre_p);
5307 /* If needed, push the cleanup for the temp. */
5308 if (TARGET_EXPR_CLEANUP (targ))
5310 if (CLEANUP_EH_ONLY (targ))
5311 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5312 CLEANUP_EH_ONLY (targ), pre_p);
5313 else
5314 cleanup = TARGET_EXPR_CLEANUP (targ);
5317 /* Add a clobber for the temporary going out of scope, like
5318 gimplify_bind_expr. */
5319 if (gimplify_ctxp->in_cleanup_point_expr
5320 && needs_to_live_in_memory (temp)
5321 && flag_stack_reuse == SR_ALL)
5323 tree clobber = build_constructor (TREE_TYPE (temp),
5324 NULL);
5325 TREE_THIS_VOLATILE (clobber) = true;
5326 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5327 if (cleanup)
5328 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5329 clobber);
5330 else
5331 cleanup = clobber;
5334 if (cleanup)
5335 gimple_push_cleanup (temp, cleanup, false, pre_p);
5337 /* Only expand this once. */
5338 TREE_OPERAND (targ, 3) = init;
5339 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5341 else
5342 /* We should have expanded this before. */
5343 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5345 *expr_p = temp;
5346 return GS_OK;
5349 /* Gimplification of expression trees. */
5351 /* Gimplify an expression which appears at statement context. The
5352 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5353 NULL, a new sequence is allocated.
5355 Return true if we actually added a statement to the queue. */
5357 bool
5358 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5360 gimple_seq_node last;
5362 last = gimple_seq_last (*seq_p);
5363 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5364 return last != gimple_seq_last (*seq_p);
5367 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5368 to CTX. If entries already exist, force them to be some flavor of private.
5369 If there is no enclosing parallel, do nothing. */
5371 void
5372 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5374 splay_tree_node n;
5376 if (decl == NULL || !DECL_P (decl))
5377 return;
5381 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5382 if (n != NULL)
5384 if (n->value & GOVD_SHARED)
5385 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5386 else if (n->value & GOVD_MAP)
5387 n->value |= GOVD_MAP_TO_ONLY;
5388 else
5389 return;
5391 else if (ctx->region_type == ORT_TARGET)
5392 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5393 else if (ctx->region_type != ORT_WORKSHARE
5394 && ctx->region_type != ORT_SIMD
5395 && ctx->region_type != ORT_TARGET_DATA)
5396 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5398 ctx = ctx->outer_context;
5400 while (ctx);
5403 /* Similarly for each of the type sizes of TYPE. */
5405 static void
5406 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5408 if (type == NULL || type == error_mark_node)
5409 return;
5410 type = TYPE_MAIN_VARIANT (type);
5412 if (pointer_set_insert (ctx->privatized_types, type))
5413 return;
5415 switch (TREE_CODE (type))
5417 case INTEGER_TYPE:
5418 case ENUMERAL_TYPE:
5419 case BOOLEAN_TYPE:
5420 case REAL_TYPE:
5421 case FIXED_POINT_TYPE:
5422 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5423 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5424 break;
5426 case ARRAY_TYPE:
5427 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5428 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5429 break;
5431 case RECORD_TYPE:
5432 case UNION_TYPE:
5433 case QUAL_UNION_TYPE:
5435 tree field;
5436 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5437 if (TREE_CODE (field) == FIELD_DECL)
5439 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5440 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5443 break;
5445 case POINTER_TYPE:
5446 case REFERENCE_TYPE:
5447 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5448 break;
5450 default:
5451 break;
5454 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5455 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5456 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5459 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5461 static void
5462 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5464 splay_tree_node n;
5465 unsigned int nflags;
5466 tree t;
5468 if (error_operand_p (decl))
5469 return;
5471 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5472 there are constructors involved somewhere. */
5473 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5474 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5475 flags |= GOVD_SEEN;
5477 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5478 if (n != NULL && n->value != GOVD_ALIGNED)
5480 /* We shouldn't be re-adding the decl with the same data
5481 sharing class. */
5482 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5483 /* The only combination of data sharing classes we should see is
5484 FIRSTPRIVATE and LASTPRIVATE. */
5485 nflags = n->value | flags;
5486 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5487 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5488 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5489 n->value = nflags;
5490 return;
5493 /* When adding a variable-sized variable, we have to handle all sorts
5494 of additional bits of data: the pointer replacement variable, and
5495 the parameters of the type. */
5496 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5498 /* Add the pointer replacement variable as PRIVATE if the variable
5499 replacement is private, else FIRSTPRIVATE since we'll need the
5500 address of the original variable either for SHARED, or for the
5501 copy into or out of the context. */
5502 if (!(flags & GOVD_LOCAL))
5504 nflags = flags & GOVD_MAP
5505 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5506 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5507 nflags |= flags & GOVD_SEEN;
5508 t = DECL_VALUE_EXPR (decl);
5509 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5510 t = TREE_OPERAND (t, 0);
5511 gcc_assert (DECL_P (t));
5512 omp_add_variable (ctx, t, nflags);
5515 /* Add all of the variable and type parameters (which should have
5516 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5517 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5518 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5519 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5521 /* The variable-sized variable itself is never SHARED, only some form
5522 of PRIVATE. The sharing would take place via the pointer variable
5523 which we remapped above. */
5524 if (flags & GOVD_SHARED)
5525 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5526 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5528 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5529 alloca statement we generate for the variable, so make sure it
5530 is available. This isn't automatically needed for the SHARED
5531 case, since we won't be allocating local storage then.
5532 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5533 in this case omp_notice_variable will be called later
5534 on when it is gimplified. */
5535 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5536 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5537 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5539 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5540 && lang_hooks.decls.omp_privatize_by_reference (decl))
5542 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5544 /* Similar to the direct variable sized case above, we'll need the
5545 size of references being privatized. */
5546 if ((flags & GOVD_SHARED) == 0)
5548 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5549 if (TREE_CODE (t) != INTEGER_CST)
5550 omp_notice_variable (ctx, t, true);
5554 if (n != NULL)
5555 n->value |= flags;
5556 else
5557 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5560 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5561 This just prints out diagnostics about threadprivate variable uses
5562 in untied tasks. If DECL2 is non-NULL, prevent this warning
5563 on that variable. */
5565 static bool
5566 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5567 tree decl2)
5569 splay_tree_node n;
5570 struct gimplify_omp_ctx *octx;
5572 for (octx = ctx; octx; octx = octx->outer_context)
5573 if (octx->region_type == ORT_TARGET)
5575 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5576 if (n == NULL)
5578 error ("threadprivate variable %qE used in target region",
5579 DECL_NAME (decl));
5580 error_at (octx->location, "enclosing target region");
5581 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5583 if (decl2)
5584 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5587 if (ctx->region_type != ORT_UNTIED_TASK)
5588 return false;
5589 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5590 if (n == NULL)
5592 error ("threadprivate variable %qE used in untied task",
5593 DECL_NAME (decl));
5594 error_at (ctx->location, "enclosing task");
5595 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5597 if (decl2)
5598 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5599 return false;
5602 /* Record the fact that DECL was used within the OpenMP context CTX.
5603 IN_CODE is true when real code uses DECL, and false when we should
5604 merely emit default(none) errors. Return true if DECL is going to
5605 be remapped and thus DECL shouldn't be gimplified into its
5606 DECL_VALUE_EXPR (if any). */
5608 static bool
5609 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5611 splay_tree_node n;
5612 unsigned flags = in_code ? GOVD_SEEN : 0;
5613 bool ret = false, shared;
5615 if (error_operand_p (decl))
5616 return false;
5618 /* Threadprivate variables are predetermined. */
5619 if (is_global_var (decl))
5621 if (DECL_THREAD_LOCAL_P (decl))
5622 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5624 if (DECL_HAS_VALUE_EXPR_P (decl))
5626 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5628 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5629 return omp_notice_threadprivate_variable (ctx, decl, value);
5633 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5634 if (ctx->region_type == ORT_TARGET)
5636 if (n == NULL)
5638 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5640 error ("%qD referenced in target region does not have "
5641 "a mappable type", decl);
5642 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5644 else
5645 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5647 else
5648 n->value |= flags;
5649 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5650 goto do_outer;
5653 if (n == NULL)
5655 enum omp_clause_default_kind default_kind, kind;
5656 struct gimplify_omp_ctx *octx;
5658 if (ctx->region_type == ORT_WORKSHARE
5659 || ctx->region_type == ORT_SIMD
5660 || ctx->region_type == ORT_TARGET_DATA)
5661 goto do_outer;
5663 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5664 remapped firstprivate instead of shared. To some extent this is
5665 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5666 default_kind = ctx->default_kind;
5667 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5668 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5669 default_kind = kind;
5671 switch (default_kind)
5673 case OMP_CLAUSE_DEFAULT_NONE:
5674 if ((ctx->region_type & ORT_TASK) != 0)
5676 error ("%qE not specified in enclosing task",
5677 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5678 error_at (ctx->location, "enclosing task");
5680 else if (ctx->region_type == ORT_TEAMS)
5682 error ("%qE not specified in enclosing teams construct",
5683 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5684 error_at (ctx->location, "enclosing teams construct");
5686 else
5688 error ("%qE not specified in enclosing parallel",
5689 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5690 error_at (ctx->location, "enclosing parallel");
5692 /* FALLTHRU */
5693 case OMP_CLAUSE_DEFAULT_SHARED:
5694 flags |= GOVD_SHARED;
5695 break;
5696 case OMP_CLAUSE_DEFAULT_PRIVATE:
5697 flags |= GOVD_PRIVATE;
5698 break;
5699 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5700 flags |= GOVD_FIRSTPRIVATE;
5701 break;
5702 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5703 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5704 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5705 if (ctx->outer_context)
5706 omp_notice_variable (ctx->outer_context, decl, in_code);
5707 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5709 splay_tree_node n2;
5711 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5712 continue;
5713 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5714 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5716 flags |= GOVD_FIRSTPRIVATE;
5717 break;
5719 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5720 break;
5722 if (flags & GOVD_FIRSTPRIVATE)
5723 break;
5724 if (octx == NULL
5725 && (TREE_CODE (decl) == PARM_DECL
5726 || (!is_global_var (decl)
5727 && DECL_CONTEXT (decl) == current_function_decl)))
5729 flags |= GOVD_FIRSTPRIVATE;
5730 break;
5732 flags |= GOVD_SHARED;
5733 break;
5734 default:
5735 gcc_unreachable ();
5738 if ((flags & GOVD_PRIVATE)
5739 && lang_hooks.decls.omp_private_outer_ref (decl))
5740 flags |= GOVD_PRIVATE_OUTER_REF;
5742 omp_add_variable (ctx, decl, flags);
5744 shared = (flags & GOVD_SHARED) != 0;
5745 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5746 goto do_outer;
5749 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5750 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5751 && DECL_SIZE (decl)
5752 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5754 splay_tree_node n2;
5755 tree t = DECL_VALUE_EXPR (decl);
5756 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5757 t = TREE_OPERAND (t, 0);
5758 gcc_assert (DECL_P (t));
5759 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5760 n2->value |= GOVD_SEEN;
5763 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5764 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5766 /* If nothing changed, there's nothing left to do. */
5767 if ((n->value & flags) == flags)
5768 return ret;
5769 flags |= n->value;
5770 n->value = flags;
5772 do_outer:
5773 /* If the variable is private in the current context, then we don't
5774 need to propagate anything to an outer context. */
5775 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5776 return ret;
5777 if (ctx->outer_context
5778 && omp_notice_variable (ctx->outer_context, decl, in_code))
5779 return true;
5780 return ret;
5783 /* Verify that DECL is private within CTX. If there's specific information
5784 to the contrary in the innermost scope, generate an error. */
5786 static bool
5787 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
5789 splay_tree_node n;
5791 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5792 if (n != NULL)
5794 if (n->value & GOVD_SHARED)
5796 if (ctx == gimplify_omp_ctxp)
5798 if (simd)
5799 error ("iteration variable %qE is predetermined linear",
5800 DECL_NAME (decl));
5801 else
5802 error ("iteration variable %qE should be private",
5803 DECL_NAME (decl));
5804 n->value = GOVD_PRIVATE;
5805 return true;
5807 else
5808 return false;
5810 else if ((n->value & GOVD_EXPLICIT) != 0
5811 && (ctx == gimplify_omp_ctxp
5812 || (ctx->region_type == ORT_COMBINED_PARALLEL
5813 && gimplify_omp_ctxp->outer_context == ctx)))
5815 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5816 error ("iteration variable %qE should not be firstprivate",
5817 DECL_NAME (decl));
5818 else if ((n->value & GOVD_REDUCTION) != 0)
5819 error ("iteration variable %qE should not be reduction",
5820 DECL_NAME (decl));
5821 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
5822 error ("iteration variable %qE should not be lastprivate",
5823 DECL_NAME (decl));
5824 else if (simd && (n->value & GOVD_PRIVATE) != 0)
5825 error ("iteration variable %qE should not be private",
5826 DECL_NAME (decl));
5827 else if (simd && (n->value & GOVD_LINEAR) != 0)
5828 error ("iteration variable %qE is predetermined linear",
5829 DECL_NAME (decl));
5831 return (ctx == gimplify_omp_ctxp
5832 || (ctx->region_type == ORT_COMBINED_PARALLEL
5833 && gimplify_omp_ctxp->outer_context == ctx));
5836 if (ctx->region_type != ORT_WORKSHARE
5837 && ctx->region_type != ORT_SIMD)
5838 return false;
5839 else if (ctx->outer_context)
5840 return omp_is_private (ctx->outer_context, decl, simd);
5841 return false;
5844 /* Return true if DECL is private within a parallel region
5845 that binds to the current construct's context or in parallel
5846 region's REDUCTION clause. */
5848 static bool
5849 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
5851 splay_tree_node n;
5855 ctx = ctx->outer_context;
5856 if (ctx == NULL)
5857 return !(is_global_var (decl)
5858 /* References might be private, but might be shared too,
5859 when checking for copyprivate, assume they might be
5860 private, otherwise assume they might be shared. */
5861 || (!copyprivate
5862 && lang_hooks.decls.omp_privatize_by_reference (decl)));
5864 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
5865 continue;
5867 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5868 if (n != NULL)
5869 return (n->value & GOVD_SHARED) == 0;
5871 while (ctx->region_type == ORT_WORKSHARE
5872 || ctx->region_type == ORT_SIMD);
5873 return false;
5876 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5877 and previous omp contexts. */
5879 static void
5880 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5881 enum omp_region_type region_type)
5883 struct gimplify_omp_ctx *ctx, *outer_ctx;
5884 tree c;
5886 ctx = new_omp_context (region_type);
5887 outer_ctx = ctx->outer_context;
5889 while ((c = *list_p) != NULL)
5891 bool remove = false;
5892 bool notice_outer = true;
5893 const char *check_non_private = NULL;
5894 unsigned int flags;
5895 tree decl;
5897 switch (OMP_CLAUSE_CODE (c))
5899 case OMP_CLAUSE_PRIVATE:
5900 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5901 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5903 flags |= GOVD_PRIVATE_OUTER_REF;
5904 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5906 else
5907 notice_outer = false;
5908 goto do_add;
5909 case OMP_CLAUSE_SHARED:
5910 flags = GOVD_SHARED | GOVD_EXPLICIT;
5911 goto do_add;
5912 case OMP_CLAUSE_FIRSTPRIVATE:
5913 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5914 check_non_private = "firstprivate";
5915 goto do_add;
5916 case OMP_CLAUSE_LASTPRIVATE:
5917 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5918 check_non_private = "lastprivate";
5919 goto do_add;
5920 case OMP_CLAUSE_REDUCTION:
5921 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5922 check_non_private = "reduction";
5923 goto do_add;
5924 case OMP_CLAUSE_LINEAR:
5925 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
5926 is_gimple_val, fb_rvalue) == GS_ERROR)
5928 remove = true;
5929 break;
5931 flags = GOVD_LINEAR | GOVD_EXPLICIT;
5932 goto do_add;
5934 case OMP_CLAUSE_MAP:
5935 if (OMP_CLAUSE_SIZE (c)
5936 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5937 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5939 remove = true;
5940 break;
5942 decl = OMP_CLAUSE_DECL (c);
5943 if (!DECL_P (decl))
5945 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5946 NULL, is_gimple_lvalue, fb_lvalue)
5947 == GS_ERROR)
5949 remove = true;
5950 break;
5952 break;
5954 flags = GOVD_MAP | GOVD_EXPLICIT;
5955 goto do_add;
5957 case OMP_CLAUSE_DEPEND:
5958 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
5960 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
5961 NULL, is_gimple_val, fb_rvalue);
5962 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
5964 if (error_operand_p (OMP_CLAUSE_DECL (c)))
5966 remove = true;
5967 break;
5969 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
5970 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
5971 is_gimple_val, fb_rvalue) == GS_ERROR)
5973 remove = true;
5974 break;
5976 break;
5978 case OMP_CLAUSE_TO:
5979 case OMP_CLAUSE_FROM:
5980 if (OMP_CLAUSE_SIZE (c)
5981 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
5982 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
5984 remove = true;
5985 break;
5987 decl = OMP_CLAUSE_DECL (c);
5988 if (error_operand_p (decl))
5990 remove = true;
5991 break;
5993 if (!DECL_P (decl))
5995 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
5996 NULL, is_gimple_lvalue, fb_lvalue)
5997 == GS_ERROR)
5999 remove = true;
6000 break;
6002 break;
6004 goto do_notice;
6006 do_add:
6007 decl = OMP_CLAUSE_DECL (c);
6008 if (error_operand_p (decl))
6010 remove = true;
6011 break;
6013 omp_add_variable (ctx, decl, flags);
6014 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6015 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6017 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6018 GOVD_LOCAL | GOVD_SEEN);
6019 gimplify_omp_ctxp = ctx;
6020 push_gimplify_context ();
6022 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6023 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6025 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6026 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6027 pop_gimplify_context
6028 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6029 push_gimplify_context ();
6030 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6031 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6032 pop_gimplify_context
6033 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6034 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6035 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6037 gimplify_omp_ctxp = outer_ctx;
6039 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6040 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6042 gimplify_omp_ctxp = ctx;
6043 push_gimplify_context ();
6044 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6046 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6047 NULL, NULL);
6048 TREE_SIDE_EFFECTS (bind) = 1;
6049 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6050 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6052 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6053 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6054 pop_gimplify_context
6055 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6056 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6058 gimplify_omp_ctxp = outer_ctx;
6060 if (notice_outer)
6061 goto do_notice;
6062 break;
6064 case OMP_CLAUSE_COPYIN:
6065 case OMP_CLAUSE_COPYPRIVATE:
6066 decl = OMP_CLAUSE_DECL (c);
6067 if (error_operand_p (decl))
6069 remove = true;
6070 break;
6072 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6073 && !remove
6074 && !omp_check_private (ctx, decl, true))
6076 remove = true;
6077 if (is_global_var (decl))
6079 if (DECL_THREAD_LOCAL_P (decl))
6080 remove = false;
6081 else if (DECL_HAS_VALUE_EXPR_P (decl))
6083 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6085 if (value
6086 && DECL_P (value)
6087 && DECL_THREAD_LOCAL_P (value))
6088 remove = false;
6091 if (remove)
6092 error_at (OMP_CLAUSE_LOCATION (c),
6093 "copyprivate variable %qE is not threadprivate"
6094 " or private in outer context", DECL_NAME (decl));
6096 do_notice:
6097 if (outer_ctx)
6098 omp_notice_variable (outer_ctx, decl, true);
6099 if (check_non_private
6100 && region_type == ORT_WORKSHARE
6101 && omp_check_private (ctx, decl, false))
6103 error ("%s variable %qE is private in outer context",
6104 check_non_private, DECL_NAME (decl));
6105 remove = true;
6107 break;
6109 case OMP_CLAUSE_FINAL:
6110 case OMP_CLAUSE_IF:
6111 OMP_CLAUSE_OPERAND (c, 0)
6112 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6113 /* Fall through. */
6115 case OMP_CLAUSE_SCHEDULE:
6116 case OMP_CLAUSE_NUM_THREADS:
6117 case OMP_CLAUSE_NUM_TEAMS:
6118 case OMP_CLAUSE_THREAD_LIMIT:
6119 case OMP_CLAUSE_DIST_SCHEDULE:
6120 case OMP_CLAUSE_DEVICE:
6121 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6122 is_gimple_val, fb_rvalue) == GS_ERROR)
6123 remove = true;
6124 break;
6126 case OMP_CLAUSE_NOWAIT:
6127 case OMP_CLAUSE_ORDERED:
6128 case OMP_CLAUSE_UNTIED:
6129 case OMP_CLAUSE_COLLAPSE:
6130 case OMP_CLAUSE_MERGEABLE:
6131 case OMP_CLAUSE_PROC_BIND:
6132 case OMP_CLAUSE_SAFELEN:
6133 break;
6135 case OMP_CLAUSE_ALIGNED:
6136 decl = OMP_CLAUSE_DECL (c);
6137 if (error_operand_p (decl))
6139 remove = true;
6140 break;
6142 if (!is_global_var (decl)
6143 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6144 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6145 break;
6147 case OMP_CLAUSE_DEFAULT:
6148 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6149 break;
6151 default:
6152 gcc_unreachable ();
6155 if (remove)
6156 *list_p = OMP_CLAUSE_CHAIN (c);
6157 else
6158 list_p = &OMP_CLAUSE_CHAIN (c);
6161 gimplify_omp_ctxp = ctx;
6164 /* For all variables that were not actually used within the context,
6165 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6167 static int
6168 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6170 tree *list_p = (tree *) data;
6171 tree decl = (tree) n->key;
6172 unsigned flags = n->value;
6173 enum omp_clause_code code;
6174 tree clause;
6175 bool private_debug;
6177 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6178 return 0;
6179 if ((flags & GOVD_SEEN) == 0)
6180 return 0;
6181 if (flags & GOVD_DEBUG_PRIVATE)
6183 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6184 private_debug = true;
6186 else if (flags & GOVD_MAP)
6187 private_debug = false;
6188 else
6189 private_debug
6190 = lang_hooks.decls.omp_private_debug_clause (decl,
6191 !!(flags & GOVD_SHARED));
6192 if (private_debug)
6193 code = OMP_CLAUSE_PRIVATE;
6194 else if (flags & GOVD_MAP)
6195 code = OMP_CLAUSE_MAP;
6196 else if (flags & GOVD_SHARED)
6198 if (is_global_var (decl))
6200 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6201 while (ctx != NULL)
6203 splay_tree_node on
6204 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6205 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6206 | GOVD_PRIVATE | GOVD_REDUCTION
6207 | GOVD_LINEAR)) != 0)
6208 break;
6209 ctx = ctx->outer_context;
6211 if (ctx == NULL)
6212 return 0;
6214 code = OMP_CLAUSE_SHARED;
6216 else if (flags & GOVD_PRIVATE)
6217 code = OMP_CLAUSE_PRIVATE;
6218 else if (flags & GOVD_FIRSTPRIVATE)
6219 code = OMP_CLAUSE_FIRSTPRIVATE;
6220 else if (flags & GOVD_LASTPRIVATE)
6221 code = OMP_CLAUSE_LASTPRIVATE;
6222 else if (flags & GOVD_ALIGNED)
6223 return 0;
6224 else
6225 gcc_unreachable ();
6227 clause = build_omp_clause (input_location, code);
6228 OMP_CLAUSE_DECL (clause) = decl;
6229 OMP_CLAUSE_CHAIN (clause) = *list_p;
6230 if (private_debug)
6231 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6232 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6233 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6234 else if (code == OMP_CLAUSE_MAP)
6236 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6237 ? OMP_CLAUSE_MAP_TO
6238 : OMP_CLAUSE_MAP_TOFROM;
6239 if (DECL_SIZE (decl)
6240 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6242 tree decl2 = DECL_VALUE_EXPR (decl);
6243 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6244 decl2 = TREE_OPERAND (decl2, 0);
6245 gcc_assert (DECL_P (decl2));
6246 tree mem = build_simple_mem_ref (decl2);
6247 OMP_CLAUSE_DECL (clause) = mem;
6248 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6249 if (gimplify_omp_ctxp->outer_context)
6251 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6252 omp_notice_variable (ctx, decl2, true);
6253 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6255 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6256 OMP_CLAUSE_MAP);
6257 OMP_CLAUSE_DECL (nc) = decl;
6258 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6259 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6260 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6261 OMP_CLAUSE_CHAIN (clause) = nc;
6264 *list_p = clause;
6265 lang_hooks.decls.omp_finish_clause (clause);
6267 return 0;
6270 static void
6271 gimplify_adjust_omp_clauses (tree *list_p)
6273 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6274 tree c, decl;
6276 while ((c = *list_p) != NULL)
6278 splay_tree_node n;
6279 bool remove = false;
6281 switch (OMP_CLAUSE_CODE (c))
6283 case OMP_CLAUSE_PRIVATE:
6284 case OMP_CLAUSE_SHARED:
6285 case OMP_CLAUSE_FIRSTPRIVATE:
6286 case OMP_CLAUSE_LINEAR:
6287 decl = OMP_CLAUSE_DECL (c);
6288 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6289 remove = !(n->value & GOVD_SEEN);
6290 if (! remove)
6292 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6293 if ((n->value & GOVD_DEBUG_PRIVATE)
6294 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6296 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6297 || ((n->value & GOVD_DATA_SHARE_CLASS)
6298 == GOVD_PRIVATE));
6299 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6300 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6302 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6303 && ctx->outer_context
6304 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6305 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6306 && !is_global_var (decl))
6308 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6310 n = splay_tree_lookup (ctx->outer_context->variables,
6311 (splay_tree_key) decl);
6312 if (n == NULL
6313 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6315 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6316 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6317 if (n == NULL)
6318 omp_add_variable (ctx->outer_context, decl,
6319 flags | GOVD_SEEN);
6320 else
6321 n->value |= flags | GOVD_SEEN;
6324 else
6325 omp_notice_variable (ctx->outer_context, decl, true);
6328 break;
6330 case OMP_CLAUSE_LASTPRIVATE:
6331 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6332 accurately reflect the presence of a FIRSTPRIVATE clause. */
6333 decl = OMP_CLAUSE_DECL (c);
6334 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6335 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6336 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6337 break;
6339 case OMP_CLAUSE_ALIGNED:
6340 decl = OMP_CLAUSE_DECL (c);
6341 if (!is_global_var (decl))
6343 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6344 remove = n == NULL || !(n->value & GOVD_SEEN);
6345 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6347 struct gimplify_omp_ctx *octx;
6348 if (n != NULL
6349 && (n->value & (GOVD_DATA_SHARE_CLASS
6350 & ~GOVD_FIRSTPRIVATE)))
6351 remove = true;
6352 else
6353 for (octx = ctx->outer_context; octx;
6354 octx = octx->outer_context)
6356 n = splay_tree_lookup (octx->variables,
6357 (splay_tree_key) decl);
6358 if (n == NULL)
6359 continue;
6360 if (n->value & GOVD_LOCAL)
6361 break;
6362 /* We have to avoid assigning a shared variable
6363 to itself when trying to add
6364 __builtin_assume_aligned. */
6365 if (n->value & GOVD_SHARED)
6367 remove = true;
6368 break;
6373 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6375 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6376 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6377 remove = true;
6379 break;
6381 case OMP_CLAUSE_MAP:
6382 decl = OMP_CLAUSE_DECL (c);
6383 if (!DECL_P (decl))
6384 break;
6385 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6386 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6387 remove = true;
6388 else if (DECL_SIZE (decl)
6389 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6390 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6392 tree decl2 = DECL_VALUE_EXPR (decl);
6393 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6394 decl2 = TREE_OPERAND (decl2, 0);
6395 gcc_assert (DECL_P (decl2));
6396 tree mem = build_simple_mem_ref (decl2);
6397 OMP_CLAUSE_DECL (c) = mem;
6398 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6399 if (ctx->outer_context)
6401 omp_notice_variable (ctx->outer_context, decl2, true);
6402 omp_notice_variable (ctx->outer_context,
6403 OMP_CLAUSE_SIZE (c), true);
6405 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6406 OMP_CLAUSE_MAP);
6407 OMP_CLAUSE_DECL (nc) = decl;
6408 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6409 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6410 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6411 OMP_CLAUSE_CHAIN (c) = nc;
6412 c = nc;
6414 break;
6416 case OMP_CLAUSE_TO:
6417 case OMP_CLAUSE_FROM:
6418 decl = OMP_CLAUSE_DECL (c);
6419 if (!DECL_P (decl))
6420 break;
6421 if (DECL_SIZE (decl)
6422 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6424 tree decl2 = DECL_VALUE_EXPR (decl);
6425 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6426 decl2 = TREE_OPERAND (decl2, 0);
6427 gcc_assert (DECL_P (decl2));
6428 tree mem = build_simple_mem_ref (decl2);
6429 OMP_CLAUSE_DECL (c) = mem;
6430 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6431 if (ctx->outer_context)
6433 omp_notice_variable (ctx->outer_context, decl2, true);
6434 omp_notice_variable (ctx->outer_context,
6435 OMP_CLAUSE_SIZE (c), true);
6438 break;
6440 case OMP_CLAUSE_REDUCTION:
6441 case OMP_CLAUSE_COPYIN:
6442 case OMP_CLAUSE_COPYPRIVATE:
6443 case OMP_CLAUSE_IF:
6444 case OMP_CLAUSE_NUM_THREADS:
6445 case OMP_CLAUSE_NUM_TEAMS:
6446 case OMP_CLAUSE_THREAD_LIMIT:
6447 case OMP_CLAUSE_DIST_SCHEDULE:
6448 case OMP_CLAUSE_DEVICE:
6449 case OMP_CLAUSE_SCHEDULE:
6450 case OMP_CLAUSE_NOWAIT:
6451 case OMP_CLAUSE_ORDERED:
6452 case OMP_CLAUSE_DEFAULT:
6453 case OMP_CLAUSE_UNTIED:
6454 case OMP_CLAUSE_COLLAPSE:
6455 case OMP_CLAUSE_FINAL:
6456 case OMP_CLAUSE_MERGEABLE:
6457 case OMP_CLAUSE_PROC_BIND:
6458 case OMP_CLAUSE_SAFELEN:
6459 case OMP_CLAUSE_DEPEND:
6460 break;
6462 default:
6463 gcc_unreachable ();
6466 if (remove)
6467 *list_p = OMP_CLAUSE_CHAIN (c);
6468 else
6469 list_p = &OMP_CLAUSE_CHAIN (c);
6472 /* Add in any implicit data sharing. */
6473 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6475 gimplify_omp_ctxp = ctx->outer_context;
6476 delete_omp_context (ctx);
6479 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6480 gimplification of the body, as well as scanning the body for used
6481 variables. We need to do this scan now, because variable-sized
6482 decls will be decomposed during gimplification. */
6484 static void
6485 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6487 tree expr = *expr_p;
6488 gimple g;
6489 gimple_seq body = NULL;
6491 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6492 OMP_PARALLEL_COMBINED (expr)
6493 ? ORT_COMBINED_PARALLEL
6494 : ORT_PARALLEL);
6496 push_gimplify_context ();
6498 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6499 if (gimple_code (g) == GIMPLE_BIND)
6500 pop_gimplify_context (g);
6501 else
6502 pop_gimplify_context (NULL);
6504 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6506 g = gimple_build_omp_parallel (body,
6507 OMP_PARALLEL_CLAUSES (expr),
6508 NULL_TREE, NULL_TREE);
6509 if (OMP_PARALLEL_COMBINED (expr))
6510 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6511 gimplify_seq_add_stmt (pre_p, g);
6512 *expr_p = NULL_TREE;
6515 /* Gimplify the contents of an OMP_TASK statement. This involves
6516 gimplification of the body, as well as scanning the body for used
6517 variables. We need to do this scan now, because variable-sized
6518 decls will be decomposed during gimplification. */
6520 static void
6521 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6523 tree expr = *expr_p;
6524 gimple g;
6525 gimple_seq body = NULL;
6527 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6528 find_omp_clause (OMP_TASK_CLAUSES (expr),
6529 OMP_CLAUSE_UNTIED)
6530 ? ORT_UNTIED_TASK : ORT_TASK);
6532 push_gimplify_context ();
6534 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6535 if (gimple_code (g) == GIMPLE_BIND)
6536 pop_gimplify_context (g);
6537 else
6538 pop_gimplify_context (NULL);
6540 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6542 g = gimple_build_omp_task (body,
6543 OMP_TASK_CLAUSES (expr),
6544 NULL_TREE, NULL_TREE,
6545 NULL_TREE, NULL_TREE, NULL_TREE);
6546 gimplify_seq_add_stmt (pre_p, g);
6547 *expr_p = NULL_TREE;
6550 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6551 with non-NULL OMP_FOR_INIT. */
6553 static tree
6554 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6556 *walk_subtrees = 0;
6557 switch (TREE_CODE (*tp))
6559 case OMP_FOR:
6560 *walk_subtrees = 1;
6561 /* FALLTHRU */
6562 case OMP_SIMD:
6563 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6564 return *tp;
6565 break;
6566 case BIND_EXPR:
6567 case STATEMENT_LIST:
6568 case OMP_PARALLEL:
6569 *walk_subtrees = 1;
6570 break;
6571 default:
6572 break;
6574 return NULL_TREE;
6577 /* Gimplify the gross structure of an OMP_FOR statement. */
6579 static enum gimplify_status
6580 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6582 tree for_stmt, orig_for_stmt, decl, var, t;
6583 enum gimplify_status ret = GS_ALL_DONE;
6584 enum gimplify_status tret;
6585 gimple gfor;
6586 gimple_seq for_body, for_pre_body;
6587 int i;
6588 bool simd;
6589 bitmap has_decl_expr = NULL;
6591 orig_for_stmt = for_stmt = *expr_p;
6593 simd = TREE_CODE (for_stmt) == OMP_SIMD
6594 || TREE_CODE (for_stmt) == CILK_SIMD;
6595 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6596 simd ? ORT_SIMD : ORT_WORKSHARE);
6598 /* Handle OMP_FOR_INIT. */
6599 for_pre_body = NULL;
6600 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6602 has_decl_expr = BITMAP_ALLOC (NULL);
6603 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6604 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6605 == VAR_DECL)
6607 t = OMP_FOR_PRE_BODY (for_stmt);
6608 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6610 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6612 tree_stmt_iterator si;
6613 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6614 tsi_next (&si))
6616 t = tsi_stmt (si);
6617 if (TREE_CODE (t) == DECL_EXPR
6618 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6619 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6623 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6624 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6626 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6628 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6629 NULL, NULL);
6630 gcc_assert (for_stmt != NULL_TREE);
6631 gimplify_omp_ctxp->combined_loop = true;
6634 for_body = NULL;
6635 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6636 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6637 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6638 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6639 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6641 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6642 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6643 decl = TREE_OPERAND (t, 0);
6644 gcc_assert (DECL_P (decl));
6645 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6646 || POINTER_TYPE_P (TREE_TYPE (decl)));
6648 /* Make sure the iteration variable is private. */
6649 tree c = NULL_TREE;
6650 if (orig_for_stmt != for_stmt)
6651 /* Do this only on innermost construct for combined ones. */;
6652 else if (simd)
6654 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6655 (splay_tree_key)decl);
6656 omp_is_private (gimplify_omp_ctxp, decl, simd);
6657 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6658 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6659 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6661 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6662 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6663 if (has_decl_expr
6664 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6665 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6666 OMP_CLAUSE_DECL (c) = decl;
6667 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6668 OMP_FOR_CLAUSES (for_stmt) = c;
6669 omp_add_variable (gimplify_omp_ctxp, decl,
6670 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6672 else
6674 bool lastprivate
6675 = (!has_decl_expr
6676 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6677 c = build_omp_clause (input_location,
6678 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6679 : OMP_CLAUSE_PRIVATE);
6680 OMP_CLAUSE_DECL (c) = decl;
6681 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6682 omp_add_variable (gimplify_omp_ctxp, decl,
6683 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6684 | GOVD_SEEN);
6685 c = NULL_TREE;
6688 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6689 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6690 else
6691 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6693 /* If DECL is not a gimple register, create a temporary variable to act
6694 as an iteration counter. This is valid, since DECL cannot be
6695 modified in the body of the loop. */
6696 if (orig_for_stmt != for_stmt)
6697 var = decl;
6698 else if (!is_gimple_reg (decl))
6700 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6701 TREE_OPERAND (t, 0) = var;
6703 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6705 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6707 else
6708 var = decl;
6710 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6711 is_gimple_val, fb_rvalue);
6712 ret = MIN (ret, tret);
6713 if (ret == GS_ERROR)
6714 return ret;
6716 /* Handle OMP_FOR_COND. */
6717 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6718 gcc_assert (COMPARISON_CLASS_P (t));
6719 gcc_assert (TREE_OPERAND (t, 0) == decl);
6721 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6722 is_gimple_val, fb_rvalue);
6723 ret = MIN (ret, tret);
6725 /* Handle OMP_FOR_INCR. */
6726 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6727 switch (TREE_CODE (t))
6729 case PREINCREMENT_EXPR:
6730 case POSTINCREMENT_EXPR:
6732 tree decl = TREE_OPERAND (t, 0);
6733 // c_omp_for_incr_canonicalize_ptr() should have been
6734 // called to massage things appropriately.
6735 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
6737 if (orig_for_stmt != for_stmt)
6738 break;
6739 t = build_int_cst (TREE_TYPE (decl), 1);
6740 if (c)
6741 OMP_CLAUSE_LINEAR_STEP (c) = t;
6742 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6743 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6744 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6745 break;
6748 case PREDECREMENT_EXPR:
6749 case POSTDECREMENT_EXPR:
6750 if (orig_for_stmt != for_stmt)
6751 break;
6752 t = build_int_cst (TREE_TYPE (decl), -1);
6753 if (c)
6754 OMP_CLAUSE_LINEAR_STEP (c) = t;
6755 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6756 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6757 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6758 break;
6760 case MODIFY_EXPR:
6761 gcc_assert (TREE_OPERAND (t, 0) == decl);
6762 TREE_OPERAND (t, 0) = var;
6764 t = TREE_OPERAND (t, 1);
6765 switch (TREE_CODE (t))
6767 case PLUS_EXPR:
6768 if (TREE_OPERAND (t, 1) == decl)
6770 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6771 TREE_OPERAND (t, 0) = var;
6772 break;
6775 /* Fallthru. */
6776 case MINUS_EXPR:
6777 case POINTER_PLUS_EXPR:
6778 gcc_assert (TREE_OPERAND (t, 0) == decl);
6779 TREE_OPERAND (t, 0) = var;
6780 break;
6781 default:
6782 gcc_unreachable ();
6785 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6786 is_gimple_val, fb_rvalue);
6787 ret = MIN (ret, tret);
6788 if (c)
6790 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
6791 if (TREE_CODE (t) == MINUS_EXPR)
6793 t = TREE_OPERAND (t, 1);
6794 OMP_CLAUSE_LINEAR_STEP (c)
6795 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
6796 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
6797 &for_pre_body, NULL,
6798 is_gimple_val, fb_rvalue);
6799 ret = MIN (ret, tret);
6802 break;
6804 default:
6805 gcc_unreachable ();
6808 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6809 && orig_for_stmt == for_stmt)
6811 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6812 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6813 && OMP_CLAUSE_DECL (c) == decl
6814 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6816 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6817 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6818 gcc_assert (TREE_OPERAND (t, 0) == var);
6819 t = TREE_OPERAND (t, 1);
6820 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6821 || TREE_CODE (t) == MINUS_EXPR
6822 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6823 gcc_assert (TREE_OPERAND (t, 0) == var);
6824 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6825 TREE_OPERAND (t, 1));
6826 gimplify_assign (decl, t,
6827 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6832 BITMAP_FREE (has_decl_expr);
6834 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
6836 if (orig_for_stmt != for_stmt)
6837 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6839 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6840 decl = TREE_OPERAND (t, 0);
6841 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6842 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6843 TREE_OPERAND (t, 0) = var;
6844 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6845 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
6846 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
6849 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
6851 int kind;
6852 switch (TREE_CODE (orig_for_stmt))
6854 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
6855 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
6856 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
6857 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
6858 default:
6859 gcc_unreachable ();
6861 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
6862 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6863 for_pre_body);
6864 if (orig_for_stmt != for_stmt)
6865 gimple_omp_for_set_combined_p (gfor, true);
6866 if (gimplify_omp_ctxp
6867 && (gimplify_omp_ctxp->combined_loop
6868 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
6869 && gimplify_omp_ctxp->outer_context
6870 && gimplify_omp_ctxp->outer_context->combined_loop)))
6872 gimple_omp_for_set_combined_into_p (gfor, true);
6873 if (gimplify_omp_ctxp->combined_loop)
6874 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
6875 else
6876 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
6879 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6881 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6882 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6883 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6884 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6885 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6886 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6887 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6888 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6891 gimplify_seq_add_stmt (pre_p, gfor);
6892 if (ret != GS_ALL_DONE)
6893 return GS_ERROR;
6894 *expr_p = NULL_TREE;
6895 return GS_ALL_DONE;
6898 /* Gimplify the gross structure of other OpenMP constructs.
6899 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
6900 and OMP_TEAMS. */
6902 static void
6903 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6905 tree expr = *expr_p;
6906 gimple stmt;
6907 gimple_seq body = NULL;
6908 enum omp_region_type ort = ORT_WORKSHARE;
6910 switch (TREE_CODE (expr))
6912 case OMP_SECTIONS:
6913 case OMP_SINGLE:
6914 break;
6915 case OMP_TARGET:
6916 ort = ORT_TARGET;
6917 break;
6918 case OMP_TARGET_DATA:
6919 ort = ORT_TARGET_DATA;
6920 break;
6921 case OMP_TEAMS:
6922 ort = ORT_TEAMS;
6923 break;
6924 default:
6925 gcc_unreachable ();
6927 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
6928 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
6930 push_gimplify_context ();
6931 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
6932 if (gimple_code (g) == GIMPLE_BIND)
6933 pop_gimplify_context (g);
6934 else
6935 pop_gimplify_context (NULL);
6936 if (ort == ORT_TARGET_DATA)
6938 gimple_seq cleanup = NULL;
6939 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
6940 g = gimple_build_call (fn, 0);
6941 gimple_seq_add_stmt (&cleanup, g);
6942 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
6943 body = NULL;
6944 gimple_seq_add_stmt (&body, g);
6947 else
6948 gimplify_and_add (OMP_BODY (expr), &body);
6949 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6951 switch (TREE_CODE (expr))
6953 case OMP_SECTIONS:
6954 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6955 break;
6956 case OMP_SINGLE:
6957 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6958 break;
6959 case OMP_TARGET:
6960 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
6961 OMP_CLAUSES (expr));
6962 break;
6963 case OMP_TARGET_DATA:
6964 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
6965 OMP_CLAUSES (expr));
6966 break;
6967 case OMP_TEAMS:
6968 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
6969 break;
6970 default:
6971 gcc_unreachable ();
6974 gimplify_seq_add_stmt (pre_p, stmt);
6975 *expr_p = NULL_TREE;
6978 /* Gimplify the gross structure of OpenMP target update construct. */
6980 static void
6981 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
6983 tree expr = *expr_p;
6984 gimple stmt;
6986 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
6987 ORT_WORKSHARE);
6988 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
6989 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
6990 OMP_TARGET_UPDATE_CLAUSES (expr));
6992 gimplify_seq_add_stmt (pre_p, stmt);
6993 *expr_p = NULL_TREE;
6996 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6997 stabilized the lhs of the atomic operation as *ADDR. Return true if
6998 EXPR is this stabilized form. */
7000 static bool
7001 goa_lhs_expr_p (tree expr, tree addr)
7003 /* Also include casts to other type variants. The C front end is fond
7004 of adding these for e.g. volatile variables. This is like
7005 STRIP_TYPE_NOPS but includes the main variant lookup. */
7006 STRIP_USELESS_TYPE_CONVERSION (expr);
7008 if (TREE_CODE (expr) == INDIRECT_REF)
7010 expr = TREE_OPERAND (expr, 0);
7011 while (expr != addr
7012 && (CONVERT_EXPR_P (expr)
7013 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7014 && TREE_CODE (expr) == TREE_CODE (addr)
7015 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7017 expr = TREE_OPERAND (expr, 0);
7018 addr = TREE_OPERAND (addr, 0);
7020 if (expr == addr)
7021 return true;
7022 return (TREE_CODE (addr) == ADDR_EXPR
7023 && TREE_CODE (expr) == ADDR_EXPR
7024 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7026 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7027 return true;
7028 return false;
7031 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7032 expression does not involve the lhs, evaluate it into a temporary.
7033 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7034 or -1 if an error was encountered. */
7036 static int
7037 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7038 tree lhs_var)
7040 tree expr = *expr_p;
7041 int saw_lhs;
7043 if (goa_lhs_expr_p (expr, lhs_addr))
7045 *expr_p = lhs_var;
7046 return 1;
7048 if (is_gimple_val (expr))
7049 return 0;
7051 saw_lhs = 0;
7052 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7054 case tcc_binary:
7055 case tcc_comparison:
7056 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7057 lhs_var);
7058 case tcc_unary:
7059 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7060 lhs_var);
7061 break;
7062 case tcc_expression:
7063 switch (TREE_CODE (expr))
7065 case TRUTH_ANDIF_EXPR:
7066 case TRUTH_ORIF_EXPR:
7067 case TRUTH_AND_EXPR:
7068 case TRUTH_OR_EXPR:
7069 case TRUTH_XOR_EXPR:
7070 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7071 lhs_addr, lhs_var);
7072 case TRUTH_NOT_EXPR:
7073 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7074 lhs_addr, lhs_var);
7075 break;
7076 case COMPOUND_EXPR:
7077 /* Break out any preevaluations from cp_build_modify_expr. */
7078 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7079 expr = TREE_OPERAND (expr, 1))
7080 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7081 *expr_p = expr;
7082 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7083 default:
7084 break;
7086 break;
7087 default:
7088 break;
7091 if (saw_lhs == 0)
7093 enum gimplify_status gs;
7094 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7095 if (gs != GS_ALL_DONE)
7096 saw_lhs = -1;
7099 return saw_lhs;
7102 /* Gimplify an OMP_ATOMIC statement. */
7104 static enum gimplify_status
7105 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7107 tree addr = TREE_OPERAND (*expr_p, 0);
7108 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7109 ? NULL : TREE_OPERAND (*expr_p, 1);
7110 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7111 tree tmp_load;
7112 gimple loadstmt, storestmt;
7114 tmp_load = create_tmp_reg (type, NULL);
7115 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7116 return GS_ERROR;
7118 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7119 != GS_ALL_DONE)
7120 return GS_ERROR;
7122 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7123 gimplify_seq_add_stmt (pre_p, loadstmt);
7124 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7125 != GS_ALL_DONE)
7126 return GS_ERROR;
7128 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7129 rhs = tmp_load;
7130 storestmt = gimple_build_omp_atomic_store (rhs);
7131 gimplify_seq_add_stmt (pre_p, storestmt);
7132 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7134 gimple_omp_atomic_set_seq_cst (loadstmt);
7135 gimple_omp_atomic_set_seq_cst (storestmt);
7137 switch (TREE_CODE (*expr_p))
7139 case OMP_ATOMIC_READ:
7140 case OMP_ATOMIC_CAPTURE_OLD:
7141 *expr_p = tmp_load;
7142 gimple_omp_atomic_set_need_value (loadstmt);
7143 break;
7144 case OMP_ATOMIC_CAPTURE_NEW:
7145 *expr_p = rhs;
7146 gimple_omp_atomic_set_need_value (storestmt);
7147 break;
7148 default:
7149 *expr_p = NULL;
7150 break;
7153 return GS_ALL_DONE;
7156 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7157 body, and adding some EH bits. */
7159 static enum gimplify_status
7160 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7162 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7163 gimple g;
7164 gimple_seq body = NULL;
7165 int subcode = 0;
7167 /* Wrap the transaction body in a BIND_EXPR so we have a context
7168 where to put decls for OpenMP. */
7169 if (TREE_CODE (tbody) != BIND_EXPR)
7171 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7172 TREE_SIDE_EFFECTS (bind) = 1;
7173 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7174 TRANSACTION_EXPR_BODY (expr) = bind;
7177 push_gimplify_context ();
7178 temp = voidify_wrapper_expr (*expr_p, NULL);
7180 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7181 pop_gimplify_context (g);
7183 g = gimple_build_transaction (body, NULL);
7184 if (TRANSACTION_EXPR_OUTER (expr))
7185 subcode = GTMA_IS_OUTER;
7186 else if (TRANSACTION_EXPR_RELAXED (expr))
7187 subcode = GTMA_IS_RELAXED;
7188 gimple_transaction_set_subcode (g, subcode);
7190 gimplify_seq_add_stmt (pre_p, g);
7192 if (temp)
7194 *expr_p = temp;
7195 return GS_OK;
7198 *expr_p = NULL_TREE;
7199 return GS_ALL_DONE;
7202 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7203 expression produces a value to be used as an operand inside a GIMPLE
7204 statement, the value will be stored back in *EXPR_P. This value will
7205 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7206 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7207 emitted in PRE_P and POST_P.
7209 Additionally, this process may overwrite parts of the input
7210 expression during gimplification. Ideally, it should be
7211 possible to do non-destructive gimplification.
7213 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7214 the expression needs to evaluate to a value to be used as
7215 an operand in a GIMPLE statement, this value will be stored in
7216 *EXPR_P on exit. This happens when the caller specifies one
7217 of fb_lvalue or fb_rvalue fallback flags.
7219 PRE_P will contain the sequence of GIMPLE statements corresponding
7220 to the evaluation of EXPR and all the side-effects that must
7221 be executed before the main expression. On exit, the last
7222 statement of PRE_P is the core statement being gimplified. For
7223 instance, when gimplifying 'if (++a)' the last statement in
7224 PRE_P will be 'if (t.1)' where t.1 is the result of
7225 pre-incrementing 'a'.
7227 POST_P will contain the sequence of GIMPLE statements corresponding
7228 to the evaluation of all the side-effects that must be executed
7229 after the main expression. If this is NULL, the post
7230 side-effects are stored at the end of PRE_P.
7232 The reason why the output is split in two is to handle post
7233 side-effects explicitly. In some cases, an expression may have
7234 inner and outer post side-effects which need to be emitted in
7235 an order different from the one given by the recursive
7236 traversal. For instance, for the expression (*p--)++ the post
7237 side-effects of '--' must actually occur *after* the post
7238 side-effects of '++'. However, gimplification will first visit
7239 the inner expression, so if a separate POST sequence was not
7240 used, the resulting sequence would be:
7242 1 t.1 = *p
7243 2 p = p - 1
7244 3 t.2 = t.1 + 1
7245 4 *p = t.2
7247 However, the post-decrement operation in line #2 must not be
7248 evaluated until after the store to *p at line #4, so the
7249 correct sequence should be:
7251 1 t.1 = *p
7252 2 t.2 = t.1 + 1
7253 3 *p = t.2
7254 4 p = p - 1
7256 So, by specifying a separate post queue, it is possible
7257 to emit the post side-effects in the correct order.
7258 If POST_P is NULL, an internal queue will be used. Before
7259 returning to the caller, the sequence POST_P is appended to
7260 the main output sequence PRE_P.
7262 GIMPLE_TEST_F points to a function that takes a tree T and
7263 returns nonzero if T is in the GIMPLE form requested by the
7264 caller. The GIMPLE predicates are in gimple.c.
7266 FALLBACK tells the function what sort of a temporary we want if
7267 gimplification cannot produce an expression that complies with
7268 GIMPLE_TEST_F.
7270 fb_none means that no temporary should be generated
7271 fb_rvalue means that an rvalue is OK to generate
7272 fb_lvalue means that an lvalue is OK to generate
7273 fb_either means that either is OK, but an lvalue is preferable.
7274 fb_mayfail means that gimplification may fail (in which case
7275 GS_ERROR will be returned)
7277 The return value is either GS_ERROR or GS_ALL_DONE, since this
7278 function iterates until EXPR is completely gimplified or an error
7279 occurs. */
7281 enum gimplify_status
7282 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7283 bool (*gimple_test_f) (tree), fallback_t fallback)
7285 tree tmp;
7286 gimple_seq internal_pre = NULL;
7287 gimple_seq internal_post = NULL;
7288 tree save_expr;
7289 bool is_statement;
7290 location_t saved_location;
7291 enum gimplify_status ret;
7292 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7294 save_expr = *expr_p;
7295 if (save_expr == NULL_TREE)
7296 return GS_ALL_DONE;
7298 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7299 is_statement = gimple_test_f == is_gimple_stmt;
7300 if (is_statement)
7301 gcc_assert (pre_p);
7303 /* Consistency checks. */
7304 if (gimple_test_f == is_gimple_reg)
7305 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7306 else if (gimple_test_f == is_gimple_val
7307 || gimple_test_f == is_gimple_call_addr
7308 || gimple_test_f == is_gimple_condexpr
7309 || gimple_test_f == is_gimple_mem_rhs
7310 || gimple_test_f == is_gimple_mem_rhs_or_call
7311 || gimple_test_f == is_gimple_reg_rhs
7312 || gimple_test_f == is_gimple_reg_rhs_or_call
7313 || gimple_test_f == is_gimple_asm_val
7314 || gimple_test_f == is_gimple_mem_ref_addr)
7315 gcc_assert (fallback & fb_rvalue);
7316 else if (gimple_test_f == is_gimple_min_lval
7317 || gimple_test_f == is_gimple_lvalue)
7318 gcc_assert (fallback & fb_lvalue);
7319 else if (gimple_test_f == is_gimple_addressable)
7320 gcc_assert (fallback & fb_either);
7321 else if (gimple_test_f == is_gimple_stmt)
7322 gcc_assert (fallback == fb_none);
7323 else
7325 /* We should have recognized the GIMPLE_TEST_F predicate to
7326 know what kind of fallback to use in case a temporary is
7327 needed to hold the value or address of *EXPR_P. */
7328 gcc_unreachable ();
7331 /* We used to check the predicate here and return immediately if it
7332 succeeds. This is wrong; the design is for gimplification to be
7333 idempotent, and for the predicates to only test for valid forms, not
7334 whether they are fully simplified. */
7335 if (pre_p == NULL)
7336 pre_p = &internal_pre;
7338 if (post_p == NULL)
7339 post_p = &internal_post;
7341 /* Remember the last statements added to PRE_P and POST_P. Every
7342 new statement added by the gimplification helpers needs to be
7343 annotated with location information. To centralize the
7344 responsibility, we remember the last statement that had been
7345 added to both queues before gimplifying *EXPR_P. If
7346 gimplification produces new statements in PRE_P and POST_P, those
7347 statements will be annotated with the same location information
7348 as *EXPR_P. */
7349 pre_last_gsi = gsi_last (*pre_p);
7350 post_last_gsi = gsi_last (*post_p);
7352 saved_location = input_location;
7353 if (save_expr != error_mark_node
7354 && EXPR_HAS_LOCATION (*expr_p))
7355 input_location = EXPR_LOCATION (*expr_p);
7357 /* Loop over the specific gimplifiers until the toplevel node
7358 remains the same. */
7361 /* Strip away as many useless type conversions as possible
7362 at the toplevel. */
7363 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7365 /* Remember the expr. */
7366 save_expr = *expr_p;
7368 /* Die, die, die, my darling. */
7369 if (save_expr == error_mark_node
7370 || (TREE_TYPE (save_expr)
7371 && TREE_TYPE (save_expr) == error_mark_node))
7373 ret = GS_ERROR;
7374 break;
7377 /* Do any language-specific gimplification. */
7378 ret = ((enum gimplify_status)
7379 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7380 if (ret == GS_OK)
7382 if (*expr_p == NULL_TREE)
7383 break;
7384 if (*expr_p != save_expr)
7385 continue;
7387 else if (ret != GS_UNHANDLED)
7388 break;
7390 /* Make sure that all the cases set 'ret' appropriately. */
7391 ret = GS_UNHANDLED;
7392 switch (TREE_CODE (*expr_p))
7394 /* First deal with the special cases. */
7396 case POSTINCREMENT_EXPR:
7397 case POSTDECREMENT_EXPR:
7398 case PREINCREMENT_EXPR:
7399 case PREDECREMENT_EXPR:
7400 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7401 fallback != fb_none,
7402 TREE_TYPE (*expr_p));
7403 break;
7405 case VIEW_CONVERT_EXPR:
7406 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
7407 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
7409 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7410 post_p, is_gimple_val, fb_rvalue);
7411 recalculate_side_effects (*expr_p);
7412 break;
7414 /* Fallthru. */
7416 case ARRAY_REF:
7417 case ARRAY_RANGE_REF:
7418 case REALPART_EXPR:
7419 case IMAGPART_EXPR:
7420 case COMPONENT_REF:
7421 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7422 fallback ? fallback : fb_rvalue);
7423 break;
7425 case COND_EXPR:
7426 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7428 /* C99 code may assign to an array in a structure value of a
7429 conditional expression, and this has undefined behavior
7430 only on execution, so create a temporary if an lvalue is
7431 required. */
7432 if (fallback == fb_lvalue)
7434 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7435 mark_addressable (*expr_p);
7436 ret = GS_OK;
7438 break;
7440 case CALL_EXPR:
7441 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7443 /* C99 code may assign to an array in a structure returned
7444 from a function, and this has undefined behavior only on
7445 execution, so create a temporary if an lvalue is
7446 required. */
7447 if (fallback == fb_lvalue)
7449 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7450 mark_addressable (*expr_p);
7451 ret = GS_OK;
7453 break;
7455 case TREE_LIST:
7456 gcc_unreachable ();
7458 case COMPOUND_EXPR:
7459 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7460 break;
7462 case COMPOUND_LITERAL_EXPR:
7463 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7464 gimple_test_f, fallback);
7465 break;
7467 case MODIFY_EXPR:
7468 case INIT_EXPR:
7469 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7470 fallback != fb_none);
7471 break;
7473 case TRUTH_ANDIF_EXPR:
7474 case TRUTH_ORIF_EXPR:
7476 /* Preserve the original type of the expression and the
7477 source location of the outer expression. */
7478 tree org_type = TREE_TYPE (*expr_p);
7479 *expr_p = gimple_boolify (*expr_p);
7480 *expr_p = build3_loc (input_location, COND_EXPR,
7481 org_type, *expr_p,
7482 fold_convert_loc
7483 (input_location,
7484 org_type, boolean_true_node),
7485 fold_convert_loc
7486 (input_location,
7487 org_type, boolean_false_node));
7488 ret = GS_OK;
7489 break;
7492 case TRUTH_NOT_EXPR:
7494 tree type = TREE_TYPE (*expr_p);
7495 /* The parsers are careful to generate TRUTH_NOT_EXPR
7496 only with operands that are always zero or one.
7497 We do not fold here but handle the only interesting case
7498 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7499 *expr_p = gimple_boolify (*expr_p);
7500 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7501 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7502 TREE_TYPE (*expr_p),
7503 TREE_OPERAND (*expr_p, 0));
7504 else
7505 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7506 TREE_TYPE (*expr_p),
7507 TREE_OPERAND (*expr_p, 0),
7508 build_int_cst (TREE_TYPE (*expr_p), 1));
7509 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7510 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7511 ret = GS_OK;
7512 break;
7515 case ADDR_EXPR:
7516 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7517 break;
7519 case ANNOTATE_EXPR:
7521 tree cond = TREE_OPERAND (*expr_p, 0);
7522 tree id = TREE_OPERAND (*expr_p, 1);
7523 tree type = TREE_TYPE (cond);
7524 if (!INTEGRAL_TYPE_P (type))
7526 *expr_p = cond;
7527 ret = GS_OK;
7528 break;
7530 tree tmp = create_tmp_var (type, NULL);
7531 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7532 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7533 cond, id);
7534 gimple_call_set_lhs (call, tmp);
7535 gimplify_seq_add_stmt (pre_p, call);
7536 *expr_p = tmp;
7537 ret = GS_ALL_DONE;
7538 break;
7541 case VA_ARG_EXPR:
7542 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7543 break;
7545 CASE_CONVERT:
7546 if (IS_EMPTY_STMT (*expr_p))
7548 ret = GS_ALL_DONE;
7549 break;
7552 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7553 || fallback == fb_none)
7555 /* Just strip a conversion to void (or in void context) and
7556 try again. */
7557 *expr_p = TREE_OPERAND (*expr_p, 0);
7558 ret = GS_OK;
7559 break;
7562 ret = gimplify_conversion (expr_p);
7563 if (ret == GS_ERROR)
7564 break;
7565 if (*expr_p != save_expr)
7566 break;
7567 /* FALLTHRU */
7569 case FIX_TRUNC_EXPR:
7570 /* unary_expr: ... | '(' cast ')' val | ... */
7571 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7572 is_gimple_val, fb_rvalue);
7573 recalculate_side_effects (*expr_p);
7574 break;
7576 case INDIRECT_REF:
7578 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7579 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7580 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7582 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7583 if (*expr_p != save_expr)
7585 ret = GS_OK;
7586 break;
7589 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7590 is_gimple_reg, fb_rvalue);
7591 if (ret == GS_ERROR)
7592 break;
7594 recalculate_side_effects (*expr_p);
7595 *expr_p = fold_build2_loc (input_location, MEM_REF,
7596 TREE_TYPE (*expr_p),
7597 TREE_OPERAND (*expr_p, 0),
7598 build_int_cst (saved_ptr_type, 0));
7599 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7600 TREE_THIS_NOTRAP (*expr_p) = notrap;
7601 ret = GS_OK;
7602 break;
7605 /* We arrive here through the various re-gimplifcation paths. */
7606 case MEM_REF:
7607 /* First try re-folding the whole thing. */
7608 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7609 TREE_OPERAND (*expr_p, 0),
7610 TREE_OPERAND (*expr_p, 1));
7611 if (tmp)
7613 *expr_p = tmp;
7614 recalculate_side_effects (*expr_p);
7615 ret = GS_OK;
7616 break;
7618 /* Avoid re-gimplifying the address operand if it is already
7619 in suitable form. Re-gimplifying would mark the address
7620 operand addressable. Always gimplify when not in SSA form
7621 as we still may have to gimplify decls with value-exprs. */
7622 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7623 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7625 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7626 is_gimple_mem_ref_addr, fb_rvalue);
7627 if (ret == GS_ERROR)
7628 break;
7630 recalculate_side_effects (*expr_p);
7631 ret = GS_ALL_DONE;
7632 break;
7634 /* Constants need not be gimplified. */
7635 case INTEGER_CST:
7636 case REAL_CST:
7637 case FIXED_CST:
7638 case STRING_CST:
7639 case COMPLEX_CST:
7640 case VECTOR_CST:
7641 /* Drop the overflow flag on constants, we do not want
7642 that in the GIMPLE IL. */
7643 if (TREE_OVERFLOW_P (*expr_p))
7644 *expr_p = drop_tree_overflow (*expr_p);
7645 ret = GS_ALL_DONE;
7646 break;
7648 case CONST_DECL:
7649 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7650 CONST_DECL node. Otherwise the decl is replaceable by its
7651 value. */
7652 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7653 if (fallback & fb_lvalue)
7654 ret = GS_ALL_DONE;
7655 else
7657 *expr_p = DECL_INITIAL (*expr_p);
7658 ret = GS_OK;
7660 break;
7662 case DECL_EXPR:
7663 ret = gimplify_decl_expr (expr_p, pre_p);
7664 break;
7666 case BIND_EXPR:
7667 ret = gimplify_bind_expr (expr_p, pre_p);
7668 break;
7670 case LOOP_EXPR:
7671 ret = gimplify_loop_expr (expr_p, pre_p);
7672 break;
7674 case SWITCH_EXPR:
7675 ret = gimplify_switch_expr (expr_p, pre_p);
7676 break;
7678 case EXIT_EXPR:
7679 ret = gimplify_exit_expr (expr_p);
7680 break;
7682 case GOTO_EXPR:
7683 /* If the target is not LABEL, then it is a computed jump
7684 and the target needs to be gimplified. */
7685 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7687 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7688 NULL, is_gimple_val, fb_rvalue);
7689 if (ret == GS_ERROR)
7690 break;
7692 gimplify_seq_add_stmt (pre_p,
7693 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7694 ret = GS_ALL_DONE;
7695 break;
7697 case PREDICT_EXPR:
7698 gimplify_seq_add_stmt (pre_p,
7699 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7700 PREDICT_EXPR_OUTCOME (*expr_p)));
7701 ret = GS_ALL_DONE;
7702 break;
7704 case LABEL_EXPR:
7705 ret = GS_ALL_DONE;
7706 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7707 == current_function_decl);
7708 gimplify_seq_add_stmt (pre_p,
7709 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7710 break;
7712 case CASE_LABEL_EXPR:
7713 ret = gimplify_case_label_expr (expr_p, pre_p);
7714 break;
7716 case RETURN_EXPR:
7717 ret = gimplify_return_expr (*expr_p, pre_p);
7718 break;
7720 case CONSTRUCTOR:
7721 /* Don't reduce this in place; let gimplify_init_constructor work its
7722 magic. Buf if we're just elaborating this for side effects, just
7723 gimplify any element that has side-effects. */
7724 if (fallback == fb_none)
7726 unsigned HOST_WIDE_INT ix;
7727 tree val;
7728 tree temp = NULL_TREE;
7729 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7730 if (TREE_SIDE_EFFECTS (val))
7731 append_to_statement_list (val, &temp);
7733 *expr_p = temp;
7734 ret = temp ? GS_OK : GS_ALL_DONE;
7736 /* C99 code may assign to an array in a constructed
7737 structure or union, and this has undefined behavior only
7738 on execution, so create a temporary if an lvalue is
7739 required. */
7740 else if (fallback == fb_lvalue)
7742 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7743 mark_addressable (*expr_p);
7744 ret = GS_OK;
7746 else
7747 ret = GS_ALL_DONE;
7748 break;
7750 /* The following are special cases that are not handled by the
7751 original GIMPLE grammar. */
7753 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7754 eliminated. */
7755 case SAVE_EXPR:
7756 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7757 break;
7759 case BIT_FIELD_REF:
7760 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7761 post_p, is_gimple_lvalue, fb_either);
7762 recalculate_side_effects (*expr_p);
7763 break;
7765 case TARGET_MEM_REF:
7767 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7769 if (TMR_BASE (*expr_p))
7770 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7771 post_p, is_gimple_mem_ref_addr, fb_either);
7772 if (TMR_INDEX (*expr_p))
7773 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7774 post_p, is_gimple_val, fb_rvalue);
7775 if (TMR_INDEX2 (*expr_p))
7776 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7777 post_p, is_gimple_val, fb_rvalue);
7778 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7779 ret = MIN (r0, r1);
7781 break;
7783 case NON_LVALUE_EXPR:
7784 /* This should have been stripped above. */
7785 gcc_unreachable ();
7787 case ASM_EXPR:
7788 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7789 break;
7791 case TRY_FINALLY_EXPR:
7792 case TRY_CATCH_EXPR:
7794 gimple_seq eval, cleanup;
7795 gimple try_;
7797 /* Calls to destructors are generated automatically in FINALLY/CATCH
7798 block. They should have location as UNKNOWN_LOCATION. However,
7799 gimplify_call_expr will reset these call stmts to input_location
7800 if it finds stmt's location is unknown. To prevent resetting for
7801 destructors, we set the input_location to unknown.
7802 Note that this only affects the destructor calls in FINALLY/CATCH
7803 block, and will automatically reset to its original value by the
7804 end of gimplify_expr. */
7805 input_location = UNKNOWN_LOCATION;
7806 eval = cleanup = NULL;
7807 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7808 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7809 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7810 if (gimple_seq_empty_p (cleanup))
7812 gimple_seq_add_seq (pre_p, eval);
7813 ret = GS_ALL_DONE;
7814 break;
7816 try_ = gimple_build_try (eval, cleanup,
7817 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7818 ? GIMPLE_TRY_FINALLY
7819 : GIMPLE_TRY_CATCH);
7820 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7821 gimple_set_location (try_, saved_location);
7822 else
7823 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7824 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7825 gimple_try_set_catch_is_cleanup (try_,
7826 TRY_CATCH_IS_CLEANUP (*expr_p));
7827 gimplify_seq_add_stmt (pre_p, try_);
7828 ret = GS_ALL_DONE;
7829 break;
7832 case CLEANUP_POINT_EXPR:
7833 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7834 break;
7836 case TARGET_EXPR:
7837 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7838 break;
7840 case CATCH_EXPR:
7842 gimple c;
7843 gimple_seq handler = NULL;
7844 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7845 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7846 gimplify_seq_add_stmt (pre_p, c);
7847 ret = GS_ALL_DONE;
7848 break;
7851 case EH_FILTER_EXPR:
7853 gimple ehf;
7854 gimple_seq failure = NULL;
7856 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7857 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7858 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7859 gimplify_seq_add_stmt (pre_p, ehf);
7860 ret = GS_ALL_DONE;
7861 break;
7864 case OBJ_TYPE_REF:
7866 enum gimplify_status r0, r1;
7867 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7868 post_p, is_gimple_val, fb_rvalue);
7869 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7870 post_p, is_gimple_val, fb_rvalue);
7871 TREE_SIDE_EFFECTS (*expr_p) = 0;
7872 ret = MIN (r0, r1);
7874 break;
7876 case LABEL_DECL:
7877 /* We get here when taking the address of a label. We mark
7878 the label as "forced"; meaning it can never be removed and
7879 it is a potential target for any computed goto. */
7880 FORCED_LABEL (*expr_p) = 1;
7881 ret = GS_ALL_DONE;
7882 break;
7884 case STATEMENT_LIST:
7885 ret = gimplify_statement_list (expr_p, pre_p);
7886 break;
7888 case WITH_SIZE_EXPR:
7890 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7891 post_p == &internal_post ? NULL : post_p,
7892 gimple_test_f, fallback);
7893 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7894 is_gimple_val, fb_rvalue);
7895 ret = GS_ALL_DONE;
7897 break;
7899 case VAR_DECL:
7900 case PARM_DECL:
7901 ret = gimplify_var_or_parm_decl (expr_p);
7902 break;
7904 case RESULT_DECL:
7905 /* When within an OpenMP context, notice uses of variables. */
7906 if (gimplify_omp_ctxp)
7907 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7908 ret = GS_ALL_DONE;
7909 break;
7911 case SSA_NAME:
7912 /* Allow callbacks into the gimplifier during optimization. */
7913 ret = GS_ALL_DONE;
7914 break;
7916 case OMP_PARALLEL:
7917 gimplify_omp_parallel (expr_p, pre_p);
7918 ret = GS_ALL_DONE;
7919 break;
7921 case OMP_TASK:
7922 gimplify_omp_task (expr_p, pre_p);
7923 ret = GS_ALL_DONE;
7924 break;
7926 case OMP_FOR:
7927 case OMP_SIMD:
7928 case CILK_SIMD:
7929 case OMP_DISTRIBUTE:
7930 ret = gimplify_omp_for (expr_p, pre_p);
7931 break;
7933 case OMP_SECTIONS:
7934 case OMP_SINGLE:
7935 case OMP_TARGET:
7936 case OMP_TARGET_DATA:
7937 case OMP_TEAMS:
7938 gimplify_omp_workshare (expr_p, pre_p);
7939 ret = GS_ALL_DONE;
7940 break;
7942 case OMP_TARGET_UPDATE:
7943 gimplify_omp_target_update (expr_p, pre_p);
7944 ret = GS_ALL_DONE;
7945 break;
7947 case OMP_SECTION:
7948 case OMP_MASTER:
7949 case OMP_TASKGROUP:
7950 case OMP_ORDERED:
7951 case OMP_CRITICAL:
7953 gimple_seq body = NULL;
7954 gimple g;
7956 gimplify_and_add (OMP_BODY (*expr_p), &body);
7957 switch (TREE_CODE (*expr_p))
7959 case OMP_SECTION:
7960 g = gimple_build_omp_section (body);
7961 break;
7962 case OMP_MASTER:
7963 g = gimple_build_omp_master (body);
7964 break;
7965 case OMP_TASKGROUP:
7967 gimple_seq cleanup = NULL;
7968 tree fn
7969 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
7970 g = gimple_build_call (fn, 0);
7971 gimple_seq_add_stmt (&cleanup, g);
7972 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7973 body = NULL;
7974 gimple_seq_add_stmt (&body, g);
7975 g = gimple_build_omp_taskgroup (body);
7977 break;
7978 case OMP_ORDERED:
7979 g = gimple_build_omp_ordered (body);
7980 break;
7981 case OMP_CRITICAL:
7982 g = gimple_build_omp_critical (body,
7983 OMP_CRITICAL_NAME (*expr_p));
7984 break;
7985 default:
7986 gcc_unreachable ();
7988 gimplify_seq_add_stmt (pre_p, g);
7989 ret = GS_ALL_DONE;
7990 break;
7993 case OMP_ATOMIC:
7994 case OMP_ATOMIC_READ:
7995 case OMP_ATOMIC_CAPTURE_OLD:
7996 case OMP_ATOMIC_CAPTURE_NEW:
7997 ret = gimplify_omp_atomic (expr_p, pre_p);
7998 break;
8000 case TRANSACTION_EXPR:
8001 ret = gimplify_transaction (expr_p, pre_p);
8002 break;
8004 case TRUTH_AND_EXPR:
8005 case TRUTH_OR_EXPR:
8006 case TRUTH_XOR_EXPR:
8008 tree orig_type = TREE_TYPE (*expr_p);
8009 tree new_type, xop0, xop1;
8010 *expr_p = gimple_boolify (*expr_p);
8011 new_type = TREE_TYPE (*expr_p);
8012 if (!useless_type_conversion_p (orig_type, new_type))
8014 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8015 ret = GS_OK;
8016 break;
8019 /* Boolified binary truth expressions are semantically equivalent
8020 to bitwise binary expressions. Canonicalize them to the
8021 bitwise variant. */
8022 switch (TREE_CODE (*expr_p))
8024 case TRUTH_AND_EXPR:
8025 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8026 break;
8027 case TRUTH_OR_EXPR:
8028 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8029 break;
8030 case TRUTH_XOR_EXPR:
8031 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8032 break;
8033 default:
8034 break;
8036 /* Now make sure that operands have compatible type to
8037 expression's new_type. */
8038 xop0 = TREE_OPERAND (*expr_p, 0);
8039 xop1 = TREE_OPERAND (*expr_p, 1);
8040 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8041 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8042 new_type,
8043 xop0);
8044 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8045 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8046 new_type,
8047 xop1);
8048 /* Continue classified as tcc_binary. */
8049 goto expr_2;
8052 case FMA_EXPR:
8053 case VEC_COND_EXPR:
8054 case VEC_PERM_EXPR:
8055 /* Classified as tcc_expression. */
8056 goto expr_3;
8058 case POINTER_PLUS_EXPR:
8060 enum gimplify_status r0, r1;
8061 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8062 post_p, is_gimple_val, fb_rvalue);
8063 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8064 post_p, is_gimple_val, fb_rvalue);
8065 recalculate_side_effects (*expr_p);
8066 ret = MIN (r0, r1);
8067 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8068 after gimplifying operands - this is similar to how
8069 it would be folding all gimplified stmts on creation
8070 to have them canonicalized, which is what we eventually
8071 should do anyway. */
8072 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8073 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8075 *expr_p = build_fold_addr_expr_with_type_loc
8076 (input_location,
8077 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8078 TREE_OPERAND (*expr_p, 0),
8079 fold_convert (ptr_type_node,
8080 TREE_OPERAND (*expr_p, 1))),
8081 TREE_TYPE (*expr_p));
8082 ret = MIN (ret, GS_OK);
8084 break;
8087 case CILK_SYNC_STMT:
8089 if (!fn_contains_cilk_spawn_p (cfun))
8091 error_at (EXPR_LOCATION (*expr_p),
8092 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8093 ret = GS_ERROR;
8095 else
8097 gimplify_cilk_sync (expr_p, pre_p);
8098 ret = GS_ALL_DONE;
8100 break;
8103 default:
8104 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8106 case tcc_comparison:
8107 /* Handle comparison of objects of non scalar mode aggregates
8108 with a call to memcmp. It would be nice to only have to do
8109 this for variable-sized objects, but then we'd have to allow
8110 the same nest of reference nodes we allow for MODIFY_EXPR and
8111 that's too complex.
8113 Compare scalar mode aggregates as scalar mode values. Using
8114 memcmp for them would be very inefficient at best, and is
8115 plain wrong if bitfields are involved. */
8117 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8119 /* Vector comparisons need no boolification. */
8120 if (TREE_CODE (type) == VECTOR_TYPE)
8121 goto expr_2;
8122 else if (!AGGREGATE_TYPE_P (type))
8124 tree org_type = TREE_TYPE (*expr_p);
8125 *expr_p = gimple_boolify (*expr_p);
8126 if (!useless_type_conversion_p (org_type,
8127 TREE_TYPE (*expr_p)))
8129 *expr_p = fold_convert_loc (input_location,
8130 org_type, *expr_p);
8131 ret = GS_OK;
8133 else
8134 goto expr_2;
8136 else if (TYPE_MODE (type) != BLKmode)
8137 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8138 else
8139 ret = gimplify_variable_sized_compare (expr_p);
8141 break;
8144 /* If *EXPR_P does not need to be special-cased, handle it
8145 according to its class. */
8146 case tcc_unary:
8147 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8148 post_p, is_gimple_val, fb_rvalue);
8149 break;
8151 case tcc_binary:
8152 expr_2:
8154 enum gimplify_status r0, r1;
8156 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8157 post_p, is_gimple_val, fb_rvalue);
8158 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8159 post_p, is_gimple_val, fb_rvalue);
8161 ret = MIN (r0, r1);
8162 break;
8165 expr_3:
8167 enum gimplify_status r0, r1, r2;
8169 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8170 post_p, is_gimple_val, fb_rvalue);
8171 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8172 post_p, is_gimple_val, fb_rvalue);
8173 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8174 post_p, is_gimple_val, fb_rvalue);
8176 ret = MIN (MIN (r0, r1), r2);
8177 break;
8180 case tcc_declaration:
8181 case tcc_constant:
8182 ret = GS_ALL_DONE;
8183 goto dont_recalculate;
8185 default:
8186 gcc_unreachable ();
8189 recalculate_side_effects (*expr_p);
8191 dont_recalculate:
8192 break;
8195 gcc_assert (*expr_p || ret != GS_OK);
8197 while (ret == GS_OK);
8199 /* If we encountered an error_mark somewhere nested inside, either
8200 stub out the statement or propagate the error back out. */
8201 if (ret == GS_ERROR)
8203 if (is_statement)
8204 *expr_p = NULL;
8205 goto out;
8208 /* This was only valid as a return value from the langhook, which
8209 we handled. Make sure it doesn't escape from any other context. */
8210 gcc_assert (ret != GS_UNHANDLED);
8212 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8214 /* We aren't looking for a value, and we don't have a valid
8215 statement. If it doesn't have side-effects, throw it away. */
8216 if (!TREE_SIDE_EFFECTS (*expr_p))
8217 *expr_p = NULL;
8218 else if (!TREE_THIS_VOLATILE (*expr_p))
8220 /* This is probably a _REF that contains something nested that
8221 has side effects. Recurse through the operands to find it. */
8222 enum tree_code code = TREE_CODE (*expr_p);
8224 switch (code)
8226 case COMPONENT_REF:
8227 case REALPART_EXPR:
8228 case IMAGPART_EXPR:
8229 case VIEW_CONVERT_EXPR:
8230 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8231 gimple_test_f, fallback);
8232 break;
8234 case ARRAY_REF:
8235 case ARRAY_RANGE_REF:
8236 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8237 gimple_test_f, fallback);
8238 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8239 gimple_test_f, fallback);
8240 break;
8242 default:
8243 /* Anything else with side-effects must be converted to
8244 a valid statement before we get here. */
8245 gcc_unreachable ();
8248 *expr_p = NULL;
8250 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8251 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8253 /* Historically, the compiler has treated a bare reference
8254 to a non-BLKmode volatile lvalue as forcing a load. */
8255 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8257 /* Normally, we do not want to create a temporary for a
8258 TREE_ADDRESSABLE type because such a type should not be
8259 copied by bitwise-assignment. However, we make an
8260 exception here, as all we are doing here is ensuring that
8261 we read the bytes that make up the type. We use
8262 create_tmp_var_raw because create_tmp_var will abort when
8263 given a TREE_ADDRESSABLE type. */
8264 tree tmp = create_tmp_var_raw (type, "vol");
8265 gimple_add_tmp_var (tmp);
8266 gimplify_assign (tmp, *expr_p, pre_p);
8267 *expr_p = NULL;
8269 else
8270 /* We can't do anything useful with a volatile reference to
8271 an incomplete type, so just throw it away. Likewise for
8272 a BLKmode type, since any implicit inner load should
8273 already have been turned into an explicit one by the
8274 gimplification process. */
8275 *expr_p = NULL;
8278 /* If we are gimplifying at the statement level, we're done. Tack
8279 everything together and return. */
8280 if (fallback == fb_none || is_statement)
8282 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8283 it out for GC to reclaim it. */
8284 *expr_p = NULL_TREE;
8286 if (!gimple_seq_empty_p (internal_pre)
8287 || !gimple_seq_empty_p (internal_post))
8289 gimplify_seq_add_seq (&internal_pre, internal_post);
8290 gimplify_seq_add_seq (pre_p, internal_pre);
8293 /* The result of gimplifying *EXPR_P is going to be the last few
8294 statements in *PRE_P and *POST_P. Add location information
8295 to all the statements that were added by the gimplification
8296 helpers. */
8297 if (!gimple_seq_empty_p (*pre_p))
8298 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8300 if (!gimple_seq_empty_p (*post_p))
8301 annotate_all_with_location_after (*post_p, post_last_gsi,
8302 input_location);
8304 goto out;
8307 #ifdef ENABLE_GIMPLE_CHECKING
8308 if (*expr_p)
8310 enum tree_code code = TREE_CODE (*expr_p);
8311 /* These expressions should already be in gimple IR form. */
8312 gcc_assert (code != MODIFY_EXPR
8313 && code != ASM_EXPR
8314 && code != BIND_EXPR
8315 && code != CATCH_EXPR
8316 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8317 && code != EH_FILTER_EXPR
8318 && code != GOTO_EXPR
8319 && code != LABEL_EXPR
8320 && code != LOOP_EXPR
8321 && code != SWITCH_EXPR
8322 && code != TRY_FINALLY_EXPR
8323 && code != OMP_CRITICAL
8324 && code != OMP_FOR
8325 && code != OMP_MASTER
8326 && code != OMP_TASKGROUP
8327 && code != OMP_ORDERED
8328 && code != OMP_PARALLEL
8329 && code != OMP_SECTIONS
8330 && code != OMP_SECTION
8331 && code != OMP_SINGLE);
8333 #endif
8335 /* Otherwise we're gimplifying a subexpression, so the resulting
8336 value is interesting. If it's a valid operand that matches
8337 GIMPLE_TEST_F, we're done. Unless we are handling some
8338 post-effects internally; if that's the case, we need to copy into
8339 a temporary before adding the post-effects to POST_P. */
8340 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8341 goto out;
8343 /* Otherwise, we need to create a new temporary for the gimplified
8344 expression. */
8346 /* We can't return an lvalue if we have an internal postqueue. The
8347 object the lvalue refers to would (probably) be modified by the
8348 postqueue; we need to copy the value out first, which means an
8349 rvalue. */
8350 if ((fallback & fb_lvalue)
8351 && gimple_seq_empty_p (internal_post)
8352 && is_gimple_addressable (*expr_p))
8354 /* An lvalue will do. Take the address of the expression, store it
8355 in a temporary, and replace the expression with an INDIRECT_REF of
8356 that temporary. */
8357 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8358 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8359 *expr_p = build_simple_mem_ref (tmp);
8361 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8363 /* An rvalue will do. Assign the gimplified expression into a
8364 new temporary TMP and replace the original expression with
8365 TMP. First, make sure that the expression has a type so that
8366 it can be assigned into a temporary. */
8367 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8368 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8370 else
8372 #ifdef ENABLE_GIMPLE_CHECKING
8373 if (!(fallback & fb_mayfail))
8375 fprintf (stderr, "gimplification failed:\n");
8376 print_generic_expr (stderr, *expr_p, 0);
8377 debug_tree (*expr_p);
8378 internal_error ("gimplification failed");
8380 #endif
8381 gcc_assert (fallback & fb_mayfail);
8383 /* If this is an asm statement, and the user asked for the
8384 impossible, don't die. Fail and let gimplify_asm_expr
8385 issue an error. */
8386 ret = GS_ERROR;
8387 goto out;
8390 /* Make sure the temporary matches our predicate. */
8391 gcc_assert ((*gimple_test_f) (*expr_p));
8393 if (!gimple_seq_empty_p (internal_post))
8395 annotate_all_with_location (internal_post, input_location);
8396 gimplify_seq_add_seq (pre_p, internal_post);
8399 out:
8400 input_location = saved_location;
8401 return ret;
8404 /* Look through TYPE for variable-sized objects and gimplify each such
8405 size that we find. Add to LIST_P any statements generated. */
8407 void
8408 gimplify_type_sizes (tree type, gimple_seq *list_p)
8410 tree field, t;
8412 if (type == NULL || type == error_mark_node)
8413 return;
8415 /* We first do the main variant, then copy into any other variants. */
8416 type = TYPE_MAIN_VARIANT (type);
8418 /* Avoid infinite recursion. */
8419 if (TYPE_SIZES_GIMPLIFIED (type))
8420 return;
8422 TYPE_SIZES_GIMPLIFIED (type) = 1;
8424 switch (TREE_CODE (type))
8426 case INTEGER_TYPE:
8427 case ENUMERAL_TYPE:
8428 case BOOLEAN_TYPE:
8429 case REAL_TYPE:
8430 case FIXED_POINT_TYPE:
8431 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8432 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8434 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8436 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8437 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8439 break;
8441 case ARRAY_TYPE:
8442 /* These types may not have declarations, so handle them here. */
8443 gimplify_type_sizes (TREE_TYPE (type), list_p);
8444 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8445 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8446 with assigned stack slots, for -O1+ -g they should be tracked
8447 by VTA. */
8448 if (!(TYPE_NAME (type)
8449 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8450 && DECL_IGNORED_P (TYPE_NAME (type)))
8451 && TYPE_DOMAIN (type)
8452 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8454 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8455 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8456 DECL_IGNORED_P (t) = 0;
8457 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8458 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8459 DECL_IGNORED_P (t) = 0;
8461 break;
8463 case RECORD_TYPE:
8464 case UNION_TYPE:
8465 case QUAL_UNION_TYPE:
8466 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8467 if (TREE_CODE (field) == FIELD_DECL)
8469 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8470 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8471 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8472 gimplify_type_sizes (TREE_TYPE (field), list_p);
8474 break;
8476 case POINTER_TYPE:
8477 case REFERENCE_TYPE:
8478 /* We used to recurse on the pointed-to type here, which turned out to
8479 be incorrect because its definition might refer to variables not
8480 yet initialized at this point if a forward declaration is involved.
8482 It was actually useful for anonymous pointed-to types to ensure
8483 that the sizes evaluation dominates every possible later use of the
8484 values. Restricting to such types here would be safe since there
8485 is no possible forward declaration around, but would introduce an
8486 undesirable middle-end semantic to anonymity. We then defer to
8487 front-ends the responsibility of ensuring that the sizes are
8488 evaluated both early and late enough, e.g. by attaching artificial
8489 type declarations to the tree. */
8490 break;
8492 default:
8493 break;
8496 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8497 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8499 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8501 TYPE_SIZE (t) = TYPE_SIZE (type);
8502 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8503 TYPE_SIZES_GIMPLIFIED (t) = 1;
8507 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8508 a size or position, has had all of its SAVE_EXPRs evaluated.
8509 We add any required statements to *STMT_P. */
8511 void
8512 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8514 tree expr = *expr_p;
8516 /* We don't do anything if the value isn't there, is constant, or contains
8517 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8518 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8519 will want to replace it with a new variable, but that will cause problems
8520 if this type is from outside the function. It's OK to have that here. */
8521 if (is_gimple_sizepos (expr))
8522 return;
8524 *expr_p = unshare_expr (expr);
8526 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8529 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8530 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8531 is true, also gimplify the parameters. */
8533 gimple
8534 gimplify_body (tree fndecl, bool do_parms)
8536 location_t saved_location = input_location;
8537 gimple_seq parm_stmts, seq;
8538 gimple outer_bind;
8539 struct cgraph_node *cgn;
8541 timevar_push (TV_TREE_GIMPLIFY);
8543 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8544 gimplification. */
8545 default_rtl_profile ();
8547 gcc_assert (gimplify_ctxp == NULL);
8548 push_gimplify_context ();
8550 if (flag_openmp)
8552 gcc_assert (gimplify_omp_ctxp == NULL);
8553 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8554 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8557 /* Unshare most shared trees in the body and in that of any nested functions.
8558 It would seem we don't have to do this for nested functions because
8559 they are supposed to be output and then the outer function gimplified
8560 first, but the g++ front end doesn't always do it that way. */
8561 unshare_body (fndecl);
8562 unvisit_body (fndecl);
8564 cgn = cgraph_get_node (fndecl);
8565 if (cgn && cgn->origin)
8566 nonlocal_vlas = pointer_set_create ();
8568 /* Make sure input_location isn't set to something weird. */
8569 input_location = DECL_SOURCE_LOCATION (fndecl);
8571 /* Resolve callee-copies. This has to be done before processing
8572 the body so that DECL_VALUE_EXPR gets processed correctly. */
8573 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8575 /* Gimplify the function's body. */
8576 seq = NULL;
8577 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8578 outer_bind = gimple_seq_first_stmt (seq);
8579 if (!outer_bind)
8581 outer_bind = gimple_build_nop ();
8582 gimplify_seq_add_stmt (&seq, outer_bind);
8585 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8586 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8587 if (gimple_code (outer_bind) == GIMPLE_BIND
8588 && gimple_seq_first (seq) == gimple_seq_last (seq))
8590 else
8591 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8593 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8595 /* If we had callee-copies statements, insert them at the beginning
8596 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8597 if (!gimple_seq_empty_p (parm_stmts))
8599 tree parm;
8601 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8602 gimple_bind_set_body (outer_bind, parm_stmts);
8604 for (parm = DECL_ARGUMENTS (current_function_decl);
8605 parm; parm = DECL_CHAIN (parm))
8606 if (DECL_HAS_VALUE_EXPR_P (parm))
8608 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8609 DECL_IGNORED_P (parm) = 0;
8613 if (nonlocal_vlas)
8615 if (nonlocal_vla_vars)
8617 /* tree-nested.c may later on call declare_vars (..., true);
8618 which relies on BLOCK_VARS chain to be the tail of the
8619 gimple_bind_vars chain. Ensure we don't violate that
8620 assumption. */
8621 if (gimple_bind_block (outer_bind)
8622 == DECL_INITIAL (current_function_decl))
8623 declare_vars (nonlocal_vla_vars, outer_bind, true);
8624 else
8625 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8626 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8627 nonlocal_vla_vars);
8628 nonlocal_vla_vars = NULL_TREE;
8630 pointer_set_destroy (nonlocal_vlas);
8631 nonlocal_vlas = NULL;
8634 if ((flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp)
8636 delete_omp_context (gimplify_omp_ctxp);
8637 gimplify_omp_ctxp = NULL;
8640 pop_gimplify_context (outer_bind);
8641 gcc_assert (gimplify_ctxp == NULL);
8643 #ifdef ENABLE_CHECKING
8644 if (!seen_error ())
8645 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8646 #endif
8648 timevar_pop (TV_TREE_GIMPLIFY);
8649 input_location = saved_location;
8651 return outer_bind;
8654 typedef char *char_p; /* For DEF_VEC_P. */
8656 /* Return whether we should exclude FNDECL from instrumentation. */
8658 static bool
8659 flag_instrument_functions_exclude_p (tree fndecl)
8661 vec<char_p> *v;
8663 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8664 if (v && v->length () > 0)
8666 const char *name;
8667 int i;
8668 char *s;
8670 name = lang_hooks.decl_printable_name (fndecl, 0);
8671 FOR_EACH_VEC_ELT (*v, i, s)
8672 if (strstr (name, s) != NULL)
8673 return true;
8676 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8677 if (v && v->length () > 0)
8679 const char *name;
8680 int i;
8681 char *s;
8683 name = DECL_SOURCE_FILE (fndecl);
8684 FOR_EACH_VEC_ELT (*v, i, s)
8685 if (strstr (name, s) != NULL)
8686 return true;
8689 return false;
8692 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8693 node for the function we want to gimplify.
8695 Return the sequence of GIMPLE statements corresponding to the body
8696 of FNDECL. */
8698 void
8699 gimplify_function_tree (tree fndecl)
8701 tree parm, ret;
8702 gimple_seq seq;
8703 gimple bind;
8705 gcc_assert (!gimple_body (fndecl));
8707 if (DECL_STRUCT_FUNCTION (fndecl))
8708 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8709 else
8710 push_struct_function (fndecl);
8712 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8714 /* Preliminarily mark non-addressed complex variables as eligible
8715 for promotion to gimple registers. We'll transform their uses
8716 as we find them. */
8717 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8718 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8719 && !TREE_THIS_VOLATILE (parm)
8720 && !needs_to_live_in_memory (parm))
8721 DECL_GIMPLE_REG_P (parm) = 1;
8724 ret = DECL_RESULT (fndecl);
8725 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8726 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8727 && !needs_to_live_in_memory (ret))
8728 DECL_GIMPLE_REG_P (ret) = 1;
8730 bind = gimplify_body (fndecl, true);
8732 /* The tree body of the function is no longer needed, replace it
8733 with the new GIMPLE body. */
8734 seq = NULL;
8735 gimple_seq_add_stmt (&seq, bind);
8736 gimple_set_body (fndecl, seq);
8738 /* If we're instrumenting function entry/exit, then prepend the call to
8739 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8740 catch the exit hook. */
8741 /* ??? Add some way to ignore exceptions for this TFE. */
8742 if (flag_instrument_function_entry_exit
8743 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8744 && !flag_instrument_functions_exclude_p (fndecl))
8746 tree x;
8747 gimple new_bind;
8748 gimple tf;
8749 gimple_seq cleanup = NULL, body = NULL;
8750 tree tmp_var;
8751 gimple call;
8753 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8754 call = gimple_build_call (x, 1, integer_zero_node);
8755 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8756 gimple_call_set_lhs (call, tmp_var);
8757 gimplify_seq_add_stmt (&cleanup, call);
8758 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8759 call = gimple_build_call (x, 2,
8760 build_fold_addr_expr (current_function_decl),
8761 tmp_var);
8762 gimplify_seq_add_stmt (&cleanup, call);
8763 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8765 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8766 call = gimple_build_call (x, 1, integer_zero_node);
8767 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8768 gimple_call_set_lhs (call, tmp_var);
8769 gimplify_seq_add_stmt (&body, call);
8770 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8771 call = gimple_build_call (x, 2,
8772 build_fold_addr_expr (current_function_decl),
8773 tmp_var);
8774 gimplify_seq_add_stmt (&body, call);
8775 gimplify_seq_add_stmt (&body, tf);
8776 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8777 /* Clear the block for BIND, since it is no longer directly inside
8778 the function, but within a try block. */
8779 gimple_bind_set_block (bind, NULL);
8781 /* Replace the current function body with the body
8782 wrapped in the try/finally TF. */
8783 seq = NULL;
8784 gimple_seq_add_stmt (&seq, new_bind);
8785 gimple_set_body (fndecl, seq);
8788 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8789 cfun->curr_properties = PROP_gimple_any;
8791 pop_cfun ();
8794 /* Return a dummy expression of type TYPE in order to keep going after an
8795 error. */
8797 static tree
8798 dummy_object (tree type)
8800 tree t = build_int_cst (build_pointer_type (type), 0);
8801 return build2 (MEM_REF, type, t, t);
8804 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
8805 builtin function, but a very special sort of operator. */
8807 enum gimplify_status
8808 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
8810 tree promoted_type, have_va_type;
8811 tree valist = TREE_OPERAND (*expr_p, 0);
8812 tree type = TREE_TYPE (*expr_p);
8813 tree t;
8814 location_t loc = EXPR_LOCATION (*expr_p);
8816 /* Verify that valist is of the proper type. */
8817 have_va_type = TREE_TYPE (valist);
8818 if (have_va_type == error_mark_node)
8819 return GS_ERROR;
8820 have_va_type = targetm.canonical_va_list_type (have_va_type);
8822 if (have_va_type == NULL_TREE)
8824 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
8825 return GS_ERROR;
8828 /* Generate a diagnostic for requesting data of a type that cannot
8829 be passed through `...' due to type promotion at the call site. */
8830 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
8831 != type)
8833 static bool gave_help;
8834 bool warned;
8836 /* Unfortunately, this is merely undefined, rather than a constraint
8837 violation, so we cannot make this an error. If this call is never
8838 executed, the program is still strictly conforming. */
8839 warned = warning_at (loc, 0,
8840 "%qT is promoted to %qT when passed through %<...%>",
8841 type, promoted_type);
8842 if (!gave_help && warned)
8844 gave_help = true;
8845 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
8846 promoted_type, type);
8849 /* We can, however, treat "undefined" any way we please.
8850 Call abort to encourage the user to fix the program. */
8851 if (warned)
8852 inform (loc, "if this code is reached, the program will abort");
8853 /* Before the abort, allow the evaluation of the va_list
8854 expression to exit or longjmp. */
8855 gimplify_and_add (valist, pre_p);
8856 t = build_call_expr_loc (loc,
8857 builtin_decl_implicit (BUILT_IN_TRAP), 0);
8858 gimplify_and_add (t, pre_p);
8860 /* This is dead code, but go ahead and finish so that the
8861 mode of the result comes out right. */
8862 *expr_p = dummy_object (type);
8863 return GS_ALL_DONE;
8865 else
8867 /* Make it easier for the backends by protecting the valist argument
8868 from multiple evaluations. */
8869 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
8871 /* For this case, the backends will be expecting a pointer to
8872 TREE_TYPE (abi), but it's possible we've
8873 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
8874 So fix it. */
8875 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
8877 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
8878 valist = fold_convert_loc (loc, p1,
8879 build_fold_addr_expr_loc (loc, valist));
8882 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
8884 else
8885 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
8887 if (!targetm.gimplify_va_arg_expr)
8888 /* FIXME: Once most targets are converted we should merely
8889 assert this is non-null. */
8890 return GS_ALL_DONE;
8892 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
8893 return GS_OK;
8897 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
8899 DST/SRC are the destination and source respectively. You can pass
8900 ungimplified trees in DST or SRC, in which case they will be
8901 converted to a gimple operand if necessary.
8903 This function returns the newly created GIMPLE_ASSIGN tuple. */
8905 gimple
8906 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
8908 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
8909 gimplify_and_add (t, seq_p);
8910 ggc_free (t);
8911 return gimple_seq_last_stmt (*seq_p);
8914 inline hashval_t
8915 gimplify_hasher::hash (const value_type *p)
8917 tree t = p->val;
8918 return iterative_hash_expr (t, 0);
8921 inline bool
8922 gimplify_hasher::equal (const value_type *p1, const compare_type *p2)
8924 tree t1 = p1->val;
8925 tree t2 = p2->val;
8926 enum tree_code code = TREE_CODE (t1);
8928 if (TREE_CODE (t2) != code
8929 || TREE_TYPE (t1) != TREE_TYPE (t2))
8930 return false;
8932 if (!operand_equal_p (t1, t2, 0))
8933 return false;
8935 #ifdef ENABLE_CHECKING
8936 /* Only allow them to compare equal if they also hash equal; otherwise
8937 results are nondeterminate, and we fail bootstrap comparison. */
8938 gcc_assert (hash (p1) == hash (p2));
8939 #endif
8941 return true;