* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / gimplify.c
blob74b2dcd25d2053883892a6bb6bc2d041487c6df4
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2015 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 "alias.h"
27 #include "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "calls.h"
41 #include "emit-rtl.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "predict.h"
46 #include "basic-block.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "gimple.h"
53 #include "gimplify.h"
54 #include "gimple-iterator.h"
55 #include "stringpool.h"
56 #include "stor-layout.h"
57 #include "print-tree.h"
58 #include "tree-iterator.h"
59 #include "tree-inline.h"
60 #include "tree-pretty-print.h"
61 #include "langhooks.h"
62 #include "bitmap.h"
63 #include "gimple-ssa.h"
64 #include "cgraph.h"
65 #include "tree-cfg.h"
66 #include "tree-ssanames.h"
67 #include "tree-ssa.h"
68 #include "diagnostic-core.h"
69 #include "target.h"
70 #include "splay-tree.h"
71 #include "omp-low.h"
72 #include "gimple-low.h"
73 #include "cilk.h"
74 #include "gomp-constants.h"
75 #include "tree-dump.h"
77 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
78 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
79 #include "builtins.h"
81 enum gimplify_omp_var_data
83 GOVD_SEEN = 1,
84 GOVD_EXPLICIT = 2,
85 GOVD_SHARED = 4,
86 GOVD_PRIVATE = 8,
87 GOVD_FIRSTPRIVATE = 16,
88 GOVD_LASTPRIVATE = 32,
89 GOVD_REDUCTION = 64,
90 GOVD_LOCAL = 128,
91 GOVD_MAP = 256,
92 GOVD_DEBUG_PRIVATE = 512,
93 GOVD_PRIVATE_OUTER_REF = 1024,
94 GOVD_LINEAR = 2048,
95 GOVD_ALIGNED = 4096,
97 /* Flag for GOVD_MAP: don't copy back. */
98 GOVD_MAP_TO_ONLY = 8192,
100 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
101 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
103 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
104 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
105 | GOVD_LOCAL)
109 enum omp_region_type
111 ORT_WORKSHARE = 0,
112 ORT_SIMD = 1,
113 ORT_PARALLEL = 2,
114 ORT_COMBINED_PARALLEL = 3,
115 ORT_TASK = 4,
116 ORT_UNTIED_TASK = 5,
117 ORT_TEAMS = 8,
118 ORT_COMBINED_TEAMS = 9,
119 /* Data region. */
120 ORT_TARGET_DATA = 16,
121 /* Data region with offloading. */
122 ORT_TARGET = 32
125 /* Gimplify hashtable helper. */
127 struct gimplify_hasher : free_ptr_hash <elt_t>
129 static inline hashval_t hash (const elt_t *);
130 static inline bool equal (const elt_t *, const elt_t *);
133 struct gimplify_ctx
135 struct gimplify_ctx *prev_context;
137 vec<gbind *> bind_expr_stack;
138 tree temps;
139 gimple_seq conditional_cleanups;
140 tree exit_label;
141 tree return_temp;
143 vec<tree> case_labels;
144 /* The formal temporary table. Should this be persistent? */
145 hash_table<gimplify_hasher> *temp_htab;
147 int conditions;
148 bool save_stack;
149 bool into_ssa;
150 bool allow_rhs_cond_expr;
151 bool in_cleanup_point_expr;
154 struct gimplify_omp_ctx
156 struct gimplify_omp_ctx *outer_context;
157 splay_tree variables;
158 hash_set<tree> *privatized_types;
159 location_t location;
160 enum omp_clause_default_kind default_kind;
161 enum omp_region_type region_type;
162 bool combined_loop;
163 bool distribute;
166 static struct gimplify_ctx *gimplify_ctxp;
167 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
169 /* Forward declaration. */
170 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
172 /* Shorter alias name for the above function for use in gimplify.c
173 only. */
175 static inline void
176 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
178 gimple_seq_add_stmt_without_update (seq_p, gs);
181 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
182 NULL, a new sequence is allocated. This function is
183 similar to gimple_seq_add_seq, but does not scan the operands.
184 During gimplification, we need to manipulate statement sequences
185 before the def/use vectors have been constructed. */
187 static void
188 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
190 gimple_stmt_iterator si;
192 if (src == NULL)
193 return;
195 si = gsi_last (*dst_p);
196 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
200 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
201 and popping gimplify contexts. */
203 static struct gimplify_ctx *ctx_pool = NULL;
205 /* Return a gimplify context struct from the pool. */
207 static inline struct gimplify_ctx *
208 ctx_alloc (void)
210 struct gimplify_ctx * c = ctx_pool;
212 if (c)
213 ctx_pool = c->prev_context;
214 else
215 c = XNEW (struct gimplify_ctx);
217 memset (c, '\0', sizeof (*c));
218 return c;
221 /* Put gimplify context C back into the pool. */
223 static inline void
224 ctx_free (struct gimplify_ctx *c)
226 c->prev_context = ctx_pool;
227 ctx_pool = c;
230 /* Free allocated ctx stack memory. */
232 void
233 free_gimplify_stack (void)
235 struct gimplify_ctx *c;
237 while ((c = ctx_pool))
239 ctx_pool = c->prev_context;
240 free (c);
245 /* Set up a context for the gimplifier. */
247 void
248 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
250 struct gimplify_ctx *c = ctx_alloc ();
252 c->prev_context = gimplify_ctxp;
253 gimplify_ctxp = c;
254 gimplify_ctxp->into_ssa = in_ssa;
255 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
258 /* Tear down a context for the gimplifier. If BODY is non-null, then
259 put the temporaries into the outer BIND_EXPR. Otherwise, put them
260 in the local_decls.
262 BODY is not a sequence, but the first tuple in a sequence. */
264 void
265 pop_gimplify_context (gimple body)
267 struct gimplify_ctx *c = gimplify_ctxp;
269 gcc_assert (c
270 && (!c->bind_expr_stack.exists ()
271 || c->bind_expr_stack.is_empty ()));
272 c->bind_expr_stack.release ();
273 gimplify_ctxp = c->prev_context;
275 if (body)
276 declare_vars (c->temps, body, false);
277 else
278 record_vars (c->temps);
280 delete c->temp_htab;
281 c->temp_htab = NULL;
282 ctx_free (c);
285 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
287 static void
288 gimple_push_bind_expr (gbind *bind_stmt)
290 gimplify_ctxp->bind_expr_stack.reserve (8);
291 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
294 /* Pop the first element off the stack of bindings. */
296 static void
297 gimple_pop_bind_expr (void)
299 gimplify_ctxp->bind_expr_stack.pop ();
302 /* Return the first element of the stack of bindings. */
304 gbind *
305 gimple_current_bind_expr (void)
307 return gimplify_ctxp->bind_expr_stack.last ();
310 /* Return the stack of bindings created during gimplification. */
312 vec<gbind *>
313 gimple_bind_expr_stack (void)
315 return gimplify_ctxp->bind_expr_stack;
318 /* Return true iff there is a COND_EXPR between us and the innermost
319 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
321 static bool
322 gimple_conditional_context (void)
324 return gimplify_ctxp->conditions > 0;
327 /* Note that we've entered a COND_EXPR. */
329 static void
330 gimple_push_condition (void)
332 #ifdef ENABLE_GIMPLE_CHECKING
333 if (gimplify_ctxp->conditions == 0)
334 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
335 #endif
336 ++(gimplify_ctxp->conditions);
339 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
340 now, add any conditional cleanups we've seen to the prequeue. */
342 static void
343 gimple_pop_condition (gimple_seq *pre_p)
345 int conds = --(gimplify_ctxp->conditions);
347 gcc_assert (conds >= 0);
348 if (conds == 0)
350 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
351 gimplify_ctxp->conditional_cleanups = NULL;
355 /* A stable comparison routine for use with splay trees and DECLs. */
357 static int
358 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
360 tree a = (tree) xa;
361 tree b = (tree) xb;
363 return DECL_UID (a) - DECL_UID (b);
366 /* Create a new omp construct that deals with variable remapping. */
368 static struct gimplify_omp_ctx *
369 new_omp_context (enum omp_region_type region_type)
371 struct gimplify_omp_ctx *c;
373 c = XCNEW (struct gimplify_omp_ctx);
374 c->outer_context = gimplify_omp_ctxp;
375 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
376 c->privatized_types = new hash_set<tree>;
377 c->location = input_location;
378 c->region_type = region_type;
379 if ((region_type & ORT_TASK) == 0)
380 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
381 else
382 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
384 return c;
387 /* Destroy an omp construct that deals with variable remapping. */
389 static void
390 delete_omp_context (struct gimplify_omp_ctx *c)
392 splay_tree_delete (c->variables);
393 delete c->privatized_types;
394 XDELETE (c);
397 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
398 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
400 /* Both gimplify the statement T and append it to *SEQ_P. This function
401 behaves exactly as gimplify_stmt, but you don't have to pass T as a
402 reference. */
404 void
405 gimplify_and_add (tree t, gimple_seq *seq_p)
407 gimplify_stmt (&t, seq_p);
410 /* Gimplify statement T into sequence *SEQ_P, and return the first
411 tuple in the sequence of generated tuples for this statement.
412 Return NULL if gimplifying T produced no tuples. */
414 static gimple
415 gimplify_and_return_first (tree t, gimple_seq *seq_p)
417 gimple_stmt_iterator last = gsi_last (*seq_p);
419 gimplify_and_add (t, seq_p);
421 if (!gsi_end_p (last))
423 gsi_next (&last);
424 return gsi_stmt (last);
426 else
427 return gimple_seq_first_stmt (*seq_p);
430 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
431 LHS, or for a call argument. */
433 static bool
434 is_gimple_mem_rhs (tree t)
436 /* If we're dealing with a renamable type, either source or dest must be
437 a renamed variable. */
438 if (is_gimple_reg_type (TREE_TYPE (t)))
439 return is_gimple_val (t);
440 else
441 return is_gimple_val (t) || is_gimple_lvalue (t);
444 /* Return true if T is a CALL_EXPR or an expression that can be
445 assigned to a temporary. Note that this predicate should only be
446 used during gimplification. See the rationale for this in
447 gimplify_modify_expr. */
449 static bool
450 is_gimple_reg_rhs_or_call (tree t)
452 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
453 || TREE_CODE (t) == CALL_EXPR);
456 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
457 this predicate should only be used during gimplification. See the
458 rationale for this in gimplify_modify_expr. */
460 static bool
461 is_gimple_mem_rhs_or_call (tree t)
463 /* If we're dealing with a renamable type, either source or dest must be
464 a renamed variable. */
465 if (is_gimple_reg_type (TREE_TYPE (t)))
466 return is_gimple_val (t);
467 else
468 return (is_gimple_val (t) || is_gimple_lvalue (t)
469 || TREE_CODE (t) == CALL_EXPR);
472 /* Create a temporary with a name derived from VAL. Subroutine of
473 lookup_tmp_var; nobody else should call this function. */
475 static inline tree
476 create_tmp_from_val (tree val)
478 /* Drop all qualifiers and address-space information from the value type. */
479 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
480 tree var = create_tmp_var (type, get_name (val));
481 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
482 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
483 DECL_GIMPLE_REG_P (var) = 1;
484 return var;
487 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
488 an existing expression temporary. */
490 static tree
491 lookup_tmp_var (tree val, bool is_formal)
493 tree ret;
495 /* If not optimizing, never really reuse a temporary. local-alloc
496 won't allocate any variable that is used in more than one basic
497 block, which means it will go into memory, causing much extra
498 work in reload and final and poorer code generation, outweighing
499 the extra memory allocation here. */
500 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
501 ret = create_tmp_from_val (val);
502 else
504 elt_t elt, *elt_p;
505 elt_t **slot;
507 elt.val = val;
508 if (!gimplify_ctxp->temp_htab)
509 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
510 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
511 if (*slot == NULL)
513 elt_p = XNEW (elt_t);
514 elt_p->val = val;
515 elt_p->temp = ret = create_tmp_from_val (val);
516 *slot = elt_p;
518 else
520 elt_p = *slot;
521 ret = elt_p->temp;
525 return ret;
528 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
530 static tree
531 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
532 bool is_formal)
534 tree t, mod;
536 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
537 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
538 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
539 fb_rvalue);
541 if (gimplify_ctxp->into_ssa
542 && is_gimple_reg_type (TREE_TYPE (val)))
543 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
544 else
545 t = lookup_tmp_var (val, is_formal);
547 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
549 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
551 /* gimplify_modify_expr might want to reduce this further. */
552 gimplify_and_add (mod, pre_p);
553 ggc_free (mod);
555 return t;
558 /* Return a formal temporary variable initialized with VAL. PRE_P is as
559 in gimplify_expr. Only use this function if:
561 1) The value of the unfactored expression represented by VAL will not
562 change between the initialization and use of the temporary, and
563 2) The temporary will not be otherwise modified.
565 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
566 and #2 means it is inappropriate for && temps.
568 For other cases, use get_initialized_tmp_var instead. */
570 tree
571 get_formal_tmp_var (tree val, gimple_seq *pre_p)
573 return internal_get_tmp_var (val, pre_p, NULL, true);
576 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
577 are as in gimplify_expr. */
579 tree
580 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
582 return internal_get_tmp_var (val, pre_p, post_p, false);
585 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
586 generate debug info for them; otherwise don't. */
588 void
589 declare_vars (tree vars, gimple gs, bool debug_info)
591 tree last = vars;
592 if (last)
594 tree temps, block;
596 gbind *scope = as_a <gbind *> (gs);
598 temps = nreverse (last);
600 block = gimple_bind_block (scope);
601 gcc_assert (!block || TREE_CODE (block) == BLOCK);
602 if (!block || !debug_info)
604 DECL_CHAIN (last) = gimple_bind_vars (scope);
605 gimple_bind_set_vars (scope, temps);
607 else
609 /* We need to attach the nodes both to the BIND_EXPR and to its
610 associated BLOCK for debugging purposes. The key point here
611 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
612 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
613 if (BLOCK_VARS (block))
614 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
615 else
617 gimple_bind_set_vars (scope,
618 chainon (gimple_bind_vars (scope), temps));
619 BLOCK_VARS (block) = temps;
625 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
626 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
627 no such upper bound can be obtained. */
629 static void
630 force_constant_size (tree var)
632 /* The only attempt we make is by querying the maximum size of objects
633 of the variable's type. */
635 HOST_WIDE_INT max_size;
637 gcc_assert (TREE_CODE (var) == VAR_DECL);
639 max_size = max_int_size_in_bytes (TREE_TYPE (var));
641 gcc_assert (max_size >= 0);
643 DECL_SIZE_UNIT (var)
644 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
645 DECL_SIZE (var)
646 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
649 /* Push the temporary variable TMP into the current binding. */
651 void
652 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
654 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
656 /* Later processing assumes that the object size is constant, which might
657 not be true at this point. Force the use of a constant upper bound in
658 this case. */
659 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
660 force_constant_size (tmp);
662 DECL_CONTEXT (tmp) = fn->decl;
663 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
665 record_vars_into (tmp, fn->decl);
668 /* Push the temporary variable TMP into the current binding. */
670 void
671 gimple_add_tmp_var (tree tmp)
673 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
675 /* Later processing assumes that the object size is constant, which might
676 not be true at this point. Force the use of a constant upper bound in
677 this case. */
678 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
679 force_constant_size (tmp);
681 DECL_CONTEXT (tmp) = current_function_decl;
682 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
684 if (gimplify_ctxp)
686 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
687 gimplify_ctxp->temps = tmp;
689 /* Mark temporaries local within the nearest enclosing parallel. */
690 if (gimplify_omp_ctxp)
692 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
693 while (ctx
694 && (ctx->region_type == ORT_WORKSHARE
695 || ctx->region_type == ORT_SIMD))
696 ctx = ctx->outer_context;
697 if (ctx)
698 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
701 else if (cfun)
702 record_vars (tmp);
703 else
705 gimple_seq body_seq;
707 /* This case is for nested functions. We need to expose the locals
708 they create. */
709 body_seq = gimple_body (current_function_decl);
710 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
716 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
717 nodes that are referenced more than once in GENERIC functions. This is
718 necessary because gimplification (translation into GIMPLE) is performed
719 by modifying tree nodes in-place, so gimplication of a shared node in a
720 first context could generate an invalid GIMPLE form in a second context.
722 This is achieved with a simple mark/copy/unmark algorithm that walks the
723 GENERIC representation top-down, marks nodes with TREE_VISITED the first
724 time it encounters them, duplicates them if they already have TREE_VISITED
725 set, and finally removes the TREE_VISITED marks it has set.
727 The algorithm works only at the function level, i.e. it generates a GENERIC
728 representation of a function with no nodes shared within the function when
729 passed a GENERIC function (except for nodes that are allowed to be shared).
731 At the global level, it is also necessary to unshare tree nodes that are
732 referenced in more than one function, for the same aforementioned reason.
733 This requires some cooperation from the front-end. There are 2 strategies:
735 1. Manual unsharing. The front-end needs to call unshare_expr on every
736 expression that might end up being shared across functions.
738 2. Deep unsharing. This is an extension of regular unsharing. Instead
739 of calling unshare_expr on expressions that might be shared across
740 functions, the front-end pre-marks them with TREE_VISITED. This will
741 ensure that they are unshared on the first reference within functions
742 when the regular unsharing algorithm runs. The counterpart is that
743 this algorithm must look deeper than for manual unsharing, which is
744 specified by LANG_HOOKS_DEEP_UNSHARING.
746 If there are only few specific cases of node sharing across functions, it is
747 probably easier for a front-end to unshare the expressions manually. On the
748 contrary, if the expressions generated at the global level are as widespread
749 as expressions generated within functions, deep unsharing is very likely the
750 way to go. */
752 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
753 These nodes model computations that must be done once. If we were to
754 unshare something like SAVE_EXPR(i++), the gimplification process would
755 create wrong code. However, if DATA is non-null, it must hold a pointer
756 set that is used to unshare the subtrees of these nodes. */
758 static tree
759 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
761 tree t = *tp;
762 enum tree_code code = TREE_CODE (t);
764 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
765 copy their subtrees if we can make sure to do it only once. */
766 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
768 if (data && !((hash_set<tree> *)data)->add (t))
770 else
771 *walk_subtrees = 0;
774 /* Stop at types, decls, constants like copy_tree_r. */
775 else if (TREE_CODE_CLASS (code) == tcc_type
776 || TREE_CODE_CLASS (code) == tcc_declaration
777 || TREE_CODE_CLASS (code) == tcc_constant
778 /* We can't do anything sensible with a BLOCK used as an
779 expression, but we also can't just die when we see it
780 because of non-expression uses. So we avert our eyes
781 and cross our fingers. Silly Java. */
782 || code == BLOCK)
783 *walk_subtrees = 0;
785 /* Cope with the statement expression extension. */
786 else if (code == STATEMENT_LIST)
789 /* Leave the bulk of the work to copy_tree_r itself. */
790 else
791 copy_tree_r (tp, walk_subtrees, NULL);
793 return NULL_TREE;
796 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
797 If *TP has been visited already, then *TP is deeply copied by calling
798 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
800 static tree
801 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
803 tree t = *tp;
804 enum tree_code code = TREE_CODE (t);
806 /* Skip types, decls, and constants. But we do want to look at their
807 types and the bounds of types. Mark them as visited so we properly
808 unmark their subtrees on the unmark pass. If we've already seen them,
809 don't look down further. */
810 if (TREE_CODE_CLASS (code) == tcc_type
811 || TREE_CODE_CLASS (code) == tcc_declaration
812 || TREE_CODE_CLASS (code) == tcc_constant)
814 if (TREE_VISITED (t))
815 *walk_subtrees = 0;
816 else
817 TREE_VISITED (t) = 1;
820 /* If this node has been visited already, unshare it and don't look
821 any deeper. */
822 else if (TREE_VISITED (t))
824 walk_tree (tp, mostly_copy_tree_r, data, NULL);
825 *walk_subtrees = 0;
828 /* Otherwise, mark the node as visited and keep looking. */
829 else
830 TREE_VISITED (t) = 1;
832 return NULL_TREE;
835 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
836 copy_if_shared_r callback unmodified. */
838 static inline void
839 copy_if_shared (tree *tp, void *data)
841 walk_tree (tp, copy_if_shared_r, data, NULL);
844 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
845 any nested functions. */
847 static void
848 unshare_body (tree fndecl)
850 struct cgraph_node *cgn = cgraph_node::get (fndecl);
851 /* If the language requires deep unsharing, we need a pointer set to make
852 sure we don't repeatedly unshare subtrees of unshareable nodes. */
853 hash_set<tree> *visited
854 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
856 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
857 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
858 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
860 delete visited;
862 if (cgn)
863 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
864 unshare_body (cgn->decl);
867 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
868 Subtrees are walked until the first unvisited node is encountered. */
870 static tree
871 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
873 tree t = *tp;
875 /* If this node has been visited, unmark it and keep looking. */
876 if (TREE_VISITED (t))
877 TREE_VISITED (t) = 0;
879 /* Otherwise, don't look any deeper. */
880 else
881 *walk_subtrees = 0;
883 return NULL_TREE;
886 /* Unmark the visited trees rooted at *TP. */
888 static inline void
889 unmark_visited (tree *tp)
891 walk_tree (tp, unmark_visited_r, NULL, NULL);
894 /* Likewise, but mark all trees as not visited. */
896 static void
897 unvisit_body (tree fndecl)
899 struct cgraph_node *cgn = cgraph_node::get (fndecl);
901 unmark_visited (&DECL_SAVED_TREE (fndecl));
902 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
903 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
905 if (cgn)
906 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
907 unvisit_body (cgn->decl);
910 /* Unconditionally make an unshared copy of EXPR. This is used when using
911 stored expressions which span multiple functions, such as BINFO_VTABLE,
912 as the normal unsharing process can't tell that they're shared. */
914 tree
915 unshare_expr (tree expr)
917 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
918 return expr;
921 /* Worker for unshare_expr_without_location. */
923 static tree
924 prune_expr_location (tree *tp, int *walk_subtrees, void *)
926 if (EXPR_P (*tp))
927 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
928 else
929 *walk_subtrees = 0;
930 return NULL_TREE;
933 /* Similar to unshare_expr but also prune all expression locations
934 from EXPR. */
936 tree
937 unshare_expr_without_location (tree expr)
939 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
940 if (EXPR_P (expr))
941 walk_tree (&expr, prune_expr_location, NULL, NULL);
942 return expr;
945 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
946 contain statements and have a value. Assign its value to a temporary
947 and give it void_type_node. Return the temporary, or NULL_TREE if
948 WRAPPER was already void. */
950 tree
951 voidify_wrapper_expr (tree wrapper, tree temp)
953 tree type = TREE_TYPE (wrapper);
954 if (type && !VOID_TYPE_P (type))
956 tree *p;
958 /* Set p to point to the body of the wrapper. Loop until we find
959 something that isn't a wrapper. */
960 for (p = &wrapper; p && *p; )
962 switch (TREE_CODE (*p))
964 case BIND_EXPR:
965 TREE_SIDE_EFFECTS (*p) = 1;
966 TREE_TYPE (*p) = void_type_node;
967 /* For a BIND_EXPR, the body is operand 1. */
968 p = &BIND_EXPR_BODY (*p);
969 break;
971 case CLEANUP_POINT_EXPR:
972 case TRY_FINALLY_EXPR:
973 case TRY_CATCH_EXPR:
974 TREE_SIDE_EFFECTS (*p) = 1;
975 TREE_TYPE (*p) = void_type_node;
976 p = &TREE_OPERAND (*p, 0);
977 break;
979 case STATEMENT_LIST:
981 tree_stmt_iterator i = tsi_last (*p);
982 TREE_SIDE_EFFECTS (*p) = 1;
983 TREE_TYPE (*p) = void_type_node;
984 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
986 break;
988 case COMPOUND_EXPR:
989 /* Advance to the last statement. Set all container types to
990 void. */
991 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
993 TREE_SIDE_EFFECTS (*p) = 1;
994 TREE_TYPE (*p) = void_type_node;
996 break;
998 case TRANSACTION_EXPR:
999 TREE_SIDE_EFFECTS (*p) = 1;
1000 TREE_TYPE (*p) = void_type_node;
1001 p = &TRANSACTION_EXPR_BODY (*p);
1002 break;
1004 default:
1005 /* Assume that any tree upon which voidify_wrapper_expr is
1006 directly called is a wrapper, and that its body is op0. */
1007 if (p == &wrapper)
1009 TREE_SIDE_EFFECTS (*p) = 1;
1010 TREE_TYPE (*p) = void_type_node;
1011 p = &TREE_OPERAND (*p, 0);
1012 break;
1014 goto out;
1018 out:
1019 if (p == NULL || IS_EMPTY_STMT (*p))
1020 temp = NULL_TREE;
1021 else if (temp)
1023 /* The wrapper is on the RHS of an assignment that we're pushing
1024 down. */
1025 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1026 || TREE_CODE (temp) == MODIFY_EXPR);
1027 TREE_OPERAND (temp, 1) = *p;
1028 *p = temp;
1030 else
1032 temp = create_tmp_var (type, "retval");
1033 *p = build2 (INIT_EXPR, type, temp, *p);
1036 return temp;
1039 return NULL_TREE;
1042 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1043 a temporary through which they communicate. */
1045 static void
1046 build_stack_save_restore (gcall **save, gcall **restore)
1048 tree tmp_var;
1050 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1051 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1052 gimple_call_set_lhs (*save, tmp_var);
1054 *restore
1055 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1056 1, tmp_var);
1059 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1061 static enum gimplify_status
1062 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1064 tree bind_expr = *expr_p;
1065 bool old_save_stack = gimplify_ctxp->save_stack;
1066 tree t;
1067 gbind *bind_stmt;
1068 gimple_seq body, cleanup;
1069 gcall *stack_save;
1070 location_t start_locus = 0, end_locus = 0;
1072 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1074 /* Mark variables seen in this bind expr. */
1075 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1077 if (TREE_CODE (t) == VAR_DECL)
1079 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1081 /* Mark variable as local. */
1082 if (ctx && !DECL_EXTERNAL (t)
1083 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1084 || splay_tree_lookup (ctx->variables,
1085 (splay_tree_key) t) == NULL))
1087 if (ctx->region_type == ORT_SIMD
1088 && TREE_ADDRESSABLE (t)
1089 && !TREE_STATIC (t))
1090 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1091 else
1092 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1095 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1097 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1098 cfun->has_local_explicit_reg_vars = true;
1101 /* Preliminarily mark non-addressed complex variables as eligible
1102 for promotion to gimple registers. We'll transform their uses
1103 as we find them. */
1104 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1105 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1106 && !TREE_THIS_VOLATILE (t)
1107 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1108 && !needs_to_live_in_memory (t))
1109 DECL_GIMPLE_REG_P (t) = 1;
1112 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1113 BIND_EXPR_BLOCK (bind_expr));
1114 gimple_push_bind_expr (bind_stmt);
1116 gimplify_ctxp->save_stack = false;
1118 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1119 body = NULL;
1120 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1121 gimple_bind_set_body (bind_stmt, body);
1123 /* Source location wise, the cleanup code (stack_restore and clobbers)
1124 belongs to the end of the block, so propagate what we have. The
1125 stack_save operation belongs to the beginning of block, which we can
1126 infer from the bind_expr directly if the block has no explicit
1127 assignment. */
1128 if (BIND_EXPR_BLOCK (bind_expr))
1130 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1131 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1133 if (start_locus == 0)
1134 start_locus = EXPR_LOCATION (bind_expr);
1136 cleanup = NULL;
1137 stack_save = NULL;
1138 if (gimplify_ctxp->save_stack)
1140 gcall *stack_restore;
1142 /* Save stack on entry and restore it on exit. Add a try_finally
1143 block to achieve this. */
1144 build_stack_save_restore (&stack_save, &stack_restore);
1146 gimple_set_location (stack_save, start_locus);
1147 gimple_set_location (stack_restore, end_locus);
1149 gimplify_seq_add_stmt (&cleanup, stack_restore);
1152 /* Add clobbers for all variables that go out of scope. */
1153 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1155 if (TREE_CODE (t) == VAR_DECL
1156 && !is_global_var (t)
1157 && DECL_CONTEXT (t) == current_function_decl
1158 && !DECL_HARD_REGISTER (t)
1159 && !TREE_THIS_VOLATILE (t)
1160 && !DECL_HAS_VALUE_EXPR_P (t)
1161 /* Only care for variables that have to be in memory. Others
1162 will be rewritten into SSA names, hence moved to the top-level. */
1163 && !is_gimple_reg (t)
1164 && flag_stack_reuse != SR_NONE)
1166 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1167 gimple clobber_stmt;
1168 TREE_THIS_VOLATILE (clobber) = 1;
1169 clobber_stmt = gimple_build_assign (t, clobber);
1170 gimple_set_location (clobber_stmt, end_locus);
1171 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1175 if (cleanup)
1177 gtry *gs;
1178 gimple_seq new_body;
1180 new_body = NULL;
1181 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1182 GIMPLE_TRY_FINALLY);
1184 if (stack_save)
1185 gimplify_seq_add_stmt (&new_body, stack_save);
1186 gimplify_seq_add_stmt (&new_body, gs);
1187 gimple_bind_set_body (bind_stmt, new_body);
1190 gimplify_ctxp->save_stack = old_save_stack;
1191 gimple_pop_bind_expr ();
1193 gimplify_seq_add_stmt (pre_p, bind_stmt);
1195 if (temp)
1197 *expr_p = temp;
1198 return GS_OK;
1201 *expr_p = NULL_TREE;
1202 return GS_ALL_DONE;
1205 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1206 GIMPLE value, it is assigned to a new temporary and the statement is
1207 re-written to return the temporary.
1209 PRE_P points to the sequence where side effects that must happen before
1210 STMT should be stored. */
1212 static enum gimplify_status
1213 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1215 greturn *ret;
1216 tree ret_expr = TREE_OPERAND (stmt, 0);
1217 tree result_decl, result;
1219 if (ret_expr == error_mark_node)
1220 return GS_ERROR;
1222 /* Implicit _Cilk_sync must be inserted right before any return statement
1223 if there is a _Cilk_spawn in the function. If the user has provided a
1224 _Cilk_sync, the optimizer should remove this duplicate one. */
1225 if (fn_contains_cilk_spawn_p (cfun))
1227 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1228 gimplify_and_add (impl_sync, pre_p);
1231 if (!ret_expr
1232 || TREE_CODE (ret_expr) == RESULT_DECL
1233 || ret_expr == error_mark_node)
1235 greturn *ret = gimple_build_return (ret_expr);
1236 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1237 gimplify_seq_add_stmt (pre_p, ret);
1238 return GS_ALL_DONE;
1241 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1242 result_decl = NULL_TREE;
1243 else
1245 result_decl = TREE_OPERAND (ret_expr, 0);
1247 /* See through a return by reference. */
1248 if (TREE_CODE (result_decl) == INDIRECT_REF)
1249 result_decl = TREE_OPERAND (result_decl, 0);
1251 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1252 || TREE_CODE (ret_expr) == INIT_EXPR)
1253 && TREE_CODE (result_decl) == RESULT_DECL);
1256 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1257 Recall that aggregate_value_p is FALSE for any aggregate type that is
1258 returned in registers. If we're returning values in registers, then
1259 we don't want to extend the lifetime of the RESULT_DECL, particularly
1260 across another call. In addition, for those aggregates for which
1261 hard_function_value generates a PARALLEL, we'll die during normal
1262 expansion of structure assignments; there's special code in expand_return
1263 to handle this case that does not exist in expand_expr. */
1264 if (!result_decl)
1265 result = NULL_TREE;
1266 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1268 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1270 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1271 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1272 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1273 should be effectively allocated by the caller, i.e. all calls to
1274 this function must be subject to the Return Slot Optimization. */
1275 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1276 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1278 result = result_decl;
1280 else if (gimplify_ctxp->return_temp)
1281 result = gimplify_ctxp->return_temp;
1282 else
1284 result = create_tmp_reg (TREE_TYPE (result_decl));
1286 /* ??? With complex control flow (usually involving abnormal edges),
1287 we can wind up warning about an uninitialized value for this. Due
1288 to how this variable is constructed and initialized, this is never
1289 true. Give up and never warn. */
1290 TREE_NO_WARNING (result) = 1;
1292 gimplify_ctxp->return_temp = result;
1295 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1296 Then gimplify the whole thing. */
1297 if (result != result_decl)
1298 TREE_OPERAND (ret_expr, 0) = result;
1300 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1302 ret = gimple_build_return (result);
1303 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1304 gimplify_seq_add_stmt (pre_p, ret);
1306 return GS_ALL_DONE;
1309 /* Gimplify a variable-length array DECL. */
1311 static void
1312 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1314 /* This is a variable-sized decl. Simplify its size and mark it
1315 for deferred expansion. */
1316 tree t, addr, ptr_type;
1318 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1319 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1321 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1322 if (DECL_HAS_VALUE_EXPR_P (decl))
1323 return;
1325 /* All occurrences of this decl in final gimplified code will be
1326 replaced by indirection. Setting DECL_VALUE_EXPR does two
1327 things: First, it lets the rest of the gimplifier know what
1328 replacement to use. Second, it lets the debug info know
1329 where to find the value. */
1330 ptr_type = build_pointer_type (TREE_TYPE (decl));
1331 addr = create_tmp_var (ptr_type, get_name (decl));
1332 DECL_IGNORED_P (addr) = 0;
1333 t = build_fold_indirect_ref (addr);
1334 TREE_THIS_NOTRAP (t) = 1;
1335 SET_DECL_VALUE_EXPR (decl, t);
1336 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1338 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1339 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1340 size_int (DECL_ALIGN (decl)));
1341 /* The call has been built for a variable-sized object. */
1342 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1343 t = fold_convert (ptr_type, t);
1344 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1346 gimplify_and_add (t, seq_p);
1348 /* Indicate that we need to restore the stack level when the
1349 enclosing BIND_EXPR is exited. */
1350 gimplify_ctxp->save_stack = true;
1353 /* A helper function to be called via walk_tree. Mark all labels under *TP
1354 as being forced. To be called for DECL_INITIAL of static variables. */
1356 static tree
1357 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1359 if (TYPE_P (*tp))
1360 *walk_subtrees = 0;
1361 if (TREE_CODE (*tp) == LABEL_DECL)
1362 FORCED_LABEL (*tp) = 1;
1364 return NULL_TREE;
1367 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1368 and initialization explicit. */
1370 static enum gimplify_status
1371 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1373 tree stmt = *stmt_p;
1374 tree decl = DECL_EXPR_DECL (stmt);
1376 *stmt_p = NULL_TREE;
1378 if (TREE_TYPE (decl) == error_mark_node)
1379 return GS_ERROR;
1381 if ((TREE_CODE (decl) == TYPE_DECL
1382 || TREE_CODE (decl) == VAR_DECL)
1383 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1384 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1386 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1387 in case its size expressions contain problematic nodes like CALL_EXPR. */
1388 if (TREE_CODE (decl) == TYPE_DECL
1389 && DECL_ORIGINAL_TYPE (decl)
1390 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1391 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1393 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1395 tree init = DECL_INITIAL (decl);
1397 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1398 || (!TREE_STATIC (decl)
1399 && flag_stack_check == GENERIC_STACK_CHECK
1400 && compare_tree_int (DECL_SIZE_UNIT (decl),
1401 STACK_CHECK_MAX_VAR_SIZE) > 0))
1402 gimplify_vla_decl (decl, seq_p);
1404 /* Some front ends do not explicitly declare all anonymous
1405 artificial variables. We compensate here by declaring the
1406 variables, though it would be better if the front ends would
1407 explicitly declare them. */
1408 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1409 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1410 gimple_add_tmp_var (decl);
1412 if (init && init != error_mark_node)
1414 if (!TREE_STATIC (decl))
1416 DECL_INITIAL (decl) = NULL_TREE;
1417 init = build2 (INIT_EXPR, void_type_node, decl, init);
1418 gimplify_and_add (init, seq_p);
1419 ggc_free (init);
1421 else
1422 /* We must still examine initializers for static variables
1423 as they may contain a label address. */
1424 walk_tree (&init, force_labels_r, NULL, NULL);
1428 return GS_ALL_DONE;
1431 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1432 and replacing the LOOP_EXPR with goto, but if the loop contains an
1433 EXIT_EXPR, we need to append a label for it to jump to. */
1435 static enum gimplify_status
1436 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1438 tree saved_label = gimplify_ctxp->exit_label;
1439 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1441 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1443 gimplify_ctxp->exit_label = NULL_TREE;
1445 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1447 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1449 if (gimplify_ctxp->exit_label)
1450 gimplify_seq_add_stmt (pre_p,
1451 gimple_build_label (gimplify_ctxp->exit_label));
1453 gimplify_ctxp->exit_label = saved_label;
1455 *expr_p = NULL;
1456 return GS_ALL_DONE;
1459 /* Gimplify a statement list onto a sequence. These may be created either
1460 by an enlightened front-end, or by shortcut_cond_expr. */
1462 static enum gimplify_status
1463 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1465 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1467 tree_stmt_iterator i = tsi_start (*expr_p);
1469 while (!tsi_end_p (i))
1471 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1472 tsi_delink (&i);
1475 if (temp)
1477 *expr_p = temp;
1478 return GS_OK;
1481 return GS_ALL_DONE;
1485 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1486 branch to. */
1488 static enum gimplify_status
1489 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1491 tree switch_expr = *expr_p;
1492 gimple_seq switch_body_seq = NULL;
1493 enum gimplify_status ret;
1494 tree index_type = TREE_TYPE (switch_expr);
1495 if (index_type == NULL_TREE)
1496 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1498 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1499 fb_rvalue);
1500 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1501 return ret;
1503 if (SWITCH_BODY (switch_expr))
1505 vec<tree> labels;
1506 vec<tree> saved_labels;
1507 tree default_case = NULL_TREE;
1508 gswitch *switch_stmt;
1510 /* If someone can be bothered to fill in the labels, they can
1511 be bothered to null out the body too. */
1512 gcc_assert (!SWITCH_LABELS (switch_expr));
1514 /* Save old labels, get new ones from body, then restore the old
1515 labels. Save all the things from the switch body to append after. */
1516 saved_labels = gimplify_ctxp->case_labels;
1517 gimplify_ctxp->case_labels.create (8);
1519 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1520 labels = gimplify_ctxp->case_labels;
1521 gimplify_ctxp->case_labels = saved_labels;
1523 preprocess_case_label_vec_for_gimple (labels, index_type,
1524 &default_case);
1526 if (!default_case)
1528 glabel *new_default;
1530 default_case
1531 = build_case_label (NULL_TREE, NULL_TREE,
1532 create_artificial_label (UNKNOWN_LOCATION));
1533 new_default = gimple_build_label (CASE_LABEL (default_case));
1534 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1537 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1538 default_case, labels);
1539 gimplify_seq_add_stmt (pre_p, switch_stmt);
1540 gimplify_seq_add_seq (pre_p, switch_body_seq);
1541 labels.release ();
1543 else
1544 gcc_assert (SWITCH_LABELS (switch_expr));
1546 return GS_ALL_DONE;
1549 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1551 static enum gimplify_status
1552 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1554 struct gimplify_ctx *ctxp;
1555 glabel *label_stmt;
1557 /* Invalid programs can play Duff's Device type games with, for example,
1558 #pragma omp parallel. At least in the C front end, we don't
1559 detect such invalid branches until after gimplification, in the
1560 diagnose_omp_blocks pass. */
1561 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1562 if (ctxp->case_labels.exists ())
1563 break;
1565 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1566 ctxp->case_labels.safe_push (*expr_p);
1567 gimplify_seq_add_stmt (pre_p, label_stmt);
1569 return GS_ALL_DONE;
1572 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1573 if necessary. */
1575 tree
1576 build_and_jump (tree *label_p)
1578 if (label_p == NULL)
1579 /* If there's nowhere to jump, just fall through. */
1580 return NULL_TREE;
1582 if (*label_p == NULL_TREE)
1584 tree label = create_artificial_label (UNKNOWN_LOCATION);
1585 *label_p = label;
1588 return build1 (GOTO_EXPR, void_type_node, *label_p);
1591 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1592 This also involves building a label to jump to and communicating it to
1593 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1595 static enum gimplify_status
1596 gimplify_exit_expr (tree *expr_p)
1598 tree cond = TREE_OPERAND (*expr_p, 0);
1599 tree expr;
1601 expr = build_and_jump (&gimplify_ctxp->exit_label);
1602 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1603 *expr_p = expr;
1605 return GS_OK;
1608 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1609 different from its canonical type, wrap the whole thing inside a
1610 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1611 type.
1613 The canonical type of a COMPONENT_REF is the type of the field being
1614 referenced--unless the field is a bit-field which can be read directly
1615 in a smaller mode, in which case the canonical type is the
1616 sign-appropriate type corresponding to that mode. */
1618 static void
1619 canonicalize_component_ref (tree *expr_p)
1621 tree expr = *expr_p;
1622 tree type;
1624 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1626 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1627 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1628 else
1629 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1631 /* One could argue that all the stuff below is not necessary for
1632 the non-bitfield case and declare it a FE error if type
1633 adjustment would be needed. */
1634 if (TREE_TYPE (expr) != type)
1636 #ifdef ENABLE_TYPES_CHECKING
1637 tree old_type = TREE_TYPE (expr);
1638 #endif
1639 int type_quals;
1641 /* We need to preserve qualifiers and propagate them from
1642 operand 0. */
1643 type_quals = TYPE_QUALS (type)
1644 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1645 if (TYPE_QUALS (type) != type_quals)
1646 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1648 /* Set the type of the COMPONENT_REF to the underlying type. */
1649 TREE_TYPE (expr) = type;
1651 #ifdef ENABLE_TYPES_CHECKING
1652 /* It is now a FE error, if the conversion from the canonical
1653 type to the original expression type is not useless. */
1654 gcc_assert (useless_type_conversion_p (old_type, type));
1655 #endif
1659 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1660 to foo, embed that change in the ADDR_EXPR by converting
1661 T array[U];
1662 (T *)&array
1664 &array[L]
1665 where L is the lower bound. For simplicity, only do this for constant
1666 lower bound.
1667 The constraint is that the type of &array[L] is trivially convertible
1668 to T *. */
1670 static void
1671 canonicalize_addr_expr (tree *expr_p)
1673 tree expr = *expr_p;
1674 tree addr_expr = TREE_OPERAND (expr, 0);
1675 tree datype, ddatype, pddatype;
1677 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1678 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1679 || TREE_CODE (addr_expr) != ADDR_EXPR)
1680 return;
1682 /* The addr_expr type should be a pointer to an array. */
1683 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1684 if (TREE_CODE (datype) != ARRAY_TYPE)
1685 return;
1687 /* The pointer to element type shall be trivially convertible to
1688 the expression pointer type. */
1689 ddatype = TREE_TYPE (datype);
1690 pddatype = build_pointer_type (ddatype);
1691 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1692 pddatype))
1693 return;
1695 /* The lower bound and element sizes must be constant. */
1696 if (!TYPE_SIZE_UNIT (ddatype)
1697 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1698 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1699 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1700 return;
1702 /* All checks succeeded. Build a new node to merge the cast. */
1703 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1704 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1705 NULL_TREE, NULL_TREE);
1706 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1708 /* We can have stripped a required restrict qualifier above. */
1709 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1710 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1713 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1714 underneath as appropriate. */
1716 static enum gimplify_status
1717 gimplify_conversion (tree *expr_p)
1719 location_t loc = EXPR_LOCATION (*expr_p);
1720 gcc_assert (CONVERT_EXPR_P (*expr_p));
1722 /* Then strip away all but the outermost conversion. */
1723 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1725 /* And remove the outermost conversion if it's useless. */
1726 if (tree_ssa_useless_type_conversion (*expr_p))
1727 *expr_p = TREE_OPERAND (*expr_p, 0);
1729 /* If we still have a conversion at the toplevel,
1730 then canonicalize some constructs. */
1731 if (CONVERT_EXPR_P (*expr_p))
1733 tree sub = TREE_OPERAND (*expr_p, 0);
1735 /* If a NOP conversion is changing the type of a COMPONENT_REF
1736 expression, then canonicalize its type now in order to expose more
1737 redundant conversions. */
1738 if (TREE_CODE (sub) == COMPONENT_REF)
1739 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1741 /* If a NOP conversion is changing a pointer to array of foo
1742 to a pointer to foo, embed that change in the ADDR_EXPR. */
1743 else if (TREE_CODE (sub) == ADDR_EXPR)
1744 canonicalize_addr_expr (expr_p);
1747 /* If we have a conversion to a non-register type force the
1748 use of a VIEW_CONVERT_EXPR instead. */
1749 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1750 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1751 TREE_OPERAND (*expr_p, 0));
1753 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1754 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1755 TREE_SET_CODE (*expr_p, NOP_EXPR);
1757 return GS_OK;
1760 /* Nonlocal VLAs seen in the current function. */
1761 static hash_set<tree> *nonlocal_vlas;
1763 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1764 static tree nonlocal_vla_vars;
1766 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1767 DECL_VALUE_EXPR, and it's worth re-examining things. */
1769 static enum gimplify_status
1770 gimplify_var_or_parm_decl (tree *expr_p)
1772 tree decl = *expr_p;
1774 /* ??? If this is a local variable, and it has not been seen in any
1775 outer BIND_EXPR, then it's probably the result of a duplicate
1776 declaration, for which we've already issued an error. It would
1777 be really nice if the front end wouldn't leak these at all.
1778 Currently the only known culprit is C++ destructors, as seen
1779 in g++.old-deja/g++.jason/binding.C. */
1780 if (TREE_CODE (decl) == VAR_DECL
1781 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1782 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1783 && decl_function_context (decl) == current_function_decl)
1785 gcc_assert (seen_error ());
1786 return GS_ERROR;
1789 /* When within an OMP context, notice uses of variables. */
1790 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1791 return GS_ALL_DONE;
1793 /* If the decl is an alias for another expression, substitute it now. */
1794 if (DECL_HAS_VALUE_EXPR_P (decl))
1796 tree value_expr = DECL_VALUE_EXPR (decl);
1798 /* For referenced nonlocal VLAs add a decl for debugging purposes
1799 to the current function. */
1800 if (TREE_CODE (decl) == VAR_DECL
1801 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1802 && nonlocal_vlas != NULL
1803 && TREE_CODE (value_expr) == INDIRECT_REF
1804 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1805 && decl_function_context (decl) != current_function_decl)
1807 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1808 while (ctx
1809 && (ctx->region_type == ORT_WORKSHARE
1810 || ctx->region_type == ORT_SIMD))
1811 ctx = ctx->outer_context;
1812 if (!ctx && !nonlocal_vlas->add (decl))
1814 tree copy = copy_node (decl);
1816 lang_hooks.dup_lang_specific_decl (copy);
1817 SET_DECL_RTL (copy, 0);
1818 TREE_USED (copy) = 1;
1819 DECL_CHAIN (copy) = nonlocal_vla_vars;
1820 nonlocal_vla_vars = copy;
1821 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1822 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1826 *expr_p = unshare_expr (value_expr);
1827 return GS_OK;
1830 return GS_ALL_DONE;
1833 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1835 static void
1836 recalculate_side_effects (tree t)
1838 enum tree_code code = TREE_CODE (t);
1839 int len = TREE_OPERAND_LENGTH (t);
1840 int i;
1842 switch (TREE_CODE_CLASS (code))
1844 case tcc_expression:
1845 switch (code)
1847 case INIT_EXPR:
1848 case MODIFY_EXPR:
1849 case VA_ARG_EXPR:
1850 case PREDECREMENT_EXPR:
1851 case PREINCREMENT_EXPR:
1852 case POSTDECREMENT_EXPR:
1853 case POSTINCREMENT_EXPR:
1854 /* All of these have side-effects, no matter what their
1855 operands are. */
1856 return;
1858 default:
1859 break;
1861 /* Fall through. */
1863 case tcc_comparison: /* a comparison expression */
1864 case tcc_unary: /* a unary arithmetic expression */
1865 case tcc_binary: /* a binary arithmetic expression */
1866 case tcc_reference: /* a reference */
1867 case tcc_vl_exp: /* a function call */
1868 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1869 for (i = 0; i < len; ++i)
1871 tree op = TREE_OPERAND (t, i);
1872 if (op && TREE_SIDE_EFFECTS (op))
1873 TREE_SIDE_EFFECTS (t) = 1;
1875 break;
1877 case tcc_constant:
1878 /* No side-effects. */
1879 return;
1881 default:
1882 gcc_unreachable ();
1886 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1887 node *EXPR_P.
1889 compound_lval
1890 : min_lval '[' val ']'
1891 | min_lval '.' ID
1892 | compound_lval '[' val ']'
1893 | compound_lval '.' ID
1895 This is not part of the original SIMPLE definition, which separates
1896 array and member references, but it seems reasonable to handle them
1897 together. Also, this way we don't run into problems with union
1898 aliasing; gcc requires that for accesses through a union to alias, the
1899 union reference must be explicit, which was not always the case when we
1900 were splitting up array and member refs.
1902 PRE_P points to the sequence where side effects that must happen before
1903 *EXPR_P should be stored.
1905 POST_P points to the sequence where side effects that must happen after
1906 *EXPR_P should be stored. */
1908 static enum gimplify_status
1909 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1910 fallback_t fallback)
1912 tree *p;
1913 enum gimplify_status ret = GS_ALL_DONE, tret;
1914 int i;
1915 location_t loc = EXPR_LOCATION (*expr_p);
1916 tree expr = *expr_p;
1918 /* Create a stack of the subexpressions so later we can walk them in
1919 order from inner to outer. */
1920 auto_vec<tree, 10> expr_stack;
1922 /* We can handle anything that get_inner_reference can deal with. */
1923 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1925 restart:
1926 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1927 if (TREE_CODE (*p) == INDIRECT_REF)
1928 *p = fold_indirect_ref_loc (loc, *p);
1930 if (handled_component_p (*p))
1932 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1933 additional COMPONENT_REFs. */
1934 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1935 && gimplify_var_or_parm_decl (p) == GS_OK)
1936 goto restart;
1937 else
1938 break;
1940 expr_stack.safe_push (*p);
1943 gcc_assert (expr_stack.length ());
1945 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1946 walked through and P points to the innermost expression.
1948 Java requires that we elaborated nodes in source order. That
1949 means we must gimplify the inner expression followed by each of
1950 the indices, in order. But we can't gimplify the inner
1951 expression until we deal with any variable bounds, sizes, or
1952 positions in order to deal with PLACEHOLDER_EXPRs.
1954 So we do this in three steps. First we deal with the annotations
1955 for any variables in the components, then we gimplify the base,
1956 then we gimplify any indices, from left to right. */
1957 for (i = expr_stack.length () - 1; i >= 0; i--)
1959 tree t = expr_stack[i];
1961 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1963 /* Gimplify the low bound and element type size and put them into
1964 the ARRAY_REF. If these values are set, they have already been
1965 gimplified. */
1966 if (TREE_OPERAND (t, 2) == NULL_TREE)
1968 tree low = unshare_expr (array_ref_low_bound (t));
1969 if (!is_gimple_min_invariant (low))
1971 TREE_OPERAND (t, 2) = low;
1972 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1973 post_p, is_gimple_reg,
1974 fb_rvalue);
1975 ret = MIN (ret, tret);
1978 else
1980 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1981 is_gimple_reg, fb_rvalue);
1982 ret = MIN (ret, tret);
1985 if (TREE_OPERAND (t, 3) == NULL_TREE)
1987 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1988 tree elmt_size = unshare_expr (array_ref_element_size (t));
1989 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1991 /* Divide the element size by the alignment of the element
1992 type (above). */
1993 elmt_size
1994 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1996 if (!is_gimple_min_invariant (elmt_size))
1998 TREE_OPERAND (t, 3) = elmt_size;
1999 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2000 post_p, is_gimple_reg,
2001 fb_rvalue);
2002 ret = MIN (ret, tret);
2005 else
2007 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2008 is_gimple_reg, fb_rvalue);
2009 ret = MIN (ret, tret);
2012 else if (TREE_CODE (t) == COMPONENT_REF)
2014 /* Set the field offset into T and gimplify it. */
2015 if (TREE_OPERAND (t, 2) == NULL_TREE)
2017 tree offset = unshare_expr (component_ref_field_offset (t));
2018 tree field = TREE_OPERAND (t, 1);
2019 tree factor
2020 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2022 /* Divide the offset by its alignment. */
2023 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2025 if (!is_gimple_min_invariant (offset))
2027 TREE_OPERAND (t, 2) = offset;
2028 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2029 post_p, is_gimple_reg,
2030 fb_rvalue);
2031 ret = MIN (ret, tret);
2034 else
2036 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2037 is_gimple_reg, fb_rvalue);
2038 ret = MIN (ret, tret);
2043 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2044 so as to match the min_lval predicate. Failure to do so may result
2045 in the creation of large aggregate temporaries. */
2046 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2047 fallback | fb_lvalue);
2048 ret = MIN (ret, tret);
2050 /* And finally, the indices and operands of ARRAY_REF. During this
2051 loop we also remove any useless conversions. */
2052 for (; expr_stack.length () > 0; )
2054 tree t = expr_stack.pop ();
2056 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2058 /* Gimplify the dimension. */
2059 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2061 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2062 is_gimple_val, fb_rvalue);
2063 ret = MIN (ret, tret);
2067 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2069 /* The innermost expression P may have originally had
2070 TREE_SIDE_EFFECTS set which would have caused all the outer
2071 expressions in *EXPR_P leading to P to also have had
2072 TREE_SIDE_EFFECTS set. */
2073 recalculate_side_effects (t);
2076 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2077 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2079 canonicalize_component_ref (expr_p);
2082 expr_stack.release ();
2084 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2086 return ret;
2089 /* Gimplify the self modifying expression pointed to by EXPR_P
2090 (++, --, +=, -=).
2092 PRE_P points to the list where side effects that must happen before
2093 *EXPR_P should be stored.
2095 POST_P points to the list where side effects that must happen after
2096 *EXPR_P should be stored.
2098 WANT_VALUE is nonzero iff we want to use the value of this expression
2099 in another expression.
2101 ARITH_TYPE is the type the computation should be performed in. */
2103 enum gimplify_status
2104 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2105 bool want_value, tree arith_type)
2107 enum tree_code code;
2108 tree lhs, lvalue, rhs, t1;
2109 gimple_seq post = NULL, *orig_post_p = post_p;
2110 bool postfix;
2111 enum tree_code arith_code;
2112 enum gimplify_status ret;
2113 location_t loc = EXPR_LOCATION (*expr_p);
2115 code = TREE_CODE (*expr_p);
2117 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2118 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2120 /* Prefix or postfix? */
2121 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2122 /* Faster to treat as prefix if result is not used. */
2123 postfix = want_value;
2124 else
2125 postfix = false;
2127 /* For postfix, make sure the inner expression's post side effects
2128 are executed after side effects from this expression. */
2129 if (postfix)
2130 post_p = &post;
2132 /* Add or subtract? */
2133 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2134 arith_code = PLUS_EXPR;
2135 else
2136 arith_code = MINUS_EXPR;
2138 /* Gimplify the LHS into a GIMPLE lvalue. */
2139 lvalue = TREE_OPERAND (*expr_p, 0);
2140 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2141 if (ret == GS_ERROR)
2142 return ret;
2144 /* Extract the operands to the arithmetic operation. */
2145 lhs = lvalue;
2146 rhs = TREE_OPERAND (*expr_p, 1);
2148 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2149 that as the result value and in the postqueue operation. */
2150 if (postfix)
2152 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2153 if (ret == GS_ERROR)
2154 return ret;
2156 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2159 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2160 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2162 rhs = convert_to_ptrofftype_loc (loc, rhs);
2163 if (arith_code == MINUS_EXPR)
2164 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2165 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2167 else
2168 t1 = fold_convert (TREE_TYPE (*expr_p),
2169 fold_build2 (arith_code, arith_type,
2170 fold_convert (arith_type, lhs),
2171 fold_convert (arith_type, rhs)));
2173 if (postfix)
2175 gimplify_assign (lvalue, t1, pre_p);
2176 gimplify_seq_add_seq (orig_post_p, post);
2177 *expr_p = lhs;
2178 return GS_ALL_DONE;
2180 else
2182 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2183 return GS_OK;
2187 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2189 static void
2190 maybe_with_size_expr (tree *expr_p)
2192 tree expr = *expr_p;
2193 tree type = TREE_TYPE (expr);
2194 tree size;
2196 /* If we've already wrapped this or the type is error_mark_node, we can't do
2197 anything. */
2198 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2199 || type == error_mark_node)
2200 return;
2202 /* If the size isn't known or is a constant, we have nothing to do. */
2203 size = TYPE_SIZE_UNIT (type);
2204 if (!size || TREE_CODE (size) == INTEGER_CST)
2205 return;
2207 /* Otherwise, make a WITH_SIZE_EXPR. */
2208 size = unshare_expr (size);
2209 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2210 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2213 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2214 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2215 the CALL_EXPR. */
2217 enum gimplify_status
2218 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2220 bool (*test) (tree);
2221 fallback_t fb;
2223 /* In general, we allow lvalues for function arguments to avoid
2224 extra overhead of copying large aggregates out of even larger
2225 aggregates into temporaries only to copy the temporaries to
2226 the argument list. Make optimizers happy by pulling out to
2227 temporaries those types that fit in registers. */
2228 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2229 test = is_gimple_val, fb = fb_rvalue;
2230 else
2232 test = is_gimple_lvalue, fb = fb_either;
2233 /* Also strip a TARGET_EXPR that would force an extra copy. */
2234 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2236 tree init = TARGET_EXPR_INITIAL (*arg_p);
2237 if (init
2238 && !VOID_TYPE_P (TREE_TYPE (init)))
2239 *arg_p = init;
2243 /* If this is a variable sized type, we must remember the size. */
2244 maybe_with_size_expr (arg_p);
2246 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2247 /* Make sure arguments have the same location as the function call
2248 itself. */
2249 protected_set_expr_location (*arg_p, call_location);
2251 /* There is a sequence point before a function call. Side effects in
2252 the argument list must occur before the actual call. So, when
2253 gimplifying arguments, force gimplify_expr to use an internal
2254 post queue which is then appended to the end of PRE_P. */
2255 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2258 /* Don't fold inside offloading regions: it can break code by adding decl
2259 references that weren't in the source. We'll do it during omplower pass
2260 instead. */
2262 static bool
2263 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2265 struct gimplify_omp_ctx *ctx;
2266 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2267 if (ctx->region_type == ORT_TARGET)
2268 return false;
2269 return fold_stmt (gsi);
2272 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2273 WANT_VALUE is true if the result of the call is desired. */
2275 static enum gimplify_status
2276 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2278 tree fndecl, parms, p, fnptrtype;
2279 enum gimplify_status ret;
2280 int i, nargs;
2281 gcall *call;
2282 bool builtin_va_start_p = false;
2283 location_t loc = EXPR_LOCATION (*expr_p);
2285 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2287 /* For reliable diagnostics during inlining, it is necessary that
2288 every call_expr be annotated with file and line. */
2289 if (! EXPR_HAS_LOCATION (*expr_p))
2290 SET_EXPR_LOCATION (*expr_p, input_location);
2292 /* Gimplify internal functions created in the FEs. */
2293 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2295 if (want_value)
2296 return GS_ALL_DONE;
2298 nargs = call_expr_nargs (*expr_p);
2299 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2300 auto_vec<tree> vargs (nargs);
2302 for (i = 0; i < nargs; i++)
2304 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2305 EXPR_LOCATION (*expr_p));
2306 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2308 gimple call = gimple_build_call_internal_vec (ifn, vargs);
2309 gimplify_seq_add_stmt (pre_p, call);
2310 return GS_ALL_DONE;
2313 /* This may be a call to a builtin function.
2315 Builtin function calls may be transformed into different
2316 (and more efficient) builtin function calls under certain
2317 circumstances. Unfortunately, gimplification can muck things
2318 up enough that the builtin expanders are not aware that certain
2319 transformations are still valid.
2321 So we attempt transformation/gimplification of the call before
2322 we gimplify the CALL_EXPR. At this time we do not manage to
2323 transform all calls in the same manner as the expanders do, but
2324 we do transform most of them. */
2325 fndecl = get_callee_fndecl (*expr_p);
2326 if (fndecl
2327 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2328 switch (DECL_FUNCTION_CODE (fndecl))
2330 case BUILT_IN_VA_START:
2332 builtin_va_start_p = TRUE;
2333 if (call_expr_nargs (*expr_p) < 2)
2335 error ("too few arguments to function %<va_start%>");
2336 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2337 return GS_OK;
2340 if (fold_builtin_next_arg (*expr_p, true))
2342 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2343 return GS_OK;
2345 break;
2347 case BUILT_IN_LINE:
2349 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2350 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2351 return GS_OK;
2353 case BUILT_IN_FILE:
2355 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2356 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2357 return GS_OK;
2359 case BUILT_IN_FUNCTION:
2361 const char *function;
2362 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2363 *expr_p = build_string_literal (strlen (function) + 1, function);
2364 return GS_OK;
2366 default:
2369 if (fndecl && DECL_BUILT_IN (fndecl))
2371 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2372 if (new_tree && new_tree != *expr_p)
2374 /* There was a transformation of this call which computes the
2375 same value, but in a more efficient way. Return and try
2376 again. */
2377 *expr_p = new_tree;
2378 return GS_OK;
2382 /* Remember the original function pointer type. */
2383 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2385 /* There is a sequence point before the call, so any side effects in
2386 the calling expression must occur before the actual call. Force
2387 gimplify_expr to use an internal post queue. */
2388 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2389 is_gimple_call_addr, fb_rvalue);
2391 nargs = call_expr_nargs (*expr_p);
2393 /* Get argument types for verification. */
2394 fndecl = get_callee_fndecl (*expr_p);
2395 parms = NULL_TREE;
2396 if (fndecl)
2397 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2398 else
2399 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2401 if (fndecl && DECL_ARGUMENTS (fndecl))
2402 p = DECL_ARGUMENTS (fndecl);
2403 else if (parms)
2404 p = parms;
2405 else
2406 p = NULL_TREE;
2407 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2410 /* If the last argument is __builtin_va_arg_pack () and it is not
2411 passed as a named argument, decrease the number of CALL_EXPR
2412 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2413 if (!p
2414 && i < nargs
2415 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2417 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2418 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2420 if (last_arg_fndecl
2421 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2422 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2423 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2425 tree call = *expr_p;
2427 --nargs;
2428 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2429 CALL_EXPR_FN (call),
2430 nargs, CALL_EXPR_ARGP (call));
2432 /* Copy all CALL_EXPR flags, location and block, except
2433 CALL_EXPR_VA_ARG_PACK flag. */
2434 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2435 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2436 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2437 = CALL_EXPR_RETURN_SLOT_OPT (call);
2438 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2439 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2441 /* Set CALL_EXPR_VA_ARG_PACK. */
2442 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2446 /* Gimplify the function arguments. */
2447 if (nargs > 0)
2449 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2450 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2451 PUSH_ARGS_REVERSED ? i-- : i++)
2453 enum gimplify_status t;
2455 /* Avoid gimplifying the second argument to va_start, which needs to
2456 be the plain PARM_DECL. */
2457 if ((i != 1) || !builtin_va_start_p)
2459 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2460 EXPR_LOCATION (*expr_p));
2462 if (t == GS_ERROR)
2463 ret = GS_ERROR;
2468 /* Gimplify the static chain. */
2469 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2471 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2472 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2473 else
2475 enum gimplify_status t;
2476 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2477 EXPR_LOCATION (*expr_p));
2478 if (t == GS_ERROR)
2479 ret = GS_ERROR;
2483 /* Verify the function result. */
2484 if (want_value && fndecl
2485 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2487 error_at (loc, "using result of function returning %<void%>");
2488 ret = GS_ERROR;
2491 /* Try this again in case gimplification exposed something. */
2492 if (ret != GS_ERROR)
2494 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2496 if (new_tree && new_tree != *expr_p)
2498 /* There was a transformation of this call which computes the
2499 same value, but in a more efficient way. Return and try
2500 again. */
2501 *expr_p = new_tree;
2502 return GS_OK;
2505 else
2507 *expr_p = error_mark_node;
2508 return GS_ERROR;
2511 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2512 decl. This allows us to eliminate redundant or useless
2513 calls to "const" functions. */
2514 if (TREE_CODE (*expr_p) == CALL_EXPR)
2516 int flags = call_expr_flags (*expr_p);
2517 if (flags & (ECF_CONST | ECF_PURE)
2518 /* An infinite loop is considered a side effect. */
2519 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2520 TREE_SIDE_EFFECTS (*expr_p) = 0;
2523 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2524 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2525 form and delegate the creation of a GIMPLE_CALL to
2526 gimplify_modify_expr. This is always possible because when
2527 WANT_VALUE is true, the caller wants the result of this call into
2528 a temporary, which means that we will emit an INIT_EXPR in
2529 internal_get_tmp_var which will then be handled by
2530 gimplify_modify_expr. */
2531 if (!want_value)
2533 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2534 have to do is replicate it as a GIMPLE_CALL tuple. */
2535 gimple_stmt_iterator gsi;
2536 call = gimple_build_call_from_tree (*expr_p);
2537 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2538 notice_special_calls (call);
2539 gimplify_seq_add_stmt (pre_p, call);
2540 gsi = gsi_last (*pre_p);
2541 maybe_fold_stmt (&gsi);
2542 *expr_p = NULL_TREE;
2544 else
2545 /* Remember the original function type. */
2546 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2547 CALL_EXPR_FN (*expr_p));
2549 return ret;
2552 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2553 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2555 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2556 condition is true or false, respectively. If null, we should generate
2557 our own to skip over the evaluation of this specific expression.
2559 LOCUS is the source location of the COND_EXPR.
2561 This function is the tree equivalent of do_jump.
2563 shortcut_cond_r should only be called by shortcut_cond_expr. */
2565 static tree
2566 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2567 location_t locus)
2569 tree local_label = NULL_TREE;
2570 tree t, expr = NULL;
2572 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2573 retain the shortcut semantics. Just insert the gotos here;
2574 shortcut_cond_expr will append the real blocks later. */
2575 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2577 location_t new_locus;
2579 /* Turn if (a && b) into
2581 if (a); else goto no;
2582 if (b) goto yes; else goto no;
2583 (no:) */
2585 if (false_label_p == NULL)
2586 false_label_p = &local_label;
2588 /* Keep the original source location on the first 'if'. */
2589 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2590 append_to_statement_list (t, &expr);
2592 /* Set the source location of the && on the second 'if'. */
2593 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2594 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2595 new_locus);
2596 append_to_statement_list (t, &expr);
2598 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2600 location_t new_locus;
2602 /* Turn if (a || b) into
2604 if (a) goto yes;
2605 if (b) goto yes; else goto no;
2606 (yes:) */
2608 if (true_label_p == NULL)
2609 true_label_p = &local_label;
2611 /* Keep the original source location on the first 'if'. */
2612 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2613 append_to_statement_list (t, &expr);
2615 /* Set the source location of the || on the second 'if'. */
2616 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2617 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2618 new_locus);
2619 append_to_statement_list (t, &expr);
2621 else if (TREE_CODE (pred) == COND_EXPR
2622 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2623 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2625 location_t new_locus;
2627 /* As long as we're messing with gotos, turn if (a ? b : c) into
2628 if (a)
2629 if (b) goto yes; else goto no;
2630 else
2631 if (c) goto yes; else goto no;
2633 Don't do this if one of the arms has void type, which can happen
2634 in C++ when the arm is throw. */
2636 /* Keep the original source location on the first 'if'. Set the source
2637 location of the ? on the second 'if'. */
2638 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2639 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2640 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2641 false_label_p, locus),
2642 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2643 false_label_p, new_locus));
2645 else
2647 expr = build3 (COND_EXPR, void_type_node, pred,
2648 build_and_jump (true_label_p),
2649 build_and_jump (false_label_p));
2650 SET_EXPR_LOCATION (expr, locus);
2653 if (local_label)
2655 t = build1 (LABEL_EXPR, void_type_node, local_label);
2656 append_to_statement_list (t, &expr);
2659 return expr;
2662 /* Given a conditional expression EXPR with short-circuit boolean
2663 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2664 predicate apart into the equivalent sequence of conditionals. */
2666 static tree
2667 shortcut_cond_expr (tree expr)
2669 tree pred = TREE_OPERAND (expr, 0);
2670 tree then_ = TREE_OPERAND (expr, 1);
2671 tree else_ = TREE_OPERAND (expr, 2);
2672 tree true_label, false_label, end_label, t;
2673 tree *true_label_p;
2674 tree *false_label_p;
2675 bool emit_end, emit_false, jump_over_else;
2676 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2677 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2679 /* First do simple transformations. */
2680 if (!else_se)
2682 /* If there is no 'else', turn
2683 if (a && b) then c
2684 into
2685 if (a) if (b) then c. */
2686 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2688 /* Keep the original source location on the first 'if'. */
2689 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2690 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2691 /* Set the source location of the && on the second 'if'. */
2692 if (EXPR_HAS_LOCATION (pred))
2693 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2694 then_ = shortcut_cond_expr (expr);
2695 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2696 pred = TREE_OPERAND (pred, 0);
2697 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2698 SET_EXPR_LOCATION (expr, locus);
2702 if (!then_se)
2704 /* If there is no 'then', turn
2705 if (a || b); else d
2706 into
2707 if (a); else if (b); else d. */
2708 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2710 /* Keep the original source location on the first 'if'. */
2711 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2712 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2713 /* Set the source location of the || on the second 'if'. */
2714 if (EXPR_HAS_LOCATION (pred))
2715 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2716 else_ = shortcut_cond_expr (expr);
2717 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2718 pred = TREE_OPERAND (pred, 0);
2719 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2720 SET_EXPR_LOCATION (expr, locus);
2724 /* If we're done, great. */
2725 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2726 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2727 return expr;
2729 /* Otherwise we need to mess with gotos. Change
2730 if (a) c; else d;
2732 if (a); else goto no;
2733 c; goto end;
2734 no: d; end:
2735 and recursively gimplify the condition. */
2737 true_label = false_label = end_label = NULL_TREE;
2739 /* If our arms just jump somewhere, hijack those labels so we don't
2740 generate jumps to jumps. */
2742 if (then_
2743 && TREE_CODE (then_) == GOTO_EXPR
2744 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2746 true_label = GOTO_DESTINATION (then_);
2747 then_ = NULL;
2748 then_se = false;
2751 if (else_
2752 && TREE_CODE (else_) == GOTO_EXPR
2753 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2755 false_label = GOTO_DESTINATION (else_);
2756 else_ = NULL;
2757 else_se = false;
2760 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2761 if (true_label)
2762 true_label_p = &true_label;
2763 else
2764 true_label_p = NULL;
2766 /* The 'else' branch also needs a label if it contains interesting code. */
2767 if (false_label || else_se)
2768 false_label_p = &false_label;
2769 else
2770 false_label_p = NULL;
2772 /* If there was nothing else in our arms, just forward the label(s). */
2773 if (!then_se && !else_se)
2774 return shortcut_cond_r (pred, true_label_p, false_label_p,
2775 EXPR_LOC_OR_LOC (expr, input_location));
2777 /* If our last subexpression already has a terminal label, reuse it. */
2778 if (else_se)
2779 t = expr_last (else_);
2780 else if (then_se)
2781 t = expr_last (then_);
2782 else
2783 t = NULL;
2784 if (t && TREE_CODE (t) == LABEL_EXPR)
2785 end_label = LABEL_EXPR_LABEL (t);
2787 /* If we don't care about jumping to the 'else' branch, jump to the end
2788 if the condition is false. */
2789 if (!false_label_p)
2790 false_label_p = &end_label;
2792 /* We only want to emit these labels if we aren't hijacking them. */
2793 emit_end = (end_label == NULL_TREE);
2794 emit_false = (false_label == NULL_TREE);
2796 /* We only emit the jump over the else clause if we have to--if the
2797 then clause may fall through. Otherwise we can wind up with a
2798 useless jump and a useless label at the end of gimplified code,
2799 which will cause us to think that this conditional as a whole
2800 falls through even if it doesn't. If we then inline a function
2801 which ends with such a condition, that can cause us to issue an
2802 inappropriate warning about control reaching the end of a
2803 non-void function. */
2804 jump_over_else = block_may_fallthru (then_);
2806 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2807 EXPR_LOC_OR_LOC (expr, input_location));
2809 expr = NULL;
2810 append_to_statement_list (pred, &expr);
2812 append_to_statement_list (then_, &expr);
2813 if (else_se)
2815 if (jump_over_else)
2817 tree last = expr_last (expr);
2818 t = build_and_jump (&end_label);
2819 if (EXPR_HAS_LOCATION (last))
2820 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2821 append_to_statement_list (t, &expr);
2823 if (emit_false)
2825 t = build1 (LABEL_EXPR, void_type_node, false_label);
2826 append_to_statement_list (t, &expr);
2828 append_to_statement_list (else_, &expr);
2830 if (emit_end && end_label)
2832 t = build1 (LABEL_EXPR, void_type_node, end_label);
2833 append_to_statement_list (t, &expr);
2836 return expr;
2839 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2841 tree
2842 gimple_boolify (tree expr)
2844 tree type = TREE_TYPE (expr);
2845 location_t loc = EXPR_LOCATION (expr);
2847 if (TREE_CODE (expr) == NE_EXPR
2848 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2849 && integer_zerop (TREE_OPERAND (expr, 1)))
2851 tree call = TREE_OPERAND (expr, 0);
2852 tree fn = get_callee_fndecl (call);
2854 /* For __builtin_expect ((long) (x), y) recurse into x as well
2855 if x is truth_value_p. */
2856 if (fn
2857 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2858 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2859 && call_expr_nargs (call) == 2)
2861 tree arg = CALL_EXPR_ARG (call, 0);
2862 if (arg)
2864 if (TREE_CODE (arg) == NOP_EXPR
2865 && TREE_TYPE (arg) == TREE_TYPE (call))
2866 arg = TREE_OPERAND (arg, 0);
2867 if (truth_value_p (TREE_CODE (arg)))
2869 arg = gimple_boolify (arg);
2870 CALL_EXPR_ARG (call, 0)
2871 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2877 switch (TREE_CODE (expr))
2879 case TRUTH_AND_EXPR:
2880 case TRUTH_OR_EXPR:
2881 case TRUTH_XOR_EXPR:
2882 case TRUTH_ANDIF_EXPR:
2883 case TRUTH_ORIF_EXPR:
2884 /* Also boolify the arguments of truth exprs. */
2885 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2886 /* FALLTHRU */
2888 case TRUTH_NOT_EXPR:
2889 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2891 /* These expressions always produce boolean results. */
2892 if (TREE_CODE (type) != BOOLEAN_TYPE)
2893 TREE_TYPE (expr) = boolean_type_node;
2894 return expr;
2896 case ANNOTATE_EXPR:
2897 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2899 case annot_expr_ivdep_kind:
2900 case annot_expr_no_vector_kind:
2901 case annot_expr_vector_kind:
2902 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2903 if (TREE_CODE (type) != BOOLEAN_TYPE)
2904 TREE_TYPE (expr) = boolean_type_node;
2905 return expr;
2906 default:
2907 gcc_unreachable ();
2910 default:
2911 if (COMPARISON_CLASS_P (expr))
2913 /* There expressions always prduce boolean results. */
2914 if (TREE_CODE (type) != BOOLEAN_TYPE)
2915 TREE_TYPE (expr) = boolean_type_node;
2916 return expr;
2918 /* Other expressions that get here must have boolean values, but
2919 might need to be converted to the appropriate mode. */
2920 if (TREE_CODE (type) == BOOLEAN_TYPE)
2921 return expr;
2922 return fold_convert_loc (loc, boolean_type_node, expr);
2926 /* Given a conditional expression *EXPR_P without side effects, gimplify
2927 its operands. New statements are inserted to PRE_P. */
2929 static enum gimplify_status
2930 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2932 tree expr = *expr_p, cond;
2933 enum gimplify_status ret, tret;
2934 enum tree_code code;
2936 cond = gimple_boolify (COND_EXPR_COND (expr));
2938 /* We need to handle && and || specially, as their gimplification
2939 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2940 code = TREE_CODE (cond);
2941 if (code == TRUTH_ANDIF_EXPR)
2942 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2943 else if (code == TRUTH_ORIF_EXPR)
2944 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2945 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2946 COND_EXPR_COND (*expr_p) = cond;
2948 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2949 is_gimple_val, fb_rvalue);
2950 ret = MIN (ret, tret);
2951 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2952 is_gimple_val, fb_rvalue);
2954 return MIN (ret, tret);
2957 /* Return true if evaluating EXPR could trap.
2958 EXPR is GENERIC, while tree_could_trap_p can be called
2959 only on GIMPLE. */
2961 static bool
2962 generic_expr_could_trap_p (tree expr)
2964 unsigned i, n;
2966 if (!expr || is_gimple_val (expr))
2967 return false;
2969 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2970 return true;
2972 n = TREE_OPERAND_LENGTH (expr);
2973 for (i = 0; i < n; i++)
2974 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2975 return true;
2977 return false;
2980 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2981 into
2983 if (p) if (p)
2984 t1 = a; a;
2985 else or else
2986 t1 = b; b;
2989 The second form is used when *EXPR_P is of type void.
2991 PRE_P points to the list where side effects that must happen before
2992 *EXPR_P should be stored. */
2994 static enum gimplify_status
2995 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2997 tree expr = *expr_p;
2998 tree type = TREE_TYPE (expr);
2999 location_t loc = EXPR_LOCATION (expr);
3000 tree tmp, arm1, arm2;
3001 enum gimplify_status ret;
3002 tree label_true, label_false, label_cont;
3003 bool have_then_clause_p, have_else_clause_p;
3004 gcond *cond_stmt;
3005 enum tree_code pred_code;
3006 gimple_seq seq = NULL;
3008 /* If this COND_EXPR has a value, copy the values into a temporary within
3009 the arms. */
3010 if (!VOID_TYPE_P (type))
3012 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3013 tree result;
3015 /* If either an rvalue is ok or we do not require an lvalue, create the
3016 temporary. But we cannot do that if the type is addressable. */
3017 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3018 && !TREE_ADDRESSABLE (type))
3020 if (gimplify_ctxp->allow_rhs_cond_expr
3021 /* If either branch has side effects or could trap, it can't be
3022 evaluated unconditionally. */
3023 && !TREE_SIDE_EFFECTS (then_)
3024 && !generic_expr_could_trap_p (then_)
3025 && !TREE_SIDE_EFFECTS (else_)
3026 && !generic_expr_could_trap_p (else_))
3027 return gimplify_pure_cond_expr (expr_p, pre_p);
3029 tmp = create_tmp_var (type, "iftmp");
3030 result = tmp;
3033 /* Otherwise, only create and copy references to the values. */
3034 else
3036 type = build_pointer_type (type);
3038 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3039 then_ = build_fold_addr_expr_loc (loc, then_);
3041 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3042 else_ = build_fold_addr_expr_loc (loc, else_);
3044 expr
3045 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3047 tmp = create_tmp_var (type, "iftmp");
3048 result = build_simple_mem_ref_loc (loc, tmp);
3051 /* Build the new then clause, `tmp = then_;'. But don't build the
3052 assignment if the value is void; in C++ it can be if it's a throw. */
3053 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3054 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3056 /* Similarly, build the new else clause, `tmp = else_;'. */
3057 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3058 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3060 TREE_TYPE (expr) = void_type_node;
3061 recalculate_side_effects (expr);
3063 /* Move the COND_EXPR to the prequeue. */
3064 gimplify_stmt (&expr, pre_p);
3066 *expr_p = result;
3067 return GS_ALL_DONE;
3070 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3071 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3072 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3073 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3075 /* Make sure the condition has BOOLEAN_TYPE. */
3076 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3078 /* Break apart && and || conditions. */
3079 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3080 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3082 expr = shortcut_cond_expr (expr);
3084 if (expr != *expr_p)
3086 *expr_p = expr;
3088 /* We can't rely on gimplify_expr to re-gimplify the expanded
3089 form properly, as cleanups might cause the target labels to be
3090 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3091 set up a conditional context. */
3092 gimple_push_condition ();
3093 gimplify_stmt (expr_p, &seq);
3094 gimple_pop_condition (pre_p);
3095 gimple_seq_add_seq (pre_p, seq);
3097 return GS_ALL_DONE;
3101 /* Now do the normal gimplification. */
3103 /* Gimplify condition. */
3104 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3105 fb_rvalue);
3106 if (ret == GS_ERROR)
3107 return GS_ERROR;
3108 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3110 gimple_push_condition ();
3112 have_then_clause_p = have_else_clause_p = false;
3113 if (TREE_OPERAND (expr, 1) != NULL
3114 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3115 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3116 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3117 == current_function_decl)
3118 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3119 have different locations, otherwise we end up with incorrect
3120 location information on the branches. */
3121 && (optimize
3122 || !EXPR_HAS_LOCATION (expr)
3123 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3124 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3126 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3127 have_then_clause_p = true;
3129 else
3130 label_true = create_artificial_label (UNKNOWN_LOCATION);
3131 if (TREE_OPERAND (expr, 2) != NULL
3132 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3133 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3134 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3135 == current_function_decl)
3136 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3137 have different locations, otherwise we end up with incorrect
3138 location information on the branches. */
3139 && (optimize
3140 || !EXPR_HAS_LOCATION (expr)
3141 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3142 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3144 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3145 have_else_clause_p = true;
3147 else
3148 label_false = create_artificial_label (UNKNOWN_LOCATION);
3150 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3151 &arm2);
3153 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3154 label_false);
3156 gimplify_seq_add_stmt (&seq, cond_stmt);
3157 label_cont = NULL_TREE;
3158 if (!have_then_clause_p)
3160 /* For if (...) {} else { code; } put label_true after
3161 the else block. */
3162 if (TREE_OPERAND (expr, 1) == NULL_TREE
3163 && !have_else_clause_p
3164 && TREE_OPERAND (expr, 2) != NULL_TREE)
3165 label_cont = label_true;
3166 else
3168 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3169 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3170 /* For if (...) { code; } else {} or
3171 if (...) { code; } else goto label; or
3172 if (...) { code; return; } else { ... }
3173 label_cont isn't needed. */
3174 if (!have_else_clause_p
3175 && TREE_OPERAND (expr, 2) != NULL_TREE
3176 && gimple_seq_may_fallthru (seq))
3178 gimple g;
3179 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3181 g = gimple_build_goto (label_cont);
3183 /* GIMPLE_COND's are very low level; they have embedded
3184 gotos. This particular embedded goto should not be marked
3185 with the location of the original COND_EXPR, as it would
3186 correspond to the COND_EXPR's condition, not the ELSE or the
3187 THEN arms. To avoid marking it with the wrong location, flag
3188 it as "no location". */
3189 gimple_set_do_not_emit_location (g);
3191 gimplify_seq_add_stmt (&seq, g);
3195 if (!have_else_clause_p)
3197 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3198 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3200 if (label_cont)
3201 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3203 gimple_pop_condition (pre_p);
3204 gimple_seq_add_seq (pre_p, seq);
3206 if (ret == GS_ERROR)
3207 ; /* Do nothing. */
3208 else if (have_then_clause_p || have_else_clause_p)
3209 ret = GS_ALL_DONE;
3210 else
3212 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3213 expr = TREE_OPERAND (expr, 0);
3214 gimplify_stmt (&expr, pre_p);
3217 *expr_p = NULL;
3218 return ret;
3221 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3222 to be marked addressable.
3224 We cannot rely on such an expression being directly markable if a temporary
3225 has been created by the gimplification. In this case, we create another
3226 temporary and initialize it with a copy, which will become a store after we
3227 mark it addressable. This can happen if the front-end passed us something
3228 that it could not mark addressable yet, like a Fortran pass-by-reference
3229 parameter (int) floatvar. */
3231 static void
3232 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3234 while (handled_component_p (*expr_p))
3235 expr_p = &TREE_OPERAND (*expr_p, 0);
3236 if (is_gimple_reg (*expr_p))
3238 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3239 DECL_GIMPLE_REG_P (var) = 0;
3240 *expr_p = var;
3244 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3245 a call to __builtin_memcpy. */
3247 static enum gimplify_status
3248 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3249 gimple_seq *seq_p)
3251 tree t, to, to_ptr, from, from_ptr;
3252 gcall *gs;
3253 location_t loc = EXPR_LOCATION (*expr_p);
3255 to = TREE_OPERAND (*expr_p, 0);
3256 from = TREE_OPERAND (*expr_p, 1);
3258 /* Mark the RHS addressable. Beware that it may not be possible to do so
3259 directly if a temporary has been created by the gimplification. */
3260 prepare_gimple_addressable (&from, seq_p);
3262 mark_addressable (from);
3263 from_ptr = build_fold_addr_expr_loc (loc, from);
3264 gimplify_arg (&from_ptr, seq_p, loc);
3266 mark_addressable (to);
3267 to_ptr = build_fold_addr_expr_loc (loc, to);
3268 gimplify_arg (&to_ptr, seq_p, loc);
3270 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3272 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3274 if (want_value)
3276 /* tmp = memcpy() */
3277 t = create_tmp_var (TREE_TYPE (to_ptr));
3278 gimple_call_set_lhs (gs, t);
3279 gimplify_seq_add_stmt (seq_p, gs);
3281 *expr_p = build_simple_mem_ref (t);
3282 return GS_ALL_DONE;
3285 gimplify_seq_add_stmt (seq_p, gs);
3286 *expr_p = NULL;
3287 return GS_ALL_DONE;
3290 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3291 a call to __builtin_memset. In this case we know that the RHS is
3292 a CONSTRUCTOR with an empty element list. */
3294 static enum gimplify_status
3295 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3296 gimple_seq *seq_p)
3298 tree t, from, to, to_ptr;
3299 gcall *gs;
3300 location_t loc = EXPR_LOCATION (*expr_p);
3302 /* Assert our assumptions, to abort instead of producing wrong code
3303 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3304 not be immediately exposed. */
3305 from = TREE_OPERAND (*expr_p, 1);
3306 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3307 from = TREE_OPERAND (from, 0);
3309 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3310 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3312 /* Now proceed. */
3313 to = TREE_OPERAND (*expr_p, 0);
3315 to_ptr = build_fold_addr_expr_loc (loc, to);
3316 gimplify_arg (&to_ptr, seq_p, loc);
3317 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3319 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3321 if (want_value)
3323 /* tmp = memset() */
3324 t = create_tmp_var (TREE_TYPE (to_ptr));
3325 gimple_call_set_lhs (gs, t);
3326 gimplify_seq_add_stmt (seq_p, gs);
3328 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3329 return GS_ALL_DONE;
3332 gimplify_seq_add_stmt (seq_p, gs);
3333 *expr_p = NULL;
3334 return GS_ALL_DONE;
3337 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3338 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3339 assignment. Return non-null if we detect a potential overlap. */
3341 struct gimplify_init_ctor_preeval_data
3343 /* The base decl of the lhs object. May be NULL, in which case we
3344 have to assume the lhs is indirect. */
3345 tree lhs_base_decl;
3347 /* The alias set of the lhs object. */
3348 alias_set_type lhs_alias_set;
3351 static tree
3352 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3354 struct gimplify_init_ctor_preeval_data *data
3355 = (struct gimplify_init_ctor_preeval_data *) xdata;
3356 tree t = *tp;
3358 /* If we find the base object, obviously we have overlap. */
3359 if (data->lhs_base_decl == t)
3360 return t;
3362 /* If the constructor component is indirect, determine if we have a
3363 potential overlap with the lhs. The only bits of information we
3364 have to go on at this point are addressability and alias sets. */
3365 if ((INDIRECT_REF_P (t)
3366 || TREE_CODE (t) == MEM_REF)
3367 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3368 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3369 return t;
3371 /* If the constructor component is a call, determine if it can hide a
3372 potential overlap with the lhs through an INDIRECT_REF like above.
3373 ??? Ugh - this is completely broken. In fact this whole analysis
3374 doesn't look conservative. */
3375 if (TREE_CODE (t) == CALL_EXPR)
3377 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3379 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3380 if (POINTER_TYPE_P (TREE_VALUE (type))
3381 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3382 && alias_sets_conflict_p (data->lhs_alias_set,
3383 get_alias_set
3384 (TREE_TYPE (TREE_VALUE (type)))))
3385 return t;
3388 if (IS_TYPE_OR_DECL_P (t))
3389 *walk_subtrees = 0;
3390 return NULL;
3393 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3394 force values that overlap with the lhs (as described by *DATA)
3395 into temporaries. */
3397 static void
3398 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3399 struct gimplify_init_ctor_preeval_data *data)
3401 enum gimplify_status one;
3403 /* If the value is constant, then there's nothing to pre-evaluate. */
3404 if (TREE_CONSTANT (*expr_p))
3406 /* Ensure it does not have side effects, it might contain a reference to
3407 the object we're initializing. */
3408 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3409 return;
3412 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3413 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3414 return;
3416 /* Recurse for nested constructors. */
3417 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3419 unsigned HOST_WIDE_INT ix;
3420 constructor_elt *ce;
3421 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3423 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3424 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3426 return;
3429 /* If this is a variable sized type, we must remember the size. */
3430 maybe_with_size_expr (expr_p);
3432 /* Gimplify the constructor element to something appropriate for the rhs
3433 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3434 the gimplifier will consider this a store to memory. Doing this
3435 gimplification now means that we won't have to deal with complicated
3436 language-specific trees, nor trees like SAVE_EXPR that can induce
3437 exponential search behavior. */
3438 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3439 if (one == GS_ERROR)
3441 *expr_p = NULL;
3442 return;
3445 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3446 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3447 always be true for all scalars, since is_gimple_mem_rhs insists on a
3448 temporary variable for them. */
3449 if (DECL_P (*expr_p))
3450 return;
3452 /* If this is of variable size, we have no choice but to assume it doesn't
3453 overlap since we can't make a temporary for it. */
3454 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3455 return;
3457 /* Otherwise, we must search for overlap ... */
3458 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3459 return;
3461 /* ... and if found, force the value into a temporary. */
3462 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3465 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3466 a RANGE_EXPR in a CONSTRUCTOR for an array.
3468 var = lower;
3469 loop_entry:
3470 object[var] = value;
3471 if (var == upper)
3472 goto loop_exit;
3473 var = var + 1;
3474 goto loop_entry;
3475 loop_exit:
3477 We increment var _after_ the loop exit check because we might otherwise
3478 fail if upper == TYPE_MAX_VALUE (type for upper).
3480 Note that we never have to deal with SAVE_EXPRs here, because this has
3481 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3483 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3484 gimple_seq *, bool);
3486 static void
3487 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3488 tree value, tree array_elt_type,
3489 gimple_seq *pre_p, bool cleared)
3491 tree loop_entry_label, loop_exit_label, fall_thru_label;
3492 tree var, var_type, cref, tmp;
3494 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3495 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3496 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3498 /* Create and initialize the index variable. */
3499 var_type = TREE_TYPE (upper);
3500 var = create_tmp_var (var_type);
3501 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3503 /* Add the loop entry label. */
3504 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3506 /* Build the reference. */
3507 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3508 var, NULL_TREE, NULL_TREE);
3510 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3511 the store. Otherwise just assign value to the reference. */
3513 if (TREE_CODE (value) == CONSTRUCTOR)
3514 /* NB we might have to call ourself recursively through
3515 gimplify_init_ctor_eval if the value is a constructor. */
3516 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3517 pre_p, cleared);
3518 else
3519 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3521 /* We exit the loop when the index var is equal to the upper bound. */
3522 gimplify_seq_add_stmt (pre_p,
3523 gimple_build_cond (EQ_EXPR, var, upper,
3524 loop_exit_label, fall_thru_label));
3526 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3528 /* Otherwise, increment the index var... */
3529 tmp = build2 (PLUS_EXPR, var_type, var,
3530 fold_convert (var_type, integer_one_node));
3531 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3533 /* ...and jump back to the loop entry. */
3534 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3536 /* Add the loop exit label. */
3537 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3540 /* Return true if FDECL is accessing a field that is zero sized. */
3542 static bool
3543 zero_sized_field_decl (const_tree fdecl)
3545 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3546 && integer_zerop (DECL_SIZE (fdecl)))
3547 return true;
3548 return false;
3551 /* Return true if TYPE is zero sized. */
3553 static bool
3554 zero_sized_type (const_tree type)
3556 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3557 && integer_zerop (TYPE_SIZE (type)))
3558 return true;
3559 return false;
3562 /* A subroutine of gimplify_init_constructor. Generate individual
3563 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3564 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3565 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3566 zeroed first. */
3568 static void
3569 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3570 gimple_seq *pre_p, bool cleared)
3572 tree array_elt_type = NULL;
3573 unsigned HOST_WIDE_INT ix;
3574 tree purpose, value;
3576 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3577 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3579 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3581 tree cref;
3583 /* NULL values are created above for gimplification errors. */
3584 if (value == NULL)
3585 continue;
3587 if (cleared && initializer_zerop (value))
3588 continue;
3590 /* ??? Here's to hoping the front end fills in all of the indices,
3591 so we don't have to figure out what's missing ourselves. */
3592 gcc_assert (purpose);
3594 /* Skip zero-sized fields, unless value has side-effects. This can
3595 happen with calls to functions returning a zero-sized type, which
3596 we shouldn't discard. As a number of downstream passes don't
3597 expect sets of zero-sized fields, we rely on the gimplification of
3598 the MODIFY_EXPR we make below to drop the assignment statement. */
3599 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3600 continue;
3602 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3603 whole range. */
3604 if (TREE_CODE (purpose) == RANGE_EXPR)
3606 tree lower = TREE_OPERAND (purpose, 0);
3607 tree upper = TREE_OPERAND (purpose, 1);
3609 /* If the lower bound is equal to upper, just treat it as if
3610 upper was the index. */
3611 if (simple_cst_equal (lower, upper))
3612 purpose = upper;
3613 else
3615 gimplify_init_ctor_eval_range (object, lower, upper, value,
3616 array_elt_type, pre_p, cleared);
3617 continue;
3621 if (array_elt_type)
3623 /* Do not use bitsizetype for ARRAY_REF indices. */
3624 if (TYPE_DOMAIN (TREE_TYPE (object)))
3625 purpose
3626 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3627 purpose);
3628 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3629 purpose, NULL_TREE, NULL_TREE);
3631 else
3633 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3634 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3635 unshare_expr (object), purpose, NULL_TREE);
3638 if (TREE_CODE (value) == CONSTRUCTOR
3639 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3640 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3641 pre_p, cleared);
3642 else
3644 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3645 gimplify_and_add (init, pre_p);
3646 ggc_free (init);
3651 /* Return the appropriate RHS predicate for this LHS. */
3653 gimple_predicate
3654 rhs_predicate_for (tree lhs)
3656 if (is_gimple_reg (lhs))
3657 return is_gimple_reg_rhs_or_call;
3658 else
3659 return is_gimple_mem_rhs_or_call;
3662 /* Gimplify a C99 compound literal expression. This just means adding
3663 the DECL_EXPR before the current statement and using its anonymous
3664 decl instead. */
3666 static enum gimplify_status
3667 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3668 bool (*gimple_test_f) (tree),
3669 fallback_t fallback)
3671 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3672 tree decl = DECL_EXPR_DECL (decl_s);
3673 tree init = DECL_INITIAL (decl);
3674 /* Mark the decl as addressable if the compound literal
3675 expression is addressable now, otherwise it is marked too late
3676 after we gimplify the initialization expression. */
3677 if (TREE_ADDRESSABLE (*expr_p))
3678 TREE_ADDRESSABLE (decl) = 1;
3679 /* Otherwise, if we don't need an lvalue and have a literal directly
3680 substitute it. Check if it matches the gimple predicate, as
3681 otherwise we'd generate a new temporary, and we can as well just
3682 use the decl we already have. */
3683 else if (!TREE_ADDRESSABLE (decl)
3684 && init
3685 && (fallback & fb_lvalue) == 0
3686 && gimple_test_f (init))
3688 *expr_p = init;
3689 return GS_OK;
3692 /* Preliminarily mark non-addressed complex variables as eligible
3693 for promotion to gimple registers. We'll transform their uses
3694 as we find them. */
3695 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3696 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3697 && !TREE_THIS_VOLATILE (decl)
3698 && !needs_to_live_in_memory (decl))
3699 DECL_GIMPLE_REG_P (decl) = 1;
3701 /* If the decl is not addressable, then it is being used in some
3702 expression or on the right hand side of a statement, and it can
3703 be put into a readonly data section. */
3704 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3705 TREE_READONLY (decl) = 1;
3707 /* This decl isn't mentioned in the enclosing block, so add it to the
3708 list of temps. FIXME it seems a bit of a kludge to say that
3709 anonymous artificial vars aren't pushed, but everything else is. */
3710 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3711 gimple_add_tmp_var (decl);
3713 gimplify_and_add (decl_s, pre_p);
3714 *expr_p = decl;
3715 return GS_OK;
3718 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3719 return a new CONSTRUCTOR if something changed. */
3721 static tree
3722 optimize_compound_literals_in_ctor (tree orig_ctor)
3724 tree ctor = orig_ctor;
3725 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3726 unsigned int idx, num = vec_safe_length (elts);
3728 for (idx = 0; idx < num; idx++)
3730 tree value = (*elts)[idx].value;
3731 tree newval = value;
3732 if (TREE_CODE (value) == CONSTRUCTOR)
3733 newval = optimize_compound_literals_in_ctor (value);
3734 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3736 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3737 tree decl = DECL_EXPR_DECL (decl_s);
3738 tree init = DECL_INITIAL (decl);
3740 if (!TREE_ADDRESSABLE (value)
3741 && !TREE_ADDRESSABLE (decl)
3742 && init
3743 && TREE_CODE (init) == CONSTRUCTOR)
3744 newval = optimize_compound_literals_in_ctor (init);
3746 if (newval == value)
3747 continue;
3749 if (ctor == orig_ctor)
3751 ctor = copy_node (orig_ctor);
3752 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3753 elts = CONSTRUCTOR_ELTS (ctor);
3755 (*elts)[idx].value = newval;
3757 return ctor;
3760 /* A subroutine of gimplify_modify_expr. Break out elements of a
3761 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3763 Note that we still need to clear any elements that don't have explicit
3764 initializers, so if not all elements are initialized we keep the
3765 original MODIFY_EXPR, we just remove all of the constructor elements.
3767 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3768 GS_ERROR if we would have to create a temporary when gimplifying
3769 this constructor. Otherwise, return GS_OK.
3771 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3773 static enum gimplify_status
3774 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3775 bool want_value, bool notify_temp_creation)
3777 tree object, ctor, type;
3778 enum gimplify_status ret;
3779 vec<constructor_elt, va_gc> *elts;
3781 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3783 if (!notify_temp_creation)
3785 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3786 is_gimple_lvalue, fb_lvalue);
3787 if (ret == GS_ERROR)
3788 return ret;
3791 object = TREE_OPERAND (*expr_p, 0);
3792 ctor = TREE_OPERAND (*expr_p, 1) =
3793 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3794 type = TREE_TYPE (ctor);
3795 elts = CONSTRUCTOR_ELTS (ctor);
3796 ret = GS_ALL_DONE;
3798 switch (TREE_CODE (type))
3800 case RECORD_TYPE:
3801 case UNION_TYPE:
3802 case QUAL_UNION_TYPE:
3803 case ARRAY_TYPE:
3805 struct gimplify_init_ctor_preeval_data preeval_data;
3806 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3807 bool cleared, complete_p, valid_const_initializer;
3809 /* Aggregate types must lower constructors to initialization of
3810 individual elements. The exception is that a CONSTRUCTOR node
3811 with no elements indicates zero-initialization of the whole. */
3812 if (vec_safe_is_empty (elts))
3814 if (notify_temp_creation)
3815 return GS_OK;
3816 break;
3819 /* Fetch information about the constructor to direct later processing.
3820 We might want to make static versions of it in various cases, and
3821 can only do so if it known to be a valid constant initializer. */
3822 valid_const_initializer
3823 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3824 &num_ctor_elements, &complete_p);
3826 /* If a const aggregate variable is being initialized, then it
3827 should never be a lose to promote the variable to be static. */
3828 if (valid_const_initializer
3829 && num_nonzero_elements > 1
3830 && TREE_READONLY (object)
3831 && TREE_CODE (object) == VAR_DECL
3832 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3834 if (notify_temp_creation)
3835 return GS_ERROR;
3836 DECL_INITIAL (object) = ctor;
3837 TREE_STATIC (object) = 1;
3838 if (!DECL_NAME (object))
3839 DECL_NAME (object) = create_tmp_var_name ("C");
3840 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3842 /* ??? C++ doesn't automatically append a .<number> to the
3843 assembler name, and even when it does, it looks at FE private
3844 data structures to figure out what that number should be,
3845 which are not set for this variable. I suppose this is
3846 important for local statics for inline functions, which aren't
3847 "local" in the object file sense. So in order to get a unique
3848 TU-local symbol, we must invoke the lhd version now. */
3849 lhd_set_decl_assembler_name (object);
3851 *expr_p = NULL_TREE;
3852 break;
3855 /* If there are "lots" of initialized elements, even discounting
3856 those that are not address constants (and thus *must* be
3857 computed at runtime), then partition the constructor into
3858 constant and non-constant parts. Block copy the constant
3859 parts in, then generate code for the non-constant parts. */
3860 /* TODO. There's code in cp/typeck.c to do this. */
3862 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3863 /* store_constructor will ignore the clearing of variable-sized
3864 objects. Initializers for such objects must explicitly set
3865 every field that needs to be set. */
3866 cleared = false;
3867 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3868 /* If the constructor isn't complete, clear the whole object
3869 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3871 ??? This ought not to be needed. For any element not present
3872 in the initializer, we should simply set them to zero. Except
3873 we'd need to *find* the elements that are not present, and that
3874 requires trickery to avoid quadratic compile-time behavior in
3875 large cases or excessive memory use in small cases. */
3876 cleared = true;
3877 else if (num_ctor_elements - num_nonzero_elements
3878 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3879 && num_nonzero_elements < num_ctor_elements / 4)
3880 /* If there are "lots" of zeros, it's more efficient to clear
3881 the memory and then set the nonzero elements. */
3882 cleared = true;
3883 else
3884 cleared = false;
3886 /* If there are "lots" of initialized elements, and all of them
3887 are valid address constants, then the entire initializer can
3888 be dropped to memory, and then memcpy'd out. Don't do this
3889 for sparse arrays, though, as it's more efficient to follow
3890 the standard CONSTRUCTOR behavior of memset followed by
3891 individual element initialization. Also don't do this for small
3892 all-zero initializers (which aren't big enough to merit
3893 clearing), and don't try to make bitwise copies of
3894 TREE_ADDRESSABLE types.
3896 We cannot apply such transformation when compiling chkp static
3897 initializer because creation of initializer image in the memory
3898 will require static initialization of bounds for it. It should
3899 result in another gimplification of similar initializer and we
3900 may fall into infinite loop. */
3901 if (valid_const_initializer
3902 && !(cleared || num_nonzero_elements == 0)
3903 && !TREE_ADDRESSABLE (type)
3904 && (!current_function_decl
3905 || !lookup_attribute ("chkp ctor",
3906 DECL_ATTRIBUTES (current_function_decl))))
3908 HOST_WIDE_INT size = int_size_in_bytes (type);
3909 unsigned int align;
3911 /* ??? We can still get unbounded array types, at least
3912 from the C++ front end. This seems wrong, but attempt
3913 to work around it for now. */
3914 if (size < 0)
3916 size = int_size_in_bytes (TREE_TYPE (object));
3917 if (size >= 0)
3918 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3921 /* Find the maximum alignment we can assume for the object. */
3922 /* ??? Make use of DECL_OFFSET_ALIGN. */
3923 if (DECL_P (object))
3924 align = DECL_ALIGN (object);
3925 else
3926 align = TYPE_ALIGN (type);
3928 /* Do a block move either if the size is so small as to make
3929 each individual move a sub-unit move on average, or if it
3930 is so large as to make individual moves inefficient. */
3931 if (size > 0
3932 && num_nonzero_elements > 1
3933 && (size < num_nonzero_elements
3934 || !can_move_by_pieces (size, align)))
3936 if (notify_temp_creation)
3937 return GS_ERROR;
3939 walk_tree (&ctor, force_labels_r, NULL, NULL);
3940 ctor = tree_output_constant_def (ctor);
3941 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3942 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3943 TREE_OPERAND (*expr_p, 1) = ctor;
3945 /* This is no longer an assignment of a CONSTRUCTOR, but
3946 we still may have processing to do on the LHS. So
3947 pretend we didn't do anything here to let that happen. */
3948 return GS_UNHANDLED;
3952 /* If the target is volatile, we have non-zero elements and more than
3953 one field to assign, initialize the target from a temporary. */
3954 if (TREE_THIS_VOLATILE (object)
3955 && !TREE_ADDRESSABLE (type)
3956 && num_nonzero_elements > 0
3957 && vec_safe_length (elts) > 1)
3959 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3960 TREE_OPERAND (*expr_p, 0) = temp;
3961 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3962 *expr_p,
3963 build2 (MODIFY_EXPR, void_type_node,
3964 object, temp));
3965 return GS_OK;
3968 if (notify_temp_creation)
3969 return GS_OK;
3971 /* If there are nonzero elements and if needed, pre-evaluate to capture
3972 elements overlapping with the lhs into temporaries. We must do this
3973 before clearing to fetch the values before they are zeroed-out. */
3974 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3976 preeval_data.lhs_base_decl = get_base_address (object);
3977 if (!DECL_P (preeval_data.lhs_base_decl))
3978 preeval_data.lhs_base_decl = NULL;
3979 preeval_data.lhs_alias_set = get_alias_set (object);
3981 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3982 pre_p, post_p, &preeval_data);
3985 bool ctor_has_side_effects_p
3986 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
3988 if (cleared)
3990 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3991 Note that we still have to gimplify, in order to handle the
3992 case of variable sized types. Avoid shared tree structures. */
3993 CONSTRUCTOR_ELTS (ctor) = NULL;
3994 TREE_SIDE_EFFECTS (ctor) = 0;
3995 object = unshare_expr (object);
3996 gimplify_stmt (expr_p, pre_p);
3999 /* If we have not block cleared the object, or if there are nonzero
4000 elements in the constructor, or if the constructor has side effects,
4001 add assignments to the individual scalar fields of the object. */
4002 if (!cleared
4003 || num_nonzero_elements > 0
4004 || ctor_has_side_effects_p)
4005 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4007 *expr_p = NULL_TREE;
4009 break;
4011 case COMPLEX_TYPE:
4013 tree r, i;
4015 if (notify_temp_creation)
4016 return GS_OK;
4018 /* Extract the real and imaginary parts out of the ctor. */
4019 gcc_assert (elts->length () == 2);
4020 r = (*elts)[0].value;
4021 i = (*elts)[1].value;
4022 if (r == NULL || i == NULL)
4024 tree zero = build_zero_cst (TREE_TYPE (type));
4025 if (r == NULL)
4026 r = zero;
4027 if (i == NULL)
4028 i = zero;
4031 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4032 represent creation of a complex value. */
4033 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4035 ctor = build_complex (type, r, i);
4036 TREE_OPERAND (*expr_p, 1) = ctor;
4038 else
4040 ctor = build2 (COMPLEX_EXPR, type, r, i);
4041 TREE_OPERAND (*expr_p, 1) = ctor;
4042 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4043 pre_p,
4044 post_p,
4045 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4046 fb_rvalue);
4049 break;
4051 case VECTOR_TYPE:
4053 unsigned HOST_WIDE_INT ix;
4054 constructor_elt *ce;
4056 if (notify_temp_creation)
4057 return GS_OK;
4059 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4060 if (TREE_CONSTANT (ctor))
4062 bool constant_p = true;
4063 tree value;
4065 /* Even when ctor is constant, it might contain non-*_CST
4066 elements, such as addresses or trapping values like
4067 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4068 in VECTOR_CST nodes. */
4069 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4070 if (!CONSTANT_CLASS_P (value))
4072 constant_p = false;
4073 break;
4076 if (constant_p)
4078 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4079 break;
4082 TREE_CONSTANT (ctor) = 0;
4085 /* Vector types use CONSTRUCTOR all the way through gimple
4086 compilation as a general initializer. */
4087 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4089 enum gimplify_status tret;
4090 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4091 fb_rvalue);
4092 if (tret == GS_ERROR)
4093 ret = GS_ERROR;
4095 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4096 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4098 break;
4100 default:
4101 /* So how did we get a CONSTRUCTOR for a scalar type? */
4102 gcc_unreachable ();
4105 if (ret == GS_ERROR)
4106 return GS_ERROR;
4107 else if (want_value)
4109 *expr_p = object;
4110 return GS_OK;
4112 else
4114 /* If we have gimplified both sides of the initializer but have
4115 not emitted an assignment, do so now. */
4116 if (*expr_p)
4118 tree lhs = TREE_OPERAND (*expr_p, 0);
4119 tree rhs = TREE_OPERAND (*expr_p, 1);
4120 gassign *init = gimple_build_assign (lhs, rhs);
4121 gimplify_seq_add_stmt (pre_p, init);
4122 *expr_p = NULL;
4125 return GS_ALL_DONE;
4129 /* Given a pointer value OP0, return a simplified version of an
4130 indirection through OP0, or NULL_TREE if no simplification is
4131 possible. This may only be applied to a rhs of an expression.
4132 Note that the resulting type may be different from the type pointed
4133 to in the sense that it is still compatible from the langhooks
4134 point of view. */
4136 static tree
4137 gimple_fold_indirect_ref_rhs (tree t)
4139 return gimple_fold_indirect_ref (t);
4142 /* Subroutine of gimplify_modify_expr to do simplifications of
4143 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4144 something changes. */
4146 static enum gimplify_status
4147 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4148 gimple_seq *pre_p, gimple_seq *post_p,
4149 bool want_value)
4151 enum gimplify_status ret = GS_UNHANDLED;
4152 bool changed;
4156 changed = false;
4157 switch (TREE_CODE (*from_p))
4159 case VAR_DECL:
4160 /* If we're assigning from a read-only variable initialized with
4161 a constructor, do the direct assignment from the constructor,
4162 but only if neither source nor target are volatile since this
4163 latter assignment might end up being done on a per-field basis. */
4164 if (DECL_INITIAL (*from_p)
4165 && TREE_READONLY (*from_p)
4166 && !TREE_THIS_VOLATILE (*from_p)
4167 && !TREE_THIS_VOLATILE (*to_p)
4168 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4170 tree old_from = *from_p;
4171 enum gimplify_status subret;
4173 /* Move the constructor into the RHS. */
4174 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4176 /* Let's see if gimplify_init_constructor will need to put
4177 it in memory. */
4178 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4179 false, true);
4180 if (subret == GS_ERROR)
4182 /* If so, revert the change. */
4183 *from_p = old_from;
4185 else
4187 ret = GS_OK;
4188 changed = true;
4191 break;
4192 case INDIRECT_REF:
4194 /* If we have code like
4196 *(const A*)(A*)&x
4198 where the type of "x" is a (possibly cv-qualified variant
4199 of "A"), treat the entire expression as identical to "x".
4200 This kind of code arises in C++ when an object is bound
4201 to a const reference, and if "x" is a TARGET_EXPR we want
4202 to take advantage of the optimization below. */
4203 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4204 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4205 if (t)
4207 if (TREE_THIS_VOLATILE (t) != volatile_p)
4209 if (DECL_P (t))
4210 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4211 build_fold_addr_expr (t));
4212 if (REFERENCE_CLASS_P (t))
4213 TREE_THIS_VOLATILE (t) = volatile_p;
4215 *from_p = t;
4216 ret = GS_OK;
4217 changed = true;
4219 break;
4222 case TARGET_EXPR:
4224 /* If we are initializing something from a TARGET_EXPR, strip the
4225 TARGET_EXPR and initialize it directly, if possible. This can't
4226 be done if the initializer is void, since that implies that the
4227 temporary is set in some non-trivial way.
4229 ??? What about code that pulls out the temp and uses it
4230 elsewhere? I think that such code never uses the TARGET_EXPR as
4231 an initializer. If I'm wrong, we'll die because the temp won't
4232 have any RTL. In that case, I guess we'll need to replace
4233 references somehow. */
4234 tree init = TARGET_EXPR_INITIAL (*from_p);
4236 if (init
4237 && !VOID_TYPE_P (TREE_TYPE (init)))
4239 *from_p = init;
4240 ret = GS_OK;
4241 changed = true;
4244 break;
4246 case COMPOUND_EXPR:
4247 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4248 caught. */
4249 gimplify_compound_expr (from_p, pre_p, true);
4250 ret = GS_OK;
4251 changed = true;
4252 break;
4254 case CONSTRUCTOR:
4255 /* If we already made some changes, let the front end have a
4256 crack at this before we break it down. */
4257 if (ret != GS_UNHANDLED)
4258 break;
4259 /* If we're initializing from a CONSTRUCTOR, break this into
4260 individual MODIFY_EXPRs. */
4261 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4262 false);
4264 case COND_EXPR:
4265 /* If we're assigning to a non-register type, push the assignment
4266 down into the branches. This is mandatory for ADDRESSABLE types,
4267 since we cannot generate temporaries for such, but it saves a
4268 copy in other cases as well. */
4269 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4271 /* This code should mirror the code in gimplify_cond_expr. */
4272 enum tree_code code = TREE_CODE (*expr_p);
4273 tree cond = *from_p;
4274 tree result = *to_p;
4276 ret = gimplify_expr (&result, pre_p, post_p,
4277 is_gimple_lvalue, fb_lvalue);
4278 if (ret != GS_ERROR)
4279 ret = GS_OK;
4281 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4282 TREE_OPERAND (cond, 1)
4283 = build2 (code, void_type_node, result,
4284 TREE_OPERAND (cond, 1));
4285 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4286 TREE_OPERAND (cond, 2)
4287 = build2 (code, void_type_node, unshare_expr (result),
4288 TREE_OPERAND (cond, 2));
4290 TREE_TYPE (cond) = void_type_node;
4291 recalculate_side_effects (cond);
4293 if (want_value)
4295 gimplify_and_add (cond, pre_p);
4296 *expr_p = unshare_expr (result);
4298 else
4299 *expr_p = cond;
4300 return ret;
4302 break;
4304 case CALL_EXPR:
4305 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4306 return slot so that we don't generate a temporary. */
4307 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4308 && aggregate_value_p (*from_p, *from_p))
4310 bool use_target;
4312 if (!(rhs_predicate_for (*to_p))(*from_p))
4313 /* If we need a temporary, *to_p isn't accurate. */
4314 use_target = false;
4315 /* It's OK to use the return slot directly unless it's an NRV. */
4316 else if (TREE_CODE (*to_p) == RESULT_DECL
4317 && DECL_NAME (*to_p) == NULL_TREE
4318 && needs_to_live_in_memory (*to_p))
4319 use_target = true;
4320 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4321 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4322 /* Don't force regs into memory. */
4323 use_target = false;
4324 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4325 /* It's OK to use the target directly if it's being
4326 initialized. */
4327 use_target = true;
4328 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4329 != INTEGER_CST)
4330 /* Always use the target and thus RSO for variable-sized types.
4331 GIMPLE cannot deal with a variable-sized assignment
4332 embedded in a call statement. */
4333 use_target = true;
4334 else if (TREE_CODE (*to_p) != SSA_NAME
4335 && (!is_gimple_variable (*to_p)
4336 || needs_to_live_in_memory (*to_p)))
4337 /* Don't use the original target if it's already addressable;
4338 if its address escapes, and the called function uses the
4339 NRV optimization, a conforming program could see *to_p
4340 change before the called function returns; see c++/19317.
4341 When optimizing, the return_slot pass marks more functions
4342 as safe after we have escape info. */
4343 use_target = false;
4344 else
4345 use_target = true;
4347 if (use_target)
4349 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4350 mark_addressable (*to_p);
4353 break;
4355 case WITH_SIZE_EXPR:
4356 /* Likewise for calls that return an aggregate of non-constant size,
4357 since we would not be able to generate a temporary at all. */
4358 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4360 *from_p = TREE_OPERAND (*from_p, 0);
4361 /* We don't change ret in this case because the
4362 WITH_SIZE_EXPR might have been added in
4363 gimplify_modify_expr, so returning GS_OK would lead to an
4364 infinite loop. */
4365 changed = true;
4367 break;
4369 /* If we're initializing from a container, push the initialization
4370 inside it. */
4371 case CLEANUP_POINT_EXPR:
4372 case BIND_EXPR:
4373 case STATEMENT_LIST:
4375 tree wrap = *from_p;
4376 tree t;
4378 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4379 fb_lvalue);
4380 if (ret != GS_ERROR)
4381 ret = GS_OK;
4383 t = voidify_wrapper_expr (wrap, *expr_p);
4384 gcc_assert (t == *expr_p);
4386 if (want_value)
4388 gimplify_and_add (wrap, pre_p);
4389 *expr_p = unshare_expr (*to_p);
4391 else
4392 *expr_p = wrap;
4393 return GS_OK;
4396 case COMPOUND_LITERAL_EXPR:
4398 tree complit = TREE_OPERAND (*expr_p, 1);
4399 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4400 tree decl = DECL_EXPR_DECL (decl_s);
4401 tree init = DECL_INITIAL (decl);
4403 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4404 into struct T x = { 0, 1, 2 } if the address of the
4405 compound literal has never been taken. */
4406 if (!TREE_ADDRESSABLE (complit)
4407 && !TREE_ADDRESSABLE (decl)
4408 && init)
4410 *expr_p = copy_node (*expr_p);
4411 TREE_OPERAND (*expr_p, 1) = init;
4412 return GS_OK;
4416 default:
4417 break;
4420 while (changed);
4422 return ret;
4426 /* Return true if T looks like a valid GIMPLE statement. */
4428 static bool
4429 is_gimple_stmt (tree t)
4431 const enum tree_code code = TREE_CODE (t);
4433 switch (code)
4435 case NOP_EXPR:
4436 /* The only valid NOP_EXPR is the empty statement. */
4437 return IS_EMPTY_STMT (t);
4439 case BIND_EXPR:
4440 case COND_EXPR:
4441 /* These are only valid if they're void. */
4442 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4444 case SWITCH_EXPR:
4445 case GOTO_EXPR:
4446 case RETURN_EXPR:
4447 case LABEL_EXPR:
4448 case CASE_LABEL_EXPR:
4449 case TRY_CATCH_EXPR:
4450 case TRY_FINALLY_EXPR:
4451 case EH_FILTER_EXPR:
4452 case CATCH_EXPR:
4453 case ASM_EXPR:
4454 case STATEMENT_LIST:
4455 case OACC_PARALLEL:
4456 case OACC_KERNELS:
4457 case OACC_DATA:
4458 case OACC_HOST_DATA:
4459 case OACC_DECLARE:
4460 case OACC_UPDATE:
4461 case OACC_ENTER_DATA:
4462 case OACC_EXIT_DATA:
4463 case OACC_CACHE:
4464 case OMP_PARALLEL:
4465 case OMP_FOR:
4466 case OMP_SIMD:
4467 case CILK_SIMD:
4468 case OMP_DISTRIBUTE:
4469 case OACC_LOOP:
4470 case OMP_SECTIONS:
4471 case OMP_SECTION:
4472 case OMP_SINGLE:
4473 case OMP_MASTER:
4474 case OMP_TASKGROUP:
4475 case OMP_ORDERED:
4476 case OMP_CRITICAL:
4477 case OMP_TASK:
4478 /* These are always void. */
4479 return true;
4481 case CALL_EXPR:
4482 case MODIFY_EXPR:
4483 case PREDICT_EXPR:
4484 /* These are valid regardless of their type. */
4485 return true;
4487 default:
4488 return false;
4493 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4494 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4495 DECL_GIMPLE_REG_P set.
4497 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4498 other, unmodified part of the complex object just before the total store.
4499 As a consequence, if the object is still uninitialized, an undefined value
4500 will be loaded into a register, which may result in a spurious exception
4501 if the register is floating-point and the value happens to be a signaling
4502 NaN for example. Then the fully-fledged complex operations lowering pass
4503 followed by a DCE pass are necessary in order to fix things up. */
4505 static enum gimplify_status
4506 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4507 bool want_value)
4509 enum tree_code code, ocode;
4510 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4512 lhs = TREE_OPERAND (*expr_p, 0);
4513 rhs = TREE_OPERAND (*expr_p, 1);
4514 code = TREE_CODE (lhs);
4515 lhs = TREE_OPERAND (lhs, 0);
4517 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4518 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4519 TREE_NO_WARNING (other) = 1;
4520 other = get_formal_tmp_var (other, pre_p);
4522 realpart = code == REALPART_EXPR ? rhs : other;
4523 imagpart = code == REALPART_EXPR ? other : rhs;
4525 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4526 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4527 else
4528 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4530 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4531 *expr_p = (want_value) ? rhs : NULL_TREE;
4533 return GS_ALL_DONE;
4536 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4538 modify_expr
4539 : varname '=' rhs
4540 | '*' ID '=' rhs
4542 PRE_P points to the list where side effects that must happen before
4543 *EXPR_P should be stored.
4545 POST_P points to the list where side effects that must happen after
4546 *EXPR_P should be stored.
4548 WANT_VALUE is nonzero iff we want to use the value of this expression
4549 in another expression. */
4551 static enum gimplify_status
4552 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4553 bool want_value)
4555 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4556 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4557 enum gimplify_status ret = GS_UNHANDLED;
4558 gimple assign;
4559 location_t loc = EXPR_LOCATION (*expr_p);
4560 gimple_stmt_iterator gsi;
4562 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4563 || TREE_CODE (*expr_p) == INIT_EXPR);
4565 /* Trying to simplify a clobber using normal logic doesn't work,
4566 so handle it here. */
4567 if (TREE_CLOBBER_P (*from_p))
4569 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4570 if (ret == GS_ERROR)
4571 return ret;
4572 gcc_assert (!want_value
4573 && (TREE_CODE (*to_p) == VAR_DECL
4574 || TREE_CODE (*to_p) == MEM_REF));
4575 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4576 *expr_p = NULL;
4577 return GS_ALL_DONE;
4580 /* Insert pointer conversions required by the middle-end that are not
4581 required by the frontend. This fixes middle-end type checking for
4582 for example gcc.dg/redecl-6.c. */
4583 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4585 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4586 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4587 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4590 /* See if any simplifications can be done based on what the RHS is. */
4591 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4592 want_value);
4593 if (ret != GS_UNHANDLED)
4594 return ret;
4596 /* For zero sized types only gimplify the left hand side and right hand
4597 side as statements and throw away the assignment. Do this after
4598 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4599 types properly. */
4600 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4602 gimplify_stmt (from_p, pre_p);
4603 gimplify_stmt (to_p, pre_p);
4604 *expr_p = NULL_TREE;
4605 return GS_ALL_DONE;
4608 /* If the value being copied is of variable width, compute the length
4609 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4610 before gimplifying any of the operands so that we can resolve any
4611 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4612 the size of the expression to be copied, not of the destination, so
4613 that is what we must do here. */
4614 maybe_with_size_expr (from_p);
4616 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4617 if (ret == GS_ERROR)
4618 return ret;
4620 /* As a special case, we have to temporarily allow for assignments
4621 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4622 a toplevel statement, when gimplifying the GENERIC expression
4623 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4624 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4626 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4627 prevent gimplify_expr from trying to create a new temporary for
4628 foo's LHS, we tell it that it should only gimplify until it
4629 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4630 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4631 and all we need to do here is set 'a' to be its LHS. */
4632 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4633 fb_rvalue);
4634 if (ret == GS_ERROR)
4635 return ret;
4637 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4638 size as argument to the the call. */
4639 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4641 tree call = TREE_OPERAND (*from_p, 0);
4642 tree vlasize = TREE_OPERAND (*from_p, 1);
4644 if (TREE_CODE (call) == CALL_EXPR
4645 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
4647 int nargs = call_expr_nargs (call);
4648 tree type = TREE_TYPE (call);
4649 tree ap = CALL_EXPR_ARG (call, 0);
4650 tree tag = CALL_EXPR_ARG (call, 1);
4651 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
4652 IFN_VA_ARG, type,
4653 nargs + 1, ap, tag,
4654 vlasize);
4655 tree *call_p = &(TREE_OPERAND (*from_p, 0));
4656 *call_p = newcall;
4660 /* Now see if the above changed *from_p to something we handle specially. */
4661 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4662 want_value);
4663 if (ret != GS_UNHANDLED)
4664 return ret;
4666 /* If we've got a variable sized assignment between two lvalues (i.e. does
4667 not involve a call), then we can make things a bit more straightforward
4668 by converting the assignment to memcpy or memset. */
4669 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4671 tree from = TREE_OPERAND (*from_p, 0);
4672 tree size = TREE_OPERAND (*from_p, 1);
4674 if (TREE_CODE (from) == CONSTRUCTOR)
4675 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4677 if (is_gimple_addressable (from))
4679 *from_p = from;
4680 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4681 pre_p);
4685 /* Transform partial stores to non-addressable complex variables into
4686 total stores. This allows us to use real instead of virtual operands
4687 for these variables, which improves optimization. */
4688 if ((TREE_CODE (*to_p) == REALPART_EXPR
4689 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4690 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4691 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4693 /* Try to alleviate the effects of the gimplification creating artificial
4694 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4695 make sure not to create DECL_DEBUG_EXPR links across functions. */
4696 if (!gimplify_ctxp->into_ssa
4697 && TREE_CODE (*from_p) == VAR_DECL
4698 && DECL_IGNORED_P (*from_p)
4699 && DECL_P (*to_p)
4700 && !DECL_IGNORED_P (*to_p)
4701 && decl_function_context (*to_p) == current_function_decl)
4703 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4704 DECL_NAME (*from_p)
4705 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4706 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4707 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4710 if (want_value && TREE_THIS_VOLATILE (*to_p))
4711 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4713 if (TREE_CODE (*from_p) == CALL_EXPR)
4715 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4716 instead of a GIMPLE_ASSIGN. */
4717 gcall *call_stmt;
4718 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4720 /* Gimplify internal functions created in the FEs. */
4721 int nargs = call_expr_nargs (*from_p), i;
4722 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4723 auto_vec<tree> vargs (nargs);
4725 for (i = 0; i < nargs; i++)
4727 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4728 EXPR_LOCATION (*from_p));
4729 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4731 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4732 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4734 else
4736 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4737 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4738 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4739 tree fndecl = get_callee_fndecl (*from_p);
4740 if (fndecl
4741 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4742 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4743 && call_expr_nargs (*from_p) == 3)
4744 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4745 CALL_EXPR_ARG (*from_p, 0),
4746 CALL_EXPR_ARG (*from_p, 1),
4747 CALL_EXPR_ARG (*from_p, 2));
4748 else
4750 call_stmt = gimple_build_call_from_tree (*from_p);
4751 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4754 notice_special_calls (call_stmt);
4755 if (!gimple_call_noreturn_p (call_stmt))
4756 gimple_call_set_lhs (call_stmt, *to_p);
4757 assign = call_stmt;
4759 else
4761 assign = gimple_build_assign (*to_p, *from_p);
4762 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4765 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4767 /* We should have got an SSA name from the start. */
4768 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4771 gimplify_seq_add_stmt (pre_p, assign);
4772 gsi = gsi_last (*pre_p);
4773 maybe_fold_stmt (&gsi);
4775 if (want_value)
4777 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4778 return GS_OK;
4780 else
4781 *expr_p = NULL;
4783 return GS_ALL_DONE;
4786 /* Gimplify a comparison between two variable-sized objects. Do this
4787 with a call to BUILT_IN_MEMCMP. */
4789 static enum gimplify_status
4790 gimplify_variable_sized_compare (tree *expr_p)
4792 location_t loc = EXPR_LOCATION (*expr_p);
4793 tree op0 = TREE_OPERAND (*expr_p, 0);
4794 tree op1 = TREE_OPERAND (*expr_p, 1);
4795 tree t, arg, dest, src, expr;
4797 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4798 arg = unshare_expr (arg);
4799 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4800 src = build_fold_addr_expr_loc (loc, op1);
4801 dest = build_fold_addr_expr_loc (loc, op0);
4802 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4803 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4805 expr
4806 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4807 SET_EXPR_LOCATION (expr, loc);
4808 *expr_p = expr;
4810 return GS_OK;
4813 /* Gimplify a comparison between two aggregate objects of integral scalar
4814 mode as a comparison between the bitwise equivalent scalar values. */
4816 static enum gimplify_status
4817 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4819 location_t loc = EXPR_LOCATION (*expr_p);
4820 tree op0 = TREE_OPERAND (*expr_p, 0);
4821 tree op1 = TREE_OPERAND (*expr_p, 1);
4823 tree type = TREE_TYPE (op0);
4824 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4826 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4827 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4829 *expr_p
4830 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4832 return GS_OK;
4835 /* Gimplify an expression sequence. This function gimplifies each
4836 expression and rewrites the original expression with the last
4837 expression of the sequence in GIMPLE form.
4839 PRE_P points to the list where the side effects for all the
4840 expressions in the sequence will be emitted.
4842 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4844 static enum gimplify_status
4845 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4847 tree t = *expr_p;
4851 tree *sub_p = &TREE_OPERAND (t, 0);
4853 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4854 gimplify_compound_expr (sub_p, pre_p, false);
4855 else
4856 gimplify_stmt (sub_p, pre_p);
4858 t = TREE_OPERAND (t, 1);
4860 while (TREE_CODE (t) == COMPOUND_EXPR);
4862 *expr_p = t;
4863 if (want_value)
4864 return GS_OK;
4865 else
4867 gimplify_stmt (expr_p, pre_p);
4868 return GS_ALL_DONE;
4872 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4873 gimplify. After gimplification, EXPR_P will point to a new temporary
4874 that holds the original value of the SAVE_EXPR node.
4876 PRE_P points to the list where side effects that must happen before
4877 *EXPR_P should be stored. */
4879 static enum gimplify_status
4880 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4882 enum gimplify_status ret = GS_ALL_DONE;
4883 tree val;
4885 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4886 val = TREE_OPERAND (*expr_p, 0);
4888 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4889 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4891 /* The operand may be a void-valued expression such as SAVE_EXPRs
4892 generated by the Java frontend for class initialization. It is
4893 being executed only for its side-effects. */
4894 if (TREE_TYPE (val) == void_type_node)
4896 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4897 is_gimple_stmt, fb_none);
4898 val = NULL;
4900 else
4901 val = get_initialized_tmp_var (val, pre_p, post_p);
4903 TREE_OPERAND (*expr_p, 0) = val;
4904 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4907 *expr_p = val;
4909 return ret;
4912 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4914 unary_expr
4915 : ...
4916 | '&' varname
4919 PRE_P points to the list where side effects that must happen before
4920 *EXPR_P should be stored.
4922 POST_P points to the list where side effects that must happen after
4923 *EXPR_P should be stored. */
4925 static enum gimplify_status
4926 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4928 tree expr = *expr_p;
4929 tree op0 = TREE_OPERAND (expr, 0);
4930 enum gimplify_status ret;
4931 location_t loc = EXPR_LOCATION (*expr_p);
4933 switch (TREE_CODE (op0))
4935 case INDIRECT_REF:
4936 do_indirect_ref:
4937 /* Check if we are dealing with an expression of the form '&*ptr'.
4938 While the front end folds away '&*ptr' into 'ptr', these
4939 expressions may be generated internally by the compiler (e.g.,
4940 builtins like __builtin_va_end). */
4941 /* Caution: the silent array decomposition semantics we allow for
4942 ADDR_EXPR means we can't always discard the pair. */
4943 /* Gimplification of the ADDR_EXPR operand may drop
4944 cv-qualification conversions, so make sure we add them if
4945 needed. */
4947 tree op00 = TREE_OPERAND (op0, 0);
4948 tree t_expr = TREE_TYPE (expr);
4949 tree t_op00 = TREE_TYPE (op00);
4951 if (!useless_type_conversion_p (t_expr, t_op00))
4952 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4953 *expr_p = op00;
4954 ret = GS_OK;
4956 break;
4958 case VIEW_CONVERT_EXPR:
4959 /* Take the address of our operand and then convert it to the type of
4960 this ADDR_EXPR.
4962 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4963 all clear. The impact of this transformation is even less clear. */
4965 /* If the operand is a useless conversion, look through it. Doing so
4966 guarantees that the ADDR_EXPR and its operand will remain of the
4967 same type. */
4968 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4969 op0 = TREE_OPERAND (op0, 0);
4971 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4972 build_fold_addr_expr_loc (loc,
4973 TREE_OPERAND (op0, 0)));
4974 ret = GS_OK;
4975 break;
4977 default:
4978 /* If we see a call to a declared builtin or see its address
4979 being taken (we can unify those cases here) then we can mark
4980 the builtin for implicit generation by GCC. */
4981 if (TREE_CODE (op0) == FUNCTION_DECL
4982 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
4983 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
4984 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
4986 /* We use fb_either here because the C frontend sometimes takes
4987 the address of a call that returns a struct; see
4988 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4989 the implied temporary explicit. */
4991 /* Make the operand addressable. */
4992 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4993 is_gimple_addressable, fb_either);
4994 if (ret == GS_ERROR)
4995 break;
4997 /* Then mark it. Beware that it may not be possible to do so directly
4998 if a temporary has been created by the gimplification. */
4999 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5001 op0 = TREE_OPERAND (expr, 0);
5003 /* For various reasons, the gimplification of the expression
5004 may have made a new INDIRECT_REF. */
5005 if (TREE_CODE (op0) == INDIRECT_REF)
5006 goto do_indirect_ref;
5008 mark_addressable (TREE_OPERAND (expr, 0));
5010 /* The FEs may end up building ADDR_EXPRs early on a decl with
5011 an incomplete type. Re-build ADDR_EXPRs in canonical form
5012 here. */
5013 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5014 *expr_p = build_fold_addr_expr (op0);
5016 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5017 recompute_tree_invariant_for_addr_expr (*expr_p);
5019 /* If we re-built the ADDR_EXPR add a conversion to the original type
5020 if required. */
5021 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5022 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5024 break;
5027 return ret;
5030 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5031 value; output operands should be a gimple lvalue. */
5033 static enum gimplify_status
5034 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5036 tree expr;
5037 int noutputs;
5038 const char **oconstraints;
5039 int i;
5040 tree link;
5041 const char *constraint;
5042 bool allows_mem, allows_reg, is_inout;
5043 enum gimplify_status ret, tret;
5044 gasm *stmt;
5045 vec<tree, va_gc> *inputs;
5046 vec<tree, va_gc> *outputs;
5047 vec<tree, va_gc> *clobbers;
5048 vec<tree, va_gc> *labels;
5049 tree link_next;
5051 expr = *expr_p;
5052 noutputs = list_length (ASM_OUTPUTS (expr));
5053 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5055 inputs = NULL;
5056 outputs = NULL;
5057 clobbers = NULL;
5058 labels = NULL;
5060 ret = GS_ALL_DONE;
5061 link_next = NULL_TREE;
5062 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5064 bool ok;
5065 size_t constraint_len;
5067 link_next = TREE_CHAIN (link);
5069 oconstraints[i]
5070 = constraint
5071 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5072 constraint_len = strlen (constraint);
5073 if (constraint_len == 0)
5074 continue;
5076 ok = parse_output_constraint (&constraint, i, 0, 0,
5077 &allows_mem, &allows_reg, &is_inout);
5078 if (!ok)
5080 ret = GS_ERROR;
5081 is_inout = false;
5084 if (!allows_reg && allows_mem)
5085 mark_addressable (TREE_VALUE (link));
5087 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5088 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5089 fb_lvalue | fb_mayfail);
5090 if (tret == GS_ERROR)
5092 error ("invalid lvalue in asm output %d", i);
5093 ret = tret;
5096 vec_safe_push (outputs, link);
5097 TREE_CHAIN (link) = NULL_TREE;
5099 if (is_inout)
5101 /* An input/output operand. To give the optimizers more
5102 flexibility, split it into separate input and output
5103 operands. */
5104 tree input;
5105 char buf[10];
5107 /* Turn the in/out constraint into an output constraint. */
5108 char *p = xstrdup (constraint);
5109 p[0] = '=';
5110 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5112 /* And add a matching input constraint. */
5113 if (allows_reg)
5115 sprintf (buf, "%d", i);
5117 /* If there are multiple alternatives in the constraint,
5118 handle each of them individually. Those that allow register
5119 will be replaced with operand number, the others will stay
5120 unchanged. */
5121 if (strchr (p, ',') != NULL)
5123 size_t len = 0, buflen = strlen (buf);
5124 char *beg, *end, *str, *dst;
5126 for (beg = p + 1;;)
5128 end = strchr (beg, ',');
5129 if (end == NULL)
5130 end = strchr (beg, '\0');
5131 if ((size_t) (end - beg) < buflen)
5132 len += buflen + 1;
5133 else
5134 len += end - beg + 1;
5135 if (*end)
5136 beg = end + 1;
5137 else
5138 break;
5141 str = (char *) alloca (len);
5142 for (beg = p + 1, dst = str;;)
5144 const char *tem;
5145 bool mem_p, reg_p, inout_p;
5147 end = strchr (beg, ',');
5148 if (end)
5149 *end = '\0';
5150 beg[-1] = '=';
5151 tem = beg - 1;
5152 parse_output_constraint (&tem, i, 0, 0,
5153 &mem_p, &reg_p, &inout_p);
5154 if (dst != str)
5155 *dst++ = ',';
5156 if (reg_p)
5158 memcpy (dst, buf, buflen);
5159 dst += buflen;
5161 else
5163 if (end)
5164 len = end - beg;
5165 else
5166 len = strlen (beg);
5167 memcpy (dst, beg, len);
5168 dst += len;
5170 if (end)
5171 beg = end + 1;
5172 else
5173 break;
5175 *dst = '\0';
5176 input = build_string (dst - str, str);
5178 else
5179 input = build_string (strlen (buf), buf);
5181 else
5182 input = build_string (constraint_len - 1, constraint + 1);
5184 free (p);
5186 input = build_tree_list (build_tree_list (NULL_TREE, input),
5187 unshare_expr (TREE_VALUE (link)));
5188 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5192 link_next = NULL_TREE;
5193 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5195 link_next = TREE_CHAIN (link);
5196 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5197 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5198 oconstraints, &allows_mem, &allows_reg);
5200 /* If we can't make copies, we can only accept memory. */
5201 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5203 if (allows_mem)
5204 allows_reg = 0;
5205 else
5207 error ("impossible constraint in %<asm%>");
5208 error ("non-memory input %d must stay in memory", i);
5209 return GS_ERROR;
5213 /* If the operand is a memory input, it should be an lvalue. */
5214 if (!allows_reg && allows_mem)
5216 tree inputv = TREE_VALUE (link);
5217 STRIP_NOPS (inputv);
5218 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5219 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5220 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5221 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5222 TREE_VALUE (link) = error_mark_node;
5223 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5224 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5225 mark_addressable (TREE_VALUE (link));
5226 if (tret == GS_ERROR)
5228 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5229 input_location = EXPR_LOCATION (TREE_VALUE (link));
5230 error ("memory input %d is not directly addressable", i);
5231 ret = tret;
5234 else
5236 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5237 is_gimple_asm_val, fb_rvalue);
5238 if (tret == GS_ERROR)
5239 ret = tret;
5242 TREE_CHAIN (link) = NULL_TREE;
5243 vec_safe_push (inputs, link);
5246 link_next = NULL_TREE;
5247 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5249 link_next = TREE_CHAIN (link);
5250 TREE_CHAIN (link) = NULL_TREE;
5251 vec_safe_push (clobbers, link);
5254 link_next = NULL_TREE;
5255 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5257 link_next = TREE_CHAIN (link);
5258 TREE_CHAIN (link) = NULL_TREE;
5259 vec_safe_push (labels, link);
5262 /* Do not add ASMs with errors to the gimple IL stream. */
5263 if (ret != GS_ERROR)
5265 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5266 inputs, outputs, clobbers, labels);
5268 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5269 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5271 gimplify_seq_add_stmt (pre_p, stmt);
5274 return ret;
5277 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5278 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5279 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5280 return to this function.
5282 FIXME should we complexify the prequeue handling instead? Or use flags
5283 for all the cleanups and let the optimizer tighten them up? The current
5284 code seems pretty fragile; it will break on a cleanup within any
5285 non-conditional nesting. But any such nesting would be broken, anyway;
5286 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5287 and continues out of it. We can do that at the RTL level, though, so
5288 having an optimizer to tighten up try/finally regions would be a Good
5289 Thing. */
5291 static enum gimplify_status
5292 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5294 gimple_stmt_iterator iter;
5295 gimple_seq body_sequence = NULL;
5297 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5299 /* We only care about the number of conditions between the innermost
5300 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5301 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5302 int old_conds = gimplify_ctxp->conditions;
5303 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5304 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5305 gimplify_ctxp->conditions = 0;
5306 gimplify_ctxp->conditional_cleanups = NULL;
5307 gimplify_ctxp->in_cleanup_point_expr = true;
5309 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5311 gimplify_ctxp->conditions = old_conds;
5312 gimplify_ctxp->conditional_cleanups = old_cleanups;
5313 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5315 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5317 gimple wce = gsi_stmt (iter);
5319 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5321 if (gsi_one_before_end_p (iter))
5323 /* Note that gsi_insert_seq_before and gsi_remove do not
5324 scan operands, unlike some other sequence mutators. */
5325 if (!gimple_wce_cleanup_eh_only (wce))
5326 gsi_insert_seq_before_without_update (&iter,
5327 gimple_wce_cleanup (wce),
5328 GSI_SAME_STMT);
5329 gsi_remove (&iter, true);
5330 break;
5332 else
5334 gtry *gtry;
5335 gimple_seq seq;
5336 enum gimple_try_flags kind;
5338 if (gimple_wce_cleanup_eh_only (wce))
5339 kind = GIMPLE_TRY_CATCH;
5340 else
5341 kind = GIMPLE_TRY_FINALLY;
5342 seq = gsi_split_seq_after (iter);
5344 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5345 /* Do not use gsi_replace here, as it may scan operands.
5346 We want to do a simple structural modification only. */
5347 gsi_set_stmt (&iter, gtry);
5348 iter = gsi_start (gtry->eval);
5351 else
5352 gsi_next (&iter);
5355 gimplify_seq_add_seq (pre_p, body_sequence);
5356 if (temp)
5358 *expr_p = temp;
5359 return GS_OK;
5361 else
5363 *expr_p = NULL;
5364 return GS_ALL_DONE;
5368 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5369 is the cleanup action required. EH_ONLY is true if the cleanup should
5370 only be executed if an exception is thrown, not on normal exit. */
5372 static void
5373 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5375 gimple wce;
5376 gimple_seq cleanup_stmts = NULL;
5378 /* Errors can result in improperly nested cleanups. Which results in
5379 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5380 if (seen_error ())
5381 return;
5383 if (gimple_conditional_context ())
5385 /* If we're in a conditional context, this is more complex. We only
5386 want to run the cleanup if we actually ran the initialization that
5387 necessitates it, but we want to run it after the end of the
5388 conditional context. So we wrap the try/finally around the
5389 condition and use a flag to determine whether or not to actually
5390 run the destructor. Thus
5392 test ? f(A()) : 0
5394 becomes (approximately)
5396 flag = 0;
5397 try {
5398 if (test) { A::A(temp); flag = 1; val = f(temp); }
5399 else { val = 0; }
5400 } finally {
5401 if (flag) A::~A(temp);
5405 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5406 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5407 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5409 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5410 gimplify_stmt (&cleanup, &cleanup_stmts);
5411 wce = gimple_build_wce (cleanup_stmts);
5413 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5414 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5415 gimplify_seq_add_stmt (pre_p, ftrue);
5417 /* Because of this manipulation, and the EH edges that jump
5418 threading cannot redirect, the temporary (VAR) will appear
5419 to be used uninitialized. Don't warn. */
5420 TREE_NO_WARNING (var) = 1;
5422 else
5424 gimplify_stmt (&cleanup, &cleanup_stmts);
5425 wce = gimple_build_wce (cleanup_stmts);
5426 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5427 gimplify_seq_add_stmt (pre_p, wce);
5431 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5433 static enum gimplify_status
5434 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5436 tree targ = *expr_p;
5437 tree temp = TARGET_EXPR_SLOT (targ);
5438 tree init = TARGET_EXPR_INITIAL (targ);
5439 enum gimplify_status ret;
5441 if (init)
5443 tree cleanup = NULL_TREE;
5445 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5446 to the temps list. Handle also variable length TARGET_EXPRs. */
5447 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5449 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5450 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5451 gimplify_vla_decl (temp, pre_p);
5453 else
5454 gimple_add_tmp_var (temp);
5456 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5457 expression is supposed to initialize the slot. */
5458 if (VOID_TYPE_P (TREE_TYPE (init)))
5459 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5460 else
5462 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5463 init = init_expr;
5464 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5465 init = NULL;
5466 ggc_free (init_expr);
5468 if (ret == GS_ERROR)
5470 /* PR c++/28266 Make sure this is expanded only once. */
5471 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5472 return GS_ERROR;
5474 if (init)
5475 gimplify_and_add (init, pre_p);
5477 /* If needed, push the cleanup for the temp. */
5478 if (TARGET_EXPR_CLEANUP (targ))
5480 if (CLEANUP_EH_ONLY (targ))
5481 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5482 CLEANUP_EH_ONLY (targ), pre_p);
5483 else
5484 cleanup = TARGET_EXPR_CLEANUP (targ);
5487 /* Add a clobber for the temporary going out of scope, like
5488 gimplify_bind_expr. */
5489 if (gimplify_ctxp->in_cleanup_point_expr
5490 && needs_to_live_in_memory (temp)
5491 && flag_stack_reuse == SR_ALL)
5493 tree clobber = build_constructor (TREE_TYPE (temp),
5494 NULL);
5495 TREE_THIS_VOLATILE (clobber) = true;
5496 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5497 if (cleanup)
5498 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5499 clobber);
5500 else
5501 cleanup = clobber;
5504 if (cleanup)
5505 gimple_push_cleanup (temp, cleanup, false, pre_p);
5507 /* Only expand this once. */
5508 TREE_OPERAND (targ, 3) = init;
5509 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5511 else
5512 /* We should have expanded this before. */
5513 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5515 *expr_p = temp;
5516 return GS_OK;
5519 /* Gimplification of expression trees. */
5521 /* Gimplify an expression which appears at statement context. The
5522 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5523 NULL, a new sequence is allocated.
5525 Return true if we actually added a statement to the queue. */
5527 bool
5528 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5530 gimple_seq_node last;
5532 last = gimple_seq_last (*seq_p);
5533 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5534 return last != gimple_seq_last (*seq_p);
5537 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5538 to CTX. If entries already exist, force them to be some flavor of private.
5539 If there is no enclosing parallel, do nothing. */
5541 void
5542 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5544 splay_tree_node n;
5546 if (decl == NULL || !DECL_P (decl))
5547 return;
5551 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5552 if (n != NULL)
5554 if (n->value & GOVD_SHARED)
5555 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5556 else if (n->value & GOVD_MAP)
5557 n->value |= GOVD_MAP_TO_ONLY;
5558 else
5559 return;
5561 else if (ctx->region_type == ORT_TARGET)
5562 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5563 else if (ctx->region_type != ORT_WORKSHARE
5564 && ctx->region_type != ORT_SIMD
5565 && ctx->region_type != ORT_TARGET_DATA)
5566 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5568 ctx = ctx->outer_context;
5570 while (ctx);
5573 /* Similarly for each of the type sizes of TYPE. */
5575 static void
5576 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5578 if (type == NULL || type == error_mark_node)
5579 return;
5580 type = TYPE_MAIN_VARIANT (type);
5582 if (ctx->privatized_types->add (type))
5583 return;
5585 switch (TREE_CODE (type))
5587 case INTEGER_TYPE:
5588 case ENUMERAL_TYPE:
5589 case BOOLEAN_TYPE:
5590 case REAL_TYPE:
5591 case FIXED_POINT_TYPE:
5592 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5593 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5594 break;
5596 case ARRAY_TYPE:
5597 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5598 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5599 break;
5601 case RECORD_TYPE:
5602 case UNION_TYPE:
5603 case QUAL_UNION_TYPE:
5605 tree field;
5606 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5607 if (TREE_CODE (field) == FIELD_DECL)
5609 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5610 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5613 break;
5615 case POINTER_TYPE:
5616 case REFERENCE_TYPE:
5617 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5618 break;
5620 default:
5621 break;
5624 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5625 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5626 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5629 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5631 static void
5632 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5634 splay_tree_node n;
5635 unsigned int nflags;
5636 tree t;
5638 if (error_operand_p (decl))
5639 return;
5641 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5642 there are constructors involved somewhere. */
5643 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5644 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5645 flags |= GOVD_SEEN;
5647 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5648 if (n != NULL && n->value != GOVD_ALIGNED)
5650 /* We shouldn't be re-adding the decl with the same data
5651 sharing class. */
5652 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5653 /* The only combination of data sharing classes we should see is
5654 FIRSTPRIVATE and LASTPRIVATE. */
5655 nflags = n->value | flags;
5656 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5657 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5658 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5659 n->value = nflags;
5660 return;
5663 /* When adding a variable-sized variable, we have to handle all sorts
5664 of additional bits of data: the pointer replacement variable, and
5665 the parameters of the type. */
5666 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5668 /* Add the pointer replacement variable as PRIVATE if the variable
5669 replacement is private, else FIRSTPRIVATE since we'll need the
5670 address of the original variable either for SHARED, or for the
5671 copy into or out of the context. */
5672 if (!(flags & GOVD_LOCAL))
5674 if (flags & GOVD_MAP)
5675 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5676 else if (flags & GOVD_PRIVATE)
5677 nflags = GOVD_PRIVATE;
5678 else
5679 nflags = GOVD_FIRSTPRIVATE;
5680 nflags |= flags & GOVD_SEEN;
5681 t = DECL_VALUE_EXPR (decl);
5682 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5683 t = TREE_OPERAND (t, 0);
5684 gcc_assert (DECL_P (t));
5685 omp_add_variable (ctx, t, nflags);
5688 /* Add all of the variable and type parameters (which should have
5689 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5690 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5691 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5692 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5694 /* The variable-sized variable itself is never SHARED, only some form
5695 of PRIVATE. The sharing would take place via the pointer variable
5696 which we remapped above. */
5697 if (flags & GOVD_SHARED)
5698 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5699 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5701 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5702 alloca statement we generate for the variable, so make sure it
5703 is available. This isn't automatically needed for the SHARED
5704 case, since we won't be allocating local storage then.
5705 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5706 in this case omp_notice_variable will be called later
5707 on when it is gimplified. */
5708 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5709 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5710 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5712 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5713 && lang_hooks.decls.omp_privatize_by_reference (decl))
5715 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5717 /* Similar to the direct variable sized case above, we'll need the
5718 size of references being privatized. */
5719 if ((flags & GOVD_SHARED) == 0)
5721 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5722 if (TREE_CODE (t) != INTEGER_CST)
5723 omp_notice_variable (ctx, t, true);
5727 if (n != NULL)
5728 n->value |= flags;
5729 else
5730 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5733 /* Notice a threadprivate variable DECL used in OMP context CTX.
5734 This just prints out diagnostics about threadprivate variable uses
5735 in untied tasks. If DECL2 is non-NULL, prevent this warning
5736 on that variable. */
5738 static bool
5739 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5740 tree decl2)
5742 splay_tree_node n;
5743 struct gimplify_omp_ctx *octx;
5745 for (octx = ctx; octx; octx = octx->outer_context)
5746 if (octx->region_type == ORT_TARGET)
5748 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5749 if (n == NULL)
5751 error ("threadprivate variable %qE used in target region",
5752 DECL_NAME (decl));
5753 error_at (octx->location, "enclosing target region");
5754 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5756 if (decl2)
5757 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5760 if (ctx->region_type != ORT_UNTIED_TASK)
5761 return false;
5762 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5763 if (n == NULL)
5765 error ("threadprivate variable %qE used in untied task",
5766 DECL_NAME (decl));
5767 error_at (ctx->location, "enclosing task");
5768 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5770 if (decl2)
5771 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5772 return false;
5775 /* Record the fact that DECL was used within the OMP context CTX.
5776 IN_CODE is true when real code uses DECL, and false when we should
5777 merely emit default(none) errors. Return true if DECL is going to
5778 be remapped and thus DECL shouldn't be gimplified into its
5779 DECL_VALUE_EXPR (if any). */
5781 static bool
5782 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5784 splay_tree_node n;
5785 unsigned flags = in_code ? GOVD_SEEN : 0;
5786 bool ret = false, shared;
5788 if (error_operand_p (decl))
5789 return false;
5791 /* Threadprivate variables are predetermined. */
5792 if (is_global_var (decl))
5794 if (DECL_THREAD_LOCAL_P (decl))
5795 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5797 if (DECL_HAS_VALUE_EXPR_P (decl))
5799 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5801 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5802 return omp_notice_threadprivate_variable (ctx, decl, value);
5806 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5807 if (ctx->region_type == ORT_TARGET)
5809 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5810 if (n == NULL)
5812 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5814 error ("%qD referenced in target region does not have "
5815 "a mappable type", decl);
5816 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5818 else
5819 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5821 else
5823 /* If nothing changed, there's nothing left to do. */
5824 if ((n->value & flags) == flags)
5825 return ret;
5826 n->value |= flags;
5828 goto do_outer;
5831 if (n == NULL)
5833 enum omp_clause_default_kind default_kind, kind;
5834 struct gimplify_omp_ctx *octx;
5836 if (ctx->region_type == ORT_WORKSHARE
5837 || ctx->region_type == ORT_SIMD
5838 || ctx->region_type == ORT_TARGET_DATA)
5839 goto do_outer;
5841 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5842 remapped firstprivate instead of shared. To some extent this is
5843 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5844 default_kind = ctx->default_kind;
5845 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5846 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5847 default_kind = kind;
5849 switch (default_kind)
5851 case OMP_CLAUSE_DEFAULT_NONE:
5852 if ((ctx->region_type & ORT_PARALLEL) != 0)
5854 error ("%qE not specified in enclosing parallel",
5855 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5856 error_at (ctx->location, "enclosing parallel");
5858 else if ((ctx->region_type & ORT_TASK) != 0)
5860 error ("%qE not specified in enclosing task",
5861 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5862 error_at (ctx->location, "enclosing task");
5864 else if (ctx->region_type & ORT_TEAMS)
5866 error ("%qE not specified in enclosing teams construct",
5867 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5868 error_at (ctx->location, "enclosing teams construct");
5870 else
5871 gcc_unreachable ();
5872 /* FALLTHRU */
5873 case OMP_CLAUSE_DEFAULT_SHARED:
5874 flags |= GOVD_SHARED;
5875 break;
5876 case OMP_CLAUSE_DEFAULT_PRIVATE:
5877 flags |= GOVD_PRIVATE;
5878 break;
5879 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5880 flags |= GOVD_FIRSTPRIVATE;
5881 break;
5882 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5883 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5884 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5885 if (ctx->outer_context)
5886 omp_notice_variable (ctx->outer_context, decl, in_code);
5887 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5889 splay_tree_node n2;
5891 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5892 continue;
5893 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5894 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5896 flags |= GOVD_FIRSTPRIVATE;
5897 break;
5899 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5900 break;
5902 if (flags & GOVD_FIRSTPRIVATE)
5903 break;
5904 if (octx == NULL
5905 && (TREE_CODE (decl) == PARM_DECL
5906 || (!is_global_var (decl)
5907 && DECL_CONTEXT (decl) == current_function_decl)))
5909 flags |= GOVD_FIRSTPRIVATE;
5910 break;
5912 flags |= GOVD_SHARED;
5913 break;
5914 default:
5915 gcc_unreachable ();
5918 if ((flags & GOVD_PRIVATE)
5919 && lang_hooks.decls.omp_private_outer_ref (decl))
5920 flags |= GOVD_PRIVATE_OUTER_REF;
5922 omp_add_variable (ctx, decl, flags);
5924 shared = (flags & GOVD_SHARED) != 0;
5925 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5926 goto do_outer;
5929 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5930 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5931 && DECL_SIZE (decl)
5932 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5934 splay_tree_node n2;
5935 tree t = DECL_VALUE_EXPR (decl);
5936 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5937 t = TREE_OPERAND (t, 0);
5938 gcc_assert (DECL_P (t));
5939 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5940 n2->value |= GOVD_SEEN;
5943 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5944 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5946 /* If nothing changed, there's nothing left to do. */
5947 if ((n->value & flags) == flags)
5948 return ret;
5949 flags |= n->value;
5950 n->value = flags;
5952 do_outer:
5953 /* If the variable is private in the current context, then we don't
5954 need to propagate anything to an outer context. */
5955 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5956 return ret;
5957 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
5958 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
5959 return ret;
5960 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
5961 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
5962 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
5963 return ret;
5964 if (ctx->outer_context
5965 && omp_notice_variable (ctx->outer_context, decl, in_code))
5966 return true;
5967 return ret;
5970 /* Verify that DECL is private within CTX. If there's specific information
5971 to the contrary in the innermost scope, generate an error. */
5973 static bool
5974 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
5976 splay_tree_node n;
5978 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5979 if (n != NULL)
5981 if (n->value & GOVD_SHARED)
5983 if (ctx == gimplify_omp_ctxp)
5985 if (simd)
5986 error ("iteration variable %qE is predetermined linear",
5987 DECL_NAME (decl));
5988 else
5989 error ("iteration variable %qE should be private",
5990 DECL_NAME (decl));
5991 n->value = GOVD_PRIVATE;
5992 return true;
5994 else
5995 return false;
5997 else if ((n->value & GOVD_EXPLICIT) != 0
5998 && (ctx == gimplify_omp_ctxp
5999 || (ctx->region_type == ORT_COMBINED_PARALLEL
6000 && gimplify_omp_ctxp->outer_context == ctx)))
6002 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6003 error ("iteration variable %qE should not be firstprivate",
6004 DECL_NAME (decl));
6005 else if ((n->value & GOVD_REDUCTION) != 0)
6006 error ("iteration variable %qE should not be reduction",
6007 DECL_NAME (decl));
6008 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
6009 error ("iteration variable %qE should not be lastprivate",
6010 DECL_NAME (decl));
6011 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6012 error ("iteration variable %qE should not be private",
6013 DECL_NAME (decl));
6014 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
6015 error ("iteration variable %qE is predetermined linear",
6016 DECL_NAME (decl));
6018 return (ctx == gimplify_omp_ctxp
6019 || (ctx->region_type == ORT_COMBINED_PARALLEL
6020 && gimplify_omp_ctxp->outer_context == ctx));
6023 if (ctx->region_type != ORT_WORKSHARE
6024 && ctx->region_type != ORT_SIMD)
6025 return false;
6026 else if (ctx->outer_context)
6027 return omp_is_private (ctx->outer_context, decl, simd);
6028 return false;
6031 /* Return true if DECL is private within a parallel region
6032 that binds to the current construct's context or in parallel
6033 region's REDUCTION clause. */
6035 static bool
6036 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6038 splay_tree_node n;
6042 ctx = ctx->outer_context;
6043 if (ctx == NULL)
6044 return !(is_global_var (decl)
6045 /* References might be private, but might be shared too,
6046 when checking for copyprivate, assume they might be
6047 private, otherwise assume they might be shared. */
6048 || (!copyprivate
6049 && lang_hooks.decls.omp_privatize_by_reference (decl)));
6051 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6052 continue;
6054 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6055 if (n != NULL)
6056 return (n->value & GOVD_SHARED) == 0;
6058 while (ctx->region_type == ORT_WORKSHARE
6059 || ctx->region_type == ORT_SIMD);
6060 return false;
6063 /* Return true if the CTX is combined with distribute and thus
6064 lastprivate can't be supported. */
6066 static bool
6067 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
6071 if (ctx->outer_context == NULL)
6072 return false;
6073 ctx = ctx->outer_context;
6074 switch (ctx->region_type)
6076 case ORT_WORKSHARE:
6077 if (!ctx->combined_loop)
6078 return false;
6079 if (ctx->distribute)
6080 return true;
6081 break;
6082 case ORT_COMBINED_PARALLEL:
6083 break;
6084 case ORT_COMBINED_TEAMS:
6085 return true;
6086 default:
6087 return false;
6090 while (1);
6093 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6094 and previous omp contexts. */
6096 static void
6097 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6098 enum omp_region_type region_type)
6100 struct gimplify_omp_ctx *ctx, *outer_ctx;
6101 tree c;
6103 ctx = new_omp_context (region_type);
6104 outer_ctx = ctx->outer_context;
6106 while ((c = *list_p) != NULL)
6108 bool remove = false;
6109 bool notice_outer = true;
6110 const char *check_non_private = NULL;
6111 unsigned int flags;
6112 tree decl;
6114 switch (OMP_CLAUSE_CODE (c))
6116 case OMP_CLAUSE_PRIVATE:
6117 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6118 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6120 flags |= GOVD_PRIVATE_OUTER_REF;
6121 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6123 else
6124 notice_outer = false;
6125 goto do_add;
6126 case OMP_CLAUSE_SHARED:
6127 flags = GOVD_SHARED | GOVD_EXPLICIT;
6128 goto do_add;
6129 case OMP_CLAUSE_FIRSTPRIVATE:
6130 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6131 check_non_private = "firstprivate";
6132 goto do_add;
6133 case OMP_CLAUSE_LASTPRIVATE:
6134 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6135 check_non_private = "lastprivate";
6136 decl = OMP_CLAUSE_DECL (c);
6137 if (omp_no_lastprivate (ctx))
6139 notice_outer = false;
6140 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6142 else if (error_operand_p (decl))
6143 goto do_add;
6144 else if (outer_ctx
6145 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
6146 && splay_tree_lookup (outer_ctx->variables,
6147 (splay_tree_key) decl) == NULL)
6148 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
6149 else if (outer_ctx
6150 && outer_ctx->region_type == ORT_WORKSHARE
6151 && outer_ctx->combined_loop
6152 && splay_tree_lookup (outer_ctx->variables,
6153 (splay_tree_key) decl) == NULL
6154 && !omp_check_private (outer_ctx, decl, false))
6156 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6157 if (outer_ctx->outer_context
6158 && (outer_ctx->outer_context->region_type
6159 == ORT_COMBINED_PARALLEL)
6160 && splay_tree_lookup (outer_ctx->outer_context->variables,
6161 (splay_tree_key) decl) == NULL)
6162 omp_add_variable (outer_ctx->outer_context, decl,
6163 GOVD_SHARED | GOVD_SEEN);
6165 goto do_add;
6166 case OMP_CLAUSE_REDUCTION:
6167 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6168 check_non_private = "reduction";
6169 goto do_add;
6170 case OMP_CLAUSE_LINEAR:
6171 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6172 is_gimple_val, fb_rvalue) == GS_ERROR)
6174 remove = true;
6175 break;
6177 else
6179 /* For combined #pragma omp parallel for simd, need to put
6180 lastprivate and perhaps firstprivate too on the
6181 parallel. Similarly for #pragma omp for simd. */
6182 struct gimplify_omp_ctx *octx = outer_ctx;
6183 decl = NULL_TREE;
6184 if (omp_no_lastprivate (ctx))
6185 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6188 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6189 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6190 break;
6191 decl = OMP_CLAUSE_DECL (c);
6192 if (error_operand_p (decl))
6194 decl = NULL_TREE;
6195 break;
6197 if (octx
6198 && octx->region_type == ORT_WORKSHARE
6199 && octx->combined_loop)
6201 if (octx->outer_context
6202 && (octx->outer_context->region_type
6203 == ORT_COMBINED_PARALLEL
6204 || (octx->outer_context->region_type
6205 == ORT_COMBINED_TEAMS)))
6206 octx = octx->outer_context;
6207 else if (omp_check_private (octx, decl, false))
6208 break;
6210 else
6211 break;
6212 gcc_checking_assert (splay_tree_lookup (octx->variables,
6213 (splay_tree_key)
6214 decl) == NULL);
6215 flags = GOVD_SEEN;
6216 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6217 flags |= GOVD_FIRSTPRIVATE;
6218 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6219 flags |= GOVD_LASTPRIVATE;
6220 omp_add_variable (octx, decl, flags);
6221 if (octx->outer_context == NULL)
6222 break;
6223 octx = octx->outer_context;
6225 while (1);
6226 if (octx
6227 && decl
6228 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6229 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6230 omp_notice_variable (octx, decl, true);
6232 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6233 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6234 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6236 notice_outer = false;
6237 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6239 goto do_add;
6241 case OMP_CLAUSE_MAP:
6242 decl = OMP_CLAUSE_DECL (c);
6243 if (error_operand_p (decl))
6245 remove = true;
6246 break;
6248 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6249 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6250 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6251 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6252 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6254 remove = true;
6255 break;
6257 if (!DECL_P (decl))
6259 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6260 NULL, is_gimple_lvalue, fb_lvalue)
6261 == GS_ERROR)
6263 remove = true;
6264 break;
6266 break;
6268 flags = GOVD_MAP | GOVD_EXPLICIT;
6269 goto do_add;
6271 case OMP_CLAUSE_DEPEND:
6272 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6274 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6275 NULL, is_gimple_val, fb_rvalue);
6276 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6278 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6280 remove = true;
6281 break;
6283 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6284 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6285 is_gimple_val, fb_rvalue) == GS_ERROR)
6287 remove = true;
6288 break;
6290 break;
6292 case OMP_CLAUSE_TO:
6293 case OMP_CLAUSE_FROM:
6294 case OMP_CLAUSE__CACHE_:
6295 decl = OMP_CLAUSE_DECL (c);
6296 if (error_operand_p (decl))
6298 remove = true;
6299 break;
6301 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6302 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6303 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6304 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6305 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6307 remove = true;
6308 break;
6310 if (!DECL_P (decl))
6312 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6313 NULL, is_gimple_lvalue, fb_lvalue)
6314 == GS_ERROR)
6316 remove = true;
6317 break;
6319 break;
6321 goto do_notice;
6323 do_add:
6324 decl = OMP_CLAUSE_DECL (c);
6325 if (error_operand_p (decl))
6327 remove = true;
6328 break;
6330 omp_add_variable (ctx, decl, flags);
6331 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6332 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6334 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6335 GOVD_LOCAL | GOVD_SEEN);
6336 gimplify_omp_ctxp = ctx;
6337 push_gimplify_context ();
6339 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6340 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6342 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6343 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6344 pop_gimplify_context
6345 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6346 push_gimplify_context ();
6347 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6348 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6349 pop_gimplify_context
6350 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6351 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6352 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6354 gimplify_omp_ctxp = outer_ctx;
6356 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6357 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6359 gimplify_omp_ctxp = ctx;
6360 push_gimplify_context ();
6361 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6363 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6364 NULL, NULL);
6365 TREE_SIDE_EFFECTS (bind) = 1;
6366 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6367 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6369 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6370 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6371 pop_gimplify_context
6372 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6373 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6375 gimplify_omp_ctxp = outer_ctx;
6377 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6378 && OMP_CLAUSE_LINEAR_STMT (c))
6380 gimplify_omp_ctxp = ctx;
6381 push_gimplify_context ();
6382 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6384 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6385 NULL, NULL);
6386 TREE_SIDE_EFFECTS (bind) = 1;
6387 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6388 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6390 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6391 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6392 pop_gimplify_context
6393 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6394 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6396 gimplify_omp_ctxp = outer_ctx;
6398 if (notice_outer)
6399 goto do_notice;
6400 break;
6402 case OMP_CLAUSE_COPYIN:
6403 case OMP_CLAUSE_COPYPRIVATE:
6404 decl = OMP_CLAUSE_DECL (c);
6405 if (error_operand_p (decl))
6407 remove = true;
6408 break;
6410 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6411 && !remove
6412 && !omp_check_private (ctx, decl, true))
6414 remove = true;
6415 if (is_global_var (decl))
6417 if (DECL_THREAD_LOCAL_P (decl))
6418 remove = false;
6419 else if (DECL_HAS_VALUE_EXPR_P (decl))
6421 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6423 if (value
6424 && DECL_P (value)
6425 && DECL_THREAD_LOCAL_P (value))
6426 remove = false;
6429 if (remove)
6430 error_at (OMP_CLAUSE_LOCATION (c),
6431 "copyprivate variable %qE is not threadprivate"
6432 " or private in outer context", DECL_NAME (decl));
6434 do_notice:
6435 if (outer_ctx)
6436 omp_notice_variable (outer_ctx, decl, true);
6437 if (check_non_private
6438 && region_type == ORT_WORKSHARE
6439 && omp_check_private (ctx, decl, false))
6441 error ("%s variable %qE is private in outer context",
6442 check_non_private, DECL_NAME (decl));
6443 remove = true;
6445 break;
6447 case OMP_CLAUSE_FINAL:
6448 case OMP_CLAUSE_IF:
6449 OMP_CLAUSE_OPERAND (c, 0)
6450 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6451 /* Fall through. */
6453 case OMP_CLAUSE_SCHEDULE:
6454 case OMP_CLAUSE_NUM_THREADS:
6455 case OMP_CLAUSE_NUM_TEAMS:
6456 case OMP_CLAUSE_THREAD_LIMIT:
6457 case OMP_CLAUSE_DIST_SCHEDULE:
6458 case OMP_CLAUSE_DEVICE:
6459 case OMP_CLAUSE__CILK_FOR_COUNT_:
6460 case OMP_CLAUSE_ASYNC:
6461 case OMP_CLAUSE_WAIT:
6462 case OMP_CLAUSE_NUM_GANGS:
6463 case OMP_CLAUSE_NUM_WORKERS:
6464 case OMP_CLAUSE_VECTOR_LENGTH:
6465 case OMP_CLAUSE_GANG:
6466 case OMP_CLAUSE_WORKER:
6467 case OMP_CLAUSE_VECTOR:
6468 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6469 is_gimple_val, fb_rvalue) == GS_ERROR)
6470 remove = true;
6471 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_GANG
6472 && gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
6473 is_gimple_val, fb_rvalue) == GS_ERROR)
6474 remove = true;
6475 break;
6477 case OMP_CLAUSE_DEVICE_RESIDENT:
6478 case OMP_CLAUSE_USE_DEVICE:
6479 case OMP_CLAUSE_INDEPENDENT:
6480 remove = true;
6481 break;
6483 case OMP_CLAUSE_NOWAIT:
6484 case OMP_CLAUSE_ORDERED:
6485 case OMP_CLAUSE_UNTIED:
6486 case OMP_CLAUSE_COLLAPSE:
6487 case OMP_CLAUSE_AUTO:
6488 case OMP_CLAUSE_SEQ:
6489 case OMP_CLAUSE_MERGEABLE:
6490 case OMP_CLAUSE_PROC_BIND:
6491 case OMP_CLAUSE_SAFELEN:
6492 break;
6494 case OMP_CLAUSE_ALIGNED:
6495 decl = OMP_CLAUSE_DECL (c);
6496 if (error_operand_p (decl))
6498 remove = true;
6499 break;
6501 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
6502 is_gimple_val, fb_rvalue) == GS_ERROR)
6504 remove = true;
6505 break;
6507 if (!is_global_var (decl)
6508 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6509 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6510 break;
6512 case OMP_CLAUSE_DEFAULT:
6513 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6514 break;
6516 default:
6517 gcc_unreachable ();
6520 if (remove)
6521 *list_p = OMP_CLAUSE_CHAIN (c);
6522 else
6523 list_p = &OMP_CLAUSE_CHAIN (c);
6526 gimplify_omp_ctxp = ctx;
6529 struct gimplify_adjust_omp_clauses_data
6531 tree *list_p;
6532 gimple_seq *pre_p;
6535 /* For all variables that were not actually used within the context,
6536 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6538 static int
6539 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6541 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
6542 gimple_seq *pre_p
6543 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
6544 tree decl = (tree) n->key;
6545 unsigned flags = n->value;
6546 enum omp_clause_code code;
6547 tree clause;
6548 bool private_debug;
6550 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6551 return 0;
6552 if ((flags & GOVD_SEEN) == 0)
6553 return 0;
6554 if (flags & GOVD_DEBUG_PRIVATE)
6556 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6557 private_debug = true;
6559 else if (flags & GOVD_MAP)
6560 private_debug = false;
6561 else
6562 private_debug
6563 = lang_hooks.decls.omp_private_debug_clause (decl,
6564 !!(flags & GOVD_SHARED));
6565 if (private_debug)
6566 code = OMP_CLAUSE_PRIVATE;
6567 else if (flags & GOVD_MAP)
6568 code = OMP_CLAUSE_MAP;
6569 else if (flags & GOVD_SHARED)
6571 if (is_global_var (decl))
6573 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6574 while (ctx != NULL)
6576 splay_tree_node on
6577 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6578 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6579 | GOVD_PRIVATE | GOVD_REDUCTION
6580 | GOVD_LINEAR | GOVD_MAP)) != 0)
6581 break;
6582 ctx = ctx->outer_context;
6584 if (ctx == NULL)
6585 return 0;
6587 code = OMP_CLAUSE_SHARED;
6589 else if (flags & GOVD_PRIVATE)
6590 code = OMP_CLAUSE_PRIVATE;
6591 else if (flags & GOVD_FIRSTPRIVATE)
6592 code = OMP_CLAUSE_FIRSTPRIVATE;
6593 else if (flags & GOVD_LASTPRIVATE)
6594 code = OMP_CLAUSE_LASTPRIVATE;
6595 else if (flags & GOVD_ALIGNED)
6596 return 0;
6597 else
6598 gcc_unreachable ();
6600 clause = build_omp_clause (input_location, code);
6601 OMP_CLAUSE_DECL (clause) = decl;
6602 OMP_CLAUSE_CHAIN (clause) = *list_p;
6603 if (private_debug)
6604 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6605 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6606 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6607 else if (code == OMP_CLAUSE_MAP)
6609 OMP_CLAUSE_SET_MAP_KIND (clause,
6610 flags & GOVD_MAP_TO_ONLY
6611 ? GOMP_MAP_TO
6612 : GOMP_MAP_TOFROM);
6613 if (DECL_SIZE (decl)
6614 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6616 tree decl2 = DECL_VALUE_EXPR (decl);
6617 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6618 decl2 = TREE_OPERAND (decl2, 0);
6619 gcc_assert (DECL_P (decl2));
6620 tree mem = build_simple_mem_ref (decl2);
6621 OMP_CLAUSE_DECL (clause) = mem;
6622 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6623 if (gimplify_omp_ctxp->outer_context)
6625 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6626 omp_notice_variable (ctx, decl2, true);
6627 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6629 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6630 OMP_CLAUSE_MAP);
6631 OMP_CLAUSE_DECL (nc) = decl;
6632 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6633 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
6634 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6635 OMP_CLAUSE_CHAIN (clause) = nc;
6637 else
6638 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
6640 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
6642 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
6643 OMP_CLAUSE_DECL (nc) = decl;
6644 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
6645 OMP_CLAUSE_CHAIN (nc) = *list_p;
6646 OMP_CLAUSE_CHAIN (clause) = nc;
6647 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6648 gimplify_omp_ctxp = ctx->outer_context;
6649 lang_hooks.decls.omp_finish_clause (nc, pre_p);
6650 gimplify_omp_ctxp = ctx;
6652 *list_p = clause;
6653 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6654 gimplify_omp_ctxp = ctx->outer_context;
6655 lang_hooks.decls.omp_finish_clause (clause, pre_p);
6656 gimplify_omp_ctxp = ctx;
6657 return 0;
6660 static void
6661 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p)
6663 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6664 tree c, decl;
6666 while ((c = *list_p) != NULL)
6668 splay_tree_node n;
6669 bool remove = false;
6671 switch (OMP_CLAUSE_CODE (c))
6673 case OMP_CLAUSE_PRIVATE:
6674 case OMP_CLAUSE_SHARED:
6675 case OMP_CLAUSE_FIRSTPRIVATE:
6676 case OMP_CLAUSE_LINEAR:
6677 decl = OMP_CLAUSE_DECL (c);
6678 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6679 remove = !(n->value & GOVD_SEEN);
6680 if (! remove)
6682 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6683 if ((n->value & GOVD_DEBUG_PRIVATE)
6684 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6686 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6687 || ((n->value & GOVD_DATA_SHARE_CLASS)
6688 == GOVD_PRIVATE));
6689 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6690 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6693 break;
6695 case OMP_CLAUSE_LASTPRIVATE:
6696 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6697 accurately reflect the presence of a FIRSTPRIVATE clause. */
6698 decl = OMP_CLAUSE_DECL (c);
6699 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6700 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6701 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6702 if (omp_no_lastprivate (ctx))
6704 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
6705 remove = true;
6706 else
6707 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
6709 break;
6711 case OMP_CLAUSE_ALIGNED:
6712 decl = OMP_CLAUSE_DECL (c);
6713 if (!is_global_var (decl))
6715 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6716 remove = n == NULL || !(n->value & GOVD_SEEN);
6717 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6719 struct gimplify_omp_ctx *octx;
6720 if (n != NULL
6721 && (n->value & (GOVD_DATA_SHARE_CLASS
6722 & ~GOVD_FIRSTPRIVATE)))
6723 remove = true;
6724 else
6725 for (octx = ctx->outer_context; octx;
6726 octx = octx->outer_context)
6728 n = splay_tree_lookup (octx->variables,
6729 (splay_tree_key) decl);
6730 if (n == NULL)
6731 continue;
6732 if (n->value & GOVD_LOCAL)
6733 break;
6734 /* We have to avoid assigning a shared variable
6735 to itself when trying to add
6736 __builtin_assume_aligned. */
6737 if (n->value & GOVD_SHARED)
6739 remove = true;
6740 break;
6745 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6747 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6748 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6749 remove = true;
6751 break;
6753 case OMP_CLAUSE_MAP:
6754 decl = OMP_CLAUSE_DECL (c);
6755 if (!DECL_P (decl))
6756 break;
6757 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6758 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6759 remove = true;
6760 else if (DECL_SIZE (decl)
6761 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6762 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER)
6764 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
6765 for these, TREE_CODE (DECL_SIZE (decl)) will always be
6766 INTEGER_CST. */
6767 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
6769 tree decl2 = DECL_VALUE_EXPR (decl);
6770 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6771 decl2 = TREE_OPERAND (decl2, 0);
6772 gcc_assert (DECL_P (decl2));
6773 tree mem = build_simple_mem_ref (decl2);
6774 OMP_CLAUSE_DECL (c) = mem;
6775 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6776 if (ctx->outer_context)
6778 omp_notice_variable (ctx->outer_context, decl2, true);
6779 omp_notice_variable (ctx->outer_context,
6780 OMP_CLAUSE_SIZE (c), true);
6782 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6783 OMP_CLAUSE_MAP);
6784 OMP_CLAUSE_DECL (nc) = decl;
6785 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6786 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
6787 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6788 OMP_CLAUSE_CHAIN (c) = nc;
6789 c = nc;
6791 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6792 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6793 break;
6795 case OMP_CLAUSE_TO:
6796 case OMP_CLAUSE_FROM:
6797 case OMP_CLAUSE__CACHE_:
6798 decl = OMP_CLAUSE_DECL (c);
6799 if (!DECL_P (decl))
6800 break;
6801 if (DECL_SIZE (decl)
6802 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6804 tree decl2 = DECL_VALUE_EXPR (decl);
6805 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6806 decl2 = TREE_OPERAND (decl2, 0);
6807 gcc_assert (DECL_P (decl2));
6808 tree mem = build_simple_mem_ref (decl2);
6809 OMP_CLAUSE_DECL (c) = mem;
6810 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6811 if (ctx->outer_context)
6813 omp_notice_variable (ctx->outer_context, decl2, true);
6814 omp_notice_variable (ctx->outer_context,
6815 OMP_CLAUSE_SIZE (c), true);
6818 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6819 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
6820 break;
6822 case OMP_CLAUSE_REDUCTION:
6823 case OMP_CLAUSE_COPYIN:
6824 case OMP_CLAUSE_COPYPRIVATE:
6825 case OMP_CLAUSE_IF:
6826 case OMP_CLAUSE_NUM_THREADS:
6827 case OMP_CLAUSE_NUM_TEAMS:
6828 case OMP_CLAUSE_THREAD_LIMIT:
6829 case OMP_CLAUSE_DIST_SCHEDULE:
6830 case OMP_CLAUSE_DEVICE:
6831 case OMP_CLAUSE_SCHEDULE:
6832 case OMP_CLAUSE_NOWAIT:
6833 case OMP_CLAUSE_ORDERED:
6834 case OMP_CLAUSE_DEFAULT:
6835 case OMP_CLAUSE_UNTIED:
6836 case OMP_CLAUSE_COLLAPSE:
6837 case OMP_CLAUSE_FINAL:
6838 case OMP_CLAUSE_MERGEABLE:
6839 case OMP_CLAUSE_PROC_BIND:
6840 case OMP_CLAUSE_SAFELEN:
6841 case OMP_CLAUSE_DEPEND:
6842 case OMP_CLAUSE__CILK_FOR_COUNT_:
6843 case OMP_CLAUSE_ASYNC:
6844 case OMP_CLAUSE_WAIT:
6845 case OMP_CLAUSE_DEVICE_RESIDENT:
6846 case OMP_CLAUSE_USE_DEVICE:
6847 case OMP_CLAUSE_INDEPENDENT:
6848 case OMP_CLAUSE_NUM_GANGS:
6849 case OMP_CLAUSE_NUM_WORKERS:
6850 case OMP_CLAUSE_VECTOR_LENGTH:
6851 case OMP_CLAUSE_GANG:
6852 case OMP_CLAUSE_WORKER:
6853 case OMP_CLAUSE_VECTOR:
6854 case OMP_CLAUSE_AUTO:
6855 case OMP_CLAUSE_SEQ:
6856 break;
6858 default:
6859 gcc_unreachable ();
6862 if (remove)
6863 *list_p = OMP_CLAUSE_CHAIN (c);
6864 else
6865 list_p = &OMP_CLAUSE_CHAIN (c);
6868 /* Add in any implicit data sharing. */
6869 struct gimplify_adjust_omp_clauses_data data;
6870 data.list_p = list_p;
6871 data.pre_p = pre_p;
6872 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
6874 gimplify_omp_ctxp = ctx->outer_context;
6875 delete_omp_context (ctx);
6878 /* Gimplify OACC_CACHE. */
6880 static void
6881 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
6883 tree expr = *expr_p;
6885 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6886 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr));
6888 /* TODO: Do something sensible with this information. */
6890 *expr_p = NULL_TREE;
6893 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6894 gimplification of the body, as well as scanning the body for used
6895 variables. We need to do this scan now, because variable-sized
6896 decls will be decomposed during gimplification. */
6898 static void
6899 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6901 tree expr = *expr_p;
6902 gimple g;
6903 gimple_seq body = NULL;
6905 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6906 OMP_PARALLEL_COMBINED (expr)
6907 ? ORT_COMBINED_PARALLEL
6908 : ORT_PARALLEL);
6910 push_gimplify_context ();
6912 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6913 if (gimple_code (g) == GIMPLE_BIND)
6914 pop_gimplify_context (g);
6915 else
6916 pop_gimplify_context (NULL);
6918 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr));
6920 g = gimple_build_omp_parallel (body,
6921 OMP_PARALLEL_CLAUSES (expr),
6922 NULL_TREE, NULL_TREE);
6923 if (OMP_PARALLEL_COMBINED (expr))
6924 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6925 gimplify_seq_add_stmt (pre_p, g);
6926 *expr_p = NULL_TREE;
6929 /* Gimplify the contents of an OMP_TASK statement. This involves
6930 gimplification of the body, as well as scanning the body for used
6931 variables. We need to do this scan now, because variable-sized
6932 decls will be decomposed during gimplification. */
6934 static void
6935 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6937 tree expr = *expr_p;
6938 gimple g;
6939 gimple_seq body = NULL;
6941 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6942 find_omp_clause (OMP_TASK_CLAUSES (expr),
6943 OMP_CLAUSE_UNTIED)
6944 ? ORT_UNTIED_TASK : ORT_TASK);
6946 push_gimplify_context ();
6948 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6949 if (gimple_code (g) == GIMPLE_BIND)
6950 pop_gimplify_context (g);
6951 else
6952 pop_gimplify_context (NULL);
6954 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr));
6956 g = gimple_build_omp_task (body,
6957 OMP_TASK_CLAUSES (expr),
6958 NULL_TREE, NULL_TREE,
6959 NULL_TREE, NULL_TREE, NULL_TREE);
6960 gimplify_seq_add_stmt (pre_p, g);
6961 *expr_p = NULL_TREE;
6964 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6965 with non-NULL OMP_FOR_INIT. */
6967 static tree
6968 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6970 *walk_subtrees = 0;
6971 switch (TREE_CODE (*tp))
6973 case OMP_FOR:
6974 *walk_subtrees = 1;
6975 /* FALLTHRU */
6976 case OMP_SIMD:
6977 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6978 return *tp;
6979 break;
6980 case BIND_EXPR:
6981 case STATEMENT_LIST:
6982 case OMP_PARALLEL:
6983 *walk_subtrees = 1;
6984 break;
6985 default:
6986 break;
6988 return NULL_TREE;
6991 /* Gimplify the gross structure of an OMP_FOR statement. */
6993 static enum gimplify_status
6994 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6996 tree for_stmt, orig_for_stmt, decl, var, t;
6997 enum gimplify_status ret = GS_ALL_DONE;
6998 enum gimplify_status tret;
6999 gomp_for *gfor;
7000 gimple_seq for_body, for_pre_body;
7001 int i;
7002 bool simd;
7003 bitmap has_decl_expr = NULL;
7005 orig_for_stmt = for_stmt = *expr_p;
7007 switch (TREE_CODE (for_stmt))
7009 case OMP_FOR:
7010 case CILK_FOR:
7011 case OMP_DISTRIBUTE:
7012 case OACC_LOOP:
7013 simd = false;
7014 break;
7015 case OMP_SIMD:
7016 case CILK_SIMD:
7017 simd = true;
7018 break;
7019 default:
7020 gcc_unreachable ();
7023 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
7024 clause for the IV. */
7025 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7027 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
7028 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7029 decl = TREE_OPERAND (t, 0);
7030 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
7031 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7032 && OMP_CLAUSE_DECL (c) == decl)
7034 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7035 break;
7039 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
7040 simd ? ORT_SIMD : ORT_WORKSHARE);
7041 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
7042 gimplify_omp_ctxp->distribute = true;
7044 /* Handle OMP_FOR_INIT. */
7045 for_pre_body = NULL;
7046 if (simd && OMP_FOR_PRE_BODY (for_stmt))
7048 has_decl_expr = BITMAP_ALLOC (NULL);
7049 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
7050 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
7051 == VAR_DECL)
7053 t = OMP_FOR_PRE_BODY (for_stmt);
7054 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7056 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
7058 tree_stmt_iterator si;
7059 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
7060 tsi_next (&si))
7062 t = tsi_stmt (si);
7063 if (TREE_CODE (t) == DECL_EXPR
7064 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
7065 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7069 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7070 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
7072 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7074 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
7075 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
7076 NULL, NULL);
7077 gcc_assert (for_stmt != NULL_TREE);
7078 gimplify_omp_ctxp->combined_loop = true;
7081 for_body = NULL;
7082 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7083 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
7084 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7085 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
7086 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7088 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7089 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7090 decl = TREE_OPERAND (t, 0);
7091 gcc_assert (DECL_P (decl));
7092 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
7093 || POINTER_TYPE_P (TREE_TYPE (decl)));
7095 /* Make sure the iteration variable is private. */
7096 tree c = NULL_TREE;
7097 tree c2 = NULL_TREE;
7098 if (orig_for_stmt != for_stmt)
7099 /* Do this only on innermost construct for combined ones. */;
7100 else if (simd)
7102 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
7103 (splay_tree_key)decl);
7104 omp_is_private (gimplify_omp_ctxp, decl,
7105 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7106 != 1));
7107 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7108 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7109 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7111 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7112 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7113 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
7114 if ((has_decl_expr
7115 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7116 || omp_no_lastprivate (gimplify_omp_ctxp))
7118 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7119 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7121 OMP_CLAUSE_DECL (c) = decl;
7122 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7123 OMP_FOR_CLAUSES (for_stmt) = c;
7125 omp_add_variable (gimplify_omp_ctxp, decl, flags);
7126 struct gimplify_omp_ctx *outer
7127 = gimplify_omp_ctxp->outer_context;
7128 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7130 if (outer->region_type == ORT_WORKSHARE
7131 && outer->combined_loop)
7133 if (outer->outer_context
7134 && (outer->outer_context->region_type
7135 == ORT_COMBINED_PARALLEL))
7136 outer = outer->outer_context;
7137 else if (omp_check_private (outer, decl, false))
7138 outer = NULL;
7140 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7141 outer = NULL;
7142 if (outer)
7144 omp_add_variable (outer, decl,
7145 GOVD_LASTPRIVATE | GOVD_SEEN);
7146 if (outer->outer_context)
7147 omp_notice_variable (outer->outer_context, decl, true);
7151 else
7153 bool lastprivate
7154 = (!has_decl_expr
7155 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7156 && !omp_no_lastprivate (gimplify_omp_ctxp);
7157 struct gimplify_omp_ctx *outer
7158 = gimplify_omp_ctxp->outer_context;
7159 if (outer && lastprivate)
7161 if (outer->region_type == ORT_WORKSHARE
7162 && outer->combined_loop)
7164 if (outer->outer_context
7165 && (outer->outer_context->region_type
7166 == ORT_COMBINED_PARALLEL))
7167 outer = outer->outer_context;
7168 else if (omp_check_private (outer, decl, false))
7169 outer = NULL;
7171 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7172 outer = NULL;
7173 if (outer)
7175 omp_add_variable (outer, decl,
7176 GOVD_LASTPRIVATE | GOVD_SEEN);
7177 if (outer->outer_context)
7178 omp_notice_variable (outer->outer_context, decl, true);
7182 c = build_omp_clause (input_location,
7183 lastprivate ? OMP_CLAUSE_LASTPRIVATE
7184 : OMP_CLAUSE_PRIVATE);
7185 OMP_CLAUSE_DECL (c) = decl;
7186 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7187 OMP_FOR_CLAUSES (for_stmt) = c;
7188 omp_add_variable (gimplify_omp_ctxp, decl,
7189 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
7190 | GOVD_EXPLICIT | GOVD_SEEN);
7191 c = NULL_TREE;
7194 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7195 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7196 else
7197 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7199 /* If DECL is not a gimple register, create a temporary variable to act
7200 as an iteration counter. This is valid, since DECL cannot be
7201 modified in the body of the loop. Similarly for any iteration vars
7202 in simd with collapse > 1 where the iterator vars must be
7203 lastprivate. */
7204 if (orig_for_stmt != for_stmt)
7205 var = decl;
7206 else if (!is_gimple_reg (decl)
7207 || (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
7209 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7210 TREE_OPERAND (t, 0) = var;
7212 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
7214 if (simd && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7216 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7217 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
7218 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
7219 OMP_CLAUSE_DECL (c2) = var;
7220 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
7221 OMP_FOR_CLAUSES (for_stmt) = c2;
7222 omp_add_variable (gimplify_omp_ctxp, var,
7223 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
7224 if (c == NULL_TREE)
7226 c = c2;
7227 c2 = NULL_TREE;
7230 else
7231 omp_add_variable (gimplify_omp_ctxp, var,
7232 GOVD_PRIVATE | GOVD_SEEN);
7234 else
7235 var = decl;
7237 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7238 is_gimple_val, fb_rvalue);
7239 ret = MIN (ret, tret);
7240 if (ret == GS_ERROR)
7241 return ret;
7243 /* Handle OMP_FOR_COND. */
7244 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7245 gcc_assert (COMPARISON_CLASS_P (t));
7246 gcc_assert (TREE_OPERAND (t, 0) == decl);
7248 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7249 is_gimple_val, fb_rvalue);
7250 ret = MIN (ret, tret);
7252 /* Handle OMP_FOR_INCR. */
7253 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7254 switch (TREE_CODE (t))
7256 case PREINCREMENT_EXPR:
7257 case POSTINCREMENT_EXPR:
7259 tree decl = TREE_OPERAND (t, 0);
7260 /* c_omp_for_incr_canonicalize_ptr() should have been
7261 called to massage things appropriately. */
7262 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7264 if (orig_for_stmt != for_stmt)
7265 break;
7266 t = build_int_cst (TREE_TYPE (decl), 1);
7267 if (c)
7268 OMP_CLAUSE_LINEAR_STEP (c) = t;
7269 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7270 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7271 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7272 break;
7275 case PREDECREMENT_EXPR:
7276 case POSTDECREMENT_EXPR:
7277 /* c_omp_for_incr_canonicalize_ptr() should have been
7278 called to massage things appropriately. */
7279 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7280 if (orig_for_stmt != for_stmt)
7281 break;
7282 t = build_int_cst (TREE_TYPE (decl), -1);
7283 if (c)
7284 OMP_CLAUSE_LINEAR_STEP (c) = t;
7285 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7286 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7287 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7288 break;
7290 case MODIFY_EXPR:
7291 gcc_assert (TREE_OPERAND (t, 0) == decl);
7292 TREE_OPERAND (t, 0) = var;
7294 t = TREE_OPERAND (t, 1);
7295 switch (TREE_CODE (t))
7297 case PLUS_EXPR:
7298 if (TREE_OPERAND (t, 1) == decl)
7300 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7301 TREE_OPERAND (t, 0) = var;
7302 break;
7305 /* Fallthru. */
7306 case MINUS_EXPR:
7307 case POINTER_PLUS_EXPR:
7308 gcc_assert (TREE_OPERAND (t, 0) == decl);
7309 TREE_OPERAND (t, 0) = var;
7310 break;
7311 default:
7312 gcc_unreachable ();
7315 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7316 is_gimple_val, fb_rvalue);
7317 ret = MIN (ret, tret);
7318 if (c)
7320 tree step = TREE_OPERAND (t, 1);
7321 tree stept = TREE_TYPE (decl);
7322 if (POINTER_TYPE_P (stept))
7323 stept = sizetype;
7324 step = fold_convert (stept, step);
7325 if (TREE_CODE (t) == MINUS_EXPR)
7326 step = fold_build1 (NEGATE_EXPR, stept, step);
7327 OMP_CLAUSE_LINEAR_STEP (c) = step;
7328 if (step != TREE_OPERAND (t, 1))
7330 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7331 &for_pre_body, NULL,
7332 is_gimple_val, fb_rvalue);
7333 ret = MIN (ret, tret);
7336 break;
7338 default:
7339 gcc_unreachable ();
7342 if (c2)
7344 gcc_assert (c);
7345 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
7348 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7349 && orig_for_stmt == for_stmt)
7351 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7352 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7353 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7354 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7355 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
7356 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
7357 && OMP_CLAUSE_DECL (c) == decl)
7359 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7360 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7361 gcc_assert (TREE_OPERAND (t, 0) == var);
7362 t = TREE_OPERAND (t, 1);
7363 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7364 || TREE_CODE (t) == MINUS_EXPR
7365 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7366 gcc_assert (TREE_OPERAND (t, 0) == var);
7367 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7368 TREE_OPERAND (t, 1));
7369 gimple_seq *seq;
7370 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
7371 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
7372 else
7373 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
7374 gimplify_assign (decl, t, seq);
7379 BITMAP_FREE (has_decl_expr);
7381 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7383 if (orig_for_stmt != for_stmt)
7384 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7386 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7387 decl = TREE_OPERAND (t, 0);
7388 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7389 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7390 TREE_OPERAND (t, 0) = var;
7391 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7392 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7393 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7396 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt));
7398 int kind;
7399 switch (TREE_CODE (orig_for_stmt))
7401 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7402 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7403 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
7404 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
7405 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7406 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
7407 default:
7408 gcc_unreachable ();
7410 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7411 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7412 for_pre_body);
7413 if (orig_for_stmt != for_stmt)
7414 gimple_omp_for_set_combined_p (gfor, true);
7415 if (gimplify_omp_ctxp
7416 && (gimplify_omp_ctxp->combined_loop
7417 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7418 && gimplify_omp_ctxp->outer_context
7419 && gimplify_omp_ctxp->outer_context->combined_loop)))
7421 gimple_omp_for_set_combined_into_p (gfor, true);
7422 if (gimplify_omp_ctxp->combined_loop)
7423 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7424 else
7425 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7428 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7430 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7431 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7432 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7433 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7434 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7435 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7436 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7437 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7440 gimplify_seq_add_stmt (pre_p, gfor);
7441 if (ret != GS_ALL_DONE)
7442 return GS_ERROR;
7443 *expr_p = NULL_TREE;
7444 return GS_ALL_DONE;
7447 /* Gimplify the gross structure of several OMP constructs. */
7449 static void
7450 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7452 tree expr = *expr_p;
7453 gimple stmt;
7454 gimple_seq body = NULL;
7455 enum omp_region_type ort;
7457 switch (TREE_CODE (expr))
7459 case OMP_SECTIONS:
7460 case OMP_SINGLE:
7461 ort = ORT_WORKSHARE;
7462 break;
7463 case OACC_KERNELS:
7464 case OACC_PARALLEL:
7465 case OMP_TARGET:
7466 ort = ORT_TARGET;
7467 break;
7468 case OACC_DATA:
7469 case OMP_TARGET_DATA:
7470 ort = ORT_TARGET_DATA;
7471 break;
7472 case OMP_TEAMS:
7473 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
7474 break;
7475 default:
7476 gcc_unreachable ();
7478 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7479 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7481 push_gimplify_context ();
7482 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7483 if (gimple_code (g) == GIMPLE_BIND)
7484 pop_gimplify_context (g);
7485 else
7486 pop_gimplify_context (NULL);
7487 if (ort == ORT_TARGET_DATA)
7489 enum built_in_function end_ix;
7490 switch (TREE_CODE (expr))
7492 case OACC_DATA:
7493 end_ix = BUILT_IN_GOACC_DATA_END;
7494 break;
7495 case OMP_TARGET_DATA:
7496 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
7497 break;
7498 default:
7499 gcc_unreachable ();
7501 tree fn = builtin_decl_explicit (end_ix);
7502 g = gimple_build_call (fn, 0);
7503 gimple_seq cleanup = NULL;
7504 gimple_seq_add_stmt (&cleanup, g);
7505 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7506 body = NULL;
7507 gimple_seq_add_stmt (&body, g);
7510 else
7511 gimplify_and_add (OMP_BODY (expr), &body);
7512 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr));
7514 switch (TREE_CODE (expr))
7516 case OACC_DATA:
7517 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
7518 OMP_CLAUSES (expr));
7519 break;
7520 case OACC_KERNELS:
7521 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
7522 OMP_CLAUSES (expr));
7523 break;
7524 case OACC_PARALLEL:
7525 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
7526 OMP_CLAUSES (expr));
7527 break;
7528 case OMP_SECTIONS:
7529 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7530 break;
7531 case OMP_SINGLE:
7532 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7533 break;
7534 case OMP_TARGET:
7535 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7536 OMP_CLAUSES (expr));
7537 break;
7538 case OMP_TARGET_DATA:
7539 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7540 OMP_CLAUSES (expr));
7541 break;
7542 case OMP_TEAMS:
7543 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7544 break;
7545 default:
7546 gcc_unreachable ();
7549 gimplify_seq_add_stmt (pre_p, stmt);
7550 *expr_p = NULL_TREE;
7553 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
7554 target update constructs. */
7556 static void
7557 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7559 tree expr = *expr_p;
7560 int kind;
7561 gomp_target *stmt;
7563 switch (TREE_CODE (expr))
7565 case OACC_ENTER_DATA:
7566 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7567 break;
7568 case OACC_EXIT_DATA:
7569 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
7570 break;
7571 case OACC_UPDATE:
7572 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
7573 break;
7574 case OMP_TARGET_UPDATE:
7575 kind = GF_OMP_TARGET_KIND_UPDATE;
7576 break;
7577 default:
7578 gcc_unreachable ();
7580 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
7581 ORT_WORKSHARE);
7582 gimplify_adjust_omp_clauses (pre_p, &OMP_STANDALONE_CLAUSES (expr));
7583 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
7585 gimplify_seq_add_stmt (pre_p, stmt);
7586 *expr_p = NULL_TREE;
7589 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7590 stabilized the lhs of the atomic operation as *ADDR. Return true if
7591 EXPR is this stabilized form. */
7593 static bool
7594 goa_lhs_expr_p (tree expr, tree addr)
7596 /* Also include casts to other type variants. The C front end is fond
7597 of adding these for e.g. volatile variables. This is like
7598 STRIP_TYPE_NOPS but includes the main variant lookup. */
7599 STRIP_USELESS_TYPE_CONVERSION (expr);
7601 if (TREE_CODE (expr) == INDIRECT_REF)
7603 expr = TREE_OPERAND (expr, 0);
7604 while (expr != addr
7605 && (CONVERT_EXPR_P (expr)
7606 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7607 && TREE_CODE (expr) == TREE_CODE (addr)
7608 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7610 expr = TREE_OPERAND (expr, 0);
7611 addr = TREE_OPERAND (addr, 0);
7613 if (expr == addr)
7614 return true;
7615 return (TREE_CODE (addr) == ADDR_EXPR
7616 && TREE_CODE (expr) == ADDR_EXPR
7617 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7619 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7620 return true;
7621 return false;
7624 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7625 expression does not involve the lhs, evaluate it into a temporary.
7626 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7627 or -1 if an error was encountered. */
7629 static int
7630 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7631 tree lhs_var)
7633 tree expr = *expr_p;
7634 int saw_lhs;
7636 if (goa_lhs_expr_p (expr, lhs_addr))
7638 *expr_p = lhs_var;
7639 return 1;
7641 if (is_gimple_val (expr))
7642 return 0;
7644 saw_lhs = 0;
7645 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7647 case tcc_binary:
7648 case tcc_comparison:
7649 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7650 lhs_var);
7651 case tcc_unary:
7652 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7653 lhs_var);
7654 break;
7655 case tcc_expression:
7656 switch (TREE_CODE (expr))
7658 case TRUTH_ANDIF_EXPR:
7659 case TRUTH_ORIF_EXPR:
7660 case TRUTH_AND_EXPR:
7661 case TRUTH_OR_EXPR:
7662 case TRUTH_XOR_EXPR:
7663 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7664 lhs_addr, lhs_var);
7665 case TRUTH_NOT_EXPR:
7666 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7667 lhs_addr, lhs_var);
7668 break;
7669 case COMPOUND_EXPR:
7670 /* Break out any preevaluations from cp_build_modify_expr. */
7671 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7672 expr = TREE_OPERAND (expr, 1))
7673 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7674 *expr_p = expr;
7675 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7676 default:
7677 break;
7679 break;
7680 default:
7681 break;
7684 if (saw_lhs == 0)
7686 enum gimplify_status gs;
7687 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7688 if (gs != GS_ALL_DONE)
7689 saw_lhs = -1;
7692 return saw_lhs;
7695 /* Gimplify an OMP_ATOMIC statement. */
7697 static enum gimplify_status
7698 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7700 tree addr = TREE_OPERAND (*expr_p, 0);
7701 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7702 ? NULL : TREE_OPERAND (*expr_p, 1);
7703 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7704 tree tmp_load;
7705 gomp_atomic_load *loadstmt;
7706 gomp_atomic_store *storestmt;
7708 tmp_load = create_tmp_reg (type);
7709 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7710 return GS_ERROR;
7712 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7713 != GS_ALL_DONE)
7714 return GS_ERROR;
7716 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7717 gimplify_seq_add_stmt (pre_p, loadstmt);
7718 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7719 != GS_ALL_DONE)
7720 return GS_ERROR;
7722 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7723 rhs = tmp_load;
7724 storestmt = gimple_build_omp_atomic_store (rhs);
7725 gimplify_seq_add_stmt (pre_p, storestmt);
7726 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7728 gimple_omp_atomic_set_seq_cst (loadstmt);
7729 gimple_omp_atomic_set_seq_cst (storestmt);
7731 switch (TREE_CODE (*expr_p))
7733 case OMP_ATOMIC_READ:
7734 case OMP_ATOMIC_CAPTURE_OLD:
7735 *expr_p = tmp_load;
7736 gimple_omp_atomic_set_need_value (loadstmt);
7737 break;
7738 case OMP_ATOMIC_CAPTURE_NEW:
7739 *expr_p = rhs;
7740 gimple_omp_atomic_set_need_value (storestmt);
7741 break;
7742 default:
7743 *expr_p = NULL;
7744 break;
7747 return GS_ALL_DONE;
7750 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7751 body, and adding some EH bits. */
7753 static enum gimplify_status
7754 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7756 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7757 gimple body_stmt;
7758 gtransaction *trans_stmt;
7759 gimple_seq body = NULL;
7760 int subcode = 0;
7762 /* Wrap the transaction body in a BIND_EXPR so we have a context
7763 where to put decls for OMP. */
7764 if (TREE_CODE (tbody) != BIND_EXPR)
7766 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7767 TREE_SIDE_EFFECTS (bind) = 1;
7768 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7769 TRANSACTION_EXPR_BODY (expr) = bind;
7772 push_gimplify_context ();
7773 temp = voidify_wrapper_expr (*expr_p, NULL);
7775 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7776 pop_gimplify_context (body_stmt);
7778 trans_stmt = gimple_build_transaction (body, NULL);
7779 if (TRANSACTION_EXPR_OUTER (expr))
7780 subcode = GTMA_IS_OUTER;
7781 else if (TRANSACTION_EXPR_RELAXED (expr))
7782 subcode = GTMA_IS_RELAXED;
7783 gimple_transaction_set_subcode (trans_stmt, subcode);
7785 gimplify_seq_add_stmt (pre_p, trans_stmt);
7787 if (temp)
7789 *expr_p = temp;
7790 return GS_OK;
7793 *expr_p = NULL_TREE;
7794 return GS_ALL_DONE;
7797 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7798 expression produces a value to be used as an operand inside a GIMPLE
7799 statement, the value will be stored back in *EXPR_P. This value will
7800 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7801 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7802 emitted in PRE_P and POST_P.
7804 Additionally, this process may overwrite parts of the input
7805 expression during gimplification. Ideally, it should be
7806 possible to do non-destructive gimplification.
7808 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7809 the expression needs to evaluate to a value to be used as
7810 an operand in a GIMPLE statement, this value will be stored in
7811 *EXPR_P on exit. This happens when the caller specifies one
7812 of fb_lvalue or fb_rvalue fallback flags.
7814 PRE_P will contain the sequence of GIMPLE statements corresponding
7815 to the evaluation of EXPR and all the side-effects that must
7816 be executed before the main expression. On exit, the last
7817 statement of PRE_P is the core statement being gimplified. For
7818 instance, when gimplifying 'if (++a)' the last statement in
7819 PRE_P will be 'if (t.1)' where t.1 is the result of
7820 pre-incrementing 'a'.
7822 POST_P will contain the sequence of GIMPLE statements corresponding
7823 to the evaluation of all the side-effects that must be executed
7824 after the main expression. If this is NULL, the post
7825 side-effects are stored at the end of PRE_P.
7827 The reason why the output is split in two is to handle post
7828 side-effects explicitly. In some cases, an expression may have
7829 inner and outer post side-effects which need to be emitted in
7830 an order different from the one given by the recursive
7831 traversal. For instance, for the expression (*p--)++ the post
7832 side-effects of '--' must actually occur *after* the post
7833 side-effects of '++'. However, gimplification will first visit
7834 the inner expression, so if a separate POST sequence was not
7835 used, the resulting sequence would be:
7837 1 t.1 = *p
7838 2 p = p - 1
7839 3 t.2 = t.1 + 1
7840 4 *p = t.2
7842 However, the post-decrement operation in line #2 must not be
7843 evaluated until after the store to *p at line #4, so the
7844 correct sequence should be:
7846 1 t.1 = *p
7847 2 t.2 = t.1 + 1
7848 3 *p = t.2
7849 4 p = p - 1
7851 So, by specifying a separate post queue, it is possible
7852 to emit the post side-effects in the correct order.
7853 If POST_P is NULL, an internal queue will be used. Before
7854 returning to the caller, the sequence POST_P is appended to
7855 the main output sequence PRE_P.
7857 GIMPLE_TEST_F points to a function that takes a tree T and
7858 returns nonzero if T is in the GIMPLE form requested by the
7859 caller. The GIMPLE predicates are in gimple.c.
7861 FALLBACK tells the function what sort of a temporary we want if
7862 gimplification cannot produce an expression that complies with
7863 GIMPLE_TEST_F.
7865 fb_none means that no temporary should be generated
7866 fb_rvalue means that an rvalue is OK to generate
7867 fb_lvalue means that an lvalue is OK to generate
7868 fb_either means that either is OK, but an lvalue is preferable.
7869 fb_mayfail means that gimplification may fail (in which case
7870 GS_ERROR will be returned)
7872 The return value is either GS_ERROR or GS_ALL_DONE, since this
7873 function iterates until EXPR is completely gimplified or an error
7874 occurs. */
7876 enum gimplify_status
7877 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7878 bool (*gimple_test_f) (tree), fallback_t fallback)
7880 tree tmp;
7881 gimple_seq internal_pre = NULL;
7882 gimple_seq internal_post = NULL;
7883 tree save_expr;
7884 bool is_statement;
7885 location_t saved_location;
7886 enum gimplify_status ret;
7887 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7889 save_expr = *expr_p;
7890 if (save_expr == NULL_TREE)
7891 return GS_ALL_DONE;
7893 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7894 is_statement = gimple_test_f == is_gimple_stmt;
7895 if (is_statement)
7896 gcc_assert (pre_p);
7898 /* Consistency checks. */
7899 if (gimple_test_f == is_gimple_reg)
7900 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7901 else if (gimple_test_f == is_gimple_val
7902 || gimple_test_f == is_gimple_call_addr
7903 || gimple_test_f == is_gimple_condexpr
7904 || gimple_test_f == is_gimple_mem_rhs
7905 || gimple_test_f == is_gimple_mem_rhs_or_call
7906 || gimple_test_f == is_gimple_reg_rhs
7907 || gimple_test_f == is_gimple_reg_rhs_or_call
7908 || gimple_test_f == is_gimple_asm_val
7909 || gimple_test_f == is_gimple_mem_ref_addr)
7910 gcc_assert (fallback & fb_rvalue);
7911 else if (gimple_test_f == is_gimple_min_lval
7912 || gimple_test_f == is_gimple_lvalue)
7913 gcc_assert (fallback & fb_lvalue);
7914 else if (gimple_test_f == is_gimple_addressable)
7915 gcc_assert (fallback & fb_either);
7916 else if (gimple_test_f == is_gimple_stmt)
7917 gcc_assert (fallback == fb_none);
7918 else
7920 /* We should have recognized the GIMPLE_TEST_F predicate to
7921 know what kind of fallback to use in case a temporary is
7922 needed to hold the value or address of *EXPR_P. */
7923 gcc_unreachable ();
7926 /* We used to check the predicate here and return immediately if it
7927 succeeds. This is wrong; the design is for gimplification to be
7928 idempotent, and for the predicates to only test for valid forms, not
7929 whether they are fully simplified. */
7930 if (pre_p == NULL)
7931 pre_p = &internal_pre;
7933 if (post_p == NULL)
7934 post_p = &internal_post;
7936 /* Remember the last statements added to PRE_P and POST_P. Every
7937 new statement added by the gimplification helpers needs to be
7938 annotated with location information. To centralize the
7939 responsibility, we remember the last statement that had been
7940 added to both queues before gimplifying *EXPR_P. If
7941 gimplification produces new statements in PRE_P and POST_P, those
7942 statements will be annotated with the same location information
7943 as *EXPR_P. */
7944 pre_last_gsi = gsi_last (*pre_p);
7945 post_last_gsi = gsi_last (*post_p);
7947 saved_location = input_location;
7948 if (save_expr != error_mark_node
7949 && EXPR_HAS_LOCATION (*expr_p))
7950 input_location = EXPR_LOCATION (*expr_p);
7952 /* Loop over the specific gimplifiers until the toplevel node
7953 remains the same. */
7956 /* Strip away as many useless type conversions as possible
7957 at the toplevel. */
7958 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7960 /* Remember the expr. */
7961 save_expr = *expr_p;
7963 /* Die, die, die, my darling. */
7964 if (save_expr == error_mark_node
7965 || (TREE_TYPE (save_expr)
7966 && TREE_TYPE (save_expr) == error_mark_node))
7968 ret = GS_ERROR;
7969 break;
7972 /* Do any language-specific gimplification. */
7973 ret = ((enum gimplify_status)
7974 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7975 if (ret == GS_OK)
7977 if (*expr_p == NULL_TREE)
7978 break;
7979 if (*expr_p != save_expr)
7980 continue;
7982 else if (ret != GS_UNHANDLED)
7983 break;
7985 /* Make sure that all the cases set 'ret' appropriately. */
7986 ret = GS_UNHANDLED;
7987 switch (TREE_CODE (*expr_p))
7989 /* First deal with the special cases. */
7991 case POSTINCREMENT_EXPR:
7992 case POSTDECREMENT_EXPR:
7993 case PREINCREMENT_EXPR:
7994 case PREDECREMENT_EXPR:
7995 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7996 fallback != fb_none,
7997 TREE_TYPE (*expr_p));
7998 break;
8000 case VIEW_CONVERT_EXPR:
8001 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
8002 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
8004 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8005 post_p, is_gimple_val, fb_rvalue);
8006 recalculate_side_effects (*expr_p);
8007 break;
8009 /* Fallthru. */
8011 case ARRAY_REF:
8012 case ARRAY_RANGE_REF:
8013 case REALPART_EXPR:
8014 case IMAGPART_EXPR:
8015 case COMPONENT_REF:
8016 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
8017 fallback ? fallback : fb_rvalue);
8018 break;
8020 case COND_EXPR:
8021 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
8023 /* C99 code may assign to an array in a structure value of a
8024 conditional expression, and this has undefined behavior
8025 only on execution, so create a temporary if an lvalue is
8026 required. */
8027 if (fallback == fb_lvalue)
8029 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8030 mark_addressable (*expr_p);
8031 ret = GS_OK;
8033 break;
8035 case CALL_EXPR:
8036 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
8038 /* C99 code may assign to an array in a structure returned
8039 from a function, and this has undefined behavior only on
8040 execution, so create a temporary if an lvalue is
8041 required. */
8042 if (fallback == fb_lvalue)
8044 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8045 mark_addressable (*expr_p);
8046 ret = GS_OK;
8048 break;
8050 case TREE_LIST:
8051 gcc_unreachable ();
8053 case COMPOUND_EXPR:
8054 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
8055 break;
8057 case COMPOUND_LITERAL_EXPR:
8058 ret = gimplify_compound_literal_expr (expr_p, pre_p,
8059 gimple_test_f, fallback);
8060 break;
8062 case MODIFY_EXPR:
8063 case INIT_EXPR:
8064 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
8065 fallback != fb_none);
8066 break;
8068 case TRUTH_ANDIF_EXPR:
8069 case TRUTH_ORIF_EXPR:
8071 /* Preserve the original type of the expression and the
8072 source location of the outer expression. */
8073 tree org_type = TREE_TYPE (*expr_p);
8074 *expr_p = gimple_boolify (*expr_p);
8075 *expr_p = build3_loc (input_location, COND_EXPR,
8076 org_type, *expr_p,
8077 fold_convert_loc
8078 (input_location,
8079 org_type, boolean_true_node),
8080 fold_convert_loc
8081 (input_location,
8082 org_type, boolean_false_node));
8083 ret = GS_OK;
8084 break;
8087 case TRUTH_NOT_EXPR:
8089 tree type = TREE_TYPE (*expr_p);
8090 /* The parsers are careful to generate TRUTH_NOT_EXPR
8091 only with operands that are always zero or one.
8092 We do not fold here but handle the only interesting case
8093 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
8094 *expr_p = gimple_boolify (*expr_p);
8095 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
8096 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
8097 TREE_TYPE (*expr_p),
8098 TREE_OPERAND (*expr_p, 0));
8099 else
8100 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
8101 TREE_TYPE (*expr_p),
8102 TREE_OPERAND (*expr_p, 0),
8103 build_int_cst (TREE_TYPE (*expr_p), 1));
8104 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
8105 *expr_p = fold_convert_loc (input_location, type, *expr_p);
8106 ret = GS_OK;
8107 break;
8110 case ADDR_EXPR:
8111 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
8112 break;
8114 case ANNOTATE_EXPR:
8116 tree cond = TREE_OPERAND (*expr_p, 0);
8117 tree kind = TREE_OPERAND (*expr_p, 1);
8118 tree type = TREE_TYPE (cond);
8119 if (!INTEGRAL_TYPE_P (type))
8121 *expr_p = cond;
8122 ret = GS_OK;
8123 break;
8125 tree tmp = create_tmp_var (type);
8126 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
8127 gcall *call
8128 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
8129 gimple_call_set_lhs (call, tmp);
8130 gimplify_seq_add_stmt (pre_p, call);
8131 *expr_p = tmp;
8132 ret = GS_ALL_DONE;
8133 break;
8136 case VA_ARG_EXPR:
8137 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
8138 break;
8140 CASE_CONVERT:
8141 if (IS_EMPTY_STMT (*expr_p))
8143 ret = GS_ALL_DONE;
8144 break;
8147 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
8148 || fallback == fb_none)
8150 /* Just strip a conversion to void (or in void context) and
8151 try again. */
8152 *expr_p = TREE_OPERAND (*expr_p, 0);
8153 ret = GS_OK;
8154 break;
8157 ret = gimplify_conversion (expr_p);
8158 if (ret == GS_ERROR)
8159 break;
8160 if (*expr_p != save_expr)
8161 break;
8162 /* FALLTHRU */
8164 case FIX_TRUNC_EXPR:
8165 /* unary_expr: ... | '(' cast ')' val | ... */
8166 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8167 is_gimple_val, fb_rvalue);
8168 recalculate_side_effects (*expr_p);
8169 break;
8171 case INDIRECT_REF:
8173 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
8174 bool notrap = TREE_THIS_NOTRAP (*expr_p);
8175 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
8177 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
8178 if (*expr_p != save_expr)
8180 ret = GS_OK;
8181 break;
8184 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8185 is_gimple_reg, fb_rvalue);
8186 if (ret == GS_ERROR)
8187 break;
8189 recalculate_side_effects (*expr_p);
8190 *expr_p = fold_build2_loc (input_location, MEM_REF,
8191 TREE_TYPE (*expr_p),
8192 TREE_OPERAND (*expr_p, 0),
8193 build_int_cst (saved_ptr_type, 0));
8194 TREE_THIS_VOLATILE (*expr_p) = volatilep;
8195 TREE_THIS_NOTRAP (*expr_p) = notrap;
8196 ret = GS_OK;
8197 break;
8200 /* We arrive here through the various re-gimplifcation paths. */
8201 case MEM_REF:
8202 /* First try re-folding the whole thing. */
8203 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
8204 TREE_OPERAND (*expr_p, 0),
8205 TREE_OPERAND (*expr_p, 1));
8206 if (tmp)
8208 *expr_p = tmp;
8209 recalculate_side_effects (*expr_p);
8210 ret = GS_OK;
8211 break;
8213 /* Avoid re-gimplifying the address operand if it is already
8214 in suitable form. Re-gimplifying would mark the address
8215 operand addressable. Always gimplify when not in SSA form
8216 as we still may have to gimplify decls with value-exprs. */
8217 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
8218 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
8220 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8221 is_gimple_mem_ref_addr, fb_rvalue);
8222 if (ret == GS_ERROR)
8223 break;
8225 recalculate_side_effects (*expr_p);
8226 ret = GS_ALL_DONE;
8227 break;
8229 /* Constants need not be gimplified. */
8230 case INTEGER_CST:
8231 case REAL_CST:
8232 case FIXED_CST:
8233 case STRING_CST:
8234 case COMPLEX_CST:
8235 case VECTOR_CST:
8236 /* Drop the overflow flag on constants, we do not want
8237 that in the GIMPLE IL. */
8238 if (TREE_OVERFLOW_P (*expr_p))
8239 *expr_p = drop_tree_overflow (*expr_p);
8240 ret = GS_ALL_DONE;
8241 break;
8243 case CONST_DECL:
8244 /* If we require an lvalue, such as for ADDR_EXPR, retain the
8245 CONST_DECL node. Otherwise the decl is replaceable by its
8246 value. */
8247 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
8248 if (fallback & fb_lvalue)
8249 ret = GS_ALL_DONE;
8250 else
8252 *expr_p = DECL_INITIAL (*expr_p);
8253 ret = GS_OK;
8255 break;
8257 case DECL_EXPR:
8258 ret = gimplify_decl_expr (expr_p, pre_p);
8259 break;
8261 case BIND_EXPR:
8262 ret = gimplify_bind_expr (expr_p, pre_p);
8263 break;
8265 case LOOP_EXPR:
8266 ret = gimplify_loop_expr (expr_p, pre_p);
8267 break;
8269 case SWITCH_EXPR:
8270 ret = gimplify_switch_expr (expr_p, pre_p);
8271 break;
8273 case EXIT_EXPR:
8274 ret = gimplify_exit_expr (expr_p);
8275 break;
8277 case GOTO_EXPR:
8278 /* If the target is not LABEL, then it is a computed jump
8279 and the target needs to be gimplified. */
8280 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
8282 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
8283 NULL, is_gimple_val, fb_rvalue);
8284 if (ret == GS_ERROR)
8285 break;
8287 gimplify_seq_add_stmt (pre_p,
8288 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
8289 ret = GS_ALL_DONE;
8290 break;
8292 case PREDICT_EXPR:
8293 gimplify_seq_add_stmt (pre_p,
8294 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
8295 PREDICT_EXPR_OUTCOME (*expr_p)));
8296 ret = GS_ALL_DONE;
8297 break;
8299 case LABEL_EXPR:
8300 ret = GS_ALL_DONE;
8301 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
8302 == current_function_decl);
8303 gimplify_seq_add_stmt (pre_p,
8304 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
8305 break;
8307 case CASE_LABEL_EXPR:
8308 ret = gimplify_case_label_expr (expr_p, pre_p);
8309 break;
8311 case RETURN_EXPR:
8312 ret = gimplify_return_expr (*expr_p, pre_p);
8313 break;
8315 case CONSTRUCTOR:
8316 /* Don't reduce this in place; let gimplify_init_constructor work its
8317 magic. Buf if we're just elaborating this for side effects, just
8318 gimplify any element that has side-effects. */
8319 if (fallback == fb_none)
8321 unsigned HOST_WIDE_INT ix;
8322 tree val;
8323 tree temp = NULL_TREE;
8324 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
8325 if (TREE_SIDE_EFFECTS (val))
8326 append_to_statement_list (val, &temp);
8328 *expr_p = temp;
8329 ret = temp ? GS_OK : GS_ALL_DONE;
8331 /* C99 code may assign to an array in a constructed
8332 structure or union, and this has undefined behavior only
8333 on execution, so create a temporary if an lvalue is
8334 required. */
8335 else if (fallback == fb_lvalue)
8337 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8338 mark_addressable (*expr_p);
8339 ret = GS_OK;
8341 else
8342 ret = GS_ALL_DONE;
8343 break;
8345 /* The following are special cases that are not handled by the
8346 original GIMPLE grammar. */
8348 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8349 eliminated. */
8350 case SAVE_EXPR:
8351 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8352 break;
8354 case BIT_FIELD_REF:
8355 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8356 post_p, is_gimple_lvalue, fb_either);
8357 recalculate_side_effects (*expr_p);
8358 break;
8360 case TARGET_MEM_REF:
8362 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8364 if (TMR_BASE (*expr_p))
8365 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8366 post_p, is_gimple_mem_ref_addr, fb_either);
8367 if (TMR_INDEX (*expr_p))
8368 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8369 post_p, is_gimple_val, fb_rvalue);
8370 if (TMR_INDEX2 (*expr_p))
8371 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8372 post_p, is_gimple_val, fb_rvalue);
8373 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8374 ret = MIN (r0, r1);
8376 break;
8378 case NON_LVALUE_EXPR:
8379 /* This should have been stripped above. */
8380 gcc_unreachable ();
8382 case ASM_EXPR:
8383 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8384 break;
8386 case TRY_FINALLY_EXPR:
8387 case TRY_CATCH_EXPR:
8389 gimple_seq eval, cleanup;
8390 gtry *try_;
8392 /* Calls to destructors are generated automatically in FINALLY/CATCH
8393 block. They should have location as UNKNOWN_LOCATION. However,
8394 gimplify_call_expr will reset these call stmts to input_location
8395 if it finds stmt's location is unknown. To prevent resetting for
8396 destructors, we set the input_location to unknown.
8397 Note that this only affects the destructor calls in FINALLY/CATCH
8398 block, and will automatically reset to its original value by the
8399 end of gimplify_expr. */
8400 input_location = UNKNOWN_LOCATION;
8401 eval = cleanup = NULL;
8402 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8403 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8404 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8405 if (gimple_seq_empty_p (cleanup))
8407 gimple_seq_add_seq (pre_p, eval);
8408 ret = GS_ALL_DONE;
8409 break;
8411 try_ = gimple_build_try (eval, cleanup,
8412 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8413 ? GIMPLE_TRY_FINALLY
8414 : GIMPLE_TRY_CATCH);
8415 if (EXPR_HAS_LOCATION (save_expr))
8416 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8417 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8418 gimple_set_location (try_, saved_location);
8419 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8420 gimple_try_set_catch_is_cleanup (try_,
8421 TRY_CATCH_IS_CLEANUP (*expr_p));
8422 gimplify_seq_add_stmt (pre_p, try_);
8423 ret = GS_ALL_DONE;
8424 break;
8427 case CLEANUP_POINT_EXPR:
8428 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8429 break;
8431 case TARGET_EXPR:
8432 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8433 break;
8435 case CATCH_EXPR:
8437 gimple c;
8438 gimple_seq handler = NULL;
8439 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8440 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8441 gimplify_seq_add_stmt (pre_p, c);
8442 ret = GS_ALL_DONE;
8443 break;
8446 case EH_FILTER_EXPR:
8448 gimple ehf;
8449 gimple_seq failure = NULL;
8451 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8452 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8453 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8454 gimplify_seq_add_stmt (pre_p, ehf);
8455 ret = GS_ALL_DONE;
8456 break;
8459 case OBJ_TYPE_REF:
8461 enum gimplify_status r0, r1;
8462 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8463 post_p, is_gimple_val, fb_rvalue);
8464 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8465 post_p, is_gimple_val, fb_rvalue);
8466 TREE_SIDE_EFFECTS (*expr_p) = 0;
8467 ret = MIN (r0, r1);
8469 break;
8471 case LABEL_DECL:
8472 /* We get here when taking the address of a label. We mark
8473 the label as "forced"; meaning it can never be removed and
8474 it is a potential target for any computed goto. */
8475 FORCED_LABEL (*expr_p) = 1;
8476 ret = GS_ALL_DONE;
8477 break;
8479 case STATEMENT_LIST:
8480 ret = gimplify_statement_list (expr_p, pre_p);
8481 break;
8483 case WITH_SIZE_EXPR:
8485 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8486 post_p == &internal_post ? NULL : post_p,
8487 gimple_test_f, fallback);
8488 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8489 is_gimple_val, fb_rvalue);
8490 ret = GS_ALL_DONE;
8492 break;
8494 case VAR_DECL:
8495 case PARM_DECL:
8496 ret = gimplify_var_or_parm_decl (expr_p);
8497 break;
8499 case RESULT_DECL:
8500 /* When within an OMP context, notice uses of variables. */
8501 if (gimplify_omp_ctxp)
8502 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8503 ret = GS_ALL_DONE;
8504 break;
8506 case SSA_NAME:
8507 /* Allow callbacks into the gimplifier during optimization. */
8508 ret = GS_ALL_DONE;
8509 break;
8511 case OMP_PARALLEL:
8512 gimplify_omp_parallel (expr_p, pre_p);
8513 ret = GS_ALL_DONE;
8514 break;
8516 case OMP_TASK:
8517 gimplify_omp_task (expr_p, pre_p);
8518 ret = GS_ALL_DONE;
8519 break;
8521 case OMP_FOR:
8522 case OMP_SIMD:
8523 case CILK_SIMD:
8524 case CILK_FOR:
8525 case OMP_DISTRIBUTE:
8526 case OACC_LOOP:
8527 ret = gimplify_omp_for (expr_p, pre_p);
8528 break;
8530 case OACC_CACHE:
8531 gimplify_oacc_cache (expr_p, pre_p);
8532 ret = GS_ALL_DONE;
8533 break;
8535 case OACC_HOST_DATA:
8536 case OACC_DECLARE:
8537 sorry ("directive not yet implemented");
8538 ret = GS_ALL_DONE;
8539 break;
8541 case OACC_KERNELS:
8542 if (OACC_KERNELS_COMBINED (*expr_p))
8543 sorry ("directive not yet implemented");
8544 else
8545 gimplify_omp_workshare (expr_p, pre_p);
8546 ret = GS_ALL_DONE;
8547 break;
8549 case OACC_PARALLEL:
8550 if (OACC_PARALLEL_COMBINED (*expr_p))
8551 sorry ("directive not yet implemented");
8552 else
8553 gimplify_omp_workshare (expr_p, pre_p);
8554 ret = GS_ALL_DONE;
8555 break;
8557 case OACC_DATA:
8558 case OMP_SECTIONS:
8559 case OMP_SINGLE:
8560 case OMP_TARGET:
8561 case OMP_TARGET_DATA:
8562 case OMP_TEAMS:
8563 gimplify_omp_workshare (expr_p, pre_p);
8564 ret = GS_ALL_DONE;
8565 break;
8567 case OACC_ENTER_DATA:
8568 case OACC_EXIT_DATA:
8569 case OACC_UPDATE:
8570 case OMP_TARGET_UPDATE:
8571 gimplify_omp_target_update (expr_p, pre_p);
8572 ret = GS_ALL_DONE;
8573 break;
8575 case OMP_SECTION:
8576 case OMP_MASTER:
8577 case OMP_TASKGROUP:
8578 case OMP_ORDERED:
8579 case OMP_CRITICAL:
8581 gimple_seq body = NULL;
8582 gimple g;
8584 gimplify_and_add (OMP_BODY (*expr_p), &body);
8585 switch (TREE_CODE (*expr_p))
8587 case OMP_SECTION:
8588 g = gimple_build_omp_section (body);
8589 break;
8590 case OMP_MASTER:
8591 g = gimple_build_omp_master (body);
8592 break;
8593 case OMP_TASKGROUP:
8595 gimple_seq cleanup = NULL;
8596 tree fn
8597 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8598 g = gimple_build_call (fn, 0);
8599 gimple_seq_add_stmt (&cleanup, g);
8600 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8601 body = NULL;
8602 gimple_seq_add_stmt (&body, g);
8603 g = gimple_build_omp_taskgroup (body);
8605 break;
8606 case OMP_ORDERED:
8607 g = gimple_build_omp_ordered (body);
8608 break;
8609 case OMP_CRITICAL:
8610 g = gimple_build_omp_critical (body,
8611 OMP_CRITICAL_NAME (*expr_p));
8612 break;
8613 default:
8614 gcc_unreachable ();
8616 gimplify_seq_add_stmt (pre_p, g);
8617 ret = GS_ALL_DONE;
8618 break;
8621 case OMP_ATOMIC:
8622 case OMP_ATOMIC_READ:
8623 case OMP_ATOMIC_CAPTURE_OLD:
8624 case OMP_ATOMIC_CAPTURE_NEW:
8625 ret = gimplify_omp_atomic (expr_p, pre_p);
8626 break;
8628 case TRANSACTION_EXPR:
8629 ret = gimplify_transaction (expr_p, pre_p);
8630 break;
8632 case TRUTH_AND_EXPR:
8633 case TRUTH_OR_EXPR:
8634 case TRUTH_XOR_EXPR:
8636 tree orig_type = TREE_TYPE (*expr_p);
8637 tree new_type, xop0, xop1;
8638 *expr_p = gimple_boolify (*expr_p);
8639 new_type = TREE_TYPE (*expr_p);
8640 if (!useless_type_conversion_p (orig_type, new_type))
8642 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8643 ret = GS_OK;
8644 break;
8647 /* Boolified binary truth expressions are semantically equivalent
8648 to bitwise binary expressions. Canonicalize them to the
8649 bitwise variant. */
8650 switch (TREE_CODE (*expr_p))
8652 case TRUTH_AND_EXPR:
8653 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8654 break;
8655 case TRUTH_OR_EXPR:
8656 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8657 break;
8658 case TRUTH_XOR_EXPR:
8659 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8660 break;
8661 default:
8662 break;
8664 /* Now make sure that operands have compatible type to
8665 expression's new_type. */
8666 xop0 = TREE_OPERAND (*expr_p, 0);
8667 xop1 = TREE_OPERAND (*expr_p, 1);
8668 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8669 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8670 new_type,
8671 xop0);
8672 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8673 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8674 new_type,
8675 xop1);
8676 /* Continue classified as tcc_binary. */
8677 goto expr_2;
8680 case FMA_EXPR:
8681 case VEC_COND_EXPR:
8682 case VEC_PERM_EXPR:
8683 /* Classified as tcc_expression. */
8684 goto expr_3;
8686 case POINTER_PLUS_EXPR:
8688 enum gimplify_status r0, r1;
8689 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8690 post_p, is_gimple_val, fb_rvalue);
8691 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8692 post_p, is_gimple_val, fb_rvalue);
8693 recalculate_side_effects (*expr_p);
8694 ret = MIN (r0, r1);
8695 break;
8698 case CILK_SYNC_STMT:
8700 if (!fn_contains_cilk_spawn_p (cfun))
8702 error_at (EXPR_LOCATION (*expr_p),
8703 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8704 ret = GS_ERROR;
8706 else
8708 gimplify_cilk_sync (expr_p, pre_p);
8709 ret = GS_ALL_DONE;
8711 break;
8714 default:
8715 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8717 case tcc_comparison:
8718 /* Handle comparison of objects of non scalar mode aggregates
8719 with a call to memcmp. It would be nice to only have to do
8720 this for variable-sized objects, but then we'd have to allow
8721 the same nest of reference nodes we allow for MODIFY_EXPR and
8722 that's too complex.
8724 Compare scalar mode aggregates as scalar mode values. Using
8725 memcmp for them would be very inefficient at best, and is
8726 plain wrong if bitfields are involved. */
8728 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8730 /* Vector comparisons need no boolification. */
8731 if (TREE_CODE (type) == VECTOR_TYPE)
8732 goto expr_2;
8733 else if (!AGGREGATE_TYPE_P (type))
8735 tree org_type = TREE_TYPE (*expr_p);
8736 *expr_p = gimple_boolify (*expr_p);
8737 if (!useless_type_conversion_p (org_type,
8738 TREE_TYPE (*expr_p)))
8740 *expr_p = fold_convert_loc (input_location,
8741 org_type, *expr_p);
8742 ret = GS_OK;
8744 else
8745 goto expr_2;
8747 else if (TYPE_MODE (type) != BLKmode)
8748 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8749 else
8750 ret = gimplify_variable_sized_compare (expr_p);
8752 break;
8755 /* If *EXPR_P does not need to be special-cased, handle it
8756 according to its class. */
8757 case tcc_unary:
8758 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8759 post_p, is_gimple_val, fb_rvalue);
8760 break;
8762 case tcc_binary:
8763 expr_2:
8765 enum gimplify_status r0, r1;
8767 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8768 post_p, is_gimple_val, fb_rvalue);
8769 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8770 post_p, is_gimple_val, fb_rvalue);
8772 ret = MIN (r0, r1);
8773 break;
8776 expr_3:
8778 enum gimplify_status r0, r1, r2;
8780 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8781 post_p, is_gimple_val, fb_rvalue);
8782 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8783 post_p, is_gimple_val, fb_rvalue);
8784 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8785 post_p, is_gimple_val, fb_rvalue);
8787 ret = MIN (MIN (r0, r1), r2);
8788 break;
8791 case tcc_declaration:
8792 case tcc_constant:
8793 ret = GS_ALL_DONE;
8794 goto dont_recalculate;
8796 default:
8797 gcc_unreachable ();
8800 recalculate_side_effects (*expr_p);
8802 dont_recalculate:
8803 break;
8806 gcc_assert (*expr_p || ret != GS_OK);
8808 while (ret == GS_OK);
8810 /* If we encountered an error_mark somewhere nested inside, either
8811 stub out the statement or propagate the error back out. */
8812 if (ret == GS_ERROR)
8814 if (is_statement)
8815 *expr_p = NULL;
8816 goto out;
8819 /* This was only valid as a return value from the langhook, which
8820 we handled. Make sure it doesn't escape from any other context. */
8821 gcc_assert (ret != GS_UNHANDLED);
8823 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8825 /* We aren't looking for a value, and we don't have a valid
8826 statement. If it doesn't have side-effects, throw it away. */
8827 if (!TREE_SIDE_EFFECTS (*expr_p))
8828 *expr_p = NULL;
8829 else if (!TREE_THIS_VOLATILE (*expr_p))
8831 /* This is probably a _REF that contains something nested that
8832 has side effects. Recurse through the operands to find it. */
8833 enum tree_code code = TREE_CODE (*expr_p);
8835 switch (code)
8837 case COMPONENT_REF:
8838 case REALPART_EXPR:
8839 case IMAGPART_EXPR:
8840 case VIEW_CONVERT_EXPR:
8841 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8842 gimple_test_f, fallback);
8843 break;
8845 case ARRAY_REF:
8846 case ARRAY_RANGE_REF:
8847 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8848 gimple_test_f, fallback);
8849 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8850 gimple_test_f, fallback);
8851 break;
8853 default:
8854 /* Anything else with side-effects must be converted to
8855 a valid statement before we get here. */
8856 gcc_unreachable ();
8859 *expr_p = NULL;
8861 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8862 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8864 /* Historically, the compiler has treated a bare reference
8865 to a non-BLKmode volatile lvalue as forcing a load. */
8866 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8868 /* Normally, we do not want to create a temporary for a
8869 TREE_ADDRESSABLE type because such a type should not be
8870 copied by bitwise-assignment. However, we make an
8871 exception here, as all we are doing here is ensuring that
8872 we read the bytes that make up the type. We use
8873 create_tmp_var_raw because create_tmp_var will abort when
8874 given a TREE_ADDRESSABLE type. */
8875 tree tmp = create_tmp_var_raw (type, "vol");
8876 gimple_add_tmp_var (tmp);
8877 gimplify_assign (tmp, *expr_p, pre_p);
8878 *expr_p = NULL;
8880 else
8881 /* We can't do anything useful with a volatile reference to
8882 an incomplete type, so just throw it away. Likewise for
8883 a BLKmode type, since any implicit inner load should
8884 already have been turned into an explicit one by the
8885 gimplification process. */
8886 *expr_p = NULL;
8889 /* If we are gimplifying at the statement level, we're done. Tack
8890 everything together and return. */
8891 if (fallback == fb_none || is_statement)
8893 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8894 it out for GC to reclaim it. */
8895 *expr_p = NULL_TREE;
8897 if (!gimple_seq_empty_p (internal_pre)
8898 || !gimple_seq_empty_p (internal_post))
8900 gimplify_seq_add_seq (&internal_pre, internal_post);
8901 gimplify_seq_add_seq (pre_p, internal_pre);
8904 /* The result of gimplifying *EXPR_P is going to be the last few
8905 statements in *PRE_P and *POST_P. Add location information
8906 to all the statements that were added by the gimplification
8907 helpers. */
8908 if (!gimple_seq_empty_p (*pre_p))
8909 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8911 if (!gimple_seq_empty_p (*post_p))
8912 annotate_all_with_location_after (*post_p, post_last_gsi,
8913 input_location);
8915 goto out;
8918 #ifdef ENABLE_GIMPLE_CHECKING
8919 if (*expr_p)
8921 enum tree_code code = TREE_CODE (*expr_p);
8922 /* These expressions should already be in gimple IR form. */
8923 gcc_assert (code != MODIFY_EXPR
8924 && code != ASM_EXPR
8925 && code != BIND_EXPR
8926 && code != CATCH_EXPR
8927 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8928 && code != EH_FILTER_EXPR
8929 && code != GOTO_EXPR
8930 && code != LABEL_EXPR
8931 && code != LOOP_EXPR
8932 && code != SWITCH_EXPR
8933 && code != TRY_FINALLY_EXPR
8934 && code != OACC_PARALLEL
8935 && code != OACC_KERNELS
8936 && code != OACC_DATA
8937 && code != OACC_HOST_DATA
8938 && code != OACC_DECLARE
8939 && code != OACC_UPDATE
8940 && code != OACC_ENTER_DATA
8941 && code != OACC_EXIT_DATA
8942 && code != OACC_CACHE
8943 && code != OMP_CRITICAL
8944 && code != OMP_FOR
8945 && code != OACC_LOOP
8946 && code != OMP_MASTER
8947 && code != OMP_TASKGROUP
8948 && code != OMP_ORDERED
8949 && code != OMP_PARALLEL
8950 && code != OMP_SECTIONS
8951 && code != OMP_SECTION
8952 && code != OMP_SINGLE);
8954 #endif
8956 /* Otherwise we're gimplifying a subexpression, so the resulting
8957 value is interesting. If it's a valid operand that matches
8958 GIMPLE_TEST_F, we're done. Unless we are handling some
8959 post-effects internally; if that's the case, we need to copy into
8960 a temporary before adding the post-effects to POST_P. */
8961 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8962 goto out;
8964 /* Otherwise, we need to create a new temporary for the gimplified
8965 expression. */
8967 /* We can't return an lvalue if we have an internal postqueue. The
8968 object the lvalue refers to would (probably) be modified by the
8969 postqueue; we need to copy the value out first, which means an
8970 rvalue. */
8971 if ((fallback & fb_lvalue)
8972 && gimple_seq_empty_p (internal_post)
8973 && is_gimple_addressable (*expr_p))
8975 /* An lvalue will do. Take the address of the expression, store it
8976 in a temporary, and replace the expression with an INDIRECT_REF of
8977 that temporary. */
8978 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8979 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8980 *expr_p = build_simple_mem_ref (tmp);
8982 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8984 /* An rvalue will do. Assign the gimplified expression into a
8985 new temporary TMP and replace the original expression with
8986 TMP. First, make sure that the expression has a type so that
8987 it can be assigned into a temporary. */
8988 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8989 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8991 else
8993 #ifdef ENABLE_GIMPLE_CHECKING
8994 if (!(fallback & fb_mayfail))
8996 fprintf (stderr, "gimplification failed:\n");
8997 print_generic_expr (stderr, *expr_p, 0);
8998 debug_tree (*expr_p);
8999 internal_error ("gimplification failed");
9001 #endif
9002 gcc_assert (fallback & fb_mayfail);
9004 /* If this is an asm statement, and the user asked for the
9005 impossible, don't die. Fail and let gimplify_asm_expr
9006 issue an error. */
9007 ret = GS_ERROR;
9008 goto out;
9011 /* Make sure the temporary matches our predicate. */
9012 gcc_assert ((*gimple_test_f) (*expr_p));
9014 if (!gimple_seq_empty_p (internal_post))
9016 annotate_all_with_location (internal_post, input_location);
9017 gimplify_seq_add_seq (pre_p, internal_post);
9020 out:
9021 input_location = saved_location;
9022 return ret;
9025 /* Look through TYPE for variable-sized objects and gimplify each such
9026 size that we find. Add to LIST_P any statements generated. */
9028 void
9029 gimplify_type_sizes (tree type, gimple_seq *list_p)
9031 tree field, t;
9033 if (type == NULL || type == error_mark_node)
9034 return;
9036 /* We first do the main variant, then copy into any other variants. */
9037 type = TYPE_MAIN_VARIANT (type);
9039 /* Avoid infinite recursion. */
9040 if (TYPE_SIZES_GIMPLIFIED (type))
9041 return;
9043 TYPE_SIZES_GIMPLIFIED (type) = 1;
9045 switch (TREE_CODE (type))
9047 case INTEGER_TYPE:
9048 case ENUMERAL_TYPE:
9049 case BOOLEAN_TYPE:
9050 case REAL_TYPE:
9051 case FIXED_POINT_TYPE:
9052 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
9053 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
9055 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9057 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
9058 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
9060 break;
9062 case ARRAY_TYPE:
9063 /* These types may not have declarations, so handle them here. */
9064 gimplify_type_sizes (TREE_TYPE (type), list_p);
9065 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
9066 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
9067 with assigned stack slots, for -O1+ -g they should be tracked
9068 by VTA. */
9069 if (!(TYPE_NAME (type)
9070 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9071 && DECL_IGNORED_P (TYPE_NAME (type)))
9072 && TYPE_DOMAIN (type)
9073 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
9075 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
9076 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
9077 DECL_IGNORED_P (t) = 0;
9078 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
9079 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
9080 DECL_IGNORED_P (t) = 0;
9082 break;
9084 case RECORD_TYPE:
9085 case UNION_TYPE:
9086 case QUAL_UNION_TYPE:
9087 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9088 if (TREE_CODE (field) == FIELD_DECL)
9090 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
9091 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
9092 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
9093 gimplify_type_sizes (TREE_TYPE (field), list_p);
9095 break;
9097 case POINTER_TYPE:
9098 case REFERENCE_TYPE:
9099 /* We used to recurse on the pointed-to type here, which turned out to
9100 be incorrect because its definition might refer to variables not
9101 yet initialized at this point if a forward declaration is involved.
9103 It was actually useful for anonymous pointed-to types to ensure
9104 that the sizes evaluation dominates every possible later use of the
9105 values. Restricting to such types here would be safe since there
9106 is no possible forward declaration around, but would introduce an
9107 undesirable middle-end semantic to anonymity. We then defer to
9108 front-ends the responsibility of ensuring that the sizes are
9109 evaluated both early and late enough, e.g. by attaching artificial
9110 type declarations to the tree. */
9111 break;
9113 default:
9114 break;
9117 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
9118 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
9120 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9122 TYPE_SIZE (t) = TYPE_SIZE (type);
9123 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
9124 TYPE_SIZES_GIMPLIFIED (t) = 1;
9128 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
9129 a size or position, has had all of its SAVE_EXPRs evaluated.
9130 We add any required statements to *STMT_P. */
9132 void
9133 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
9135 tree expr = *expr_p;
9137 /* We don't do anything if the value isn't there, is constant, or contains
9138 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
9139 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
9140 will want to replace it with a new variable, but that will cause problems
9141 if this type is from outside the function. It's OK to have that here. */
9142 if (is_gimple_sizepos (expr))
9143 return;
9145 *expr_p = unshare_expr (expr);
9147 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
9150 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
9151 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
9152 is true, also gimplify the parameters. */
9154 gbind *
9155 gimplify_body (tree fndecl, bool do_parms)
9157 location_t saved_location = input_location;
9158 gimple_seq parm_stmts, seq;
9159 gimple outer_stmt;
9160 gbind *outer_bind;
9161 struct cgraph_node *cgn;
9163 timevar_push (TV_TREE_GIMPLIFY);
9165 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
9166 gimplification. */
9167 default_rtl_profile ();
9169 gcc_assert (gimplify_ctxp == NULL);
9170 push_gimplify_context ();
9172 if (flag_openacc || flag_openmp)
9174 gcc_assert (gimplify_omp_ctxp == NULL);
9175 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
9176 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
9179 /* Unshare most shared trees in the body and in that of any nested functions.
9180 It would seem we don't have to do this for nested functions because
9181 they are supposed to be output and then the outer function gimplified
9182 first, but the g++ front end doesn't always do it that way. */
9183 unshare_body (fndecl);
9184 unvisit_body (fndecl);
9186 cgn = cgraph_node::get (fndecl);
9187 if (cgn && cgn->origin)
9188 nonlocal_vlas = new hash_set<tree>;
9190 /* Make sure input_location isn't set to something weird. */
9191 input_location = DECL_SOURCE_LOCATION (fndecl);
9193 /* Resolve callee-copies. This has to be done before processing
9194 the body so that DECL_VALUE_EXPR gets processed correctly. */
9195 parm_stmts = do_parms ? gimplify_parameters () : NULL;
9197 /* Gimplify the function's body. */
9198 seq = NULL;
9199 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
9200 outer_stmt = gimple_seq_first_stmt (seq);
9201 if (!outer_stmt)
9203 outer_stmt = gimple_build_nop ();
9204 gimplify_seq_add_stmt (&seq, outer_stmt);
9207 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
9208 not the case, wrap everything in a GIMPLE_BIND to make it so. */
9209 if (gimple_code (outer_stmt) == GIMPLE_BIND
9210 && gimple_seq_first (seq) == gimple_seq_last (seq))
9211 outer_bind = as_a <gbind *> (outer_stmt);
9212 else
9213 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
9215 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9217 /* If we had callee-copies statements, insert them at the beginning
9218 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
9219 if (!gimple_seq_empty_p (parm_stmts))
9221 tree parm;
9223 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
9224 gimple_bind_set_body (outer_bind, parm_stmts);
9226 for (parm = DECL_ARGUMENTS (current_function_decl);
9227 parm; parm = DECL_CHAIN (parm))
9228 if (DECL_HAS_VALUE_EXPR_P (parm))
9230 DECL_HAS_VALUE_EXPR_P (parm) = 0;
9231 DECL_IGNORED_P (parm) = 0;
9235 if (nonlocal_vlas)
9237 if (nonlocal_vla_vars)
9239 /* tree-nested.c may later on call declare_vars (..., true);
9240 which relies on BLOCK_VARS chain to be the tail of the
9241 gimple_bind_vars chain. Ensure we don't violate that
9242 assumption. */
9243 if (gimple_bind_block (outer_bind)
9244 == DECL_INITIAL (current_function_decl))
9245 declare_vars (nonlocal_vla_vars, outer_bind, true);
9246 else
9247 BLOCK_VARS (DECL_INITIAL (current_function_decl))
9248 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
9249 nonlocal_vla_vars);
9250 nonlocal_vla_vars = NULL_TREE;
9252 delete nonlocal_vlas;
9253 nonlocal_vlas = NULL;
9256 if ((flag_openacc || flag_openmp || flag_openmp_simd)
9257 && gimplify_omp_ctxp)
9259 delete_omp_context (gimplify_omp_ctxp);
9260 gimplify_omp_ctxp = NULL;
9263 pop_gimplify_context (outer_bind);
9264 gcc_assert (gimplify_ctxp == NULL);
9266 #ifdef ENABLE_CHECKING
9267 if (!seen_error ())
9268 verify_gimple_in_seq (gimple_bind_body (outer_bind));
9269 #endif
9271 timevar_pop (TV_TREE_GIMPLIFY);
9272 input_location = saved_location;
9274 return outer_bind;
9277 typedef char *char_p; /* For DEF_VEC_P. */
9279 /* Return whether we should exclude FNDECL from instrumentation. */
9281 static bool
9282 flag_instrument_functions_exclude_p (tree fndecl)
9284 vec<char_p> *v;
9286 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
9287 if (v && v->length () > 0)
9289 const char *name;
9290 int i;
9291 char *s;
9293 name = lang_hooks.decl_printable_name (fndecl, 0);
9294 FOR_EACH_VEC_ELT (*v, i, s)
9295 if (strstr (name, s) != NULL)
9296 return true;
9299 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
9300 if (v && v->length () > 0)
9302 const char *name;
9303 int i;
9304 char *s;
9306 name = DECL_SOURCE_FILE (fndecl);
9307 FOR_EACH_VEC_ELT (*v, i, s)
9308 if (strstr (name, s) != NULL)
9309 return true;
9312 return false;
9315 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
9316 node for the function we want to gimplify.
9318 Return the sequence of GIMPLE statements corresponding to the body
9319 of FNDECL. */
9321 void
9322 gimplify_function_tree (tree fndecl)
9324 tree parm, ret;
9325 gimple_seq seq;
9326 gbind *bind;
9328 gcc_assert (!gimple_body (fndecl));
9330 if (DECL_STRUCT_FUNCTION (fndecl))
9331 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
9332 else
9333 push_struct_function (fndecl);
9335 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
9336 if necessary. */
9337 cfun->curr_properties |= PROP_gimple_lva;
9339 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
9341 /* Preliminarily mark non-addressed complex variables as eligible
9342 for promotion to gimple registers. We'll transform their uses
9343 as we find them. */
9344 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
9345 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
9346 && !TREE_THIS_VOLATILE (parm)
9347 && !needs_to_live_in_memory (parm))
9348 DECL_GIMPLE_REG_P (parm) = 1;
9351 ret = DECL_RESULT (fndecl);
9352 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
9353 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
9354 && !needs_to_live_in_memory (ret))
9355 DECL_GIMPLE_REG_P (ret) = 1;
9357 bind = gimplify_body (fndecl, true);
9359 /* The tree body of the function is no longer needed, replace it
9360 with the new GIMPLE body. */
9361 seq = NULL;
9362 gimple_seq_add_stmt (&seq, bind);
9363 gimple_set_body (fndecl, seq);
9365 /* If we're instrumenting function entry/exit, then prepend the call to
9366 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
9367 catch the exit hook. */
9368 /* ??? Add some way to ignore exceptions for this TFE. */
9369 if (flag_instrument_function_entry_exit
9370 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
9371 && !flag_instrument_functions_exclude_p (fndecl))
9373 tree x;
9374 gbind *new_bind;
9375 gimple tf;
9376 gimple_seq cleanup = NULL, body = NULL;
9377 tree tmp_var;
9378 gcall *call;
9380 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9381 call = gimple_build_call (x, 1, integer_zero_node);
9382 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9383 gimple_call_set_lhs (call, tmp_var);
9384 gimplify_seq_add_stmt (&cleanup, call);
9385 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9386 call = gimple_build_call (x, 2,
9387 build_fold_addr_expr (current_function_decl),
9388 tmp_var);
9389 gimplify_seq_add_stmt (&cleanup, call);
9390 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9392 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9393 call = gimple_build_call (x, 1, integer_zero_node);
9394 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9395 gimple_call_set_lhs (call, tmp_var);
9396 gimplify_seq_add_stmt (&body, call);
9397 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9398 call = gimple_build_call (x, 2,
9399 build_fold_addr_expr (current_function_decl),
9400 tmp_var);
9401 gimplify_seq_add_stmt (&body, call);
9402 gimplify_seq_add_stmt (&body, tf);
9403 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9404 /* Clear the block for BIND, since it is no longer directly inside
9405 the function, but within a try block. */
9406 gimple_bind_set_block (bind, NULL);
9408 /* Replace the current function body with the body
9409 wrapped in the try/finally TF. */
9410 seq = NULL;
9411 gimple_seq_add_stmt (&seq, new_bind);
9412 gimple_set_body (fndecl, seq);
9413 bind = new_bind;
9416 if ((flag_sanitize & SANITIZE_THREAD) != 0
9417 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
9419 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
9420 gimple tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
9421 gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
9422 /* Clear the block for BIND, since it is no longer directly inside
9423 the function, but within a try block. */
9424 gimple_bind_set_block (bind, NULL);
9425 /* Replace the current function body with the body
9426 wrapped in the try/finally TF. */
9427 seq = NULL;
9428 gimple_seq_add_stmt (&seq, new_bind);
9429 gimple_set_body (fndecl, seq);
9432 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9433 cfun->curr_properties |= PROP_gimple_any;
9435 pop_cfun ();
9437 dump_function (TDI_generic, fndecl);
9440 /* Return a dummy expression of type TYPE in order to keep going after an
9441 error. */
9443 static tree
9444 dummy_object (tree type)
9446 tree t = build_int_cst (build_pointer_type (type), 0);
9447 return build2 (MEM_REF, type, t, t);
9450 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9451 builtin function, but a very special sort of operator. */
9453 enum gimplify_status
9454 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
9455 gimple_seq *post_p ATTRIBUTE_UNUSED)
9457 tree promoted_type, have_va_type;
9458 tree valist = TREE_OPERAND (*expr_p, 0);
9459 tree type = TREE_TYPE (*expr_p);
9460 tree t, tag;
9461 location_t loc = EXPR_LOCATION (*expr_p);
9463 /* Verify that valist is of the proper type. */
9464 have_va_type = TREE_TYPE (valist);
9465 if (have_va_type == error_mark_node)
9466 return GS_ERROR;
9467 have_va_type = targetm.canonical_va_list_type (have_va_type);
9469 if (have_va_type == NULL_TREE)
9471 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9472 return GS_ERROR;
9475 /* Generate a diagnostic for requesting data of a type that cannot
9476 be passed through `...' due to type promotion at the call site. */
9477 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9478 != type)
9480 static bool gave_help;
9481 bool warned;
9483 /* Unfortunately, this is merely undefined, rather than a constraint
9484 violation, so we cannot make this an error. If this call is never
9485 executed, the program is still strictly conforming. */
9486 warned = warning_at (loc, 0,
9487 "%qT is promoted to %qT when passed through %<...%>",
9488 type, promoted_type);
9489 if (!gave_help && warned)
9491 gave_help = true;
9492 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9493 promoted_type, type);
9496 /* We can, however, treat "undefined" any way we please.
9497 Call abort to encourage the user to fix the program. */
9498 if (warned)
9499 inform (loc, "if this code is reached, the program will abort");
9500 /* Before the abort, allow the evaluation of the va_list
9501 expression to exit or longjmp. */
9502 gimplify_and_add (valist, pre_p);
9503 t = build_call_expr_loc (loc,
9504 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9505 gimplify_and_add (t, pre_p);
9507 /* This is dead code, but go ahead and finish so that the
9508 mode of the result comes out right. */
9509 *expr_p = dummy_object (type);
9510 return GS_ALL_DONE;
9513 tag = build_int_cst (build_pointer_type (type), 0);
9514 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 2, valist, tag);
9516 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
9517 needs to be expanded. */
9518 cfun->curr_properties &= ~PROP_gimple_lva;
9520 return GS_OK;
9523 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
9525 DST/SRC are the destination and source respectively. You can pass
9526 ungimplified trees in DST or SRC, in which case they will be
9527 converted to a gimple operand if necessary.
9529 This function returns the newly created GIMPLE_ASSIGN tuple. */
9531 gimple
9532 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
9534 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
9535 gimplify_and_add (t, seq_p);
9536 ggc_free (t);
9537 return gimple_seq_last_stmt (*seq_p);
9540 inline hashval_t
9541 gimplify_hasher::hash (const elt_t *p)
9543 tree t = p->val;
9544 return iterative_hash_expr (t, 0);
9547 inline bool
9548 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
9550 tree t1 = p1->val;
9551 tree t2 = p2->val;
9552 enum tree_code code = TREE_CODE (t1);
9554 if (TREE_CODE (t2) != code
9555 || TREE_TYPE (t1) != TREE_TYPE (t2))
9556 return false;
9558 if (!operand_equal_p (t1, t2, 0))
9559 return false;
9561 #ifdef ENABLE_CHECKING
9562 /* Only allow them to compare equal if they also hash equal; otherwise
9563 results are nondeterminate, and we fail bootstrap comparison. */
9564 gcc_assert (hash (p1) == hash (p2));
9565 #endif
9567 return true;