2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / gimplify.c
blobab9e540e76d488dc43b1c88547146b44622f407b
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "alias.h"
27 #include "backend.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "gimple-predict.h"
31 #include "rtl.h"
32 #include "ssa.h"
33 #include "options.h"
34 #include "fold-const.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "calls.h"
41 #include "emit-rtl.h"
42 #include "varasm.h"
43 #include "stmt.h"
44 #include "expr.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
47 #include "tree-eh.h"
48 #include "gimplify.h"
49 #include "gimple-iterator.h"
50 #include "stor-layout.h"
51 #include "print-tree.h"
52 #include "tree-iterator.h"
53 #include "tree-inline.h"
54 #include "tree-pretty-print.h"
55 #include "langhooks.h"
56 #include "cgraph.h"
57 #include "tree-cfg.h"
58 #include "tree-ssa.h"
59 #include "diagnostic-core.h"
60 #include "target.h"
61 #include "splay-tree.h"
62 #include "omp-low.h"
63 #include "gimple-low.h"
64 #include "cilk.h"
65 #include "gomp-constants.h"
66 #include "tree-dump.h"
68 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
69 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
70 #include "builtins.h"
72 enum gimplify_omp_var_data
74 GOVD_SEEN = 1,
75 GOVD_EXPLICIT = 2,
76 GOVD_SHARED = 4,
77 GOVD_PRIVATE = 8,
78 GOVD_FIRSTPRIVATE = 16,
79 GOVD_LASTPRIVATE = 32,
80 GOVD_REDUCTION = 64,
81 GOVD_LOCAL = 128,
82 GOVD_MAP = 256,
83 GOVD_DEBUG_PRIVATE = 512,
84 GOVD_PRIVATE_OUTER_REF = 1024,
85 GOVD_LINEAR = 2048,
86 GOVD_ALIGNED = 4096,
88 /* Flag for GOVD_MAP: don't copy back. */
89 GOVD_MAP_TO_ONLY = 8192,
91 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
92 GOVD_LINEAR_LASTPRIVATE_NO_OUTER = 16384,
94 GOVD_MAP_0LEN_ARRAY = 32768,
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 = 0,
105 ORT_SIMD = 1,
106 ORT_PARALLEL = 2,
107 ORT_COMBINED_PARALLEL = 3,
108 ORT_TASK = 4,
109 ORT_UNTIED_TASK = 5,
110 ORT_TEAMS = 8,
111 ORT_COMBINED_TEAMS = 9,
112 /* Data region. */
113 ORT_TARGET_DATA = 16,
114 /* Data region with offloading. */
115 ORT_TARGET = 32,
116 ORT_COMBINED_TARGET = 33,
117 /* Dummy OpenMP region, used to disable expansion of
118 DECL_VALUE_EXPRs in taskloop pre body. */
119 ORT_NONE = 64
122 /* Gimplify hashtable helper. */
124 struct gimplify_hasher : free_ptr_hash <elt_t>
126 static inline hashval_t hash (const elt_t *);
127 static inline bool equal (const elt_t *, const elt_t *);
130 struct gimplify_ctx
132 struct gimplify_ctx *prev_context;
134 vec<gbind *> bind_expr_stack;
135 tree temps;
136 gimple_seq conditional_cleanups;
137 tree exit_label;
138 tree return_temp;
140 vec<tree> case_labels;
141 /* The formal temporary table. Should this be persistent? */
142 hash_table<gimplify_hasher> *temp_htab;
144 int conditions;
145 bool save_stack;
146 bool into_ssa;
147 bool allow_rhs_cond_expr;
148 bool in_cleanup_point_expr;
151 struct gimplify_omp_ctx
153 struct gimplify_omp_ctx *outer_context;
154 splay_tree variables;
155 hash_set<tree> *privatized_types;
156 /* Iteration variables in an OMP_FOR. */
157 vec<tree> loop_iter_var;
158 location_t location;
159 enum omp_clause_default_kind default_kind;
160 enum omp_region_type region_type;
161 bool combined_loop;
162 bool distribute;
163 bool target_map_scalars_firstprivate;
164 bool target_map_pointers_as_0len_arrays;
165 bool target_firstprivatize_array_bases;
168 static struct gimplify_ctx *gimplify_ctxp;
169 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
171 /* Forward declaration. */
172 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
174 /* Shorter alias name for the above function for use in gimplify.c
175 only. */
177 static inline void
178 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
180 gimple_seq_add_stmt_without_update (seq_p, gs);
183 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
184 NULL, a new sequence is allocated. This function is
185 similar to gimple_seq_add_seq, but does not scan the operands.
186 During gimplification, we need to manipulate statement sequences
187 before the def/use vectors have been constructed. */
189 static void
190 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
192 gimple_stmt_iterator si;
194 if (src == NULL)
195 return;
197 si = gsi_last (*dst_p);
198 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
202 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
203 and popping gimplify contexts. */
205 static struct gimplify_ctx *ctx_pool = NULL;
207 /* Return a gimplify context struct from the pool. */
209 static inline struct gimplify_ctx *
210 ctx_alloc (void)
212 struct gimplify_ctx * c = ctx_pool;
214 if (c)
215 ctx_pool = c->prev_context;
216 else
217 c = XNEW (struct gimplify_ctx);
219 memset (c, '\0', sizeof (*c));
220 return c;
223 /* Put gimplify context C back into the pool. */
225 static inline void
226 ctx_free (struct gimplify_ctx *c)
228 c->prev_context = ctx_pool;
229 ctx_pool = c;
232 /* Free allocated ctx stack memory. */
234 void
235 free_gimplify_stack (void)
237 struct gimplify_ctx *c;
239 while ((c = ctx_pool))
241 ctx_pool = c->prev_context;
242 free (c);
247 /* Set up a context for the gimplifier. */
249 void
250 push_gimplify_context (bool in_ssa, bool rhs_cond_ok)
252 struct gimplify_ctx *c = ctx_alloc ();
254 c->prev_context = gimplify_ctxp;
255 gimplify_ctxp = c;
256 gimplify_ctxp->into_ssa = in_ssa;
257 gimplify_ctxp->allow_rhs_cond_expr = rhs_cond_ok;
260 /* Tear down a context for the gimplifier. If BODY is non-null, then
261 put the temporaries into the outer BIND_EXPR. Otherwise, put them
262 in the local_decls.
264 BODY is not a sequence, but the first tuple in a sequence. */
266 void
267 pop_gimplify_context (gimple *body)
269 struct gimplify_ctx *c = gimplify_ctxp;
271 gcc_assert (c
272 && (!c->bind_expr_stack.exists ()
273 || c->bind_expr_stack.is_empty ()));
274 c->bind_expr_stack.release ();
275 gimplify_ctxp = c->prev_context;
277 if (body)
278 declare_vars (c->temps, body, false);
279 else
280 record_vars (c->temps);
282 delete c->temp_htab;
283 c->temp_htab = NULL;
284 ctx_free (c);
287 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
289 static void
290 gimple_push_bind_expr (gbind *bind_stmt)
292 gimplify_ctxp->bind_expr_stack.reserve (8);
293 gimplify_ctxp->bind_expr_stack.safe_push (bind_stmt);
296 /* Pop the first element off the stack of bindings. */
298 static void
299 gimple_pop_bind_expr (void)
301 gimplify_ctxp->bind_expr_stack.pop ();
304 /* Return the first element of the stack of bindings. */
306 gbind *
307 gimple_current_bind_expr (void)
309 return gimplify_ctxp->bind_expr_stack.last ();
312 /* Return the stack of bindings created during gimplification. */
314 vec<gbind *>
315 gimple_bind_expr_stack (void)
317 return gimplify_ctxp->bind_expr_stack;
320 /* Return true iff there is a COND_EXPR between us and the innermost
321 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
323 static bool
324 gimple_conditional_context (void)
326 return gimplify_ctxp->conditions > 0;
329 /* Note that we've entered a COND_EXPR. */
331 static void
332 gimple_push_condition (void)
334 #ifdef ENABLE_GIMPLE_CHECKING
335 if (gimplify_ctxp->conditions == 0)
336 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
337 #endif
338 ++(gimplify_ctxp->conditions);
341 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
342 now, add any conditional cleanups we've seen to the prequeue. */
344 static void
345 gimple_pop_condition (gimple_seq *pre_p)
347 int conds = --(gimplify_ctxp->conditions);
349 gcc_assert (conds >= 0);
350 if (conds == 0)
352 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
353 gimplify_ctxp->conditional_cleanups = NULL;
357 /* A stable comparison routine for use with splay trees and DECLs. */
359 static int
360 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
362 tree a = (tree) xa;
363 tree b = (tree) xb;
365 return DECL_UID (a) - DECL_UID (b);
368 /* Create a new omp construct that deals with variable remapping. */
370 static struct gimplify_omp_ctx *
371 new_omp_context (enum omp_region_type region_type)
373 struct gimplify_omp_ctx *c;
375 c = XCNEW (struct gimplify_omp_ctx);
376 c->outer_context = gimplify_omp_ctxp;
377 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
378 c->privatized_types = new hash_set<tree>;
379 c->location = input_location;
380 c->region_type = region_type;
381 if ((region_type & ORT_TASK) == 0)
382 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
383 else
384 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
386 return c;
389 /* Destroy an omp construct that deals with variable remapping. */
391 static void
392 delete_omp_context (struct gimplify_omp_ctx *c)
394 splay_tree_delete (c->variables);
395 delete c->privatized_types;
396 c->loop_iter_var.release ();
397 XDELETE (c);
400 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
401 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
403 /* Both gimplify the statement T and append it to *SEQ_P. This function
404 behaves exactly as gimplify_stmt, but you don't have to pass T as a
405 reference. */
407 void
408 gimplify_and_add (tree t, gimple_seq *seq_p)
410 gimplify_stmt (&t, seq_p);
413 /* Gimplify statement T into sequence *SEQ_P, and return the first
414 tuple in the sequence of generated tuples for this statement.
415 Return NULL if gimplifying T produced no tuples. */
417 static gimple *
418 gimplify_and_return_first (tree t, gimple_seq *seq_p)
420 gimple_stmt_iterator last = gsi_last (*seq_p);
422 gimplify_and_add (t, seq_p);
424 if (!gsi_end_p (last))
426 gsi_next (&last);
427 return gsi_stmt (last);
429 else
430 return gimple_seq_first_stmt (*seq_p);
433 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
434 LHS, or for a call argument. */
436 static bool
437 is_gimple_mem_rhs (tree t)
439 /* If we're dealing with a renamable type, either source or dest must be
440 a renamed variable. */
441 if (is_gimple_reg_type (TREE_TYPE (t)))
442 return is_gimple_val (t);
443 else
444 return is_gimple_val (t) || is_gimple_lvalue (t);
447 /* Return true if T is a CALL_EXPR or an expression that can be
448 assigned to a temporary. Note that this predicate should only be
449 used during gimplification. See the rationale for this in
450 gimplify_modify_expr. */
452 static bool
453 is_gimple_reg_rhs_or_call (tree t)
455 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
456 || TREE_CODE (t) == CALL_EXPR);
459 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
460 this predicate should only be used during gimplification. See the
461 rationale for this in gimplify_modify_expr. */
463 static bool
464 is_gimple_mem_rhs_or_call (tree t)
466 /* If we're dealing with a renamable type, either source or dest must be
467 a renamed variable. */
468 if (is_gimple_reg_type (TREE_TYPE (t)))
469 return is_gimple_val (t);
470 else
471 return (is_gimple_val (t) || is_gimple_lvalue (t)
472 || TREE_CODE (t) == CALL_EXPR);
475 /* Create a temporary with a name derived from VAL. Subroutine of
476 lookup_tmp_var; nobody else should call this function. */
478 static inline tree
479 create_tmp_from_val (tree val)
481 /* Drop all qualifiers and address-space information from the value type. */
482 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
483 tree var = create_tmp_var (type, get_name (val));
484 if (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
485 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE)
486 DECL_GIMPLE_REG_P (var) = 1;
487 return var;
490 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
491 an existing expression temporary. */
493 static tree
494 lookup_tmp_var (tree val, bool is_formal)
496 tree ret;
498 /* If not optimizing, never really reuse a temporary. local-alloc
499 won't allocate any variable that is used in more than one basic
500 block, which means it will go into memory, causing much extra
501 work in reload and final and poorer code generation, outweighing
502 the extra memory allocation here. */
503 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
504 ret = create_tmp_from_val (val);
505 else
507 elt_t elt, *elt_p;
508 elt_t **slot;
510 elt.val = val;
511 if (!gimplify_ctxp->temp_htab)
512 gimplify_ctxp->temp_htab = new hash_table<gimplify_hasher> (1000);
513 slot = gimplify_ctxp->temp_htab->find_slot (&elt, INSERT);
514 if (*slot == NULL)
516 elt_p = XNEW (elt_t);
517 elt_p->val = val;
518 elt_p->temp = ret = create_tmp_from_val (val);
519 *slot = elt_p;
521 else
523 elt_p = *slot;
524 ret = elt_p->temp;
528 return ret;
531 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
533 static tree
534 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
535 bool is_formal)
537 tree t, mod;
539 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
540 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
541 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
542 fb_rvalue);
544 if (gimplify_ctxp->into_ssa
545 && is_gimple_reg_type (TREE_TYPE (val)))
546 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)));
547 else
548 t = lookup_tmp_var (val, is_formal);
550 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
552 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_LOC (val, input_location));
554 /* gimplify_modify_expr might want to reduce this further. */
555 gimplify_and_add (mod, pre_p);
556 ggc_free (mod);
558 return t;
561 /* Return a formal temporary variable initialized with VAL. PRE_P is as
562 in gimplify_expr. Only use this function if:
564 1) The value of the unfactored expression represented by VAL will not
565 change between the initialization and use of the temporary, and
566 2) The temporary will not be otherwise modified.
568 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
569 and #2 means it is inappropriate for && temps.
571 For other cases, use get_initialized_tmp_var instead. */
573 tree
574 get_formal_tmp_var (tree val, gimple_seq *pre_p)
576 return internal_get_tmp_var (val, pre_p, NULL, true);
579 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
580 are as in gimplify_expr. */
582 tree
583 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
585 return internal_get_tmp_var (val, pre_p, post_p, false);
588 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
589 generate debug info for them; otherwise don't. */
591 void
592 declare_vars (tree vars, gimple *gs, bool debug_info)
594 tree last = vars;
595 if (last)
597 tree temps, block;
599 gbind *scope = as_a <gbind *> (gs);
601 temps = nreverse (last);
603 block = gimple_bind_block (scope);
604 gcc_assert (!block || TREE_CODE (block) == BLOCK);
605 if (!block || !debug_info)
607 DECL_CHAIN (last) = gimple_bind_vars (scope);
608 gimple_bind_set_vars (scope, temps);
610 else
612 /* We need to attach the nodes both to the BIND_EXPR and to its
613 associated BLOCK for debugging purposes. The key point here
614 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
615 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
616 if (BLOCK_VARS (block))
617 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
618 else
620 gimple_bind_set_vars (scope,
621 chainon (gimple_bind_vars (scope), temps));
622 BLOCK_VARS (block) = temps;
628 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
629 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
630 no such upper bound can be obtained. */
632 static void
633 force_constant_size (tree var)
635 /* The only attempt we make is by querying the maximum size of objects
636 of the variable's type. */
638 HOST_WIDE_INT max_size;
640 gcc_assert (TREE_CODE (var) == VAR_DECL);
642 max_size = max_int_size_in_bytes (TREE_TYPE (var));
644 gcc_assert (max_size >= 0);
646 DECL_SIZE_UNIT (var)
647 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
648 DECL_SIZE (var)
649 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
652 /* Push the temporary variable TMP into the current binding. */
654 void
655 gimple_add_tmp_var_fn (struct function *fn, tree tmp)
657 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
659 /* Later processing assumes that the object size is constant, which might
660 not be true at this point. Force the use of a constant upper bound in
661 this case. */
662 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
663 force_constant_size (tmp);
665 DECL_CONTEXT (tmp) = fn->decl;
666 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
668 record_vars_into (tmp, fn->decl);
671 /* Push the temporary variable TMP into the current binding. */
673 void
674 gimple_add_tmp_var (tree tmp)
676 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
678 /* Later processing assumes that the object size is constant, which might
679 not be true at this point. Force the use of a constant upper bound in
680 this case. */
681 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp)))
682 force_constant_size (tmp);
684 DECL_CONTEXT (tmp) = current_function_decl;
685 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
687 if (gimplify_ctxp)
689 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
690 gimplify_ctxp->temps = tmp;
692 /* Mark temporaries local within the nearest enclosing parallel. */
693 if (gimplify_omp_ctxp)
695 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
696 while (ctx
697 && (ctx->region_type == ORT_WORKSHARE
698 || ctx->region_type == ORT_SIMD))
699 ctx = ctx->outer_context;
700 if (ctx)
701 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
704 else if (cfun)
705 record_vars (tmp);
706 else
708 gimple_seq body_seq;
710 /* This case is for nested functions. We need to expose the locals
711 they create. */
712 body_seq = gimple_body (current_function_decl);
713 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
719 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
720 nodes that are referenced more than once in GENERIC functions. This is
721 necessary because gimplification (translation into GIMPLE) is performed
722 by modifying tree nodes in-place, so gimplication of a shared node in a
723 first context could generate an invalid GIMPLE form in a second context.
725 This is achieved with a simple mark/copy/unmark algorithm that walks the
726 GENERIC representation top-down, marks nodes with TREE_VISITED the first
727 time it encounters them, duplicates them if they already have TREE_VISITED
728 set, and finally removes the TREE_VISITED marks it has set.
730 The algorithm works only at the function level, i.e. it generates a GENERIC
731 representation of a function with no nodes shared within the function when
732 passed a GENERIC function (except for nodes that are allowed to be shared).
734 At the global level, it is also necessary to unshare tree nodes that are
735 referenced in more than one function, for the same aforementioned reason.
736 This requires some cooperation from the front-end. There are 2 strategies:
738 1. Manual unsharing. The front-end needs to call unshare_expr on every
739 expression that might end up being shared across functions.
741 2. Deep unsharing. This is an extension of regular unsharing. Instead
742 of calling unshare_expr on expressions that might be shared across
743 functions, the front-end pre-marks them with TREE_VISITED. This will
744 ensure that they are unshared on the first reference within functions
745 when the regular unsharing algorithm runs. The counterpart is that
746 this algorithm must look deeper than for manual unsharing, which is
747 specified by LANG_HOOKS_DEEP_UNSHARING.
749 If there are only few specific cases of node sharing across functions, it is
750 probably easier for a front-end to unshare the expressions manually. On the
751 contrary, if the expressions generated at the global level are as widespread
752 as expressions generated within functions, deep unsharing is very likely the
753 way to go. */
755 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
756 These nodes model computations that must be done once. If we were to
757 unshare something like SAVE_EXPR(i++), the gimplification process would
758 create wrong code. However, if DATA is non-null, it must hold a pointer
759 set that is used to unshare the subtrees of these nodes. */
761 static tree
762 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
764 tree t = *tp;
765 enum tree_code code = TREE_CODE (t);
767 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
768 copy their subtrees if we can make sure to do it only once. */
769 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
771 if (data && !((hash_set<tree> *)data)->add (t))
773 else
774 *walk_subtrees = 0;
777 /* Stop at types, decls, constants like copy_tree_r. */
778 else if (TREE_CODE_CLASS (code) == tcc_type
779 || TREE_CODE_CLASS (code) == tcc_declaration
780 || TREE_CODE_CLASS (code) == tcc_constant
781 /* We can't do anything sensible with a BLOCK used as an
782 expression, but we also can't just die when we see it
783 because of non-expression uses. So we avert our eyes
784 and cross our fingers. Silly Java. */
785 || code == BLOCK)
786 *walk_subtrees = 0;
788 /* Cope with the statement expression extension. */
789 else if (code == STATEMENT_LIST)
792 /* Leave the bulk of the work to copy_tree_r itself. */
793 else
794 copy_tree_r (tp, walk_subtrees, NULL);
796 return NULL_TREE;
799 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
800 If *TP has been visited already, then *TP is deeply copied by calling
801 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
803 static tree
804 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
806 tree t = *tp;
807 enum tree_code code = TREE_CODE (t);
809 /* Skip types, decls, and constants. But we do want to look at their
810 types and the bounds of types. Mark them as visited so we properly
811 unmark their subtrees on the unmark pass. If we've already seen them,
812 don't look down further. */
813 if (TREE_CODE_CLASS (code) == tcc_type
814 || TREE_CODE_CLASS (code) == tcc_declaration
815 || TREE_CODE_CLASS (code) == tcc_constant)
817 if (TREE_VISITED (t))
818 *walk_subtrees = 0;
819 else
820 TREE_VISITED (t) = 1;
823 /* If this node has been visited already, unshare it and don't look
824 any deeper. */
825 else if (TREE_VISITED (t))
827 walk_tree (tp, mostly_copy_tree_r, data, NULL);
828 *walk_subtrees = 0;
831 /* Otherwise, mark the node as visited and keep looking. */
832 else
833 TREE_VISITED (t) = 1;
835 return NULL_TREE;
838 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
839 copy_if_shared_r callback unmodified. */
841 static inline void
842 copy_if_shared (tree *tp, void *data)
844 walk_tree (tp, copy_if_shared_r, data, NULL);
847 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
848 any nested functions. */
850 static void
851 unshare_body (tree fndecl)
853 struct cgraph_node *cgn = cgraph_node::get (fndecl);
854 /* If the language requires deep unsharing, we need a pointer set to make
855 sure we don't repeatedly unshare subtrees of unshareable nodes. */
856 hash_set<tree> *visited
857 = lang_hooks.deep_unsharing ? new hash_set<tree> : NULL;
859 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
860 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
861 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
863 delete visited;
865 if (cgn)
866 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
867 unshare_body (cgn->decl);
870 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
871 Subtrees are walked until the first unvisited node is encountered. */
873 static tree
874 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
876 tree t = *tp;
878 /* If this node has been visited, unmark it and keep looking. */
879 if (TREE_VISITED (t))
880 TREE_VISITED (t) = 0;
882 /* Otherwise, don't look any deeper. */
883 else
884 *walk_subtrees = 0;
886 return NULL_TREE;
889 /* Unmark the visited trees rooted at *TP. */
891 static inline void
892 unmark_visited (tree *tp)
894 walk_tree (tp, unmark_visited_r, NULL, NULL);
897 /* Likewise, but mark all trees as not visited. */
899 static void
900 unvisit_body (tree fndecl)
902 struct cgraph_node *cgn = cgraph_node::get (fndecl);
904 unmark_visited (&DECL_SAVED_TREE (fndecl));
905 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
906 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
908 if (cgn)
909 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
910 unvisit_body (cgn->decl);
913 /* Unconditionally make an unshared copy of EXPR. This is used when using
914 stored expressions which span multiple functions, such as BINFO_VTABLE,
915 as the normal unsharing process can't tell that they're shared. */
917 tree
918 unshare_expr (tree expr)
920 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
921 return expr;
924 /* Worker for unshare_expr_without_location. */
926 static tree
927 prune_expr_location (tree *tp, int *walk_subtrees, void *)
929 if (EXPR_P (*tp))
930 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
931 else
932 *walk_subtrees = 0;
933 return NULL_TREE;
936 /* Similar to unshare_expr but also prune all expression locations
937 from EXPR. */
939 tree
940 unshare_expr_without_location (tree expr)
942 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
943 if (EXPR_P (expr))
944 walk_tree (&expr, prune_expr_location, NULL, NULL);
945 return expr;
948 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
949 contain statements and have a value. Assign its value to a temporary
950 and give it void_type_node. Return the temporary, or NULL_TREE if
951 WRAPPER was already void. */
953 tree
954 voidify_wrapper_expr (tree wrapper, tree temp)
956 tree type = TREE_TYPE (wrapper);
957 if (type && !VOID_TYPE_P (type))
959 tree *p;
961 /* Set p to point to the body of the wrapper. Loop until we find
962 something that isn't a wrapper. */
963 for (p = &wrapper; p && *p; )
965 switch (TREE_CODE (*p))
967 case BIND_EXPR:
968 TREE_SIDE_EFFECTS (*p) = 1;
969 TREE_TYPE (*p) = void_type_node;
970 /* For a BIND_EXPR, the body is operand 1. */
971 p = &BIND_EXPR_BODY (*p);
972 break;
974 case CLEANUP_POINT_EXPR:
975 case TRY_FINALLY_EXPR:
976 case TRY_CATCH_EXPR:
977 TREE_SIDE_EFFECTS (*p) = 1;
978 TREE_TYPE (*p) = void_type_node;
979 p = &TREE_OPERAND (*p, 0);
980 break;
982 case STATEMENT_LIST:
984 tree_stmt_iterator i = tsi_last (*p);
985 TREE_SIDE_EFFECTS (*p) = 1;
986 TREE_TYPE (*p) = void_type_node;
987 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
989 break;
991 case COMPOUND_EXPR:
992 /* Advance to the last statement. Set all container types to
993 void. */
994 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
996 TREE_SIDE_EFFECTS (*p) = 1;
997 TREE_TYPE (*p) = void_type_node;
999 break;
1001 case TRANSACTION_EXPR:
1002 TREE_SIDE_EFFECTS (*p) = 1;
1003 TREE_TYPE (*p) = void_type_node;
1004 p = &TRANSACTION_EXPR_BODY (*p);
1005 break;
1007 default:
1008 /* Assume that any tree upon which voidify_wrapper_expr is
1009 directly called is a wrapper, and that its body is op0. */
1010 if (p == &wrapper)
1012 TREE_SIDE_EFFECTS (*p) = 1;
1013 TREE_TYPE (*p) = void_type_node;
1014 p = &TREE_OPERAND (*p, 0);
1015 break;
1017 goto out;
1021 out:
1022 if (p == NULL || IS_EMPTY_STMT (*p))
1023 temp = NULL_TREE;
1024 else if (temp)
1026 /* The wrapper is on the RHS of an assignment that we're pushing
1027 down. */
1028 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1029 || TREE_CODE (temp) == MODIFY_EXPR);
1030 TREE_OPERAND (temp, 1) = *p;
1031 *p = temp;
1033 else
1035 temp = create_tmp_var (type, "retval");
1036 *p = build2 (INIT_EXPR, type, temp, *p);
1039 return temp;
1042 return NULL_TREE;
1045 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1046 a temporary through which they communicate. */
1048 static void
1049 build_stack_save_restore (gcall **save, gcall **restore)
1051 tree tmp_var;
1053 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1054 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1055 gimple_call_set_lhs (*save, tmp_var);
1057 *restore
1058 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1059 1, tmp_var);
1062 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1064 static enum gimplify_status
1065 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1067 tree bind_expr = *expr_p;
1068 bool old_save_stack = gimplify_ctxp->save_stack;
1069 tree t;
1070 gbind *bind_stmt;
1071 gimple_seq body, cleanup;
1072 gcall *stack_save;
1073 location_t start_locus = 0, end_locus = 0;
1075 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1077 /* Mark variables seen in this bind expr. */
1078 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1080 if (TREE_CODE (t) == VAR_DECL)
1082 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1084 /* Mark variable as local. */
1085 if (ctx && ctx->region_type != ORT_NONE && !DECL_EXTERNAL (t)
1086 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1087 || splay_tree_lookup (ctx->variables,
1088 (splay_tree_key) t) == NULL))
1090 if (ctx->region_type == ORT_SIMD
1091 && TREE_ADDRESSABLE (t)
1092 && !TREE_STATIC (t))
1093 omp_add_variable (ctx, t, GOVD_PRIVATE | GOVD_SEEN);
1094 else
1095 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1098 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1100 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1101 cfun->has_local_explicit_reg_vars = true;
1104 /* Preliminarily mark non-addressed complex variables as eligible
1105 for promotion to gimple registers. We'll transform their uses
1106 as we find them. */
1107 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1108 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1109 && !TREE_THIS_VOLATILE (t)
1110 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1111 && !needs_to_live_in_memory (t))
1112 DECL_GIMPLE_REG_P (t) = 1;
1115 bind_stmt = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1116 BIND_EXPR_BLOCK (bind_expr));
1117 gimple_push_bind_expr (bind_stmt);
1119 gimplify_ctxp->save_stack = false;
1121 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1122 body = NULL;
1123 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1124 gimple_bind_set_body (bind_stmt, body);
1126 /* Source location wise, the cleanup code (stack_restore and clobbers)
1127 belongs to the end of the block, so propagate what we have. The
1128 stack_save operation belongs to the beginning of block, which we can
1129 infer from the bind_expr directly if the block has no explicit
1130 assignment. */
1131 if (BIND_EXPR_BLOCK (bind_expr))
1133 end_locus = BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1134 start_locus = BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr));
1136 if (start_locus == 0)
1137 start_locus = EXPR_LOCATION (bind_expr);
1139 cleanup = NULL;
1140 stack_save = NULL;
1141 if (gimplify_ctxp->save_stack)
1143 gcall *stack_restore;
1145 /* Save stack on entry and restore it on exit. Add a try_finally
1146 block to achieve this. */
1147 build_stack_save_restore (&stack_save, &stack_restore);
1149 gimple_set_location (stack_save, start_locus);
1150 gimple_set_location (stack_restore, end_locus);
1152 gimplify_seq_add_stmt (&cleanup, stack_restore);
1155 /* Add clobbers for all variables that go out of scope. */
1156 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1158 if (TREE_CODE (t) == VAR_DECL
1159 && !is_global_var (t)
1160 && DECL_CONTEXT (t) == current_function_decl
1161 && !DECL_HARD_REGISTER (t)
1162 && !TREE_THIS_VOLATILE (t)
1163 && !DECL_HAS_VALUE_EXPR_P (t)
1164 /* Only care for variables that have to be in memory. Others
1165 will be rewritten into SSA names, hence moved to the top-level. */
1166 && !is_gimple_reg (t)
1167 && flag_stack_reuse != SR_NONE)
1169 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1170 gimple *clobber_stmt;
1171 TREE_THIS_VOLATILE (clobber) = 1;
1172 clobber_stmt = gimple_build_assign (t, clobber);
1173 gimple_set_location (clobber_stmt, end_locus);
1174 gimplify_seq_add_stmt (&cleanup, clobber_stmt);
1178 if (cleanup)
1180 gtry *gs;
1181 gimple_seq new_body;
1183 new_body = NULL;
1184 gs = gimple_build_try (gimple_bind_body (bind_stmt), cleanup,
1185 GIMPLE_TRY_FINALLY);
1187 if (stack_save)
1188 gimplify_seq_add_stmt (&new_body, stack_save);
1189 gimplify_seq_add_stmt (&new_body, gs);
1190 gimple_bind_set_body (bind_stmt, new_body);
1193 gimplify_ctxp->save_stack = old_save_stack;
1194 gimple_pop_bind_expr ();
1196 gimplify_seq_add_stmt (pre_p, bind_stmt);
1198 if (temp)
1200 *expr_p = temp;
1201 return GS_OK;
1204 *expr_p = NULL_TREE;
1205 return GS_ALL_DONE;
1208 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1209 GIMPLE value, it is assigned to a new temporary and the statement is
1210 re-written to return the temporary.
1212 PRE_P points to the sequence where side effects that must happen before
1213 STMT should be stored. */
1215 static enum gimplify_status
1216 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1218 greturn *ret;
1219 tree ret_expr = TREE_OPERAND (stmt, 0);
1220 tree result_decl, result;
1222 if (ret_expr == error_mark_node)
1223 return GS_ERROR;
1225 /* Implicit _Cilk_sync must be inserted right before any return statement
1226 if there is a _Cilk_spawn in the function. If the user has provided a
1227 _Cilk_sync, the optimizer should remove this duplicate one. */
1228 if (fn_contains_cilk_spawn_p (cfun))
1230 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1231 gimplify_and_add (impl_sync, pre_p);
1234 if (!ret_expr
1235 || TREE_CODE (ret_expr) == RESULT_DECL
1236 || ret_expr == error_mark_node)
1238 greturn *ret = gimple_build_return (ret_expr);
1239 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1240 gimplify_seq_add_stmt (pre_p, ret);
1241 return GS_ALL_DONE;
1244 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1245 result_decl = NULL_TREE;
1246 else
1248 result_decl = TREE_OPERAND (ret_expr, 0);
1250 /* See through a return by reference. */
1251 if (TREE_CODE (result_decl) == INDIRECT_REF)
1252 result_decl = TREE_OPERAND (result_decl, 0);
1254 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1255 || TREE_CODE (ret_expr) == INIT_EXPR)
1256 && TREE_CODE (result_decl) == RESULT_DECL);
1259 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1260 Recall that aggregate_value_p is FALSE for any aggregate type that is
1261 returned in registers. If we're returning values in registers, then
1262 we don't want to extend the lifetime of the RESULT_DECL, particularly
1263 across another call. In addition, for those aggregates for which
1264 hard_function_value generates a PARALLEL, we'll die during normal
1265 expansion of structure assignments; there's special code in expand_return
1266 to handle this case that does not exist in expand_expr. */
1267 if (!result_decl)
1268 result = NULL_TREE;
1269 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1271 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1273 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1274 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1275 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1276 should be effectively allocated by the caller, i.e. all calls to
1277 this function must be subject to the Return Slot Optimization. */
1278 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1279 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1281 result = result_decl;
1283 else if (gimplify_ctxp->return_temp)
1284 result = gimplify_ctxp->return_temp;
1285 else
1287 result = create_tmp_reg (TREE_TYPE (result_decl));
1289 /* ??? With complex control flow (usually involving abnormal edges),
1290 we can wind up warning about an uninitialized value for this. Due
1291 to how this variable is constructed and initialized, this is never
1292 true. Give up and never warn. */
1293 TREE_NO_WARNING (result) = 1;
1295 gimplify_ctxp->return_temp = result;
1298 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1299 Then gimplify the whole thing. */
1300 if (result != result_decl)
1301 TREE_OPERAND (ret_expr, 0) = result;
1303 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1305 ret = gimple_build_return (result);
1306 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1307 gimplify_seq_add_stmt (pre_p, ret);
1309 return GS_ALL_DONE;
1312 /* Gimplify a variable-length array DECL. */
1314 static void
1315 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1317 /* This is a variable-sized decl. Simplify its size and mark it
1318 for deferred expansion. */
1319 tree t, addr, ptr_type;
1321 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1322 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1324 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1325 if (DECL_HAS_VALUE_EXPR_P (decl))
1326 return;
1328 /* All occurrences of this decl in final gimplified code will be
1329 replaced by indirection. Setting DECL_VALUE_EXPR does two
1330 things: First, it lets the rest of the gimplifier know what
1331 replacement to use. Second, it lets the debug info know
1332 where to find the value. */
1333 ptr_type = build_pointer_type (TREE_TYPE (decl));
1334 addr = create_tmp_var (ptr_type, get_name (decl));
1335 DECL_IGNORED_P (addr) = 0;
1336 t = build_fold_indirect_ref (addr);
1337 TREE_THIS_NOTRAP (t) = 1;
1338 SET_DECL_VALUE_EXPR (decl, t);
1339 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1341 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1342 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1343 size_int (DECL_ALIGN (decl)));
1344 /* The call has been built for a variable-sized object. */
1345 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1346 t = fold_convert (ptr_type, t);
1347 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1349 gimplify_and_add (t, seq_p);
1351 /* Indicate that we need to restore the stack level when the
1352 enclosing BIND_EXPR is exited. */
1353 gimplify_ctxp->save_stack = true;
1356 /* A helper function to be called via walk_tree. Mark all labels under *TP
1357 as being forced. To be called for DECL_INITIAL of static variables. */
1359 static tree
1360 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1362 if (TYPE_P (*tp))
1363 *walk_subtrees = 0;
1364 if (TREE_CODE (*tp) == LABEL_DECL)
1365 FORCED_LABEL (*tp) = 1;
1367 return NULL_TREE;
1370 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1371 and initialization explicit. */
1373 static enum gimplify_status
1374 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1376 tree stmt = *stmt_p;
1377 tree decl = DECL_EXPR_DECL (stmt);
1379 *stmt_p = NULL_TREE;
1381 if (TREE_TYPE (decl) == error_mark_node)
1382 return GS_ERROR;
1384 if ((TREE_CODE (decl) == TYPE_DECL
1385 || TREE_CODE (decl) == VAR_DECL)
1386 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1387 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1389 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1390 in case its size expressions contain problematic nodes like CALL_EXPR. */
1391 if (TREE_CODE (decl) == TYPE_DECL
1392 && DECL_ORIGINAL_TYPE (decl)
1393 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1394 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1396 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1398 tree init = DECL_INITIAL (decl);
1400 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1401 || (!TREE_STATIC (decl)
1402 && flag_stack_check == GENERIC_STACK_CHECK
1403 && compare_tree_int (DECL_SIZE_UNIT (decl),
1404 STACK_CHECK_MAX_VAR_SIZE) > 0))
1405 gimplify_vla_decl (decl, seq_p);
1407 /* Some front ends do not explicitly declare all anonymous
1408 artificial variables. We compensate here by declaring the
1409 variables, though it would be better if the front ends would
1410 explicitly declare them. */
1411 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1412 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1413 gimple_add_tmp_var (decl);
1415 if (init && init != error_mark_node)
1417 if (!TREE_STATIC (decl))
1419 DECL_INITIAL (decl) = NULL_TREE;
1420 init = build2 (INIT_EXPR, void_type_node, decl, init);
1421 gimplify_and_add (init, seq_p);
1422 ggc_free (init);
1424 else
1425 /* We must still examine initializers for static variables
1426 as they may contain a label address. */
1427 walk_tree (&init, force_labels_r, NULL, NULL);
1431 return GS_ALL_DONE;
1434 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1435 and replacing the LOOP_EXPR with goto, but if the loop contains an
1436 EXIT_EXPR, we need to append a label for it to jump to. */
1438 static enum gimplify_status
1439 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1441 tree saved_label = gimplify_ctxp->exit_label;
1442 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1444 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1446 gimplify_ctxp->exit_label = NULL_TREE;
1448 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1450 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1452 if (gimplify_ctxp->exit_label)
1453 gimplify_seq_add_stmt (pre_p,
1454 gimple_build_label (gimplify_ctxp->exit_label));
1456 gimplify_ctxp->exit_label = saved_label;
1458 *expr_p = NULL;
1459 return GS_ALL_DONE;
1462 /* Gimplify a statement list onto a sequence. These may be created either
1463 by an enlightened front-end, or by shortcut_cond_expr. */
1465 static enum gimplify_status
1466 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1468 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1470 tree_stmt_iterator i = tsi_start (*expr_p);
1472 while (!tsi_end_p (i))
1474 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1475 tsi_delink (&i);
1478 if (temp)
1480 *expr_p = temp;
1481 return GS_OK;
1484 return GS_ALL_DONE;
1488 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1489 branch to. */
1491 static enum gimplify_status
1492 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1494 tree switch_expr = *expr_p;
1495 gimple_seq switch_body_seq = NULL;
1496 enum gimplify_status ret;
1497 tree index_type = TREE_TYPE (switch_expr);
1498 if (index_type == NULL_TREE)
1499 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1501 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1502 fb_rvalue);
1503 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1504 return ret;
1506 if (SWITCH_BODY (switch_expr))
1508 vec<tree> labels;
1509 vec<tree> saved_labels;
1510 tree default_case = NULL_TREE;
1511 gswitch *switch_stmt;
1513 /* If someone can be bothered to fill in the labels, they can
1514 be bothered to null out the body too. */
1515 gcc_assert (!SWITCH_LABELS (switch_expr));
1517 /* Save old labels, get new ones from body, then restore the old
1518 labels. Save all the things from the switch body to append after. */
1519 saved_labels = gimplify_ctxp->case_labels;
1520 gimplify_ctxp->case_labels.create (8);
1522 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1523 labels = gimplify_ctxp->case_labels;
1524 gimplify_ctxp->case_labels = saved_labels;
1526 preprocess_case_label_vec_for_gimple (labels, index_type,
1527 &default_case);
1529 if (!default_case)
1531 glabel *new_default;
1533 default_case
1534 = build_case_label (NULL_TREE, NULL_TREE,
1535 create_artificial_label (UNKNOWN_LOCATION));
1536 new_default = gimple_build_label (CASE_LABEL (default_case));
1537 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1540 switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
1541 default_case, labels);
1542 gimplify_seq_add_stmt (pre_p, switch_stmt);
1543 gimplify_seq_add_seq (pre_p, switch_body_seq);
1544 labels.release ();
1546 else
1547 gcc_assert (SWITCH_LABELS (switch_expr));
1549 return GS_ALL_DONE;
1552 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1554 static enum gimplify_status
1555 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1557 struct gimplify_ctx *ctxp;
1558 glabel *label_stmt;
1560 /* Invalid programs can play Duff's Device type games with, for example,
1561 #pragma omp parallel. At least in the C front end, we don't
1562 detect such invalid branches until after gimplification, in the
1563 diagnose_omp_blocks pass. */
1564 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1565 if (ctxp->case_labels.exists ())
1566 break;
1568 label_stmt = gimple_build_label (CASE_LABEL (*expr_p));
1569 ctxp->case_labels.safe_push (*expr_p);
1570 gimplify_seq_add_stmt (pre_p, label_stmt);
1572 return GS_ALL_DONE;
1575 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1576 if necessary. */
1578 tree
1579 build_and_jump (tree *label_p)
1581 if (label_p == NULL)
1582 /* If there's nowhere to jump, just fall through. */
1583 return NULL_TREE;
1585 if (*label_p == NULL_TREE)
1587 tree label = create_artificial_label (UNKNOWN_LOCATION);
1588 *label_p = label;
1591 return build1 (GOTO_EXPR, void_type_node, *label_p);
1594 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1595 This also involves building a label to jump to and communicating it to
1596 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1598 static enum gimplify_status
1599 gimplify_exit_expr (tree *expr_p)
1601 tree cond = TREE_OPERAND (*expr_p, 0);
1602 tree expr;
1604 expr = build_and_jump (&gimplify_ctxp->exit_label);
1605 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1606 *expr_p = expr;
1608 return GS_OK;
1611 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1612 different from its canonical type, wrap the whole thing inside a
1613 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1614 type.
1616 The canonical type of a COMPONENT_REF is the type of the field being
1617 referenced--unless the field is a bit-field which can be read directly
1618 in a smaller mode, in which case the canonical type is the
1619 sign-appropriate type corresponding to that mode. */
1621 static void
1622 canonicalize_component_ref (tree *expr_p)
1624 tree expr = *expr_p;
1625 tree type;
1627 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1629 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1630 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1631 else
1632 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1634 /* One could argue that all the stuff below is not necessary for
1635 the non-bitfield case and declare it a FE error if type
1636 adjustment would be needed. */
1637 if (TREE_TYPE (expr) != type)
1639 #ifdef ENABLE_TYPES_CHECKING
1640 tree old_type = TREE_TYPE (expr);
1641 #endif
1642 int type_quals;
1644 /* We need to preserve qualifiers and propagate them from
1645 operand 0. */
1646 type_quals = TYPE_QUALS (type)
1647 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1648 if (TYPE_QUALS (type) != type_quals)
1649 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1651 /* Set the type of the COMPONENT_REF to the underlying type. */
1652 TREE_TYPE (expr) = type;
1654 #ifdef ENABLE_TYPES_CHECKING
1655 /* It is now a FE error, if the conversion from the canonical
1656 type to the original expression type is not useless. */
1657 gcc_assert (useless_type_conversion_p (old_type, type));
1658 #endif
1662 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1663 to foo, embed that change in the ADDR_EXPR by converting
1664 T array[U];
1665 (T *)&array
1667 &array[L]
1668 where L is the lower bound. For simplicity, only do this for constant
1669 lower bound.
1670 The constraint is that the type of &array[L] is trivially convertible
1671 to T *. */
1673 static void
1674 canonicalize_addr_expr (tree *expr_p)
1676 tree expr = *expr_p;
1677 tree addr_expr = TREE_OPERAND (expr, 0);
1678 tree datype, ddatype, pddatype;
1680 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1681 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1682 || TREE_CODE (addr_expr) != ADDR_EXPR)
1683 return;
1685 /* The addr_expr type should be a pointer to an array. */
1686 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1687 if (TREE_CODE (datype) != ARRAY_TYPE)
1688 return;
1690 /* The pointer to element type shall be trivially convertible to
1691 the expression pointer type. */
1692 ddatype = TREE_TYPE (datype);
1693 pddatype = build_pointer_type (ddatype);
1694 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1695 pddatype))
1696 return;
1698 /* The lower bound and element sizes must be constant. */
1699 if (!TYPE_SIZE_UNIT (ddatype)
1700 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1701 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1702 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1703 return;
1705 /* All checks succeeded. Build a new node to merge the cast. */
1706 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1707 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1708 NULL_TREE, NULL_TREE);
1709 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1711 /* We can have stripped a required restrict qualifier above. */
1712 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1713 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1716 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1717 underneath as appropriate. */
1719 static enum gimplify_status
1720 gimplify_conversion (tree *expr_p)
1722 location_t loc = EXPR_LOCATION (*expr_p);
1723 gcc_assert (CONVERT_EXPR_P (*expr_p));
1725 /* Then strip away all but the outermost conversion. */
1726 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1728 /* And remove the outermost conversion if it's useless. */
1729 if (tree_ssa_useless_type_conversion (*expr_p))
1730 *expr_p = TREE_OPERAND (*expr_p, 0);
1732 /* If we still have a conversion at the toplevel,
1733 then canonicalize some constructs. */
1734 if (CONVERT_EXPR_P (*expr_p))
1736 tree sub = TREE_OPERAND (*expr_p, 0);
1738 /* If a NOP conversion is changing the type of a COMPONENT_REF
1739 expression, then canonicalize its type now in order to expose more
1740 redundant conversions. */
1741 if (TREE_CODE (sub) == COMPONENT_REF)
1742 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1744 /* If a NOP conversion is changing a pointer to array of foo
1745 to a pointer to foo, embed that change in the ADDR_EXPR. */
1746 else if (TREE_CODE (sub) == ADDR_EXPR)
1747 canonicalize_addr_expr (expr_p);
1750 /* If we have a conversion to a non-register type force the
1751 use of a VIEW_CONVERT_EXPR instead. */
1752 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1753 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1754 TREE_OPERAND (*expr_p, 0));
1756 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1757 if (TREE_CODE (*expr_p) == CONVERT_EXPR)
1758 TREE_SET_CODE (*expr_p, NOP_EXPR);
1760 return GS_OK;
1763 /* Nonlocal VLAs seen in the current function. */
1764 static hash_set<tree> *nonlocal_vlas;
1766 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1767 static tree nonlocal_vla_vars;
1769 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1770 DECL_VALUE_EXPR, and it's worth re-examining things. */
1772 static enum gimplify_status
1773 gimplify_var_or_parm_decl (tree *expr_p)
1775 tree decl = *expr_p;
1777 /* ??? If this is a local variable, and it has not been seen in any
1778 outer BIND_EXPR, then it's probably the result of a duplicate
1779 declaration, for which we've already issued an error. It would
1780 be really nice if the front end wouldn't leak these at all.
1781 Currently the only known culprit is C++ destructors, as seen
1782 in g++.old-deja/g++.jason/binding.C. */
1783 if (TREE_CODE (decl) == VAR_DECL
1784 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1785 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1786 && decl_function_context (decl) == current_function_decl)
1788 gcc_assert (seen_error ());
1789 return GS_ERROR;
1792 /* When within an OMP context, notice uses of variables. */
1793 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1794 return GS_ALL_DONE;
1796 /* If the decl is an alias for another expression, substitute it now. */
1797 if (DECL_HAS_VALUE_EXPR_P (decl))
1799 tree value_expr = DECL_VALUE_EXPR (decl);
1801 /* For referenced nonlocal VLAs add a decl for debugging purposes
1802 to the current function. */
1803 if (TREE_CODE (decl) == VAR_DECL
1804 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1805 && nonlocal_vlas != NULL
1806 && TREE_CODE (value_expr) == INDIRECT_REF
1807 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1808 && decl_function_context (decl) != current_function_decl)
1810 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1811 while (ctx
1812 && (ctx->region_type == ORT_WORKSHARE
1813 || ctx->region_type == ORT_SIMD))
1814 ctx = ctx->outer_context;
1815 if (!ctx && !nonlocal_vlas->add (decl))
1817 tree copy = copy_node (decl);
1819 lang_hooks.dup_lang_specific_decl (copy);
1820 SET_DECL_RTL (copy, 0);
1821 TREE_USED (copy) = 1;
1822 DECL_CHAIN (copy) = nonlocal_vla_vars;
1823 nonlocal_vla_vars = copy;
1824 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1825 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1829 *expr_p = unshare_expr (value_expr);
1830 return GS_OK;
1833 return GS_ALL_DONE;
1836 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1838 static void
1839 recalculate_side_effects (tree t)
1841 enum tree_code code = TREE_CODE (t);
1842 int len = TREE_OPERAND_LENGTH (t);
1843 int i;
1845 switch (TREE_CODE_CLASS (code))
1847 case tcc_expression:
1848 switch (code)
1850 case INIT_EXPR:
1851 case MODIFY_EXPR:
1852 case VA_ARG_EXPR:
1853 case PREDECREMENT_EXPR:
1854 case PREINCREMENT_EXPR:
1855 case POSTDECREMENT_EXPR:
1856 case POSTINCREMENT_EXPR:
1857 /* All of these have side-effects, no matter what their
1858 operands are. */
1859 return;
1861 default:
1862 break;
1864 /* Fall through. */
1866 case tcc_comparison: /* a comparison expression */
1867 case tcc_unary: /* a unary arithmetic expression */
1868 case tcc_binary: /* a binary arithmetic expression */
1869 case tcc_reference: /* a reference */
1870 case tcc_vl_exp: /* a function call */
1871 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
1872 for (i = 0; i < len; ++i)
1874 tree op = TREE_OPERAND (t, i);
1875 if (op && TREE_SIDE_EFFECTS (op))
1876 TREE_SIDE_EFFECTS (t) = 1;
1878 break;
1880 case tcc_constant:
1881 /* No side-effects. */
1882 return;
1884 default:
1885 gcc_unreachable ();
1889 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1890 node *EXPR_P.
1892 compound_lval
1893 : min_lval '[' val ']'
1894 | min_lval '.' ID
1895 | compound_lval '[' val ']'
1896 | compound_lval '.' ID
1898 This is not part of the original SIMPLE definition, which separates
1899 array and member references, but it seems reasonable to handle them
1900 together. Also, this way we don't run into problems with union
1901 aliasing; gcc requires that for accesses through a union to alias, the
1902 union reference must be explicit, which was not always the case when we
1903 were splitting up array and member refs.
1905 PRE_P points to the sequence where side effects that must happen before
1906 *EXPR_P should be stored.
1908 POST_P points to the sequence where side effects that must happen after
1909 *EXPR_P should be stored. */
1911 static enum gimplify_status
1912 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1913 fallback_t fallback)
1915 tree *p;
1916 enum gimplify_status ret = GS_ALL_DONE, tret;
1917 int i;
1918 location_t loc = EXPR_LOCATION (*expr_p);
1919 tree expr = *expr_p;
1921 /* Create a stack of the subexpressions so later we can walk them in
1922 order from inner to outer. */
1923 auto_vec<tree, 10> expr_stack;
1925 /* We can handle anything that get_inner_reference can deal with. */
1926 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1928 restart:
1929 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1930 if (TREE_CODE (*p) == INDIRECT_REF)
1931 *p = fold_indirect_ref_loc (loc, *p);
1933 if (handled_component_p (*p))
1935 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1936 additional COMPONENT_REFs. */
1937 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1938 && gimplify_var_or_parm_decl (p) == GS_OK)
1939 goto restart;
1940 else
1941 break;
1943 expr_stack.safe_push (*p);
1946 gcc_assert (expr_stack.length ());
1948 /* Now EXPR_STACK is a stack of pointers to all the refs we've
1949 walked through and P points to the innermost expression.
1951 Java requires that we elaborated nodes in source order. That
1952 means we must gimplify the inner expression followed by each of
1953 the indices, in order. But we can't gimplify the inner
1954 expression until we deal with any variable bounds, sizes, or
1955 positions in order to deal with PLACEHOLDER_EXPRs.
1957 So we do this in three steps. First we deal with the annotations
1958 for any variables in the components, then we gimplify the base,
1959 then we gimplify any indices, from left to right. */
1960 for (i = expr_stack.length () - 1; i >= 0; i--)
1962 tree t = expr_stack[i];
1964 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1966 /* Gimplify the low bound and element type size and put them into
1967 the ARRAY_REF. If these values are set, they have already been
1968 gimplified. */
1969 if (TREE_OPERAND (t, 2) == NULL_TREE)
1971 tree low = unshare_expr (array_ref_low_bound (t));
1972 if (!is_gimple_min_invariant (low))
1974 TREE_OPERAND (t, 2) = low;
1975 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1976 post_p, is_gimple_reg,
1977 fb_rvalue);
1978 ret = MIN (ret, tret);
1981 else
1983 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1984 is_gimple_reg, fb_rvalue);
1985 ret = MIN (ret, tret);
1988 if (TREE_OPERAND (t, 3) == NULL_TREE)
1990 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1991 tree elmt_size = unshare_expr (array_ref_element_size (t));
1992 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1994 /* Divide the element size by the alignment of the element
1995 type (above). */
1996 elmt_size
1997 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
1999 if (!is_gimple_min_invariant (elmt_size))
2001 TREE_OPERAND (t, 3) = elmt_size;
2002 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2003 post_p, is_gimple_reg,
2004 fb_rvalue);
2005 ret = MIN (ret, tret);
2008 else
2010 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2011 is_gimple_reg, fb_rvalue);
2012 ret = MIN (ret, tret);
2015 else if (TREE_CODE (t) == COMPONENT_REF)
2017 /* Set the field offset into T and gimplify it. */
2018 if (TREE_OPERAND (t, 2) == NULL_TREE)
2020 tree offset = unshare_expr (component_ref_field_offset (t));
2021 tree field = TREE_OPERAND (t, 1);
2022 tree factor
2023 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2025 /* Divide the offset by its alignment. */
2026 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2028 if (!is_gimple_min_invariant (offset))
2030 TREE_OPERAND (t, 2) = offset;
2031 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2032 post_p, is_gimple_reg,
2033 fb_rvalue);
2034 ret = MIN (ret, tret);
2037 else
2039 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2040 is_gimple_reg, fb_rvalue);
2041 ret = MIN (ret, tret);
2046 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2047 so as to match the min_lval predicate. Failure to do so may result
2048 in the creation of large aggregate temporaries. */
2049 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2050 fallback | fb_lvalue);
2051 ret = MIN (ret, tret);
2053 /* And finally, the indices and operands of ARRAY_REF. During this
2054 loop we also remove any useless conversions. */
2055 for (; expr_stack.length () > 0; )
2057 tree t = expr_stack.pop ();
2059 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2061 /* Gimplify the dimension. */
2062 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2064 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2065 is_gimple_val, fb_rvalue);
2066 ret = MIN (ret, tret);
2070 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2072 /* The innermost expression P may have originally had
2073 TREE_SIDE_EFFECTS set which would have caused all the outer
2074 expressions in *EXPR_P leading to P to also have had
2075 TREE_SIDE_EFFECTS set. */
2076 recalculate_side_effects (t);
2079 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2080 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2082 canonicalize_component_ref (expr_p);
2085 expr_stack.release ();
2087 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2089 return ret;
2092 /* Gimplify the self modifying expression pointed to by EXPR_P
2093 (++, --, +=, -=).
2095 PRE_P points to the list where side effects that must happen before
2096 *EXPR_P should be stored.
2098 POST_P points to the list where side effects that must happen after
2099 *EXPR_P should be stored.
2101 WANT_VALUE is nonzero iff we want to use the value of this expression
2102 in another expression.
2104 ARITH_TYPE is the type the computation should be performed in. */
2106 enum gimplify_status
2107 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2108 bool want_value, tree arith_type)
2110 enum tree_code code;
2111 tree lhs, lvalue, rhs, t1;
2112 gimple_seq post = NULL, *orig_post_p = post_p;
2113 bool postfix;
2114 enum tree_code arith_code;
2115 enum gimplify_status ret;
2116 location_t loc = EXPR_LOCATION (*expr_p);
2118 code = TREE_CODE (*expr_p);
2120 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2121 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2123 /* Prefix or postfix? */
2124 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2125 /* Faster to treat as prefix if result is not used. */
2126 postfix = want_value;
2127 else
2128 postfix = false;
2130 /* For postfix, make sure the inner expression's post side effects
2131 are executed after side effects from this expression. */
2132 if (postfix)
2133 post_p = &post;
2135 /* Add or subtract? */
2136 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2137 arith_code = PLUS_EXPR;
2138 else
2139 arith_code = MINUS_EXPR;
2141 /* Gimplify the LHS into a GIMPLE lvalue. */
2142 lvalue = TREE_OPERAND (*expr_p, 0);
2143 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2144 if (ret == GS_ERROR)
2145 return ret;
2147 /* Extract the operands to the arithmetic operation. */
2148 lhs = lvalue;
2149 rhs = TREE_OPERAND (*expr_p, 1);
2151 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2152 that as the result value and in the postqueue operation. */
2153 if (postfix)
2155 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2156 if (ret == GS_ERROR)
2157 return ret;
2159 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2162 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2163 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2165 rhs = convert_to_ptrofftype_loc (loc, rhs);
2166 if (arith_code == MINUS_EXPR)
2167 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2168 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2170 else
2171 t1 = fold_convert (TREE_TYPE (*expr_p),
2172 fold_build2 (arith_code, arith_type,
2173 fold_convert (arith_type, lhs),
2174 fold_convert (arith_type, rhs)));
2176 if (postfix)
2178 gimplify_assign (lvalue, t1, pre_p);
2179 gimplify_seq_add_seq (orig_post_p, post);
2180 *expr_p = lhs;
2181 return GS_ALL_DONE;
2183 else
2185 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2186 return GS_OK;
2190 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2192 static void
2193 maybe_with_size_expr (tree *expr_p)
2195 tree expr = *expr_p;
2196 tree type = TREE_TYPE (expr);
2197 tree size;
2199 /* If we've already wrapped this or the type is error_mark_node, we can't do
2200 anything. */
2201 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2202 || type == error_mark_node)
2203 return;
2205 /* If the size isn't known or is a constant, we have nothing to do. */
2206 size = TYPE_SIZE_UNIT (type);
2207 if (!size || TREE_CODE (size) == INTEGER_CST)
2208 return;
2210 /* Otherwise, make a WITH_SIZE_EXPR. */
2211 size = unshare_expr (size);
2212 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2213 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2216 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2217 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2218 the CALL_EXPR. */
2220 enum gimplify_status
2221 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2223 bool (*test) (tree);
2224 fallback_t fb;
2226 /* In general, we allow lvalues for function arguments to avoid
2227 extra overhead of copying large aggregates out of even larger
2228 aggregates into temporaries only to copy the temporaries to
2229 the argument list. Make optimizers happy by pulling out to
2230 temporaries those types that fit in registers. */
2231 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2232 test = is_gimple_val, fb = fb_rvalue;
2233 else
2235 test = is_gimple_lvalue, fb = fb_either;
2236 /* Also strip a TARGET_EXPR that would force an extra copy. */
2237 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2239 tree init = TARGET_EXPR_INITIAL (*arg_p);
2240 if (init
2241 && !VOID_TYPE_P (TREE_TYPE (init)))
2242 *arg_p = init;
2246 /* If this is a variable sized type, we must remember the size. */
2247 maybe_with_size_expr (arg_p);
2249 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2250 /* Make sure arguments have the same location as the function call
2251 itself. */
2252 protected_set_expr_location (*arg_p, call_location);
2254 /* There is a sequence point before a function call. Side effects in
2255 the argument list must occur before the actual call. So, when
2256 gimplifying arguments, force gimplify_expr to use an internal
2257 post queue which is then appended to the end of PRE_P. */
2258 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2261 /* Don't fold inside offloading or taskreg regions: it can break code by
2262 adding decl references that weren't in the source. We'll do it during
2263 omplower pass instead. */
2265 static bool
2266 maybe_fold_stmt (gimple_stmt_iterator *gsi)
2268 struct gimplify_omp_ctx *ctx;
2269 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2270 if ((ctx->region_type & (ORT_TARGET | ORT_PARALLEL | ORT_TASK)) != 0)
2271 return false;
2272 return fold_stmt (gsi);
2275 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2276 WANT_VALUE is true if the result of the call is desired. */
2278 static enum gimplify_status
2279 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2281 tree fndecl, parms, p, fnptrtype;
2282 enum gimplify_status ret;
2283 int i, nargs;
2284 gcall *call;
2285 bool builtin_va_start_p = false;
2286 location_t loc = EXPR_LOCATION (*expr_p);
2288 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2290 /* For reliable diagnostics during inlining, it is necessary that
2291 every call_expr be annotated with file and line. */
2292 if (! EXPR_HAS_LOCATION (*expr_p))
2293 SET_EXPR_LOCATION (*expr_p, input_location);
2295 /* Gimplify internal functions created in the FEs. */
2296 if (CALL_EXPR_FN (*expr_p) == NULL_TREE)
2298 if (want_value)
2299 return GS_ALL_DONE;
2301 nargs = call_expr_nargs (*expr_p);
2302 enum internal_fn ifn = CALL_EXPR_IFN (*expr_p);
2303 auto_vec<tree> vargs (nargs);
2305 for (i = 0; i < nargs; i++)
2307 gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2308 EXPR_LOCATION (*expr_p));
2309 vargs.quick_push (CALL_EXPR_ARG (*expr_p, i));
2311 gimple *call = gimple_build_call_internal_vec (ifn, vargs);
2312 gimplify_seq_add_stmt (pre_p, call);
2313 return GS_ALL_DONE;
2316 /* This may be a call to a builtin function.
2318 Builtin function calls may be transformed into different
2319 (and more efficient) builtin function calls under certain
2320 circumstances. Unfortunately, gimplification can muck things
2321 up enough that the builtin expanders are not aware that certain
2322 transformations are still valid.
2324 So we attempt transformation/gimplification of the call before
2325 we gimplify the CALL_EXPR. At this time we do not manage to
2326 transform all calls in the same manner as the expanders do, but
2327 we do transform most of them. */
2328 fndecl = get_callee_fndecl (*expr_p);
2329 if (fndecl
2330 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2331 switch (DECL_FUNCTION_CODE (fndecl))
2333 case BUILT_IN_VA_START:
2335 builtin_va_start_p = TRUE;
2336 if (call_expr_nargs (*expr_p) < 2)
2338 error ("too few arguments to function %<va_start%>");
2339 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2340 return GS_OK;
2343 if (fold_builtin_next_arg (*expr_p, true))
2345 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2346 return GS_OK;
2348 break;
2350 case BUILT_IN_LINE:
2352 *expr_p = build_int_cst (TREE_TYPE (*expr_p),
2353 LOCATION_LINE (EXPR_LOCATION (*expr_p)));
2354 return GS_OK;
2356 case BUILT_IN_FILE:
2358 const char *locfile = LOCATION_FILE (EXPR_LOCATION (*expr_p));
2359 *expr_p = build_string_literal (strlen (locfile) + 1, locfile);
2360 return GS_OK;
2362 case BUILT_IN_FUNCTION:
2364 const char *function;
2365 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2366 *expr_p = build_string_literal (strlen (function) + 1, function);
2367 return GS_OK;
2369 default:
2372 if (fndecl && DECL_BUILT_IN (fndecl))
2374 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2375 if (new_tree && new_tree != *expr_p)
2377 /* There was a transformation of this call which computes the
2378 same value, but in a more efficient way. Return and try
2379 again. */
2380 *expr_p = new_tree;
2381 return GS_OK;
2385 /* Remember the original function pointer type. */
2386 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2388 /* There is a sequence point before the call, so any side effects in
2389 the calling expression must occur before the actual call. Force
2390 gimplify_expr to use an internal post queue. */
2391 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2392 is_gimple_call_addr, fb_rvalue);
2394 nargs = call_expr_nargs (*expr_p);
2396 /* Get argument types for verification. */
2397 fndecl = get_callee_fndecl (*expr_p);
2398 parms = NULL_TREE;
2399 if (fndecl)
2400 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2401 else
2402 parms = TYPE_ARG_TYPES (TREE_TYPE (fnptrtype));
2404 if (fndecl && DECL_ARGUMENTS (fndecl))
2405 p = DECL_ARGUMENTS (fndecl);
2406 else if (parms)
2407 p = parms;
2408 else
2409 p = NULL_TREE;
2410 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2413 /* If the last argument is __builtin_va_arg_pack () and it is not
2414 passed as a named argument, decrease the number of CALL_EXPR
2415 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2416 if (!p
2417 && i < nargs
2418 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2420 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2421 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2423 if (last_arg_fndecl
2424 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2425 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2426 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2428 tree call = *expr_p;
2430 --nargs;
2431 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2432 CALL_EXPR_FN (call),
2433 nargs, CALL_EXPR_ARGP (call));
2435 /* Copy all CALL_EXPR flags, location and block, except
2436 CALL_EXPR_VA_ARG_PACK flag. */
2437 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2438 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2439 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2440 = CALL_EXPR_RETURN_SLOT_OPT (call);
2441 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2442 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2444 /* Set CALL_EXPR_VA_ARG_PACK. */
2445 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2449 /* Gimplify the function arguments. */
2450 if (nargs > 0)
2452 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2453 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2454 PUSH_ARGS_REVERSED ? i-- : i++)
2456 enum gimplify_status t;
2458 /* Avoid gimplifying the second argument to va_start, which needs to
2459 be the plain PARM_DECL. */
2460 if ((i != 1) || !builtin_va_start_p)
2462 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2463 EXPR_LOCATION (*expr_p));
2465 if (t == GS_ERROR)
2466 ret = GS_ERROR;
2471 /* Gimplify the static chain. */
2472 if (CALL_EXPR_STATIC_CHAIN (*expr_p))
2474 if (fndecl && !DECL_STATIC_CHAIN (fndecl))
2475 CALL_EXPR_STATIC_CHAIN (*expr_p) = NULL;
2476 else
2478 enum gimplify_status t;
2479 t = gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p,
2480 EXPR_LOCATION (*expr_p));
2481 if (t == GS_ERROR)
2482 ret = GS_ERROR;
2486 /* Verify the function result. */
2487 if (want_value && fndecl
2488 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2490 error_at (loc, "using result of function returning %<void%>");
2491 ret = GS_ERROR;
2494 /* Try this again in case gimplification exposed something. */
2495 if (ret != GS_ERROR)
2497 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2499 if (new_tree && new_tree != *expr_p)
2501 /* There was a transformation of this call which computes the
2502 same value, but in a more efficient way. Return and try
2503 again. */
2504 *expr_p = new_tree;
2505 return GS_OK;
2508 else
2510 *expr_p = error_mark_node;
2511 return GS_ERROR;
2514 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2515 decl. This allows us to eliminate redundant or useless
2516 calls to "const" functions. */
2517 if (TREE_CODE (*expr_p) == CALL_EXPR)
2519 int flags = call_expr_flags (*expr_p);
2520 if (flags & (ECF_CONST | ECF_PURE)
2521 /* An infinite loop is considered a side effect. */
2522 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2523 TREE_SIDE_EFFECTS (*expr_p) = 0;
2526 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2527 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2528 form and delegate the creation of a GIMPLE_CALL to
2529 gimplify_modify_expr. This is always possible because when
2530 WANT_VALUE is true, the caller wants the result of this call into
2531 a temporary, which means that we will emit an INIT_EXPR in
2532 internal_get_tmp_var which will then be handled by
2533 gimplify_modify_expr. */
2534 if (!want_value)
2536 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2537 have to do is replicate it as a GIMPLE_CALL tuple. */
2538 gimple_stmt_iterator gsi;
2539 call = gimple_build_call_from_tree (*expr_p);
2540 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2541 notice_special_calls (call);
2542 gimplify_seq_add_stmt (pre_p, call);
2543 gsi = gsi_last (*pre_p);
2544 maybe_fold_stmt (&gsi);
2545 *expr_p = NULL_TREE;
2547 else
2548 /* Remember the original function type. */
2549 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2550 CALL_EXPR_FN (*expr_p));
2552 return ret;
2555 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2556 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2558 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2559 condition is true or false, respectively. If null, we should generate
2560 our own to skip over the evaluation of this specific expression.
2562 LOCUS is the source location of the COND_EXPR.
2564 This function is the tree equivalent of do_jump.
2566 shortcut_cond_r should only be called by shortcut_cond_expr. */
2568 static tree
2569 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2570 location_t locus)
2572 tree local_label = NULL_TREE;
2573 tree t, expr = NULL;
2575 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2576 retain the shortcut semantics. Just insert the gotos here;
2577 shortcut_cond_expr will append the real blocks later. */
2578 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2580 location_t new_locus;
2582 /* Turn if (a && b) into
2584 if (a); else goto no;
2585 if (b) goto yes; else goto no;
2586 (no:) */
2588 if (false_label_p == NULL)
2589 false_label_p = &local_label;
2591 /* Keep the original source location on the first 'if'. */
2592 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2593 append_to_statement_list (t, &expr);
2595 /* Set the source location of the && on the second 'if'. */
2596 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2597 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2598 new_locus);
2599 append_to_statement_list (t, &expr);
2601 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2603 location_t new_locus;
2605 /* Turn if (a || b) into
2607 if (a) goto yes;
2608 if (b) goto yes; else goto no;
2609 (yes:) */
2611 if (true_label_p == NULL)
2612 true_label_p = &local_label;
2614 /* Keep the original source location on the first 'if'. */
2615 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2616 append_to_statement_list (t, &expr);
2618 /* Set the source location of the || on the second 'if'. */
2619 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2620 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2621 new_locus);
2622 append_to_statement_list (t, &expr);
2624 else if (TREE_CODE (pred) == COND_EXPR
2625 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2626 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2628 location_t new_locus;
2630 /* As long as we're messing with gotos, turn if (a ? b : c) into
2631 if (a)
2632 if (b) goto yes; else goto no;
2633 else
2634 if (c) goto yes; else goto no;
2636 Don't do this if one of the arms has void type, which can happen
2637 in C++ when the arm is throw. */
2639 /* Keep the original source location on the first 'if'. Set the source
2640 location of the ? on the second 'if'. */
2641 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2642 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2643 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2644 false_label_p, locus),
2645 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2646 false_label_p, new_locus));
2648 else
2650 expr = build3 (COND_EXPR, void_type_node, pred,
2651 build_and_jump (true_label_p),
2652 build_and_jump (false_label_p));
2653 SET_EXPR_LOCATION (expr, locus);
2656 if (local_label)
2658 t = build1 (LABEL_EXPR, void_type_node, local_label);
2659 append_to_statement_list (t, &expr);
2662 return expr;
2665 /* Given a conditional expression EXPR with short-circuit boolean
2666 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2667 predicate apart into the equivalent sequence of conditionals. */
2669 static tree
2670 shortcut_cond_expr (tree expr)
2672 tree pred = TREE_OPERAND (expr, 0);
2673 tree then_ = TREE_OPERAND (expr, 1);
2674 tree else_ = TREE_OPERAND (expr, 2);
2675 tree true_label, false_label, end_label, t;
2676 tree *true_label_p;
2677 tree *false_label_p;
2678 bool emit_end, emit_false, jump_over_else;
2679 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2680 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2682 /* First do simple transformations. */
2683 if (!else_se)
2685 /* If there is no 'else', turn
2686 if (a && b) then c
2687 into
2688 if (a) if (b) then c. */
2689 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2691 /* Keep the original source location on the first 'if'. */
2692 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2693 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2694 /* Set the source location of the && on the second 'if'. */
2695 if (EXPR_HAS_LOCATION (pred))
2696 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2697 then_ = shortcut_cond_expr (expr);
2698 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2699 pred = TREE_OPERAND (pred, 0);
2700 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2701 SET_EXPR_LOCATION (expr, locus);
2705 if (!then_se)
2707 /* If there is no 'then', turn
2708 if (a || b); else d
2709 into
2710 if (a); else if (b); else d. */
2711 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2713 /* Keep the original source location on the first 'if'. */
2714 location_t locus = EXPR_LOC_OR_LOC (expr, input_location);
2715 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2716 /* Set the source location of the || on the second 'if'. */
2717 if (EXPR_HAS_LOCATION (pred))
2718 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2719 else_ = shortcut_cond_expr (expr);
2720 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2721 pred = TREE_OPERAND (pred, 0);
2722 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2723 SET_EXPR_LOCATION (expr, locus);
2727 /* If we're done, great. */
2728 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2729 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2730 return expr;
2732 /* Otherwise we need to mess with gotos. Change
2733 if (a) c; else d;
2735 if (a); else goto no;
2736 c; goto end;
2737 no: d; end:
2738 and recursively gimplify the condition. */
2740 true_label = false_label = end_label = NULL_TREE;
2742 /* If our arms just jump somewhere, hijack those labels so we don't
2743 generate jumps to jumps. */
2745 if (then_
2746 && TREE_CODE (then_) == GOTO_EXPR
2747 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2749 true_label = GOTO_DESTINATION (then_);
2750 then_ = NULL;
2751 then_se = false;
2754 if (else_
2755 && TREE_CODE (else_) == GOTO_EXPR
2756 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2758 false_label = GOTO_DESTINATION (else_);
2759 else_ = NULL;
2760 else_se = false;
2763 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2764 if (true_label)
2765 true_label_p = &true_label;
2766 else
2767 true_label_p = NULL;
2769 /* The 'else' branch also needs a label if it contains interesting code. */
2770 if (false_label || else_se)
2771 false_label_p = &false_label;
2772 else
2773 false_label_p = NULL;
2775 /* If there was nothing else in our arms, just forward the label(s). */
2776 if (!then_se && !else_se)
2777 return shortcut_cond_r (pred, true_label_p, false_label_p,
2778 EXPR_LOC_OR_LOC (expr, input_location));
2780 /* If our last subexpression already has a terminal label, reuse it. */
2781 if (else_se)
2782 t = expr_last (else_);
2783 else if (then_se)
2784 t = expr_last (then_);
2785 else
2786 t = NULL;
2787 if (t && TREE_CODE (t) == LABEL_EXPR)
2788 end_label = LABEL_EXPR_LABEL (t);
2790 /* If we don't care about jumping to the 'else' branch, jump to the end
2791 if the condition is false. */
2792 if (!false_label_p)
2793 false_label_p = &end_label;
2795 /* We only want to emit these labels if we aren't hijacking them. */
2796 emit_end = (end_label == NULL_TREE);
2797 emit_false = (false_label == NULL_TREE);
2799 /* We only emit the jump over the else clause if we have to--if the
2800 then clause may fall through. Otherwise we can wind up with a
2801 useless jump and a useless label at the end of gimplified code,
2802 which will cause us to think that this conditional as a whole
2803 falls through even if it doesn't. If we then inline a function
2804 which ends with such a condition, that can cause us to issue an
2805 inappropriate warning about control reaching the end of a
2806 non-void function. */
2807 jump_over_else = block_may_fallthru (then_);
2809 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2810 EXPR_LOC_OR_LOC (expr, input_location));
2812 expr = NULL;
2813 append_to_statement_list (pred, &expr);
2815 append_to_statement_list (then_, &expr);
2816 if (else_se)
2818 if (jump_over_else)
2820 tree last = expr_last (expr);
2821 t = build_and_jump (&end_label);
2822 if (EXPR_HAS_LOCATION (last))
2823 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2824 append_to_statement_list (t, &expr);
2826 if (emit_false)
2828 t = build1 (LABEL_EXPR, void_type_node, false_label);
2829 append_to_statement_list (t, &expr);
2831 append_to_statement_list (else_, &expr);
2833 if (emit_end && end_label)
2835 t = build1 (LABEL_EXPR, void_type_node, end_label);
2836 append_to_statement_list (t, &expr);
2839 return expr;
2842 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2844 tree
2845 gimple_boolify (tree expr)
2847 tree type = TREE_TYPE (expr);
2848 location_t loc = EXPR_LOCATION (expr);
2850 if (TREE_CODE (expr) == NE_EXPR
2851 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2852 && integer_zerop (TREE_OPERAND (expr, 1)))
2854 tree call = TREE_OPERAND (expr, 0);
2855 tree fn = get_callee_fndecl (call);
2857 /* For __builtin_expect ((long) (x), y) recurse into x as well
2858 if x is truth_value_p. */
2859 if (fn
2860 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2861 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2862 && call_expr_nargs (call) == 2)
2864 tree arg = CALL_EXPR_ARG (call, 0);
2865 if (arg)
2867 if (TREE_CODE (arg) == NOP_EXPR
2868 && TREE_TYPE (arg) == TREE_TYPE (call))
2869 arg = TREE_OPERAND (arg, 0);
2870 if (truth_value_p (TREE_CODE (arg)))
2872 arg = gimple_boolify (arg);
2873 CALL_EXPR_ARG (call, 0)
2874 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2880 switch (TREE_CODE (expr))
2882 case TRUTH_AND_EXPR:
2883 case TRUTH_OR_EXPR:
2884 case TRUTH_XOR_EXPR:
2885 case TRUTH_ANDIF_EXPR:
2886 case TRUTH_ORIF_EXPR:
2887 /* Also boolify the arguments of truth exprs. */
2888 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2889 /* FALLTHRU */
2891 case TRUTH_NOT_EXPR:
2892 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2894 /* These expressions always produce boolean results. */
2895 if (TREE_CODE (type) != BOOLEAN_TYPE)
2896 TREE_TYPE (expr) = boolean_type_node;
2897 return expr;
2899 case ANNOTATE_EXPR:
2900 switch ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)))
2902 case annot_expr_ivdep_kind:
2903 case annot_expr_no_vector_kind:
2904 case annot_expr_vector_kind:
2905 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2906 if (TREE_CODE (type) != BOOLEAN_TYPE)
2907 TREE_TYPE (expr) = boolean_type_node;
2908 return expr;
2909 default:
2910 gcc_unreachable ();
2913 default:
2914 if (COMPARISON_CLASS_P (expr))
2916 /* There expressions always prduce boolean results. */
2917 if (TREE_CODE (type) != BOOLEAN_TYPE)
2918 TREE_TYPE (expr) = boolean_type_node;
2919 return expr;
2921 /* Other expressions that get here must have boolean values, but
2922 might need to be converted to the appropriate mode. */
2923 if (TREE_CODE (type) == BOOLEAN_TYPE)
2924 return expr;
2925 return fold_convert_loc (loc, boolean_type_node, expr);
2929 /* Given a conditional expression *EXPR_P without side effects, gimplify
2930 its operands. New statements are inserted to PRE_P. */
2932 static enum gimplify_status
2933 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2935 tree expr = *expr_p, cond;
2936 enum gimplify_status ret, tret;
2937 enum tree_code code;
2939 cond = gimple_boolify (COND_EXPR_COND (expr));
2941 /* We need to handle && and || specially, as their gimplification
2942 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2943 code = TREE_CODE (cond);
2944 if (code == TRUTH_ANDIF_EXPR)
2945 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2946 else if (code == TRUTH_ORIF_EXPR)
2947 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2948 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2949 COND_EXPR_COND (*expr_p) = cond;
2951 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2952 is_gimple_val, fb_rvalue);
2953 ret = MIN (ret, tret);
2954 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2955 is_gimple_val, fb_rvalue);
2957 return MIN (ret, tret);
2960 /* Return true if evaluating EXPR could trap.
2961 EXPR is GENERIC, while tree_could_trap_p can be called
2962 only on GIMPLE. */
2964 static bool
2965 generic_expr_could_trap_p (tree expr)
2967 unsigned i, n;
2969 if (!expr || is_gimple_val (expr))
2970 return false;
2972 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2973 return true;
2975 n = TREE_OPERAND_LENGTH (expr);
2976 for (i = 0; i < n; i++)
2977 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2978 return true;
2980 return false;
2983 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2984 into
2986 if (p) if (p)
2987 t1 = a; a;
2988 else or else
2989 t1 = b; b;
2992 The second form is used when *EXPR_P is of type void.
2994 PRE_P points to the list where side effects that must happen before
2995 *EXPR_P should be stored. */
2997 static enum gimplify_status
2998 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3000 tree expr = *expr_p;
3001 tree type = TREE_TYPE (expr);
3002 location_t loc = EXPR_LOCATION (expr);
3003 tree tmp, arm1, arm2;
3004 enum gimplify_status ret;
3005 tree label_true, label_false, label_cont;
3006 bool have_then_clause_p, have_else_clause_p;
3007 gcond *cond_stmt;
3008 enum tree_code pred_code;
3009 gimple_seq seq = NULL;
3011 /* If this COND_EXPR has a value, copy the values into a temporary within
3012 the arms. */
3013 if (!VOID_TYPE_P (type))
3015 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3016 tree result;
3018 /* If either an rvalue is ok or we do not require an lvalue, create the
3019 temporary. But we cannot do that if the type is addressable. */
3020 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3021 && !TREE_ADDRESSABLE (type))
3023 if (gimplify_ctxp->allow_rhs_cond_expr
3024 /* If either branch has side effects or could trap, it can't be
3025 evaluated unconditionally. */
3026 && !TREE_SIDE_EFFECTS (then_)
3027 && !generic_expr_could_trap_p (then_)
3028 && !TREE_SIDE_EFFECTS (else_)
3029 && !generic_expr_could_trap_p (else_))
3030 return gimplify_pure_cond_expr (expr_p, pre_p);
3032 tmp = create_tmp_var (type, "iftmp");
3033 result = tmp;
3036 /* Otherwise, only create and copy references to the values. */
3037 else
3039 type = build_pointer_type (type);
3041 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3042 then_ = build_fold_addr_expr_loc (loc, then_);
3044 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3045 else_ = build_fold_addr_expr_loc (loc, else_);
3047 expr
3048 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3050 tmp = create_tmp_var (type, "iftmp");
3051 result = build_simple_mem_ref_loc (loc, tmp);
3054 /* Build the new then clause, `tmp = then_;'. But don't build the
3055 assignment if the value is void; in C++ it can be if it's a throw. */
3056 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3057 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3059 /* Similarly, build the new else clause, `tmp = else_;'. */
3060 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3061 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3063 TREE_TYPE (expr) = void_type_node;
3064 recalculate_side_effects (expr);
3066 /* Move the COND_EXPR to the prequeue. */
3067 gimplify_stmt (&expr, pre_p);
3069 *expr_p = result;
3070 return GS_ALL_DONE;
3073 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3074 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3075 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3076 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3078 /* Make sure the condition has BOOLEAN_TYPE. */
3079 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3081 /* Break apart && and || conditions. */
3082 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3083 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3085 expr = shortcut_cond_expr (expr);
3087 if (expr != *expr_p)
3089 *expr_p = expr;
3091 /* We can't rely on gimplify_expr to re-gimplify the expanded
3092 form properly, as cleanups might cause the target labels to be
3093 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3094 set up a conditional context. */
3095 gimple_push_condition ();
3096 gimplify_stmt (expr_p, &seq);
3097 gimple_pop_condition (pre_p);
3098 gimple_seq_add_seq (pre_p, seq);
3100 return GS_ALL_DONE;
3104 /* Now do the normal gimplification. */
3106 /* Gimplify condition. */
3107 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3108 fb_rvalue);
3109 if (ret == GS_ERROR)
3110 return GS_ERROR;
3111 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3113 gimple_push_condition ();
3115 have_then_clause_p = have_else_clause_p = false;
3116 if (TREE_OPERAND (expr, 1) != NULL
3117 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3118 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3119 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3120 == current_function_decl)
3121 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3122 have different locations, otherwise we end up with incorrect
3123 location information on the branches. */
3124 && (optimize
3125 || !EXPR_HAS_LOCATION (expr)
3126 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3127 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3129 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3130 have_then_clause_p = true;
3132 else
3133 label_true = create_artificial_label (UNKNOWN_LOCATION);
3134 if (TREE_OPERAND (expr, 2) != NULL
3135 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3136 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3137 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3138 == current_function_decl)
3139 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3140 have different locations, otherwise we end up with incorrect
3141 location information on the branches. */
3142 && (optimize
3143 || !EXPR_HAS_LOCATION (expr)
3144 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3145 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3147 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3148 have_else_clause_p = true;
3150 else
3151 label_false = create_artificial_label (UNKNOWN_LOCATION);
3153 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3154 &arm2);
3155 cond_stmt = gimple_build_cond (pred_code, arm1, arm2, label_true,
3156 label_false);
3157 gimplify_seq_add_stmt (&seq, cond_stmt);
3158 gimple_stmt_iterator gsi = gsi_last (seq);
3159 maybe_fold_stmt (&gsi);
3161 label_cont = NULL_TREE;
3162 if (!have_then_clause_p)
3164 /* For if (...) {} else { code; } put label_true after
3165 the else block. */
3166 if (TREE_OPERAND (expr, 1) == NULL_TREE
3167 && !have_else_clause_p
3168 && TREE_OPERAND (expr, 2) != NULL_TREE)
3169 label_cont = label_true;
3170 else
3172 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3173 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3174 /* For if (...) { code; } else {} or
3175 if (...) { code; } else goto label; or
3176 if (...) { code; return; } else { ... }
3177 label_cont isn't needed. */
3178 if (!have_else_clause_p
3179 && TREE_OPERAND (expr, 2) != NULL_TREE
3180 && gimple_seq_may_fallthru (seq))
3182 gimple *g;
3183 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3185 g = gimple_build_goto (label_cont);
3187 /* GIMPLE_COND's are very low level; they have embedded
3188 gotos. This particular embedded goto should not be marked
3189 with the location of the original COND_EXPR, as it would
3190 correspond to the COND_EXPR's condition, not the ELSE or the
3191 THEN arms. To avoid marking it with the wrong location, flag
3192 it as "no location". */
3193 gimple_set_do_not_emit_location (g);
3195 gimplify_seq_add_stmt (&seq, g);
3199 if (!have_else_clause_p)
3201 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3202 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3204 if (label_cont)
3205 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3207 gimple_pop_condition (pre_p);
3208 gimple_seq_add_seq (pre_p, seq);
3210 if (ret == GS_ERROR)
3211 ; /* Do nothing. */
3212 else if (have_then_clause_p || have_else_clause_p)
3213 ret = GS_ALL_DONE;
3214 else
3216 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3217 expr = TREE_OPERAND (expr, 0);
3218 gimplify_stmt (&expr, pre_p);
3221 *expr_p = NULL;
3222 return ret;
3225 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3226 to be marked addressable.
3228 We cannot rely on such an expression being directly markable if a temporary
3229 has been created by the gimplification. In this case, we create another
3230 temporary and initialize it with a copy, which will become a store after we
3231 mark it addressable. This can happen if the front-end passed us something
3232 that it could not mark addressable yet, like a Fortran pass-by-reference
3233 parameter (int) floatvar. */
3235 static void
3236 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3238 while (handled_component_p (*expr_p))
3239 expr_p = &TREE_OPERAND (*expr_p, 0);
3240 if (is_gimple_reg (*expr_p))
3242 tree var = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3243 DECL_GIMPLE_REG_P (var) = 0;
3244 *expr_p = var;
3248 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3249 a call to __builtin_memcpy. */
3251 static enum gimplify_status
3252 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3253 gimple_seq *seq_p)
3255 tree t, to, to_ptr, from, from_ptr;
3256 gcall *gs;
3257 location_t loc = EXPR_LOCATION (*expr_p);
3259 to = TREE_OPERAND (*expr_p, 0);
3260 from = TREE_OPERAND (*expr_p, 1);
3262 /* Mark the RHS addressable. Beware that it may not be possible to do so
3263 directly if a temporary has been created by the gimplification. */
3264 prepare_gimple_addressable (&from, seq_p);
3266 mark_addressable (from);
3267 from_ptr = build_fold_addr_expr_loc (loc, from);
3268 gimplify_arg (&from_ptr, seq_p, loc);
3270 mark_addressable (to);
3271 to_ptr = build_fold_addr_expr_loc (loc, to);
3272 gimplify_arg (&to_ptr, seq_p, loc);
3274 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3276 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3278 if (want_value)
3280 /* tmp = memcpy() */
3281 t = create_tmp_var (TREE_TYPE (to_ptr));
3282 gimple_call_set_lhs (gs, t);
3283 gimplify_seq_add_stmt (seq_p, gs);
3285 *expr_p = build_simple_mem_ref (t);
3286 return GS_ALL_DONE;
3289 gimplify_seq_add_stmt (seq_p, gs);
3290 *expr_p = NULL;
3291 return GS_ALL_DONE;
3294 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3295 a call to __builtin_memset. In this case we know that the RHS is
3296 a CONSTRUCTOR with an empty element list. */
3298 static enum gimplify_status
3299 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3300 gimple_seq *seq_p)
3302 tree t, from, to, to_ptr;
3303 gcall *gs;
3304 location_t loc = EXPR_LOCATION (*expr_p);
3306 /* Assert our assumptions, to abort instead of producing wrong code
3307 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3308 not be immediately exposed. */
3309 from = TREE_OPERAND (*expr_p, 1);
3310 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3311 from = TREE_OPERAND (from, 0);
3313 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3314 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3316 /* Now proceed. */
3317 to = TREE_OPERAND (*expr_p, 0);
3319 to_ptr = build_fold_addr_expr_loc (loc, to);
3320 gimplify_arg (&to_ptr, seq_p, loc);
3321 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3323 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3325 if (want_value)
3327 /* tmp = memset() */
3328 t = create_tmp_var (TREE_TYPE (to_ptr));
3329 gimple_call_set_lhs (gs, t);
3330 gimplify_seq_add_stmt (seq_p, gs);
3332 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3333 return GS_ALL_DONE;
3336 gimplify_seq_add_stmt (seq_p, gs);
3337 *expr_p = NULL;
3338 return GS_ALL_DONE;
3341 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3342 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3343 assignment. Return non-null if we detect a potential overlap. */
3345 struct gimplify_init_ctor_preeval_data
3347 /* The base decl of the lhs object. May be NULL, in which case we
3348 have to assume the lhs is indirect. */
3349 tree lhs_base_decl;
3351 /* The alias set of the lhs object. */
3352 alias_set_type lhs_alias_set;
3355 static tree
3356 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3358 struct gimplify_init_ctor_preeval_data *data
3359 = (struct gimplify_init_ctor_preeval_data *) xdata;
3360 tree t = *tp;
3362 /* If we find the base object, obviously we have overlap. */
3363 if (data->lhs_base_decl == t)
3364 return t;
3366 /* If the constructor component is indirect, determine if we have a
3367 potential overlap with the lhs. The only bits of information we
3368 have to go on at this point are addressability and alias sets. */
3369 if ((INDIRECT_REF_P (t)
3370 || TREE_CODE (t) == MEM_REF)
3371 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3372 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3373 return t;
3375 /* If the constructor component is a call, determine if it can hide a
3376 potential overlap with the lhs through an INDIRECT_REF like above.
3377 ??? Ugh - this is completely broken. In fact this whole analysis
3378 doesn't look conservative. */
3379 if (TREE_CODE (t) == CALL_EXPR)
3381 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3383 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3384 if (POINTER_TYPE_P (TREE_VALUE (type))
3385 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3386 && alias_sets_conflict_p (data->lhs_alias_set,
3387 get_alias_set
3388 (TREE_TYPE (TREE_VALUE (type)))))
3389 return t;
3392 if (IS_TYPE_OR_DECL_P (t))
3393 *walk_subtrees = 0;
3394 return NULL;
3397 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3398 force values that overlap with the lhs (as described by *DATA)
3399 into temporaries. */
3401 static void
3402 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3403 struct gimplify_init_ctor_preeval_data *data)
3405 enum gimplify_status one;
3407 /* If the value is constant, then there's nothing to pre-evaluate. */
3408 if (TREE_CONSTANT (*expr_p))
3410 /* Ensure it does not have side effects, it might contain a reference to
3411 the object we're initializing. */
3412 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3413 return;
3416 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3417 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3418 return;
3420 /* Recurse for nested constructors. */
3421 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3423 unsigned HOST_WIDE_INT ix;
3424 constructor_elt *ce;
3425 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3427 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3428 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3430 return;
3433 /* If this is a variable sized type, we must remember the size. */
3434 maybe_with_size_expr (expr_p);
3436 /* Gimplify the constructor element to something appropriate for the rhs
3437 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3438 the gimplifier will consider this a store to memory. Doing this
3439 gimplification now means that we won't have to deal with complicated
3440 language-specific trees, nor trees like SAVE_EXPR that can induce
3441 exponential search behavior. */
3442 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3443 if (one == GS_ERROR)
3445 *expr_p = NULL;
3446 return;
3449 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3450 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3451 always be true for all scalars, since is_gimple_mem_rhs insists on a
3452 temporary variable for them. */
3453 if (DECL_P (*expr_p))
3454 return;
3456 /* If this is of variable size, we have no choice but to assume it doesn't
3457 overlap since we can't make a temporary for it. */
3458 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3459 return;
3461 /* Otherwise, we must search for overlap ... */
3462 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3463 return;
3465 /* ... and if found, force the value into a temporary. */
3466 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3469 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3470 a RANGE_EXPR in a CONSTRUCTOR for an array.
3472 var = lower;
3473 loop_entry:
3474 object[var] = value;
3475 if (var == upper)
3476 goto loop_exit;
3477 var = var + 1;
3478 goto loop_entry;
3479 loop_exit:
3481 We increment var _after_ the loop exit check because we might otherwise
3482 fail if upper == TYPE_MAX_VALUE (type for upper).
3484 Note that we never have to deal with SAVE_EXPRs here, because this has
3485 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3487 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3488 gimple_seq *, bool);
3490 static void
3491 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3492 tree value, tree array_elt_type,
3493 gimple_seq *pre_p, bool cleared)
3495 tree loop_entry_label, loop_exit_label, fall_thru_label;
3496 tree var, var_type, cref, tmp;
3498 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3499 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3500 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3502 /* Create and initialize the index variable. */
3503 var_type = TREE_TYPE (upper);
3504 var = create_tmp_var (var_type);
3505 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3507 /* Add the loop entry label. */
3508 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3510 /* Build the reference. */
3511 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3512 var, NULL_TREE, NULL_TREE);
3514 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3515 the store. Otherwise just assign value to the reference. */
3517 if (TREE_CODE (value) == CONSTRUCTOR)
3518 /* NB we might have to call ourself recursively through
3519 gimplify_init_ctor_eval if the value is a constructor. */
3520 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3521 pre_p, cleared);
3522 else
3523 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3525 /* We exit the loop when the index var is equal to the upper bound. */
3526 gimplify_seq_add_stmt (pre_p,
3527 gimple_build_cond (EQ_EXPR, var, upper,
3528 loop_exit_label, fall_thru_label));
3530 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3532 /* Otherwise, increment the index var... */
3533 tmp = build2 (PLUS_EXPR, var_type, var,
3534 fold_convert (var_type, integer_one_node));
3535 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3537 /* ...and jump back to the loop entry. */
3538 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3540 /* Add the loop exit label. */
3541 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3544 /* Return true if FDECL is accessing a field that is zero sized. */
3546 static bool
3547 zero_sized_field_decl (const_tree fdecl)
3549 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3550 && integer_zerop (DECL_SIZE (fdecl)))
3551 return true;
3552 return false;
3555 /* Return true if TYPE is zero sized. */
3557 static bool
3558 zero_sized_type (const_tree type)
3560 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3561 && integer_zerop (TYPE_SIZE (type)))
3562 return true;
3563 return false;
3566 /* A subroutine of gimplify_init_constructor. Generate individual
3567 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3568 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3569 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3570 zeroed first. */
3572 static void
3573 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3574 gimple_seq *pre_p, bool cleared)
3576 tree array_elt_type = NULL;
3577 unsigned HOST_WIDE_INT ix;
3578 tree purpose, value;
3580 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3581 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3583 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3585 tree cref;
3587 /* NULL values are created above for gimplification errors. */
3588 if (value == NULL)
3589 continue;
3591 if (cleared && initializer_zerop (value))
3592 continue;
3594 /* ??? Here's to hoping the front end fills in all of the indices,
3595 so we don't have to figure out what's missing ourselves. */
3596 gcc_assert (purpose);
3598 /* Skip zero-sized fields, unless value has side-effects. This can
3599 happen with calls to functions returning a zero-sized type, which
3600 we shouldn't discard. As a number of downstream passes don't
3601 expect sets of zero-sized fields, we rely on the gimplification of
3602 the MODIFY_EXPR we make below to drop the assignment statement. */
3603 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3604 continue;
3606 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3607 whole range. */
3608 if (TREE_CODE (purpose) == RANGE_EXPR)
3610 tree lower = TREE_OPERAND (purpose, 0);
3611 tree upper = TREE_OPERAND (purpose, 1);
3613 /* If the lower bound is equal to upper, just treat it as if
3614 upper was the index. */
3615 if (simple_cst_equal (lower, upper))
3616 purpose = upper;
3617 else
3619 gimplify_init_ctor_eval_range (object, lower, upper, value,
3620 array_elt_type, pre_p, cleared);
3621 continue;
3625 if (array_elt_type)
3627 /* Do not use bitsizetype for ARRAY_REF indices. */
3628 if (TYPE_DOMAIN (TREE_TYPE (object)))
3629 purpose
3630 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3631 purpose);
3632 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3633 purpose, NULL_TREE, NULL_TREE);
3635 else
3637 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3638 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3639 unshare_expr (object), purpose, NULL_TREE);
3642 if (TREE_CODE (value) == CONSTRUCTOR
3643 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3644 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3645 pre_p, cleared);
3646 else
3648 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3649 gimplify_and_add (init, pre_p);
3650 ggc_free (init);
3655 /* Return the appropriate RHS predicate for this LHS. */
3657 gimple_predicate
3658 rhs_predicate_for (tree lhs)
3660 if (is_gimple_reg (lhs))
3661 return is_gimple_reg_rhs_or_call;
3662 else
3663 return is_gimple_mem_rhs_or_call;
3666 /* Gimplify a C99 compound literal expression. This just means adding
3667 the DECL_EXPR before the current statement and using its anonymous
3668 decl instead. */
3670 static enum gimplify_status
3671 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3672 bool (*gimple_test_f) (tree),
3673 fallback_t fallback)
3675 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3676 tree decl = DECL_EXPR_DECL (decl_s);
3677 tree init = DECL_INITIAL (decl);
3678 /* Mark the decl as addressable if the compound literal
3679 expression is addressable now, otherwise it is marked too late
3680 after we gimplify the initialization expression. */
3681 if (TREE_ADDRESSABLE (*expr_p))
3682 TREE_ADDRESSABLE (decl) = 1;
3683 /* Otherwise, if we don't need an lvalue and have a literal directly
3684 substitute it. Check if it matches the gimple predicate, as
3685 otherwise we'd generate a new temporary, and we can as well just
3686 use the decl we already have. */
3687 else if (!TREE_ADDRESSABLE (decl)
3688 && init
3689 && (fallback & fb_lvalue) == 0
3690 && gimple_test_f (init))
3692 *expr_p = init;
3693 return GS_OK;
3696 /* Preliminarily mark non-addressed complex variables as eligible
3697 for promotion to gimple registers. We'll transform their uses
3698 as we find them. */
3699 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3700 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3701 && !TREE_THIS_VOLATILE (decl)
3702 && !needs_to_live_in_memory (decl))
3703 DECL_GIMPLE_REG_P (decl) = 1;
3705 /* If the decl is not addressable, then it is being used in some
3706 expression or on the right hand side of a statement, and it can
3707 be put into a readonly data section. */
3708 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3709 TREE_READONLY (decl) = 1;
3711 /* This decl isn't mentioned in the enclosing block, so add it to the
3712 list of temps. FIXME it seems a bit of a kludge to say that
3713 anonymous artificial vars aren't pushed, but everything else is. */
3714 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3715 gimple_add_tmp_var (decl);
3717 gimplify_and_add (decl_s, pre_p);
3718 *expr_p = decl;
3719 return GS_OK;
3722 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3723 return a new CONSTRUCTOR if something changed. */
3725 static tree
3726 optimize_compound_literals_in_ctor (tree orig_ctor)
3728 tree ctor = orig_ctor;
3729 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3730 unsigned int idx, num = vec_safe_length (elts);
3732 for (idx = 0; idx < num; idx++)
3734 tree value = (*elts)[idx].value;
3735 tree newval = value;
3736 if (TREE_CODE (value) == CONSTRUCTOR)
3737 newval = optimize_compound_literals_in_ctor (value);
3738 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3740 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3741 tree decl = DECL_EXPR_DECL (decl_s);
3742 tree init = DECL_INITIAL (decl);
3744 if (!TREE_ADDRESSABLE (value)
3745 && !TREE_ADDRESSABLE (decl)
3746 && init
3747 && TREE_CODE (init) == CONSTRUCTOR)
3748 newval = optimize_compound_literals_in_ctor (init);
3750 if (newval == value)
3751 continue;
3753 if (ctor == orig_ctor)
3755 ctor = copy_node (orig_ctor);
3756 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3757 elts = CONSTRUCTOR_ELTS (ctor);
3759 (*elts)[idx].value = newval;
3761 return ctor;
3764 /* A subroutine of gimplify_modify_expr. Break out elements of a
3765 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3767 Note that we still need to clear any elements that don't have explicit
3768 initializers, so if not all elements are initialized we keep the
3769 original MODIFY_EXPR, we just remove all of the constructor elements.
3771 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3772 GS_ERROR if we would have to create a temporary when gimplifying
3773 this constructor. Otherwise, return GS_OK.
3775 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3777 static enum gimplify_status
3778 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3779 bool want_value, bool notify_temp_creation)
3781 tree object, ctor, type;
3782 enum gimplify_status ret;
3783 vec<constructor_elt, va_gc> *elts;
3785 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3787 if (!notify_temp_creation)
3789 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3790 is_gimple_lvalue, fb_lvalue);
3791 if (ret == GS_ERROR)
3792 return ret;
3795 object = TREE_OPERAND (*expr_p, 0);
3796 ctor = TREE_OPERAND (*expr_p, 1) =
3797 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3798 type = TREE_TYPE (ctor);
3799 elts = CONSTRUCTOR_ELTS (ctor);
3800 ret = GS_ALL_DONE;
3802 switch (TREE_CODE (type))
3804 case RECORD_TYPE:
3805 case UNION_TYPE:
3806 case QUAL_UNION_TYPE:
3807 case ARRAY_TYPE:
3809 struct gimplify_init_ctor_preeval_data preeval_data;
3810 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3811 bool cleared, complete_p, valid_const_initializer;
3813 /* Aggregate types must lower constructors to initialization of
3814 individual elements. The exception is that a CONSTRUCTOR node
3815 with no elements indicates zero-initialization of the whole. */
3816 if (vec_safe_is_empty (elts))
3818 if (notify_temp_creation)
3819 return GS_OK;
3820 break;
3823 /* Fetch information about the constructor to direct later processing.
3824 We might want to make static versions of it in various cases, and
3825 can only do so if it known to be a valid constant initializer. */
3826 valid_const_initializer
3827 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3828 &num_ctor_elements, &complete_p);
3830 /* If a const aggregate variable is being initialized, then it
3831 should never be a lose to promote the variable to be static. */
3832 if (valid_const_initializer
3833 && num_nonzero_elements > 1
3834 && TREE_READONLY (object)
3835 && TREE_CODE (object) == VAR_DECL
3836 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3838 if (notify_temp_creation)
3839 return GS_ERROR;
3840 DECL_INITIAL (object) = ctor;
3841 TREE_STATIC (object) = 1;
3842 if (!DECL_NAME (object))
3843 DECL_NAME (object) = create_tmp_var_name ("C");
3844 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3846 /* ??? C++ doesn't automatically append a .<number> to the
3847 assembler name, and even when it does, it looks at FE private
3848 data structures to figure out what that number should be,
3849 which are not set for this variable. I suppose this is
3850 important for local statics for inline functions, which aren't
3851 "local" in the object file sense. So in order to get a unique
3852 TU-local symbol, we must invoke the lhd version now. */
3853 lhd_set_decl_assembler_name (object);
3855 *expr_p = NULL_TREE;
3856 break;
3859 /* If there are "lots" of initialized elements, even discounting
3860 those that are not address constants (and thus *must* be
3861 computed at runtime), then partition the constructor into
3862 constant and non-constant parts. Block copy the constant
3863 parts in, then generate code for the non-constant parts. */
3864 /* TODO. There's code in cp/typeck.c to do this. */
3866 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3867 /* store_constructor will ignore the clearing of variable-sized
3868 objects. Initializers for such objects must explicitly set
3869 every field that needs to be set. */
3870 cleared = false;
3871 else if (!complete_p && !CONSTRUCTOR_NO_CLEARING (ctor))
3872 /* If the constructor isn't complete, clear the whole object
3873 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3875 ??? This ought not to be needed. For any element not present
3876 in the initializer, we should simply set them to zero. Except
3877 we'd need to *find* the elements that are not present, and that
3878 requires trickery to avoid quadratic compile-time behavior in
3879 large cases or excessive memory use in small cases. */
3880 cleared = true;
3881 else if (num_ctor_elements - num_nonzero_elements
3882 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3883 && num_nonzero_elements < num_ctor_elements / 4)
3884 /* If there are "lots" of zeros, it's more efficient to clear
3885 the memory and then set the nonzero elements. */
3886 cleared = true;
3887 else
3888 cleared = false;
3890 /* If there are "lots" of initialized elements, and all of them
3891 are valid address constants, then the entire initializer can
3892 be dropped to memory, and then memcpy'd out. Don't do this
3893 for sparse arrays, though, as it's more efficient to follow
3894 the standard CONSTRUCTOR behavior of memset followed by
3895 individual element initialization. Also don't do this for small
3896 all-zero initializers (which aren't big enough to merit
3897 clearing), and don't try to make bitwise copies of
3898 TREE_ADDRESSABLE types.
3900 We cannot apply such transformation when compiling chkp static
3901 initializer because creation of initializer image in the memory
3902 will require static initialization of bounds for it. It should
3903 result in another gimplification of similar initializer and we
3904 may fall into infinite loop. */
3905 if (valid_const_initializer
3906 && !(cleared || num_nonzero_elements == 0)
3907 && !TREE_ADDRESSABLE (type)
3908 && (!current_function_decl
3909 || !lookup_attribute ("chkp ctor",
3910 DECL_ATTRIBUTES (current_function_decl))))
3912 HOST_WIDE_INT size = int_size_in_bytes (type);
3913 unsigned int align;
3915 /* ??? We can still get unbounded array types, at least
3916 from the C++ front end. This seems wrong, but attempt
3917 to work around it for now. */
3918 if (size < 0)
3920 size = int_size_in_bytes (TREE_TYPE (object));
3921 if (size >= 0)
3922 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3925 /* Find the maximum alignment we can assume for the object. */
3926 /* ??? Make use of DECL_OFFSET_ALIGN. */
3927 if (DECL_P (object))
3928 align = DECL_ALIGN (object);
3929 else
3930 align = TYPE_ALIGN (type);
3932 /* Do a block move either if the size is so small as to make
3933 each individual move a sub-unit move on average, or if it
3934 is so large as to make individual moves inefficient. */
3935 if (size > 0
3936 && num_nonzero_elements > 1
3937 && (size < num_nonzero_elements
3938 || !can_move_by_pieces (size, align)))
3940 if (notify_temp_creation)
3941 return GS_ERROR;
3943 walk_tree (&ctor, force_labels_r, NULL, NULL);
3944 ctor = tree_output_constant_def (ctor);
3945 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3946 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3947 TREE_OPERAND (*expr_p, 1) = ctor;
3949 /* This is no longer an assignment of a CONSTRUCTOR, but
3950 we still may have processing to do on the LHS. So
3951 pretend we didn't do anything here to let that happen. */
3952 return GS_UNHANDLED;
3956 /* If the target is volatile, we have non-zero elements and more than
3957 one field to assign, initialize the target from a temporary. */
3958 if (TREE_THIS_VOLATILE (object)
3959 && !TREE_ADDRESSABLE (type)
3960 && num_nonzero_elements > 0
3961 && vec_safe_length (elts) > 1)
3963 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type));
3964 TREE_OPERAND (*expr_p, 0) = temp;
3965 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3966 *expr_p,
3967 build2 (MODIFY_EXPR, void_type_node,
3968 object, temp));
3969 return GS_OK;
3972 if (notify_temp_creation)
3973 return GS_OK;
3975 /* If there are nonzero elements and if needed, pre-evaluate to capture
3976 elements overlapping with the lhs into temporaries. We must do this
3977 before clearing to fetch the values before they are zeroed-out. */
3978 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3980 preeval_data.lhs_base_decl = get_base_address (object);
3981 if (!DECL_P (preeval_data.lhs_base_decl))
3982 preeval_data.lhs_base_decl = NULL;
3983 preeval_data.lhs_alias_set = get_alias_set (object);
3985 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3986 pre_p, post_p, &preeval_data);
3989 bool ctor_has_side_effects_p
3990 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1));
3992 if (cleared)
3994 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3995 Note that we still have to gimplify, in order to handle the
3996 case of variable sized types. Avoid shared tree structures. */
3997 CONSTRUCTOR_ELTS (ctor) = NULL;
3998 TREE_SIDE_EFFECTS (ctor) = 0;
3999 object = unshare_expr (object);
4000 gimplify_stmt (expr_p, pre_p);
4003 /* If we have not block cleared the object, or if there are nonzero
4004 elements in the constructor, or if the constructor has side effects,
4005 add assignments to the individual scalar fields of the object. */
4006 if (!cleared
4007 || num_nonzero_elements > 0
4008 || ctor_has_side_effects_p)
4009 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4011 *expr_p = NULL_TREE;
4013 break;
4015 case COMPLEX_TYPE:
4017 tree r, i;
4019 if (notify_temp_creation)
4020 return GS_OK;
4022 /* Extract the real and imaginary parts out of the ctor. */
4023 gcc_assert (elts->length () == 2);
4024 r = (*elts)[0].value;
4025 i = (*elts)[1].value;
4026 if (r == NULL || i == NULL)
4028 tree zero = build_zero_cst (TREE_TYPE (type));
4029 if (r == NULL)
4030 r = zero;
4031 if (i == NULL)
4032 i = zero;
4035 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4036 represent creation of a complex value. */
4037 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4039 ctor = build_complex (type, r, i);
4040 TREE_OPERAND (*expr_p, 1) = ctor;
4042 else
4044 ctor = build2 (COMPLEX_EXPR, type, r, i);
4045 TREE_OPERAND (*expr_p, 1) = ctor;
4046 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4047 pre_p,
4048 post_p,
4049 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4050 fb_rvalue);
4053 break;
4055 case VECTOR_TYPE:
4057 unsigned HOST_WIDE_INT ix;
4058 constructor_elt *ce;
4060 if (notify_temp_creation)
4061 return GS_OK;
4063 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4064 if (TREE_CONSTANT (ctor))
4066 bool constant_p = true;
4067 tree value;
4069 /* Even when ctor is constant, it might contain non-*_CST
4070 elements, such as addresses or trapping values like
4071 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4072 in VECTOR_CST nodes. */
4073 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4074 if (!CONSTANT_CLASS_P (value))
4076 constant_p = false;
4077 break;
4080 if (constant_p)
4082 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4083 break;
4086 TREE_CONSTANT (ctor) = 0;
4089 /* Vector types use CONSTRUCTOR all the way through gimple
4090 compilation as a general initializer. */
4091 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4093 enum gimplify_status tret;
4094 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4095 fb_rvalue);
4096 if (tret == GS_ERROR)
4097 ret = GS_ERROR;
4099 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4100 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4102 break;
4104 default:
4105 /* So how did we get a CONSTRUCTOR for a scalar type? */
4106 gcc_unreachable ();
4109 if (ret == GS_ERROR)
4110 return GS_ERROR;
4111 else if (want_value)
4113 *expr_p = object;
4114 return GS_OK;
4116 else
4118 /* If we have gimplified both sides of the initializer but have
4119 not emitted an assignment, do so now. */
4120 if (*expr_p)
4122 tree lhs = TREE_OPERAND (*expr_p, 0);
4123 tree rhs = TREE_OPERAND (*expr_p, 1);
4124 gassign *init = gimple_build_assign (lhs, rhs);
4125 gimplify_seq_add_stmt (pre_p, init);
4126 *expr_p = NULL;
4129 return GS_ALL_DONE;
4133 /* Given a pointer value OP0, return a simplified version of an
4134 indirection through OP0, or NULL_TREE if no simplification is
4135 possible. This may only be applied to a rhs of an expression.
4136 Note that the resulting type may be different from the type pointed
4137 to in the sense that it is still compatible from the langhooks
4138 point of view. */
4140 static tree
4141 gimple_fold_indirect_ref_rhs (tree t)
4143 return gimple_fold_indirect_ref (t);
4146 /* Subroutine of gimplify_modify_expr to do simplifications of
4147 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4148 something changes. */
4150 static enum gimplify_status
4151 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4152 gimple_seq *pre_p, gimple_seq *post_p,
4153 bool want_value)
4155 enum gimplify_status ret = GS_UNHANDLED;
4156 bool changed;
4160 changed = false;
4161 switch (TREE_CODE (*from_p))
4163 case VAR_DECL:
4164 /* If we're assigning from a read-only variable initialized with
4165 a constructor, do the direct assignment from the constructor,
4166 but only if neither source nor target are volatile since this
4167 latter assignment might end up being done on a per-field basis. */
4168 if (DECL_INITIAL (*from_p)
4169 && TREE_READONLY (*from_p)
4170 && !TREE_THIS_VOLATILE (*from_p)
4171 && !TREE_THIS_VOLATILE (*to_p)
4172 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4174 tree old_from = *from_p;
4175 enum gimplify_status subret;
4177 /* Move the constructor into the RHS. */
4178 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4180 /* Let's see if gimplify_init_constructor will need to put
4181 it in memory. */
4182 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4183 false, true);
4184 if (subret == GS_ERROR)
4186 /* If so, revert the change. */
4187 *from_p = old_from;
4189 else
4191 ret = GS_OK;
4192 changed = true;
4195 break;
4196 case INDIRECT_REF:
4198 /* If we have code like
4200 *(const A*)(A*)&x
4202 where the type of "x" is a (possibly cv-qualified variant
4203 of "A"), treat the entire expression as identical to "x".
4204 This kind of code arises in C++ when an object is bound
4205 to a const reference, and if "x" is a TARGET_EXPR we want
4206 to take advantage of the optimization below. */
4207 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4208 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4209 if (t)
4211 if (TREE_THIS_VOLATILE (t) != volatile_p)
4213 if (DECL_P (t))
4214 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4215 build_fold_addr_expr (t));
4216 if (REFERENCE_CLASS_P (t))
4217 TREE_THIS_VOLATILE (t) = volatile_p;
4219 *from_p = t;
4220 ret = GS_OK;
4221 changed = true;
4223 break;
4226 case TARGET_EXPR:
4228 /* If we are initializing something from a TARGET_EXPR, strip the
4229 TARGET_EXPR and initialize it directly, if possible. This can't
4230 be done if the initializer is void, since that implies that the
4231 temporary is set in some non-trivial way.
4233 ??? What about code that pulls out the temp and uses it
4234 elsewhere? I think that such code never uses the TARGET_EXPR as
4235 an initializer. If I'm wrong, we'll die because the temp won't
4236 have any RTL. In that case, I guess we'll need to replace
4237 references somehow. */
4238 tree init = TARGET_EXPR_INITIAL (*from_p);
4240 if (init
4241 && !VOID_TYPE_P (TREE_TYPE (init)))
4243 *from_p = init;
4244 ret = GS_OK;
4245 changed = true;
4248 break;
4250 case COMPOUND_EXPR:
4251 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4252 caught. */
4253 gimplify_compound_expr (from_p, pre_p, true);
4254 ret = GS_OK;
4255 changed = true;
4256 break;
4258 case CONSTRUCTOR:
4259 /* If we already made some changes, let the front end have a
4260 crack at this before we break it down. */
4261 if (ret != GS_UNHANDLED)
4262 break;
4263 /* If we're initializing from a CONSTRUCTOR, break this into
4264 individual MODIFY_EXPRs. */
4265 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4266 false);
4268 case COND_EXPR:
4269 /* If we're assigning to a non-register type, push the assignment
4270 down into the branches. This is mandatory for ADDRESSABLE types,
4271 since we cannot generate temporaries for such, but it saves a
4272 copy in other cases as well. */
4273 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4275 /* This code should mirror the code in gimplify_cond_expr. */
4276 enum tree_code code = TREE_CODE (*expr_p);
4277 tree cond = *from_p;
4278 tree result = *to_p;
4280 ret = gimplify_expr (&result, pre_p, post_p,
4281 is_gimple_lvalue, fb_lvalue);
4282 if (ret != GS_ERROR)
4283 ret = GS_OK;
4285 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4286 TREE_OPERAND (cond, 1)
4287 = build2 (code, void_type_node, result,
4288 TREE_OPERAND (cond, 1));
4289 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4290 TREE_OPERAND (cond, 2)
4291 = build2 (code, void_type_node, unshare_expr (result),
4292 TREE_OPERAND (cond, 2));
4294 TREE_TYPE (cond) = void_type_node;
4295 recalculate_side_effects (cond);
4297 if (want_value)
4299 gimplify_and_add (cond, pre_p);
4300 *expr_p = unshare_expr (result);
4302 else
4303 *expr_p = cond;
4304 return ret;
4306 break;
4308 case CALL_EXPR:
4309 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4310 return slot so that we don't generate a temporary. */
4311 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4312 && aggregate_value_p (*from_p, *from_p))
4314 bool use_target;
4316 if (!(rhs_predicate_for (*to_p))(*from_p))
4317 /* If we need a temporary, *to_p isn't accurate. */
4318 use_target = false;
4319 /* It's OK to use the return slot directly unless it's an NRV. */
4320 else if (TREE_CODE (*to_p) == RESULT_DECL
4321 && DECL_NAME (*to_p) == NULL_TREE
4322 && needs_to_live_in_memory (*to_p))
4323 use_target = true;
4324 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4325 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4326 /* Don't force regs into memory. */
4327 use_target = false;
4328 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4329 /* It's OK to use the target directly if it's being
4330 initialized. */
4331 use_target = true;
4332 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p)))
4333 != INTEGER_CST)
4334 /* Always use the target and thus RSO for variable-sized types.
4335 GIMPLE cannot deal with a variable-sized assignment
4336 embedded in a call statement. */
4337 use_target = true;
4338 else if (TREE_CODE (*to_p) != SSA_NAME
4339 && (!is_gimple_variable (*to_p)
4340 || needs_to_live_in_memory (*to_p)))
4341 /* Don't use the original target if it's already addressable;
4342 if its address escapes, and the called function uses the
4343 NRV optimization, a conforming program could see *to_p
4344 change before the called function returns; see c++/19317.
4345 When optimizing, the return_slot pass marks more functions
4346 as safe after we have escape info. */
4347 use_target = false;
4348 else
4349 use_target = true;
4351 if (use_target)
4353 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4354 mark_addressable (*to_p);
4357 break;
4359 case WITH_SIZE_EXPR:
4360 /* Likewise for calls that return an aggregate of non-constant size,
4361 since we would not be able to generate a temporary at all. */
4362 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4364 *from_p = TREE_OPERAND (*from_p, 0);
4365 /* We don't change ret in this case because the
4366 WITH_SIZE_EXPR might have been added in
4367 gimplify_modify_expr, so returning GS_OK would lead to an
4368 infinite loop. */
4369 changed = true;
4371 break;
4373 /* If we're initializing from a container, push the initialization
4374 inside it. */
4375 case CLEANUP_POINT_EXPR:
4376 case BIND_EXPR:
4377 case STATEMENT_LIST:
4379 tree wrap = *from_p;
4380 tree t;
4382 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4383 fb_lvalue);
4384 if (ret != GS_ERROR)
4385 ret = GS_OK;
4387 t = voidify_wrapper_expr (wrap, *expr_p);
4388 gcc_assert (t == *expr_p);
4390 if (want_value)
4392 gimplify_and_add (wrap, pre_p);
4393 *expr_p = unshare_expr (*to_p);
4395 else
4396 *expr_p = wrap;
4397 return GS_OK;
4400 case COMPOUND_LITERAL_EXPR:
4402 tree complit = TREE_OPERAND (*expr_p, 1);
4403 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4404 tree decl = DECL_EXPR_DECL (decl_s);
4405 tree init = DECL_INITIAL (decl);
4407 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4408 into struct T x = { 0, 1, 2 } if the address of the
4409 compound literal has never been taken. */
4410 if (!TREE_ADDRESSABLE (complit)
4411 && !TREE_ADDRESSABLE (decl)
4412 && init)
4414 *expr_p = copy_node (*expr_p);
4415 TREE_OPERAND (*expr_p, 1) = init;
4416 return GS_OK;
4420 default:
4421 break;
4424 while (changed);
4426 return ret;
4430 /* Return true if T looks like a valid GIMPLE statement. */
4432 static bool
4433 is_gimple_stmt (tree t)
4435 const enum tree_code code = TREE_CODE (t);
4437 switch (code)
4439 case NOP_EXPR:
4440 /* The only valid NOP_EXPR is the empty statement. */
4441 return IS_EMPTY_STMT (t);
4443 case BIND_EXPR:
4444 case COND_EXPR:
4445 /* These are only valid if they're void. */
4446 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4448 case SWITCH_EXPR:
4449 case GOTO_EXPR:
4450 case RETURN_EXPR:
4451 case LABEL_EXPR:
4452 case CASE_LABEL_EXPR:
4453 case TRY_CATCH_EXPR:
4454 case TRY_FINALLY_EXPR:
4455 case EH_FILTER_EXPR:
4456 case CATCH_EXPR:
4457 case ASM_EXPR:
4458 case STATEMENT_LIST:
4459 case OACC_PARALLEL:
4460 case OACC_KERNELS:
4461 case OACC_DATA:
4462 case OACC_HOST_DATA:
4463 case OACC_DECLARE:
4464 case OACC_UPDATE:
4465 case OACC_ENTER_DATA:
4466 case OACC_EXIT_DATA:
4467 case OACC_CACHE:
4468 case OMP_PARALLEL:
4469 case OMP_FOR:
4470 case OMP_SIMD:
4471 case CILK_SIMD:
4472 case OMP_DISTRIBUTE:
4473 case OACC_LOOP:
4474 case OMP_SECTIONS:
4475 case OMP_SECTION:
4476 case OMP_SINGLE:
4477 case OMP_MASTER:
4478 case OMP_TASKGROUP:
4479 case OMP_ORDERED:
4480 case OMP_CRITICAL:
4481 case OMP_TASK:
4482 case OMP_TARGET:
4483 case OMP_TARGET_DATA:
4484 case OMP_TARGET_UPDATE:
4485 case OMP_TARGET_ENTER_DATA:
4486 case OMP_TARGET_EXIT_DATA:
4487 case OMP_TASKLOOP:
4488 case OMP_TEAMS:
4489 /* These are always void. */
4490 return true;
4492 case CALL_EXPR:
4493 case MODIFY_EXPR:
4494 case PREDICT_EXPR:
4495 /* These are valid regardless of their type. */
4496 return true;
4498 default:
4499 return false;
4504 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4505 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4506 DECL_GIMPLE_REG_P set.
4508 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4509 other, unmodified part of the complex object just before the total store.
4510 As a consequence, if the object is still uninitialized, an undefined value
4511 will be loaded into a register, which may result in a spurious exception
4512 if the register is floating-point and the value happens to be a signaling
4513 NaN for example. Then the fully-fledged complex operations lowering pass
4514 followed by a DCE pass are necessary in order to fix things up. */
4516 static enum gimplify_status
4517 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4518 bool want_value)
4520 enum tree_code code, ocode;
4521 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4523 lhs = TREE_OPERAND (*expr_p, 0);
4524 rhs = TREE_OPERAND (*expr_p, 1);
4525 code = TREE_CODE (lhs);
4526 lhs = TREE_OPERAND (lhs, 0);
4528 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4529 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4530 TREE_NO_WARNING (other) = 1;
4531 other = get_formal_tmp_var (other, pre_p);
4533 realpart = code == REALPART_EXPR ? rhs : other;
4534 imagpart = code == REALPART_EXPR ? other : rhs;
4536 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4537 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4538 else
4539 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4541 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4542 *expr_p = (want_value) ? rhs : NULL_TREE;
4544 return GS_ALL_DONE;
4547 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4549 modify_expr
4550 : varname '=' rhs
4551 | '*' ID '=' rhs
4553 PRE_P points to the list where side effects that must happen before
4554 *EXPR_P should be stored.
4556 POST_P points to the list where side effects that must happen after
4557 *EXPR_P should be stored.
4559 WANT_VALUE is nonzero iff we want to use the value of this expression
4560 in another expression. */
4562 static enum gimplify_status
4563 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4564 bool want_value)
4566 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4567 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4568 enum gimplify_status ret = GS_UNHANDLED;
4569 gimple *assign;
4570 location_t loc = EXPR_LOCATION (*expr_p);
4571 gimple_stmt_iterator gsi;
4573 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4574 || TREE_CODE (*expr_p) == INIT_EXPR);
4576 /* Trying to simplify a clobber using normal logic doesn't work,
4577 so handle it here. */
4578 if (TREE_CLOBBER_P (*from_p))
4580 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4581 if (ret == GS_ERROR)
4582 return ret;
4583 gcc_assert (!want_value
4584 && (TREE_CODE (*to_p) == VAR_DECL
4585 || TREE_CODE (*to_p) == MEM_REF));
4586 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4587 *expr_p = NULL;
4588 return GS_ALL_DONE;
4591 /* Insert pointer conversions required by the middle-end that are not
4592 required by the frontend. This fixes middle-end type checking for
4593 for example gcc.dg/redecl-6.c. */
4594 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4596 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4597 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4598 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4601 /* See if any simplifications can be done based on what the RHS is. */
4602 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4603 want_value);
4604 if (ret != GS_UNHANDLED)
4605 return ret;
4607 /* For zero sized types only gimplify the left hand side and right hand
4608 side as statements and throw away the assignment. Do this after
4609 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4610 types properly. */
4611 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4613 gimplify_stmt (from_p, pre_p);
4614 gimplify_stmt (to_p, pre_p);
4615 *expr_p = NULL_TREE;
4616 return GS_ALL_DONE;
4619 /* If the value being copied is of variable width, compute the length
4620 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4621 before gimplifying any of the operands so that we can resolve any
4622 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4623 the size of the expression to be copied, not of the destination, so
4624 that is what we must do here. */
4625 maybe_with_size_expr (from_p);
4627 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4628 if (ret == GS_ERROR)
4629 return ret;
4631 /* As a special case, we have to temporarily allow for assignments
4632 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4633 a toplevel statement, when gimplifying the GENERIC expression
4634 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4635 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4637 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4638 prevent gimplify_expr from trying to create a new temporary for
4639 foo's LHS, we tell it that it should only gimplify until it
4640 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4641 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4642 and all we need to do here is set 'a' to be its LHS. */
4643 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4644 fb_rvalue);
4645 if (ret == GS_ERROR)
4646 return ret;
4648 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4649 size as argument to the call. */
4650 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4652 tree call = TREE_OPERAND (*from_p, 0);
4653 tree vlasize = TREE_OPERAND (*from_p, 1);
4655 if (TREE_CODE (call) == CALL_EXPR
4656 && CALL_EXPR_IFN (call) == IFN_VA_ARG)
4658 int nargs = call_expr_nargs (call);
4659 tree type = TREE_TYPE (call);
4660 tree ap = CALL_EXPR_ARG (call, 0);
4661 tree tag = CALL_EXPR_ARG (call, 1);
4662 tree newcall = build_call_expr_internal_loc (EXPR_LOCATION (call),
4663 IFN_VA_ARG, type,
4664 nargs + 1, ap, tag,
4665 vlasize);
4666 tree *call_p = &(TREE_OPERAND (*from_p, 0));
4667 *call_p = newcall;
4671 /* Now see if the above changed *from_p to something we handle specially. */
4672 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4673 want_value);
4674 if (ret != GS_UNHANDLED)
4675 return ret;
4677 /* If we've got a variable sized assignment between two lvalues (i.e. does
4678 not involve a call), then we can make things a bit more straightforward
4679 by converting the assignment to memcpy or memset. */
4680 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4682 tree from = TREE_OPERAND (*from_p, 0);
4683 tree size = TREE_OPERAND (*from_p, 1);
4685 if (TREE_CODE (from) == CONSTRUCTOR)
4686 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4688 if (is_gimple_addressable (from))
4690 *from_p = from;
4691 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4692 pre_p);
4696 /* Transform partial stores to non-addressable complex variables into
4697 total stores. This allows us to use real instead of virtual operands
4698 for these variables, which improves optimization. */
4699 if ((TREE_CODE (*to_p) == REALPART_EXPR
4700 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4701 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4702 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4704 /* Try to alleviate the effects of the gimplification creating artificial
4705 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4706 make sure not to create DECL_DEBUG_EXPR links across functions. */
4707 if (!gimplify_ctxp->into_ssa
4708 && TREE_CODE (*from_p) == VAR_DECL
4709 && DECL_IGNORED_P (*from_p)
4710 && DECL_P (*to_p)
4711 && !DECL_IGNORED_P (*to_p)
4712 && decl_function_context (*to_p) == current_function_decl)
4714 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4715 DECL_NAME (*from_p)
4716 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4717 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4718 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4721 if (want_value && TREE_THIS_VOLATILE (*to_p))
4722 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4724 if (TREE_CODE (*from_p) == CALL_EXPR)
4726 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4727 instead of a GIMPLE_ASSIGN. */
4728 gcall *call_stmt;
4729 if (CALL_EXPR_FN (*from_p) == NULL_TREE)
4731 /* Gimplify internal functions created in the FEs. */
4732 int nargs = call_expr_nargs (*from_p), i;
4733 enum internal_fn ifn = CALL_EXPR_IFN (*from_p);
4734 auto_vec<tree> vargs (nargs);
4736 for (i = 0; i < nargs; i++)
4738 gimplify_arg (&CALL_EXPR_ARG (*from_p, i), pre_p,
4739 EXPR_LOCATION (*from_p));
4740 vargs.quick_push (CALL_EXPR_ARG (*from_p, i));
4742 call_stmt = gimple_build_call_internal_vec (ifn, vargs);
4743 gimple_set_location (call_stmt, EXPR_LOCATION (*expr_p));
4745 else
4747 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4748 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4749 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4750 tree fndecl = get_callee_fndecl (*from_p);
4751 if (fndecl
4752 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4753 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
4754 && call_expr_nargs (*from_p) == 3)
4755 call_stmt = gimple_build_call_internal (IFN_BUILTIN_EXPECT, 3,
4756 CALL_EXPR_ARG (*from_p, 0),
4757 CALL_EXPR_ARG (*from_p, 1),
4758 CALL_EXPR_ARG (*from_p, 2));
4759 else
4761 call_stmt = gimple_build_call_from_tree (*from_p);
4762 gimple_call_set_fntype (call_stmt, TREE_TYPE (fnptrtype));
4765 notice_special_calls (call_stmt);
4766 if (!gimple_call_noreturn_p (call_stmt))
4767 gimple_call_set_lhs (call_stmt, *to_p);
4768 assign = call_stmt;
4770 else
4772 assign = gimple_build_assign (*to_p, *from_p);
4773 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4776 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4778 /* We should have got an SSA name from the start. */
4779 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4782 gimplify_seq_add_stmt (pre_p, assign);
4783 gsi = gsi_last (*pre_p);
4784 maybe_fold_stmt (&gsi);
4786 if (want_value)
4788 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4789 return GS_OK;
4791 else
4792 *expr_p = NULL;
4794 return GS_ALL_DONE;
4797 /* Gimplify a comparison between two variable-sized objects. Do this
4798 with a call to BUILT_IN_MEMCMP. */
4800 static enum gimplify_status
4801 gimplify_variable_sized_compare (tree *expr_p)
4803 location_t loc = EXPR_LOCATION (*expr_p);
4804 tree op0 = TREE_OPERAND (*expr_p, 0);
4805 tree op1 = TREE_OPERAND (*expr_p, 1);
4806 tree t, arg, dest, src, expr;
4808 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4809 arg = unshare_expr (arg);
4810 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4811 src = build_fold_addr_expr_loc (loc, op1);
4812 dest = build_fold_addr_expr_loc (loc, op0);
4813 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4814 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4816 expr
4817 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4818 SET_EXPR_LOCATION (expr, loc);
4819 *expr_p = expr;
4821 return GS_OK;
4824 /* Gimplify a comparison between two aggregate objects of integral scalar
4825 mode as a comparison between the bitwise equivalent scalar values. */
4827 static enum gimplify_status
4828 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4830 location_t loc = EXPR_LOCATION (*expr_p);
4831 tree op0 = TREE_OPERAND (*expr_p, 0);
4832 tree op1 = TREE_OPERAND (*expr_p, 1);
4834 tree type = TREE_TYPE (op0);
4835 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4837 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4838 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4840 *expr_p
4841 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4843 return GS_OK;
4846 /* Gimplify an expression sequence. This function gimplifies each
4847 expression and rewrites the original expression with the last
4848 expression of the sequence in GIMPLE form.
4850 PRE_P points to the list where the side effects for all the
4851 expressions in the sequence will be emitted.
4853 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4855 static enum gimplify_status
4856 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4858 tree t = *expr_p;
4862 tree *sub_p = &TREE_OPERAND (t, 0);
4864 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4865 gimplify_compound_expr (sub_p, pre_p, false);
4866 else
4867 gimplify_stmt (sub_p, pre_p);
4869 t = TREE_OPERAND (t, 1);
4871 while (TREE_CODE (t) == COMPOUND_EXPR);
4873 *expr_p = t;
4874 if (want_value)
4875 return GS_OK;
4876 else
4878 gimplify_stmt (expr_p, pre_p);
4879 return GS_ALL_DONE;
4883 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4884 gimplify. After gimplification, EXPR_P will point to a new temporary
4885 that holds the original value of the SAVE_EXPR node.
4887 PRE_P points to the list where side effects that must happen before
4888 *EXPR_P should be stored. */
4890 static enum gimplify_status
4891 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4893 enum gimplify_status ret = GS_ALL_DONE;
4894 tree val;
4896 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4897 val = TREE_OPERAND (*expr_p, 0);
4899 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4900 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4902 /* The operand may be a void-valued expression such as SAVE_EXPRs
4903 generated by the Java frontend for class initialization. It is
4904 being executed only for its side-effects. */
4905 if (TREE_TYPE (val) == void_type_node)
4907 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4908 is_gimple_stmt, fb_none);
4909 val = NULL;
4911 else
4912 val = get_initialized_tmp_var (val, pre_p, post_p);
4914 TREE_OPERAND (*expr_p, 0) = val;
4915 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4918 *expr_p = val;
4920 return ret;
4923 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4925 unary_expr
4926 : ...
4927 | '&' varname
4930 PRE_P points to the list where side effects that must happen before
4931 *EXPR_P should be stored.
4933 POST_P points to the list where side effects that must happen after
4934 *EXPR_P should be stored. */
4936 static enum gimplify_status
4937 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4939 tree expr = *expr_p;
4940 tree op0 = TREE_OPERAND (expr, 0);
4941 enum gimplify_status ret;
4942 location_t loc = EXPR_LOCATION (*expr_p);
4944 switch (TREE_CODE (op0))
4946 case INDIRECT_REF:
4947 do_indirect_ref:
4948 /* Check if we are dealing with an expression of the form '&*ptr'.
4949 While the front end folds away '&*ptr' into 'ptr', these
4950 expressions may be generated internally by the compiler (e.g.,
4951 builtins like __builtin_va_end). */
4952 /* Caution: the silent array decomposition semantics we allow for
4953 ADDR_EXPR means we can't always discard the pair. */
4954 /* Gimplification of the ADDR_EXPR operand may drop
4955 cv-qualification conversions, so make sure we add them if
4956 needed. */
4958 tree op00 = TREE_OPERAND (op0, 0);
4959 tree t_expr = TREE_TYPE (expr);
4960 tree t_op00 = TREE_TYPE (op00);
4962 if (!useless_type_conversion_p (t_expr, t_op00))
4963 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4964 *expr_p = op00;
4965 ret = GS_OK;
4967 break;
4969 case VIEW_CONVERT_EXPR:
4970 /* Take the address of our operand and then convert it to the type of
4971 this ADDR_EXPR.
4973 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4974 all clear. The impact of this transformation is even less clear. */
4976 /* If the operand is a useless conversion, look through it. Doing so
4977 guarantees that the ADDR_EXPR and its operand will remain of the
4978 same type. */
4979 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4980 op0 = TREE_OPERAND (op0, 0);
4982 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4983 build_fold_addr_expr_loc (loc,
4984 TREE_OPERAND (op0, 0)));
4985 ret = GS_OK;
4986 break;
4988 case MEM_REF:
4989 if (integer_zerop (TREE_OPERAND (op0, 1)))
4990 goto do_indirect_ref;
4992 /* ... fall through ... */
4994 default:
4995 /* If we see a call to a declared builtin or see its address
4996 being taken (we can unify those cases here) then we can mark
4997 the builtin for implicit generation by GCC. */
4998 if (TREE_CODE (op0) == FUNCTION_DECL
4999 && DECL_BUILT_IN_CLASS (op0) == BUILT_IN_NORMAL
5000 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0)))
5001 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0), true);
5003 /* We use fb_either here because the C frontend sometimes takes
5004 the address of a call that returns a struct; see
5005 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5006 the implied temporary explicit. */
5008 /* Make the operand addressable. */
5009 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5010 is_gimple_addressable, fb_either);
5011 if (ret == GS_ERROR)
5012 break;
5014 /* Then mark it. Beware that it may not be possible to do so directly
5015 if a temporary has been created by the gimplification. */
5016 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5018 op0 = TREE_OPERAND (expr, 0);
5020 /* For various reasons, the gimplification of the expression
5021 may have made a new INDIRECT_REF. */
5022 if (TREE_CODE (op0) == INDIRECT_REF)
5023 goto do_indirect_ref;
5025 mark_addressable (TREE_OPERAND (expr, 0));
5027 /* The FEs may end up building ADDR_EXPRs early on a decl with
5028 an incomplete type. Re-build ADDR_EXPRs in canonical form
5029 here. */
5030 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5031 *expr_p = build_fold_addr_expr (op0);
5033 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5034 recompute_tree_invariant_for_addr_expr (*expr_p);
5036 /* If we re-built the ADDR_EXPR add a conversion to the original type
5037 if required. */
5038 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5039 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5041 break;
5044 return ret;
5047 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5048 value; output operands should be a gimple lvalue. */
5050 static enum gimplify_status
5051 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5053 tree expr;
5054 int noutputs;
5055 const char **oconstraints;
5056 int i;
5057 tree link;
5058 const char *constraint;
5059 bool allows_mem, allows_reg, is_inout;
5060 enum gimplify_status ret, tret;
5061 gasm *stmt;
5062 vec<tree, va_gc> *inputs;
5063 vec<tree, va_gc> *outputs;
5064 vec<tree, va_gc> *clobbers;
5065 vec<tree, va_gc> *labels;
5066 tree link_next;
5068 expr = *expr_p;
5069 noutputs = list_length (ASM_OUTPUTS (expr));
5070 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5072 inputs = NULL;
5073 outputs = NULL;
5074 clobbers = NULL;
5075 labels = NULL;
5077 ret = GS_ALL_DONE;
5078 link_next = NULL_TREE;
5079 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5081 bool ok;
5082 size_t constraint_len;
5084 link_next = TREE_CHAIN (link);
5086 oconstraints[i]
5087 = constraint
5088 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5089 constraint_len = strlen (constraint);
5090 if (constraint_len == 0)
5091 continue;
5093 ok = parse_output_constraint (&constraint, i, 0, 0,
5094 &allows_mem, &allows_reg, &is_inout);
5095 if (!ok)
5097 ret = GS_ERROR;
5098 is_inout = false;
5101 if (!allows_reg && allows_mem)
5102 mark_addressable (TREE_VALUE (link));
5104 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5105 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5106 fb_lvalue | fb_mayfail);
5107 if (tret == GS_ERROR)
5109 error ("invalid lvalue in asm output %d", i);
5110 ret = tret;
5113 vec_safe_push (outputs, link);
5114 TREE_CHAIN (link) = NULL_TREE;
5116 if (is_inout)
5118 /* An input/output operand. To give the optimizers more
5119 flexibility, split it into separate input and output
5120 operands. */
5121 tree input;
5122 char buf[10];
5124 /* Turn the in/out constraint into an output constraint. */
5125 char *p = xstrdup (constraint);
5126 p[0] = '=';
5127 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5129 /* And add a matching input constraint. */
5130 if (allows_reg)
5132 sprintf (buf, "%d", i);
5134 /* If there are multiple alternatives in the constraint,
5135 handle each of them individually. Those that allow register
5136 will be replaced with operand number, the others will stay
5137 unchanged. */
5138 if (strchr (p, ',') != NULL)
5140 size_t len = 0, buflen = strlen (buf);
5141 char *beg, *end, *str, *dst;
5143 for (beg = p + 1;;)
5145 end = strchr (beg, ',');
5146 if (end == NULL)
5147 end = strchr (beg, '\0');
5148 if ((size_t) (end - beg) < buflen)
5149 len += buflen + 1;
5150 else
5151 len += end - beg + 1;
5152 if (*end)
5153 beg = end + 1;
5154 else
5155 break;
5158 str = (char *) alloca (len);
5159 for (beg = p + 1, dst = str;;)
5161 const char *tem;
5162 bool mem_p, reg_p, inout_p;
5164 end = strchr (beg, ',');
5165 if (end)
5166 *end = '\0';
5167 beg[-1] = '=';
5168 tem = beg - 1;
5169 parse_output_constraint (&tem, i, 0, 0,
5170 &mem_p, &reg_p, &inout_p);
5171 if (dst != str)
5172 *dst++ = ',';
5173 if (reg_p)
5175 memcpy (dst, buf, buflen);
5176 dst += buflen;
5178 else
5180 if (end)
5181 len = end - beg;
5182 else
5183 len = strlen (beg);
5184 memcpy (dst, beg, len);
5185 dst += len;
5187 if (end)
5188 beg = end + 1;
5189 else
5190 break;
5192 *dst = '\0';
5193 input = build_string (dst - str, str);
5195 else
5196 input = build_string (strlen (buf), buf);
5198 else
5199 input = build_string (constraint_len - 1, constraint + 1);
5201 free (p);
5203 input = build_tree_list (build_tree_list (NULL_TREE, input),
5204 unshare_expr (TREE_VALUE (link)));
5205 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5209 link_next = NULL_TREE;
5210 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5212 link_next = TREE_CHAIN (link);
5213 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5214 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5215 oconstraints, &allows_mem, &allows_reg);
5217 /* If we can't make copies, we can only accept memory. */
5218 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5220 if (allows_mem)
5221 allows_reg = 0;
5222 else
5224 error ("impossible constraint in %<asm%>");
5225 error ("non-memory input %d must stay in memory", i);
5226 return GS_ERROR;
5230 /* If the operand is a memory input, it should be an lvalue. */
5231 if (!allows_reg && allows_mem)
5233 tree inputv = TREE_VALUE (link);
5234 STRIP_NOPS (inputv);
5235 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5236 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5237 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5238 || TREE_CODE (inputv) == POSTINCREMENT_EXPR
5239 || TREE_CODE (inputv) == MODIFY_EXPR)
5240 TREE_VALUE (link) = error_mark_node;
5241 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5242 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5243 mark_addressable (TREE_VALUE (link));
5244 if (tret == GS_ERROR)
5246 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5247 input_location = EXPR_LOCATION (TREE_VALUE (link));
5248 error ("memory input %d is not directly addressable", i);
5249 ret = tret;
5252 else
5254 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5255 is_gimple_asm_val, fb_rvalue);
5256 if (tret == GS_ERROR)
5257 ret = tret;
5260 TREE_CHAIN (link) = NULL_TREE;
5261 vec_safe_push (inputs, link);
5264 link_next = NULL_TREE;
5265 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5267 link_next = TREE_CHAIN (link);
5268 TREE_CHAIN (link) = NULL_TREE;
5269 vec_safe_push (clobbers, link);
5272 link_next = NULL_TREE;
5273 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5275 link_next = TREE_CHAIN (link);
5276 TREE_CHAIN (link) = NULL_TREE;
5277 vec_safe_push (labels, link);
5280 /* Do not add ASMs with errors to the gimple IL stream. */
5281 if (ret != GS_ERROR)
5283 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5284 inputs, outputs, clobbers, labels);
5286 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr) || noutputs == 0);
5287 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5289 gimplify_seq_add_stmt (pre_p, stmt);
5292 return ret;
5295 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5296 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5297 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5298 return to this function.
5300 FIXME should we complexify the prequeue handling instead? Or use flags
5301 for all the cleanups and let the optimizer tighten them up? The current
5302 code seems pretty fragile; it will break on a cleanup within any
5303 non-conditional nesting. But any such nesting would be broken, anyway;
5304 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5305 and continues out of it. We can do that at the RTL level, though, so
5306 having an optimizer to tighten up try/finally regions would be a Good
5307 Thing. */
5309 static enum gimplify_status
5310 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5312 gimple_stmt_iterator iter;
5313 gimple_seq body_sequence = NULL;
5315 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5317 /* We only care about the number of conditions between the innermost
5318 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5319 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5320 int old_conds = gimplify_ctxp->conditions;
5321 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5322 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5323 gimplify_ctxp->conditions = 0;
5324 gimplify_ctxp->conditional_cleanups = NULL;
5325 gimplify_ctxp->in_cleanup_point_expr = true;
5327 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5329 gimplify_ctxp->conditions = old_conds;
5330 gimplify_ctxp->conditional_cleanups = old_cleanups;
5331 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5333 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5335 gimple *wce = gsi_stmt (iter);
5337 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5339 if (gsi_one_before_end_p (iter))
5341 /* Note that gsi_insert_seq_before and gsi_remove do not
5342 scan operands, unlike some other sequence mutators. */
5343 if (!gimple_wce_cleanup_eh_only (wce))
5344 gsi_insert_seq_before_without_update (&iter,
5345 gimple_wce_cleanup (wce),
5346 GSI_SAME_STMT);
5347 gsi_remove (&iter, true);
5348 break;
5350 else
5352 gtry *gtry;
5353 gimple_seq seq;
5354 enum gimple_try_flags kind;
5356 if (gimple_wce_cleanup_eh_only (wce))
5357 kind = GIMPLE_TRY_CATCH;
5358 else
5359 kind = GIMPLE_TRY_FINALLY;
5360 seq = gsi_split_seq_after (iter);
5362 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5363 /* Do not use gsi_replace here, as it may scan operands.
5364 We want to do a simple structural modification only. */
5365 gsi_set_stmt (&iter, gtry);
5366 iter = gsi_start (gtry->eval);
5369 else
5370 gsi_next (&iter);
5373 gimplify_seq_add_seq (pre_p, body_sequence);
5374 if (temp)
5376 *expr_p = temp;
5377 return GS_OK;
5379 else
5381 *expr_p = NULL;
5382 return GS_ALL_DONE;
5386 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5387 is the cleanup action required. EH_ONLY is true if the cleanup should
5388 only be executed if an exception is thrown, not on normal exit. */
5390 static void
5391 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5393 gimple *wce;
5394 gimple_seq cleanup_stmts = NULL;
5396 /* Errors can result in improperly nested cleanups. Which results in
5397 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5398 if (seen_error ())
5399 return;
5401 if (gimple_conditional_context ())
5403 /* If we're in a conditional context, this is more complex. We only
5404 want to run the cleanup if we actually ran the initialization that
5405 necessitates it, but we want to run it after the end of the
5406 conditional context. So we wrap the try/finally around the
5407 condition and use a flag to determine whether or not to actually
5408 run the destructor. Thus
5410 test ? f(A()) : 0
5412 becomes (approximately)
5414 flag = 0;
5415 try {
5416 if (test) { A::A(temp); flag = 1; val = f(temp); }
5417 else { val = 0; }
5418 } finally {
5419 if (flag) A::~A(temp);
5423 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5424 gassign *ffalse = gimple_build_assign (flag, boolean_false_node);
5425 gassign *ftrue = gimple_build_assign (flag, boolean_true_node);
5427 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5428 gimplify_stmt (&cleanup, &cleanup_stmts);
5429 wce = gimple_build_wce (cleanup_stmts);
5431 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5432 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5433 gimplify_seq_add_stmt (pre_p, ftrue);
5435 /* Because of this manipulation, and the EH edges that jump
5436 threading cannot redirect, the temporary (VAR) will appear
5437 to be used uninitialized. Don't warn. */
5438 TREE_NO_WARNING (var) = 1;
5440 else
5442 gimplify_stmt (&cleanup, &cleanup_stmts);
5443 wce = gimple_build_wce (cleanup_stmts);
5444 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5445 gimplify_seq_add_stmt (pre_p, wce);
5449 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5451 static enum gimplify_status
5452 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5454 tree targ = *expr_p;
5455 tree temp = TARGET_EXPR_SLOT (targ);
5456 tree init = TARGET_EXPR_INITIAL (targ);
5457 enum gimplify_status ret;
5459 if (init)
5461 tree cleanup = NULL_TREE;
5463 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5464 to the temps list. Handle also variable length TARGET_EXPRs. */
5465 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5467 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5468 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5469 gimplify_vla_decl (temp, pre_p);
5471 else
5472 gimple_add_tmp_var (temp);
5474 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5475 expression is supposed to initialize the slot. */
5476 if (VOID_TYPE_P (TREE_TYPE (init)))
5477 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5478 else
5480 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5481 init = init_expr;
5482 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5483 init = NULL;
5484 ggc_free (init_expr);
5486 if (ret == GS_ERROR)
5488 /* PR c++/28266 Make sure this is expanded only once. */
5489 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5490 return GS_ERROR;
5492 if (init)
5493 gimplify_and_add (init, pre_p);
5495 /* If needed, push the cleanup for the temp. */
5496 if (TARGET_EXPR_CLEANUP (targ))
5498 if (CLEANUP_EH_ONLY (targ))
5499 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5500 CLEANUP_EH_ONLY (targ), pre_p);
5501 else
5502 cleanup = TARGET_EXPR_CLEANUP (targ);
5505 /* Add a clobber for the temporary going out of scope, like
5506 gimplify_bind_expr. */
5507 if (gimplify_ctxp->in_cleanup_point_expr
5508 && needs_to_live_in_memory (temp)
5509 && flag_stack_reuse == SR_ALL)
5511 tree clobber = build_constructor (TREE_TYPE (temp),
5512 NULL);
5513 TREE_THIS_VOLATILE (clobber) = true;
5514 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5515 if (cleanup)
5516 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5517 clobber);
5518 else
5519 cleanup = clobber;
5522 if (cleanup)
5523 gimple_push_cleanup (temp, cleanup, false, pre_p);
5525 /* Only expand this once. */
5526 TREE_OPERAND (targ, 3) = init;
5527 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5529 else
5530 /* We should have expanded this before. */
5531 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5533 *expr_p = temp;
5534 return GS_OK;
5537 /* Gimplification of expression trees. */
5539 /* Gimplify an expression which appears at statement context. The
5540 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5541 NULL, a new sequence is allocated.
5543 Return true if we actually added a statement to the queue. */
5545 bool
5546 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5548 gimple_seq_node last;
5550 last = gimple_seq_last (*seq_p);
5551 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5552 return last != gimple_seq_last (*seq_p);
5555 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5556 to CTX. If entries already exist, force them to be some flavor of private.
5557 If there is no enclosing parallel, do nothing. */
5559 void
5560 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5562 splay_tree_node n;
5564 if (decl == NULL || !DECL_P (decl) || ctx->region_type == ORT_NONE)
5565 return;
5569 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5570 if (n != NULL)
5572 if (n->value & GOVD_SHARED)
5573 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5574 else if (n->value & GOVD_MAP)
5575 n->value |= GOVD_MAP_TO_ONLY;
5576 else
5577 return;
5579 else if ((ctx->region_type & ORT_TARGET) != 0)
5581 if (ctx->target_map_scalars_firstprivate)
5582 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5583 else
5584 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5586 else if (ctx->region_type != ORT_WORKSHARE
5587 && ctx->region_type != ORT_SIMD
5588 && ctx->region_type != ORT_TARGET_DATA)
5589 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5591 ctx = ctx->outer_context;
5593 while (ctx);
5596 /* Similarly for each of the type sizes of TYPE. */
5598 static void
5599 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5601 if (type == NULL || type == error_mark_node)
5602 return;
5603 type = TYPE_MAIN_VARIANT (type);
5605 if (ctx->privatized_types->add (type))
5606 return;
5608 switch (TREE_CODE (type))
5610 case INTEGER_TYPE:
5611 case ENUMERAL_TYPE:
5612 case BOOLEAN_TYPE:
5613 case REAL_TYPE:
5614 case FIXED_POINT_TYPE:
5615 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5616 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5617 break;
5619 case ARRAY_TYPE:
5620 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5621 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5622 break;
5624 case RECORD_TYPE:
5625 case UNION_TYPE:
5626 case QUAL_UNION_TYPE:
5628 tree field;
5629 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5630 if (TREE_CODE (field) == FIELD_DECL)
5632 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5633 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5636 break;
5638 case POINTER_TYPE:
5639 case REFERENCE_TYPE:
5640 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5641 break;
5643 default:
5644 break;
5647 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5648 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5649 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5652 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5654 static void
5655 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5657 splay_tree_node n;
5658 unsigned int nflags;
5659 tree t;
5661 if (error_operand_p (decl) || ctx->region_type == ORT_NONE)
5662 return;
5664 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5665 there are constructors involved somewhere. */
5666 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5667 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5668 flags |= GOVD_SEEN;
5670 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5671 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5673 /* We shouldn't be re-adding the decl with the same data
5674 sharing class. */
5675 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5676 /* The only combination of data sharing classes we should see is
5677 FIRSTPRIVATE and LASTPRIVATE. */
5678 nflags = n->value | flags;
5679 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5680 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5681 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5682 n->value = nflags;
5683 return;
5686 /* When adding a variable-sized variable, we have to handle all sorts
5687 of additional bits of data: the pointer replacement variable, and
5688 the parameters of the type. */
5689 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5691 /* Add the pointer replacement variable as PRIVATE if the variable
5692 replacement is private, else FIRSTPRIVATE since we'll need the
5693 address of the original variable either for SHARED, or for the
5694 copy into or out of the context. */
5695 if (!(flags & GOVD_LOCAL))
5697 if (flags & GOVD_MAP)
5698 nflags = GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT;
5699 else if (flags & GOVD_PRIVATE)
5700 nflags = GOVD_PRIVATE;
5701 else if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0
5702 && (flags & GOVD_FIRSTPRIVATE))
5703 nflags = GOVD_PRIVATE | GOVD_EXPLICIT;
5704 else
5705 nflags = GOVD_FIRSTPRIVATE;
5706 nflags |= flags & GOVD_SEEN;
5707 t = DECL_VALUE_EXPR (decl);
5708 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5709 t = TREE_OPERAND (t, 0);
5710 gcc_assert (DECL_P (t));
5711 omp_add_variable (ctx, t, nflags);
5714 /* Add all of the variable and type parameters (which should have
5715 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5716 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5717 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5718 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5720 /* The variable-sized variable itself is never SHARED, only some form
5721 of PRIVATE. The sharing would take place via the pointer variable
5722 which we remapped above. */
5723 if (flags & GOVD_SHARED)
5724 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5725 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5727 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5728 alloca statement we generate for the variable, so make sure it
5729 is available. This isn't automatically needed for the SHARED
5730 case, since we won't be allocating local storage then.
5731 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5732 in this case omp_notice_variable will be called later
5733 on when it is gimplified. */
5734 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5735 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5736 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5738 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5739 && lang_hooks.decls.omp_privatize_by_reference (decl))
5741 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5743 /* Similar to the direct variable sized case above, we'll need the
5744 size of references being privatized. */
5745 if ((flags & GOVD_SHARED) == 0)
5747 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5748 if (DECL_P (t))
5749 omp_notice_variable (ctx, t, true);
5753 if (n != NULL)
5754 n->value |= flags;
5755 else
5756 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5759 /* Notice a threadprivate variable DECL used in OMP context CTX.
5760 This just prints out diagnostics about threadprivate variable uses
5761 in untied tasks. If DECL2 is non-NULL, prevent this warning
5762 on that variable. */
5764 static bool
5765 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5766 tree decl2)
5768 splay_tree_node n;
5769 struct gimplify_omp_ctx *octx;
5771 for (octx = ctx; octx; octx = octx->outer_context)
5772 if ((octx->region_type & ORT_TARGET) != 0)
5774 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5775 if (n == NULL)
5777 error ("threadprivate variable %qE used in target region",
5778 DECL_NAME (decl));
5779 error_at (octx->location, "enclosing target region");
5780 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5782 if (decl2)
5783 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5786 if (ctx->region_type != ORT_UNTIED_TASK)
5787 return false;
5788 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5789 if (n == NULL)
5791 error ("threadprivate variable %qE used in untied task",
5792 DECL_NAME (decl));
5793 error_at (ctx->location, "enclosing task");
5794 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5796 if (decl2)
5797 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5798 return false;
5801 /* Determine outer default flags for DECL mentioned in an OMP region
5802 but not declared in an enclosing clause.
5804 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5805 remapped firstprivate instead of shared. To some extent this is
5806 addressed in omp_firstprivatize_type_sizes, but not
5807 effectively. */
5809 static unsigned
5810 omp_default_clause (struct gimplify_omp_ctx *ctx, tree decl,
5811 bool in_code, unsigned flags)
5813 enum omp_clause_default_kind default_kind = ctx->default_kind;
5814 enum omp_clause_default_kind kind;
5816 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5817 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5818 default_kind = kind;
5820 switch (default_kind)
5822 case OMP_CLAUSE_DEFAULT_NONE:
5824 const char *rtype;
5826 if (ctx->region_type & ORT_PARALLEL)
5827 rtype = "parallel";
5828 else if (ctx->region_type & ORT_TASK)
5829 rtype = "task";
5830 else if (ctx->region_type & ORT_TEAMS)
5831 rtype = "teams";
5832 else
5833 gcc_unreachable ();
5835 error ("%qE not specified in enclosing %s",
5836 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)), rtype);
5837 error_at (ctx->location, "enclosing %s", rtype);
5839 /* FALLTHRU */
5840 case OMP_CLAUSE_DEFAULT_SHARED:
5841 flags |= GOVD_SHARED;
5842 break;
5843 case OMP_CLAUSE_DEFAULT_PRIVATE:
5844 flags |= GOVD_PRIVATE;
5845 break;
5846 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5847 flags |= GOVD_FIRSTPRIVATE;
5848 break;
5849 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5850 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5851 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5852 if (struct gimplify_omp_ctx *octx = ctx->outer_context)
5854 omp_notice_variable (octx, decl, in_code);
5855 for (; octx; octx = octx->outer_context)
5857 splay_tree_node n2;
5859 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5860 continue;
5861 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5862 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5864 flags |= GOVD_FIRSTPRIVATE;
5865 goto found_outer;
5867 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5869 flags |= GOVD_SHARED;
5870 goto found_outer;
5875 if (TREE_CODE (decl) == PARM_DECL
5876 || (!is_global_var (decl)
5877 && DECL_CONTEXT (decl) == current_function_decl))
5878 flags |= GOVD_FIRSTPRIVATE;
5879 else
5880 flags |= GOVD_SHARED;
5881 found_outer:
5882 break;
5884 default:
5885 gcc_unreachable ();
5888 return flags;
5891 /* Record the fact that DECL was used within the OMP context CTX.
5892 IN_CODE is true when real code uses DECL, and false when we should
5893 merely emit default(none) errors. Return true if DECL is going to
5894 be remapped and thus DECL shouldn't be gimplified into its
5895 DECL_VALUE_EXPR (if any). */
5897 static bool
5898 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5900 splay_tree_node n;
5901 unsigned flags = in_code ? GOVD_SEEN : 0;
5902 bool ret = false, shared;
5904 if (error_operand_p (decl))
5905 return false;
5907 if (ctx->region_type == ORT_NONE)
5908 return lang_hooks.decls.omp_disregard_value_expr (decl, false);
5910 /* Threadprivate variables are predetermined. */
5911 if (is_global_var (decl))
5913 if (DECL_THREAD_LOCAL_P (decl))
5914 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5916 if (DECL_HAS_VALUE_EXPR_P (decl))
5918 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5920 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5921 return omp_notice_threadprivate_variable (ctx, decl, value);
5925 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5926 if ((ctx->region_type & ORT_TARGET) != 0)
5928 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5929 if (n == NULL)
5931 unsigned nflags = flags;
5932 if (ctx->target_map_pointers_as_0len_arrays
5933 || ctx->target_map_scalars_firstprivate)
5935 bool is_declare_target = false;
5936 bool is_scalar = false;
5937 if (is_global_var (decl)
5938 && varpool_node::get_create (decl)->offloadable)
5940 struct gimplify_omp_ctx *octx;
5941 for (octx = ctx->outer_context;
5942 octx; octx = octx->outer_context)
5944 n = splay_tree_lookup (octx->variables,
5945 (splay_tree_key)decl);
5946 if (n
5947 && (n->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED
5948 && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
5949 break;
5951 is_declare_target = octx == NULL;
5953 if (!is_declare_target && ctx->target_map_scalars_firstprivate)
5955 tree type = TREE_TYPE (decl);
5956 if (TREE_CODE (type) == REFERENCE_TYPE)
5957 type = TREE_TYPE (type);
5958 if (TREE_CODE (type) == COMPLEX_TYPE)
5959 type = TREE_TYPE (type);
5960 if (INTEGRAL_TYPE_P (type)
5961 || SCALAR_FLOAT_TYPE_P (type)
5962 || TREE_CODE (type) == POINTER_TYPE)
5963 is_scalar = true;
5965 if (is_declare_target)
5967 else if (ctx->target_map_pointers_as_0len_arrays
5968 && (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
5969 || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
5970 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
5971 == POINTER_TYPE)))
5972 nflags |= GOVD_MAP | GOVD_MAP_0LEN_ARRAY;
5973 else if (is_scalar)
5974 nflags |= GOVD_FIRSTPRIVATE;
5976 if (nflags == flags
5977 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5979 error ("%qD referenced in target region does not have "
5980 "a mappable type", decl);
5981 nflags |= GOVD_MAP | GOVD_EXPLICIT;
5983 else if (nflags == flags)
5984 nflags |= GOVD_MAP;
5985 omp_add_variable (ctx, decl, nflags);
5987 else
5989 /* If nothing changed, there's nothing left to do. */
5990 if ((n->value & flags) == flags)
5991 return ret;
5992 n->value |= flags;
5994 goto do_outer;
5997 if (n == NULL)
5999 if (ctx->region_type == ORT_WORKSHARE
6000 || ctx->region_type == ORT_SIMD
6001 || ctx->region_type == ORT_TARGET_DATA)
6002 goto do_outer;
6004 flags = omp_default_clause (ctx, decl, in_code, flags);
6006 if ((flags & GOVD_PRIVATE)
6007 && lang_hooks.decls.omp_private_outer_ref (decl))
6008 flags |= GOVD_PRIVATE_OUTER_REF;
6010 omp_add_variable (ctx, decl, flags);
6012 shared = (flags & GOVD_SHARED) != 0;
6013 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6014 goto do_outer;
6017 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6018 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6019 && DECL_SIZE (decl)
6020 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6022 splay_tree_node n2;
6023 tree t = DECL_VALUE_EXPR (decl);
6024 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6025 t = TREE_OPERAND (t, 0);
6026 gcc_assert (DECL_P (t));
6027 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6028 n2->value |= GOVD_SEEN;
6031 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6032 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6034 /* If nothing changed, there's nothing left to do. */
6035 if ((n->value & flags) == flags)
6036 return ret;
6037 flags |= n->value;
6038 n->value = flags;
6040 do_outer:
6041 /* If the variable is private in the current context, then we don't
6042 need to propagate anything to an outer context. */
6043 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6044 return ret;
6045 if ((flags & (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6046 == (GOVD_LINEAR | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6047 return ret;
6048 if ((flags & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6049 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6050 == (GOVD_LASTPRIVATE | GOVD_LINEAR_LASTPRIVATE_NO_OUTER))
6051 return ret;
6052 if (ctx->outer_context
6053 && omp_notice_variable (ctx->outer_context, decl, in_code))
6054 return true;
6055 return ret;
6058 /* Verify that DECL is private within CTX. If there's specific information
6059 to the contrary in the innermost scope, generate an error. */
6061 static bool
6062 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, int simd)
6064 splay_tree_node n;
6066 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6067 if (n != NULL)
6069 if (n->value & GOVD_SHARED)
6071 if (ctx == gimplify_omp_ctxp)
6073 if (simd)
6074 error ("iteration variable %qE is predetermined linear",
6075 DECL_NAME (decl));
6076 else
6077 error ("iteration variable %qE should be private",
6078 DECL_NAME (decl));
6079 n->value = GOVD_PRIVATE;
6080 return true;
6082 else
6083 return false;
6085 else if ((n->value & GOVD_EXPLICIT) != 0
6086 && (ctx == gimplify_omp_ctxp
6087 || (ctx->region_type == ORT_COMBINED_PARALLEL
6088 && gimplify_omp_ctxp->outer_context == ctx)))
6090 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6091 error ("iteration variable %qE should not be firstprivate",
6092 DECL_NAME (decl));
6093 else if ((n->value & GOVD_REDUCTION) != 0)
6094 error ("iteration variable %qE should not be reduction",
6095 DECL_NAME (decl));
6096 else if (simd == 1 && (n->value & GOVD_LASTPRIVATE) != 0)
6097 error ("iteration variable %qE should not be lastprivate",
6098 DECL_NAME (decl));
6099 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6100 error ("iteration variable %qE should not be private",
6101 DECL_NAME (decl));
6102 else if (simd == 2 && (n->value & GOVD_LINEAR) != 0)
6103 error ("iteration variable %qE is predetermined linear",
6104 DECL_NAME (decl));
6106 return (ctx == gimplify_omp_ctxp
6107 || (ctx->region_type == ORT_COMBINED_PARALLEL
6108 && gimplify_omp_ctxp->outer_context == ctx));
6111 if (ctx->region_type != ORT_WORKSHARE
6112 && ctx->region_type != ORT_SIMD)
6113 return false;
6114 else if (ctx->outer_context)
6115 return omp_is_private (ctx->outer_context, decl, simd);
6116 return false;
6119 /* Return true if DECL is private within a parallel region
6120 that binds to the current construct's context or in parallel
6121 region's REDUCTION clause. */
6123 static bool
6124 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6126 splay_tree_node n;
6130 ctx = ctx->outer_context;
6131 if (ctx == NULL)
6133 if (is_global_var (decl))
6134 return false;
6136 /* References might be private, but might be shared too,
6137 when checking for copyprivate, assume they might be
6138 private, otherwise assume they might be shared. */
6139 if (copyprivate)
6140 return true;
6142 if (lang_hooks.decls.omp_privatize_by_reference (decl))
6143 return false;
6145 /* Treat C++ privatized non-static data members outside
6146 of the privatization the same. */
6147 if (omp_member_access_dummy_var (decl))
6148 return false;
6150 return true;
6153 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6154 continue;
6156 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6157 if (n != NULL)
6159 if ((n->value & GOVD_LOCAL) != 0
6160 && omp_member_access_dummy_var (decl))
6161 return false;
6162 return (n->value & GOVD_SHARED) == 0;
6165 while (ctx->region_type == ORT_WORKSHARE
6166 || ctx->region_type == ORT_SIMD);
6167 return false;
6170 /* Return true if the CTX is combined with distribute and thus
6171 lastprivate can't be supported. */
6173 static bool
6174 omp_no_lastprivate (struct gimplify_omp_ctx *ctx)
6178 if (ctx->outer_context == NULL)
6179 return false;
6180 ctx = ctx->outer_context;
6181 switch (ctx->region_type)
6183 case ORT_WORKSHARE:
6184 if (!ctx->combined_loop)
6185 return false;
6186 if (ctx->distribute)
6187 return true;
6188 break;
6189 case ORT_COMBINED_PARALLEL:
6190 break;
6191 case ORT_COMBINED_TEAMS:
6192 return true;
6193 default:
6194 return false;
6197 while (1);
6200 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
6202 static tree
6203 find_decl_expr (tree *tp, int *walk_subtrees, void *data)
6205 tree t = *tp;
6207 /* If this node has been visited, unmark it and keep looking. */
6208 if (TREE_CODE (t) == DECL_EXPR && DECL_EXPR_DECL (t) == (tree) data)
6209 return t;
6211 if (IS_TYPE_OR_DECL_P (t))
6212 *walk_subtrees = 0;
6213 return NULL_TREE;
6216 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6217 and previous omp contexts. */
6219 static void
6220 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6221 enum omp_region_type region_type,
6222 enum tree_code code)
6224 struct gimplify_omp_ctx *ctx, *outer_ctx;
6225 tree c;
6226 hash_map<tree, tree> *struct_map_to_clause = NULL;
6227 tree *orig_list_p = list_p;
6229 ctx = new_omp_context (region_type);
6230 outer_ctx = ctx->outer_context;
6231 if (code == OMP_TARGET && !lang_GNU_Fortran ())
6233 ctx->target_map_pointers_as_0len_arrays = true;
6234 /* FIXME: For Fortran we want to set this too, when
6235 the Fortran FE is updated to OpenMP 4.5. */
6236 ctx->target_map_scalars_firstprivate = true;
6238 if (!lang_GNU_Fortran ())
6239 switch (code)
6241 case OMP_TARGET:
6242 case OMP_TARGET_DATA:
6243 case OMP_TARGET_ENTER_DATA:
6244 case OMP_TARGET_EXIT_DATA:
6245 ctx->target_firstprivatize_array_bases = true;
6246 default:
6247 break;
6250 while ((c = *list_p) != NULL)
6252 bool remove = false;
6253 bool notice_outer = true;
6254 const char *check_non_private = NULL;
6255 unsigned int flags;
6256 tree decl;
6258 switch (OMP_CLAUSE_CODE (c))
6260 case OMP_CLAUSE_PRIVATE:
6261 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6262 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6264 flags |= GOVD_PRIVATE_OUTER_REF;
6265 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6267 else
6268 notice_outer = false;
6269 goto do_add;
6270 case OMP_CLAUSE_SHARED:
6271 flags = GOVD_SHARED | GOVD_EXPLICIT;
6272 goto do_add;
6273 case OMP_CLAUSE_FIRSTPRIVATE:
6274 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6275 check_non_private = "firstprivate";
6276 goto do_add;
6277 case OMP_CLAUSE_LASTPRIVATE:
6278 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6279 check_non_private = "lastprivate";
6280 decl = OMP_CLAUSE_DECL (c);
6281 if (omp_no_lastprivate (ctx))
6283 notice_outer = false;
6284 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6286 else if (error_operand_p (decl))
6287 goto do_add;
6288 else if (outer_ctx
6289 && outer_ctx->region_type == ORT_COMBINED_PARALLEL
6290 && splay_tree_lookup (outer_ctx->variables,
6291 (splay_tree_key) decl) == NULL)
6292 omp_add_variable (outer_ctx, decl, GOVD_SHARED | GOVD_SEEN);
6293 else if (outer_ctx
6294 && (outer_ctx->region_type & ORT_TASK) != 0
6295 && outer_ctx->combined_loop
6296 && splay_tree_lookup (outer_ctx->variables,
6297 (splay_tree_key) decl) == NULL)
6298 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6299 else if (outer_ctx
6300 && outer_ctx->region_type == ORT_WORKSHARE
6301 && outer_ctx->combined_loop
6302 && splay_tree_lookup (outer_ctx->variables,
6303 (splay_tree_key) decl) == NULL
6304 && !omp_check_private (outer_ctx, decl, false))
6306 omp_add_variable (outer_ctx, decl, GOVD_LASTPRIVATE | GOVD_SEEN);
6307 if (outer_ctx->outer_context
6308 && (outer_ctx->outer_context->region_type
6309 == ORT_COMBINED_PARALLEL)
6310 && splay_tree_lookup (outer_ctx->outer_context->variables,
6311 (splay_tree_key) decl) == NULL)
6312 omp_add_variable (outer_ctx->outer_context, decl,
6313 GOVD_SHARED | GOVD_SEEN);
6315 goto do_add;
6316 case OMP_CLAUSE_REDUCTION:
6317 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6318 check_non_private = "reduction";
6319 decl = OMP_CLAUSE_DECL (c);
6320 if (TREE_CODE (decl) == MEM_REF)
6322 tree type = TREE_TYPE (decl);
6323 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type)), pre_p,
6324 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6326 remove = true;
6327 break;
6329 tree v = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
6330 if (DECL_P (v))
6332 omp_firstprivatize_variable (ctx, v);
6333 omp_notice_variable (ctx, v, true);
6335 decl = TREE_OPERAND (decl, 0);
6336 if (TREE_CODE (decl) == ADDR_EXPR
6337 || TREE_CODE (decl) == INDIRECT_REF)
6338 decl = TREE_OPERAND (decl, 0);
6340 goto do_add_decl;
6341 case OMP_CLAUSE_LINEAR:
6342 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6343 is_gimple_val, fb_rvalue) == GS_ERROR)
6345 remove = true;
6346 break;
6348 else
6350 if (code == OMP_SIMD
6351 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6353 struct gimplify_omp_ctx *octx = outer_ctx;
6354 if (octx
6355 && octx->region_type == ORT_WORKSHARE
6356 && octx->combined_loop
6357 && !octx->distribute)
6359 if (octx->outer_context
6360 && (octx->outer_context->region_type
6361 == ORT_COMBINED_PARALLEL))
6362 octx = octx->outer_context->outer_context;
6363 else
6364 octx = octx->outer_context;
6366 if (octx
6367 && octx->region_type == ORT_WORKSHARE
6368 && octx->combined_loop
6369 && octx->distribute
6370 && !lang_GNU_Fortran ())
6372 error_at (OMP_CLAUSE_LOCATION (c),
6373 "%<linear%> clause for variable other than "
6374 "loop iterator specified on construct "
6375 "combined with %<distribute%>");
6376 remove = true;
6377 break;
6380 /* For combined #pragma omp parallel for simd, need to put
6381 lastprivate and perhaps firstprivate too on the
6382 parallel. Similarly for #pragma omp for simd. */
6383 struct gimplify_omp_ctx *octx = outer_ctx;
6384 decl = NULL_TREE;
6385 if (omp_no_lastprivate (ctx))
6386 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6389 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6390 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6391 break;
6392 decl = OMP_CLAUSE_DECL (c);
6393 if (error_operand_p (decl))
6395 decl = NULL_TREE;
6396 break;
6398 flags = GOVD_SEEN;
6399 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c))
6400 flags |= GOVD_FIRSTPRIVATE;
6401 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6402 flags |= GOVD_LASTPRIVATE;
6403 if (octx
6404 && octx->region_type == ORT_WORKSHARE
6405 && octx->combined_loop)
6407 if (octx->outer_context
6408 && (octx->outer_context->region_type
6409 == ORT_COMBINED_PARALLEL
6410 || (octx->outer_context->region_type
6411 == ORT_COMBINED_TEAMS)))
6412 octx = octx->outer_context;
6413 else if (omp_check_private (octx, decl, false))
6414 break;
6416 else if (octx
6417 && (octx->region_type & ORT_TASK) != 0
6418 && octx->combined_loop)
6420 else if (octx
6421 && octx->region_type == ORT_COMBINED_PARALLEL
6422 && ctx->region_type == ORT_WORKSHARE
6423 && octx == outer_ctx)
6424 flags = GOVD_SEEN | GOVD_SHARED;
6425 else if (octx
6426 && octx->region_type == ORT_COMBINED_TARGET)
6427 flags &= ~GOVD_LASTPRIVATE;
6428 else
6429 break;
6430 splay_tree_node on
6431 = splay_tree_lookup (octx->variables,
6432 (splay_tree_key) decl);
6433 if (on && (on->value & GOVD_DATA_SHARE_CLASS) != 0)
6435 octx = NULL;
6436 break;
6438 omp_add_variable (octx, decl, flags);
6439 if (octx->outer_context == NULL)
6440 break;
6441 octx = octx->outer_context;
6443 while (1);
6444 if (octx
6445 && decl
6446 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6447 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)))
6448 omp_notice_variable (octx, decl, true);
6450 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6451 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6452 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6454 notice_outer = false;
6455 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
6457 goto do_add;
6459 case OMP_CLAUSE_MAP:
6460 decl = OMP_CLAUSE_DECL (c);
6461 if (error_operand_p (decl))
6462 remove = true;
6463 switch (code)
6465 case OMP_TARGET:
6466 break;
6467 case OMP_TARGET_DATA:
6468 case OMP_TARGET_ENTER_DATA:
6469 case OMP_TARGET_EXIT_DATA:
6470 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6471 /* For target {,enter ,exit }data only the array slice is
6472 mapped, but not the pointer to it. */
6473 remove = true;
6474 break;
6475 default:
6476 break;
6478 if (remove)
6479 break;
6480 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6481 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6482 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6483 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6484 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6486 remove = true;
6487 break;
6489 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
6490 && TREE_CODE (OMP_CLAUSE_SIZE (c)) != INTEGER_CST)
6492 OMP_CLAUSE_SIZE (c)
6493 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c), pre_p, NULL);
6494 omp_add_variable (ctx, OMP_CLAUSE_SIZE (c),
6495 GOVD_FIRSTPRIVATE | GOVD_SEEN);
6497 if (!DECL_P (decl))
6499 tree d = decl, *pd;
6500 if (TREE_CODE (d) == ARRAY_REF)
6502 while (TREE_CODE (d) == ARRAY_REF)
6503 d = TREE_OPERAND (d, 0);
6504 if (TREE_CODE (d) == COMPONENT_REF
6505 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE)
6506 decl = d;
6508 pd = &OMP_CLAUSE_DECL (c);
6509 if (d == decl
6510 && TREE_CODE (decl) == INDIRECT_REF
6511 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
6512 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
6513 == REFERENCE_TYPE))
6515 pd = &TREE_OPERAND (decl, 0);
6516 decl = TREE_OPERAND (decl, 0);
6518 if (TREE_CODE (decl) == COMPONENT_REF)
6520 while (TREE_CODE (decl) == COMPONENT_REF)
6521 decl = TREE_OPERAND (decl, 0);
6523 if (gimplify_expr (pd, pre_p, NULL, is_gimple_lvalue, fb_lvalue)
6524 == GS_ERROR)
6526 remove = true;
6527 break;
6529 if (DECL_P (decl))
6531 if (error_operand_p (decl))
6533 remove = true;
6534 break;
6537 if (TYPE_SIZE_UNIT (TREE_TYPE (decl)) == NULL
6538 || (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (decl)))
6539 != INTEGER_CST))
6541 error_at (OMP_CLAUSE_LOCATION (c),
6542 "mapping field %qE of variable length "
6543 "structure", OMP_CLAUSE_DECL (c));
6544 remove = true;
6545 break;
6548 tree offset;
6549 HOST_WIDE_INT bitsize, bitpos;
6550 machine_mode mode;
6551 int unsignedp, volatilep = 0;
6552 tree base = OMP_CLAUSE_DECL (c);
6553 while (TREE_CODE (base) == ARRAY_REF)
6554 base = TREE_OPERAND (base, 0);
6555 if (TREE_CODE (base) == INDIRECT_REF)
6556 base = TREE_OPERAND (base, 0);
6557 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
6558 &mode, &unsignedp,
6559 &volatilep, false);
6560 gcc_assert (base == decl
6561 && (offset == NULL_TREE
6562 || TREE_CODE (offset) == INTEGER_CST));
6564 splay_tree_node n
6565 = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6566 bool ptr = (OMP_CLAUSE_MAP_KIND (c)
6567 == GOMP_MAP_FIRSTPRIVATE_POINTER);
6568 if (n == NULL || (n->value & (ptr ? GOVD_PRIVATE
6569 : GOVD_MAP)) == 0)
6571 if (ptr)
6573 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6574 OMP_CLAUSE_PRIVATE);
6575 OMP_CLAUSE_DECL (c2) = decl;
6576 OMP_CLAUSE_CHAIN (c2) = *orig_list_p;
6577 *orig_list_p = c2;
6578 if (struct_map_to_clause == NULL)
6579 struct_map_to_clause = new hash_map<tree, tree>;
6580 tree *osc;
6581 if (n == NULL || (n->value & GOVD_MAP) == 0)
6582 osc = NULL;
6583 else
6584 osc = struct_map_to_clause->get (decl);
6585 if (osc == NULL)
6586 struct_map_to_clause->put (decl,
6587 tree_cons (NULL_TREE,
6589 NULL_TREE));
6590 else
6591 *osc = tree_cons (*osc, c, NULL_TREE);
6592 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6593 goto do_add_decl;
6595 *list_p = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6596 OMP_CLAUSE_MAP);
6597 OMP_CLAUSE_SET_MAP_KIND (*list_p, GOMP_MAP_STRUCT);
6598 OMP_CLAUSE_DECL (*list_p) = decl;
6599 OMP_CLAUSE_SIZE (*list_p) = size_int (1);
6600 OMP_CLAUSE_CHAIN (*list_p) = c;
6601 if (struct_map_to_clause == NULL)
6602 struct_map_to_clause = new hash_map<tree, tree>;
6603 struct_map_to_clause->put (decl, *list_p);
6604 list_p = &OMP_CLAUSE_CHAIN (*list_p);
6605 flags = GOVD_MAP | GOVD_EXPLICIT;
6606 if (OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS)
6607 flags |= GOVD_SEEN;
6608 goto do_add_decl;
6610 else
6612 tree *osc = struct_map_to_clause->get (decl);
6613 tree *sc = NULL, *pt = NULL;
6614 if (!ptr && TREE_CODE (*osc) == TREE_LIST)
6615 osc = &TREE_PURPOSE (*osc);
6616 if (OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS)
6617 n->value |= GOVD_SEEN;
6618 offset_int o1, o2;
6619 if (offset)
6620 o1 = wi::to_offset (offset);
6621 else
6622 o1 = 0;
6623 if (bitpos)
6624 o1 = o1 + bitpos / BITS_PER_UNIT;
6625 if (ptr)
6626 pt = osc;
6627 else
6628 sc = &OMP_CLAUSE_CHAIN (*osc);
6629 for (; ptr ? (*pt && (sc = &TREE_VALUE (*pt)))
6630 : *sc != c;
6631 ptr ? (pt = &TREE_CHAIN (*pt))
6632 : (sc = &OMP_CLAUSE_CHAIN (*sc)))
6633 if (TREE_CODE (OMP_CLAUSE_DECL (*sc)) != COMPONENT_REF
6634 && (TREE_CODE (OMP_CLAUSE_DECL (*sc))
6635 != INDIRECT_REF)
6636 && TREE_CODE (OMP_CLAUSE_DECL (*sc)) != ARRAY_REF)
6637 break;
6638 else
6640 tree offset2;
6641 HOST_WIDE_INT bitsize2, bitpos2;
6642 base = OMP_CLAUSE_DECL (*sc);
6643 if (TREE_CODE (base) == ARRAY_REF)
6645 while (TREE_CODE (base) == ARRAY_REF)
6646 base = TREE_OPERAND (base, 0);
6647 if (TREE_CODE (base) != COMPONENT_REF
6648 || (TREE_CODE (TREE_TYPE (base))
6649 != ARRAY_TYPE))
6650 break;
6652 else if (TREE_CODE (base) == INDIRECT_REF
6653 && (TREE_CODE (TREE_OPERAND (base, 0))
6654 == COMPONENT_REF)
6655 && (TREE_CODE (TREE_TYPE
6656 (TREE_OPERAND (base, 0)))
6657 == REFERENCE_TYPE))
6658 base = TREE_OPERAND (base, 0);
6659 base = get_inner_reference (base, &bitsize2,
6660 &bitpos2, &offset2,
6661 &mode, &unsignedp,
6662 &volatilep, false);
6663 if (base != decl)
6664 break;
6665 gcc_assert (offset == NULL_TREE
6666 || TREE_CODE (offset) == INTEGER_CST);
6667 tree d1 = OMP_CLAUSE_DECL (*sc);
6668 tree d2 = OMP_CLAUSE_DECL (c);
6669 while (TREE_CODE (d1) == ARRAY_REF)
6670 d1 = TREE_OPERAND (d1, 0);
6671 while (TREE_CODE (d2) == ARRAY_REF)
6672 d2 = TREE_OPERAND (d2, 0);
6673 if (TREE_CODE (d1) == INDIRECT_REF)
6674 d1 = TREE_OPERAND (d1, 0);
6675 if (TREE_CODE (d2) == INDIRECT_REF)
6676 d2 = TREE_OPERAND (d2, 0);
6677 while (TREE_CODE (d1) == COMPONENT_REF)
6678 if (TREE_CODE (d2) == COMPONENT_REF
6679 && TREE_OPERAND (d1, 1)
6680 == TREE_OPERAND (d2, 1))
6682 d1 = TREE_OPERAND (d1, 0);
6683 d2 = TREE_OPERAND (d2, 0);
6685 else
6686 break;
6687 if (d1 == d2)
6689 error_at (OMP_CLAUSE_LOCATION (c),
6690 "%qE appears more than once in map "
6691 "clauses", OMP_CLAUSE_DECL (c));
6692 remove = true;
6693 break;
6695 if (offset2)
6696 o2 = wi::to_offset (offset2);
6697 else
6698 o2 = 0;
6699 if (bitpos2)
6700 o2 = o2 + bitpos2 / BITS_PER_UNIT;
6701 if (wi::ltu_p (o1, o2)
6702 || (wi::eq_p (o1, o2) && bitpos < bitpos2))
6703 break;
6705 if (ptr)
6707 if (!remove)
6708 *pt = tree_cons (TREE_PURPOSE (*osc), c, *pt);
6709 break;
6711 if (!remove)
6712 OMP_CLAUSE_SIZE (*osc)
6713 = size_binop (PLUS_EXPR, OMP_CLAUSE_SIZE (*osc),
6714 size_one_node);
6715 if (!remove && *sc != c)
6717 *list_p = OMP_CLAUSE_CHAIN (c);
6718 OMP_CLAUSE_CHAIN (c) = *sc;
6719 *sc = c;
6720 continue;
6724 break;
6726 flags = GOVD_MAP | GOVD_EXPLICIT;
6727 goto do_add;
6729 case OMP_CLAUSE_DEPEND:
6730 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
6731 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
6733 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
6734 omp-low.c. */
6735 break;
6737 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6739 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6740 NULL, is_gimple_val, fb_rvalue);
6741 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6743 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6745 remove = true;
6746 break;
6748 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6749 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6750 is_gimple_val, fb_rvalue) == GS_ERROR)
6752 remove = true;
6753 break;
6755 break;
6757 case OMP_CLAUSE_TO:
6758 case OMP_CLAUSE_FROM:
6759 case OMP_CLAUSE__CACHE_:
6760 decl = OMP_CLAUSE_DECL (c);
6761 if (error_operand_p (decl))
6763 remove = true;
6764 break;
6766 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6767 OMP_CLAUSE_SIZE (c) = DECL_P (decl) ? DECL_SIZE_UNIT (decl)
6768 : TYPE_SIZE_UNIT (TREE_TYPE (decl));
6769 if (gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6770 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6772 remove = true;
6773 break;
6775 if (!DECL_P (decl))
6777 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6778 NULL, is_gimple_lvalue, fb_lvalue)
6779 == GS_ERROR)
6781 remove = true;
6782 break;
6784 break;
6786 goto do_notice;
6788 case OMP_CLAUSE_USE_DEVICE_PTR:
6789 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6790 goto do_add;
6791 case OMP_CLAUSE_IS_DEVICE_PTR:
6792 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6793 goto do_add;
6795 do_add:
6796 decl = OMP_CLAUSE_DECL (c);
6797 do_add_decl:
6798 if (error_operand_p (decl))
6800 remove = true;
6801 break;
6803 if (DECL_NAME (decl) == NULL_TREE && (flags & GOVD_SHARED) == 0)
6805 tree t = omp_member_access_dummy_var (decl);
6806 if (t)
6808 tree v = DECL_VALUE_EXPR (decl);
6809 DECL_NAME (decl) = DECL_NAME (TREE_OPERAND (v, 1));
6810 if (outer_ctx)
6811 omp_notice_variable (outer_ctx, t, true);
6814 omp_add_variable (ctx, decl, flags);
6815 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6816 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6818 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6819 GOVD_LOCAL | GOVD_SEEN);
6820 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
6821 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6822 find_decl_expr,
6823 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6824 NULL) == NULL_TREE)
6825 omp_add_variable (ctx,
6826 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c),
6827 GOVD_LOCAL | GOVD_SEEN);
6828 gimplify_omp_ctxp = ctx;
6829 push_gimplify_context ();
6831 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6832 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6834 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6835 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6836 pop_gimplify_context
6837 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6838 push_gimplify_context ();
6839 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6840 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6841 pop_gimplify_context
6842 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6843 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6844 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6846 gimplify_omp_ctxp = outer_ctx;
6848 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6849 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6851 gimplify_omp_ctxp = ctx;
6852 push_gimplify_context ();
6853 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6855 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6856 NULL, NULL);
6857 TREE_SIDE_EFFECTS (bind) = 1;
6858 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6859 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6861 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6862 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6863 pop_gimplify_context
6864 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6865 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6867 gimplify_omp_ctxp = outer_ctx;
6869 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6870 && OMP_CLAUSE_LINEAR_STMT (c))
6872 gimplify_omp_ctxp = ctx;
6873 push_gimplify_context ();
6874 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c)) != BIND_EXPR)
6876 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6877 NULL, NULL);
6878 TREE_SIDE_EFFECTS (bind) = 1;
6879 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LINEAR_STMT (c);
6880 OMP_CLAUSE_LINEAR_STMT (c) = bind;
6882 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c),
6883 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c));
6884 pop_gimplify_context
6885 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c)));
6886 OMP_CLAUSE_LINEAR_STMT (c) = NULL_TREE;
6888 gimplify_omp_ctxp = outer_ctx;
6890 if (notice_outer)
6891 goto do_notice;
6892 break;
6894 case OMP_CLAUSE_COPYIN:
6895 case OMP_CLAUSE_COPYPRIVATE:
6896 decl = OMP_CLAUSE_DECL (c);
6897 if (error_operand_p (decl))
6899 remove = true;
6900 break;
6902 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6903 && !remove
6904 && !omp_check_private (ctx, decl, true))
6906 remove = true;
6907 if (is_global_var (decl))
6909 if (DECL_THREAD_LOCAL_P (decl))
6910 remove = false;
6911 else if (DECL_HAS_VALUE_EXPR_P (decl))
6913 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6915 if (value
6916 && DECL_P (value)
6917 && DECL_THREAD_LOCAL_P (value))
6918 remove = false;
6921 if (remove)
6922 error_at (OMP_CLAUSE_LOCATION (c),
6923 "copyprivate variable %qE is not threadprivate"
6924 " or private in outer context", DECL_NAME (decl));
6926 do_notice:
6927 if (outer_ctx)
6928 omp_notice_variable (outer_ctx, decl, true);
6929 if (check_non_private
6930 && region_type == ORT_WORKSHARE
6931 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
6932 || decl == OMP_CLAUSE_DECL (c)
6933 || (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF
6934 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c), 0))
6935 == ADDR_EXPR)))
6936 && omp_check_private (ctx, decl, false))
6938 error ("%s variable %qE is private in outer context",
6939 check_non_private, DECL_NAME (decl));
6940 remove = true;
6942 break;
6944 case OMP_CLAUSE_IF:
6945 if (OMP_CLAUSE_IF_MODIFIER (c) != ERROR_MARK
6946 && OMP_CLAUSE_IF_MODIFIER (c) != code)
6948 const char *p[2];
6949 for (int i = 0; i < 2; i++)
6950 switch (i ? OMP_CLAUSE_IF_MODIFIER (c) : code)
6952 case OMP_PARALLEL: p[i] = "parallel"; break;
6953 case OMP_TASK: p[i] = "task"; break;
6954 case OMP_TASKLOOP: p[i] = "taskloop"; break;
6955 case OMP_TARGET_DATA: p[i] = "target data"; break;
6956 case OMP_TARGET: p[i] = "target"; break;
6957 case OMP_TARGET_UPDATE: p[i] = "target update"; break;
6958 case OMP_TARGET_ENTER_DATA:
6959 p[i] = "target enter data"; break;
6960 case OMP_TARGET_EXIT_DATA: p[i] = "target exit data"; break;
6961 default: gcc_unreachable ();
6963 error_at (OMP_CLAUSE_LOCATION (c),
6964 "expected %qs %<if%> clause modifier rather than %qs",
6965 p[0], p[1]);
6966 remove = true;
6968 /* Fall through. */
6970 case OMP_CLAUSE_FINAL:
6971 OMP_CLAUSE_OPERAND (c, 0)
6972 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6973 /* Fall through. */
6975 case OMP_CLAUSE_SCHEDULE:
6976 case OMP_CLAUSE_NUM_THREADS:
6977 case OMP_CLAUSE_NUM_TEAMS:
6978 case OMP_CLAUSE_THREAD_LIMIT:
6979 case OMP_CLAUSE_DIST_SCHEDULE:
6980 case OMP_CLAUSE_DEVICE:
6981 case OMP_CLAUSE_PRIORITY:
6982 case OMP_CLAUSE_GRAINSIZE:
6983 case OMP_CLAUSE_NUM_TASKS:
6984 case OMP_CLAUSE_HINT:
6985 case OMP_CLAUSE__CILK_FOR_COUNT_:
6986 case OMP_CLAUSE_ASYNC:
6987 case OMP_CLAUSE_WAIT:
6988 case OMP_CLAUSE_NUM_GANGS:
6989 case OMP_CLAUSE_NUM_WORKERS:
6990 case OMP_CLAUSE_VECTOR_LENGTH:
6991 case OMP_CLAUSE_WORKER:
6992 case OMP_CLAUSE_VECTOR:
6993 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6994 is_gimple_val, fb_rvalue) == GS_ERROR)
6995 remove = true;
6996 break;
6998 case OMP_CLAUSE_GANG:
6999 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
7000 is_gimple_val, fb_rvalue) == GS_ERROR)
7001 remove = true;
7002 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 1), pre_p, NULL,
7003 is_gimple_val, fb_rvalue) == GS_ERROR)
7004 remove = true;
7005 break;
7007 case OMP_CLAUSE_DEVICE_RESIDENT:
7008 case OMP_CLAUSE_USE_DEVICE:
7009 case OMP_CLAUSE_INDEPENDENT:
7010 remove = true;
7011 break;
7013 case OMP_CLAUSE_NOWAIT:
7014 case OMP_CLAUSE_ORDERED:
7015 case OMP_CLAUSE_UNTIED:
7016 case OMP_CLAUSE_COLLAPSE:
7017 case OMP_CLAUSE_AUTO:
7018 case OMP_CLAUSE_SEQ:
7019 case OMP_CLAUSE_MERGEABLE:
7020 case OMP_CLAUSE_PROC_BIND:
7021 case OMP_CLAUSE_SAFELEN:
7022 case OMP_CLAUSE_SIMDLEN:
7023 case OMP_CLAUSE_NOGROUP:
7024 case OMP_CLAUSE_THREADS:
7025 case OMP_CLAUSE_SIMD:
7026 break;
7028 case OMP_CLAUSE_DEFAULTMAP:
7029 ctx->target_map_scalars_firstprivate = false;
7030 break;
7032 case OMP_CLAUSE_ALIGNED:
7033 decl = OMP_CLAUSE_DECL (c);
7034 if (error_operand_p (decl))
7036 remove = true;
7037 break;
7039 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c), pre_p, NULL,
7040 is_gimple_val, fb_rvalue) == GS_ERROR)
7042 remove = true;
7043 break;
7045 if (!is_global_var (decl)
7046 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7047 omp_add_variable (ctx, decl, GOVD_ALIGNED);
7048 break;
7050 case OMP_CLAUSE_DEFAULT:
7051 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
7052 break;
7054 default:
7055 gcc_unreachable ();
7058 if (remove)
7059 *list_p = OMP_CLAUSE_CHAIN (c);
7060 else
7061 list_p = &OMP_CLAUSE_CHAIN (c);
7064 gimplify_omp_ctxp = ctx;
7065 if (struct_map_to_clause)
7066 delete struct_map_to_clause;
7069 struct gimplify_adjust_omp_clauses_data
7071 tree *list_p;
7072 gimple_seq *pre_p;
7075 /* For all variables that were not actually used within the context,
7076 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
7078 static int
7079 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
7081 tree *list_p = ((struct gimplify_adjust_omp_clauses_data *) data)->list_p;
7082 gimple_seq *pre_p
7083 = ((struct gimplify_adjust_omp_clauses_data *) data)->pre_p;
7084 tree decl = (tree) n->key;
7085 unsigned flags = n->value;
7086 enum omp_clause_code code;
7087 tree clause;
7088 bool private_debug;
7090 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
7091 return 0;
7092 if ((flags & GOVD_SEEN) == 0)
7093 return 0;
7094 if (flags & GOVD_DEBUG_PRIVATE)
7096 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
7097 private_debug = true;
7099 else if (flags & GOVD_MAP)
7100 private_debug = false;
7101 else
7102 private_debug
7103 = lang_hooks.decls.omp_private_debug_clause (decl,
7104 !!(flags & GOVD_SHARED));
7105 if (private_debug)
7106 code = OMP_CLAUSE_PRIVATE;
7107 else if (flags & GOVD_MAP)
7108 code = OMP_CLAUSE_MAP;
7109 else if (flags & GOVD_SHARED)
7111 if (is_global_var (decl))
7113 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7114 while (ctx != NULL)
7116 splay_tree_node on
7117 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7118 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
7119 | GOVD_PRIVATE | GOVD_REDUCTION
7120 | GOVD_LINEAR | GOVD_MAP)) != 0)
7121 break;
7122 ctx = ctx->outer_context;
7124 if (ctx == NULL)
7125 return 0;
7127 code = OMP_CLAUSE_SHARED;
7129 else if (flags & GOVD_PRIVATE)
7130 code = OMP_CLAUSE_PRIVATE;
7131 else if (flags & GOVD_FIRSTPRIVATE)
7132 code = OMP_CLAUSE_FIRSTPRIVATE;
7133 else if (flags & GOVD_LASTPRIVATE)
7134 code = OMP_CLAUSE_LASTPRIVATE;
7135 else if (flags & GOVD_ALIGNED)
7136 return 0;
7137 else
7138 gcc_unreachable ();
7140 clause = build_omp_clause (input_location, code);
7141 OMP_CLAUSE_DECL (clause) = decl;
7142 OMP_CLAUSE_CHAIN (clause) = *list_p;
7143 if (private_debug)
7144 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
7145 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
7146 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
7147 else if (code == OMP_CLAUSE_MAP && (flags & GOVD_MAP_0LEN_ARRAY) != 0)
7149 tree nc = build_omp_clause (input_location, OMP_CLAUSE_MAP);
7150 OMP_CLAUSE_DECL (nc) = decl;
7151 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
7152 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == POINTER_TYPE)
7153 OMP_CLAUSE_DECL (clause)
7154 = build_simple_mem_ref_loc (input_location, decl);
7155 OMP_CLAUSE_DECL (clause)
7156 = build2 (MEM_REF, char_type_node, OMP_CLAUSE_DECL (clause),
7157 build_int_cst (build_pointer_type (char_type_node), 0));
7158 OMP_CLAUSE_SIZE (clause) = size_zero_node;
7159 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7160 OMP_CLAUSE_SET_MAP_KIND (clause, GOMP_MAP_ALLOC);
7161 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause) = 1;
7162 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7163 OMP_CLAUSE_CHAIN (nc) = *list_p;
7164 OMP_CLAUSE_CHAIN (clause) = nc;
7165 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7166 gimplify_omp_ctxp = ctx->outer_context;
7167 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause), 0),
7168 pre_p, NULL, is_gimple_val, fb_rvalue);
7169 gimplify_omp_ctxp = ctx;
7171 else if (code == OMP_CLAUSE_MAP)
7173 OMP_CLAUSE_SET_MAP_KIND (clause,
7174 flags & GOVD_MAP_TO_ONLY
7175 ? GOMP_MAP_TO
7176 : GOMP_MAP_TOFROM);
7177 if (DECL_SIZE (decl)
7178 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7180 tree decl2 = DECL_VALUE_EXPR (decl);
7181 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7182 decl2 = TREE_OPERAND (decl2, 0);
7183 gcc_assert (DECL_P (decl2));
7184 tree mem = build_simple_mem_ref (decl2);
7185 OMP_CLAUSE_DECL (clause) = mem;
7186 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7187 if (gimplify_omp_ctxp->outer_context)
7189 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
7190 omp_notice_variable (ctx, decl2, true);
7191 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
7193 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
7194 OMP_CLAUSE_MAP);
7195 OMP_CLAUSE_DECL (nc) = decl;
7196 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7197 if (gimplify_omp_ctxp->target_firstprivatize_array_bases)
7198 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_FIRSTPRIVATE_POINTER);
7199 else
7200 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7201 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
7202 OMP_CLAUSE_CHAIN (clause) = nc;
7204 else
7205 OMP_CLAUSE_SIZE (clause) = DECL_SIZE_UNIT (decl);
7207 if (code == OMP_CLAUSE_FIRSTPRIVATE && (flags & GOVD_LASTPRIVATE) != 0)
7209 tree nc = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
7210 OMP_CLAUSE_DECL (nc) = decl;
7211 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc) = 1;
7212 OMP_CLAUSE_CHAIN (nc) = *list_p;
7213 OMP_CLAUSE_CHAIN (clause) = nc;
7214 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7215 gimplify_omp_ctxp = ctx->outer_context;
7216 lang_hooks.decls.omp_finish_clause (nc, pre_p);
7217 gimplify_omp_ctxp = ctx;
7219 *list_p = clause;
7220 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7221 gimplify_omp_ctxp = ctx->outer_context;
7222 lang_hooks.decls.omp_finish_clause (clause, pre_p);
7223 gimplify_omp_ctxp = ctx;
7224 return 0;
7227 static void
7228 gimplify_adjust_omp_clauses (gimple_seq *pre_p, tree *list_p,
7229 enum tree_code code)
7231 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
7232 tree c, decl;
7234 while ((c = *list_p) != NULL)
7236 splay_tree_node n;
7237 bool remove = false;
7239 switch (OMP_CLAUSE_CODE (c))
7241 case OMP_CLAUSE_PRIVATE:
7242 case OMP_CLAUSE_SHARED:
7243 case OMP_CLAUSE_FIRSTPRIVATE:
7244 case OMP_CLAUSE_LINEAR:
7245 decl = OMP_CLAUSE_DECL (c);
7246 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7247 remove = !(n->value & GOVD_SEEN);
7248 if (! remove)
7250 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
7251 if ((n->value & GOVD_DEBUG_PRIVATE)
7252 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
7254 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
7255 || ((n->value & GOVD_DATA_SHARE_CLASS)
7256 == GOVD_PRIVATE));
7257 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
7258 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
7261 break;
7263 case OMP_CLAUSE_LASTPRIVATE:
7264 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
7265 accurately reflect the presence of a FIRSTPRIVATE clause. */
7266 decl = OMP_CLAUSE_DECL (c);
7267 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7268 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
7269 = (n->value & GOVD_FIRSTPRIVATE) != 0;
7270 if (omp_no_lastprivate (ctx))
7272 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
7273 remove = true;
7274 else
7275 OMP_CLAUSE_CODE (c) = OMP_CLAUSE_PRIVATE;
7277 break;
7279 case OMP_CLAUSE_ALIGNED:
7280 decl = OMP_CLAUSE_DECL (c);
7281 if (!is_global_var (decl))
7283 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7284 remove = n == NULL || !(n->value & GOVD_SEEN);
7285 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
7287 struct gimplify_omp_ctx *octx;
7288 if (n != NULL
7289 && (n->value & (GOVD_DATA_SHARE_CLASS
7290 & ~GOVD_FIRSTPRIVATE)))
7291 remove = true;
7292 else
7293 for (octx = ctx->outer_context; octx;
7294 octx = octx->outer_context)
7296 n = splay_tree_lookup (octx->variables,
7297 (splay_tree_key) decl);
7298 if (n == NULL)
7299 continue;
7300 if (n->value & GOVD_LOCAL)
7301 break;
7302 /* We have to avoid assigning a shared variable
7303 to itself when trying to add
7304 __builtin_assume_aligned. */
7305 if (n->value & GOVD_SHARED)
7307 remove = true;
7308 break;
7313 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7315 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7316 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7317 remove = true;
7319 break;
7321 case OMP_CLAUSE_MAP:
7322 decl = OMP_CLAUSE_DECL (c);
7323 if (!DECL_P (decl))
7325 if ((ctx->region_type & ORT_TARGET) != 0
7326 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7328 if (TREE_CODE (decl) == INDIRECT_REF
7329 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
7330 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl, 0)))
7331 == REFERENCE_TYPE))
7332 decl = TREE_OPERAND (decl, 0);
7333 if (TREE_CODE (decl) == COMPONENT_REF)
7335 while (TREE_CODE (decl) == COMPONENT_REF)
7336 decl = TREE_OPERAND (decl, 0);
7337 if (DECL_P (decl))
7339 n = splay_tree_lookup (ctx->variables,
7340 (splay_tree_key) decl);
7341 if (!(n->value & GOVD_SEEN))
7342 remove = true;
7346 break;
7348 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
7349 if ((ctx->region_type & ORT_TARGET) != 0
7350 && !(n->value & GOVD_SEEN)
7351 && ((OMP_CLAUSE_MAP_KIND (c) & GOMP_MAP_FLAG_ALWAYS) == 0
7352 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT))
7354 remove = true;
7355 /* For struct element mapping, if struct is never referenced
7356 in target block and none of the mapping has always modifier,
7357 remove all the struct element mappings, which immediately
7358 follow the GOMP_MAP_STRUCT map clause. */
7359 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT)
7361 HOST_WIDE_INT cnt = tree_to_shwi (OMP_CLAUSE_SIZE (c));
7362 while (cnt--)
7363 OMP_CLAUSE_CHAIN (c)
7364 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c));
7367 else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_STRUCT
7368 && code == OMP_TARGET_EXIT_DATA)
7369 remove = true;
7370 else if (DECL_SIZE (decl)
7371 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
7372 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_POINTER
7373 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER)
7375 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
7376 for these, TREE_CODE (DECL_SIZE (decl)) will always be
7377 INTEGER_CST. */
7378 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
7380 tree decl2 = DECL_VALUE_EXPR (decl);
7381 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7382 decl2 = TREE_OPERAND (decl2, 0);
7383 gcc_assert (DECL_P (decl2));
7384 tree mem = build_simple_mem_ref (decl2);
7385 OMP_CLAUSE_DECL (c) = mem;
7386 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7387 if (ctx->outer_context)
7389 omp_notice_variable (ctx->outer_context, decl2, true);
7390 omp_notice_variable (ctx->outer_context,
7391 OMP_CLAUSE_SIZE (c), true);
7393 if (((ctx->region_type & ORT_TARGET) != 0
7394 || !ctx->target_firstprivatize_array_bases)
7395 && ((n->value & GOVD_SEEN) == 0
7396 || (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)) == 0))
7398 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7399 OMP_CLAUSE_MAP);
7400 OMP_CLAUSE_DECL (nc) = decl;
7401 OMP_CLAUSE_SIZE (nc) = size_zero_node;
7402 if (ctx->target_firstprivatize_array_bases)
7403 OMP_CLAUSE_SET_MAP_KIND (nc,
7404 GOMP_MAP_FIRSTPRIVATE_POINTER);
7405 else
7406 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_POINTER);
7407 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
7408 OMP_CLAUSE_CHAIN (c) = nc;
7409 c = nc;
7412 else
7414 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7415 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7416 if ((n->value & GOVD_SEEN)
7417 && (n->value & (GOVD_PRIVATE | GOVD_FIRSTPRIVATE)))
7418 OMP_CLAUSE_MAP_PRIVATE (c) = 1;
7420 break;
7422 case OMP_CLAUSE_TO:
7423 case OMP_CLAUSE_FROM:
7424 case OMP_CLAUSE__CACHE_:
7425 decl = OMP_CLAUSE_DECL (c);
7426 if (!DECL_P (decl))
7427 break;
7428 if (DECL_SIZE (decl)
7429 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
7431 tree decl2 = DECL_VALUE_EXPR (decl);
7432 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
7433 decl2 = TREE_OPERAND (decl2, 0);
7434 gcc_assert (DECL_P (decl2));
7435 tree mem = build_simple_mem_ref (decl2);
7436 OMP_CLAUSE_DECL (c) = mem;
7437 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
7438 if (ctx->outer_context)
7440 omp_notice_variable (ctx->outer_context, decl2, true);
7441 omp_notice_variable (ctx->outer_context,
7442 OMP_CLAUSE_SIZE (c), true);
7445 else if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7446 OMP_CLAUSE_SIZE (c) = DECL_SIZE_UNIT (decl);
7447 break;
7449 case OMP_CLAUSE_REDUCTION:
7450 case OMP_CLAUSE_COPYIN:
7451 case OMP_CLAUSE_COPYPRIVATE:
7452 case OMP_CLAUSE_IF:
7453 case OMP_CLAUSE_NUM_THREADS:
7454 case OMP_CLAUSE_NUM_TEAMS:
7455 case OMP_CLAUSE_THREAD_LIMIT:
7456 case OMP_CLAUSE_DIST_SCHEDULE:
7457 case OMP_CLAUSE_DEVICE:
7458 case OMP_CLAUSE_SCHEDULE:
7459 case OMP_CLAUSE_NOWAIT:
7460 case OMP_CLAUSE_ORDERED:
7461 case OMP_CLAUSE_DEFAULT:
7462 case OMP_CLAUSE_UNTIED:
7463 case OMP_CLAUSE_COLLAPSE:
7464 case OMP_CLAUSE_FINAL:
7465 case OMP_CLAUSE_MERGEABLE:
7466 case OMP_CLAUSE_PROC_BIND:
7467 case OMP_CLAUSE_SAFELEN:
7468 case OMP_CLAUSE_SIMDLEN:
7469 case OMP_CLAUSE_DEPEND:
7470 case OMP_CLAUSE_PRIORITY:
7471 case OMP_CLAUSE_GRAINSIZE:
7472 case OMP_CLAUSE_NUM_TASKS:
7473 case OMP_CLAUSE_NOGROUP:
7474 case OMP_CLAUSE_THREADS:
7475 case OMP_CLAUSE_SIMD:
7476 case OMP_CLAUSE_HINT:
7477 case OMP_CLAUSE_DEFAULTMAP:
7478 case OMP_CLAUSE_USE_DEVICE_PTR:
7479 case OMP_CLAUSE_IS_DEVICE_PTR:
7480 case OMP_CLAUSE__CILK_FOR_COUNT_:
7481 case OMP_CLAUSE_ASYNC:
7482 case OMP_CLAUSE_WAIT:
7483 case OMP_CLAUSE_DEVICE_RESIDENT:
7484 case OMP_CLAUSE_USE_DEVICE:
7485 case OMP_CLAUSE_INDEPENDENT:
7486 case OMP_CLAUSE_NUM_GANGS:
7487 case OMP_CLAUSE_NUM_WORKERS:
7488 case OMP_CLAUSE_VECTOR_LENGTH:
7489 case OMP_CLAUSE_GANG:
7490 case OMP_CLAUSE_WORKER:
7491 case OMP_CLAUSE_VECTOR:
7492 case OMP_CLAUSE_AUTO:
7493 case OMP_CLAUSE_SEQ:
7494 break;
7496 default:
7497 gcc_unreachable ();
7500 if (remove)
7501 *list_p = OMP_CLAUSE_CHAIN (c);
7502 else
7503 list_p = &OMP_CLAUSE_CHAIN (c);
7506 /* Add in any implicit data sharing. */
7507 struct gimplify_adjust_omp_clauses_data data;
7508 data.list_p = list_p;
7509 data.pre_p = pre_p;
7510 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, &data);
7512 gimplify_omp_ctxp = ctx->outer_context;
7513 delete_omp_context (ctx);
7516 /* Gimplify OACC_CACHE. */
7518 static void
7519 gimplify_oacc_cache (tree *expr_p, gimple_seq *pre_p)
7521 tree expr = *expr_p;
7523 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr), pre_p, ORT_WORKSHARE,
7524 OACC_CACHE);
7525 gimplify_adjust_omp_clauses (pre_p, &OACC_CACHE_CLAUSES (expr), OACC_CACHE);
7527 /* TODO: Do something sensible with this information. */
7529 *expr_p = NULL_TREE;
7532 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
7533 gimplification of the body, as well as scanning the body for used
7534 variables. We need to do this scan now, because variable-sized
7535 decls will be decomposed during gimplification. */
7537 static void
7538 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
7540 tree expr = *expr_p;
7541 gimple *g;
7542 gimple_seq body = NULL;
7544 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
7545 OMP_PARALLEL_COMBINED (expr)
7546 ? ORT_COMBINED_PARALLEL
7547 : ORT_PARALLEL, OMP_PARALLEL);
7549 push_gimplify_context ();
7551 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
7552 if (gimple_code (g) == GIMPLE_BIND)
7553 pop_gimplify_context (g);
7554 else
7555 pop_gimplify_context (NULL);
7557 gimplify_adjust_omp_clauses (pre_p, &OMP_PARALLEL_CLAUSES (expr),
7558 OMP_PARALLEL);
7560 g = gimple_build_omp_parallel (body,
7561 OMP_PARALLEL_CLAUSES (expr),
7562 NULL_TREE, NULL_TREE);
7563 if (OMP_PARALLEL_COMBINED (expr))
7564 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
7565 gimplify_seq_add_stmt (pre_p, g);
7566 *expr_p = NULL_TREE;
7569 /* Gimplify the contents of an OMP_TASK statement. This involves
7570 gimplification of the body, as well as scanning the body for used
7571 variables. We need to do this scan now, because variable-sized
7572 decls will be decomposed during gimplification. */
7574 static void
7575 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
7577 tree expr = *expr_p;
7578 gimple *g;
7579 gimple_seq body = NULL;
7581 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
7582 find_omp_clause (OMP_TASK_CLAUSES (expr),
7583 OMP_CLAUSE_UNTIED)
7584 ? ORT_UNTIED_TASK : ORT_TASK, OMP_TASK);
7586 push_gimplify_context ();
7588 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
7589 if (gimple_code (g) == GIMPLE_BIND)
7590 pop_gimplify_context (g);
7591 else
7592 pop_gimplify_context (NULL);
7594 gimplify_adjust_omp_clauses (pre_p, &OMP_TASK_CLAUSES (expr), OMP_TASK);
7596 g = gimple_build_omp_task (body,
7597 OMP_TASK_CLAUSES (expr),
7598 NULL_TREE, NULL_TREE,
7599 NULL_TREE, NULL_TREE, NULL_TREE);
7600 gimplify_seq_add_stmt (pre_p, g);
7601 *expr_p = NULL_TREE;
7604 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
7605 with non-NULL OMP_FOR_INIT. */
7607 static tree
7608 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
7610 *walk_subtrees = 0;
7611 switch (TREE_CODE (*tp))
7613 case OMP_FOR:
7614 *walk_subtrees = 1;
7615 /* FALLTHRU */
7616 case OMP_SIMD:
7617 if (OMP_FOR_INIT (*tp) != NULL_TREE)
7618 return *tp;
7619 break;
7620 case BIND_EXPR:
7621 case STATEMENT_LIST:
7622 case OMP_PARALLEL:
7623 *walk_subtrees = 1;
7624 break;
7625 default:
7626 break;
7628 return NULL_TREE;
7631 /* Gimplify the gross structure of an OMP_FOR statement. */
7633 static enum gimplify_status
7634 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
7636 tree for_stmt, orig_for_stmt, inner_for_stmt = NULL_TREE, decl, var, t;
7637 enum gimplify_status ret = GS_ALL_DONE;
7638 enum gimplify_status tret;
7639 gomp_for *gfor;
7640 gimple_seq for_body, for_pre_body;
7641 int i;
7642 bitmap has_decl_expr = NULL;
7643 enum omp_region_type ort = ORT_WORKSHARE;
7645 orig_for_stmt = for_stmt = *expr_p;
7647 switch (TREE_CODE (for_stmt))
7649 case OMP_FOR:
7650 case CILK_FOR:
7651 case OMP_DISTRIBUTE:
7652 case OACC_LOOP:
7653 break;
7654 case OMP_TASKLOOP:
7655 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_UNTIED))
7656 ort = ORT_UNTIED_TASK;
7657 else
7658 ort = ORT_TASK;
7659 break;
7660 case OMP_SIMD:
7661 case CILK_SIMD:
7662 ort = ORT_SIMD;
7663 break;
7664 default:
7665 gcc_unreachable ();
7668 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
7669 clause for the IV. */
7670 if (ort == ORT_SIMD && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7672 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), 0);
7673 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7674 decl = TREE_OPERAND (t, 0);
7675 for (tree c = OMP_FOR_CLAUSES (for_stmt); c; c = OMP_CLAUSE_CHAIN (c))
7676 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7677 && OMP_CLAUSE_DECL (c) == decl)
7679 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7680 break;
7684 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7686 gcc_assert (TREE_CODE (for_stmt) != OACC_LOOP);
7687 inner_for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt),
7688 find_combined_omp_for, NULL, NULL);
7689 if (inner_for_stmt == NULL_TREE)
7691 gcc_assert (seen_error ());
7692 *expr_p = NULL_TREE;
7693 return GS_ERROR;
7697 if (TREE_CODE (for_stmt) != OMP_TASKLOOP)
7698 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, ort,
7699 TREE_CODE (for_stmt));
7701 if (TREE_CODE (for_stmt) == OMP_DISTRIBUTE)
7702 gimplify_omp_ctxp->distribute = true;
7704 /* Handle OMP_FOR_INIT. */
7705 for_pre_body = NULL;
7706 if (ort == ORT_SIMD && OMP_FOR_PRE_BODY (for_stmt))
7708 has_decl_expr = BITMAP_ALLOC (NULL);
7709 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
7710 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
7711 == VAR_DECL)
7713 t = OMP_FOR_PRE_BODY (for_stmt);
7714 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7716 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
7718 tree_stmt_iterator si;
7719 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
7720 tsi_next (&si))
7722 t = tsi_stmt (si);
7723 if (TREE_CODE (t) == DECL_EXPR
7724 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
7725 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
7729 if (OMP_FOR_PRE_BODY (for_stmt))
7731 if (TREE_CODE (for_stmt) != OMP_TASKLOOP || gimplify_omp_ctxp)
7732 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7733 else
7735 struct gimplify_omp_ctx ctx;
7736 memset (&ctx, 0, sizeof (ctx));
7737 ctx.region_type = ORT_NONE;
7738 gimplify_omp_ctxp = &ctx;
7739 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
7740 gimplify_omp_ctxp = NULL;
7743 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
7745 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
7746 for_stmt = inner_for_stmt;
7748 /* For taskloop, need to gimplify the start, end and step before the
7749 taskloop, outside of the taskloop omp context. */
7750 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
7752 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7754 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7755 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7757 TREE_OPERAND (t, 1)
7758 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7759 pre_p, NULL);
7760 tree c = build_omp_clause (input_location,
7761 OMP_CLAUSE_FIRSTPRIVATE);
7762 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7763 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7764 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7767 /* Handle OMP_FOR_COND. */
7768 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7769 if (!is_gimple_constant (TREE_OPERAND (t, 1)))
7771 TREE_OPERAND (t, 1)
7772 = get_initialized_tmp_var (TREE_OPERAND (t, 1),
7773 gimple_seq_empty_p (for_pre_body)
7774 ? pre_p : &for_pre_body, NULL);
7775 tree c = build_omp_clause (input_location,
7776 OMP_CLAUSE_FIRSTPRIVATE);
7777 OMP_CLAUSE_DECL (c) = TREE_OPERAND (t, 1);
7778 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7779 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7782 /* Handle OMP_FOR_INCR. */
7783 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7784 if (TREE_CODE (t) == MODIFY_EXPR)
7786 decl = TREE_OPERAND (t, 0);
7787 t = TREE_OPERAND (t, 1);
7788 tree *tp = &TREE_OPERAND (t, 1);
7789 if (TREE_CODE (t) == PLUS_EXPR && *tp == decl)
7790 tp = &TREE_OPERAND (t, 0);
7792 if (!is_gimple_constant (*tp))
7794 gimple_seq *seq = gimple_seq_empty_p (for_pre_body)
7795 ? pre_p : &for_pre_body;
7796 *tp = get_initialized_tmp_var (*tp, seq, NULL);
7797 tree c = build_omp_clause (input_location,
7798 OMP_CLAUSE_FIRSTPRIVATE);
7799 OMP_CLAUSE_DECL (c) = *tp;
7800 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (orig_for_stmt);
7801 OMP_FOR_CLAUSES (orig_for_stmt) = c;
7806 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt), pre_p, ort,
7807 OMP_TASKLOOP);
7810 if (orig_for_stmt != for_stmt)
7811 gimplify_omp_ctxp->combined_loop = true;
7813 for_body = NULL;
7814 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7815 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
7816 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7817 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
7819 tree c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_ORDERED);
7820 bool is_doacross = false;
7821 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
7823 is_doacross = true;
7824 gimplify_omp_ctxp->loop_iter_var.create (TREE_VEC_LENGTH
7825 (OMP_FOR_INIT (for_stmt))
7826 * 2);
7828 int collapse = 1;
7829 c = find_omp_clause (OMP_FOR_CLAUSES (for_stmt), OMP_CLAUSE_COLLAPSE);
7830 if (c)
7831 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7832 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7834 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7835 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7836 decl = TREE_OPERAND (t, 0);
7837 gcc_assert (DECL_P (decl));
7838 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
7839 || POINTER_TYPE_P (TREE_TYPE (decl)));
7840 if (is_doacross)
7842 if (TREE_CODE (for_stmt) == OMP_FOR && OMP_FOR_ORIG_DECLS (for_stmt))
7843 gimplify_omp_ctxp->loop_iter_var.quick_push
7844 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt), i));
7845 else
7846 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
7847 gimplify_omp_ctxp->loop_iter_var.quick_push (decl);
7850 /* Make sure the iteration variable is private. */
7851 tree c = NULL_TREE;
7852 tree c2 = NULL_TREE;
7853 if (orig_for_stmt != for_stmt)
7854 /* Do this only on innermost construct for combined ones. */;
7855 else if (ort == ORT_SIMD)
7857 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
7858 (splay_tree_key) decl);
7859 omp_is_private (gimplify_omp_ctxp, decl,
7860 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
7861 != 1));
7862 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
7863 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7864 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
7866 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
7867 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
7868 unsigned int flags = GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN;
7869 if ((has_decl_expr
7870 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7871 || omp_no_lastprivate (gimplify_omp_ctxp))
7873 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7874 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7876 struct gimplify_omp_ctx *outer
7877 = gimplify_omp_ctxp->outer_context;
7878 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7880 if (outer->region_type == ORT_WORKSHARE
7881 && outer->combined_loop)
7883 n = splay_tree_lookup (outer->variables,
7884 (splay_tree_key)decl);
7885 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
7887 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
7888 flags |= GOVD_LINEAR_LASTPRIVATE_NO_OUTER;
7893 OMP_CLAUSE_DECL (c) = decl;
7894 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7895 OMP_FOR_CLAUSES (for_stmt) = c;
7896 omp_add_variable (gimplify_omp_ctxp, decl, flags);
7897 if (outer && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
7899 if (outer->region_type == ORT_WORKSHARE
7900 && outer->combined_loop)
7902 if (outer->outer_context
7903 && (outer->outer_context->region_type
7904 == ORT_COMBINED_PARALLEL))
7905 outer = outer->outer_context;
7906 else if (omp_check_private (outer, decl, false))
7907 outer = NULL;
7909 else if (((outer->region_type & ORT_TASK) != 0)
7910 && outer->combined_loop
7911 && !omp_check_private (gimplify_omp_ctxp,
7912 decl, false))
7914 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7915 outer = NULL;
7916 if (outer)
7918 n = splay_tree_lookup (outer->variables,
7919 (splay_tree_key)decl);
7920 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
7922 omp_add_variable (outer, decl,
7923 GOVD_LASTPRIVATE | GOVD_SEEN);
7924 if (outer->outer_context)
7925 omp_notice_variable (outer->outer_context, decl,
7926 true);
7931 else
7933 bool lastprivate
7934 = (!has_decl_expr
7935 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
7936 && !omp_no_lastprivate (gimplify_omp_ctxp);
7937 struct gimplify_omp_ctx *outer
7938 = gimplify_omp_ctxp->outer_context;
7939 if (outer && lastprivate)
7941 if (outer->region_type == ORT_WORKSHARE
7942 && outer->combined_loop)
7944 n = splay_tree_lookup (outer->variables,
7945 (splay_tree_key)decl);
7946 if (n != NULL && (n->value & GOVD_LOCAL) != 0)
7948 lastprivate = false;
7949 outer = NULL;
7951 else if (outer->outer_context
7952 && (outer->outer_context->region_type
7953 == ORT_COMBINED_PARALLEL))
7954 outer = outer->outer_context;
7955 else if (omp_check_private (outer, decl, false))
7956 outer = NULL;
7958 else if (((outer->region_type & ORT_TASK) != 0)
7959 && outer->combined_loop
7960 && !omp_check_private (gimplify_omp_ctxp,
7961 decl, false))
7963 else if (outer->region_type != ORT_COMBINED_PARALLEL)
7964 outer = NULL;
7965 if (outer)
7967 n = splay_tree_lookup (outer->variables,
7968 (splay_tree_key)decl);
7969 if (n == NULL || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
7971 omp_add_variable (outer, decl,
7972 GOVD_LASTPRIVATE | GOVD_SEEN);
7973 if (outer->outer_context)
7974 omp_notice_variable (outer->outer_context, decl,
7975 true);
7980 c = build_omp_clause (input_location,
7981 lastprivate ? OMP_CLAUSE_LASTPRIVATE
7982 : OMP_CLAUSE_PRIVATE);
7983 OMP_CLAUSE_DECL (c) = decl;
7984 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
7985 OMP_FOR_CLAUSES (for_stmt) = c;
7986 omp_add_variable (gimplify_omp_ctxp, decl,
7987 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
7988 | GOVD_EXPLICIT | GOVD_SEEN);
7989 c = NULL_TREE;
7992 else if (omp_is_private (gimplify_omp_ctxp, decl, 0))
7993 omp_notice_variable (gimplify_omp_ctxp, decl, true);
7994 else
7995 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
7997 /* If DECL is not a gimple register, create a temporary variable to act
7998 as an iteration counter. This is valid, since DECL cannot be
7999 modified in the body of the loop. Similarly for any iteration vars
8000 in simd with collapse > 1 where the iterator vars must be
8001 lastprivate. */
8002 if (orig_for_stmt != for_stmt)
8003 var = decl;
8004 else if (!is_gimple_reg (decl)
8005 || (ort == ORT_SIMD
8006 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1))
8008 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8009 TREE_OPERAND (t, 0) = var;
8011 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
8013 if (ort == ORT_SIMD
8014 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
8016 c2 = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
8017 OMP_CLAUSE_LINEAR_NO_COPYIN (c2) = 1;
8018 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2) = 1;
8019 OMP_CLAUSE_DECL (c2) = var;
8020 OMP_CLAUSE_CHAIN (c2) = OMP_FOR_CLAUSES (for_stmt);
8021 OMP_FOR_CLAUSES (for_stmt) = c2;
8022 omp_add_variable (gimplify_omp_ctxp, var,
8023 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
8024 if (c == NULL_TREE)
8026 c = c2;
8027 c2 = NULL_TREE;
8030 else
8031 omp_add_variable (gimplify_omp_ctxp, var,
8032 GOVD_PRIVATE | GOVD_SEEN);
8034 else
8035 var = decl;
8037 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8038 is_gimple_val, fb_rvalue);
8039 ret = MIN (ret, tret);
8040 if (ret == GS_ERROR)
8041 return ret;
8043 /* Handle OMP_FOR_COND. */
8044 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8045 gcc_assert (COMPARISON_CLASS_P (t));
8046 gcc_assert (TREE_OPERAND (t, 0) == decl);
8048 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8049 is_gimple_val, fb_rvalue);
8050 ret = MIN (ret, tret);
8052 /* Handle OMP_FOR_INCR. */
8053 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8054 switch (TREE_CODE (t))
8056 case PREINCREMENT_EXPR:
8057 case POSTINCREMENT_EXPR:
8059 tree decl = TREE_OPERAND (t, 0);
8060 /* c_omp_for_incr_canonicalize_ptr() should have been
8061 called to massage things appropriately. */
8062 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8064 if (orig_for_stmt != for_stmt)
8065 break;
8066 t = build_int_cst (TREE_TYPE (decl), 1);
8067 if (c)
8068 OMP_CLAUSE_LINEAR_STEP (c) = t;
8069 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8070 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8071 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8072 break;
8075 case PREDECREMENT_EXPR:
8076 case POSTDECREMENT_EXPR:
8077 /* c_omp_for_incr_canonicalize_ptr() should have been
8078 called to massage things appropriately. */
8079 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8080 if (orig_for_stmt != for_stmt)
8081 break;
8082 t = build_int_cst (TREE_TYPE (decl), -1);
8083 if (c)
8084 OMP_CLAUSE_LINEAR_STEP (c) = t;
8085 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
8086 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
8087 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
8088 break;
8090 case MODIFY_EXPR:
8091 gcc_assert (TREE_OPERAND (t, 0) == decl);
8092 TREE_OPERAND (t, 0) = var;
8094 t = TREE_OPERAND (t, 1);
8095 switch (TREE_CODE (t))
8097 case PLUS_EXPR:
8098 if (TREE_OPERAND (t, 1) == decl)
8100 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
8101 TREE_OPERAND (t, 0) = var;
8102 break;
8105 /* Fallthru. */
8106 case MINUS_EXPR:
8107 case POINTER_PLUS_EXPR:
8108 gcc_assert (TREE_OPERAND (t, 0) == decl);
8109 TREE_OPERAND (t, 0) = var;
8110 break;
8111 default:
8112 gcc_unreachable ();
8115 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
8116 is_gimple_val, fb_rvalue);
8117 ret = MIN (ret, tret);
8118 if (c)
8120 tree step = TREE_OPERAND (t, 1);
8121 tree stept = TREE_TYPE (decl);
8122 if (POINTER_TYPE_P (stept))
8123 stept = sizetype;
8124 step = fold_convert (stept, step);
8125 if (TREE_CODE (t) == MINUS_EXPR)
8126 step = fold_build1 (NEGATE_EXPR, stept, step);
8127 OMP_CLAUSE_LINEAR_STEP (c) = step;
8128 if (step != TREE_OPERAND (t, 1))
8130 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
8131 &for_pre_body, NULL,
8132 is_gimple_val, fb_rvalue);
8133 ret = MIN (ret, tret);
8136 break;
8138 default:
8139 gcc_unreachable ();
8142 if (c2)
8144 gcc_assert (c);
8145 OMP_CLAUSE_LINEAR_STEP (c2) = OMP_CLAUSE_LINEAR_STEP (c);
8148 if ((var != decl || collapse > 1) && orig_for_stmt == for_stmt)
8150 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
8151 if (((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8152 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
8153 || (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
8154 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c)
8155 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c) == NULL))
8156 && OMP_CLAUSE_DECL (c) == decl)
8158 if (is_doacross && (collapse == 1 || i >= collapse))
8159 t = var;
8160 else
8162 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8163 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
8164 gcc_assert (TREE_OPERAND (t, 0) == var);
8165 t = TREE_OPERAND (t, 1);
8166 gcc_assert (TREE_CODE (t) == PLUS_EXPR
8167 || TREE_CODE (t) == MINUS_EXPR
8168 || TREE_CODE (t) == POINTER_PLUS_EXPR);
8169 gcc_assert (TREE_OPERAND (t, 0) == var);
8170 t = build2 (TREE_CODE (t), TREE_TYPE (decl),
8171 is_doacross ? var : decl,
8172 TREE_OPERAND (t, 1));
8174 gimple_seq *seq;
8175 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8176 seq = &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c);
8177 else
8178 seq = &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c);
8179 gimplify_assign (decl, t, seq);
8184 BITMAP_FREE (has_decl_expr);
8186 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8188 push_gimplify_context ();
8189 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt)) != BIND_EXPR)
8191 OMP_FOR_BODY (orig_for_stmt)
8192 = build3 (BIND_EXPR, void_type_node, NULL,
8193 OMP_FOR_BODY (orig_for_stmt), NULL);
8194 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt)) = 1;
8198 gimple *g = gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt),
8199 &for_body);
8201 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8203 if (gimple_code (g) == GIMPLE_BIND)
8204 pop_gimplify_context (g);
8205 else
8206 pop_gimplify_context (NULL);
8209 if (orig_for_stmt != for_stmt)
8210 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8212 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8213 decl = TREE_OPERAND (t, 0);
8214 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
8215 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8216 gimplify_omp_ctxp = ctx->outer_context;
8217 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
8218 gimplify_omp_ctxp = ctx;
8219 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
8220 TREE_OPERAND (t, 0) = var;
8221 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8222 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
8223 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
8226 gimplify_adjust_omp_clauses (pre_p, &OMP_FOR_CLAUSES (orig_for_stmt),
8227 TREE_CODE (orig_for_stmt));
8229 int kind;
8230 switch (TREE_CODE (orig_for_stmt))
8232 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
8233 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
8234 case CILK_SIMD: kind = GF_OMP_FOR_KIND_CILKSIMD; break;
8235 case CILK_FOR: kind = GF_OMP_FOR_KIND_CILKFOR; break;
8236 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
8237 case OMP_TASKLOOP: kind = GF_OMP_FOR_KIND_TASKLOOP; break;
8238 case OACC_LOOP: kind = GF_OMP_FOR_KIND_OACC_LOOP; break;
8239 default:
8240 gcc_unreachable ();
8242 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
8243 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
8244 for_pre_body);
8245 if (orig_for_stmt != for_stmt)
8246 gimple_omp_for_set_combined_p (gfor, true);
8247 if (gimplify_omp_ctxp
8248 && (gimplify_omp_ctxp->combined_loop
8249 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
8250 && gimplify_omp_ctxp->outer_context
8251 && gimplify_omp_ctxp->outer_context->combined_loop)))
8253 gimple_omp_for_set_combined_into_p (gfor, true);
8254 if (gimplify_omp_ctxp->combined_loop)
8255 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
8256 else
8257 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
8260 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
8262 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
8263 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
8264 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
8265 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
8266 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
8267 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
8268 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
8269 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
8272 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
8273 constructs with GIMPLE_OMP_TASK sandwiched in between them.
8274 The outer taskloop stands for computing the number of iterations,
8275 counts for collapsed loops and holding taskloop specific clauses.
8276 The task construct stands for the effect of data sharing on the
8277 explicit task it creates and the inner taskloop stands for expansion
8278 of the static loop inside of the explicit task construct. */
8279 if (TREE_CODE (orig_for_stmt) == OMP_TASKLOOP)
8281 tree *gfor_clauses_ptr = gimple_omp_for_clauses_ptr (gfor);
8282 tree task_clauses = NULL_TREE;
8283 tree c = *gfor_clauses_ptr;
8284 tree *gtask_clauses_ptr = &task_clauses;
8285 tree outer_for_clauses = NULL_TREE;
8286 tree *gforo_clauses_ptr = &outer_for_clauses;
8287 for (; c; c = OMP_CLAUSE_CHAIN (c))
8288 switch (OMP_CLAUSE_CODE (c))
8290 /* These clauses are allowed on task, move them there. */
8291 case OMP_CLAUSE_SHARED:
8292 case OMP_CLAUSE_FIRSTPRIVATE:
8293 case OMP_CLAUSE_DEFAULT:
8294 case OMP_CLAUSE_IF:
8295 case OMP_CLAUSE_UNTIED:
8296 case OMP_CLAUSE_FINAL:
8297 case OMP_CLAUSE_MERGEABLE:
8298 case OMP_CLAUSE_PRIORITY:
8299 *gtask_clauses_ptr = c;
8300 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8301 break;
8302 case OMP_CLAUSE_PRIVATE:
8303 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c))
8305 /* We want private on outer for and firstprivate
8306 on task. */
8307 *gtask_clauses_ptr
8308 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8309 OMP_CLAUSE_FIRSTPRIVATE);
8310 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8311 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8312 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8313 *gforo_clauses_ptr = c;
8314 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8316 else
8318 *gtask_clauses_ptr = c;
8319 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8321 break;
8322 /* These clauses go into outer taskloop clauses. */
8323 case OMP_CLAUSE_GRAINSIZE:
8324 case OMP_CLAUSE_NUM_TASKS:
8325 case OMP_CLAUSE_NOGROUP:
8326 *gforo_clauses_ptr = c;
8327 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8328 break;
8329 /* Taskloop clause we duplicate on both taskloops. */
8330 case OMP_CLAUSE_COLLAPSE:
8331 *gfor_clauses_ptr = c;
8332 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8333 *gforo_clauses_ptr = copy_node (c);
8334 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8335 break;
8336 /* For lastprivate, keep the clause on inner taskloop, and add
8337 a shared clause on task. If the same decl is also firstprivate,
8338 add also firstprivate clause on the inner taskloop. */
8339 case OMP_CLAUSE_LASTPRIVATE:
8340 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c))
8342 /* For taskloop C++ lastprivate IVs, we want:
8343 1) private on outer taskloop
8344 2) firstprivate and shared on task
8345 3) lastprivate on inner taskloop */
8346 *gtask_clauses_ptr
8347 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8348 OMP_CLAUSE_FIRSTPRIVATE);
8349 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8350 lang_hooks.decls.omp_finish_clause (*gtask_clauses_ptr, NULL);
8351 gtask_clauses_ptr = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8352 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c) = 1;
8353 *gforo_clauses_ptr = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8354 OMP_CLAUSE_PRIVATE);
8355 OMP_CLAUSE_DECL (*gforo_clauses_ptr) = OMP_CLAUSE_DECL (c);
8356 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr) = 1;
8357 TREE_TYPE (*gforo_clauses_ptr) = TREE_TYPE (c);
8358 gforo_clauses_ptr = &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr);
8360 *gfor_clauses_ptr = c;
8361 gfor_clauses_ptr = &OMP_CLAUSE_CHAIN (c);
8362 *gtask_clauses_ptr
8363 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_SHARED);
8364 OMP_CLAUSE_DECL (*gtask_clauses_ptr) = OMP_CLAUSE_DECL (c);
8365 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c))
8366 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr) = 1;
8367 gtask_clauses_ptr
8368 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr);
8369 break;
8370 default:
8371 gcc_unreachable ();
8373 *gfor_clauses_ptr = NULL_TREE;
8374 *gtask_clauses_ptr = NULL_TREE;
8375 *gforo_clauses_ptr = NULL_TREE;
8376 g = gimple_build_bind (NULL_TREE, gfor, NULL_TREE);
8377 g = gimple_build_omp_task (g, task_clauses, NULL_TREE, NULL_TREE,
8378 NULL_TREE, NULL_TREE, NULL_TREE);
8379 gimple_omp_task_set_taskloop_p (g, true);
8380 g = gimple_build_bind (NULL_TREE, g, NULL_TREE);
8381 gomp_for *gforo
8382 = gimple_build_omp_for (g, GF_OMP_FOR_KIND_TASKLOOP, outer_for_clauses,
8383 gimple_omp_for_collapse (gfor),
8384 gimple_omp_for_pre_body (gfor));
8385 gimple_omp_for_set_pre_body (gfor, NULL);
8386 gimple_omp_for_set_combined_p (gforo, true);
8387 gimple_omp_for_set_combined_into_p (gfor, true);
8388 for (i = 0; i < (int) gimple_omp_for_collapse (gfor); i++)
8390 t = unshare_expr (gimple_omp_for_index (gfor, i));
8391 gimple_omp_for_set_index (gforo, i, t);
8392 t = unshare_expr (gimple_omp_for_initial (gfor, i));
8393 gimple_omp_for_set_initial (gforo, i, t);
8394 gimple_omp_for_set_cond (gforo, i,
8395 gimple_omp_for_cond (gfor, i));
8396 t = unshare_expr (gimple_omp_for_final (gfor, i));
8397 gimple_omp_for_set_final (gforo, i, t);
8398 t = unshare_expr (gimple_omp_for_incr (gfor, i));
8399 gimple_omp_for_set_incr (gforo, i, t);
8401 gimplify_seq_add_stmt (pre_p, gforo);
8403 else
8404 gimplify_seq_add_stmt (pre_p, gfor);
8405 if (ret != GS_ALL_DONE)
8406 return GS_ERROR;
8407 *expr_p = NULL_TREE;
8408 return GS_ALL_DONE;
8411 /* Gimplify the gross structure of several OMP constructs. */
8413 static void
8414 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
8416 tree expr = *expr_p;
8417 gimple *stmt;
8418 gimple_seq body = NULL;
8419 enum omp_region_type ort;
8421 switch (TREE_CODE (expr))
8423 case OMP_SECTIONS:
8424 case OMP_SINGLE:
8425 ort = ORT_WORKSHARE;
8426 break;
8427 case OMP_TARGET:
8428 ort = OMP_TARGET_COMBINED (expr) ? ORT_COMBINED_TARGET : ORT_TARGET;
8429 break;
8430 case OACC_KERNELS:
8431 case OACC_PARALLEL:
8432 ort = ORT_TARGET;
8433 break;
8434 case OACC_DATA:
8435 case OMP_TARGET_DATA:
8436 ort = ORT_TARGET_DATA;
8437 break;
8438 case OMP_TEAMS:
8439 ort = OMP_TEAMS_COMBINED (expr) ? ORT_COMBINED_TEAMS : ORT_TEAMS;
8440 break;
8441 default:
8442 gcc_unreachable ();
8444 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort,
8445 TREE_CODE (expr));
8446 if ((ort & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
8448 push_gimplify_context ();
8449 gimple *g = gimplify_and_return_first (OMP_BODY (expr), &body);
8450 if (gimple_code (g) == GIMPLE_BIND)
8451 pop_gimplify_context (g);
8452 else
8453 pop_gimplify_context (NULL);
8454 if (ort == ORT_TARGET_DATA)
8456 enum built_in_function end_ix;
8457 switch (TREE_CODE (expr))
8459 case OACC_DATA:
8460 end_ix = BUILT_IN_GOACC_DATA_END;
8461 break;
8462 case OMP_TARGET_DATA:
8463 end_ix = BUILT_IN_GOMP_TARGET_END_DATA;
8464 break;
8465 default:
8466 gcc_unreachable ();
8468 tree fn = builtin_decl_explicit (end_ix);
8469 g = gimple_build_call (fn, 0);
8470 gimple_seq cleanup = NULL;
8471 gimple_seq_add_stmt (&cleanup, g);
8472 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8473 body = NULL;
8474 gimple_seq_add_stmt (&body, g);
8477 else
8478 gimplify_and_add (OMP_BODY (expr), &body);
8479 gimplify_adjust_omp_clauses (pre_p, &OMP_CLAUSES (expr), TREE_CODE (expr));
8481 switch (TREE_CODE (expr))
8483 case OACC_DATA:
8484 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_DATA,
8485 OMP_CLAUSES (expr));
8486 break;
8487 case OACC_KERNELS:
8488 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_KERNELS,
8489 OMP_CLAUSES (expr));
8490 break;
8491 case OACC_PARALLEL:
8492 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_OACC_PARALLEL,
8493 OMP_CLAUSES (expr));
8494 break;
8495 case OMP_SECTIONS:
8496 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
8497 break;
8498 case OMP_SINGLE:
8499 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
8500 break;
8501 case OMP_TARGET:
8502 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
8503 OMP_CLAUSES (expr));
8504 break;
8505 case OMP_TARGET_DATA:
8506 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
8507 OMP_CLAUSES (expr));
8508 break;
8509 case OMP_TEAMS:
8510 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
8511 break;
8512 default:
8513 gcc_unreachable ();
8516 gimplify_seq_add_stmt (pre_p, stmt);
8517 *expr_p = NULL_TREE;
8520 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
8521 target update constructs. */
8523 static void
8524 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
8526 tree expr = *expr_p;
8527 int kind;
8528 gomp_target *stmt;
8530 switch (TREE_CODE (expr))
8532 case OACC_ENTER_DATA:
8533 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
8534 break;
8535 case OACC_EXIT_DATA:
8536 kind = GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA;
8537 break;
8538 case OACC_UPDATE:
8539 kind = GF_OMP_TARGET_KIND_OACC_UPDATE;
8540 break;
8541 case OMP_TARGET_UPDATE:
8542 kind = GF_OMP_TARGET_KIND_UPDATE;
8543 break;
8544 case OMP_TARGET_ENTER_DATA:
8545 kind = GF_OMP_TARGET_KIND_ENTER_DATA;
8546 break;
8547 case OMP_TARGET_EXIT_DATA:
8548 kind = GF_OMP_TARGET_KIND_EXIT_DATA;
8549 break;
8550 default:
8551 gcc_unreachable ();
8553 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr), pre_p,
8554 ORT_WORKSHARE, TREE_CODE (expr));
8555 gimplify_adjust_omp_clauses (pre_p, &OMP_STANDALONE_CLAUSES (expr),
8556 TREE_CODE (expr));
8557 stmt = gimple_build_omp_target (NULL, kind, OMP_STANDALONE_CLAUSES (expr));
8559 gimplify_seq_add_stmt (pre_p, stmt);
8560 *expr_p = NULL_TREE;
8563 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
8564 stabilized the lhs of the atomic operation as *ADDR. Return true if
8565 EXPR is this stabilized form. */
8567 static bool
8568 goa_lhs_expr_p (tree expr, tree addr)
8570 /* Also include casts to other type variants. The C front end is fond
8571 of adding these for e.g. volatile variables. This is like
8572 STRIP_TYPE_NOPS but includes the main variant lookup. */
8573 STRIP_USELESS_TYPE_CONVERSION (expr);
8575 if (TREE_CODE (expr) == INDIRECT_REF)
8577 expr = TREE_OPERAND (expr, 0);
8578 while (expr != addr
8579 && (CONVERT_EXPR_P (expr)
8580 || TREE_CODE (expr) == NON_LVALUE_EXPR)
8581 && TREE_CODE (expr) == TREE_CODE (addr)
8582 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
8584 expr = TREE_OPERAND (expr, 0);
8585 addr = TREE_OPERAND (addr, 0);
8587 if (expr == addr)
8588 return true;
8589 return (TREE_CODE (addr) == ADDR_EXPR
8590 && TREE_CODE (expr) == ADDR_EXPR
8591 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
8593 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
8594 return true;
8595 return false;
8598 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
8599 expression does not involve the lhs, evaluate it into a temporary.
8600 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
8601 or -1 if an error was encountered. */
8603 static int
8604 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
8605 tree lhs_var)
8607 tree expr = *expr_p;
8608 int saw_lhs;
8610 if (goa_lhs_expr_p (expr, lhs_addr))
8612 *expr_p = lhs_var;
8613 return 1;
8615 if (is_gimple_val (expr))
8616 return 0;
8618 saw_lhs = 0;
8619 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
8621 case tcc_binary:
8622 case tcc_comparison:
8623 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
8624 lhs_var);
8625 case tcc_unary:
8626 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
8627 lhs_var);
8628 break;
8629 case tcc_expression:
8630 switch (TREE_CODE (expr))
8632 case TRUTH_ANDIF_EXPR:
8633 case TRUTH_ORIF_EXPR:
8634 case TRUTH_AND_EXPR:
8635 case TRUTH_OR_EXPR:
8636 case TRUTH_XOR_EXPR:
8637 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
8638 lhs_addr, lhs_var);
8639 case TRUTH_NOT_EXPR:
8640 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
8641 lhs_addr, lhs_var);
8642 break;
8643 case COMPOUND_EXPR:
8644 /* Break out any preevaluations from cp_build_modify_expr. */
8645 for (; TREE_CODE (expr) == COMPOUND_EXPR;
8646 expr = TREE_OPERAND (expr, 1))
8647 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
8648 *expr_p = expr;
8649 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
8650 default:
8651 break;
8653 break;
8654 default:
8655 break;
8658 if (saw_lhs == 0)
8660 enum gimplify_status gs;
8661 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
8662 if (gs != GS_ALL_DONE)
8663 saw_lhs = -1;
8666 return saw_lhs;
8669 /* Gimplify an OMP_ATOMIC statement. */
8671 static enum gimplify_status
8672 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
8674 tree addr = TREE_OPERAND (*expr_p, 0);
8675 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
8676 ? NULL : TREE_OPERAND (*expr_p, 1);
8677 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
8678 tree tmp_load;
8679 gomp_atomic_load *loadstmt;
8680 gomp_atomic_store *storestmt;
8682 tmp_load = create_tmp_reg (type);
8683 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
8684 return GS_ERROR;
8686 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
8687 != GS_ALL_DONE)
8688 return GS_ERROR;
8690 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
8691 gimplify_seq_add_stmt (pre_p, loadstmt);
8692 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
8693 != GS_ALL_DONE)
8694 return GS_ERROR;
8696 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
8697 rhs = tmp_load;
8698 storestmt = gimple_build_omp_atomic_store (rhs);
8699 gimplify_seq_add_stmt (pre_p, storestmt);
8700 if (OMP_ATOMIC_SEQ_CST (*expr_p))
8702 gimple_omp_atomic_set_seq_cst (loadstmt);
8703 gimple_omp_atomic_set_seq_cst (storestmt);
8705 switch (TREE_CODE (*expr_p))
8707 case OMP_ATOMIC_READ:
8708 case OMP_ATOMIC_CAPTURE_OLD:
8709 *expr_p = tmp_load;
8710 gimple_omp_atomic_set_need_value (loadstmt);
8711 break;
8712 case OMP_ATOMIC_CAPTURE_NEW:
8713 *expr_p = rhs;
8714 gimple_omp_atomic_set_need_value (storestmt);
8715 break;
8716 default:
8717 *expr_p = NULL;
8718 break;
8721 return GS_ALL_DONE;
8724 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
8725 body, and adding some EH bits. */
8727 static enum gimplify_status
8728 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
8730 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
8731 gimple *body_stmt;
8732 gtransaction *trans_stmt;
8733 gimple_seq body = NULL;
8734 int subcode = 0;
8736 /* Wrap the transaction body in a BIND_EXPR so we have a context
8737 where to put decls for OMP. */
8738 if (TREE_CODE (tbody) != BIND_EXPR)
8740 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
8741 TREE_SIDE_EFFECTS (bind) = 1;
8742 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
8743 TRANSACTION_EXPR_BODY (expr) = bind;
8746 push_gimplify_context ();
8747 temp = voidify_wrapper_expr (*expr_p, NULL);
8749 body_stmt = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
8750 pop_gimplify_context (body_stmt);
8752 trans_stmt = gimple_build_transaction (body, NULL);
8753 if (TRANSACTION_EXPR_OUTER (expr))
8754 subcode = GTMA_IS_OUTER;
8755 else if (TRANSACTION_EXPR_RELAXED (expr))
8756 subcode = GTMA_IS_RELAXED;
8757 gimple_transaction_set_subcode (trans_stmt, subcode);
8759 gimplify_seq_add_stmt (pre_p, trans_stmt);
8761 if (temp)
8763 *expr_p = temp;
8764 return GS_OK;
8767 *expr_p = NULL_TREE;
8768 return GS_ALL_DONE;
8771 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
8772 is the OMP_BODY of the original EXPR (which has already been
8773 gimplified so it's not present in the EXPR).
8775 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
8777 static gimple *
8778 gimplify_omp_ordered (tree expr, gimple_seq body)
8780 tree c, decls;
8781 int failures = 0;
8782 unsigned int i;
8783 tree source_c = NULL_TREE;
8784 tree sink_c = NULL_TREE;
8786 if (gimplify_omp_ctxp)
8787 for (c = OMP_ORDERED_CLAUSES (expr); c; c = OMP_CLAUSE_CHAIN (c))
8788 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8789 && gimplify_omp_ctxp->loop_iter_var.is_empty ()
8790 && (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK
8791 || OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE))
8793 error_at (OMP_CLAUSE_LOCATION (c),
8794 "%<depend%> clause must be closely nested "
8795 "inside a loop with %<ordered%> clause with "
8796 "a parameter");
8797 failures++;
8799 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8800 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
8802 bool fail = false;
8803 for (decls = OMP_CLAUSE_DECL (c), i = 0;
8804 decls && TREE_CODE (decls) == TREE_LIST;
8805 decls = TREE_CHAIN (decls), ++i)
8806 if (i >= gimplify_omp_ctxp->loop_iter_var.length () / 2)
8807 continue;
8808 else if (TREE_VALUE (decls)
8809 != gimplify_omp_ctxp->loop_iter_var[2 * i])
8811 error_at (OMP_CLAUSE_LOCATION (c),
8812 "variable %qE is not an iteration "
8813 "of outermost loop %d, expected %qE",
8814 TREE_VALUE (decls), i + 1,
8815 gimplify_omp_ctxp->loop_iter_var[2 * i]);
8816 fail = true;
8817 failures++;
8819 else
8820 TREE_VALUE (decls)
8821 = gimplify_omp_ctxp->loop_iter_var[2 * i + 1];
8822 if (!fail && i != gimplify_omp_ctxp->loop_iter_var.length () / 2)
8824 error_at (OMP_CLAUSE_LOCATION (c),
8825 "number of variables in %<depend(sink)%> "
8826 "clause does not match number of "
8827 "iteration variables");
8828 failures++;
8830 sink_c = c;
8832 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8833 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SOURCE)
8835 if (source_c)
8837 error_at (OMP_CLAUSE_LOCATION (c),
8838 "more than one %<depend(source)%> clause on an "
8839 "%<ordered%> construct");
8840 failures++;
8842 else
8843 source_c = c;
8845 if (source_c && sink_c)
8847 error_at (OMP_CLAUSE_LOCATION (source_c),
8848 "%<depend(source)%> clause specified together with "
8849 "%<depend(sink:)%> clauses on the same construct");
8850 failures++;
8853 if (failures)
8854 return gimple_build_nop ();
8855 return gimple_build_omp_ordered (body, OMP_ORDERED_CLAUSES (expr));
8858 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
8859 expression produces a value to be used as an operand inside a GIMPLE
8860 statement, the value will be stored back in *EXPR_P. This value will
8861 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
8862 an SSA_NAME. The corresponding sequence of GIMPLE statements is
8863 emitted in PRE_P and POST_P.
8865 Additionally, this process may overwrite parts of the input
8866 expression during gimplification. Ideally, it should be
8867 possible to do non-destructive gimplification.
8869 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
8870 the expression needs to evaluate to a value to be used as
8871 an operand in a GIMPLE statement, this value will be stored in
8872 *EXPR_P on exit. This happens when the caller specifies one
8873 of fb_lvalue or fb_rvalue fallback flags.
8875 PRE_P will contain the sequence of GIMPLE statements corresponding
8876 to the evaluation of EXPR and all the side-effects that must
8877 be executed before the main expression. On exit, the last
8878 statement of PRE_P is the core statement being gimplified. For
8879 instance, when gimplifying 'if (++a)' the last statement in
8880 PRE_P will be 'if (t.1)' where t.1 is the result of
8881 pre-incrementing 'a'.
8883 POST_P will contain the sequence of GIMPLE statements corresponding
8884 to the evaluation of all the side-effects that must be executed
8885 after the main expression. If this is NULL, the post
8886 side-effects are stored at the end of PRE_P.
8888 The reason why the output is split in two is to handle post
8889 side-effects explicitly. In some cases, an expression may have
8890 inner and outer post side-effects which need to be emitted in
8891 an order different from the one given by the recursive
8892 traversal. For instance, for the expression (*p--)++ the post
8893 side-effects of '--' must actually occur *after* the post
8894 side-effects of '++'. However, gimplification will first visit
8895 the inner expression, so if a separate POST sequence was not
8896 used, the resulting sequence would be:
8898 1 t.1 = *p
8899 2 p = p - 1
8900 3 t.2 = t.1 + 1
8901 4 *p = t.2
8903 However, the post-decrement operation in line #2 must not be
8904 evaluated until after the store to *p at line #4, so the
8905 correct sequence should be:
8907 1 t.1 = *p
8908 2 t.2 = t.1 + 1
8909 3 *p = t.2
8910 4 p = p - 1
8912 So, by specifying a separate post queue, it is possible
8913 to emit the post side-effects in the correct order.
8914 If POST_P is NULL, an internal queue will be used. Before
8915 returning to the caller, the sequence POST_P is appended to
8916 the main output sequence PRE_P.
8918 GIMPLE_TEST_F points to a function that takes a tree T and
8919 returns nonzero if T is in the GIMPLE form requested by the
8920 caller. The GIMPLE predicates are in gimple.c.
8922 FALLBACK tells the function what sort of a temporary we want if
8923 gimplification cannot produce an expression that complies with
8924 GIMPLE_TEST_F.
8926 fb_none means that no temporary should be generated
8927 fb_rvalue means that an rvalue is OK to generate
8928 fb_lvalue means that an lvalue is OK to generate
8929 fb_either means that either is OK, but an lvalue is preferable.
8930 fb_mayfail means that gimplification may fail (in which case
8931 GS_ERROR will be returned)
8933 The return value is either GS_ERROR or GS_ALL_DONE, since this
8934 function iterates until EXPR is completely gimplified or an error
8935 occurs. */
8937 enum gimplify_status
8938 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
8939 bool (*gimple_test_f) (tree), fallback_t fallback)
8941 tree tmp;
8942 gimple_seq internal_pre = NULL;
8943 gimple_seq internal_post = NULL;
8944 tree save_expr;
8945 bool is_statement;
8946 location_t saved_location;
8947 enum gimplify_status ret;
8948 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
8950 save_expr = *expr_p;
8951 if (save_expr == NULL_TREE)
8952 return GS_ALL_DONE;
8954 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
8955 is_statement = gimple_test_f == is_gimple_stmt;
8956 if (is_statement)
8957 gcc_assert (pre_p);
8959 /* Consistency checks. */
8960 if (gimple_test_f == is_gimple_reg)
8961 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
8962 else if (gimple_test_f == is_gimple_val
8963 || gimple_test_f == is_gimple_call_addr
8964 || gimple_test_f == is_gimple_condexpr
8965 || gimple_test_f == is_gimple_mem_rhs
8966 || gimple_test_f == is_gimple_mem_rhs_or_call
8967 || gimple_test_f == is_gimple_reg_rhs
8968 || gimple_test_f == is_gimple_reg_rhs_or_call
8969 || gimple_test_f == is_gimple_asm_val
8970 || gimple_test_f == is_gimple_mem_ref_addr)
8971 gcc_assert (fallback & fb_rvalue);
8972 else if (gimple_test_f == is_gimple_min_lval
8973 || gimple_test_f == is_gimple_lvalue)
8974 gcc_assert (fallback & fb_lvalue);
8975 else if (gimple_test_f == is_gimple_addressable)
8976 gcc_assert (fallback & fb_either);
8977 else if (gimple_test_f == is_gimple_stmt)
8978 gcc_assert (fallback == fb_none);
8979 else
8981 /* We should have recognized the GIMPLE_TEST_F predicate to
8982 know what kind of fallback to use in case a temporary is
8983 needed to hold the value or address of *EXPR_P. */
8984 gcc_unreachable ();
8987 /* We used to check the predicate here and return immediately if it
8988 succeeds. This is wrong; the design is for gimplification to be
8989 idempotent, and for the predicates to only test for valid forms, not
8990 whether they are fully simplified. */
8991 if (pre_p == NULL)
8992 pre_p = &internal_pre;
8994 if (post_p == NULL)
8995 post_p = &internal_post;
8997 /* Remember the last statements added to PRE_P and POST_P. Every
8998 new statement added by the gimplification helpers needs to be
8999 annotated with location information. To centralize the
9000 responsibility, we remember the last statement that had been
9001 added to both queues before gimplifying *EXPR_P. If
9002 gimplification produces new statements in PRE_P and POST_P, those
9003 statements will be annotated with the same location information
9004 as *EXPR_P. */
9005 pre_last_gsi = gsi_last (*pre_p);
9006 post_last_gsi = gsi_last (*post_p);
9008 saved_location = input_location;
9009 if (save_expr != error_mark_node
9010 && EXPR_HAS_LOCATION (*expr_p))
9011 input_location = EXPR_LOCATION (*expr_p);
9013 /* Loop over the specific gimplifiers until the toplevel node
9014 remains the same. */
9017 /* Strip away as many useless type conversions as possible
9018 at the toplevel. */
9019 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
9021 /* Remember the expr. */
9022 save_expr = *expr_p;
9024 /* Die, die, die, my darling. */
9025 if (save_expr == error_mark_node
9026 || (TREE_TYPE (save_expr)
9027 && TREE_TYPE (save_expr) == error_mark_node))
9029 ret = GS_ERROR;
9030 break;
9033 /* Do any language-specific gimplification. */
9034 ret = ((enum gimplify_status)
9035 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
9036 if (ret == GS_OK)
9038 if (*expr_p == NULL_TREE)
9039 break;
9040 if (*expr_p != save_expr)
9041 continue;
9043 else if (ret != GS_UNHANDLED)
9044 break;
9046 /* Make sure that all the cases set 'ret' appropriately. */
9047 ret = GS_UNHANDLED;
9048 switch (TREE_CODE (*expr_p))
9050 /* First deal with the special cases. */
9052 case POSTINCREMENT_EXPR:
9053 case POSTDECREMENT_EXPR:
9054 case PREINCREMENT_EXPR:
9055 case PREDECREMENT_EXPR:
9056 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
9057 fallback != fb_none,
9058 TREE_TYPE (*expr_p));
9059 break;
9061 case VIEW_CONVERT_EXPR:
9062 if (is_gimple_reg_type (TREE_TYPE (*expr_p))
9063 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p, 0))))
9065 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9066 post_p, is_gimple_val, fb_rvalue);
9067 recalculate_side_effects (*expr_p);
9068 break;
9070 /* Fallthru. */
9072 case ARRAY_REF:
9073 case ARRAY_RANGE_REF:
9074 case REALPART_EXPR:
9075 case IMAGPART_EXPR:
9076 case COMPONENT_REF:
9077 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
9078 fallback ? fallback : fb_rvalue);
9079 break;
9081 case COND_EXPR:
9082 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
9084 /* C99 code may assign to an array in a structure value of a
9085 conditional expression, and this has undefined behavior
9086 only on execution, so create a temporary if an lvalue is
9087 required. */
9088 if (fallback == fb_lvalue)
9090 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9091 mark_addressable (*expr_p);
9092 ret = GS_OK;
9094 break;
9096 case CALL_EXPR:
9097 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
9099 /* C99 code may assign to an array in a structure returned
9100 from a function, and this has undefined behavior only on
9101 execution, so create a temporary if an lvalue is
9102 required. */
9103 if (fallback == fb_lvalue)
9105 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9106 mark_addressable (*expr_p);
9107 ret = GS_OK;
9109 break;
9111 case TREE_LIST:
9112 gcc_unreachable ();
9114 case COMPOUND_EXPR:
9115 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
9116 break;
9118 case COMPOUND_LITERAL_EXPR:
9119 ret = gimplify_compound_literal_expr (expr_p, pre_p,
9120 gimple_test_f, fallback);
9121 break;
9123 case MODIFY_EXPR:
9124 case INIT_EXPR:
9125 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
9126 fallback != fb_none);
9127 break;
9129 case TRUTH_ANDIF_EXPR:
9130 case TRUTH_ORIF_EXPR:
9132 /* Preserve the original type of the expression and the
9133 source location of the outer expression. */
9134 tree org_type = TREE_TYPE (*expr_p);
9135 *expr_p = gimple_boolify (*expr_p);
9136 *expr_p = build3_loc (input_location, COND_EXPR,
9137 org_type, *expr_p,
9138 fold_convert_loc
9139 (input_location,
9140 org_type, boolean_true_node),
9141 fold_convert_loc
9142 (input_location,
9143 org_type, boolean_false_node));
9144 ret = GS_OK;
9145 break;
9148 case TRUTH_NOT_EXPR:
9150 tree type = TREE_TYPE (*expr_p);
9151 /* The parsers are careful to generate TRUTH_NOT_EXPR
9152 only with operands that are always zero or one.
9153 We do not fold here but handle the only interesting case
9154 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
9155 *expr_p = gimple_boolify (*expr_p);
9156 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
9157 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
9158 TREE_TYPE (*expr_p),
9159 TREE_OPERAND (*expr_p, 0));
9160 else
9161 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
9162 TREE_TYPE (*expr_p),
9163 TREE_OPERAND (*expr_p, 0),
9164 build_int_cst (TREE_TYPE (*expr_p), 1));
9165 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
9166 *expr_p = fold_convert_loc (input_location, type, *expr_p);
9167 ret = GS_OK;
9168 break;
9171 case ADDR_EXPR:
9172 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
9173 break;
9175 case ANNOTATE_EXPR:
9177 tree cond = TREE_OPERAND (*expr_p, 0);
9178 tree kind = TREE_OPERAND (*expr_p, 1);
9179 tree type = TREE_TYPE (cond);
9180 if (!INTEGRAL_TYPE_P (type))
9182 *expr_p = cond;
9183 ret = GS_OK;
9184 break;
9186 tree tmp = create_tmp_var (type);
9187 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
9188 gcall *call
9189 = gimple_build_call_internal (IFN_ANNOTATE, 2, cond, kind);
9190 gimple_call_set_lhs (call, tmp);
9191 gimplify_seq_add_stmt (pre_p, call);
9192 *expr_p = tmp;
9193 ret = GS_ALL_DONE;
9194 break;
9197 case VA_ARG_EXPR:
9198 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
9199 break;
9201 CASE_CONVERT:
9202 if (IS_EMPTY_STMT (*expr_p))
9204 ret = GS_ALL_DONE;
9205 break;
9208 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
9209 || fallback == fb_none)
9211 /* Just strip a conversion to void (or in void context) and
9212 try again. */
9213 *expr_p = TREE_OPERAND (*expr_p, 0);
9214 ret = GS_OK;
9215 break;
9218 ret = gimplify_conversion (expr_p);
9219 if (ret == GS_ERROR)
9220 break;
9221 if (*expr_p != save_expr)
9222 break;
9223 /* FALLTHRU */
9225 case FIX_TRUNC_EXPR:
9226 /* unary_expr: ... | '(' cast ')' val | ... */
9227 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9228 is_gimple_val, fb_rvalue);
9229 recalculate_side_effects (*expr_p);
9230 break;
9232 case INDIRECT_REF:
9234 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
9235 bool notrap = TREE_THIS_NOTRAP (*expr_p);
9236 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
9238 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
9239 if (*expr_p != save_expr)
9241 ret = GS_OK;
9242 break;
9245 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9246 is_gimple_reg, fb_rvalue);
9247 if (ret == GS_ERROR)
9248 break;
9250 recalculate_side_effects (*expr_p);
9251 *expr_p = fold_build2_loc (input_location, MEM_REF,
9252 TREE_TYPE (*expr_p),
9253 TREE_OPERAND (*expr_p, 0),
9254 build_int_cst (saved_ptr_type, 0));
9255 TREE_THIS_VOLATILE (*expr_p) = volatilep;
9256 TREE_THIS_NOTRAP (*expr_p) = notrap;
9257 ret = GS_OK;
9258 break;
9261 /* We arrive here through the various re-gimplifcation paths. */
9262 case MEM_REF:
9263 /* First try re-folding the whole thing. */
9264 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
9265 TREE_OPERAND (*expr_p, 0),
9266 TREE_OPERAND (*expr_p, 1));
9267 if (tmp)
9269 *expr_p = tmp;
9270 recalculate_side_effects (*expr_p);
9271 ret = GS_OK;
9272 break;
9274 /* Avoid re-gimplifying the address operand if it is already
9275 in suitable form. Re-gimplifying would mark the address
9276 operand addressable. Always gimplify when not in SSA form
9277 as we still may have to gimplify decls with value-exprs. */
9278 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
9279 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
9281 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9282 is_gimple_mem_ref_addr, fb_rvalue);
9283 if (ret == GS_ERROR)
9284 break;
9286 recalculate_side_effects (*expr_p);
9287 ret = GS_ALL_DONE;
9288 break;
9290 /* Constants need not be gimplified. */
9291 case INTEGER_CST:
9292 case REAL_CST:
9293 case FIXED_CST:
9294 case STRING_CST:
9295 case COMPLEX_CST:
9296 case VECTOR_CST:
9297 /* Drop the overflow flag on constants, we do not want
9298 that in the GIMPLE IL. */
9299 if (TREE_OVERFLOW_P (*expr_p))
9300 *expr_p = drop_tree_overflow (*expr_p);
9301 ret = GS_ALL_DONE;
9302 break;
9304 case CONST_DECL:
9305 /* If we require an lvalue, such as for ADDR_EXPR, retain the
9306 CONST_DECL node. Otherwise the decl is replaceable by its
9307 value. */
9308 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
9309 if (fallback & fb_lvalue)
9310 ret = GS_ALL_DONE;
9311 else
9313 *expr_p = DECL_INITIAL (*expr_p);
9314 ret = GS_OK;
9316 break;
9318 case DECL_EXPR:
9319 ret = gimplify_decl_expr (expr_p, pre_p);
9320 break;
9322 case BIND_EXPR:
9323 ret = gimplify_bind_expr (expr_p, pre_p);
9324 break;
9326 case LOOP_EXPR:
9327 ret = gimplify_loop_expr (expr_p, pre_p);
9328 break;
9330 case SWITCH_EXPR:
9331 ret = gimplify_switch_expr (expr_p, pre_p);
9332 break;
9334 case EXIT_EXPR:
9335 ret = gimplify_exit_expr (expr_p);
9336 break;
9338 case GOTO_EXPR:
9339 /* If the target is not LABEL, then it is a computed jump
9340 and the target needs to be gimplified. */
9341 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
9343 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
9344 NULL, is_gimple_val, fb_rvalue);
9345 if (ret == GS_ERROR)
9346 break;
9348 gimplify_seq_add_stmt (pre_p,
9349 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
9350 ret = GS_ALL_DONE;
9351 break;
9353 case PREDICT_EXPR:
9354 gimplify_seq_add_stmt (pre_p,
9355 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
9356 PREDICT_EXPR_OUTCOME (*expr_p)));
9357 ret = GS_ALL_DONE;
9358 break;
9360 case LABEL_EXPR:
9361 ret = GS_ALL_DONE;
9362 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
9363 == current_function_decl);
9364 gimplify_seq_add_stmt (pre_p,
9365 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
9366 break;
9368 case CASE_LABEL_EXPR:
9369 ret = gimplify_case_label_expr (expr_p, pre_p);
9370 break;
9372 case RETURN_EXPR:
9373 ret = gimplify_return_expr (*expr_p, pre_p);
9374 break;
9376 case CONSTRUCTOR:
9377 /* Don't reduce this in place; let gimplify_init_constructor work its
9378 magic. Buf if we're just elaborating this for side effects, just
9379 gimplify any element that has side-effects. */
9380 if (fallback == fb_none)
9382 unsigned HOST_WIDE_INT ix;
9383 tree val;
9384 tree temp = NULL_TREE;
9385 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
9386 if (TREE_SIDE_EFFECTS (val))
9387 append_to_statement_list (val, &temp);
9389 *expr_p = temp;
9390 ret = temp ? GS_OK : GS_ALL_DONE;
9392 /* C99 code may assign to an array in a constructed
9393 structure or union, and this has undefined behavior only
9394 on execution, so create a temporary if an lvalue is
9395 required. */
9396 else if (fallback == fb_lvalue)
9398 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
9399 mark_addressable (*expr_p);
9400 ret = GS_OK;
9402 else
9403 ret = GS_ALL_DONE;
9404 break;
9406 /* The following are special cases that are not handled by the
9407 original GIMPLE grammar. */
9409 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
9410 eliminated. */
9411 case SAVE_EXPR:
9412 ret = gimplify_save_expr (expr_p, pre_p, post_p);
9413 break;
9415 case BIT_FIELD_REF:
9416 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9417 post_p, is_gimple_lvalue, fb_either);
9418 recalculate_side_effects (*expr_p);
9419 break;
9421 case TARGET_MEM_REF:
9423 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
9425 if (TMR_BASE (*expr_p))
9426 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
9427 post_p, is_gimple_mem_ref_addr, fb_either);
9428 if (TMR_INDEX (*expr_p))
9429 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
9430 post_p, is_gimple_val, fb_rvalue);
9431 if (TMR_INDEX2 (*expr_p))
9432 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
9433 post_p, is_gimple_val, fb_rvalue);
9434 /* TMR_STEP and TMR_OFFSET are always integer constants. */
9435 ret = MIN (r0, r1);
9437 break;
9439 case NON_LVALUE_EXPR:
9440 /* This should have been stripped above. */
9441 gcc_unreachable ();
9443 case ASM_EXPR:
9444 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
9445 break;
9447 case TRY_FINALLY_EXPR:
9448 case TRY_CATCH_EXPR:
9450 gimple_seq eval, cleanup;
9451 gtry *try_;
9453 /* Calls to destructors are generated automatically in FINALLY/CATCH
9454 block. They should have location as UNKNOWN_LOCATION. However,
9455 gimplify_call_expr will reset these call stmts to input_location
9456 if it finds stmt's location is unknown. To prevent resetting for
9457 destructors, we set the input_location to unknown.
9458 Note that this only affects the destructor calls in FINALLY/CATCH
9459 block, and will automatically reset to its original value by the
9460 end of gimplify_expr. */
9461 input_location = UNKNOWN_LOCATION;
9462 eval = cleanup = NULL;
9463 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
9464 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
9465 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
9466 if (gimple_seq_empty_p (cleanup))
9468 gimple_seq_add_seq (pre_p, eval);
9469 ret = GS_ALL_DONE;
9470 break;
9472 try_ = gimple_build_try (eval, cleanup,
9473 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
9474 ? GIMPLE_TRY_FINALLY
9475 : GIMPLE_TRY_CATCH);
9476 if (EXPR_HAS_LOCATION (save_expr))
9477 gimple_set_location (try_, EXPR_LOCATION (save_expr));
9478 else if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
9479 gimple_set_location (try_, saved_location);
9480 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
9481 gimple_try_set_catch_is_cleanup (try_,
9482 TRY_CATCH_IS_CLEANUP (*expr_p));
9483 gimplify_seq_add_stmt (pre_p, try_);
9484 ret = GS_ALL_DONE;
9485 break;
9488 case CLEANUP_POINT_EXPR:
9489 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
9490 break;
9492 case TARGET_EXPR:
9493 ret = gimplify_target_expr (expr_p, pre_p, post_p);
9494 break;
9496 case CATCH_EXPR:
9498 gimple *c;
9499 gimple_seq handler = NULL;
9500 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
9501 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
9502 gimplify_seq_add_stmt (pre_p, c);
9503 ret = GS_ALL_DONE;
9504 break;
9507 case EH_FILTER_EXPR:
9509 gimple *ehf;
9510 gimple_seq failure = NULL;
9512 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
9513 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
9514 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
9515 gimplify_seq_add_stmt (pre_p, ehf);
9516 ret = GS_ALL_DONE;
9517 break;
9520 case OBJ_TYPE_REF:
9522 enum gimplify_status r0, r1;
9523 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
9524 post_p, is_gimple_val, fb_rvalue);
9525 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
9526 post_p, is_gimple_val, fb_rvalue);
9527 TREE_SIDE_EFFECTS (*expr_p) = 0;
9528 ret = MIN (r0, r1);
9530 break;
9532 case LABEL_DECL:
9533 /* We get here when taking the address of a label. We mark
9534 the label as "forced"; meaning it can never be removed and
9535 it is a potential target for any computed goto. */
9536 FORCED_LABEL (*expr_p) = 1;
9537 ret = GS_ALL_DONE;
9538 break;
9540 case STATEMENT_LIST:
9541 ret = gimplify_statement_list (expr_p, pre_p);
9542 break;
9544 case WITH_SIZE_EXPR:
9546 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9547 post_p == &internal_post ? NULL : post_p,
9548 gimple_test_f, fallback);
9549 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
9550 is_gimple_val, fb_rvalue);
9551 ret = GS_ALL_DONE;
9553 break;
9555 case VAR_DECL:
9556 case PARM_DECL:
9557 ret = gimplify_var_or_parm_decl (expr_p);
9558 break;
9560 case RESULT_DECL:
9561 /* When within an OMP context, notice uses of variables. */
9562 if (gimplify_omp_ctxp)
9563 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
9564 ret = GS_ALL_DONE;
9565 break;
9567 case SSA_NAME:
9568 /* Allow callbacks into the gimplifier during optimization. */
9569 ret = GS_ALL_DONE;
9570 break;
9572 case OMP_PARALLEL:
9573 gimplify_omp_parallel (expr_p, pre_p);
9574 ret = GS_ALL_DONE;
9575 break;
9577 case OMP_TASK:
9578 gimplify_omp_task (expr_p, pre_p);
9579 ret = GS_ALL_DONE;
9580 break;
9582 case OMP_FOR:
9583 case OMP_SIMD:
9584 case CILK_SIMD:
9585 case CILK_FOR:
9586 case OMP_DISTRIBUTE:
9587 case OMP_TASKLOOP:
9588 case OACC_LOOP:
9589 ret = gimplify_omp_for (expr_p, pre_p);
9590 break;
9592 case OACC_CACHE:
9593 gimplify_oacc_cache (expr_p, pre_p);
9594 ret = GS_ALL_DONE;
9595 break;
9597 case OACC_HOST_DATA:
9598 case OACC_DECLARE:
9599 sorry ("directive not yet implemented");
9600 ret = GS_ALL_DONE;
9601 break;
9603 case OACC_KERNELS:
9604 if (OACC_KERNELS_COMBINED (*expr_p))
9605 sorry ("directive not yet implemented");
9606 else
9607 gimplify_omp_workshare (expr_p, pre_p);
9608 ret = GS_ALL_DONE;
9609 break;
9611 case OACC_PARALLEL:
9612 if (OACC_PARALLEL_COMBINED (*expr_p))
9613 sorry ("directive not yet implemented");
9614 else
9615 gimplify_omp_workshare (expr_p, pre_p);
9616 ret = GS_ALL_DONE;
9617 break;
9619 case OACC_DATA:
9620 case OMP_SECTIONS:
9621 case OMP_SINGLE:
9622 case OMP_TARGET:
9623 case OMP_TARGET_DATA:
9624 case OMP_TEAMS:
9625 gimplify_omp_workshare (expr_p, pre_p);
9626 ret = GS_ALL_DONE;
9627 break;
9629 case OACC_ENTER_DATA:
9630 case OACC_EXIT_DATA:
9631 case OACC_UPDATE:
9632 case OMP_TARGET_UPDATE:
9633 case OMP_TARGET_ENTER_DATA:
9634 case OMP_TARGET_EXIT_DATA:
9635 gimplify_omp_target_update (expr_p, pre_p);
9636 ret = GS_ALL_DONE;
9637 break;
9639 case OMP_SECTION:
9640 case OMP_MASTER:
9641 case OMP_TASKGROUP:
9642 case OMP_ORDERED:
9643 case OMP_CRITICAL:
9645 gimple_seq body = NULL;
9646 gimple *g;
9648 gimplify_and_add (OMP_BODY (*expr_p), &body);
9649 switch (TREE_CODE (*expr_p))
9651 case OMP_SECTION:
9652 g = gimple_build_omp_section (body);
9653 break;
9654 case OMP_MASTER:
9655 g = gimple_build_omp_master (body);
9656 break;
9657 case OMP_TASKGROUP:
9659 gimple_seq cleanup = NULL;
9660 tree fn
9661 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
9662 g = gimple_build_call (fn, 0);
9663 gimple_seq_add_stmt (&cleanup, g);
9664 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
9665 body = NULL;
9666 gimple_seq_add_stmt (&body, g);
9667 g = gimple_build_omp_taskgroup (body);
9669 break;
9670 case OMP_ORDERED:
9671 g = gimplify_omp_ordered (*expr_p, body);
9672 break;
9673 case OMP_CRITICAL:
9674 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p),
9675 pre_p, ORT_WORKSHARE, OMP_CRITICAL);
9676 gimplify_adjust_omp_clauses (pre_p,
9677 &OMP_CRITICAL_CLAUSES (*expr_p),
9678 OMP_CRITICAL);
9679 g = gimple_build_omp_critical (body,
9680 OMP_CRITICAL_NAME (*expr_p),
9681 OMP_CRITICAL_CLAUSES (*expr_p));
9682 break;
9683 default:
9684 gcc_unreachable ();
9686 gimplify_seq_add_stmt (pre_p, g);
9687 ret = GS_ALL_DONE;
9688 break;
9691 case OMP_ATOMIC:
9692 case OMP_ATOMIC_READ:
9693 case OMP_ATOMIC_CAPTURE_OLD:
9694 case OMP_ATOMIC_CAPTURE_NEW:
9695 ret = gimplify_omp_atomic (expr_p, pre_p);
9696 break;
9698 case TRANSACTION_EXPR:
9699 ret = gimplify_transaction (expr_p, pre_p);
9700 break;
9702 case TRUTH_AND_EXPR:
9703 case TRUTH_OR_EXPR:
9704 case TRUTH_XOR_EXPR:
9706 tree orig_type = TREE_TYPE (*expr_p);
9707 tree new_type, xop0, xop1;
9708 *expr_p = gimple_boolify (*expr_p);
9709 new_type = TREE_TYPE (*expr_p);
9710 if (!useless_type_conversion_p (orig_type, new_type))
9712 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
9713 ret = GS_OK;
9714 break;
9717 /* Boolified binary truth expressions are semantically equivalent
9718 to bitwise binary expressions. Canonicalize them to the
9719 bitwise variant. */
9720 switch (TREE_CODE (*expr_p))
9722 case TRUTH_AND_EXPR:
9723 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
9724 break;
9725 case TRUTH_OR_EXPR:
9726 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
9727 break;
9728 case TRUTH_XOR_EXPR:
9729 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
9730 break;
9731 default:
9732 break;
9734 /* Now make sure that operands have compatible type to
9735 expression's new_type. */
9736 xop0 = TREE_OPERAND (*expr_p, 0);
9737 xop1 = TREE_OPERAND (*expr_p, 1);
9738 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
9739 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
9740 new_type,
9741 xop0);
9742 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
9743 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
9744 new_type,
9745 xop1);
9746 /* Continue classified as tcc_binary. */
9747 goto expr_2;
9750 case FMA_EXPR:
9751 case VEC_COND_EXPR:
9752 case VEC_PERM_EXPR:
9753 /* Classified as tcc_expression. */
9754 goto expr_3;
9756 case POINTER_PLUS_EXPR:
9758 enum gimplify_status r0, r1;
9759 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9760 post_p, is_gimple_val, fb_rvalue);
9761 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9762 post_p, is_gimple_val, fb_rvalue);
9763 recalculate_side_effects (*expr_p);
9764 ret = MIN (r0, r1);
9765 break;
9768 case CILK_SYNC_STMT:
9770 if (!fn_contains_cilk_spawn_p (cfun))
9772 error_at (EXPR_LOCATION (*expr_p),
9773 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
9774 ret = GS_ERROR;
9776 else
9778 gimplify_cilk_sync (expr_p, pre_p);
9779 ret = GS_ALL_DONE;
9781 break;
9784 default:
9785 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
9787 case tcc_comparison:
9788 /* Handle comparison of objects of non scalar mode aggregates
9789 with a call to memcmp. It would be nice to only have to do
9790 this for variable-sized objects, but then we'd have to allow
9791 the same nest of reference nodes we allow for MODIFY_EXPR and
9792 that's too complex.
9794 Compare scalar mode aggregates as scalar mode values. Using
9795 memcmp for them would be very inefficient at best, and is
9796 plain wrong if bitfields are involved. */
9798 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
9800 /* Vector comparisons need no boolification. */
9801 if (TREE_CODE (type) == VECTOR_TYPE)
9802 goto expr_2;
9803 else if (!AGGREGATE_TYPE_P (type))
9805 tree org_type = TREE_TYPE (*expr_p);
9806 *expr_p = gimple_boolify (*expr_p);
9807 if (!useless_type_conversion_p (org_type,
9808 TREE_TYPE (*expr_p)))
9810 *expr_p = fold_convert_loc (input_location,
9811 org_type, *expr_p);
9812 ret = GS_OK;
9814 else
9815 goto expr_2;
9817 else if (TYPE_MODE (type) != BLKmode)
9818 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
9819 else
9820 ret = gimplify_variable_sized_compare (expr_p);
9822 break;
9825 /* If *EXPR_P does not need to be special-cased, handle it
9826 according to its class. */
9827 case tcc_unary:
9828 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9829 post_p, is_gimple_val, fb_rvalue);
9830 break;
9832 case tcc_binary:
9833 expr_2:
9835 enum gimplify_status r0, r1;
9837 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9838 post_p, is_gimple_val, fb_rvalue);
9839 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9840 post_p, is_gimple_val, fb_rvalue);
9842 ret = MIN (r0, r1);
9843 break;
9846 expr_3:
9848 enum gimplify_status r0, r1, r2;
9850 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
9851 post_p, is_gimple_val, fb_rvalue);
9852 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
9853 post_p, is_gimple_val, fb_rvalue);
9854 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
9855 post_p, is_gimple_val, fb_rvalue);
9857 ret = MIN (MIN (r0, r1), r2);
9858 break;
9861 case tcc_declaration:
9862 case tcc_constant:
9863 ret = GS_ALL_DONE;
9864 goto dont_recalculate;
9866 default:
9867 gcc_unreachable ();
9870 recalculate_side_effects (*expr_p);
9872 dont_recalculate:
9873 break;
9876 gcc_assert (*expr_p || ret != GS_OK);
9878 while (ret == GS_OK);
9880 /* If we encountered an error_mark somewhere nested inside, either
9881 stub out the statement or propagate the error back out. */
9882 if (ret == GS_ERROR)
9884 if (is_statement)
9885 *expr_p = NULL;
9886 goto out;
9889 /* This was only valid as a return value from the langhook, which
9890 we handled. Make sure it doesn't escape from any other context. */
9891 gcc_assert (ret != GS_UNHANDLED);
9893 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
9895 /* We aren't looking for a value, and we don't have a valid
9896 statement. If it doesn't have side-effects, throw it away. */
9897 if (!TREE_SIDE_EFFECTS (*expr_p))
9898 *expr_p = NULL;
9899 else if (!TREE_THIS_VOLATILE (*expr_p))
9901 /* This is probably a _REF that contains something nested that
9902 has side effects. Recurse through the operands to find it. */
9903 enum tree_code code = TREE_CODE (*expr_p);
9905 switch (code)
9907 case COMPONENT_REF:
9908 case REALPART_EXPR:
9909 case IMAGPART_EXPR:
9910 case VIEW_CONVERT_EXPR:
9911 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9912 gimple_test_f, fallback);
9913 break;
9915 case ARRAY_REF:
9916 case ARRAY_RANGE_REF:
9917 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
9918 gimple_test_f, fallback);
9919 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
9920 gimple_test_f, fallback);
9921 break;
9923 default:
9924 /* Anything else with side-effects must be converted to
9925 a valid statement before we get here. */
9926 gcc_unreachable ();
9929 *expr_p = NULL;
9931 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
9932 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
9934 /* Historically, the compiler has treated a bare reference
9935 to a non-BLKmode volatile lvalue as forcing a load. */
9936 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
9938 /* Normally, we do not want to create a temporary for a
9939 TREE_ADDRESSABLE type because such a type should not be
9940 copied by bitwise-assignment. However, we make an
9941 exception here, as all we are doing here is ensuring that
9942 we read the bytes that make up the type. We use
9943 create_tmp_var_raw because create_tmp_var will abort when
9944 given a TREE_ADDRESSABLE type. */
9945 tree tmp = create_tmp_var_raw (type, "vol");
9946 gimple_add_tmp_var (tmp);
9947 gimplify_assign (tmp, *expr_p, pre_p);
9948 *expr_p = NULL;
9950 else
9951 /* We can't do anything useful with a volatile reference to
9952 an incomplete type, so just throw it away. Likewise for
9953 a BLKmode type, since any implicit inner load should
9954 already have been turned into an explicit one by the
9955 gimplification process. */
9956 *expr_p = NULL;
9959 /* If we are gimplifying at the statement level, we're done. Tack
9960 everything together and return. */
9961 if (fallback == fb_none || is_statement)
9963 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
9964 it out for GC to reclaim it. */
9965 *expr_p = NULL_TREE;
9967 if (!gimple_seq_empty_p (internal_pre)
9968 || !gimple_seq_empty_p (internal_post))
9970 gimplify_seq_add_seq (&internal_pre, internal_post);
9971 gimplify_seq_add_seq (pre_p, internal_pre);
9974 /* The result of gimplifying *EXPR_P is going to be the last few
9975 statements in *PRE_P and *POST_P. Add location information
9976 to all the statements that were added by the gimplification
9977 helpers. */
9978 if (!gimple_seq_empty_p (*pre_p))
9979 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
9981 if (!gimple_seq_empty_p (*post_p))
9982 annotate_all_with_location_after (*post_p, post_last_gsi,
9983 input_location);
9985 goto out;
9988 #ifdef ENABLE_GIMPLE_CHECKING
9989 if (*expr_p)
9991 enum tree_code code = TREE_CODE (*expr_p);
9992 /* These expressions should already be in gimple IR form. */
9993 gcc_assert (code != MODIFY_EXPR
9994 && code != ASM_EXPR
9995 && code != BIND_EXPR
9996 && code != CATCH_EXPR
9997 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
9998 && code != EH_FILTER_EXPR
9999 && code != GOTO_EXPR
10000 && code != LABEL_EXPR
10001 && code != LOOP_EXPR
10002 && code != SWITCH_EXPR
10003 && code != TRY_FINALLY_EXPR
10004 && code != OACC_PARALLEL
10005 && code != OACC_KERNELS
10006 && code != OACC_DATA
10007 && code != OACC_HOST_DATA
10008 && code != OACC_DECLARE
10009 && code != OACC_UPDATE
10010 && code != OACC_ENTER_DATA
10011 && code != OACC_EXIT_DATA
10012 && code != OACC_CACHE
10013 && code != OMP_CRITICAL
10014 && code != OMP_FOR
10015 && code != OACC_LOOP
10016 && code != OMP_MASTER
10017 && code != OMP_TASKGROUP
10018 && code != OMP_ORDERED
10019 && code != OMP_PARALLEL
10020 && code != OMP_SECTIONS
10021 && code != OMP_SECTION
10022 && code != OMP_SINGLE);
10024 #endif
10026 /* Otherwise we're gimplifying a subexpression, so the resulting
10027 value is interesting. If it's a valid operand that matches
10028 GIMPLE_TEST_F, we're done. Unless we are handling some
10029 post-effects internally; if that's the case, we need to copy into
10030 a temporary before adding the post-effects to POST_P. */
10031 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
10032 goto out;
10034 /* Otherwise, we need to create a new temporary for the gimplified
10035 expression. */
10037 /* We can't return an lvalue if we have an internal postqueue. The
10038 object the lvalue refers to would (probably) be modified by the
10039 postqueue; we need to copy the value out first, which means an
10040 rvalue. */
10041 if ((fallback & fb_lvalue)
10042 && gimple_seq_empty_p (internal_post)
10043 && is_gimple_addressable (*expr_p))
10045 /* An lvalue will do. Take the address of the expression, store it
10046 in a temporary, and replace the expression with an INDIRECT_REF of
10047 that temporary. */
10048 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
10049 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
10050 *expr_p = build_simple_mem_ref (tmp);
10052 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
10054 /* An rvalue will do. Assign the gimplified expression into a
10055 new temporary TMP and replace the original expression with
10056 TMP. First, make sure that the expression has a type so that
10057 it can be assigned into a temporary. */
10058 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
10059 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
10061 else
10063 #ifdef ENABLE_GIMPLE_CHECKING
10064 if (!(fallback & fb_mayfail))
10066 fprintf (stderr, "gimplification failed:\n");
10067 print_generic_expr (stderr, *expr_p, 0);
10068 debug_tree (*expr_p);
10069 internal_error ("gimplification failed");
10071 #endif
10072 gcc_assert (fallback & fb_mayfail);
10074 /* If this is an asm statement, and the user asked for the
10075 impossible, don't die. Fail and let gimplify_asm_expr
10076 issue an error. */
10077 ret = GS_ERROR;
10078 goto out;
10081 /* Make sure the temporary matches our predicate. */
10082 gcc_assert ((*gimple_test_f) (*expr_p));
10084 if (!gimple_seq_empty_p (internal_post))
10086 annotate_all_with_location (internal_post, input_location);
10087 gimplify_seq_add_seq (pre_p, internal_post);
10090 out:
10091 input_location = saved_location;
10092 return ret;
10095 /* Look through TYPE for variable-sized objects and gimplify each such
10096 size that we find. Add to LIST_P any statements generated. */
10098 void
10099 gimplify_type_sizes (tree type, gimple_seq *list_p)
10101 tree field, t;
10103 if (type == NULL || type == error_mark_node)
10104 return;
10106 /* We first do the main variant, then copy into any other variants. */
10107 type = TYPE_MAIN_VARIANT (type);
10109 /* Avoid infinite recursion. */
10110 if (TYPE_SIZES_GIMPLIFIED (type))
10111 return;
10113 TYPE_SIZES_GIMPLIFIED (type) = 1;
10115 switch (TREE_CODE (type))
10117 case INTEGER_TYPE:
10118 case ENUMERAL_TYPE:
10119 case BOOLEAN_TYPE:
10120 case REAL_TYPE:
10121 case FIXED_POINT_TYPE:
10122 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
10123 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
10125 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10127 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
10128 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
10130 break;
10132 case ARRAY_TYPE:
10133 /* These types may not have declarations, so handle them here. */
10134 gimplify_type_sizes (TREE_TYPE (type), list_p);
10135 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
10136 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
10137 with assigned stack slots, for -O1+ -g they should be tracked
10138 by VTA. */
10139 if (!(TYPE_NAME (type)
10140 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10141 && DECL_IGNORED_P (TYPE_NAME (type)))
10142 && TYPE_DOMAIN (type)
10143 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
10145 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
10146 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10147 DECL_IGNORED_P (t) = 0;
10148 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
10149 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
10150 DECL_IGNORED_P (t) = 0;
10152 break;
10154 case RECORD_TYPE:
10155 case UNION_TYPE:
10156 case QUAL_UNION_TYPE:
10157 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10158 if (TREE_CODE (field) == FIELD_DECL)
10160 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
10161 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
10162 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
10163 gimplify_type_sizes (TREE_TYPE (field), list_p);
10165 break;
10167 case POINTER_TYPE:
10168 case REFERENCE_TYPE:
10169 /* We used to recurse on the pointed-to type here, which turned out to
10170 be incorrect because its definition might refer to variables not
10171 yet initialized at this point if a forward declaration is involved.
10173 It was actually useful for anonymous pointed-to types to ensure
10174 that the sizes evaluation dominates every possible later use of the
10175 values. Restricting to such types here would be safe since there
10176 is no possible forward declaration around, but would introduce an
10177 undesirable middle-end semantic to anonymity. We then defer to
10178 front-ends the responsibility of ensuring that the sizes are
10179 evaluated both early and late enough, e.g. by attaching artificial
10180 type declarations to the tree. */
10181 break;
10183 default:
10184 break;
10187 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
10188 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
10190 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10192 TYPE_SIZE (t) = TYPE_SIZE (type);
10193 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
10194 TYPE_SIZES_GIMPLIFIED (t) = 1;
10198 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
10199 a size or position, has had all of its SAVE_EXPRs evaluated.
10200 We add any required statements to *STMT_P. */
10202 void
10203 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
10205 tree expr = *expr_p;
10207 /* We don't do anything if the value isn't there, is constant, or contains
10208 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
10209 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
10210 will want to replace it with a new variable, but that will cause problems
10211 if this type is from outside the function. It's OK to have that here. */
10212 if (is_gimple_sizepos (expr))
10213 return;
10215 *expr_p = unshare_expr (expr);
10217 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
10220 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
10221 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
10222 is true, also gimplify the parameters. */
10224 gbind *
10225 gimplify_body (tree fndecl, bool do_parms)
10227 location_t saved_location = input_location;
10228 gimple_seq parm_stmts, seq;
10229 gimple *outer_stmt;
10230 gbind *outer_bind;
10231 struct cgraph_node *cgn;
10233 timevar_push (TV_TREE_GIMPLIFY);
10235 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
10236 gimplification. */
10237 default_rtl_profile ();
10239 gcc_assert (gimplify_ctxp == NULL);
10240 push_gimplify_context ();
10242 if (flag_openacc || flag_openmp)
10244 gcc_assert (gimplify_omp_ctxp == NULL);
10245 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
10246 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
10249 /* Unshare most shared trees in the body and in that of any nested functions.
10250 It would seem we don't have to do this for nested functions because
10251 they are supposed to be output and then the outer function gimplified
10252 first, but the g++ front end doesn't always do it that way. */
10253 unshare_body (fndecl);
10254 unvisit_body (fndecl);
10256 cgn = cgraph_node::get (fndecl);
10257 if (cgn && cgn->origin)
10258 nonlocal_vlas = new hash_set<tree>;
10260 /* Make sure input_location isn't set to something weird. */
10261 input_location = DECL_SOURCE_LOCATION (fndecl);
10263 /* Resolve callee-copies. This has to be done before processing
10264 the body so that DECL_VALUE_EXPR gets processed correctly. */
10265 parm_stmts = do_parms ? gimplify_parameters () : NULL;
10267 /* Gimplify the function's body. */
10268 seq = NULL;
10269 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
10270 outer_stmt = gimple_seq_first_stmt (seq);
10271 if (!outer_stmt)
10273 outer_stmt = gimple_build_nop ();
10274 gimplify_seq_add_stmt (&seq, outer_stmt);
10277 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
10278 not the case, wrap everything in a GIMPLE_BIND to make it so. */
10279 if (gimple_code (outer_stmt) == GIMPLE_BIND
10280 && gimple_seq_first (seq) == gimple_seq_last (seq))
10281 outer_bind = as_a <gbind *> (outer_stmt);
10282 else
10283 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
10285 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10287 /* If we had callee-copies statements, insert them at the beginning
10288 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
10289 if (!gimple_seq_empty_p (parm_stmts))
10291 tree parm;
10293 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
10294 gimple_bind_set_body (outer_bind, parm_stmts);
10296 for (parm = DECL_ARGUMENTS (current_function_decl);
10297 parm; parm = DECL_CHAIN (parm))
10298 if (DECL_HAS_VALUE_EXPR_P (parm))
10300 DECL_HAS_VALUE_EXPR_P (parm) = 0;
10301 DECL_IGNORED_P (parm) = 0;
10305 if (nonlocal_vlas)
10307 if (nonlocal_vla_vars)
10309 /* tree-nested.c may later on call declare_vars (..., true);
10310 which relies on BLOCK_VARS chain to be the tail of the
10311 gimple_bind_vars chain. Ensure we don't violate that
10312 assumption. */
10313 if (gimple_bind_block (outer_bind)
10314 == DECL_INITIAL (current_function_decl))
10315 declare_vars (nonlocal_vla_vars, outer_bind, true);
10316 else
10317 BLOCK_VARS (DECL_INITIAL (current_function_decl))
10318 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
10319 nonlocal_vla_vars);
10320 nonlocal_vla_vars = NULL_TREE;
10322 delete nonlocal_vlas;
10323 nonlocal_vlas = NULL;
10326 if ((flag_openacc || flag_openmp || flag_openmp_simd)
10327 && gimplify_omp_ctxp)
10329 delete_omp_context (gimplify_omp_ctxp);
10330 gimplify_omp_ctxp = NULL;
10333 pop_gimplify_context (outer_bind);
10334 gcc_assert (gimplify_ctxp == NULL);
10336 #ifdef ENABLE_CHECKING
10337 if (!seen_error ())
10338 verify_gimple_in_seq (gimple_bind_body (outer_bind));
10339 #endif
10341 timevar_pop (TV_TREE_GIMPLIFY);
10342 input_location = saved_location;
10344 return outer_bind;
10347 typedef char *char_p; /* For DEF_VEC_P. */
10349 /* Return whether we should exclude FNDECL from instrumentation. */
10351 static bool
10352 flag_instrument_functions_exclude_p (tree fndecl)
10354 vec<char_p> *v;
10356 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
10357 if (v && v->length () > 0)
10359 const char *name;
10360 int i;
10361 char *s;
10363 name = lang_hooks.decl_printable_name (fndecl, 0);
10364 FOR_EACH_VEC_ELT (*v, i, s)
10365 if (strstr (name, s) != NULL)
10366 return true;
10369 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
10370 if (v && v->length () > 0)
10372 const char *name;
10373 int i;
10374 char *s;
10376 name = DECL_SOURCE_FILE (fndecl);
10377 FOR_EACH_VEC_ELT (*v, i, s)
10378 if (strstr (name, s) != NULL)
10379 return true;
10382 return false;
10385 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
10386 node for the function we want to gimplify.
10388 Return the sequence of GIMPLE statements corresponding to the body
10389 of FNDECL. */
10391 void
10392 gimplify_function_tree (tree fndecl)
10394 tree parm, ret;
10395 gimple_seq seq;
10396 gbind *bind;
10398 gcc_assert (!gimple_body (fndecl));
10400 if (DECL_STRUCT_FUNCTION (fndecl))
10401 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
10402 else
10403 push_struct_function (fndecl);
10405 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
10406 if necessary. */
10407 cfun->curr_properties |= PROP_gimple_lva;
10409 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
10411 /* Preliminarily mark non-addressed complex variables as eligible
10412 for promotion to gimple registers. We'll transform their uses
10413 as we find them. */
10414 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
10415 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
10416 && !TREE_THIS_VOLATILE (parm)
10417 && !needs_to_live_in_memory (parm))
10418 DECL_GIMPLE_REG_P (parm) = 1;
10421 ret = DECL_RESULT (fndecl);
10422 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
10423 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
10424 && !needs_to_live_in_memory (ret))
10425 DECL_GIMPLE_REG_P (ret) = 1;
10427 bind = gimplify_body (fndecl, true);
10429 /* The tree body of the function is no longer needed, replace it
10430 with the new GIMPLE body. */
10431 seq = NULL;
10432 gimple_seq_add_stmt (&seq, bind);
10433 gimple_set_body (fndecl, seq);
10435 /* If we're instrumenting function entry/exit, then prepend the call to
10436 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
10437 catch the exit hook. */
10438 /* ??? Add some way to ignore exceptions for this TFE. */
10439 if (flag_instrument_function_entry_exit
10440 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
10441 && !flag_instrument_functions_exclude_p (fndecl))
10443 tree x;
10444 gbind *new_bind;
10445 gimple *tf;
10446 gimple_seq cleanup = NULL, body = NULL;
10447 tree tmp_var;
10448 gcall *call;
10450 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10451 call = gimple_build_call (x, 1, integer_zero_node);
10452 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10453 gimple_call_set_lhs (call, tmp_var);
10454 gimplify_seq_add_stmt (&cleanup, call);
10455 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
10456 call = gimple_build_call (x, 2,
10457 build_fold_addr_expr (current_function_decl),
10458 tmp_var);
10459 gimplify_seq_add_stmt (&cleanup, call);
10460 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
10462 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
10463 call = gimple_build_call (x, 1, integer_zero_node);
10464 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
10465 gimple_call_set_lhs (call, tmp_var);
10466 gimplify_seq_add_stmt (&body, call);
10467 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
10468 call = gimple_build_call (x, 2,
10469 build_fold_addr_expr (current_function_decl),
10470 tmp_var);
10471 gimplify_seq_add_stmt (&body, call);
10472 gimplify_seq_add_stmt (&body, tf);
10473 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
10474 /* Clear the block for BIND, since it is no longer directly inside
10475 the function, but within a try block. */
10476 gimple_bind_set_block (bind, NULL);
10478 /* Replace the current function body with the body
10479 wrapped in the try/finally TF. */
10480 seq = NULL;
10481 gimple_seq_add_stmt (&seq, new_bind);
10482 gimple_set_body (fndecl, seq);
10483 bind = new_bind;
10486 if ((flag_sanitize & SANITIZE_THREAD) != 0
10487 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl)))
10489 gcall *call = gimple_build_call_internal (IFN_TSAN_FUNC_EXIT, 0);
10490 gimple *tf = gimple_build_try (seq, call, GIMPLE_TRY_FINALLY);
10491 gbind *new_bind = gimple_build_bind (NULL, tf, gimple_bind_block (bind));
10492 /* Clear the block for BIND, since it is no longer directly inside
10493 the function, but within a try block. */
10494 gimple_bind_set_block (bind, NULL);
10495 /* Replace the current function body with the body
10496 wrapped in the try/finally TF. */
10497 seq = NULL;
10498 gimple_seq_add_stmt (&seq, new_bind);
10499 gimple_set_body (fndecl, seq);
10502 DECL_SAVED_TREE (fndecl) = NULL_TREE;
10503 cfun->curr_properties |= PROP_gimple_any;
10505 pop_cfun ();
10507 dump_function (TDI_generic, fndecl);
10510 /* Return a dummy expression of type TYPE in order to keep going after an
10511 error. */
10513 static tree
10514 dummy_object (tree type)
10516 tree t = build_int_cst (build_pointer_type (type), 0);
10517 return build2 (MEM_REF, type, t, t);
10520 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
10521 builtin function, but a very special sort of operator. */
10523 enum gimplify_status
10524 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p,
10525 gimple_seq *post_p ATTRIBUTE_UNUSED)
10527 tree promoted_type, have_va_type;
10528 tree valist = TREE_OPERAND (*expr_p, 0);
10529 tree type = TREE_TYPE (*expr_p);
10530 tree t, tag;
10531 location_t loc = EXPR_LOCATION (*expr_p);
10533 /* Verify that valist is of the proper type. */
10534 have_va_type = TREE_TYPE (valist);
10535 if (have_va_type == error_mark_node)
10536 return GS_ERROR;
10537 have_va_type = targetm.canonical_va_list_type (have_va_type);
10539 if (have_va_type == NULL_TREE)
10541 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
10542 return GS_ERROR;
10545 /* Generate a diagnostic for requesting data of a type that cannot
10546 be passed through `...' due to type promotion at the call site. */
10547 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
10548 != type)
10550 static bool gave_help;
10551 bool warned;
10553 /* Unfortunately, this is merely undefined, rather than a constraint
10554 violation, so we cannot make this an error. If this call is never
10555 executed, the program is still strictly conforming. */
10556 warned = warning_at (loc, 0,
10557 "%qT is promoted to %qT when passed through %<...%>",
10558 type, promoted_type);
10559 if (!gave_help && warned)
10561 gave_help = true;
10562 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
10563 promoted_type, type);
10566 /* We can, however, treat "undefined" any way we please.
10567 Call abort to encourage the user to fix the program. */
10568 if (warned)
10569 inform (loc, "if this code is reached, the program will abort");
10570 /* Before the abort, allow the evaluation of the va_list
10571 expression to exit or longjmp. */
10572 gimplify_and_add (valist, pre_p);
10573 t = build_call_expr_loc (loc,
10574 builtin_decl_implicit (BUILT_IN_TRAP), 0);
10575 gimplify_and_add (t, pre_p);
10577 /* This is dead code, but go ahead and finish so that the
10578 mode of the result comes out right. */
10579 *expr_p = dummy_object (type);
10580 return GS_ALL_DONE;
10583 tag = build_int_cst (build_pointer_type (type), 0);
10584 *expr_p = build_call_expr_internal_loc (loc, IFN_VA_ARG, type, 2, valist, tag);
10586 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
10587 needs to be expanded. */
10588 cfun->curr_properties &= ~PROP_gimple_lva;
10590 return GS_OK;
10593 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
10595 DST/SRC are the destination and source respectively. You can pass
10596 ungimplified trees in DST or SRC, in which case they will be
10597 converted to a gimple operand if necessary.
10599 This function returns the newly created GIMPLE_ASSIGN tuple. */
10601 gimple *
10602 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
10604 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
10605 gimplify_and_add (t, seq_p);
10606 ggc_free (t);
10607 return gimple_seq_last_stmt (*seq_p);
10610 inline hashval_t
10611 gimplify_hasher::hash (const elt_t *p)
10613 tree t = p->val;
10614 return iterative_hash_expr (t, 0);
10617 inline bool
10618 gimplify_hasher::equal (const elt_t *p1, const elt_t *p2)
10620 tree t1 = p1->val;
10621 tree t2 = p2->val;
10622 enum tree_code code = TREE_CODE (t1);
10624 if (TREE_CODE (t2) != code
10625 || TREE_TYPE (t1) != TREE_TYPE (t2))
10626 return false;
10628 if (!operand_equal_p (t1, t2, 0))
10629 return false;
10631 #ifdef ENABLE_CHECKING
10632 /* Only allow them to compare equal if they also hash equal; otherwise
10633 results are nondeterminate, and we fail bootstrap comparison. */
10634 gcc_assert (hash (p1) == hash (p2));
10635 #endif
10637 return true;