Missing copyright for mem-stats header files.
[official-gcc.git] / gcc / gimplify.c
blob32bc1fdb2fb8dd11b25ddfca11548fdb90ce947e
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2016 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"
59 #include "gimple-walk.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 /* Flag for shared vars that are or might be stored to in the region. */
91 GOVD_WRITTEN = 131072,
93 /* Flag for GOVD_MAP, if it is a forced mapping. */
94 GOVD_MAP_FORCE = 262144,
96 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
97 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
98 | GOVD_LOCAL)
102 enum omp_region_type
104 ORT_WORKSHARE = 0x00,
105 ORT_SIMD = 0x01,
107 ORT_PARALLEL = 0x02,
108 ORT_COMBINED_PARALLEL = 0x03,
110 ORT_TASK = 0x04,
111 ORT_UNTIED_TASK = 0x05,
113 ORT_TEAMS = 0x08,
114 ORT_COMBINED_TEAMS = 0x09,
116 /* Data region. */
117 ORT_TARGET_DATA = 0x10,
119 /* Data region with offloading. */
120 ORT_TARGET = 0x20,
121 ORT_COMBINED_TARGET = 0x21,
123 /* OpenACC variants. */
124 ORT_ACC = 0x40, /* A generic OpenACC region. */
125 ORT_ACC_DATA = ORT_ACC | ORT_TARGET_DATA, /* Data construct. */
126 ORT_ACC_PARALLEL = ORT_ACC | ORT_TARGET, /* Parallel construct */
127 ORT_ACC_KERNELS = ORT_ACC | ORT_TARGET | 0x80, /* Kernels construct. */
128 ORT_ACC_HOST_DATA = ORT_ACC | ORT_TARGET_DATA | 0x80, /* Host data. */
130 /* Dummy OpenMP region, used to disable expansion of
131 DECL_VALUE_EXPRs in taskloop pre body. */
132 ORT_NONE = 0x100
135 /* Gimplify hashtable helper. */
137 struct gimplify_hasher : free_ptr_hash <elt_t>
139 static inline hashval_t hash (const elt_t *);
140 static inline bool equal (const elt_t *, const elt_t *);
143 struct gimplify_ctx
145 struct gimplify_ctx *prev_context;
147 vec<gbind *> bind_expr_stack;
148 tree temps;
149 gimple_seq conditional_cleanups;
150 tree exit_label;
151 tree return_temp;
153 vec<tree> case_labels;
154 /* The formal temporary table. Should this be persistent? */
155 hash_table<gimplify_hasher> *temp_htab;
157 int conditions;
158 unsigned into_ssa : 1;
159 unsigned allow_rhs_cond_expr : 1;
160 unsigned in_cleanup_point_expr : 1;
161 unsigned keep_stack : 1;
162 unsigned save_stack : 1;
165 struct gimplify_omp_ctx
167 struct gimplify_omp_ctx *outer_context;
168 splay_tree variables;
169 hash_set<tree> *privatized_types;
170 /* Iteration variables in an OMP_FOR. */
171 vec<tree> loop_iter_var;
172 location_t location;
173 enum omp_clause_default_kind default_kind;
174 enum omp_region_type region_type;
175 bool combined_loop;
176 bool distribute;
177 bool target_map_scalars_firstprivate;
178 bool target_map_pointers_as_0len_arrays;
179 bool target_firstprivatize_array_bases;
182 static struct gimplify_ctx *gimplify_ctxp;
183 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
185 /* Forward declaration. */
186 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
187 static hash_map<tree, tree> *oacc_declare_returns;
189 /* Shorter alias name for the above function for use in gimplify.c
190 only. */
192 static inline void
193 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
195 gimple_seq_add_stmt_without_update (seq_p, gs);
198 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
199 NULL, a new sequence is allocated. This function is
200 similar to gimple_seq_add_seq, but does not scan the operands.
201 During gimplification, we need to manipulate statement sequences
202 before the def/use vectors have been constructed. */
204 static void
205 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
207 gimple_stmt_iterator si;
209 if (src == NULL)
210 return;
212 si = gsi_last (*dst_p);
213 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
217 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
218 and popping gimplify contexts. */
220 static struct gimplify_ctx *ctx_pool = NULL;
222 /* Return a gimplify context struct from the pool. */
224 static inline struct gimplify_ctx *
225 ctx_alloc (void)
227 struct gimplify_ctx * c = ctx_pool;
229 if (c)
230 ctx_pool = c->prev_context;
231 else
232 c = XNEW (struct gimplify_ctx);
234 memset (c, '\0', sizeof (*c));
235 return c;
238 /* Put gimplify context C back into the pool. */
240 static inline void
241 ctx_free (struct gimplify_ctx *c)
243 c->prev_context = ctx_pool;
244 ctx_pool = c;
247 /* Free allocated ctx stack memory. */
249 void
250 free_gimplify_stack (void)
252 struct gimplify_ctx *c;
254 while ((c = ctx_pool))
256 ctx_pool = c->prev_context;
257 free (c);
262 /* Set up a context for the gimplifier. */
264 void
265 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
267 struct gimplify_ctx *c = ctx_alloc ();
269 c->prev_context = gimplify_ctxp;
270 gimplify_ctxp = c;
271 gimplify_ctxp->into_ssa = in_ssa;
272 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
275 /* Tear down a context for the gimplifier. If BODY is non-null, then
276 put the temporaries into the outer BIND_EXPR. Otherwise, put them
277 in the local_decls.
279 BODY is not a sequence, but the first tuple in a sequence. */
281 void
282 pop_gimplify_context (gimple *body)
284 struct gimplify_ctx *c = gimplify_ctxp;
286 gcc_assert (c
287 && (!c->bind_expr_stack.exists ()
288 || c->bind_expr_stack.is_empty ()));
289 c->bind_expr_stack.release ();
290 gimplify_ctxp = c->prev_context;
292 if (body)
293 declare_vars (c->temps, body, false);
294 else
295 record_vars (c->temps);
297 delete c->temp_htab;
298 c->temp_htab = NULL;
299 ctx_free (c);
302 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
304 static void
305 gimple_push_bind_expr (gbind *bind_stmt)
307 gimplify_ctxp->bind_expr_stack.reserve (8);
308 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
311 /* Pop the first element off the stack of bindings. */
313 static void
314 gimple_pop_bind_expr (void)
316 gimplify_ctxp->bind_expr_stack.pop ();
319 /* Return the first element of the stack of bindings. */
321 gbind *
322 gimple_current_bind_expr (void)
324 return gimplify_ctxp->bind_expr_stack.last ();
327 /* Return the stack of bindings created during gimplification. */
329 vec<gbind *>
330 gimple_bind_expr_stack (void)
332 return gimplify_ctxp->bind_expr_stack;
335 /* Return true iff there is a COND_EXPR between us and the innermost
336 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
338 static bool
339 gimple_conditional_context (void)
341 return gimplify_ctxp->conditions > 0;
344 /* Note that we've entered a COND_EXPR. */
346 static void
347 gimple_push_condition (void)
349 #ifdef ENABLE_GIMPLE_CHECKING
350 if (gimplify_ctxp->conditions == 0)
351 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
352 #endif
353 ++(gimplify_ctxp->conditions);
356 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
357 now, add any conditional cleanups we've seen to the prequeue. */
359 static void
360 gimple_pop_condition (gimple_seq *pre_p)
362 int conds = --(gimplify_ctxp->conditions);
364 gcc_assert (conds >= 0);
365 if (conds == 0)
367 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
368 gimplify_ctxp->conditional_cleanups = NULL;
372 /* A stable comparison routine for use with splay trees and DECLs. */
374 static int
375 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
377 tree a = (tree) xa;
378 tree b = (tree) xb;
380 return DECL_UID (a) - DECL_UID (b);
383 /* Create a new omp construct that deals with variable remapping. */
385 static struct gimplify_omp_ctx *
386 new_omp_context (enum omp_region_type region_type)
388 struct gimplify_omp_ctx *c;
390 c = XCNEW (struct gimplify_omp_ctx);
391 c->outer_context = gimplify_omp_ctxp;
392 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
393 c->privatized_types = new hash_set<tree>;
394 c->location = input_location;
395 c->region_type = region_type;
396 if ((region_type & ORT_TASK) == 0)
397 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
398 else
399 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
401 return c;
404 /* Destroy an omp construct that deals with variable remapping. */
406 static void
407 delete_omp_context (struct gimplify_omp_ctx *c)
409 splay_tree_delete (c->variables);
410 delete c->privatized_types;
411 c->loop_iter_var.release ();
412 XDELETE (c);
415 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
416 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
418 /* Both gimplify the statement T and append it to *SEQ_P. This function
419 behaves exactly as gimplify_stmt, but you don't have to pass T as a
420 reference. */
422 void
423 gimplify_and_add (tree t, gimple_seq *seq_p)
425 gimplify_stmt (&t, seq_p);
428 /* Gimplify statement T into sequence *SEQ_P, and return the first
429 tuple in the sequence of generated tuples for this statement.
430 Return NULL if gimplifying T produced no tuples. */
432 static gimple *
433 gimplify_and_return_first (tree t, gimple_seq *seq_p)
435 gimple_stmt_iterator last = gsi_last (*seq_p);
437 gimplify_and_add (t, seq_p);
439 if (!gsi_end_p (last))
441 gsi_next (&last);
442 return gsi_stmt (last);
444 else
445 return gimple_seq_first_stmt (*seq_p);
448 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
449 LHS, or for a call argument. */
451 static bool
452 is_gimple_mem_rhs (tree t)
454 /* If we're dealing with a renamable type, either source or dest must be
455 a renamed variable. */
456 if (is_gimple_reg_type (TREE_TYPE (t)))
457 return is_gimple_val (t);
458 else
459 return is_gimple_val (t) || is_gimple_lvalue (t);
462 /* Return true if T is a CALL_EXPR or an expression that can be
463 assigned to a temporary. Note that this predicate should only be
464 used during gimplification. See the rationale for this in
465 gimplify_modify_expr. */
467 static bool
468 is_gimple_reg_rhs_or_call (tree t)
470 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
471 || TREE_CODE (t) == CALL_EXPR);
474 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
475 this predicate should only be used during gimplification. See the
476 rationale for this in gimplify_modify_expr. */
478 static bool
479 is_gimple_mem_rhs_or_call (tree t)
481 /* If we're dealing with a renamable type, either source or dest must be
482 a renamed variable. */
483 if (is_gimple_reg_type (TREE_TYPE (t)))
484 return is_gimple_val (t);
485 else
486 return (is_gimple_val (t) || is_gimple_lvalue (t)
487 || TREE_CODE (t) == CALL_EXPR);
490 /* Create a temporary with a name derived from VAL. Subroutine of
491 lookup_tmp_var; nobody else should call this function. */
493 static inline tree
494 create_tmp_from_val (tree val)
496 /* Drop all qualifiers and address-space information from the value type. */
497 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
498 tree var = create_tmp_var (type, get_name (val));
499 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
500 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
501 DECL_GIMPLE_REG_P (var) = 1;
502 return var;
505 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
506 an existing expression temporary. */
508 static tree
509 lookup_tmp_var (tree val, bool is_formal)
511 tree ret;
513 /* If not optimizing, never really reuse a temporary. local-alloc
514 won't allocate any variable that is used in more than one basic
515 block, which means it will go into memory, causing much extra
516 work in reload and final and poorer code generation, outweighing
517 the extra memory allocation here. */
518 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
519 ret = create_tmp_from_val (val);
520 else
522 elt_t elt, *elt_p;
523 elt_t **slot;
525 elt.val = val;
526 if (!gimplify_ctxp->temp_htab)
527 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
528 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
529 if (*slot == NULL)
531 elt_p = XNEW (elt_t);
532 elt_p->val = val;
533 elt_p->temp = ret = create_tmp_from_val (val);
534 *slot = elt_p;
536 else
538 elt_p = *slot;
539 ret = elt_p->temp;
543 return ret;
546 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
548 static tree
549 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
550 bool is_formal)
552 tree t, mod;
554 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
555 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
556 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
557 fb_rvalue);
559 if (gimplify_ctxp->into_ssa
560 && is_gimple_reg_type (TREE_TYPE (val)))
561 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
562 else
563 t = lookup_tmp_var (val, is_formal);
565 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
567 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
569 /* gimplify_modify_expr might want to reduce this further. */
570 gimplify_and_add (mod, pre_p);
571 ggc_free (mod);
573 return t;
576 /* Return a formal temporary variable initialized with VAL. PRE_P is as
577 in gimplify_expr. Only use this function if:
579 1) The value of the unfactored expression represented by VAL will not
580 change between the initialization and use of the temporary, and
581 2) The temporary will not be otherwise modified.
583 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
584 and #2 means it is inappropriate for && temps.
586 For other cases, use get_initialized_tmp_var instead. */
588 tree
589 get_formal_tmp_var (tree val, gimple_seq *pre_p)
591 return internal_get_tmp_var (val, pre_p, NULL, true);
594 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
595 are as in gimplify_expr. */
597 tree
598 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
600 return internal_get_tmp_var (val, pre_p, post_p, false);
603 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
604 generate debug info for them; otherwise don't. */
606 void
607 declare_vars (tree vars, gimple *gs, bool debug_info)
609 tree last = vars;
610 if (last)
612 tree temps, block;
614 gbind *scope = as_a <gbind *> (gs);
616 temps = nreverse (last);
618 block = gimple_bind_block (scope);
619 gcc_assert (!block || TREE_CODE (block) == BLOCK);
620 if (!block || !debug_info)
622 DECL_CHAIN (last) = gimple_bind_vars (scope);
623 gimple_bind_set_vars (scope, temps);
625 else
627 /* We need to attach the nodes both to the BIND_EXPR and to its
628 associated BLOCK for debugging purposes. The key point here
629 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
630 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
631 if (BLOCK_VARS (block))
632 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
633 else
635 gimple_bind_set_vars (scope,
636 chainon (gimple_bind_vars (scope), temps));
637 BLOCK_VARS (block) = temps;
643 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
644 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
645 no such upper bound can be obtained. */
647 static void
648 force_constant_size (tree var)
650 /* The only attempt we make is by querying the maximum size of objects
651 of the variable's type. */
653 HOST_WIDE_INT max_size;
655 gcc_assert (TREE_CODE (var) == VAR_DECL);
657 max_size = max_int_size_in_bytes (TREE_TYPE (var));
659 gcc_assert (max_size >= 0);
661 DECL_SIZE_UNIT (var)
662 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
663 DECL_SIZE (var)
664 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
667 /* Push the temporary variable TMP into the current binding. */
669 void
670 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
672 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
674 /* Later processing assumes that the object size is constant, which might
675 not be true at this point. Force the use of a constant upper bound in
676 this case. */
677 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
678 force_constant_size (tmp);
680 DECL_CONTEXT (tmp) = fn->decl;
681 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
683 record_vars_into (tmp, fn->decl);
686 /* Push the temporary variable TMP into the current binding. */
688 void
689 gimple_add_tmp_var (tree tmp)
691 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
693 /* Later processing assumes that the object size is constant, which might
694 not be true at this point. Force the use of a constant upper bound in
695 this case. */
696 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
697 force_constant_size (tmp);
699 DECL_CONTEXT (tmp) = current_function_decl;
700 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
702 if (gimplify_ctxp)
704 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
705 gimplify_ctxp->temps = tmp;
707 /* Mark temporaries local within the nearest enclosing parallel. */
708 if (gimplify_omp_ctxp)
710 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
711 while (ctx
712 && (ctx->region_type == ORT_WORKSHARE
713 || ctx->region_type == ORT_SIMD
714 || ctx->region_type == ORT_ACC))
715 ctx = ctx->outer_context;
716 if (ctx)
717 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
720 else if (cfun)
721 record_vars (tmp);
722 else
724 gimple_seq body_seq;
726 /* This case is for nested functions. We need to expose the locals
727 they create. */
728 body_seq = gimple_body (current_function_decl);
729 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
735 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
736 nodes that are referenced more than once in GENERIC functions. This is
737 necessary because gimplification (translation into GIMPLE) is performed
738 by modifying tree nodes in-place, so gimplication of a shared node in a
739 first context could generate an invalid GIMPLE form in a second context.
741 This is achieved with a simple mark/copy/unmark algorithm that walks the
742 GENERIC representation top-down, marks nodes with TREE_VISITED the first
743 time it encounters them, duplicates them if they already have TREE_VISITED
744 set, and finally removes the TREE_VISITED marks it has set.
746 The algorithm works only at the function level, i.e. it generates a GENERIC
747 representation of a function with no nodes shared within the function when
748 passed a GENERIC function (except for nodes that are allowed to be shared).
750 At the global level, it is also necessary to unshare tree nodes that are
751 referenced in more than one function, for the same aforementioned reason.
752 This requires some cooperation from the front-end. There are 2 strategies:
754 1. Manual unsharing. The front-end needs to call unshare_expr on every
755 expression that might end up being shared across functions.
757 2. Deep unsharing. This is an extension of regular unsharing. Instead
758 of calling unshare_expr on expressions that might be shared across
759 functions, the front-end pre-marks them with TREE_VISITED. This will
760 ensure that they are unshared on the first reference within functions
761 when the regular unsharing algorithm runs. The counterpart is that
762 this algorithm must look deeper than for manual unsharing, which is
763 specified by LANG_HOOKS_DEEP_UNSHARING.
765 If there are only few specific cases of node sharing across functions, it is
766 probably easier for a front-end to unshare the expressions manually. On the
767 contrary, if the expressions generated at the global level are as widespread
768 as expressions generated within functions, deep unsharing is very likely the
769 way to go. */
771 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
772 These nodes model computations that must be done once. If we were to
773 unshare something like SAVE_EXPR(i++), the gimplification process would
774 create wrong code. However, if DATA is non-null, it must hold a pointer
775 set that is used to unshare the subtrees of these nodes. */
777 static tree
778 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
780 tree t = *tp;
781 enum tree_code code = TREE_CODE (t);
783 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
784 copy their subtrees if we can make sure to do it only once. */
785 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
787 if (data && !((hash_set<tree> *)data)->add (t))
789 else
790 *walk_subtrees = 0;
793 /* Stop at types, decls, constants like copy_tree_r. */
794 else if (TREE_CODE_CLASS (code) == tcc_type
795 || TREE_CODE_CLASS (code) == tcc_declaration
796 || TREE_CODE_CLASS (code) == tcc_constant
797 /* We can't do anything sensible with a BLOCK used as an
798 expression, but we also can't just die when we see it
799 because of non-expression uses. So we avert our eyes
800 and cross our fingers. Silly Java. */
801 || code == BLOCK)
802 *walk_subtrees = 0;
804 /* Cope with the statement expression extension. */
805 else if (code == STATEMENT_LIST)
808 /* Leave the bulk of the work to copy_tree_r itself. */
809 else
810 copy_tree_r (tp, walk_subtrees, NULL);
812 return NULL_TREE;
815 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
816 If *TP has been visited already, then *TP is deeply copied by calling
817 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
819 static tree
820 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
822 tree t = *tp;
823 enum tree_code code = TREE_CODE (t);
825 /* Skip types, decls, and constants. But we do want to look at their
826 types and the bounds of types. Mark them as visited so we properly
827 unmark their subtrees on the unmark pass. If we've already seen them,
828 don't look down further. */
829 if (TREE_CODE_CLASS (code) == tcc_type
830 || TREE_CODE_CLASS (code) == tcc_declaration
831 || TREE_CODE_CLASS (code) == tcc_constant)
833 if (TREE_VISITED (t))
834 *walk_subtrees = 0;
835 else
836 TREE_VISITED (t) = 1;
839 /* If this node has been visited already, unshare it and don't look
840 any deeper. */
841 else if (TREE_VISITED (t))
843 walk_tree (tp, mostly_copy_tree_r, data, NULL);
844 *walk_subtrees = 0;
847 /* Otherwise, mark the node as visited and keep looking. */
848 else
849 TREE_VISITED (t) = 1;
851 return NULL_TREE;
854 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
855 copy_if_shared_r callback unmodified. */
857 static inline void
858 copy_if_shared (tree *tp, void *data)
860 walk_tree (tp, copy_if_shared_r, data, NULL);
863 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
864 any nested functions. */
866 static void
867 unshare_body (tree fndecl)
869 struct cgraph_node *cgn = cgraph_node::get (fndecl);
870 /* If the language requires deep unsharing, we need a pointer set to make
871 sure we don't repeatedly unshare subtrees of unshareable nodes. */
872 hash_set<tree> *visited
873 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
875 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
876 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
877 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
879 delete visited;
881 if (cgn)
882 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
883 unshare_body (cgn->decl);
886 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
887 Subtrees are walked until the first unvisited node is encountered. */
889 static tree
890 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
892 tree t = *tp;
894 /* If this node has been visited, unmark it and keep looking. */
895 if (TREE_VISITED (t))
896 TREE_VISITED (t) = 0;
898 /* Otherwise, don't look any deeper. */
899 else
900 *walk_subtrees = 0;
902 return NULL_TREE;
905 /* Unmark the visited trees rooted at *TP. */
907 static inline void
908 unmark_visited (tree *tp)
910 walk_tree (tp, unmark_visited_r, NULL, NULL);
913 /* Likewise, but mark all trees as not visited. */
915 static void
916 unvisit_body (tree fndecl)
918 struct cgraph_node *cgn = cgraph_node::get (fndecl);
920 unmark_visited (&DECL_SAVED_TREE (fndecl));
921 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
922 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
924 if (cgn)
925 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
926 unvisit_body (cgn->decl);
929 /* Unconditionally make an unshared copy of EXPR. This is used when using
930 stored expressions which span multiple functions, such as BINFO_VTABLE,
931 as the normal unsharing process can't tell that they're shared. */
933 tree
934 unshare_expr (tree expr)
936 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
937 return expr;
940 /* Worker for unshare_expr_without_location. */
942 static tree
943 prune_expr_location (tree *tp, int *walk_subtrees, void *)
945 if (EXPR_P (*tp))
946 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
947 else
948 *walk_subtrees = 0;
949 return NULL_TREE;
952 /* Similar to unshare_expr but also prune all expression locations
953 from EXPR. */
955 tree
956 unshare_expr_without_location (tree expr)
958 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
959 if (EXPR_P (expr))
960 walk_tree (&expr, prune_expr_location, NULL, NULL);
961 return expr;
964 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
965 contain statements and have a value. Assign its value to a temporary
966 and give it void_type_node. Return the temporary, or NULL_TREE if
967 WRAPPER was already void. */
969 tree
970 voidify_wrapper_expr (tree wrapper, tree temp)
972 tree type = TREE_TYPE (wrapper);
973 if (type && !VOID_TYPE_P (type))
975 tree *p;
977 /* Set p to point to the body of the wrapper. Loop until we find
978 something that isn't a wrapper. */
979 for (p = &wrapper; p && *p; )
981 switch (TREE_CODE (*p))
983 case BIND_EXPR:
984 TREE_SIDE_EFFECTS (*p) = 1;
985 TREE_TYPE (*p) = void_type_node;
986 /* For a BIND_EXPR, the body is operand 1. */
987 p = &BIND_EXPR_BODY (*p);
988 break;
990 case CLEANUP_POINT_EXPR:
991 case TRY_FINALLY_EXPR:
992 case TRY_CATCH_EXPR:
993 TREE_SIDE_EFFECTS (*p) = 1;
994 TREE_TYPE (*p) = void_type_node;
995 p = &TREE_OPERAND (*p, 0);
996 break;
998 case STATEMENT_LIST:
1000 tree_stmt_iterator i = tsi_last (*p);
1001 TREE_SIDE_EFFECTS (*p) = 1;
1002 TREE_TYPE (*p) = void_type_node;
1003 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1005 break;
1007 case COMPOUND_EXPR:
1008 /* Advance to the last statement. Set all container types to
1009 void. */
1010 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1012 TREE_SIDE_EFFECTS (*p) = 1;
1013 TREE_TYPE (*p) = void_type_node;
1015 break;
1017 case TRANSACTION_EXPR:
1018 TREE_SIDE_EFFECTS (*p) = 1;
1019 TREE_TYPE (*p) = void_type_node;
1020 p = &TRANSACTION_EXPR_BODY (*p);
1021 break;
1023 default:
1024 /* Assume that any tree upon which voidify_wrapper_expr is
1025 directly called is a wrapper, and that its body is op0. */
1026 if (p == &wrapper)
1028 TREE_SIDE_EFFECTS (*p) = 1;
1029 TREE_TYPE (*p) = void_type_node;
1030 p = &TREE_OPERAND (*p, 0);
1031 break;
1033 goto out;
1037 out:
1038 if (p == NULL || IS_EMPTY_STMT (*p))
1039 temp = NULL_TREE;
1040 else if (temp)
1042 /* The wrapper is on the RHS of an assignment that we're pushing
1043 down. */
1044 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1045 || TREE_CODE (temp) == MODIFY_EXPR);
1046 TREE_OPERAND (temp, 1) = *p;
1047 *p = temp;
1049 else
1051 temp = create_tmp_var (type, "retval");
1052 *p = build2 (INIT_EXPR, type, temp, *p);
1055 return temp;
1058 return NULL_TREE;
1061 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1062 a temporary through which they communicate. */
1064 static void
1065 build_stack_save_restore (gcall **save, gcall **restore)
1067 tree tmp_var;
1069 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1070 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1071 gimple_call_set_lhs (*save, tmp_var);
1073 *restore
1074 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1075 1, tmp_var);
1078 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1080 static enum gimplify_status
1081 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1083 tree bind_expr = *expr_p;
1084 bool old_keep_stack = gimplify_ctxp->keep_stack;
1085 bool old_save_stack = gimplify_ctxp->save_stack;
1086 tree t;
1087 gbind *bind_stmt;
1088 gimple_seq body, cleanup;
1089 gcall *stack_save;
1090 location_t start_locus = 0, end_locus = 0;
1091 tree ret_clauses = NULL;
1093 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1095 /* Mark variables seen in this bind expr. */
1096 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1098 if (TREE_CODE (t) == VAR_DECL)
1100 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1102 /* Mark variable as local. */
1103 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1104 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1105 || splay_tree_lookup (ctx->variables,
1106 (splay_tree_key) t) == NULL))
1108 if (ctx->region_type == ORT_SIMD
1109 && TREE_ADDRESSABLE (t)
1110 && !TREE_STATIC (t))
1111 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1112 else
1113 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1116 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1118 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1119 cfun->has_local_explicit_reg_vars = true;
1122 /* Preliminarily mark non-addressed complex variables as eligible
1123 for promotion to gimple registers. We'll transform their uses
1124 as we find them. */
1125 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1126 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1127 && !TREE_THIS_VOLATILE (t)
1128 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1129 && !needs_to_live_in_memory (t))
1130 DECL_GIMPLE_REG_P (t) = 1;
1133 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1134 BIND_EXPR_BLOCK (bind_expr));
1135 gimple_push_bind_expr (bind_stmt);
1137 gimplify_ctxp->keep_stack = false;
1138 gimplify_ctxp->save_stack = false;
1140 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1141 body = NULL;
1142 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1143 gimple_bind_set_body (bind_stmt, body);
1145 /* Source location wise, the cleanup code (stack_restore and clobbers)
1146 belongs to the end of the block, so propagate what we have. The
1147 stack_save operation belongs to the beginning of block, which we can
1148 infer from the bind_expr directly if the block has no explicit
1149 assignment. */
1150 if (BIND_EXPR_BLOCK (bind_expr))
1152 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1153 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1155 if (start_locus == 0)
1156 start_locus = EXPR_LOCATION (bind_expr);
1158 cleanup = NULL;
1159 stack_save = NULL;
1161 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1162 the stack space allocated to the VLAs. */
1163 if (gimplify_ctxp->save_stack && !gimplify_ctxp->keep_stack)
1165 gcall *stack_restore;
1167 /* Save stack on entry and restore it on exit. Add a try_finally
1168 block to achieve this. */
1169 build_stack_save_restore (&stack_save, &stack_restore);
1171 gimple_set_location (stack_save, start_locus);
1172 gimple_set_location (stack_restore, end_locus);
1174 gimplify_seq_add_stmt (&cleanup, stack_restore);
1177 /* Add clobbers for all variables that go out of scope. */
1178 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1180 if (TREE_CODE (t) == VAR_DECL
1181 && !is_global_var (t)
1182 && DECL_CONTEXT (t) == current_function_decl
1183 && !DECL_HARD_REGISTER (t)
1184 && !TREE_THIS_VOLATILE (t)
1185 && !DECL_HAS_VALUE_EXPR_P (t)
1186 /* Only care for variables that have to be in memory. Others
1187 will be rewritten into SSA names, hence moved to the top-level. */
1188 && !is_gimple_reg (t)
1189 && flag_stack_reuse != SR_NONE)
1191 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1192 gimple *clobber_stmt;
1193 TREE_THIS_VOLATILE (clobber) = 1;
1194 clobber_stmt = gimple_build_assign (t, clobber);
1195 gimple_set_location (clobber_stmt, end_locus);
1196 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1198 if (flag_openacc && oacc_declare_returns != NULL)
1200 tree *c = oacc_declare_returns->get (t);
1201 if (c != NULL)
1203 if (ret_clauses)
1204 OMP_CLAUSE_CHAIN (*c) = ret_clauses;
1206 ret_clauses = *c;
1208 oacc_declare_returns->remove (t);
1210 if (oacc_declare_returns->elements () == 0)
1212 delete oacc_declare_returns;
1213 oacc_declare_returns = NULL;
1220 if (ret_clauses)
1222 gomp_target *stmt;
1223 gimple_stmt_iterator si = gsi_start (cleanup);
1225 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
1226 ret_clauses);
1227 gsi_insert_seq_before_without_update (&si, stmt, GSI_NEW_STMT);
1230 if (cleanup)
1232 gtry *gs;
1233 gimple_seq new_body;
1235 new_body = NULL;
1236 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1237 GIMPLE_TRY_FINALLY);
1239 if (stack_save)
1240 gimplify_seq_add_stmt (&new_body, stack_save);
1241 gimplify_seq_add_stmt (&new_body, gs);
1242 gimple_bind_set_body (bind_stmt, new_body);
1245 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1246 if (!gimplify_ctxp->keep_stack)
1247 gimplify_ctxp->keep_stack = old_keep_stack;
1248 gimplify_ctxp->save_stack = old_save_stack;
1250 gimple_pop_bind_expr ();
1252 gimplify_seq_add_stmt (pre_p, bind_stmt);
1254 if (temp)
1256 *expr_p = temp;
1257 return GS_OK;
1260 *expr_p = NULL_TREE;
1261 return GS_ALL_DONE;
1264 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1265 GIMPLE value, it is assigned to a new temporary and the statement is
1266 re-written to return the temporary.
1268 PRE_P points to the sequence where side effects that must happen before
1269 STMT should be stored. */
1271 static enum gimplify_status
1272 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1274 greturn *ret;
1275 tree ret_expr = TREE_OPERAND (stmt, 0);
1276 tree result_decl, result;
1278 if (ret_expr == error_mark_node)
1279 return GS_ERROR;
1281 /* Implicit _Cilk_sync must be inserted right before any return statement
1282 if there is a _Cilk_spawn in the function. If the user has provided a
1283 _Cilk_sync, the optimizer should remove this duplicate one. */
1284 if (fn_contains_cilk_spawn_p (cfun))
1286 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1287 gimplify_and_add (impl_sync, pre_p);
1290 if (!ret_expr
1291 || TREE_CODE (ret_expr) == RESULT_DECL
1292 || ret_expr == error_mark_node)
1294 greturn *ret = gimple_build_return (ret_expr);
1295 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1296 gimplify_seq_add_stmt (pre_p, ret);
1297 return GS_ALL_DONE;
1300 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1301 result_decl = NULL_TREE;
1302 else
1304 result_decl = TREE_OPERAND (ret_expr, 0);
1306 /* See through a return by reference. */
1307 if (TREE_CODE (result_decl) == INDIRECT_REF)
1308 result_decl = TREE_OPERAND (result_decl, 0);
1310 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1311 || TREE_CODE (ret_expr) == INIT_EXPR)
1312 && TREE_CODE (result_decl) == RESULT_DECL);
1315 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1316 Recall that aggregate_value_p is FALSE for any aggregate type that is
1317 returned in registers. If we're returning values in registers, then
1318 we don't want to extend the lifetime of the RESULT_DECL, particularly
1319 across another call. In addition, for those aggregates for which
1320 hard_function_value generates a PARALLEL, we'll die during normal
1321 expansion of structure assignments; there's special code in expand_return
1322 to handle this case that does not exist in expand_expr. */
1323 if (!result_decl)
1324 result = NULL_TREE;
1325 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1327 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1329 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1330 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1331 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1332 should be effectively allocated by the caller, i.e. all calls to
1333 this function must be subject to the Return Slot Optimization. */
1334 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1335 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1337 result = result_decl;
1339 else if (gimplify_ctxp->return_temp)
1340 result = gimplify_ctxp->return_temp;
1341 else
1343 result = create_tmp_reg (TREE_TYPE (result_decl));
1345 /* ??? With complex control flow (usually involving abnormal edges),
1346 we can wind up warning about an uninitialized value for this. Due
1347 to how this variable is constructed and initialized, this is never
1348 true. Give up and never warn. */
1349 TREE_NO_WARNING (result) = 1;
1351 gimplify_ctxp->return_temp = result;
1354 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1355 Then gimplify the whole thing. */
1356 if (result != result_decl)
1357 TREE_OPERAND (ret_expr, 0) = result;
1359 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1361 ret = gimple_build_return (result);
1362 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1363 gimplify_seq_add_stmt (pre_p, ret);
1365 return GS_ALL_DONE;
1368 /* Gimplify a variable-length array DECL. */
1370 static void
1371 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1373 /* This is a variable-sized decl. Simplify its size and mark it
1374 for deferred expansion. */
1375 tree t, addr, ptr_type;
1377 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1378 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1380 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1381 if (DECL_HAS_VALUE_EXPR_P (decl))
1382 return;
1384 /* All occurrences of this decl in final gimplified code will be
1385 replaced by indirection. Setting DECL_VALUE_EXPR does two
1386 things: First, it lets the rest of the gimplifier know what
1387 replacement to use. Second, it lets the debug info know
1388 where to find the value. */
1389 ptr_type = build_pointer_type (TREE_TYPE (decl));
1390 addr = create_tmp_var (ptr_type, get_name (decl));
1391 DECL_IGNORED_P (addr) = 0;
1392 t = build_fold_indirect_ref (addr);
1393 TREE_THIS_NOTRAP (t) = 1;
1394 SET_DECL_VALUE_EXPR (decl, t);
1395 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1397 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1398 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1399 size_int (DECL_ALIGN (decl)));
1400 /* The call has been built for a variable-sized object. */
1401 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1402 t = fold_convert (ptr_type, t);
1403 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1405 gimplify_and_add (t, seq_p);
1408 /* A helper function to be called via walk_tree. Mark all labels under *TP
1409 as being forced. To be called for DECL_INITIAL of static variables. */
1411 static tree
1412 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1414 if (TYPE_P (*tp))
1415 *walk_subtrees = 0;
1416 if (TREE_CODE (*tp) == LABEL_DECL)
1417 FORCED_LABEL (*tp) = 1;
1419 return NULL_TREE;
1422 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1423 and initialization explicit. */
1425 static enum gimplify_status
1426 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1428 tree stmt = *stmt_p;
1429 tree decl = DECL_EXPR_DECL (stmt);
1431 *stmt_p = NULL_TREE;
1433 if (TREE_TYPE (decl) == error_mark_node)
1434 return GS_ERROR;
1436 if ((TREE_CODE (decl) == TYPE_DECL
1437 || TREE_CODE (decl) == VAR_DECL)
1438 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1439 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1441 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1442 in case its size expressions contain problematic nodes like CALL_EXPR. */
1443 if (TREE_CODE (decl) == TYPE_DECL
1444 && DECL_ORIGINAL_TYPE (decl)
1445 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1446 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1448 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1450 tree init = DECL_INITIAL (decl);
1452 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1453 || (!TREE_STATIC (decl)
1454 && flag_stack_check == GENERIC_STACK_CHECK
1455 && compare_tree_int (DECL_SIZE_UNIT (decl),
1456 STACK_CHECK_MAX_VAR_SIZE) > 0))
1457 gimplify_vla_decl (decl, seq_p);
1459 /* Some front ends do not explicitly declare all anonymous
1460 artificial variables. We compensate here by declaring the
1461 variables, though it would be better if the front ends would
1462 explicitly declare them. */
1463 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1464 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1465 gimple_add_tmp_var (decl);
1467 if (init && init != error_mark_node)
1469 if (!TREE_STATIC (decl))
1471 DECL_INITIAL (decl) = NULL_TREE;
1472 init = build2 (INIT_EXPR, void_type_node, decl, init);
1473 gimplify_and_add (init, seq_p);
1474 ggc_free (init);
1476 else
1477 /* We must still examine initializers for static variables
1478 as they may contain a label address. */
1479 walk_tree (&init, force_labels_r, NULL, NULL);
1483 return GS_ALL_DONE;
1486 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1487 and replacing the LOOP_EXPR with goto, but if the loop contains an
1488 EXIT_EXPR, we need to append a label for it to jump to. */
1490 static enum gimplify_status
1491 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1493 tree saved_label = gimplify_ctxp->exit_label;
1494 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1496 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1498 gimplify_ctxp->exit_label = NULL_TREE;
1500 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1502 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1504 if (gimplify_ctxp->exit_label)
1505 gimplify_seq_add_stmt (pre_p,
1506 gimple_build_label (gimplify_ctxp->exit_label));
1508 gimplify_ctxp->exit_label = saved_label;
1510 *expr_p = NULL;
1511 return GS_ALL_DONE;
1514 /* Gimplify a statement list onto a sequence. These may be created either
1515 by an enlightened front-end, or by shortcut_cond_expr. */
1517 static enum gimplify_status
1518 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1520 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1522 tree_stmt_iterator i = tsi_start (*expr_p);
1524 while (!tsi_end_p (i))
1526 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1527 tsi_delink (&i);
1530 if (temp)
1532 *expr_p = temp;
1533 return GS_OK;
1536 return GS_ALL_DONE;
1540 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1541 branch to. */
1543 static enum gimplify_status
1544 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1546 tree switch_expr = *expr_p;
1547 gimple_seq switch_body_seq = NULL;
1548 enum gimplify_status ret;
1549 tree index_type = TREE_TYPE (switch_expr);
1550 if (index_type == NULL_TREE)
1551 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1553 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1554 fb_rvalue);
1555 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1556 return ret;
1558 if (SWITCH_BODY (switch_expr))
1560 vec<tree> labels;
1561 vec<tree> saved_labels;
1562 tree default_case = NULL_TREE;
1563 gswitch *switch_stmt;
1565 /* If someone can be bothered to fill in the labels, they can
1566 be bothered to null out the body too. */
1567 gcc_assert (!SWITCH_LABELS (switch_expr));
1569 /* Save old labels, get new ones from body, then restore the old
1570 labels. Save all the things from the switch body to append after. */
1571 saved_labels = gimplify_ctxp->case_labels;
1572 gimplify_ctxp->case_labels.create (8);
1574 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1575 labels = gimplify_ctxp->case_labels;
1576 gimplify_ctxp->case_labels = saved_labels;
1578 preprocess_case_label_vec_for_gimple (labels, index_type,
1579 &default_case);
1581 if (!default_case)
1583 glabel *new_default;
1585 default_case
1586 = build_case_label (NULL_TREE, NULL_TREE,
1587 create_artificial_label (UNKNOWN_LOCATION));
1588 new_default = gimple_build_label (CASE_LABEL (default_case));
1589 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1592 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1593 default_case, labels);
1594 gimplify_seq_add_stmt (pre_p, switch_stmt);
1595 gimplify_seq_add_seq (pre_p, switch_body_seq);
1596 labels.release ();
1598 else
1599 gcc_assert (SWITCH_LABELS (switch_expr));
1601 return GS_ALL_DONE;
1604 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1606 static enum gimplify_status
1607 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1609 struct gimplify_ctx *ctxp;
1610 glabel *label_stmt;
1612 /* Invalid programs can play Duff's Device type games with, for example,
1613 #pragma omp parallel. At least in the C front end, we don't
1614 detect such invalid branches until after gimplification, in the
1615 diagnose_omp_blocks pass. */
1616 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1617 if (ctxp->case_labels.exists ())
1618 break;
1620 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1621 ctxp->case_labels.safe_push (*expr_p);
1622 gimplify_seq_add_stmt (pre_p, label_stmt);
1624 return GS_ALL_DONE;
1627 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1628 if necessary. */
1630 tree
1631 build_and_jump (tree *label_p)
1633 if (label_p == NULL)
1634 /* If there's nowhere to jump, just fall through. */
1635 return NULL_TREE;
1637 if (*label_p == NULL_TREE)
1639 tree label = create_artificial_label (UNKNOWN_LOCATION);
1640 *label_p = label;
1643 return build1 (GOTO_EXPR, void_type_node, *label_p);
1646 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1647 This also involves building a label to jump to and communicating it to
1648 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1650 static enum gimplify_status
1651 gimplify_exit_expr (tree *expr_p)
1653 tree cond = TREE_OPERAND (*expr_p, 0);
1654 tree expr;
1656 expr = build_and_jump (&gimplify_ctxp->exit_label);
1657 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1658 *expr_p = expr;
1660 return GS_OK;
1663 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1664 different from its canonical type, wrap the whole thing inside a
1665 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1666 type.
1668 The canonical type of a COMPONENT_REF is the type of the field being
1669 referenced--unless the field is a bit-field which can be read directly
1670 in a smaller mode, in which case the canonical type is the
1671 sign-appropriate type corresponding to that mode. */
1673 static void
1674 canonicalize_component_ref (tree *expr_p)
1676 tree expr = *expr_p;
1677 tree type;
1679 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1681 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1682 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1683 else
1684 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1686 /* One could argue that all the stuff below is not necessary for
1687 the non-bitfield case and declare it a FE error if type
1688 adjustment would be needed. */
1689 if (TREE_TYPE (expr) != type)
1691 #ifdef ENABLE_TYPES_CHECKING
1692 tree old_type = TREE_TYPE (expr);
1693 #endif
1694 int type_quals;
1696 /* We need to preserve qualifiers and propagate them from
1697 operand 0. */
1698 type_quals = TYPE_QUALS (type)
1699 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1700 if (TYPE_QUALS (type) != type_quals)
1701 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1703 /* Set the type of the COMPONENT_REF to the underlying type. */
1704 TREE_TYPE (expr) = type;
1706 #ifdef ENABLE_TYPES_CHECKING
1707 /* It is now a FE error, if the conversion from the canonical
1708 type to the original expression type is not useless. */
1709 gcc_assert (useless_type_conversion_p (old_type, type));
1710 #endif
1714 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1715 to foo, embed that change in the ADDR_EXPR by converting
1716 T array[U];
1717 (T *)&array
1719 &array[L]
1720 where L is the lower bound. For simplicity, only do this for constant
1721 lower bound.
1722 The constraint is that the type of &array[L] is trivially convertible
1723 to T *. */
1725 static void
1726 canonicalize_addr_expr (tree *expr_p)
1728 tree expr = *expr_p;
1729 tree addr_expr = TREE_OPERAND (expr, 0);
1730 tree datype, ddatype, pddatype;
1732 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1733 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1734 || TREE_CODE (addr_expr) != ADDR_EXPR)
1735 return;
1737 /* The addr_expr type should be a pointer to an array. */
1738 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1739 if (TREE_CODE (datype) != ARRAY_TYPE)
1740 return;
1742 /* The pointer to element type shall be trivially convertible to
1743 the expression pointer type. */
1744 ddatype = TREE_TYPE (datype);
1745 pddatype = build_pointer_type (ddatype);
1746 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1747 pddatype))
1748 return;
1750 /* The lower bound and element sizes must be constant. */
1751 if (!TYPE_SIZE_UNIT (ddatype)
1752 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1753 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1754 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1755 return;
1757 /* All checks succeeded. Build a new node to merge the cast. */
1758 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1759 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1760 NULL_TREE, NULL_TREE);
1761 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1763 /* We can have stripped a required restrict qualifier above. */
1764 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1765 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1768 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1769 underneath as appropriate. */
1771 static enum gimplify_status
1772 gimplify_conversion (tree *expr_p)
1774 location_t loc = EXPR_LOCATION (*expr_p);
1775 gcc_assert (CONVERT_EXPR_P (*expr_p));
1777 /* Then strip away all but the outermost conversion. */
1778 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1780 /* And remove the outermost conversion if it's useless. */
1781 if (tree_ssa_useless_type_conversion (*expr_p))
1782 *expr_p = TREE_OPERAND (*expr_p, 0);
1784 /* If we still have a conversion at the toplevel,
1785 then canonicalize some constructs. */
1786 if (CONVERT_EXPR_P (*expr_p))
1788 tree sub = TREE_OPERAND (*expr_p, 0);
1790 /* If a NOP conversion is changing the type of a COMPONENT_REF
1791 expression, then canonicalize its type now in order to expose more
1792 redundant conversions. */
1793 if (TREE_CODE (sub) == COMPONENT_REF)
1794 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1796 /* If a NOP conversion is changing a pointer to array of foo
1797 to a pointer to foo, embed that change in the ADDR_EXPR. */
1798 else if (TREE_CODE (sub) == ADDR_EXPR)
1799 canonicalize_addr_expr (expr_p);
1802 /* If we have a conversion to a non-register type force the
1803 use of a VIEW_CONVERT_EXPR instead. */
1804 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1805 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1806 TREE_OPERAND (*expr_p, 0));
1808 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1809 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1810 TREE_SET_CODE (*expr_p, NOP_EXPR);
1812 return GS_OK;
1815 /* Nonlocal VLAs seen in the current function. */
1816 static hash_set<tree> *nonlocal_vlas;
1818 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1819 static tree nonlocal_vla_vars;
1821 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1822 DECL_VALUE_EXPR, and it's worth re-examining things. */
1824 static enum gimplify_status
1825 gimplify_var_or_parm_decl (tree *expr_p)
1827 tree decl = *expr_p;
1829 /* ??? If this is a local variable, and it has not been seen in any
1830 outer BIND_EXPR, then it's probably the result of a duplicate
1831 declaration, for which we've already issued an error. It would
1832 be really nice if the front end wouldn't leak these at all.
1833 Currently the only known culprit is C++ destructors, as seen
1834 in g++.old-deja/g++.jason/binding.C. */
1835 if (TREE_CODE (decl) == VAR_DECL
1836 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1837 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1838 && decl_function_context (decl) == current_function_decl)
1840 gcc_assert (seen_error ());
1841 return GS_ERROR;
1844 /* When within an OMP context, notice uses of variables. */
1845 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1846 return GS_ALL_DONE;
1848 /* If the decl is an alias for another expression, substitute it now. */
1849 if (DECL_HAS_VALUE_EXPR_P (decl))
1851 tree value_expr = DECL_VALUE_EXPR (decl);
1853 /* For referenced nonlocal VLAs add a decl for debugging purposes
1854 to the current function. */
1855 if (TREE_CODE (decl) == VAR_DECL
1856 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1857 && nonlocal_vlas != NULL
1858 && TREE_CODE (value_expr) == INDIRECT_REF
1859 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1860 && decl_function_context (decl) != current_function_decl)
1862 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1863 while (ctx
1864 && (ctx->region_type == ORT_WORKSHARE
1865 || ctx->region_type == ORT_SIMD
1866 || ctx->region_type == ORT_ACC))
1867 ctx = ctx->outer_context;
1868 if (!ctx && !nonlocal_vlas->add (decl))
1870 tree copy = copy_node (decl);
1872 lang_hooks.dup_lang_specific_decl (copy);
1873 SET_DECL_RTL (copy, 0);
1874 TREE_USED (copy) = 1;
1875 DECL_CHAIN (copy) = nonlocal_vla_vars;
1876 nonlocal_vla_vars = copy;
1877 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1878 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1882 *expr_p = unshare_expr (value_expr);
1883 return GS_OK;
1886 return GS_ALL_DONE;
1889 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1891 static void
1892 recalculate_side_effects (tree t)
1894 enum tree_code code = TREE_CODE (t);
1895 int len = TREE_OPERAND_LENGTH (t);
1896 int i;
1898 switch (TREE_CODE_CLASS (code))
1900 case tcc_expression:
1901 switch (code)
1903 case INIT_EXPR:
1904 case MODIFY_EXPR:
1905 case VA_ARG_EXPR:
1906 case PREDECREMENT_EXPR:
1907 case PREINCREMENT_EXPR:
1908 case POSTDECREMENT_EXPR:
1909 case POSTINCREMENT_EXPR:
1910 /* All of these have side-effects, no matter what their
1911 operands are. */
1912 return;
1914 default:
1915 break;
1917 /* Fall through. */
1919 case tcc_comparison: /* a comparison expression */
1920 case tcc_unary: /* a unary arithmetic expression */
1921 case tcc_binary: /* a binary arithmetic expression */
1922 case tcc_reference: /* a reference */
1923 case tcc_vl_exp: /* a function call */
1924 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1925 for (i = 0; i < len; ++i)
1927 tree op = TREE_OPERAND (t, i);
1928 if (op && TREE_SIDE_EFFECTS (op))
1929 TREE_SIDE_EFFECTS (t) = 1;
1931 break;
1933 case tcc_constant:
1934 /* No side-effects. */
1935 return;
1937 default:
1938 gcc_unreachable ();
1942 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1943 node *EXPR_P.
1945 compound_lval
1946 : min_lval '[' val ']'
1947 | min_lval '.' ID
1948 | compound_lval '[' val ']'
1949 | compound_lval '.' ID
1951 This is not part of the original SIMPLE definition, which separates
1952 array and member references, but it seems reasonable to handle them
1953 together. Also, this way we don't run into problems with union
1954 aliasing; gcc requires that for accesses through a union to alias, the
1955 union reference must be explicit, which was not always the case when we
1956 were splitting up array and member refs.
1958 PRE_P points to the sequence where side effects that must happen before
1959 *EXPR_P should be stored.
1961 POST_P points to the sequence where side effects that must happen after
1962 *EXPR_P should be stored. */
1964 static enum gimplify_status
1965 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1966 fallback_t fallback)
1968 tree *p;
1969 enum gimplify_status ret = GS_ALL_DONE, tret;
1970 int i;
1971 location_t loc = EXPR_LOCATION (*expr_p);
1972 tree expr = *expr_p;
1974 /* Create a stack of the subexpressions so later we can walk them in
1975 order from inner to outer. */
1976 auto_vec<tree, 10> expr_stack;
1978 /* We can handle anything that get_inner_reference can deal with. */
1979 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1981 restart:
1982 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1983 if (TREE_CODE (*p) == INDIRECT_REF)
1984 *p = fold_indirect_ref_loc (loc, *p);
1986 if (handled_component_p (*p))
1988 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1989 additional COMPONENT_REFs. */
1990 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1991 && gimplify_var_or_parm_decl (p) == GS_OK)
1992 goto restart;
1993 else
1994 break;
1996 expr_stack.safe_push (*p);
1999 gcc_assert (expr_stack.length ());
2001 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2002 walked through and P points to the innermost expression.
2004 Java requires that we elaborated nodes in source order. That
2005 means we must gimplify the inner expression followed by each of
2006 the indices, in order. But we can't gimplify the inner
2007 expression until we deal with any variable bounds, sizes, or
2008 positions in order to deal with PLACEHOLDER_EXPRs.
2010 So we do this in three steps. First we deal with the annotations
2011 for any variables in the components, then we gimplify the base,
2012 then we gimplify any indices, from left to right. */
2013 for (i = expr_stack.length () - 1; i >= 0; i--)
2015 tree t = expr_stack[i];
2017 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2019 /* Gimplify the low bound and element type size and put them into
2020 the ARRAY_REF. If these values are set, they have already been
2021 gimplified. */
2022 if (TREE_OPERAND (t, 2) == NULL_TREE)
2024 tree low = unshare_expr (array_ref_low_bound (t));
2025 if (!is_gimple_min_invariant (low))
2027 TREE_OPERAND (t, 2) = low;
2028 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2029 post_p, is_gimple_reg,
2030 fb_rvalue);
2031 ret = MIN (ret, tret);
2034 else
2036 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2037 is_gimple_reg, fb_rvalue);
2038 ret = MIN (ret, tret);
2041 if (TREE_OPERAND (t, 3) == NULL_TREE)
2043 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2044 tree elmt_size = unshare_expr (array_ref_element_size (t));
2045 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2047 /* Divide the element size by the alignment of the element
2048 type (above). */
2049 elmt_size
2050 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2052 if (!is_gimple_min_invariant (elmt_size))
2054 TREE_OPERAND (t, 3) = elmt_size;
2055 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2056 post_p, is_gimple_reg,
2057 fb_rvalue);
2058 ret = MIN (ret, tret);
2061 else
2063 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2064 is_gimple_reg, fb_rvalue);
2065 ret = MIN (ret, tret);
2068 else if (TREE_CODE (t) == COMPONENT_REF)
2070 /* Set the field offset into T and gimplify it. */
2071 if (TREE_OPERAND (t, 2) == NULL_TREE)
2073 tree offset = unshare_expr (component_ref_field_offset (t));
2074 tree field = TREE_OPERAND (t, 1);
2075 tree factor
2076 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2078 /* Divide the offset by its alignment. */
2079 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2081 if (!is_gimple_min_invariant (offset))
2083 TREE_OPERAND (t, 2) = offset;
2084 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2085 post_p, is_gimple_reg,
2086 fb_rvalue);
2087 ret = MIN (ret, tret);
2090 else
2092 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2093 is_gimple_reg, fb_rvalue);
2094 ret = MIN (ret, tret);
2099 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2100 so as to match the min_lval predicate. Failure to do so may result
2101 in the creation of large aggregate temporaries. */
2102 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2103 fallback | fb_lvalue);
2104 ret = MIN (ret, tret);
2106 /* And finally, the indices and operands of ARRAY_REF. During this
2107 loop we also remove any useless conversions. */
2108 for (; expr_stack.length () > 0; )
2110 tree t = expr_stack.pop ();
2112 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2114 /* Gimplify the dimension. */
2115 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2117 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2118 is_gimple_val, fb_rvalue);
2119 ret = MIN (ret, tret);
2123 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2125 /* The innermost expression P may have originally had
2126 TREE_SIDE_EFFECTS set which would have caused all the outer
2127 expressions in *EXPR_P leading to P to also have had
2128 TREE_SIDE_EFFECTS set. */
2129 recalculate_side_effects (t);
2132 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2133 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2135 canonicalize_component_ref (expr_p);
2138 expr_stack.release ();
2140 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2142 return ret;
2145 /* Gimplify the self modifying expression pointed to by EXPR_P
2146 (++, --, +=, -=).
2148 PRE_P points to the list where side effects that must happen before
2149 *EXPR_P should be stored.
2151 POST_P points to the list where side effects that must happen after
2152 *EXPR_P should be stored.
2154 WANT_VALUE is nonzero iff we want to use the value of this expression
2155 in another expression.
2157 ARITH_TYPE is the type the computation should be performed in. */
2159 enum gimplify_status
2160 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2161 bool want_value, tree arith_type)
2163 enum tree_code code;
2164 tree lhs, lvalue, rhs, t1;
2165 gimple_seq post = NULL, *orig_post_p = post_p;
2166 bool postfix;
2167 enum tree_code arith_code;
2168 enum gimplify_status ret;
2169 location_t loc = EXPR_LOCATION (*expr_p);
2171 code = TREE_CODE (*expr_p);
2173 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2174 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2176 /* Prefix or postfix? */
2177 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2178 /* Faster to treat as prefix if result is not used. */
2179 postfix = want_value;
2180 else
2181 postfix = false;
2183 /* For postfix, make sure the inner expression's post side effects
2184 are executed after side effects from this expression. */
2185 if (postfix)
2186 post_p = &post;
2188 /* Add or subtract? */
2189 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2190 arith_code = PLUS_EXPR;
2191 else
2192 arith_code = MINUS_EXPR;
2194 /* Gimplify the LHS into a GIMPLE lvalue. */
2195 lvalue = TREE_OPERAND (*expr_p, 0);
2196 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2197 if (ret == GS_ERROR)
2198 return ret;
2200 /* Extract the operands to the arithmetic operation. */
2201 lhs = lvalue;
2202 rhs = TREE_OPERAND (*expr_p, 1);
2204 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2205 that as the result value and in the postqueue operation. */
2206 if (postfix)
2208 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2209 if (ret == GS_ERROR)
2210 return ret;
2212 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2215 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2216 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2218 rhs = convert_to_ptrofftype_loc (loc, rhs);
2219 if (arith_code == MINUS_EXPR)
2220 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2221 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2223 else
2224 t1 = fold_convert (TREE_TYPE (*expr_p),
2225 fold_build2 (arith_code, arith_type,
2226 fold_convert (arith_type, lhs),
2227 fold_convert (arith_type, rhs)));
2229 if (postfix)
2231 gimplify_assign (lvalue, t1, pre_p);
2232 gimplify_seq_add_seq (orig_post_p, post);
2233 *expr_p = lhs;
2234 return GS_ALL_DONE;
2236 else
2238 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2239 return GS_OK;
2243 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2245 static void
2246 maybe_with_size_expr (tree *expr_p)
2248 tree expr = *expr_p;
2249 tree type = TREE_TYPE (expr);
2250 tree size;
2252 /* If we've already wrapped this or the type is error_mark_node, we can't do
2253 anything. */
2254 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2255 || type == error_mark_node)
2256 return;
2258 /* If the size isn't known or is a constant, we have nothing to do. */
2259 size = TYPE_SIZE_UNIT (type);
2260 if (!size || TREE_CODE (size) == INTEGER_CST)
2261 return;
2263 /* Otherwise, make a WITH_SIZE_EXPR. */
2264 size = unshare_expr (size);
2265 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2266 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2269 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2270 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2271 the CALL_EXPR. */
2273 enum gimplify_status
2274 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2276 bool (*test) (tree);
2277 fallback_t fb;
2279 /* In general, we allow lvalues for function arguments to avoid
2280 extra overhead of copying large aggregates out of even larger
2281 aggregates into temporaries only to copy the temporaries to
2282 the argument list. Make optimizers happy by pulling out to
2283 temporaries those types that fit in registers. */
2284 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2285 test = is_gimple_val, fb = fb_rvalue;
2286 else
2288 test = is_gimple_lvalue, fb = fb_either;
2289 /* Also strip a TARGET_EXPR that would force an extra copy. */
2290 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2292 tree init = TARGET_EXPR_INITIAL (*arg_p);
2293 if (init
2294 && !VOID_TYPE_P (TREE_TYPE (init)))
2295 *arg_p = init;
2299 /* If this is a variable sized type, we must remember the size. */
2300 maybe_with_size_expr (arg_p);
2302 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2303 /* Make sure arguments have the same location as the function call
2304 itself. */
2305 protected_set_expr_location (*arg_p, call_location);
2307 /* There is a sequence point before a function call. Side effects in
2308 the argument list must occur before the actual call. So, when
2309 gimplifying arguments, force gimplify_expr to use an internal
2310 post queue which is then appended to the end of PRE_P. */
2311 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2314 /* Don't fold inside offloading or taskreg regions: it can break code by
2315 adding decl references that weren't in the source. We'll do it during
2316 omplower pass instead. */
2318 static bool
2319 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2321 struct gimplify_omp_ctx *ctx;
2322 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2323 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
2324 return false;
2325 return fold_stmt (gsi);
2328 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2329 WANT_VALUE is true if the result of the call is desired. */
2331 static enum gimplify_status
2332 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2334 tree fndecl, parms, p, fnptrtype;
2335 enum gimplify_status ret;
2336 int i, nargs;
2337 gcall *call;
2338 bool builtin_va_start_p = false;
2339 location_t loc = EXPR_LOCATION (*expr_p);
2341 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2343 /* For reliable diagnostics during inlining, it is necessary that
2344 every call_expr be annotated with file and line. */
2345 if (! EXPR_HAS_LOCATION (*expr_p))
2346 SET_EXPR_LOCATION (*expr_p, input_location);
2348 /* Gimplify internal functions created in the FEs. */
2349 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2351 if (want_value)
2352 return GS_ALL_DONE;
2354 nargs = call_expr_nargs (*expr_p);
2355 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2356 auto_vec<tree> vargs (nargs);
2358 for (i = 0; i < nargs; i++)
2360 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2361 EXPR_LOCATION (*expr_p));
2362 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2364 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
2365 gimplify_seq_add_stmt (pre_p, call);
2366 return GS_ALL_DONE;
2369 /* This may be a call to a builtin function.
2371 Builtin function calls may be transformed into different
2372 (and more efficient) builtin function calls under certain
2373 circumstances. Unfortunately, gimplification can muck things
2374 up enough that the builtin expanders are not aware that certain
2375 transformations are still valid.
2377 So we attempt transformation/gimplification of the call before
2378 we gimplify the CALL_EXPR. At this time we do not manage to
2379 transform all calls in the same manner as the expanders do, but
2380 we do transform most of them. */
2381 fndecl = get_callee_fndecl (*expr_p);
2382 if (fndecl
2383 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2384 switch (DECL_FUNCTION_CODE (fndecl))
2386 case BUILT_IN_ALLOCA:
2387 case BUILT_IN_ALLOCA_WITH_ALIGN:
2388 /* If the call has been built for a variable-sized object, then we
2389 want to restore the stack level when the enclosing BIND_EXPR is
2390 exited to reclaim the allocated space; otherwise, we precisely
2391 need to do the opposite and preserve the latest stack level. */
2392 if (CALL_ALLOCA_FOR_VAR_P (*expr_p))
2393 gimplify_ctxp->save_stack = true;
2394 else
2395 gimplify_ctxp->keep_stack = true;
2396 break;
2398 case BUILT_IN_VA_START:
2400 builtin_va_start_p = TRUE;
2401 if (call_expr_nargs (*expr_p) < 2)
2403 error ("too few arguments to function %<va_start%>");
2404 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2405 return GS_OK;
2408 if (fold_builtin_next_arg (*expr_p, true))
2410 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2411 return GS_OK;
2413 break;
2415 case BUILT_IN_LINE:
2417 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2418 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2419 return GS_OK;
2421 case BUILT_IN_FILE:
2423 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2424 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2425 return GS_OK;
2427 case BUILT_IN_FUNCTION:
2429 const char *function;
2430 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2431 *expr_p = build_string_literal (strlen (function) + 1, function);
2432 return GS_OK;
2434 default:
2437 if (fndecl && DECL_BUILT_IN (fndecl))
2439 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2440 if (new_tree && new_tree != *expr_p)
2442 /* There was a transformation of this call which computes the
2443 same value, but in a more efficient way. Return and try
2444 again. */
2445 *expr_p = new_tree;
2446 return GS_OK;
2450 /* Remember the original function pointer type. */
2451 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2453 /* There is a sequence point before the call, so any side effects in
2454 the calling expression must occur before the actual call. Force
2455 gimplify_expr to use an internal post queue. */
2456 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2457 is_gimple_call_addr, fb_rvalue);
2459 nargs = call_expr_nargs (*expr_p);
2461 /* Get argument types for verification. */
2462 fndecl = get_callee_fndecl (*expr_p);
2463 parms = NULL_TREE;
2464 if (fndecl)
2465 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2466 else
2467 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2469 if (fndecl && DECL_ARGUMENTS (fndecl))
2470 p = DECL_ARGUMENTS (fndecl);
2471 else if (parms)
2472 p = parms;
2473 else
2474 p = NULL_TREE;
2475 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2478 /* If the last argument is __builtin_va_arg_pack () and it is not
2479 passed as a named argument, decrease the number of CALL_EXPR
2480 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2481 if (!p
2482 && i < nargs
2483 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2485 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2486 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2488 if (last_arg_fndecl
2489 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2490 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2491 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2493 tree call = *expr_p;
2495 --nargs;
2496 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2497 CALL_EXPR_FN (call),
2498 nargs, CALL_EXPR_ARGP (call));
2500 /* Copy all CALL_EXPR flags, location and block, except
2501 CALL_EXPR_VA_ARG_PACK flag. */
2502 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2503 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2504 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2505 = CALL_EXPR_RETURN_SLOT_OPT (call);
2506 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2507 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2509 /* Set CALL_EXPR_VA_ARG_PACK. */
2510 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2514 /* Gimplify the function arguments. */
2515 if (nargs > 0)
2517 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2518 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2519 PUSH_ARGS_REVERSED ? i-- : i++)
2521 enum gimplify_status t;
2523 /* Avoid gimplifying the second argument to va_start, which needs to
2524 be the plain PARM_DECL. */
2525 if ((i != 1) || !builtin_va_start_p)
2527 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2528 EXPR_LOCATION (*expr_p));
2530 if (t == GS_ERROR)
2531 ret = GS_ERROR;
2536 /* Gimplify the static chain. */
2537 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2539 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2540 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2541 else
2543 enum gimplify_status t;
2544 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2545 EXPR_LOCATION (*expr_p));
2546 if (t == GS_ERROR)
2547 ret = GS_ERROR;
2551 /* Verify the function result. */
2552 if (want_value && fndecl
2553 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2555 error_at (loc, "using result of function returning %<void%>");
2556 ret = GS_ERROR;
2559 /* Try this again in case gimplification exposed something. */
2560 if (ret != GS_ERROR)
2562 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2564 if (new_tree && new_tree != *expr_p)
2566 /* There was a transformation of this call which computes the
2567 same value, but in a more efficient way. Return and try
2568 again. */
2569 *expr_p = new_tree;
2570 return GS_OK;
2573 else
2575 *expr_p = error_mark_node;
2576 return GS_ERROR;
2579 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2580 decl. This allows us to eliminate redundant or useless
2581 calls to "const" functions. */
2582 if (TREE_CODE (*expr_p) == CALL_EXPR)
2584 int flags = call_expr_flags (*expr_p);
2585 if (flags & (ECF_CONST | ECF_PURE)
2586 /* An infinite loop is considered a side effect. */
2587 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2588 TREE_SIDE_EFFECTS (*expr_p) = 0;
2591 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2592 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2593 form and delegate the creation of a GIMPLE_CALL to
2594 gimplify_modify_expr. This is always possible because when
2595 WANT_VALUE is true, the caller wants the result of this call into
2596 a temporary, which means that we will emit an INIT_EXPR in
2597 internal_get_tmp_var which will then be handled by
2598 gimplify_modify_expr. */
2599 if (!want_value)
2601 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2602 have to do is replicate it as a GIMPLE_CALL tuple. */
2603 gimple_stmt_iterator gsi;
2604 call = gimple_build_call_from_tree (*expr_p);
2605 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2606 notice_special_calls (call);
2607 gimplify_seq_add_stmt (pre_p, call);
2608 gsi = gsi_last (*pre_p);
2609 maybe_fold_stmt (&gsi);
2610 *expr_p = NULL_TREE;
2612 else
2613 /* Remember the original function type. */
2614 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2615 CALL_EXPR_FN (*expr_p));
2617 return ret;
2620 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2621 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2623 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2624 condition is true or false, respectively. If null, we should generate
2625 our own to skip over the evaluation of this specific expression.
2627 LOCUS is the source location of the COND_EXPR.
2629 This function is the tree equivalent of do_jump.
2631 shortcut_cond_r should only be called by shortcut_cond_expr. */
2633 static tree
2634 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2635 location_t locus)
2637 tree local_label = NULL_TREE;
2638 tree t, expr = NULL;
2640 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2641 retain the shortcut semantics. Just insert the gotos here;
2642 shortcut_cond_expr will append the real blocks later. */
2643 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2645 location_t new_locus;
2647 /* Turn if (a && b) into
2649 if (a); else goto no;
2650 if (b) goto yes; else goto no;
2651 (no:) */
2653 if (false_label_p == NULL)
2654 false_label_p = &local_label;
2656 /* Keep the original source location on the first 'if'. */
2657 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2658 append_to_statement_list (t, &expr);
2660 /* Set the source location of the && on the second 'if'. */
2661 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2662 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2663 new_locus);
2664 append_to_statement_list (t, &expr);
2666 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2668 location_t new_locus;
2670 /* Turn if (a || b) into
2672 if (a) goto yes;
2673 if (b) goto yes; else goto no;
2674 (yes:) */
2676 if (true_label_p == NULL)
2677 true_label_p = &local_label;
2679 /* Keep the original source location on the first 'if'. */
2680 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2681 append_to_statement_list (t, &expr);
2683 /* Set the source location of the || on the second 'if'. */
2684 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2685 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2686 new_locus);
2687 append_to_statement_list (t, &expr);
2689 else if (TREE_CODE (pred) == COND_EXPR
2690 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2691 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2693 location_t new_locus;
2695 /* As long as we're messing with gotos, turn if (a ? b : c) into
2696 if (a)
2697 if (b) goto yes; else goto no;
2698 else
2699 if (c) goto yes; else goto no;
2701 Don't do this if one of the arms has void type, which can happen
2702 in C++ when the arm is throw. */
2704 /* Keep the original source location on the first 'if'. Set the source
2705 location of the ? on the second 'if'. */
2706 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2707 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2708 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2709 false_label_p, locus),
2710 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2711 false_label_p, new_locus));
2713 else
2715 expr = build3 (COND_EXPR, void_type_node, pred,
2716 build_and_jump (true_label_p),
2717 build_and_jump (false_label_p));
2718 SET_EXPR_LOCATION (expr, locus);
2721 if (local_label)
2723 t = build1 (LABEL_EXPR, void_type_node, local_label);
2724 append_to_statement_list (t, &expr);
2727 return expr;
2730 /* Given a conditional expression EXPR with short-circuit boolean
2731 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2732 predicate apart into the equivalent sequence of conditionals. */
2734 static tree
2735 shortcut_cond_expr (tree expr)
2737 tree pred = TREE_OPERAND (expr, 0);
2738 tree then_ = TREE_OPERAND (expr, 1);
2739 tree else_ = TREE_OPERAND (expr, 2);
2740 tree true_label, false_label, end_label, t;
2741 tree *true_label_p;
2742 tree *false_label_p;
2743 bool emit_end, emit_false, jump_over_else;
2744 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2745 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2747 /* First do simple transformations. */
2748 if (!else_se)
2750 /* If there is no 'else', turn
2751 if (a && b) then c
2752 into
2753 if (a) if (b) then c. */
2754 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2756 /* Keep the original source location on the first 'if'. */
2757 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2758 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2759 /* Set the source location of the && on the second 'if'. */
2760 if (EXPR_HAS_LOCATION (pred))
2761 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2762 then_ = shortcut_cond_expr (expr);
2763 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2764 pred = TREE_OPERAND (pred, 0);
2765 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2766 SET_EXPR_LOCATION (expr, locus);
2770 if (!then_se)
2772 /* If there is no 'then', turn
2773 if (a || b); else d
2774 into
2775 if (a); else if (b); else d. */
2776 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2778 /* Keep the original source location on the first 'if'. */
2779 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2780 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2781 /* Set the source location of the || on the second 'if'. */
2782 if (EXPR_HAS_LOCATION (pred))
2783 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2784 else_ = shortcut_cond_expr (expr);
2785 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2786 pred = TREE_OPERAND (pred, 0);
2787 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2788 SET_EXPR_LOCATION (expr, locus);
2792 /* If we're done, great. */
2793 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2794 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2795 return expr;
2797 /* Otherwise we need to mess with gotos. Change
2798 if (a) c; else d;
2800 if (a); else goto no;
2801 c; goto end;
2802 no: d; end:
2803 and recursively gimplify the condition. */
2805 true_label = false_label = end_label = NULL_TREE;
2807 /* If our arms just jump somewhere, hijack those labels so we don't
2808 generate jumps to jumps. */
2810 if (then_
2811 && TREE_CODE (then_) == GOTO_EXPR
2812 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2814 true_label = GOTO_DESTINATION (then_);
2815 then_ = NULL;
2816 then_se = false;
2819 if (else_
2820 && TREE_CODE (else_) == GOTO_EXPR
2821 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2823 false_label = GOTO_DESTINATION (else_);
2824 else_ = NULL;
2825 else_se = false;
2828 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2829 if (true_label)
2830 true_label_p = &true_label;
2831 else
2832 true_label_p = NULL;
2834 /* The 'else' branch also needs a label if it contains interesting code. */
2835 if (false_label || else_se)
2836 false_label_p = &false_label;
2837 else
2838 false_label_p = NULL;
2840 /* If there was nothing else in our arms, just forward the label(s). */
2841 if (!then_se && !else_se)
2842 return shortcut_cond_r (pred, true_label_p, false_label_p,
2843 EXPR_LOC_OR_LOC (expr, input_location));
2845 /* If our last subexpression already has a terminal label, reuse it. */
2846 if (else_se)
2847 t = expr_last (else_);
2848 else if (then_se)
2849 t = expr_last (then_);
2850 else
2851 t = NULL;
2852 if (t && TREE_CODE (t) == LABEL_EXPR)
2853 end_label = LABEL_EXPR_LABEL (t);
2855 /* If we don't care about jumping to the 'else' branch, jump to the end
2856 if the condition is false. */
2857 if (!false_label_p)
2858 false_label_p = &end_label;
2860 /* We only want to emit these labels if we aren't hijacking them. */
2861 emit_end = (end_label == NULL_TREE);
2862 emit_false = (false_label == NULL_TREE);
2864 /* We only emit the jump over the else clause if we have to--if the
2865 then clause may fall through. Otherwise we can wind up with a
2866 useless jump and a useless label at the end of gimplified code,
2867 which will cause us to think that this conditional as a whole
2868 falls through even if it doesn't. If we then inline a function
2869 which ends with such a condition, that can cause us to issue an
2870 inappropriate warning about control reaching the end of a
2871 non-void function. */
2872 jump_over_else = block_may_fallthru (then_);
2874 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2875 EXPR_LOC_OR_LOC (expr, input_location));
2877 expr = NULL;
2878 append_to_statement_list (pred, &expr);
2880 append_to_statement_list (then_, &expr);
2881 if (else_se)
2883 if (jump_over_else)
2885 tree last = expr_last (expr);
2886 t = build_and_jump (&end_label);
2887 if (EXPR_HAS_LOCATION (last))
2888 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2889 append_to_statement_list (t, &expr);
2891 if (emit_false)
2893 t = build1 (LABEL_EXPR, void_type_node, false_label);
2894 append_to_statement_list (t, &expr);
2896 append_to_statement_list (else_, &expr);
2898 if (emit_end && end_label)
2900 t = build1 (LABEL_EXPR, void_type_node, end_label);
2901 append_to_statement_list (t, &expr);
2904 return expr;
2907 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2909 tree
2910 gimple_boolify (tree expr)
2912 tree type = TREE_TYPE (expr);
2913 location_t loc = EXPR_LOCATION (expr);
2915 if (TREE_CODE (expr) == NE_EXPR
2916 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2917 && integer_zerop (TREE_OPERAND (expr, 1)))
2919 tree call = TREE_OPERAND (expr, 0);
2920 tree fn = get_callee_fndecl (call);
2922 /* For __builtin_expect ((long) (x), y) recurse into x as well
2923 if x is truth_value_p. */
2924 if (fn
2925 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2926 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2927 && call_expr_nargs (call) == 2)
2929 tree arg = CALL_EXPR_ARG (call, 0);
2930 if (arg)
2932 if (TREE_CODE (arg) == NOP_EXPR
2933 && TREE_TYPE (arg) == TREE_TYPE (call))
2934 arg = TREE_OPERAND (arg, 0);
2935 if (truth_value_p (TREE_CODE (arg)))
2937 arg = gimple_boolify (arg);
2938 CALL_EXPR_ARG (call, 0)
2939 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2945 switch (TREE_CODE (expr))
2947 case TRUTH_AND_EXPR:
2948 case TRUTH_OR_EXPR:
2949 case TRUTH_XOR_EXPR:
2950 case TRUTH_ANDIF_EXPR:
2951 case TRUTH_ORIF_EXPR:
2952 /* Also boolify the arguments of truth exprs. */
2953 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2954 /* FALLTHRU */
2956 case TRUTH_NOT_EXPR:
2957 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2959 /* These expressions always produce boolean results. */
2960 if (TREE_CODE (type) != BOOLEAN_TYPE)
2961 TREE_TYPE (expr) = boolean_type_node;
2962 return expr;
2964 case ANNOTATE_EXPR:
2965 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2967 case annot_expr_ivdep_kind:
2968 case annot_expr_no_vector_kind:
2969 case annot_expr_vector_kind:
2970 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2971 if (TREE_CODE (type) != BOOLEAN_TYPE)
2972 TREE_TYPE (expr) = boolean_type_node;
2973 return expr;
2974 default:
2975 gcc_unreachable ();
2978 default:
2979 if (COMPARISON_CLASS_P (expr))
2981 /* There expressions always prduce boolean results. */
2982 if (TREE_CODE (type) != BOOLEAN_TYPE)
2983 TREE_TYPE (expr) = boolean_type_node;
2984 return expr;
2986 /* Other expressions that get here must have boolean values, but
2987 might need to be converted to the appropriate mode. */
2988 if (TREE_CODE (type) == BOOLEAN_TYPE)
2989 return expr;
2990 return fold_convert_loc (loc, boolean_type_node, expr);
2994 /* Given a conditional expression *EXPR_P without side effects, gimplify
2995 its operands. New statements are inserted to PRE_P. */
2997 static enum gimplify_status
2998 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3000 tree expr = *expr_p, cond;
3001 enum gimplify_status ret, tret;
3002 enum tree_code code;
3004 cond = gimple_boolify (COND_EXPR_COND (expr));
3006 /* We need to handle && and || specially, as their gimplification
3007 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3008 code = TREE_CODE (cond);
3009 if (code == TRUTH_ANDIF_EXPR)
3010 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3011 else if (code == TRUTH_ORIF_EXPR)
3012 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3013 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3014 COND_EXPR_COND (*expr_p) = cond;
3016 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3017 is_gimple_val, fb_rvalue);
3018 ret = MIN (ret, tret);
3019 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3020 is_gimple_val, fb_rvalue);
3022 return MIN (ret, tret);
3025 /* Return true if evaluating EXPR could trap.
3026 EXPR is GENERIC, while tree_could_trap_p can be called
3027 only on GIMPLE. */
3029 static bool
3030 generic_expr_could_trap_p (tree expr)
3032 unsigned i, n;
3034 if (!expr || is_gimple_val (expr))
3035 return false;
3037 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3038 return true;
3040 n = TREE_OPERAND_LENGTH (expr);
3041 for (i = 0; i < n; i++)
3042 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3043 return true;
3045 return false;
3048 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3049 into
3051 if (p) if (p)
3052 t1 = a; a;
3053 else or else
3054 t1 = b; b;
3057 The second form is used when *EXPR_P is of type void.
3059 PRE_P points to the list where side effects that must happen before
3060 *EXPR_P should be stored. */
3062 static enum gimplify_status
3063 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3065 tree expr = *expr_p;
3066 tree type = TREE_TYPE (expr);
3067 location_t loc = EXPR_LOCATION (expr);
3068 tree tmp, arm1, arm2;
3069 enum gimplify_status ret;
3070 tree label_true, label_false, label_cont;
3071 bool have_then_clause_p, have_else_clause_p;
3072 gcond *cond_stmt;
3073 enum tree_code pred_code;
3074 gimple_seq seq = NULL;
3076 /* If this COND_EXPR has a value, copy the values into a temporary within
3077 the arms. */
3078 if (!VOID_TYPE_P (type))
3080 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3081 tree result;
3083 /* If either an rvalue is ok or we do not require an lvalue, create the
3084 temporary. But we cannot do that if the type is addressable. */
3085 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3086 && !TREE_ADDRESSABLE (type))
3088 if (gimplify_ctxp->allow_rhs_cond_expr
3089 /* If either branch has side effects or could trap, it can't be
3090 evaluated unconditionally. */
3091 && !TREE_SIDE_EFFECTS (then_)
3092 && !generic_expr_could_trap_p (then_)
3093 && !TREE_SIDE_EFFECTS (else_)
3094 && !generic_expr_could_trap_p (else_))
3095 return gimplify_pure_cond_expr (expr_p, pre_p);
3097 tmp = create_tmp_var (type, "iftmp");
3098 result = tmp;
3101 /* Otherwise, only create and copy references to the values. */
3102 else
3104 type = build_pointer_type (type);
3106 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3107 then_ = build_fold_addr_expr_loc (loc, then_);
3109 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3110 else_ = build_fold_addr_expr_loc (loc, else_);
3112 expr
3113 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3115 tmp = create_tmp_var (type, "iftmp");
3116 result = build_simple_mem_ref_loc (loc, tmp);
3119 /* Build the new then clause, `tmp = then_;'. But don't build the
3120 assignment if the value is void; in C++ it can be if it's a throw. */
3121 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3122 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3124 /* Similarly, build the new else clause, `tmp = else_;'. */
3125 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3126 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3128 TREE_TYPE (expr) = void_type_node;
3129 recalculate_side_effects (expr);
3131 /* Move the COND_EXPR to the prequeue. */
3132 gimplify_stmt (&expr, pre_p);
3134 *expr_p = result;
3135 return GS_ALL_DONE;
3138 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3139 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3140 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3141 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3143 /* Make sure the condition has BOOLEAN_TYPE. */
3144 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3146 /* Break apart && and || conditions. */
3147 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3148 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3150 expr = shortcut_cond_expr (expr);
3152 if (expr != *expr_p)
3154 *expr_p = expr;
3156 /* We can't rely on gimplify_expr to re-gimplify the expanded
3157 form properly, as cleanups might cause the target labels to be
3158 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3159 set up a conditional context. */
3160 gimple_push_condition ();
3161 gimplify_stmt (expr_p, &seq);
3162 gimple_pop_condition (pre_p);
3163 gimple_seq_add_seq (pre_p, seq);
3165 return GS_ALL_DONE;
3169 /* Now do the normal gimplification. */
3171 /* Gimplify condition. */
3172 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3173 fb_rvalue);
3174 if (ret == GS_ERROR)
3175 return GS_ERROR;
3176 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3178 gimple_push_condition ();
3180 have_then_clause_p = have_else_clause_p = false;
3181 if (TREE_OPERAND (expr, 1) != NULL
3182 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3183 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3184 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3185 == current_function_decl)
3186 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3187 have different locations, otherwise we end up with incorrect
3188 location information on the branches. */
3189 && (optimize
3190 || !EXPR_HAS_LOCATION (expr)
3191 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3192 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3194 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3195 have_then_clause_p = true;
3197 else
3198 label_true = create_artificial_label (UNKNOWN_LOCATION);
3199 if (TREE_OPERAND (expr, 2) != NULL
3200 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3201 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3202 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3203 == current_function_decl)
3204 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3205 have different locations, otherwise we end up with incorrect
3206 location information on the branches. */
3207 && (optimize
3208 || !EXPR_HAS_LOCATION (expr)
3209 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3210 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3212 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3213 have_else_clause_p = true;
3215 else
3216 label_false = create_artificial_label (UNKNOWN_LOCATION);
3218 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3219 &arm2);
3220 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3221 label_false);
3222 gimplify_seq_add_stmt (&seq, cond_stmt);
3223 gimple_stmt_iterator gsi = gsi_last (seq);
3224 maybe_fold_stmt (&gsi);
3226 label_cont = NULL_TREE;
3227 if (!have_then_clause_p)
3229 /* For if (...) {} else { code; } put label_true after
3230 the else block. */
3231 if (TREE_OPERAND (expr, 1) == NULL_TREE
3232 && !have_else_clause_p
3233 && TREE_OPERAND (expr, 2) != NULL_TREE)
3234 label_cont = label_true;
3235 else
3237 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3238 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3239 /* For if (...) { code; } else {} or
3240 if (...) { code; } else goto label; or
3241 if (...) { code; return; } else { ... }
3242 label_cont isn't needed. */
3243 if (!have_else_clause_p
3244 && TREE_OPERAND (expr, 2) != NULL_TREE
3245 && gimple_seq_may_fallthru (seq))
3247 gimple *g;
3248 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3250 g = gimple_build_goto (label_cont);
3252 /* GIMPLE_COND's are very low level; they have embedded
3253 gotos. This particular embedded goto should not be marked
3254 with the location of the original COND_EXPR, as it would
3255 correspond to the COND_EXPR's condition, not the ELSE or the
3256 THEN arms. To avoid marking it with the wrong location, flag
3257 it as "no location". */
3258 gimple_set_do_not_emit_location (g);
3260 gimplify_seq_add_stmt (&seq, g);
3264 if (!have_else_clause_p)
3266 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3267 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3269 if (label_cont)
3270 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3272 gimple_pop_condition (pre_p);
3273 gimple_seq_add_seq (pre_p, seq);
3275 if (ret == GS_ERROR)
3276 ; /* Do nothing. */
3277 else if (have_then_clause_p || have_else_clause_p)
3278 ret = GS_ALL_DONE;
3279 else
3281 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3282 expr = TREE_OPERAND (expr, 0);
3283 gimplify_stmt (&expr, pre_p);
3286 *expr_p = NULL;
3287 return ret;
3290 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3291 to be marked addressable.
3293 We cannot rely on such an expression being directly markable if a temporary
3294 has been created by the gimplification. In this case, we create another
3295 temporary and initialize it with a copy, which will become a store after we
3296 mark it addressable. This can happen if the front-end passed us something
3297 that it could not mark addressable yet, like a Fortran pass-by-reference
3298 parameter (int) floatvar. */
3300 static void
3301 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3303 while (handled_component_p (*expr_p))
3304 expr_p = &TREE_OPERAND (*expr_p, 0);
3305 if (is_gimple_reg (*expr_p))
3307 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3308 DECL_GIMPLE_REG_P (var) = 0;
3309 *expr_p = var;
3313 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3314 a call to __builtin_memcpy. */
3316 static enum gimplify_status
3317 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3318 gimple_seq *seq_p)
3320 tree t, to, to_ptr, from, from_ptr;
3321 gcall *gs;
3322 location_t loc = EXPR_LOCATION (*expr_p);
3324 to = TREE_OPERAND (*expr_p, 0);
3325 from = TREE_OPERAND (*expr_p, 1);
3327 /* Mark the RHS addressable. Beware that it may not be possible to do so
3328 directly if a temporary has been created by the gimplification. */
3329 prepare_gimple_addressable (&from, seq_p);
3331 mark_addressable (from);
3332 from_ptr = build_fold_addr_expr_loc (loc, from);
3333 gimplify_arg (&from_ptr, seq_p, loc);
3335 mark_addressable (to);
3336 to_ptr = build_fold_addr_expr_loc (loc, to);
3337 gimplify_arg (&to_ptr, seq_p, loc);
3339 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3341 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3343 if (want_value)
3345 /* tmp = memcpy() */
3346 t = create_tmp_var (TREE_TYPE (to_ptr));
3347 gimple_call_set_lhs (gs, t);
3348 gimplify_seq_add_stmt (seq_p, gs);
3350 *expr_p = build_simple_mem_ref (t);
3351 return GS_ALL_DONE;
3354 gimplify_seq_add_stmt (seq_p, gs);
3355 *expr_p = NULL;
3356 return GS_ALL_DONE;
3359 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3360 a call to __builtin_memset. In this case we know that the RHS is
3361 a CONSTRUCTOR with an empty element list. */
3363 static enum gimplify_status
3364 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3365 gimple_seq *seq_p)
3367 tree t, from, to, to_ptr;
3368 gcall *gs;
3369 location_t loc = EXPR_LOCATION (*expr_p);
3371 /* Assert our assumptions, to abort instead of producing wrong code
3372 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3373 not be immediately exposed. */
3374 from = TREE_OPERAND (*expr_p, 1);
3375 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3376 from = TREE_OPERAND (from, 0);
3378 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3379 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3381 /* Now proceed. */
3382 to = TREE_OPERAND (*expr_p, 0);
3384 to_ptr = build_fold_addr_expr_loc (loc, to);
3385 gimplify_arg (&to_ptr, seq_p, loc);
3386 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3388 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3390 if (want_value)
3392 /* tmp = memset() */
3393 t = create_tmp_var (TREE_TYPE (to_ptr));
3394 gimple_call_set_lhs (gs, t);
3395 gimplify_seq_add_stmt (seq_p, gs);
3397 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3398 return GS_ALL_DONE;
3401 gimplify_seq_add_stmt (seq_p, gs);
3402 *expr_p = NULL;
3403 return GS_ALL_DONE;
3406 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3407 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3408 assignment. Return non-null if we detect a potential overlap. */
3410 struct gimplify_init_ctor_preeval_data
3412 /* The base decl of the lhs object. May be NULL, in which case we
3413 have to assume the lhs is indirect. */
3414 tree lhs_base_decl;
3416 /* The alias set of the lhs object. */
3417 alias_set_type lhs_alias_set;
3420 static tree
3421 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3423 struct gimplify_init_ctor_preeval_data *data
3424 = (struct gimplify_init_ctor_preeval_data *) xdata;
3425 tree t = *tp;
3427 /* If we find the base object, obviously we have overlap. */
3428 if (data->lhs_base_decl == t)
3429 return t;
3431 /* If the constructor component is indirect, determine if we have a
3432 potential overlap with the lhs. The only bits of information we
3433 have to go on at this point are addressability and alias sets. */
3434 if ((INDIRECT_REF_P (t)
3435 || TREE_CODE (t) == MEM_REF)
3436 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3437 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3438 return t;
3440 /* If the constructor component is a call, determine if it can hide a
3441 potential overlap with the lhs through an INDIRECT_REF like above.
3442 ??? Ugh - this is completely broken. In fact this whole analysis
3443 doesn't look conservative. */
3444 if (TREE_CODE (t) == CALL_EXPR)
3446 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3448 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3449 if (POINTER_TYPE_P (TREE_VALUE (type))
3450 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3451 && alias_sets_conflict_p (data->lhs_alias_set,
3452 get_alias_set
3453 (TREE_TYPE (TREE_VALUE (type)))))
3454 return t;
3457 if (IS_TYPE_OR_DECL_P (t))
3458 *walk_subtrees = 0;
3459 return NULL;
3462 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3463 force values that overlap with the lhs (as described by *DATA)
3464 into temporaries. */
3466 static void
3467 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3468 struct gimplify_init_ctor_preeval_data *data)
3470 enum gimplify_status one;
3472 /* If the value is constant, then there's nothing to pre-evaluate. */
3473 if (TREE_CONSTANT (*expr_p))
3475 /* Ensure it does not have side effects, it might contain a reference to
3476 the object we're initializing. */
3477 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3478 return;
3481 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3482 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3483 return;
3485 /* Recurse for nested constructors. */
3486 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3488 unsigned HOST_WIDE_INT ix;
3489 constructor_elt *ce;
3490 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3492 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3493 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3495 return;
3498 /* If this is a variable sized type, we must remember the size. */
3499 maybe_with_size_expr (expr_p);
3501 /* Gimplify the constructor element to something appropriate for the rhs
3502 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3503 the gimplifier will consider this a store to memory. Doing this
3504 gimplification now means that we won't have to deal with complicated
3505 language-specific trees, nor trees like SAVE_EXPR that can induce
3506 exponential search behavior. */
3507 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3508 if (one == GS_ERROR)
3510 *expr_p = NULL;
3511 return;
3514 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3515 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3516 always be true for all scalars, since is_gimple_mem_rhs insists on a
3517 temporary variable for them. */
3518 if (DECL_P (*expr_p))
3519 return;
3521 /* If this is of variable size, we have no choice but to assume it doesn't
3522 overlap since we can't make a temporary for it. */
3523 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3524 return;
3526 /* Otherwise, we must search for overlap ... */
3527 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3528 return;
3530 /* ... and if found, force the value into a temporary. */
3531 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3534 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3535 a RANGE_EXPR in a CONSTRUCTOR for an array.
3537 var = lower;
3538 loop_entry:
3539 object[var] = value;
3540 if (var == upper)
3541 goto loop_exit;
3542 var = var + 1;
3543 goto loop_entry;
3544 loop_exit:
3546 We increment var _after_ the loop exit check because we might otherwise
3547 fail if upper == TYPE_MAX_VALUE (type for upper).
3549 Note that we never have to deal with SAVE_EXPRs here, because this has
3550 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3552 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3553 gimple_seq *, bool);
3555 static void
3556 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3557 tree value, tree array_elt_type,
3558 gimple_seq *pre_p, bool cleared)
3560 tree loop_entry_label, loop_exit_label, fall_thru_label;
3561 tree var, var_type, cref, tmp;
3563 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3564 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3565 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3567 /* Create and initialize the index variable. */
3568 var_type = TREE_TYPE (upper);
3569 var = create_tmp_var (var_type);
3570 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3572 /* Add the loop entry label. */
3573 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3575 /* Build the reference. */
3576 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3577 var, NULL_TREE, NULL_TREE);
3579 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3580 the store. Otherwise just assign value to the reference. */
3582 if (TREE_CODE (value) == CONSTRUCTOR)
3583 /* NB we might have to call ourself recursively through
3584 gimplify_init_ctor_eval if the value is a constructor. */
3585 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3586 pre_p, cleared);
3587 else
3588 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3590 /* We exit the loop when the index var is equal to the upper bound. */
3591 gimplify_seq_add_stmt (pre_p,
3592 gimple_build_cond (EQ_EXPR, var, upper,
3593 loop_exit_label, fall_thru_label));
3595 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3597 /* Otherwise, increment the index var... */
3598 tmp = build2 (PLUS_EXPR, var_type, var,
3599 fold_convert (var_type, integer_one_node));
3600 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3602 /* ...and jump back to the loop entry. */
3603 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3605 /* Add the loop exit label. */
3606 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3609 /* Return true if FDECL is accessing a field that is zero sized. */
3611 static bool
3612 zero_sized_field_decl (const_tree fdecl)
3614 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3615 && integer_zerop (DECL_SIZE (fdecl)))
3616 return true;
3617 return false;
3620 /* Return true if TYPE is zero sized. */
3622 static bool
3623 zero_sized_type (const_tree type)
3625 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3626 && integer_zerop (TYPE_SIZE (type)))
3627 return true;
3628 return false;
3631 /* A subroutine of gimplify_init_constructor. Generate individual
3632 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3633 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3634 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3635 zeroed first. */
3637 static void
3638 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3639 gimple_seq *pre_p, bool cleared)
3641 tree array_elt_type = NULL;
3642 unsigned HOST_WIDE_INT ix;
3643 tree purpose, value;
3645 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3646 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3648 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3650 tree cref;
3652 /* NULL values are created above for gimplification errors. */
3653 if (value == NULL)
3654 continue;
3656 if (cleared && initializer_zerop (value))
3657 continue;
3659 /* ??? Here's to hoping the front end fills in all of the indices,
3660 so we don't have to figure out what's missing ourselves. */
3661 gcc_assert (purpose);
3663 /* Skip zero-sized fields, unless value has side-effects. This can
3664 happen with calls to functions returning a zero-sized type, which
3665 we shouldn't discard. As a number of downstream passes don't
3666 expect sets of zero-sized fields, we rely on the gimplification of
3667 the MODIFY_EXPR we make below to drop the assignment statement. */
3668 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3669 continue;
3671 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3672 whole range. */
3673 if (TREE_CODE (purpose) == RANGE_EXPR)
3675 tree lower = TREE_OPERAND (purpose, 0);
3676 tree upper = TREE_OPERAND (purpose, 1);
3678 /* If the lower bound is equal to upper, just treat it as if
3679 upper was the index. */
3680 if (simple_cst_equal (lower, upper))
3681 purpose = upper;
3682 else
3684 gimplify_init_ctor_eval_range (object, lower, upper, value,
3685 array_elt_type, pre_p, cleared);
3686 continue;
3690 if (array_elt_type)
3692 /* Do not use bitsizetype for ARRAY_REF indices. */
3693 if (TYPE_DOMAIN (TREE_TYPE (object)))
3694 purpose
3695 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3696 purpose);
3697 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3698 purpose, NULL_TREE, NULL_TREE);
3700 else
3702 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3703 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3704 unshare_expr (object), purpose, NULL_TREE);
3707 if (TREE_CODE (value) == CONSTRUCTOR
3708 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3709 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3710 pre_p, cleared);
3711 else
3713 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3714 gimplify_and_add (init, pre_p);
3715 ggc_free (init);
3720 /* Return the appropriate RHS predicate for this LHS. */
3722 gimple_predicate
3723 rhs_predicate_for (tree lhs)
3725 if (is_gimple_reg (lhs))
3726 return is_gimple_reg_rhs_or_call;
3727 else
3728 return is_gimple_mem_rhs_or_call;
3731 /* Gimplify a C99 compound literal expression. This just means adding
3732 the DECL_EXPR before the current statement and using its anonymous
3733 decl instead. */
3735 static enum gimplify_status
3736 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3737 bool (*gimple_test_f) (tree),
3738 fallback_t fallback)
3740 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3741 tree decl = DECL_EXPR_DECL (decl_s);
3742 tree init = DECL_INITIAL (decl);
3743 /* Mark the decl as addressable if the compound literal
3744 expression is addressable now, otherwise it is marked too late
3745 after we gimplify the initialization expression. */
3746 if (TREE_ADDRESSABLE (*expr_p))
3747 TREE_ADDRESSABLE (decl) = 1;
3748 /* Otherwise, if we don't need an lvalue and have a literal directly
3749 substitute it. Check if it matches the gimple predicate, as
3750 otherwise we'd generate a new temporary, and we can as well just
3751 use the decl we already have. */
3752 else if (!TREE_ADDRESSABLE (decl)
3753 && init
3754 && (fallback & fb_lvalue) == 0
3755 && gimple_test_f (init))
3757 *expr_p = init;
3758 return GS_OK;
3761 /* Preliminarily mark non-addressed complex variables as eligible
3762 for promotion to gimple registers. We'll transform their uses
3763 as we find them. */
3764 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3765 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3766 && !TREE_THIS_VOLATILE (decl)
3767 && !needs_to_live_in_memory (decl))
3768 DECL_GIMPLE_REG_P (decl) = 1;
3770 /* If the decl is not addressable, then it is being used in some
3771 expression or on the right hand side of a statement, and it can
3772 be put into a readonly data section. */
3773 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3774 TREE_READONLY (decl) = 1;
3776 /* This decl isn't mentioned in the enclosing block, so add it to the
3777 list of temps. FIXME it seems a bit of a kludge to say that
3778 anonymous artificial vars aren't pushed, but everything else is. */
3779 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3780 gimple_add_tmp_var (decl);
3782 gimplify_and_add (decl_s, pre_p);
3783 *expr_p = decl;
3784 return GS_OK;
3787 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3788 return a new CONSTRUCTOR if something changed. */
3790 static tree
3791 optimize_compound_literals_in_ctor (tree orig_ctor)
3793 tree ctor = orig_ctor;
3794 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3795 unsigned int idx, num = vec_safe_length (elts);
3797 for (idx = 0; idx < num; idx++)
3799 tree value = (*elts)[idx].value;
3800 tree newval = value;
3801 if (TREE_CODE (value) == CONSTRUCTOR)
3802 newval = optimize_compound_literals_in_ctor (value);
3803 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3805 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3806 tree decl = DECL_EXPR_DECL (decl_s);
3807 tree init = DECL_INITIAL (decl);
3809 if (!TREE_ADDRESSABLE (value)
3810 && !TREE_ADDRESSABLE (decl)
3811 && init
3812 && TREE_CODE (init) == CONSTRUCTOR)
3813 newval = optimize_compound_literals_in_ctor (init);
3815 if (newval == value)
3816 continue;
3818 if (ctor == orig_ctor)
3820 ctor = copy_node (orig_ctor);
3821 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3822 elts = CONSTRUCTOR_ELTS (ctor);
3824 (*elts)[idx].value = newval;
3826 return ctor;
3829 /* A subroutine of gimplify_modify_expr. Break out elements of a
3830 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3832 Note that we still need to clear any elements that don't have explicit
3833 initializers, so if not all elements are initialized we keep the
3834 original MODIFY_EXPR, we just remove all of the constructor elements.
3836 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3837 GS_ERROR if we would have to create a temporary when gimplifying
3838 this constructor. Otherwise, return GS_OK.
3840 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3842 static enum gimplify_status
3843 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3844 bool want_value, bool notify_temp_creation)
3846 tree object, ctor, type;
3847 enum gimplify_status ret;
3848 vec<constructor_elt, va_gc> *elts;
3850 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3852 if (!notify_temp_creation)
3854 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3855 is_gimple_lvalue, fb_lvalue);
3856 if (ret == GS_ERROR)
3857 return ret;
3860 object = TREE_OPERAND (*expr_p, 0);
3861 ctor = TREE_OPERAND (*expr_p, 1) =
3862 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3863 type = TREE_TYPE (ctor);
3864 elts = CONSTRUCTOR_ELTS (ctor);
3865 ret = GS_ALL_DONE;
3867 switch (TREE_CODE (type))
3869 case RECORD_TYPE:
3870 case UNION_TYPE:
3871 case QUAL_UNION_TYPE:
3872 case ARRAY_TYPE:
3874 struct gimplify_init_ctor_preeval_data preeval_data;
3875 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3876 bool cleared, complete_p, valid_const_initializer;
3878 /* Aggregate types must lower constructors to initialization of
3879 individual elements. The exception is that a CONSTRUCTOR node
3880 with no elements indicates zero-initialization of the whole. */
3881 if (vec_safe_is_empty (elts))
3883 if (notify_temp_creation)
3884 return GS_OK;
3885 break;
3888 /* Fetch information about the constructor to direct later processing.
3889 We might want to make static versions of it in various cases, and
3890 can only do so if it known to be a valid constant initializer. */
3891 valid_const_initializer
3892 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3893 &num_ctor_elements, &complete_p);
3895 /* If a const aggregate variable is being initialized, then it
3896 should never be a lose to promote the variable to be static. */
3897 if (valid_const_initializer
3898 && num_nonzero_elements > 1
3899 && TREE_READONLY (object)
3900 && TREE_CODE (object) == VAR_DECL
3901 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3903 if (notify_temp_creation)
3904 return GS_ERROR;
3905 DECL_INITIAL (object) = ctor;
3906 TREE_STATIC (object) = 1;
3907 if (!DECL_NAME (object))
3908 DECL_NAME (object) = create_tmp_var_name ("C");
3909 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3911 /* ??? C++ doesn't automatically append a .<number> to the
3912 assembler name, and even when it does, it looks at FE private
3913 data structures to figure out what that number should be,
3914 which are not set for this variable. I suppose this is
3915 important for local statics for inline functions, which aren't
3916 "local" in the object file sense. So in order to get a unique
3917 TU-local symbol, we must invoke the lhd version now. */
3918 lhd_set_decl_assembler_name (object);
3920 *expr_p = NULL_TREE;
3921 break;
3924 /* If there are "lots" of initialized elements, even discounting
3925 those that are not address constants (and thus *must* be
3926 computed at runtime), then partition the constructor into
3927 constant and non-constant parts. Block copy the constant
3928 parts in, then generate code for the non-constant parts. */
3929 /* TODO. There's code in cp/typeck.c to do this. */
3931 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3932 /* store_constructor will ignore the clearing of variable-sized
3933 objects. Initializers for such objects must explicitly set
3934 every field that needs to be set. */
3935 cleared = false;
3936 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3937 /* If the constructor isn't complete, clear the whole object
3938 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3940 ??? This ought not to be needed. For any element not present
3941 in the initializer, we should simply set them to zero. Except
3942 we'd need to *find* the elements that are not present, and that
3943 requires trickery to avoid quadratic compile-time behavior in
3944 large cases or excessive memory use in small cases. */
3945 cleared = true;
3946 else if (num_ctor_elements - num_nonzero_elements
3947 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3948 && num_nonzero_elements < num_ctor_elements / 4)
3949 /* If there are "lots" of zeros, it's more efficient to clear
3950 the memory and then set the nonzero elements. */
3951 cleared = true;
3952 else
3953 cleared = false;
3955 /* If there are "lots" of initialized elements, and all of them
3956 are valid address constants, then the entire initializer can
3957 be dropped to memory, and then memcpy'd out. Don't do this
3958 for sparse arrays, though, as it's more efficient to follow
3959 the standard CONSTRUCTOR behavior of memset followed by
3960 individual element initialization. Also don't do this for small
3961 all-zero initializers (which aren't big enough to merit
3962 clearing), and don't try to make bitwise copies of
3963 TREE_ADDRESSABLE types.
3965 We cannot apply such transformation when compiling chkp static
3966 initializer because creation of initializer image in the memory
3967 will require static initialization of bounds for it. It should
3968 result in another gimplification of similar initializer and we
3969 may fall into infinite loop. */
3970 if (valid_const_initializer
3971 && !(cleared || num_nonzero_elements == 0)
3972 && !TREE_ADDRESSABLE (type)
3973 && (!current_function_decl
3974 || !lookup_attribute ("chkp ctor",
3975 DECL_ATTRIBUTES (current_function_decl))))
3977 HOST_WIDE_INT size = int_size_in_bytes (type);
3978 unsigned int align;
3980 /* ??? We can still get unbounded array types, at least
3981 from the C++ front end. This seems wrong, but attempt
3982 to work around it for now. */
3983 if (size < 0)
3985 size = int_size_in_bytes (TREE_TYPE (object));
3986 if (size >= 0)
3987 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3990 /* Find the maximum alignment we can assume for the object. */
3991 /* ??? Make use of DECL_OFFSET_ALIGN. */
3992 if (DECL_P (object))
3993 align = DECL_ALIGN (object);
3994 else
3995 align = TYPE_ALIGN (type);
3997 /* Do a block move either if the size is so small as to make
3998 each individual move a sub-unit move on average, or if it
3999 is so large as to make individual moves inefficient. */
4000 if (size > 0
4001 && num_nonzero_elements > 1
4002 && (size < num_nonzero_elements
4003 || !can_move_by_pieces (size, align)))
4005 if (notify_temp_creation)
4006 return GS_ERROR;
4008 walk_tree (&ctor, force_labels_r, NULL, NULL);
4009 ctor = tree_output_constant_def (ctor);
4010 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4011 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4012 TREE_OPERAND (*expr_p, 1) = ctor;
4014 /* This is no longer an assignment of a CONSTRUCTOR, but
4015 we still may have processing to do on the LHS. So
4016 pretend we didn't do anything here to let that happen. */
4017 return GS_UNHANDLED;
4021 /* If the target is volatile, we have non-zero elements and more than
4022 one field to assign, initialize the target from a temporary. */
4023 if (TREE_THIS_VOLATILE (object)
4024 && !TREE_ADDRESSABLE (type)
4025 && num_nonzero_elements > 0
4026 && vec_safe_length (elts) > 1)
4028 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
4029 TREE_OPERAND (*expr_p, 0) = temp;
4030 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4031 *expr_p,
4032 build2 (MODIFY_EXPR, void_type_node,
4033 object, temp));
4034 return GS_OK;
4037 if (notify_temp_creation)
4038 return GS_OK;
4040 /* If there are nonzero elements and if needed, pre-evaluate to capture
4041 elements overlapping with the lhs into temporaries. We must do this
4042 before clearing to fetch the values before they are zeroed-out. */
4043 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4045 preeval_data.lhs_base_decl = get_base_address (object);
4046 if (!DECL_P (preeval_data.lhs_base_decl))
4047 preeval_data.lhs_base_decl = NULL;
4048 preeval_data.lhs_alias_set = get_alias_set (object);
4050 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4051 pre_p, post_p, &preeval_data);
4054 bool ctor_has_side_effects_p
4055 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
4057 if (cleared)
4059 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4060 Note that we still have to gimplify, in order to handle the
4061 case of variable sized types. Avoid shared tree structures. */
4062 CONSTRUCTOR_ELTS (ctor) = NULL;
4063 TREE_SIDE_EFFECTS (ctor) = 0;
4064 object = unshare_expr (object);
4065 gimplify_stmt (expr_p, pre_p);
4068 /* If we have not block cleared the object, or if there are nonzero
4069 elements in the constructor, or if the constructor has side effects,
4070 add assignments to the individual scalar fields of the object. */
4071 if (!cleared
4072 || num_nonzero_elements > 0
4073 || ctor_has_side_effects_p)
4074 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4076 *expr_p = NULL_TREE;
4078 break;
4080 case COMPLEX_TYPE:
4082 tree r, i;
4084 if (notify_temp_creation)
4085 return GS_OK;
4087 /* Extract the real and imaginary parts out of the ctor. */
4088 gcc_assert (elts->length () == 2);
4089 r = (*elts)[0].value;
4090 i = (*elts)[1].value;
4091 if (r == NULL || i == NULL)
4093 tree zero = build_zero_cst (TREE_TYPE (type));
4094 if (r == NULL)
4095 r = zero;
4096 if (i == NULL)
4097 i = zero;
4100 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4101 represent creation of a complex value. */
4102 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4104 ctor = build_complex (type, r, i);
4105 TREE_OPERAND (*expr_p, 1) = ctor;
4107 else
4109 ctor = build2 (COMPLEX_EXPR, type, r, i);
4110 TREE_OPERAND (*expr_p, 1) = ctor;
4111 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4112 pre_p,
4113 post_p,
4114 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4115 fb_rvalue);
4118 break;
4120 case VECTOR_TYPE:
4122 unsigned HOST_WIDE_INT ix;
4123 constructor_elt *ce;
4125 if (notify_temp_creation)
4126 return GS_OK;
4128 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4129 if (TREE_CONSTANT (ctor))
4131 bool constant_p = true;
4132 tree value;
4134 /* Even when ctor is constant, it might contain non-*_CST
4135 elements, such as addresses or trapping values like
4136 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4137 in VECTOR_CST nodes. */
4138 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4139 if (!CONSTANT_CLASS_P (value))
4141 constant_p = false;
4142 break;
4145 if (constant_p)
4147 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4148 break;
4151 TREE_CONSTANT (ctor) = 0;
4154 /* Vector types use CONSTRUCTOR all the way through gimple
4155 compilation as a general initializer. */
4156 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4158 enum gimplify_status tret;
4159 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4160 fb_rvalue);
4161 if (tret == GS_ERROR)
4162 ret = GS_ERROR;
4164 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4165 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4167 break;
4169 default:
4170 /* So how did we get a CONSTRUCTOR for a scalar type? */
4171 gcc_unreachable ();
4174 if (ret == GS_ERROR)
4175 return GS_ERROR;
4176 else if (want_value)
4178 *expr_p = object;
4179 return GS_OK;
4181 else
4183 /* If we have gimplified both sides of the initializer but have
4184 not emitted an assignment, do so now. */
4185 if (*expr_p)
4187 tree lhs = TREE_OPERAND (*expr_p, 0);
4188 tree rhs = TREE_OPERAND (*expr_p, 1);
4189 gassign *init = gimple_build_assign (lhs, rhs);
4190 gimplify_seq_add_stmt (pre_p, init);
4191 *expr_p = NULL;
4194 return GS_ALL_DONE;
4198 /* Given a pointer value OP0, return a simplified version of an
4199 indirection through OP0, or NULL_TREE if no simplification is
4200 possible. This may only be applied to a rhs of an expression.
4201 Note that the resulting type may be different from the type pointed
4202 to in the sense that it is still compatible from the langhooks
4203 point of view. */
4205 static tree
4206 gimple_fold_indirect_ref_rhs (tree t)
4208 return gimple_fold_indirect_ref (t);
4211 /* Subroutine of gimplify_modify_expr to do simplifications of
4212 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4213 something changes. */
4215 static enum gimplify_status
4216 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4217 gimple_seq *pre_p, gimple_seq *post_p,
4218 bool want_value)
4220 enum gimplify_status ret = GS_UNHANDLED;
4221 bool changed;
4225 changed = false;
4226 switch (TREE_CODE (*from_p))
4228 case VAR_DECL:
4229 /* If we're assigning from a read-only variable initialized with
4230 a constructor, do the direct assignment from the constructor,
4231 but only if neither source nor target are volatile since this
4232 latter assignment might end up being done on a per-field basis. */
4233 if (DECL_INITIAL (*from_p)
4234 && TREE_READONLY (*from_p)
4235 && !TREE_THIS_VOLATILE (*from_p)
4236 && !TREE_THIS_VOLATILE (*to_p)
4237 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4239 tree old_from = *from_p;
4240 enum gimplify_status subret;
4242 /* Move the constructor into the RHS. */
4243 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4245 /* Let's see if gimplify_init_constructor will need to put
4246 it in memory. */
4247 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4248 false, true);
4249 if (subret == GS_ERROR)
4251 /* If so, revert the change. */
4252 *from_p = old_from;
4254 else
4256 ret = GS_OK;
4257 changed = true;
4260 break;
4261 case INDIRECT_REF:
4263 /* If we have code like
4265 *(const A*)(A*)&x
4267 where the type of "x" is a (possibly cv-qualified variant
4268 of "A"), treat the entire expression as identical to "x".
4269 This kind of code arises in C++ when an object is bound
4270 to a const reference, and if "x" is a TARGET_EXPR we want
4271 to take advantage of the optimization below. */
4272 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4273 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4274 if (t)
4276 if (TREE_THIS_VOLATILE (t) != volatile_p)
4278 if (DECL_P (t))
4279 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4280 build_fold_addr_expr (t));
4281 if (REFERENCE_CLASS_P (t))
4282 TREE_THIS_VOLATILE (t) = volatile_p;
4284 *from_p = t;
4285 ret = GS_OK;
4286 changed = true;
4288 break;
4291 case TARGET_EXPR:
4293 /* If we are initializing something from a TARGET_EXPR, strip the
4294 TARGET_EXPR and initialize it directly, if possible. This can't
4295 be done if the initializer is void, since that implies that the
4296 temporary is set in some non-trivial way.
4298 ??? What about code that pulls out the temp and uses it
4299 elsewhere? I think that such code never uses the TARGET_EXPR as
4300 an initializer. If I'm wrong, we'll die because the temp won't
4301 have any RTL. In that case, I guess we'll need to replace
4302 references somehow. */
4303 tree init = TARGET_EXPR_INITIAL (*from_p);
4305 if (init
4306 && !VOID_TYPE_P (TREE_TYPE (init)))
4308 *from_p = init;
4309 ret = GS_OK;
4310 changed = true;
4313 break;
4315 case COMPOUND_EXPR:
4316 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4317 caught. */
4318 gimplify_compound_expr (from_p, pre_p, true);
4319 ret = GS_OK;
4320 changed = true;
4321 break;
4323 case CONSTRUCTOR:
4324 /* If we already made some changes, let the front end have a
4325 crack at this before we break it down. */
4326 if (ret != GS_UNHANDLED)
4327 break;
4328 /* If we're initializing from a CONSTRUCTOR, break this into
4329 individual MODIFY_EXPRs. */
4330 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4331 false);
4333 case COND_EXPR:
4334 /* If we're assigning to a non-register type, push the assignment
4335 down into the branches. This is mandatory for ADDRESSABLE types,
4336 since we cannot generate temporaries for such, but it saves a
4337 copy in other cases as well. */
4338 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4340 /* This code should mirror the code in gimplify_cond_expr. */
4341 enum tree_code code = TREE_CODE (*expr_p);
4342 tree cond = *from_p;
4343 tree result = *to_p;
4345 ret = gimplify_expr (&result, pre_p, post_p,
4346 is_gimple_lvalue, fb_lvalue);
4347 if (ret != GS_ERROR)
4348 ret = GS_OK;
4350 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4351 TREE_OPERAND (cond, 1)
4352 = build2 (code, void_type_node, result,
4353 TREE_OPERAND (cond, 1));
4354 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4355 TREE_OPERAND (cond, 2)
4356 = build2 (code, void_type_node, unshare_expr (result),
4357 TREE_OPERAND (cond, 2));
4359 TREE_TYPE (cond) = void_type_node;
4360 recalculate_side_effects (cond);
4362 if (want_value)
4364 gimplify_and_add (cond, pre_p);
4365 *expr_p = unshare_expr (result);
4367 else
4368 *expr_p = cond;
4369 return ret;
4371 break;
4373 case CALL_EXPR:
4374 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4375 return slot so that we don't generate a temporary. */
4376 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4377 && aggregate_value_p (*from_p, *from_p))
4379 bool use_target;
4381 if (!(rhs_predicate_for (*to_p))(*from_p))
4382 /* If we need a temporary, *to_p isn't accurate. */
4383 use_target = false;
4384 /* It's OK to use the return slot directly unless it's an NRV. */
4385 else if (TREE_CODE (*to_p) == RESULT_DECL
4386 && DECL_NAME (*to_p) == NULL_TREE
4387 && needs_to_live_in_memory (*to_p))
4388 use_target = true;
4389 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4390 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4391 /* Don't force regs into memory. */
4392 use_target = false;
4393 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4394 /* It's OK to use the target directly if it's being
4395 initialized. */
4396 use_target = true;
4397 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4398 != INTEGER_CST)
4399 /* Always use the target and thus RSO for variable-sized types.
4400 GIMPLE cannot deal with a variable-sized assignment
4401 embedded in a call statement. */
4402 use_target = true;
4403 else if (TREE_CODE (*to_p) != SSA_NAME
4404 && (!is_gimple_variable (*to_p)
4405 || needs_to_live_in_memory (*to_p)))
4406 /* Don't use the original target if it's already addressable;
4407 if its address escapes, and the called function uses the
4408 NRV optimization, a conforming program could see *to_p
4409 change before the called function returns; see c++/19317.
4410 When optimizing, the return_slot pass marks more functions
4411 as safe after we have escape info. */
4412 use_target = false;
4413 else
4414 use_target = true;
4416 if (use_target)
4418 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4419 mark_addressable (*to_p);
4422 break;
4424 case WITH_SIZE_EXPR:
4425 /* Likewise for calls that return an aggregate of non-constant size,
4426 since we would not be able to generate a temporary at all. */
4427 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4429 *from_p = TREE_OPERAND (*from_p, 0);
4430 /* We don't change ret in this case because the
4431 WITH_SIZE_EXPR might have been added in
4432 gimplify_modify_expr, so returning GS_OK would lead to an
4433 infinite loop. */
4434 changed = true;
4436 break;
4438 /* If we're initializing from a container, push the initialization
4439 inside it. */
4440 case CLEANUP_POINT_EXPR:
4441 case BIND_EXPR:
4442 case STATEMENT_LIST:
4444 tree wrap = *from_p;
4445 tree t;
4447 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4448 fb_lvalue);
4449 if (ret != GS_ERROR)
4450 ret = GS_OK;
4452 t = voidify_wrapper_expr (wrap, *expr_p);
4453 gcc_assert (t == *expr_p);
4455 if (want_value)
4457 gimplify_and_add (wrap, pre_p);
4458 *expr_p = unshare_expr (*to_p);
4460 else
4461 *expr_p = wrap;
4462 return GS_OK;
4465 case COMPOUND_LITERAL_EXPR:
4467 tree complit = TREE_OPERAND (*expr_p, 1);
4468 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4469 tree decl = DECL_EXPR_DECL (decl_s);
4470 tree init = DECL_INITIAL (decl);
4472 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4473 into struct T x = { 0, 1, 2 } if the address of the
4474 compound literal has never been taken. */
4475 if (!TREE_ADDRESSABLE (complit)
4476 && !TREE_ADDRESSABLE (decl)
4477 && init)
4479 *expr_p = copy_node (*expr_p);
4480 TREE_OPERAND (*expr_p, 1) = init;
4481 return GS_OK;
4485 default:
4486 break;
4489 while (changed);
4491 return ret;
4495 /* Return true if T looks like a valid GIMPLE statement. */
4497 static bool
4498 is_gimple_stmt (tree t)
4500 const enum tree_code code = TREE_CODE (t);
4502 switch (code)
4504 case NOP_EXPR:
4505 /* The only valid NOP_EXPR is the empty statement. */
4506 return IS_EMPTY_STMT (t);
4508 case BIND_EXPR:
4509 case COND_EXPR:
4510 /* These are only valid if they're void. */
4511 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4513 case SWITCH_EXPR:
4514 case GOTO_EXPR:
4515 case RETURN_EXPR:
4516 case LABEL_EXPR:
4517 case CASE_LABEL_EXPR:
4518 case TRY_CATCH_EXPR:
4519 case TRY_FINALLY_EXPR:
4520 case EH_FILTER_EXPR:
4521 case CATCH_EXPR:
4522 case ASM_EXPR:
4523 case STATEMENT_LIST:
4524 case OACC_PARALLEL:
4525 case OACC_KERNELS:
4526 case OACC_DATA:
4527 case OACC_HOST_DATA:
4528 case OACC_DECLARE:
4529 case OACC_UPDATE:
4530 case OACC_ENTER_DATA:
4531 case OACC_EXIT_DATA:
4532 case OACC_CACHE:
4533 case OMP_PARALLEL:
4534 case OMP_FOR:
4535 case OMP_SIMD:
4536 case CILK_SIMD:
4537 case OMP_DISTRIBUTE:
4538 case OACC_LOOP:
4539 case OMP_SECTIONS:
4540 case OMP_SECTION:
4541 case OMP_SINGLE:
4542 case OMP_MASTER:
4543 case OMP_TASKGROUP:
4544 case OMP_ORDERED:
4545 case OMP_CRITICAL:
4546 case OMP_TASK:
4547 case OMP_TARGET:
4548 case OMP_TARGET_DATA:
4549 case OMP_TARGET_UPDATE:
4550 case OMP_TARGET_ENTER_DATA:
4551 case OMP_TARGET_EXIT_DATA:
4552 case OMP_TASKLOOP:
4553 case OMP_TEAMS:
4554 /* These are always void. */
4555 return true;
4557 case CALL_EXPR:
4558 case MODIFY_EXPR:
4559 case PREDICT_EXPR:
4560 /* These are valid regardless of their type. */
4561 return true;
4563 default:
4564 return false;
4569 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4570 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4571 DECL_GIMPLE_REG_P set.
4573 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4574 other, unmodified part of the complex object just before the total store.
4575 As a consequence, if the object is still uninitialized, an undefined value
4576 will be loaded into a register, which may result in a spurious exception
4577 if the register is floating-point and the value happens to be a signaling
4578 NaN for example. Then the fully-fledged complex operations lowering pass
4579 followed by a DCE pass are necessary in order to fix things up. */
4581 static enum gimplify_status
4582 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4583 bool want_value)
4585 enum tree_code code, ocode;
4586 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4588 lhs = TREE_OPERAND (*expr_p, 0);
4589 rhs = TREE_OPERAND (*expr_p, 1);
4590 code = TREE_CODE (lhs);
4591 lhs = TREE_OPERAND (lhs, 0);
4593 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4594 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4595 TREE_NO_WARNING (other) = 1;
4596 other = get_formal_tmp_var (other, pre_p);
4598 realpart = code == REALPART_EXPR ? rhs : other;
4599 imagpart = code == REALPART_EXPR ? other : rhs;
4601 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4602 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4603 else
4604 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4606 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4607 *expr_p = (want_value) ? rhs : NULL_TREE;
4609 return GS_ALL_DONE;
4612 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4614 modify_expr
4615 : varname '=' rhs
4616 | '*' ID '=' rhs
4618 PRE_P points to the list where side effects that must happen before
4619 *EXPR_P should be stored.
4621 POST_P points to the list where side effects that must happen after
4622 *EXPR_P should be stored.
4624 WANT_VALUE is nonzero iff we want to use the value of this expression
4625 in another expression. */
4627 static enum gimplify_status
4628 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4629 bool want_value)
4631 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4632 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4633 enum gimplify_status ret = GS_UNHANDLED;
4634 gimple *assign;
4635 location_t loc = EXPR_LOCATION (*expr_p);
4636 gimple_stmt_iterator gsi;
4638 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4639 || TREE_CODE (*expr_p) == INIT_EXPR);
4641 /* Trying to simplify a clobber using normal logic doesn't work,
4642 so handle it here. */
4643 if (TREE_CLOBBER_P (*from_p))
4645 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4646 if (ret == GS_ERROR)
4647 return ret;
4648 gcc_assert (!want_value
4649 && (TREE_CODE (*to_p) == VAR_DECL
4650 || TREE_CODE (*to_p) == MEM_REF));
4651 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4652 *expr_p = NULL;
4653 return GS_ALL_DONE;
4656 /* Insert pointer conversions required by the middle-end that are not
4657 required by the frontend. This fixes middle-end type checking for
4658 for example gcc.dg/redecl-6.c. */
4659 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4661 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4662 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4663 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4666 /* See if any simplifications can be done based on what the RHS is. */
4667 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4668 want_value);
4669 if (ret != GS_UNHANDLED)
4670 return ret;
4672 /* For zero sized types only gimplify the left hand side and right hand
4673 side as statements and throw away the assignment. Do this after
4674 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4675 types properly. */
4676 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4678 gimplify_stmt (from_p, pre_p);
4679 gimplify_stmt (to_p, pre_p);
4680 *expr_p = NULL_TREE;
4681 return GS_ALL_DONE;
4684 /* If the value being copied is of variable width, compute the length
4685 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4686 before gimplifying any of the operands so that we can resolve any
4687 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4688 the size of the expression to be copied, not of the destination, so
4689 that is what we must do here. */
4690 maybe_with_size_expr (from_p);
4692 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4693 if (ret == GS_ERROR)
4694 return ret;
4696 /* As a special case, we have to temporarily allow for assignments
4697 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4698 a toplevel statement, when gimplifying the GENERIC expression
4699 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4700 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4702 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4703 prevent gimplify_expr from trying to create a new temporary for
4704 foo's LHS, we tell it that it should only gimplify until it
4705 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4706 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4707 and all we need to do here is set 'a' to be its LHS. */
4708 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4709 fb_rvalue);
4710 if (ret == GS_ERROR)
4711 return ret;
4713 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4714 size as argument to the call. */
4715 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4717 tree call = TREE_OPERAND (*from_p, 0);
4718 tree vlasize = TREE_OPERAND (*from_p, 1);
4720 if (TREE_CODE (call) == CALL_EXPR
4721 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
4723 int nargs = call_expr_nargs (call);
4724 tree type = TREE_TYPE (call);
4725 tree ap = CALL_EXPR_ARG (call, 0);
4726 tree tag = CALL_EXPR_ARG (call, 1);
4727 tree aptag = CALL_EXPR_ARG (call, 2);
4728 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
4729 IFN_VA_ARG, type,
4730 nargs + 1, ap, tag,
4731 aptag, vlasize);
4732 TREE_OPERAND (*from_p, 0) = newcall;
4736 /* Now see if the above changed *from_p to something we handle specially. */
4737 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4738 want_value);
4739 if (ret != GS_UNHANDLED)
4740 return ret;
4742 /* If we've got a variable sized assignment between two lvalues (i.e. does
4743 not involve a call), then we can make things a bit more straightforward
4744 by converting the assignment to memcpy or memset. */
4745 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4747 tree from = TREE_OPERAND (*from_p, 0);
4748 tree size = TREE_OPERAND (*from_p, 1);
4750 if (TREE_CODE (from) == CONSTRUCTOR)
4751 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4753 if (is_gimple_addressable (from))
4755 *from_p = from;
4756 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4757 pre_p);
4761 /* Transform partial stores to non-addressable complex variables into
4762 total stores. This allows us to use real instead of virtual operands
4763 for these variables, which improves optimization. */
4764 if ((TREE_CODE (*to_p) == REALPART_EXPR
4765 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4766 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4767 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4769 /* Try to alleviate the effects of the gimplification creating artificial
4770 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4771 make sure not to create DECL_DEBUG_EXPR links across functions. */
4772 if (!gimplify_ctxp->into_ssa
4773 && TREE_CODE (*from_p) == VAR_DECL
4774 && DECL_IGNORED_P (*from_p)
4775 && DECL_P (*to_p)
4776 && !DECL_IGNORED_P (*to_p)
4777 && decl_function_context (*to_p) == current_function_decl)
4779 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4780 DECL_NAME (*from_p)
4781 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4782 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4783 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4786 if (want_value && TREE_THIS_VOLATILE (*to_p))
4787 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4789 if (TREE_CODE (*from_p) == CALL_EXPR)
4791 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4792 instead of a GIMPLE_ASSIGN. */
4793 gcall *call_stmt;
4794 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4796 /* Gimplify internal functions created in the FEs. */
4797 int nargs = call_expr_nargs (*from_p), i;
4798 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4799 auto_vec<tree> vargs (nargs);
4801 for (i = 0; i < nargs; i++)
4803 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4804 EXPR_LOCATION (*from_p));
4805 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4807 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4808 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4810 else
4812 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4813 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4814 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4815 tree fndecl = get_callee_fndecl (*from_p);
4816 if (fndecl
4817 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4818 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4819 && call_expr_nargs (*from_p) == 3)
4820 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4821 CALL_EXPR_ARG (*from_p, 0),
4822 CALL_EXPR_ARG (*from_p, 1),
4823 CALL_EXPR_ARG (*from_p, 2));
4824 else
4826 call_stmt = gimple_build_call_from_tree (*from_p);
4827 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4830 notice_special_calls (call_stmt);
4831 if (!gimple_call_noreturn_p (call_stmt))
4832 gimple_call_set_lhs (call_stmt, *to_p);
4833 assign = call_stmt;
4835 else
4837 assign = gimple_build_assign (*to_p, *from_p);
4838 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4841 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4843 /* We should have got an SSA name from the start. */
4844 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4847 gimplify_seq_add_stmt (pre_p, assign);
4848 gsi = gsi_last (*pre_p);
4849 maybe_fold_stmt (&gsi);
4851 if (want_value)
4853 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4854 return GS_OK;
4856 else
4857 *expr_p = NULL;
4859 return GS_ALL_DONE;
4862 /* Gimplify a comparison between two variable-sized objects. Do this
4863 with a call to BUILT_IN_MEMCMP. */
4865 static enum gimplify_status
4866 gimplify_variable_sized_compare (tree *expr_p)
4868 location_t loc = EXPR_LOCATION (*expr_p);
4869 tree op0 = TREE_OPERAND (*expr_p, 0);
4870 tree op1 = TREE_OPERAND (*expr_p, 1);
4871 tree t, arg, dest, src, expr;
4873 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4874 arg = unshare_expr (arg);
4875 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4876 src = build_fold_addr_expr_loc (loc, op1);
4877 dest = build_fold_addr_expr_loc (loc, op0);
4878 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4879 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4881 expr
4882 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4883 SET_EXPR_LOCATION (expr, loc);
4884 *expr_p = expr;
4886 return GS_OK;
4889 /* Gimplify a comparison between two aggregate objects of integral scalar
4890 mode as a comparison between the bitwise equivalent scalar values. */
4892 static enum gimplify_status
4893 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4895 location_t loc = EXPR_LOCATION (*expr_p);
4896 tree op0 = TREE_OPERAND (*expr_p, 0);
4897 tree op1 = TREE_OPERAND (*expr_p, 1);
4899 tree type = TREE_TYPE (op0);
4900 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4902 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4903 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4905 *expr_p
4906 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4908 return GS_OK;
4911 /* Gimplify an expression sequence. This function gimplifies each
4912 expression and rewrites the original expression with the last
4913 expression of the sequence in GIMPLE form.
4915 PRE_P points to the list where the side effects for all the
4916 expressions in the sequence will be emitted.
4918 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4920 static enum gimplify_status
4921 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4923 tree t = *expr_p;
4927 tree *sub_p = &TREE_OPERAND (t, 0);
4929 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4930 gimplify_compound_expr (sub_p, pre_p, false);
4931 else
4932 gimplify_stmt (sub_p, pre_p);
4934 t = TREE_OPERAND (t, 1);
4936 while (TREE_CODE (t) == COMPOUND_EXPR);
4938 *expr_p = t;
4939 if (want_value)
4940 return GS_OK;
4941 else
4943 gimplify_stmt (expr_p, pre_p);
4944 return GS_ALL_DONE;
4948 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4949 gimplify. After gimplification, EXPR_P will point to a new temporary
4950 that holds the original value of the SAVE_EXPR node.
4952 PRE_P points to the list where side effects that must happen before
4953 *EXPR_P should be stored. */
4955 static enum gimplify_status
4956 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4958 enum gimplify_status ret = GS_ALL_DONE;
4959 tree val;
4961 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4962 val = TREE_OPERAND (*expr_p, 0);
4964 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4965 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4967 /* The operand may be a void-valued expression such as SAVE_EXPRs
4968 generated by the Java frontend for class initialization. It is
4969 being executed only for its side-effects. */
4970 if (TREE_TYPE (val) == void_type_node)
4972 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4973 is_gimple_stmt, fb_none);
4974 val = NULL;
4976 else
4977 val = get_initialized_tmp_var (val, pre_p, post_p);
4979 TREE_OPERAND (*expr_p, 0) = val;
4980 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4983 *expr_p = val;
4985 return ret;
4988 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4990 unary_expr
4991 : ...
4992 | '&' varname
4995 PRE_P points to the list where side effects that must happen before
4996 *EXPR_P should be stored.
4998 POST_P points to the list where side effects that must happen after
4999 *EXPR_P should be stored. */
5001 static enum gimplify_status
5002 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5004 tree expr = *expr_p;
5005 tree op0 = TREE_OPERAND (expr, 0);
5006 enum gimplify_status ret;
5007 location_t loc = EXPR_LOCATION (*expr_p);
5009 switch (TREE_CODE (op0))
5011 case INDIRECT_REF:
5012 do_indirect_ref:
5013 /* Check if we are dealing with an expression of the form '&*ptr'.
5014 While the front end folds away '&*ptr' into 'ptr', these
5015 expressions may be generated internally by the compiler (e.g.,
5016 builtins like __builtin_va_end). */
5017 /* Caution: the silent array decomposition semantics we allow for
5018 ADDR_EXPR means we can't always discard the pair. */
5019 /* Gimplification of the ADDR_EXPR operand may drop
5020 cv-qualification conversions, so make sure we add them if
5021 needed. */
5023 tree op00 = TREE_OPERAND (op0, 0);
5024 tree t_expr = TREE_TYPE (expr);
5025 tree t_op00 = TREE_TYPE (op00);
5027 if (!useless_type_conversion_p (t_expr, t_op00))
5028 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5029 *expr_p = op00;
5030 ret = GS_OK;
5032 break;
5034 case VIEW_CONVERT_EXPR:
5035 /* Take the address of our operand and then convert it to the type of
5036 this ADDR_EXPR.
5038 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5039 all clear. The impact of this transformation is even less clear. */
5041 /* If the operand is a useless conversion, look through it. Doing so
5042 guarantees that the ADDR_EXPR and its operand will remain of the
5043 same type. */
5044 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5045 op0 = TREE_OPERAND (op0, 0);
5047 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5048 build_fold_addr_expr_loc (loc,
5049 TREE_OPERAND (op0, 0)));
5050 ret = GS_OK;
5051 break;
5053 case MEM_REF:
5054 if (integer_zerop (TREE_OPERAND (op0, 1)))
5055 goto do_indirect_ref;
5057 /* ... fall through ... */
5059 default:
5060 /* If we see a call to a declared builtin or see its address
5061 being taken (we can unify those cases here) then we can mark
5062 the builtin for implicit generation by GCC. */
5063 if (TREE_CODE (op0) == FUNCTION_DECL
5064 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5065 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5066 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5068 /* We use fb_either here because the C frontend sometimes takes
5069 the address of a call that returns a struct; see
5070 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5071 the implied temporary explicit. */
5073 /* Make the operand addressable. */
5074 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5075 is_gimple_addressable, fb_either);
5076 if (ret == GS_ERROR)
5077 break;
5079 /* Then mark it. Beware that it may not be possible to do so directly
5080 if a temporary has been created by the gimplification. */
5081 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5083 op0 = TREE_OPERAND (expr, 0);
5085 /* For various reasons, the gimplification of the expression
5086 may have made a new INDIRECT_REF. */
5087 if (TREE_CODE (op0) == INDIRECT_REF)
5088 goto do_indirect_ref;
5090 mark_addressable (TREE_OPERAND (expr, 0));
5092 /* The FEs may end up building ADDR_EXPRs early on a decl with
5093 an incomplete type. Re-build ADDR_EXPRs in canonical form
5094 here. */
5095 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5096 *expr_p = build_fold_addr_expr (op0);
5098 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5099 recompute_tree_invariant_for_addr_expr (*expr_p);
5101 /* If we re-built the ADDR_EXPR add a conversion to the original type
5102 if required. */
5103 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5104 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5106 break;
5109 return ret;
5112 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5113 value; output operands should be a gimple lvalue. */
5115 static enum gimplify_status
5116 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5118 tree expr;
5119 int noutputs;
5120 const char **oconstraints;
5121 int i;
5122 tree link;
5123 const char *constraint;
5124 bool allows_mem, allows_reg, is_inout;
5125 enum gimplify_status ret, tret;
5126 gasm *stmt;
5127 vec<tree, va_gc> *inputs;
5128 vec<tree, va_gc> *outputs;
5129 vec<tree, va_gc> *clobbers;
5130 vec<tree, va_gc> *labels;
5131 tree link_next;
5133 expr = *expr_p;
5134 noutputs = list_length (ASM_OUTPUTS (expr));
5135 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5137 inputs = NULL;
5138 outputs = NULL;
5139 clobbers = NULL;
5140 labels = NULL;
5142 ret = GS_ALL_DONE;
5143 link_next = NULL_TREE;
5144 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5146 bool ok;
5147 size_t constraint_len;
5149 link_next = TREE_CHAIN (link);
5151 oconstraints[i]
5152 = constraint
5153 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5154 constraint_len = strlen (constraint);
5155 if (constraint_len == 0)
5156 continue;
5158 ok = parse_output_constraint (&constraint, i, 0, 0,
5159 &allows_mem, &allows_reg, &is_inout);
5160 if (!ok)
5162 ret = GS_ERROR;
5163 is_inout = false;
5166 if (!allows_reg && allows_mem)
5167 mark_addressable (TREE_VALUE (link));
5169 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5170 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5171 fb_lvalue | fb_mayfail);
5172 if (tret == GS_ERROR)
5174 error ("invalid lvalue in asm output %d", i);
5175 ret = tret;
5178 vec_safe_push (outputs, link);
5179 TREE_CHAIN (link) = NULL_TREE;
5181 if (is_inout)
5183 /* An input/output operand. To give the optimizers more
5184 flexibility, split it into separate input and output
5185 operands. */
5186 tree input;
5187 char buf[10];
5189 /* Turn the in/out constraint into an output constraint. */
5190 char *p = xstrdup (constraint);
5191 p[0] = '=';
5192 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5194 /* And add a matching input constraint. */
5195 if (allows_reg)
5197 sprintf (buf, "%d", i);
5199 /* If there are multiple alternatives in the constraint,
5200 handle each of them individually. Those that allow register
5201 will be replaced with operand number, the others will stay
5202 unchanged. */
5203 if (strchr (p, ',') != NULL)
5205 size_t len = 0, buflen = strlen (buf);
5206 char *beg, *end, *str, *dst;
5208 for (beg = p + 1;;)
5210 end = strchr (beg, ',');
5211 if (end == NULL)
5212 end = strchr (beg, '\0');
5213 if ((size_t) (end - beg) < buflen)
5214 len += buflen + 1;
5215 else
5216 len += end - beg + 1;
5217 if (*end)
5218 beg = end + 1;
5219 else
5220 break;
5223 str = (char *) alloca (len);
5224 for (beg = p + 1, dst = str;;)
5226 const char *tem;
5227 bool mem_p, reg_p, inout_p;
5229 end = strchr (beg, ',');
5230 if (end)
5231 *end = '\0';
5232 beg[-1] = '=';
5233 tem = beg - 1;
5234 parse_output_constraint (&tem, i, 0, 0,
5235 &mem_p, &reg_p, &inout_p);
5236 if (dst != str)
5237 *dst++ = ',';
5238 if (reg_p)
5240 memcpy (dst, buf, buflen);
5241 dst += buflen;
5243 else
5245 if (end)
5246 len = end - beg;
5247 else
5248 len = strlen (beg);
5249 memcpy (dst, beg, len);
5250 dst += len;
5252 if (end)
5253 beg = end + 1;
5254 else
5255 break;
5257 *dst = '\0';
5258 input = build_string (dst - str, str);
5260 else
5261 input = build_string (strlen (buf), buf);
5263 else
5264 input = build_string (constraint_len - 1, constraint + 1);
5266 free (p);
5268 input = build_tree_list (build_tree_list (NULL_TREE, input),
5269 unshare_expr (TREE_VALUE (link)));
5270 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5274 link_next = NULL_TREE;
5275 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5277 link_next = TREE_CHAIN (link);
5278 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5279 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5280 oconstraints, &allows_mem, &allows_reg);
5282 /* If we can't make copies, we can only accept memory. */
5283 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5285 if (allows_mem)
5286 allows_reg = 0;
5287 else
5289 error ("impossible constraint in %<asm%>");
5290 error ("non-memory input %d must stay in memory", i);
5291 return GS_ERROR;
5295 /* If the operand is a memory input, it should be an lvalue. */
5296 if (!allows_reg && allows_mem)
5298 tree inputv = TREE_VALUE (link);
5299 STRIP_NOPS (inputv);
5300 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5301 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5302 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5303 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
5304 || TREE_CODE (inputv) == MODIFY_EXPR)
5305 TREE_VALUE (link) = error_mark_node;
5306 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5307 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5308 if (tret != GS_ERROR)
5310 /* Unlike output operands, memory inputs are not guaranteed
5311 to be lvalues by the FE, and while the expressions are
5312 marked addressable there, if it is e.g. a statement
5313 expression, temporaries in it might not end up being
5314 addressable. They might be already used in the IL and thus
5315 it is too late to make them addressable now though. */
5316 tree x = TREE_VALUE (link);
5317 while (handled_component_p (x))
5318 x = TREE_OPERAND (x, 0);
5319 if (TREE_CODE (x) == MEM_REF
5320 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
5321 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
5322 if ((TREE_CODE (x) == VAR_DECL
5323 || TREE_CODE (x) == PARM_DECL
5324 || TREE_CODE (x) == RESULT_DECL)
5325 && !TREE_ADDRESSABLE (x)
5326 && is_gimple_reg (x))
5328 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link),
5329 input_location), 0,
5330 "memory input %d is not directly addressable",
5332 prepare_gimple_addressable (&TREE_VALUE (link), pre_p);
5335 mark_addressable (TREE_VALUE (link));
5336 if (tret == GS_ERROR)
5338 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link), input_location),
5339 "memory input %d is not directly addressable", i);
5340 ret = tret;
5343 else
5345 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5346 is_gimple_asm_val, fb_rvalue);
5347 if (tret == GS_ERROR)
5348 ret = tret;
5351 TREE_CHAIN (link) = NULL_TREE;
5352 vec_safe_push (inputs, link);
5355 link_next = NULL_TREE;
5356 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5358 link_next = TREE_CHAIN (link);
5359 TREE_CHAIN (link) = NULL_TREE;
5360 vec_safe_push (clobbers, link);
5363 link_next = NULL_TREE;
5364 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5366 link_next = TREE_CHAIN (link);
5367 TREE_CHAIN (link) = NULL_TREE;
5368 vec_safe_push (labels, link);
5371 /* Do not add ASMs with errors to the gimple IL stream. */
5372 if (ret != GS_ERROR)
5374 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5375 inputs, outputs, clobbers, labels);
5377 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5378 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5380 gimplify_seq_add_stmt (pre_p, stmt);
5383 return ret;
5386 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5387 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5388 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5389 return to this function.
5391 FIXME should we complexify the prequeue handling instead? Or use flags
5392 for all the cleanups and let the optimizer tighten them up? The current
5393 code seems pretty fragile; it will break on a cleanup within any
5394 non-conditional nesting. But any such nesting would be broken, anyway;
5395 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5396 and continues out of it. We can do that at the RTL level, though, so
5397 having an optimizer to tighten up try/finally regions would be a Good
5398 Thing. */
5400 static enum gimplify_status
5401 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5403 gimple_stmt_iterator iter;
5404 gimple_seq body_sequence = NULL;
5406 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5408 /* We only care about the number of conditions between the innermost
5409 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5410 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5411 int old_conds = gimplify_ctxp->conditions;
5412 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5413 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5414 gimplify_ctxp->conditions = 0;
5415 gimplify_ctxp->conditional_cleanups = NULL;
5416 gimplify_ctxp->in_cleanup_point_expr = true;
5418 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5420 gimplify_ctxp->conditions = old_conds;
5421 gimplify_ctxp->conditional_cleanups = old_cleanups;
5422 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5424 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5426 gimple *wce = gsi_stmt (iter);
5428 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5430 if (gsi_one_before_end_p (iter))
5432 /* Note that gsi_insert_seq_before and gsi_remove do not
5433 scan operands, unlike some other sequence mutators. */
5434 if (!gimple_wce_cleanup_eh_only (wce))
5435 gsi_insert_seq_before_without_update (&iter,
5436 gimple_wce_cleanup (wce),
5437 GSI_SAME_STMT);
5438 gsi_remove (&iter, true);
5439 break;
5441 else
5443 gtry *gtry;
5444 gimple_seq seq;
5445 enum gimple_try_flags kind;
5447 if (gimple_wce_cleanup_eh_only (wce))
5448 kind = GIMPLE_TRY_CATCH;
5449 else
5450 kind = GIMPLE_TRY_FINALLY;
5451 seq = gsi_split_seq_after (iter);
5453 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5454 /* Do not use gsi_replace here, as it may scan operands.
5455 We want to do a simple structural modification only. */
5456 gsi_set_stmt (&iter, gtry);
5457 iter = gsi_start (gtry->eval);
5460 else
5461 gsi_next (&iter);
5464 gimplify_seq_add_seq (pre_p, body_sequence);
5465 if (temp)
5467 *expr_p = temp;
5468 return GS_OK;
5470 else
5472 *expr_p = NULL;
5473 return GS_ALL_DONE;
5477 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5478 is the cleanup action required. EH_ONLY is true if the cleanup should
5479 only be executed if an exception is thrown, not on normal exit. */
5481 static void
5482 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5484 gimple *wce;
5485 gimple_seq cleanup_stmts = NULL;
5487 /* Errors can result in improperly nested cleanups. Which results in
5488 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5489 if (seen_error ())
5490 return;
5492 if (gimple_conditional_context ())
5494 /* If we're in a conditional context, this is more complex. We only
5495 want to run the cleanup if we actually ran the initialization that
5496 necessitates it, but we want to run it after the end of the
5497 conditional context. So we wrap the try/finally around the
5498 condition and use a flag to determine whether or not to actually
5499 run the destructor. Thus
5501 test ? f(A()) : 0
5503 becomes (approximately)
5505 flag = 0;
5506 try {
5507 if (test) { A::A(temp); flag = 1; val = f(temp); }
5508 else { val = 0; }
5509 } finally {
5510 if (flag) A::~A(temp);
5514 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5515 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5516 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5518 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5519 gimplify_stmt (&cleanup, &cleanup_stmts);
5520 wce = gimple_build_wce (cleanup_stmts);
5522 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5523 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5524 gimplify_seq_add_stmt (pre_p, ftrue);
5526 /* Because of this manipulation, and the EH edges that jump
5527 threading cannot redirect, the temporary (VAR) will appear
5528 to be used uninitialized. Don't warn. */
5529 TREE_NO_WARNING (var) = 1;
5531 else
5533 gimplify_stmt (&cleanup, &cleanup_stmts);
5534 wce = gimple_build_wce (cleanup_stmts);
5535 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5536 gimplify_seq_add_stmt (pre_p, wce);
5540 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5542 static enum gimplify_status
5543 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5545 tree targ = *expr_p;
5546 tree temp = TARGET_EXPR_SLOT (targ);
5547 tree init = TARGET_EXPR_INITIAL (targ);
5548 enum gimplify_status ret;
5550 if (init)
5552 tree cleanup = NULL_TREE;
5554 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5555 to the temps list. Handle also variable length TARGET_EXPRs. */
5556 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5558 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5559 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5560 gimplify_vla_decl (temp, pre_p);
5562 else
5563 gimple_add_tmp_var (temp);
5565 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5566 expression is supposed to initialize the slot. */
5567 if (VOID_TYPE_P (TREE_TYPE (init)))
5568 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5569 else
5571 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5572 init = init_expr;
5573 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5574 init = NULL;
5575 ggc_free (init_expr);
5577 if (ret == GS_ERROR)
5579 /* PR c++/28266 Make sure this is expanded only once. */
5580 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5581 return GS_ERROR;
5583 if (init)
5584 gimplify_and_add (init, pre_p);
5586 /* If needed, push the cleanup for the temp. */
5587 if (TARGET_EXPR_CLEANUP (targ))
5589 if (CLEANUP_EH_ONLY (targ))
5590 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5591 CLEANUP_EH_ONLY (targ), pre_p);
5592 else
5593 cleanup = TARGET_EXPR_CLEANUP (targ);
5596 /* Add a clobber for the temporary going out of scope, like
5597 gimplify_bind_expr. */
5598 if (gimplify_ctxp->in_cleanup_point_expr
5599 && needs_to_live_in_memory (temp)
5600 && flag_stack_reuse == SR_ALL)
5602 tree clobber = build_constructor (TREE_TYPE (temp),
5603 NULL);
5604 TREE_THIS_VOLATILE (clobber) = true;
5605 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5606 if (cleanup)
5607 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5608 clobber);
5609 else
5610 cleanup = clobber;
5613 if (cleanup)
5614 gimple_push_cleanup (temp, cleanup, false, pre_p);
5616 /* Only expand this once. */
5617 TREE_OPERAND (targ, 3) = init;
5618 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5620 else
5621 /* We should have expanded this before. */
5622 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5624 *expr_p = temp;
5625 return GS_OK;
5628 /* Gimplification of expression trees. */
5630 /* Gimplify an expression which appears at statement context. The
5631 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5632 NULL, a new sequence is allocated.
5634 Return true if we actually added a statement to the queue. */
5636 bool
5637 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5639 gimple_seq_node last;
5641 last = gimple_seq_last (*seq_p);
5642 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5643 return last != gimple_seq_last (*seq_p);
5646 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5647 to CTX. If entries already exist, force them to be some flavor of private.
5648 If there is no enclosing parallel, do nothing. */
5650 void
5651 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5653 splay_tree_node n;
5655 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
5656 return;
5660 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5661 if (n != NULL)
5663 if (n->value & GOVD_SHARED)
5664 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5665 else if (n->value & GOVD_MAP)
5666 n->value |= GOVD_MAP_TO_ONLY;
5667 else
5668 return;
5670 else if ((ctx->region_type & ORT_TARGET) != 0)
5672 if (ctx->target_map_scalars_firstprivate)
5673 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5674 else
5675 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5677 else if (ctx->region_type != ORT_WORKSHARE
5678 && ctx->region_type != ORT_SIMD
5679 && ctx->region_type != ORT_ACC
5680 && !(ctx->region_type & ORT_TARGET_DATA))
5681 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5683 ctx = ctx->outer_context;
5685 while (ctx);
5688 /* Similarly for each of the type sizes of TYPE. */
5690 static void
5691 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5693 if (type == NULL || type == error_mark_node)
5694 return;
5695 type = TYPE_MAIN_VARIANT (type);
5697 if (ctx->privatized_types->add (type))
5698 return;
5700 switch (TREE_CODE (type))
5702 case INTEGER_TYPE:
5703 case ENUMERAL_TYPE:
5704 case BOOLEAN_TYPE:
5705 case REAL_TYPE:
5706 case FIXED_POINT_TYPE:
5707 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5708 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5709 break;
5711 case ARRAY_TYPE:
5712 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5713 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5714 break;
5716 case RECORD_TYPE:
5717 case UNION_TYPE:
5718 case QUAL_UNION_TYPE:
5720 tree field;
5721 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5722 if (TREE_CODE (field) == FIELD_DECL)
5724 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5725 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5728 break;
5730 case POINTER_TYPE:
5731 case REFERENCE_TYPE:
5732 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5733 break;
5735 default:
5736 break;
5739 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5740 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5741 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5744 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5746 static void
5747 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5749 splay_tree_node n;
5750 unsigned int nflags;
5751 tree t;
5753 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
5754 return;
5756 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5757 there are constructors involved somewhere. */
5758 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5759 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5760 flags |= GOVD_SEEN;
5762 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5763 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5765 /* We shouldn't be re-adding the decl with the same data
5766 sharing class. */
5767 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5768 nflags = n->value | flags;
5769 /* The only combination of data sharing classes we should see is
5770 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
5771 reduction variables to be used in data sharing clauses. */
5772 gcc_assert ((ctx->region_type & ORT_ACC) != 0
5773 || ((nflags & GOVD_DATA_SHARE_CLASS)
5774 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE))
5775 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5776 n->value = nflags;
5777 return;
5780 /* When adding a variable-sized variable, we have to handle all sorts
5781 of additional bits of data: the pointer replacement variable, and
5782 the parameters of the type. */
5783 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5785 /* Add the pointer replacement variable as PRIVATE if the variable
5786 replacement is private, else FIRSTPRIVATE since we'll need the
5787 address of the original variable either for SHARED, or for the
5788 copy into or out of the context. */
5789 if (!(flags & GOVD_LOCAL))
5791 if (flags & GOVD_MAP)
5792 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5793 else if (flags & GOVD_PRIVATE)
5794 nflags = GOVD_PRIVATE;
5795 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
5796 && (flags & GOVD_FIRSTPRIVATE))
5797 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
5798 else
5799 nflags = GOVD_FIRSTPRIVATE;
5800 nflags |= flags & GOVD_SEEN;
5801 t = DECL_VALUE_EXPR (decl);
5802 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5803 t = TREE_OPERAND (t, 0);
5804 gcc_assert (DECL_P (t));
5805 omp_add_variable (ctx, t, nflags);
5808 /* Add all of the variable and type parameters (which should have
5809 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5810 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5811 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5812 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5814 /* The variable-sized variable itself is never SHARED, only some form
5815 of PRIVATE. The sharing would take place via the pointer variable
5816 which we remapped above. */
5817 if (flags & GOVD_SHARED)
5818 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5819 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5821 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5822 alloca statement we generate for the variable, so make sure it
5823 is available. This isn't automatically needed for the SHARED
5824 case, since we won't be allocating local storage then.
5825 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5826 in this case omp_notice_variable will be called later
5827 on when it is gimplified. */
5828 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5829 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5830 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5832 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5833 && lang_hooks.decls.omp_privatize_by_reference (decl))
5835 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5837 /* Similar to the direct variable sized case above, we'll need the
5838 size of references being privatized. */
5839 if ((flags & GOVD_SHARED) == 0)
5841 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5842 if (DECL_P (t))
5843 omp_notice_variable (ctx, t, true);
5847 if (n != NULL)
5848 n->value |= flags;
5849 else
5850 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5853 /* Notice a threadprivate variable DECL used in OMP context CTX.
5854 This just prints out diagnostics about threadprivate variable uses
5855 in untied tasks. If DECL2 is non-NULL, prevent this warning
5856 on that variable. */
5858 static bool
5859 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5860 tree decl2)
5862 splay_tree_node n;
5863 struct gimplify_omp_ctx *octx;
5865 for (octx = ctx; octx; octx = octx->outer_context)
5866 if ((octx->region_type & ORT_TARGET) != 0)
5868 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5869 if (n == NULL)
5871 error ("threadprivate variable %qE used in target region",
5872 DECL_NAME (decl));
5873 error_at (octx->location, "enclosing target region");
5874 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5876 if (decl2)
5877 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5880 if (ctx->region_type != ORT_UNTIED_TASK)
5881 return false;
5882 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5883 if (n == NULL)
5885 error ("threadprivate variable %qE used in untied task",
5886 DECL_NAME (decl));
5887 error_at (ctx->location, "enclosing task");
5888 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5890 if (decl2)
5891 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5892 return false;
5895 /* Return true if global var DECL is device resident. */
5897 static bool
5898 device_resident_p (tree decl)
5900 tree attr = lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl));
5902 if (!attr)
5903 return false;
5905 for (tree t = TREE_VALUE (attr); t; t = TREE_PURPOSE (t))
5907 tree c = TREE_VALUE (t);
5908 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DEVICE_RESIDENT)
5909 return true;
5912 return false;
5915 /* Determine outer default flags for DECL mentioned in an OMP region
5916 but not declared in an enclosing clause.
5918 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5919 remapped firstprivate instead of shared. To some extent this is
5920 addressed in omp_firstprivatize_type_sizes, but not
5921 effectively. */
5923 static unsigned
5924 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
5925 bool in_code, unsigned flags)
5927 enum omp_clause_default_kind default_kind = ctx->default_kind;
5928 enum omp_clause_default_kind kind;
5930 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5931 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5932 default_kind = kind;
5934 switch (default_kind)
5936 case OMP_CLAUSE_DEFAULT_NONE:
5938 const char *rtype;
5940 if (ctx->region_type & ORT_PARALLEL)
5941 rtype = "parallel";
5942 else if (ctx->region_type & ORT_TASK)
5943 rtype = "task";
5944 else if (ctx->region_type & ORT_TEAMS)
5945 rtype = "teams";
5946 else
5947 gcc_unreachable ();
5949 error ("%qE not specified in enclosing %s",
5950 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
5951 error_at (ctx->location, "enclosing %s", rtype);
5953 /* FALLTHRU */
5954 case OMP_CLAUSE_DEFAULT_SHARED:
5955 flags |= GOVD_SHARED;
5956 break;
5957 case OMP_CLAUSE_DEFAULT_PRIVATE:
5958 flags |= GOVD_PRIVATE;
5959 break;
5960 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5961 flags |= GOVD_FIRSTPRIVATE;
5962 break;
5963 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5964 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5965 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5966 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
5968 omp_notice_variable (octx, decl, in_code);
5969 for (; octx; octx = octx->outer_context)
5971 splay_tree_node n2;
5973 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5974 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0
5975 && (n2 == NULL || (n2->value & GOVD_DATA_SHARE_CLASS) == 0))
5976 continue;
5977 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5979 flags |= GOVD_FIRSTPRIVATE;
5980 goto found_outer;
5982 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5984 flags |= GOVD_SHARED;
5985 goto found_outer;
5990 if (TREE_CODE (decl) == PARM_DECL
5991 || (!is_global_var (decl)
5992 && DECL_CONTEXT (decl) == current_function_decl))
5993 flags |= GOVD_FIRSTPRIVATE;
5994 else
5995 flags |= GOVD_SHARED;
5996 found_outer:
5997 break;
5999 default:
6000 gcc_unreachable ();
6003 return flags;
6007 /* Determine outer default flags for DECL mentioned in an OACC region
6008 but not declared in an enclosing clause. */
6010 static unsigned
6011 oacc_default_clause (struct gimplify_omp_ctx *ctx, tree decl, unsigned flags)
6013 const char *rkind;
6014 bool on_device = false;
6015 tree type = TREE_TYPE (decl);
6017 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6018 type = TREE_TYPE (type);
6020 if ((ctx->region_type & (ORT_ACC_PARALLEL | ORT_ACC_KERNELS)) != 0
6021 && is_global_var (decl)
6022 && device_resident_p (decl))
6024 on_device = true;
6025 flags |= GOVD_MAP_TO_ONLY;
6028 switch (ctx->region_type)
6030 default:
6031 gcc_unreachable ();
6033 case ORT_ACC_KERNELS:
6034 /* Scalars are default 'copy' under kernels, non-scalars are default
6035 'present_or_copy'. */
6036 flags |= GOVD_MAP;
6037 if (!AGGREGATE_TYPE_P (type))
6038 flags |= GOVD_MAP_FORCE;
6040 rkind = "kernels";
6041 break;
6043 case ORT_ACC_PARALLEL:
6045 if (on_device || AGGREGATE_TYPE_P (type))
6046 /* Aggregates default to 'present_or_copy'. */
6047 flags |= GOVD_MAP;
6048 else
6049 /* Scalars default to 'firstprivate'. */
6050 flags |= GOVD_FIRSTPRIVATE;
6051 rkind = "parallel";
6053 break;
6056 if (DECL_ARTIFICIAL (decl))
6057 ; /* We can get compiler-generated decls, and should not complain
6058 about them. */
6059 else if (ctx->default_kind == OMP_CLAUSE_DEFAULT_NONE)
6061 error ("%qE not specified in enclosing OpenACC %qs construct",
6062 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rkind);
6063 inform (ctx->location, "enclosing OpenACC %qs construct", rkind);
6065 else
6066 gcc_checking_assert (ctx->default_kind == OMP_CLAUSE_DEFAULT_SHARED);
6068 return flags;
6071 /* Record the fact that DECL was used within the OMP context CTX.
6072 IN_CODE is true when real code uses DECL, and false when we should
6073 merely emit default(none) errors. Return true if DECL is going to
6074 be remapped and thus DECL shouldn't be gimplified into its
6075 DECL_VALUE_EXPR (if any). */
6077 static bool
6078 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
6080 splay_tree_node n;
6081 unsigned flags = in_code ? GOVD_SEEN : 0;
6082 bool ret = false, shared;
6084 if (error_operand_p (decl))
6085 return false;
6087 if (ctx->region_type == ORT_NONE)
6088 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
6090 /* Threadprivate variables are predetermined. */
6091 if (is_global_var (decl))
6093 if (DECL_THREAD_LOCAL_P (decl))
6094 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
6096 if (DECL_HAS_VALUE_EXPR_P (decl))
6098 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6100 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
6101 return omp_notice_threadprivate_variable (ctx, decl, value);
6105 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6106 if ((ctx->region_type & ORT_TARGET) != 0)
6108 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
6109 if (n == NULL)
6111 unsigned nflags = flags;
6112 if (ctx->target_map_pointers_as_0len_arrays
6113 || ctx->target_map_scalars_firstprivate)
6115 bool is_declare_target = false;
6116 bool is_scalar = false;
6117 if (is_global_var (decl)
6118 && varpool_node::get_create (decl)->offloadable)
6120 struct gimplify_omp_ctx *octx;
6121 for (octx = ctx->outer_context;
6122 octx; octx = octx->outer_context)
6124 n = splay_tree_lookup (octx->variables,
6125 (splay_tree_key)decl);
6126 if (n
6127 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
6128 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6129 break;
6131 is_declare_target = octx == NULL;
6133 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
6135 tree type = TREE_TYPE (decl);
6136 if (TREE_CODE (type) == REFERENCE_TYPE)
6137 type = TREE_TYPE (type);
6138 if (TREE_CODE (type) == COMPLEX_TYPE)
6139 type = TREE_TYPE (type);
6140 if (INTEGRAL_TYPE_P (type)
6141 || SCALAR_FLOAT_TYPE_P (type)
6142 || TREE_CODE (type) == POINTER_TYPE)
6143 is_scalar = true;
6145 if (is_declare_target)
6147 else if (ctx->target_map_pointers_as_0len_arrays
6148 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
6149 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
6150 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
6151 == POINTER_TYPE)))
6152 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
6153 else if (is_scalar)
6154 nflags |= GOVD_FIRSTPRIVATE;
6157 struct gimplify_omp_ctx *octx = ctx->outer_context;
6158 if ((ctx->region_type & ORT_ACC) && octx)
6160 /* Look in outer OpenACC contexts, to see if there's a
6161 data attribute for this variable. */
6162 omp_notice_variable (octx, decl, in_code);
6164 for (; octx; octx = octx->outer_context)
6166 if (!(octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)))
6167 break;
6168 splay_tree_node n2
6169 = splay_tree_lookup (octx->variables,
6170 (splay_tree_key) decl);
6171 if (n2)
6173 if (octx->region_type == ORT_ACC_HOST_DATA)
6174 error ("variable %qE declared in enclosing "
6175 "%<host_data%> region", DECL_NAME (decl));
6176 nflags |= GOVD_MAP;
6177 goto found_outer;
6183 tree type = TREE_TYPE (decl);
6185 if (nflags == flags
6186 && gimplify_omp_ctxp->target_firstprivatize_array_bases
6187 && lang_hooks.decls.omp_privatize_by_reference (decl))
6188 type = TREE_TYPE (type);
6189 if (nflags == flags
6190 && !lang_hooks.types.omp_mappable_type (type))
6192 error ("%qD referenced in target region does not have "
6193 "a mappable type", decl);
6194 nflags |= GOVD_MAP | GOVD_EXPLICIT;
6196 else if (nflags == flags)
6198 if ((ctx->region_type & ORT_ACC) != 0)
6199 nflags = oacc_default_clause (ctx, decl, flags);
6200 else
6201 nflags |= GOVD_MAP;
6204 found_outer:
6205 omp_add_variable (ctx, decl, nflags);
6207 else
6209 /* If nothing changed, there's nothing left to do. */
6210 if ((n->value & flags) == flags)
6211 return ret;
6212 flags |= n->value;
6213 n->value = flags;
6215 goto do_outer;
6218 if (n == NULL)
6220 if (ctx->region_type == ORT_WORKSHARE
6221 || ctx->region_type == ORT_SIMD
6222 || ctx->region_type == ORT_ACC
6223 || (ctx->region_type & ORT_TARGET_DATA) != 0)
6224 goto do_outer;
6226 flags = omp_default_clause (ctx, decl, in_code, flags);
6228 if ((flags & GOVD_PRIVATE)
6229 && lang_hooks.decls.omp_private_outer_ref (decl))
6230 flags |= GOVD_PRIVATE_OUTER_REF;
6232 omp_add_variable (ctx, decl, flags);
6234 shared = (flags & GOVD_SHARED) != 0;
6235 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6236 goto do_outer;
6239 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6240 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6241 && DECL_SIZE (decl)
6242 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6244 splay_tree_node n2;
6245 tree t = DECL_VALUE_EXPR (decl);
6246 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6247 t = TREE_OPERAND (t, 0);
6248 gcc_assert (DECL_P (t));
6249 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6250 n2->value |= GOVD_SEEN;
6253 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6254 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6256 /* If nothing changed, there's nothing left to do. */
6257 if ((n->value & flags) == flags)
6258 return ret;
6259 flags |= n->value;
6260 n->value = flags;
6262 do_outer:
6263 /* If the variable is private in the current context, then we don't
6264 need to propagate anything to an outer context. */
6265 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6266 return ret;
6267 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6268 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6269 return ret;
6270 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6271 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6272 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6273 return ret;
6274 if (ctx->outer_context
6275 && omp_notice_variable (ctx->outer_context, decl, in_code))
6276 return true;
6277 return ret;
6280 /* Verify that DECL is private within CTX. If there's specific information
6281 to the contrary in the innermost scope, generate an error. */
6283 static bool
6284 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
6286 splay_tree_node n;
6288 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6289 if (n != NULL)
6291 if (n->value & GOVD_SHARED)
6293 if (ctx == gimplify_omp_ctxp)
6295 if (simd)
6296 error ("iteration variable %qE is predetermined linear",
6297 DECL_NAME (decl));
6298 else
6299 error ("iteration variable %qE should be private",
6300 DECL_NAME (decl));
6301 n->value = GOVD_PRIVATE;
6302 return true;
6304 else
6305 return false;
6307 else if ((n->value & GOVD_EXPLICIT) != 0
6308 && (ctx == gimplify_omp_ctxp
6309 || (ctx->region_type == ORT_COMBINED_PARALLEL
6310 && gimplify_omp_ctxp->outer_context == ctx)))
6312 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6313 error ("iteration variable %qE should not be firstprivate",
6314 DECL_NAME (decl));
6315 else if ((n->value & GOVD_REDUCTION) != 0)
6316 error ("iteration variable %qE should not be reduction",
6317 DECL_NAME (decl));
6318 else if (simd == 0 && (n->value & GOVD_LINEAR) != 0)
6319 error ("iteration variable %qE should not be linear",
6320 DECL_NAME (decl));
6321 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
6322 error ("iteration variable %qE should not be lastprivate",
6323 DECL_NAME (decl));
6324 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6325 error ("iteration variable %qE should not be private",
6326 DECL_NAME (decl));
6327 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
6328 error ("iteration variable %qE is predetermined linear",
6329 DECL_NAME (decl));
6331 return (ctx == gimplify_omp_ctxp
6332 || (ctx->region_type == ORT_COMBINED_PARALLEL
6333 && gimplify_omp_ctxp->outer_context == ctx));
6336 if (ctx->region_type != ORT_WORKSHARE
6337 && ctx->region_type != ORT_SIMD
6338 && ctx->region_type != ORT_ACC)
6339 return false;
6340 else if (ctx->outer_context)
6341 return omp_is_private (ctx->outer_context, decl, simd);
6342 return false;
6345 /* Return true if DECL is private within a parallel region
6346 that binds to the current construct's context or in parallel
6347 region's REDUCTION clause. */
6349 static bool
6350 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6352 splay_tree_node n;
6356 ctx = ctx->outer_context;
6357 if (ctx == NULL)
6359 if (is_global_var (decl))
6360 return false;
6362 /* References might be private, but might be shared too,
6363 when checking for copyprivate, assume they might be
6364 private, otherwise assume they might be shared. */
6365 if (copyprivate)
6366 return true;
6368 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6369 return false;
6371 /* Treat C++ privatized non-static data members outside
6372 of the privatization the same. */
6373 if (omp_member_access_dummy_var (decl))
6374 return false;
6376 return true;
6379 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6381 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
6382 && (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0))
6383 continue;
6385 if (n != NULL)
6387 if ((n->value & GOVD_LOCAL) != 0
6388 && omp_member_access_dummy_var (decl))
6389 return false;
6390 return (n->value & GOVD_SHARED) == 0;
6393 while (ctx->region_type == ORT_WORKSHARE
6394 || ctx->region_type == ORT_SIMD
6395 || ctx->region_type == ORT_ACC);
6396 return false;
6399 /* Return true if the CTX is combined with distribute and thus
6400 lastprivate can't be supported. */
6402 static bool
6403 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
6407 if (ctx->outer_context == NULL)
6408 return false;
6409 ctx = ctx->outer_context;
6410 switch (ctx->region_type)
6412 case ORT_WORKSHARE:
6413 if (!ctx->combined_loop)
6414 return false;
6415 if (ctx->distribute)
6416 return lang_GNU_Fortran ();
6417 break;
6418 case ORT_COMBINED_PARALLEL:
6419 break;
6420 case ORT_COMBINED_TEAMS:
6421 return lang_GNU_Fortran ();
6422 default:
6423 return false;
6426 while (1);
6429 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
6431 static tree
6432 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
6434 tree t = *tp;
6436 /* If this node has been visited, unmark it and keep looking. */
6437 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
6438 return t;
6440 if (IS_TYPE_OR_DECL_P (t))
6441 *walk_subtrees = 0;
6442 return NULL_TREE;
6445 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6446 and previous omp contexts. */
6448 static void
6449 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6450 enum omp_region_type region_type,
6451 enum tree_code code)
6453 struct gimplify_omp_ctx *ctx, *outer_ctx;
6454 tree c;
6455 hash_map<tree, tree> *struct_map_to_clause = NULL;
6456 tree *prev_list_p = NULL;
6458 ctx = new_omp_context (region_type);
6459 outer_ctx = ctx->outer_context;
6460 if (code == OMP_TARGET && !lang_GNU_Fortran ())
6462 ctx->target_map_pointers_as_0len_arrays = true;
6463 /* FIXME: For Fortran we want to set this too, when
6464 the Fortran FE is updated to OpenMP 4.5. */
6465 ctx->target_map_scalars_firstprivate = true;
6467 if (!lang_GNU_Fortran ())
6468 switch (code)
6470 case OMP_TARGET:
6471 case OMP_TARGET_DATA:
6472 case OMP_TARGET_ENTER_DATA:
6473 case OMP_TARGET_EXIT_DATA:
6474 case OACC_HOST_DATA:
6475 ctx->target_firstprivatize_array_bases = true;
6476 default:
6477 break;
6480 while ((c = *list_p) != NULL)
6482 bool remove = false;
6483 bool notice_outer = true;
6484 const char *check_non_private = NULL;
6485 unsigned int flags;
6486 tree decl;
6488 switch (OMP_CLAUSE_CODE (c))
6490 case OMP_CLAUSE_PRIVATE:
6491 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6492 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6494 flags |= GOVD_PRIVATE_OUTER_REF;
6495 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6497 else
6498 notice_outer = false;
6499 goto do_add;
6500 case OMP_CLAUSE_SHARED:
6501 flags = GOVD_SHARED | GOVD_EXPLICIT;
6502 goto do_add;
6503 case OMP_CLAUSE_FIRSTPRIVATE:
6504 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6505 check_non_private = "firstprivate";
6506 goto do_add;
6507 case OMP_CLAUSE_LASTPRIVATE:
6508 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6509 check_non_private = "lastprivate";
6510 decl = OMP_CLAUSE_DECL (c);
6511 if (omp_no_lastprivate (ctx))
6513 notice_outer = false;
6514 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6516 else if (error_operand_p (decl))
6517 goto do_add;
6518 else if (outer_ctx
6519 && (outer_ctx->region_type == ORT_COMBINED_PARALLEL
6520 || outer_ctx->region_type == ORT_COMBINED_TEAMS)
6521 && splay_tree_lookup (outer_ctx->variables,
6522 (splay_tree_key) decl) == NULL)
6524 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
6525 if (outer_ctx->outer_context)
6526 omp_notice_variable (outer_ctx->outer_context, decl, true);
6528 else if (outer_ctx
6529 && (outer_ctx->region_type & ORT_TASK) != 0
6530 && outer_ctx->combined_loop
6531 && splay_tree_lookup (outer_ctx->variables,
6532 (splay_tree_key) decl) == NULL)
6534 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6535 if (outer_ctx->outer_context)
6536 omp_notice_variable (outer_ctx->outer_context, decl, true);
6538 else if (outer_ctx
6539 && (outer_ctx->region_type == ORT_WORKSHARE
6540 || outer_ctx->region_type == ORT_ACC)
6541 && outer_ctx->combined_loop
6542 && splay_tree_lookup (outer_ctx->variables,
6543 (splay_tree_key) decl) == NULL
6544 && !omp_check_private (outer_ctx, decl, false))
6546 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6547 if (outer_ctx->outer_context
6548 && (outer_ctx->outer_context->region_type
6549 == ORT_COMBINED_PARALLEL)
6550 && splay_tree_lookup (outer_ctx->outer_context->variables,
6551 (splay_tree_key) decl) == NULL)
6553 struct gimplify_omp_ctx *octx = outer_ctx->outer_context;
6554 omp_add_variable (octx, decl, GOVD_SHARED | GOVD_SEEN);
6555 if (octx->outer_context)
6556 omp_notice_variable (octx->outer_context, decl, true);
6558 else if (outer_ctx->outer_context)
6559 omp_notice_variable (outer_ctx->outer_context, decl, true);
6561 goto do_add;
6562 case OMP_CLAUSE_REDUCTION:
6563 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6564 /* OpenACC permits reductions on private variables. */
6565 if (!(region_type & ORT_ACC))
6566 check_non_private = "reduction";
6567 decl = OMP_CLAUSE_DECL (c);
6568 if (TREE_CODE (decl) == MEM_REF)
6570 tree type = TREE_TYPE (decl);
6571 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
6572 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6574 remove = true;
6575 break;
6577 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6578 if (DECL_P (v))
6580 omp_firstprivatize_variable (ctx, v);
6581 omp_notice_variable (ctx, v, true);
6583 decl = TREE_OPERAND (decl, 0);
6584 if (TREE_CODE (decl) == POINTER_PLUS_EXPR)
6586 if (gimplify_expr (&TREE_OPERAND (decl, 1), pre_p,
6587 NULL, is_gimple_val, fb_rvalue)
6588 == GS_ERROR)
6590 remove = true;
6591 break;
6593 v = TREE_OPERAND (decl, 1);
6594 if (DECL_P (v))
6596 omp_firstprivatize_variable (ctx, v);
6597 omp_notice_variable (ctx, v, true);
6599 decl = TREE_OPERAND (decl, 0);
6601 if (TREE_CODE (decl) == ADDR_EXPR
6602 || TREE_CODE (decl) == INDIRECT_REF)
6603 decl = TREE_OPERAND (decl, 0);
6605 goto do_add_decl;
6606 case OMP_CLAUSE_LINEAR:
6607 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6608 is_gimple_val, fb_rvalue) == GS_ERROR)
6610 remove = true;
6611 break;
6613 else
6615 if (code == OMP_SIMD
6616 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6618 struct gimplify_omp_ctx *octx = outer_ctx;
6619 if (octx
6620 && octx->region_type == ORT_WORKSHARE
6621 && octx->combined_loop
6622 && !octx->distribute)
6624 if (octx->outer_context
6625 && (octx->outer_context->region_type
6626 == ORT_COMBINED_PARALLEL))
6627 octx = octx->outer_context->outer_context;
6628 else
6629 octx = octx->outer_context;
6631 if (octx
6632 && octx->region_type == ORT_WORKSHARE
6633 && octx->combined_loop
6634 && octx->distribute
6635 && !lang_GNU_Fortran ())
6637 error_at (OMP_CLAUSE_LOCATION (c),
6638 "%<linear%> clause for variable other than "
6639 "loop iterator specified on construct "
6640 "combined with %<distribute%>");
6641 remove = true;
6642 break;
6645 /* For combined #pragma omp parallel for simd, need to put
6646 lastprivate and perhaps firstprivate too on the
6647 parallel. Similarly for #pragma omp for simd. */
6648 struct gimplify_omp_ctx *octx = outer_ctx;
6649 decl = NULL_TREE;
6650 if (omp_no_lastprivate (ctx))
6651 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6654 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6655 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6656 break;
6657 decl = OMP_CLAUSE_DECL (c);
6658 if (error_operand_p (decl))
6660 decl = NULL_TREE;
6661 break;
6663 flags = GOVD_SEEN;
6664 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6665 flags |= GOVD_FIRSTPRIVATE;
6666 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6667 flags |= GOVD_LASTPRIVATE;
6668 if (octx
6669 && octx->region_type == ORT_WORKSHARE
6670 && octx->combined_loop)
6672 if (octx->outer_context
6673 && (octx->outer_context->region_type
6674 == ORT_COMBINED_PARALLEL))
6675 octx = octx->outer_context;
6676 else if (omp_check_private (octx, decl, false))
6677 break;
6679 else if (octx
6680 && (octx->region_type & ORT_TASK) != 0
6681 && octx->combined_loop)
6683 else if (octx
6684 && octx->region_type == ORT_COMBINED_PARALLEL
6685 && ctx->region_type == ORT_WORKSHARE
6686 && octx == outer_ctx)
6687 flags = GOVD_SEEN | GOVD_SHARED;
6688 else if (octx
6689 && octx->region_type == ORT_COMBINED_TEAMS)
6690 flags = GOVD_SEEN | GOVD_SHARED;
6691 else if (octx
6692 && octx->region_type == ORT_COMBINED_TARGET)
6694 flags &= ~GOVD_LASTPRIVATE;
6695 if (flags == GOVD_SEEN)
6696 break;
6698 else
6699 break;
6700 splay_tree_node on
6701 = splay_tree_lookup (octx->variables,
6702 (splay_tree_key) decl);
6703 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
6705 octx = NULL;
6706 break;
6708 omp_add_variable (octx, decl, flags);
6709 if (octx->outer_context == NULL)
6710 break;
6711 octx = octx->outer_context;
6713 while (1);
6714 if (octx
6715 && decl
6716 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6717 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6718 omp_notice_variable (octx, decl, true);
6720 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6721 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6722 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6724 notice_outer = false;
6725 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6727 goto do_add;
6729 case OMP_CLAUSE_MAP:
6730 decl = OMP_CLAUSE_DECL (c);
6731 if (error_operand_p (decl))
6732 remove = true;
6733 switch (code)
6735 case OMP_TARGET:
6736 break;
6737 case OMP_TARGET_DATA:
6738 case OMP_TARGET_ENTER_DATA:
6739 case OMP_TARGET_EXIT_DATA:
6740 case OACC_HOST_DATA:
6741 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6742 || (OMP_CLAUSE_MAP_KIND (c)
6743 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
6744 /* For target {,enter ,exit }data only the array slice is
6745 mapped, but not the pointer to it. */
6746 remove = true;
6747 break;
6748 default:
6749 break;
6751 if (remove)
6752 break;
6753 if (DECL_P (decl) && outer_ctx && (region_type & ORT_ACC))
6755 struct gimplify_omp_ctx *octx;
6756 for (octx = outer_ctx; octx; octx = octx->outer_context)
6758 if (octx->region_type != ORT_ACC_HOST_DATA)
6759 break;
6760 splay_tree_node n2
6761 = splay_tree_lookup (octx->variables,
6762 (splay_tree_key) decl);
6763 if (n2)
6764 error_at (OMP_CLAUSE_LOCATION (c), "variable %qE "
6765 "declared in enclosing %<host_data%> region",
6766 DECL_NAME (decl));
6769 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6770 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6771 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6772 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6773 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6775 remove = true;
6776 break;
6778 else if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6779 || (OMP_CLAUSE_MAP_KIND (c)
6780 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
6781 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
6783 OMP_CLAUSE_SIZE (c)
6784 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL);
6785 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
6786 GOVD_FIRSTPRIVATE | GOVD_SEEN);
6788 if (!DECL_P (decl))
6790 tree d = decl, *pd;
6791 if (TREE_CODE (d) == ARRAY_REF)
6793 while (TREE_CODE (d) == ARRAY_REF)
6794 d = TREE_OPERAND (d, 0);
6795 if (TREE_CODE (d) == COMPONENT_REF
6796 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
6797 decl = d;
6799 pd = &OMP_CLAUSE_DECL (c);
6800 if (d == decl
6801 && TREE_CODE (decl) == INDIRECT_REF
6802 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
6803 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
6804 == REFERENCE_TYPE))
6806 pd = &TREE_OPERAND (decl, 0);
6807 decl = TREE_OPERAND (decl, 0);
6809 if (TREE_CODE (decl) == COMPONENT_REF)
6811 while (TREE_CODE (decl) == COMPONENT_REF)
6812 decl = TREE_OPERAND (decl, 0);
6814 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
6815 == GS_ERROR)
6817 remove = true;
6818 break;
6820 if (DECL_P (decl))
6822 if (error_operand_p (decl))
6824 remove = true;
6825 break;
6828 if (TYPE_SIZE_UNIT (TREE_TYPE (decl)) == NULL
6829 || (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (decl)))
6830 != INTEGER_CST))
6832 error_at (OMP_CLAUSE_LOCATION (c),
6833 "mapping field %qE of variable length "
6834 "structure", OMP_CLAUSE_DECL (c));
6835 remove = true;
6836 break;
6839 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
6841 /* Error recovery. */
6842 if (prev_list_p == NULL)
6844 remove = true;
6845 break;
6847 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
6849 tree ch = OMP_CLAUSE_CHAIN (*prev_list_p);
6850 if (ch == NULL_TREE || OMP_CLAUSE_CHAIN (ch) != c)
6852 remove = true;
6853 break;
6858 tree offset;
6859 HOST_WIDE_INT bitsize, bitpos;
6860 machine_mode mode;
6861 int unsignedp, reversep, volatilep = 0;
6862 tree base = OMP_CLAUSE_DECL (c);
6863 while (TREE_CODE (base) == ARRAY_REF)
6864 base = TREE_OPERAND (base, 0);
6865 if (TREE_CODE (base) == INDIRECT_REF)
6866 base = TREE_OPERAND (base, 0);
6867 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
6868 &mode, &unsignedp, &reversep,
6869 &volatilep, false);
6870 gcc_assert (base == decl
6871 && (offset == NULL_TREE
6872 || TREE_CODE (offset) == INTEGER_CST));
6874 splay_tree_node n
6875 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6876 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
6877 == GOMP_MAP_ALWAYS_POINTER);
6878 if (n == NULL || (n->value & GOVD_MAP) == 0)
6880 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6881 OMP_CLAUSE_MAP);
6882 OMP_CLAUSE_SET_MAP_KIND (l, GOMP_MAP_STRUCT);
6883 OMP_CLAUSE_DECL (l) = decl;
6884 OMP_CLAUSE_SIZE (l) = size_int (1);
6885 if (struct_map_to_clause == NULL)
6886 struct_map_to_clause = new hash_map<tree, tree>;
6887 struct_map_to_clause->put (decl, l);
6888 if (ptr)
6890 enum gomp_map_kind mkind
6891 = code == OMP_TARGET_EXIT_DATA
6892 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
6893 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6894 OMP_CLAUSE_MAP);
6895 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
6896 OMP_CLAUSE_DECL (c2)
6897 = unshare_expr (OMP_CLAUSE_DECL (c));
6898 OMP_CLAUSE_CHAIN (c2) = *prev_list_p;
6899 OMP_CLAUSE_SIZE (c2)
6900 = TYPE_SIZE_UNIT (ptr_type_node);
6901 OMP_CLAUSE_CHAIN (l) = c2;
6902 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
6904 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
6905 tree c3
6906 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6907 OMP_CLAUSE_MAP);
6908 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
6909 OMP_CLAUSE_DECL (c3)
6910 = unshare_expr (OMP_CLAUSE_DECL (c4));
6911 OMP_CLAUSE_SIZE (c3)
6912 = TYPE_SIZE_UNIT (ptr_type_node);
6913 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
6914 OMP_CLAUSE_CHAIN (c2) = c3;
6916 *prev_list_p = l;
6917 prev_list_p = NULL;
6919 else
6921 OMP_CLAUSE_CHAIN (l) = c;
6922 *list_p = l;
6923 list_p = &OMP_CLAUSE_CHAIN (l);
6925 flags = GOVD_MAP | GOVD_EXPLICIT;
6926 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
6927 flags |= GOVD_SEEN;
6928 goto do_add_decl;
6930 else
6932 tree *osc = struct_map_to_clause->get (decl);
6933 tree *sc = NULL, *scp = NULL;
6934 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) || ptr)
6935 n->value |= GOVD_SEEN;
6936 offset_int o1, o2;
6937 if (offset)
6938 o1 = wi::to_offset (offset);
6939 else
6940 o1 = 0;
6941 if (bitpos)
6942 o1 = o1 + bitpos / BITS_PER_UNIT;
6943 for (sc = &OMP_CLAUSE_CHAIN (*osc);
6944 *sc != c; sc = &OMP_CLAUSE_CHAIN (*sc))
6945 if (ptr && sc == prev_list_p)
6946 break;
6947 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6948 != COMPONENT_REF
6949 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6950 != INDIRECT_REF)
6951 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6952 != ARRAY_REF))
6953 break;
6954 else
6956 tree offset2;
6957 HOST_WIDE_INT bitsize2, bitpos2;
6958 base = OMP_CLAUSE_DECL (*sc);
6959 if (TREE_CODE (base) == ARRAY_REF)
6961 while (TREE_CODE (base) == ARRAY_REF)
6962 base = TREE_OPERAND (base, 0);
6963 if (TREE_CODE (base) != COMPONENT_REF
6964 || (TREE_CODE (TREE_TYPE (base))
6965 != ARRAY_TYPE))
6966 break;
6968 else if (TREE_CODE (base) == INDIRECT_REF
6969 && (TREE_CODE (TREE_OPERAND (base, 0))
6970 == COMPONENT_REF)
6971 && (TREE_CODE (TREE_TYPE
6972 (TREE_OPERAND (base, 0)))
6973 == REFERENCE_TYPE))
6974 base = TREE_OPERAND (base, 0);
6975 base = get_inner_reference (base, &bitsize2,
6976 &bitpos2, &offset2,
6977 &mode, &unsignedp,
6978 &reversep, &volatilep,
6979 false);
6980 if (base != decl)
6981 break;
6982 if (scp)
6983 continue;
6984 gcc_assert (offset == NULL_TREE
6985 || TREE_CODE (offset) == INTEGER_CST);
6986 tree d1 = OMP_CLAUSE_DECL (*sc);
6987 tree d2 = OMP_CLAUSE_DECL (c);
6988 while (TREE_CODE (d1) == ARRAY_REF)
6989 d1 = TREE_OPERAND (d1, 0);
6990 while (TREE_CODE (d2) == ARRAY_REF)
6991 d2 = TREE_OPERAND (d2, 0);
6992 if (TREE_CODE (d1) == INDIRECT_REF)
6993 d1 = TREE_OPERAND (d1, 0);
6994 if (TREE_CODE (d2) == INDIRECT_REF)
6995 d2 = TREE_OPERAND (d2, 0);
6996 while (TREE_CODE (d1) == COMPONENT_REF)
6997 if (TREE_CODE (d2) == COMPONENT_REF
6998 && TREE_OPERAND (d1, 1)
6999 == TREE_OPERAND (d2, 1))
7001 d1 = TREE_OPERAND (d1, 0);
7002 d2 = TREE_OPERAND (d2, 0);
7004 else
7005 break;
7006 if (d1 == d2)
7008 error_at (OMP_CLAUSE_LOCATION (c),
7009 "%qE appears more than once in map "
7010 "clauses", OMP_CLAUSE_DECL (c));
7011 remove = true;
7012 break;
7014 if (offset2)
7015 o2 = wi::to_offset (offset2);
7016 else
7017 o2 = 0;
7018 if (bitpos2)
7019 o2 = o2 + bitpos2 / BITS_PER_UNIT;
7020 if (wi::ltu_p (o1, o2)
7021 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
7023 if (ptr)
7024 scp = sc;
7025 else
7026 break;
7029 if (remove)
7030 break;
7031 OMP_CLAUSE_SIZE (*osc)
7032 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
7033 size_one_node);
7034 if (ptr)
7036 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7037 OMP_CLAUSE_MAP);
7038 tree cl = NULL_TREE;
7039 enum gomp_map_kind mkind
7040 = code == OMP_TARGET_EXIT_DATA
7041 ? GOMP_MAP_RELEASE : GOMP_MAP_ALLOC;
7042 OMP_CLAUSE_SET_MAP_KIND (c2, mkind);
7043 OMP_CLAUSE_DECL (c2)
7044 = unshare_expr (OMP_CLAUSE_DECL (c));
7045 OMP_CLAUSE_CHAIN (c2) = scp ? *scp : *prev_list_p;
7046 OMP_CLAUSE_SIZE (c2)
7047 = TYPE_SIZE_UNIT (ptr_type_node);
7048 cl = scp ? *prev_list_p : c2;
7049 if (OMP_CLAUSE_CHAIN (*prev_list_p) != c)
7051 tree c4 = OMP_CLAUSE_CHAIN (*prev_list_p);
7052 tree c3
7053 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7054 OMP_CLAUSE_MAP);
7055 OMP_CLAUSE_SET_MAP_KIND (c3, mkind);
7056 OMP_CLAUSE_DECL (c3)
7057 = unshare_expr (OMP_CLAUSE_DECL (c4));
7058 OMP_CLAUSE_SIZE (c3)
7059 = TYPE_SIZE_UNIT (ptr_type_node);
7060 OMP_CLAUSE_CHAIN (c3) = *prev_list_p;
7061 if (!scp)
7062 OMP_CLAUSE_CHAIN (c2) = c3;
7063 else
7064 cl = c3;
7066 if (scp)
7067 *scp = c2;
7068 if (sc == prev_list_p)
7070 *sc = cl;
7071 prev_list_p = NULL;
7073 else
7075 *prev_list_p = OMP_CLAUSE_CHAIN (c);
7076 list_p = prev_list_p;
7077 prev_list_p = NULL;
7078 OMP_CLAUSE_CHAIN (c) = *sc;
7079 *sc = cl;
7080 continue;
7083 else if (*sc != c)
7085 *list_p = OMP_CLAUSE_CHAIN (c);
7086 OMP_CLAUSE_CHAIN (c) = *sc;
7087 *sc = c;
7088 continue;
7092 if (!remove
7093 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
7094 && OMP_CLAUSE_CHAIN (c)
7095 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c)) == OMP_CLAUSE_MAP
7096 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c))
7097 == GOMP_MAP_ALWAYS_POINTER))
7098 prev_list_p = list_p;
7099 break;
7101 flags = GOVD_MAP | GOVD_EXPLICIT;
7102 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
7103 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
7104 flags |= GOVD_MAP_ALWAYS_TO;
7105 goto do_add;
7107 case OMP_CLAUSE_DEPEND:
7108 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
7109 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
7111 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
7112 omp-low.c. */
7113 break;
7115 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
7117 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
7118 NULL, is_gimple_val, fb_rvalue);
7119 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
7121 if (error_operand_p (OMP_CLAUSE_DECL (c)))
7123 remove = true;
7124 break;
7126 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
7127 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
7128 is_gimple_val, fb_rvalue) == GS_ERROR)
7130 remove = true;
7131 break;
7133 break;
7135 case OMP_CLAUSE_TO:
7136 case OMP_CLAUSE_FROM:
7137 case OMP_CLAUSE__CACHE_:
7138 decl = OMP_CLAUSE_DECL (c);
7139 if (error_operand_p (decl))
7141 remove = true;
7142 break;
7144 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7145 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
7146 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
7147 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
7148 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
7150 remove = true;
7151 break;
7153 if (!DECL_P (decl))
7155 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
7156 NULL, is_gimple_lvalue, fb_lvalue)
7157 == GS_ERROR)
7159 remove = true;
7160 break;
7162 break;
7164 goto do_notice;
7166 case OMP_CLAUSE_USE_DEVICE_PTR:
7167 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7168 goto do_add;
7169 case OMP_CLAUSE_IS_DEVICE_PTR:
7170 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
7171 goto do_add;
7173 do_add:
7174 decl = OMP_CLAUSE_DECL (c);
7175 do_add_decl:
7176 if (error_operand_p (decl))
7178 remove = true;
7179 break;
7181 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
7183 tree t = omp_member_access_dummy_var (decl);
7184 if (t)
7186 tree v = DECL_VALUE_EXPR (decl);
7187 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
7188 if (outer_ctx)
7189 omp_notice_variable (outer_ctx, t, true);
7192 omp_add_variable (ctx, decl, flags);
7193 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7194 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7196 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
7197 GOVD_LOCAL | GOVD_SEEN);
7198 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
7199 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
7200 find_decl_expr,
7201 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
7202 NULL) == NULL_TREE)
7203 omp_add_variable (ctx,
7204 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
7205 GOVD_LOCAL | GOVD_SEEN);
7206 gimplify_omp_ctxp = ctx;
7207 push_gimplify_context ();
7209 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
7210 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
7212 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
7213 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
7214 pop_gimplify_context
7215 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
7216 push_gimplify_context ();
7217 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
7218 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
7219 pop_gimplify_context
7220 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
7221 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
7222 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
7224 gimplify_omp_ctxp = outer_ctx;
7226 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7227 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
7229 gimplify_omp_ctxp = ctx;
7230 push_gimplify_context ();
7231 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
7233 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
7234 NULL, NULL);
7235 TREE_SIDE_EFFECTS (bind) = 1;
7236 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
7237 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
7239 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
7240 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7241 pop_gimplify_context
7242 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
7243 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
7245 gimplify_omp_ctxp = outer_ctx;
7247 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7248 && OMP_CLAUSE_LINEAR_STMT (c))
7250 gimplify_omp_ctxp = ctx;
7251 push_gimplify_context ();
7252 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
7254 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
7255 NULL, NULL);
7256 TREE_SIDE_EFFECTS (bind) = 1;
7257 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
7258 OMP_CLAUSE_LINEAR_STMT (c) = bind;
7260 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
7261 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
7262 pop_gimplify_context
7263 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
7264 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
7266 gimplify_omp_ctxp = outer_ctx;
7268 if (notice_outer)
7269 goto do_notice;
7270 break;
7272 case OMP_CLAUSE_COPYIN:
7273 case OMP_CLAUSE_COPYPRIVATE:
7274 decl = OMP_CLAUSE_DECL (c);
7275 if (error_operand_p (decl))
7277 remove = true;
7278 break;
7280 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
7281 && !remove
7282 && !omp_check_private (ctx, decl, true))
7284 remove = true;
7285 if (is_global_var (decl))
7287 if (DECL_THREAD_LOCAL_P (decl))
7288 remove = false;
7289 else if (DECL_HAS_VALUE_EXPR_P (decl))
7291 tree value = get_base_address (DECL_VALUE_EXPR (decl));
7293 if (value
7294 && DECL_P (value)
7295 && DECL_THREAD_LOCAL_P (value))
7296 remove = false;
7299 if (remove)
7300 error_at (OMP_CLAUSE_LOCATION (c),
7301 "copyprivate variable %qE is not threadprivate"
7302 " or private in outer context", DECL_NAME (decl));
7304 do_notice:
7305 if (outer_ctx)
7306 omp_notice_variable (outer_ctx, decl, true);
7307 if (check_non_private
7308 && region_type == ORT_WORKSHARE
7309 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
7310 || decl == OMP_CLAUSE_DECL (c)
7311 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
7312 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
7313 == ADDR_EXPR
7314 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
7315 == POINTER_PLUS_EXPR
7316 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
7317 (OMP_CLAUSE_DECL (c), 0), 0))
7318 == ADDR_EXPR)))))
7319 && omp_check_private (ctx, decl, false))
7321 error ("%s variable %qE is private in outer context",
7322 check_non_private, DECL_NAME (decl));
7323 remove = true;
7325 break;
7327 case OMP_CLAUSE_IF:
7328 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
7329 && OMP_CLAUSE_IF_MODIFIER (c) != code)
7331 const char *p[2];
7332 for (int i = 0; i < 2; i++)
7333 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
7335 case OMP_PARALLEL: p[i] = "parallel"; break;
7336 case OMP_TASK: p[i] = "task"; break;
7337 case OMP_TASKLOOP: p[i] = "taskloop"; break;
7338 case OMP_TARGET_DATA: p[i] = "target data"; break;
7339 case OMP_TARGET: p[i] = "target"; break;
7340 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
7341 case OMP_TARGET_ENTER_DATA:
7342 p[i] = "target enter data"; break;
7343 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
7344 default: gcc_unreachable ();
7346 error_at (OMP_CLAUSE_LOCATION (c),
7347 "expected %qs %<if%> clause modifier rather than %qs",
7348 p[0], p[1]);
7349 remove = true;
7351 /* Fall through. */
7353 case OMP_CLAUSE_FINAL:
7354 OMP_CLAUSE_OPERAND (c, 0)
7355 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
7356 /* Fall through. */
7358 case OMP_CLAUSE_SCHEDULE:
7359 case OMP_CLAUSE_NUM_THREADS:
7360 case OMP_CLAUSE_NUM_TEAMS:
7361 case OMP_CLAUSE_THREAD_LIMIT:
7362 case OMP_CLAUSE_DIST_SCHEDULE:
7363 case OMP_CLAUSE_DEVICE:
7364 case OMP_CLAUSE_PRIORITY:
7365 case OMP_CLAUSE_GRAINSIZE:
7366 case OMP_CLAUSE_NUM_TASKS:
7367 case OMP_CLAUSE_HINT:
7368 case OMP_CLAUSE__CILK_FOR_COUNT_:
7369 case OMP_CLAUSE_ASYNC:
7370 case OMP_CLAUSE_WAIT:
7371 case OMP_CLAUSE_NUM_GANGS:
7372 case OMP_CLAUSE_NUM_WORKERS:
7373 case OMP_CLAUSE_VECTOR_LENGTH:
7374 case OMP_CLAUSE_WORKER:
7375 case OMP_CLAUSE_VECTOR:
7376 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
7377 is_gimple_val, fb_rvalue) == GS_ERROR)
7378 remove = true;
7379 break;
7381 case OMP_CLAUSE_GANG:
7382 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
7383 is_gimple_val, fb_rvalue) == GS_ERROR)
7384 remove = true;
7385 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
7386 is_gimple_val, fb_rvalue) == GS_ERROR)
7387 remove = true;
7388 break;
7390 case OMP_CLAUSE_TILE:
7391 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7392 list = TREE_CHAIN (list))
7394 if (gimplify_expr (&TREE_VALUE (list), pre_p, NULL,
7395 is_gimple_val, fb_rvalue) == GS_ERROR)
7396 remove = true;
7398 break;
7400 case OMP_CLAUSE_DEVICE_RESIDENT:
7401 remove = true;
7402 break;
7404 case OMP_CLAUSE_NOWAIT:
7405 case OMP_CLAUSE_ORDERED:
7406 case OMP_CLAUSE_UNTIED:
7407 case OMP_CLAUSE_COLLAPSE:
7408 case OMP_CLAUSE_AUTO:
7409 case OMP_CLAUSE_SEQ:
7410 case OMP_CLAUSE_INDEPENDENT:
7411 case OMP_CLAUSE_MERGEABLE:
7412 case OMP_CLAUSE_PROC_BIND:
7413 case OMP_CLAUSE_SAFELEN:
7414 case OMP_CLAUSE_SIMDLEN:
7415 case OMP_CLAUSE_NOGROUP:
7416 case OMP_CLAUSE_THREADS:
7417 case OMP_CLAUSE_SIMD:
7418 break;
7420 case OMP_CLAUSE_DEFAULTMAP:
7421 ctx->target_map_scalars_firstprivate = false;
7422 break;
7424 case OMP_CLAUSE_ALIGNED:
7425 decl = OMP_CLAUSE_DECL (c);
7426 if (error_operand_p (decl))
7428 remove = true;
7429 break;
7431 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
7432 is_gimple_val, fb_rvalue) == GS_ERROR)
7434 remove = true;
7435 break;
7437 if (!is_global_var (decl)
7438 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7439 omp_add_variable (ctx, decl, GOVD_ALIGNED);
7440 break;
7442 case OMP_CLAUSE_DEFAULT:
7443 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
7444 break;
7446 default:
7447 gcc_unreachable ();
7450 if (remove)
7451 *list_p = OMP_CLAUSE_CHAIN (c);
7452 else
7453 list_p = &OMP_CLAUSE_CHAIN (c);
7456 gimplify_omp_ctxp = ctx;
7457 if (struct_map_to_clause)
7458 delete struct_map_to_clause;
7461 /* Return true if DECL is a candidate for shared to firstprivate
7462 optimization. We only consider non-addressable scalars, not
7463 too big, and not references. */
7465 static bool
7466 omp_shared_to_firstprivate_optimizable_decl_p (tree decl)
7468 if (TREE_ADDRESSABLE (decl))
7469 return false;
7470 tree type = TREE_TYPE (decl);
7471 if (!is_gimple_reg_type (type)
7472 || TREE_CODE (type) == REFERENCE_TYPE
7473 || TREE_ADDRESSABLE (type))
7474 return false;
7475 /* Don't optimize too large decls, as each thread/task will have
7476 its own. */
7477 HOST_WIDE_INT len = int_size_in_bytes (type);
7478 if (len == -1 || len > 4 * POINTER_SIZE / BITS_PER_UNIT)
7479 return false;
7480 if (lang_hooks.decls.omp_privatize_by_reference (decl))
7481 return false;
7482 return true;
7485 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
7486 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
7487 GOVD_WRITTEN in outer contexts. */
7489 static void
7490 omp_mark_stores (struct gimplify_omp_ctx *ctx, tree decl)
7492 for (; ctx; ctx = ctx->outer_context)
7494 splay_tree_node n = splay_tree_lookup (ctx->variables,
7495 (splay_tree_key) decl);
7496 if (n == NULL)
7497 continue;
7498 else if (n->value & GOVD_SHARED)
7500 n->value |= GOVD_WRITTEN;
7501 return;
7503 else if (n->value & GOVD_DATA_SHARE_CLASS)
7504 return;
7508 /* Helper callback for walk_gimple_seq to discover possible stores
7509 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
7510 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
7511 for those. */
7513 static tree
7514 omp_find_stores_op (tree *tp, int *walk_subtrees, void *data)
7516 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
7518 *walk_subtrees = 0;
7519 if (!wi->is_lhs)
7520 return NULL_TREE;
7522 tree op = *tp;
7525 if (handled_component_p (op))
7526 op = TREE_OPERAND (op, 0);
7527 else if ((TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
7528 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
7529 op = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
7530 else
7531 break;
7533 while (1);
7534 if (!DECL_P (op) || !omp_shared_to_firstprivate_optimizable_decl_p (op))
7535 return NULL_TREE;
7537 omp_mark_stores (gimplify_omp_ctxp, op);
7538 return NULL_TREE;
7541 /* Helper callback for walk_gimple_seq to discover possible stores
7542 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
7543 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
7544 for those. */
7546 static tree
7547 omp_find_stores_stmt (gimple_stmt_iterator *gsi_p,
7548 bool *handled_ops_p,
7549 struct walk_stmt_info *wi)
7551 gimple *stmt = gsi_stmt (*gsi_p);
7552 switch (gimple_code (stmt))
7554 /* Don't recurse on OpenMP constructs for which
7555 gimplify_adjust_omp_clauses already handled the bodies,
7556 except handle gimple_omp_for_pre_body. */
7557 case GIMPLE_OMP_FOR:
7558 *handled_ops_p = true;
7559 if (gimple_omp_for_pre_body (stmt))
7560 walk_gimple_seq (gimple_omp_for_pre_body (stmt),
7561 omp_find_stores_stmt, omp_find_stores_op, wi);
7562 break;
7563 case GIMPLE_OMP_PARALLEL:
7564 case GIMPLE_OMP_TASK:
7565 case GIMPLE_OMP_SECTIONS:
7566 case GIMPLE_OMP_SINGLE:
7567 case GIMPLE_OMP_TARGET:
7568 case GIMPLE_OMP_TEAMS:
7569 case GIMPLE_OMP_CRITICAL:
7570 *handled_ops_p = true;
7571 break;
7572 default:
7573 break;
7575 return NULL_TREE;
7578 struct gimplify_adjust_omp_clauses_data
7580 tree *list_p;
7581 gimple_seq *pre_p;
7584 /* For all variables that were not actually used within the context,
7585 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
7587 static int
7588 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
7590 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
7591 gimple_seq *pre_p
7592 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
7593 tree decl = (tree) n->key;
7594 unsigned flags = n->value;
7595 enum omp_clause_code code;
7596 tree clause;
7597 bool private_debug;
7599 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
7600 return 0;
7601 if ((flags & GOVD_SEEN) == 0)
7602 return 0;
7603 if (flags & GOVD_DEBUG_PRIVATE)
7605 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
7606 private_debug = true;
7608 else if (flags & GOVD_MAP)
7609 private_debug = false;
7610 else
7611 private_debug
7612 = lang_hooks.decls.omp_private_debug_clause (decl,
7613 !!(flags & GOVD_SHARED));
7614 if (private_debug)
7615 code = OMP_CLAUSE_PRIVATE;
7616 else if (flags & GOVD_MAP)
7617 code = OMP_CLAUSE_MAP;
7618 else if (flags & GOVD_SHARED)
7620 if (is_global_var (decl))
7622 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7623 while (ctx != NULL)
7625 splay_tree_node on
7626 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7627 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7628 | GOVD_PRIVATE | GOVD_REDUCTION
7629 | GOVD_LINEAR | GOVD_MAP)) != 0)
7630 break;
7631 ctx = ctx->outer_context;
7633 if (ctx == NULL)
7634 return 0;
7636 code = OMP_CLAUSE_SHARED;
7638 else if (flags & GOVD_PRIVATE)
7639 code = OMP_CLAUSE_PRIVATE;
7640 else if (flags & GOVD_FIRSTPRIVATE)
7641 code = OMP_CLAUSE_FIRSTPRIVATE;
7642 else if (flags & GOVD_LASTPRIVATE)
7643 code = OMP_CLAUSE_LASTPRIVATE;
7644 else if (flags & GOVD_ALIGNED)
7645 return 0;
7646 else
7647 gcc_unreachable ();
7649 if (((flags & GOVD_LASTPRIVATE)
7650 || (code == OMP_CLAUSE_SHARED && (flags & GOVD_WRITTEN)))
7651 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
7652 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
7654 clause = build_omp_clause (input_location, code);
7655 OMP_CLAUSE_DECL (clause) = decl;
7656 OMP_CLAUSE_CHAIN (clause) = *list_p;
7657 if (private_debug)
7658 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
7659 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
7660 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
7661 else if (code == OMP_CLAUSE_SHARED
7662 && (flags & GOVD_WRITTEN) == 0
7663 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
7664 OMP_CLAUSE_SHARED_READONLY (clause) = 1;
7665 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
7667 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
7668 OMP_CLAUSE_DECL (nc) = decl;
7669 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7670 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
7671 OMP_CLAUSE_DECL (clause)
7672 = build_simple_mem_ref_loc (input_location, decl);
7673 OMP_CLAUSE_DECL (clause)
7674 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
7675 build_int_cst (build_pointer_type (char_type_node), 0));
7676 OMP_CLAUSE_SIZE (clause) = size_zero_node;
7677 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7678 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
7679 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
7680 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7681 OMP_CLAUSE_CHAIN (nc) = *list_p;
7682 OMP_CLAUSE_CHAIN (clause) = nc;
7683 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7684 gimplify_omp_ctxp = ctx->outer_context;
7685 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
7686 pre_p, NULL, is_gimple_val, fb_rvalue);
7687 gimplify_omp_ctxp = ctx;
7689 else if (code == OMP_CLAUSE_MAP)
7691 int kind = (flags & GOVD_MAP_TO_ONLY
7692 ? GOMP_MAP_TO
7693 : GOMP_MAP_TOFROM);
7694 if (flags & GOVD_MAP_FORCE)
7695 kind |= GOMP_MAP_FLAG_FORCE;
7696 OMP_CLAUSE_SET_MAP_KIND (clause, kind);
7697 if (DECL_SIZE (decl)
7698 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7700 tree decl2 = DECL_VALUE_EXPR (decl);
7701 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7702 decl2 = TREE_OPERAND (decl2, 0);
7703 gcc_assert (DECL_P (decl2));
7704 tree mem = build_simple_mem_ref (decl2);
7705 OMP_CLAUSE_DECL (clause) = mem;
7706 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7707 if (gimplify_omp_ctxp->outer_context)
7709 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7710 omp_notice_variable (ctx, decl2, true);
7711 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
7713 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7714 OMP_CLAUSE_MAP);
7715 OMP_CLAUSE_DECL (nc) = decl;
7716 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7717 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
7718 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7719 else
7720 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7721 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7722 OMP_CLAUSE_CHAIN (clause) = nc;
7724 else if (gimplify_omp_ctxp->target_firstprivatize_array_bases
7725 && lang_hooks.decls.omp_privatize_by_reference (decl))
7727 OMP_CLAUSE_DECL (clause) = build_simple_mem_ref (decl);
7728 OMP_CLAUSE_SIZE (clause)
7729 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))));
7730 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7731 gimplify_omp_ctxp = ctx->outer_context;
7732 gimplify_expr (&OMP_CLAUSE_SIZE (clause),
7733 pre_p, NULL, is_gimple_val, fb_rvalue);
7734 gimplify_omp_ctxp = ctx;
7735 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7736 OMP_CLAUSE_MAP);
7737 OMP_CLAUSE_DECL (nc) = decl;
7738 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7739 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7740 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7741 OMP_CLAUSE_CHAIN (clause) = nc;
7743 else
7744 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
7746 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
7748 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
7749 OMP_CLAUSE_DECL (nc) = decl;
7750 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
7751 OMP_CLAUSE_CHAIN (nc) = *list_p;
7752 OMP_CLAUSE_CHAIN (clause) = nc;
7753 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7754 gimplify_omp_ctxp = ctx->outer_context;
7755 lang_hooks.decls.omp_finish_clause (nc, pre_p);
7756 gimplify_omp_ctxp = ctx;
7758 *list_p = clause;
7759 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7760 gimplify_omp_ctxp = ctx->outer_context;
7761 lang_hooks.decls.omp_finish_clause (clause, pre_p);
7762 gimplify_omp_ctxp = ctx;
7763 return 0;
7766 static void
7767 gimplify_adjust_omp_clauses (gimple_seq *pre_p, gimple_seq body, tree *list_p,
7768 enum tree_code code)
7770 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7771 tree c, decl;
7773 if (body)
7775 struct gimplify_omp_ctx *octx;
7776 for (octx = ctx; octx; octx = octx->outer_context)
7777 if ((octx->region_type & (ORT_PARALLEL | ORT_TASK | ORT_TEAMS)) != 0)
7778 break;
7779 if (octx)
7781 struct walk_stmt_info wi;
7782 memset (&wi, 0, sizeof (wi));
7783 walk_gimple_seq (body, omp_find_stores_stmt,
7784 omp_find_stores_op, &wi);
7787 while ((c = *list_p) != NULL)
7789 splay_tree_node n;
7790 bool remove = false;
7792 switch (OMP_CLAUSE_CODE (c))
7794 case OMP_CLAUSE_PRIVATE:
7795 case OMP_CLAUSE_SHARED:
7796 case OMP_CLAUSE_FIRSTPRIVATE:
7797 case OMP_CLAUSE_LINEAR:
7798 decl = OMP_CLAUSE_DECL (c);
7799 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7800 remove = !(n->value & GOVD_SEEN);
7801 if (! remove)
7803 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
7804 if ((n->value & GOVD_DEBUG_PRIVATE)
7805 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
7807 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
7808 || ((n->value & GOVD_DATA_SHARE_CLASS)
7809 == GOVD_PRIVATE));
7810 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
7811 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
7813 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
7814 && (n->value & GOVD_WRITTEN) == 0
7815 && DECL_P (decl)
7816 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
7817 OMP_CLAUSE_SHARED_READONLY (c) = 1;
7818 else if (DECL_P (decl)
7819 && ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
7820 && (n->value & GOVD_WRITTEN) != 1)
7821 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7822 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
7823 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
7824 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
7826 break;
7828 case OMP_CLAUSE_LASTPRIVATE:
7829 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
7830 accurately reflect the presence of a FIRSTPRIVATE clause. */
7831 decl = OMP_CLAUSE_DECL (c);
7832 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7833 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7834 = (n->value & GOVD_FIRSTPRIVATE) != 0;
7835 if (omp_no_lastprivate (ctx))
7837 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7838 remove = true;
7839 else
7840 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
7842 else if (code == OMP_DISTRIBUTE
7843 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7845 remove = true;
7846 error_at (OMP_CLAUSE_LOCATION (c),
7847 "same variable used in %<firstprivate%> and "
7848 "%<lastprivate%> clauses on %<distribute%> "
7849 "construct");
7851 if (!remove
7852 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7853 && DECL_P (decl)
7854 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
7855 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
7856 break;
7858 case OMP_CLAUSE_ALIGNED:
7859 decl = OMP_CLAUSE_DECL (c);
7860 if (!is_global_var (decl))
7862 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7863 remove = n == NULL || !(n->value & GOVD_SEEN);
7864 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7866 struct gimplify_omp_ctx *octx;
7867 if (n != NULL
7868 && (n->value & (GOVD_DATA_SHARE_CLASS
7869 & ~GOVD_FIRSTPRIVATE)))
7870 remove = true;
7871 else
7872 for (octx = ctx->outer_context; octx;
7873 octx = octx->outer_context)
7875 n = splay_tree_lookup (octx->variables,
7876 (splay_tree_key) decl);
7877 if (n == NULL)
7878 continue;
7879 if (n->value & GOVD_LOCAL)
7880 break;
7881 /* We have to avoid assigning a shared variable
7882 to itself when trying to add
7883 __builtin_assume_aligned. */
7884 if (n->value & GOVD_SHARED)
7886 remove = true;
7887 break;
7892 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7894 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7895 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7896 remove = true;
7898 break;
7900 case OMP_CLAUSE_MAP:
7901 if (code == OMP_TARGET_EXIT_DATA
7902 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER)
7904 remove = true;
7905 break;
7907 decl = OMP_CLAUSE_DECL (c);
7908 if (!DECL_P (decl))
7910 if ((ctx->region_type & ORT_TARGET) != 0
7911 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7913 if (TREE_CODE (decl) == INDIRECT_REF
7914 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7915 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7916 == REFERENCE_TYPE))
7917 decl = TREE_OPERAND (decl, 0);
7918 if (TREE_CODE (decl) == COMPONENT_REF)
7920 while (TREE_CODE (decl) == COMPONENT_REF)
7921 decl = TREE_OPERAND (decl, 0);
7922 if (DECL_P (decl))
7924 n = splay_tree_lookup (ctx->variables,
7925 (splay_tree_key) decl);
7926 if (!(n->value & GOVD_SEEN))
7927 remove = true;
7931 break;
7933 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7934 if ((ctx->region_type & ORT_TARGET) != 0
7935 && !(n->value & GOVD_SEEN)
7936 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0
7937 && !lookup_attribute ("omp declare target link",
7938 DECL_ATTRIBUTES (decl)))
7940 remove = true;
7941 /* For struct element mapping, if struct is never referenced
7942 in target block and none of the mapping has always modifier,
7943 remove all the struct element mappings, which immediately
7944 follow the GOMP_MAP_STRUCT map clause. */
7945 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
7947 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
7948 while (cnt--)
7949 OMP_CLAUSE_CHAIN (c)
7950 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
7953 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
7954 && code == OMP_TARGET_EXIT_DATA)
7955 remove = true;
7956 else if (DECL_SIZE (decl)
7957 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
7958 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
7959 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
7960 && (OMP_CLAUSE_MAP_KIND (c)
7961 != GOMP_MAP_FIRSTPRIVATE_REFERENCE))
7963 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
7964 for these, TREE_CODE (DECL_SIZE (decl)) will always be
7965 INTEGER_CST. */
7966 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
7968 tree decl2 = DECL_VALUE_EXPR (decl);
7969 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7970 decl2 = TREE_OPERAND (decl2, 0);
7971 gcc_assert (DECL_P (decl2));
7972 tree mem = build_simple_mem_ref (decl2);
7973 OMP_CLAUSE_DECL (c) = mem;
7974 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7975 if (ctx->outer_context)
7977 omp_notice_variable (ctx->outer_context, decl2, true);
7978 omp_notice_variable (ctx->outer_context,
7979 OMP_CLAUSE_SIZE (c), true);
7981 if (((ctx->region_type & ORT_TARGET) != 0
7982 || !ctx->target_firstprivatize_array_bases)
7983 && ((n->value & GOVD_SEEN) == 0
7984 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
7986 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7987 OMP_CLAUSE_MAP);
7988 OMP_CLAUSE_DECL (nc) = decl;
7989 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7990 if (ctx->target_firstprivatize_array_bases)
7991 OMP_CLAUSE_SET_MAP_KIND (nc,
7992 GOMP_MAP_FIRSTPRIVATE_POINTER);
7993 else
7994 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7995 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
7996 OMP_CLAUSE_CHAIN (c) = nc;
7997 c = nc;
8000 else
8002 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8003 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
8004 gcc_assert ((n->value & GOVD_SEEN) == 0
8005 || ((n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE))
8006 == 0));
8008 break;
8010 case OMP_CLAUSE_TO:
8011 case OMP_CLAUSE_FROM:
8012 case OMP_CLAUSE__CACHE_:
8013 decl = OMP_CLAUSE_DECL (c);
8014 if (!DECL_P (decl))
8015 break;
8016 if (DECL_SIZE (decl)
8017 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
8019 tree decl2 = DECL_VALUE_EXPR (decl);
8020 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
8021 decl2 = TREE_OPERAND (decl2, 0);
8022 gcc_assert (DECL_P (decl2));
8023 tree mem = build_simple_mem_ref (decl2);
8024 OMP_CLAUSE_DECL (c) = mem;
8025 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
8026 if (ctx->outer_context)
8028 omp_notice_variable (ctx->outer_context, decl2, true);
8029 omp_notice_variable (ctx->outer_context,
8030 OMP_CLAUSE_SIZE (c), true);
8033 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8034 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
8035 break;
8037 case OMP_CLAUSE_REDUCTION:
8038 decl = OMP_CLAUSE_DECL (c);
8039 if (DECL_P (decl)
8040 && omp_shared_to_firstprivate_optimizable_decl_p (decl))
8041 omp_mark_stores (gimplify_omp_ctxp->outer_context, decl);
8042 break;
8043 case OMP_CLAUSE_COPYIN:
8044 case OMP_CLAUSE_COPYPRIVATE:
8045 case OMP_CLAUSE_IF:
8046 case OMP_CLAUSE_NUM_THREADS:
8047 case OMP_CLAUSE_NUM_TEAMS:
8048 case OMP_CLAUSE_THREAD_LIMIT:
8049 case OMP_CLAUSE_DIST_SCHEDULE:
8050 case OMP_CLAUSE_DEVICE:
8051 case OMP_CLAUSE_SCHEDULE:
8052 case OMP_CLAUSE_NOWAIT:
8053 case OMP_CLAUSE_ORDERED:
8054 case OMP_CLAUSE_DEFAULT:
8055 case OMP_CLAUSE_UNTIED:
8056 case OMP_CLAUSE_COLLAPSE:
8057 case OMP_CLAUSE_FINAL:
8058 case OMP_CLAUSE_MERGEABLE:
8059 case OMP_CLAUSE_PROC_BIND:
8060 case OMP_CLAUSE_SAFELEN:
8061 case OMP_CLAUSE_SIMDLEN:
8062 case OMP_CLAUSE_DEPEND:
8063 case OMP_CLAUSE_PRIORITY:
8064 case OMP_CLAUSE_GRAINSIZE:
8065 case OMP_CLAUSE_NUM_TASKS:
8066 case OMP_CLAUSE_NOGROUP:
8067 case OMP_CLAUSE_THREADS:
8068 case OMP_CLAUSE_SIMD:
8069 case OMP_CLAUSE_HINT:
8070 case OMP_CLAUSE_DEFAULTMAP:
8071 case OMP_CLAUSE_USE_DEVICE_PTR:
8072 case OMP_CLAUSE_IS_DEVICE_PTR:
8073 case OMP_CLAUSE__CILK_FOR_COUNT_:
8074 case OMP_CLAUSE_ASYNC:
8075 case OMP_CLAUSE_WAIT:
8076 case OMP_CLAUSE_DEVICE_RESIDENT:
8077 case OMP_CLAUSE_INDEPENDENT:
8078 case OMP_CLAUSE_NUM_GANGS:
8079 case OMP_CLAUSE_NUM_WORKERS:
8080 case OMP_CLAUSE_VECTOR_LENGTH:
8081 case OMP_CLAUSE_GANG:
8082 case OMP_CLAUSE_WORKER:
8083 case OMP_CLAUSE_VECTOR:
8084 case OMP_CLAUSE_AUTO:
8085 case OMP_CLAUSE_SEQ:
8086 case OMP_CLAUSE_TILE:
8087 break;
8089 default:
8090 gcc_unreachable ();
8093 if (remove)
8094 *list_p = OMP_CLAUSE_CHAIN (c);
8095 else
8096 list_p = &OMP_CLAUSE_CHAIN (c);
8099 /* Add in any implicit data sharing. */
8100 struct gimplify_adjust_omp_clauses_data data;
8101 data.list_p = list_p;
8102 data.pre_p = pre_p;
8103 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
8105 gimplify_omp_ctxp = ctx->outer_context;
8106 delete_omp_context (ctx);
8109 /* Gimplify OACC_CACHE. */
8111 static void
8112 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
8114 tree expr = *expr_p;
8116 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_ACC,
8117 OACC_CACHE);
8118 gimplify_adjust_omp_clauses (pre_p, NULL, &OACC_CACHE_CLAUSES (expr),
8119 OACC_CACHE);
8121 /* TODO: Do something sensible with this information. */
8123 *expr_p = NULL_TREE;
8126 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
8127 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
8128 kind. The entry kind will replace the one in CLAUSE, while the exit
8129 kind will be used in a new omp_clause and returned to the caller. */
8131 static tree
8132 gimplify_oacc_declare_1 (tree clause)
8134 HOST_WIDE_INT kind, new_op;
8135 bool ret = false;
8136 tree c = NULL;
8138 kind = OMP_CLAUSE_MAP_KIND (clause);
8140 switch (kind)
8142 case GOMP_MAP_ALLOC:
8143 case GOMP_MAP_FORCE_ALLOC:
8144 case GOMP_MAP_FORCE_TO:
8145 new_op = GOMP_MAP_FORCE_DEALLOC;
8146 ret = true;
8147 break;
8149 case GOMP_MAP_FORCE_FROM:
8150 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
8151 new_op = GOMP_MAP_FORCE_FROM;
8152 ret = true;
8153 break;
8155 case GOMP_MAP_FORCE_TOFROM:
8156 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_TO);
8157 new_op = GOMP_MAP_FORCE_FROM;
8158 ret = true;
8159 break;
8161 case GOMP_MAP_FROM:
8162 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_FORCE_ALLOC);
8163 new_op = GOMP_MAP_FROM;
8164 ret = true;
8165 break;
8167 case GOMP_MAP_TOFROM:
8168 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_TO);
8169 new_op = GOMP_MAP_FROM;
8170 ret = true;
8171 break;
8173 case GOMP_MAP_DEVICE_RESIDENT:
8174 case GOMP_MAP_FORCE_DEVICEPTR:
8175 case GOMP_MAP_FORCE_PRESENT:
8176 case GOMP_MAP_LINK:
8177 case GOMP_MAP_POINTER:
8178 case GOMP_MAP_TO:
8179 break;
8181 default:
8182 gcc_unreachable ();
8183 break;
8186 if (ret)
8188 c = build_omp_clause (OMP_CLAUSE_LOCATION (clause), OMP_CLAUSE_MAP);
8189 OMP_CLAUSE_SET_MAP_KIND (c, new_op);
8190 OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clause);
8193 return c;
8196 /* Gimplify OACC_DECLARE. */
8198 static void
8199 gimplify_oacc_declare (tree *expr_p, gimple_seq *pre_p)
8201 tree expr = *expr_p;
8202 gomp_target *stmt;
8203 tree clauses, t;
8205 clauses = OACC_DECLARE_CLAUSES (expr);
8207 gimplify_scan_omp_clauses (&clauses, pre_p, ORT_TARGET_DATA, OACC_DECLARE);
8209 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
8211 tree decl = OMP_CLAUSE_DECL (t);
8213 if (TREE_CODE (decl) == MEM_REF)
8214 continue;
8216 if (TREE_CODE (decl) == VAR_DECL
8217 && !is_global_var (decl)
8218 && DECL_CONTEXT (decl) == current_function_decl)
8220 tree c = gimplify_oacc_declare_1 (t);
8221 if (c)
8223 if (oacc_declare_returns == NULL)
8224 oacc_declare_returns = new hash_map<tree, tree>;
8226 oacc_declare_returns->put (decl, c);
8230 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_SEEN);
8233 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_OACC_DECLARE,
8234 clauses);
8236 gimplify_seq_add_stmt (pre_p, stmt);
8238 *expr_p = NULL_TREE;
8241 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
8242 gimplification of the body, as well as scanning the body for used
8243 variables. We need to do this scan now, because variable-sized
8244 decls will be decomposed during gimplification. */
8246 static void
8247 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
8249 tree expr = *expr_p;
8250 gimple *g;
8251 gimple_seq body = NULL;
8253 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
8254 OMP_PARALLEL_COMBINED (expr)
8255 ? ORT_COMBINED_PARALLEL
8256 : ORT_PARALLEL, OMP_PARALLEL);
8258 push_gimplify_context ();
8260 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
8261 if (gimple_code (g) == GIMPLE_BIND)
8262 pop_gimplify_context (g);
8263 else
8264 pop_gimplify_context (NULL);
8266 gimplify_adjust_omp_clauses (pre_p, body, &OMP_PARALLEL_CLAUSES (expr),
8267 OMP_PARALLEL);
8269 g = gimple_build_omp_parallel (body,
8270 OMP_PARALLEL_CLAUSES (expr),
8271 NULL_TREE, NULL_TREE);
8272 if (OMP_PARALLEL_COMBINED (expr))
8273 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
8274 gimplify_seq_add_stmt (pre_p, g);
8275 *expr_p = NULL_TREE;
8278 /* Gimplify the contents of an OMP_TASK statement. This involves
8279 gimplification of the body, as well as scanning the body for used
8280 variables. We need to do this scan now, because variable-sized
8281 decls will be decomposed during gimplification. */
8283 static void
8284 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
8286 tree expr = *expr_p;
8287 gimple *g;
8288 gimple_seq body = NULL;
8290 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
8291 find_omp_clause (OMP_TASK_CLAUSES (expr),
8292 OMP_CLAUSE_UNTIED)
8293 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
8295 push_gimplify_context ();
8297 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
8298 if (gimple_code (g) == GIMPLE_BIND)
8299 pop_gimplify_context (g);
8300 else
8301 pop_gimplify_context (NULL);
8303 gimplify_adjust_omp_clauses (pre_p, body, &OMP_TASK_CLAUSES (expr),
8304 OMP_TASK);
8306 g = gimple_build_omp_task (body,
8307 OMP_TASK_CLAUSES (expr),
8308 NULL_TREE, NULL_TREE,
8309 NULL_TREE, NULL_TREE, NULL_TREE);
8310 gimplify_seq_add_stmt (pre_p, g);
8311 *expr_p = NULL_TREE;
8314 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
8315 with non-NULL OMP_FOR_INIT. */
8317 static tree
8318 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
8320 *walk_subtrees = 0;
8321 switch (TREE_CODE (*tp))
8323 case OMP_FOR:
8324 *walk_subtrees = 1;
8325 /* FALLTHRU */
8326 case OMP_SIMD:
8327 if (OMP_FOR_INIT (*tp) != NULL_TREE)
8328 return *tp;
8329 break;
8330 case BIND_EXPR:
8331 case STATEMENT_LIST:
8332 case OMP_PARALLEL:
8333 *walk_subtrees = 1;
8334 break;
8335 default:
8336 break;
8338 return NULL_TREE;
8341 /* Gimplify the gross structure of an OMP_FOR statement. */
8343 static enum gimplify_status
8344 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
8346 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
8347 enum gimplify_status ret = GS_ALL_DONE;
8348 enum gimplify_status tret;
8349 gomp_for *gfor;
8350 gimple_seq for_body, for_pre_body;
8351 int i;
8352 bitmap has_decl_expr = NULL;
8353 enum omp_region_type ort = ORT_WORKSHARE;
8355 orig_for_stmt = for_stmt = *expr_p;
8357 switch (TREE_CODE (for_stmt))
8359 case OMP_FOR:
8360 case CILK_FOR:
8361 case OMP_DISTRIBUTE:
8362 break;
8363 case OACC_LOOP:
8364 ort = ORT_ACC;
8365 break;
8366 case OMP_TASKLOOP:
8367 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
8368 ort = ORT_UNTIED_TASK;
8369 else
8370 ort = ORT_TASK;
8371 break;
8372 case OMP_SIMD:
8373 case CILK_SIMD:
8374 ort = ORT_SIMD;
8375 break;
8376 default:
8377 gcc_unreachable ();
8380 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
8381 clause for the IV. */
8382 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8384 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
8385 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8386 decl = TREE_OPERAND (t, 0);
8387 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
8388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8389 && OMP_CLAUSE_DECL (c) == decl)
8391 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
8392 break;
8396 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
8398 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
8399 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
8400 find_combined_omp_for, NULL, NULL);
8401 if (inner_for_stmt == NULL_TREE)
8403 gcc_assert (seen_error ());
8404 *expr_p = NULL_TREE;
8405 return GS_ERROR;
8409 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
8410 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
8411 TREE_CODE (for_stmt));
8413 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
8414 gimplify_omp_ctxp->distribute = true;
8416 /* Handle OMP_FOR_INIT. */
8417 for_pre_body = NULL;
8418 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
8420 has_decl_expr = BITMAP_ALLOC (NULL);
8421 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
8422 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
8423 == VAR_DECL)
8425 t = OMP_FOR_PRE_BODY (for_stmt);
8426 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
8428 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
8430 tree_stmt_iterator si;
8431 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
8432 tsi_next (&si))
8434 t = tsi_stmt (si);
8435 if (TREE_CODE (t) == DECL_EXPR
8436 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
8437 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
8441 if (OMP_FOR_PRE_BODY (for_stmt))
8443 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
8444 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
8445 else
8447 struct gimplify_omp_ctx ctx;
8448 memset (&ctx, 0, sizeof (ctx));
8449 ctx.region_type = ORT_NONE;
8450 gimplify_omp_ctxp = &ctx;
8451 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
8452 gimplify_omp_ctxp = NULL;
8455 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
8457 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
8458 for_stmt = inner_for_stmt;
8460 /* For taskloop, need to gimplify the start, end and step before the
8461 taskloop, outside of the taskloop omp context. */
8462 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8464 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8466 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8467 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
8469 TREE_OPERAND (t, 1)
8470 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
8471 pre_p, NULL);
8472 tree c = build_omp_clause (input_location,
8473 OMP_CLAUSE_FIRSTPRIVATE);
8474 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
8475 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
8476 OMP_FOR_CLAUSES (orig_for_stmt) = c;
8479 /* Handle OMP_FOR_COND. */
8480 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8481 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
8483 TREE_OPERAND (t, 1)
8484 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
8485 gimple_seq_empty_p (for_pre_body)
8486 ? pre_p : &for_pre_body, NULL);
8487 tree c = build_omp_clause (input_location,
8488 OMP_CLAUSE_FIRSTPRIVATE);
8489 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
8490 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
8491 OMP_FOR_CLAUSES (orig_for_stmt) = c;
8494 /* Handle OMP_FOR_INCR. */
8495 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8496 if (TREE_CODE (t) == MODIFY_EXPR)
8498 decl = TREE_OPERAND (t, 0);
8499 t = TREE_OPERAND (t, 1);
8500 tree *tp = &TREE_OPERAND (t, 1);
8501 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
8502 tp = &TREE_OPERAND (t, 0);
8504 if (!is_gimple_constant (*tp))
8506 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
8507 ? pre_p : &for_pre_body;
8508 *tp = get_initialized_tmp_var (*tp, seq, NULL);
8509 tree c = build_omp_clause (input_location,
8510 OMP_CLAUSE_FIRSTPRIVATE);
8511 OMP_CLAUSE_DECL (c) = *tp;
8512 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
8513 OMP_FOR_CLAUSES (orig_for_stmt) = c;
8518 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
8519 OMP_TASKLOOP);
8522 if (orig_for_stmt != for_stmt)
8523 gimplify_omp_ctxp->combined_loop = true;
8525 for_body = NULL;
8526 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
8527 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
8528 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
8529 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
8531 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
8532 bool is_doacross = false;
8533 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
8535 is_doacross = true;
8536 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
8537 (OMP_FOR_INIT (for_stmt))
8538 * 2);
8540 int collapse = 1;
8541 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
8542 if (c)
8543 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8544 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8546 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8547 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8548 decl = TREE_OPERAND (t, 0);
8549 gcc_assert (DECL_P (decl));
8550 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
8551 || POINTER_TYPE_P (TREE_TYPE (decl)));
8552 if (is_doacross)
8554 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
8555 gimplify_omp_ctxp->loop_iter_var.quick_push
8556 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
8557 else
8558 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
8559 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
8562 /* Make sure the iteration variable is private. */
8563 tree c = NULL_TREE;
8564 tree c2 = NULL_TREE;
8565 if (orig_for_stmt != for_stmt)
8566 /* Do this only on innermost construct for combined ones. */;
8567 else if (ort == ORT_SIMD)
8569 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
8570 (splay_tree_key) decl);
8571 omp_is_private (gimplify_omp_ctxp, decl,
8572 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
8573 != 1));
8574 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
8575 omp_notice_variable (gimplify_omp_ctxp, decl, true);
8576 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8578 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8579 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
8580 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
8581 if ((has_decl_expr
8582 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
8583 || omp_no_lastprivate (gimplify_omp_ctxp))
8585 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8586 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8588 struct gimplify_omp_ctx *outer
8589 = gimplify_omp_ctxp->outer_context;
8590 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8592 if (outer->region_type == ORT_WORKSHARE
8593 && outer->combined_loop)
8595 n = splay_tree_lookup (outer->variables,
8596 (splay_tree_key)decl);
8597 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8599 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8600 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8602 else
8604 struct gimplify_omp_ctx *octx = outer->outer_context;
8605 if (octx
8606 && octx->region_type == ORT_COMBINED_PARALLEL
8607 && octx->outer_context
8608 && (octx->outer_context->region_type
8609 == ORT_WORKSHARE)
8610 && octx->outer_context->combined_loop)
8612 octx = octx->outer_context;
8613 n = splay_tree_lookup (octx->variables,
8614 (splay_tree_key)decl);
8615 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8617 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
8618 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
8625 OMP_CLAUSE_DECL (c) = decl;
8626 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
8627 OMP_FOR_CLAUSES (for_stmt) = c;
8628 omp_add_variable (gimplify_omp_ctxp, decl, flags);
8629 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
8631 if (outer->region_type == ORT_WORKSHARE
8632 && outer->combined_loop)
8634 if (outer->outer_context
8635 && (outer->outer_context->region_type
8636 == ORT_COMBINED_PARALLEL))
8637 outer = outer->outer_context;
8638 else if (omp_check_private (outer, decl, false))
8639 outer = NULL;
8641 else if (((outer->region_type & ORT_TASK) != 0)
8642 && outer->combined_loop
8643 && !omp_check_private (gimplify_omp_ctxp,
8644 decl, false))
8646 else if (outer->region_type != ORT_COMBINED_PARALLEL)
8647 outer = NULL;
8648 if (outer)
8650 n = splay_tree_lookup (outer->variables,
8651 (splay_tree_key)decl);
8652 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8654 omp_add_variable (outer, decl,
8655 GOVD_LASTPRIVATE | GOVD_SEEN);
8656 if (outer->region_type == ORT_COMBINED_PARALLEL
8657 && outer->outer_context
8658 && (outer->outer_context->region_type
8659 == ORT_WORKSHARE)
8660 && outer->outer_context->combined_loop)
8662 outer = outer->outer_context;
8663 n = splay_tree_lookup (outer->variables,
8664 (splay_tree_key)decl);
8665 if (omp_check_private (outer, decl, false))
8666 outer = NULL;
8667 else if (n == NULL
8668 || ((n->value & GOVD_DATA_SHARE_CLASS)
8669 == 0))
8670 omp_add_variable (outer, decl,
8671 GOVD_LASTPRIVATE
8672 | GOVD_SEEN);
8673 else
8674 outer = NULL;
8676 if (outer && outer->outer_context
8677 && (outer->outer_context->region_type
8678 == ORT_COMBINED_TEAMS))
8680 outer = outer->outer_context;
8681 n = splay_tree_lookup (outer->variables,
8682 (splay_tree_key)decl);
8683 if (n == NULL
8684 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8685 omp_add_variable (outer, decl,
8686 GOVD_SHARED | GOVD_SEEN);
8687 else
8688 outer = NULL;
8690 if (outer && outer->outer_context)
8691 omp_notice_variable (outer->outer_context, decl,
8692 true);
8697 else
8699 bool lastprivate
8700 = (!has_decl_expr
8701 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
8702 && !omp_no_lastprivate (gimplify_omp_ctxp);
8703 struct gimplify_omp_ctx *outer
8704 = gimplify_omp_ctxp->outer_context;
8705 if (outer && lastprivate)
8707 if (outer->region_type == ORT_WORKSHARE
8708 && outer->combined_loop)
8710 n = splay_tree_lookup (outer->variables,
8711 (splay_tree_key)decl);
8712 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
8714 lastprivate = false;
8715 outer = NULL;
8717 else if (outer->outer_context
8718 && (outer->outer_context->region_type
8719 == ORT_COMBINED_PARALLEL))
8720 outer = outer->outer_context;
8721 else if (omp_check_private (outer, decl, false))
8722 outer = NULL;
8724 else if (((outer->region_type & ORT_TASK) != 0)
8725 && outer->combined_loop
8726 && !omp_check_private (gimplify_omp_ctxp,
8727 decl, false))
8729 else if (outer->region_type != ORT_COMBINED_PARALLEL)
8730 outer = NULL;
8731 if (outer)
8733 n = splay_tree_lookup (outer->variables,
8734 (splay_tree_key)decl);
8735 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8737 omp_add_variable (outer, decl,
8738 GOVD_LASTPRIVATE | GOVD_SEEN);
8739 if (outer->region_type == ORT_COMBINED_PARALLEL
8740 && outer->outer_context
8741 && (outer->outer_context->region_type
8742 == ORT_WORKSHARE)
8743 && outer->outer_context->combined_loop)
8745 outer = outer->outer_context;
8746 n = splay_tree_lookup (outer->variables,
8747 (splay_tree_key)decl);
8748 if (omp_check_private (outer, decl, false))
8749 outer = NULL;
8750 else if (n == NULL
8751 || ((n->value & GOVD_DATA_SHARE_CLASS)
8752 == 0))
8753 omp_add_variable (outer, decl,
8754 GOVD_LASTPRIVATE
8755 | GOVD_SEEN);
8756 else
8757 outer = NULL;
8759 if (outer && outer->outer_context
8760 && (outer->outer_context->region_type
8761 == ORT_COMBINED_TEAMS))
8763 outer = outer->outer_context;
8764 n = splay_tree_lookup (outer->variables,
8765 (splay_tree_key)decl);
8766 if (n == NULL
8767 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
8768 omp_add_variable (outer, decl,
8769 GOVD_SHARED | GOVD_SEEN);
8770 else
8771 outer = NULL;
8773 if (outer && outer->outer_context)
8774 omp_notice_variable (outer->outer_context, decl,
8775 true);
8780 c = build_omp_clause (input_location,
8781 lastprivate ? OMP_CLAUSE_LASTPRIVATE
8782 : OMP_CLAUSE_PRIVATE);
8783 OMP_CLAUSE_DECL (c) = decl;
8784 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
8785 OMP_FOR_CLAUSES (for_stmt) = c;
8786 omp_add_variable (gimplify_omp_ctxp, decl,
8787 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
8788 | GOVD_EXPLICIT | GOVD_SEEN);
8789 c = NULL_TREE;
8792 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
8793 omp_notice_variable (gimplify_omp_ctxp, decl, true);
8794 else
8795 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
8797 /* If DECL is not a gimple register, create a temporary variable to act
8798 as an iteration counter. This is valid, since DECL cannot be
8799 modified in the body of the loop. Similarly for any iteration vars
8800 in simd with collapse > 1 where the iterator vars must be
8801 lastprivate. */
8802 if (orig_for_stmt != for_stmt)
8803 var = decl;
8804 else if (!is_gimple_reg (decl)
8805 || (ort == ORT_SIMD
8806 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
8808 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8809 TREE_OPERAND (t, 0) = var;
8811 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
8813 if (ort == ORT_SIMD
8814 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8816 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8817 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
8818 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
8819 OMP_CLAUSE_DECL (c2) = var;
8820 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
8821 OMP_FOR_CLAUSES (for_stmt) = c2;
8822 omp_add_variable (gimplify_omp_ctxp, var,
8823 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
8824 if (c == NULL_TREE)
8826 c = c2;
8827 c2 = NULL_TREE;
8830 else
8831 omp_add_variable (gimplify_omp_ctxp, var,
8832 GOVD_PRIVATE | GOVD_SEEN);
8834 else
8835 var = decl;
8837 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8838 is_gimple_val, fb_rvalue);
8839 ret = MIN (ret, tret);
8840 if (ret == GS_ERROR)
8841 return ret;
8843 /* Handle OMP_FOR_COND. */
8844 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8845 gcc_assert (COMPARISON_CLASS_P (t));
8846 gcc_assert (TREE_OPERAND (t, 0) == decl);
8848 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8849 is_gimple_val, fb_rvalue);
8850 ret = MIN (ret, tret);
8852 /* Handle OMP_FOR_INCR. */
8853 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8854 switch (TREE_CODE (t))
8856 case PREINCREMENT_EXPR:
8857 case POSTINCREMENT_EXPR:
8859 tree decl = TREE_OPERAND (t, 0);
8860 /* c_omp_for_incr_canonicalize_ptr() should have been
8861 called to massage things appropriately. */
8862 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8864 if (orig_for_stmt != for_stmt)
8865 break;
8866 t = build_int_cst (TREE_TYPE (decl), 1);
8867 if (c)
8868 OMP_CLAUSE_LINEAR_STEP (c) = t;
8869 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8870 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8871 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8872 break;
8875 case PREDECREMENT_EXPR:
8876 case POSTDECREMENT_EXPR:
8877 /* c_omp_for_incr_canonicalize_ptr() should have been
8878 called to massage things appropriately. */
8879 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8880 if (orig_for_stmt != for_stmt)
8881 break;
8882 t = build_int_cst (TREE_TYPE (decl), -1);
8883 if (c)
8884 OMP_CLAUSE_LINEAR_STEP (c) = t;
8885 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8886 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8887 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8888 break;
8890 case MODIFY_EXPR:
8891 gcc_assert (TREE_OPERAND (t, 0) == decl);
8892 TREE_OPERAND (t, 0) = var;
8894 t = TREE_OPERAND (t, 1);
8895 switch (TREE_CODE (t))
8897 case PLUS_EXPR:
8898 if (TREE_OPERAND (t, 1) == decl)
8900 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
8901 TREE_OPERAND (t, 0) = var;
8902 break;
8905 /* Fallthru. */
8906 case MINUS_EXPR:
8907 case POINTER_PLUS_EXPR:
8908 gcc_assert (TREE_OPERAND (t, 0) == decl);
8909 TREE_OPERAND (t, 0) = var;
8910 break;
8911 default:
8912 gcc_unreachable ();
8915 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8916 is_gimple_val, fb_rvalue);
8917 ret = MIN (ret, tret);
8918 if (c)
8920 tree step = TREE_OPERAND (t, 1);
8921 tree stept = TREE_TYPE (decl);
8922 if (POINTER_TYPE_P (stept))
8923 stept = sizetype;
8924 step = fold_convert (stept, step);
8925 if (TREE_CODE (t) == MINUS_EXPR)
8926 step = fold_build1 (NEGATE_EXPR, stept, step);
8927 OMP_CLAUSE_LINEAR_STEP (c) = step;
8928 if (step != TREE_OPERAND (t, 1))
8930 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
8931 &for_pre_body, NULL,
8932 is_gimple_val, fb_rvalue);
8933 ret = MIN (ret, tret);
8936 break;
8938 default:
8939 gcc_unreachable ();
8942 if (c2)
8944 gcc_assert (c);
8945 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
8948 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
8950 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
8951 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8952 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
8953 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8954 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
8955 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
8956 && OMP_CLAUSE_DECL (c) == decl)
8958 if (is_doacross && (collapse == 1 || i >= collapse))
8959 t = var;
8960 else
8962 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8963 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8964 gcc_assert (TREE_OPERAND (t, 0) == var);
8965 t = TREE_OPERAND (t, 1);
8966 gcc_assert (TREE_CODE (t) == PLUS_EXPR
8967 || TREE_CODE (t) == MINUS_EXPR
8968 || TREE_CODE (t) == POINTER_PLUS_EXPR);
8969 gcc_assert (TREE_OPERAND (t, 0) == var);
8970 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
8971 is_doacross ? var : decl,
8972 TREE_OPERAND (t, 1));
8974 gimple_seq *seq;
8975 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8976 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
8977 else
8978 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
8979 gimplify_assign (decl, t, seq);
8984 BITMAP_FREE (has_decl_expr);
8986 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8988 push_gimplify_context ();
8989 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
8991 OMP_FOR_BODY (orig_for_stmt)
8992 = build3 (BIND_EXPR, void_type_node, NULL,
8993 OMP_FOR_BODY (orig_for_stmt), NULL);
8994 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
8998 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
8999 &for_body);
9001 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9003 if (gimple_code (g) == GIMPLE_BIND)
9004 pop_gimplify_context (g);
9005 else
9006 pop_gimplify_context (NULL);
9009 if (orig_for_stmt != for_stmt)
9010 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9012 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9013 decl = TREE_OPERAND (t, 0);
9014 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
9015 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9016 gimplify_omp_ctxp = ctx->outer_context;
9017 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
9018 gimplify_omp_ctxp = ctx;
9019 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
9020 TREE_OPERAND (t, 0) = var;
9021 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9022 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
9023 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
9026 gimplify_adjust_omp_clauses (pre_p, for_body,
9027 &OMP_FOR_CLAUSES (orig_for_stmt),
9028 TREE_CODE (orig_for_stmt));
9030 int kind;
9031 switch (TREE_CODE (orig_for_stmt))
9033 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
9034 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
9035 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
9036 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
9037 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
9038 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
9039 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
9040 default:
9041 gcc_unreachable ();
9043 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
9044 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
9045 for_pre_body);
9046 if (orig_for_stmt != for_stmt)
9047 gimple_omp_for_set_combined_p (gfor, true);
9048 if (gimplify_omp_ctxp
9049 && (gimplify_omp_ctxp->combined_loop
9050 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
9051 && gimplify_omp_ctxp->outer_context
9052 && gimplify_omp_ctxp->outer_context->combined_loop)))
9054 gimple_omp_for_set_combined_into_p (gfor, true);
9055 if (gimplify_omp_ctxp->combined_loop)
9056 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
9057 else
9058 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
9061 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
9063 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
9064 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
9065 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
9066 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
9067 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
9068 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
9069 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
9070 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
9073 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
9074 constructs with GIMPLE_OMP_TASK sandwiched in between them.
9075 The outer taskloop stands for computing the number of iterations,
9076 counts for collapsed loops and holding taskloop specific clauses.
9077 The task construct stands for the effect of data sharing on the
9078 explicit task it creates and the inner taskloop stands for expansion
9079 of the static loop inside of the explicit task construct. */
9080 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
9082 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
9083 tree task_clauses = NULL_TREE;
9084 tree c = *gfor_clauses_ptr;
9085 tree *gtask_clauses_ptr = &task_clauses;
9086 tree outer_for_clauses = NULL_TREE;
9087 tree *gforo_clauses_ptr = &outer_for_clauses;
9088 for (; c; c = OMP_CLAUSE_CHAIN (c))
9089 switch (OMP_CLAUSE_CODE (c))
9091 /* These clauses are allowed on task, move them there. */
9092 case OMP_CLAUSE_SHARED:
9093 case OMP_CLAUSE_FIRSTPRIVATE:
9094 case OMP_CLAUSE_DEFAULT:
9095 case OMP_CLAUSE_IF:
9096 case OMP_CLAUSE_UNTIED:
9097 case OMP_CLAUSE_FINAL:
9098 case OMP_CLAUSE_MERGEABLE:
9099 case OMP_CLAUSE_PRIORITY:
9100 *gtask_clauses_ptr = c;
9101 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9102 break;
9103 case OMP_CLAUSE_PRIVATE:
9104 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
9106 /* We want private on outer for and firstprivate
9107 on task. */
9108 *gtask_clauses_ptr
9109 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9110 OMP_CLAUSE_FIRSTPRIVATE);
9111 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
9112 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
9113 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
9114 *gforo_clauses_ptr = c;
9115 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9117 else
9119 *gtask_clauses_ptr = c;
9120 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9122 break;
9123 /* These clauses go into outer taskloop clauses. */
9124 case OMP_CLAUSE_GRAINSIZE:
9125 case OMP_CLAUSE_NUM_TASKS:
9126 case OMP_CLAUSE_NOGROUP:
9127 *gforo_clauses_ptr = c;
9128 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9129 break;
9130 /* Taskloop clause we duplicate on both taskloops. */
9131 case OMP_CLAUSE_COLLAPSE:
9132 *gfor_clauses_ptr = c;
9133 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9134 *gforo_clauses_ptr = copy_node (c);
9135 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
9136 break;
9137 /* For lastprivate, keep the clause on inner taskloop, and add
9138 a shared clause on task. If the same decl is also firstprivate,
9139 add also firstprivate clause on the inner taskloop. */
9140 case OMP_CLAUSE_LASTPRIVATE:
9141 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
9143 /* For taskloop C++ lastprivate IVs, we want:
9144 1) private on outer taskloop
9145 2) firstprivate and shared on task
9146 3) lastprivate on inner taskloop */
9147 *gtask_clauses_ptr
9148 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9149 OMP_CLAUSE_FIRSTPRIVATE);
9150 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
9151 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
9152 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
9153 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
9154 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
9155 OMP_CLAUSE_PRIVATE);
9156 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
9157 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
9158 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
9159 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
9161 *gfor_clauses_ptr = c;
9162 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
9163 *gtask_clauses_ptr
9164 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
9165 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
9166 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
9167 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
9168 gtask_clauses_ptr
9169 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
9170 break;
9171 default:
9172 gcc_unreachable ();
9174 *gfor_clauses_ptr = NULL_TREE;
9175 *gtask_clauses_ptr = NULL_TREE;
9176 *gforo_clauses_ptr = NULL_TREE;
9177 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
9178 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
9179 NULL_TREE, NULL_TREE, NULL_TREE);
9180 gimple_omp_task_set_taskloop_p (g, true);
9181 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
9182 gomp_for *gforo
9183 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
9184 gimple_omp_for_collapse (gfor),
9185 gimple_omp_for_pre_body (gfor));
9186 gimple_omp_for_set_pre_body (gfor, NULL);
9187 gimple_omp_for_set_combined_p (gforo, true);
9188 gimple_omp_for_set_combined_into_p (gfor, true);
9189 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
9191 t = unshare_expr (gimple_omp_for_index (gfor, i));
9192 gimple_omp_for_set_index (gforo, i, t);
9193 t = unshare_expr (gimple_omp_for_initial (gfor, i));
9194 gimple_omp_for_set_initial (gforo, i, t);
9195 gimple_omp_for_set_cond (gforo, i,
9196 gimple_omp_for_cond (gfor, i));
9197 t = unshare_expr (gimple_omp_for_final (gfor, i));
9198 gimple_omp_for_set_final (gforo, i, t);
9199 t = unshare_expr (gimple_omp_for_incr (gfor, i));
9200 gimple_omp_for_set_incr (gforo, i, t);
9202 gimplify_seq_add_stmt (pre_p, gforo);
9204 else
9205 gimplify_seq_add_stmt (pre_p, gfor);
9206 if (ret != GS_ALL_DONE)
9207 return GS_ERROR;
9208 *expr_p = NULL_TREE;
9209 return GS_ALL_DONE;
9212 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
9213 of OMP_TARGET's body. */
9215 static tree
9216 find_omp_teams (tree *tp, int *walk_subtrees, void *)
9218 *walk_subtrees = 0;
9219 switch (TREE_CODE (*tp))
9221 case OMP_TEAMS:
9222 return *tp;
9223 case BIND_EXPR:
9224 case STATEMENT_LIST:
9225 *walk_subtrees = 1;
9226 break;
9227 default:
9228 break;
9230 return NULL_TREE;
9233 /* Helper function of optimize_target_teams, determine if the expression
9234 can be computed safely before the target construct on the host. */
9236 static tree
9237 computable_teams_clause (tree *tp, int *walk_subtrees, void *)
9239 splay_tree_node n;
9241 if (TYPE_P (*tp))
9243 *walk_subtrees = 0;
9244 return NULL_TREE;
9246 switch (TREE_CODE (*tp))
9248 case VAR_DECL:
9249 case PARM_DECL:
9250 case RESULT_DECL:
9251 *walk_subtrees = 0;
9252 if (error_operand_p (*tp)
9253 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp))
9254 || DECL_HAS_VALUE_EXPR_P (*tp)
9255 || DECL_THREAD_LOCAL_P (*tp)
9256 || TREE_SIDE_EFFECTS (*tp)
9257 || TREE_THIS_VOLATILE (*tp))
9258 return *tp;
9259 if (is_global_var (*tp)
9260 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp))
9261 || lookup_attribute ("omp declare target link",
9262 DECL_ATTRIBUTES (*tp))))
9263 return *tp;
9264 n = splay_tree_lookup (gimplify_omp_ctxp->variables,
9265 (splay_tree_key) *tp);
9266 if (n == NULL)
9268 if (gimplify_omp_ctxp->target_map_scalars_firstprivate)
9269 return NULL_TREE;
9270 return *tp;
9272 else if (n->value & GOVD_LOCAL)
9273 return *tp;
9274 else if (n->value & GOVD_FIRSTPRIVATE)
9275 return NULL_TREE;
9276 else if ((n->value & (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
9277 == (GOVD_MAP | GOVD_MAP_ALWAYS_TO))
9278 return NULL_TREE;
9279 return *tp;
9280 case INTEGER_CST:
9281 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
9282 return *tp;
9283 return NULL_TREE;
9284 case TARGET_EXPR:
9285 if (TARGET_EXPR_INITIAL (*tp)
9286 || TREE_CODE (TARGET_EXPR_SLOT (*tp)) != VAR_DECL)
9287 return *tp;
9288 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp),
9289 walk_subtrees, NULL);
9290 /* Allow some reasonable subset of integral arithmetics. */
9291 case PLUS_EXPR:
9292 case MINUS_EXPR:
9293 case MULT_EXPR:
9294 case TRUNC_DIV_EXPR:
9295 case CEIL_DIV_EXPR:
9296 case FLOOR_DIV_EXPR:
9297 case ROUND_DIV_EXPR:
9298 case TRUNC_MOD_EXPR:
9299 case CEIL_MOD_EXPR:
9300 case FLOOR_MOD_EXPR:
9301 case ROUND_MOD_EXPR:
9302 case RDIV_EXPR:
9303 case EXACT_DIV_EXPR:
9304 case MIN_EXPR:
9305 case MAX_EXPR:
9306 case LSHIFT_EXPR:
9307 case RSHIFT_EXPR:
9308 case BIT_IOR_EXPR:
9309 case BIT_XOR_EXPR:
9310 case BIT_AND_EXPR:
9311 case NEGATE_EXPR:
9312 case ABS_EXPR:
9313 case BIT_NOT_EXPR:
9314 case NON_LVALUE_EXPR:
9315 CASE_CONVERT:
9316 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp)))
9317 return *tp;
9318 return NULL_TREE;
9319 /* And disallow anything else, except for comparisons. */
9320 default:
9321 if (COMPARISON_CLASS_P (*tp))
9322 return NULL_TREE;
9323 return *tp;
9327 /* Try to determine if the num_teams and/or thread_limit expressions
9328 can have their values determined already before entering the
9329 target construct.
9330 INTEGER_CSTs trivially are,
9331 integral decls that are firstprivate (explicitly or implicitly)
9332 or explicitly map(always, to:) or map(always, tofrom:) on the target
9333 region too, and expressions involving simple arithmetics on those
9334 too, function calls are not ok, dereferencing something neither etc.
9335 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
9336 EXPR based on what we find:
9337 0 stands for clause not specified at all, use implementation default
9338 -1 stands for value that can't be determined easily before entering
9339 the target construct.
9340 If teams construct is not present at all, use 1 for num_teams
9341 and 0 for thread_limit (only one team is involved, and the thread
9342 limit is implementation defined. */
9344 static void
9345 optimize_target_teams (tree target, gimple_seq *pre_p)
9347 tree body = OMP_BODY (target);
9348 tree teams = walk_tree (&body, find_omp_teams, NULL, NULL);
9349 tree num_teams = integer_zero_node;
9350 tree thread_limit = integer_zero_node;
9351 location_t num_teams_loc = EXPR_LOCATION (target);
9352 location_t thread_limit_loc = EXPR_LOCATION (target);
9353 tree c, *p, expr;
9354 struct gimplify_omp_ctx *target_ctx = gimplify_omp_ctxp;
9356 if (teams == NULL_TREE)
9357 num_teams = integer_one_node;
9358 else
9359 for (c = OMP_TEAMS_CLAUSES (teams); c; c = OMP_CLAUSE_CHAIN (c))
9361 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS)
9363 p = &num_teams;
9364 num_teams_loc = OMP_CLAUSE_LOCATION (c);
9366 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
9368 p = &thread_limit;
9369 thread_limit_loc = OMP_CLAUSE_LOCATION (c);
9371 else
9372 continue;
9373 expr = OMP_CLAUSE_OPERAND (c, 0);
9374 if (TREE_CODE (expr) == INTEGER_CST)
9376 *p = expr;
9377 continue;
9379 if (walk_tree (&expr, computable_teams_clause, NULL, NULL))
9381 *p = integer_minus_one_node;
9382 continue;
9384 *p = expr;
9385 gimplify_omp_ctxp = gimplify_omp_ctxp->outer_context;
9386 if (gimplify_expr (p, pre_p, NULL, is_gimple_val, fb_rvalue)
9387 == GS_ERROR)
9389 gimplify_omp_ctxp = target_ctx;
9390 *p = integer_minus_one_node;
9391 continue;
9393 gimplify_omp_ctxp = target_ctx;
9394 if (!DECL_P (expr) && TREE_CODE (expr) != TARGET_EXPR)
9395 OMP_CLAUSE_OPERAND (c, 0) = *p;
9397 c = build_omp_clause (thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
9398 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = thread_limit;
9399 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
9400 OMP_TARGET_CLAUSES (target) = c;
9401 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
9402 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = num_teams;
9403 OMP_CLAUSE_CHAIN (c) = OMP_TARGET_CLAUSES (target);
9404 OMP_TARGET_CLAUSES (target) = c;
9407 /* Gimplify the gross structure of several OMP constructs. */
9409 static void
9410 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
9412 tree expr = *expr_p;
9413 gimple *stmt;
9414 gimple_seq body = NULL;
9415 enum omp_region_type ort;
9417 switch (TREE_CODE (expr))
9419 case OMP_SECTIONS:
9420 case OMP_SINGLE:
9421 ort = ORT_WORKSHARE;
9422 break;
9423 case OMP_TARGET:
9424 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
9425 break;
9426 case OACC_KERNELS:
9427 ort = ORT_ACC_KERNELS;
9428 break;
9429 case OACC_PARALLEL:
9430 ort = ORT_ACC_PARALLEL;
9431 break;
9432 case OACC_DATA:
9433 ort = ORT_ACC_DATA;
9434 break;
9435 case OMP_TARGET_DATA:
9436 ort = ORT_TARGET_DATA;
9437 break;
9438 case OMP_TEAMS:
9439 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
9440 break;
9441 case OACC_HOST_DATA:
9442 ort = ORT_ACC_HOST_DATA;
9443 break;
9444 default:
9445 gcc_unreachable ();
9447 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
9448 TREE_CODE (expr));
9449 if (TREE_CODE (expr) == OMP_TARGET)
9450 optimize_target_teams (expr, pre_p);
9451 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
9453 push_gimplify_context ();
9454 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
9455 if (gimple_code (g) == GIMPLE_BIND)
9456 pop_gimplify_context (g);
9457 else
9458 pop_gimplify_context (NULL);
9459 if ((ort & ORT_TARGET_DATA) != 0)
9461 enum built_in_function end_ix;
9462 switch (TREE_CODE (expr))
9464 case OACC_DATA:
9465 case OACC_HOST_DATA:
9466 end_ix = BUILT_IN_GOACC_DATA_END;
9467 break;
9468 case OMP_TARGET_DATA:
9469 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
9470 break;
9471 default:
9472 gcc_unreachable ();
9474 tree fn = builtin_decl_explicit (end_ix);
9475 g = gimple_build_call (fn, 0);
9476 gimple_seq cleanup = NULL;
9477 gimple_seq_add_stmt (&cleanup, g);
9478 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
9479 body = NULL;
9480 gimple_seq_add_stmt (&body, g);
9483 else
9484 gimplify_and_add (OMP_BODY (expr), &body);
9485 gimplify_adjust_omp_clauses (pre_p, body, &OMP_CLAUSES (expr),
9486 TREE_CODE (expr));
9488 switch (TREE_CODE (expr))
9490 case OACC_DATA:
9491 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
9492 OMP_CLAUSES (expr));
9493 break;
9494 case OACC_KERNELS:
9495 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
9496 OMP_CLAUSES (expr));
9497 break;
9498 case OACC_HOST_DATA:
9499 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_HOST_DATA,
9500 OMP_CLAUSES (expr));
9501 break;
9502 case OACC_PARALLEL:
9503 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
9504 OMP_CLAUSES (expr));
9505 break;
9506 case OMP_SECTIONS:
9507 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
9508 break;
9509 case OMP_SINGLE:
9510 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
9511 break;
9512 case OMP_TARGET:
9513 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
9514 OMP_CLAUSES (expr));
9515 break;
9516 case OMP_TARGET_DATA:
9517 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
9518 OMP_CLAUSES (expr));
9519 break;
9520 case OMP_TEAMS:
9521 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
9522 break;
9523 default:
9524 gcc_unreachable ();
9527 gimplify_seq_add_stmt (pre_p, stmt);
9528 *expr_p = NULL_TREE;
9531 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
9532 target update constructs. */
9534 static void
9535 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
9537 tree expr = *expr_p;
9538 int kind;
9539 gomp_target *stmt;
9540 enum omp_region_type ort = ORT_WORKSHARE;
9542 switch (TREE_CODE (expr))
9544 case OACC_ENTER_DATA:
9545 case OACC_EXIT_DATA:
9546 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
9547 ort = ORT_ACC;
9548 break;
9549 case OACC_UPDATE:
9550 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
9551 ort = ORT_ACC;
9552 break;
9553 case OMP_TARGET_UPDATE:
9554 kind = GF_OMP_TARGET_KIND_UPDATE;
9555 break;
9556 case OMP_TARGET_ENTER_DATA:
9557 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
9558 break;
9559 case OMP_TARGET_EXIT_DATA:
9560 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
9561 break;
9562 default:
9563 gcc_unreachable ();
9565 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
9566 ort, TREE_CODE (expr));
9567 gimplify_adjust_omp_clauses (pre_p, NULL, &OMP_STANDALONE_CLAUSES (expr),
9568 TREE_CODE (expr));
9569 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
9571 gimplify_seq_add_stmt (pre_p, stmt);
9572 *expr_p = NULL_TREE;
9575 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
9576 stabilized the lhs of the atomic operation as *ADDR. Return true if
9577 EXPR is this stabilized form. */
9579 static bool
9580 goa_lhs_expr_p (tree expr, tree addr)
9582 /* Also include casts to other type variants. The C front end is fond
9583 of adding these for e.g. volatile variables. This is like
9584 STRIP_TYPE_NOPS but includes the main variant lookup. */
9585 STRIP_USELESS_TYPE_CONVERSION (expr);
9587 if (TREE_CODE (expr) == INDIRECT_REF)
9589 expr = TREE_OPERAND (expr, 0);
9590 while (expr != addr
9591 && (CONVERT_EXPR_P (expr)
9592 || TREE_CODE (expr) == NON_LVALUE_EXPR)
9593 && TREE_CODE (expr) == TREE_CODE (addr)
9594 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
9596 expr = TREE_OPERAND (expr, 0);
9597 addr = TREE_OPERAND (addr, 0);
9599 if (expr == addr)
9600 return true;
9601 return (TREE_CODE (addr) == ADDR_EXPR
9602 && TREE_CODE (expr) == ADDR_EXPR
9603 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
9605 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
9606 return true;
9607 return false;
9610 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
9611 expression does not involve the lhs, evaluate it into a temporary.
9612 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
9613 or -1 if an error was encountered. */
9615 static int
9616 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
9617 tree lhs_var)
9619 tree expr = *expr_p;
9620 int saw_lhs;
9622 if (goa_lhs_expr_p (expr, lhs_addr))
9624 *expr_p = lhs_var;
9625 return 1;
9627 if (is_gimple_val (expr))
9628 return 0;
9630 saw_lhs = 0;
9631 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
9633 case tcc_binary:
9634 case tcc_comparison:
9635 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
9636 lhs_var);
9637 case tcc_unary:
9638 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
9639 lhs_var);
9640 break;
9641 case tcc_expression:
9642 switch (TREE_CODE (expr))
9644 case TRUTH_ANDIF_EXPR:
9645 case TRUTH_ORIF_EXPR:
9646 case TRUTH_AND_EXPR:
9647 case TRUTH_OR_EXPR:
9648 case TRUTH_XOR_EXPR:
9649 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
9650 lhs_addr, lhs_var);
9651 case TRUTH_NOT_EXPR:
9652 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
9653 lhs_addr, lhs_var);
9654 break;
9655 case COMPOUND_EXPR:
9656 /* Break out any preevaluations from cp_build_modify_expr. */
9657 for (; TREE_CODE (expr) == COMPOUND_EXPR;
9658 expr = TREE_OPERAND (expr, 1))
9659 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
9660 *expr_p = expr;
9661 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
9662 default:
9663 break;
9665 break;
9666 default:
9667 break;
9670 if (saw_lhs == 0)
9672 enum gimplify_status gs;
9673 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
9674 if (gs != GS_ALL_DONE)
9675 saw_lhs = -1;
9678 return saw_lhs;
9681 /* Gimplify an OMP_ATOMIC statement. */
9683 static enum gimplify_status
9684 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
9686 tree addr = TREE_OPERAND (*expr_p, 0);
9687 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
9688 ? NULL : TREE_OPERAND (*expr_p, 1);
9689 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
9690 tree tmp_load;
9691 gomp_atomic_load *loadstmt;
9692 gomp_atomic_store *storestmt;
9694 tmp_load = create_tmp_reg (type);
9695 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
9696 return GS_ERROR;
9698 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
9699 != GS_ALL_DONE)
9700 return GS_ERROR;
9702 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
9703 gimplify_seq_add_stmt (pre_p, loadstmt);
9704 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
9705 != GS_ALL_DONE)
9706 return GS_ERROR;
9708 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
9709 rhs = tmp_load;
9710 storestmt = gimple_build_omp_atomic_store (rhs);
9711 gimplify_seq_add_stmt (pre_p, storestmt);
9712 if (OMP_ATOMIC_SEQ_CST (*expr_p))
9714 gimple_omp_atomic_set_seq_cst (loadstmt);
9715 gimple_omp_atomic_set_seq_cst (storestmt);
9717 switch (TREE_CODE (*expr_p))
9719 case OMP_ATOMIC_READ:
9720 case OMP_ATOMIC_CAPTURE_OLD:
9721 *expr_p = tmp_load;
9722 gimple_omp_atomic_set_need_value (loadstmt);
9723 break;
9724 case OMP_ATOMIC_CAPTURE_NEW:
9725 *expr_p = rhs;
9726 gimple_omp_atomic_set_need_value (storestmt);
9727 break;
9728 default:
9729 *expr_p = NULL;
9730 break;
9733 return GS_ALL_DONE;
9736 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
9737 body, and adding some EH bits. */
9739 static enum gimplify_status
9740 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
9742 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
9743 gimple *body_stmt;
9744 gtransaction *trans_stmt;
9745 gimple_seq body = NULL;
9746 int subcode = 0;
9748 /* Wrap the transaction body in a BIND_EXPR so we have a context
9749 where to put decls for OMP. */
9750 if (TREE_CODE (tbody) != BIND_EXPR)
9752 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
9753 TREE_SIDE_EFFECTS (bind) = 1;
9754 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
9755 TRANSACTION_EXPR_BODY (expr) = bind;
9758 push_gimplify_context ();
9759 temp = voidify_wrapper_expr (*expr_p, NULL);
9761 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
9762 pop_gimplify_context (body_stmt);
9764 trans_stmt = gimple_build_transaction (body);
9765 if (TRANSACTION_EXPR_OUTER (expr))
9766 subcode = GTMA_IS_OUTER;
9767 else if (TRANSACTION_EXPR_RELAXED (expr))
9768 subcode = GTMA_IS_RELAXED;
9769 gimple_transaction_set_subcode (trans_stmt, subcode);
9771 gimplify_seq_add_stmt (pre_p, trans_stmt);
9773 if (temp)
9775 *expr_p = temp;
9776 return GS_OK;
9779 *expr_p = NULL_TREE;
9780 return GS_ALL_DONE;
9783 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
9784 is the OMP_BODY of the original EXPR (which has already been
9785 gimplified so it's not present in the EXPR).
9787 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
9789 static gimple *
9790 gimplify_omp_ordered (tree expr, gimple_seq body)
9792 tree c, decls;
9793 int failures = 0;
9794 unsigned int i;
9795 tree source_c = NULL_TREE;
9796 tree sink_c = NULL_TREE;
9798 if (gimplify_omp_ctxp)
9799 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
9800 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9801 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
9802 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
9803 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
9805 error_at (OMP_CLAUSE_LOCATION (c),
9806 "%<ordered%> construct with %<depend%> clause must be "
9807 "closely nested inside a loop with %<ordered%> clause "
9808 "with a parameter");
9809 failures++;
9811 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9812 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
9814 bool fail = false;
9815 for (decls = OMP_CLAUSE_DECL (c), i = 0;
9816 decls && TREE_CODE (decls) == TREE_LIST;
9817 decls = TREE_CHAIN (decls), ++i)
9818 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
9819 continue;
9820 else if (TREE_VALUE (decls)
9821 != gimplify_omp_ctxp->loop_iter_var[2 * i])
9823 error_at (OMP_CLAUSE_LOCATION (c),
9824 "variable %qE is not an iteration "
9825 "of outermost loop %d, expected %qE",
9826 TREE_VALUE (decls), i + 1,
9827 gimplify_omp_ctxp->loop_iter_var[2 * i]);
9828 fail = true;
9829 failures++;
9831 else
9832 TREE_VALUE (decls)
9833 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
9834 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
9836 error_at (OMP_CLAUSE_LOCATION (c),
9837 "number of variables in %<depend(sink)%> "
9838 "clause does not match number of "
9839 "iteration variables");
9840 failures++;
9842 sink_c = c;
9844 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
9845 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
9847 if (source_c)
9849 error_at (OMP_CLAUSE_LOCATION (c),
9850 "more than one %<depend(source)%> clause on an "
9851 "%<ordered%> construct");
9852 failures++;
9854 else
9855 source_c = c;
9857 if (source_c && sink_c)
9859 error_at (OMP_CLAUSE_LOCATION (source_c),
9860 "%<depend(source)%> clause specified together with "
9861 "%<depend(sink:)%> clauses on the same construct");
9862 failures++;
9865 if (failures)
9866 return gimple_build_nop ();
9867 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
9870 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
9871 expression produces a value to be used as an operand inside a GIMPLE
9872 statement, the value will be stored back in *EXPR_P. This value will
9873 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
9874 an SSA_NAME. The corresponding sequence of GIMPLE statements is
9875 emitted in PRE_P and POST_P.
9877 Additionally, this process may overwrite parts of the input
9878 expression during gimplification. Ideally, it should be
9879 possible to do non-destructive gimplification.
9881 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
9882 the expression needs to evaluate to a value to be used as
9883 an operand in a GIMPLE statement, this value will be stored in
9884 *EXPR_P on exit. This happens when the caller specifies one
9885 of fb_lvalue or fb_rvalue fallback flags.
9887 PRE_P will contain the sequence of GIMPLE statements corresponding
9888 to the evaluation of EXPR and all the side-effects that must
9889 be executed before the main expression. On exit, the last
9890 statement of PRE_P is the core statement being gimplified. For
9891 instance, when gimplifying 'if (++a)' the last statement in
9892 PRE_P will be 'if (t.1)' where t.1 is the result of
9893 pre-incrementing 'a'.
9895 POST_P will contain the sequence of GIMPLE statements corresponding
9896 to the evaluation of all the side-effects that must be executed
9897 after the main expression. If this is NULL, the post
9898 side-effects are stored at the end of PRE_P.
9900 The reason why the output is split in two is to handle post
9901 side-effects explicitly. In some cases, an expression may have
9902 inner and outer post side-effects which need to be emitted in
9903 an order different from the one given by the recursive
9904 traversal. For instance, for the expression (*p--)++ the post
9905 side-effects of '--' must actually occur *after* the post
9906 side-effects of '++'. However, gimplification will first visit
9907 the inner expression, so if a separate POST sequence was not
9908 used, the resulting sequence would be:
9910 1 t.1 = *p
9911 2 p = p - 1
9912 3 t.2 = t.1 + 1
9913 4 *p = t.2
9915 However, the post-decrement operation in line #2 must not be
9916 evaluated until after the store to *p at line #4, so the
9917 correct sequence should be:
9919 1 t.1 = *p
9920 2 t.2 = t.1 + 1
9921 3 *p = t.2
9922 4 p = p - 1
9924 So, by specifying a separate post queue, it is possible
9925 to emit the post side-effects in the correct order.
9926 If POST_P is NULL, an internal queue will be used. Before
9927 returning to the caller, the sequence POST_P is appended to
9928 the main output sequence PRE_P.
9930 GIMPLE_TEST_F points to a function that takes a tree T and
9931 returns nonzero if T is in the GIMPLE form requested by the
9932 caller. The GIMPLE predicates are in gimple.c.
9934 FALLBACK tells the function what sort of a temporary we want if
9935 gimplification cannot produce an expression that complies with
9936 GIMPLE_TEST_F.
9938 fb_none means that no temporary should be generated
9939 fb_rvalue means that an rvalue is OK to generate
9940 fb_lvalue means that an lvalue is OK to generate
9941 fb_either means that either is OK, but an lvalue is preferable.
9942 fb_mayfail means that gimplification may fail (in which case
9943 GS_ERROR will be returned)
9945 The return value is either GS_ERROR or GS_ALL_DONE, since this
9946 function iterates until EXPR is completely gimplified or an error
9947 occurs. */
9949 enum gimplify_status
9950 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
9951 bool (*gimple_test_f) (tree), fallback_t fallback)
9953 tree tmp;
9954 gimple_seq internal_pre = NULL;
9955 gimple_seq internal_post = NULL;
9956 tree save_expr;
9957 bool is_statement;
9958 location_t saved_location;
9959 enum gimplify_status ret;
9960 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
9962 save_expr = *expr_p;
9963 if (save_expr == NULL_TREE)
9964 return GS_ALL_DONE;
9966 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
9967 is_statement = gimple_test_f == is_gimple_stmt;
9968 if (is_statement)
9969 gcc_assert (pre_p);
9971 /* Consistency checks. */
9972 if (gimple_test_f == is_gimple_reg)
9973 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
9974 else if (gimple_test_f == is_gimple_val
9975 || gimple_test_f == is_gimple_call_addr
9976 || gimple_test_f == is_gimple_condexpr
9977 || gimple_test_f == is_gimple_mem_rhs
9978 || gimple_test_f == is_gimple_mem_rhs_or_call
9979 || gimple_test_f == is_gimple_reg_rhs
9980 || gimple_test_f == is_gimple_reg_rhs_or_call
9981 || gimple_test_f == is_gimple_asm_val
9982 || gimple_test_f == is_gimple_mem_ref_addr)
9983 gcc_assert (fallback & fb_rvalue);
9984 else if (gimple_test_f == is_gimple_min_lval
9985 || gimple_test_f == is_gimple_lvalue)
9986 gcc_assert (fallback & fb_lvalue);
9987 else if (gimple_test_f == is_gimple_addressable)
9988 gcc_assert (fallback & fb_either);
9989 else if (gimple_test_f == is_gimple_stmt)
9990 gcc_assert (fallback == fb_none);
9991 else
9993 /* We should have recognized the GIMPLE_TEST_F predicate to
9994 know what kind of fallback to use in case a temporary is
9995 needed to hold the value or address of *EXPR_P. */
9996 gcc_unreachable ();
9999 /* We used to check the predicate here and return immediately if it
10000 succeeds. This is wrong; the design is for gimplification to be
10001 idempotent, and for the predicates to only test for valid forms, not
10002 whether they are fully simplified. */
10003 if (pre_p == NULL)
10004 pre_p = &internal_pre;
10006 if (post_p == NULL)
10007 post_p = &internal_post;
10009 /* Remember the last statements added to PRE_P and POST_P. Every
10010 new statement added by the gimplification helpers needs to be
10011 annotated with location information. To centralize the
10012 responsibility, we remember the last statement that had been
10013 added to both queues before gimplifying *EXPR_P. If
10014 gimplification produces new statements in PRE_P and POST_P, those
10015 statements will be annotated with the same location information
10016 as *EXPR_P. */
10017 pre_last_gsi = gsi_last (*pre_p);
10018 post_last_gsi = gsi_last (*post_p);
10020 saved_location = input_location;
10021 if (save_expr != error_mark_node
10022 && EXPR_HAS_LOCATION (*expr_p))
10023 input_location = EXPR_LOCATION (*expr_p);
10025 /* Loop over the specific gimplifiers until the toplevel node
10026 remains the same. */
10029 /* Strip away as many useless type conversions as possible
10030 at the toplevel. */
10031 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
10033 /* Remember the expr. */
10034 save_expr = *expr_p;
10036 /* Die, die, die, my darling. */
10037 if (save_expr == error_mark_node
10038 || (TREE_TYPE (save_expr)
10039 && TREE_TYPE (save_expr) == error_mark_node))
10041 ret = GS_ERROR;
10042 break;
10045 /* Do any language-specific gimplification. */
10046 ret = ((enum gimplify_status)
10047 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
10048 if (ret == GS_OK)
10050 if (*expr_p == NULL_TREE)
10051 break;
10052 if (*expr_p != save_expr)
10053 continue;
10055 else if (ret != GS_UNHANDLED)
10056 break;
10058 /* Make sure that all the cases set 'ret' appropriately. */
10059 ret = GS_UNHANDLED;
10060 switch (TREE_CODE (*expr_p))
10062 /* First deal with the special cases. */
10064 case POSTINCREMENT_EXPR:
10065 case POSTDECREMENT_EXPR:
10066 case PREINCREMENT_EXPR:
10067 case PREDECREMENT_EXPR:
10068 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
10069 fallback != fb_none,
10070 TREE_TYPE (*expr_p));
10071 break;
10073 case VIEW_CONVERT_EXPR:
10074 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
10075 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
10077 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10078 post_p, is_gimple_val, fb_rvalue);
10079 recalculate_side_effects (*expr_p);
10080 break;
10082 /* Fallthru. */
10084 case ARRAY_REF:
10085 case ARRAY_RANGE_REF:
10086 case REALPART_EXPR:
10087 case IMAGPART_EXPR:
10088 case COMPONENT_REF:
10089 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
10090 fallback ? fallback : fb_rvalue);
10091 break;
10093 case COND_EXPR:
10094 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
10096 /* C99 code may assign to an array in a structure value of a
10097 conditional expression, and this has undefined behavior
10098 only on execution, so create a temporary if an lvalue is
10099 required. */
10100 if (fallback == fb_lvalue)
10102 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
10103 mark_addressable (*expr_p);
10104 ret = GS_OK;
10106 break;
10108 case CALL_EXPR:
10109 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
10111 /* C99 code may assign to an array in a structure returned
10112 from a function, and this has undefined behavior only on
10113 execution, so create a temporary if an lvalue is
10114 required. */
10115 if (fallback == fb_lvalue)
10117 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
10118 mark_addressable (*expr_p);
10119 ret = GS_OK;
10121 break;
10123 case TREE_LIST:
10124 gcc_unreachable ();
10126 case COMPOUND_EXPR:
10127 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
10128 break;
10130 case COMPOUND_LITERAL_EXPR:
10131 ret = gimplify_compound_literal_expr (expr_p, pre_p,
10132 gimple_test_f, fallback);
10133 break;
10135 case MODIFY_EXPR:
10136 case INIT_EXPR:
10137 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
10138 fallback != fb_none);
10139 break;
10141 case TRUTH_ANDIF_EXPR:
10142 case TRUTH_ORIF_EXPR:
10144 /* Preserve the original type of the expression and the
10145 source location of the outer expression. */
10146 tree org_type = TREE_TYPE (*expr_p);
10147 *expr_p = gimple_boolify (*expr_p);
10148 *expr_p = build3_loc (input_location, COND_EXPR,
10149 org_type, *expr_p,
10150 fold_convert_loc
10151 (input_location,
10152 org_type, boolean_true_node),
10153 fold_convert_loc
10154 (input_location,
10155 org_type, boolean_false_node));
10156 ret = GS_OK;
10157 break;
10160 case TRUTH_NOT_EXPR:
10162 tree type = TREE_TYPE (*expr_p);
10163 /* The parsers are careful to generate TRUTH_NOT_EXPR
10164 only with operands that are always zero or one.
10165 We do not fold here but handle the only interesting case
10166 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
10167 *expr_p = gimple_boolify (*expr_p);
10168 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
10169 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
10170 TREE_TYPE (*expr_p),
10171 TREE_OPERAND (*expr_p, 0));
10172 else
10173 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
10174 TREE_TYPE (*expr_p),
10175 TREE_OPERAND (*expr_p, 0),
10176 build_int_cst (TREE_TYPE (*expr_p), 1));
10177 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
10178 *expr_p = fold_convert_loc (input_location, type, *expr_p);
10179 ret = GS_OK;
10180 break;
10183 case ADDR_EXPR:
10184 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
10185 break;
10187 case ANNOTATE_EXPR:
10189 tree cond = TREE_OPERAND (*expr_p, 0);
10190 tree kind = TREE_OPERAND (*expr_p, 1);
10191 tree type = TREE_TYPE (cond);
10192 if (!INTEGRAL_TYPE_P (type))
10194 *expr_p = cond;
10195 ret = GS_OK;
10196 break;
10198 tree tmp = create_tmp_var (type);
10199 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
10200 gcall *call
10201 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
10202 gimple_call_set_lhs (call, tmp);
10203 gimplify_seq_add_stmt (pre_p, call);
10204 *expr_p = tmp;
10205 ret = GS_ALL_DONE;
10206 break;
10209 case VA_ARG_EXPR:
10210 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
10211 break;
10213 CASE_CONVERT:
10214 if (IS_EMPTY_STMT (*expr_p))
10216 ret = GS_ALL_DONE;
10217 break;
10220 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
10221 || fallback == fb_none)
10223 /* Just strip a conversion to void (or in void context) and
10224 try again. */
10225 *expr_p = TREE_OPERAND (*expr_p, 0);
10226 ret = GS_OK;
10227 break;
10230 ret = gimplify_conversion (expr_p);
10231 if (ret == GS_ERROR)
10232 break;
10233 if (*expr_p != save_expr)
10234 break;
10235 /* FALLTHRU */
10237 case FIX_TRUNC_EXPR:
10238 /* unary_expr: ... | '(' cast ')' val | ... */
10239 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10240 is_gimple_val, fb_rvalue);
10241 recalculate_side_effects (*expr_p);
10242 break;
10244 case INDIRECT_REF:
10246 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
10247 bool notrap = TREE_THIS_NOTRAP (*expr_p);
10248 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
10250 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
10251 if (*expr_p != save_expr)
10253 ret = GS_OK;
10254 break;
10257 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10258 is_gimple_reg, fb_rvalue);
10259 if (ret == GS_ERROR)
10260 break;
10262 recalculate_side_effects (*expr_p);
10263 *expr_p = fold_build2_loc (input_location, MEM_REF,
10264 TREE_TYPE (*expr_p),
10265 TREE_OPERAND (*expr_p, 0),
10266 build_int_cst (saved_ptr_type, 0));
10267 TREE_THIS_VOLATILE (*expr_p) = volatilep;
10268 TREE_THIS_NOTRAP (*expr_p) = notrap;
10269 ret = GS_OK;
10270 break;
10273 /* We arrive here through the various re-gimplifcation paths. */
10274 case MEM_REF:
10275 /* First try re-folding the whole thing. */
10276 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
10277 TREE_OPERAND (*expr_p, 0),
10278 TREE_OPERAND (*expr_p, 1));
10279 if (tmp)
10281 REF_REVERSE_STORAGE_ORDER (tmp)
10282 = REF_REVERSE_STORAGE_ORDER (*expr_p);
10283 *expr_p = tmp;
10284 recalculate_side_effects (*expr_p);
10285 ret = GS_OK;
10286 break;
10288 /* Avoid re-gimplifying the address operand if it is already
10289 in suitable form. Re-gimplifying would mark the address
10290 operand addressable. Always gimplify when not in SSA form
10291 as we still may have to gimplify decls with value-exprs. */
10292 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
10293 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
10295 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10296 is_gimple_mem_ref_addr, fb_rvalue);
10297 if (ret == GS_ERROR)
10298 break;
10300 recalculate_side_effects (*expr_p);
10301 ret = GS_ALL_DONE;
10302 break;
10304 /* Constants need not be gimplified. */
10305 case INTEGER_CST:
10306 case REAL_CST:
10307 case FIXED_CST:
10308 case STRING_CST:
10309 case COMPLEX_CST:
10310 case VECTOR_CST:
10311 /* Drop the overflow flag on constants, we do not want
10312 that in the GIMPLE IL. */
10313 if (TREE_OVERFLOW_P (*expr_p))
10314 *expr_p = drop_tree_overflow (*expr_p);
10315 ret = GS_ALL_DONE;
10316 break;
10318 case CONST_DECL:
10319 /* If we require an lvalue, such as for ADDR_EXPR, retain the
10320 CONST_DECL node. Otherwise the decl is replaceable by its
10321 value. */
10322 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
10323 if (fallback & fb_lvalue)
10324 ret = GS_ALL_DONE;
10325 else
10327 *expr_p = DECL_INITIAL (*expr_p);
10328 ret = GS_OK;
10330 break;
10332 case DECL_EXPR:
10333 ret = gimplify_decl_expr (expr_p, pre_p);
10334 break;
10336 case BIND_EXPR:
10337 ret = gimplify_bind_expr (expr_p, pre_p);
10338 break;
10340 case LOOP_EXPR:
10341 ret = gimplify_loop_expr (expr_p, pre_p);
10342 break;
10344 case SWITCH_EXPR:
10345 ret = gimplify_switch_expr (expr_p, pre_p);
10346 break;
10348 case EXIT_EXPR:
10349 ret = gimplify_exit_expr (expr_p);
10350 break;
10352 case GOTO_EXPR:
10353 /* If the target is not LABEL, then it is a computed jump
10354 and the target needs to be gimplified. */
10355 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
10357 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
10358 NULL, is_gimple_val, fb_rvalue);
10359 if (ret == GS_ERROR)
10360 break;
10362 gimplify_seq_add_stmt (pre_p,
10363 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
10364 ret = GS_ALL_DONE;
10365 break;
10367 case PREDICT_EXPR:
10368 gimplify_seq_add_stmt (pre_p,
10369 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
10370 PREDICT_EXPR_OUTCOME (*expr_p)));
10371 ret = GS_ALL_DONE;
10372 break;
10374 case LABEL_EXPR:
10375 ret = GS_ALL_DONE;
10376 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
10377 == current_function_decl);
10378 gimplify_seq_add_stmt (pre_p,
10379 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
10380 break;
10382 case CASE_LABEL_EXPR:
10383 ret = gimplify_case_label_expr (expr_p, pre_p);
10384 break;
10386 case RETURN_EXPR:
10387 ret = gimplify_return_expr (*expr_p, pre_p);
10388 break;
10390 case CONSTRUCTOR:
10391 /* Don't reduce this in place; let gimplify_init_constructor work its
10392 magic. Buf if we're just elaborating this for side effects, just
10393 gimplify any element that has side-effects. */
10394 if (fallback == fb_none)
10396 unsigned HOST_WIDE_INT ix;
10397 tree val;
10398 tree temp = NULL_TREE;
10399 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
10400 if (TREE_SIDE_EFFECTS (val))
10401 append_to_statement_list (val, &temp);
10403 *expr_p = temp;
10404 ret = temp ? GS_OK : GS_ALL_DONE;
10406 /* C99 code may assign to an array in a constructed
10407 structure or union, and this has undefined behavior only
10408 on execution, so create a temporary if an lvalue is
10409 required. */
10410 else if (fallback == fb_lvalue)
10412 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
10413 mark_addressable (*expr_p);
10414 ret = GS_OK;
10416 else
10417 ret = GS_ALL_DONE;
10418 break;
10420 /* The following are special cases that are not handled by the
10421 original GIMPLE grammar. */
10423 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
10424 eliminated. */
10425 case SAVE_EXPR:
10426 ret = gimplify_save_expr (expr_p, pre_p, post_p);
10427 break;
10429 case BIT_FIELD_REF:
10430 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10431 post_p, is_gimple_lvalue, fb_either);
10432 recalculate_side_effects (*expr_p);
10433 break;
10435 case TARGET_MEM_REF:
10437 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
10439 if (TMR_BASE (*expr_p))
10440 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
10441 post_p, is_gimple_mem_ref_addr, fb_either);
10442 if (TMR_INDEX (*expr_p))
10443 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
10444 post_p, is_gimple_val, fb_rvalue);
10445 if (TMR_INDEX2 (*expr_p))
10446 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
10447 post_p, is_gimple_val, fb_rvalue);
10448 /* TMR_STEP and TMR_OFFSET are always integer constants. */
10449 ret = MIN (r0, r1);
10451 break;
10453 case NON_LVALUE_EXPR:
10454 /* This should have been stripped above. */
10455 gcc_unreachable ();
10457 case ASM_EXPR:
10458 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
10459 break;
10461 case TRY_FINALLY_EXPR:
10462 case TRY_CATCH_EXPR:
10464 gimple_seq eval, cleanup;
10465 gtry *try_;
10467 /* Calls to destructors are generated automatically in FINALLY/CATCH
10468 block. They should have location as UNKNOWN_LOCATION. However,
10469 gimplify_call_expr will reset these call stmts to input_location
10470 if it finds stmt's location is unknown. To prevent resetting for
10471 destructors, we set the input_location to unknown.
10472 Note that this only affects the destructor calls in FINALLY/CATCH
10473 block, and will automatically reset to its original value by the
10474 end of gimplify_expr. */
10475 input_location = UNKNOWN_LOCATION;
10476 eval = cleanup = NULL;
10477 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
10478 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
10479 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
10480 if (gimple_seq_empty_p (cleanup))
10482 gimple_seq_add_seq (pre_p, eval);
10483 ret = GS_ALL_DONE;
10484 break;
10486 try_ = gimple_build_try (eval, cleanup,
10487 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
10488 ? GIMPLE_TRY_FINALLY
10489 : GIMPLE_TRY_CATCH);
10490 if (EXPR_HAS_LOCATION (save_expr))
10491 gimple_set_location (try_, EXPR_LOCATION (save_expr));
10492 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
10493 gimple_set_location (try_, saved_location);
10494 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
10495 gimple_try_set_catch_is_cleanup (try_,
10496 TRY_CATCH_IS_CLEANUP (*expr_p));
10497 gimplify_seq_add_stmt (pre_p, try_);
10498 ret = GS_ALL_DONE;
10499 break;
10502 case CLEANUP_POINT_EXPR:
10503 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
10504 break;
10506 case TARGET_EXPR:
10507 ret = gimplify_target_expr (expr_p, pre_p, post_p);
10508 break;
10510 case CATCH_EXPR:
10512 gimple *c;
10513 gimple_seq handler = NULL;
10514 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
10515 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
10516 gimplify_seq_add_stmt (pre_p, c);
10517 ret = GS_ALL_DONE;
10518 break;
10521 case EH_FILTER_EXPR:
10523 gimple *ehf;
10524 gimple_seq failure = NULL;
10526 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
10527 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
10528 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
10529 gimplify_seq_add_stmt (pre_p, ehf);
10530 ret = GS_ALL_DONE;
10531 break;
10534 case OBJ_TYPE_REF:
10536 enum gimplify_status r0, r1;
10537 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
10538 post_p, is_gimple_val, fb_rvalue);
10539 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
10540 post_p, is_gimple_val, fb_rvalue);
10541 TREE_SIDE_EFFECTS (*expr_p) = 0;
10542 ret = MIN (r0, r1);
10544 break;
10546 case LABEL_DECL:
10547 /* We get here when taking the address of a label. We mark
10548 the label as "forced"; meaning it can never be removed and
10549 it is a potential target for any computed goto. */
10550 FORCED_LABEL (*expr_p) = 1;
10551 ret = GS_ALL_DONE;
10552 break;
10554 case STATEMENT_LIST:
10555 ret = gimplify_statement_list (expr_p, pre_p);
10556 break;
10558 case WITH_SIZE_EXPR:
10560 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10561 post_p == &internal_post ? NULL : post_p,
10562 gimple_test_f, fallback);
10563 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
10564 is_gimple_val, fb_rvalue);
10565 ret = GS_ALL_DONE;
10567 break;
10569 case VAR_DECL:
10570 case PARM_DECL:
10571 ret = gimplify_var_or_parm_decl (expr_p);
10572 break;
10574 case RESULT_DECL:
10575 /* When within an OMP context, notice uses of variables. */
10576 if (gimplify_omp_ctxp)
10577 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
10578 ret = GS_ALL_DONE;
10579 break;
10581 case SSA_NAME:
10582 /* Allow callbacks into the gimplifier during optimization. */
10583 ret = GS_ALL_DONE;
10584 break;
10586 case OMP_PARALLEL:
10587 gimplify_omp_parallel (expr_p, pre_p);
10588 ret = GS_ALL_DONE;
10589 break;
10591 case OMP_TASK:
10592 gimplify_omp_task (expr_p, pre_p);
10593 ret = GS_ALL_DONE;
10594 break;
10596 case OMP_FOR:
10597 case OMP_SIMD:
10598 case CILK_SIMD:
10599 case CILK_FOR:
10600 case OMP_DISTRIBUTE:
10601 case OMP_TASKLOOP:
10602 case OACC_LOOP:
10603 ret = gimplify_omp_for (expr_p, pre_p);
10604 break;
10606 case OACC_CACHE:
10607 gimplify_oacc_cache (expr_p, pre_p);
10608 ret = GS_ALL_DONE;
10609 break;
10611 case OACC_DECLARE:
10612 gimplify_oacc_declare (expr_p, pre_p);
10613 ret = GS_ALL_DONE;
10614 break;
10616 case OACC_HOST_DATA:
10617 case OACC_DATA:
10618 case OACC_KERNELS:
10619 case OACC_PARALLEL:
10620 case OMP_SECTIONS:
10621 case OMP_SINGLE:
10622 case OMP_TARGET:
10623 case OMP_TARGET_DATA:
10624 case OMP_TEAMS:
10625 gimplify_omp_workshare (expr_p, pre_p);
10626 ret = GS_ALL_DONE;
10627 break;
10629 case OACC_ENTER_DATA:
10630 case OACC_EXIT_DATA:
10631 case OACC_UPDATE:
10632 case OMP_TARGET_UPDATE:
10633 case OMP_TARGET_ENTER_DATA:
10634 case OMP_TARGET_EXIT_DATA:
10635 gimplify_omp_target_update (expr_p, pre_p);
10636 ret = GS_ALL_DONE;
10637 break;
10639 case OMP_SECTION:
10640 case OMP_MASTER:
10641 case OMP_TASKGROUP:
10642 case OMP_ORDERED:
10643 case OMP_CRITICAL:
10645 gimple_seq body = NULL;
10646 gimple *g;
10648 gimplify_and_add (OMP_BODY (*expr_p), &body);
10649 switch (TREE_CODE (*expr_p))
10651 case OMP_SECTION:
10652 g = gimple_build_omp_section (body);
10653 break;
10654 case OMP_MASTER:
10655 g = gimple_build_omp_master (body);
10656 break;
10657 case OMP_TASKGROUP:
10659 gimple_seq cleanup = NULL;
10660 tree fn
10661 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
10662 g = gimple_build_call (fn, 0);
10663 gimple_seq_add_stmt (&cleanup, g);
10664 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
10665 body = NULL;
10666 gimple_seq_add_stmt (&body, g);
10667 g = gimple_build_omp_taskgroup (body);
10669 break;
10670 case OMP_ORDERED:
10671 g = gimplify_omp_ordered (*expr_p, body);
10672 break;
10673 case OMP_CRITICAL:
10674 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
10675 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
10676 gimplify_adjust_omp_clauses (pre_p, body,
10677 &OMP_CRITICAL_CLAUSES (*expr_p),
10678 OMP_CRITICAL);
10679 g = gimple_build_omp_critical (body,
10680 OMP_CRITICAL_NAME (*expr_p),
10681 OMP_CRITICAL_CLAUSES (*expr_p));
10682 break;
10683 default:
10684 gcc_unreachable ();
10686 gimplify_seq_add_stmt (pre_p, g);
10687 ret = GS_ALL_DONE;
10688 break;
10691 case OMP_ATOMIC:
10692 case OMP_ATOMIC_READ:
10693 case OMP_ATOMIC_CAPTURE_OLD:
10694 case OMP_ATOMIC_CAPTURE_NEW:
10695 ret = gimplify_omp_atomic (expr_p, pre_p);
10696 break;
10698 case TRANSACTION_EXPR:
10699 ret = gimplify_transaction (expr_p, pre_p);
10700 break;
10702 case TRUTH_AND_EXPR:
10703 case TRUTH_OR_EXPR:
10704 case TRUTH_XOR_EXPR:
10706 tree orig_type = TREE_TYPE (*expr_p);
10707 tree new_type, xop0, xop1;
10708 *expr_p = gimple_boolify (*expr_p);
10709 new_type = TREE_TYPE (*expr_p);
10710 if (!useless_type_conversion_p (orig_type, new_type))
10712 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
10713 ret = GS_OK;
10714 break;
10717 /* Boolified binary truth expressions are semantically equivalent
10718 to bitwise binary expressions. Canonicalize them to the
10719 bitwise variant. */
10720 switch (TREE_CODE (*expr_p))
10722 case TRUTH_AND_EXPR:
10723 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
10724 break;
10725 case TRUTH_OR_EXPR:
10726 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
10727 break;
10728 case TRUTH_XOR_EXPR:
10729 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
10730 break;
10731 default:
10732 break;
10734 /* Now make sure that operands have compatible type to
10735 expression's new_type. */
10736 xop0 = TREE_OPERAND (*expr_p, 0);
10737 xop1 = TREE_OPERAND (*expr_p, 1);
10738 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
10739 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
10740 new_type,
10741 xop0);
10742 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
10743 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
10744 new_type,
10745 xop1);
10746 /* Continue classified as tcc_binary. */
10747 goto expr_2;
10750 case FMA_EXPR:
10751 case VEC_COND_EXPR:
10752 case VEC_PERM_EXPR:
10753 /* Classified as tcc_expression. */
10754 goto expr_3;
10756 case POINTER_PLUS_EXPR:
10758 enum gimplify_status r0, r1;
10759 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10760 post_p, is_gimple_val, fb_rvalue);
10761 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10762 post_p, is_gimple_val, fb_rvalue);
10763 recalculate_side_effects (*expr_p);
10764 ret = MIN (r0, r1);
10765 break;
10768 case CILK_SYNC_STMT:
10770 if (!fn_contains_cilk_spawn_p (cfun))
10772 error_at (EXPR_LOCATION (*expr_p),
10773 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
10774 ret = GS_ERROR;
10776 else
10778 gimplify_cilk_sync (expr_p, pre_p);
10779 ret = GS_ALL_DONE;
10781 break;
10784 default:
10785 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
10787 case tcc_comparison:
10788 /* Handle comparison of objects of non scalar mode aggregates
10789 with a call to memcmp. It would be nice to only have to do
10790 this for variable-sized objects, but then we'd have to allow
10791 the same nest of reference nodes we allow for MODIFY_EXPR and
10792 that's too complex.
10794 Compare scalar mode aggregates as scalar mode values. Using
10795 memcmp for them would be very inefficient at best, and is
10796 plain wrong if bitfields are involved. */
10798 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
10800 /* Vector comparisons need no boolification. */
10801 if (TREE_CODE (type) == VECTOR_TYPE)
10802 goto expr_2;
10803 else if (!AGGREGATE_TYPE_P (type))
10805 tree org_type = TREE_TYPE (*expr_p);
10806 *expr_p = gimple_boolify (*expr_p);
10807 if (!useless_type_conversion_p (org_type,
10808 TREE_TYPE (*expr_p)))
10810 *expr_p = fold_convert_loc (input_location,
10811 org_type, *expr_p);
10812 ret = GS_OK;
10814 else
10815 goto expr_2;
10817 else if (TYPE_MODE (type) != BLKmode)
10818 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
10819 else
10820 ret = gimplify_variable_sized_compare (expr_p);
10822 break;
10825 /* If *EXPR_P does not need to be special-cased, handle it
10826 according to its class. */
10827 case tcc_unary:
10828 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10829 post_p, is_gimple_val, fb_rvalue);
10830 break;
10832 case tcc_binary:
10833 expr_2:
10835 enum gimplify_status r0, r1;
10837 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10838 post_p, is_gimple_val, fb_rvalue);
10839 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10840 post_p, is_gimple_val, fb_rvalue);
10842 ret = MIN (r0, r1);
10843 break;
10846 expr_3:
10848 enum gimplify_status r0, r1, r2;
10850 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
10851 post_p, is_gimple_val, fb_rvalue);
10852 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
10853 post_p, is_gimple_val, fb_rvalue);
10854 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
10855 post_p, is_gimple_val, fb_rvalue);
10857 ret = MIN (MIN (r0, r1), r2);
10858 break;
10861 case tcc_declaration:
10862 case tcc_constant:
10863 ret = GS_ALL_DONE;
10864 goto dont_recalculate;
10866 default:
10867 gcc_unreachable ();
10870 recalculate_side_effects (*expr_p);
10872 dont_recalculate:
10873 break;
10876 gcc_assert (*expr_p || ret != GS_OK);
10878 while (ret == GS_OK);
10880 /* If we encountered an error_mark somewhere nested inside, either
10881 stub out the statement or propagate the error back out. */
10882 if (ret == GS_ERROR)
10884 if (is_statement)
10885 *expr_p = NULL;
10886 goto out;
10889 /* This was only valid as a return value from the langhook, which
10890 we handled. Make sure it doesn't escape from any other context. */
10891 gcc_assert (ret != GS_UNHANDLED);
10893 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
10895 /* We aren't looking for a value, and we don't have a valid
10896 statement. If it doesn't have side-effects, throw it away. */
10897 if (!TREE_SIDE_EFFECTS (*expr_p))
10898 *expr_p = NULL;
10899 else if (!TREE_THIS_VOLATILE (*expr_p))
10901 /* This is probably a _REF that contains something nested that
10902 has side effects. Recurse through the operands to find it. */
10903 enum tree_code code = TREE_CODE (*expr_p);
10905 switch (code)
10907 case COMPONENT_REF:
10908 case REALPART_EXPR:
10909 case IMAGPART_EXPR:
10910 case VIEW_CONVERT_EXPR:
10911 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10912 gimple_test_f, fallback);
10913 break;
10915 case ARRAY_REF:
10916 case ARRAY_RANGE_REF:
10917 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
10918 gimple_test_f, fallback);
10919 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
10920 gimple_test_f, fallback);
10921 break;
10923 default:
10924 /* Anything else with side-effects must be converted to
10925 a valid statement before we get here. */
10926 gcc_unreachable ();
10929 *expr_p = NULL;
10931 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
10932 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
10934 /* Historically, the compiler has treated a bare reference
10935 to a non-BLKmode volatile lvalue as forcing a load. */
10936 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
10938 /* Normally, we do not want to create a temporary for a
10939 TREE_ADDRESSABLE type because such a type should not be
10940 copied by bitwise-assignment. However, we make an
10941 exception here, as all we are doing here is ensuring that
10942 we read the bytes that make up the type. We use
10943 create_tmp_var_raw because create_tmp_var will abort when
10944 given a TREE_ADDRESSABLE type. */
10945 tree tmp = create_tmp_var_raw (type, "vol");
10946 gimple_add_tmp_var (tmp);
10947 gimplify_assign (tmp, *expr_p, pre_p);
10948 *expr_p = NULL;
10950 else
10951 /* We can't do anything useful with a volatile reference to
10952 an incomplete type, so just throw it away. Likewise for
10953 a BLKmode type, since any implicit inner load should
10954 already have been turned into an explicit one by the
10955 gimplification process. */
10956 *expr_p = NULL;
10959 /* If we are gimplifying at the statement level, we're done. Tack
10960 everything together and return. */
10961 if (fallback == fb_none || is_statement)
10963 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
10964 it out for GC to reclaim it. */
10965 *expr_p = NULL_TREE;
10967 if (!gimple_seq_empty_p (internal_pre)
10968 || !gimple_seq_empty_p (internal_post))
10970 gimplify_seq_add_seq (&internal_pre, internal_post);
10971 gimplify_seq_add_seq (pre_p, internal_pre);
10974 /* The result of gimplifying *EXPR_P is going to be the last few
10975 statements in *PRE_P and *POST_P. Add location information
10976 to all the statements that were added by the gimplification
10977 helpers. */
10978 if (!gimple_seq_empty_p (*pre_p))
10979 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
10981 if (!gimple_seq_empty_p (*post_p))
10982 annotate_all_with_location_after (*post_p, post_last_gsi,
10983 input_location);
10985 goto out;
10988 #ifdef ENABLE_GIMPLE_CHECKING
10989 if (*expr_p)
10991 enum tree_code code = TREE_CODE (*expr_p);
10992 /* These expressions should already be in gimple IR form. */
10993 gcc_assert (code != MODIFY_EXPR
10994 && code != ASM_EXPR
10995 && code != BIND_EXPR
10996 && code != CATCH_EXPR
10997 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
10998 && code != EH_FILTER_EXPR
10999 && code != GOTO_EXPR
11000 && code != LABEL_EXPR
11001 && code != LOOP_EXPR
11002 && code != SWITCH_EXPR
11003 && code != TRY_FINALLY_EXPR
11004 && code != OACC_PARALLEL
11005 && code != OACC_KERNELS
11006 && code != OACC_DATA
11007 && code != OACC_HOST_DATA
11008 && code != OACC_DECLARE
11009 && code != OACC_UPDATE
11010 && code != OACC_ENTER_DATA
11011 && code != OACC_EXIT_DATA
11012 && code != OACC_CACHE
11013 && code != OMP_CRITICAL
11014 && code != OMP_FOR
11015 && code != OACC_LOOP
11016 && code != OMP_MASTER
11017 && code != OMP_TASKGROUP
11018 && code != OMP_ORDERED
11019 && code != OMP_PARALLEL
11020 && code != OMP_SECTIONS
11021 && code != OMP_SECTION
11022 && code != OMP_SINGLE);
11024 #endif
11026 /* Otherwise we're gimplifying a subexpression, so the resulting
11027 value is interesting. If it's a valid operand that matches
11028 GIMPLE_TEST_F, we're done. Unless we are handling some
11029 post-effects internally; if that's the case, we need to copy into
11030 a temporary before adding the post-effects to POST_P. */
11031 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
11032 goto out;
11034 /* Otherwise, we need to create a new temporary for the gimplified
11035 expression. */
11037 /* We can't return an lvalue if we have an internal postqueue. The
11038 object the lvalue refers to would (probably) be modified by the
11039 postqueue; we need to copy the value out first, which means an
11040 rvalue. */
11041 if ((fallback & fb_lvalue)
11042 && gimple_seq_empty_p (internal_post)
11043 && is_gimple_addressable (*expr_p))
11045 /* An lvalue will do. Take the address of the expression, store it
11046 in a temporary, and replace the expression with an INDIRECT_REF of
11047 that temporary. */
11048 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
11049 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
11050 *expr_p = build_simple_mem_ref (tmp);
11052 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
11054 /* An rvalue will do. Assign the gimplified expression into a
11055 new temporary TMP and replace the original expression with
11056 TMP. First, make sure that the expression has a type so that
11057 it can be assigned into a temporary. */
11058 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
11059 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
11061 else
11063 #ifdef ENABLE_GIMPLE_CHECKING
11064 if (!(fallback & fb_mayfail))
11066 fprintf (stderr, "gimplification failed:\n");
11067 print_generic_expr (stderr, *expr_p, 0);
11068 debug_tree (*expr_p);
11069 internal_error ("gimplification failed");
11071 #endif
11072 gcc_assert (fallback & fb_mayfail);
11074 /* If this is an asm statement, and the user asked for the
11075 impossible, don't die. Fail and let gimplify_asm_expr
11076 issue an error. */
11077 ret = GS_ERROR;
11078 goto out;
11081 /* Make sure the temporary matches our predicate. */
11082 gcc_assert ((*gimple_test_f) (*expr_p));
11084 if (!gimple_seq_empty_p (internal_post))
11086 annotate_all_with_location (internal_post, input_location);
11087 gimplify_seq_add_seq (pre_p, internal_post);
11090 out:
11091 input_location = saved_location;
11092 return ret;
11095 /* Look through TYPE for variable-sized objects and gimplify each such
11096 size that we find. Add to LIST_P any statements generated. */
11098 void
11099 gimplify_type_sizes (tree type, gimple_seq *list_p)
11101 tree field, t;
11103 if (type == NULL || type == error_mark_node)
11104 return;
11106 /* We first do the main variant, then copy into any other variants. */
11107 type = TYPE_MAIN_VARIANT (type);
11109 /* Avoid infinite recursion. */
11110 if (TYPE_SIZES_GIMPLIFIED (type))
11111 return;
11113 TYPE_SIZES_GIMPLIFIED (type) = 1;
11115 switch (TREE_CODE (type))
11117 case INTEGER_TYPE:
11118 case ENUMERAL_TYPE:
11119 case BOOLEAN_TYPE:
11120 case REAL_TYPE:
11121 case FIXED_POINT_TYPE:
11122 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
11123 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
11125 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11127 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
11128 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
11130 break;
11132 case ARRAY_TYPE:
11133 /* These types may not have declarations, so handle them here. */
11134 gimplify_type_sizes (TREE_TYPE (type), list_p);
11135 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
11136 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
11137 with assigned stack slots, for -O1+ -g they should be tracked
11138 by VTA. */
11139 if (!(TYPE_NAME (type)
11140 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11141 && DECL_IGNORED_P (TYPE_NAME (type)))
11142 && TYPE_DOMAIN (type)
11143 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
11145 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
11146 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
11147 DECL_IGNORED_P (t) = 0;
11148 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
11149 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
11150 DECL_IGNORED_P (t) = 0;
11152 break;
11154 case RECORD_TYPE:
11155 case UNION_TYPE:
11156 case QUAL_UNION_TYPE:
11157 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11158 if (TREE_CODE (field) == FIELD_DECL)
11160 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
11161 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
11162 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
11163 gimplify_type_sizes (TREE_TYPE (field), list_p);
11165 break;
11167 case POINTER_TYPE:
11168 case REFERENCE_TYPE:
11169 /* We used to recurse on the pointed-to type here, which turned out to
11170 be incorrect because its definition might refer to variables not
11171 yet initialized at this point if a forward declaration is involved.
11173 It was actually useful for anonymous pointed-to types to ensure
11174 that the sizes evaluation dominates every possible later use of the
11175 values. Restricting to such types here would be safe since there
11176 is no possible forward declaration around, but would introduce an
11177 undesirable middle-end semantic to anonymity. We then defer to
11178 front-ends the responsibility of ensuring that the sizes are
11179 evaluated both early and late enough, e.g. by attaching artificial
11180 type declarations to the tree. */
11181 break;
11183 default:
11184 break;
11187 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
11188 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
11190 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11192 TYPE_SIZE (t) = TYPE_SIZE (type);
11193 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
11194 TYPE_SIZES_GIMPLIFIED (t) = 1;
11198 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
11199 a size or position, has had all of its SAVE_EXPRs evaluated.
11200 We add any required statements to *STMT_P. */
11202 void
11203 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
11205 tree expr = *expr_p;
11207 /* We don't do anything if the value isn't there, is constant, or contains
11208 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
11209 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
11210 will want to replace it with a new variable, but that will cause problems
11211 if this type is from outside the function. It's OK to have that here. */
11212 if (is_gimple_sizepos (expr))
11213 return;
11215 *expr_p = unshare_expr (expr);
11217 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
11220 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
11221 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
11222 is true, also gimplify the parameters. */
11224 gbind *
11225 gimplify_body (tree fndecl, bool do_parms)
11227 location_t saved_location = input_location;
11228 gimple_seq parm_stmts, seq;
11229 gimple *outer_stmt;
11230 gbind *outer_bind;
11231 struct cgraph_node *cgn;
11233 timevar_push (TV_TREE_GIMPLIFY);
11235 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
11236 gimplification. */
11237 default_rtl_profile ();
11239 gcc_assert (gimplify_ctxp == NULL);
11240 push_gimplify_context ();
11242 if (flag_openacc || flag_openmp)
11244 gcc_assert (gimplify_omp_ctxp == NULL);
11245 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
11246 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
11249 /* Unshare most shared trees in the body and in that of any nested functions.
11250 It would seem we don't have to do this for nested functions because
11251 they are supposed to be output and then the outer function gimplified
11252 first, but the g++ front end doesn't always do it that way. */
11253 unshare_body (fndecl);
11254 unvisit_body (fndecl);
11256 cgn = cgraph_node::get (fndecl);
11257 if (cgn && cgn->origin)
11258 nonlocal_vlas = new hash_set<tree>;
11260 /* Make sure input_location isn't set to something weird. */
11261 input_location = DECL_SOURCE_LOCATION (fndecl);
11263 /* Resolve callee-copies. This has to be done before processing
11264 the body so that DECL_VALUE_EXPR gets processed correctly. */
11265 parm_stmts = do_parms ? gimplify_parameters () : NULL;
11267 /* Gimplify the function's body. */
11268 seq = NULL;
11269 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
11270 outer_stmt = gimple_seq_first_stmt (seq);
11271 if (!outer_stmt)
11273 outer_stmt = gimple_build_nop ();
11274 gimplify_seq_add_stmt (&seq, outer_stmt);
11277 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
11278 not the case, wrap everything in a GIMPLE_BIND to make it so. */
11279 if (gimple_code (outer_stmt) == GIMPLE_BIND
11280 && gimple_seq_first (seq) == gimple_seq_last (seq))
11281 outer_bind = as_a <gbind *> (outer_stmt);
11282 else
11283 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
11285 DECL_SAVED_TREE (fndecl) = NULL_TREE;
11287 /* If we had callee-copies statements, insert them at the beginning
11288 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
11289 if (!gimple_seq_empty_p (parm_stmts))
11291 tree parm;
11293 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
11294 gimple_bind_set_body (outer_bind, parm_stmts);
11296 for (parm = DECL_ARGUMENTS (current_function_decl);
11297 parm; parm = DECL_CHAIN (parm))
11298 if (DECL_HAS_VALUE_EXPR_P (parm))
11300 DECL_HAS_VALUE_EXPR_P (parm) = 0;
11301 DECL_IGNORED_P (parm) = 0;
11305 if (nonlocal_vlas)
11307 if (nonlocal_vla_vars)
11309 /* tree-nested.c may later on call declare_vars (..., true);
11310 which relies on BLOCK_VARS chain to be the tail of the
11311 gimple_bind_vars chain. Ensure we don't violate that
11312 assumption. */
11313 if (gimple_bind_block (outer_bind)
11314 == DECL_INITIAL (current_function_decl))
11315 declare_vars (nonlocal_vla_vars, outer_bind, true);
11316 else
11317 BLOCK_VARS (DECL_INITIAL (current_function_decl))
11318 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
11319 nonlocal_vla_vars);
11320 nonlocal_vla_vars = NULL_TREE;
11322 delete nonlocal_vlas;
11323 nonlocal_vlas = NULL;
11326 if ((flag_openacc || flag_openmp || flag_openmp_simd)
11327 && gimplify_omp_ctxp)
11329 delete_omp_context (gimplify_omp_ctxp);
11330 gimplify_omp_ctxp = NULL;
11333 pop_gimplify_context (outer_bind);
11334 gcc_assert (gimplify_ctxp == NULL);
11336 if (flag_checking && !seen_error ())
11337 verify_gimple_in_seq (gimple_bind_body (outer_bind));
11339 timevar_pop (TV_TREE_GIMPLIFY);
11340 input_location = saved_location;
11342 return outer_bind;
11345 typedef char *char_p; /* For DEF_VEC_P. */
11347 /* Return whether we should exclude FNDECL from instrumentation. */
11349 static bool
11350 flag_instrument_functions_exclude_p (tree fndecl)
11352 vec<char_p> *v;
11354 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
11355 if (v && v->length () > 0)
11357 const char *name;
11358 int i;
11359 char *s;
11361 name = lang_hooks.decl_printable_name (fndecl, 0);
11362 FOR_EACH_VEC_ELT (*v, i, s)
11363 if (strstr (name, s) != NULL)
11364 return true;
11367 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
11368 if (v && v->length () > 0)
11370 const char *name;
11371 int i;
11372 char *s;
11374 name = DECL_SOURCE_FILE (fndecl);
11375 FOR_EACH_VEC_ELT (*v, i, s)
11376 if (strstr (name, s) != NULL)
11377 return true;
11380 return false;
11383 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
11384 node for the function we want to gimplify.
11386 Return the sequence of GIMPLE statements corresponding to the body
11387 of FNDECL. */
11389 void
11390 gimplify_function_tree (tree fndecl)
11392 tree parm, ret;
11393 gimple_seq seq;
11394 gbind *bind;
11396 gcc_assert (!gimple_body (fndecl));
11398 if (DECL_STRUCT_FUNCTION (fndecl))
11399 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
11400 else
11401 push_struct_function (fndecl);
11403 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
11404 if necessary. */
11405 cfun->curr_properties |= PROP_gimple_lva;
11407 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
11409 /* Preliminarily mark non-addressed complex variables as eligible
11410 for promotion to gimple registers. We'll transform their uses
11411 as we find them. */
11412 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
11413 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
11414 && !TREE_THIS_VOLATILE (parm)
11415 && !needs_to_live_in_memory (parm))
11416 DECL_GIMPLE_REG_P (parm) = 1;
11419 ret = DECL_RESULT (fndecl);
11420 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
11421 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
11422 && !needs_to_live_in_memory (ret))
11423 DECL_GIMPLE_REG_P (ret) = 1;
11425 bind = gimplify_body (fndecl, true);
11427 /* The tree body of the function is no longer needed, replace it
11428 with the new GIMPLE body. */
11429 seq = NULL;
11430 gimple_seq_add_stmt (&seq, bind);
11431 gimple_set_body (fndecl, seq);
11433 /* If we're instrumenting function entry/exit, then prepend the call to
11434 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
11435 catch the exit hook. */
11436 /* ??? Add some way to ignore exceptions for this TFE. */
11437 if (flag_instrument_function_entry_exit
11438 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
11439 && !flag_instrument_functions_exclude_p (fndecl))
11441 tree x;
11442 gbind *new_bind;
11443 gimple *tf;
11444 gimple_seq cleanup = NULL, body = NULL;
11445 tree tmp_var;
11446 gcall *call;
11448 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
11449 call = gimple_build_call (x, 1, integer_zero_node);
11450 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
11451 gimple_call_set_lhs (call, tmp_var);
11452 gimplify_seq_add_stmt (&cleanup, call);
11453 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
11454 call = gimple_build_call (x, 2,
11455 build_fold_addr_expr (current_function_decl),
11456 tmp_var);
11457 gimplify_seq_add_stmt (&cleanup, call);
11458 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
11460 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
11461 call = gimple_build_call (x, 1, integer_zero_node);
11462 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
11463 gimple_call_set_lhs (call, tmp_var);
11464 gimplify_seq_add_stmt (&body, call);
11465 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
11466 call = gimple_build_call (x, 2,
11467 build_fold_addr_expr (current_function_decl),
11468 tmp_var);
11469 gimplify_seq_add_stmt (&body, call);
11470 gimplify_seq_add_stmt (&body, tf);
11471 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
11472 /* Clear the block for BIND, since it is no longer directly inside
11473 the function, but within a try block. */
11474 gimple_bind_set_block (bind, NULL);
11476 /* Replace the current function body with the body
11477 wrapped in the try/finally TF. */
11478 seq = NULL;
11479 gimple_seq_add_stmt (&seq, new_bind);
11480 gimple_set_body (fndecl, seq);
11481 bind = new_bind;
11484 if ((flag_sanitize & SANITIZE_THREAD) != 0
11485 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
11487 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
11488 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
11489 gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
11490 /* Clear the block for BIND, since it is no longer directly inside
11491 the function, but within a try block. */
11492 gimple_bind_set_block (bind, NULL);
11493 /* Replace the current function body with the body
11494 wrapped in the try/finally TF. */
11495 seq = NULL;
11496 gimple_seq_add_stmt (&seq, new_bind);
11497 gimple_set_body (fndecl, seq);
11500 DECL_SAVED_TREE (fndecl) = NULL_TREE;
11501 cfun->curr_properties |= PROP_gimple_any;
11503 pop_cfun ();
11505 dump_function (TDI_generic, fndecl);
11508 /* Return a dummy expression of type TYPE in order to keep going after an
11509 error. */
11511 static tree
11512 dummy_object (tree type)
11514 tree t = build_int_cst (build_pointer_type (type), 0);
11515 return build2 (MEM_REF, type, t, t);
11518 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
11519 builtin function, but a very special sort of operator. */
11521 enum gimplify_status
11522 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
11523 gimple_seq *post_p ATTRIBUTE_UNUSED)
11525 tree promoted_type, have_va_type;
11526 tree valist = TREE_OPERAND (*expr_p, 0);
11527 tree type = TREE_TYPE (*expr_p);
11528 tree t, tag, aptag;
11529 location_t loc = EXPR_LOCATION (*expr_p);
11531 /* Verify that valist is of the proper type. */
11532 have_va_type = TREE_TYPE (valist);
11533 if (have_va_type == error_mark_node)
11534 return GS_ERROR;
11535 have_va_type = targetm.canonical_va_list_type (have_va_type);
11537 if (have_va_type == NULL_TREE)
11539 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
11540 return GS_ERROR;
11543 /* Generate a diagnostic for requesting data of a type that cannot
11544 be passed through `...' due to type promotion at the call site. */
11545 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
11546 != type)
11548 static bool gave_help;
11549 bool warned;
11551 /* Unfortunately, this is merely undefined, rather than a constraint
11552 violation, so we cannot make this an error. If this call is never
11553 executed, the program is still strictly conforming. */
11554 warned = warning_at (loc, 0,
11555 "%qT is promoted to %qT when passed through %<...%>",
11556 type, promoted_type);
11557 if (!gave_help && warned)
11559 gave_help = true;
11560 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
11561 promoted_type, type);
11564 /* We can, however, treat "undefined" any way we please.
11565 Call abort to encourage the user to fix the program. */
11566 if (warned)
11567 inform (loc, "if this code is reached, the program will abort");
11568 /* Before the abort, allow the evaluation of the va_list
11569 expression to exit or longjmp. */
11570 gimplify_and_add (valist, pre_p);
11571 t = build_call_expr_loc (loc,
11572 builtin_decl_implicit (BUILT_IN_TRAP), 0);
11573 gimplify_and_add (t, pre_p);
11575 /* This is dead code, but go ahead and finish so that the
11576 mode of the result comes out right. */
11577 *expr_p = dummy_object (type);
11578 return GS_ALL_DONE;
11581 tag = build_int_cst (build_pointer_type (type), 0);
11582 aptag = build_int_cst (TREE_TYPE (valist), 0);
11584 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 3,
11585 valist, tag, aptag);
11587 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
11588 needs to be expanded. */
11589 cfun->curr_properties &= ~PROP_gimple_lva;
11591 return GS_OK;
11594 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
11596 DST/SRC are the destination and source respectively. You can pass
11597 ungimplified trees in DST or SRC, in which case they will be
11598 converted to a gimple operand if necessary.
11600 This function returns the newly created GIMPLE_ASSIGN tuple. */
11602 gimple *
11603 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
11605 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
11606 gimplify_and_add (t, seq_p);
11607 ggc_free (t);
11608 return gimple_seq_last_stmt (*seq_p);
11611 inline hashval_t
11612 gimplify_hasher::hash (const elt_t *p)
11614 tree t = p->val;
11615 return iterative_hash_expr (t, 0);
11618 inline bool
11619 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
11621 tree t1 = p1->val;
11622 tree t2 = p2->val;
11623 enum tree_code code = TREE_CODE (t1);
11625 if (TREE_CODE (t2) != code
11626 || TREE_TYPE (t1) != TREE_TYPE (t2))
11627 return false;
11629 if (!operand_equal_p (t1, t2, 0))
11630 return false;
11632 /* Only allow them to compare equal if they also hash equal; otherwise
11633 results are nondeterminate, and we fail bootstrap comparison. */
11634 gcc_checking_assert (hash (p1) == hash (p2));
11636 return true;