2015-11-09 Steve Ellcey <sellcey@imgtec.com>
[official-gcc.git] / gcc / gimplify.c
blob287e51e201657e4b5d0b806388d8638ec6da5569
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 "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "gimple.h"
31 #include "gimple-predict.h"
32 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
33 #include "ssa.h"
34 #include "cgraph.h"
35 #include "tree-pretty-print.h"
36 #include "diagnostic-core.h"
37 #include "alias.h"
38 #include "fold-const.h"
39 #include "calls.h"
40 #include "varasm.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimplify.h"
46 #include "gimple-iterator.h"
47 #include "stor-layout.h"
48 #include "print-tree.h"
49 #include "tree-iterator.h"
50 #include "tree-inline.h"
51 #include "langhooks.h"
52 #include "tree-cfg.h"
53 #include "tree-ssa.h"
54 #include "omp-low.h"
55 #include "gimple-low.h"
56 #include "cilk.h"
57 #include "gomp-constants.h"
58 #include "tree-dump.h"
60 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
61 #include "builtins.h"
63 enum gimplify_omp_var_data
65 GOVD_SEEN = 1,
66 GOVD_EXPLICIT = 2,
67 GOVD_SHARED = 4,
68 GOVD_PRIVATE = 8,
69 GOVD_FIRSTPRIVATE = 16,
70 GOVD_LASTPRIVATE = 32,
71 GOVD_REDUCTION = 64,
72 GOVD_LOCAL = 128,
73 GOVD_MAP = 256,
74 GOVD_DEBUG_PRIVATE = 512,
75 GOVD_PRIVATE_OUTER_REF = 1024,
76 GOVD_LINEAR = 2048,
77 GOVD_ALIGNED = 4096,
79 /* Flag for GOVD_MAP: don't copy back. */
80 GOVD_MAP_TO_ONLY = 8192,
82 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
83 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
85 GOVD_MAP_0LEN_ARRAY = 32768,
87 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
88 GOVD_MAP_ALWAYS_TO = 65536,
90 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
91 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
92 | GOVD_LOCAL)
96 enum omp_region_type
98 ORT_WORKSHARE = 0,
99 ORT_SIMD = 1,
100 ORT_PARALLEL = 2,
101 ORT_COMBINED_PARALLEL = 3,
102 ORT_TASK = 4,
103 ORT_UNTIED_TASK = 5,
104 ORT_TEAMS = 8,
105 ORT_COMBINED_TEAMS = 9,
106 /* Data region. */
107 ORT_TARGET_DATA = 16,
108 /* Data region with offloading. */
109 ORT_TARGET = 32,
110 ORT_COMBINED_TARGET = 33,
111 /* Dummy OpenMP region, used to disable expansion of
112 DECL_VALUE_EXPRs in taskloop pre body. */
113 ORT_NONE = 64
116 /* Gimplify hashtable helper. */
118 struct gimplify_hasher : free_ptr_hash <elt_t>
120 static inline hashval_t hash (const elt_t *);
121 static inline bool equal (const elt_t *, const elt_t *);
124 struct gimplify_ctx
126 struct gimplify_ctx *prev_context;
128 vec<gbind *> bind_expr_stack;
129 tree temps;
130 gimple_seq conditional_cleanups;
131 tree exit_label;
132 tree return_temp;
134 vec<tree> case_labels;
135 /* The formal temporary table. Should this be persistent? */
136 hash_table<gimplify_hasher> *temp_htab;
138 int conditions;
139 bool save_stack;
140 bool into_ssa;
141 bool allow_rhs_cond_expr;
142 bool in_cleanup_point_expr;
145 struct gimplify_omp_ctx
147 struct gimplify_omp_ctx *outer_context;
148 splay_tree variables;
149 hash_set<tree> *privatized_types;
150 /* Iteration variables in an OMP_FOR. */
151 vec<tree> loop_iter_var;
152 location_t location;
153 enum omp_clause_default_kind default_kind;
154 enum omp_region_type region_type;
155 bool combined_loop;
156 bool distribute;
157 bool target_map_scalars_firstprivate;
158 bool target_map_pointers_as_0len_arrays;
159 bool target_firstprivatize_array_bases;
162 static struct gimplify_ctx *gimplify_ctxp;
163 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
165 /* Forward declaration. */
166 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
168 /* Shorter alias name for the above function for use in gimplify.c
169 only. */
171 static inline void
172 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
174 gimple_seq_add_stmt_without_update (seq_p, gs);
177 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
178 NULL, a new sequence is allocated. This function is
179 similar to gimple_seq_add_seq, but does not scan the operands.
180 During gimplification, we need to manipulate statement sequences
181 before the def/use vectors have been constructed. */
183 static void
184 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
186 gimple_stmt_iterator si;
188 if (src == NULL)
189 return;
191 si = gsi_last (*dst_p);
192 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
196 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
197 and popping gimplify contexts. */
199 static struct gimplify_ctx *ctx_pool = NULL;
201 /* Return a gimplify context struct from the pool. */
203 static inline struct gimplify_ctx *
204 ctx_alloc (void)
206 struct gimplify_ctx * c = ctx_pool;
208 if (c)
209 ctx_pool = c->prev_context;
210 else
211 c = XNEW (struct gimplify_ctx);
213 memset (c, '\0', sizeof (*c));
214 return c;
217 /* Put gimplify context C back into the pool. */
219 static inline void
220 ctx_free (struct gimplify_ctx *c)
222 c->prev_context = ctx_pool;
223 ctx_pool = c;
226 /* Free allocated ctx stack memory. */
228 void
229 free_gimplify_stack (void)
231 struct gimplify_ctx *c;
233 while ((c = ctx_pool))
235 ctx_pool = c->prev_context;
236 free (c);
241 /* Set up a context for the gimplifier. */
243 void
244 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
246 struct gimplify_ctx *c = ctx_alloc ();
248 c->prev_context = gimplify_ctxp;
249 gimplify_ctxp = c;
250 gimplify_ctxp->into_ssa = in_ssa;
251 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
254 /* Tear down a context for the gimplifier. If BODY is non-null, then
255 put the temporaries into the outer BIND_EXPR. Otherwise, put them
256 in the local_decls.
258 BODY is not a sequence, but the first tuple in a sequence. */
260 void
261 pop_gimplify_context (gimple *body)
263 struct gimplify_ctx *c = gimplify_ctxp;
265 gcc_assert (c
266 && (!c->bind_expr_stack.exists ()
267 || c->bind_expr_stack.is_empty ()));
268 c->bind_expr_stack.release ();
269 gimplify_ctxp = c->prev_context;
271 if (body)
272 declare_vars (c->temps, body, false);
273 else
274 record_vars (c->temps);
276 delete c->temp_htab;
277 c->temp_htab = NULL;
278 ctx_free (c);
281 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
283 static void
284 gimple_push_bind_expr (gbind *bind_stmt)
286 gimplify_ctxp->bind_expr_stack.reserve (8);
287 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
290 /* Pop the first element off the stack of bindings. */
292 static void
293 gimple_pop_bind_expr (void)
295 gimplify_ctxp->bind_expr_stack.pop ();
298 /* Return the first element of the stack of bindings. */
300 gbind *
301 gimple_current_bind_expr (void)
303 return gimplify_ctxp->bind_expr_stack.last ();
306 /* Return the stack of bindings created during gimplification. */
308 vec<gbind *>
309 gimple_bind_expr_stack (void)
311 return gimplify_ctxp->bind_expr_stack;
314 /* Return true iff there is a COND_EXPR between us and the innermost
315 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
317 static bool
318 gimple_conditional_context (void)
320 return gimplify_ctxp->conditions > 0;
323 /* Note that we've entered a COND_EXPR. */
325 static void
326 gimple_push_condition (void)
328 #ifdef ENABLE_GIMPLE_CHECKING
329 if (gimplify_ctxp->conditions == 0)
330 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
331 #endif
332 ++(gimplify_ctxp->conditions);
335 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
336 now, add any conditional cleanups we've seen to the prequeue. */
338 static void
339 gimple_pop_condition (gimple_seq *pre_p)
341 int conds = --(gimplify_ctxp->conditions);
343 gcc_assert (conds >= 0);
344 if (conds == 0)
346 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
347 gimplify_ctxp->conditional_cleanups = NULL;
351 /* A stable comparison routine for use with splay trees and DECLs. */
353 static int
354 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
356 tree a = (tree) xa;
357 tree b = (tree) xb;
359 return DECL_UID (a) - DECL_UID (b);
362 /* Create a new omp construct that deals with variable remapping. */
364 static struct gimplify_omp_ctx *
365 new_omp_context (enum omp_region_type region_type)
367 struct gimplify_omp_ctx *c;
369 c = XCNEW (struct gimplify_omp_ctx);
370 c->outer_context = gimplify_omp_ctxp;
371 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
372 c->privatized_types = new hash_set<tree>;
373 c->location = input_location;
374 c->region_type = region_type;
375 if ((region_type & ORT_TASK) == 0)
376 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
377 else
378 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
380 return c;
383 /* Destroy an omp construct that deals with variable remapping. */
385 static void
386 delete_omp_context (struct gimplify_omp_ctx *c)
388 splay_tree_delete (c->variables);
389 delete c->privatized_types;
390 c->loop_iter_var.release ();
391 XDELETE (c);
394 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
395 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
397 /* Both gimplify the statement T and append it to *SEQ_P. This function
398 behaves exactly as gimplify_stmt, but you don't have to pass T as a
399 reference. */
401 void
402 gimplify_and_add (tree t, gimple_seq *seq_p)
404 gimplify_stmt (&t, seq_p);
407 /* Gimplify statement T into sequence *SEQ_P, and return the first
408 tuple in the sequence of generated tuples for this statement.
409 Return NULL if gimplifying T produced no tuples. */
411 static gimple *
412 gimplify_and_return_first (tree t, gimple_seq *seq_p)
414 gimple_stmt_iterator last = gsi_last (*seq_p);
416 gimplify_and_add (t, seq_p);
418 if (!gsi_end_p (last))
420 gsi_next (&last);
421 return gsi_stmt (last);
423 else
424 return gimple_seq_first_stmt (*seq_p);
427 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
428 LHS, or for a call argument. */
430 static bool
431 is_gimple_mem_rhs (tree t)
433 /* If we're dealing with a renamable type, either source or dest must be
434 a renamed variable. */
435 if (is_gimple_reg_type (TREE_TYPE (t)))
436 return is_gimple_val (t);
437 else
438 return is_gimple_val (t) || is_gimple_lvalue (t);
441 /* Return true if T is a CALL_EXPR or an expression that can be
442 assigned to a temporary. Note that this predicate should only be
443 used during gimplification. See the rationale for this in
444 gimplify_modify_expr. */
446 static bool
447 is_gimple_reg_rhs_or_call (tree t)
449 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
450 || TREE_CODE (t) == CALL_EXPR);
453 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
454 this predicate should only be used during gimplification. See the
455 rationale for this in gimplify_modify_expr. */
457 static bool
458 is_gimple_mem_rhs_or_call (tree t)
460 /* If we're dealing with a renamable type, either source or dest must be
461 a renamed variable. */
462 if (is_gimple_reg_type (TREE_TYPE (t)))
463 return is_gimple_val (t);
464 else
465 return (is_gimple_val (t) || is_gimple_lvalue (t)
466 || TREE_CODE (t) == CALL_EXPR);
469 /* Create a temporary with a name derived from VAL. Subroutine of
470 lookup_tmp_var; nobody else should call this function. */
472 static inline tree
473 create_tmp_from_val (tree val)
475 /* Drop all qualifiers and address-space information from the value type. */
476 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
477 tree var = create_tmp_var (type, get_name (val));
478 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
479 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
480 DECL_GIMPLE_REG_P (var) = 1;
481 return var;
484 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
485 an existing expression temporary. */
487 static tree
488 lookup_tmp_var (tree val, bool is_formal)
490 tree ret;
492 /* If not optimizing, never really reuse a temporary. local-alloc
493 won't allocate any variable that is used in more than one basic
494 block, which means it will go into memory, causing much extra
495 work in reload and final and poorer code generation, outweighing
496 the extra memory allocation here. */
497 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
498 ret = create_tmp_from_val (val);
499 else
501 elt_t elt, *elt_p;
502 elt_t **slot;
504 elt.val = val;
505 if (!gimplify_ctxp->temp_htab)
506 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
507 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
508 if (*slot == NULL)
510 elt_p = XNEW (elt_t);
511 elt_p->val = val;
512 elt_p->temp = ret = create_tmp_from_val (val);
513 *slot = elt_p;
515 else
517 elt_p = *slot;
518 ret = elt_p->temp;
522 return ret;
525 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
527 static tree
528 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
529 bool is_formal)
531 tree t, mod;
533 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
534 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
535 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
536 fb_rvalue);
538 if (gimplify_ctxp->into_ssa
539 && is_gimple_reg_type (TREE_TYPE (val)))
540 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
541 else
542 t = lookup_tmp_var (val, is_formal);
544 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
546 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
548 /* gimplify_modify_expr might want to reduce this further. */
549 gimplify_and_add (mod, pre_p);
550 ggc_free (mod);
552 return t;
555 /* Return a formal temporary variable initialized with VAL. PRE_P is as
556 in gimplify_expr. Only use this function if:
558 1) The value of the unfactored expression represented by VAL will not
559 change between the initialization and use of the temporary, and
560 2) The temporary will not be otherwise modified.
562 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
563 and #2 means it is inappropriate for && temps.
565 For other cases, use get_initialized_tmp_var instead. */
567 tree
568 get_formal_tmp_var (tree val, gimple_seq *pre_p)
570 return internal_get_tmp_var (val, pre_p, NULL, true);
573 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
574 are as in gimplify_expr. */
576 tree
577 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
579 return internal_get_tmp_var (val, pre_p, post_p, false);
582 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
583 generate debug info for them; otherwise don't. */
585 void
586 declare_vars (tree vars, gimple *gs, bool debug_info)
588 tree last = vars;
589 if (last)
591 tree temps, block;
593 gbind *scope = as_a <gbind *> (gs);
595 temps = nreverse (last);
597 block = gimple_bind_block (scope);
598 gcc_assert (!block || TREE_CODE (block) == BLOCK);
599 if (!block || !debug_info)
601 DECL_CHAIN (last) = gimple_bind_vars (scope);
602 gimple_bind_set_vars (scope, temps);
604 else
606 /* We need to attach the nodes both to the BIND_EXPR and to its
607 associated BLOCK for debugging purposes. The key point here
608 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
609 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
610 if (BLOCK_VARS (block))
611 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
612 else
614 gimple_bind_set_vars (scope,
615 chainon (gimple_bind_vars (scope), temps));
616 BLOCK_VARS (block) = temps;
622 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
623 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
624 no such upper bound can be obtained. */
626 static void
627 force_constant_size (tree var)
629 /* The only attempt we make is by querying the maximum size of objects
630 of the variable's type. */
632 HOST_WIDE_INT max_size;
634 gcc_assert (TREE_CODE (var) == VAR_DECL);
636 max_size = max_int_size_in_bytes (TREE_TYPE (var));
638 gcc_assert (max_size >= 0);
640 DECL_SIZE_UNIT (var)
641 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
642 DECL_SIZE (var)
643 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
646 /* Push the temporary variable TMP into the current binding. */
648 void
649 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
651 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
653 /* Later processing assumes that the object size is constant, which might
654 not be true at this point. Force the use of a constant upper bound in
655 this case. */
656 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
657 force_constant_size (tmp);
659 DECL_CONTEXT (tmp) = fn->decl;
660 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
662 record_vars_into (tmp, fn->decl);
665 /* Push the temporary variable TMP into the current binding. */
667 void
668 gimple_add_tmp_var (tree tmp)
670 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
672 /* Later processing assumes that the object size is constant, which might
673 not be true at this point. Force the use of a constant upper bound in
674 this case. */
675 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
676 force_constant_size (tmp);
678 DECL_CONTEXT (tmp) = current_function_decl;
679 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
681 if (gimplify_ctxp)
683 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
684 gimplify_ctxp->temps = tmp;
686 /* Mark temporaries local within the nearest enclosing parallel. */
687 if (gimplify_omp_ctxp)
689 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
690 while (ctx
691 && (ctx->region_type == ORT_WORKSHARE
692 || ctx->region_type == ORT_SIMD))
693 ctx = ctx->outer_context;
694 if (ctx)
695 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
698 else if (cfun)
699 record_vars (tmp);
700 else
702 gimple_seq body_seq;
704 /* This case is for nested functions. We need to expose the locals
705 they create. */
706 body_seq = gimple_body (current_function_decl);
707 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
713 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
714 nodes that are referenced more than once in GENERIC functions. This is
715 necessary because gimplification (translation into GIMPLE) is performed
716 by modifying tree nodes in-place, so gimplication of a shared node in a
717 first context could generate an invalid GIMPLE form in a second context.
719 This is achieved with a simple mark/copy/unmark algorithm that walks the
720 GENERIC representation top-down, marks nodes with TREE_VISITED the first
721 time it encounters them, duplicates them if they already have TREE_VISITED
722 set, and finally removes the TREE_VISITED marks it has set.
724 The algorithm works only at the function level, i.e. it generates a GENERIC
725 representation of a function with no nodes shared within the function when
726 passed a GENERIC function (except for nodes that are allowed to be shared).
728 At the global level, it is also necessary to unshare tree nodes that are
729 referenced in more than one function, for the same aforementioned reason.
730 This requires some cooperation from the front-end. There are 2 strategies:
732 1. Manual unsharing. The front-end needs to call unshare_expr on every
733 expression that might end up being shared across functions.
735 2. Deep unsharing. This is an extension of regular unsharing. Instead
736 of calling unshare_expr on expressions that might be shared across
737 functions, the front-end pre-marks them with TREE_VISITED. This will
738 ensure that they are unshared on the first reference within functions
739 when the regular unsharing algorithm runs. The counterpart is that
740 this algorithm must look deeper than for manual unsharing, which is
741 specified by LANG_HOOKS_DEEP_UNSHARING.
743 If there are only few specific cases of node sharing across functions, it is
744 probably easier for a front-end to unshare the expressions manually. On the
745 contrary, if the expressions generated at the global level are as widespread
746 as expressions generated within functions, deep unsharing is very likely the
747 way to go. */
749 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
750 These nodes model computations that must be done once. If we were to
751 unshare something like SAVE_EXPR(i++), the gimplification process would
752 create wrong code. However, if DATA is non-null, it must hold a pointer
753 set that is used to unshare the subtrees of these nodes. */
755 static tree
756 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
758 tree t = *tp;
759 enum tree_code code = TREE_CODE (t);
761 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
762 copy their subtrees if we can make sure to do it only once. */
763 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
765 if (data && !((hash_set<tree> *)data)->add (t))
767 else
768 *walk_subtrees = 0;
771 /* Stop at types, decls, constants like copy_tree_r. */
772 else if (TREE_CODE_CLASS (code) == tcc_type
773 || TREE_CODE_CLASS (code) == tcc_declaration
774 || TREE_CODE_CLASS (code) == tcc_constant
775 /* We can't do anything sensible with a BLOCK used as an
776 expression, but we also can't just die when we see it
777 because of non-expression uses. So we avert our eyes
778 and cross our fingers. Silly Java. */
779 || code == BLOCK)
780 *walk_subtrees = 0;
782 /* Cope with the statement expression extension. */
783 else if (code == STATEMENT_LIST)
786 /* Leave the bulk of the work to copy_tree_r itself. */
787 else
788 copy_tree_r (tp, walk_subtrees, NULL);
790 return NULL_TREE;
793 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
794 If *TP has been visited already, then *TP is deeply copied by calling
795 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
797 static tree
798 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
800 tree t = *tp;
801 enum tree_code code = TREE_CODE (t);
803 /* Skip types, decls, and constants. But we do want to look at their
804 types and the bounds of types. Mark them as visited so we properly
805 unmark their subtrees on the unmark pass. If we've already seen them,
806 don't look down further. */
807 if (TREE_CODE_CLASS (code) == tcc_type
808 || TREE_CODE_CLASS (code) == tcc_declaration
809 || TREE_CODE_CLASS (code) == tcc_constant)
811 if (TREE_VISITED (t))
812 *walk_subtrees = 0;
813 else
814 TREE_VISITED (t) = 1;
817 /* If this node has been visited already, unshare it and don't look
818 any deeper. */
819 else if (TREE_VISITED (t))
821 walk_tree (tp, mostly_copy_tree_r, data, NULL);
822 *walk_subtrees = 0;
825 /* Otherwise, mark the node as visited and keep looking. */
826 else
827 TREE_VISITED (t) = 1;
829 return NULL_TREE;
832 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
833 copy_if_shared_r callback unmodified. */
835 static inline void
836 copy_if_shared (tree *tp, void *data)
838 walk_tree (tp, copy_if_shared_r, data, NULL);
841 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
842 any nested functions. */
844 static void
845 unshare_body (tree fndecl)
847 struct cgraph_node *cgn = cgraph_node::get (fndecl);
848 /* If the language requires deep unsharing, we need a pointer set to make
849 sure we don't repeatedly unshare subtrees of unshareable nodes. */
850 hash_set<tree> *visited
851 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
853 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
854 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
855 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
857 delete visited;
859 if (cgn)
860 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
861 unshare_body (cgn->decl);
864 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
865 Subtrees are walked until the first unvisited node is encountered. */
867 static tree
868 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
870 tree t = *tp;
872 /* If this node has been visited, unmark it and keep looking. */
873 if (TREE_VISITED (t))
874 TREE_VISITED (t) = 0;
876 /* Otherwise, don't look any deeper. */
877 else
878 *walk_subtrees = 0;
880 return NULL_TREE;
883 /* Unmark the visited trees rooted at *TP. */
885 static inline void
886 unmark_visited (tree *tp)
888 walk_tree (tp, unmark_visited_r, NULL, NULL);
891 /* Likewise, but mark all trees as not visited. */
893 static void
894 unvisit_body (tree fndecl)
896 struct cgraph_node *cgn = cgraph_node::get (fndecl);
898 unmark_visited (&DECL_SAVED_TREE (fndecl));
899 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
900 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
902 if (cgn)
903 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
904 unvisit_body (cgn->decl);
907 /* Unconditionally make an unshared copy of EXPR. This is used when using
908 stored expressions which span multiple functions, such as BINFO_VTABLE,
909 as the normal unsharing process can't tell that they're shared. */
911 tree
912 unshare_expr (tree expr)
914 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
915 return expr;
918 /* Worker for unshare_expr_without_location. */
920 static tree
921 prune_expr_location (tree *tp, int *walk_subtrees, void *)
923 if (EXPR_P (*tp))
924 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
925 else
926 *walk_subtrees = 0;
927 return NULL_TREE;
930 /* Similar to unshare_expr but also prune all expression locations
931 from EXPR. */
933 tree
934 unshare_expr_without_location (tree expr)
936 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
937 if (EXPR_P (expr))
938 walk_tree (&expr, prune_expr_location, NULL, NULL);
939 return expr;
942 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
943 contain statements and have a value. Assign its value to a temporary
944 and give it void_type_node. Return the temporary, or NULL_TREE if
945 WRAPPER was already void. */
947 tree
948 voidify_wrapper_expr (tree wrapper, tree temp)
950 tree type = TREE_TYPE (wrapper);
951 if (type && !VOID_TYPE_P (type))
953 tree *p;
955 /* Set p to point to the body of the wrapper. Loop until we find
956 something that isn't a wrapper. */
957 for (p = &wrapper; p && *p; )
959 switch (TREE_CODE (*p))
961 case BIND_EXPR:
962 TREE_SIDE_EFFECTS (*p) = 1;
963 TREE_TYPE (*p) = void_type_node;
964 /* For a BIND_EXPR, the body is operand 1. */
965 p = &BIND_EXPR_BODY (*p);
966 break;
968 case CLEANUP_POINT_EXPR:
969 case TRY_FINALLY_EXPR:
970 case TRY_CATCH_EXPR:
971 TREE_SIDE_EFFECTS (*p) = 1;
972 TREE_TYPE (*p) = void_type_node;
973 p = &TREE_OPERAND (*p, 0);
974 break;
976 case STATEMENT_LIST:
978 tree_stmt_iterator i = tsi_last (*p);
979 TREE_SIDE_EFFECTS (*p) = 1;
980 TREE_TYPE (*p) = void_type_node;
981 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
983 break;
985 case COMPOUND_EXPR:
986 /* Advance to the last statement. Set all container types to
987 void. */
988 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
990 TREE_SIDE_EFFECTS (*p) = 1;
991 TREE_TYPE (*p) = void_type_node;
993 break;
995 case TRANSACTION_EXPR:
996 TREE_SIDE_EFFECTS (*p) = 1;
997 TREE_TYPE (*p) = void_type_node;
998 p = &TRANSACTION_EXPR_BODY (*p);
999 break;
1001 default:
1002 /* Assume that any tree upon which voidify_wrapper_expr is
1003 directly called is a wrapper, and that its body is op0. */
1004 if (p == &wrapper)
1006 TREE_SIDE_EFFECTS (*p) = 1;
1007 TREE_TYPE (*p) = void_type_node;
1008 p = &TREE_OPERAND (*p, 0);
1009 break;
1011 goto out;
1015 out:
1016 if (p == NULL || IS_EMPTY_STMT (*p))
1017 temp = NULL_TREE;
1018 else if (temp)
1020 /* The wrapper is on the RHS of an assignment that we're pushing
1021 down. */
1022 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1023 || TREE_CODE (temp) == MODIFY_EXPR);
1024 TREE_OPERAND (temp, 1) = *p;
1025 *p = temp;
1027 else
1029 temp = create_tmp_var (type, "retval");
1030 *p = build2 (INIT_EXPR, type, temp, *p);
1033 return temp;
1036 return NULL_TREE;
1039 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1040 a temporary through which they communicate. */
1042 static void
1043 build_stack_save_restore (gcall **save, gcall **restore)
1045 tree tmp_var;
1047 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1048 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1049 gimple_call_set_lhs (*save, tmp_var);
1051 *restore
1052 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1053 1, tmp_var);
1056 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1058 static enum gimplify_status
1059 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1061 tree bind_expr = *expr_p;
1062 bool old_save_stack = gimplify_ctxp->save_stack;
1063 tree t;
1064 gbind *bind_stmt;
1065 gimple_seq body, cleanup;
1066 gcall *stack_save;
1067 location_t start_locus = 0, end_locus = 0;
1069 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1071 /* Mark variables seen in this bind expr. */
1072 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1074 if (TREE_CODE (t) == VAR_DECL)
1076 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1078 /* Mark variable as local. */
1079 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1080 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1081 || splay_tree_lookup (ctx->variables,
1082 (splay_tree_key) t) == NULL))
1084 if (ctx->region_type == ORT_SIMD
1085 && TREE_ADDRESSABLE (t)
1086 && !TREE_STATIC (t))
1087 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1088 else
1089 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1092 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1094 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1095 cfun->has_local_explicit_reg_vars = true;
1098 /* Preliminarily mark non-addressed complex variables as eligible
1099 for promotion to gimple registers. We'll transform their uses
1100 as we find them. */
1101 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1102 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1103 && !TREE_THIS_VOLATILE (t)
1104 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1105 && !needs_to_live_in_memory (t))
1106 DECL_GIMPLE_REG_P (t) = 1;
1109 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1110 BIND_EXPR_BLOCK (bind_expr));
1111 gimple_push_bind_expr (bind_stmt);
1113 gimplify_ctxp->save_stack = false;
1115 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1116 body = NULL;
1117 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1118 gimple_bind_set_body (bind_stmt, body);
1120 /* Source location wise, the cleanup code (stack_restore and clobbers)
1121 belongs to the end of the block, so propagate what we have. The
1122 stack_save operation belongs to the beginning of block, which we can
1123 infer from the bind_expr directly if the block has no explicit
1124 assignment. */
1125 if (BIND_EXPR_BLOCK (bind_expr))
1127 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1128 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1130 if (start_locus == 0)
1131 start_locus = EXPR_LOCATION (bind_expr);
1133 cleanup = NULL;
1134 stack_save = NULL;
1135 if (gimplify_ctxp->save_stack)
1137 gcall *stack_restore;
1139 /* Save stack on entry and restore it on exit. Add a try_finally
1140 block to achieve this. */
1141 build_stack_save_restore (&stack_save, &stack_restore);
1143 gimple_set_location (stack_save, start_locus);
1144 gimple_set_location (stack_restore, end_locus);
1146 gimplify_seq_add_stmt (&cleanup, stack_restore);
1149 /* Add clobbers for all variables that go out of scope. */
1150 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1152 if (TREE_CODE (t) == VAR_DECL
1153 && !is_global_var (t)
1154 && DECL_CONTEXT (t) == current_function_decl
1155 && !DECL_HARD_REGISTER (t)
1156 && !TREE_THIS_VOLATILE (t)
1157 && !DECL_HAS_VALUE_EXPR_P (t)
1158 /* Only care for variables that have to be in memory. Others
1159 will be rewritten into SSA names, hence moved to the top-level. */
1160 && !is_gimple_reg (t)
1161 && flag_stack_reuse != SR_NONE)
1163 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1164 gimple *clobber_stmt;
1165 TREE_THIS_VOLATILE (clobber) = 1;
1166 clobber_stmt = gimple_build_assign (t, clobber);
1167 gimple_set_location (clobber_stmt, end_locus);
1168 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1172 if (cleanup)
1174 gtry *gs;
1175 gimple_seq new_body;
1177 new_body = NULL;
1178 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1179 GIMPLE_TRY_FINALLY);
1181 if (stack_save)
1182 gimplify_seq_add_stmt (&new_body, stack_save);
1183 gimplify_seq_add_stmt (&new_body, gs);
1184 gimple_bind_set_body (bind_stmt, new_body);
1187 gimplify_ctxp->save_stack = old_save_stack;
1188 gimple_pop_bind_expr ();
1190 gimplify_seq_add_stmt (pre_p, bind_stmt);
1192 if (temp)
1194 *expr_p = temp;
1195 return GS_OK;
1198 *expr_p = NULL_TREE;
1199 return GS_ALL_DONE;
1202 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1203 GIMPLE value, it is assigned to a new temporary and the statement is
1204 re-written to return the temporary.
1206 PRE_P points to the sequence where side effects that must happen before
1207 STMT should be stored. */
1209 static enum gimplify_status
1210 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1212 greturn *ret;
1213 tree ret_expr = TREE_OPERAND (stmt, 0);
1214 tree result_decl, result;
1216 if (ret_expr == error_mark_node)
1217 return GS_ERROR;
1219 /* Implicit _Cilk_sync must be inserted right before any return statement
1220 if there is a _Cilk_spawn in the function. If the user has provided a
1221 _Cilk_sync, the optimizer should remove this duplicate one. */
1222 if (fn_contains_cilk_spawn_p (cfun))
1224 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1225 gimplify_and_add (impl_sync, pre_p);
1228 if (!ret_expr
1229 || TREE_CODE (ret_expr) == RESULT_DECL
1230 || ret_expr == error_mark_node)
1232 greturn *ret = gimple_build_return (ret_expr);
1233 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1234 gimplify_seq_add_stmt (pre_p, ret);
1235 return GS_ALL_DONE;
1238 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1239 result_decl = NULL_TREE;
1240 else
1242 result_decl = TREE_OPERAND (ret_expr, 0);
1244 /* See through a return by reference. */
1245 if (TREE_CODE (result_decl) == INDIRECT_REF)
1246 result_decl = TREE_OPERAND (result_decl, 0);
1248 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1249 || TREE_CODE (ret_expr) == INIT_EXPR)
1250 && TREE_CODE (result_decl) == RESULT_DECL);
1253 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1254 Recall that aggregate_value_p is FALSE for any aggregate type that is
1255 returned in registers. If we're returning values in registers, then
1256 we don't want to extend the lifetime of the RESULT_DECL, particularly
1257 across another call. In addition, for those aggregates for which
1258 hard_function_value generates a PARALLEL, we'll die during normal
1259 expansion of structure assignments; there's special code in expand_return
1260 to handle this case that does not exist in expand_expr. */
1261 if (!result_decl)
1262 result = NULL_TREE;
1263 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1265 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1267 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1268 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1269 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1270 should be effectively allocated by the caller, i.e. all calls to
1271 this function must be subject to the Return Slot Optimization. */
1272 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1273 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1275 result = result_decl;
1277 else if (gimplify_ctxp->return_temp)
1278 result = gimplify_ctxp->return_temp;
1279 else
1281 result = create_tmp_reg (TREE_TYPE (result_decl));
1283 /* ??? With complex control flow (usually involving abnormal edges),
1284 we can wind up warning about an uninitialized value for this. Due
1285 to how this variable is constructed and initialized, this is never
1286 true. Give up and never warn. */
1287 TREE_NO_WARNING (result) = 1;
1289 gimplify_ctxp->return_temp = result;
1292 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1293 Then gimplify the whole thing. */
1294 if (result != result_decl)
1295 TREE_OPERAND (ret_expr, 0) = result;
1297 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1299 ret = gimple_build_return (result);
1300 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1301 gimplify_seq_add_stmt (pre_p, ret);
1303 return GS_ALL_DONE;
1306 /* Gimplify a variable-length array DECL. */
1308 static void
1309 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1311 /* This is a variable-sized decl. Simplify its size and mark it
1312 for deferred expansion. */
1313 tree t, addr, ptr_type;
1315 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1316 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1318 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1319 if (DECL_HAS_VALUE_EXPR_P (decl))
1320 return;
1322 /* All occurrences of this decl in final gimplified code will be
1323 replaced by indirection. Setting DECL_VALUE_EXPR does two
1324 things: First, it lets the rest of the gimplifier know what
1325 replacement to use. Second, it lets the debug info know
1326 where to find the value. */
1327 ptr_type = build_pointer_type (TREE_TYPE (decl));
1328 addr = create_tmp_var (ptr_type, get_name (decl));
1329 DECL_IGNORED_P (addr) = 0;
1330 t = build_fold_indirect_ref (addr);
1331 TREE_THIS_NOTRAP (t) = 1;
1332 SET_DECL_VALUE_EXPR (decl, t);
1333 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1335 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1336 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1337 size_int (DECL_ALIGN (decl)));
1338 /* The call has been built for a variable-sized object. */
1339 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1340 t = fold_convert (ptr_type, t);
1341 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1343 gimplify_and_add (t, seq_p);
1345 /* Indicate that we need to restore the stack level when the
1346 enclosing BIND_EXPR is exited. */
1347 gimplify_ctxp->save_stack = true;
1350 /* A helper function to be called via walk_tree. Mark all labels under *TP
1351 as being forced. To be called for DECL_INITIAL of static variables. */
1353 static tree
1354 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1356 if (TYPE_P (*tp))
1357 *walk_subtrees = 0;
1358 if (TREE_CODE (*tp) == LABEL_DECL)
1359 FORCED_LABEL (*tp) = 1;
1361 return NULL_TREE;
1364 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1365 and initialization explicit. */
1367 static enum gimplify_status
1368 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1370 tree stmt = *stmt_p;
1371 tree decl = DECL_EXPR_DECL (stmt);
1373 *stmt_p = NULL_TREE;
1375 if (TREE_TYPE (decl) == error_mark_node)
1376 return GS_ERROR;
1378 if ((TREE_CODE (decl) == TYPE_DECL
1379 || TREE_CODE (decl) == VAR_DECL)
1380 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1381 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1383 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1384 in case its size expressions contain problematic nodes like CALL_EXPR. */
1385 if (TREE_CODE (decl) == TYPE_DECL
1386 && DECL_ORIGINAL_TYPE (decl)
1387 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1388 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1390 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1392 tree init = DECL_INITIAL (decl);
1394 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1395 || (!TREE_STATIC (decl)
1396 && flag_stack_check == GENERIC_STACK_CHECK
1397 && compare_tree_int (DECL_SIZE_UNIT (decl),
1398 STACK_CHECK_MAX_VAR_SIZE) > 0))
1399 gimplify_vla_decl (decl, seq_p);
1401 /* Some front ends do not explicitly declare all anonymous
1402 artificial variables. We compensate here by declaring the
1403 variables, though it would be better if the front ends would
1404 explicitly declare them. */
1405 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1406 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1407 gimple_add_tmp_var (decl);
1409 if (init && init != error_mark_node)
1411 if (!TREE_STATIC (decl))
1413 DECL_INITIAL (decl) = NULL_TREE;
1414 init = build2 (INIT_EXPR, void_type_node, decl, init);
1415 gimplify_and_add (init, seq_p);
1416 ggc_free (init);
1418 else
1419 /* We must still examine initializers for static variables
1420 as they may contain a label address. */
1421 walk_tree (&init, force_labels_r, NULL, NULL);
1425 return GS_ALL_DONE;
1428 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1429 and replacing the LOOP_EXPR with goto, but if the loop contains an
1430 EXIT_EXPR, we need to append a label for it to jump to. */
1432 static enum gimplify_status
1433 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1435 tree saved_label = gimplify_ctxp->exit_label;
1436 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1438 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1440 gimplify_ctxp->exit_label = NULL_TREE;
1442 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1444 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1446 if (gimplify_ctxp->exit_label)
1447 gimplify_seq_add_stmt (pre_p,
1448 gimple_build_label (gimplify_ctxp->exit_label));
1450 gimplify_ctxp->exit_label = saved_label;
1452 *expr_p = NULL;
1453 return GS_ALL_DONE;
1456 /* Gimplify a statement list onto a sequence. These may be created either
1457 by an enlightened front-end, or by shortcut_cond_expr. */
1459 static enum gimplify_status
1460 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1462 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1464 tree_stmt_iterator i = tsi_start (*expr_p);
1466 while (!tsi_end_p (i))
1468 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1469 tsi_delink (&i);
1472 if (temp)
1474 *expr_p = temp;
1475 return GS_OK;
1478 return GS_ALL_DONE;
1482 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1483 branch to. */
1485 static enum gimplify_status
1486 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1488 tree switch_expr = *expr_p;
1489 gimple_seq switch_body_seq = NULL;
1490 enum gimplify_status ret;
1491 tree index_type = TREE_TYPE (switch_expr);
1492 if (index_type == NULL_TREE)
1493 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1495 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1496 fb_rvalue);
1497 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1498 return ret;
1500 if (SWITCH_BODY (switch_expr))
1502 vec<tree> labels;
1503 vec<tree> saved_labels;
1504 tree default_case = NULL_TREE;
1505 gswitch *switch_stmt;
1507 /* If someone can be bothered to fill in the labels, they can
1508 be bothered to null out the body too. */
1509 gcc_assert (!SWITCH_LABELS (switch_expr));
1511 /* Save old labels, get new ones from body, then restore the old
1512 labels. Save all the things from the switch body to append after. */
1513 saved_labels = gimplify_ctxp->case_labels;
1514 gimplify_ctxp->case_labels.create (8);
1516 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1517 labels = gimplify_ctxp->case_labels;
1518 gimplify_ctxp->case_labels = saved_labels;
1520 preprocess_case_label_vec_for_gimple (labels, index_type,
1521 &default_case);
1523 if (!default_case)
1525 glabel *new_default;
1527 default_case
1528 = build_case_label (NULL_TREE, NULL_TREE,
1529 create_artificial_label (UNKNOWN_LOCATION));
1530 new_default = gimple_build_label (CASE_LABEL (default_case));
1531 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1534 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1535 default_case, labels);
1536 gimplify_seq_add_stmt (pre_p, switch_stmt);
1537 gimplify_seq_add_seq (pre_p, switch_body_seq);
1538 labels.release ();
1540 else
1541 gcc_assert (SWITCH_LABELS (switch_expr));
1543 return GS_ALL_DONE;
1546 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1548 static enum gimplify_status
1549 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1551 struct gimplify_ctx *ctxp;
1552 glabel *label_stmt;
1554 /* Invalid programs can play Duff's Device type games with, for example,
1555 #pragma omp parallel. At least in the C front end, we don't
1556 detect such invalid branches until after gimplification, in the
1557 diagnose_omp_blocks pass. */
1558 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1559 if (ctxp->case_labels.exists ())
1560 break;
1562 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1563 ctxp->case_labels.safe_push (*expr_p);
1564 gimplify_seq_add_stmt (pre_p, label_stmt);
1566 return GS_ALL_DONE;
1569 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1570 if necessary. */
1572 tree
1573 build_and_jump (tree *label_p)
1575 if (label_p == NULL)
1576 /* If there's nowhere to jump, just fall through. */
1577 return NULL_TREE;
1579 if (*label_p == NULL_TREE)
1581 tree label = create_artificial_label (UNKNOWN_LOCATION);
1582 *label_p = label;
1585 return build1 (GOTO_EXPR, void_type_node, *label_p);
1588 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1589 This also involves building a label to jump to and communicating it to
1590 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1592 static enum gimplify_status
1593 gimplify_exit_expr (tree *expr_p)
1595 tree cond = TREE_OPERAND (*expr_p, 0);
1596 tree expr;
1598 expr = build_and_jump (&gimplify_ctxp->exit_label);
1599 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1600 *expr_p = expr;
1602 return GS_OK;
1605 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1606 different from its canonical type, wrap the whole thing inside a
1607 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1608 type.
1610 The canonical type of a COMPONENT_REF is the type of the field being
1611 referenced--unless the field is a bit-field which can be read directly
1612 in a smaller mode, in which case the canonical type is the
1613 sign-appropriate type corresponding to that mode. */
1615 static void
1616 canonicalize_component_ref (tree *expr_p)
1618 tree expr = *expr_p;
1619 tree type;
1621 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1623 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1624 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1625 else
1626 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1628 /* One could argue that all the stuff below is not necessary for
1629 the non-bitfield case and declare it a FE error if type
1630 adjustment would be needed. */
1631 if (TREE_TYPE (expr) != type)
1633 #ifdef ENABLE_TYPES_CHECKING
1634 tree old_type = TREE_TYPE (expr);
1635 #endif
1636 int type_quals;
1638 /* We need to preserve qualifiers and propagate them from
1639 operand 0. */
1640 type_quals = TYPE_QUALS (type)
1641 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1642 if (TYPE_QUALS (type) != type_quals)
1643 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1645 /* Set the type of the COMPONENT_REF to the underlying type. */
1646 TREE_TYPE (expr) = type;
1648 #ifdef ENABLE_TYPES_CHECKING
1649 /* It is now a FE error, if the conversion from the canonical
1650 type to the original expression type is not useless. */
1651 gcc_assert (useless_type_conversion_p (old_type, type));
1652 #endif
1656 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1657 to foo, embed that change in the ADDR_EXPR by converting
1658 T array[U];
1659 (T *)&array
1661 &array[L]
1662 where L is the lower bound. For simplicity, only do this for constant
1663 lower bound.
1664 The constraint is that the type of &array[L] is trivially convertible
1665 to T *. */
1667 static void
1668 canonicalize_addr_expr (tree *expr_p)
1670 tree expr = *expr_p;
1671 tree addr_expr = TREE_OPERAND (expr, 0);
1672 tree datype, ddatype, pddatype;
1674 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1675 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1676 || TREE_CODE (addr_expr) != ADDR_EXPR)
1677 return;
1679 /* The addr_expr type should be a pointer to an array. */
1680 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1681 if (TREE_CODE (datype) != ARRAY_TYPE)
1682 return;
1684 /* The pointer to element type shall be trivially convertible to
1685 the expression pointer type. */
1686 ddatype = TREE_TYPE (datype);
1687 pddatype = build_pointer_type (ddatype);
1688 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1689 pddatype))
1690 return;
1692 /* The lower bound and element sizes must be constant. */
1693 if (!TYPE_SIZE_UNIT (ddatype)
1694 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1695 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1696 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1697 return;
1699 /* All checks succeeded. Build a new node to merge the cast. */
1700 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1701 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1702 NULL_TREE, NULL_TREE);
1703 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1705 /* We can have stripped a required restrict qualifier above. */
1706 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1707 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1710 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1711 underneath as appropriate. */
1713 static enum gimplify_status
1714 gimplify_conversion (tree *expr_p)
1716 location_t loc = EXPR_LOCATION (*expr_p);
1717 gcc_assert (CONVERT_EXPR_P (*expr_p));
1719 /* Then strip away all but the outermost conversion. */
1720 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1722 /* And remove the outermost conversion if it's useless. */
1723 if (tree_ssa_useless_type_conversion (*expr_p))
1724 *expr_p = TREE_OPERAND (*expr_p, 0);
1726 /* If we still have a conversion at the toplevel,
1727 then canonicalize some constructs. */
1728 if (CONVERT_EXPR_P (*expr_p))
1730 tree sub = TREE_OPERAND (*expr_p, 0);
1732 /* If a NOP conversion is changing the type of a COMPONENT_REF
1733 expression, then canonicalize its type now in order to expose more
1734 redundant conversions. */
1735 if (TREE_CODE (sub) == COMPONENT_REF)
1736 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1738 /* If a NOP conversion is changing a pointer to array of foo
1739 to a pointer to foo, embed that change in the ADDR_EXPR. */
1740 else if (TREE_CODE (sub) == ADDR_EXPR)
1741 canonicalize_addr_expr (expr_p);
1744 /* If we have a conversion to a non-register type force the
1745 use of a VIEW_CONVERT_EXPR instead. */
1746 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1747 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1748 TREE_OPERAND (*expr_p, 0));
1750 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1751 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1752 TREE_SET_CODE (*expr_p, NOP_EXPR);
1754 return GS_OK;
1757 /* Nonlocal VLAs seen in the current function. */
1758 static hash_set<tree> *nonlocal_vlas;
1760 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1761 static tree nonlocal_vla_vars;
1763 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1764 DECL_VALUE_EXPR, and it's worth re-examining things. */
1766 static enum gimplify_status
1767 gimplify_var_or_parm_decl (tree *expr_p)
1769 tree decl = *expr_p;
1771 /* ??? If this is a local variable, and it has not been seen in any
1772 outer BIND_EXPR, then it's probably the result of a duplicate
1773 declaration, for which we've already issued an error. It would
1774 be really nice if the front end wouldn't leak these at all.
1775 Currently the only known culprit is C++ destructors, as seen
1776 in g++.old-deja/g++.jason/binding.C. */
1777 if (TREE_CODE (decl) == VAR_DECL
1778 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1779 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1780 && decl_function_context (decl) == current_function_decl)
1782 gcc_assert (seen_error ());
1783 return GS_ERROR;
1786 /* When within an OMP context, notice uses of variables. */
1787 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1788 return GS_ALL_DONE;
1790 /* If the decl is an alias for another expression, substitute it now. */
1791 if (DECL_HAS_VALUE_EXPR_P (decl))
1793 tree value_expr = DECL_VALUE_EXPR (decl);
1795 /* For referenced nonlocal VLAs add a decl for debugging purposes
1796 to the current function. */
1797 if (TREE_CODE (decl) == VAR_DECL
1798 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1799 && nonlocal_vlas != NULL
1800 && TREE_CODE (value_expr) == INDIRECT_REF
1801 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1802 && decl_function_context (decl) != current_function_decl)
1804 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1805 while (ctx
1806 && (ctx->region_type == ORT_WORKSHARE
1807 || ctx->region_type == ORT_SIMD))
1808 ctx = ctx->outer_context;
1809 if (!ctx && !nonlocal_vlas->add (decl))
1811 tree copy = copy_node (decl);
1813 lang_hooks.dup_lang_specific_decl (copy);
1814 SET_DECL_RTL (copy, 0);
1815 TREE_USED (copy) = 1;
1816 DECL_CHAIN (copy) = nonlocal_vla_vars;
1817 nonlocal_vla_vars = copy;
1818 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1819 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1823 *expr_p = unshare_expr (value_expr);
1824 return GS_OK;
1827 return GS_ALL_DONE;
1830 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1832 static void
1833 recalculate_side_effects (tree t)
1835 enum tree_code code = TREE_CODE (t);
1836 int len = TREE_OPERAND_LENGTH (t);
1837 int i;
1839 switch (TREE_CODE_CLASS (code))
1841 case tcc_expression:
1842 switch (code)
1844 case INIT_EXPR:
1845 case MODIFY_EXPR:
1846 case VA_ARG_EXPR:
1847 case PREDECREMENT_EXPR:
1848 case PREINCREMENT_EXPR:
1849 case POSTDECREMENT_EXPR:
1850 case POSTINCREMENT_EXPR:
1851 /* All of these have side-effects, no matter what their
1852 operands are. */
1853 return;
1855 default:
1856 break;
1858 /* Fall through. */
1860 case tcc_comparison: /* a comparison expression */
1861 case tcc_unary: /* a unary arithmetic expression */
1862 case tcc_binary: /* a binary arithmetic expression */
1863 case tcc_reference: /* a reference */
1864 case tcc_vl_exp: /* a function call */
1865 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1866 for (i = 0; i < len; ++i)
1868 tree op = TREE_OPERAND (t, i);
1869 if (op && TREE_SIDE_EFFECTS (op))
1870 TREE_SIDE_EFFECTS (t) = 1;
1872 break;
1874 case tcc_constant:
1875 /* No side-effects. */
1876 return;
1878 default:
1879 gcc_unreachable ();
1883 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1884 node *EXPR_P.
1886 compound_lval
1887 : min_lval '[' val ']'
1888 | min_lval '.' ID
1889 | compound_lval '[' val ']'
1890 | compound_lval '.' ID
1892 This is not part of the original SIMPLE definition, which separates
1893 array and member references, but it seems reasonable to handle them
1894 together. Also, this way we don't run into problems with union
1895 aliasing; gcc requires that for accesses through a union to alias, the
1896 union reference must be explicit, which was not always the case when we
1897 were splitting up array and member refs.
1899 PRE_P points to the sequence where side effects that must happen before
1900 *EXPR_P should be stored.
1902 POST_P points to the sequence where side effects that must happen after
1903 *EXPR_P should be stored. */
1905 static enum gimplify_status
1906 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1907 fallback_t fallback)
1909 tree *p;
1910 enum gimplify_status ret = GS_ALL_DONE, tret;
1911 int i;
1912 location_t loc = EXPR_LOCATION (*expr_p);
1913 tree expr = *expr_p;
1915 /* Create a stack of the subexpressions so later we can walk them in
1916 order from inner to outer. */
1917 auto_vec<tree, 10> expr_stack;
1919 /* We can handle anything that get_inner_reference can deal with. */
1920 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1922 restart:
1923 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1924 if (TREE_CODE (*p) == INDIRECT_REF)
1925 *p = fold_indirect_ref_loc (loc, *p);
1927 if (handled_component_p (*p))
1929 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1930 additional COMPONENT_REFs. */
1931 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1932 && gimplify_var_or_parm_decl (p) == GS_OK)
1933 goto restart;
1934 else
1935 break;
1937 expr_stack.safe_push (*p);
1940 gcc_assert (expr_stack.length ());
1942 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1943 walked through and P points to the innermost expression.
1945 Java requires that we elaborated nodes in source order. That
1946 means we must gimplify the inner expression followed by each of
1947 the indices, in order. But we can't gimplify the inner
1948 expression until we deal with any variable bounds, sizes, or
1949 positions in order to deal with PLACEHOLDER_EXPRs.
1951 So we do this in three steps. First we deal with the annotations
1952 for any variables in the components, then we gimplify the base,
1953 then we gimplify any indices, from left to right. */
1954 for (i = expr_stack.length () - 1; i >= 0; i--)
1956 tree t = expr_stack[i];
1958 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1960 /* Gimplify the low bound and element type size and put them into
1961 the ARRAY_REF. If these values are set, they have already been
1962 gimplified. */
1963 if (TREE_OPERAND (t, 2) == NULL_TREE)
1965 tree low = unshare_expr (array_ref_low_bound (t));
1966 if (!is_gimple_min_invariant (low))
1968 TREE_OPERAND (t, 2) = low;
1969 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1970 post_p, is_gimple_reg,
1971 fb_rvalue);
1972 ret = MIN (ret, tret);
1975 else
1977 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1978 is_gimple_reg, fb_rvalue);
1979 ret = MIN (ret, tret);
1982 if (TREE_OPERAND (t, 3) == NULL_TREE)
1984 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1985 tree elmt_size = unshare_expr (array_ref_element_size (t));
1986 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1988 /* Divide the element size by the alignment of the element
1989 type (above). */
1990 elmt_size
1991 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1993 if (!is_gimple_min_invariant (elmt_size))
1995 TREE_OPERAND (t, 3) = elmt_size;
1996 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1997 post_p, is_gimple_reg,
1998 fb_rvalue);
1999 ret = MIN (ret, tret);
2002 else
2004 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2005 is_gimple_reg, fb_rvalue);
2006 ret = MIN (ret, tret);
2009 else if (TREE_CODE (t) == COMPONENT_REF)
2011 /* Set the field offset into T and gimplify it. */
2012 if (TREE_OPERAND (t, 2) == NULL_TREE)
2014 tree offset = unshare_expr (component_ref_field_offset (t));
2015 tree field = TREE_OPERAND (t, 1);
2016 tree factor
2017 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2019 /* Divide the offset by its alignment. */
2020 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2022 if (!is_gimple_min_invariant (offset))
2024 TREE_OPERAND (t, 2) = offset;
2025 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2026 post_p, is_gimple_reg,
2027 fb_rvalue);
2028 ret = MIN (ret, tret);
2031 else
2033 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2034 is_gimple_reg, fb_rvalue);
2035 ret = MIN (ret, tret);
2040 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2041 so as to match the min_lval predicate. Failure to do so may result
2042 in the creation of large aggregate temporaries. */
2043 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2044 fallback | fb_lvalue);
2045 ret = MIN (ret, tret);
2047 /* And finally, the indices and operands of ARRAY_REF. During this
2048 loop we also remove any useless conversions. */
2049 for (; expr_stack.length () > 0; )
2051 tree t = expr_stack.pop ();
2053 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2055 /* Gimplify the dimension. */
2056 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2058 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2059 is_gimple_val, fb_rvalue);
2060 ret = MIN (ret, tret);
2064 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2066 /* The innermost expression P may have originally had
2067 TREE_SIDE_EFFECTS set which would have caused all the outer
2068 expressions in *EXPR_P leading to P to also have had
2069 TREE_SIDE_EFFECTS set. */
2070 recalculate_side_effects (t);
2073 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2074 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2076 canonicalize_component_ref (expr_p);
2079 expr_stack.release ();
2081 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2083 return ret;
2086 /* Gimplify the self modifying expression pointed to by EXPR_P
2087 (++, --, +=, -=).
2089 PRE_P points to the list where side effects that must happen before
2090 *EXPR_P should be stored.
2092 POST_P points to the list where side effects that must happen after
2093 *EXPR_P should be stored.
2095 WANT_VALUE is nonzero iff we want to use the value of this expression
2096 in another expression.
2098 ARITH_TYPE is the type the computation should be performed in. */
2100 enum gimplify_status
2101 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2102 bool want_value, tree arith_type)
2104 enum tree_code code;
2105 tree lhs, lvalue, rhs, t1;
2106 gimple_seq post = NULL, *orig_post_p = post_p;
2107 bool postfix;
2108 enum tree_code arith_code;
2109 enum gimplify_status ret;
2110 location_t loc = EXPR_LOCATION (*expr_p);
2112 code = TREE_CODE (*expr_p);
2114 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2115 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2117 /* Prefix or postfix? */
2118 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2119 /* Faster to treat as prefix if result is not used. */
2120 postfix = want_value;
2121 else
2122 postfix = false;
2124 /* For postfix, make sure the inner expression's post side effects
2125 are executed after side effects from this expression. */
2126 if (postfix)
2127 post_p = &post;
2129 /* Add or subtract? */
2130 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2131 arith_code = PLUS_EXPR;
2132 else
2133 arith_code = MINUS_EXPR;
2135 /* Gimplify the LHS into a GIMPLE lvalue. */
2136 lvalue = TREE_OPERAND (*expr_p, 0);
2137 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2138 if (ret == GS_ERROR)
2139 return ret;
2141 /* Extract the operands to the arithmetic operation. */
2142 lhs = lvalue;
2143 rhs = TREE_OPERAND (*expr_p, 1);
2145 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2146 that as the result value and in the postqueue operation. */
2147 if (postfix)
2149 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2150 if (ret == GS_ERROR)
2151 return ret;
2153 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2156 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2157 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2159 rhs = convert_to_ptrofftype_loc (loc, rhs);
2160 if (arith_code == MINUS_EXPR)
2161 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2162 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2164 else
2165 t1 = fold_convert (TREE_TYPE (*expr_p),
2166 fold_build2 (arith_code, arith_type,
2167 fold_convert (arith_type, lhs),
2168 fold_convert (arith_type, rhs)));
2170 if (postfix)
2172 gimplify_assign (lvalue, t1, pre_p);
2173 gimplify_seq_add_seq (orig_post_p, post);
2174 *expr_p = lhs;
2175 return GS_ALL_DONE;
2177 else
2179 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2180 return GS_OK;
2184 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2186 static void
2187 maybe_with_size_expr (tree *expr_p)
2189 tree expr = *expr_p;
2190 tree type = TREE_TYPE (expr);
2191 tree size;
2193 /* If we've already wrapped this or the type is error_mark_node, we can't do
2194 anything. */
2195 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2196 || type == error_mark_node)
2197 return;
2199 /* If the size isn't known or is a constant, we have nothing to do. */
2200 size = TYPE_SIZE_UNIT (type);
2201 if (!size || TREE_CODE (size) == INTEGER_CST)
2202 return;
2204 /* Otherwise, make a WITH_SIZE_EXPR. */
2205 size = unshare_expr (size);
2206 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2207 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2210 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2211 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2212 the CALL_EXPR. */
2214 enum gimplify_status
2215 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2217 bool (*test) (tree);
2218 fallback_t fb;
2220 /* In general, we allow lvalues for function arguments to avoid
2221 extra overhead of copying large aggregates out of even larger
2222 aggregates into temporaries only to copy the temporaries to
2223 the argument list. Make optimizers happy by pulling out to
2224 temporaries those types that fit in registers. */
2225 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2226 test = is_gimple_val, fb = fb_rvalue;
2227 else
2229 test = is_gimple_lvalue, fb = fb_either;
2230 /* Also strip a TARGET_EXPR that would force an extra copy. */
2231 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2233 tree init = TARGET_EXPR_INITIAL (*arg_p);
2234 if (init
2235 && !VOID_TYPE_P (TREE_TYPE (init)))
2236 *arg_p = init;
2240 /* If this is a variable sized type, we must remember the size. */
2241 maybe_with_size_expr (arg_p);
2243 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2244 /* Make sure arguments have the same location as the function call
2245 itself. */
2246 protected_set_expr_location (*arg_p, call_location);
2248 /* There is a sequence point before a function call. Side effects in
2249 the argument list must occur before the actual call. So, when
2250 gimplifying arguments, force gimplify_expr to use an internal
2251 post queue which is then appended to the end of PRE_P. */
2252 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2255 /* Don't fold inside offloading or taskreg regions: it can break code by
2256 adding decl references that weren't in the source. We'll do it during
2257 omplower pass instead. */
2259 static bool
2260 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2262 struct gimplify_omp_ctx *ctx;
2263 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2264 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
2265 return false;
2266 return fold_stmt (gsi);
2269 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2270 WANT_VALUE is true if the result of the call is desired. */
2272 static enum gimplify_status
2273 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2275 tree fndecl, parms, p, fnptrtype;
2276 enum gimplify_status ret;
2277 int i, nargs;
2278 gcall *call;
2279 bool builtin_va_start_p = false;
2280 location_t loc = EXPR_LOCATION (*expr_p);
2282 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2284 /* For reliable diagnostics during inlining, it is necessary that
2285 every call_expr be annotated with file and line. */
2286 if (! EXPR_HAS_LOCATION (*expr_p))
2287 SET_EXPR_LOCATION (*expr_p, input_location);
2289 /* Gimplify internal functions created in the FEs. */
2290 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2292 if (want_value)
2293 return GS_ALL_DONE;
2295 nargs = call_expr_nargs (*expr_p);
2296 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2297 auto_vec<tree> vargs (nargs);
2299 for (i = 0; i < nargs; i++)
2301 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2302 EXPR_LOCATION (*expr_p));
2303 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2305 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
2306 gimplify_seq_add_stmt (pre_p, call);
2307 return GS_ALL_DONE;
2310 /* This may be a call to a builtin function.
2312 Builtin function calls may be transformed into different
2313 (and more efficient) builtin function calls under certain
2314 circumstances. Unfortunately, gimplification can muck things
2315 up enough that the builtin expanders are not aware that certain
2316 transformations are still valid.
2318 So we attempt transformation/gimplification of the call before
2319 we gimplify the CALL_EXPR. At this time we do not manage to
2320 transform all calls in the same manner as the expanders do, but
2321 we do transform most of them. */
2322 fndecl = get_callee_fndecl (*expr_p);
2323 if (fndecl
2324 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2325 switch (DECL_FUNCTION_CODE (fndecl))
2327 case BUILT_IN_VA_START:
2329 builtin_va_start_p = TRUE;
2330 if (call_expr_nargs (*expr_p) < 2)
2332 error ("too few arguments to function %<va_start%>");
2333 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2334 return GS_OK;
2337 if (fold_builtin_next_arg (*expr_p, true))
2339 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2340 return GS_OK;
2342 break;
2344 case BUILT_IN_LINE:
2346 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2347 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2348 return GS_OK;
2350 case BUILT_IN_FILE:
2352 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2353 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2354 return GS_OK;
2356 case BUILT_IN_FUNCTION:
2358 const char *function;
2359 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2360 *expr_p = build_string_literal (strlen (function) + 1, function);
2361 return GS_OK;
2363 default:
2366 if (fndecl && DECL_BUILT_IN (fndecl))
2368 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2369 if (new_tree && new_tree != *expr_p)
2371 /* There was a transformation of this call which computes the
2372 same value, but in a more efficient way. Return and try
2373 again. */
2374 *expr_p = new_tree;
2375 return GS_OK;
2379 /* Remember the original function pointer type. */
2380 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2382 /* There is a sequence point before the call, so any side effects in
2383 the calling expression must occur before the actual call. Force
2384 gimplify_expr to use an internal post queue. */
2385 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2386 is_gimple_call_addr, fb_rvalue);
2388 nargs = call_expr_nargs (*expr_p);
2390 /* Get argument types for verification. */
2391 fndecl = get_callee_fndecl (*expr_p);
2392 parms = NULL_TREE;
2393 if (fndecl)
2394 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2395 else
2396 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2398 if (fndecl && DECL_ARGUMENTS (fndecl))
2399 p = DECL_ARGUMENTS (fndecl);
2400 else if (parms)
2401 p = parms;
2402 else
2403 p = NULL_TREE;
2404 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2407 /* If the last argument is __builtin_va_arg_pack () and it is not
2408 passed as a named argument, decrease the number of CALL_EXPR
2409 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2410 if (!p
2411 && i < nargs
2412 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2414 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2415 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2417 if (last_arg_fndecl
2418 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2419 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2420 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2422 tree call = *expr_p;
2424 --nargs;
2425 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2426 CALL_EXPR_FN (call),
2427 nargs, CALL_EXPR_ARGP (call));
2429 /* Copy all CALL_EXPR flags, location and block, except
2430 CALL_EXPR_VA_ARG_PACK flag. */
2431 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2432 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2433 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2434 = CALL_EXPR_RETURN_SLOT_OPT (call);
2435 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2436 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2438 /* Set CALL_EXPR_VA_ARG_PACK. */
2439 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2443 /* Gimplify the function arguments. */
2444 if (nargs > 0)
2446 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2447 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2448 PUSH_ARGS_REVERSED ? i-- : i++)
2450 enum gimplify_status t;
2452 /* Avoid gimplifying the second argument to va_start, which needs to
2453 be the plain PARM_DECL. */
2454 if ((i != 1) || !builtin_va_start_p)
2456 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2457 EXPR_LOCATION (*expr_p));
2459 if (t == GS_ERROR)
2460 ret = GS_ERROR;
2465 /* Gimplify the static chain. */
2466 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2468 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2469 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2470 else
2472 enum gimplify_status t;
2473 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2474 EXPR_LOCATION (*expr_p));
2475 if (t == GS_ERROR)
2476 ret = GS_ERROR;
2480 /* Verify the function result. */
2481 if (want_value && fndecl
2482 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2484 error_at (loc, "using result of function returning %<void%>");
2485 ret = GS_ERROR;
2488 /* Try this again in case gimplification exposed something. */
2489 if (ret != GS_ERROR)
2491 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2493 if (new_tree && new_tree != *expr_p)
2495 /* There was a transformation of this call which computes the
2496 same value, but in a more efficient way. Return and try
2497 again. */
2498 *expr_p = new_tree;
2499 return GS_OK;
2502 else
2504 *expr_p = error_mark_node;
2505 return GS_ERROR;
2508 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2509 decl. This allows us to eliminate redundant or useless
2510 calls to "const" functions. */
2511 if (TREE_CODE (*expr_p) == CALL_EXPR)
2513 int flags = call_expr_flags (*expr_p);
2514 if (flags & (ECF_CONST | ECF_PURE)
2515 /* An infinite loop is considered a side effect. */
2516 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2517 TREE_SIDE_EFFECTS (*expr_p) = 0;
2520 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2521 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2522 form and delegate the creation of a GIMPLE_CALL to
2523 gimplify_modify_expr. This is always possible because when
2524 WANT_VALUE is true, the caller wants the result of this call into
2525 a temporary, which means that we will emit an INIT_EXPR in
2526 internal_get_tmp_var which will then be handled by
2527 gimplify_modify_expr. */
2528 if (!want_value)
2530 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2531 have to do is replicate it as a GIMPLE_CALL tuple. */
2532 gimple_stmt_iterator gsi;
2533 call = gimple_build_call_from_tree (*expr_p);
2534 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2535 notice_special_calls (call);
2536 gimplify_seq_add_stmt (pre_p, call);
2537 gsi = gsi_last (*pre_p);
2538 maybe_fold_stmt (&gsi);
2539 *expr_p = NULL_TREE;
2541 else
2542 /* Remember the original function type. */
2543 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2544 CALL_EXPR_FN (*expr_p));
2546 return ret;
2549 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2550 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2552 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2553 condition is true or false, respectively. If null, we should generate
2554 our own to skip over the evaluation of this specific expression.
2556 LOCUS is the source location of the COND_EXPR.
2558 This function is the tree equivalent of do_jump.
2560 shortcut_cond_r should only be called by shortcut_cond_expr. */
2562 static tree
2563 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2564 location_t locus)
2566 tree local_label = NULL_TREE;
2567 tree t, expr = NULL;
2569 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2570 retain the shortcut semantics. Just insert the gotos here;
2571 shortcut_cond_expr will append the real blocks later. */
2572 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2574 location_t new_locus;
2576 /* Turn if (a && b) into
2578 if (a); else goto no;
2579 if (b) goto yes; else goto no;
2580 (no:) */
2582 if (false_label_p == NULL)
2583 false_label_p = &local_label;
2585 /* Keep the original source location on the first 'if'. */
2586 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2587 append_to_statement_list (t, &expr);
2589 /* Set the source location of the && on the second 'if'. */
2590 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2591 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2592 new_locus);
2593 append_to_statement_list (t, &expr);
2595 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2597 location_t new_locus;
2599 /* Turn if (a || b) into
2601 if (a) goto yes;
2602 if (b) goto yes; else goto no;
2603 (yes:) */
2605 if (true_label_p == NULL)
2606 true_label_p = &local_label;
2608 /* Keep the original source location on the first 'if'. */
2609 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2610 append_to_statement_list (t, &expr);
2612 /* Set the source location of the || on the second 'if'. */
2613 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2614 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2615 new_locus);
2616 append_to_statement_list (t, &expr);
2618 else if (TREE_CODE (pred) == COND_EXPR
2619 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2620 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2622 location_t new_locus;
2624 /* As long as we're messing with gotos, turn if (a ? b : c) into
2625 if (a)
2626 if (b) goto yes; else goto no;
2627 else
2628 if (c) goto yes; else goto no;
2630 Don't do this if one of the arms has void type, which can happen
2631 in C++ when the arm is throw. */
2633 /* Keep the original source location on the first 'if'. Set the source
2634 location of the ? on the second 'if'. */
2635 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2636 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2637 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2638 false_label_p, locus),
2639 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2640 false_label_p, new_locus));
2642 else
2644 expr = build3 (COND_EXPR, void_type_node, pred,
2645 build_and_jump (true_label_p),
2646 build_and_jump (false_label_p));
2647 SET_EXPR_LOCATION (expr, locus);
2650 if (local_label)
2652 t = build1 (LABEL_EXPR, void_type_node, local_label);
2653 append_to_statement_list (t, &expr);
2656 return expr;
2659 /* Given a conditional expression EXPR with short-circuit boolean
2660 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2661 predicate apart into the equivalent sequence of conditionals. */
2663 static tree
2664 shortcut_cond_expr (tree expr)
2666 tree pred = TREE_OPERAND (expr, 0);
2667 tree then_ = TREE_OPERAND (expr, 1);
2668 tree else_ = TREE_OPERAND (expr, 2);
2669 tree true_label, false_label, end_label, t;
2670 tree *true_label_p;
2671 tree *false_label_p;
2672 bool emit_end, emit_false, jump_over_else;
2673 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2674 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2676 /* First do simple transformations. */
2677 if (!else_se)
2679 /* If there is no 'else', turn
2680 if (a && b) then c
2681 into
2682 if (a) if (b) then c. */
2683 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2685 /* Keep the original source location on the first 'if'. */
2686 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2687 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2688 /* Set the source location of the && on the second 'if'. */
2689 if (EXPR_HAS_LOCATION (pred))
2690 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2691 then_ = shortcut_cond_expr (expr);
2692 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2693 pred = TREE_OPERAND (pred, 0);
2694 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2695 SET_EXPR_LOCATION (expr, locus);
2699 if (!then_se)
2701 /* If there is no 'then', turn
2702 if (a || b); else d
2703 into
2704 if (a); else if (b); else d. */
2705 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2707 /* Keep the original source location on the first 'if'. */
2708 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2709 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2710 /* Set the source location of the || on the second 'if'. */
2711 if (EXPR_HAS_LOCATION (pred))
2712 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2713 else_ = shortcut_cond_expr (expr);
2714 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2715 pred = TREE_OPERAND (pred, 0);
2716 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2717 SET_EXPR_LOCATION (expr, locus);
2721 /* If we're done, great. */
2722 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2723 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2724 return expr;
2726 /* Otherwise we need to mess with gotos. Change
2727 if (a) c; else d;
2729 if (a); else goto no;
2730 c; goto end;
2731 no: d; end:
2732 and recursively gimplify the condition. */
2734 true_label = false_label = end_label = NULL_TREE;
2736 /* If our arms just jump somewhere, hijack those labels so we don't
2737 generate jumps to jumps. */
2739 if (then_
2740 && TREE_CODE (then_) == GOTO_EXPR
2741 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2743 true_label = GOTO_DESTINATION (then_);
2744 then_ = NULL;
2745 then_se = false;
2748 if (else_
2749 && TREE_CODE (else_) == GOTO_EXPR
2750 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2752 false_label = GOTO_DESTINATION (else_);
2753 else_ = NULL;
2754 else_se = false;
2757 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2758 if (true_label)
2759 true_label_p = &true_label;
2760 else
2761 true_label_p = NULL;
2763 /* The 'else' branch also needs a label if it contains interesting code. */
2764 if (false_label || else_se)
2765 false_label_p = &false_label;
2766 else
2767 false_label_p = NULL;
2769 /* If there was nothing else in our arms, just forward the label(s). */
2770 if (!then_se && !else_se)
2771 return shortcut_cond_r (pred, true_label_p, false_label_p,
2772 EXPR_LOC_OR_LOC (expr, input_location));
2774 /* If our last subexpression already has a terminal label, reuse it. */
2775 if (else_se)
2776 t = expr_last (else_);
2777 else if (then_se)
2778 t = expr_last (then_);
2779 else
2780 t = NULL;
2781 if (t && TREE_CODE (t) == LABEL_EXPR)
2782 end_label = LABEL_EXPR_LABEL (t);
2784 /* If we don't care about jumping to the 'else' branch, jump to the end
2785 if the condition is false. */
2786 if (!false_label_p)
2787 false_label_p = &end_label;
2789 /* We only want to emit these labels if we aren't hijacking them. */
2790 emit_end = (end_label == NULL_TREE);
2791 emit_false = (false_label == NULL_TREE);
2793 /* We only emit the jump over the else clause if we have to--if the
2794 then clause may fall through. Otherwise we can wind up with a
2795 useless jump and a useless label at the end of gimplified code,
2796 which will cause us to think that this conditional as a whole
2797 falls through even if it doesn't. If we then inline a function
2798 which ends with such a condition, that can cause us to issue an
2799 inappropriate warning about control reaching the end of a
2800 non-void function. */
2801 jump_over_else = block_may_fallthru (then_);
2803 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2804 EXPR_LOC_OR_LOC (expr, input_location));
2806 expr = NULL;
2807 append_to_statement_list (pred, &expr);
2809 append_to_statement_list (then_, &expr);
2810 if (else_se)
2812 if (jump_over_else)
2814 tree last = expr_last (expr);
2815 t = build_and_jump (&end_label);
2816 if (EXPR_HAS_LOCATION (last))
2817 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2818 append_to_statement_list (t, &expr);
2820 if (emit_false)
2822 t = build1 (LABEL_EXPR, void_type_node, false_label);
2823 append_to_statement_list (t, &expr);
2825 append_to_statement_list (else_, &expr);
2827 if (emit_end && end_label)
2829 t = build1 (LABEL_EXPR, void_type_node, end_label);
2830 append_to_statement_list (t, &expr);
2833 return expr;
2836 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2838 tree
2839 gimple_boolify (tree expr)
2841 tree type = TREE_TYPE (expr);
2842 location_t loc = EXPR_LOCATION (expr);
2844 if (TREE_CODE (expr) == NE_EXPR
2845 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2846 && integer_zerop (TREE_OPERAND (expr, 1)))
2848 tree call = TREE_OPERAND (expr, 0);
2849 tree fn = get_callee_fndecl (call);
2851 /* For __builtin_expect ((long) (x), y) recurse into x as well
2852 if x is truth_value_p. */
2853 if (fn
2854 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2855 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2856 && call_expr_nargs (call) == 2)
2858 tree arg = CALL_EXPR_ARG (call, 0);
2859 if (arg)
2861 if (TREE_CODE (arg) == NOP_EXPR
2862 && TREE_TYPE (arg) == TREE_TYPE (call))
2863 arg = TREE_OPERAND (arg, 0);
2864 if (truth_value_p (TREE_CODE (arg)))
2866 arg = gimple_boolify (arg);
2867 CALL_EXPR_ARG (call, 0)
2868 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2874 switch (TREE_CODE (expr))
2876 case TRUTH_AND_EXPR:
2877 case TRUTH_OR_EXPR:
2878 case TRUTH_XOR_EXPR:
2879 case TRUTH_ANDIF_EXPR:
2880 case TRUTH_ORIF_EXPR:
2881 /* Also boolify the arguments of truth exprs. */
2882 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2883 /* FALLTHRU */
2885 case TRUTH_NOT_EXPR:
2886 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2888 /* These expressions always produce boolean results. */
2889 if (TREE_CODE (type) != BOOLEAN_TYPE)
2890 TREE_TYPE (expr) = boolean_type_node;
2891 return expr;
2893 case ANNOTATE_EXPR:
2894 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2896 case annot_expr_ivdep_kind:
2897 case annot_expr_no_vector_kind:
2898 case annot_expr_vector_kind:
2899 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2900 if (TREE_CODE (type) != BOOLEAN_TYPE)
2901 TREE_TYPE (expr) = boolean_type_node;
2902 return expr;
2903 default:
2904 gcc_unreachable ();
2907 default:
2908 if (COMPARISON_CLASS_P (expr))
2910 /* There expressions always prduce boolean results. */
2911 if (TREE_CODE (type) != BOOLEAN_TYPE)
2912 TREE_TYPE (expr) = boolean_type_node;
2913 return expr;
2915 /* Other expressions that get here must have boolean values, but
2916 might need to be converted to the appropriate mode. */
2917 if (TREE_CODE (type) == BOOLEAN_TYPE)
2918 return expr;
2919 return fold_convert_loc (loc, boolean_type_node, expr);
2923 /* Given a conditional expression *EXPR_P without side effects, gimplify
2924 its operands. New statements are inserted to PRE_P. */
2926 static enum gimplify_status
2927 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2929 tree expr = *expr_p, cond;
2930 enum gimplify_status ret, tret;
2931 enum tree_code code;
2933 cond = gimple_boolify (COND_EXPR_COND (expr));
2935 /* We need to handle && and || specially, as their gimplification
2936 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2937 code = TREE_CODE (cond);
2938 if (code == TRUTH_ANDIF_EXPR)
2939 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2940 else if (code == TRUTH_ORIF_EXPR)
2941 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2942 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2943 COND_EXPR_COND (*expr_p) = cond;
2945 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2946 is_gimple_val, fb_rvalue);
2947 ret = MIN (ret, tret);
2948 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2949 is_gimple_val, fb_rvalue);
2951 return MIN (ret, tret);
2954 /* Return true if evaluating EXPR could trap.
2955 EXPR is GENERIC, while tree_could_trap_p can be called
2956 only on GIMPLE. */
2958 static bool
2959 generic_expr_could_trap_p (tree expr)
2961 unsigned i, n;
2963 if (!expr || is_gimple_val (expr))
2964 return false;
2966 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2967 return true;
2969 n = TREE_OPERAND_LENGTH (expr);
2970 for (i = 0; i < n; i++)
2971 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2972 return true;
2974 return false;
2977 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2978 into
2980 if (p) if (p)
2981 t1 = a; a;
2982 else or else
2983 t1 = b; b;
2986 The second form is used when *EXPR_P is of type void.
2988 PRE_P points to the list where side effects that must happen before
2989 *EXPR_P should be stored. */
2991 static enum gimplify_status
2992 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2994 tree expr = *expr_p;
2995 tree type = TREE_TYPE (expr);
2996 location_t loc = EXPR_LOCATION (expr);
2997 tree tmp, arm1, arm2;
2998 enum gimplify_status ret;
2999 tree label_true, label_false, label_cont;
3000 bool have_then_clause_p, have_else_clause_p;
3001 gcond *cond_stmt;
3002 enum tree_code pred_code;
3003 gimple_seq seq = NULL;
3005 /* If this COND_EXPR has a value, copy the values into a temporary within
3006 the arms. */
3007 if (!VOID_TYPE_P (type))
3009 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3010 tree result;
3012 /* If either an rvalue is ok or we do not require an lvalue, create the
3013 temporary. But we cannot do that if the type is addressable. */
3014 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3015 && !TREE_ADDRESSABLE (type))
3017 if (gimplify_ctxp->allow_rhs_cond_expr
3018 /* If either branch has side effects or could trap, it can't be
3019 evaluated unconditionally. */
3020 && !TREE_SIDE_EFFECTS (then_)
3021 && !generic_expr_could_trap_p (then_)
3022 && !TREE_SIDE_EFFECTS (else_)
3023 && !generic_expr_could_trap_p (else_))
3024 return gimplify_pure_cond_expr (expr_p, pre_p);
3026 tmp = create_tmp_var (type, "iftmp");
3027 result = tmp;
3030 /* Otherwise, only create and copy references to the values. */
3031 else
3033 type = build_pointer_type (type);
3035 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3036 then_ = build_fold_addr_expr_loc (loc, then_);
3038 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3039 else_ = build_fold_addr_expr_loc (loc, else_);
3041 expr
3042 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3044 tmp = create_tmp_var (type, "iftmp");
3045 result = build_simple_mem_ref_loc (loc, tmp);
3048 /* Build the new then clause, `tmp = then_;'. But don't build the
3049 assignment if the value is void; in C++ it can be if it's a throw. */
3050 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3051 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3053 /* Similarly, build the new else clause, `tmp = else_;'. */
3054 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3055 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3057 TREE_TYPE (expr) = void_type_node;
3058 recalculate_side_effects (expr);
3060 /* Move the COND_EXPR to the prequeue. */
3061 gimplify_stmt (&expr, pre_p);
3063 *expr_p = result;
3064 return GS_ALL_DONE;
3067 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3068 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3069 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3070 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3072 /* Make sure the condition has BOOLEAN_TYPE. */
3073 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3075 /* Break apart && and || conditions. */
3076 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3077 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3079 expr = shortcut_cond_expr (expr);
3081 if (expr != *expr_p)
3083 *expr_p = expr;
3085 /* We can't rely on gimplify_expr to re-gimplify the expanded
3086 form properly, as cleanups might cause the target labels to be
3087 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3088 set up a conditional context. */
3089 gimple_push_condition ();
3090 gimplify_stmt (expr_p, &seq);
3091 gimple_pop_condition (pre_p);
3092 gimple_seq_add_seq (pre_p, seq);
3094 return GS_ALL_DONE;
3098 /* Now do the normal gimplification. */
3100 /* Gimplify condition. */
3101 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3102 fb_rvalue);
3103 if (ret == GS_ERROR)
3104 return GS_ERROR;
3105 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3107 gimple_push_condition ();
3109 have_then_clause_p = have_else_clause_p = false;
3110 if (TREE_OPERAND (expr, 1) != NULL
3111 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3112 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3113 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3114 == current_function_decl)
3115 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3116 have different locations, otherwise we end up with incorrect
3117 location information on the branches. */
3118 && (optimize
3119 || !EXPR_HAS_LOCATION (expr)
3120 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3121 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3123 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3124 have_then_clause_p = true;
3126 else
3127 label_true = create_artificial_label (UNKNOWN_LOCATION);
3128 if (TREE_OPERAND (expr, 2) != NULL
3129 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3130 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3131 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3132 == current_function_decl)
3133 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3134 have different locations, otherwise we end up with incorrect
3135 location information on the branches. */
3136 && (optimize
3137 || !EXPR_HAS_LOCATION (expr)
3138 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3139 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3141 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3142 have_else_clause_p = true;
3144 else
3145 label_false = create_artificial_label (UNKNOWN_LOCATION);
3147 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3148 &arm2);
3149 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3150 label_false);
3151 gimplify_seq_add_stmt (&seq, cond_stmt);
3152 gimple_stmt_iterator gsi = gsi_last (seq);
3153 maybe_fold_stmt (&gsi);
3155 label_cont = NULL_TREE;
3156 if (!have_then_clause_p)
3158 /* For if (...) {} else { code; } put label_true after
3159 the else block. */
3160 if (TREE_OPERAND (expr, 1) == NULL_TREE
3161 && !have_else_clause_p
3162 && TREE_OPERAND (expr, 2) != NULL_TREE)
3163 label_cont = label_true;
3164 else
3166 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3167 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3168 /* For if (...) { code; } else {} or
3169 if (...) { code; } else goto label; or
3170 if (...) { code; return; } else { ... }
3171 label_cont isn't needed. */
3172 if (!have_else_clause_p
3173 && TREE_OPERAND (expr, 2) != NULL_TREE
3174 && gimple_seq_may_fallthru (seq))
3176 gimple *g;
3177 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3179 g = gimple_build_goto (label_cont);
3181 /* GIMPLE_COND's are very low level; they have embedded
3182 gotos. This particular embedded goto should not be marked
3183 with the location of the original COND_EXPR, as it would
3184 correspond to the COND_EXPR's condition, not the ELSE or the
3185 THEN arms. To avoid marking it with the wrong location, flag
3186 it as "no location". */
3187 gimple_set_do_not_emit_location (g);
3189 gimplify_seq_add_stmt (&seq, g);
3193 if (!have_else_clause_p)
3195 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3196 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3198 if (label_cont)
3199 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3201 gimple_pop_condition (pre_p);
3202 gimple_seq_add_seq (pre_p, seq);
3204 if (ret == GS_ERROR)
3205 ; /* Do nothing. */
3206 else if (have_then_clause_p || have_else_clause_p)
3207 ret = GS_ALL_DONE;
3208 else
3210 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3211 expr = TREE_OPERAND (expr, 0);
3212 gimplify_stmt (&expr, pre_p);
3215 *expr_p = NULL;
3216 return ret;
3219 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3220 to be marked addressable.
3222 We cannot rely on such an expression being directly markable if a temporary
3223 has been created by the gimplification. In this case, we create another
3224 temporary and initialize it with a copy, which will become a store after we
3225 mark it addressable. This can happen if the front-end passed us something
3226 that it could not mark addressable yet, like a Fortran pass-by-reference
3227 parameter (int) floatvar. */
3229 static void
3230 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3232 while (handled_component_p (*expr_p))
3233 expr_p = &TREE_OPERAND (*expr_p, 0);
3234 if (is_gimple_reg (*expr_p))
3236 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3237 DECL_GIMPLE_REG_P (var) = 0;
3238 *expr_p = var;
3242 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3243 a call to __builtin_memcpy. */
3245 static enum gimplify_status
3246 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3247 gimple_seq *seq_p)
3249 tree t, to, to_ptr, from, from_ptr;
3250 gcall *gs;
3251 location_t loc = EXPR_LOCATION (*expr_p);
3253 to = TREE_OPERAND (*expr_p, 0);
3254 from = TREE_OPERAND (*expr_p, 1);
3256 /* Mark the RHS addressable. Beware that it may not be possible to do so
3257 directly if a temporary has been created by the gimplification. */
3258 prepare_gimple_addressable (&from, seq_p);
3260 mark_addressable (from);
3261 from_ptr = build_fold_addr_expr_loc (loc, from);
3262 gimplify_arg (&from_ptr, seq_p, loc);
3264 mark_addressable (to);
3265 to_ptr = build_fold_addr_expr_loc (loc, to);
3266 gimplify_arg (&to_ptr, seq_p, loc);
3268 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3270 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3272 if (want_value)
3274 /* tmp = memcpy() */
3275 t = create_tmp_var (TREE_TYPE (to_ptr));
3276 gimple_call_set_lhs (gs, t);
3277 gimplify_seq_add_stmt (seq_p, gs);
3279 *expr_p = build_simple_mem_ref (t);
3280 return GS_ALL_DONE;
3283 gimplify_seq_add_stmt (seq_p, gs);
3284 *expr_p = NULL;
3285 return GS_ALL_DONE;
3288 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3289 a call to __builtin_memset. In this case we know that the RHS is
3290 a CONSTRUCTOR with an empty element list. */
3292 static enum gimplify_status
3293 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3294 gimple_seq *seq_p)
3296 tree t, from, to, to_ptr;
3297 gcall *gs;
3298 location_t loc = EXPR_LOCATION (*expr_p);
3300 /* Assert our assumptions, to abort instead of producing wrong code
3301 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3302 not be immediately exposed. */
3303 from = TREE_OPERAND (*expr_p, 1);
3304 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3305 from = TREE_OPERAND (from, 0);
3307 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3308 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3310 /* Now proceed. */
3311 to = TREE_OPERAND (*expr_p, 0);
3313 to_ptr = build_fold_addr_expr_loc (loc, to);
3314 gimplify_arg (&to_ptr, seq_p, loc);
3315 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3317 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3319 if (want_value)
3321 /* tmp = memset() */
3322 t = create_tmp_var (TREE_TYPE (to_ptr));
3323 gimple_call_set_lhs (gs, t);
3324 gimplify_seq_add_stmt (seq_p, gs);
3326 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3327 return GS_ALL_DONE;
3330 gimplify_seq_add_stmt (seq_p, gs);
3331 *expr_p = NULL;
3332 return GS_ALL_DONE;
3335 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3336 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3337 assignment. Return non-null if we detect a potential overlap. */
3339 struct gimplify_init_ctor_preeval_data
3341 /* The base decl of the lhs object. May be NULL, in which case we
3342 have to assume the lhs is indirect. */
3343 tree lhs_base_decl;
3345 /* The alias set of the lhs object. */
3346 alias_set_type lhs_alias_set;
3349 static tree
3350 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3352 struct gimplify_init_ctor_preeval_data *data
3353 = (struct gimplify_init_ctor_preeval_data *) xdata;
3354 tree t = *tp;
3356 /* If we find the base object, obviously we have overlap. */
3357 if (data->lhs_base_decl == t)
3358 return t;
3360 /* If the constructor component is indirect, determine if we have a
3361 potential overlap with the lhs. The only bits of information we
3362 have to go on at this point are addressability and alias sets. */
3363 if ((INDIRECT_REF_P (t)
3364 || TREE_CODE (t) == MEM_REF)
3365 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3366 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3367 return t;
3369 /* If the constructor component is a call, determine if it can hide a
3370 potential overlap with the lhs through an INDIRECT_REF like above.
3371 ??? Ugh - this is completely broken. In fact this whole analysis
3372 doesn't look conservative. */
3373 if (TREE_CODE (t) == CALL_EXPR)
3375 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3377 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3378 if (POINTER_TYPE_P (TREE_VALUE (type))
3379 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3380 && alias_sets_conflict_p (data->lhs_alias_set,
3381 get_alias_set
3382 (TREE_TYPE (TREE_VALUE (type)))))
3383 return t;
3386 if (IS_TYPE_OR_DECL_P (t))
3387 *walk_subtrees = 0;
3388 return NULL;
3391 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3392 force values that overlap with the lhs (as described by *DATA)
3393 into temporaries. */
3395 static void
3396 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3397 struct gimplify_init_ctor_preeval_data *data)
3399 enum gimplify_status one;
3401 /* If the value is constant, then there's nothing to pre-evaluate. */
3402 if (TREE_CONSTANT (*expr_p))
3404 /* Ensure it does not have side effects, it might contain a reference to
3405 the object we're initializing. */
3406 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3407 return;
3410 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3411 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3412 return;
3414 /* Recurse for nested constructors. */
3415 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3417 unsigned HOST_WIDE_INT ix;
3418 constructor_elt *ce;
3419 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3421 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3422 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3424 return;
3427 /* If this is a variable sized type, we must remember the size. */
3428 maybe_with_size_expr (expr_p);
3430 /* Gimplify the constructor element to something appropriate for the rhs
3431 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3432 the gimplifier will consider this a store to memory. Doing this
3433 gimplification now means that we won't have to deal with complicated
3434 language-specific trees, nor trees like SAVE_EXPR that can induce
3435 exponential search behavior. */
3436 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3437 if (one == GS_ERROR)
3439 *expr_p = NULL;
3440 return;
3443 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3444 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3445 always be true for all scalars, since is_gimple_mem_rhs insists on a
3446 temporary variable for them. */
3447 if (DECL_P (*expr_p))
3448 return;
3450 /* If this is of variable size, we have no choice but to assume it doesn't
3451 overlap since we can't make a temporary for it. */
3452 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3453 return;
3455 /* Otherwise, we must search for overlap ... */
3456 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3457 return;
3459 /* ... and if found, force the value into a temporary. */
3460 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3463 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3464 a RANGE_EXPR in a CONSTRUCTOR for an array.
3466 var = lower;
3467 loop_entry:
3468 object[var] = value;
3469 if (var == upper)
3470 goto loop_exit;
3471 var = var + 1;
3472 goto loop_entry;
3473 loop_exit:
3475 We increment var _after_ the loop exit check because we might otherwise
3476 fail if upper == TYPE_MAX_VALUE (type for upper).
3478 Note that we never have to deal with SAVE_EXPRs here, because this has
3479 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3481 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3482 gimple_seq *, bool);
3484 static void
3485 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3486 tree value, tree array_elt_type,
3487 gimple_seq *pre_p, bool cleared)
3489 tree loop_entry_label, loop_exit_label, fall_thru_label;
3490 tree var, var_type, cref, tmp;
3492 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3493 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3494 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3496 /* Create and initialize the index variable. */
3497 var_type = TREE_TYPE (upper);
3498 var = create_tmp_var (var_type);
3499 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3501 /* Add the loop entry label. */
3502 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3504 /* Build the reference. */
3505 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3506 var, NULL_TREE, NULL_TREE);
3508 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3509 the store. Otherwise just assign value to the reference. */
3511 if (TREE_CODE (value) == CONSTRUCTOR)
3512 /* NB we might have to call ourself recursively through
3513 gimplify_init_ctor_eval if the value is a constructor. */
3514 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3515 pre_p, cleared);
3516 else
3517 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3519 /* We exit the loop when the index var is equal to the upper bound. */
3520 gimplify_seq_add_stmt (pre_p,
3521 gimple_build_cond (EQ_EXPR, var, upper,
3522 loop_exit_label, fall_thru_label));
3524 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3526 /* Otherwise, increment the index var... */
3527 tmp = build2 (PLUS_EXPR, var_type, var,
3528 fold_convert (var_type, integer_one_node));
3529 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3531 /* ...and jump back to the loop entry. */
3532 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3534 /* Add the loop exit label. */
3535 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3538 /* Return true if FDECL is accessing a field that is zero sized. */
3540 static bool
3541 zero_sized_field_decl (const_tree fdecl)
3543 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3544 && integer_zerop (DECL_SIZE (fdecl)))
3545 return true;
3546 return false;
3549 /* Return true if TYPE is zero sized. */
3551 static bool
3552 zero_sized_type (const_tree type)
3554 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3555 && integer_zerop (TYPE_SIZE (type)))
3556 return true;
3557 return false;
3560 /* A subroutine of gimplify_init_constructor. Generate individual
3561 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3562 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3563 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3564 zeroed first. */
3566 static void
3567 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3568 gimple_seq *pre_p, bool cleared)
3570 tree array_elt_type = NULL;
3571 unsigned HOST_WIDE_INT ix;
3572 tree purpose, value;
3574 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3575 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3577 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3579 tree cref;
3581 /* NULL values are created above for gimplification errors. */
3582 if (value == NULL)
3583 continue;
3585 if (cleared && initializer_zerop (value))
3586 continue;
3588 /* ??? Here's to hoping the front end fills in all of the indices,
3589 so we don't have to figure out what's missing ourselves. */
3590 gcc_assert (purpose);
3592 /* Skip zero-sized fields, unless value has side-effects. This can
3593 happen with calls to functions returning a zero-sized type, which
3594 we shouldn't discard. As a number of downstream passes don't
3595 expect sets of zero-sized fields, we rely on the gimplification of
3596 the MODIFY_EXPR we make below to drop the assignment statement. */
3597 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3598 continue;
3600 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3601 whole range. */
3602 if (TREE_CODE (purpose) == RANGE_EXPR)
3604 tree lower = TREE_OPERAND (purpose, 0);
3605 tree upper = TREE_OPERAND (purpose, 1);
3607 /* If the lower bound is equal to upper, just treat it as if
3608 upper was the index. */
3609 if (simple_cst_equal (lower, upper))
3610 purpose = upper;
3611 else
3613 gimplify_init_ctor_eval_range (object, lower, upper, value,
3614 array_elt_type, pre_p, cleared);
3615 continue;
3619 if (array_elt_type)
3621 /* Do not use bitsizetype for ARRAY_REF indices. */
3622 if (TYPE_DOMAIN (TREE_TYPE (object)))
3623 purpose
3624 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3625 purpose);
3626 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3627 purpose, NULL_TREE, NULL_TREE);
3629 else
3631 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3632 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3633 unshare_expr (object), purpose, NULL_TREE);
3636 if (TREE_CODE (value) == CONSTRUCTOR
3637 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3638 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3639 pre_p, cleared);
3640 else
3642 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3643 gimplify_and_add (init, pre_p);
3644 ggc_free (init);
3649 /* Return the appropriate RHS predicate for this LHS. */
3651 gimple_predicate
3652 rhs_predicate_for (tree lhs)
3654 if (is_gimple_reg (lhs))
3655 return is_gimple_reg_rhs_or_call;
3656 else
3657 return is_gimple_mem_rhs_or_call;
3660 /* Gimplify a C99 compound literal expression. This just means adding
3661 the DECL_EXPR before the current statement and using its anonymous
3662 decl instead. */
3664 static enum gimplify_status
3665 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3666 bool (*gimple_test_f) (tree),
3667 fallback_t fallback)
3669 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3670 tree decl = DECL_EXPR_DECL (decl_s);
3671 tree init = DECL_INITIAL (decl);
3672 /* Mark the decl as addressable if the compound literal
3673 expression is addressable now, otherwise it is marked too late
3674 after we gimplify the initialization expression. */
3675 if (TREE_ADDRESSABLE (*expr_p))
3676 TREE_ADDRESSABLE (decl) = 1;
3677 /* Otherwise, if we don't need an lvalue and have a literal directly
3678 substitute it. Check if it matches the gimple predicate, as
3679 otherwise we'd generate a new temporary, and we can as well just
3680 use the decl we already have. */
3681 else if (!TREE_ADDRESSABLE (decl)
3682 && init
3683 && (fallback & fb_lvalue) == 0
3684 && gimple_test_f (init))
3686 *expr_p = init;
3687 return GS_OK;
3690 /* Preliminarily mark non-addressed complex variables as eligible
3691 for promotion to gimple registers. We'll transform their uses
3692 as we find them. */
3693 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3694 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3695 && !TREE_THIS_VOLATILE (decl)
3696 && !needs_to_live_in_memory (decl))
3697 DECL_GIMPLE_REG_P (decl) = 1;
3699 /* If the decl is not addressable, then it is being used in some
3700 expression or on the right hand side of a statement, and it can
3701 be put into a readonly data section. */
3702 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3703 TREE_READONLY (decl) = 1;
3705 /* This decl isn't mentioned in the enclosing block, so add it to the
3706 list of temps. FIXME it seems a bit of a kludge to say that
3707 anonymous artificial vars aren't pushed, but everything else is. */
3708 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3709 gimple_add_tmp_var (decl);
3711 gimplify_and_add (decl_s, pre_p);
3712 *expr_p = decl;
3713 return GS_OK;
3716 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3717 return a new CONSTRUCTOR if something changed. */
3719 static tree
3720 optimize_compound_literals_in_ctor (tree orig_ctor)
3722 tree ctor = orig_ctor;
3723 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3724 unsigned int idx, num = vec_safe_length (elts);
3726 for (idx = 0; idx < num; idx++)
3728 tree value = (*elts)[idx].value;
3729 tree newval = value;
3730 if (TREE_CODE (value) == CONSTRUCTOR)
3731 newval = optimize_compound_literals_in_ctor (value);
3732 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3734 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3735 tree decl = DECL_EXPR_DECL (decl_s);
3736 tree init = DECL_INITIAL (decl);
3738 if (!TREE_ADDRESSABLE (value)
3739 && !TREE_ADDRESSABLE (decl)
3740 && init
3741 && TREE_CODE (init) == CONSTRUCTOR)
3742 newval = optimize_compound_literals_in_ctor (init);
3744 if (newval == value)
3745 continue;
3747 if (ctor == orig_ctor)
3749 ctor = copy_node (orig_ctor);
3750 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3751 elts = CONSTRUCTOR_ELTS (ctor);
3753 (*elts)[idx].value = newval;
3755 return ctor;
3758 /* A subroutine of gimplify_modify_expr. Break out elements of a
3759 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3761 Note that we still need to clear any elements that don't have explicit
3762 initializers, so if not all elements are initialized we keep the
3763 original MODIFY_EXPR, we just remove all of the constructor elements.
3765 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3766 GS_ERROR if we would have to create a temporary when gimplifying
3767 this constructor. Otherwise, return GS_OK.
3769 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3771 static enum gimplify_status
3772 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3773 bool want_value, bool notify_temp_creation)
3775 tree object, ctor, type;
3776 enum gimplify_status ret;
3777 vec<constructor_elt, va_gc> *elts;
3779 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3781 if (!notify_temp_creation)
3783 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3784 is_gimple_lvalue, fb_lvalue);
3785 if (ret == GS_ERROR)
3786 return ret;
3789 object = TREE_OPERAND (*expr_p, 0);
3790 ctor = TREE_OPERAND (*expr_p, 1) =
3791 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3792 type = TREE_TYPE (ctor);
3793 elts = CONSTRUCTOR_ELTS (ctor);
3794 ret = GS_ALL_DONE;
3796 switch (TREE_CODE (type))
3798 case RECORD_TYPE:
3799 case UNION_TYPE:
3800 case QUAL_UNION_TYPE:
3801 case ARRAY_TYPE:
3803 struct gimplify_init_ctor_preeval_data preeval_data;
3804 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3805 bool cleared, complete_p, valid_const_initializer;
3807 /* Aggregate types must lower constructors to initialization of
3808 individual elements. The exception is that a CONSTRUCTOR node
3809 with no elements indicates zero-initialization of the whole. */
3810 if (vec_safe_is_empty (elts))
3812 if (notify_temp_creation)
3813 return GS_OK;
3814 break;
3817 /* Fetch information about the constructor to direct later processing.
3818 We might want to make static versions of it in various cases, and
3819 can only do so if it known to be a valid constant initializer. */
3820 valid_const_initializer
3821 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3822 &num_ctor_elements, &complete_p);
3824 /* If a const aggregate variable is being initialized, then it
3825 should never be a lose to promote the variable to be static. */
3826 if (valid_const_initializer
3827 && num_nonzero_elements > 1
3828 && TREE_READONLY (object)
3829 && TREE_CODE (object) == VAR_DECL
3830 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3832 if (notify_temp_creation)
3833 return GS_ERROR;
3834 DECL_INITIAL (object) = ctor;
3835 TREE_STATIC (object) = 1;
3836 if (!DECL_NAME (object))
3837 DECL_NAME (object) = create_tmp_var_name ("C");
3838 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3840 /* ??? C++ doesn't automatically append a .<number> to the
3841 assembler name, and even when it does, it looks at FE private
3842 data structures to figure out what that number should be,
3843 which are not set for this variable. I suppose this is
3844 important for local statics for inline functions, which aren't
3845 "local" in the object file sense. So in order to get a unique
3846 TU-local symbol, we must invoke the lhd version now. */
3847 lhd_set_decl_assembler_name (object);
3849 *expr_p = NULL_TREE;
3850 break;
3853 /* If there are "lots" of initialized elements, even discounting
3854 those that are not address constants (and thus *must* be
3855 computed at runtime), then partition the constructor into
3856 constant and non-constant parts. Block copy the constant
3857 parts in, then generate code for the non-constant parts. */
3858 /* TODO. There's code in cp/typeck.c to do this. */
3860 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3861 /* store_constructor will ignore the clearing of variable-sized
3862 objects. Initializers for such objects must explicitly set
3863 every field that needs to be set. */
3864 cleared = false;
3865 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3866 /* If the constructor isn't complete, clear the whole object
3867 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3869 ??? This ought not to be needed. For any element not present
3870 in the initializer, we should simply set them to zero. Except
3871 we'd need to *find* the elements that are not present, and that
3872 requires trickery to avoid quadratic compile-time behavior in
3873 large cases or excessive memory use in small cases. */
3874 cleared = true;
3875 else if (num_ctor_elements - num_nonzero_elements
3876 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3877 && num_nonzero_elements < num_ctor_elements / 4)
3878 /* If there are "lots" of zeros, it's more efficient to clear
3879 the memory and then set the nonzero elements. */
3880 cleared = true;
3881 else
3882 cleared = false;
3884 /* If there are "lots" of initialized elements, and all of them
3885 are valid address constants, then the entire initializer can
3886 be dropped to memory, and then memcpy'd out. Don't do this
3887 for sparse arrays, though, as it's more efficient to follow
3888 the standard CONSTRUCTOR behavior of memset followed by
3889 individual element initialization. Also don't do this for small
3890 all-zero initializers (which aren't big enough to merit
3891 clearing), and don't try to make bitwise copies of
3892 TREE_ADDRESSABLE types.
3894 We cannot apply such transformation when compiling chkp static
3895 initializer because creation of initializer image in the memory
3896 will require static initialization of bounds for it. It should
3897 result in another gimplification of similar initializer and we
3898 may fall into infinite loop. */
3899 if (valid_const_initializer
3900 && !(cleared || num_nonzero_elements == 0)
3901 && !TREE_ADDRESSABLE (type)
3902 && (!current_function_decl
3903 || !lookup_attribute ("chkp ctor",
3904 DECL_ATTRIBUTES (current_function_decl))))
3906 HOST_WIDE_INT size = int_size_in_bytes (type);
3907 unsigned int align;
3909 /* ??? We can still get unbounded array types, at least
3910 from the C++ front end. This seems wrong, but attempt
3911 to work around it for now. */
3912 if (size < 0)
3914 size = int_size_in_bytes (TREE_TYPE (object));
3915 if (size >= 0)
3916 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3919 /* Find the maximum alignment we can assume for the object. */
3920 /* ??? Make use of DECL_OFFSET_ALIGN. */
3921 if (DECL_P (object))
3922 align = DECL_ALIGN (object);
3923 else
3924 align = TYPE_ALIGN (type);
3926 /* Do a block move either if the size is so small as to make
3927 each individual move a sub-unit move on average, or if it
3928 is so large as to make individual moves inefficient. */
3929 if (size > 0
3930 && num_nonzero_elements > 1
3931 && (size < num_nonzero_elements
3932 || !can_move_by_pieces (size, align)))
3934 if (notify_temp_creation)
3935 return GS_ERROR;
3937 walk_tree (&ctor, force_labels_r, NULL, NULL);
3938 ctor = tree_output_constant_def (ctor);
3939 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3940 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3941 TREE_OPERAND (*expr_p, 1) = ctor;
3943 /* This is no longer an assignment of a CONSTRUCTOR, but
3944 we still may have processing to do on the LHS. So
3945 pretend we didn't do anything here to let that happen. */
3946 return GS_UNHANDLED;
3950 /* If the target is volatile, we have non-zero elements and more than
3951 one field to assign, initialize the target from a temporary. */
3952 if (TREE_THIS_VOLATILE (object)
3953 && !TREE_ADDRESSABLE (type)
3954 && num_nonzero_elements > 0
3955 && vec_safe_length (elts) > 1)
3957 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3958 TREE_OPERAND (*expr_p, 0) = temp;
3959 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3960 *expr_p,
3961 build2 (MODIFY_EXPR, void_type_node,
3962 object, temp));
3963 return GS_OK;
3966 if (notify_temp_creation)
3967 return GS_OK;
3969 /* If there are nonzero elements and if needed, pre-evaluate to capture
3970 elements overlapping with the lhs into temporaries. We must do this
3971 before clearing to fetch the values before they are zeroed-out. */
3972 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3974 preeval_data.lhs_base_decl = get_base_address (object);
3975 if (!DECL_P (preeval_data.lhs_base_decl))
3976 preeval_data.lhs_base_decl = NULL;
3977 preeval_data.lhs_alias_set = get_alias_set (object);
3979 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3980 pre_p, post_p, &preeval_data);
3983 bool ctor_has_side_effects_p
3984 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
3986 if (cleared)
3988 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3989 Note that we still have to gimplify, in order to handle the
3990 case of variable sized types. Avoid shared tree structures. */
3991 CONSTRUCTOR_ELTS (ctor) = NULL;
3992 TREE_SIDE_EFFECTS (ctor) = 0;
3993 object = unshare_expr (object);
3994 gimplify_stmt (expr_p, pre_p);
3997 /* If we have not block cleared the object, or if there are nonzero
3998 elements in the constructor, or if the constructor has side effects,
3999 add assignments to the individual scalar fields of the object. */
4000 if (!cleared
4001 || num_nonzero_elements > 0
4002 || ctor_has_side_effects_p)
4003 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4005 *expr_p = NULL_TREE;
4007 break;
4009 case COMPLEX_TYPE:
4011 tree r, i;
4013 if (notify_temp_creation)
4014 return GS_OK;
4016 /* Extract the real and imaginary parts out of the ctor. */
4017 gcc_assert (elts->length () == 2);
4018 r = (*elts)[0].value;
4019 i = (*elts)[1].value;
4020 if (r == NULL || i == NULL)
4022 tree zero = build_zero_cst (TREE_TYPE (type));
4023 if (r == NULL)
4024 r = zero;
4025 if (i == NULL)
4026 i = zero;
4029 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4030 represent creation of a complex value. */
4031 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4033 ctor = build_complex (type, r, i);
4034 TREE_OPERAND (*expr_p, 1) = ctor;
4036 else
4038 ctor = build2 (COMPLEX_EXPR, type, r, i);
4039 TREE_OPERAND (*expr_p, 1) = ctor;
4040 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4041 pre_p,
4042 post_p,
4043 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4044 fb_rvalue);
4047 break;
4049 case VECTOR_TYPE:
4051 unsigned HOST_WIDE_INT ix;
4052 constructor_elt *ce;
4054 if (notify_temp_creation)
4055 return GS_OK;
4057 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4058 if (TREE_CONSTANT (ctor))
4060 bool constant_p = true;
4061 tree value;
4063 /* Even when ctor is constant, it might contain non-*_CST
4064 elements, such as addresses or trapping values like
4065 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4066 in VECTOR_CST nodes. */
4067 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4068 if (!CONSTANT_CLASS_P (value))
4070 constant_p = false;
4071 break;
4074 if (constant_p)
4076 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4077 break;
4080 TREE_CONSTANT (ctor) = 0;
4083 /* Vector types use CONSTRUCTOR all the way through gimple
4084 compilation as a general initializer. */
4085 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4087 enum gimplify_status tret;
4088 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4089 fb_rvalue);
4090 if (tret == GS_ERROR)
4091 ret = GS_ERROR;
4093 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4094 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4096 break;
4098 default:
4099 /* So how did we get a CONSTRUCTOR for a scalar type? */
4100 gcc_unreachable ();
4103 if (ret == GS_ERROR)
4104 return GS_ERROR;
4105 else if (want_value)
4107 *expr_p = object;
4108 return GS_OK;
4110 else
4112 /* If we have gimplified both sides of the initializer but have
4113 not emitted an assignment, do so now. */
4114 if (*expr_p)
4116 tree lhs = TREE_OPERAND (*expr_p, 0);
4117 tree rhs = TREE_OPERAND (*expr_p, 1);
4118 gassign *init = gimple_build_assign (lhs, rhs);
4119 gimplify_seq_add_stmt (pre_p, init);
4120 *expr_p = NULL;
4123 return GS_ALL_DONE;
4127 /* Given a pointer value OP0, return a simplified version of an
4128 indirection through OP0, or NULL_TREE if no simplification is
4129 possible. This may only be applied to a rhs of an expression.
4130 Note that the resulting type may be different from the type pointed
4131 to in the sense that it is still compatible from the langhooks
4132 point of view. */
4134 static tree
4135 gimple_fold_indirect_ref_rhs (tree t)
4137 return gimple_fold_indirect_ref (t);
4140 /* Subroutine of gimplify_modify_expr to do simplifications of
4141 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4142 something changes. */
4144 static enum gimplify_status
4145 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4146 gimple_seq *pre_p, gimple_seq *post_p,
4147 bool want_value)
4149 enum gimplify_status ret = GS_UNHANDLED;
4150 bool changed;
4154 changed = false;
4155 switch (TREE_CODE (*from_p))
4157 case VAR_DECL:
4158 /* If we're assigning from a read-only variable initialized with
4159 a constructor, do the direct assignment from the constructor,
4160 but only if neither source nor target are volatile since this
4161 latter assignment might end up being done on a per-field basis. */
4162 if (DECL_INITIAL (*from_p)
4163 && TREE_READONLY (*from_p)
4164 && !TREE_THIS_VOLATILE (*from_p)
4165 && !TREE_THIS_VOLATILE (*to_p)
4166 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4168 tree old_from = *from_p;
4169 enum gimplify_status subret;
4171 /* Move the constructor into the RHS. */
4172 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4174 /* Let's see if gimplify_init_constructor will need to put
4175 it in memory. */
4176 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4177 false, true);
4178 if (subret == GS_ERROR)
4180 /* If so, revert the change. */
4181 *from_p = old_from;
4183 else
4185 ret = GS_OK;
4186 changed = true;
4189 break;
4190 case INDIRECT_REF:
4192 /* If we have code like
4194 *(const A*)(A*)&x
4196 where the type of "x" is a (possibly cv-qualified variant
4197 of "A"), treat the entire expression as identical to "x".
4198 This kind of code arises in C++ when an object is bound
4199 to a const reference, and if "x" is a TARGET_EXPR we want
4200 to take advantage of the optimization below. */
4201 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4202 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4203 if (t)
4205 if (TREE_THIS_VOLATILE (t) != volatile_p)
4207 if (DECL_P (t))
4208 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4209 build_fold_addr_expr (t));
4210 if (REFERENCE_CLASS_P (t))
4211 TREE_THIS_VOLATILE (t) = volatile_p;
4213 *from_p = t;
4214 ret = GS_OK;
4215 changed = true;
4217 break;
4220 case TARGET_EXPR:
4222 /* If we are initializing something from a TARGET_EXPR, strip the
4223 TARGET_EXPR and initialize it directly, if possible. This can't
4224 be done if the initializer is void, since that implies that the
4225 temporary is set in some non-trivial way.
4227 ??? What about code that pulls out the temp and uses it
4228 elsewhere? I think that such code never uses the TARGET_EXPR as
4229 an initializer. If I'm wrong, we'll die because the temp won't
4230 have any RTL. In that case, I guess we'll need to replace
4231 references somehow. */
4232 tree init = TARGET_EXPR_INITIAL (*from_p);
4234 if (init
4235 && !VOID_TYPE_P (TREE_TYPE (init)))
4237 *from_p = init;
4238 ret = GS_OK;
4239 changed = true;
4242 break;
4244 case COMPOUND_EXPR:
4245 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4246 caught. */
4247 gimplify_compound_expr (from_p, pre_p, true);
4248 ret = GS_OK;
4249 changed = true;
4250 break;
4252 case CONSTRUCTOR:
4253 /* If we already made some changes, let the front end have a
4254 crack at this before we break it down. */
4255 if (ret != GS_UNHANDLED)
4256 break;
4257 /* If we're initializing from a CONSTRUCTOR, break this into
4258 individual MODIFY_EXPRs. */
4259 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4260 false);
4262 case COND_EXPR:
4263 /* If we're assigning to a non-register type, push the assignment
4264 down into the branches. This is mandatory for ADDRESSABLE types,
4265 since we cannot generate temporaries for such, but it saves a
4266 copy in other cases as well. */
4267 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4269 /* This code should mirror the code in gimplify_cond_expr. */
4270 enum tree_code code = TREE_CODE (*expr_p);
4271 tree cond = *from_p;
4272 tree result = *to_p;
4274 ret = gimplify_expr (&result, pre_p, post_p,
4275 is_gimple_lvalue, fb_lvalue);
4276 if (ret != GS_ERROR)
4277 ret = GS_OK;
4279 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4280 TREE_OPERAND (cond, 1)
4281 = build2 (code, void_type_node, result,
4282 TREE_OPERAND (cond, 1));
4283 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4284 TREE_OPERAND (cond, 2)
4285 = build2 (code, void_type_node, unshare_expr (result),
4286 TREE_OPERAND (cond, 2));
4288 TREE_TYPE (cond) = void_type_node;
4289 recalculate_side_effects (cond);
4291 if (want_value)
4293 gimplify_and_add (cond, pre_p);
4294 *expr_p = unshare_expr (result);
4296 else
4297 *expr_p = cond;
4298 return ret;
4300 break;
4302 case CALL_EXPR:
4303 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4304 return slot so that we don't generate a temporary. */
4305 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4306 && aggregate_value_p (*from_p, *from_p))
4308 bool use_target;
4310 if (!(rhs_predicate_for (*to_p))(*from_p))
4311 /* If we need a temporary, *to_p isn't accurate. */
4312 use_target = false;
4313 /* It's OK to use the return slot directly unless it's an NRV. */
4314 else if (TREE_CODE (*to_p) == RESULT_DECL
4315 && DECL_NAME (*to_p) == NULL_TREE
4316 && needs_to_live_in_memory (*to_p))
4317 use_target = true;
4318 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4319 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4320 /* Don't force regs into memory. */
4321 use_target = false;
4322 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4323 /* It's OK to use the target directly if it's being
4324 initialized. */
4325 use_target = true;
4326 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4327 != INTEGER_CST)
4328 /* Always use the target and thus RSO for variable-sized types.
4329 GIMPLE cannot deal with a variable-sized assignment
4330 embedded in a call statement. */
4331 use_target = true;
4332 else if (TREE_CODE (*to_p) != SSA_NAME
4333 && (!is_gimple_variable (*to_p)
4334 || needs_to_live_in_memory (*to_p)))
4335 /* Don't use the original target if it's already addressable;
4336 if its address escapes, and the called function uses the
4337 NRV optimization, a conforming program could see *to_p
4338 change before the called function returns; see c++/19317.
4339 When optimizing, the return_slot pass marks more functions
4340 as safe after we have escape info. */
4341 use_target = false;
4342 else
4343 use_target = true;
4345 if (use_target)
4347 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4348 mark_addressable (*to_p);
4351 break;
4353 case WITH_SIZE_EXPR:
4354 /* Likewise for calls that return an aggregate of non-constant size,
4355 since we would not be able to generate a temporary at all. */
4356 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4358 *from_p = TREE_OPERAND (*from_p, 0);
4359 /* We don't change ret in this case because the
4360 WITH_SIZE_EXPR might have been added in
4361 gimplify_modify_expr, so returning GS_OK would lead to an
4362 infinite loop. */
4363 changed = true;
4365 break;
4367 /* If we're initializing from a container, push the initialization
4368 inside it. */
4369 case CLEANUP_POINT_EXPR:
4370 case BIND_EXPR:
4371 case STATEMENT_LIST:
4373 tree wrap = *from_p;
4374 tree t;
4376 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4377 fb_lvalue);
4378 if (ret != GS_ERROR)
4379 ret = GS_OK;
4381 t = voidify_wrapper_expr (wrap, *expr_p);
4382 gcc_assert (t == *expr_p);
4384 if (want_value)
4386 gimplify_and_add (wrap, pre_p);
4387 *expr_p = unshare_expr (*to_p);
4389 else
4390 *expr_p = wrap;
4391 return GS_OK;
4394 case COMPOUND_LITERAL_EXPR:
4396 tree complit = TREE_OPERAND (*expr_p, 1);
4397 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4398 tree decl = DECL_EXPR_DECL (decl_s);
4399 tree init = DECL_INITIAL (decl);
4401 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4402 into struct T x = { 0, 1, 2 } if the address of the
4403 compound literal has never been taken. */
4404 if (!TREE_ADDRESSABLE (complit)
4405 && !TREE_ADDRESSABLE (decl)
4406 && init)
4408 *expr_p = copy_node (*expr_p);
4409 TREE_OPERAND (*expr_p, 1) = init;
4410 return GS_OK;
4414 default:
4415 break;
4418 while (changed);
4420 return ret;
4424 /* Return true if T looks like a valid GIMPLE statement. */
4426 static bool
4427 is_gimple_stmt (tree t)
4429 const enum tree_code code = TREE_CODE (t);
4431 switch (code)
4433 case NOP_EXPR:
4434 /* The only valid NOP_EXPR is the empty statement. */
4435 return IS_EMPTY_STMT (t);
4437 case BIND_EXPR:
4438 case COND_EXPR:
4439 /* These are only valid if they're void. */
4440 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4442 case SWITCH_EXPR:
4443 case GOTO_EXPR:
4444 case RETURN_EXPR:
4445 case LABEL_EXPR:
4446 case CASE_LABEL_EXPR:
4447 case TRY_CATCH_EXPR:
4448 case TRY_FINALLY_EXPR:
4449 case EH_FILTER_EXPR:
4450 case CATCH_EXPR:
4451 case ASM_EXPR:
4452 case STATEMENT_LIST:
4453 case OACC_PARALLEL:
4454 case OACC_KERNELS:
4455 case OACC_DATA:
4456 case OACC_HOST_DATA:
4457 case OACC_DECLARE:
4458 case OACC_UPDATE:
4459 case OACC_ENTER_DATA:
4460 case OACC_EXIT_DATA:
4461 case OACC_CACHE:
4462 case OMP_PARALLEL:
4463 case OMP_FOR:
4464 case OMP_SIMD:
4465 case CILK_SIMD:
4466 case OMP_DISTRIBUTE:
4467 case OACC_LOOP:
4468 case OMP_SECTIONS:
4469 case OMP_SECTION:
4470 case OMP_SINGLE:
4471 case OMP_MASTER:
4472 case OMP_TASKGROUP:
4473 case OMP_ORDERED:
4474 case OMP_CRITICAL:
4475 case OMP_TASK:
4476 case OMP_TARGET:
4477 case OMP_TARGET_DATA:
4478 case OMP_TARGET_UPDATE:
4479 case OMP_TARGET_ENTER_DATA:
4480 case OMP_TARGET_EXIT_DATA:
4481 case OMP_TASKLOOP:
4482 case OMP_TEAMS:
4483 /* These are always void. */
4484 return true;
4486 case CALL_EXPR:
4487 case MODIFY_EXPR:
4488 case PREDICT_EXPR:
4489 /* These are valid regardless of their type. */
4490 return true;
4492 default:
4493 return false;
4498 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4499 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4500 DECL_GIMPLE_REG_P set.
4502 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4503 other, unmodified part of the complex object just before the total store.
4504 As a consequence, if the object is still uninitialized, an undefined value
4505 will be loaded into a register, which may result in a spurious exception
4506 if the register is floating-point and the value happens to be a signaling
4507 NaN for example. Then the fully-fledged complex operations lowering pass
4508 followed by a DCE pass are necessary in order to fix things up. */
4510 static enum gimplify_status
4511 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4512 bool want_value)
4514 enum tree_code code, ocode;
4515 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4517 lhs = TREE_OPERAND (*expr_p, 0);
4518 rhs = TREE_OPERAND (*expr_p, 1);
4519 code = TREE_CODE (lhs);
4520 lhs = TREE_OPERAND (lhs, 0);
4522 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4523 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4524 TREE_NO_WARNING (other) = 1;
4525 other = get_formal_tmp_var (other, pre_p);
4527 realpart = code == REALPART_EXPR ? rhs : other;
4528 imagpart = code == REALPART_EXPR ? other : rhs;
4530 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4531 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4532 else
4533 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4535 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4536 *expr_p = (want_value) ? rhs : NULL_TREE;
4538 return GS_ALL_DONE;
4541 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4543 modify_expr
4544 : varname '=' rhs
4545 | '*' ID '=' rhs
4547 PRE_P points to the list where side effects that must happen before
4548 *EXPR_P should be stored.
4550 POST_P points to the list where side effects that must happen after
4551 *EXPR_P should be stored.
4553 WANT_VALUE is nonzero iff we want to use the value of this expression
4554 in another expression. */
4556 static enum gimplify_status
4557 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4558 bool want_value)
4560 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4561 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4562 enum gimplify_status ret = GS_UNHANDLED;
4563 gimple *assign;
4564 location_t loc = EXPR_LOCATION (*expr_p);
4565 gimple_stmt_iterator gsi;
4567 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4568 || TREE_CODE (*expr_p) == INIT_EXPR);
4570 /* Trying to simplify a clobber using normal logic doesn't work,
4571 so handle it here. */
4572 if (TREE_CLOBBER_P (*from_p))
4574 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4575 if (ret == GS_ERROR)
4576 return ret;
4577 gcc_assert (!want_value
4578 && (TREE_CODE (*to_p) == VAR_DECL
4579 || TREE_CODE (*to_p) == MEM_REF));
4580 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4581 *expr_p = NULL;
4582 return GS_ALL_DONE;
4585 /* Insert pointer conversions required by the middle-end that are not
4586 required by the frontend. This fixes middle-end type checking for
4587 for example gcc.dg/redecl-6.c. */
4588 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4590 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4591 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4592 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4595 /* See if any simplifications can be done based on what the RHS is. */
4596 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4597 want_value);
4598 if (ret != GS_UNHANDLED)
4599 return ret;
4601 /* For zero sized types only gimplify the left hand side and right hand
4602 side as statements and throw away the assignment. Do this after
4603 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4604 types properly. */
4605 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4607 gimplify_stmt (from_p, pre_p);
4608 gimplify_stmt (to_p, pre_p);
4609 *expr_p = NULL_TREE;
4610 return GS_ALL_DONE;
4613 /* If the value being copied is of variable width, compute the length
4614 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4615 before gimplifying any of the operands so that we can resolve any
4616 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4617 the size of the expression to be copied, not of the destination, so
4618 that is what we must do here. */
4619 maybe_with_size_expr (from_p);
4621 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4622 if (ret == GS_ERROR)
4623 return ret;
4625 /* As a special case, we have to temporarily allow for assignments
4626 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4627 a toplevel statement, when gimplifying the GENERIC expression
4628 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4629 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4631 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4632 prevent gimplify_expr from trying to create a new temporary for
4633 foo's LHS, we tell it that it should only gimplify until it
4634 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4635 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4636 and all we need to do here is set 'a' to be its LHS. */
4637 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4638 fb_rvalue);
4639 if (ret == GS_ERROR)
4640 return ret;
4642 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4643 size as argument to the call. */
4644 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4646 tree call = TREE_OPERAND (*from_p, 0);
4647 tree vlasize = TREE_OPERAND (*from_p, 1);
4649 if (TREE_CODE (call) == CALL_EXPR
4650 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
4652 int nargs = call_expr_nargs (call);
4653 tree type = TREE_TYPE (call);
4654 tree ap = CALL_EXPR_ARG (call, 0);
4655 tree tag = CALL_EXPR_ARG (call, 1);
4656 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
4657 IFN_VA_ARG, type,
4658 nargs + 1, ap, tag,
4659 vlasize);
4660 tree *call_p = &(TREE_OPERAND (*from_p, 0));
4661 *call_p = newcall;
4665 /* Now see if the above changed *from_p to something we handle specially. */
4666 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4667 want_value);
4668 if (ret != GS_UNHANDLED)
4669 return ret;
4671 /* If we've got a variable sized assignment between two lvalues (i.e. does
4672 not involve a call), then we can make things a bit more straightforward
4673 by converting the assignment to memcpy or memset. */
4674 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4676 tree from = TREE_OPERAND (*from_p, 0);
4677 tree size = TREE_OPERAND (*from_p, 1);
4679 if (TREE_CODE (from) == CONSTRUCTOR)
4680 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4682 if (is_gimple_addressable (from))
4684 *from_p = from;
4685 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4686 pre_p);
4690 /* Transform partial stores to non-addressable complex variables into
4691 total stores. This allows us to use real instead of virtual operands
4692 for these variables, which improves optimization. */
4693 if ((TREE_CODE (*to_p) == REALPART_EXPR
4694 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4695 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4696 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4698 /* Try to alleviate the effects of the gimplification creating artificial
4699 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4700 make sure not to create DECL_DEBUG_EXPR links across functions. */
4701 if (!gimplify_ctxp->into_ssa
4702 && TREE_CODE (*from_p) == VAR_DECL
4703 && DECL_IGNORED_P (*from_p)
4704 && DECL_P (*to_p)
4705 && !DECL_IGNORED_P (*to_p)
4706 && decl_function_context (*to_p) == current_function_decl)
4708 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4709 DECL_NAME (*from_p)
4710 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4711 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4712 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4715 if (want_value && TREE_THIS_VOLATILE (*to_p))
4716 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4718 if (TREE_CODE (*from_p) == CALL_EXPR)
4720 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4721 instead of a GIMPLE_ASSIGN. */
4722 gcall *call_stmt;
4723 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4725 /* Gimplify internal functions created in the FEs. */
4726 int nargs = call_expr_nargs (*from_p), i;
4727 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4728 auto_vec<tree> vargs (nargs);
4730 for (i = 0; i < nargs; i++)
4732 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4733 EXPR_LOCATION (*from_p));
4734 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4736 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4737 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4739 else
4741 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4742 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4743 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4744 tree fndecl = get_callee_fndecl (*from_p);
4745 if (fndecl
4746 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4747 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4748 && call_expr_nargs (*from_p) == 3)
4749 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4750 CALL_EXPR_ARG (*from_p, 0),
4751 CALL_EXPR_ARG (*from_p, 1),
4752 CALL_EXPR_ARG (*from_p, 2));
4753 else
4755 call_stmt = gimple_build_call_from_tree (*from_p);
4756 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4759 notice_special_calls (call_stmt);
4760 if (!gimple_call_noreturn_p (call_stmt))
4761 gimple_call_set_lhs (call_stmt, *to_p);
4762 assign = call_stmt;
4764 else
4766 assign = gimple_build_assign (*to_p, *from_p);
4767 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4770 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4772 /* We should have got an SSA name from the start. */
4773 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4776 gimplify_seq_add_stmt (pre_p, assign);
4777 gsi = gsi_last (*pre_p);
4778 maybe_fold_stmt (&gsi);
4780 if (want_value)
4782 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4783 return GS_OK;
4785 else
4786 *expr_p = NULL;
4788 return GS_ALL_DONE;
4791 /* Gimplify a comparison between two variable-sized objects. Do this
4792 with a call to BUILT_IN_MEMCMP. */
4794 static enum gimplify_status
4795 gimplify_variable_sized_compare (tree *expr_p)
4797 location_t loc = EXPR_LOCATION (*expr_p);
4798 tree op0 = TREE_OPERAND (*expr_p, 0);
4799 tree op1 = TREE_OPERAND (*expr_p, 1);
4800 tree t, arg, dest, src, expr;
4802 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4803 arg = unshare_expr (arg);
4804 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4805 src = build_fold_addr_expr_loc (loc, op1);
4806 dest = build_fold_addr_expr_loc (loc, op0);
4807 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4808 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4810 expr
4811 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4812 SET_EXPR_LOCATION (expr, loc);
4813 *expr_p = expr;
4815 return GS_OK;
4818 /* Gimplify a comparison between two aggregate objects of integral scalar
4819 mode as a comparison between the bitwise equivalent scalar values. */
4821 static enum gimplify_status
4822 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4824 location_t loc = EXPR_LOCATION (*expr_p);
4825 tree op0 = TREE_OPERAND (*expr_p, 0);
4826 tree op1 = TREE_OPERAND (*expr_p, 1);
4828 tree type = TREE_TYPE (op0);
4829 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4831 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4832 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4834 *expr_p
4835 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4837 return GS_OK;
4840 /* Gimplify an expression sequence. This function gimplifies each
4841 expression and rewrites the original expression with the last
4842 expression of the sequence in GIMPLE form.
4844 PRE_P points to the list where the side effects for all the
4845 expressions in the sequence will be emitted.
4847 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4849 static enum gimplify_status
4850 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4852 tree t = *expr_p;
4856 tree *sub_p = &TREE_OPERAND (t, 0);
4858 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4859 gimplify_compound_expr (sub_p, pre_p, false);
4860 else
4861 gimplify_stmt (sub_p, pre_p);
4863 t = TREE_OPERAND (t, 1);
4865 while (TREE_CODE (t) == COMPOUND_EXPR);
4867 *expr_p = t;
4868 if (want_value)
4869 return GS_OK;
4870 else
4872 gimplify_stmt (expr_p, pre_p);
4873 return GS_ALL_DONE;
4877 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4878 gimplify. After gimplification, EXPR_P will point to a new temporary
4879 that holds the original value of the SAVE_EXPR node.
4881 PRE_P points to the list where side effects that must happen before
4882 *EXPR_P should be stored. */
4884 static enum gimplify_status
4885 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4887 enum gimplify_status ret = GS_ALL_DONE;
4888 tree val;
4890 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4891 val = TREE_OPERAND (*expr_p, 0);
4893 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4894 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4896 /* The operand may be a void-valued expression such as SAVE_EXPRs
4897 generated by the Java frontend for class initialization. It is
4898 being executed only for its side-effects. */
4899 if (TREE_TYPE (val) == void_type_node)
4901 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4902 is_gimple_stmt, fb_none);
4903 val = NULL;
4905 else
4906 val = get_initialized_tmp_var (val, pre_p, post_p);
4908 TREE_OPERAND (*expr_p, 0) = val;
4909 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4912 *expr_p = val;
4914 return ret;
4917 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4919 unary_expr
4920 : ...
4921 | '&' varname
4924 PRE_P points to the list where side effects that must happen before
4925 *EXPR_P should be stored.
4927 POST_P points to the list where side effects that must happen after
4928 *EXPR_P should be stored. */
4930 static enum gimplify_status
4931 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4933 tree expr = *expr_p;
4934 tree op0 = TREE_OPERAND (expr, 0);
4935 enum gimplify_status ret;
4936 location_t loc = EXPR_LOCATION (*expr_p);
4938 switch (TREE_CODE (op0))
4940 case INDIRECT_REF:
4941 do_indirect_ref:
4942 /* Check if we are dealing with an expression of the form '&*ptr'.
4943 While the front end folds away '&*ptr' into 'ptr', these
4944 expressions may be generated internally by the compiler (e.g.,
4945 builtins like __builtin_va_end). */
4946 /* Caution: the silent array decomposition semantics we allow for
4947 ADDR_EXPR means we can't always discard the pair. */
4948 /* Gimplification of the ADDR_EXPR operand may drop
4949 cv-qualification conversions, so make sure we add them if
4950 needed. */
4952 tree op00 = TREE_OPERAND (op0, 0);
4953 tree t_expr = TREE_TYPE (expr);
4954 tree t_op00 = TREE_TYPE (op00);
4956 if (!useless_type_conversion_p (t_expr, t_op00))
4957 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4958 *expr_p = op00;
4959 ret = GS_OK;
4961 break;
4963 case VIEW_CONVERT_EXPR:
4964 /* Take the address of our operand and then convert it to the type of
4965 this ADDR_EXPR.
4967 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4968 all clear. The impact of this transformation is even less clear. */
4970 /* If the operand is a useless conversion, look through it. Doing so
4971 guarantees that the ADDR_EXPR and its operand will remain of the
4972 same type. */
4973 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4974 op0 = TREE_OPERAND (op0, 0);
4976 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4977 build_fold_addr_expr_loc (loc,
4978 TREE_OPERAND (op0, 0)));
4979 ret = GS_OK;
4980 break;
4982 case MEM_REF:
4983 if (integer_zerop (TREE_OPERAND (op0, 1)))
4984 goto do_indirect_ref;
4986 /* ... fall through ... */
4988 default:
4989 /* If we see a call to a declared builtin or see its address
4990 being taken (we can unify those cases here) then we can mark
4991 the builtin for implicit generation by GCC. */
4992 if (TREE_CODE (op0) == FUNCTION_DECL
4993 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
4994 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
4995 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
4997 /* We use fb_either here because the C frontend sometimes takes
4998 the address of a call that returns a struct; see
4999 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5000 the implied temporary explicit. */
5002 /* Make the operand addressable. */
5003 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5004 is_gimple_addressable, fb_either);
5005 if (ret == GS_ERROR)
5006 break;
5008 /* Then mark it. Beware that it may not be possible to do so directly
5009 if a temporary has been created by the gimplification. */
5010 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5012 op0 = TREE_OPERAND (expr, 0);
5014 /* For various reasons, the gimplification of the expression
5015 may have made a new INDIRECT_REF. */
5016 if (TREE_CODE (op0) == INDIRECT_REF)
5017 goto do_indirect_ref;
5019 mark_addressable (TREE_OPERAND (expr, 0));
5021 /* The FEs may end up building ADDR_EXPRs early on a decl with
5022 an incomplete type. Re-build ADDR_EXPRs in canonical form
5023 here. */
5024 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5025 *expr_p = build_fold_addr_expr (op0);
5027 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5028 recompute_tree_invariant_for_addr_expr (*expr_p);
5030 /* If we re-built the ADDR_EXPR add a conversion to the original type
5031 if required. */
5032 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5033 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5035 break;
5038 return ret;
5041 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5042 value; output operands should be a gimple lvalue. */
5044 static enum gimplify_status
5045 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5047 tree expr;
5048 int noutputs;
5049 const char **oconstraints;
5050 int i;
5051 tree link;
5052 const char *constraint;
5053 bool allows_mem, allows_reg, is_inout;
5054 enum gimplify_status ret, tret;
5055 gasm *stmt;
5056 vec<tree, va_gc> *inputs;
5057 vec<tree, va_gc> *outputs;
5058 vec<tree, va_gc> *clobbers;
5059 vec<tree, va_gc> *labels;
5060 tree link_next;
5062 expr = *expr_p;
5063 noutputs = list_length (ASM_OUTPUTS (expr));
5064 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5066 inputs = NULL;
5067 outputs = NULL;
5068 clobbers = NULL;
5069 labels = NULL;
5071 ret = GS_ALL_DONE;
5072 link_next = NULL_TREE;
5073 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5075 bool ok;
5076 size_t constraint_len;
5078 link_next = TREE_CHAIN (link);
5080 oconstraints[i]
5081 = constraint
5082 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5083 constraint_len = strlen (constraint);
5084 if (constraint_len == 0)
5085 continue;
5087 ok = parse_output_constraint (&constraint, i, 0, 0,
5088 &allows_mem, &allows_reg, &is_inout);
5089 if (!ok)
5091 ret = GS_ERROR;
5092 is_inout = false;
5095 if (!allows_reg && allows_mem)
5096 mark_addressable (TREE_VALUE (link));
5098 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5099 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5100 fb_lvalue | fb_mayfail);
5101 if (tret == GS_ERROR)
5103 error ("invalid lvalue in asm output %d", i);
5104 ret = tret;
5107 vec_safe_push (outputs, link);
5108 TREE_CHAIN (link) = NULL_TREE;
5110 if (is_inout)
5112 /* An input/output operand. To give the optimizers more
5113 flexibility, split it into separate input and output
5114 operands. */
5115 tree input;
5116 char buf[10];
5118 /* Turn the in/out constraint into an output constraint. */
5119 char *p = xstrdup (constraint);
5120 p[0] = '=';
5121 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5123 /* And add a matching input constraint. */
5124 if (allows_reg)
5126 sprintf (buf, "%d", i);
5128 /* If there are multiple alternatives in the constraint,
5129 handle each of them individually. Those that allow register
5130 will be replaced with operand number, the others will stay
5131 unchanged. */
5132 if (strchr (p, ',') != NULL)
5134 size_t len = 0, buflen = strlen (buf);
5135 char *beg, *end, *str, *dst;
5137 for (beg = p + 1;;)
5139 end = strchr (beg, ',');
5140 if (end == NULL)
5141 end = strchr (beg, '\0');
5142 if ((size_t) (end - beg) < buflen)
5143 len += buflen + 1;
5144 else
5145 len += end - beg + 1;
5146 if (*end)
5147 beg = end + 1;
5148 else
5149 break;
5152 str = (char *) alloca (len);
5153 for (beg = p + 1, dst = str;;)
5155 const char *tem;
5156 bool mem_p, reg_p, inout_p;
5158 end = strchr (beg, ',');
5159 if (end)
5160 *end = '\0';
5161 beg[-1] = '=';
5162 tem = beg - 1;
5163 parse_output_constraint (&tem, i, 0, 0,
5164 &mem_p, &reg_p, &inout_p);
5165 if (dst != str)
5166 *dst++ = ',';
5167 if (reg_p)
5169 memcpy (dst, buf, buflen);
5170 dst += buflen;
5172 else
5174 if (end)
5175 len = end - beg;
5176 else
5177 len = strlen (beg);
5178 memcpy (dst, beg, len);
5179 dst += len;
5181 if (end)
5182 beg = end + 1;
5183 else
5184 break;
5186 *dst = '\0';
5187 input = build_string (dst - str, str);
5189 else
5190 input = build_string (strlen (buf), buf);
5192 else
5193 input = build_string (constraint_len - 1, constraint + 1);
5195 free (p);
5197 input = build_tree_list (build_tree_list (NULL_TREE, input),
5198 unshare_expr (TREE_VALUE (link)));
5199 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5203 link_next = NULL_TREE;
5204 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5206 link_next = TREE_CHAIN (link);
5207 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5208 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5209 oconstraints, &allows_mem, &allows_reg);
5211 /* If we can't make copies, we can only accept memory. */
5212 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5214 if (allows_mem)
5215 allows_reg = 0;
5216 else
5218 error ("impossible constraint in %<asm%>");
5219 error ("non-memory input %d must stay in memory", i);
5220 return GS_ERROR;
5224 /* If the operand is a memory input, it should be an lvalue. */
5225 if (!allows_reg && allows_mem)
5227 tree inputv = TREE_VALUE (link);
5228 STRIP_NOPS (inputv);
5229 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5230 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5231 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5232 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
5233 || TREE_CODE (inputv) == MODIFY_EXPR)
5234 TREE_VALUE (link) = error_mark_node;
5235 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5236 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5237 mark_addressable (TREE_VALUE (link));
5238 if (tret == GS_ERROR)
5240 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5241 input_location = EXPR_LOCATION (TREE_VALUE (link));
5242 error ("memory input %d is not directly addressable", i);
5243 ret = tret;
5246 else
5248 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5249 is_gimple_asm_val, fb_rvalue);
5250 if (tret == GS_ERROR)
5251 ret = tret;
5254 TREE_CHAIN (link) = NULL_TREE;
5255 vec_safe_push (inputs, link);
5258 link_next = NULL_TREE;
5259 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5261 link_next = TREE_CHAIN (link);
5262 TREE_CHAIN (link) = NULL_TREE;
5263 vec_safe_push (clobbers, link);
5266 link_next = NULL_TREE;
5267 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5269 link_next = TREE_CHAIN (link);
5270 TREE_CHAIN (link) = NULL_TREE;
5271 vec_safe_push (labels, link);
5274 /* Do not add ASMs with errors to the gimple IL stream. */
5275 if (ret != GS_ERROR)
5277 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5278 inputs, outputs, clobbers, labels);
5280 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5281 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5283 gimplify_seq_add_stmt (pre_p, stmt);
5286 return ret;
5289 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5290 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5291 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5292 return to this function.
5294 FIXME should we complexify the prequeue handling instead? Or use flags
5295 for all the cleanups and let the optimizer tighten them up? The current
5296 code seems pretty fragile; it will break on a cleanup within any
5297 non-conditional nesting. But any such nesting would be broken, anyway;
5298 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5299 and continues out of it. We can do that at the RTL level, though, so
5300 having an optimizer to tighten up try/finally regions would be a Good
5301 Thing. */
5303 static enum gimplify_status
5304 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5306 gimple_stmt_iterator iter;
5307 gimple_seq body_sequence = NULL;
5309 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5311 /* We only care about the number of conditions between the innermost
5312 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5313 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5314 int old_conds = gimplify_ctxp->conditions;
5315 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5316 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5317 gimplify_ctxp->conditions = 0;
5318 gimplify_ctxp->conditional_cleanups = NULL;
5319 gimplify_ctxp->in_cleanup_point_expr = true;
5321 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5323 gimplify_ctxp->conditions = old_conds;
5324 gimplify_ctxp->conditional_cleanups = old_cleanups;
5325 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5327 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5329 gimple *wce = gsi_stmt (iter);
5331 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5333 if (gsi_one_before_end_p (iter))
5335 /* Note that gsi_insert_seq_before and gsi_remove do not
5336 scan operands, unlike some other sequence mutators. */
5337 if (!gimple_wce_cleanup_eh_only (wce))
5338 gsi_insert_seq_before_without_update (&iter,
5339 gimple_wce_cleanup (wce),
5340 GSI_SAME_STMT);
5341 gsi_remove (&iter, true);
5342 break;
5344 else
5346 gtry *gtry;
5347 gimple_seq seq;
5348 enum gimple_try_flags kind;
5350 if (gimple_wce_cleanup_eh_only (wce))
5351 kind = GIMPLE_TRY_CATCH;
5352 else
5353 kind = GIMPLE_TRY_FINALLY;
5354 seq = gsi_split_seq_after (iter);
5356 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5357 /* Do not use gsi_replace here, as it may scan operands.
5358 We want to do a simple structural modification only. */
5359 gsi_set_stmt (&iter, gtry);
5360 iter = gsi_start (gtry->eval);
5363 else
5364 gsi_next (&iter);
5367 gimplify_seq_add_seq (pre_p, body_sequence);
5368 if (temp)
5370 *expr_p = temp;
5371 return GS_OK;
5373 else
5375 *expr_p = NULL;
5376 return GS_ALL_DONE;
5380 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5381 is the cleanup action required. EH_ONLY is true if the cleanup should
5382 only be executed if an exception is thrown, not on normal exit. */
5384 static void
5385 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5387 gimple *wce;
5388 gimple_seq cleanup_stmts = NULL;
5390 /* Errors can result in improperly nested cleanups. Which results in
5391 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5392 if (seen_error ())
5393 return;
5395 if (gimple_conditional_context ())
5397 /* If we're in a conditional context, this is more complex. We only
5398 want to run the cleanup if we actually ran the initialization that
5399 necessitates it, but we want to run it after the end of the
5400 conditional context. So we wrap the try/finally around the
5401 condition and use a flag to determine whether or not to actually
5402 run the destructor. Thus
5404 test ? f(A()) : 0
5406 becomes (approximately)
5408 flag = 0;
5409 try {
5410 if (test) { A::A(temp); flag = 1; val = f(temp); }
5411 else { val = 0; }
5412 } finally {
5413 if (flag) A::~A(temp);
5417 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5418 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5419 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5421 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5422 gimplify_stmt (&cleanup, &cleanup_stmts);
5423 wce = gimple_build_wce (cleanup_stmts);
5425 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5426 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5427 gimplify_seq_add_stmt (pre_p, ftrue);
5429 /* Because of this manipulation, and the EH edges that jump
5430 threading cannot redirect, the temporary (VAR) will appear
5431 to be used uninitialized. Don't warn. */
5432 TREE_NO_WARNING (var) = 1;
5434 else
5436 gimplify_stmt (&cleanup, &cleanup_stmts);
5437 wce = gimple_build_wce (cleanup_stmts);
5438 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5439 gimplify_seq_add_stmt (pre_p, wce);
5443 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5445 static enum gimplify_status
5446 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5448 tree targ = *expr_p;
5449 tree temp = TARGET_EXPR_SLOT (targ);
5450 tree init = TARGET_EXPR_INITIAL (targ);
5451 enum gimplify_status ret;
5453 if (init)
5455 tree cleanup = NULL_TREE;
5457 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5458 to the temps list. Handle also variable length TARGET_EXPRs. */
5459 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5461 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5462 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5463 gimplify_vla_decl (temp, pre_p);
5465 else
5466 gimple_add_tmp_var (temp);
5468 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5469 expression is supposed to initialize the slot. */
5470 if (VOID_TYPE_P (TREE_TYPE (init)))
5471 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5472 else
5474 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5475 init = init_expr;
5476 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5477 init = NULL;
5478 ggc_free (init_expr);
5480 if (ret == GS_ERROR)
5482 /* PR c++/28266 Make sure this is expanded only once. */
5483 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5484 return GS_ERROR;
5486 if (init)
5487 gimplify_and_add (init, pre_p);
5489 /* If needed, push the cleanup for the temp. */
5490 if (TARGET_EXPR_CLEANUP (targ))
5492 if (CLEANUP_EH_ONLY (targ))
5493 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5494 CLEANUP_EH_ONLY (targ), pre_p);
5495 else
5496 cleanup = TARGET_EXPR_CLEANUP (targ);
5499 /* Add a clobber for the temporary going out of scope, like
5500 gimplify_bind_expr. */
5501 if (gimplify_ctxp->in_cleanup_point_expr
5502 && needs_to_live_in_memory (temp)
5503 && flag_stack_reuse == SR_ALL)
5505 tree clobber = build_constructor (TREE_TYPE (temp),
5506 NULL);
5507 TREE_THIS_VOLATILE (clobber) = true;
5508 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5509 if (cleanup)
5510 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5511 clobber);
5512 else
5513 cleanup = clobber;
5516 if (cleanup)
5517 gimple_push_cleanup (temp, cleanup, false, pre_p);
5519 /* Only expand this once. */
5520 TREE_OPERAND (targ, 3) = init;
5521 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5523 else
5524 /* We should have expanded this before. */
5525 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5527 *expr_p = temp;
5528 return GS_OK;
5531 /* Gimplification of expression trees. */
5533 /* Gimplify an expression which appears at statement context. The
5534 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5535 NULL, a new sequence is allocated.
5537 Return true if we actually added a statement to the queue. */
5539 bool
5540 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5542 gimple_seq_node last;
5544 last = gimple_seq_last (*seq_p);
5545 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5546 return last != gimple_seq_last (*seq_p);
5549 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5550 to CTX. If entries already exist, force them to be some flavor of private.
5551 If there is no enclosing parallel, do nothing. */
5553 void
5554 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5556 splay_tree_node n;
5558 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
5559 return;
5563 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5564 if (n != NULL)
5566 if (n->value & GOVD_SHARED)
5567 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5568 else if (n->value & GOVD_MAP)
5569 n->value |= GOVD_MAP_TO_ONLY;
5570 else
5571 return;
5573 else if ((ctx->region_type & ORT_TARGET) != 0)
5575 if (ctx->target_map_scalars_firstprivate)
5576 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5577 else
5578 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5580 else if (ctx->region_type != ORT_WORKSHARE
5581 && ctx->region_type != ORT_SIMD
5582 && ctx->region_type != ORT_TARGET_DATA)
5583 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5585 ctx = ctx->outer_context;
5587 while (ctx);
5590 /* Similarly for each of the type sizes of TYPE. */
5592 static void
5593 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5595 if (type == NULL || type == error_mark_node)
5596 return;
5597 type = TYPE_MAIN_VARIANT (type);
5599 if (ctx->privatized_types->add (type))
5600 return;
5602 switch (TREE_CODE (type))
5604 case INTEGER_TYPE:
5605 case ENUMERAL_TYPE:
5606 case BOOLEAN_TYPE:
5607 case REAL_TYPE:
5608 case FIXED_POINT_TYPE:
5609 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5610 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5611 break;
5613 case ARRAY_TYPE:
5614 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5615 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5616 break;
5618 case RECORD_TYPE:
5619 case UNION_TYPE:
5620 case QUAL_UNION_TYPE:
5622 tree field;
5623 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5624 if (TREE_CODE (field) == FIELD_DECL)
5626 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5627 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5630 break;
5632 case POINTER_TYPE:
5633 case REFERENCE_TYPE:
5634 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5635 break;
5637 default:
5638 break;
5641 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5642 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5643 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5646 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5648 static void
5649 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5651 splay_tree_node n;
5652 unsigned int nflags;
5653 tree t;
5655 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
5656 return;
5658 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5659 there are constructors involved somewhere. */
5660 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5661 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5662 flags |= GOVD_SEEN;
5664 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5665 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5667 /* We shouldn't be re-adding the decl with the same data
5668 sharing class. */
5669 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5670 /* The only combination of data sharing classes we should see is
5671 FIRSTPRIVATE and LASTPRIVATE. */
5672 nflags = n->value | flags;
5673 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5674 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5675 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5676 n->value = nflags;
5677 return;
5680 /* When adding a variable-sized variable, we have to handle all sorts
5681 of additional bits of data: the pointer replacement variable, and
5682 the parameters of the type. */
5683 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5685 /* Add the pointer replacement variable as PRIVATE if the variable
5686 replacement is private, else FIRSTPRIVATE since we'll need the
5687 address of the original variable either for SHARED, or for the
5688 copy into or out of the context. */
5689 if (!(flags & GOVD_LOCAL))
5691 if (flags & GOVD_MAP)
5692 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5693 else if (flags & GOVD_PRIVATE)
5694 nflags = GOVD_PRIVATE;
5695 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
5696 && (flags & GOVD_FIRSTPRIVATE))
5697 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
5698 else
5699 nflags = GOVD_FIRSTPRIVATE;
5700 nflags |= flags & GOVD_SEEN;
5701 t = DECL_VALUE_EXPR (decl);
5702 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5703 t = TREE_OPERAND (t, 0);
5704 gcc_assert (DECL_P (t));
5705 omp_add_variable (ctx, t, nflags);
5708 /* Add all of the variable and type parameters (which should have
5709 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5710 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5711 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5712 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5714 /* The variable-sized variable itself is never SHARED, only some form
5715 of PRIVATE. The sharing would take place via the pointer variable
5716 which we remapped above. */
5717 if (flags & GOVD_SHARED)
5718 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5719 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5721 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5722 alloca statement we generate for the variable, so make sure it
5723 is available. This isn't automatically needed for the SHARED
5724 case, since we won't be allocating local storage then.
5725 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5726 in this case omp_notice_variable will be called later
5727 on when it is gimplified. */
5728 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5729 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5730 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5732 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5733 && lang_hooks.decls.omp_privatize_by_reference (decl))
5735 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5737 /* Similar to the direct variable sized case above, we'll need the
5738 size of references being privatized. */
5739 if ((flags & GOVD_SHARED) == 0)
5741 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5742 if (DECL_P (t))
5743 omp_notice_variable (ctx, t, true);
5747 if (n != NULL)
5748 n->value |= flags;
5749 else
5750 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5753 /* Notice a threadprivate variable DECL used in OMP context CTX.
5754 This just prints out diagnostics about threadprivate variable uses
5755 in untied tasks. If DECL2 is non-NULL, prevent this warning
5756 on that variable. */
5758 static bool
5759 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5760 tree decl2)
5762 splay_tree_node n;
5763 struct gimplify_omp_ctx *octx;
5765 for (octx = ctx; octx; octx = octx->outer_context)
5766 if ((octx->region_type & ORT_TARGET) != 0)
5768 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5769 if (n == NULL)
5771 error ("threadprivate variable %qE used in target region",
5772 DECL_NAME (decl));
5773 error_at (octx->location, "enclosing target region");
5774 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5776 if (decl2)
5777 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5780 if (ctx->region_type != ORT_UNTIED_TASK)
5781 return false;
5782 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5783 if (n == NULL)
5785 error ("threadprivate variable %qE used in untied task",
5786 DECL_NAME (decl));
5787 error_at (ctx->location, "enclosing task");
5788 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5790 if (decl2)
5791 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5792 return false;
5795 /* Determine outer default flags for DECL mentioned in an OMP region
5796 but not declared in an enclosing clause.
5798 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5799 remapped firstprivate instead of shared. To some extent this is
5800 addressed in omp_firstprivatize_type_sizes, but not
5801 effectively. */
5803 static unsigned
5804 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
5805 bool in_code, unsigned flags)
5807 enum omp_clause_default_kind default_kind = ctx->default_kind;
5808 enum omp_clause_default_kind kind;
5810 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5811 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5812 default_kind = kind;
5814 switch (default_kind)
5816 case OMP_CLAUSE_DEFAULT_NONE:
5818 const char *rtype;
5820 if (ctx->region_type & ORT_PARALLEL)
5821 rtype = "parallel";
5822 else if (ctx->region_type & ORT_TASK)
5823 rtype = "task";
5824 else if (ctx->region_type & ORT_TEAMS)
5825 rtype = "teams";
5826 else
5827 gcc_unreachable ();
5829 error ("%qE not specified in enclosing %s",
5830 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
5831 error_at (ctx->location, "enclosing %s", rtype);
5833 /* FALLTHRU */
5834 case OMP_CLAUSE_DEFAULT_SHARED:
5835 flags |= GOVD_SHARED;
5836 break;
5837 case OMP_CLAUSE_DEFAULT_PRIVATE:
5838 flags |= GOVD_PRIVATE;
5839 break;
5840 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5841 flags |= GOVD_FIRSTPRIVATE;
5842 break;
5843 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5844 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5845 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5846 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
5848 omp_notice_variable (octx, decl, in_code);
5849 for (; octx; octx = octx->outer_context)
5851 splay_tree_node n2;
5853 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5854 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
5855 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
5856 continue;
5857 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5859 flags |= GOVD_FIRSTPRIVATE;
5860 goto found_outer;
5862 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5864 flags |= GOVD_SHARED;
5865 goto found_outer;
5870 if (TREE_CODE (decl) == PARM_DECL
5871 || (!is_global_var (decl)
5872 && DECL_CONTEXT (decl) == current_function_decl))
5873 flags |= GOVD_FIRSTPRIVATE;
5874 else
5875 flags |= GOVD_SHARED;
5876 found_outer:
5877 break;
5879 default:
5880 gcc_unreachable ();
5883 return flags;
5886 /* Record the fact that DECL was used within the OMP context CTX.
5887 IN_CODE is true when real code uses DECL, and false when we should
5888 merely emit default(none) errors. Return true if DECL is going to
5889 be remapped and thus DECL shouldn't be gimplified into its
5890 DECL_VALUE_EXPR (if any). */
5892 static bool
5893 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5895 splay_tree_node n;
5896 unsigned flags = in_code ? GOVD_SEEN : 0;
5897 bool ret = false, shared;
5899 if (error_operand_p (decl))
5900 return false;
5902 if (ctx->region_type == ORT_NONE)
5903 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
5905 /* Threadprivate variables are predetermined. */
5906 if (is_global_var (decl))
5908 if (DECL_THREAD_LOCAL_P (decl))
5909 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5911 if (DECL_HAS_VALUE_EXPR_P (decl))
5913 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5915 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5916 return omp_notice_threadprivate_variable (ctx, decl, value);
5920 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5921 if ((ctx->region_type & ORT_TARGET) != 0)
5923 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5924 if (n == NULL)
5926 unsigned nflags = flags;
5927 if (ctx->target_map_pointers_as_0len_arrays
5928 || ctx->target_map_scalars_firstprivate)
5930 bool is_declare_target = false;
5931 bool is_scalar = false;
5932 if (is_global_var (decl)
5933 && varpool_node::get_create (decl)->offloadable)
5935 struct gimplify_omp_ctx *octx;
5936 for (octx = ctx->outer_context;
5937 octx; octx = octx->outer_context)
5939 n = splay_tree_lookup (octx->variables,
5940 (splay_tree_key)decl);
5941 if (n
5942 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
5943 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5944 break;
5946 is_declare_target = octx == NULL;
5948 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
5950 tree type = TREE_TYPE (decl);
5951 if (TREE_CODE (type) == REFERENCE_TYPE)
5952 type = TREE_TYPE (type);
5953 if (TREE_CODE (type) == COMPLEX_TYPE)
5954 type = TREE_TYPE (type);
5955 if (INTEGRAL_TYPE_P (type)
5956 || SCALAR_FLOAT_TYPE_P (type)
5957 || TREE_CODE (type) == POINTER_TYPE)
5958 is_scalar = true;
5960 if (is_declare_target)
5962 else if (ctx->target_map_pointers_as_0len_arrays
5963 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
5964 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
5965 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
5966 == POINTER_TYPE)))
5967 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
5968 else if (is_scalar)
5969 nflags |= GOVD_FIRSTPRIVATE;
5971 tree type = TREE_TYPE (decl);
5972 if (nflags == flags
5973 && gimplify_omp_ctxp->target_firstprivatize_array_bases
5974 && lang_hooks.decls.omp_privatize_by_reference (decl))
5975 type = TREE_TYPE (type);
5976 if (nflags == flags
5977 && !lang_hooks.types.omp_mappable_type (type))
5979 error ("%qD referenced in target region does not have "
5980 "a mappable type", decl);
5981 nflags |= GOVD_MAP | GOVD_EXPLICIT;
5983 else if (nflags == flags)
5984 nflags |= GOVD_MAP;
5985 omp_add_variable (ctx, decl, nflags);
5987 else
5989 /* If nothing changed, there's nothing left to do. */
5990 if ((n->value & flags) == flags)
5991 return ret;
5992 n->value |= flags;
5994 goto do_outer;
5997 if (n == NULL)
5999 if (ctx->region_type == ORT_WORKSHARE
6000 || ctx->region_type == ORT_SIMD
6001 || ctx->region_type == ORT_TARGET_DATA)
6002 goto do_outer;
6004 flags = omp_default_clause (ctx, decl, in_code, flags);
6006 if ((flags & GOVD_PRIVATE)
6007 && lang_hooks.decls.omp_private_outer_ref (decl))
6008 flags |= GOVD_PRIVATE_OUTER_REF;
6010 omp_add_variable (ctx, decl, flags);
6012 shared = (flags & GOVD_SHARED) != 0;
6013 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6014 goto do_outer;
6017 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6018 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6019 && DECL_SIZE (decl)
6020 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6022 splay_tree_node n2;
6023 tree t = DECL_VALUE_EXPR (decl);
6024 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6025 t = TREE_OPERAND (t, 0);
6026 gcc_assert (DECL_P (t));
6027 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6028 n2->value |= GOVD_SEEN;
6031 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6032 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6034 /* If nothing changed, there's nothing left to do. */
6035 if ((n->value & flags) == flags)
6036 return ret;
6037 flags |= n->value;
6038 n->value = flags;
6040 do_outer:
6041 /* If the variable is private in the current context, then we don't
6042 need to propagate anything to an outer context. */
6043 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6044 return ret;
6045 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6046 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6047 return ret;
6048 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6049 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6050 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6051 return ret;
6052 if (ctx->outer_context
6053 && omp_notice_variable (ctx->outer_context, decl, in_code))
6054 return true;
6055 return ret;
6058 /* Verify that DECL is private within CTX. If there's specific information
6059 to the contrary in the innermost scope, generate an error. */
6061 static bool
6062 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
6064 splay_tree_node n;
6066 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6067 if (n != NULL)
6069 if (n->value & GOVD_SHARED)
6071 if (ctx == gimplify_omp_ctxp)
6073 if (simd)
6074 error ("iteration variable %qE is predetermined linear",
6075 DECL_NAME (decl));
6076 else
6077 error ("iteration variable %qE should be private",
6078 DECL_NAME (decl));
6079 n->value = GOVD_PRIVATE;
6080 return true;
6082 else
6083 return false;
6085 else if ((n->value & GOVD_EXPLICIT) != 0
6086 && (ctx == gimplify_omp_ctxp
6087 || (ctx->region_type == ORT_COMBINED_PARALLEL
6088 && gimplify_omp_ctxp->outer_context == ctx)))
6090 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6091 error ("iteration variable %qE should not be firstprivate",
6092 DECL_NAME (decl));
6093 else if ((n->value & GOVD_REDUCTION) != 0)
6094 error ("iteration variable %qE should not be reduction",
6095 DECL_NAME (decl));
6096 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
6097 error ("iteration variable %qE should not be linear",
6098 DECL_NAME (decl));
6099 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
6100 error ("iteration variable %qE should not be lastprivate",
6101 DECL_NAME (decl));
6102 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6103 error ("iteration variable %qE should not be private",
6104 DECL_NAME (decl));
6105 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
6106 error ("iteration variable %qE is predetermined linear",
6107 DECL_NAME (decl));
6109 return (ctx == gimplify_omp_ctxp
6110 || (ctx->region_type == ORT_COMBINED_PARALLEL
6111 && gimplify_omp_ctxp->outer_context == ctx));
6114 if (ctx->region_type != ORT_WORKSHARE
6115 && ctx->region_type != ORT_SIMD)
6116 return false;
6117 else if (ctx->outer_context)
6118 return omp_is_private (ctx->outer_context, decl, simd);
6119 return false;
6122 /* Return true if DECL is private within a parallel region
6123 that binds to the current construct's context or in parallel
6124 region's REDUCTION clause. */
6126 static bool
6127 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6129 splay_tree_node n;
6133 ctx = ctx->outer_context;
6134 if (ctx == NULL)
6136 if (is_global_var (decl))
6137 return false;
6139 /* References might be private, but might be shared too,
6140 when checking for copyprivate, assume they might be
6141 private, otherwise assume they might be shared. */
6142 if (copyprivate)
6143 return true;
6145 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6146 return false;
6148 /* Treat C++ privatized non-static data members outside
6149 of the privatization the same. */
6150 if (omp_member_access_dummy_var (decl))
6151 return false;
6153 return true;
6156 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6158 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6159 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
6160 continue;
6162 if (n != NULL)
6164 if ((n->value & GOVD_LOCAL) != 0
6165 && omp_member_access_dummy_var (decl))
6166 return false;
6167 return (n->value & GOVD_SHARED) == 0;
6170 while (ctx->region_type == ORT_WORKSHARE
6171 || ctx->region_type == ORT_SIMD);
6172 return false;
6175 /* Return true if the CTX is combined with distribute and thus
6176 lastprivate can't be supported. */
6178 static bool
6179 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
6183 if (ctx->outer_context == NULL)
6184 return false;
6185 ctx = ctx->outer_context;
6186 switch (ctx->region_type)
6188 case ORT_WORKSHARE:
6189 if (!ctx->combined_loop)
6190 return false;
6191 if (ctx->distribute)
6192 return lang_GNU_Fortran ();
6193 break;
6194 case ORT_COMBINED_PARALLEL:
6195 break;
6196 case ORT_COMBINED_TEAMS:
6197 return lang_GNU_Fortran ();
6198 default:
6199 return false;
6202 while (1);
6205 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
6207 static tree
6208 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
6210 tree t = *tp;
6212 /* If this node has been visited, unmark it and keep looking. */
6213 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
6214 return t;
6216 if (IS_TYPE_OR_DECL_P (t))
6217 *walk_subtrees = 0;
6218 return NULL_TREE;
6221 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6222 and previous omp contexts. */
6224 static void
6225 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6226 enum omp_region_type region_type,
6227 enum tree_code code)
6229 struct gimplify_omp_ctx *ctx, *outer_ctx;
6230 tree c;
6231 hash_map<tree, tree> *struct_map_to_clause = NULL;
6232 tree *prev_list_p = NULL;
6234 ctx = new_omp_context (region_type);
6235 outer_ctx = ctx->outer_context;
6236 if (code == OMP_TARGET && !lang_GNU_Fortran ())
6238 ctx->target_map_pointers_as_0len_arrays = true;
6239 /* FIXME: For Fortran we want to set this too, when
6240 the Fortran FE is updated to OpenMP 4.5. */
6241 ctx->target_map_scalars_firstprivate = true;
6243 if (!lang_GNU_Fortran ())
6244 switch (code)
6246 case OMP_TARGET:
6247 case OMP_TARGET_DATA:
6248 case OMP_TARGET_ENTER_DATA:
6249 case OMP_TARGET_EXIT_DATA:
6250 ctx->target_firstprivatize_array_bases = true;
6251 default:
6252 break;
6255 while ((c = *list_p) != NULL)
6257 bool remove = false;
6258 bool notice_outer = true;
6259 const char *check_non_private = NULL;
6260 unsigned int flags;
6261 tree decl;
6263 switch (OMP_CLAUSE_CODE (c))
6265 case OMP_CLAUSE_PRIVATE:
6266 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6267 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6269 flags |= GOVD_PRIVATE_OUTER_REF;
6270 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6272 else
6273 notice_outer = false;
6274 goto do_add;
6275 case OMP_CLAUSE_SHARED:
6276 flags = GOVD_SHARED | GOVD_EXPLICIT;
6277 goto do_add;
6278 case OMP_CLAUSE_FIRSTPRIVATE:
6279 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6280 check_non_private = "firstprivate";
6281 goto do_add;
6282 case OMP_CLAUSE_LASTPRIVATE:
6283 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6284 check_non_private = "lastprivate";
6285 decl = OMP_CLAUSE_DECL (c);
6286 if (omp_no_lastprivate (ctx))
6288 notice_outer = false;
6289 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6291 else if (error_operand_p (decl))
6292 goto do_add;
6293 else if (outer_ctx
6294 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
6295 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
6296 && splay_tree_lookup (outer_ctx->variables,
6297 (splay_tree_key) decl) == NULL)
6299 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
6300 if (outer_ctx->outer_context)
6301 omp_notice_variable (outer_ctx->outer_context, decl, true);
6303 else if (outer_ctx
6304 && (outer_ctx->region_type & ORT_TASK) != 0
6305 && outer_ctx->combined_loop
6306 && splay_tree_lookup (outer_ctx->variables,
6307 (splay_tree_key) decl) == NULL)
6309 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6310 if (outer_ctx->outer_context)
6311 omp_notice_variable (outer_ctx->outer_context, decl, true);
6313 else if (outer_ctx
6314 && outer_ctx->region_type == ORT_WORKSHARE
6315 && outer_ctx->combined_loop
6316 && splay_tree_lookup (outer_ctx->variables,
6317 (splay_tree_key) decl) == NULL
6318 && !omp_check_private (outer_ctx, decl, false))
6320 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6321 if (outer_ctx->outer_context
6322 && (outer_ctx->outer_context->region_type
6323 == ORT_COMBINED_PARALLEL)
6324 && splay_tree_lookup (outer_ctx->outer_context->variables,
6325 (splay_tree_key) decl) == NULL)
6327 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
6328 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
6329 if (octx->outer_context)
6330 omp_notice_variable (octx->outer_context, decl, true);
6332 else if (outer_ctx->outer_context)
6333 omp_notice_variable (outer_ctx->outer_context, decl, true);
6335 goto do_add;
6336 case OMP_CLAUSE_REDUCTION:
6337 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6338 check_non_private = "reduction";
6339 decl = OMP_CLAUSE_DECL (c);
6340 if (TREE_CODE (decl) == MEM_REF)
6342 tree type = TREE_TYPE (decl);
6343 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
6344 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6346 remove = true;
6347 break;
6349 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6350 if (DECL_P (v))
6352 omp_firstprivatize_variable (ctx, v);
6353 omp_notice_variable (ctx, v, true);
6355 decl = TREE_OPERAND (decl, 0);
6356 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
6358 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
6359 NULL, is_gimple_val, fb_rvalue)
6360 == GS_ERROR)
6362 remove = true;
6363 break;
6365 v = TREE_OPERAND (decl, 1);
6366 if (DECL_P (v))
6368 omp_firstprivatize_variable (ctx, v);
6369 omp_notice_variable (ctx, v, true);
6371 decl = TREE_OPERAND (decl, 0);
6373 if (TREE_CODE (decl) == ADDR_EXPR
6374 || TREE_CODE (decl) == INDIRECT_REF)
6375 decl = TREE_OPERAND (decl, 0);
6377 goto do_add_decl;
6378 case OMP_CLAUSE_LINEAR:
6379 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6380 is_gimple_val, fb_rvalue) == GS_ERROR)
6382 remove = true;
6383 break;
6385 else
6387 if (code == OMP_SIMD
6388 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6390 struct gimplify_omp_ctx *octx = outer_ctx;
6391 if (octx
6392 && octx->region_type == ORT_WORKSHARE
6393 && octx->combined_loop
6394 && !octx->distribute)
6396 if (octx->outer_context
6397 && (octx->outer_context->region_type
6398 == ORT_COMBINED_PARALLEL))
6399 octx = octx->outer_context->outer_context;
6400 else
6401 octx = octx->outer_context;
6403 if (octx
6404 && octx->region_type == ORT_WORKSHARE
6405 && octx->combined_loop
6406 && octx->distribute
6407 && !lang_GNU_Fortran ())
6409 error_at (OMP_CLAUSE_LOCATION (c),
6410 "%<linear%> clause for variable other than "
6411 "loop iterator specified on construct "
6412 "combined with %<distribute%>");
6413 remove = true;
6414 break;
6417 /* For combined #pragma omp parallel for simd, need to put
6418 lastprivate and perhaps firstprivate too on the
6419 parallel. Similarly for #pragma omp for simd. */
6420 struct gimplify_omp_ctx *octx = outer_ctx;
6421 decl = NULL_TREE;
6422 if (omp_no_lastprivate (ctx))
6423 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6426 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6427 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6428 break;
6429 decl = OMP_CLAUSE_DECL (c);
6430 if (error_operand_p (decl))
6432 decl = NULL_TREE;
6433 break;
6435 flags = GOVD_SEEN;
6436 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6437 flags |= GOVD_FIRSTPRIVATE;
6438 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6439 flags |= GOVD_LASTPRIVATE;
6440 if (octx
6441 && octx->region_type == ORT_WORKSHARE
6442 && octx->combined_loop)
6444 if (octx->outer_context
6445 && (octx->outer_context->region_type
6446 == ORT_COMBINED_PARALLEL))
6447 octx = octx->outer_context;
6448 else if (omp_check_private (octx, decl, false))
6449 break;
6451 else if (octx
6452 && (octx->region_type & ORT_TASK) != 0
6453 && octx->combined_loop)
6455 else if (octx
6456 && octx->region_type == ORT_COMBINED_PARALLEL
6457 && ctx->region_type == ORT_WORKSHARE
6458 && octx == outer_ctx)
6459 flags = GOVD_SEEN | GOVD_SHARED;
6460 else if (octx
6461 && octx->region_type == ORT_COMBINED_TEAMS)
6462 flags = GOVD_SEEN | GOVD_SHARED;
6463 else if (octx
6464 && octx->region_type == ORT_COMBINED_TARGET)
6466 flags &= ~GOVD_LASTPRIVATE;
6467 if (flags == GOVD_SEEN)
6468 break;
6470 else
6471 break;
6472 splay_tree_node on
6473 = splay_tree_lookup (octx->variables,
6474 (splay_tree_key) decl);
6475 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
6477 octx = NULL;
6478 break;
6480 omp_add_variable (octx, decl, flags);
6481 if (octx->outer_context == NULL)
6482 break;
6483 octx = octx->outer_context;
6485 while (1);
6486 if (octx
6487 && decl
6488 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6489 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6490 omp_notice_variable (octx, decl, true);
6492 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6493 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6494 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6496 notice_outer = false;
6497 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6499 goto do_add;
6501 case OMP_CLAUSE_MAP:
6502 decl = OMP_CLAUSE_DECL (c);
6503 if (error_operand_p (decl))
6504 remove = true;
6505 switch (code)
6507 case OMP_TARGET:
6508 break;
6509 case OMP_TARGET_DATA:
6510 case OMP_TARGET_ENTER_DATA:
6511 case OMP_TARGET_EXIT_DATA:
6512 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6513 || (OMP_CLAUSE_MAP_KIND (c)
6514 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
6515 /* For target {,enter ,exit }data only the array slice is
6516 mapped, but not the pointer to it. */
6517 remove = true;
6518 break;
6519 default:
6520 break;
6522 if (remove)
6523 break;
6524 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6525 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6526 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6527 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6528 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6530 remove = true;
6531 break;
6533 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6534 || (OMP_CLAUSE_MAP_KIND (c)
6535 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
6536 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
6538 OMP_CLAUSE_SIZE (c)
6539 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL);
6540 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
6541 GOVD_FIRSTPRIVATE | GOVD_SEEN);
6543 if (!DECL_P (decl))
6545 tree d = decl, *pd;
6546 if (TREE_CODE (d) == ARRAY_REF)
6548 while (TREE_CODE (d) == ARRAY_REF)
6549 d = TREE_OPERAND (d, 0);
6550 if (TREE_CODE (d) == COMPONENT_REF
6551 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
6552 decl = d;
6554 pd = &OMP_CLAUSE_DECL (c);
6555 if (d == decl
6556 && TREE_CODE (decl) == INDIRECT_REF
6557 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
6558 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
6559 == REFERENCE_TYPE))
6561 pd = &TREE_OPERAND (decl, 0);
6562 decl = TREE_OPERAND (decl, 0);
6564 if (TREE_CODE (decl) == COMPONENT_REF)
6566 while (TREE_CODE (decl) == COMPONENT_REF)
6567 decl = TREE_OPERAND (decl, 0);
6569 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
6570 == GS_ERROR)
6572 remove = true;
6573 break;
6575 if (DECL_P (decl))
6577 if (error_operand_p (decl))
6579 remove = true;
6580 break;
6583 if (TYPE_SIZE_UNIT (TREE_TYPE (decl)) == NULL
6584 || (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (decl)))
6585 != INTEGER_CST))
6587 error_at (OMP_CLAUSE_LOCATION (c),
6588 "mapping field %qE of variable length "
6589 "structure", OMP_CLAUSE_DECL (c));
6590 remove = true;
6591 break;
6594 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
6596 /* Error recovery. */
6597 if (prev_list_p == NULL)
6599 remove = true;
6600 break;
6602 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
6604 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
6605 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
6607 remove = true;
6608 break;
6613 tree offset;
6614 HOST_WIDE_INT bitsize, bitpos;
6615 machine_mode mode;
6616 int unsignedp, reversep, volatilep = 0;
6617 tree base = OMP_CLAUSE_DECL (c);
6618 while (TREE_CODE (base) == ARRAY_REF)
6619 base = TREE_OPERAND (base, 0);
6620 if (TREE_CODE (base) == INDIRECT_REF)
6621 base = TREE_OPERAND (base, 0);
6622 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
6623 &mode, &unsignedp, &reversep,
6624 &volatilep, false);
6625 gcc_assert (base == decl
6626 && (offset == NULL_TREE
6627 || TREE_CODE (offset) == INTEGER_CST));
6629 splay_tree_node n
6630 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6631 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
6632 == GOMP_MAP_ALWAYS_POINTER);
6633 if (n == NULL || (n->value & GOVD_MAP) == 0)
6635 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6636 OMP_CLAUSE_MAP);
6637 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
6638 OMP_CLAUSE_DECL (l) = decl;
6639 OMP_CLAUSE_SIZE (l) = size_int (1);
6640 if (struct_map_to_clause == NULL)
6641 struct_map_to_clause = new hash_map<tree, tree>;
6642 struct_map_to_clause->put (decl, l);
6643 if (ptr)
6645 enum gomp_map_kind mkind
6646 = code == OMP_TARGET_EXIT_DATA
6647 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
6648 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6649 OMP_CLAUSE_MAP);
6650 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
6651 OMP_CLAUSE_DECL (c2)
6652 = unshare_expr (OMP_CLAUSE_DECL (c));
6653 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
6654 OMP_CLAUSE_SIZE (c2)
6655 = TYPE_SIZE_UNIT (ptr_type_node);
6656 OMP_CLAUSE_CHAIN (l) = c2;
6657 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
6659 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
6660 tree c3
6661 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6662 OMP_CLAUSE_MAP);
6663 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
6664 OMP_CLAUSE_DECL (c3)
6665 = unshare_expr (OMP_CLAUSE_DECL (c4));
6666 OMP_CLAUSE_SIZE (c3)
6667 = TYPE_SIZE_UNIT (ptr_type_node);
6668 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
6669 OMP_CLAUSE_CHAIN (c2) = c3;
6671 *prev_list_p = l;
6672 prev_list_p = NULL;
6674 else
6676 OMP_CLAUSE_CHAIN (l) = c;
6677 *list_p = l;
6678 list_p = &OMP_CLAUSE_CHAIN (l);
6680 flags = GOVD_MAP | GOVD_EXPLICIT;
6681 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
6682 flags |= GOVD_SEEN;
6683 goto do_add_decl;
6685 else
6687 tree *osc = struct_map_to_clause->get (decl);
6688 tree *sc = NULL, *scp = NULL;
6689 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
6690 n->value |= GOVD_SEEN;
6691 offset_int o1, o2;
6692 if (offset)
6693 o1 = wi::to_offset (offset);
6694 else
6695 o1 = 0;
6696 if (bitpos)
6697 o1 = o1 + bitpos / BITS_PER_UNIT;
6698 for (sc = &OMP_CLAUSE_CHAIN (*osc);
6699 *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
6700 if (ptr && sc == prev_list_p)
6701 break;
6702 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6703 != COMPONENT_REF
6704 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6705 != INDIRECT_REF)
6706 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6707 != ARRAY_REF))
6708 break;
6709 else
6711 tree offset2;
6712 HOST_WIDE_INT bitsize2, bitpos2;
6713 base = OMP_CLAUSE_DECL (*sc);
6714 if (TREE_CODE (base) == ARRAY_REF)
6716 while (TREE_CODE (base) == ARRAY_REF)
6717 base = TREE_OPERAND (base, 0);
6718 if (TREE_CODE (base) != COMPONENT_REF
6719 || (TREE_CODE (TREE_TYPE (base))
6720 != ARRAY_TYPE))
6721 break;
6723 else if (TREE_CODE (base) == INDIRECT_REF
6724 && (TREE_CODE (TREE_OPERAND (base, 0))
6725 == COMPONENT_REF)
6726 && (TREE_CODE (TREE_TYPE
6727 (TREE_OPERAND (base, 0)))
6728 == REFERENCE_TYPE))
6729 base = TREE_OPERAND (base, 0);
6730 base = get_inner_reference (base, &bitsize2,
6731 &bitpos2, &offset2,
6732 &mode, &unsignedp,
6733 &reversep, &volatilep,
6734 false);
6735 if (base != decl)
6736 break;
6737 if (scp)
6738 continue;
6739 gcc_assert (offset == NULL_TREE
6740 || TREE_CODE (offset) == INTEGER_CST);
6741 tree d1 = OMP_CLAUSE_DECL (*sc);
6742 tree d2 = OMP_CLAUSE_DECL (c);
6743 while (TREE_CODE (d1) == ARRAY_REF)
6744 d1 = TREE_OPERAND (d1, 0);
6745 while (TREE_CODE (d2) == ARRAY_REF)
6746 d2 = TREE_OPERAND (d2, 0);
6747 if (TREE_CODE (d1) == INDIRECT_REF)
6748 d1 = TREE_OPERAND (d1, 0);
6749 if (TREE_CODE (d2) == INDIRECT_REF)
6750 d2 = TREE_OPERAND (d2, 0);
6751 while (TREE_CODE (d1) == COMPONENT_REF)
6752 if (TREE_CODE (d2) == COMPONENT_REF
6753 && TREE_OPERAND (d1, 1)
6754 == TREE_OPERAND (d2, 1))
6756 d1 = TREE_OPERAND (d1, 0);
6757 d2 = TREE_OPERAND (d2, 0);
6759 else
6760 break;
6761 if (d1 == d2)
6763 error_at (OMP_CLAUSE_LOCATION (c),
6764 "%qE appears more than once in map "
6765 "clauses", OMP_CLAUSE_DECL (c));
6766 remove = true;
6767 break;
6769 if (offset2)
6770 o2 = wi::to_offset (offset2);
6771 else
6772 o2 = 0;
6773 if (bitpos2)
6774 o2 = o2 + bitpos2 / BITS_PER_UNIT;
6775 if (wi::ltu_p (o1, o2)
6776 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
6778 if (ptr)
6779 scp = sc;
6780 else
6781 break;
6784 if (remove)
6785 break;
6786 OMP_CLAUSE_SIZE (*osc)
6787 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
6788 size_one_node);
6789 if (ptr)
6791 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6792 OMP_CLAUSE_MAP);
6793 tree cl = NULL_TREE;
6794 enum gomp_map_kind mkind
6795 = code == OMP_TARGET_EXIT_DATA
6796 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
6797 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
6798 OMP_CLAUSE_DECL (c2)
6799 = unshare_expr (OMP_CLAUSE_DECL (c));
6800 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
6801 OMP_CLAUSE_SIZE (c2)
6802 = TYPE_SIZE_UNIT (ptr_type_node);
6803 cl = scp ? *prev_list_p : c2;
6804 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
6806 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
6807 tree c3
6808 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6809 OMP_CLAUSE_MAP);
6810 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
6811 OMP_CLAUSE_DECL (c3)
6812 = unshare_expr (OMP_CLAUSE_DECL (c4));
6813 OMP_CLAUSE_SIZE (c3)
6814 = TYPE_SIZE_UNIT (ptr_type_node);
6815 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
6816 if (!scp)
6817 OMP_CLAUSE_CHAIN (c2) = c3;
6818 else
6819 cl = c3;
6821 if (scp)
6822 *scp = c2;
6823 if (sc == prev_list_p)
6825 *sc = cl;
6826 prev_list_p = NULL;
6828 else
6830 *prev_list_p = OMP_CLAUSE_CHAIN (c);
6831 list_p = prev_list_p;
6832 prev_list_p = NULL;
6833 OMP_CLAUSE_CHAIN (c) = *sc;
6834 *sc = cl;
6835 continue;
6838 else if (*sc != c)
6840 *list_p = OMP_CLAUSE_CHAIN (c);
6841 OMP_CLAUSE_CHAIN (c) = *sc;
6842 *sc = c;
6843 continue;
6847 if (!remove
6848 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
6849 && OMP_CLAUSE_CHAIN (c)
6850 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
6851 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
6852 == GOMP_MAP_ALWAYS_POINTER))
6853 prev_list_p = list_p;
6854 break;
6856 flags = GOVD_MAP | GOVD_EXPLICIT;
6857 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
6858 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
6859 flags |= GOVD_MAP_ALWAYS_TO;
6860 goto do_add;
6862 case OMP_CLAUSE_DEPEND:
6863 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
6864 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
6866 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
6867 omp-low.c. */
6868 break;
6870 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6872 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6873 NULL, is_gimple_val, fb_rvalue);
6874 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6876 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6878 remove = true;
6879 break;
6881 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6882 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6883 is_gimple_val, fb_rvalue) == GS_ERROR)
6885 remove = true;
6886 break;
6888 break;
6890 case OMP_CLAUSE_TO:
6891 case OMP_CLAUSE_FROM:
6892 case OMP_CLAUSE__CACHE_:
6893 decl = OMP_CLAUSE_DECL (c);
6894 if (error_operand_p (decl))
6896 remove = true;
6897 break;
6899 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6900 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6901 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6902 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6903 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6905 remove = true;
6906 break;
6908 if (!DECL_P (decl))
6910 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6911 NULL, is_gimple_lvalue, fb_lvalue)
6912 == GS_ERROR)
6914 remove = true;
6915 break;
6917 break;
6919 goto do_notice;
6921 case OMP_CLAUSE_USE_DEVICE_PTR:
6922 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6923 goto do_add;
6924 case OMP_CLAUSE_IS_DEVICE_PTR:
6925 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6926 goto do_add;
6928 do_add:
6929 decl = OMP_CLAUSE_DECL (c);
6930 do_add_decl:
6931 if (error_operand_p (decl))
6933 remove = true;
6934 break;
6936 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
6938 tree t = omp_member_access_dummy_var (decl);
6939 if (t)
6941 tree v = DECL_VALUE_EXPR (decl);
6942 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
6943 if (outer_ctx)
6944 omp_notice_variable (outer_ctx, t, true);
6947 omp_add_variable (ctx, decl, flags);
6948 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6949 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6951 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6952 GOVD_LOCAL | GOVD_SEEN);
6953 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
6954 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6955 find_decl_expr,
6956 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6957 NULL) == NULL_TREE)
6958 omp_add_variable (ctx,
6959 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6960 GOVD_LOCAL | GOVD_SEEN);
6961 gimplify_omp_ctxp = ctx;
6962 push_gimplify_context ();
6964 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6965 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6967 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6968 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6969 pop_gimplify_context
6970 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6971 push_gimplify_context ();
6972 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6973 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6974 pop_gimplify_context
6975 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6976 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6977 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6979 gimplify_omp_ctxp = outer_ctx;
6981 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6982 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6984 gimplify_omp_ctxp = ctx;
6985 push_gimplify_context ();
6986 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6988 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6989 NULL, NULL);
6990 TREE_SIDE_EFFECTS (bind) = 1;
6991 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6992 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6994 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6995 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6996 pop_gimplify_context
6997 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6998 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
7000 gimplify_omp_ctxp = outer_ctx;
7002 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7003 && OMP_CLAUSE_LINEAR_STMT (c))
7005 gimplify_omp_ctxp = ctx;
7006 push_gimplify_context ();
7007 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
7009 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
7010 NULL, NULL);
7011 TREE_SIDE_EFFECTS (bind) = 1;
7012 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
7013 OMP_CLAUSE_LINEAR_STMT (c) = bind;
7015 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
7016 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
7017 pop_gimplify_context
7018 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
7019 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
7021 gimplify_omp_ctxp = outer_ctx;
7023 if (notice_outer)
7024 goto do_notice;
7025 break;
7027 case OMP_CLAUSE_COPYIN:
7028 case OMP_CLAUSE_COPYPRIVATE:
7029 decl = OMP_CLAUSE_DECL (c);
7030 if (error_operand_p (decl))
7032 remove = true;
7033 break;
7035 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
7036 && !remove
7037 && !omp_check_private (ctx, decl, true))
7039 remove = true;
7040 if (is_global_var (decl))
7042 if (DECL_THREAD_LOCAL_P (decl))
7043 remove = false;
7044 else if (DECL_HAS_VALUE_EXPR_P (decl))
7046 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7048 if (value
7049 && DECL_P (value)
7050 && DECL_THREAD_LOCAL_P (value))
7051 remove = false;
7054 if (remove)
7055 error_at (OMP_CLAUSE_LOCATION (c),
7056 "copyprivate variable %qE is not threadprivate"
7057 " or private in outer context", DECL_NAME (decl));
7059 do_notice:
7060 if (outer_ctx)
7061 omp_notice_variable (outer_ctx, decl, true);
7062 if (check_non_private
7063 && region_type == ORT_WORKSHARE
7064 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
7065 || decl == OMP_CLAUSE_DECL (c)
7066 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
7067 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
7068 == ADDR_EXPR
7069 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
7070 == POINTER_PLUS_EXPR
7071 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
7072 (OMP_CLAUSE_DECL (c), 0), 0))
7073 == ADDR_EXPR)))))
7074 && omp_check_private (ctx, decl, false))
7076 error ("%s variable %qE is private in outer context",
7077 check_non_private, DECL_NAME (decl));
7078 remove = true;
7080 break;
7082 case OMP_CLAUSE_IF:
7083 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
7084 && OMP_CLAUSE_IF_MODIFIER (c) != code)
7086 const char *p[2];
7087 for (int i = 0; i < 2; i++)
7088 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
7090 case OMP_PARALLEL: p[i] = "parallel"; break;
7091 case OMP_TASK: p[i] = "task"; break;
7092 case OMP_TASKLOOP: p[i] = "taskloop"; break;
7093 case OMP_TARGET_DATA: p[i] = "target data"; break;
7094 case OMP_TARGET: p[i] = "target"; break;
7095 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
7096 case OMP_TARGET_ENTER_DATA:
7097 p[i] = "target enter data"; break;
7098 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
7099 default: gcc_unreachable ();
7101 error_at (OMP_CLAUSE_LOCATION (c),
7102 "expected %qs %<if%> clause modifier rather than %qs",
7103 p[0], p[1]);
7104 remove = true;
7106 /* Fall through. */
7108 case OMP_CLAUSE_FINAL:
7109 OMP_CLAUSE_OPERAND (c, 0)
7110 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
7111 /* Fall through. */
7113 case OMP_CLAUSE_SCHEDULE:
7114 case OMP_CLAUSE_NUM_THREADS:
7115 case OMP_CLAUSE_NUM_TEAMS:
7116 case OMP_CLAUSE_THREAD_LIMIT:
7117 case OMP_CLAUSE_DIST_SCHEDULE:
7118 case OMP_CLAUSE_DEVICE:
7119 case OMP_CLAUSE_PRIORITY:
7120 case OMP_CLAUSE_GRAINSIZE:
7121 case OMP_CLAUSE_NUM_TASKS:
7122 case OMP_CLAUSE_HINT:
7123 case OMP_CLAUSE__CILK_FOR_COUNT_:
7124 case OMP_CLAUSE_ASYNC:
7125 case OMP_CLAUSE_WAIT:
7126 case OMP_CLAUSE_NUM_GANGS:
7127 case OMP_CLAUSE_NUM_WORKERS:
7128 case OMP_CLAUSE_VECTOR_LENGTH:
7129 case OMP_CLAUSE_WORKER:
7130 case OMP_CLAUSE_VECTOR:
7131 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
7132 is_gimple_val, fb_rvalue) == GS_ERROR)
7133 remove = true;
7134 break;
7136 case OMP_CLAUSE_GANG:
7137 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
7138 is_gimple_val, fb_rvalue) == GS_ERROR)
7139 remove = true;
7140 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
7141 is_gimple_val, fb_rvalue) == GS_ERROR)
7142 remove = true;
7143 break;
7145 case OMP_CLAUSE_TILE:
7146 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7147 list = TREE_CHAIN (list))
7149 if (gimplify_expr (&TREE_VALUE (list), pre_p, NULL,
7150 is_gimple_val, fb_rvalue) == GS_ERROR)
7151 remove = true;
7153 break;
7155 case OMP_CLAUSE_DEVICE_RESIDENT:
7156 case OMP_CLAUSE_USE_DEVICE:
7157 remove = true;
7158 break;
7160 case OMP_CLAUSE_NOWAIT:
7161 case OMP_CLAUSE_ORDERED:
7162 case OMP_CLAUSE_UNTIED:
7163 case OMP_CLAUSE_COLLAPSE:
7164 case OMP_CLAUSE_AUTO:
7165 case OMP_CLAUSE_SEQ:
7166 case OMP_CLAUSE_INDEPENDENT:
7167 case OMP_CLAUSE_MERGEABLE:
7168 case OMP_CLAUSE_PROC_BIND:
7169 case OMP_CLAUSE_SAFELEN:
7170 case OMP_CLAUSE_SIMDLEN:
7171 case OMP_CLAUSE_NOGROUP:
7172 case OMP_CLAUSE_THREADS:
7173 case OMP_CLAUSE_SIMD:
7174 break;
7176 case OMP_CLAUSE_DEFAULTMAP:
7177 ctx->target_map_scalars_firstprivate = false;
7178 break;
7180 case OMP_CLAUSE_ALIGNED:
7181 decl = OMP_CLAUSE_DECL (c);
7182 if (error_operand_p (decl))
7184 remove = true;
7185 break;
7187 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
7188 is_gimple_val, fb_rvalue) == GS_ERROR)
7190 remove = true;
7191 break;
7193 if (!is_global_var (decl)
7194 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7195 omp_add_variable (ctx, decl, GOVD_ALIGNED);
7196 break;
7198 case OMP_CLAUSE_DEFAULT:
7199 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
7200 break;
7202 default:
7203 gcc_unreachable ();
7206 if (remove)
7207 *list_p = OMP_CLAUSE_CHAIN (c);
7208 else
7209 list_p = &OMP_CLAUSE_CHAIN (c);
7212 gimplify_omp_ctxp = ctx;
7213 if (struct_map_to_clause)
7214 delete struct_map_to_clause;
7217 struct gimplify_adjust_omp_clauses_data
7219 tree *list_p;
7220 gimple_seq *pre_p;
7223 /* For all variables that were not actually used within the context,
7224 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
7226 static int
7227 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
7229 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
7230 gimple_seq *pre_p
7231 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
7232 tree decl = (tree) n->key;
7233 unsigned flags = n->value;
7234 enum omp_clause_code code;
7235 tree clause;
7236 bool private_debug;
7238 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
7239 return 0;
7240 if ((flags & GOVD_SEEN) == 0)
7241 return 0;
7242 if (flags & GOVD_DEBUG_PRIVATE)
7244 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
7245 private_debug = true;
7247 else if (flags & GOVD_MAP)
7248 private_debug = false;
7249 else
7250 private_debug
7251 = lang_hooks.decls.omp_private_debug_clause (decl,
7252 !!(flags & GOVD_SHARED));
7253 if (private_debug)
7254 code = OMP_CLAUSE_PRIVATE;
7255 else if (flags & GOVD_MAP)
7256 code = OMP_CLAUSE_MAP;
7257 else if (flags & GOVD_SHARED)
7259 if (is_global_var (decl))
7261 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7262 while (ctx != NULL)
7264 splay_tree_node on
7265 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7266 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7267 | GOVD_PRIVATE | GOVD_REDUCTION
7268 | GOVD_LINEAR | GOVD_MAP)) != 0)
7269 break;
7270 ctx = ctx->outer_context;
7272 if (ctx == NULL)
7273 return 0;
7275 code = OMP_CLAUSE_SHARED;
7277 else if (flags & GOVD_PRIVATE)
7278 code = OMP_CLAUSE_PRIVATE;
7279 else if (flags & GOVD_FIRSTPRIVATE)
7280 code = OMP_CLAUSE_FIRSTPRIVATE;
7281 else if (flags & GOVD_LASTPRIVATE)
7282 code = OMP_CLAUSE_LASTPRIVATE;
7283 else if (flags & GOVD_ALIGNED)
7284 return 0;
7285 else
7286 gcc_unreachable ();
7288 clause = build_omp_clause (input_location, code);
7289 OMP_CLAUSE_DECL (clause) = decl;
7290 OMP_CLAUSE_CHAIN (clause) = *list_p;
7291 if (private_debug)
7292 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
7293 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
7294 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
7295 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
7297 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
7298 OMP_CLAUSE_DECL (nc) = decl;
7299 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7300 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
7301 OMP_CLAUSE_DECL (clause)
7302 = build_simple_mem_ref_loc (input_location, decl);
7303 OMP_CLAUSE_DECL (clause)
7304 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
7305 build_int_cst (build_pointer_type (char_type_node), 0));
7306 OMP_CLAUSE_SIZE (clause) = size_zero_node;
7307 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7308 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
7309 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
7310 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7311 OMP_CLAUSE_CHAIN (nc) = *list_p;
7312 OMP_CLAUSE_CHAIN (clause) = nc;
7313 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7314 gimplify_omp_ctxp = ctx->outer_context;
7315 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
7316 pre_p, NULL, is_gimple_val, fb_rvalue);
7317 gimplify_omp_ctxp = ctx;
7319 else if (code == OMP_CLAUSE_MAP)
7321 OMP_CLAUSE_SET_MAP_KIND (clause,
7322 flags & GOVD_MAP_TO_ONLY
7323 ? GOMP_MAP_TO
7324 : GOMP_MAP_TOFROM);
7325 if (DECL_SIZE (decl)
7326 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7328 tree decl2 = DECL_VALUE_EXPR (decl);
7329 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7330 decl2 = TREE_OPERAND (decl2, 0);
7331 gcc_assert (DECL_P (decl2));
7332 tree mem = build_simple_mem_ref (decl2);
7333 OMP_CLAUSE_DECL (clause) = mem;
7334 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7335 if (gimplify_omp_ctxp->outer_context)
7337 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7338 omp_notice_variable (ctx, decl2, true);
7339 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
7341 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7342 OMP_CLAUSE_MAP);
7343 OMP_CLAUSE_DECL (nc) = decl;
7344 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7345 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
7346 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7347 else
7348 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7349 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7350 OMP_CLAUSE_CHAIN (clause) = nc;
7352 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
7353 && lang_hooks.decls.omp_privatize_by_reference (decl))
7355 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
7356 OMP_CLAUSE_SIZE (clause)
7357 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
7358 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7359 gimplify_omp_ctxp = ctx->outer_context;
7360 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
7361 pre_p, NULL, is_gimple_val, fb_rvalue);
7362 gimplify_omp_ctxp = ctx;
7363 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7364 OMP_CLAUSE_MAP);
7365 OMP_CLAUSE_DECL (nc) = decl;
7366 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7367 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7368 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7369 OMP_CLAUSE_CHAIN (clause) = nc;
7371 else
7372 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
7374 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
7376 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
7377 OMP_CLAUSE_DECL (nc) = decl;
7378 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
7379 OMP_CLAUSE_CHAIN (nc) = *list_p;
7380 OMP_CLAUSE_CHAIN (clause) = nc;
7381 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7382 gimplify_omp_ctxp = ctx->outer_context;
7383 lang_hooks.decls.omp_finish_clause (nc, pre_p);
7384 gimplify_omp_ctxp = ctx;
7386 *list_p = clause;
7387 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7388 gimplify_omp_ctxp = ctx->outer_context;
7389 lang_hooks.decls.omp_finish_clause (clause, pre_p);
7390 gimplify_omp_ctxp = ctx;
7391 return 0;
7394 static void
7395 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p,
7396 enum tree_code code)
7398 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7399 tree c, decl;
7401 while ((c = *list_p) != NULL)
7403 splay_tree_node n;
7404 bool remove = false;
7406 switch (OMP_CLAUSE_CODE (c))
7408 case OMP_CLAUSE_PRIVATE:
7409 case OMP_CLAUSE_SHARED:
7410 case OMP_CLAUSE_FIRSTPRIVATE:
7411 case OMP_CLAUSE_LINEAR:
7412 decl = OMP_CLAUSE_DECL (c);
7413 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7414 remove = !(n->value & GOVD_SEEN);
7415 if (! remove)
7417 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
7418 if ((n->value & GOVD_DEBUG_PRIVATE)
7419 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
7421 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
7422 || ((n->value & GOVD_DATA_SHARE_CLASS)
7423 == GOVD_PRIVATE));
7424 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
7425 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
7428 break;
7430 case OMP_CLAUSE_LASTPRIVATE:
7431 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
7432 accurately reflect the presence of a FIRSTPRIVATE clause. */
7433 decl = OMP_CLAUSE_DECL (c);
7434 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7435 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7436 = (n->value & GOVD_FIRSTPRIVATE) != 0;
7437 if (omp_no_lastprivate (ctx))
7439 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7440 remove = true;
7441 else
7442 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
7444 else if (code == OMP_DISTRIBUTE
7445 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7447 remove = true;
7448 error_at (OMP_CLAUSE_LOCATION (c),
7449 "same variable used in %<firstprivate%> and "
7450 "%<lastprivate%> clauses on %<distribute%> "
7451 "construct");
7453 break;
7455 case OMP_CLAUSE_ALIGNED:
7456 decl = OMP_CLAUSE_DECL (c);
7457 if (!is_global_var (decl))
7459 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7460 remove = n == NULL || !(n->value & GOVD_SEEN);
7461 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7463 struct gimplify_omp_ctx *octx;
7464 if (n != NULL
7465 && (n->value & (GOVD_DATA_SHARE_CLASS
7466 & ~GOVD_FIRSTPRIVATE)))
7467 remove = true;
7468 else
7469 for (octx = ctx->outer_context; octx;
7470 octx = octx->outer_context)
7472 n = splay_tree_lookup (octx->variables,
7473 (splay_tree_key) decl);
7474 if (n == NULL)
7475 continue;
7476 if (n->value & GOVD_LOCAL)
7477 break;
7478 /* We have to avoid assigning a shared variable
7479 to itself when trying to add
7480 __builtin_assume_aligned. */
7481 if (n->value & GOVD_SHARED)
7483 remove = true;
7484 break;
7489 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7491 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7492 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7493 remove = true;
7495 break;
7497 case OMP_CLAUSE_MAP:
7498 if (code == OMP_TARGET_EXIT_DATA
7499 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7501 remove = true;
7502 break;
7504 decl = OMP_CLAUSE_DECL (c);
7505 if (!DECL_P (decl))
7507 if ((ctx->region_type & ORT_TARGET) != 0
7508 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7510 if (TREE_CODE (decl) == INDIRECT_REF
7511 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7512 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7513 == REFERENCE_TYPE))
7514 decl = TREE_OPERAND (decl, 0);
7515 if (TREE_CODE (decl) == COMPONENT_REF)
7517 while (TREE_CODE (decl) == COMPONENT_REF)
7518 decl = TREE_OPERAND (decl, 0);
7519 if (DECL_P (decl))
7521 n = splay_tree_lookup (ctx->variables,
7522 (splay_tree_key) decl);
7523 if (!(n->value & GOVD_SEEN))
7524 remove = true;
7528 break;
7530 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7531 if ((ctx->region_type & ORT_TARGET) != 0
7532 && !(n->value & GOVD_SEEN)
7533 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0)
7535 remove = true;
7536 /* For struct element mapping, if struct is never referenced
7537 in target block and none of the mapping has always modifier,
7538 remove all the struct element mappings, which immediately
7539 follow the GOMP_MAP_STRUCT map clause. */
7540 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
7542 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
7543 while (cnt--)
7544 OMP_CLAUSE_CHAIN (c)
7545 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
7548 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
7549 && code == OMP_TARGET_EXIT_DATA)
7550 remove = true;
7551 else if (DECL_SIZE (decl)
7552 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
7553 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
7554 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
7555 && (OMP_CLAUSE_MAP_KIND (c)
7556 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7558 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
7559 for these, TREE_CODE (DECL_SIZE (decl)) will always be
7560 INTEGER_CST. */
7561 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
7563 tree decl2 = DECL_VALUE_EXPR (decl);
7564 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7565 decl2 = TREE_OPERAND (decl2, 0);
7566 gcc_assert (DECL_P (decl2));
7567 tree mem = build_simple_mem_ref (decl2);
7568 OMP_CLAUSE_DECL (c) = mem;
7569 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7570 if (ctx->outer_context)
7572 omp_notice_variable (ctx->outer_context, decl2, true);
7573 omp_notice_variable (ctx->outer_context,
7574 OMP_CLAUSE_SIZE (c), true);
7576 if (((ctx->region_type & ORT_TARGET) != 0
7577 || !ctx->target_firstprivatize_array_bases)
7578 && ((n->value & GOVD_SEEN) == 0
7579 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
7581 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7582 OMP_CLAUSE_MAP);
7583 OMP_CLAUSE_DECL (nc) = decl;
7584 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7585 if (ctx->target_firstprivatize_array_bases)
7586 OMP_CLAUSE_SET_MAP_KIND (nc,
7587 GOMP_MAP_FIRSTPRIVATE_POINTER);
7588 else
7589 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7590 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
7591 OMP_CLAUSE_CHAIN (c) = nc;
7592 c = nc;
7595 else
7597 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7598 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7599 gcc_assert ((n->value & GOVD_SEEN) == 0
7600 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
7601 == 0));
7603 break;
7605 case OMP_CLAUSE_TO:
7606 case OMP_CLAUSE_FROM:
7607 case OMP_CLAUSE__CACHE_:
7608 decl = OMP_CLAUSE_DECL (c);
7609 if (!DECL_P (decl))
7610 break;
7611 if (DECL_SIZE (decl)
7612 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7614 tree decl2 = DECL_VALUE_EXPR (decl);
7615 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7616 decl2 = TREE_OPERAND (decl2, 0);
7617 gcc_assert (DECL_P (decl2));
7618 tree mem = build_simple_mem_ref (decl2);
7619 OMP_CLAUSE_DECL (c) = mem;
7620 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7621 if (ctx->outer_context)
7623 omp_notice_variable (ctx->outer_context, decl2, true);
7624 omp_notice_variable (ctx->outer_context,
7625 OMP_CLAUSE_SIZE (c), true);
7628 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7629 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7630 break;
7632 case OMP_CLAUSE_REDUCTION:
7633 case OMP_CLAUSE_COPYIN:
7634 case OMP_CLAUSE_COPYPRIVATE:
7635 case OMP_CLAUSE_IF:
7636 case OMP_CLAUSE_NUM_THREADS:
7637 case OMP_CLAUSE_NUM_TEAMS:
7638 case OMP_CLAUSE_THREAD_LIMIT:
7639 case OMP_CLAUSE_DIST_SCHEDULE:
7640 case OMP_CLAUSE_DEVICE:
7641 case OMP_CLAUSE_SCHEDULE:
7642 case OMP_CLAUSE_NOWAIT:
7643 case OMP_CLAUSE_ORDERED:
7644 case OMP_CLAUSE_DEFAULT:
7645 case OMP_CLAUSE_UNTIED:
7646 case OMP_CLAUSE_COLLAPSE:
7647 case OMP_CLAUSE_FINAL:
7648 case OMP_CLAUSE_MERGEABLE:
7649 case OMP_CLAUSE_PROC_BIND:
7650 case OMP_CLAUSE_SAFELEN:
7651 case OMP_CLAUSE_SIMDLEN:
7652 case OMP_CLAUSE_DEPEND:
7653 case OMP_CLAUSE_PRIORITY:
7654 case OMP_CLAUSE_GRAINSIZE:
7655 case OMP_CLAUSE_NUM_TASKS:
7656 case OMP_CLAUSE_NOGROUP:
7657 case OMP_CLAUSE_THREADS:
7658 case OMP_CLAUSE_SIMD:
7659 case OMP_CLAUSE_HINT:
7660 case OMP_CLAUSE_DEFAULTMAP:
7661 case OMP_CLAUSE_USE_DEVICE_PTR:
7662 case OMP_CLAUSE_IS_DEVICE_PTR:
7663 case OMP_CLAUSE__CILK_FOR_COUNT_:
7664 case OMP_CLAUSE_ASYNC:
7665 case OMP_CLAUSE_WAIT:
7666 case OMP_CLAUSE_DEVICE_RESIDENT:
7667 case OMP_CLAUSE_USE_DEVICE:
7668 case OMP_CLAUSE_INDEPENDENT:
7669 case OMP_CLAUSE_NUM_GANGS:
7670 case OMP_CLAUSE_NUM_WORKERS:
7671 case OMP_CLAUSE_VECTOR_LENGTH:
7672 case OMP_CLAUSE_GANG:
7673 case OMP_CLAUSE_WORKER:
7674 case OMP_CLAUSE_VECTOR:
7675 case OMP_CLAUSE_AUTO:
7676 case OMP_CLAUSE_SEQ:
7677 case OMP_CLAUSE_TILE:
7678 break;
7680 default:
7681 gcc_unreachable ();
7684 if (remove)
7685 *list_p = OMP_CLAUSE_CHAIN (c);
7686 else
7687 list_p = &OMP_CLAUSE_CHAIN (c);
7690 /* Add in any implicit data sharing. */
7691 struct gimplify_adjust_omp_clauses_data data;
7692 data.list_p = list_p;
7693 data.pre_p = pre_p;
7694 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
7696 gimplify_omp_ctxp = ctx->outer_context;
7697 delete_omp_context (ctx);
7700 /* Gimplify OACC_CACHE. */
7702 static void
7703 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
7705 tree expr = *expr_p;
7707 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE,
7708 OACC_CACHE);
7709 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr), OACC_CACHE);
7711 /* TODO: Do something sensible with this information. */
7713 *expr_p = NULL_TREE;
7716 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
7717 gimplification of the body, as well as scanning the body for used
7718 variables. We need to do this scan now, because variable-sized
7719 decls will be decomposed during gimplification. */
7721 static void
7722 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
7724 tree expr = *expr_p;
7725 gimple *g;
7726 gimple_seq body = NULL;
7728 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
7729 OMP_PARALLEL_COMBINED (expr)
7730 ? ORT_COMBINED_PARALLEL
7731 : ORT_PARALLEL, OMP_PARALLEL);
7733 push_gimplify_context ();
7735 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
7736 if (gimple_code (g) == GIMPLE_BIND)
7737 pop_gimplify_context (g);
7738 else
7739 pop_gimplify_context (NULL);
7741 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr),
7742 OMP_PARALLEL);
7744 g = gimple_build_omp_parallel (body,
7745 OMP_PARALLEL_CLAUSES (expr),
7746 NULL_TREE, NULL_TREE);
7747 if (OMP_PARALLEL_COMBINED (expr))
7748 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
7749 gimplify_seq_add_stmt (pre_p, g);
7750 *expr_p = NULL_TREE;
7753 /* Gimplify the contents of an OMP_TASK statement. This involves
7754 gimplification of the body, as well as scanning the body for used
7755 variables. We need to do this scan now, because variable-sized
7756 decls will be decomposed during gimplification. */
7758 static void
7759 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
7761 tree expr = *expr_p;
7762 gimple *g;
7763 gimple_seq body = NULL;
7765 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
7766 find_omp_clause (OMP_TASK_CLAUSES (expr),
7767 OMP_CLAUSE_UNTIED)
7768 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
7770 push_gimplify_context ();
7772 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
7773 if (gimple_code (g) == GIMPLE_BIND)
7774 pop_gimplify_context (g);
7775 else
7776 pop_gimplify_context (NULL);
7778 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr), OMP_TASK);
7780 g = gimple_build_omp_task (body,
7781 OMP_TASK_CLAUSES (expr),
7782 NULL_TREE, NULL_TREE,
7783 NULL_TREE, NULL_TREE, NULL_TREE);
7784 gimplify_seq_add_stmt (pre_p, g);
7785 *expr_p = NULL_TREE;
7788 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
7789 with non-NULL OMP_FOR_INIT. */
7791 static tree
7792 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
7794 *walk_subtrees = 0;
7795 switch (TREE_CODE (*tp))
7797 case OMP_FOR:
7798 *walk_subtrees = 1;
7799 /* FALLTHRU */
7800 case OMP_SIMD:
7801 if (OMP_FOR_INIT (*tp) != NULL_TREE)
7802 return *tp;
7803 break;
7804 case BIND_EXPR:
7805 case STATEMENT_LIST:
7806 case OMP_PARALLEL:
7807 *walk_subtrees = 1;
7808 break;
7809 default:
7810 break;
7812 return NULL_TREE;
7815 /* Gimplify the gross structure of an OMP_FOR statement. */
7817 static enum gimplify_status
7818 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
7820 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
7821 enum gimplify_status ret = GS_ALL_DONE;
7822 enum gimplify_status tret;
7823 gomp_for *gfor;
7824 gimple_seq for_body, for_pre_body;
7825 int i;
7826 bitmap has_decl_expr = NULL;
7827 enum omp_region_type ort = ORT_WORKSHARE;
7829 orig_for_stmt = for_stmt = *expr_p;
7831 switch (TREE_CODE (for_stmt))
7833 case OMP_FOR:
7834 case CILK_FOR:
7835 case OMP_DISTRIBUTE:
7836 case OACC_LOOP:
7837 break;
7838 case OMP_TASKLOOP:
7839 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
7840 ort = ORT_UNTIED_TASK;
7841 else
7842 ort = ORT_TASK;
7843 break;
7844 case OMP_SIMD:
7845 case CILK_SIMD:
7846 ort = ORT_SIMD;
7847 break;
7848 default:
7849 gcc_unreachable ();
7852 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
7853 clause for the IV. */
7854 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7856 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
7857 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7858 decl = TREE_OPERAND (t, 0);
7859 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
7860 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7861 && OMP_CLAUSE_DECL (c) == decl)
7863 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7864 break;
7868 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7870 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
7871 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
7872 find_combined_omp_for, NULL, NULL);
7873 if (inner_for_stmt == NULL_TREE)
7875 gcc_assert (seen_error ());
7876 *expr_p = NULL_TREE;
7877 return GS_ERROR;
7881 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
7882 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
7883 TREE_CODE (for_stmt));
7885 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
7886 gimplify_omp_ctxp->distribute = true;
7888 /* Handle OMP_FOR_INIT. */
7889 for_pre_body = NULL;
7890 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
7892 has_decl_expr = BITMAP_ALLOC (NULL);
7893 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
7894 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
7895 == VAR_DECL)
7897 t = OMP_FOR_PRE_BODY (for_stmt);
7898 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7900 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
7902 tree_stmt_iterator si;
7903 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
7904 tsi_next (&si))
7906 t = tsi_stmt (si);
7907 if (TREE_CODE (t) == DECL_EXPR
7908 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
7909 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7913 if (OMP_FOR_PRE_BODY (for_stmt))
7915 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
7916 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7917 else
7919 struct gimplify_omp_ctx ctx;
7920 memset (&ctx, 0, sizeof (ctx));
7921 ctx.region_type = ORT_NONE;
7922 gimplify_omp_ctxp = &ctx;
7923 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7924 gimplify_omp_ctxp = NULL;
7927 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
7929 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7930 for_stmt = inner_for_stmt;
7932 /* For taskloop, need to gimplify the start, end and step before the
7933 taskloop, outside of the taskloop omp context. */
7934 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
7936 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7938 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7939 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7941 TREE_OPERAND (t, 1)
7942 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7943 pre_p, NULL);
7944 tree c = build_omp_clause (input_location,
7945 OMP_CLAUSE_FIRSTPRIVATE);
7946 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7947 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7948 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7951 /* Handle OMP_FOR_COND. */
7952 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7953 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7955 TREE_OPERAND (t, 1)
7956 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7957 gimple_seq_empty_p (for_pre_body)
7958 ? pre_p : &for_pre_body, NULL);
7959 tree c = build_omp_clause (input_location,
7960 OMP_CLAUSE_FIRSTPRIVATE);
7961 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7962 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7963 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7966 /* Handle OMP_FOR_INCR. */
7967 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7968 if (TREE_CODE (t) == MODIFY_EXPR)
7970 decl = TREE_OPERAND (t, 0);
7971 t = TREE_OPERAND (t, 1);
7972 tree *tp = &TREE_OPERAND (t, 1);
7973 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
7974 tp = &TREE_OPERAND (t, 0);
7976 if (!is_gimple_constant (*tp))
7978 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
7979 ? pre_p : &for_pre_body;
7980 *tp = get_initialized_tmp_var (*tp, seq, NULL);
7981 tree c = build_omp_clause (input_location,
7982 OMP_CLAUSE_FIRSTPRIVATE);
7983 OMP_CLAUSE_DECL (c) = *tp;
7984 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7985 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7990 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
7991 OMP_TASKLOOP);
7994 if (orig_for_stmt != for_stmt)
7995 gimplify_omp_ctxp->combined_loop = true;
7997 for_body = NULL;
7998 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7999 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
8000 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
8001 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
8003 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
8004 bool is_doacross = false;
8005 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
8007 is_doacross = true;
8008 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
8009 (OMP_FOR_INIT (for_stmt))
8010 * 2);
8012 int collapse = 1;
8013 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
8014 if (c)
8015 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8016 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8018 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8019 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8020 decl = TREE_OPERAND (t, 0);
8021 gcc_assert (DECL_P (decl));
8022 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
8023 || POINTER_TYPE_P (TREE_TYPE (decl)));
8024 if (is_doacross)
8026 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
8027 gimplify_omp_ctxp->loop_iter_var.quick_push
8028 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
8029 else
8030 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
8031 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
8034 /* Make sure the iteration variable is private. */
8035 tree c = NULL_TREE;
8036 tree c2 = NULL_TREE;
8037 if (orig_for_stmt != for_stmt)
8038 /* Do this only on innermost construct for combined ones. */;
8039 else if (ort == ORT_SIMD)
8041 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
8042 (splay_tree_key) decl);
8043 omp_is_private (gimplify_omp_ctxp, decl,
8044 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
8045 != 1));
8046 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8047 omp_notice_variable (gimplify_omp_ctxp, decl, true);
8048 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8050 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8051 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
8052 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
8053 if ((has_decl_expr
8054 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
8055 || omp_no_lastprivate (gimplify_omp_ctxp))
8057 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8058 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8060 struct gimplify_omp_ctx *outer
8061 = gimplify_omp_ctxp->outer_context;
8062 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8064 if (outer->region_type == ORT_WORKSHARE
8065 && outer->combined_loop)
8067 n = splay_tree_lookup (outer->variables,
8068 (splay_tree_key)decl);
8069 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8071 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8072 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8074 else
8076 struct gimplify_omp_ctx *octx = outer->outer_context;
8077 if (octx
8078 && octx->region_type == ORT_COMBINED_PARALLEL
8079 && octx->outer_context
8080 && (octx->outer_context->region_type
8081 == ORT_WORKSHARE)
8082 && octx->outer_context->combined_loop)
8084 octx = octx->outer_context;
8085 n = splay_tree_lookup (octx->variables,
8086 (splay_tree_key)decl);
8087 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8089 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8090 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8097 OMP_CLAUSE_DECL (c) = decl;
8098 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
8099 OMP_FOR_CLAUSES (for_stmt) = c;
8100 omp_add_variable (gimplify_omp_ctxp, decl, flags);
8101 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8103 if (outer->region_type == ORT_WORKSHARE
8104 && outer->combined_loop)
8106 if (outer->outer_context
8107 && (outer->outer_context->region_type
8108 == ORT_COMBINED_PARALLEL))
8109 outer = outer->outer_context;
8110 else if (omp_check_private (outer, decl, false))
8111 outer = NULL;
8113 else if (((outer->region_type & ORT_TASK) != 0)
8114 && outer->combined_loop
8115 && !omp_check_private (gimplify_omp_ctxp,
8116 decl, false))
8118 else if (outer->region_type != ORT_COMBINED_PARALLEL)
8119 outer = NULL;
8120 if (outer)
8122 n = splay_tree_lookup (outer->variables,
8123 (splay_tree_key)decl);
8124 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8126 omp_add_variable (outer, decl,
8127 GOVD_LASTPRIVATE | GOVD_SEEN);
8128 if (outer->region_type == ORT_COMBINED_PARALLEL
8129 && outer->outer_context
8130 && (outer->outer_context->region_type
8131 == ORT_WORKSHARE)
8132 && outer->outer_context->combined_loop)
8134 outer = outer->outer_context;
8135 n = splay_tree_lookup (outer->variables,
8136 (splay_tree_key)decl);
8137 if (omp_check_private (outer, decl, false))
8138 outer = NULL;
8139 else if (n == NULL
8140 || ((n->value & GOVD_DATA_SHARE_CLASS)
8141 == 0))
8142 omp_add_variable (outer, decl,
8143 GOVD_LASTPRIVATE
8144 | GOVD_SEEN);
8145 else
8146 outer = NULL;
8148 if (outer && outer->outer_context
8149 && (outer->outer_context->region_type
8150 == ORT_COMBINED_TEAMS))
8152 outer = outer->outer_context;
8153 n = splay_tree_lookup (outer->variables,
8154 (splay_tree_key)decl);
8155 if (n == NULL
8156 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8157 omp_add_variable (outer, decl,
8158 GOVD_SHARED | GOVD_SEEN);
8159 else
8160 outer = NULL;
8162 if (outer && outer->outer_context)
8163 omp_notice_variable (outer->outer_context, decl,
8164 true);
8169 else
8171 bool lastprivate
8172 = (!has_decl_expr
8173 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
8174 && !omp_no_lastprivate (gimplify_omp_ctxp);
8175 struct gimplify_omp_ctx *outer
8176 = gimplify_omp_ctxp->outer_context;
8177 if (outer && lastprivate)
8179 if (outer->region_type == ORT_WORKSHARE
8180 && outer->combined_loop)
8182 n = splay_tree_lookup (outer->variables,
8183 (splay_tree_key)decl);
8184 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8186 lastprivate = false;
8187 outer = NULL;
8189 else if (outer->outer_context
8190 && (outer->outer_context->region_type
8191 == ORT_COMBINED_PARALLEL))
8192 outer = outer->outer_context;
8193 else if (omp_check_private (outer, decl, false))
8194 outer = NULL;
8196 else if (((outer->region_type & ORT_TASK) != 0)
8197 && outer->combined_loop
8198 && !omp_check_private (gimplify_omp_ctxp,
8199 decl, false))
8201 else if (outer->region_type != ORT_COMBINED_PARALLEL)
8202 outer = NULL;
8203 if (outer)
8205 n = splay_tree_lookup (outer->variables,
8206 (splay_tree_key)decl);
8207 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8209 omp_add_variable (outer, decl,
8210 GOVD_LASTPRIVATE | GOVD_SEEN);
8211 if (outer->region_type == ORT_COMBINED_PARALLEL
8212 && outer->outer_context
8213 && (outer->outer_context->region_type
8214 == ORT_WORKSHARE)
8215 && outer->outer_context->combined_loop)
8217 outer = outer->outer_context;
8218 n = splay_tree_lookup (outer->variables,
8219 (splay_tree_key)decl);
8220 if (omp_check_private (outer, decl, false))
8221 outer = NULL;
8222 else if (n == NULL
8223 || ((n->value & GOVD_DATA_SHARE_CLASS)
8224 == 0))
8225 omp_add_variable (outer, decl,
8226 GOVD_LASTPRIVATE
8227 | GOVD_SEEN);
8228 else
8229 outer = NULL;
8231 if (outer && outer->outer_context
8232 && (outer->outer_context->region_type
8233 == ORT_COMBINED_TEAMS))
8235 outer = outer->outer_context;
8236 n = splay_tree_lookup (outer->variables,
8237 (splay_tree_key)decl);
8238 if (n == NULL
8239 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8240 omp_add_variable (outer, decl,
8241 GOVD_SHARED | GOVD_SEEN);
8242 else
8243 outer = NULL;
8245 if (outer && outer->outer_context)
8246 omp_notice_variable (outer->outer_context, decl,
8247 true);
8252 c = build_omp_clause (input_location,
8253 lastprivate ? OMP_CLAUSE_LASTPRIVATE
8254 : OMP_CLAUSE_PRIVATE);
8255 OMP_CLAUSE_DECL (c) = decl;
8256 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
8257 OMP_FOR_CLAUSES (for_stmt) = c;
8258 omp_add_variable (gimplify_omp_ctxp, decl,
8259 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
8260 | GOVD_EXPLICIT | GOVD_SEEN);
8261 c = NULL_TREE;
8264 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
8265 omp_notice_variable (gimplify_omp_ctxp, decl, true);
8266 else
8267 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
8269 /* If DECL is not a gimple register, create a temporary variable to act
8270 as an iteration counter. This is valid, since DECL cannot be
8271 modified in the body of the loop. Similarly for any iteration vars
8272 in simd with collapse > 1 where the iterator vars must be
8273 lastprivate. */
8274 if (orig_for_stmt != for_stmt)
8275 var = decl;
8276 else if (!is_gimple_reg (decl)
8277 || (ort == ORT_SIMD
8278 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
8280 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8281 TREE_OPERAND (t, 0) = var;
8283 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
8285 if (ort == ORT_SIMD
8286 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8288 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8289 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
8290 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
8291 OMP_CLAUSE_DECL (c2) = var;
8292 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
8293 OMP_FOR_CLAUSES (for_stmt) = c2;
8294 omp_add_variable (gimplify_omp_ctxp, var,
8295 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
8296 if (c == NULL_TREE)
8298 c = c2;
8299 c2 = NULL_TREE;
8302 else
8303 omp_add_variable (gimplify_omp_ctxp, var,
8304 GOVD_PRIVATE | GOVD_SEEN);
8306 else
8307 var = decl;
8309 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8310 is_gimple_val, fb_rvalue);
8311 ret = MIN (ret, tret);
8312 if (ret == GS_ERROR)
8313 return ret;
8315 /* Handle OMP_FOR_COND. */
8316 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8317 gcc_assert (COMPARISON_CLASS_P (t));
8318 gcc_assert (TREE_OPERAND (t, 0) == decl);
8320 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8321 is_gimple_val, fb_rvalue);
8322 ret = MIN (ret, tret);
8324 /* Handle OMP_FOR_INCR. */
8325 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8326 switch (TREE_CODE (t))
8328 case PREINCREMENT_EXPR:
8329 case POSTINCREMENT_EXPR:
8331 tree decl = TREE_OPERAND (t, 0);
8332 /* c_omp_for_incr_canonicalize_ptr() should have been
8333 called to massage things appropriately. */
8334 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8336 if (orig_for_stmt != for_stmt)
8337 break;
8338 t = build_int_cst (TREE_TYPE (decl), 1);
8339 if (c)
8340 OMP_CLAUSE_LINEAR_STEP (c) = t;
8341 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8342 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8343 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8344 break;
8347 case PREDECREMENT_EXPR:
8348 case POSTDECREMENT_EXPR:
8349 /* c_omp_for_incr_canonicalize_ptr() should have been
8350 called to massage things appropriately. */
8351 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8352 if (orig_for_stmt != for_stmt)
8353 break;
8354 t = build_int_cst (TREE_TYPE (decl), -1);
8355 if (c)
8356 OMP_CLAUSE_LINEAR_STEP (c) = t;
8357 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8358 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8359 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8360 break;
8362 case MODIFY_EXPR:
8363 gcc_assert (TREE_OPERAND (t, 0) == decl);
8364 TREE_OPERAND (t, 0) = var;
8366 t = TREE_OPERAND (t, 1);
8367 switch (TREE_CODE (t))
8369 case PLUS_EXPR:
8370 if (TREE_OPERAND (t, 1) == decl)
8372 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
8373 TREE_OPERAND (t, 0) = var;
8374 break;
8377 /* Fallthru. */
8378 case MINUS_EXPR:
8379 case POINTER_PLUS_EXPR:
8380 gcc_assert (TREE_OPERAND (t, 0) == decl);
8381 TREE_OPERAND (t, 0) = var;
8382 break;
8383 default:
8384 gcc_unreachable ();
8387 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8388 is_gimple_val, fb_rvalue);
8389 ret = MIN (ret, tret);
8390 if (c)
8392 tree step = TREE_OPERAND (t, 1);
8393 tree stept = TREE_TYPE (decl);
8394 if (POINTER_TYPE_P (stept))
8395 stept = sizetype;
8396 step = fold_convert (stept, step);
8397 if (TREE_CODE (t) == MINUS_EXPR)
8398 step = fold_build1 (NEGATE_EXPR, stept, step);
8399 OMP_CLAUSE_LINEAR_STEP (c) = step;
8400 if (step != TREE_OPERAND (t, 1))
8402 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
8403 &for_pre_body, NULL,
8404 is_gimple_val, fb_rvalue);
8405 ret = MIN (ret, tret);
8408 break;
8410 default:
8411 gcc_unreachable ();
8414 if (c2)
8416 gcc_assert (c);
8417 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
8420 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
8422 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
8423 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8424 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
8425 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8426 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
8427 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
8428 && OMP_CLAUSE_DECL (c) == decl)
8430 if (is_doacross && (collapse == 1 || i >= collapse))
8431 t = var;
8432 else
8434 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8435 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8436 gcc_assert (TREE_OPERAND (t, 0) == var);
8437 t = TREE_OPERAND (t, 1);
8438 gcc_assert (TREE_CODE (t) == PLUS_EXPR
8439 || TREE_CODE (t) == MINUS_EXPR
8440 || TREE_CODE (t) == POINTER_PLUS_EXPR);
8441 gcc_assert (TREE_OPERAND (t, 0) == var);
8442 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
8443 is_doacross ? var : decl,
8444 TREE_OPERAND (t, 1));
8446 gimple_seq *seq;
8447 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8448 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
8449 else
8450 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
8451 gimplify_assign (decl, t, seq);
8456 BITMAP_FREE (has_decl_expr);
8458 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8460 push_gimplify_context ();
8461 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
8463 OMP_FOR_BODY (orig_for_stmt)
8464 = build3 (BIND_EXPR, void_type_node, NULL,
8465 OMP_FOR_BODY (orig_for_stmt), NULL);
8466 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
8470 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
8471 &for_body);
8473 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8475 if (gimple_code (g) == GIMPLE_BIND)
8476 pop_gimplify_context (g);
8477 else
8478 pop_gimplify_context (NULL);
8481 if (orig_for_stmt != for_stmt)
8482 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8484 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8485 decl = TREE_OPERAND (t, 0);
8486 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8487 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8488 gimplify_omp_ctxp = ctx->outer_context;
8489 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8490 gimplify_omp_ctxp = ctx;
8491 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
8492 TREE_OPERAND (t, 0) = var;
8493 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8494 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
8495 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
8498 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt),
8499 TREE_CODE (orig_for_stmt));
8501 int kind;
8502 switch (TREE_CODE (orig_for_stmt))
8504 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
8505 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
8506 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
8507 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
8508 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
8509 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
8510 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
8511 default:
8512 gcc_unreachable ();
8514 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
8515 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
8516 for_pre_body);
8517 if (orig_for_stmt != for_stmt)
8518 gimple_omp_for_set_combined_p (gfor, true);
8519 if (gimplify_omp_ctxp
8520 && (gimplify_omp_ctxp->combined_loop
8521 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
8522 && gimplify_omp_ctxp->outer_context
8523 && gimplify_omp_ctxp->outer_context->combined_loop)))
8525 gimple_omp_for_set_combined_into_p (gfor, true);
8526 if (gimplify_omp_ctxp->combined_loop)
8527 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
8528 else
8529 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
8532 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8534 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8535 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
8536 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
8537 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8538 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
8539 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
8540 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8541 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
8544 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
8545 constructs with GIMPLE_OMP_TASK sandwiched in between them.
8546 The outer taskloop stands for computing the number of iterations,
8547 counts for collapsed loops and holding taskloop specific clauses.
8548 The task construct stands for the effect of data sharing on the
8549 explicit task it creates and the inner taskloop stands for expansion
8550 of the static loop inside of the explicit task construct. */
8551 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8553 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
8554 tree task_clauses = NULL_TREE;
8555 tree c = *gfor_clauses_ptr;
8556 tree *gtask_clauses_ptr = &task_clauses;
8557 tree outer_for_clauses = NULL_TREE;
8558 tree *gforo_clauses_ptr = &outer_for_clauses;
8559 for (; c; c = OMP_CLAUSE_CHAIN (c))
8560 switch (OMP_CLAUSE_CODE (c))
8562 /* These clauses are allowed on task, move them there. */
8563 case OMP_CLAUSE_SHARED:
8564 case OMP_CLAUSE_FIRSTPRIVATE:
8565 case OMP_CLAUSE_DEFAULT:
8566 case OMP_CLAUSE_IF:
8567 case OMP_CLAUSE_UNTIED:
8568 case OMP_CLAUSE_FINAL:
8569 case OMP_CLAUSE_MERGEABLE:
8570 case OMP_CLAUSE_PRIORITY:
8571 *gtask_clauses_ptr = c;
8572 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8573 break;
8574 case OMP_CLAUSE_PRIVATE:
8575 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
8577 /* We want private on outer for and firstprivate
8578 on task. */
8579 *gtask_clauses_ptr
8580 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8581 OMP_CLAUSE_FIRSTPRIVATE);
8582 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8583 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8584 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8585 *gforo_clauses_ptr = c;
8586 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8588 else
8590 *gtask_clauses_ptr = c;
8591 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8593 break;
8594 /* These clauses go into outer taskloop clauses. */
8595 case OMP_CLAUSE_GRAINSIZE:
8596 case OMP_CLAUSE_NUM_TASKS:
8597 case OMP_CLAUSE_NOGROUP:
8598 *gforo_clauses_ptr = c;
8599 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8600 break;
8601 /* Taskloop clause we duplicate on both taskloops. */
8602 case OMP_CLAUSE_COLLAPSE:
8603 *gfor_clauses_ptr = c;
8604 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8605 *gforo_clauses_ptr = copy_node (c);
8606 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8607 break;
8608 /* For lastprivate, keep the clause on inner taskloop, and add
8609 a shared clause on task. If the same decl is also firstprivate,
8610 add also firstprivate clause on the inner taskloop. */
8611 case OMP_CLAUSE_LASTPRIVATE:
8612 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
8614 /* For taskloop C++ lastprivate IVs, we want:
8615 1) private on outer taskloop
8616 2) firstprivate and shared on task
8617 3) lastprivate on inner taskloop */
8618 *gtask_clauses_ptr
8619 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8620 OMP_CLAUSE_FIRSTPRIVATE);
8621 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8622 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8623 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8624 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
8625 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8626 OMP_CLAUSE_PRIVATE);
8627 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
8628 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
8629 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
8630 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8632 *gfor_clauses_ptr = c;
8633 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8634 *gtask_clauses_ptr
8635 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
8636 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8637 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8638 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
8639 gtask_clauses_ptr
8640 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8641 break;
8642 default:
8643 gcc_unreachable ();
8645 *gfor_clauses_ptr = NULL_TREE;
8646 *gtask_clauses_ptr = NULL_TREE;
8647 *gforo_clauses_ptr = NULL_TREE;
8648 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
8649 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
8650 NULL_TREE, NULL_TREE, NULL_TREE);
8651 gimple_omp_task_set_taskloop_p (g, true);
8652 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
8653 gomp_for *gforo
8654 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
8655 gimple_omp_for_collapse (gfor),
8656 gimple_omp_for_pre_body (gfor));
8657 gimple_omp_for_set_pre_body (gfor, NULL);
8658 gimple_omp_for_set_combined_p (gforo, true);
8659 gimple_omp_for_set_combined_into_p (gfor, true);
8660 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
8662 t = unshare_expr (gimple_omp_for_index (gfor, i));
8663 gimple_omp_for_set_index (gforo, i, t);
8664 t = unshare_expr (gimple_omp_for_initial (gfor, i));
8665 gimple_omp_for_set_initial (gforo, i, t);
8666 gimple_omp_for_set_cond (gforo, i,
8667 gimple_omp_for_cond (gfor, i));
8668 t = unshare_expr (gimple_omp_for_final (gfor, i));
8669 gimple_omp_for_set_final (gforo, i, t);
8670 t = unshare_expr (gimple_omp_for_incr (gfor, i));
8671 gimple_omp_for_set_incr (gforo, i, t);
8673 gimplify_seq_add_stmt (pre_p, gforo);
8675 else
8676 gimplify_seq_add_stmt (pre_p, gfor);
8677 if (ret != GS_ALL_DONE)
8678 return GS_ERROR;
8679 *expr_p = NULL_TREE;
8680 return GS_ALL_DONE;
8683 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
8684 of OMP_TARGET's body. */
8686 static tree
8687 find_omp_teams (tree *tp, int *walk_subtrees, void *)
8689 *walk_subtrees = 0;
8690 switch (TREE_CODE (*tp))
8692 case OMP_TEAMS:
8693 return *tp;
8694 case BIND_EXPR:
8695 case STATEMENT_LIST:
8696 *walk_subtrees = 1;
8697 break;
8698 default:
8699 break;
8701 return NULL_TREE;
8704 /* Helper function of optimize_target_teams, determine if the expression
8705 can be computed safely before the target construct on the host. */
8707 static tree
8708 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
8710 splay_tree_node n;
8712 if (TYPE_P (*tp))
8714 *walk_subtrees = 0;
8715 return NULL_TREE;
8717 switch (TREE_CODE (*tp))
8719 case VAR_DECL:
8720 case PARM_DECL:
8721 case RESULT_DECL:
8722 *walk_subtrees = 0;
8723 if (error_operand_p (*tp)
8724 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
8725 || DECL_HAS_VALUE_EXPR_P (*tp)
8726 || DECL_THREAD_LOCAL_P (*tp)
8727 || TREE_SIDE_EFFECTS (*tp)
8728 || TREE_THIS_VOLATILE (*tp))
8729 return *tp;
8730 if (is_global_var (*tp)
8731 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
8732 || lookup_attribute ("omp declare target link",
8733 DECL_ATTRIBUTES (*tp))))
8734 return *tp;
8735 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
8736 (splay_tree_key) *tp);
8737 if (n == NULL)
8739 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
8740 return NULL_TREE;
8741 return *tp;
8743 else if (n->value & GOVD_LOCAL)
8744 return *tp;
8745 else if (n->value & GOVD_FIRSTPRIVATE)
8746 return NULL_TREE;
8747 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
8748 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
8749 return NULL_TREE;
8750 return *tp;
8751 case INTEGER_CST:
8752 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
8753 return *tp;
8754 return NULL_TREE;
8755 case TARGET_EXPR:
8756 if (TARGET_EXPR_INITIAL (*tp)
8757 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
8758 return *tp;
8759 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
8760 walk_subtrees, NULL);
8761 /* Allow some reasonable subset of integral arithmetics. */
8762 case PLUS_EXPR:
8763 case MINUS_EXPR:
8764 case MULT_EXPR:
8765 case TRUNC_DIV_EXPR:
8766 case CEIL_DIV_EXPR:
8767 case FLOOR_DIV_EXPR:
8768 case ROUND_DIV_EXPR:
8769 case TRUNC_MOD_EXPR:
8770 case CEIL_MOD_EXPR:
8771 case FLOOR_MOD_EXPR:
8772 case ROUND_MOD_EXPR:
8773 case RDIV_EXPR:
8774 case EXACT_DIV_EXPR:
8775 case MIN_EXPR:
8776 case MAX_EXPR:
8777 case LSHIFT_EXPR:
8778 case RSHIFT_EXPR:
8779 case BIT_IOR_EXPR:
8780 case BIT_XOR_EXPR:
8781 case BIT_AND_EXPR:
8782 case NEGATE_EXPR:
8783 case ABS_EXPR:
8784 case BIT_NOT_EXPR:
8785 case NON_LVALUE_EXPR:
8786 CASE_CONVERT:
8787 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
8788 return *tp;
8789 return NULL_TREE;
8790 /* And disallow anything else, except for comparisons. */
8791 default:
8792 if (COMPARISON_CLASS_P (*tp))
8793 return NULL_TREE;
8794 return *tp;
8798 /* Try to determine if the num_teams and/or thread_limit expressions
8799 can have their values determined already before entering the
8800 target construct.
8801 INTEGER_CSTs trivially are,
8802 integral decls that are firstprivate (explicitly or implicitly)
8803 or explicitly map(always, to:) or map(always, tofrom:) on the target
8804 region too, and expressions involving simple arithmetics on those
8805 too, function calls are not ok, dereferencing something neither etc.
8806 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
8807 EXPR based on what we find:
8808 0 stands for clause not specified at all, use implementation default
8809 -1 stands for value that can't be determined easily before entering
8810 the target construct.
8811 If teams construct is not present at all, use 1 for num_teams
8812 and 0 for thread_limit (only one team is involved, and the thread
8813 limit is implementation defined. */
8815 static void
8816 optimize_target_teams (tree target, gimple_seq *pre_p)
8818 tree body = OMP_BODY (target);
8819 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
8820 tree num_teams = integer_zero_node;
8821 tree thread_limit = integer_zero_node;
8822 location_t num_teams_loc = EXPR_LOCATION (target);
8823 location_t thread_limit_loc = EXPR_LOCATION (target);
8824 tree c, *p, expr;
8825 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
8827 if (teams == NULL_TREE)
8828 num_teams = integer_one_node;
8829 else
8830 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
8832 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
8834 p = &num_teams;
8835 num_teams_loc = OMP_CLAUSE_LOCATION (c);
8837 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
8839 p = &thread_limit;
8840 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
8842 else
8843 continue;
8844 expr = OMP_CLAUSE_OPERAND (c, 0);
8845 if (TREE_CODE (expr) == INTEGER_CST)
8847 *p = expr;
8848 continue;
8850 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
8852 *p = integer_minus_one_node;
8853 continue;
8855 *p = expr;
8856 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
8857 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue)
8858 == GS_ERROR)
8860 gimplify_omp_ctxp = target_ctx;
8861 *p = integer_minus_one_node;
8862 continue;
8864 gimplify_omp_ctxp = target_ctx;
8865 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
8866 OMP_CLAUSE_OPERAND (c, 0) = *p;
8868 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
8869 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
8870 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
8871 OMP_TARGET_CLAUSES (target) = c;
8872 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
8873 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
8874 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
8875 OMP_TARGET_CLAUSES (target) = c;
8878 /* Gimplify the gross structure of several OMP constructs. */
8880 static void
8881 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
8883 tree expr = *expr_p;
8884 gimple *stmt;
8885 gimple_seq body = NULL;
8886 enum omp_region_type ort;
8888 switch (TREE_CODE (expr))
8890 case OMP_SECTIONS:
8891 case OMP_SINGLE:
8892 ort = ORT_WORKSHARE;
8893 break;
8894 case OMP_TARGET:
8895 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
8896 break;
8897 case OACC_KERNELS:
8898 case OACC_PARALLEL:
8899 ort = ORT_TARGET;
8900 break;
8901 case OACC_DATA:
8902 case OMP_TARGET_DATA:
8903 ort = ORT_TARGET_DATA;
8904 break;
8905 case OMP_TEAMS:
8906 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
8907 break;
8908 default:
8909 gcc_unreachable ();
8911 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
8912 TREE_CODE (expr));
8913 if (TREE_CODE (expr) == OMP_TARGET)
8914 optimize_target_teams (expr, pre_p);
8915 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
8917 push_gimplify_context ();
8918 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
8919 if (gimple_code (g) == GIMPLE_BIND)
8920 pop_gimplify_context (g);
8921 else
8922 pop_gimplify_context (NULL);
8923 if (ort == ORT_TARGET_DATA)
8925 enum built_in_function end_ix;
8926 switch (TREE_CODE (expr))
8928 case OACC_DATA:
8929 end_ix = BUILT_IN_GOACC_DATA_END;
8930 break;
8931 case OMP_TARGET_DATA:
8932 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
8933 break;
8934 default:
8935 gcc_unreachable ();
8937 tree fn = builtin_decl_explicit (end_ix);
8938 g = gimple_build_call (fn, 0);
8939 gimple_seq cleanup = NULL;
8940 gimple_seq_add_stmt (&cleanup, g);
8941 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8942 body = NULL;
8943 gimple_seq_add_stmt (&body, g);
8946 else
8947 gimplify_and_add (OMP_BODY (expr), &body);
8948 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr), TREE_CODE (expr));
8950 switch (TREE_CODE (expr))
8952 case OACC_DATA:
8953 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
8954 OMP_CLAUSES (expr));
8955 break;
8956 case OACC_KERNELS:
8957 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
8958 OMP_CLAUSES (expr));
8959 break;
8960 case OACC_PARALLEL:
8961 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
8962 OMP_CLAUSES (expr));
8963 break;
8964 case OMP_SECTIONS:
8965 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
8966 break;
8967 case OMP_SINGLE:
8968 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
8969 break;
8970 case OMP_TARGET:
8971 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
8972 OMP_CLAUSES (expr));
8973 break;
8974 case OMP_TARGET_DATA:
8975 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
8976 OMP_CLAUSES (expr));
8977 break;
8978 case OMP_TEAMS:
8979 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
8980 break;
8981 default:
8982 gcc_unreachable ();
8985 gimplify_seq_add_stmt (pre_p, stmt);
8986 *expr_p = NULL_TREE;
8989 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
8990 target update constructs. */
8992 static void
8993 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
8995 tree expr = *expr_p;
8996 int kind;
8997 gomp_target *stmt;
8999 switch (TREE_CODE (expr))
9001 case OACC_ENTER_DATA:
9002 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
9003 break;
9004 case OACC_EXIT_DATA:
9005 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
9006 break;
9007 case OACC_UPDATE:
9008 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
9009 break;
9010 case OMP_TARGET_UPDATE:
9011 kind = GF_OMP_TARGET_KIND_UPDATE;
9012 break;
9013 case OMP_TARGET_ENTER_DATA:
9014 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
9015 break;
9016 case OMP_TARGET_EXIT_DATA:
9017 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
9018 break;
9019 default:
9020 gcc_unreachable ();
9022 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
9023 ORT_WORKSHARE, TREE_CODE (expr));
9024 gimplify_adjust_omp_clauses (pre_p, &OMP_STANDALONE_CLAUSES (expr),
9025 TREE_CODE (expr));
9026 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
9028 gimplify_seq_add_stmt (pre_p, stmt);
9029 *expr_p = NULL_TREE;
9032 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
9033 stabilized the lhs of the atomic operation as *ADDR. Return true if
9034 EXPR is this stabilized form. */
9036 static bool
9037 goa_lhs_expr_p (tree expr, tree addr)
9039 /* Also include casts to other type variants. The C front end is fond
9040 of adding these for e.g. volatile variables. This is like
9041 STRIP_TYPE_NOPS but includes the main variant lookup. */
9042 STRIP_USELESS_TYPE_CONVERSION (expr);
9044 if (TREE_CODE (expr) == INDIRECT_REF)
9046 expr = TREE_OPERAND (expr, 0);
9047 while (expr != addr
9048 && (CONVERT_EXPR_P (expr)
9049 || TREE_CODE (expr) == NON_LVALUE_EXPR)
9050 && TREE_CODE (expr) == TREE_CODE (addr)
9051 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
9053 expr = TREE_OPERAND (expr, 0);
9054 addr = TREE_OPERAND (addr, 0);
9056 if (expr == addr)
9057 return true;
9058 return (TREE_CODE (addr) == ADDR_EXPR
9059 && TREE_CODE (expr) == ADDR_EXPR
9060 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
9062 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
9063 return true;
9064 return false;
9067 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
9068 expression does not involve the lhs, evaluate it into a temporary.
9069 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
9070 or -1 if an error was encountered. */
9072 static int
9073 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
9074 tree lhs_var)
9076 tree expr = *expr_p;
9077 int saw_lhs;
9079 if (goa_lhs_expr_p (expr, lhs_addr))
9081 *expr_p = lhs_var;
9082 return 1;
9084 if (is_gimple_val (expr))
9085 return 0;
9087 saw_lhs = 0;
9088 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
9090 case tcc_binary:
9091 case tcc_comparison:
9092 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
9093 lhs_var);
9094 case tcc_unary:
9095 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
9096 lhs_var);
9097 break;
9098 case tcc_expression:
9099 switch (TREE_CODE (expr))
9101 case TRUTH_ANDIF_EXPR:
9102 case TRUTH_ORIF_EXPR:
9103 case TRUTH_AND_EXPR:
9104 case TRUTH_OR_EXPR:
9105 case TRUTH_XOR_EXPR:
9106 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
9107 lhs_addr, lhs_var);
9108 case TRUTH_NOT_EXPR:
9109 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
9110 lhs_addr, lhs_var);
9111 break;
9112 case COMPOUND_EXPR:
9113 /* Break out any preevaluations from cp_build_modify_expr. */
9114 for (; TREE_CODE (expr) == COMPOUND_EXPR;
9115 expr = TREE_OPERAND (expr, 1))
9116 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
9117 *expr_p = expr;
9118 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
9119 default:
9120 break;
9122 break;
9123 default:
9124 break;
9127 if (saw_lhs == 0)
9129 enum gimplify_status gs;
9130 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
9131 if (gs != GS_ALL_DONE)
9132 saw_lhs = -1;
9135 return saw_lhs;
9138 /* Gimplify an OMP_ATOMIC statement. */
9140 static enum gimplify_status
9141 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
9143 tree addr = TREE_OPERAND (*expr_p, 0);
9144 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
9145 ? NULL : TREE_OPERAND (*expr_p, 1);
9146 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
9147 tree tmp_load;
9148 gomp_atomic_load *loadstmt;
9149 gomp_atomic_store *storestmt;
9151 tmp_load = create_tmp_reg (type);
9152 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
9153 return GS_ERROR;
9155 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
9156 != GS_ALL_DONE)
9157 return GS_ERROR;
9159 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
9160 gimplify_seq_add_stmt (pre_p, loadstmt);
9161 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
9162 != GS_ALL_DONE)
9163 return GS_ERROR;
9165 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
9166 rhs = tmp_load;
9167 storestmt = gimple_build_omp_atomic_store (rhs);
9168 gimplify_seq_add_stmt (pre_p, storestmt);
9169 if (OMP_ATOMIC_SEQ_CST (*expr_p))
9171 gimple_omp_atomic_set_seq_cst (loadstmt);
9172 gimple_omp_atomic_set_seq_cst (storestmt);
9174 switch (TREE_CODE (*expr_p))
9176 case OMP_ATOMIC_READ:
9177 case OMP_ATOMIC_CAPTURE_OLD:
9178 *expr_p = tmp_load;
9179 gimple_omp_atomic_set_need_value (loadstmt);
9180 break;
9181 case OMP_ATOMIC_CAPTURE_NEW:
9182 *expr_p = rhs;
9183 gimple_omp_atomic_set_need_value (storestmt);
9184 break;
9185 default:
9186 *expr_p = NULL;
9187 break;
9190 return GS_ALL_DONE;
9193 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
9194 body, and adding some EH bits. */
9196 static enum gimplify_status
9197 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
9199 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
9200 gimple *body_stmt;
9201 gtransaction *trans_stmt;
9202 gimple_seq body = NULL;
9203 int subcode = 0;
9205 /* Wrap the transaction body in a BIND_EXPR so we have a context
9206 where to put decls for OMP. */
9207 if (TREE_CODE (tbody) != BIND_EXPR)
9209 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
9210 TREE_SIDE_EFFECTS (bind) = 1;
9211 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
9212 TRANSACTION_EXPR_BODY (expr) = bind;
9215 push_gimplify_context ();
9216 temp = voidify_wrapper_expr (*expr_p, NULL);
9218 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
9219 pop_gimplify_context (body_stmt);
9221 trans_stmt = gimple_build_transaction (body, NULL);
9222 if (TRANSACTION_EXPR_OUTER (expr))
9223 subcode = GTMA_IS_OUTER;
9224 else if (TRANSACTION_EXPR_RELAXED (expr))
9225 subcode = GTMA_IS_RELAXED;
9226 gimple_transaction_set_subcode (trans_stmt, subcode);
9228 gimplify_seq_add_stmt (pre_p, trans_stmt);
9230 if (temp)
9232 *expr_p = temp;
9233 return GS_OK;
9236 *expr_p = NULL_TREE;
9237 return GS_ALL_DONE;
9240 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
9241 is the OMP_BODY of the original EXPR (which has already been
9242 gimplified so it's not present in the EXPR).
9244 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
9246 static gimple *
9247 gimplify_omp_ordered (tree expr, gimple_seq body)
9249 tree c, decls;
9250 int failures = 0;
9251 unsigned int i;
9252 tree source_c = NULL_TREE;
9253 tree sink_c = NULL_TREE;
9255 if (gimplify_omp_ctxp)
9256 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
9257 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9258 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
9259 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
9260 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
9262 error_at (OMP_CLAUSE_LOCATION (c),
9263 "%<ordered%> construct with %<depend%> clause must be "
9264 "closely nested inside a loop with %<ordered%> clause "
9265 "with a parameter");
9266 failures++;
9268 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9269 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
9271 bool fail = false;
9272 for (decls = OMP_CLAUSE_DECL (c), i = 0;
9273 decls && TREE_CODE (decls) == TREE_LIST;
9274 decls = TREE_CHAIN (decls), ++i)
9275 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
9276 continue;
9277 else if (TREE_VALUE (decls)
9278 != gimplify_omp_ctxp->loop_iter_var[2 * i])
9280 error_at (OMP_CLAUSE_LOCATION (c),
9281 "variable %qE is not an iteration "
9282 "of outermost loop %d, expected %qE",
9283 TREE_VALUE (decls), i + 1,
9284 gimplify_omp_ctxp->loop_iter_var[2 * i]);
9285 fail = true;
9286 failures++;
9288 else
9289 TREE_VALUE (decls)
9290 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
9291 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
9293 error_at (OMP_CLAUSE_LOCATION (c),
9294 "number of variables in %<depend(sink)%> "
9295 "clause does not match number of "
9296 "iteration variables");
9297 failures++;
9299 sink_c = c;
9301 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9302 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
9304 if (source_c)
9306 error_at (OMP_CLAUSE_LOCATION (c),
9307 "more than one %<depend(source)%> clause on an "
9308 "%<ordered%> construct");
9309 failures++;
9311 else
9312 source_c = c;
9314 if (source_c && sink_c)
9316 error_at (OMP_CLAUSE_LOCATION (source_c),
9317 "%<depend(source)%> clause specified together with "
9318 "%<depend(sink:)%> clauses on the same construct");
9319 failures++;
9322 if (failures)
9323 return gimple_build_nop ();
9324 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
9327 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
9328 expression produces a value to be used as an operand inside a GIMPLE
9329 statement, the value will be stored back in *EXPR_P. This value will
9330 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
9331 an SSA_NAME. The corresponding sequence of GIMPLE statements is
9332 emitted in PRE_P and POST_P.
9334 Additionally, this process may overwrite parts of the input
9335 expression during gimplification. Ideally, it should be
9336 possible to do non-destructive gimplification.
9338 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
9339 the expression needs to evaluate to a value to be used as
9340 an operand in a GIMPLE statement, this value will be stored in
9341 *EXPR_P on exit. This happens when the caller specifies one
9342 of fb_lvalue or fb_rvalue fallback flags.
9344 PRE_P will contain the sequence of GIMPLE statements corresponding
9345 to the evaluation of EXPR and all the side-effects that must
9346 be executed before the main expression. On exit, the last
9347 statement of PRE_P is the core statement being gimplified. For
9348 instance, when gimplifying 'if (++a)' the last statement in
9349 PRE_P will be 'if (t.1)' where t.1 is the result of
9350 pre-incrementing 'a'.
9352 POST_P will contain the sequence of GIMPLE statements corresponding
9353 to the evaluation of all the side-effects that must be executed
9354 after the main expression. If this is NULL, the post
9355 side-effects are stored at the end of PRE_P.
9357 The reason why the output is split in two is to handle post
9358 side-effects explicitly. In some cases, an expression may have
9359 inner and outer post side-effects which need to be emitted in
9360 an order different from the one given by the recursive
9361 traversal. For instance, for the expression (*p--)++ the post
9362 side-effects of '--' must actually occur *after* the post
9363 side-effects of '++'. However, gimplification will first visit
9364 the inner expression, so if a separate POST sequence was not
9365 used, the resulting sequence would be:
9367 1 t.1 = *p
9368 2 p = p - 1
9369 3 t.2 = t.1 + 1
9370 4 *p = t.2
9372 However, the post-decrement operation in line #2 must not be
9373 evaluated until after the store to *p at line #4, so the
9374 correct sequence should be:
9376 1 t.1 = *p
9377 2 t.2 = t.1 + 1
9378 3 *p = t.2
9379 4 p = p - 1
9381 So, by specifying a separate post queue, it is possible
9382 to emit the post side-effects in the correct order.
9383 If POST_P is NULL, an internal queue will be used. Before
9384 returning to the caller, the sequence POST_P is appended to
9385 the main output sequence PRE_P.
9387 GIMPLE_TEST_F points to a function that takes a tree T and
9388 returns nonzero if T is in the GIMPLE form requested by the
9389 caller. The GIMPLE predicates are in gimple.c.
9391 FALLBACK tells the function what sort of a temporary we want if
9392 gimplification cannot produce an expression that complies with
9393 GIMPLE_TEST_F.
9395 fb_none means that no temporary should be generated
9396 fb_rvalue means that an rvalue is OK to generate
9397 fb_lvalue means that an lvalue is OK to generate
9398 fb_either means that either is OK, but an lvalue is preferable.
9399 fb_mayfail means that gimplification may fail (in which case
9400 GS_ERROR will be returned)
9402 The return value is either GS_ERROR or GS_ALL_DONE, since this
9403 function iterates until EXPR is completely gimplified or an error
9404 occurs. */
9406 enum gimplify_status
9407 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
9408 bool (*gimple_test_f) (tree), fallback_t fallback)
9410 tree tmp;
9411 gimple_seq internal_pre = NULL;
9412 gimple_seq internal_post = NULL;
9413 tree save_expr;
9414 bool is_statement;
9415 location_t saved_location;
9416 enum gimplify_status ret;
9417 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
9419 save_expr = *expr_p;
9420 if (save_expr == NULL_TREE)
9421 return GS_ALL_DONE;
9423 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
9424 is_statement = gimple_test_f == is_gimple_stmt;
9425 if (is_statement)
9426 gcc_assert (pre_p);
9428 /* Consistency checks. */
9429 if (gimple_test_f == is_gimple_reg)
9430 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
9431 else if (gimple_test_f == is_gimple_val
9432 || gimple_test_f == is_gimple_call_addr
9433 || gimple_test_f == is_gimple_condexpr
9434 || gimple_test_f == is_gimple_mem_rhs
9435 || gimple_test_f == is_gimple_mem_rhs_or_call
9436 || gimple_test_f == is_gimple_reg_rhs
9437 || gimple_test_f == is_gimple_reg_rhs_or_call
9438 || gimple_test_f == is_gimple_asm_val
9439 || gimple_test_f == is_gimple_mem_ref_addr)
9440 gcc_assert (fallback & fb_rvalue);
9441 else if (gimple_test_f == is_gimple_min_lval
9442 || gimple_test_f == is_gimple_lvalue)
9443 gcc_assert (fallback & fb_lvalue);
9444 else if (gimple_test_f == is_gimple_addressable)
9445 gcc_assert (fallback & fb_either);
9446 else if (gimple_test_f == is_gimple_stmt)
9447 gcc_assert (fallback == fb_none);
9448 else
9450 /* We should have recognized the GIMPLE_TEST_F predicate to
9451 know what kind of fallback to use in case a temporary is
9452 needed to hold the value or address of *EXPR_P. */
9453 gcc_unreachable ();
9456 /* We used to check the predicate here and return immediately if it
9457 succeeds. This is wrong; the design is for gimplification to be
9458 idempotent, and for the predicates to only test for valid forms, not
9459 whether they are fully simplified. */
9460 if (pre_p == NULL)
9461 pre_p = &internal_pre;
9463 if (post_p == NULL)
9464 post_p = &internal_post;
9466 /* Remember the last statements added to PRE_P and POST_P. Every
9467 new statement added by the gimplification helpers needs to be
9468 annotated with location information. To centralize the
9469 responsibility, we remember the last statement that had been
9470 added to both queues before gimplifying *EXPR_P. If
9471 gimplification produces new statements in PRE_P and POST_P, those
9472 statements will be annotated with the same location information
9473 as *EXPR_P. */
9474 pre_last_gsi = gsi_last (*pre_p);
9475 post_last_gsi = gsi_last (*post_p);
9477 saved_location = input_location;
9478 if (save_expr != error_mark_node
9479 && EXPR_HAS_LOCATION (*expr_p))
9480 input_location = EXPR_LOCATION (*expr_p);
9482 /* Loop over the specific gimplifiers until the toplevel node
9483 remains the same. */
9486 /* Strip away as many useless type conversions as possible
9487 at the toplevel. */
9488 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
9490 /* Remember the expr. */
9491 save_expr = *expr_p;
9493 /* Die, die, die, my darling. */
9494 if (save_expr == error_mark_node
9495 || (TREE_TYPE (save_expr)
9496 && TREE_TYPE (save_expr) == error_mark_node))
9498 ret = GS_ERROR;
9499 break;
9502 /* Do any language-specific gimplification. */
9503 ret = ((enum gimplify_status)
9504 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
9505 if (ret == GS_OK)
9507 if (*expr_p == NULL_TREE)
9508 break;
9509 if (*expr_p != save_expr)
9510 continue;
9512 else if (ret != GS_UNHANDLED)
9513 break;
9515 /* Make sure that all the cases set 'ret' appropriately. */
9516 ret = GS_UNHANDLED;
9517 switch (TREE_CODE (*expr_p))
9519 /* First deal with the special cases. */
9521 case POSTINCREMENT_EXPR:
9522 case POSTDECREMENT_EXPR:
9523 case PREINCREMENT_EXPR:
9524 case PREDECREMENT_EXPR:
9525 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
9526 fallback != fb_none,
9527 TREE_TYPE (*expr_p));
9528 break;
9530 case VIEW_CONVERT_EXPR:
9531 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
9532 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
9534 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9535 post_p, is_gimple_val, fb_rvalue);
9536 recalculate_side_effects (*expr_p);
9537 break;
9539 /* Fallthru. */
9541 case ARRAY_REF:
9542 case ARRAY_RANGE_REF:
9543 case REALPART_EXPR:
9544 case IMAGPART_EXPR:
9545 case COMPONENT_REF:
9546 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
9547 fallback ? fallback : fb_rvalue);
9548 break;
9550 case COND_EXPR:
9551 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
9553 /* C99 code may assign to an array in a structure value of a
9554 conditional expression, and this has undefined behavior
9555 only on execution, so create a temporary if an lvalue is
9556 required. */
9557 if (fallback == fb_lvalue)
9559 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9560 mark_addressable (*expr_p);
9561 ret = GS_OK;
9563 break;
9565 case CALL_EXPR:
9566 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
9568 /* C99 code may assign to an array in a structure returned
9569 from a function, and this has undefined behavior only on
9570 execution, so create a temporary if an lvalue is
9571 required. */
9572 if (fallback == fb_lvalue)
9574 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9575 mark_addressable (*expr_p);
9576 ret = GS_OK;
9578 break;
9580 case TREE_LIST:
9581 gcc_unreachable ();
9583 case COMPOUND_EXPR:
9584 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
9585 break;
9587 case COMPOUND_LITERAL_EXPR:
9588 ret = gimplify_compound_literal_expr (expr_p, pre_p,
9589 gimple_test_f, fallback);
9590 break;
9592 case MODIFY_EXPR:
9593 case INIT_EXPR:
9594 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
9595 fallback != fb_none);
9596 break;
9598 case TRUTH_ANDIF_EXPR:
9599 case TRUTH_ORIF_EXPR:
9601 /* Preserve the original type of the expression and the
9602 source location of the outer expression. */
9603 tree org_type = TREE_TYPE (*expr_p);
9604 *expr_p = gimple_boolify (*expr_p);
9605 *expr_p = build3_loc (input_location, COND_EXPR,
9606 org_type, *expr_p,
9607 fold_convert_loc
9608 (input_location,
9609 org_type, boolean_true_node),
9610 fold_convert_loc
9611 (input_location,
9612 org_type, boolean_false_node));
9613 ret = GS_OK;
9614 break;
9617 case TRUTH_NOT_EXPR:
9619 tree type = TREE_TYPE (*expr_p);
9620 /* The parsers are careful to generate TRUTH_NOT_EXPR
9621 only with operands that are always zero or one.
9622 We do not fold here but handle the only interesting case
9623 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
9624 *expr_p = gimple_boolify (*expr_p);
9625 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
9626 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
9627 TREE_TYPE (*expr_p),
9628 TREE_OPERAND (*expr_p, 0));
9629 else
9630 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
9631 TREE_TYPE (*expr_p),
9632 TREE_OPERAND (*expr_p, 0),
9633 build_int_cst (TREE_TYPE (*expr_p), 1));
9634 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
9635 *expr_p = fold_convert_loc (input_location, type, *expr_p);
9636 ret = GS_OK;
9637 break;
9640 case ADDR_EXPR:
9641 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
9642 break;
9644 case ANNOTATE_EXPR:
9646 tree cond = TREE_OPERAND (*expr_p, 0);
9647 tree kind = TREE_OPERAND (*expr_p, 1);
9648 tree type = TREE_TYPE (cond);
9649 if (!INTEGRAL_TYPE_P (type))
9651 *expr_p = cond;
9652 ret = GS_OK;
9653 break;
9655 tree tmp = create_tmp_var (type);
9656 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
9657 gcall *call
9658 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
9659 gimple_call_set_lhs (call, tmp);
9660 gimplify_seq_add_stmt (pre_p, call);
9661 *expr_p = tmp;
9662 ret = GS_ALL_DONE;
9663 break;
9666 case VA_ARG_EXPR:
9667 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
9668 break;
9670 CASE_CONVERT:
9671 if (IS_EMPTY_STMT (*expr_p))
9673 ret = GS_ALL_DONE;
9674 break;
9677 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
9678 || fallback == fb_none)
9680 /* Just strip a conversion to void (or in void context) and
9681 try again. */
9682 *expr_p = TREE_OPERAND (*expr_p, 0);
9683 ret = GS_OK;
9684 break;
9687 ret = gimplify_conversion (expr_p);
9688 if (ret == GS_ERROR)
9689 break;
9690 if (*expr_p != save_expr)
9691 break;
9692 /* FALLTHRU */
9694 case FIX_TRUNC_EXPR:
9695 /* unary_expr: ... | '(' cast ')' val | ... */
9696 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9697 is_gimple_val, fb_rvalue);
9698 recalculate_side_effects (*expr_p);
9699 break;
9701 case INDIRECT_REF:
9703 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
9704 bool notrap = TREE_THIS_NOTRAP (*expr_p);
9705 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
9707 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
9708 if (*expr_p != save_expr)
9710 ret = GS_OK;
9711 break;
9714 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9715 is_gimple_reg, fb_rvalue);
9716 if (ret == GS_ERROR)
9717 break;
9719 recalculate_side_effects (*expr_p);
9720 *expr_p = fold_build2_loc (input_location, MEM_REF,
9721 TREE_TYPE (*expr_p),
9722 TREE_OPERAND (*expr_p, 0),
9723 build_int_cst (saved_ptr_type, 0));
9724 TREE_THIS_VOLATILE (*expr_p) = volatilep;
9725 TREE_THIS_NOTRAP (*expr_p) = notrap;
9726 ret = GS_OK;
9727 break;
9730 /* We arrive here through the various re-gimplifcation paths. */
9731 case MEM_REF:
9732 /* First try re-folding the whole thing. */
9733 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
9734 TREE_OPERAND (*expr_p, 0),
9735 TREE_OPERAND (*expr_p, 1));
9736 if (tmp)
9738 REF_REVERSE_STORAGE_ORDER (tmp)
9739 = REF_REVERSE_STORAGE_ORDER (*expr_p);
9740 *expr_p = tmp;
9741 recalculate_side_effects (*expr_p);
9742 ret = GS_OK;
9743 break;
9745 /* Avoid re-gimplifying the address operand if it is already
9746 in suitable form. Re-gimplifying would mark the address
9747 operand addressable. Always gimplify when not in SSA form
9748 as we still may have to gimplify decls with value-exprs. */
9749 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
9750 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
9752 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9753 is_gimple_mem_ref_addr, fb_rvalue);
9754 if (ret == GS_ERROR)
9755 break;
9757 recalculate_side_effects (*expr_p);
9758 ret = GS_ALL_DONE;
9759 break;
9761 /* Constants need not be gimplified. */
9762 case INTEGER_CST:
9763 case REAL_CST:
9764 case FIXED_CST:
9765 case STRING_CST:
9766 case COMPLEX_CST:
9767 case VECTOR_CST:
9768 /* Drop the overflow flag on constants, we do not want
9769 that in the GIMPLE IL. */
9770 if (TREE_OVERFLOW_P (*expr_p))
9771 *expr_p = drop_tree_overflow (*expr_p);
9772 ret = GS_ALL_DONE;
9773 break;
9775 case CONST_DECL:
9776 /* If we require an lvalue, such as for ADDR_EXPR, retain the
9777 CONST_DECL node. Otherwise the decl is replaceable by its
9778 value. */
9779 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
9780 if (fallback & fb_lvalue)
9781 ret = GS_ALL_DONE;
9782 else
9784 *expr_p = DECL_INITIAL (*expr_p);
9785 ret = GS_OK;
9787 break;
9789 case DECL_EXPR:
9790 ret = gimplify_decl_expr (expr_p, pre_p);
9791 break;
9793 case BIND_EXPR:
9794 ret = gimplify_bind_expr (expr_p, pre_p);
9795 break;
9797 case LOOP_EXPR:
9798 ret = gimplify_loop_expr (expr_p, pre_p);
9799 break;
9801 case SWITCH_EXPR:
9802 ret = gimplify_switch_expr (expr_p, pre_p);
9803 break;
9805 case EXIT_EXPR:
9806 ret = gimplify_exit_expr (expr_p);
9807 break;
9809 case GOTO_EXPR:
9810 /* If the target is not LABEL, then it is a computed jump
9811 and the target needs to be gimplified. */
9812 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
9814 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
9815 NULL, is_gimple_val, fb_rvalue);
9816 if (ret == GS_ERROR)
9817 break;
9819 gimplify_seq_add_stmt (pre_p,
9820 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
9821 ret = GS_ALL_DONE;
9822 break;
9824 case PREDICT_EXPR:
9825 gimplify_seq_add_stmt (pre_p,
9826 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
9827 PREDICT_EXPR_OUTCOME (*expr_p)));
9828 ret = GS_ALL_DONE;
9829 break;
9831 case LABEL_EXPR:
9832 ret = GS_ALL_DONE;
9833 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
9834 == current_function_decl);
9835 gimplify_seq_add_stmt (pre_p,
9836 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
9837 break;
9839 case CASE_LABEL_EXPR:
9840 ret = gimplify_case_label_expr (expr_p, pre_p);
9841 break;
9843 case RETURN_EXPR:
9844 ret = gimplify_return_expr (*expr_p, pre_p);
9845 break;
9847 case CONSTRUCTOR:
9848 /* Don't reduce this in place; let gimplify_init_constructor work its
9849 magic. Buf if we're just elaborating this for side effects, just
9850 gimplify any element that has side-effects. */
9851 if (fallback == fb_none)
9853 unsigned HOST_WIDE_INT ix;
9854 tree val;
9855 tree temp = NULL_TREE;
9856 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
9857 if (TREE_SIDE_EFFECTS (val))
9858 append_to_statement_list (val, &temp);
9860 *expr_p = temp;
9861 ret = temp ? GS_OK : GS_ALL_DONE;
9863 /* C99 code may assign to an array in a constructed
9864 structure or union, and this has undefined behavior only
9865 on execution, so create a temporary if an lvalue is
9866 required. */
9867 else if (fallback == fb_lvalue)
9869 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9870 mark_addressable (*expr_p);
9871 ret = GS_OK;
9873 else
9874 ret = GS_ALL_DONE;
9875 break;
9877 /* The following are special cases that are not handled by the
9878 original GIMPLE grammar. */
9880 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
9881 eliminated. */
9882 case SAVE_EXPR:
9883 ret = gimplify_save_expr (expr_p, pre_p, post_p);
9884 break;
9886 case BIT_FIELD_REF:
9887 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9888 post_p, is_gimple_lvalue, fb_either);
9889 recalculate_side_effects (*expr_p);
9890 break;
9892 case TARGET_MEM_REF:
9894 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
9896 if (TMR_BASE (*expr_p))
9897 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
9898 post_p, is_gimple_mem_ref_addr, fb_either);
9899 if (TMR_INDEX (*expr_p))
9900 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
9901 post_p, is_gimple_val, fb_rvalue);
9902 if (TMR_INDEX2 (*expr_p))
9903 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
9904 post_p, is_gimple_val, fb_rvalue);
9905 /* TMR_STEP and TMR_OFFSET are always integer constants. */
9906 ret = MIN (r0, r1);
9908 break;
9910 case NON_LVALUE_EXPR:
9911 /* This should have been stripped above. */
9912 gcc_unreachable ();
9914 case ASM_EXPR:
9915 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
9916 break;
9918 case TRY_FINALLY_EXPR:
9919 case TRY_CATCH_EXPR:
9921 gimple_seq eval, cleanup;
9922 gtry *try_;
9924 /* Calls to destructors are generated automatically in FINALLY/CATCH
9925 block. They should have location as UNKNOWN_LOCATION. However,
9926 gimplify_call_expr will reset these call stmts to input_location
9927 if it finds stmt's location is unknown. To prevent resetting for
9928 destructors, we set the input_location to unknown.
9929 Note that this only affects the destructor calls in FINALLY/CATCH
9930 block, and will automatically reset to its original value by the
9931 end of gimplify_expr. */
9932 input_location = UNKNOWN_LOCATION;
9933 eval = cleanup = NULL;
9934 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
9935 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
9936 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
9937 if (gimple_seq_empty_p (cleanup))
9939 gimple_seq_add_seq (pre_p, eval);
9940 ret = GS_ALL_DONE;
9941 break;
9943 try_ = gimple_build_try (eval, cleanup,
9944 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
9945 ? GIMPLE_TRY_FINALLY
9946 : GIMPLE_TRY_CATCH);
9947 if (EXPR_HAS_LOCATION (save_expr))
9948 gimple_set_location (try_, EXPR_LOCATION (save_expr));
9949 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
9950 gimple_set_location (try_, saved_location);
9951 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
9952 gimple_try_set_catch_is_cleanup (try_,
9953 TRY_CATCH_IS_CLEANUP (*expr_p));
9954 gimplify_seq_add_stmt (pre_p, try_);
9955 ret = GS_ALL_DONE;
9956 break;
9959 case CLEANUP_POINT_EXPR:
9960 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
9961 break;
9963 case TARGET_EXPR:
9964 ret = gimplify_target_expr (expr_p, pre_p, post_p);
9965 break;
9967 case CATCH_EXPR:
9969 gimple *c;
9970 gimple_seq handler = NULL;
9971 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
9972 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
9973 gimplify_seq_add_stmt (pre_p, c);
9974 ret = GS_ALL_DONE;
9975 break;
9978 case EH_FILTER_EXPR:
9980 gimple *ehf;
9981 gimple_seq failure = NULL;
9983 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
9984 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
9985 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
9986 gimplify_seq_add_stmt (pre_p, ehf);
9987 ret = GS_ALL_DONE;
9988 break;
9991 case OBJ_TYPE_REF:
9993 enum gimplify_status r0, r1;
9994 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
9995 post_p, is_gimple_val, fb_rvalue);
9996 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
9997 post_p, is_gimple_val, fb_rvalue);
9998 TREE_SIDE_EFFECTS (*expr_p) = 0;
9999 ret = MIN (r0, r1);
10001 break;
10003 case LABEL_DECL:
10004 /* We get here when taking the address of a label. We mark
10005 the label as "forced"; meaning it can never be removed and
10006 it is a potential target for any computed goto. */
10007 FORCED_LABEL (*expr_p) = 1;
10008 ret = GS_ALL_DONE;
10009 break;
10011 case STATEMENT_LIST:
10012 ret = gimplify_statement_list (expr_p, pre_p);
10013 break;
10015 case WITH_SIZE_EXPR:
10017 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10018 post_p == &internal_post ? NULL : post_p,
10019 gimple_test_f, fallback);
10020 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
10021 is_gimple_val, fb_rvalue);
10022 ret = GS_ALL_DONE;
10024 break;
10026 case VAR_DECL:
10027 case PARM_DECL:
10028 ret = gimplify_var_or_parm_decl (expr_p);
10029 break;
10031 case RESULT_DECL:
10032 /* When within an OMP context, notice uses of variables. */
10033 if (gimplify_omp_ctxp)
10034 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
10035 ret = GS_ALL_DONE;
10036 break;
10038 case SSA_NAME:
10039 /* Allow callbacks into the gimplifier during optimization. */
10040 ret = GS_ALL_DONE;
10041 break;
10043 case OMP_PARALLEL:
10044 gimplify_omp_parallel (expr_p, pre_p);
10045 ret = GS_ALL_DONE;
10046 break;
10048 case OMP_TASK:
10049 gimplify_omp_task (expr_p, pre_p);
10050 ret = GS_ALL_DONE;
10051 break;
10053 case OMP_FOR:
10054 case OMP_SIMD:
10055 case CILK_SIMD:
10056 case CILK_FOR:
10057 case OMP_DISTRIBUTE:
10058 case OMP_TASKLOOP:
10059 case OACC_LOOP:
10060 ret = gimplify_omp_for (expr_p, pre_p);
10061 break;
10063 case OACC_CACHE:
10064 gimplify_oacc_cache (expr_p, pre_p);
10065 ret = GS_ALL_DONE;
10066 break;
10068 case OACC_HOST_DATA:
10069 case OACC_DECLARE:
10070 sorry ("directive not yet implemented");
10071 ret = GS_ALL_DONE;
10072 break;
10074 case OACC_DATA:
10075 case OACC_KERNELS:
10076 case OACC_PARALLEL:
10077 case OMP_SECTIONS:
10078 case OMP_SINGLE:
10079 case OMP_TARGET:
10080 case OMP_TARGET_DATA:
10081 case OMP_TEAMS:
10082 gimplify_omp_workshare (expr_p, pre_p);
10083 ret = GS_ALL_DONE;
10084 break;
10086 case OACC_ENTER_DATA:
10087 case OACC_EXIT_DATA:
10088 case OACC_UPDATE:
10089 case OMP_TARGET_UPDATE:
10090 case OMP_TARGET_ENTER_DATA:
10091 case OMP_TARGET_EXIT_DATA:
10092 gimplify_omp_target_update (expr_p, pre_p);
10093 ret = GS_ALL_DONE;
10094 break;
10096 case OMP_SECTION:
10097 case OMP_MASTER:
10098 case OMP_TASKGROUP:
10099 case OMP_ORDERED:
10100 case OMP_CRITICAL:
10102 gimple_seq body = NULL;
10103 gimple *g;
10105 gimplify_and_add (OMP_BODY (*expr_p), &body);
10106 switch (TREE_CODE (*expr_p))
10108 case OMP_SECTION:
10109 g = gimple_build_omp_section (body);
10110 break;
10111 case OMP_MASTER:
10112 g = gimple_build_omp_master (body);
10113 break;
10114 case OMP_TASKGROUP:
10116 gimple_seq cleanup = NULL;
10117 tree fn
10118 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
10119 g = gimple_build_call (fn, 0);
10120 gimple_seq_add_stmt (&cleanup, g);
10121 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10122 body = NULL;
10123 gimple_seq_add_stmt (&body, g);
10124 g = gimple_build_omp_taskgroup (body);
10126 break;
10127 case OMP_ORDERED:
10128 g = gimplify_omp_ordered (*expr_p, body);
10129 break;
10130 case OMP_CRITICAL:
10131 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
10132 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
10133 gimplify_adjust_omp_clauses (pre_p,
10134 &OMP_CRITICAL_CLAUSES (*expr_p),
10135 OMP_CRITICAL);
10136 g = gimple_build_omp_critical (body,
10137 OMP_CRITICAL_NAME (*expr_p),
10138 OMP_CRITICAL_CLAUSES (*expr_p));
10139 break;
10140 default:
10141 gcc_unreachable ();
10143 gimplify_seq_add_stmt (pre_p, g);
10144 ret = GS_ALL_DONE;
10145 break;
10148 case OMP_ATOMIC:
10149 case OMP_ATOMIC_READ:
10150 case OMP_ATOMIC_CAPTURE_OLD:
10151 case OMP_ATOMIC_CAPTURE_NEW:
10152 ret = gimplify_omp_atomic (expr_p, pre_p);
10153 break;
10155 case TRANSACTION_EXPR:
10156 ret = gimplify_transaction (expr_p, pre_p);
10157 break;
10159 case TRUTH_AND_EXPR:
10160 case TRUTH_OR_EXPR:
10161 case TRUTH_XOR_EXPR:
10163 tree orig_type = TREE_TYPE (*expr_p);
10164 tree new_type, xop0, xop1;
10165 *expr_p = gimple_boolify (*expr_p);
10166 new_type = TREE_TYPE (*expr_p);
10167 if (!useless_type_conversion_p (orig_type, new_type))
10169 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
10170 ret = GS_OK;
10171 break;
10174 /* Boolified binary truth expressions are semantically equivalent
10175 to bitwise binary expressions. Canonicalize them to the
10176 bitwise variant. */
10177 switch (TREE_CODE (*expr_p))
10179 case TRUTH_AND_EXPR:
10180 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
10181 break;
10182 case TRUTH_OR_EXPR:
10183 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
10184 break;
10185 case TRUTH_XOR_EXPR:
10186 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
10187 break;
10188 default:
10189 break;
10191 /* Now make sure that operands have compatible type to
10192 expression's new_type. */
10193 xop0 = TREE_OPERAND (*expr_p, 0);
10194 xop1 = TREE_OPERAND (*expr_p, 1);
10195 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
10196 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
10197 new_type,
10198 xop0);
10199 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
10200 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
10201 new_type,
10202 xop1);
10203 /* Continue classified as tcc_binary. */
10204 goto expr_2;
10207 case FMA_EXPR:
10208 case VEC_COND_EXPR:
10209 case VEC_PERM_EXPR:
10210 /* Classified as tcc_expression. */
10211 goto expr_3;
10213 case POINTER_PLUS_EXPR:
10215 enum gimplify_status r0, r1;
10216 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10217 post_p, is_gimple_val, fb_rvalue);
10218 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10219 post_p, is_gimple_val, fb_rvalue);
10220 recalculate_side_effects (*expr_p);
10221 ret = MIN (r0, r1);
10222 break;
10225 case CILK_SYNC_STMT:
10227 if (!fn_contains_cilk_spawn_p (cfun))
10229 error_at (EXPR_LOCATION (*expr_p),
10230 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
10231 ret = GS_ERROR;
10233 else
10235 gimplify_cilk_sync (expr_p, pre_p);
10236 ret = GS_ALL_DONE;
10238 break;
10241 default:
10242 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
10244 case tcc_comparison:
10245 /* Handle comparison of objects of non scalar mode aggregates
10246 with a call to memcmp. It would be nice to only have to do
10247 this for variable-sized objects, but then we'd have to allow
10248 the same nest of reference nodes we allow for MODIFY_EXPR and
10249 that's too complex.
10251 Compare scalar mode aggregates as scalar mode values. Using
10252 memcmp for them would be very inefficient at best, and is
10253 plain wrong if bitfields are involved. */
10255 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
10257 /* Vector comparisons need no boolification. */
10258 if (TREE_CODE (type) == VECTOR_TYPE)
10259 goto expr_2;
10260 else if (!AGGREGATE_TYPE_P (type))
10262 tree org_type = TREE_TYPE (*expr_p);
10263 *expr_p = gimple_boolify (*expr_p);
10264 if (!useless_type_conversion_p (org_type,
10265 TREE_TYPE (*expr_p)))
10267 *expr_p = fold_convert_loc (input_location,
10268 org_type, *expr_p);
10269 ret = GS_OK;
10271 else
10272 goto expr_2;
10274 else if (TYPE_MODE (type) != BLKmode)
10275 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
10276 else
10277 ret = gimplify_variable_sized_compare (expr_p);
10279 break;
10282 /* If *EXPR_P does not need to be special-cased, handle it
10283 according to its class. */
10284 case tcc_unary:
10285 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10286 post_p, is_gimple_val, fb_rvalue);
10287 break;
10289 case tcc_binary:
10290 expr_2:
10292 enum gimplify_status r0, r1;
10294 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10295 post_p, is_gimple_val, fb_rvalue);
10296 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10297 post_p, is_gimple_val, fb_rvalue);
10299 ret = MIN (r0, r1);
10300 break;
10303 expr_3:
10305 enum gimplify_status r0, r1, r2;
10307 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10308 post_p, is_gimple_val, fb_rvalue);
10309 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10310 post_p, is_gimple_val, fb_rvalue);
10311 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
10312 post_p, is_gimple_val, fb_rvalue);
10314 ret = MIN (MIN (r0, r1), r2);
10315 break;
10318 case tcc_declaration:
10319 case tcc_constant:
10320 ret = GS_ALL_DONE;
10321 goto dont_recalculate;
10323 default:
10324 gcc_unreachable ();
10327 recalculate_side_effects (*expr_p);
10329 dont_recalculate:
10330 break;
10333 gcc_assert (*expr_p || ret != GS_OK);
10335 while (ret == GS_OK);
10337 /* If we encountered an error_mark somewhere nested inside, either
10338 stub out the statement or propagate the error back out. */
10339 if (ret == GS_ERROR)
10341 if (is_statement)
10342 *expr_p = NULL;
10343 goto out;
10346 /* This was only valid as a return value from the langhook, which
10347 we handled. Make sure it doesn't escape from any other context. */
10348 gcc_assert (ret != GS_UNHANDLED);
10350 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
10352 /* We aren't looking for a value, and we don't have a valid
10353 statement. If it doesn't have side-effects, throw it away. */
10354 if (!TREE_SIDE_EFFECTS (*expr_p))
10355 *expr_p = NULL;
10356 else if (!TREE_THIS_VOLATILE (*expr_p))
10358 /* This is probably a _REF that contains something nested that
10359 has side effects. Recurse through the operands to find it. */
10360 enum tree_code code = TREE_CODE (*expr_p);
10362 switch (code)
10364 case COMPONENT_REF:
10365 case REALPART_EXPR:
10366 case IMAGPART_EXPR:
10367 case VIEW_CONVERT_EXPR:
10368 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10369 gimple_test_f, fallback);
10370 break;
10372 case ARRAY_REF:
10373 case ARRAY_RANGE_REF:
10374 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10375 gimple_test_f, fallback);
10376 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
10377 gimple_test_f, fallback);
10378 break;
10380 default:
10381 /* Anything else with side-effects must be converted to
10382 a valid statement before we get here. */
10383 gcc_unreachable ();
10386 *expr_p = NULL;
10388 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
10389 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
10391 /* Historically, the compiler has treated a bare reference
10392 to a non-BLKmode volatile lvalue as forcing a load. */
10393 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
10395 /* Normally, we do not want to create a temporary for a
10396 TREE_ADDRESSABLE type because such a type should not be
10397 copied by bitwise-assignment. However, we make an
10398 exception here, as all we are doing here is ensuring that
10399 we read the bytes that make up the type. We use
10400 create_tmp_var_raw because create_tmp_var will abort when
10401 given a TREE_ADDRESSABLE type. */
10402 tree tmp = create_tmp_var_raw (type, "vol");
10403 gimple_add_tmp_var (tmp);
10404 gimplify_assign (tmp, *expr_p, pre_p);
10405 *expr_p = NULL;
10407 else
10408 /* We can't do anything useful with a volatile reference to
10409 an incomplete type, so just throw it away. Likewise for
10410 a BLKmode type, since any implicit inner load should
10411 already have been turned into an explicit one by the
10412 gimplification process. */
10413 *expr_p = NULL;
10416 /* If we are gimplifying at the statement level, we're done. Tack
10417 everything together and return. */
10418 if (fallback == fb_none || is_statement)
10420 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
10421 it out for GC to reclaim it. */
10422 *expr_p = NULL_TREE;
10424 if (!gimple_seq_empty_p (internal_pre)
10425 || !gimple_seq_empty_p (internal_post))
10427 gimplify_seq_add_seq (&internal_pre, internal_post);
10428 gimplify_seq_add_seq (pre_p, internal_pre);
10431 /* The result of gimplifying *EXPR_P is going to be the last few
10432 statements in *PRE_P and *POST_P. Add location information
10433 to all the statements that were added by the gimplification
10434 helpers. */
10435 if (!gimple_seq_empty_p (*pre_p))
10436 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
10438 if (!gimple_seq_empty_p (*post_p))
10439 annotate_all_with_location_after (*post_p, post_last_gsi,
10440 input_location);
10442 goto out;
10445 #ifdef ENABLE_GIMPLE_CHECKING
10446 if (*expr_p)
10448 enum tree_code code = TREE_CODE (*expr_p);
10449 /* These expressions should already be in gimple IR form. */
10450 gcc_assert (code != MODIFY_EXPR
10451 && code != ASM_EXPR
10452 && code != BIND_EXPR
10453 && code != CATCH_EXPR
10454 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
10455 && code != EH_FILTER_EXPR
10456 && code != GOTO_EXPR
10457 && code != LABEL_EXPR
10458 && code != LOOP_EXPR
10459 && code != SWITCH_EXPR
10460 && code != TRY_FINALLY_EXPR
10461 && code != OACC_PARALLEL
10462 && code != OACC_KERNELS
10463 && code != OACC_DATA
10464 && code != OACC_HOST_DATA
10465 && code != OACC_DECLARE
10466 && code != OACC_UPDATE
10467 && code != OACC_ENTER_DATA
10468 && code != OACC_EXIT_DATA
10469 && code != OACC_CACHE
10470 && code != OMP_CRITICAL
10471 && code != OMP_FOR
10472 && code != OACC_LOOP
10473 && code != OMP_MASTER
10474 && code != OMP_TASKGROUP
10475 && code != OMP_ORDERED
10476 && code != OMP_PARALLEL
10477 && code != OMP_SECTIONS
10478 && code != OMP_SECTION
10479 && code != OMP_SINGLE);
10481 #endif
10483 /* Otherwise we're gimplifying a subexpression, so the resulting
10484 value is interesting. If it's a valid operand that matches
10485 GIMPLE_TEST_F, we're done. Unless we are handling some
10486 post-effects internally; if that's the case, we need to copy into
10487 a temporary before adding the post-effects to POST_P. */
10488 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
10489 goto out;
10491 /* Otherwise, we need to create a new temporary for the gimplified
10492 expression. */
10494 /* We can't return an lvalue if we have an internal postqueue. The
10495 object the lvalue refers to would (probably) be modified by the
10496 postqueue; we need to copy the value out first, which means an
10497 rvalue. */
10498 if ((fallback & fb_lvalue)
10499 && gimple_seq_empty_p (internal_post)
10500 && is_gimple_addressable (*expr_p))
10502 /* An lvalue will do. Take the address of the expression, store it
10503 in a temporary, and replace the expression with an INDIRECT_REF of
10504 that temporary. */
10505 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
10506 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
10507 *expr_p = build_simple_mem_ref (tmp);
10509 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
10511 /* An rvalue will do. Assign the gimplified expression into a
10512 new temporary TMP and replace the original expression with
10513 TMP. First, make sure that the expression has a type so that
10514 it can be assigned into a temporary. */
10515 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
10516 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
10518 else
10520 #ifdef ENABLE_GIMPLE_CHECKING
10521 if (!(fallback & fb_mayfail))
10523 fprintf (stderr, "gimplification failed:\n");
10524 print_generic_expr (stderr, *expr_p, 0);
10525 debug_tree (*expr_p);
10526 internal_error ("gimplification failed");
10528 #endif
10529 gcc_assert (fallback & fb_mayfail);
10531 /* If this is an asm statement, and the user asked for the
10532 impossible, don't die. Fail and let gimplify_asm_expr
10533 issue an error. */
10534 ret = GS_ERROR;
10535 goto out;
10538 /* Make sure the temporary matches our predicate. */
10539 gcc_assert ((*gimple_test_f) (*expr_p));
10541 if (!gimple_seq_empty_p (internal_post))
10543 annotate_all_with_location (internal_post, input_location);
10544 gimplify_seq_add_seq (pre_p, internal_post);
10547 out:
10548 input_location = saved_location;
10549 return ret;
10552 /* Look through TYPE for variable-sized objects and gimplify each such
10553 size that we find. Add to LIST_P any statements generated. */
10555 void
10556 gimplify_type_sizes (tree type, gimple_seq *list_p)
10558 tree field, t;
10560 if (type == NULL || type == error_mark_node)
10561 return;
10563 /* We first do the main variant, then copy into any other variants. */
10564 type = TYPE_MAIN_VARIANT (type);
10566 /* Avoid infinite recursion. */
10567 if (TYPE_SIZES_GIMPLIFIED (type))
10568 return;
10570 TYPE_SIZES_GIMPLIFIED (type) = 1;
10572 switch (TREE_CODE (type))
10574 case INTEGER_TYPE:
10575 case ENUMERAL_TYPE:
10576 case BOOLEAN_TYPE:
10577 case REAL_TYPE:
10578 case FIXED_POINT_TYPE:
10579 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
10580 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
10582 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10584 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
10585 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
10587 break;
10589 case ARRAY_TYPE:
10590 /* These types may not have declarations, so handle them here. */
10591 gimplify_type_sizes (TREE_TYPE (type), list_p);
10592 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
10593 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
10594 with assigned stack slots, for -O1+ -g they should be tracked
10595 by VTA. */
10596 if (!(TYPE_NAME (type)
10597 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10598 && DECL_IGNORED_P (TYPE_NAME (type)))
10599 && TYPE_DOMAIN (type)
10600 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
10602 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
10603 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10604 DECL_IGNORED_P (t) = 0;
10605 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
10606 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10607 DECL_IGNORED_P (t) = 0;
10609 break;
10611 case RECORD_TYPE:
10612 case UNION_TYPE:
10613 case QUAL_UNION_TYPE:
10614 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10615 if (TREE_CODE (field) == FIELD_DECL)
10617 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
10618 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
10619 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
10620 gimplify_type_sizes (TREE_TYPE (field), list_p);
10622 break;
10624 case POINTER_TYPE:
10625 case REFERENCE_TYPE:
10626 /* We used to recurse on the pointed-to type here, which turned out to
10627 be incorrect because its definition might refer to variables not
10628 yet initialized at this point if a forward declaration is involved.
10630 It was actually useful for anonymous pointed-to types to ensure
10631 that the sizes evaluation dominates every possible later use of the
10632 values. Restricting to such types here would be safe since there
10633 is no possible forward declaration around, but would introduce an
10634 undesirable middle-end semantic to anonymity. We then defer to
10635 front-ends the responsibility of ensuring that the sizes are
10636 evaluated both early and late enough, e.g. by attaching artificial
10637 type declarations to the tree. */
10638 break;
10640 default:
10641 break;
10644 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
10645 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
10647 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10649 TYPE_SIZE (t) = TYPE_SIZE (type);
10650 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
10651 TYPE_SIZES_GIMPLIFIED (t) = 1;
10655 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
10656 a size or position, has had all of its SAVE_EXPRs evaluated.
10657 We add any required statements to *STMT_P. */
10659 void
10660 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
10662 tree expr = *expr_p;
10664 /* We don't do anything if the value isn't there, is constant, or contains
10665 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
10666 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
10667 will want to replace it with a new variable, but that will cause problems
10668 if this type is from outside the function. It's OK to have that here. */
10669 if (is_gimple_sizepos (expr))
10670 return;
10672 *expr_p = unshare_expr (expr);
10674 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
10677 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
10678 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
10679 is true, also gimplify the parameters. */
10681 gbind *
10682 gimplify_body (tree fndecl, bool do_parms)
10684 location_t saved_location = input_location;
10685 gimple_seq parm_stmts, seq;
10686 gimple *outer_stmt;
10687 gbind *outer_bind;
10688 struct cgraph_node *cgn;
10690 timevar_push (TV_TREE_GIMPLIFY);
10692 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
10693 gimplification. */
10694 default_rtl_profile ();
10696 gcc_assert (gimplify_ctxp == NULL);
10697 push_gimplify_context ();
10699 if (flag_openacc || flag_openmp)
10701 gcc_assert (gimplify_omp_ctxp == NULL);
10702 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
10703 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
10706 /* Unshare most shared trees in the body and in that of any nested functions.
10707 It would seem we don't have to do this for nested functions because
10708 they are supposed to be output and then the outer function gimplified
10709 first, but the g++ front end doesn't always do it that way. */
10710 unshare_body (fndecl);
10711 unvisit_body (fndecl);
10713 cgn = cgraph_node::get (fndecl);
10714 if (cgn && cgn->origin)
10715 nonlocal_vlas = new hash_set<tree>;
10717 /* Make sure input_location isn't set to something weird. */
10718 input_location = DECL_SOURCE_LOCATION (fndecl);
10720 /* Resolve callee-copies. This has to be done before processing
10721 the body so that DECL_VALUE_EXPR gets processed correctly. */
10722 parm_stmts = do_parms ? gimplify_parameters () : NULL;
10724 /* Gimplify the function's body. */
10725 seq = NULL;
10726 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
10727 outer_stmt = gimple_seq_first_stmt (seq);
10728 if (!outer_stmt)
10730 outer_stmt = gimple_build_nop ();
10731 gimplify_seq_add_stmt (&seq, outer_stmt);
10734 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
10735 not the case, wrap everything in a GIMPLE_BIND to make it so. */
10736 if (gimple_code (outer_stmt) == GIMPLE_BIND
10737 && gimple_seq_first (seq) == gimple_seq_last (seq))
10738 outer_bind = as_a <gbind *> (outer_stmt);
10739 else
10740 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
10742 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10744 /* If we had callee-copies statements, insert them at the beginning
10745 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
10746 if (!gimple_seq_empty_p (parm_stmts))
10748 tree parm;
10750 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
10751 gimple_bind_set_body (outer_bind, parm_stmts);
10753 for (parm = DECL_ARGUMENTS (current_function_decl);
10754 parm; parm = DECL_CHAIN (parm))
10755 if (DECL_HAS_VALUE_EXPR_P (parm))
10757 DECL_HAS_VALUE_EXPR_P (parm) = 0;
10758 DECL_IGNORED_P (parm) = 0;
10762 if (nonlocal_vlas)
10764 if (nonlocal_vla_vars)
10766 /* tree-nested.c may later on call declare_vars (..., true);
10767 which relies on BLOCK_VARS chain to be the tail of the
10768 gimple_bind_vars chain. Ensure we don't violate that
10769 assumption. */
10770 if (gimple_bind_block (outer_bind)
10771 == DECL_INITIAL (current_function_decl))
10772 declare_vars (nonlocal_vla_vars, outer_bind, true);
10773 else
10774 BLOCK_VARS (DECL_INITIAL (current_function_decl))
10775 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
10776 nonlocal_vla_vars);
10777 nonlocal_vla_vars = NULL_TREE;
10779 delete nonlocal_vlas;
10780 nonlocal_vlas = NULL;
10783 if ((flag_openacc || flag_openmp || flag_openmp_simd)
10784 && gimplify_omp_ctxp)
10786 delete_omp_context (gimplify_omp_ctxp);
10787 gimplify_omp_ctxp = NULL;
10790 pop_gimplify_context (outer_bind);
10791 gcc_assert (gimplify_ctxp == NULL);
10793 if (flag_checking && !seen_error ())
10794 verify_gimple_in_seq (gimple_bind_body (outer_bind));
10796 timevar_pop (TV_TREE_GIMPLIFY);
10797 input_location = saved_location;
10799 return outer_bind;
10802 typedef char *char_p; /* For DEF_VEC_P. */
10804 /* Return whether we should exclude FNDECL from instrumentation. */
10806 static bool
10807 flag_instrument_functions_exclude_p (tree fndecl)
10809 vec<char_p> *v;
10811 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
10812 if (v && v->length () > 0)
10814 const char *name;
10815 int i;
10816 char *s;
10818 name = lang_hooks.decl_printable_name (fndecl, 0);
10819 FOR_EACH_VEC_ELT (*v, i, s)
10820 if (strstr (name, s) != NULL)
10821 return true;
10824 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
10825 if (v && v->length () > 0)
10827 const char *name;
10828 int i;
10829 char *s;
10831 name = DECL_SOURCE_FILE (fndecl);
10832 FOR_EACH_VEC_ELT (*v, i, s)
10833 if (strstr (name, s) != NULL)
10834 return true;
10837 return false;
10840 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
10841 node for the function we want to gimplify.
10843 Return the sequence of GIMPLE statements corresponding to the body
10844 of FNDECL. */
10846 void
10847 gimplify_function_tree (tree fndecl)
10849 tree parm, ret;
10850 gimple_seq seq;
10851 gbind *bind;
10853 gcc_assert (!gimple_body (fndecl));
10855 if (DECL_STRUCT_FUNCTION (fndecl))
10856 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
10857 else
10858 push_struct_function (fndecl);
10860 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
10861 if necessary. */
10862 cfun->curr_properties |= PROP_gimple_lva;
10864 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
10866 /* Preliminarily mark non-addressed complex variables as eligible
10867 for promotion to gimple registers. We'll transform their uses
10868 as we find them. */
10869 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
10870 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
10871 && !TREE_THIS_VOLATILE (parm)
10872 && !needs_to_live_in_memory (parm))
10873 DECL_GIMPLE_REG_P (parm) = 1;
10876 ret = DECL_RESULT (fndecl);
10877 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
10878 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
10879 && !needs_to_live_in_memory (ret))
10880 DECL_GIMPLE_REG_P (ret) = 1;
10882 bind = gimplify_body (fndecl, true);
10884 /* The tree body of the function is no longer needed, replace it
10885 with the new GIMPLE body. */
10886 seq = NULL;
10887 gimple_seq_add_stmt (&seq, bind);
10888 gimple_set_body (fndecl, seq);
10890 /* If we're instrumenting function entry/exit, then prepend the call to
10891 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
10892 catch the exit hook. */
10893 /* ??? Add some way to ignore exceptions for this TFE. */
10894 if (flag_instrument_function_entry_exit
10895 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
10896 && !flag_instrument_functions_exclude_p (fndecl))
10898 tree x;
10899 gbind *new_bind;
10900 gimple *tf;
10901 gimple_seq cleanup = NULL, body = NULL;
10902 tree tmp_var;
10903 gcall *call;
10905 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10906 call = gimple_build_call (x, 1, integer_zero_node);
10907 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10908 gimple_call_set_lhs (call, tmp_var);
10909 gimplify_seq_add_stmt (&cleanup, call);
10910 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
10911 call = gimple_build_call (x, 2,
10912 build_fold_addr_expr (current_function_decl),
10913 tmp_var);
10914 gimplify_seq_add_stmt (&cleanup, call);
10915 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
10917 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10918 call = gimple_build_call (x, 1, integer_zero_node);
10919 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10920 gimple_call_set_lhs (call, tmp_var);
10921 gimplify_seq_add_stmt (&body, call);
10922 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
10923 call = gimple_build_call (x, 2,
10924 build_fold_addr_expr (current_function_decl),
10925 tmp_var);
10926 gimplify_seq_add_stmt (&body, call);
10927 gimplify_seq_add_stmt (&body, tf);
10928 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
10929 /* Clear the block for BIND, since it is no longer directly inside
10930 the function, but within a try block. */
10931 gimple_bind_set_block (bind, NULL);
10933 /* Replace the current function body with the body
10934 wrapped in the try/finally TF. */
10935 seq = NULL;
10936 gimple_seq_add_stmt (&seq, new_bind);
10937 gimple_set_body (fndecl, seq);
10938 bind = new_bind;
10941 if ((flag_sanitize & SANITIZE_THREAD) != 0
10942 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
10944 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
10945 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
10946 gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
10947 /* Clear the block for BIND, since it is no longer directly inside
10948 the function, but within a try block. */
10949 gimple_bind_set_block (bind, NULL);
10950 /* Replace the current function body with the body
10951 wrapped in the try/finally TF. */
10952 seq = NULL;
10953 gimple_seq_add_stmt (&seq, new_bind);
10954 gimple_set_body (fndecl, seq);
10957 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10958 cfun->curr_properties |= PROP_gimple_any;
10960 pop_cfun ();
10962 dump_function (TDI_generic, fndecl);
10965 /* Return a dummy expression of type TYPE in order to keep going after an
10966 error. */
10968 static tree
10969 dummy_object (tree type)
10971 tree t = build_int_cst (build_pointer_type (type), 0);
10972 return build2 (MEM_REF, type, t, t);
10975 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
10976 builtin function, but a very special sort of operator. */
10978 enum gimplify_status
10979 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
10980 gimple_seq *post_p ATTRIBUTE_UNUSED)
10982 tree promoted_type, have_va_type;
10983 tree valist = TREE_OPERAND (*expr_p, 0);
10984 tree type = TREE_TYPE (*expr_p);
10985 tree t, tag;
10986 location_t loc = EXPR_LOCATION (*expr_p);
10988 /* Verify that valist is of the proper type. */
10989 have_va_type = TREE_TYPE (valist);
10990 if (have_va_type == error_mark_node)
10991 return GS_ERROR;
10992 have_va_type = targetm.canonical_va_list_type (have_va_type);
10994 if (have_va_type == NULL_TREE)
10996 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
10997 return GS_ERROR;
11000 /* Generate a diagnostic for requesting data of a type that cannot
11001 be passed through `...' due to type promotion at the call site. */
11002 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
11003 != type)
11005 static bool gave_help;
11006 bool warned;
11008 /* Unfortunately, this is merely undefined, rather than a constraint
11009 violation, so we cannot make this an error. If this call is never
11010 executed, the program is still strictly conforming. */
11011 warned = warning_at (loc, 0,
11012 "%qT is promoted to %qT when passed through %<...%>",
11013 type, promoted_type);
11014 if (!gave_help && warned)
11016 gave_help = true;
11017 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
11018 promoted_type, type);
11021 /* We can, however, treat "undefined" any way we please.
11022 Call abort to encourage the user to fix the program. */
11023 if (warned)
11024 inform (loc, "if this code is reached, the program will abort");
11025 /* Before the abort, allow the evaluation of the va_list
11026 expression to exit or longjmp. */
11027 gimplify_and_add (valist, pre_p);
11028 t = build_call_expr_loc (loc,
11029 builtin_decl_implicit (BUILT_IN_TRAP), 0);
11030 gimplify_and_add (t, pre_p);
11032 /* This is dead code, but go ahead and finish so that the
11033 mode of the result comes out right. */
11034 *expr_p = dummy_object (type);
11035 return GS_ALL_DONE;
11038 tag = build_int_cst (build_pointer_type (type), 0);
11039 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 2, valist, tag);
11041 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
11042 needs to be expanded. */
11043 cfun->curr_properties &= ~PROP_gimple_lva;
11045 return GS_OK;
11048 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
11050 DST/SRC are the destination and source respectively. You can pass
11051 ungimplified trees in DST or SRC, in which case they will be
11052 converted to a gimple operand if necessary.
11054 This function returns the newly created GIMPLE_ASSIGN tuple. */
11056 gimple *
11057 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
11059 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
11060 gimplify_and_add (t, seq_p);
11061 ggc_free (t);
11062 return gimple_seq_last_stmt (*seq_p);
11065 inline hashval_t
11066 gimplify_hasher::hash (const elt_t *p)
11068 tree t = p->val;
11069 return iterative_hash_expr (t, 0);
11072 inline bool
11073 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
11075 tree t1 = p1->val;
11076 tree t2 = p2->val;
11077 enum tree_code code = TREE_CODE (t1);
11079 if (TREE_CODE (t2) != code
11080 || TREE_TYPE (t1) != TREE_TYPE (t2))
11081 return false;
11083 if (!operand_equal_p (t1, t2, 0))
11084 return false;
11086 /* Only allow them to compare equal if they also hash equal; otherwise
11087 results are nondeterminate, and we fail bootstrap comparison. */
11088 gcc_checking_assert (hash (p1) == hash (p2));
11090 return true;