Merge trunk version 204345 into gupc branch.
[official-gcc.git] / gcc / gimplify.c
blob95532e0ba2fb2dcd1aba1ce1341a273fa4f50a43
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2013 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 "tm.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "tree-iterator.h"
30 #include "tree-inline.h"
31 #include "tree-pretty-print.h"
32 #include "langhooks.h"
33 #include "bitmap.h"
34 #include "gimple-ssa.h"
35 #include "cgraph.h"
36 #include "tree-cfg.h"
37 #include "tree-ssanames.h"
38 #include "tree-ssa.h"
39 #include "timevar.h"
40 #include "hashtab.h"
41 #include "flags.h"
42 #include "function.h"
43 #include "ggc.h"
44 #include "diagnostic-core.h"
45 #include "target.h"
46 #include "pointer-set.h"
47 #include "splay-tree.h"
48 #include "vec.h"
49 #include "omp-low.h"
50 #include "gimple-low.h"
51 #include "cilk.h"
53 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
54 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
55 #include "expr.h"
56 #include "tm_p.h"
58 enum gimplify_omp_var_data
60 GOVD_SEEN = 1,
61 GOVD_EXPLICIT = 2,
62 GOVD_SHARED = 4,
63 GOVD_PRIVATE = 8,
64 GOVD_FIRSTPRIVATE = 16,
65 GOVD_LASTPRIVATE = 32,
66 GOVD_REDUCTION = 64,
67 GOVD_LOCAL = 128,
68 GOVD_MAP = 256,
69 GOVD_DEBUG_PRIVATE = 512,
70 GOVD_PRIVATE_OUTER_REF = 1024,
71 GOVD_LINEAR = 2048,
72 GOVD_ALIGNED = 4096,
73 GOVD_MAP_TO_ONLY = 8192,
74 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
75 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
76 | GOVD_LOCAL)
80 enum omp_region_type
82 ORT_WORKSHARE = 0,
83 ORT_SIMD = 1,
84 ORT_PARALLEL = 2,
85 ORT_COMBINED_PARALLEL = 3,
86 ORT_TASK = 4,
87 ORT_UNTIED_TASK = 5,
88 ORT_TEAMS = 8,
89 ORT_TARGET_DATA = 16,
90 ORT_TARGET = 32
93 struct gimplify_omp_ctx
95 struct gimplify_omp_ctx *outer_context;
96 splay_tree variables;
97 struct pointer_set_t *privatized_types;
98 location_t location;
99 enum omp_clause_default_kind default_kind;
100 enum omp_region_type region_type;
101 bool combined_loop;
104 static struct gimplify_ctx *gimplify_ctxp;
105 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
108 /* Forward declaration. */
109 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
111 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
112 form and we don't do any syntax checking. */
114 void
115 mark_addressable (tree x)
117 while (handled_component_p (x))
118 x = TREE_OPERAND (x, 0);
119 if (TREE_CODE (x) == MEM_REF
120 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
121 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
122 if (TREE_CODE (x) != VAR_DECL
123 && TREE_CODE (x) != PARM_DECL
124 && TREE_CODE (x) != RESULT_DECL)
125 return;
126 TREE_ADDRESSABLE (x) = 1;
128 /* Also mark the artificial SSA_NAME that points to the partition of X. */
129 if (TREE_CODE (x) == VAR_DECL
130 && !DECL_EXTERNAL (x)
131 && !TREE_STATIC (x)
132 && cfun->gimple_df != NULL
133 && cfun->gimple_df->decls_to_pointers != NULL)
135 void *namep
136 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
137 if (namep)
138 TREE_ADDRESSABLE (*(tree *)namep) = 1;
142 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
143 *SEQ_P is NULL, a new sequence is allocated. This function is
144 similar to gimple_seq_add_stmt, but does not scan the operands.
145 During gimplification, we need to manipulate statement sequences
146 before the def/use vectors have been constructed. */
148 void
149 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
151 gimple_stmt_iterator si;
153 if (gs == NULL)
154 return;
156 si = gsi_last (*seq_p);
157 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
160 /* Shorter alias name for the above function for use in gimplify.c
161 only. */
163 static inline void
164 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
166 gimple_seq_add_stmt_without_update (seq_p, gs);
169 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
170 NULL, a new sequence is allocated. This function is
171 similar to gimple_seq_add_seq, but does not scan the operands.
172 During gimplification, we need to manipulate statement sequences
173 before the def/use vectors have been constructed. */
175 static void
176 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
178 gimple_stmt_iterator si;
180 if (src == NULL)
181 return;
183 si = gsi_last (*dst_p);
184 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
187 /* Set up a context for the gimplifier. */
189 void
190 push_gimplify_context (struct gimplify_ctx *c)
192 memset (c, '\0', sizeof (*c));
193 c->prev_context = gimplify_ctxp;
194 gimplify_ctxp = c;
197 /* Tear down a context for the gimplifier. If BODY is non-null, then
198 put the temporaries into the outer BIND_EXPR. Otherwise, put them
199 in the local_decls.
201 BODY is not a sequence, but the first tuple in a sequence. */
203 void
204 pop_gimplify_context (gimple body)
206 struct gimplify_ctx *c = gimplify_ctxp;
208 gcc_assert (c
209 && (!c->bind_expr_stack.exists ()
210 || c->bind_expr_stack.is_empty ()));
211 c->bind_expr_stack.release ();
212 gimplify_ctxp = c->prev_context;
214 if (body)
215 declare_vars (c->temps, body, false);
216 else
217 record_vars (c->temps);
219 if (c->temp_htab.is_created ())
220 c->temp_htab.dispose ();
223 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
225 static void
226 gimple_push_bind_expr (gimple gimple_bind)
228 gimplify_ctxp->bind_expr_stack.reserve (8);
229 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
232 /* Pop the first element off the stack of bindings. */
234 static void
235 gimple_pop_bind_expr (void)
237 gimplify_ctxp->bind_expr_stack.pop ();
240 /* Return the first element of the stack of bindings. */
242 gimple
243 gimple_current_bind_expr (void)
245 return gimplify_ctxp->bind_expr_stack.last ();
248 /* Return the stack of bindings created during gimplification. */
250 vec<gimple>
251 gimple_bind_expr_stack (void)
253 return gimplify_ctxp->bind_expr_stack;
256 /* Return true iff there is a COND_EXPR between us and the innermost
257 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
259 static bool
260 gimple_conditional_context (void)
262 return gimplify_ctxp->conditions > 0;
265 /* Note that we've entered a COND_EXPR. */
267 static void
268 gimple_push_condition (void)
270 #ifdef ENABLE_GIMPLE_CHECKING
271 if (gimplify_ctxp->conditions == 0)
272 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
273 #endif
274 ++(gimplify_ctxp->conditions);
277 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
278 now, add any conditional cleanups we've seen to the prequeue. */
280 static void
281 gimple_pop_condition (gimple_seq *pre_p)
283 int conds = --(gimplify_ctxp->conditions);
285 gcc_assert (conds >= 0);
286 if (conds == 0)
288 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
289 gimplify_ctxp->conditional_cleanups = NULL;
293 /* A stable comparison routine for use with splay trees and DECLs. */
295 static int
296 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
298 tree a = (tree) xa;
299 tree b = (tree) xb;
301 return DECL_UID (a) - DECL_UID (b);
304 /* Create a new omp construct that deals with variable remapping. */
306 static struct gimplify_omp_ctx *
307 new_omp_context (enum omp_region_type region_type)
309 struct gimplify_omp_ctx *c;
311 c = XCNEW (struct gimplify_omp_ctx);
312 c->outer_context = gimplify_omp_ctxp;
313 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
314 c->privatized_types = pointer_set_create ();
315 c->location = input_location;
316 c->region_type = region_type;
317 if ((region_type & ORT_TASK) == 0)
318 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
319 else
320 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
322 return c;
325 /* Destroy an omp construct that deals with variable remapping. */
327 static void
328 delete_omp_context (struct gimplify_omp_ctx *c)
330 splay_tree_delete (c->variables);
331 pointer_set_destroy (c->privatized_types);
332 XDELETE (c);
335 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
336 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
338 /* Both gimplify the statement T and append it to *SEQ_P. This function
339 behaves exactly as gimplify_stmt, but you don't have to pass T as a
340 reference. */
342 void
343 gimplify_and_add (tree t, gimple_seq *seq_p)
345 gimplify_stmt (&t, seq_p);
348 /* Gimplify statement T into sequence *SEQ_P, and return the first
349 tuple in the sequence of generated tuples for this statement.
350 Return NULL if gimplifying T produced no tuples. */
352 static gimple
353 gimplify_and_return_first (tree t, gimple_seq *seq_p)
355 gimple_stmt_iterator last = gsi_last (*seq_p);
357 gimplify_and_add (t, seq_p);
359 if (!gsi_end_p (last))
361 gsi_next (&last);
362 return gsi_stmt (last);
364 else
365 return gimple_seq_first_stmt (*seq_p);
368 /* Strip off a legitimate source ending from the input string NAME of
369 length LEN. Rather than having to know the names used by all of
370 our front ends, we strip off an ending of a period followed by
371 up to five characters. (Java uses ".class".) */
373 static inline void
374 remove_suffix (char *name, int len)
376 int i;
378 for (i = 2; i < 8 && len > i; i++)
380 if (name[len - i] == '.')
382 name[len - i] = '\0';
383 break;
388 /* Create a new temporary name with PREFIX. Return an identifier. */
390 static GTY(()) unsigned int tmp_var_id_num;
392 tree
393 create_tmp_var_name (const char *prefix)
395 char *tmp_name;
397 if (prefix)
399 char *preftmp = ASTRDUP (prefix);
401 remove_suffix (preftmp, strlen (preftmp));
402 clean_symbol_name (preftmp);
404 prefix = preftmp;
407 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
408 return get_identifier (tmp_name);
411 /* Create a new temporary variable declaration of type TYPE.
412 Do NOT push it into the current binding. */
414 tree
415 create_tmp_var_raw (tree type, const char *prefix)
417 tree tmp_var;
419 /* Temps. cannot be UPC shared qualified. */
420 gcc_assert (!upc_shared_type_p (type));
422 tmp_var = build_decl (input_location,
423 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
424 type);
426 /* The variable was declared by the compiler. */
427 DECL_ARTIFICIAL (tmp_var) = 1;
428 /* And we don't want debug info for it. */
429 DECL_IGNORED_P (tmp_var) = 1;
431 /* Make the variable writable. */
432 TREE_READONLY (tmp_var) = 0;
434 DECL_EXTERNAL (tmp_var) = 0;
435 TREE_STATIC (tmp_var) = 0;
436 TREE_USED (tmp_var) = 1;
438 return tmp_var;
441 /* Create a new temporary variable declaration of type TYPE. DO push the
442 variable into the current binding. Further, assume that this is called
443 only from gimplification or optimization, at which point the creation of
444 certain types are bugs. */
446 tree
447 create_tmp_var (tree type, const char *prefix)
449 tree tmp_var;
451 /* We don't allow types that are addressable (meaning we can't make copies),
452 or incomplete. We also used to reject every variable size objects here,
453 but now support those for which a constant upper bound can be obtained.
454 The processing for variable sizes is performed in gimple_add_tmp_var,
455 point at which it really matters and possibly reached via paths not going
456 through this function, e.g. after direct calls to create_tmp_var_raw. */
457 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
459 tmp_var = create_tmp_var_raw (type, prefix);
460 gimple_add_tmp_var (tmp_var);
461 return tmp_var;
464 /* Create a new temporary variable declaration of type TYPE by calling
465 create_tmp_var and if TYPE is a vector or a complex number, mark the new
466 temporary as gimple register. */
468 tree
469 create_tmp_reg (tree type, const char *prefix)
471 tree tmp;
473 tmp = create_tmp_var (type, prefix);
474 if (TREE_CODE (type) == COMPLEX_TYPE
475 || TREE_CODE (type) == VECTOR_TYPE)
476 DECL_GIMPLE_REG_P (tmp) = 1;
478 return tmp;
481 /* Returns true iff T is a valid RHS for an assignment to a renamed
482 user -- or front-end generated artificial -- variable. */
484 static bool
485 is_gimple_reg_rhs (tree t)
487 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
490 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
491 LHS, or for a call argument. */
493 static bool
494 is_gimple_mem_rhs (tree t)
496 /* If we're dealing with a renamable type, either source or dest must be
497 a renamed variable. */
498 if (is_gimple_reg_type (TREE_TYPE (t)))
499 return is_gimple_val (t);
500 else
501 return is_gimple_val (t) || is_gimple_lvalue (t);
504 /* Return true if T is a CALL_EXPR or an expression that can be
505 assigned to a temporary. Note that this predicate should only be
506 used during gimplification. See the rationale for this in
507 gimplify_modify_expr. */
509 static bool
510 is_gimple_reg_rhs_or_call (tree t)
512 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
513 || TREE_CODE (t) == CALL_EXPR);
516 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
517 this predicate should only be used during gimplification. See the
518 rationale for this in gimplify_modify_expr. */
520 static bool
521 is_gimple_mem_rhs_or_call (tree t)
523 /* If we're dealing with a renamable type, either source or dest must be
524 a renamed variable. */
525 if (is_gimple_reg_type (TREE_TYPE (t)))
526 return is_gimple_val (t);
527 else
528 return (is_gimple_val (t) || is_gimple_lvalue (t)
529 || TREE_CODE (t) == CALL_EXPR);
532 /* Create a temporary with a name derived from VAL. Subroutine of
533 lookup_tmp_var; nobody else should call this function. */
535 static inline tree
536 create_tmp_from_val (tree val, bool is_formal)
538 /* Drop all qualifiers and address-space information from the value type. */
539 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
540 tree var = create_tmp_var (type, get_name (val));
541 if (is_formal
542 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
543 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
544 DECL_GIMPLE_REG_P (var) = 1;
545 return var;
548 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
549 an existing expression temporary. */
551 static tree
552 lookup_tmp_var (tree val, bool is_formal)
554 tree ret;
556 /* If not optimizing, never really reuse a temporary. local-alloc
557 won't allocate any variable that is used in more than one basic
558 block, which means it will go into memory, causing much extra
559 work in reload and final and poorer code generation, outweighing
560 the extra memory allocation here. */
561 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
562 ret = create_tmp_from_val (val, is_formal);
563 else
565 elt_t elt, *elt_p;
566 elt_t **slot;
568 elt.val = val;
569 if (!gimplify_ctxp->temp_htab.is_created ())
570 gimplify_ctxp->temp_htab.create (1000);
571 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
572 if (*slot == NULL)
574 elt_p = XNEW (elt_t);
575 elt_p->val = val;
576 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
577 *slot = elt_p;
579 else
581 elt_p = *slot;
582 ret = elt_p->temp;
586 return ret;
589 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
591 static tree
592 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
593 bool is_formal)
595 tree t, mod;
597 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
598 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
599 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
600 fb_rvalue);
602 if (gimplify_ctxp->into_ssa
603 && is_gimple_reg_type (TREE_TYPE (val)))
604 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
605 else
606 t = lookup_tmp_var (val, is_formal);
608 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
610 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
612 /* gimplify_modify_expr might want to reduce this further. */
613 gimplify_and_add (mod, pre_p);
614 ggc_free (mod);
616 return t;
619 /* Return a formal temporary variable initialized with VAL. PRE_P is as
620 in gimplify_expr. Only use this function if:
622 1) The value of the unfactored expression represented by VAL will not
623 change between the initialization and use of the temporary, and
624 2) The temporary will not be otherwise modified.
626 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
627 and #2 means it is inappropriate for && temps.
629 For other cases, use get_initialized_tmp_var instead. */
631 tree
632 get_formal_tmp_var (tree val, gimple_seq *pre_p)
634 return internal_get_tmp_var (val, pre_p, NULL, true);
637 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
638 are as in gimplify_expr. */
640 tree
641 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
643 return internal_get_tmp_var (val, pre_p, post_p, false);
646 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
647 generate debug info for them; otherwise don't. */
649 void
650 declare_vars (tree vars, gimple scope, bool debug_info)
652 tree last = vars;
653 if (last)
655 tree temps, block;
657 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
659 temps = nreverse (last);
661 block = gimple_bind_block (scope);
662 gcc_assert (!block || TREE_CODE (block) == BLOCK);
663 if (!block || !debug_info)
665 DECL_CHAIN (last) = gimple_bind_vars (scope);
666 gimple_bind_set_vars (scope, temps);
668 else
670 /* We need to attach the nodes both to the BIND_EXPR and to its
671 associated BLOCK for debugging purposes. The key point here
672 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
673 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
674 if (BLOCK_VARS (block))
675 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
676 else
678 gimple_bind_set_vars (scope,
679 chainon (gimple_bind_vars (scope), temps));
680 BLOCK_VARS (block) = temps;
686 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
687 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
688 no such upper bound can be obtained. */
690 static void
691 force_constant_size (tree var)
693 /* The only attempt we make is by querying the maximum size of objects
694 of the variable's type. */
696 HOST_WIDE_INT max_size;
698 gcc_assert (TREE_CODE (var) == VAR_DECL);
700 max_size = max_int_size_in_bytes (TREE_TYPE (var));
702 gcc_assert (max_size >= 0);
704 DECL_SIZE_UNIT (var)
705 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
706 DECL_SIZE (var)
707 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
710 /* Push the temporary variable TMP into the current binding. */
712 void
713 gimple_add_tmp_var (tree tmp)
715 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
717 /* Later processing assumes that the object size is constant, which might
718 not be true at this point. Force the use of a constant upper bound in
719 this case. */
720 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
721 force_constant_size (tmp);
723 DECL_CONTEXT (tmp) = current_function_decl;
724 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
726 if (gimplify_ctxp)
728 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
729 gimplify_ctxp->temps = tmp;
731 /* Mark temporaries local within the nearest enclosing parallel. */
732 if (gimplify_omp_ctxp)
734 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
735 while (ctx
736 && (ctx->region_type == ORT_WORKSHARE
737 || ctx->region_type == ORT_SIMD))
738 ctx = ctx->outer_context;
739 if (ctx)
740 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
743 else if (cfun)
744 record_vars (tmp);
745 else
747 gimple_seq body_seq;
749 /* This case is for nested functions. We need to expose the locals
750 they create. */
751 body_seq = gimple_body (current_function_decl);
752 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
756 /* Determine whether to assign a location to the statement GS. */
758 static bool
759 should_carry_location_p (gimple gs)
761 /* Don't emit a line note for a label. We particularly don't want to
762 emit one for the break label, since it doesn't actually correspond
763 to the beginning of the loop/switch. */
764 if (gimple_code (gs) == GIMPLE_LABEL)
765 return false;
767 return true;
770 /* Return true if a location should not be emitted for this statement
771 by annotate_one_with_location. */
773 static inline bool
774 gimple_do_not_emit_location_p (gimple g)
776 return gimple_plf (g, GF_PLF_1);
779 /* Mark statement G so a location will not be emitted by
780 annotate_one_with_location. */
782 static inline void
783 gimple_set_do_not_emit_location (gimple g)
785 /* The PLF flags are initialized to 0 when a new tuple is created,
786 so no need to initialize it anywhere. */
787 gimple_set_plf (g, GF_PLF_1, true);
790 /* Set the location for gimple statement GS to LOCATION. */
792 static void
793 annotate_one_with_location (gimple gs, location_t location)
795 if (!gimple_has_location (gs)
796 && !gimple_do_not_emit_location_p (gs)
797 && should_carry_location_p (gs))
798 gimple_set_location (gs, location);
801 /* Set LOCATION for all the statements after iterator GSI in sequence
802 SEQ. If GSI is pointing to the end of the sequence, start with the
803 first statement in SEQ. */
805 static void
806 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
807 location_t location)
809 if (gsi_end_p (gsi))
810 gsi = gsi_start (seq);
811 else
812 gsi_next (&gsi);
814 for (; !gsi_end_p (gsi); gsi_next (&gsi))
815 annotate_one_with_location (gsi_stmt (gsi), location);
818 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
820 void
821 annotate_all_with_location (gimple_seq stmt_p, location_t location)
823 gimple_stmt_iterator i;
825 if (gimple_seq_empty_p (stmt_p))
826 return;
828 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
830 gimple gs = gsi_stmt (i);
831 annotate_one_with_location (gs, location);
835 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
836 nodes that are referenced more than once in GENERIC functions. This is
837 necessary because gimplification (translation into GIMPLE) is performed
838 by modifying tree nodes in-place, so gimplication of a shared node in a
839 first context could generate an invalid GIMPLE form in a second context.
841 This is achieved with a simple mark/copy/unmark algorithm that walks the
842 GENERIC representation top-down, marks nodes with TREE_VISITED the first
843 time it encounters them, duplicates them if they already have TREE_VISITED
844 set, and finally removes the TREE_VISITED marks it has set.
846 The algorithm works only at the function level, i.e. it generates a GENERIC
847 representation of a function with no nodes shared within the function when
848 passed a GENERIC function (except for nodes that are allowed to be shared).
850 At the global level, it is also necessary to unshare tree nodes that are
851 referenced in more than one function, for the same aforementioned reason.
852 This requires some cooperation from the front-end. There are 2 strategies:
854 1. Manual unsharing. The front-end needs to call unshare_expr on every
855 expression that might end up being shared across functions.
857 2. Deep unsharing. This is an extension of regular unsharing. Instead
858 of calling unshare_expr on expressions that might be shared across
859 functions, the front-end pre-marks them with TREE_VISITED. This will
860 ensure that they are unshared on the first reference within functions
861 when the regular unsharing algorithm runs. The counterpart is that
862 this algorithm must look deeper than for manual unsharing, which is
863 specified by LANG_HOOKS_DEEP_UNSHARING.
865 If there are only few specific cases of node sharing across functions, it is
866 probably easier for a front-end to unshare the expressions manually. On the
867 contrary, if the expressions generated at the global level are as widespread
868 as expressions generated within functions, deep unsharing is very likely the
869 way to go. */
871 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
872 These nodes model computations that must be done once. If we were to
873 unshare something like SAVE_EXPR(i++), the gimplification process would
874 create wrong code. However, if DATA is non-null, it must hold a pointer
875 set that is used to unshare the subtrees of these nodes. */
877 static tree
878 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
880 tree t = *tp;
881 enum tree_code code = TREE_CODE (t);
883 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
884 copy their subtrees if we can make sure to do it only once. */
885 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
887 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
889 else
890 *walk_subtrees = 0;
893 /* Stop at types, decls, constants like copy_tree_r. */
894 else if (TREE_CODE_CLASS (code) == tcc_type
895 || TREE_CODE_CLASS (code) == tcc_declaration
896 || TREE_CODE_CLASS (code) == tcc_constant
897 /* We can't do anything sensible with a BLOCK used as an
898 expression, but we also can't just die when we see it
899 because of non-expression uses. So we avert our eyes
900 and cross our fingers. Silly Java. */
901 || code == BLOCK)
902 *walk_subtrees = 0;
904 /* Cope with the statement expression extension. */
905 else if (code == STATEMENT_LIST)
908 /* Leave the bulk of the work to copy_tree_r itself. */
909 else
910 copy_tree_r (tp, walk_subtrees, NULL);
912 return NULL_TREE;
915 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
916 If *TP has been visited already, then *TP is deeply copied by calling
917 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
919 static tree
920 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
922 tree t = *tp;
923 enum tree_code code = TREE_CODE (t);
925 /* Skip types, decls, and constants. But we do want to look at their
926 types and the bounds of types. Mark them as visited so we properly
927 unmark their subtrees on the unmark pass. If we've already seen them,
928 don't look down further. */
929 if (TREE_CODE_CLASS (code) == tcc_type
930 || TREE_CODE_CLASS (code) == tcc_declaration
931 || TREE_CODE_CLASS (code) == tcc_constant)
933 if (TREE_VISITED (t))
934 *walk_subtrees = 0;
935 else
936 TREE_VISITED (t) = 1;
939 /* If this node has been visited already, unshare it and don't look
940 any deeper. */
941 else if (TREE_VISITED (t))
943 walk_tree (tp, mostly_copy_tree_r, data, NULL);
944 *walk_subtrees = 0;
947 /* Otherwise, mark the node as visited and keep looking. */
948 else
949 TREE_VISITED (t) = 1;
951 return NULL_TREE;
954 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
955 copy_if_shared_r callback unmodified. */
957 static inline void
958 copy_if_shared (tree *tp, void *data)
960 walk_tree (tp, copy_if_shared_r, data, NULL);
963 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
964 any nested functions. */
966 static void
967 unshare_body (tree fndecl)
969 struct cgraph_node *cgn = cgraph_get_node (fndecl);
970 /* If the language requires deep unsharing, we need a pointer set to make
971 sure we don't repeatedly unshare subtrees of unshareable nodes. */
972 struct pointer_set_t *visited
973 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
975 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
976 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
977 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
979 if (visited)
980 pointer_set_destroy (visited);
982 if (cgn)
983 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
984 unshare_body (cgn->decl);
987 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
988 Subtrees are walked until the first unvisited node is encountered. */
990 static tree
991 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
993 tree t = *tp;
995 /* If this node has been visited, unmark it and keep looking. */
996 if (TREE_VISITED (t))
997 TREE_VISITED (t) = 0;
999 /* Otherwise, don't look any deeper. */
1000 else
1001 *walk_subtrees = 0;
1003 return NULL_TREE;
1006 /* Unmark the visited trees rooted at *TP. */
1008 static inline void
1009 unmark_visited (tree *tp)
1011 walk_tree (tp, unmark_visited_r, NULL, NULL);
1014 /* Likewise, but mark all trees as not visited. */
1016 static void
1017 unvisit_body (tree fndecl)
1019 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1021 unmark_visited (&DECL_SAVED_TREE (fndecl));
1022 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1023 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1025 if (cgn)
1026 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1027 unvisit_body (cgn->decl);
1030 /* Unconditionally make an unshared copy of EXPR. This is used when using
1031 stored expressions which span multiple functions, such as BINFO_VTABLE,
1032 as the normal unsharing process can't tell that they're shared. */
1034 tree
1035 unshare_expr (tree expr)
1037 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1038 return expr;
1041 /* Worker for unshare_expr_without_location. */
1043 static tree
1044 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1046 if (EXPR_P (*tp))
1047 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1048 else
1049 *walk_subtrees = 0;
1050 return NULL_TREE;
1053 /* Similar to unshare_expr but also prune all expression locations
1054 from EXPR. */
1056 tree
1057 unshare_expr_without_location (tree expr)
1059 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1060 if (EXPR_P (expr))
1061 walk_tree (&expr, prune_expr_location, NULL, NULL);
1062 return expr;
1065 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1066 contain statements and have a value. Assign its value to a temporary
1067 and give it void_type_node. Return the temporary, or NULL_TREE if
1068 WRAPPER was already void. */
1070 tree
1071 voidify_wrapper_expr (tree wrapper, tree temp)
1073 tree type = TREE_TYPE (wrapper);
1074 if (type && !VOID_TYPE_P (type))
1076 tree *p;
1078 /* Set p to point to the body of the wrapper. Loop until we find
1079 something that isn't a wrapper. */
1080 for (p = &wrapper; p && *p; )
1082 switch (TREE_CODE (*p))
1084 case BIND_EXPR:
1085 TREE_SIDE_EFFECTS (*p) = 1;
1086 TREE_TYPE (*p) = void_type_node;
1087 /* For a BIND_EXPR, the body is operand 1. */
1088 p = &BIND_EXPR_BODY (*p);
1089 break;
1091 case CLEANUP_POINT_EXPR:
1092 case TRY_FINALLY_EXPR:
1093 case TRY_CATCH_EXPR:
1094 TREE_SIDE_EFFECTS (*p) = 1;
1095 TREE_TYPE (*p) = void_type_node;
1096 p = &TREE_OPERAND (*p, 0);
1097 break;
1099 case STATEMENT_LIST:
1101 tree_stmt_iterator i = tsi_last (*p);
1102 TREE_SIDE_EFFECTS (*p) = 1;
1103 TREE_TYPE (*p) = void_type_node;
1104 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1106 break;
1108 case COMPOUND_EXPR:
1109 /* Advance to the last statement. Set all container types to
1110 void. */
1111 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1113 TREE_SIDE_EFFECTS (*p) = 1;
1114 TREE_TYPE (*p) = void_type_node;
1116 break;
1118 case TRANSACTION_EXPR:
1119 TREE_SIDE_EFFECTS (*p) = 1;
1120 TREE_TYPE (*p) = void_type_node;
1121 p = &TRANSACTION_EXPR_BODY (*p);
1122 break;
1124 default:
1125 /* Assume that any tree upon which voidify_wrapper_expr is
1126 directly called is a wrapper, and that its body is op0. */
1127 if (p == &wrapper)
1129 TREE_SIDE_EFFECTS (*p) = 1;
1130 TREE_TYPE (*p) = void_type_node;
1131 p = &TREE_OPERAND (*p, 0);
1132 break;
1134 goto out;
1138 out:
1139 if (p == NULL || IS_EMPTY_STMT (*p))
1140 temp = NULL_TREE;
1141 else if (temp)
1143 /* The wrapper is on the RHS of an assignment that we're pushing
1144 down. */
1145 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1146 || TREE_CODE (temp) == MODIFY_EXPR);
1147 TREE_OPERAND (temp, 1) = *p;
1148 *p = temp;
1150 else
1152 temp = create_tmp_var (type, "retval");
1153 *p = build2 (INIT_EXPR, type, temp, *p);
1156 return temp;
1159 return NULL_TREE;
1162 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1163 a temporary through which they communicate. */
1165 static void
1166 build_stack_save_restore (gimple *save, gimple *restore)
1168 tree tmp_var;
1170 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1171 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1172 gimple_call_set_lhs (*save, tmp_var);
1174 *restore
1175 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1176 1, tmp_var);
1179 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1181 static enum gimplify_status
1182 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1184 tree bind_expr = *expr_p;
1185 bool old_save_stack = gimplify_ctxp->save_stack;
1186 tree t;
1187 gimple gimple_bind;
1188 gimple_seq body, cleanup;
1189 gimple stack_save;
1191 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1193 /* Mark variables seen in this bind expr. */
1194 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1196 if (TREE_CODE (t) == VAR_DECL)
1198 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1200 /* Mark variable as local. */
1201 if (ctx && !DECL_EXTERNAL (t)
1202 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1203 || splay_tree_lookup (ctx->variables,
1204 (splay_tree_key) t) == NULL))
1205 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1207 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1209 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1210 cfun->has_local_explicit_reg_vars = true;
1213 /* Preliminarily mark non-addressed complex variables as eligible
1214 for promotion to gimple registers. We'll transform their uses
1215 as we find them. */
1216 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1217 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1218 && !TREE_THIS_VOLATILE (t)
1219 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1220 && !needs_to_live_in_memory (t))
1221 DECL_GIMPLE_REG_P (t) = 1;
1224 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1225 BIND_EXPR_BLOCK (bind_expr));
1226 gimple_push_bind_expr (gimple_bind);
1228 gimplify_ctxp->save_stack = false;
1230 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1231 body = NULL;
1232 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1233 gimple_bind_set_body (gimple_bind, body);
1235 cleanup = NULL;
1236 stack_save = NULL;
1237 if (gimplify_ctxp->save_stack)
1239 gimple stack_restore;
1241 /* Save stack on entry and restore it on exit. Add a try_finally
1242 block to achieve this. */
1243 build_stack_save_restore (&stack_save, &stack_restore);
1245 gimplify_seq_add_stmt (&cleanup, stack_restore);
1248 /* Add clobbers for all variables that go out of scope. */
1249 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1251 if (TREE_CODE (t) == VAR_DECL
1252 && !is_global_var (t)
1253 && DECL_CONTEXT (t) == current_function_decl
1254 && !DECL_HARD_REGISTER (t)
1255 && !TREE_THIS_VOLATILE (t)
1256 && !DECL_HAS_VALUE_EXPR_P (t)
1257 /* Only care for variables that have to be in memory. Others
1258 will be rewritten into SSA names, hence moved to the top-level. */
1259 && !is_gimple_reg (t)
1260 && flag_stack_reuse != SR_NONE)
1262 tree clobber = build_constructor (TREE_TYPE (t),
1263 NULL);
1264 TREE_THIS_VOLATILE (clobber) = 1;
1265 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1269 if (cleanup)
1271 gimple gs;
1272 gimple_seq new_body;
1274 new_body = NULL;
1275 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1276 GIMPLE_TRY_FINALLY);
1278 if (stack_save)
1279 gimplify_seq_add_stmt (&new_body, stack_save);
1280 gimplify_seq_add_stmt (&new_body, gs);
1281 gimple_bind_set_body (gimple_bind, new_body);
1284 gimplify_ctxp->save_stack = old_save_stack;
1285 gimple_pop_bind_expr ();
1287 gimplify_seq_add_stmt (pre_p, gimple_bind);
1289 if (temp)
1291 *expr_p = temp;
1292 return GS_OK;
1295 *expr_p = NULL_TREE;
1296 return GS_ALL_DONE;
1299 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1300 GIMPLE value, it is assigned to a new temporary and the statement is
1301 re-written to return the temporary.
1303 PRE_P points to the sequence where side effects that must happen before
1304 STMT should be stored. */
1306 static enum gimplify_status
1307 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1309 gimple ret;
1310 tree ret_expr = TREE_OPERAND (stmt, 0);
1311 tree result_decl, result;
1313 if (ret_expr == error_mark_node)
1314 return GS_ERROR;
1316 /* Implicit _Cilk_sync must be inserted right before any return statement
1317 if there is a _Cilk_spawn in the function. If the user has provided a
1318 _Cilk_sync, the optimizer should remove this duplicate one. */
1319 if (fn_contains_cilk_spawn_p (cfun))
1321 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1322 gimplify_and_add (impl_sync, pre_p);
1325 if (!ret_expr
1326 || TREE_CODE (ret_expr) == RESULT_DECL
1327 || ret_expr == error_mark_node)
1329 gimple ret = gimple_build_return (ret_expr);
1330 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1331 gimplify_seq_add_stmt (pre_p, ret);
1332 return GS_ALL_DONE;
1335 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1336 result_decl = NULL_TREE;
1337 else
1339 result_decl = TREE_OPERAND (ret_expr, 0);
1341 /* See through a return by reference. */
1342 if (TREE_CODE (result_decl) == INDIRECT_REF)
1343 result_decl = TREE_OPERAND (result_decl, 0);
1345 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1346 || TREE_CODE (ret_expr) == INIT_EXPR)
1347 && TREE_CODE (result_decl) == RESULT_DECL);
1350 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1351 Recall that aggregate_value_p is FALSE for any aggregate type that is
1352 returned in registers. If we're returning values in registers, then
1353 we don't want to extend the lifetime of the RESULT_DECL, particularly
1354 across another call. In addition, for those aggregates for which
1355 hard_function_value generates a PARALLEL, we'll die during normal
1356 expansion of structure assignments; there's special code in expand_return
1357 to handle this case that does not exist in expand_expr. */
1358 if (!result_decl)
1359 result = NULL_TREE;
1360 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1362 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1364 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1365 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1366 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1367 should be effectively allocated by the caller, i.e. all calls to
1368 this function must be subject to the Return Slot Optimization. */
1369 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1370 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1372 result = result_decl;
1374 else if (gimplify_ctxp->return_temp)
1375 result = gimplify_ctxp->return_temp;
1376 else
1378 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1380 /* ??? With complex control flow (usually involving abnormal edges),
1381 we can wind up warning about an uninitialized value for this. Due
1382 to how this variable is constructed and initialized, this is never
1383 true. Give up and never warn. */
1384 TREE_NO_WARNING (result) = 1;
1386 gimplify_ctxp->return_temp = result;
1389 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1390 Then gimplify the whole thing. */
1391 if (result != result_decl)
1392 TREE_OPERAND (ret_expr, 0) = result;
1394 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1396 ret = gimple_build_return (result);
1397 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1398 gimplify_seq_add_stmt (pre_p, ret);
1400 return GS_ALL_DONE;
1403 /* Gimplify a variable-length array DECL. */
1405 static void
1406 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1408 /* This is a variable-sized decl. Simplify its size and mark it
1409 for deferred expansion. */
1410 tree t, addr, ptr_type;
1412 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1413 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1415 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1416 if (DECL_HAS_VALUE_EXPR_P (decl))
1417 return;
1419 /* All occurrences of this decl in final gimplified code will be
1420 replaced by indirection. Setting DECL_VALUE_EXPR does two
1421 things: First, it lets the rest of the gimplifier know what
1422 replacement to use. Second, it lets the debug info know
1423 where to find the value. */
1424 ptr_type = build_pointer_type (TREE_TYPE (decl));
1425 addr = create_tmp_var (ptr_type, get_name (decl));
1426 DECL_IGNORED_P (addr) = 0;
1427 t = build_fold_indirect_ref (addr);
1428 TREE_THIS_NOTRAP (t) = 1;
1429 SET_DECL_VALUE_EXPR (decl, t);
1430 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1432 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1433 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1434 size_int (DECL_ALIGN (decl)));
1435 /* The call has been built for a variable-sized object. */
1436 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1437 t = fold_convert (ptr_type, t);
1438 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1440 gimplify_and_add (t, seq_p);
1442 /* Indicate that we need to restore the stack level when the
1443 enclosing BIND_EXPR is exited. */
1444 gimplify_ctxp->save_stack = true;
1447 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1448 and initialization explicit. */
1450 static enum gimplify_status
1451 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1453 tree stmt = *stmt_p;
1454 tree decl = DECL_EXPR_DECL (stmt);
1456 *stmt_p = NULL_TREE;
1458 if (TREE_TYPE (decl) == error_mark_node)
1459 return GS_ERROR;
1461 if ((TREE_CODE (decl) == TYPE_DECL
1462 || TREE_CODE (decl) == VAR_DECL)
1463 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1464 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1466 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1467 in case its size expressions contain problematic nodes like CALL_EXPR. */
1468 if (TREE_CODE (decl) == TYPE_DECL
1469 && DECL_ORIGINAL_TYPE (decl)
1470 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1471 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1473 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1475 tree init = DECL_INITIAL (decl);
1477 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1478 || (!TREE_STATIC (decl)
1479 && flag_stack_check == GENERIC_STACK_CHECK
1480 && compare_tree_int (DECL_SIZE_UNIT (decl),
1481 STACK_CHECK_MAX_VAR_SIZE) > 0))
1482 gimplify_vla_decl (decl, seq_p);
1484 /* Some front ends do not explicitly declare all anonymous
1485 artificial variables. We compensate here by declaring the
1486 variables, though it would be better if the front ends would
1487 explicitly declare them. */
1488 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1489 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1490 gimple_add_tmp_var (decl);
1492 if (init && init != error_mark_node)
1494 if (!TREE_STATIC (decl))
1496 DECL_INITIAL (decl) = NULL_TREE;
1497 init = build2 (INIT_EXPR, void_type_node, decl, init);
1498 gimplify_and_add (init, seq_p);
1499 ggc_free (init);
1501 else
1502 /* We must still examine initializers for static variables
1503 as they may contain a label address. */
1504 walk_tree (&init, force_labels_r, NULL, NULL);
1508 return GS_ALL_DONE;
1511 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1512 and replacing the LOOP_EXPR with goto, but if the loop contains an
1513 EXIT_EXPR, we need to append a label for it to jump to. */
1515 static enum gimplify_status
1516 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1518 tree saved_label = gimplify_ctxp->exit_label;
1519 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1521 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1523 gimplify_ctxp->exit_label = NULL_TREE;
1525 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1527 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1529 if (gimplify_ctxp->exit_label)
1530 gimplify_seq_add_stmt (pre_p,
1531 gimple_build_label (gimplify_ctxp->exit_label));
1533 gimplify_ctxp->exit_label = saved_label;
1535 *expr_p = NULL;
1536 return GS_ALL_DONE;
1539 /* Gimplify a statement list onto a sequence. These may be created either
1540 by an enlightened front-end, or by shortcut_cond_expr. */
1542 static enum gimplify_status
1543 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1545 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1547 tree_stmt_iterator i = tsi_start (*expr_p);
1549 while (!tsi_end_p (i))
1551 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1552 tsi_delink (&i);
1555 if (temp)
1557 *expr_p = temp;
1558 return GS_OK;
1561 return GS_ALL_DONE;
1564 /* Compare two case labels. Because the front end should already have
1565 made sure that case ranges do not overlap, it is enough to only compare
1566 the CASE_LOW values of each case label. */
1568 static int
1569 compare_case_labels (const void *p1, const void *p2)
1571 const_tree const case1 = *(const_tree const*)p1;
1572 const_tree const case2 = *(const_tree const*)p2;
1574 /* The 'default' case label always goes first. */
1575 if (!CASE_LOW (case1))
1576 return -1;
1577 else if (!CASE_LOW (case2))
1578 return 1;
1579 else
1580 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1583 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1585 void
1586 sort_case_labels (vec<tree> label_vec)
1588 label_vec.qsort (compare_case_labels);
1591 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1593 LABELS is a vector that contains all case labels to look at.
1595 INDEX_TYPE is the type of the switch index expression. Case labels
1596 in LABELS are discarded if their values are not in the value range
1597 covered by INDEX_TYPE. The remaining case label values are folded
1598 to INDEX_TYPE.
1600 If a default case exists in LABELS, it is removed from LABELS and
1601 returned in DEFAULT_CASEP. If no default case exists, but the
1602 case labels already cover the whole range of INDEX_TYPE, a default
1603 case is returned pointing to one of the existing case labels.
1604 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1606 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1607 apply and no action is taken regardless of whether a default case is
1608 found or not. */
1610 void
1611 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1612 tree index_type,
1613 tree *default_casep)
1615 tree min_value, max_value;
1616 tree default_case = NULL_TREE;
1617 size_t i, len;
1619 i = 0;
1620 min_value = TYPE_MIN_VALUE (index_type);
1621 max_value = TYPE_MAX_VALUE (index_type);
1622 while (i < labels.length ())
1624 tree elt = labels[i];
1625 tree low = CASE_LOW (elt);
1626 tree high = CASE_HIGH (elt);
1627 bool remove_element = FALSE;
1629 if (low)
1631 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1632 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1634 /* This is a non-default case label, i.e. it has a value.
1636 See if the case label is reachable within the range of
1637 the index type. Remove out-of-range case values. Turn
1638 case ranges into a canonical form (high > low strictly)
1639 and convert the case label values to the index type.
1641 NB: The type of gimple_switch_index() may be the promoted
1642 type, but the case labels retain the original type. */
1644 if (high)
1646 /* This is a case range. Discard empty ranges.
1647 If the bounds or the range are equal, turn this
1648 into a simple (one-value) case. */
1649 int cmp = tree_int_cst_compare (high, low);
1650 if (cmp < 0)
1651 remove_element = TRUE;
1652 else if (cmp == 0)
1653 high = NULL_TREE;
1656 if (! high)
1658 /* If the simple case value is unreachable, ignore it. */
1659 if ((TREE_CODE (min_value) == INTEGER_CST
1660 && tree_int_cst_compare (low, min_value) < 0)
1661 || (TREE_CODE (max_value) == INTEGER_CST
1662 && tree_int_cst_compare (low, max_value) > 0))
1663 remove_element = TRUE;
1664 else
1665 low = fold_convert (index_type, low);
1667 else
1669 /* If the entire case range is unreachable, ignore it. */
1670 if ((TREE_CODE (min_value) == INTEGER_CST
1671 && tree_int_cst_compare (high, min_value) < 0)
1672 || (TREE_CODE (max_value) == INTEGER_CST
1673 && tree_int_cst_compare (low, max_value) > 0))
1674 remove_element = TRUE;
1675 else
1677 /* If the lower bound is less than the index type's
1678 minimum value, truncate the range bounds. */
1679 if (TREE_CODE (min_value) == INTEGER_CST
1680 && tree_int_cst_compare (low, min_value) < 0)
1681 low = min_value;
1682 low = fold_convert (index_type, low);
1684 /* If the upper bound is greater than the index type's
1685 maximum value, truncate the range bounds. */
1686 if (TREE_CODE (max_value) == INTEGER_CST
1687 && tree_int_cst_compare (high, max_value) > 0)
1688 high = max_value;
1689 high = fold_convert (index_type, high);
1691 /* We may have folded a case range to a one-value case. */
1692 if (tree_int_cst_equal (low, high))
1693 high = NULL_TREE;
1697 CASE_LOW (elt) = low;
1698 CASE_HIGH (elt) = high;
1700 else
1702 gcc_assert (!default_case);
1703 default_case = elt;
1704 /* The default case must be passed separately to the
1705 gimple_build_switch routine. But if DEFAULT_CASEP
1706 is NULL, we do not remove the default case (it would
1707 be completely lost). */
1708 if (default_casep)
1709 remove_element = TRUE;
1712 if (remove_element)
1713 labels.ordered_remove (i);
1714 else
1715 i++;
1717 len = i;
1719 if (!labels.is_empty ())
1720 sort_case_labels (labels);
1722 if (default_casep && !default_case)
1724 /* If the switch has no default label, add one, so that we jump
1725 around the switch body. If the labels already cover the whole
1726 range of the switch index_type, add the default label pointing
1727 to one of the existing labels. */
1728 if (len
1729 && TYPE_MIN_VALUE (index_type)
1730 && TYPE_MAX_VALUE (index_type)
1731 && tree_int_cst_equal (CASE_LOW (labels[0]),
1732 TYPE_MIN_VALUE (index_type)))
1734 tree low, high = CASE_HIGH (labels[len - 1]);
1735 if (!high)
1736 high = CASE_LOW (labels[len - 1]);
1737 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1739 for (i = 1; i < len; i++)
1741 high = CASE_LOW (labels[i]);
1742 low = CASE_HIGH (labels[i - 1]);
1743 if (!low)
1744 low = CASE_LOW (labels[i - 1]);
1745 if ((TREE_INT_CST_LOW (low) + 1
1746 != TREE_INT_CST_LOW (high))
1747 || (TREE_INT_CST_HIGH (low)
1748 + (TREE_INT_CST_LOW (high) == 0)
1749 != TREE_INT_CST_HIGH (high)))
1750 break;
1752 if (i == len)
1754 tree label = CASE_LABEL (labels[0]);
1755 default_case = build_case_label (NULL_TREE, NULL_TREE,
1756 label);
1762 if (default_casep)
1763 *default_casep = default_case;
1766 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1767 branch to. */
1769 static enum gimplify_status
1770 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1772 tree switch_expr = *expr_p;
1773 gimple_seq switch_body_seq = NULL;
1774 enum gimplify_status ret;
1775 tree index_type = TREE_TYPE (switch_expr);
1776 if (index_type == NULL_TREE)
1777 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1779 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1780 fb_rvalue);
1781 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1782 return ret;
1784 if (SWITCH_BODY (switch_expr))
1786 vec<tree> labels;
1787 vec<tree> saved_labels;
1788 tree default_case = NULL_TREE;
1789 gimple gimple_switch;
1791 /* If someone can be bothered to fill in the labels, they can
1792 be bothered to null out the body too. */
1793 gcc_assert (!SWITCH_LABELS (switch_expr));
1795 /* Save old labels, get new ones from body, then restore the old
1796 labels. Save all the things from the switch body to append after. */
1797 saved_labels = gimplify_ctxp->case_labels;
1798 gimplify_ctxp->case_labels.create (8);
1800 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1801 labels = gimplify_ctxp->case_labels;
1802 gimplify_ctxp->case_labels = saved_labels;
1804 preprocess_case_label_vec_for_gimple (labels, index_type,
1805 &default_case);
1807 if (!default_case)
1809 gimple new_default;
1811 default_case
1812 = build_case_label (NULL_TREE, NULL_TREE,
1813 create_artificial_label (UNKNOWN_LOCATION));
1814 new_default = gimple_build_label (CASE_LABEL (default_case));
1815 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1818 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1819 default_case, labels);
1820 gimplify_seq_add_stmt (pre_p, gimple_switch);
1821 gimplify_seq_add_seq (pre_p, switch_body_seq);
1822 labels.release ();
1824 else
1825 gcc_assert (SWITCH_LABELS (switch_expr));
1827 return GS_ALL_DONE;
1830 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1832 static enum gimplify_status
1833 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1835 struct gimplify_ctx *ctxp;
1836 gimple gimple_label;
1838 /* Invalid OpenMP programs can play Duff's Device type games with
1839 #pragma omp parallel. At least in the C front end, we don't
1840 detect such invalid branches until after gimplification. */
1841 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1842 if (ctxp->case_labels.exists ())
1843 break;
1845 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1846 ctxp->case_labels.safe_push (*expr_p);
1847 gimplify_seq_add_stmt (pre_p, gimple_label);
1849 return GS_ALL_DONE;
1852 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1853 if necessary. */
1855 tree
1856 build_and_jump (tree *label_p)
1858 if (label_p == NULL)
1859 /* If there's nowhere to jump, just fall through. */
1860 return NULL_TREE;
1862 if (*label_p == NULL_TREE)
1864 tree label = create_artificial_label (UNKNOWN_LOCATION);
1865 *label_p = label;
1868 return build1 (GOTO_EXPR, void_type_node, *label_p);
1871 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1872 This also involves building a label to jump to and communicating it to
1873 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1875 static enum gimplify_status
1876 gimplify_exit_expr (tree *expr_p)
1878 tree cond = TREE_OPERAND (*expr_p, 0);
1879 tree expr;
1881 expr = build_and_jump (&gimplify_ctxp->exit_label);
1882 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1883 *expr_p = expr;
1885 return GS_OK;
1888 /* A helper function to be called via walk_tree. Mark all labels under *TP
1889 as being forced. To be called for DECL_INITIAL of static variables. */
1891 tree
1892 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1894 if (TYPE_P (*tp))
1895 *walk_subtrees = 0;
1896 if (TREE_CODE (*tp) == LABEL_DECL)
1897 FORCED_LABEL (*tp) = 1;
1899 return NULL_TREE;
1902 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1903 different from its canonical type, wrap the whole thing inside a
1904 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1905 type.
1907 The canonical type of a COMPONENT_REF is the type of the field being
1908 referenced--unless the field is a bit-field which can be read directly
1909 in a smaller mode, in which case the canonical type is the
1910 sign-appropriate type corresponding to that mode. */
1912 static void
1913 canonicalize_component_ref (tree *expr_p)
1915 tree expr = *expr_p;
1916 tree type;
1918 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1920 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1921 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1922 else
1923 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1925 /* One could argue that all the stuff below is not necessary for
1926 the non-bitfield case and declare it a FE error if type
1927 adjustment would be needed. */
1928 if (TREE_TYPE (expr) != type)
1930 #ifdef ENABLE_TYPES_CHECKING
1931 tree old_type = TREE_TYPE (expr);
1932 #endif
1933 int type_quals;
1935 /* We need to preserve qualifiers and propagate them from
1936 operand 0. */
1937 type_quals = TYPE_QUALS (type)
1938 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1939 if (TYPE_QUALS (type) != type_quals)
1940 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1942 /* Set the type of the COMPONENT_REF to the underlying type. */
1943 TREE_TYPE (expr) = type;
1945 #ifdef ENABLE_TYPES_CHECKING
1946 /* It is now a FE error, if the conversion from the canonical
1947 type to the original expression type is not useless. */
1948 gcc_assert (useless_type_conversion_p (old_type, type));
1949 #endif
1953 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1954 to foo, embed that change in the ADDR_EXPR by converting
1955 T array[U];
1956 (T *)&array
1958 &array[L]
1959 where L is the lower bound. For simplicity, only do this for constant
1960 lower bound.
1961 The constraint is that the type of &array[L] is trivially convertible
1962 to T *. */
1964 static void
1965 canonicalize_addr_expr (tree *expr_p)
1967 tree expr = *expr_p;
1968 tree addr_expr = TREE_OPERAND (expr, 0);
1969 tree datype, ddatype, pddatype;
1971 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1972 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1973 || TREE_CODE (addr_expr) != ADDR_EXPR)
1974 return;
1976 /* The addr_expr type should be a pointer to an array. */
1977 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1978 if (TREE_CODE (datype) != ARRAY_TYPE)
1979 return;
1981 /* The pointer to element type shall be trivially convertible to
1982 the expression pointer type. */
1983 ddatype = TREE_TYPE (datype);
1984 pddatype = build_pointer_type (ddatype);
1985 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1986 pddatype))
1987 return;
1989 /* The lower bound and element sizes must be constant. */
1990 if (!TYPE_SIZE_UNIT (ddatype)
1991 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1992 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1993 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1994 return;
1996 /* All checks succeeded. Build a new node to merge the cast. */
1997 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1998 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1999 NULL_TREE, NULL_TREE);
2000 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2002 /* We can have stripped a required restrict qualifier above. */
2003 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2004 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2007 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2008 underneath as appropriate. */
2010 static enum gimplify_status
2011 gimplify_conversion (tree *expr_p)
2013 location_t loc = EXPR_LOCATION (*expr_p);
2014 gcc_assert (CONVERT_EXPR_P (*expr_p));
2016 /* Then strip away all but the outermost conversion. */
2017 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2019 /* And remove the outermost conversion if it's useless. */
2020 if (tree_ssa_useless_type_conversion (*expr_p))
2021 *expr_p = TREE_OPERAND (*expr_p, 0);
2023 /* If we still have a conversion at the toplevel,
2024 then canonicalize some constructs. */
2025 if (CONVERT_EXPR_P (*expr_p))
2027 tree sub = TREE_OPERAND (*expr_p, 0);
2029 /* If a NOP conversion is changing the type of a COMPONENT_REF
2030 expression, then canonicalize its type now in order to expose more
2031 redundant conversions. */
2032 if (TREE_CODE (sub) == COMPONENT_REF)
2033 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2035 /* If a NOP conversion is changing a pointer to array of foo
2036 to a pointer to foo, embed that change in the ADDR_EXPR. */
2037 else if (TREE_CODE (sub) == ADDR_EXPR)
2038 canonicalize_addr_expr (expr_p);
2041 /* If we have a conversion to a non-register type force the
2042 use of a VIEW_CONVERT_EXPR instead. */
2043 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2044 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2045 TREE_OPERAND (*expr_p, 0));
2047 return GS_OK;
2050 /* Nonlocal VLAs seen in the current function. */
2051 static struct pointer_set_t *nonlocal_vlas;
2053 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2054 DECL_VALUE_EXPR, and it's worth re-examining things. */
2056 static enum gimplify_status
2057 gimplify_var_or_parm_decl (tree *expr_p)
2059 tree decl = *expr_p;
2061 /* ??? If this is a local variable, and it has not been seen in any
2062 outer BIND_EXPR, then it's probably the result of a duplicate
2063 declaration, for which we've already issued an error. It would
2064 be really nice if the front end wouldn't leak these at all.
2065 Currently the only known culprit is C++ destructors, as seen
2066 in g++.old-deja/g++.jason/binding.C. */
2067 if (TREE_CODE (decl) == VAR_DECL
2068 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2069 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2070 && decl_function_context (decl) == current_function_decl)
2072 gcc_assert (seen_error ());
2073 return GS_ERROR;
2076 /* When within an OpenMP context, notice uses of variables. */
2077 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2078 return GS_ALL_DONE;
2080 /* If the decl is an alias for another expression, substitute it now. */
2081 if (DECL_HAS_VALUE_EXPR_P (decl))
2083 tree value_expr = DECL_VALUE_EXPR (decl);
2085 /* For referenced nonlocal VLAs add a decl for debugging purposes
2086 to the current function. */
2087 if (TREE_CODE (decl) == VAR_DECL
2088 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2089 && nonlocal_vlas != NULL
2090 && TREE_CODE (value_expr) == INDIRECT_REF
2091 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2092 && decl_function_context (decl) != current_function_decl)
2094 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2095 while (ctx
2096 && (ctx->region_type == ORT_WORKSHARE
2097 || ctx->region_type == ORT_SIMD))
2098 ctx = ctx->outer_context;
2099 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2101 tree copy = copy_node (decl), block;
2103 lang_hooks.dup_lang_specific_decl (copy);
2104 SET_DECL_RTL (copy, 0);
2105 TREE_USED (copy) = 1;
2106 block = DECL_INITIAL (current_function_decl);
2107 DECL_CHAIN (copy) = BLOCK_VARS (block);
2108 BLOCK_VARS (block) = copy;
2109 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2110 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2114 *expr_p = unshare_expr (value_expr);
2115 return GS_OK;
2118 return GS_ALL_DONE;
2121 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2122 node *EXPR_P.
2124 compound_lval
2125 : min_lval '[' val ']'
2126 | min_lval '.' ID
2127 | compound_lval '[' val ']'
2128 | compound_lval '.' ID
2130 This is not part of the original SIMPLE definition, which separates
2131 array and member references, but it seems reasonable to handle them
2132 together. Also, this way we don't run into problems with union
2133 aliasing; gcc requires that for accesses through a union to alias, the
2134 union reference must be explicit, which was not always the case when we
2135 were splitting up array and member refs.
2137 PRE_P points to the sequence where side effects that must happen before
2138 *EXPR_P should be stored.
2140 POST_P points to the sequence where side effects that must happen after
2141 *EXPR_P should be stored. */
2143 static enum gimplify_status
2144 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2145 fallback_t fallback)
2147 tree *p;
2148 enum gimplify_status ret = GS_ALL_DONE, tret;
2149 int i;
2150 location_t loc = EXPR_LOCATION (*expr_p);
2151 tree expr = *expr_p;
2153 /* Create a stack of the subexpressions so later we can walk them in
2154 order from inner to outer. */
2155 stack_vec<tree, 10> expr_stack;
2157 /* We can handle anything that get_inner_reference can deal with. */
2158 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2160 restart:
2161 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2162 if (TREE_CODE (*p) == INDIRECT_REF)
2163 *p = fold_indirect_ref_loc (loc, *p);
2165 if (handled_component_p (*p))
2167 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2168 additional COMPONENT_REFs. */
2169 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2170 && gimplify_var_or_parm_decl (p) == GS_OK)
2171 goto restart;
2172 else
2173 break;
2175 expr_stack.safe_push (*p);
2178 gcc_assert (expr_stack.length ());
2180 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2181 walked through and P points to the innermost expression.
2183 Java requires that we elaborated nodes in source order. That
2184 means we must gimplify the inner expression followed by each of
2185 the indices, in order. But we can't gimplify the inner
2186 expression until we deal with any variable bounds, sizes, or
2187 positions in order to deal with PLACEHOLDER_EXPRs.
2189 So we do this in three steps. First we deal with the annotations
2190 for any variables in the components, then we gimplify the base,
2191 then we gimplify any indices, from left to right. */
2192 for (i = expr_stack.length () - 1; i >= 0; i--)
2194 tree t = expr_stack[i];
2196 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2198 /* Gimplify the low bound and element type size and put them into
2199 the ARRAY_REF. If these values are set, they have already been
2200 gimplified. */
2201 if (TREE_OPERAND (t, 2) == NULL_TREE)
2203 tree low = unshare_expr (array_ref_low_bound (t));
2204 if (!is_gimple_min_invariant (low))
2206 TREE_OPERAND (t, 2) = low;
2207 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2208 post_p, is_gimple_reg,
2209 fb_rvalue);
2210 ret = MIN (ret, tret);
2213 else
2215 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2216 is_gimple_reg, fb_rvalue);
2217 ret = MIN (ret, tret);
2220 if (TREE_OPERAND (t, 3) == NULL_TREE)
2222 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2223 tree elmt_size = unshare_expr (array_ref_element_size (t));
2224 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2226 /* Divide the element size by the alignment of the element
2227 type (above). */
2228 elmt_size
2229 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2231 if (!is_gimple_min_invariant (elmt_size))
2233 TREE_OPERAND (t, 3) = elmt_size;
2234 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2235 post_p, is_gimple_reg,
2236 fb_rvalue);
2237 ret = MIN (ret, tret);
2240 else
2242 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2243 is_gimple_reg, fb_rvalue);
2244 ret = MIN (ret, tret);
2247 else if (TREE_CODE (t) == COMPONENT_REF)
2249 /* Set the field offset into T and gimplify it. */
2250 if (TREE_OPERAND (t, 2) == NULL_TREE)
2252 tree offset = unshare_expr (component_ref_field_offset (t));
2253 tree field = TREE_OPERAND (t, 1);
2254 tree factor
2255 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2257 /* Divide the offset by its alignment. */
2258 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2260 if (!is_gimple_min_invariant (offset))
2262 TREE_OPERAND (t, 2) = offset;
2263 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2264 post_p, is_gimple_reg,
2265 fb_rvalue);
2266 ret = MIN (ret, tret);
2269 else
2271 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2272 is_gimple_reg, fb_rvalue);
2273 ret = MIN (ret, tret);
2278 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2279 so as to match the min_lval predicate. Failure to do so may result
2280 in the creation of large aggregate temporaries. */
2281 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2282 fallback | fb_lvalue);
2283 ret = MIN (ret, tret);
2285 /* And finally, the indices and operands of ARRAY_REF. During this
2286 loop we also remove any useless conversions. */
2287 for (; expr_stack.length () > 0; )
2289 tree t = expr_stack.pop ();
2291 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2293 /* Gimplify the dimension. */
2294 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2296 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2297 is_gimple_val, fb_rvalue);
2298 ret = MIN (ret, tret);
2302 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2304 /* The innermost expression P may have originally had
2305 TREE_SIDE_EFFECTS set which would have caused all the outer
2306 expressions in *EXPR_P leading to P to also have had
2307 TREE_SIDE_EFFECTS set. */
2308 recalculate_side_effects (t);
2311 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2312 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2314 canonicalize_component_ref (expr_p);
2317 expr_stack.release ();
2319 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2321 return ret;
2324 /* Gimplify the self modifying expression pointed to by EXPR_P
2325 (++, --, +=, -=).
2327 PRE_P points to the list where side effects that must happen before
2328 *EXPR_P should be stored.
2330 POST_P points to the list where side effects that must happen after
2331 *EXPR_P should be stored.
2333 WANT_VALUE is nonzero iff we want to use the value of this expression
2334 in another expression.
2336 ARITH_TYPE is the type the computation should be performed in. */
2338 enum gimplify_status
2339 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2340 bool want_value, tree arith_type)
2342 enum tree_code code;
2343 tree lhs, lvalue, rhs, t1;
2344 gimple_seq post = NULL, *orig_post_p = post_p;
2345 bool postfix;
2346 enum tree_code arith_code;
2347 enum gimplify_status ret;
2348 location_t loc = EXPR_LOCATION (*expr_p);
2350 code = TREE_CODE (*expr_p);
2352 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2353 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2355 /* Prefix or postfix? */
2356 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2357 /* Faster to treat as prefix if result is not used. */
2358 postfix = want_value;
2359 else
2360 postfix = false;
2362 /* For postfix, make sure the inner expression's post side effects
2363 are executed after side effects from this expression. */
2364 if (postfix)
2365 post_p = &post;
2367 /* Add or subtract? */
2368 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2369 arith_code = PLUS_EXPR;
2370 else
2371 arith_code = MINUS_EXPR;
2373 /* Gimplify the LHS into a GIMPLE lvalue. */
2374 lvalue = TREE_OPERAND (*expr_p, 0);
2375 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2376 if (ret == GS_ERROR)
2377 return ret;
2379 /* Extract the operands to the arithmetic operation. */
2380 lhs = lvalue;
2381 rhs = TREE_OPERAND (*expr_p, 1);
2383 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2384 that as the result value and in the postqueue operation. */
2385 if (postfix)
2387 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2388 if (ret == GS_ERROR)
2389 return ret;
2391 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2394 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2395 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2397 rhs = convert_to_ptrofftype_loc (loc, rhs);
2398 if (arith_code == MINUS_EXPR)
2399 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2400 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2402 else
2403 t1 = fold_convert (TREE_TYPE (*expr_p),
2404 fold_build2 (arith_code, arith_type,
2405 fold_convert (arith_type, lhs),
2406 fold_convert (arith_type, rhs)));
2408 if (postfix)
2410 gimplify_assign (lvalue, t1, pre_p);
2411 gimplify_seq_add_seq (orig_post_p, post);
2412 *expr_p = lhs;
2413 return GS_ALL_DONE;
2415 else
2417 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2418 return GS_OK;
2422 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2424 static void
2425 maybe_with_size_expr (tree *expr_p)
2427 tree expr = *expr_p;
2428 tree type = TREE_TYPE (expr);
2429 tree size;
2431 /* If we've already wrapped this or the type is error_mark_node, we can't do
2432 anything. */
2433 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2434 || type == error_mark_node)
2435 return;
2437 /* If the size isn't known or is a constant, we have nothing to do. */
2438 size = TYPE_SIZE_UNIT (type);
2439 if (!size || TREE_CODE (size) == INTEGER_CST)
2440 return;
2442 /* Otherwise, make a WITH_SIZE_EXPR. */
2443 size = unshare_expr (size);
2444 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2445 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2448 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2449 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2450 the CALL_EXPR. */
2452 static enum gimplify_status
2453 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2455 bool (*test) (tree);
2456 fallback_t fb;
2458 /* In general, we allow lvalues for function arguments to avoid
2459 extra overhead of copying large aggregates out of even larger
2460 aggregates into temporaries only to copy the temporaries to
2461 the argument list. Make optimizers happy by pulling out to
2462 temporaries those types that fit in registers. */
2463 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2464 test = is_gimple_val, fb = fb_rvalue;
2465 else
2467 test = is_gimple_lvalue, fb = fb_either;
2468 /* Also strip a TARGET_EXPR that would force an extra copy. */
2469 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2471 tree init = TARGET_EXPR_INITIAL (*arg_p);
2472 if (init
2473 && !VOID_TYPE_P (TREE_TYPE (init)))
2474 *arg_p = init;
2478 /* If this is a variable sized type, we must remember the size. */
2479 maybe_with_size_expr (arg_p);
2481 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2482 /* Make sure arguments have the same location as the function call
2483 itself. */
2484 protected_set_expr_location (*arg_p, call_location);
2486 /* There is a sequence point before a function call. Side effects in
2487 the argument list must occur before the actual call. So, when
2488 gimplifying arguments, force gimplify_expr to use an internal
2489 post queue which is then appended to the end of PRE_P. */
2490 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2493 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2494 WANT_VALUE is true if the result of the call is desired. */
2496 static enum gimplify_status
2497 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2499 tree fndecl, parms, p, fnptrtype;
2500 enum gimplify_status ret;
2501 int i, nargs;
2502 gimple call;
2503 bool builtin_va_start_p = FALSE;
2504 location_t loc = EXPR_LOCATION (*expr_p);
2506 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2508 /* For reliable diagnostics during inlining, it is necessary that
2509 every call_expr be annotated with file and line. */
2510 if (! EXPR_HAS_LOCATION (*expr_p))
2511 SET_EXPR_LOCATION (*expr_p, input_location);
2513 if (fn_contains_cilk_spawn_p (cfun)
2514 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
2515 && !seen_error ())
2516 return (enum gimplify_status)
2517 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, NULL);
2519 /* This may be a call to a builtin function.
2521 Builtin function calls may be transformed into different
2522 (and more efficient) builtin function calls under certain
2523 circumstances. Unfortunately, gimplification can muck things
2524 up enough that the builtin expanders are not aware that certain
2525 transformations are still valid.
2527 So we attempt transformation/gimplification of the call before
2528 we gimplify the CALL_EXPR. At this time we do not manage to
2529 transform all calls in the same manner as the expanders do, but
2530 we do transform most of them. */
2531 fndecl = get_callee_fndecl (*expr_p);
2532 if (fndecl
2533 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2534 switch (DECL_FUNCTION_CODE (fndecl))
2536 case BUILT_IN_VA_START:
2538 builtin_va_start_p = TRUE;
2539 if (call_expr_nargs (*expr_p) < 2)
2541 error ("too few arguments to function %<va_start%>");
2542 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2543 return GS_OK;
2546 if (fold_builtin_next_arg (*expr_p, true))
2548 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2549 return GS_OK;
2551 break;
2553 case BUILT_IN_LINE:
2555 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2556 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2557 return GS_OK;
2559 case BUILT_IN_FILE:
2561 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2562 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2563 return GS_OK;
2565 case BUILT_IN_FUNCTION:
2567 const char *function;
2568 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2569 *expr_p = build_string_literal (strlen (function) + 1, function);
2570 return GS_OK;
2572 default:
2575 if (fndecl && DECL_BUILT_IN (fndecl))
2577 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2578 if (new_tree && new_tree != *expr_p)
2580 /* There was a transformation of this call which computes the
2581 same value, but in a more efficient way. Return and try
2582 again. */
2583 *expr_p = new_tree;
2584 return GS_OK;
2588 /* Remember the original function pointer type. */
2589 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2591 /* There is a sequence point before the call, so any side effects in
2592 the calling expression must occur before the actual call. Force
2593 gimplify_expr to use an internal post queue. */
2594 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2595 is_gimple_call_addr, fb_rvalue);
2597 nargs = call_expr_nargs (*expr_p);
2599 /* Get argument types for verification. */
2600 fndecl = get_callee_fndecl (*expr_p);
2601 parms = NULL_TREE;
2602 if (fndecl)
2603 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2604 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2605 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2607 if (fndecl && DECL_ARGUMENTS (fndecl))
2608 p = DECL_ARGUMENTS (fndecl);
2609 else if (parms)
2610 p = parms;
2611 else
2612 p = NULL_TREE;
2613 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2616 /* If the last argument is __builtin_va_arg_pack () and it is not
2617 passed as a named argument, decrease the number of CALL_EXPR
2618 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2619 if (!p
2620 && i < nargs
2621 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2623 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2624 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2626 if (last_arg_fndecl
2627 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2628 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2629 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2631 tree call = *expr_p;
2633 --nargs;
2634 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2635 CALL_EXPR_FN (call),
2636 nargs, CALL_EXPR_ARGP (call));
2638 /* Copy all CALL_EXPR flags, location and block, except
2639 CALL_EXPR_VA_ARG_PACK flag. */
2640 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2641 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2642 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2643 = CALL_EXPR_RETURN_SLOT_OPT (call);
2644 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2645 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2647 /* Set CALL_EXPR_VA_ARG_PACK. */
2648 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2652 /* Finally, gimplify the function arguments. */
2653 if (nargs > 0)
2655 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2656 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2657 PUSH_ARGS_REVERSED ? i-- : i++)
2659 enum gimplify_status t;
2661 /* Avoid gimplifying the second argument to va_start, which needs to
2662 be the plain PARM_DECL. */
2663 if ((i != 1) || !builtin_va_start_p)
2665 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2666 EXPR_LOCATION (*expr_p));
2668 if (t == GS_ERROR)
2669 ret = GS_ERROR;
2674 /* Verify the function result. */
2675 if (want_value && fndecl
2676 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2678 error_at (loc, "using result of function returning %<void%>");
2679 ret = GS_ERROR;
2682 /* Try this again in case gimplification exposed something. */
2683 if (ret != GS_ERROR)
2685 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2687 if (new_tree && new_tree != *expr_p)
2689 /* There was a transformation of this call which computes the
2690 same value, but in a more efficient way. Return and try
2691 again. */
2692 *expr_p = new_tree;
2693 return GS_OK;
2696 else
2698 *expr_p = error_mark_node;
2699 return GS_ERROR;
2702 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2703 decl. This allows us to eliminate redundant or useless
2704 calls to "const" functions. */
2705 if (TREE_CODE (*expr_p) == CALL_EXPR)
2707 int flags = call_expr_flags (*expr_p);
2708 if (flags & (ECF_CONST | ECF_PURE)
2709 /* An infinite loop is considered a side effect. */
2710 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2711 TREE_SIDE_EFFECTS (*expr_p) = 0;
2714 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2715 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2716 form and delegate the creation of a GIMPLE_CALL to
2717 gimplify_modify_expr. This is always possible because when
2718 WANT_VALUE is true, the caller wants the result of this call into
2719 a temporary, which means that we will emit an INIT_EXPR in
2720 internal_get_tmp_var which will then be handled by
2721 gimplify_modify_expr. */
2722 if (!want_value)
2724 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2725 have to do is replicate it as a GIMPLE_CALL tuple. */
2726 gimple_stmt_iterator gsi;
2727 call = gimple_build_call_from_tree (*expr_p);
2728 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2729 notice_special_calls (call);
2730 gimplify_seq_add_stmt (pre_p, call);
2731 gsi = gsi_last (*pre_p);
2732 /* Don't fold stmts inside of target construct. We'll do it
2733 during omplower pass instead. */
2734 struct gimplify_omp_ctx *ctx;
2735 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2736 if (ctx->region_type == ORT_TARGET)
2737 break;
2738 if (ctx == NULL)
2739 fold_stmt (&gsi);
2740 *expr_p = NULL_TREE;
2742 else
2743 /* Remember the original function type. */
2744 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2745 CALL_EXPR_FN (*expr_p));
2747 return ret;
2750 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2751 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2753 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2754 condition is true or false, respectively. If null, we should generate
2755 our own to skip over the evaluation of this specific expression.
2757 LOCUS is the source location of the COND_EXPR.
2759 This function is the tree equivalent of do_jump.
2761 shortcut_cond_r should only be called by shortcut_cond_expr. */
2763 static tree
2764 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2765 location_t locus)
2767 tree local_label = NULL_TREE;
2768 tree t, expr = NULL;
2770 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2771 retain the shortcut semantics. Just insert the gotos here;
2772 shortcut_cond_expr will append the real blocks later. */
2773 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2775 location_t new_locus;
2777 /* Turn if (a && b) into
2779 if (a); else goto no;
2780 if (b) goto yes; else goto no;
2781 (no:) */
2783 if (false_label_p == NULL)
2784 false_label_p = &local_label;
2786 /* Keep the original source location on the first 'if'. */
2787 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2788 append_to_statement_list (t, &expr);
2790 /* Set the source location of the && on the second 'if'. */
2791 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2792 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2793 new_locus);
2794 append_to_statement_list (t, &expr);
2796 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2798 location_t new_locus;
2800 /* Turn if (a || b) into
2802 if (a) goto yes;
2803 if (b) goto yes; else goto no;
2804 (yes:) */
2806 if (true_label_p == NULL)
2807 true_label_p = &local_label;
2809 /* Keep the original source location on the first 'if'. */
2810 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2811 append_to_statement_list (t, &expr);
2813 /* Set the source location of the || on the second 'if'. */
2814 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2815 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2816 new_locus);
2817 append_to_statement_list (t, &expr);
2819 else if (TREE_CODE (pred) == COND_EXPR
2820 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2821 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2823 location_t new_locus;
2825 /* As long as we're messing with gotos, turn if (a ? b : c) into
2826 if (a)
2827 if (b) goto yes; else goto no;
2828 else
2829 if (c) goto yes; else goto no;
2831 Don't do this if one of the arms has void type, which can happen
2832 in C++ when the arm is throw. */
2834 /* Keep the original source location on the first 'if'. Set the source
2835 location of the ? on the second 'if'. */
2836 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2837 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2838 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2839 false_label_p, locus),
2840 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2841 false_label_p, new_locus));
2843 else
2845 expr = build3 (COND_EXPR, void_type_node, pred,
2846 build_and_jump (true_label_p),
2847 build_and_jump (false_label_p));
2848 SET_EXPR_LOCATION (expr, locus);
2851 if (local_label)
2853 t = build1 (LABEL_EXPR, void_type_node, local_label);
2854 append_to_statement_list (t, &expr);
2857 return expr;
2860 /* Given a conditional expression EXPR with short-circuit boolean
2861 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2862 predicate apart into the equivalent sequence of conditionals. */
2864 static tree
2865 shortcut_cond_expr (tree expr)
2867 tree pred = TREE_OPERAND (expr, 0);
2868 tree then_ = TREE_OPERAND (expr, 1);
2869 tree else_ = TREE_OPERAND (expr, 2);
2870 tree true_label, false_label, end_label, t;
2871 tree *true_label_p;
2872 tree *false_label_p;
2873 bool emit_end, emit_false, jump_over_else;
2874 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2875 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2877 /* First do simple transformations. */
2878 if (!else_se)
2880 /* If there is no 'else', turn
2881 if (a && b) then c
2882 into
2883 if (a) if (b) then c. */
2884 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2886 /* Keep the original source location on the first 'if'. */
2887 location_t locus = EXPR_LOC_OR_HERE (expr);
2888 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2889 /* Set the source location of the && on the second 'if'. */
2890 if (EXPR_HAS_LOCATION (pred))
2891 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2892 then_ = shortcut_cond_expr (expr);
2893 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2894 pred = TREE_OPERAND (pred, 0);
2895 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2896 SET_EXPR_LOCATION (expr, locus);
2900 if (!then_se)
2902 /* If there is no 'then', turn
2903 if (a || b); else d
2904 into
2905 if (a); else if (b); else d. */
2906 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2908 /* Keep the original source location on the first 'if'. */
2909 location_t locus = EXPR_LOC_OR_HERE (expr);
2910 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2911 /* Set the source location of the || on the second 'if'. */
2912 if (EXPR_HAS_LOCATION (pred))
2913 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2914 else_ = shortcut_cond_expr (expr);
2915 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2916 pred = TREE_OPERAND (pred, 0);
2917 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2918 SET_EXPR_LOCATION (expr, locus);
2922 /* If we're done, great. */
2923 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2924 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2925 return expr;
2927 /* Otherwise we need to mess with gotos. Change
2928 if (a) c; else d;
2930 if (a); else goto no;
2931 c; goto end;
2932 no: d; end:
2933 and recursively gimplify the condition. */
2935 true_label = false_label = end_label = NULL_TREE;
2937 /* If our arms just jump somewhere, hijack those labels so we don't
2938 generate jumps to jumps. */
2940 if (then_
2941 && TREE_CODE (then_) == GOTO_EXPR
2942 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2944 true_label = GOTO_DESTINATION (then_);
2945 then_ = NULL;
2946 then_se = false;
2949 if (else_
2950 && TREE_CODE (else_) == GOTO_EXPR
2951 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2953 false_label = GOTO_DESTINATION (else_);
2954 else_ = NULL;
2955 else_se = false;
2958 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2959 if (true_label)
2960 true_label_p = &true_label;
2961 else
2962 true_label_p = NULL;
2964 /* The 'else' branch also needs a label if it contains interesting code. */
2965 if (false_label || else_se)
2966 false_label_p = &false_label;
2967 else
2968 false_label_p = NULL;
2970 /* If there was nothing else in our arms, just forward the label(s). */
2971 if (!then_se && !else_se)
2972 return shortcut_cond_r (pred, true_label_p, false_label_p,
2973 EXPR_LOC_OR_HERE (expr));
2975 /* If our last subexpression already has a terminal label, reuse it. */
2976 if (else_se)
2977 t = expr_last (else_);
2978 else if (then_se)
2979 t = expr_last (then_);
2980 else
2981 t = NULL;
2982 if (t && TREE_CODE (t) == LABEL_EXPR)
2983 end_label = LABEL_EXPR_LABEL (t);
2985 /* If we don't care about jumping to the 'else' branch, jump to the end
2986 if the condition is false. */
2987 if (!false_label_p)
2988 false_label_p = &end_label;
2990 /* We only want to emit these labels if we aren't hijacking them. */
2991 emit_end = (end_label == NULL_TREE);
2992 emit_false = (false_label == NULL_TREE);
2994 /* We only emit the jump over the else clause if we have to--if the
2995 then clause may fall through. Otherwise we can wind up with a
2996 useless jump and a useless label at the end of gimplified code,
2997 which will cause us to think that this conditional as a whole
2998 falls through even if it doesn't. If we then inline a function
2999 which ends with such a condition, that can cause us to issue an
3000 inappropriate warning about control reaching the end of a
3001 non-void function. */
3002 jump_over_else = block_may_fallthru (then_);
3004 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3005 EXPR_LOC_OR_HERE (expr));
3007 expr = NULL;
3008 append_to_statement_list (pred, &expr);
3010 append_to_statement_list (then_, &expr);
3011 if (else_se)
3013 if (jump_over_else)
3015 tree last = expr_last (expr);
3016 t = build_and_jump (&end_label);
3017 if (EXPR_HAS_LOCATION (last))
3018 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3019 append_to_statement_list (t, &expr);
3021 if (emit_false)
3023 t = build1 (LABEL_EXPR, void_type_node, false_label);
3024 append_to_statement_list (t, &expr);
3026 append_to_statement_list (else_, &expr);
3028 if (emit_end && end_label)
3030 t = build1 (LABEL_EXPR, void_type_node, end_label);
3031 append_to_statement_list (t, &expr);
3034 return expr;
3037 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3039 tree
3040 gimple_boolify (tree expr)
3042 tree type = TREE_TYPE (expr);
3043 location_t loc = EXPR_LOCATION (expr);
3045 if (TREE_CODE (expr) == NE_EXPR
3046 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3047 && integer_zerop (TREE_OPERAND (expr, 1)))
3049 tree call = TREE_OPERAND (expr, 0);
3050 tree fn = get_callee_fndecl (call);
3052 /* For __builtin_expect ((long) (x), y) recurse into x as well
3053 if x is truth_value_p. */
3054 if (fn
3055 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3056 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3057 && call_expr_nargs (call) == 2)
3059 tree arg = CALL_EXPR_ARG (call, 0);
3060 if (arg)
3062 if (TREE_CODE (arg) == NOP_EXPR
3063 && TREE_TYPE (arg) == TREE_TYPE (call))
3064 arg = TREE_OPERAND (arg, 0);
3065 if (truth_value_p (TREE_CODE (arg)))
3067 arg = gimple_boolify (arg);
3068 CALL_EXPR_ARG (call, 0)
3069 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3075 switch (TREE_CODE (expr))
3077 case TRUTH_AND_EXPR:
3078 case TRUTH_OR_EXPR:
3079 case TRUTH_XOR_EXPR:
3080 case TRUTH_ANDIF_EXPR:
3081 case TRUTH_ORIF_EXPR:
3082 /* Also boolify the arguments of truth exprs. */
3083 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3084 /* FALLTHRU */
3086 case TRUTH_NOT_EXPR:
3087 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3089 /* These expressions always produce boolean results. */
3090 if (TREE_CODE (type) != BOOLEAN_TYPE)
3091 TREE_TYPE (expr) = boolean_type_node;
3092 return expr;
3094 case ANNOTATE_EXPR:
3095 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
3096 == annot_expr_ivdep_kind)
3098 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3099 if (TREE_CODE (type) != BOOLEAN_TYPE)
3100 TREE_TYPE (expr) = boolean_type_node;
3101 return expr;
3103 /* FALLTHRU */
3105 default:
3106 if (COMPARISON_CLASS_P (expr))
3108 /* There expressions always prduce boolean results. */
3109 if (TREE_CODE (type) != BOOLEAN_TYPE)
3110 TREE_TYPE (expr) = boolean_type_node;
3111 return expr;
3113 /* Other expressions that get here must have boolean values, but
3114 might need to be converted to the appropriate mode. */
3115 if (TREE_CODE (type) == BOOLEAN_TYPE)
3116 return expr;
3117 return fold_convert_loc (loc, boolean_type_node, expr);
3121 /* Given a conditional expression *EXPR_P without side effects, gimplify
3122 its operands. New statements are inserted to PRE_P. */
3124 static enum gimplify_status
3125 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3127 tree expr = *expr_p, cond;
3128 enum gimplify_status ret, tret;
3129 enum tree_code code;
3131 cond = gimple_boolify (COND_EXPR_COND (expr));
3133 /* We need to handle && and || specially, as their gimplification
3134 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3135 code = TREE_CODE (cond);
3136 if (code == TRUTH_ANDIF_EXPR)
3137 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3138 else if (code == TRUTH_ORIF_EXPR)
3139 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3140 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3141 COND_EXPR_COND (*expr_p) = cond;
3143 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3144 is_gimple_val, fb_rvalue);
3145 ret = MIN (ret, tret);
3146 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3147 is_gimple_val, fb_rvalue);
3149 return MIN (ret, tret);
3152 /* Return true if evaluating EXPR could trap.
3153 EXPR is GENERIC, while tree_could_trap_p can be called
3154 only on GIMPLE. */
3156 static bool
3157 generic_expr_could_trap_p (tree expr)
3159 unsigned i, n;
3161 if (!expr || is_gimple_val (expr))
3162 return false;
3164 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3165 return true;
3167 n = TREE_OPERAND_LENGTH (expr);
3168 for (i = 0; i < n; i++)
3169 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3170 return true;
3172 return false;
3175 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3176 into
3178 if (p) if (p)
3179 t1 = a; a;
3180 else or else
3181 t1 = b; b;
3184 The second form is used when *EXPR_P is of type void.
3186 PRE_P points to the list where side effects that must happen before
3187 *EXPR_P should be stored. */
3189 static enum gimplify_status
3190 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3192 tree expr = *expr_p;
3193 tree type = TREE_TYPE (expr);
3194 location_t loc = EXPR_LOCATION (expr);
3195 tree tmp, arm1, arm2;
3196 enum gimplify_status ret;
3197 tree label_true, label_false, label_cont;
3198 bool have_then_clause_p, have_else_clause_p;
3199 gimple gimple_cond;
3200 enum tree_code pred_code;
3201 gimple_seq seq = NULL;
3203 /* If this COND_EXPR has a value, copy the values into a temporary within
3204 the arms. */
3205 if (!VOID_TYPE_P (type))
3207 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3208 tree result;
3210 /* If either an rvalue is ok or we do not require an lvalue, create the
3211 temporary. But we cannot do that if the type is addressable. */
3212 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3213 && !TREE_ADDRESSABLE (type))
3215 if (gimplify_ctxp->allow_rhs_cond_expr
3216 /* If either branch has side effects or could trap, it can't be
3217 evaluated unconditionally. */
3218 && !TREE_SIDE_EFFECTS (then_)
3219 && !generic_expr_could_trap_p (then_)
3220 && !TREE_SIDE_EFFECTS (else_)
3221 && !generic_expr_could_trap_p (else_))
3222 return gimplify_pure_cond_expr (expr_p, pre_p);
3224 tmp = create_tmp_var (type, "iftmp");
3225 result = tmp;
3228 /* Otherwise, only create and copy references to the values. */
3229 else
3231 type = build_pointer_type (type);
3233 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3234 then_ = build_fold_addr_expr_loc (loc, then_);
3236 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3237 else_ = build_fold_addr_expr_loc (loc, else_);
3239 expr
3240 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3242 tmp = create_tmp_var (type, "iftmp");
3243 result = build_simple_mem_ref_loc (loc, tmp);
3246 /* Build the new then clause, `tmp = then_;'. But don't build the
3247 assignment if the value is void; in C++ it can be if it's a throw. */
3248 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3249 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3251 /* Similarly, build the new else clause, `tmp = else_;'. */
3252 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3253 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3255 TREE_TYPE (expr) = void_type_node;
3256 recalculate_side_effects (expr);
3258 /* Move the COND_EXPR to the prequeue. */
3259 gimplify_stmt (&expr, pre_p);
3261 *expr_p = result;
3262 return GS_ALL_DONE;
3265 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3266 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3267 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3268 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3270 /* Make sure the condition has BOOLEAN_TYPE. */
3271 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3273 /* Break apart && and || conditions. */
3274 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3275 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3277 expr = shortcut_cond_expr (expr);
3279 if (expr != *expr_p)
3281 *expr_p = expr;
3283 /* We can't rely on gimplify_expr to re-gimplify the expanded
3284 form properly, as cleanups might cause the target labels to be
3285 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3286 set up a conditional context. */
3287 gimple_push_condition ();
3288 gimplify_stmt (expr_p, &seq);
3289 gimple_pop_condition (pre_p);
3290 gimple_seq_add_seq (pre_p, seq);
3292 return GS_ALL_DONE;
3296 /* Now do the normal gimplification. */
3298 /* Gimplify condition. */
3299 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3300 fb_rvalue);
3301 if (ret == GS_ERROR)
3302 return GS_ERROR;
3303 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3305 gimple_push_condition ();
3307 have_then_clause_p = have_else_clause_p = false;
3308 if (TREE_OPERAND (expr, 1) != NULL
3309 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3310 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3311 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3312 == current_function_decl)
3313 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3314 have different locations, otherwise we end up with incorrect
3315 location information on the branches. */
3316 && (optimize
3317 || !EXPR_HAS_LOCATION (expr)
3318 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3319 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3321 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3322 have_then_clause_p = true;
3324 else
3325 label_true = create_artificial_label (UNKNOWN_LOCATION);
3326 if (TREE_OPERAND (expr, 2) != NULL
3327 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3328 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3329 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3330 == current_function_decl)
3331 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3332 have different locations, otherwise we end up with incorrect
3333 location information on the branches. */
3334 && (optimize
3335 || !EXPR_HAS_LOCATION (expr)
3336 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3337 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3339 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3340 have_else_clause_p = true;
3342 else
3343 label_false = create_artificial_label (UNKNOWN_LOCATION);
3345 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3346 &arm2);
3348 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3349 label_false);
3351 gimplify_seq_add_stmt (&seq, gimple_cond);
3352 label_cont = NULL_TREE;
3353 if (!have_then_clause_p)
3355 /* For if (...) {} else { code; } put label_true after
3356 the else block. */
3357 if (TREE_OPERAND (expr, 1) == NULL_TREE
3358 && !have_else_clause_p
3359 && TREE_OPERAND (expr, 2) != NULL_TREE)
3360 label_cont = label_true;
3361 else
3363 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3364 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3365 /* For if (...) { code; } else {} or
3366 if (...) { code; } else goto label; or
3367 if (...) { code; return; } else { ... }
3368 label_cont isn't needed. */
3369 if (!have_else_clause_p
3370 && TREE_OPERAND (expr, 2) != NULL_TREE
3371 && gimple_seq_may_fallthru (seq))
3373 gimple g;
3374 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3376 g = gimple_build_goto (label_cont);
3378 /* GIMPLE_COND's are very low level; they have embedded
3379 gotos. This particular embedded goto should not be marked
3380 with the location of the original COND_EXPR, as it would
3381 correspond to the COND_EXPR's condition, not the ELSE or the
3382 THEN arms. To avoid marking it with the wrong location, flag
3383 it as "no location". */
3384 gimple_set_do_not_emit_location (g);
3386 gimplify_seq_add_stmt (&seq, g);
3390 if (!have_else_clause_p)
3392 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3393 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3395 if (label_cont)
3396 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3398 gimple_pop_condition (pre_p);
3399 gimple_seq_add_seq (pre_p, seq);
3401 if (ret == GS_ERROR)
3402 ; /* Do nothing. */
3403 else if (have_then_clause_p || have_else_clause_p)
3404 ret = GS_ALL_DONE;
3405 else
3407 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3408 expr = TREE_OPERAND (expr, 0);
3409 gimplify_stmt (&expr, pre_p);
3412 *expr_p = NULL;
3413 return ret;
3416 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3417 to be marked addressable.
3419 We cannot rely on such an expression being directly markable if a temporary
3420 has been created by the gimplification. In this case, we create another
3421 temporary and initialize it with a copy, which will become a store after we
3422 mark it addressable. This can happen if the front-end passed us something
3423 that it could not mark addressable yet, like a Fortran pass-by-reference
3424 parameter (int) floatvar. */
3426 static void
3427 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3429 while (handled_component_p (*expr_p))
3430 expr_p = &TREE_OPERAND (*expr_p, 0);
3431 if (is_gimple_reg (*expr_p))
3432 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3435 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3436 a call to __builtin_memcpy. */
3438 static enum gimplify_status
3439 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3440 gimple_seq *seq_p)
3442 tree t, to, to_ptr, from, from_ptr;
3443 gimple gs;
3444 location_t loc = EXPR_LOCATION (*expr_p);
3446 to = TREE_OPERAND (*expr_p, 0);
3447 from = TREE_OPERAND (*expr_p, 1);
3449 /* Mark the RHS addressable. Beware that it may not be possible to do so
3450 directly if a temporary has been created by the gimplification. */
3451 prepare_gimple_addressable (&from, seq_p);
3453 mark_addressable (from);
3454 from_ptr = build_fold_addr_expr_loc (loc, from);
3455 gimplify_arg (&from_ptr, seq_p, loc);
3457 mark_addressable (to);
3458 to_ptr = build_fold_addr_expr_loc (loc, to);
3459 gimplify_arg (&to_ptr, seq_p, loc);
3461 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3463 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3465 if (want_value)
3467 /* tmp = memcpy() */
3468 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3469 gimple_call_set_lhs (gs, t);
3470 gimplify_seq_add_stmt (seq_p, gs);
3472 *expr_p = build_simple_mem_ref (t);
3473 return GS_ALL_DONE;
3476 gimplify_seq_add_stmt (seq_p, gs);
3477 *expr_p = NULL;
3478 return GS_ALL_DONE;
3481 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3482 a call to __builtin_memset. In this case we know that the RHS is
3483 a CONSTRUCTOR with an empty element list. */
3485 static enum gimplify_status
3486 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3487 gimple_seq *seq_p)
3489 tree t, from, to, to_ptr;
3490 gimple gs;
3491 location_t loc = EXPR_LOCATION (*expr_p);
3493 /* Assert our assumptions, to abort instead of producing wrong code
3494 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3495 not be immediately exposed. */
3496 from = TREE_OPERAND (*expr_p, 1);
3497 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3498 from = TREE_OPERAND (from, 0);
3500 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3501 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3503 /* Now proceed. */
3504 to = TREE_OPERAND (*expr_p, 0);
3506 to_ptr = build_fold_addr_expr_loc (loc, to);
3507 gimplify_arg (&to_ptr, seq_p, loc);
3508 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3510 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3512 if (want_value)
3514 /* tmp = memset() */
3515 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3516 gimple_call_set_lhs (gs, t);
3517 gimplify_seq_add_stmt (seq_p, gs);
3519 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3520 return GS_ALL_DONE;
3523 gimplify_seq_add_stmt (seq_p, gs);
3524 *expr_p = NULL;
3525 return GS_ALL_DONE;
3528 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3529 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3530 assignment. Return non-null if we detect a potential overlap. */
3532 struct gimplify_init_ctor_preeval_data
3534 /* The base decl of the lhs object. May be NULL, in which case we
3535 have to assume the lhs is indirect. */
3536 tree lhs_base_decl;
3538 /* The alias set of the lhs object. */
3539 alias_set_type lhs_alias_set;
3542 static tree
3543 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3545 struct gimplify_init_ctor_preeval_data *data
3546 = (struct gimplify_init_ctor_preeval_data *) xdata;
3547 tree t = *tp;
3549 /* If we find the base object, obviously we have overlap. */
3550 if (data->lhs_base_decl == t)
3551 return t;
3553 /* If the constructor component is indirect, determine if we have a
3554 potential overlap with the lhs. The only bits of information we
3555 have to go on at this point are addressability and alias sets. */
3556 if ((INDIRECT_REF_P (t)
3557 || TREE_CODE (t) == MEM_REF)
3558 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3559 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3560 return t;
3562 /* If the constructor component is a call, determine if it can hide a
3563 potential overlap with the lhs through an INDIRECT_REF like above.
3564 ??? Ugh - this is completely broken. In fact this whole analysis
3565 doesn't look conservative. */
3566 if (TREE_CODE (t) == CALL_EXPR)
3568 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3570 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3571 if (POINTER_TYPE_P (TREE_VALUE (type))
3572 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3573 && alias_sets_conflict_p (data->lhs_alias_set,
3574 get_alias_set
3575 (TREE_TYPE (TREE_VALUE (type)))))
3576 return t;
3579 if (IS_TYPE_OR_DECL_P (t))
3580 *walk_subtrees = 0;
3581 return NULL;
3584 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3585 force values that overlap with the lhs (as described by *DATA)
3586 into temporaries. */
3588 static void
3589 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3590 struct gimplify_init_ctor_preeval_data *data)
3592 enum gimplify_status one;
3594 /* If the value is constant, then there's nothing to pre-evaluate. */
3595 if (TREE_CONSTANT (*expr_p))
3597 /* Ensure it does not have side effects, it might contain a reference to
3598 the object we're initializing. */
3599 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3600 return;
3603 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3604 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3605 return;
3607 /* Recurse for nested constructors. */
3608 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3610 unsigned HOST_WIDE_INT ix;
3611 constructor_elt *ce;
3612 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3614 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3615 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3617 return;
3620 /* If this is a variable sized type, we must remember the size. */
3621 maybe_with_size_expr (expr_p);
3623 /* Gimplify the constructor element to something appropriate for the rhs
3624 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3625 the gimplifier will consider this a store to memory. Doing this
3626 gimplification now means that we won't have to deal with complicated
3627 language-specific trees, nor trees like SAVE_EXPR that can induce
3628 exponential search behavior. */
3629 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3630 if (one == GS_ERROR)
3632 *expr_p = NULL;
3633 return;
3636 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3637 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3638 always be true for all scalars, since is_gimple_mem_rhs insists on a
3639 temporary variable for them. */
3640 if (DECL_P (*expr_p))
3641 return;
3643 /* If this is of variable size, we have no choice but to assume it doesn't
3644 overlap since we can't make a temporary for it. */
3645 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3646 return;
3648 /* Otherwise, we must search for overlap ... */
3649 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3650 return;
3652 /* ... and if found, force the value into a temporary. */
3653 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3656 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3657 a RANGE_EXPR in a CONSTRUCTOR for an array.
3659 var = lower;
3660 loop_entry:
3661 object[var] = value;
3662 if (var == upper)
3663 goto loop_exit;
3664 var = var + 1;
3665 goto loop_entry;
3666 loop_exit:
3668 We increment var _after_ the loop exit check because we might otherwise
3669 fail if upper == TYPE_MAX_VALUE (type for upper).
3671 Note that we never have to deal with SAVE_EXPRs here, because this has
3672 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3674 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3675 gimple_seq *, bool);
3677 static void
3678 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3679 tree value, tree array_elt_type,
3680 gimple_seq *pre_p, bool cleared)
3682 tree loop_entry_label, loop_exit_label, fall_thru_label;
3683 tree var, var_type, cref, tmp;
3685 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3686 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3687 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3689 /* Create and initialize the index variable. */
3690 var_type = TREE_TYPE (upper);
3691 var = create_tmp_var (var_type, NULL);
3692 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3694 /* Add the loop entry label. */
3695 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3697 /* Build the reference. */
3698 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3699 var, NULL_TREE, NULL_TREE);
3701 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3702 the store. Otherwise just assign value to the reference. */
3704 if (TREE_CODE (value) == CONSTRUCTOR)
3705 /* NB we might have to call ourself recursively through
3706 gimplify_init_ctor_eval if the value is a constructor. */
3707 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3708 pre_p, cleared);
3709 else
3710 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3712 /* We exit the loop when the index var is equal to the upper bound. */
3713 gimplify_seq_add_stmt (pre_p,
3714 gimple_build_cond (EQ_EXPR, var, upper,
3715 loop_exit_label, fall_thru_label));
3717 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3719 /* Otherwise, increment the index var... */
3720 tmp = build2 (PLUS_EXPR, var_type, var,
3721 fold_convert (var_type, integer_one_node));
3722 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3724 /* ...and jump back to the loop entry. */
3725 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3727 /* Add the loop exit label. */
3728 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3731 /* Return true if FDECL is accessing a field that is zero sized. */
3733 static bool
3734 zero_sized_field_decl (const_tree fdecl)
3736 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3737 && integer_zerop (DECL_SIZE (fdecl)))
3738 return true;
3739 return false;
3742 /* Return true if TYPE is zero sized. */
3744 static bool
3745 zero_sized_type (const_tree type)
3747 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3748 && integer_zerop (TYPE_SIZE (type)))
3749 return true;
3750 return false;
3753 /* A subroutine of gimplify_init_constructor. Generate individual
3754 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3755 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3756 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3757 zeroed first. */
3759 static void
3760 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3761 gimple_seq *pre_p, bool cleared)
3763 tree array_elt_type = NULL;
3764 unsigned HOST_WIDE_INT ix;
3765 tree purpose, value;
3767 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3768 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3770 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3772 tree cref;
3774 /* NULL values are created above for gimplification errors. */
3775 if (value == NULL)
3776 continue;
3778 if (cleared && initializer_zerop (value))
3779 continue;
3781 /* ??? Here's to hoping the front end fills in all of the indices,
3782 so we don't have to figure out what's missing ourselves. */
3783 gcc_assert (purpose);
3785 /* Skip zero-sized fields, unless value has side-effects. This can
3786 happen with calls to functions returning a zero-sized type, which
3787 we shouldn't discard. As a number of downstream passes don't
3788 expect sets of zero-sized fields, we rely on the gimplification of
3789 the MODIFY_EXPR we make below to drop the assignment statement. */
3790 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3791 continue;
3793 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3794 whole range. */
3795 if (TREE_CODE (purpose) == RANGE_EXPR)
3797 tree lower = TREE_OPERAND (purpose, 0);
3798 tree upper = TREE_OPERAND (purpose, 1);
3800 /* If the lower bound is equal to upper, just treat it as if
3801 upper was the index. */
3802 if (simple_cst_equal (lower, upper))
3803 purpose = upper;
3804 else
3806 gimplify_init_ctor_eval_range (object, lower, upper, value,
3807 array_elt_type, pre_p, cleared);
3808 continue;
3812 if (array_elt_type)
3814 /* Do not use bitsizetype for ARRAY_REF indices. */
3815 if (TYPE_DOMAIN (TREE_TYPE (object)))
3816 purpose
3817 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3818 purpose);
3819 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3820 purpose, NULL_TREE, NULL_TREE);
3822 else
3824 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3825 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3826 unshare_expr (object), purpose, NULL_TREE);
3829 if (TREE_CODE (value) == CONSTRUCTOR
3830 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3831 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3832 pre_p, cleared);
3833 else
3835 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3836 gimplify_and_add (init, pre_p);
3837 ggc_free (init);
3842 /* Return the appropriate RHS predicate for this LHS. */
3844 gimple_predicate
3845 rhs_predicate_for (tree lhs)
3847 if (is_gimple_reg (lhs))
3848 return is_gimple_reg_rhs_or_call;
3849 else
3850 return is_gimple_mem_rhs_or_call;
3853 /* Gimplify a C99 compound literal expression. This just means adding
3854 the DECL_EXPR before the current statement and using its anonymous
3855 decl instead. */
3857 static enum gimplify_status
3858 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3859 bool (*gimple_test_f) (tree),
3860 fallback_t fallback)
3862 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3863 tree decl = DECL_EXPR_DECL (decl_s);
3864 tree init = DECL_INITIAL (decl);
3865 /* Mark the decl as addressable if the compound literal
3866 expression is addressable now, otherwise it is marked too late
3867 after we gimplify the initialization expression. */
3868 if (TREE_ADDRESSABLE (*expr_p))
3869 TREE_ADDRESSABLE (decl) = 1;
3870 /* Otherwise, if we don't need an lvalue and have a literal directly
3871 substitute it. Check if it matches the gimple predicate, as
3872 otherwise we'd generate a new temporary, and we can as well just
3873 use the decl we already have. */
3874 else if (!TREE_ADDRESSABLE (decl)
3875 && init
3876 && (fallback & fb_lvalue) == 0
3877 && gimple_test_f (init))
3879 *expr_p = init;
3880 return GS_OK;
3883 /* Preliminarily mark non-addressed complex variables as eligible
3884 for promotion to gimple registers. We'll transform their uses
3885 as we find them. */
3886 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3887 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3888 && !TREE_THIS_VOLATILE (decl)
3889 && !needs_to_live_in_memory (decl))
3890 DECL_GIMPLE_REG_P (decl) = 1;
3892 /* If the decl is not addressable, then it is being used in some
3893 expression or on the right hand side of a statement, and it can
3894 be put into a readonly data section. */
3895 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3896 TREE_READONLY (decl) = 1;
3898 /* This decl isn't mentioned in the enclosing block, so add it to the
3899 list of temps. FIXME it seems a bit of a kludge to say that
3900 anonymous artificial vars aren't pushed, but everything else is. */
3901 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3902 gimple_add_tmp_var (decl);
3904 gimplify_and_add (decl_s, pre_p);
3905 *expr_p = decl;
3906 return GS_OK;
3909 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3910 return a new CONSTRUCTOR if something changed. */
3912 static tree
3913 optimize_compound_literals_in_ctor (tree orig_ctor)
3915 tree ctor = orig_ctor;
3916 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3917 unsigned int idx, num = vec_safe_length (elts);
3919 for (idx = 0; idx < num; idx++)
3921 tree value = (*elts)[idx].value;
3922 tree newval = value;
3923 if (TREE_CODE (value) == CONSTRUCTOR)
3924 newval = optimize_compound_literals_in_ctor (value);
3925 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3927 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3928 tree decl = DECL_EXPR_DECL (decl_s);
3929 tree init = DECL_INITIAL (decl);
3931 if (!TREE_ADDRESSABLE (value)
3932 && !TREE_ADDRESSABLE (decl)
3933 && init
3934 && TREE_CODE (init) == CONSTRUCTOR)
3935 newval = optimize_compound_literals_in_ctor (init);
3937 if (newval == value)
3938 continue;
3940 if (ctor == orig_ctor)
3942 ctor = copy_node (orig_ctor);
3943 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3944 elts = CONSTRUCTOR_ELTS (ctor);
3946 (*elts)[idx].value = newval;
3948 return ctor;
3951 /* A subroutine of gimplify_modify_expr. Break out elements of a
3952 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3954 Note that we still need to clear any elements that don't have explicit
3955 initializers, so if not all elements are initialized we keep the
3956 original MODIFY_EXPR, we just remove all of the constructor elements.
3958 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3959 GS_ERROR if we would have to create a temporary when gimplifying
3960 this constructor. Otherwise, return GS_OK.
3962 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3964 static enum gimplify_status
3965 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3966 bool want_value, bool notify_temp_creation)
3968 tree object, ctor, type;
3969 enum gimplify_status ret;
3970 vec<constructor_elt, va_gc> *elts;
3972 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3974 if (!notify_temp_creation)
3976 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3977 is_gimple_lvalue, fb_lvalue);
3978 if (ret == GS_ERROR)
3979 return ret;
3982 object = TREE_OPERAND (*expr_p, 0);
3983 ctor = TREE_OPERAND (*expr_p, 1) =
3984 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3985 type = TREE_TYPE (ctor);
3986 elts = CONSTRUCTOR_ELTS (ctor);
3987 ret = GS_ALL_DONE;
3989 switch (TREE_CODE (type))
3991 case RECORD_TYPE:
3992 case UNION_TYPE:
3993 case QUAL_UNION_TYPE:
3994 case ARRAY_TYPE:
3996 struct gimplify_init_ctor_preeval_data preeval_data;
3997 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3998 bool cleared, complete_p, valid_const_initializer;
4000 /* Aggregate types must lower constructors to initialization of
4001 individual elements. The exception is that a CONSTRUCTOR node
4002 with no elements indicates zero-initialization of the whole. */
4003 if (vec_safe_is_empty (elts))
4005 if (notify_temp_creation)
4006 return GS_OK;
4007 break;
4010 /* Fetch information about the constructor to direct later processing.
4011 We might want to make static versions of it in various cases, and
4012 can only do so if it known to be a valid constant initializer. */
4013 valid_const_initializer
4014 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4015 &num_ctor_elements, &complete_p);
4017 /* If a const aggregate variable is being initialized, then it
4018 should never be a lose to promote the variable to be static. */
4019 if (valid_const_initializer
4020 && num_nonzero_elements > 1
4021 && TREE_READONLY (object)
4022 && TREE_CODE (object) == VAR_DECL
4023 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4025 if (notify_temp_creation)
4026 return GS_ERROR;
4027 DECL_INITIAL (object) = ctor;
4028 TREE_STATIC (object) = 1;
4029 if (!DECL_NAME (object))
4030 DECL_NAME (object) = create_tmp_var_name ("C");
4031 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4033 /* ??? C++ doesn't automatically append a .<number> to the
4034 assembler name, and even when it does, it looks at FE private
4035 data structures to figure out what that number should be,
4036 which are not set for this variable. I suppose this is
4037 important for local statics for inline functions, which aren't
4038 "local" in the object file sense. So in order to get a unique
4039 TU-local symbol, we must invoke the lhd version now. */
4040 lhd_set_decl_assembler_name (object);
4042 *expr_p = NULL_TREE;
4043 break;
4046 /* If there are "lots" of initialized elements, even discounting
4047 those that are not address constants (and thus *must* be
4048 computed at runtime), then partition the constructor into
4049 constant and non-constant parts. Block copy the constant
4050 parts in, then generate code for the non-constant parts. */
4051 /* TODO. There's code in cp/typeck.c to do this. */
4053 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4054 /* store_constructor will ignore the clearing of variable-sized
4055 objects. Initializers for such objects must explicitly set
4056 every field that needs to be set. */
4057 cleared = false;
4058 else if (!complete_p)
4059 /* If the constructor isn't complete, clear the whole object
4060 beforehand.
4062 ??? This ought not to be needed. For any element not present
4063 in the initializer, we should simply set them to zero. Except
4064 we'd need to *find* the elements that are not present, and that
4065 requires trickery to avoid quadratic compile-time behavior in
4066 large cases or excessive memory use in small cases. */
4067 cleared = true;
4068 else if (num_ctor_elements - num_nonzero_elements
4069 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4070 && num_nonzero_elements < num_ctor_elements / 4)
4071 /* If there are "lots" of zeros, it's more efficient to clear
4072 the memory and then set the nonzero elements. */
4073 cleared = true;
4074 else
4075 cleared = false;
4077 /* If there are "lots" of initialized elements, and all of them
4078 are valid address constants, then the entire initializer can
4079 be dropped to memory, and then memcpy'd out. Don't do this
4080 for sparse arrays, though, as it's more efficient to follow
4081 the standard CONSTRUCTOR behavior of memset followed by
4082 individual element initialization. Also don't do this for small
4083 all-zero initializers (which aren't big enough to merit
4084 clearing), and don't try to make bitwise copies of
4085 TREE_ADDRESSABLE types.
4087 We cannot apply such transformation when compiling chkp static
4088 initializer because creation of initializer image in the memory
4089 will require static initialization of bounds for it. It should
4090 result in another gimplification of similar initializer and we
4091 may fall into infinite loop. */
4092 if (valid_const_initializer
4093 && !(cleared || num_nonzero_elements == 0)
4094 && !TREE_ADDRESSABLE (type)
4095 && (!current_function_decl
4096 || !lookup_attribute ("chkp ctor",
4097 DECL_ATTRIBUTES (current_function_decl))))
4099 HOST_WIDE_INT size = int_size_in_bytes (type);
4100 unsigned int align;
4102 /* ??? We can still get unbounded array types, at least
4103 from the C++ front end. This seems wrong, but attempt
4104 to work around it for now. */
4105 if (size < 0)
4107 size = int_size_in_bytes (TREE_TYPE (object));
4108 if (size >= 0)
4109 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4112 /* Find the maximum alignment we can assume for the object. */
4113 /* ??? Make use of DECL_OFFSET_ALIGN. */
4114 if (DECL_P (object))
4115 align = DECL_ALIGN (object);
4116 else
4117 align = TYPE_ALIGN (type);
4119 /* Do a block move either if the size is so small as to make
4120 each individual move a sub-unit move on average, or if it
4121 is so large as to make individual moves inefficient. */
4122 if (size > 0
4123 && num_nonzero_elements > 1
4124 && (size < num_nonzero_elements
4125 || !can_move_by_pieces (size, align)))
4127 if (notify_temp_creation)
4128 return GS_ERROR;
4130 walk_tree (&ctor, force_labels_r, NULL, NULL);
4131 ctor = tree_output_constant_def (ctor);
4132 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4133 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4134 TREE_OPERAND (*expr_p, 1) = ctor;
4136 /* This is no longer an assignment of a CONSTRUCTOR, but
4137 we still may have processing to do on the LHS. So
4138 pretend we didn't do anything here to let that happen. */
4139 return GS_UNHANDLED;
4143 /* If the target is volatile, we have non-zero elements and more than
4144 one field to assign, initialize the target from a temporary. */
4145 if (TREE_THIS_VOLATILE (object)
4146 && !TREE_ADDRESSABLE (type)
4147 && num_nonzero_elements > 0
4148 && vec_safe_length (elts) > 1)
4150 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4151 TREE_OPERAND (*expr_p, 0) = temp;
4152 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4153 *expr_p,
4154 build2 (MODIFY_EXPR, void_type_node,
4155 object, temp));
4156 return GS_OK;
4159 if (notify_temp_creation)
4160 return GS_OK;
4162 /* If there are nonzero elements and if needed, pre-evaluate to capture
4163 elements overlapping with the lhs into temporaries. We must do this
4164 before clearing to fetch the values before they are zeroed-out. */
4165 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4167 preeval_data.lhs_base_decl = get_base_address (object);
4168 if (!DECL_P (preeval_data.lhs_base_decl))
4169 preeval_data.lhs_base_decl = NULL;
4170 preeval_data.lhs_alias_set = get_alias_set (object);
4172 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4173 pre_p, post_p, &preeval_data);
4176 if (cleared)
4178 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4179 Note that we still have to gimplify, in order to handle the
4180 case of variable sized types. Avoid shared tree structures. */
4181 CONSTRUCTOR_ELTS (ctor) = NULL;
4182 TREE_SIDE_EFFECTS (ctor) = 0;
4183 object = unshare_expr (object);
4184 gimplify_stmt (expr_p, pre_p);
4187 /* If we have not block cleared the object, or if there are nonzero
4188 elements in the constructor, add assignments to the individual
4189 scalar fields of the object. */
4190 if (!cleared || num_nonzero_elements > 0)
4191 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4193 *expr_p = NULL_TREE;
4195 break;
4197 case COMPLEX_TYPE:
4199 tree r, i;
4201 if (notify_temp_creation)
4202 return GS_OK;
4204 /* Extract the real and imaginary parts out of the ctor. */
4205 gcc_assert (elts->length () == 2);
4206 r = (*elts)[0].value;
4207 i = (*elts)[1].value;
4208 if (r == NULL || i == NULL)
4210 tree zero = build_zero_cst (TREE_TYPE (type));
4211 if (r == NULL)
4212 r = zero;
4213 if (i == NULL)
4214 i = zero;
4217 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4218 represent creation of a complex value. */
4219 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4221 ctor = build_complex (type, r, i);
4222 TREE_OPERAND (*expr_p, 1) = ctor;
4224 else
4226 ctor = build2 (COMPLEX_EXPR, type, r, i);
4227 TREE_OPERAND (*expr_p, 1) = ctor;
4228 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4229 pre_p,
4230 post_p,
4231 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4232 fb_rvalue);
4235 break;
4237 case VECTOR_TYPE:
4239 unsigned HOST_WIDE_INT ix;
4240 constructor_elt *ce;
4242 if (notify_temp_creation)
4243 return GS_OK;
4245 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4246 if (TREE_CONSTANT (ctor))
4248 bool constant_p = true;
4249 tree value;
4251 /* Even when ctor is constant, it might contain non-*_CST
4252 elements, such as addresses or trapping values like
4253 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4254 in VECTOR_CST nodes. */
4255 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4256 if (!CONSTANT_CLASS_P (value))
4258 constant_p = false;
4259 break;
4262 if (constant_p)
4264 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4265 break;
4268 /* Don't reduce an initializer constant even if we can't
4269 make a VECTOR_CST. It won't do anything for us, and it'll
4270 prevent us from representing it as a single constant. */
4271 if (initializer_constant_valid_p (ctor, type))
4272 break;
4274 TREE_CONSTANT (ctor) = 0;
4277 /* Vector types use CONSTRUCTOR all the way through gimple
4278 compilation as a general initializer. */
4279 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4281 enum gimplify_status tret;
4282 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4283 fb_rvalue);
4284 if (tret == GS_ERROR)
4285 ret = GS_ERROR;
4287 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4288 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4290 break;
4292 default:
4293 /* So how did we get a CONSTRUCTOR for a scalar type? */
4294 gcc_unreachable ();
4297 if (ret == GS_ERROR)
4298 return GS_ERROR;
4299 else if (want_value)
4301 *expr_p = object;
4302 return GS_OK;
4304 else
4306 /* If we have gimplified both sides of the initializer but have
4307 not emitted an assignment, do so now. */
4308 if (*expr_p)
4310 tree lhs = TREE_OPERAND (*expr_p, 0);
4311 tree rhs = TREE_OPERAND (*expr_p, 1);
4312 gimple init = gimple_build_assign (lhs, rhs);
4313 gimplify_seq_add_stmt (pre_p, init);
4314 *expr_p = NULL;
4317 return GS_ALL_DONE;
4321 /* Given a pointer value OP0, return a simplified version of an
4322 indirection through OP0, or NULL_TREE if no simplification is
4323 possible. This may only be applied to a rhs of an expression.
4324 Note that the resulting type may be different from the type pointed
4325 to in the sense that it is still compatible from the langhooks
4326 point of view. */
4328 static tree
4329 gimple_fold_indirect_ref_rhs (tree t)
4331 return gimple_fold_indirect_ref (t);
4334 /* Subroutine of gimplify_modify_expr to do simplifications of
4335 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4336 something changes. */
4338 static enum gimplify_status
4339 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4340 gimple_seq *pre_p, gimple_seq *post_p,
4341 bool want_value)
4343 enum gimplify_status ret = GS_UNHANDLED;
4344 bool changed;
4348 changed = false;
4349 switch (TREE_CODE (*from_p))
4351 case VAR_DECL:
4352 /* If we're assigning from a read-only variable initialized with
4353 a constructor, do the direct assignment from the constructor,
4354 but only if neither source nor target are volatile since this
4355 latter assignment might end up being done on a per-field basis. */
4356 if (DECL_INITIAL (*from_p)
4357 && TREE_READONLY (*from_p)
4358 && !TREE_THIS_VOLATILE (*from_p)
4359 && !TREE_THIS_VOLATILE (*to_p)
4360 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4362 tree old_from = *from_p;
4363 enum gimplify_status subret;
4365 /* Move the constructor into the RHS. */
4366 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4368 /* Let's see if gimplify_init_constructor will need to put
4369 it in memory. */
4370 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4371 false, true);
4372 if (subret == GS_ERROR)
4374 /* If so, revert the change. */
4375 *from_p = old_from;
4377 else
4379 ret = GS_OK;
4380 changed = true;
4383 break;
4384 case INDIRECT_REF:
4386 /* If we have code like
4388 *(const A*)(A*)&x
4390 where the type of "x" is a (possibly cv-qualified variant
4391 of "A"), treat the entire expression as identical to "x".
4392 This kind of code arises in C++ when an object is bound
4393 to a const reference, and if "x" is a TARGET_EXPR we want
4394 to take advantage of the optimization below. */
4395 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4396 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4397 if (t)
4399 if (TREE_THIS_VOLATILE (t) != volatile_p)
4401 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4402 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4403 build_fold_addr_expr (t));
4404 if (REFERENCE_CLASS_P (t))
4405 TREE_THIS_VOLATILE (t) = volatile_p;
4407 *from_p = t;
4408 ret = GS_OK;
4409 changed = true;
4411 break;
4414 case TARGET_EXPR:
4416 /* If we are initializing something from a TARGET_EXPR, strip the
4417 TARGET_EXPR and initialize it directly, if possible. This can't
4418 be done if the initializer is void, since that implies that the
4419 temporary is set in some non-trivial way.
4421 ??? What about code that pulls out the temp and uses it
4422 elsewhere? I think that such code never uses the TARGET_EXPR as
4423 an initializer. If I'm wrong, we'll die because the temp won't
4424 have any RTL. In that case, I guess we'll need to replace
4425 references somehow. */
4426 tree init = TARGET_EXPR_INITIAL (*from_p);
4428 if (init
4429 && !VOID_TYPE_P (TREE_TYPE (init)))
4431 *from_p = init;
4432 ret = GS_OK;
4433 changed = true;
4436 break;
4438 case COMPOUND_EXPR:
4439 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4440 caught. */
4441 gimplify_compound_expr (from_p, pre_p, true);
4442 ret = GS_OK;
4443 changed = true;
4444 break;
4446 case CONSTRUCTOR:
4447 /* If we already made some changes, let the front end have a
4448 crack at this before we break it down. */
4449 if (ret != GS_UNHANDLED)
4450 break;
4451 /* If we're initializing from a CONSTRUCTOR, break this into
4452 individual MODIFY_EXPRs. */
4453 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4454 false);
4456 case COND_EXPR:
4457 /* If we're assigning to a non-register type, push the assignment
4458 down into the branches. This is mandatory for ADDRESSABLE types,
4459 since we cannot generate temporaries for such, but it saves a
4460 copy in other cases as well. */
4461 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4463 /* This code should mirror the code in gimplify_cond_expr. */
4464 enum tree_code code = TREE_CODE (*expr_p);
4465 tree cond = *from_p;
4466 tree result = *to_p;
4468 ret = gimplify_expr (&result, pre_p, post_p,
4469 is_gimple_lvalue, fb_lvalue);
4470 if (ret != GS_ERROR)
4471 ret = GS_OK;
4473 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4474 TREE_OPERAND (cond, 1)
4475 = build2 (code, void_type_node, result,
4476 TREE_OPERAND (cond, 1));
4477 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4478 TREE_OPERAND (cond, 2)
4479 = build2 (code, void_type_node, unshare_expr (result),
4480 TREE_OPERAND (cond, 2));
4482 TREE_TYPE (cond) = void_type_node;
4483 recalculate_side_effects (cond);
4485 if (want_value)
4487 gimplify_and_add (cond, pre_p);
4488 *expr_p = unshare_expr (result);
4490 else
4491 *expr_p = cond;
4492 return ret;
4494 break;
4496 case CALL_EXPR:
4497 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4498 return slot so that we don't generate a temporary. */
4499 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4500 && aggregate_value_p (*from_p, *from_p))
4502 bool use_target;
4504 if (!(rhs_predicate_for (*to_p))(*from_p))
4505 /* If we need a temporary, *to_p isn't accurate. */
4506 use_target = false;
4507 /* It's OK to use the return slot directly unless it's an NRV. */
4508 else if (TREE_CODE (*to_p) == RESULT_DECL
4509 && DECL_NAME (*to_p) == NULL_TREE
4510 && needs_to_live_in_memory (*to_p))
4511 use_target = true;
4512 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4513 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4514 /* Don't force regs into memory. */
4515 use_target = false;
4516 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4517 /* It's OK to use the target directly if it's being
4518 initialized. */
4519 use_target = true;
4520 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4521 /* Always use the target and thus RSO for variable-sized types.
4522 GIMPLE cannot deal with a variable-sized assignment
4523 embedded in a call statement. */
4524 use_target = true;
4525 else if (TREE_CODE (*to_p) != SSA_NAME
4526 && (!is_gimple_variable (*to_p)
4527 || needs_to_live_in_memory (*to_p)))
4528 /* Don't use the original target if it's already addressable;
4529 if its address escapes, and the called function uses the
4530 NRV optimization, a conforming program could see *to_p
4531 change before the called function returns; see c++/19317.
4532 When optimizing, the return_slot pass marks more functions
4533 as safe after we have escape info. */
4534 use_target = false;
4535 else
4536 use_target = true;
4538 if (use_target)
4540 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4541 mark_addressable (*to_p);
4544 break;
4546 case WITH_SIZE_EXPR:
4547 /* Likewise for calls that return an aggregate of non-constant size,
4548 since we would not be able to generate a temporary at all. */
4549 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4551 *from_p = TREE_OPERAND (*from_p, 0);
4552 /* We don't change ret in this case because the
4553 WITH_SIZE_EXPR might have been added in
4554 gimplify_modify_expr, so returning GS_OK would lead to an
4555 infinite loop. */
4556 changed = true;
4558 break;
4560 /* If we're initializing from a container, push the initialization
4561 inside it. */
4562 case CLEANUP_POINT_EXPR:
4563 case BIND_EXPR:
4564 case STATEMENT_LIST:
4566 tree wrap = *from_p;
4567 tree t;
4569 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4570 fb_lvalue);
4571 if (ret != GS_ERROR)
4572 ret = GS_OK;
4574 t = voidify_wrapper_expr (wrap, *expr_p);
4575 gcc_assert (t == *expr_p);
4577 if (want_value)
4579 gimplify_and_add (wrap, pre_p);
4580 *expr_p = unshare_expr (*to_p);
4582 else
4583 *expr_p = wrap;
4584 return GS_OK;
4587 case COMPOUND_LITERAL_EXPR:
4589 tree complit = TREE_OPERAND (*expr_p, 1);
4590 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4591 tree decl = DECL_EXPR_DECL (decl_s);
4592 tree init = DECL_INITIAL (decl);
4594 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4595 into struct T x = { 0, 1, 2 } if the address of the
4596 compound literal has never been taken. */
4597 if (!TREE_ADDRESSABLE (complit)
4598 && !TREE_ADDRESSABLE (decl)
4599 && init)
4601 *expr_p = copy_node (*expr_p);
4602 TREE_OPERAND (*expr_p, 1) = init;
4603 return GS_OK;
4607 default:
4608 break;
4611 while (changed);
4613 return ret;
4617 /* Return true if T looks like a valid GIMPLE statement. */
4619 static bool
4620 is_gimple_stmt (tree t)
4622 const enum tree_code code = TREE_CODE (t);
4624 switch (code)
4626 case NOP_EXPR:
4627 /* The only valid NOP_EXPR is the empty statement. */
4628 return IS_EMPTY_STMT (t);
4630 case BIND_EXPR:
4631 case COND_EXPR:
4632 /* These are only valid if they're void. */
4633 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4635 case SWITCH_EXPR:
4636 case GOTO_EXPR:
4637 case RETURN_EXPR:
4638 case LABEL_EXPR:
4639 case CASE_LABEL_EXPR:
4640 case TRY_CATCH_EXPR:
4641 case TRY_FINALLY_EXPR:
4642 case EH_FILTER_EXPR:
4643 case CATCH_EXPR:
4644 case ASM_EXPR:
4645 case STATEMENT_LIST:
4646 case OMP_PARALLEL:
4647 case OMP_FOR:
4648 case OMP_SIMD:
4649 case OMP_DISTRIBUTE:
4650 case OMP_SECTIONS:
4651 case OMP_SECTION:
4652 case OMP_SINGLE:
4653 case OMP_MASTER:
4654 case OMP_TASKGROUP:
4655 case OMP_ORDERED:
4656 case OMP_CRITICAL:
4657 case OMP_TASK:
4658 /* These are always void. */
4659 return true;
4661 case CALL_EXPR:
4662 case MODIFY_EXPR:
4663 case PREDICT_EXPR:
4664 /* These are valid regardless of their type. */
4665 return true;
4667 default:
4668 return false;
4673 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4674 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4675 DECL_GIMPLE_REG_P set.
4677 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4678 other, unmodified part of the complex object just before the total store.
4679 As a consequence, if the object is still uninitialized, an undefined value
4680 will be loaded into a register, which may result in a spurious exception
4681 if the register is floating-point and the value happens to be a signaling
4682 NaN for example. Then the fully-fledged complex operations lowering pass
4683 followed by a DCE pass are necessary in order to fix things up. */
4685 static enum gimplify_status
4686 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4687 bool want_value)
4689 enum tree_code code, ocode;
4690 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4692 lhs = TREE_OPERAND (*expr_p, 0);
4693 rhs = TREE_OPERAND (*expr_p, 1);
4694 code = TREE_CODE (lhs);
4695 lhs = TREE_OPERAND (lhs, 0);
4697 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4698 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4699 TREE_NO_WARNING (other) = 1;
4700 other = get_formal_tmp_var (other, pre_p);
4702 realpart = code == REALPART_EXPR ? rhs : other;
4703 imagpart = code == REALPART_EXPR ? other : rhs;
4705 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4706 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4707 else
4708 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4710 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4711 *expr_p = (want_value) ? rhs : NULL_TREE;
4713 return GS_ALL_DONE;
4716 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4718 modify_expr
4719 : varname '=' rhs
4720 | '*' ID '=' rhs
4722 PRE_P points to the list where side effects that must happen before
4723 *EXPR_P should be stored.
4725 POST_P points to the list where side effects that must happen after
4726 *EXPR_P should be stored.
4728 WANT_VALUE is nonzero iff we want to use the value of this expression
4729 in another expression. */
4731 static enum gimplify_status
4732 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4733 bool want_value)
4735 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4736 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4737 enum gimplify_status ret = GS_UNHANDLED;
4738 gimple assign;
4739 location_t loc = EXPR_LOCATION (*expr_p);
4740 gimple_stmt_iterator gsi;
4742 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4743 || TREE_CODE (*expr_p) == INIT_EXPR);
4745 if (fn_contains_cilk_spawn_p (cfun)
4746 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
4747 && !seen_error ())
4748 return (enum gimplify_status)
4749 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, post_p);
4751 /* Trying to simplify a clobber using normal logic doesn't work,
4752 so handle it here. */
4753 if (TREE_CLOBBER_P (*from_p))
4755 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4756 if (ret == GS_ERROR)
4757 return ret;
4758 gcc_assert (!want_value
4759 && (TREE_CODE (*to_p) == VAR_DECL
4760 || TREE_CODE (*to_p) == MEM_REF));
4761 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4762 *expr_p = NULL;
4763 return GS_ALL_DONE;
4766 /* Insert pointer conversions required by the middle-end that are not
4767 required by the frontend. This fixes middle-end type checking for
4768 for example gcc.dg/redecl-6.c. */
4769 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4771 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4772 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4773 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4776 /* See if any simplifications can be done based on what the RHS is. */
4777 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4778 want_value);
4779 if (ret != GS_UNHANDLED)
4780 return ret;
4782 /* For zero sized types only gimplify the left hand side and right hand
4783 side as statements and throw away the assignment. Do this after
4784 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4785 types properly. */
4786 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4788 gimplify_stmt (from_p, pre_p);
4789 gimplify_stmt (to_p, pre_p);
4790 *expr_p = NULL_TREE;
4791 return GS_ALL_DONE;
4794 /* If the value being copied is of variable width, compute the length
4795 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4796 before gimplifying any of the operands so that we can resolve any
4797 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4798 the size of the expression to be copied, not of the destination, so
4799 that is what we must do here. */
4800 maybe_with_size_expr (from_p);
4802 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4803 if (ret == GS_ERROR)
4804 return ret;
4806 /* As a special case, we have to temporarily allow for assignments
4807 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4808 a toplevel statement, when gimplifying the GENERIC expression
4809 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4810 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4812 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4813 prevent gimplify_expr from trying to create a new temporary for
4814 foo's LHS, we tell it that it should only gimplify until it
4815 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4816 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4817 and all we need to do here is set 'a' to be its LHS. */
4818 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4819 fb_rvalue);
4820 if (ret == GS_ERROR)
4821 return ret;
4823 /* Now see if the above changed *from_p to something we handle specially. */
4824 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4825 want_value);
4826 if (ret != GS_UNHANDLED)
4827 return ret;
4829 /* If we've got a variable sized assignment between two lvalues (i.e. does
4830 not involve a call), then we can make things a bit more straightforward
4831 by converting the assignment to memcpy or memset. */
4832 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4834 tree from = TREE_OPERAND (*from_p, 0);
4835 tree size = TREE_OPERAND (*from_p, 1);
4837 if (TREE_CODE (from) == CONSTRUCTOR)
4838 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4840 if (is_gimple_addressable (from))
4842 *from_p = from;
4843 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4844 pre_p);
4848 /* Transform partial stores to non-addressable complex variables into
4849 total stores. This allows us to use real instead of virtual operands
4850 for these variables, which improves optimization. */
4851 if ((TREE_CODE (*to_p) == REALPART_EXPR
4852 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4853 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4854 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4856 /* Try to alleviate the effects of the gimplification creating artificial
4857 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4858 if (!gimplify_ctxp->into_ssa
4859 && TREE_CODE (*from_p) == VAR_DECL
4860 && DECL_IGNORED_P (*from_p)
4861 && DECL_P (*to_p)
4862 && !DECL_IGNORED_P (*to_p))
4864 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4865 DECL_NAME (*from_p)
4866 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4867 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4868 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4871 if (want_value && TREE_THIS_VOLATILE (*to_p))
4872 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4874 if (TREE_CODE (*from_p) == CALL_EXPR)
4876 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4877 instead of a GIMPLE_ASSIGN. */
4878 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4879 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4880 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4881 assign = gimple_build_call_from_tree (*from_p);
4882 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4883 notice_special_calls (assign);
4884 if (!gimple_call_noreturn_p (assign))
4885 gimple_call_set_lhs (assign, *to_p);
4887 else
4889 assign = gimple_build_assign (*to_p, *from_p);
4890 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4893 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4895 /* We should have got an SSA name from the start. */
4896 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4899 gimplify_seq_add_stmt (pre_p, assign);
4900 gsi = gsi_last (*pre_p);
4901 /* Don't fold stmts inside of target construct. We'll do it
4902 during omplower pass instead. */
4903 struct gimplify_omp_ctx *ctx;
4904 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
4905 if (ctx->region_type == ORT_TARGET)
4906 break;
4907 if (ctx == NULL)
4908 fold_stmt (&gsi);
4910 if (want_value)
4912 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4913 return GS_OK;
4915 else
4916 *expr_p = NULL;
4918 return GS_ALL_DONE;
4921 /* Gimplify a comparison between two variable-sized objects. Do this
4922 with a call to BUILT_IN_MEMCMP. */
4924 static enum gimplify_status
4925 gimplify_variable_sized_compare (tree *expr_p)
4927 location_t loc = EXPR_LOCATION (*expr_p);
4928 tree op0 = TREE_OPERAND (*expr_p, 0);
4929 tree op1 = TREE_OPERAND (*expr_p, 1);
4930 tree t, arg, dest, src, expr;
4932 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4933 arg = unshare_expr (arg);
4934 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4935 src = build_fold_addr_expr_loc (loc, op1);
4936 dest = build_fold_addr_expr_loc (loc, op0);
4937 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4938 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4940 expr
4941 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4942 SET_EXPR_LOCATION (expr, loc);
4943 *expr_p = expr;
4945 return GS_OK;
4948 /* Gimplify a comparison between two aggregate objects of integral scalar
4949 mode as a comparison between the bitwise equivalent scalar values. */
4951 static enum gimplify_status
4952 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4954 location_t loc = EXPR_LOCATION (*expr_p);
4955 tree op0 = TREE_OPERAND (*expr_p, 0);
4956 tree op1 = TREE_OPERAND (*expr_p, 1);
4958 tree type = TREE_TYPE (op0);
4959 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4961 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4962 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4964 *expr_p
4965 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4967 return GS_OK;
4970 /* Gimplify an expression sequence. This function gimplifies each
4971 expression and rewrites the original expression with the last
4972 expression of the sequence in GIMPLE form.
4974 PRE_P points to the list where the side effects for all the
4975 expressions in the sequence will be emitted.
4977 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4979 static enum gimplify_status
4980 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4982 tree t = *expr_p;
4986 tree *sub_p = &TREE_OPERAND (t, 0);
4988 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4989 gimplify_compound_expr (sub_p, pre_p, false);
4990 else
4991 gimplify_stmt (sub_p, pre_p);
4993 t = TREE_OPERAND (t, 1);
4995 while (TREE_CODE (t) == COMPOUND_EXPR);
4997 *expr_p = t;
4998 if (want_value)
4999 return GS_OK;
5000 else
5002 gimplify_stmt (expr_p, pre_p);
5003 return GS_ALL_DONE;
5007 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5008 gimplify. After gimplification, EXPR_P will point to a new temporary
5009 that holds the original value of the SAVE_EXPR node.
5011 PRE_P points to the list where side effects that must happen before
5012 *EXPR_P should be stored. */
5014 static enum gimplify_status
5015 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5017 enum gimplify_status ret = GS_ALL_DONE;
5018 tree val;
5020 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5021 val = TREE_OPERAND (*expr_p, 0);
5023 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5024 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5026 /* The operand may be a void-valued expression such as SAVE_EXPRs
5027 generated by the Java frontend for class initialization. It is
5028 being executed only for its side-effects. */
5029 if (TREE_TYPE (val) == void_type_node)
5031 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5032 is_gimple_stmt, fb_none);
5033 val = NULL;
5035 else
5036 val = get_initialized_tmp_var (val, pre_p, post_p);
5038 TREE_OPERAND (*expr_p, 0) = val;
5039 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5042 *expr_p = val;
5044 return ret;
5047 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5049 unary_expr
5050 : ...
5051 | '&' varname
5054 PRE_P points to the list where side effects that must happen before
5055 *EXPR_P should be stored.
5057 POST_P points to the list where side effects that must happen after
5058 *EXPR_P should be stored. */
5060 static enum gimplify_status
5061 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5063 tree expr = *expr_p;
5064 tree op0 = TREE_OPERAND (expr, 0);
5065 enum gimplify_status ret;
5066 location_t loc = EXPR_LOCATION (*expr_p);
5068 switch (TREE_CODE (op0))
5070 case INDIRECT_REF:
5071 do_indirect_ref:
5072 /* Check if we are dealing with an expression of the form '&*ptr'.
5073 While the front end folds away '&*ptr' into 'ptr', these
5074 expressions may be generated internally by the compiler (e.g.,
5075 builtins like __builtin_va_end). */
5076 /* Caution: the silent array decomposition semantics we allow for
5077 ADDR_EXPR means we can't always discard the pair. */
5078 /* Gimplification of the ADDR_EXPR operand may drop
5079 cv-qualification conversions, so make sure we add them if
5080 needed. */
5082 tree op00 = TREE_OPERAND (op0, 0);
5083 tree t_expr = TREE_TYPE (expr);
5084 tree t_op00 = TREE_TYPE (op00);
5086 if (!useless_type_conversion_p (t_expr, t_op00))
5087 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5088 *expr_p = op00;
5089 ret = GS_OK;
5091 break;
5093 case VIEW_CONVERT_EXPR:
5094 /* Take the address of our operand and then convert it to the type of
5095 this ADDR_EXPR.
5097 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5098 all clear. The impact of this transformation is even less clear. */
5100 /* If the operand is a useless conversion, look through it. Doing so
5101 guarantees that the ADDR_EXPR and its operand will remain of the
5102 same type. */
5103 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5104 op0 = TREE_OPERAND (op0, 0);
5106 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5107 build_fold_addr_expr_loc (loc,
5108 TREE_OPERAND (op0, 0)));
5109 ret = GS_OK;
5110 break;
5112 default:
5113 /* We use fb_either here because the C frontend sometimes takes
5114 the address of a call that returns a struct; see
5115 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5116 the implied temporary explicit. */
5118 /* Make the operand addressable. */
5119 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5120 is_gimple_addressable, fb_either);
5121 if (ret == GS_ERROR)
5122 break;
5124 /* Then mark it. Beware that it may not be possible to do so directly
5125 if a temporary has been created by the gimplification. */
5126 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5128 op0 = TREE_OPERAND (expr, 0);
5130 /* For various reasons, the gimplification of the expression
5131 may have made a new INDIRECT_REF. */
5132 if (TREE_CODE (op0) == INDIRECT_REF)
5133 goto do_indirect_ref;
5135 mark_addressable (TREE_OPERAND (expr, 0));
5137 /* The FEs may end up building ADDR_EXPRs early on a decl with
5138 an incomplete type. Re-build ADDR_EXPRs in canonical form
5139 here. */
5140 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5141 *expr_p = build_fold_addr_expr (op0);
5143 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5144 recompute_tree_invariant_for_addr_expr (*expr_p);
5146 /* If we re-built the ADDR_EXPR add a conversion to the original type
5147 if required. */
5148 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5149 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5151 break;
5154 return ret;
5157 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5158 value; output operands should be a gimple lvalue. */
5160 static enum gimplify_status
5161 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5163 tree expr;
5164 int noutputs;
5165 const char **oconstraints;
5166 int i;
5167 tree link;
5168 const char *constraint;
5169 bool allows_mem, allows_reg, is_inout;
5170 enum gimplify_status ret, tret;
5171 gimple stmt;
5172 vec<tree, va_gc> *inputs;
5173 vec<tree, va_gc> *outputs;
5174 vec<tree, va_gc> *clobbers;
5175 vec<tree, va_gc> *labels;
5176 tree link_next;
5178 expr = *expr_p;
5179 noutputs = list_length (ASM_OUTPUTS (expr));
5180 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5182 inputs = NULL;
5183 outputs = NULL;
5184 clobbers = NULL;
5185 labels = NULL;
5187 ret = GS_ALL_DONE;
5188 link_next = NULL_TREE;
5189 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5191 bool ok;
5192 size_t constraint_len;
5194 link_next = TREE_CHAIN (link);
5196 oconstraints[i]
5197 = constraint
5198 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5199 constraint_len = strlen (constraint);
5200 if (constraint_len == 0)
5201 continue;
5203 ok = parse_output_constraint (&constraint, i, 0, 0,
5204 &allows_mem, &allows_reg, &is_inout);
5205 if (!ok)
5207 ret = GS_ERROR;
5208 is_inout = false;
5211 if (!allows_reg && allows_mem)
5212 mark_addressable (TREE_VALUE (link));
5214 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5215 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5216 fb_lvalue | fb_mayfail);
5217 if (tret == GS_ERROR)
5219 error ("invalid lvalue in asm output %d", i);
5220 ret = tret;
5223 vec_safe_push (outputs, link);
5224 TREE_CHAIN (link) = NULL_TREE;
5226 if (is_inout)
5228 /* An input/output operand. To give the optimizers more
5229 flexibility, split it into separate input and output
5230 operands. */
5231 tree input;
5232 char buf[10];
5234 /* Turn the in/out constraint into an output constraint. */
5235 char *p = xstrdup (constraint);
5236 p[0] = '=';
5237 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5239 /* And add a matching input constraint. */
5240 if (allows_reg)
5242 sprintf (buf, "%d", i);
5244 /* If there are multiple alternatives in the constraint,
5245 handle each of them individually. Those that allow register
5246 will be replaced with operand number, the others will stay
5247 unchanged. */
5248 if (strchr (p, ',') != NULL)
5250 size_t len = 0, buflen = strlen (buf);
5251 char *beg, *end, *str, *dst;
5253 for (beg = p + 1;;)
5255 end = strchr (beg, ',');
5256 if (end == NULL)
5257 end = strchr (beg, '\0');
5258 if ((size_t) (end - beg) < buflen)
5259 len += buflen + 1;
5260 else
5261 len += end - beg + 1;
5262 if (*end)
5263 beg = end + 1;
5264 else
5265 break;
5268 str = (char *) alloca (len);
5269 for (beg = p + 1, dst = str;;)
5271 const char *tem;
5272 bool mem_p, reg_p, inout_p;
5274 end = strchr (beg, ',');
5275 if (end)
5276 *end = '\0';
5277 beg[-1] = '=';
5278 tem = beg - 1;
5279 parse_output_constraint (&tem, i, 0, 0,
5280 &mem_p, &reg_p, &inout_p);
5281 if (dst != str)
5282 *dst++ = ',';
5283 if (reg_p)
5285 memcpy (dst, buf, buflen);
5286 dst += buflen;
5288 else
5290 if (end)
5291 len = end - beg;
5292 else
5293 len = strlen (beg);
5294 memcpy (dst, beg, len);
5295 dst += len;
5297 if (end)
5298 beg = end + 1;
5299 else
5300 break;
5302 *dst = '\0';
5303 input = build_string (dst - str, str);
5305 else
5306 input = build_string (strlen (buf), buf);
5308 else
5309 input = build_string (constraint_len - 1, constraint + 1);
5311 free (p);
5313 input = build_tree_list (build_tree_list (NULL_TREE, input),
5314 unshare_expr (TREE_VALUE (link)));
5315 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5319 link_next = NULL_TREE;
5320 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5322 link_next = TREE_CHAIN (link);
5323 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5324 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5325 oconstraints, &allows_mem, &allows_reg);
5327 /* If we can't make copies, we can only accept memory. */
5328 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5330 if (allows_mem)
5331 allows_reg = 0;
5332 else
5334 error ("impossible constraint in %<asm%>");
5335 error ("non-memory input %d must stay in memory", i);
5336 return GS_ERROR;
5340 /* If the operand is a memory input, it should be an lvalue. */
5341 if (!allows_reg && allows_mem)
5343 tree inputv = TREE_VALUE (link);
5344 STRIP_NOPS (inputv);
5345 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5346 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5347 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5348 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5349 TREE_VALUE (link) = error_mark_node;
5350 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5351 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5352 mark_addressable (TREE_VALUE (link));
5353 if (tret == GS_ERROR)
5355 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5356 input_location = EXPR_LOCATION (TREE_VALUE (link));
5357 error ("memory input %d is not directly addressable", i);
5358 ret = tret;
5361 else
5363 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5364 is_gimple_asm_val, fb_rvalue);
5365 if (tret == GS_ERROR)
5366 ret = tret;
5369 TREE_CHAIN (link) = NULL_TREE;
5370 vec_safe_push (inputs, link);
5373 link_next = NULL_TREE;
5374 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5376 link_next = TREE_CHAIN (link);
5377 TREE_CHAIN (link) = NULL_TREE;
5378 vec_safe_push (clobbers, link);
5381 link_next = NULL_TREE;
5382 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5384 link_next = TREE_CHAIN (link);
5385 TREE_CHAIN (link) = NULL_TREE;
5386 vec_safe_push (labels, link);
5389 /* Do not add ASMs with errors to the gimple IL stream. */
5390 if (ret != GS_ERROR)
5392 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5393 inputs, outputs, clobbers, labels);
5395 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5396 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5398 gimplify_seq_add_stmt (pre_p, stmt);
5401 return ret;
5404 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5405 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5406 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5407 return to this function.
5409 FIXME should we complexify the prequeue handling instead? Or use flags
5410 for all the cleanups and let the optimizer tighten them up? The current
5411 code seems pretty fragile; it will break on a cleanup within any
5412 non-conditional nesting. But any such nesting would be broken, anyway;
5413 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5414 and continues out of it. We can do that at the RTL level, though, so
5415 having an optimizer to tighten up try/finally regions would be a Good
5416 Thing. */
5418 static enum gimplify_status
5419 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5421 gimple_stmt_iterator iter;
5422 gimple_seq body_sequence = NULL;
5424 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5426 /* We only care about the number of conditions between the innermost
5427 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5428 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5429 int old_conds = gimplify_ctxp->conditions;
5430 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5431 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5432 gimplify_ctxp->conditions = 0;
5433 gimplify_ctxp->conditional_cleanups = NULL;
5434 gimplify_ctxp->in_cleanup_point_expr = true;
5436 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5438 gimplify_ctxp->conditions = old_conds;
5439 gimplify_ctxp->conditional_cleanups = old_cleanups;
5440 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5442 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5444 gimple wce = gsi_stmt (iter);
5446 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5448 if (gsi_one_before_end_p (iter))
5450 /* Note that gsi_insert_seq_before and gsi_remove do not
5451 scan operands, unlike some other sequence mutators. */
5452 if (!gimple_wce_cleanup_eh_only (wce))
5453 gsi_insert_seq_before_without_update (&iter,
5454 gimple_wce_cleanup (wce),
5455 GSI_SAME_STMT);
5456 gsi_remove (&iter, true);
5457 break;
5459 else
5461 gimple gtry;
5462 gimple_seq seq;
5463 enum gimple_try_flags kind;
5465 if (gimple_wce_cleanup_eh_only (wce))
5466 kind = GIMPLE_TRY_CATCH;
5467 else
5468 kind = GIMPLE_TRY_FINALLY;
5469 seq = gsi_split_seq_after (iter);
5471 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5472 /* Do not use gsi_replace here, as it may scan operands.
5473 We want to do a simple structural modification only. */
5474 gsi_set_stmt (&iter, gtry);
5475 iter = gsi_start (gtry->gimple_try.eval);
5478 else
5479 gsi_next (&iter);
5482 gimplify_seq_add_seq (pre_p, body_sequence);
5483 if (temp)
5485 *expr_p = temp;
5486 return GS_OK;
5488 else
5490 *expr_p = NULL;
5491 return GS_ALL_DONE;
5495 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5496 is the cleanup action required. EH_ONLY is true if the cleanup should
5497 only be executed if an exception is thrown, not on normal exit. */
5499 static void
5500 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5502 gimple wce;
5503 gimple_seq cleanup_stmts = NULL;
5505 /* Errors can result in improperly nested cleanups. Which results in
5506 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5507 if (seen_error ())
5508 return;
5510 if (gimple_conditional_context ())
5512 /* If we're in a conditional context, this is more complex. We only
5513 want to run the cleanup if we actually ran the initialization that
5514 necessitates it, but we want to run it after the end of the
5515 conditional context. So we wrap the try/finally around the
5516 condition and use a flag to determine whether or not to actually
5517 run the destructor. Thus
5519 test ? f(A()) : 0
5521 becomes (approximately)
5523 flag = 0;
5524 try {
5525 if (test) { A::A(temp); flag = 1; val = f(temp); }
5526 else { val = 0; }
5527 } finally {
5528 if (flag) A::~A(temp);
5532 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5533 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5534 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5536 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5537 gimplify_stmt (&cleanup, &cleanup_stmts);
5538 wce = gimple_build_wce (cleanup_stmts);
5540 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5541 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5542 gimplify_seq_add_stmt (pre_p, ftrue);
5544 /* Because of this manipulation, and the EH edges that jump
5545 threading cannot redirect, the temporary (VAR) will appear
5546 to be used uninitialized. Don't warn. */
5547 TREE_NO_WARNING (var) = 1;
5549 else
5551 gimplify_stmt (&cleanup, &cleanup_stmts);
5552 wce = gimple_build_wce (cleanup_stmts);
5553 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5554 gimplify_seq_add_stmt (pre_p, wce);
5558 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5560 static enum gimplify_status
5561 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5563 tree targ = *expr_p;
5564 tree temp = TARGET_EXPR_SLOT (targ);
5565 tree init = TARGET_EXPR_INITIAL (targ);
5566 enum gimplify_status ret;
5568 if (init)
5570 tree cleanup = NULL_TREE;
5572 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5573 to the temps list. Handle also variable length TARGET_EXPRs. */
5574 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5576 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5577 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5578 gimplify_vla_decl (temp, pre_p);
5580 else
5581 gimple_add_tmp_var (temp);
5583 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5584 expression is supposed to initialize the slot. */
5585 if (VOID_TYPE_P (TREE_TYPE (init)))
5586 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5587 else
5589 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5590 init = init_expr;
5591 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5592 init = NULL;
5593 ggc_free (init_expr);
5595 if (ret == GS_ERROR)
5597 /* PR c++/28266 Make sure this is expanded only once. */
5598 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5599 return GS_ERROR;
5601 if (init)
5602 gimplify_and_add (init, pre_p);
5604 /* If needed, push the cleanup for the temp. */
5605 if (TARGET_EXPR_CLEANUP (targ))
5607 if (CLEANUP_EH_ONLY (targ))
5608 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5609 CLEANUP_EH_ONLY (targ), pre_p);
5610 else
5611 cleanup = TARGET_EXPR_CLEANUP (targ);
5614 /* Add a clobber for the temporary going out of scope, like
5615 gimplify_bind_expr. */
5616 if (gimplify_ctxp->in_cleanup_point_expr
5617 && needs_to_live_in_memory (temp)
5618 && flag_stack_reuse == SR_ALL)
5620 tree clobber = build_constructor (TREE_TYPE (temp),
5621 NULL);
5622 TREE_THIS_VOLATILE (clobber) = true;
5623 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5624 if (cleanup)
5625 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5626 clobber);
5627 else
5628 cleanup = clobber;
5631 if (cleanup)
5632 gimple_push_cleanup (temp, cleanup, false, pre_p);
5634 /* Only expand this once. */
5635 TREE_OPERAND (targ, 3) = init;
5636 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5638 else
5639 /* We should have expanded this before. */
5640 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5642 *expr_p = temp;
5643 return GS_OK;
5646 /* Gimplification of expression trees. */
5648 /* Gimplify an expression which appears at statement context. The
5649 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5650 NULL, a new sequence is allocated.
5652 Return true if we actually added a statement to the queue. */
5654 bool
5655 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5657 gimple_seq_node last;
5659 last = gimple_seq_last (*seq_p);
5660 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5661 return last != gimple_seq_last (*seq_p);
5664 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5665 to CTX. If entries already exist, force them to be some flavor of private.
5666 If there is no enclosing parallel, do nothing. */
5668 void
5669 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5671 splay_tree_node n;
5673 if (decl == NULL || !DECL_P (decl))
5674 return;
5678 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5679 if (n != NULL)
5681 if (n->value & GOVD_SHARED)
5682 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5683 else if (n->value & GOVD_MAP)
5684 n->value |= GOVD_MAP_TO_ONLY;
5685 else
5686 return;
5688 else if (ctx->region_type == ORT_TARGET)
5689 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5690 else if (ctx->region_type != ORT_WORKSHARE
5691 && ctx->region_type != ORT_SIMD
5692 && ctx->region_type != ORT_TARGET_DATA)
5693 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5695 ctx = ctx->outer_context;
5697 while (ctx);
5700 /* Similarly for each of the type sizes of TYPE. */
5702 static void
5703 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5705 if (type == NULL || type == error_mark_node)
5706 return;
5707 type = TYPE_MAIN_VARIANT (type);
5709 if (pointer_set_insert (ctx->privatized_types, type))
5710 return;
5712 switch (TREE_CODE (type))
5714 case INTEGER_TYPE:
5715 case ENUMERAL_TYPE:
5716 case BOOLEAN_TYPE:
5717 case REAL_TYPE:
5718 case FIXED_POINT_TYPE:
5719 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5720 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5721 break;
5723 case ARRAY_TYPE:
5724 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5725 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5726 break;
5728 case RECORD_TYPE:
5729 case UNION_TYPE:
5730 case QUAL_UNION_TYPE:
5732 tree field;
5733 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5734 if (TREE_CODE (field) == FIELD_DECL)
5736 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5737 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5740 break;
5742 case POINTER_TYPE:
5743 case REFERENCE_TYPE:
5744 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5745 break;
5747 default:
5748 break;
5751 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5752 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5753 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5756 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5758 static void
5759 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5761 splay_tree_node n;
5762 unsigned int nflags;
5763 tree t;
5765 if (error_operand_p (decl))
5766 return;
5768 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5769 there are constructors involved somewhere. */
5770 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5771 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5772 flags |= GOVD_SEEN;
5774 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5775 if (n != NULL && n->value != GOVD_ALIGNED)
5777 /* We shouldn't be re-adding the decl with the same data
5778 sharing class. */
5779 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5780 /* The only combination of data sharing classes we should see is
5781 FIRSTPRIVATE and LASTPRIVATE. */
5782 nflags = n->value | flags;
5783 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5784 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5785 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5786 n->value = nflags;
5787 return;
5790 /* When adding a variable-sized variable, we have to handle all sorts
5791 of additional bits of data: the pointer replacement variable, and
5792 the parameters of the type. */
5793 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5795 /* Add the pointer replacement variable as PRIVATE if the variable
5796 replacement is private, else FIRSTPRIVATE since we'll need the
5797 address of the original variable either for SHARED, or for the
5798 copy into or out of the context. */
5799 if (!(flags & GOVD_LOCAL))
5801 nflags = flags & GOVD_MAP
5802 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5803 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5804 nflags |= flags & GOVD_SEEN;
5805 t = DECL_VALUE_EXPR (decl);
5806 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5807 t = TREE_OPERAND (t, 0);
5808 gcc_assert (DECL_P (t));
5809 omp_add_variable (ctx, t, nflags);
5812 /* Add all of the variable and type parameters (which should have
5813 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5814 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5815 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5816 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5818 /* The variable-sized variable itself is never SHARED, only some form
5819 of PRIVATE. The sharing would take place via the pointer variable
5820 which we remapped above. */
5821 if (flags & GOVD_SHARED)
5822 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5823 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5825 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5826 alloca statement we generate for the variable, so make sure it
5827 is available. This isn't automatically needed for the SHARED
5828 case, since we won't be allocating local storage then.
5829 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5830 in this case omp_notice_variable will be called later
5831 on when it is gimplified. */
5832 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5833 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5834 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5836 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5837 && lang_hooks.decls.omp_privatize_by_reference (decl))
5839 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5841 /* Similar to the direct variable sized case above, we'll need the
5842 size of references being privatized. */
5843 if ((flags & GOVD_SHARED) == 0)
5845 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5846 if (TREE_CODE (t) != INTEGER_CST)
5847 omp_notice_variable (ctx, t, true);
5851 if (n != NULL)
5852 n->value |= flags;
5853 else
5854 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5857 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5858 This just prints out diagnostics about threadprivate variable uses
5859 in untied tasks. If DECL2 is non-NULL, prevent this warning
5860 on that variable. */
5862 static bool
5863 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5864 tree decl2)
5866 splay_tree_node n;
5867 struct gimplify_omp_ctx *octx;
5869 for (octx = ctx; octx; octx = octx->outer_context)
5870 if (octx->region_type == ORT_TARGET)
5872 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5873 if (n == NULL)
5875 error ("threadprivate variable %qE used in target region",
5876 DECL_NAME (decl));
5877 error_at (octx->location, "enclosing target region");
5878 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5880 if (decl2)
5881 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5884 if (ctx->region_type != ORT_UNTIED_TASK)
5885 return false;
5886 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5887 if (n == NULL)
5889 error ("threadprivate variable %qE used in untied task",
5890 DECL_NAME (decl));
5891 error_at (ctx->location, "enclosing task");
5892 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5894 if (decl2)
5895 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5896 return false;
5899 /* Record the fact that DECL was used within the OpenMP context CTX.
5900 IN_CODE is true when real code uses DECL, and false when we should
5901 merely emit default(none) errors. Return true if DECL is going to
5902 be remapped and thus DECL shouldn't be gimplified into its
5903 DECL_VALUE_EXPR (if any). */
5905 static bool
5906 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5908 splay_tree_node n;
5909 unsigned flags = in_code ? GOVD_SEEN : 0;
5910 bool ret = false, shared;
5912 if (error_operand_p (decl))
5913 return false;
5915 /* Threadprivate variables are predetermined. */
5916 if (is_global_var (decl))
5918 if (DECL_THREAD_LOCAL_P (decl))
5919 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5921 if (DECL_HAS_VALUE_EXPR_P (decl))
5923 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5925 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5926 return omp_notice_threadprivate_variable (ctx, decl, value);
5930 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5931 if (ctx->region_type == ORT_TARGET)
5933 if (n == NULL)
5935 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5937 error ("%qD referenced in target region does not have "
5938 "a mappable type", decl);
5939 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5941 else
5942 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5944 else
5945 n->value |= flags;
5946 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5947 goto do_outer;
5950 if (n == NULL)
5952 enum omp_clause_default_kind default_kind, kind;
5953 struct gimplify_omp_ctx *octx;
5955 if (ctx->region_type == ORT_WORKSHARE
5956 || ctx->region_type == ORT_SIMD
5957 || ctx->region_type == ORT_TARGET_DATA)
5958 goto do_outer;
5960 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5961 remapped firstprivate instead of shared. To some extent this is
5962 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5963 default_kind = ctx->default_kind;
5964 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5965 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5966 default_kind = kind;
5968 switch (default_kind)
5970 case OMP_CLAUSE_DEFAULT_NONE:
5971 if ((ctx->region_type & ORT_TASK) != 0)
5973 error ("%qE not specified in enclosing task",
5974 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5975 error_at (ctx->location, "enclosing task");
5977 else if (ctx->region_type == ORT_TEAMS)
5979 error ("%qE not specified in enclosing teams construct",
5980 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5981 error_at (ctx->location, "enclosing teams construct");
5983 else
5985 error ("%qE not specified in enclosing parallel",
5986 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5987 error_at (ctx->location, "enclosing parallel");
5989 /* FALLTHRU */
5990 case OMP_CLAUSE_DEFAULT_SHARED:
5991 flags |= GOVD_SHARED;
5992 break;
5993 case OMP_CLAUSE_DEFAULT_PRIVATE:
5994 flags |= GOVD_PRIVATE;
5995 break;
5996 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5997 flags |= GOVD_FIRSTPRIVATE;
5998 break;
5999 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6000 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6001 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6002 if (ctx->outer_context)
6003 omp_notice_variable (ctx->outer_context, decl, in_code);
6004 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
6006 splay_tree_node n2;
6008 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
6009 continue;
6010 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6011 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6013 flags |= GOVD_FIRSTPRIVATE;
6014 break;
6016 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
6017 break;
6019 if (flags & GOVD_FIRSTPRIVATE)
6020 break;
6021 if (octx == NULL
6022 && (TREE_CODE (decl) == PARM_DECL
6023 || (!is_global_var (decl)
6024 && DECL_CONTEXT (decl) == current_function_decl)))
6026 flags |= GOVD_FIRSTPRIVATE;
6027 break;
6029 flags |= GOVD_SHARED;
6030 break;
6031 default:
6032 gcc_unreachable ();
6035 if ((flags & GOVD_PRIVATE)
6036 && lang_hooks.decls.omp_private_outer_ref (decl))
6037 flags |= GOVD_PRIVATE_OUTER_REF;
6039 omp_add_variable (ctx, decl, flags);
6041 shared = (flags & GOVD_SHARED) != 0;
6042 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6043 goto do_outer;
6046 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6047 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6048 && DECL_SIZE (decl)
6049 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6051 splay_tree_node n2;
6052 tree t = DECL_VALUE_EXPR (decl);
6053 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6054 t = TREE_OPERAND (t, 0);
6055 gcc_assert (DECL_P (t));
6056 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6057 n2->value |= GOVD_SEEN;
6060 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6061 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6063 /* If nothing changed, there's nothing left to do. */
6064 if ((n->value & flags) == flags)
6065 return ret;
6066 flags |= n->value;
6067 n->value = flags;
6069 do_outer:
6070 /* If the variable is private in the current context, then we don't
6071 need to propagate anything to an outer context. */
6072 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6073 return ret;
6074 if (ctx->outer_context
6075 && omp_notice_variable (ctx->outer_context, decl, in_code))
6076 return true;
6077 return ret;
6080 /* Verify that DECL is private within CTX. If there's specific information
6081 to the contrary in the innermost scope, generate an error. */
6083 static bool
6084 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
6086 splay_tree_node n;
6088 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6089 if (n != NULL)
6091 if (n->value & GOVD_SHARED)
6093 if (ctx == gimplify_omp_ctxp)
6095 if (simd)
6096 error ("iteration variable %qE is predetermined linear",
6097 DECL_NAME (decl));
6098 else
6099 error ("iteration variable %qE should be private",
6100 DECL_NAME (decl));
6101 n->value = GOVD_PRIVATE;
6102 return true;
6104 else
6105 return false;
6107 else if ((n->value & GOVD_EXPLICIT) != 0
6108 && (ctx == gimplify_omp_ctxp
6109 || (ctx->region_type == ORT_COMBINED_PARALLEL
6110 && gimplify_omp_ctxp->outer_context == ctx)))
6112 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6113 error ("iteration variable %qE should not be firstprivate",
6114 DECL_NAME (decl));
6115 else if ((n->value & GOVD_REDUCTION) != 0)
6116 error ("iteration variable %qE should not be reduction",
6117 DECL_NAME (decl));
6118 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
6119 error ("iteration variable %qE should not be lastprivate",
6120 DECL_NAME (decl));
6121 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6122 error ("iteration variable %qE should not be private",
6123 DECL_NAME (decl));
6124 else if (simd && (n->value & GOVD_LINEAR) != 0)
6125 error ("iteration variable %qE is predetermined linear",
6126 DECL_NAME (decl));
6128 return (ctx == gimplify_omp_ctxp
6129 || (ctx->region_type == ORT_COMBINED_PARALLEL
6130 && gimplify_omp_ctxp->outer_context == ctx));
6133 if (ctx->region_type != ORT_WORKSHARE
6134 && ctx->region_type != ORT_SIMD)
6135 return false;
6136 else if (ctx->outer_context)
6137 return omp_is_private (ctx->outer_context, decl, simd);
6138 return false;
6141 /* Return true if DECL is private within a parallel region
6142 that binds to the current construct's context or in parallel
6143 region's REDUCTION clause. */
6145 static bool
6146 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6148 splay_tree_node n;
6152 ctx = ctx->outer_context;
6153 if (ctx == NULL)
6154 return !(is_global_var (decl)
6155 /* References might be private, but might be shared too. */
6156 || lang_hooks.decls.omp_privatize_by_reference (decl));
6158 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6159 continue;
6161 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6162 if (n != NULL)
6163 return (n->value & GOVD_SHARED) == 0;
6165 while (ctx->region_type == ORT_WORKSHARE
6166 || ctx->region_type == ORT_SIMD);
6167 return false;
6170 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6171 and previous omp contexts. */
6173 static void
6174 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6175 enum omp_region_type region_type)
6177 struct gimplify_omp_ctx *ctx, *outer_ctx;
6178 struct gimplify_ctx gctx;
6179 tree c;
6181 ctx = new_omp_context (region_type);
6182 outer_ctx = ctx->outer_context;
6184 while ((c = *list_p) != NULL)
6186 bool remove = false;
6187 bool notice_outer = true;
6188 const char *check_non_private = NULL;
6189 unsigned int flags;
6190 tree decl;
6192 switch (OMP_CLAUSE_CODE (c))
6194 case OMP_CLAUSE_PRIVATE:
6195 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6196 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6198 flags |= GOVD_PRIVATE_OUTER_REF;
6199 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6201 else
6202 notice_outer = false;
6203 goto do_add;
6204 case OMP_CLAUSE_SHARED:
6205 flags = GOVD_SHARED | GOVD_EXPLICIT;
6206 goto do_add;
6207 case OMP_CLAUSE_FIRSTPRIVATE:
6208 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6209 check_non_private = "firstprivate";
6210 goto do_add;
6211 case OMP_CLAUSE_LASTPRIVATE:
6212 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6213 check_non_private = "lastprivate";
6214 goto do_add;
6215 case OMP_CLAUSE_REDUCTION:
6216 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6217 check_non_private = "reduction";
6218 goto do_add;
6219 case OMP_CLAUSE_LINEAR:
6220 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6221 is_gimple_val, fb_rvalue) == GS_ERROR)
6223 remove = true;
6224 break;
6226 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6227 goto do_add;
6229 case OMP_CLAUSE_MAP:
6230 if (OMP_CLAUSE_SIZE (c)
6231 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6232 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6234 remove = true;
6235 break;
6237 decl = OMP_CLAUSE_DECL (c);
6238 if (!DECL_P (decl))
6240 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6241 NULL, is_gimple_lvalue, fb_lvalue)
6242 == GS_ERROR)
6244 remove = true;
6245 break;
6247 break;
6249 flags = GOVD_MAP | GOVD_EXPLICIT;
6250 goto do_add;
6252 case OMP_CLAUSE_DEPEND:
6253 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6255 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6256 NULL, is_gimple_val, fb_rvalue);
6257 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6259 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6261 remove = true;
6262 break;
6264 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6265 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6266 is_gimple_val, fb_rvalue) == GS_ERROR)
6268 remove = true;
6269 break;
6271 break;
6273 case OMP_CLAUSE_TO:
6274 case OMP_CLAUSE_FROM:
6275 if (OMP_CLAUSE_SIZE (c)
6276 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6277 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6279 remove = true;
6280 break;
6282 decl = OMP_CLAUSE_DECL (c);
6283 if (error_operand_p (decl))
6285 remove = true;
6286 break;
6288 if (!DECL_P (decl))
6290 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6291 NULL, is_gimple_lvalue, fb_lvalue)
6292 == GS_ERROR)
6294 remove = true;
6295 break;
6297 break;
6299 goto do_notice;
6301 do_add:
6302 decl = OMP_CLAUSE_DECL (c);
6303 if (error_operand_p (decl))
6305 remove = true;
6306 break;
6308 omp_add_variable (ctx, decl, flags);
6309 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6310 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6312 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6313 GOVD_LOCAL | GOVD_SEEN);
6314 gimplify_omp_ctxp = ctx;
6315 push_gimplify_context (&gctx);
6317 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6318 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6320 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6321 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6322 pop_gimplify_context
6323 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6324 push_gimplify_context (&gctx);
6325 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6326 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6327 pop_gimplify_context
6328 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6329 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6330 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6332 gimplify_omp_ctxp = outer_ctx;
6334 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6335 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6337 gimplify_omp_ctxp = ctx;
6338 push_gimplify_context (&gctx);
6339 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6341 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6342 NULL, NULL);
6343 TREE_SIDE_EFFECTS (bind) = 1;
6344 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6345 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6347 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6348 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6349 pop_gimplify_context
6350 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6351 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6353 gimplify_omp_ctxp = outer_ctx;
6355 if (notice_outer)
6356 goto do_notice;
6357 break;
6359 case OMP_CLAUSE_COPYIN:
6360 case OMP_CLAUSE_COPYPRIVATE:
6361 decl = OMP_CLAUSE_DECL (c);
6362 if (error_operand_p (decl))
6364 remove = true;
6365 break;
6367 do_notice:
6368 if (outer_ctx)
6369 omp_notice_variable (outer_ctx, decl, true);
6370 if (check_non_private
6371 && region_type == ORT_WORKSHARE
6372 && omp_check_private (ctx, decl))
6374 error ("%s variable %qE is private in outer context",
6375 check_non_private, DECL_NAME (decl));
6376 remove = true;
6378 break;
6380 case OMP_CLAUSE_FINAL:
6381 case OMP_CLAUSE_IF:
6382 OMP_CLAUSE_OPERAND (c, 0)
6383 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6384 /* Fall through. */
6386 case OMP_CLAUSE_SCHEDULE:
6387 case OMP_CLAUSE_NUM_THREADS:
6388 case OMP_CLAUSE_NUM_TEAMS:
6389 case OMP_CLAUSE_THREAD_LIMIT:
6390 case OMP_CLAUSE_DIST_SCHEDULE:
6391 case OMP_CLAUSE_DEVICE:
6392 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6393 is_gimple_val, fb_rvalue) == GS_ERROR)
6394 remove = true;
6395 break;
6397 case OMP_CLAUSE_NOWAIT:
6398 case OMP_CLAUSE_ORDERED:
6399 case OMP_CLAUSE_UNTIED:
6400 case OMP_CLAUSE_COLLAPSE:
6401 case OMP_CLAUSE_MERGEABLE:
6402 case OMP_CLAUSE_PROC_BIND:
6403 case OMP_CLAUSE_SAFELEN:
6404 break;
6406 case OMP_CLAUSE_ALIGNED:
6407 decl = OMP_CLAUSE_DECL (c);
6408 if (error_operand_p (decl))
6410 remove = true;
6411 break;
6413 if (!is_global_var (decl)
6414 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6415 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6416 break;
6418 case OMP_CLAUSE_DEFAULT:
6419 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6420 break;
6422 default:
6423 gcc_unreachable ();
6426 if (remove)
6427 *list_p = OMP_CLAUSE_CHAIN (c);
6428 else
6429 list_p = &OMP_CLAUSE_CHAIN (c);
6432 gimplify_omp_ctxp = ctx;
6435 /* For all variables that were not actually used within the context,
6436 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6438 static int
6439 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6441 tree *list_p = (tree *) data;
6442 tree decl = (tree) n->key;
6443 unsigned flags = n->value;
6444 enum omp_clause_code code;
6445 tree clause;
6446 bool private_debug;
6448 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6449 return 0;
6450 if ((flags & GOVD_SEEN) == 0)
6451 return 0;
6452 if (flags & GOVD_DEBUG_PRIVATE)
6454 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6455 private_debug = true;
6457 else if (flags & GOVD_MAP)
6458 private_debug = false;
6459 else
6460 private_debug
6461 = lang_hooks.decls.omp_private_debug_clause (decl,
6462 !!(flags & GOVD_SHARED));
6463 if (private_debug)
6464 code = OMP_CLAUSE_PRIVATE;
6465 else if (flags & GOVD_MAP)
6466 code = OMP_CLAUSE_MAP;
6467 else if (flags & GOVD_SHARED)
6469 if (is_global_var (decl))
6471 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6472 while (ctx != NULL)
6474 splay_tree_node on
6475 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6476 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6477 | GOVD_PRIVATE | GOVD_REDUCTION
6478 | GOVD_LINEAR)) != 0)
6479 break;
6480 ctx = ctx->outer_context;
6482 if (ctx == NULL)
6483 return 0;
6485 code = OMP_CLAUSE_SHARED;
6487 else if (flags & GOVD_PRIVATE)
6488 code = OMP_CLAUSE_PRIVATE;
6489 else if (flags & GOVD_FIRSTPRIVATE)
6490 code = OMP_CLAUSE_FIRSTPRIVATE;
6491 else if (flags & GOVD_LASTPRIVATE)
6492 code = OMP_CLAUSE_LASTPRIVATE;
6493 else if (flags & GOVD_ALIGNED)
6494 return 0;
6495 else
6496 gcc_unreachable ();
6498 clause = build_omp_clause (input_location, code);
6499 OMP_CLAUSE_DECL (clause) = decl;
6500 OMP_CLAUSE_CHAIN (clause) = *list_p;
6501 if (private_debug)
6502 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6503 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6504 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6505 else if (code == OMP_CLAUSE_MAP)
6507 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6508 ? OMP_CLAUSE_MAP_TO
6509 : OMP_CLAUSE_MAP_TOFROM;
6510 if (DECL_SIZE (decl)
6511 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6513 tree decl2 = DECL_VALUE_EXPR (decl);
6514 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6515 decl2 = TREE_OPERAND (decl2, 0);
6516 gcc_assert (DECL_P (decl2));
6517 tree mem = build_simple_mem_ref (decl2);
6518 OMP_CLAUSE_DECL (clause) = mem;
6519 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6520 if (gimplify_omp_ctxp->outer_context)
6522 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6523 omp_notice_variable (ctx, decl2, true);
6524 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6526 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6527 OMP_CLAUSE_MAP);
6528 OMP_CLAUSE_DECL (nc) = decl;
6529 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6530 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6531 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6532 OMP_CLAUSE_CHAIN (clause) = nc;
6535 *list_p = clause;
6536 lang_hooks.decls.omp_finish_clause (clause);
6538 return 0;
6541 static void
6542 gimplify_adjust_omp_clauses (tree *list_p)
6544 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6545 tree c, decl;
6547 while ((c = *list_p) != NULL)
6549 splay_tree_node n;
6550 bool remove = false;
6552 switch (OMP_CLAUSE_CODE (c))
6554 case OMP_CLAUSE_PRIVATE:
6555 case OMP_CLAUSE_SHARED:
6556 case OMP_CLAUSE_FIRSTPRIVATE:
6557 case OMP_CLAUSE_LINEAR:
6558 decl = OMP_CLAUSE_DECL (c);
6559 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6560 remove = !(n->value & GOVD_SEEN);
6561 if (! remove)
6563 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6564 if ((n->value & GOVD_DEBUG_PRIVATE)
6565 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6567 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6568 || ((n->value & GOVD_DATA_SHARE_CLASS)
6569 == GOVD_PRIVATE));
6570 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6571 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6573 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6574 && ctx->outer_context
6575 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6576 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6577 && !is_global_var (decl))
6579 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6581 n = splay_tree_lookup (ctx->outer_context->variables,
6582 (splay_tree_key) decl);
6583 if (n == NULL
6584 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6586 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6587 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6588 if (n == NULL)
6589 omp_add_variable (ctx->outer_context, decl,
6590 flags | GOVD_SEEN);
6591 else
6592 n->value |= flags | GOVD_SEEN;
6595 else
6596 omp_notice_variable (ctx->outer_context, decl, true);
6599 break;
6601 case OMP_CLAUSE_LASTPRIVATE:
6602 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6603 accurately reflect the presence of a FIRSTPRIVATE clause. */
6604 decl = OMP_CLAUSE_DECL (c);
6605 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6606 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6607 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6608 break;
6610 case OMP_CLAUSE_ALIGNED:
6611 decl = OMP_CLAUSE_DECL (c);
6612 if (!is_global_var (decl))
6614 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6615 remove = n == NULL || !(n->value & GOVD_SEEN);
6616 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6618 struct gimplify_omp_ctx *octx;
6619 if (n != NULL
6620 && (n->value & (GOVD_DATA_SHARE_CLASS
6621 & ~GOVD_FIRSTPRIVATE)))
6622 remove = true;
6623 else
6624 for (octx = ctx->outer_context; octx;
6625 octx = octx->outer_context)
6627 n = splay_tree_lookup (octx->variables,
6628 (splay_tree_key) decl);
6629 if (n == NULL)
6630 continue;
6631 if (n->value & GOVD_LOCAL)
6632 break;
6633 /* We have to avoid assigning a shared variable
6634 to itself when trying to add
6635 __builtin_assume_aligned. */
6636 if (n->value & GOVD_SHARED)
6638 remove = true;
6639 break;
6644 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6646 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6647 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6648 remove = true;
6650 break;
6652 case OMP_CLAUSE_MAP:
6653 decl = OMP_CLAUSE_DECL (c);
6654 if (!DECL_P (decl))
6655 break;
6656 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6657 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6658 remove = true;
6659 else if (DECL_SIZE (decl)
6660 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6661 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6663 tree decl2 = DECL_VALUE_EXPR (decl);
6664 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6665 decl2 = TREE_OPERAND (decl2, 0);
6666 gcc_assert (DECL_P (decl2));
6667 tree mem = build_simple_mem_ref (decl2);
6668 OMP_CLAUSE_DECL (c) = mem;
6669 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6670 if (ctx->outer_context)
6672 omp_notice_variable (ctx->outer_context, decl2, true);
6673 omp_notice_variable (ctx->outer_context,
6674 OMP_CLAUSE_SIZE (c), true);
6676 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6677 OMP_CLAUSE_MAP);
6678 OMP_CLAUSE_DECL (nc) = decl;
6679 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6680 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6681 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6682 OMP_CLAUSE_CHAIN (c) = nc;
6683 c = nc;
6685 break;
6687 case OMP_CLAUSE_TO:
6688 case OMP_CLAUSE_FROM:
6689 decl = OMP_CLAUSE_DECL (c);
6690 if (!DECL_P (decl))
6691 break;
6692 if (DECL_SIZE (decl)
6693 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6695 tree decl2 = DECL_VALUE_EXPR (decl);
6696 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6697 decl2 = TREE_OPERAND (decl2, 0);
6698 gcc_assert (DECL_P (decl2));
6699 tree mem = build_simple_mem_ref (decl2);
6700 OMP_CLAUSE_DECL (c) = mem;
6701 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6702 if (ctx->outer_context)
6704 omp_notice_variable (ctx->outer_context, decl2, true);
6705 omp_notice_variable (ctx->outer_context,
6706 OMP_CLAUSE_SIZE (c), true);
6709 break;
6711 case OMP_CLAUSE_REDUCTION:
6712 case OMP_CLAUSE_COPYIN:
6713 case OMP_CLAUSE_COPYPRIVATE:
6714 case OMP_CLAUSE_IF:
6715 case OMP_CLAUSE_NUM_THREADS:
6716 case OMP_CLAUSE_NUM_TEAMS:
6717 case OMP_CLAUSE_THREAD_LIMIT:
6718 case OMP_CLAUSE_DIST_SCHEDULE:
6719 case OMP_CLAUSE_DEVICE:
6720 case OMP_CLAUSE_SCHEDULE:
6721 case OMP_CLAUSE_NOWAIT:
6722 case OMP_CLAUSE_ORDERED:
6723 case OMP_CLAUSE_DEFAULT:
6724 case OMP_CLAUSE_UNTIED:
6725 case OMP_CLAUSE_COLLAPSE:
6726 case OMP_CLAUSE_FINAL:
6727 case OMP_CLAUSE_MERGEABLE:
6728 case OMP_CLAUSE_PROC_BIND:
6729 case OMP_CLAUSE_SAFELEN:
6730 case OMP_CLAUSE_DEPEND:
6731 break;
6733 default:
6734 gcc_unreachable ();
6737 if (remove)
6738 *list_p = OMP_CLAUSE_CHAIN (c);
6739 else
6740 list_p = &OMP_CLAUSE_CHAIN (c);
6743 /* Add in any implicit data sharing. */
6744 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6746 gimplify_omp_ctxp = ctx->outer_context;
6747 delete_omp_context (ctx);
6750 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6751 gimplification of the body, as well as scanning the body for used
6752 variables. We need to do this scan now, because variable-sized
6753 decls will be decomposed during gimplification. */
6755 static void
6756 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6758 tree expr = *expr_p;
6759 gimple g;
6760 gimple_seq body = NULL;
6761 struct gimplify_ctx gctx;
6763 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6764 OMP_PARALLEL_COMBINED (expr)
6765 ? ORT_COMBINED_PARALLEL
6766 : ORT_PARALLEL);
6768 push_gimplify_context (&gctx);
6770 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6771 if (gimple_code (g) == GIMPLE_BIND)
6772 pop_gimplify_context (g);
6773 else
6774 pop_gimplify_context (NULL);
6776 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6778 g = gimple_build_omp_parallel (body,
6779 OMP_PARALLEL_CLAUSES (expr),
6780 NULL_TREE, NULL_TREE);
6781 if (OMP_PARALLEL_COMBINED (expr))
6782 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6783 gimplify_seq_add_stmt (pre_p, g);
6784 *expr_p = NULL_TREE;
6787 /* Gimplify the contents of an OMP_TASK statement. This involves
6788 gimplification of the body, as well as scanning the body for used
6789 variables. We need to do this scan now, because variable-sized
6790 decls will be decomposed during gimplification. */
6792 static void
6793 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6795 tree expr = *expr_p;
6796 gimple g;
6797 gimple_seq body = NULL;
6798 struct gimplify_ctx gctx;
6800 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6801 find_omp_clause (OMP_TASK_CLAUSES (expr),
6802 OMP_CLAUSE_UNTIED)
6803 ? ORT_UNTIED_TASK : ORT_TASK);
6805 push_gimplify_context (&gctx);
6807 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6808 if (gimple_code (g) == GIMPLE_BIND)
6809 pop_gimplify_context (g);
6810 else
6811 pop_gimplify_context (NULL);
6813 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6815 g = gimple_build_omp_task (body,
6816 OMP_TASK_CLAUSES (expr),
6817 NULL_TREE, NULL_TREE,
6818 NULL_TREE, NULL_TREE, NULL_TREE);
6819 gimplify_seq_add_stmt (pre_p, g);
6820 *expr_p = NULL_TREE;
6823 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6824 with non-NULL OMP_FOR_INIT. */
6826 static tree
6827 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6829 *walk_subtrees = 0;
6830 switch (TREE_CODE (*tp))
6832 case OMP_FOR:
6833 *walk_subtrees = 1;
6834 /* FALLTHRU */
6835 case OMP_SIMD:
6836 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6837 return *tp;
6838 break;
6839 case BIND_EXPR:
6840 case STATEMENT_LIST:
6841 case OMP_PARALLEL:
6842 *walk_subtrees = 1;
6843 break;
6844 default:
6845 break;
6847 return NULL_TREE;
6850 /* Gimplify the gross structure of an OMP_FOR statement. */
6852 static enum gimplify_status
6853 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6855 tree for_stmt, orig_for_stmt, decl, var, t;
6856 enum gimplify_status ret = GS_ALL_DONE;
6857 enum gimplify_status tret;
6858 gimple gfor;
6859 gimple_seq for_body, for_pre_body;
6860 int i;
6861 bool simd;
6862 bitmap has_decl_expr = NULL;
6864 orig_for_stmt = for_stmt = *expr_p;
6866 simd = TREE_CODE (for_stmt) == OMP_SIMD;
6867 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6868 simd ? ORT_SIMD : ORT_WORKSHARE);
6870 /* Handle OMP_FOR_INIT. */
6871 for_pre_body = NULL;
6872 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6874 has_decl_expr = BITMAP_ALLOC (NULL);
6875 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6876 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6877 == VAR_DECL)
6879 t = OMP_FOR_PRE_BODY (for_stmt);
6880 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6882 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6884 tree_stmt_iterator si;
6885 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6886 tsi_next (&si))
6888 t = tsi_stmt (si);
6889 if (TREE_CODE (t) == DECL_EXPR
6890 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6891 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6895 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6896 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6898 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6900 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6901 NULL, NULL);
6902 gcc_assert (for_stmt != NULL_TREE);
6903 gimplify_omp_ctxp->combined_loop = true;
6906 for_body = NULL;
6907 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6908 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6909 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6910 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6911 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6913 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6914 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6915 decl = TREE_OPERAND (t, 0);
6916 gcc_assert (DECL_P (decl));
6917 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6918 || POINTER_TYPE_P (TREE_TYPE (decl)));
6920 /* Make sure the iteration variable is private. */
6921 tree c = NULL_TREE;
6922 if (orig_for_stmt != for_stmt)
6923 /* Do this only on innermost construct for combined ones. */;
6924 else if (simd)
6926 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6927 (splay_tree_key)decl);
6928 omp_is_private (gimplify_omp_ctxp, decl, simd);
6929 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6930 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6931 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6933 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6934 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6935 if (has_decl_expr
6936 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6937 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6938 OMP_CLAUSE_DECL (c) = decl;
6939 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6940 OMP_FOR_CLAUSES (for_stmt) = c;
6941 omp_add_variable (gimplify_omp_ctxp, decl,
6942 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6944 else
6946 bool lastprivate
6947 = (!has_decl_expr
6948 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6949 c = build_omp_clause (input_location,
6950 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6951 : OMP_CLAUSE_PRIVATE);
6952 OMP_CLAUSE_DECL (c) = decl;
6953 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6954 omp_add_variable (gimplify_omp_ctxp, decl,
6955 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6956 | GOVD_SEEN);
6957 c = NULL_TREE;
6960 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6961 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6962 else
6963 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6965 /* If DECL is not a gimple register, create a temporary variable to act
6966 as an iteration counter. This is valid, since DECL cannot be
6967 modified in the body of the loop. */
6968 if (orig_for_stmt != for_stmt)
6969 var = decl;
6970 else if (!is_gimple_reg (decl))
6972 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6973 TREE_OPERAND (t, 0) = var;
6975 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6977 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6979 else
6980 var = decl;
6982 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6983 is_gimple_val, fb_rvalue);
6984 ret = MIN (ret, tret);
6985 if (ret == GS_ERROR)
6986 return ret;
6988 /* Handle OMP_FOR_COND. */
6989 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6990 gcc_assert (COMPARISON_CLASS_P (t));
6991 gcc_assert (TREE_OPERAND (t, 0) == decl);
6993 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6994 is_gimple_val, fb_rvalue);
6995 ret = MIN (ret, tret);
6997 /* Handle OMP_FOR_INCR. */
6998 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6999 switch (TREE_CODE (t))
7001 case PREINCREMENT_EXPR:
7002 case POSTINCREMENT_EXPR:
7003 if (orig_for_stmt != for_stmt)
7004 break;
7005 t = build_int_cst (TREE_TYPE (decl), 1);
7006 if (c)
7007 OMP_CLAUSE_LINEAR_STEP (c) = t;
7008 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7009 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7010 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7011 break;
7013 case PREDECREMENT_EXPR:
7014 case POSTDECREMENT_EXPR:
7015 if (orig_for_stmt != for_stmt)
7016 break;
7017 t = build_int_cst (TREE_TYPE (decl), -1);
7018 if (c)
7019 OMP_CLAUSE_LINEAR_STEP (c) = t;
7020 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7021 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7022 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7023 break;
7025 case MODIFY_EXPR:
7026 gcc_assert (TREE_OPERAND (t, 0) == decl);
7027 TREE_OPERAND (t, 0) = var;
7029 t = TREE_OPERAND (t, 1);
7030 switch (TREE_CODE (t))
7032 case PLUS_EXPR:
7033 if (TREE_OPERAND (t, 1) == decl)
7035 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7036 TREE_OPERAND (t, 0) = var;
7037 break;
7040 /* Fallthru. */
7041 case MINUS_EXPR:
7042 case POINTER_PLUS_EXPR:
7043 gcc_assert (TREE_OPERAND (t, 0) == decl);
7044 TREE_OPERAND (t, 0) = var;
7045 break;
7046 default:
7047 gcc_unreachable ();
7050 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7051 is_gimple_val, fb_rvalue);
7052 ret = MIN (ret, tret);
7053 if (c)
7055 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
7056 if (TREE_CODE (t) == MINUS_EXPR)
7058 t = TREE_OPERAND (t, 1);
7059 OMP_CLAUSE_LINEAR_STEP (c)
7060 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
7061 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7062 &for_pre_body, NULL,
7063 is_gimple_val, fb_rvalue);
7064 ret = MIN (ret, tret);
7067 break;
7069 default:
7070 gcc_unreachable ();
7073 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7074 && orig_for_stmt == for_stmt)
7076 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7077 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7078 && OMP_CLAUSE_DECL (c) == decl
7079 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7081 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7082 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7083 gcc_assert (TREE_OPERAND (t, 0) == var);
7084 t = TREE_OPERAND (t, 1);
7085 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7086 || TREE_CODE (t) == MINUS_EXPR
7087 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7088 gcc_assert (TREE_OPERAND (t, 0) == var);
7089 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7090 TREE_OPERAND (t, 1));
7091 gimplify_assign (decl, t,
7092 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7097 BITMAP_FREE (has_decl_expr);
7099 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7101 if (orig_for_stmt != for_stmt)
7102 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7104 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7105 decl = TREE_OPERAND (t, 0);
7106 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7107 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7108 TREE_OPERAND (t, 0) = var;
7109 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7110 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7111 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7114 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
7116 int kind;
7117 switch (TREE_CODE (orig_for_stmt))
7119 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7120 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7121 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7122 default:
7123 gcc_unreachable ();
7125 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7126 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7127 for_pre_body);
7128 if (orig_for_stmt != for_stmt)
7129 gimple_omp_for_set_combined_p (gfor, true);
7130 if (gimplify_omp_ctxp
7131 && (gimplify_omp_ctxp->combined_loop
7132 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7133 && gimplify_omp_ctxp->outer_context
7134 && gimplify_omp_ctxp->outer_context->combined_loop)))
7136 gimple_omp_for_set_combined_into_p (gfor, true);
7137 if (gimplify_omp_ctxp->combined_loop)
7138 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7139 else
7140 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7143 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7145 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7146 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7147 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7148 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7149 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7150 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7151 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7152 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7155 gimplify_seq_add_stmt (pre_p, gfor);
7156 if (ret != GS_ALL_DONE)
7157 return GS_ERROR;
7158 *expr_p = NULL_TREE;
7159 return GS_ALL_DONE;
7162 /* Gimplify the gross structure of other OpenMP constructs.
7163 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7164 and OMP_TEAMS. */
7166 static void
7167 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7169 tree expr = *expr_p;
7170 gimple stmt;
7171 gimple_seq body = NULL;
7172 enum omp_region_type ort = ORT_WORKSHARE;
7174 switch (TREE_CODE (expr))
7176 case OMP_SECTIONS:
7177 case OMP_SINGLE:
7178 break;
7179 case OMP_TARGET:
7180 ort = ORT_TARGET;
7181 break;
7182 case OMP_TARGET_DATA:
7183 ort = ORT_TARGET_DATA;
7184 break;
7185 case OMP_TEAMS:
7186 ort = ORT_TEAMS;
7187 break;
7188 default:
7189 gcc_unreachable ();
7191 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7192 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7194 struct gimplify_ctx gctx;
7195 push_gimplify_context (&gctx);
7196 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7197 if (gimple_code (g) == GIMPLE_BIND)
7198 pop_gimplify_context (g);
7199 else
7200 pop_gimplify_context (NULL);
7201 if (ort == ORT_TARGET_DATA)
7203 gimple_seq cleanup = NULL;
7204 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7205 g = gimple_build_call (fn, 0);
7206 gimple_seq_add_stmt (&cleanup, g);
7207 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7208 body = NULL;
7209 gimple_seq_add_stmt (&body, g);
7212 else
7213 gimplify_and_add (OMP_BODY (expr), &body);
7214 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7216 switch (TREE_CODE (expr))
7218 case OMP_SECTIONS:
7219 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7220 break;
7221 case OMP_SINGLE:
7222 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7223 break;
7224 case OMP_TARGET:
7225 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7226 OMP_CLAUSES (expr));
7227 break;
7228 case OMP_TARGET_DATA:
7229 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7230 OMP_CLAUSES (expr));
7231 break;
7232 case OMP_TEAMS:
7233 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7234 break;
7235 default:
7236 gcc_unreachable ();
7239 gimplify_seq_add_stmt (pre_p, stmt);
7240 *expr_p = NULL_TREE;
7243 /* Gimplify the gross structure of OpenMP target update construct. */
7245 static void
7246 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7248 tree expr = *expr_p;
7249 gimple stmt;
7251 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7252 ORT_WORKSHARE);
7253 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7254 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7255 OMP_TARGET_UPDATE_CLAUSES (expr));
7257 gimplify_seq_add_stmt (pre_p, stmt);
7258 *expr_p = NULL_TREE;
7261 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7262 stabilized the lhs of the atomic operation as *ADDR. Return true if
7263 EXPR is this stabilized form. */
7265 static bool
7266 goa_lhs_expr_p (tree expr, tree addr)
7268 /* Also include casts to other type variants. The C front end is fond
7269 of adding these for e.g. volatile variables. This is like
7270 STRIP_TYPE_NOPS but includes the main variant lookup. */
7271 STRIP_USELESS_TYPE_CONVERSION (expr);
7273 if (TREE_CODE (expr) == INDIRECT_REF)
7275 expr = TREE_OPERAND (expr, 0);
7276 while (expr != addr
7277 && (CONVERT_EXPR_P (expr)
7278 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7279 && TREE_CODE (expr) == TREE_CODE (addr)
7280 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7282 expr = TREE_OPERAND (expr, 0);
7283 addr = TREE_OPERAND (addr, 0);
7285 if (expr == addr)
7286 return true;
7287 return (TREE_CODE (addr) == ADDR_EXPR
7288 && TREE_CODE (expr) == ADDR_EXPR
7289 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7291 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7292 return true;
7293 return false;
7296 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7297 expression does not involve the lhs, evaluate it into a temporary.
7298 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7299 or -1 if an error was encountered. */
7301 static int
7302 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7303 tree lhs_var)
7305 tree expr = *expr_p;
7306 int saw_lhs;
7308 if (goa_lhs_expr_p (expr, lhs_addr))
7310 *expr_p = lhs_var;
7311 return 1;
7313 if (is_gimple_val (expr))
7314 return 0;
7316 saw_lhs = 0;
7317 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7319 case tcc_binary:
7320 case tcc_comparison:
7321 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7322 lhs_var);
7323 case tcc_unary:
7324 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7325 lhs_var);
7326 break;
7327 case tcc_expression:
7328 switch (TREE_CODE (expr))
7330 case TRUTH_ANDIF_EXPR:
7331 case TRUTH_ORIF_EXPR:
7332 case TRUTH_AND_EXPR:
7333 case TRUTH_OR_EXPR:
7334 case TRUTH_XOR_EXPR:
7335 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7336 lhs_addr, lhs_var);
7337 case TRUTH_NOT_EXPR:
7338 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7339 lhs_addr, lhs_var);
7340 break;
7341 case COMPOUND_EXPR:
7342 /* Break out any preevaluations from cp_build_modify_expr. */
7343 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7344 expr = TREE_OPERAND (expr, 1))
7345 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7346 *expr_p = expr;
7347 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7348 default:
7349 break;
7351 break;
7352 default:
7353 break;
7356 if (saw_lhs == 0)
7358 enum gimplify_status gs;
7359 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7360 if (gs != GS_ALL_DONE)
7361 saw_lhs = -1;
7364 return saw_lhs;
7367 /* Gimplify an OMP_ATOMIC statement. */
7369 static enum gimplify_status
7370 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7372 tree addr = TREE_OPERAND (*expr_p, 0);
7373 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7374 ? NULL : TREE_OPERAND (*expr_p, 1);
7375 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7376 tree tmp_load;
7377 gimple loadstmt, storestmt;
7379 tmp_load = create_tmp_reg (type, NULL);
7380 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7381 return GS_ERROR;
7383 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7384 != GS_ALL_DONE)
7385 return GS_ERROR;
7387 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7388 gimplify_seq_add_stmt (pre_p, loadstmt);
7389 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7390 != GS_ALL_DONE)
7391 return GS_ERROR;
7393 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7394 rhs = tmp_load;
7395 storestmt = gimple_build_omp_atomic_store (rhs);
7396 gimplify_seq_add_stmt (pre_p, storestmt);
7397 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7399 gimple_omp_atomic_set_seq_cst (loadstmt);
7400 gimple_omp_atomic_set_seq_cst (storestmt);
7402 switch (TREE_CODE (*expr_p))
7404 case OMP_ATOMIC_READ:
7405 case OMP_ATOMIC_CAPTURE_OLD:
7406 *expr_p = tmp_load;
7407 gimple_omp_atomic_set_need_value (loadstmt);
7408 break;
7409 case OMP_ATOMIC_CAPTURE_NEW:
7410 *expr_p = rhs;
7411 gimple_omp_atomic_set_need_value (storestmt);
7412 break;
7413 default:
7414 *expr_p = NULL;
7415 break;
7418 return GS_ALL_DONE;
7421 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7422 body, and adding some EH bits. */
7424 static enum gimplify_status
7425 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7427 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7428 gimple g;
7429 gimple_seq body = NULL;
7430 struct gimplify_ctx gctx;
7431 int subcode = 0;
7433 /* Wrap the transaction body in a BIND_EXPR so we have a context
7434 where to put decls for OpenMP. */
7435 if (TREE_CODE (tbody) != BIND_EXPR)
7437 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7438 TREE_SIDE_EFFECTS (bind) = 1;
7439 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7440 TRANSACTION_EXPR_BODY (expr) = bind;
7443 push_gimplify_context (&gctx);
7444 temp = voidify_wrapper_expr (*expr_p, NULL);
7446 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7447 pop_gimplify_context (g);
7449 g = gimple_build_transaction (body, NULL);
7450 if (TRANSACTION_EXPR_OUTER (expr))
7451 subcode = GTMA_IS_OUTER;
7452 else if (TRANSACTION_EXPR_RELAXED (expr))
7453 subcode = GTMA_IS_RELAXED;
7454 gimple_transaction_set_subcode (g, subcode);
7456 gimplify_seq_add_stmt (pre_p, g);
7458 if (temp)
7460 *expr_p = temp;
7461 return GS_OK;
7464 *expr_p = NULL_TREE;
7465 return GS_ALL_DONE;
7468 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7469 expression produces a value to be used as an operand inside a GIMPLE
7470 statement, the value will be stored back in *EXPR_P. This value will
7471 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7472 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7473 emitted in PRE_P and POST_P.
7475 Additionally, this process may overwrite parts of the input
7476 expression during gimplification. Ideally, it should be
7477 possible to do non-destructive gimplification.
7479 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7480 the expression needs to evaluate to a value to be used as
7481 an operand in a GIMPLE statement, this value will be stored in
7482 *EXPR_P on exit. This happens when the caller specifies one
7483 of fb_lvalue or fb_rvalue fallback flags.
7485 PRE_P will contain the sequence of GIMPLE statements corresponding
7486 to the evaluation of EXPR and all the side-effects that must
7487 be executed before the main expression. On exit, the last
7488 statement of PRE_P is the core statement being gimplified. For
7489 instance, when gimplifying 'if (++a)' the last statement in
7490 PRE_P will be 'if (t.1)' where t.1 is the result of
7491 pre-incrementing 'a'.
7493 POST_P will contain the sequence of GIMPLE statements corresponding
7494 to the evaluation of all the side-effects that must be executed
7495 after the main expression. If this is NULL, the post
7496 side-effects are stored at the end of PRE_P.
7498 The reason why the output is split in two is to handle post
7499 side-effects explicitly. In some cases, an expression may have
7500 inner and outer post side-effects which need to be emitted in
7501 an order different from the one given by the recursive
7502 traversal. For instance, for the expression (*p--)++ the post
7503 side-effects of '--' must actually occur *after* the post
7504 side-effects of '++'. However, gimplification will first visit
7505 the inner expression, so if a separate POST sequence was not
7506 used, the resulting sequence would be:
7508 1 t.1 = *p
7509 2 p = p - 1
7510 3 t.2 = t.1 + 1
7511 4 *p = t.2
7513 However, the post-decrement operation in line #2 must not be
7514 evaluated until after the store to *p at line #4, so the
7515 correct sequence should be:
7517 1 t.1 = *p
7518 2 t.2 = t.1 + 1
7519 3 *p = t.2
7520 4 p = p - 1
7522 So, by specifying a separate post queue, it is possible
7523 to emit the post side-effects in the correct order.
7524 If POST_P is NULL, an internal queue will be used. Before
7525 returning to the caller, the sequence POST_P is appended to
7526 the main output sequence PRE_P.
7528 GIMPLE_TEST_F points to a function that takes a tree T and
7529 returns nonzero if T is in the GIMPLE form requested by the
7530 caller. The GIMPLE predicates are in gimple.c.
7532 FALLBACK tells the function what sort of a temporary we want if
7533 gimplification cannot produce an expression that complies with
7534 GIMPLE_TEST_F.
7536 fb_none means that no temporary should be generated
7537 fb_rvalue means that an rvalue is OK to generate
7538 fb_lvalue means that an lvalue is OK to generate
7539 fb_either means that either is OK, but an lvalue is preferable.
7540 fb_mayfail means that gimplification may fail (in which case
7541 GS_ERROR will be returned)
7543 The return value is either GS_ERROR or GS_ALL_DONE, since this
7544 function iterates until EXPR is completely gimplified or an error
7545 occurs. */
7547 enum gimplify_status
7548 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7549 bool (*gimple_test_f) (tree), fallback_t fallback)
7551 tree tmp;
7552 gimple_seq internal_pre = NULL;
7553 gimple_seq internal_post = NULL;
7554 tree save_expr;
7555 bool is_statement;
7556 location_t saved_location;
7557 enum gimplify_status ret;
7558 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7560 save_expr = *expr_p;
7561 if (save_expr == NULL_TREE)
7562 return GS_ALL_DONE;
7564 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7565 is_statement = gimple_test_f == is_gimple_stmt;
7566 if (is_statement)
7567 gcc_assert (pre_p);
7569 /* Consistency checks. */
7570 if (gimple_test_f == is_gimple_reg)
7571 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7572 else if (gimple_test_f == is_gimple_val
7573 || gimple_test_f == is_gimple_call_addr
7574 || gimple_test_f == is_gimple_condexpr
7575 || gimple_test_f == is_gimple_mem_rhs
7576 || gimple_test_f == is_gimple_mem_rhs_or_call
7577 || gimple_test_f == is_gimple_reg_rhs
7578 || gimple_test_f == is_gimple_reg_rhs_or_call
7579 || gimple_test_f == is_gimple_asm_val
7580 || gimple_test_f == is_gimple_mem_ref_addr)
7581 gcc_assert (fallback & fb_rvalue);
7582 else if (gimple_test_f == is_gimple_min_lval
7583 || gimple_test_f == is_gimple_lvalue)
7584 gcc_assert (fallback & fb_lvalue);
7585 else if (gimple_test_f == is_gimple_addressable)
7586 gcc_assert (fallback & fb_either);
7587 else if (gimple_test_f == is_gimple_stmt)
7588 gcc_assert (fallback == fb_none);
7589 else
7591 /* We should have recognized the GIMPLE_TEST_F predicate to
7592 know what kind of fallback to use in case a temporary is
7593 needed to hold the value or address of *EXPR_P. */
7594 gcc_unreachable ();
7597 /* We used to check the predicate here and return immediately if it
7598 succeeds. This is wrong; the design is for gimplification to be
7599 idempotent, and for the predicates to only test for valid forms, not
7600 whether they are fully simplified. */
7601 if (pre_p == NULL)
7602 pre_p = &internal_pre;
7604 if (post_p == NULL)
7605 post_p = &internal_post;
7607 /* Remember the last statements added to PRE_P and POST_P. Every
7608 new statement added by the gimplification helpers needs to be
7609 annotated with location information. To centralize the
7610 responsibility, we remember the last statement that had been
7611 added to both queues before gimplifying *EXPR_P. If
7612 gimplification produces new statements in PRE_P and POST_P, those
7613 statements will be annotated with the same location information
7614 as *EXPR_P. */
7615 pre_last_gsi = gsi_last (*pre_p);
7616 post_last_gsi = gsi_last (*post_p);
7618 saved_location = input_location;
7619 if (save_expr != error_mark_node
7620 && EXPR_HAS_LOCATION (*expr_p))
7621 input_location = EXPR_LOCATION (*expr_p);
7623 /* Loop over the specific gimplifiers until the toplevel node
7624 remains the same. */
7627 /* Strip away as many useless type conversions as possible
7628 at the toplevel. */
7629 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7631 /* Remember the expr. */
7632 save_expr = *expr_p;
7634 /* Die, die, die, my darling. */
7635 if (save_expr == error_mark_node
7636 || (TREE_TYPE (save_expr)
7637 && TREE_TYPE (save_expr) == error_mark_node))
7639 ret = GS_ERROR;
7640 break;
7643 /* Do any language-specific gimplification. */
7644 ret = ((enum gimplify_status)
7645 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7646 if (ret == GS_OK)
7648 if (*expr_p == NULL_TREE)
7649 break;
7650 if (*expr_p != save_expr)
7651 continue;
7653 else if (ret != GS_UNHANDLED)
7654 break;
7656 /* Make sure that all the cases set 'ret' appropriately. */
7657 ret = GS_UNHANDLED;
7658 switch (TREE_CODE (*expr_p))
7660 /* First deal with the special cases. */
7662 case POSTINCREMENT_EXPR:
7663 case POSTDECREMENT_EXPR:
7664 case PREINCREMENT_EXPR:
7665 case PREDECREMENT_EXPR:
7666 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7667 fallback != fb_none,
7668 TREE_TYPE (*expr_p));
7669 break;
7671 case ARRAY_REF:
7672 case ARRAY_RANGE_REF:
7673 case REALPART_EXPR:
7674 case IMAGPART_EXPR:
7675 case COMPONENT_REF:
7676 case VIEW_CONVERT_EXPR:
7677 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7678 fallback ? fallback : fb_rvalue);
7679 break;
7681 case COND_EXPR:
7682 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7684 /* C99 code may assign to an array in a structure value of a
7685 conditional expression, and this has undefined behavior
7686 only on execution, so create a temporary if an lvalue is
7687 required. */
7688 if (fallback == fb_lvalue)
7690 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7691 mark_addressable (*expr_p);
7692 ret = GS_OK;
7694 break;
7696 case CILK_SPAWN_STMT:
7697 gcc_assert
7698 (fn_contains_cilk_spawn_p (cfun)
7699 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p));
7700 if (!seen_error ())
7702 ret = (enum gimplify_status)
7703 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p,
7704 post_p);
7705 break;
7707 /* If errors are seen, then just process it as a CALL_EXPR. */
7709 case CALL_EXPR:
7710 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7712 /* C99 code may assign to an array in a structure returned
7713 from a function, and this has undefined behavior only on
7714 execution, so create a temporary if an lvalue is
7715 required. */
7716 if (fallback == fb_lvalue)
7718 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7719 mark_addressable (*expr_p);
7720 ret = GS_OK;
7722 break;
7724 case TREE_LIST:
7725 gcc_unreachable ();
7727 case COMPOUND_EXPR:
7728 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7729 break;
7731 case COMPOUND_LITERAL_EXPR:
7732 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7733 gimple_test_f, fallback);
7734 break;
7736 case MODIFY_EXPR:
7737 case INIT_EXPR:
7738 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7739 fallback != fb_none);
7740 break;
7742 case TRUTH_ANDIF_EXPR:
7743 case TRUTH_ORIF_EXPR:
7745 /* Preserve the original type of the expression and the
7746 source location of the outer expression. */
7747 tree org_type = TREE_TYPE (*expr_p);
7748 *expr_p = gimple_boolify (*expr_p);
7749 *expr_p = build3_loc (input_location, COND_EXPR,
7750 org_type, *expr_p,
7751 fold_convert_loc
7752 (input_location,
7753 org_type, boolean_true_node),
7754 fold_convert_loc
7755 (input_location,
7756 org_type, boolean_false_node));
7757 ret = GS_OK;
7758 break;
7761 case TRUTH_NOT_EXPR:
7763 tree type = TREE_TYPE (*expr_p);
7764 /* The parsers are careful to generate TRUTH_NOT_EXPR
7765 only with operands that are always zero or one.
7766 We do not fold here but handle the only interesting case
7767 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7768 *expr_p = gimple_boolify (*expr_p);
7769 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7770 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7771 TREE_TYPE (*expr_p),
7772 TREE_OPERAND (*expr_p, 0));
7773 else
7774 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7775 TREE_TYPE (*expr_p),
7776 TREE_OPERAND (*expr_p, 0),
7777 build_int_cst (TREE_TYPE (*expr_p), 1));
7778 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7779 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7780 ret = GS_OK;
7781 break;
7784 case ADDR_EXPR:
7785 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7786 break;
7788 case ANNOTATE_EXPR:
7790 tree cond = TREE_OPERAND (*expr_p, 0);
7791 tree id = TREE_OPERAND (*expr_p, 1);
7792 tree tmp = create_tmp_var_raw (TREE_TYPE(cond), NULL);
7793 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7794 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7795 cond, id);
7796 gimple_call_set_lhs (call, tmp);
7797 gimplify_seq_add_stmt (pre_p, call);
7798 *expr_p = tmp;
7799 ret = GS_ALL_DONE;
7800 break;
7803 case VA_ARG_EXPR:
7804 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7805 break;
7807 CASE_CONVERT:
7808 if (IS_EMPTY_STMT (*expr_p))
7810 ret = GS_ALL_DONE;
7811 break;
7814 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7815 || fallback == fb_none)
7817 /* Just strip a conversion to void (or in void context) and
7818 try again. */
7819 *expr_p = TREE_OPERAND (*expr_p, 0);
7820 ret = GS_OK;
7821 break;
7824 ret = gimplify_conversion (expr_p);
7825 if (ret == GS_ERROR)
7826 break;
7827 if (*expr_p != save_expr)
7828 break;
7829 /* FALLTHRU */
7831 case FIX_TRUNC_EXPR:
7832 /* unary_expr: ... | '(' cast ')' val | ... */
7833 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7834 is_gimple_val, fb_rvalue);
7835 recalculate_side_effects (*expr_p);
7836 break;
7838 case INDIRECT_REF:
7840 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7841 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7842 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7844 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7845 if (*expr_p != save_expr)
7847 ret = GS_OK;
7848 break;
7851 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7852 is_gimple_reg, fb_rvalue);
7853 if (ret == GS_ERROR)
7854 break;
7856 recalculate_side_effects (*expr_p);
7857 *expr_p = fold_build2_loc (input_location, MEM_REF,
7858 TREE_TYPE (*expr_p),
7859 TREE_OPERAND (*expr_p, 0),
7860 build_int_cst (saved_ptr_type, 0));
7861 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7862 TREE_THIS_NOTRAP (*expr_p) = notrap;
7863 ret = GS_OK;
7864 break;
7867 /* We arrive here through the various re-gimplifcation paths. */
7868 case MEM_REF:
7869 /* First try re-folding the whole thing. */
7870 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7871 TREE_OPERAND (*expr_p, 0),
7872 TREE_OPERAND (*expr_p, 1));
7873 if (tmp)
7875 *expr_p = tmp;
7876 recalculate_side_effects (*expr_p);
7877 ret = GS_OK;
7878 break;
7880 /* Avoid re-gimplifying the address operand if it is already
7881 in suitable form. Re-gimplifying would mark the address
7882 operand addressable. Always gimplify when not in SSA form
7883 as we still may have to gimplify decls with value-exprs. */
7884 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7885 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7887 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7888 is_gimple_mem_ref_addr, fb_rvalue);
7889 if (ret == GS_ERROR)
7890 break;
7892 recalculate_side_effects (*expr_p);
7893 ret = GS_ALL_DONE;
7894 break;
7896 /* Constants need not be gimplified. */
7897 case INTEGER_CST:
7898 case REAL_CST:
7899 case FIXED_CST:
7900 case STRING_CST:
7901 case COMPLEX_CST:
7902 case VECTOR_CST:
7903 ret = GS_ALL_DONE;
7904 break;
7906 case CONST_DECL:
7907 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7908 CONST_DECL node. Otherwise the decl is replaceable by its
7909 value. */
7910 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7911 if (fallback & fb_lvalue)
7912 ret = GS_ALL_DONE;
7913 else
7915 *expr_p = DECL_INITIAL (*expr_p);
7916 ret = GS_OK;
7918 break;
7920 case DECL_EXPR:
7921 ret = gimplify_decl_expr (expr_p, pre_p);
7922 break;
7924 case BIND_EXPR:
7925 ret = gimplify_bind_expr (expr_p, pre_p);
7926 break;
7928 case LOOP_EXPR:
7929 ret = gimplify_loop_expr (expr_p, pre_p);
7930 break;
7932 case SWITCH_EXPR:
7933 ret = gimplify_switch_expr (expr_p, pre_p);
7934 break;
7936 case EXIT_EXPR:
7937 ret = gimplify_exit_expr (expr_p);
7938 break;
7940 case GOTO_EXPR:
7941 /* If the target is not LABEL, then it is a computed jump
7942 and the target needs to be gimplified. */
7943 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7945 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7946 NULL, is_gimple_val, fb_rvalue);
7947 if (ret == GS_ERROR)
7948 break;
7950 gimplify_seq_add_stmt (pre_p,
7951 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7952 ret = GS_ALL_DONE;
7953 break;
7955 case PREDICT_EXPR:
7956 gimplify_seq_add_stmt (pre_p,
7957 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7958 PREDICT_EXPR_OUTCOME (*expr_p)));
7959 ret = GS_ALL_DONE;
7960 break;
7962 case LABEL_EXPR:
7963 ret = GS_ALL_DONE;
7964 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7965 == current_function_decl);
7966 gimplify_seq_add_stmt (pre_p,
7967 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7968 break;
7970 case CASE_LABEL_EXPR:
7971 ret = gimplify_case_label_expr (expr_p, pre_p);
7972 break;
7974 case RETURN_EXPR:
7975 ret = gimplify_return_expr (*expr_p, pre_p);
7976 break;
7978 case CONSTRUCTOR:
7979 /* Don't reduce this in place; let gimplify_init_constructor work its
7980 magic. Buf if we're just elaborating this for side effects, just
7981 gimplify any element that has side-effects. */
7982 if (fallback == fb_none)
7984 unsigned HOST_WIDE_INT ix;
7985 tree val;
7986 tree temp = NULL_TREE;
7987 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7988 if (TREE_SIDE_EFFECTS (val))
7989 append_to_statement_list (val, &temp);
7991 *expr_p = temp;
7992 ret = temp ? GS_OK : GS_ALL_DONE;
7994 /* C99 code may assign to an array in a constructed
7995 structure or union, and this has undefined behavior only
7996 on execution, so create a temporary if an lvalue is
7997 required. */
7998 else if (fallback == fb_lvalue)
8000 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
8001 mark_addressable (*expr_p);
8002 ret = GS_OK;
8004 else
8005 ret = GS_ALL_DONE;
8006 break;
8008 /* The following are special cases that are not handled by the
8009 original GIMPLE grammar. */
8011 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8012 eliminated. */
8013 case SAVE_EXPR:
8014 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8015 break;
8017 case BIT_FIELD_REF:
8018 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8019 post_p, is_gimple_lvalue, fb_either);
8020 recalculate_side_effects (*expr_p);
8021 break;
8023 case TARGET_MEM_REF:
8025 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8027 if (TMR_BASE (*expr_p))
8028 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8029 post_p, is_gimple_mem_ref_addr, fb_either);
8030 if (TMR_INDEX (*expr_p))
8031 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8032 post_p, is_gimple_val, fb_rvalue);
8033 if (TMR_INDEX2 (*expr_p))
8034 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8035 post_p, is_gimple_val, fb_rvalue);
8036 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8037 ret = MIN (r0, r1);
8039 break;
8041 case NON_LVALUE_EXPR:
8042 /* This should have been stripped above. */
8043 gcc_unreachable ();
8045 case ASM_EXPR:
8046 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8047 break;
8049 case TRY_FINALLY_EXPR:
8050 case TRY_CATCH_EXPR:
8052 gimple_seq eval, cleanup;
8053 gimple try_;
8055 /* Calls to destructors are generated automatically in FINALLY/CATCH
8056 block. They should have location as UNKNOWN_LOCATION. However,
8057 gimplify_call_expr will reset these call stmts to input_location
8058 if it finds stmt's location is unknown. To prevent resetting for
8059 destructors, we set the input_location to unknown.
8060 Note that this only affects the destructor calls in FINALLY/CATCH
8061 block, and will automatically reset to its original value by the
8062 end of gimplify_expr. */
8063 input_location = UNKNOWN_LOCATION;
8064 eval = cleanup = NULL;
8065 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8066 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8067 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8068 if (gimple_seq_empty_p (cleanup))
8070 gimple_seq_add_seq (pre_p, eval);
8071 ret = GS_ALL_DONE;
8072 break;
8074 try_ = gimple_build_try (eval, cleanup,
8075 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8076 ? GIMPLE_TRY_FINALLY
8077 : GIMPLE_TRY_CATCH);
8078 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8079 gimple_set_location (try_, saved_location);
8080 else
8081 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8082 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8083 gimple_try_set_catch_is_cleanup (try_,
8084 TRY_CATCH_IS_CLEANUP (*expr_p));
8085 gimplify_seq_add_stmt (pre_p, try_);
8086 ret = GS_ALL_DONE;
8087 break;
8090 case CLEANUP_POINT_EXPR:
8091 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8092 break;
8094 case TARGET_EXPR:
8095 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8096 break;
8098 case CATCH_EXPR:
8100 gimple c;
8101 gimple_seq handler = NULL;
8102 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8103 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8104 gimplify_seq_add_stmt (pre_p, c);
8105 ret = GS_ALL_DONE;
8106 break;
8109 case EH_FILTER_EXPR:
8111 gimple ehf;
8112 gimple_seq failure = NULL;
8114 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8115 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8116 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8117 gimplify_seq_add_stmt (pre_p, ehf);
8118 ret = GS_ALL_DONE;
8119 break;
8122 case OBJ_TYPE_REF:
8124 enum gimplify_status r0, r1;
8125 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8126 post_p, is_gimple_val, fb_rvalue);
8127 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8128 post_p, is_gimple_val, fb_rvalue);
8129 TREE_SIDE_EFFECTS (*expr_p) = 0;
8130 ret = MIN (r0, r1);
8132 break;
8134 case LABEL_DECL:
8135 /* We get here when taking the address of a label. We mark
8136 the label as "forced"; meaning it can never be removed and
8137 it is a potential target for any computed goto. */
8138 FORCED_LABEL (*expr_p) = 1;
8139 ret = GS_ALL_DONE;
8140 break;
8142 case STATEMENT_LIST:
8143 ret = gimplify_statement_list (expr_p, pre_p);
8144 break;
8146 case WITH_SIZE_EXPR:
8148 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8149 post_p == &internal_post ? NULL : post_p,
8150 gimple_test_f, fallback);
8151 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8152 is_gimple_val, fb_rvalue);
8153 ret = GS_ALL_DONE;
8155 break;
8157 case VAR_DECL:
8158 case PARM_DECL:
8159 ret = gimplify_var_or_parm_decl (expr_p);
8160 break;
8162 case RESULT_DECL:
8163 /* When within an OpenMP context, notice uses of variables. */
8164 if (gimplify_omp_ctxp)
8165 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8166 ret = GS_ALL_DONE;
8167 break;
8169 case SSA_NAME:
8170 /* Allow callbacks into the gimplifier during optimization. */
8171 ret = GS_ALL_DONE;
8172 break;
8174 case OMP_PARALLEL:
8175 gimplify_omp_parallel (expr_p, pre_p);
8176 ret = GS_ALL_DONE;
8177 break;
8179 case OMP_TASK:
8180 gimplify_omp_task (expr_p, pre_p);
8181 ret = GS_ALL_DONE;
8182 break;
8184 case OMP_FOR:
8185 case OMP_SIMD:
8186 case OMP_DISTRIBUTE:
8187 ret = gimplify_omp_for (expr_p, pre_p);
8188 break;
8190 case OMP_SECTIONS:
8191 case OMP_SINGLE:
8192 case OMP_TARGET:
8193 case OMP_TARGET_DATA:
8194 case OMP_TEAMS:
8195 gimplify_omp_workshare (expr_p, pre_p);
8196 ret = GS_ALL_DONE;
8197 break;
8199 case OMP_TARGET_UPDATE:
8200 gimplify_omp_target_update (expr_p, pre_p);
8201 ret = GS_ALL_DONE;
8202 break;
8204 case OMP_SECTION:
8205 case OMP_MASTER:
8206 case OMP_TASKGROUP:
8207 case OMP_ORDERED:
8208 case OMP_CRITICAL:
8210 gimple_seq body = NULL;
8211 gimple g;
8213 gimplify_and_add (OMP_BODY (*expr_p), &body);
8214 switch (TREE_CODE (*expr_p))
8216 case OMP_SECTION:
8217 g = gimple_build_omp_section (body);
8218 break;
8219 case OMP_MASTER:
8220 g = gimple_build_omp_master (body);
8221 break;
8222 case OMP_TASKGROUP:
8224 gimple_seq cleanup = NULL;
8225 tree fn
8226 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8227 g = gimple_build_call (fn, 0);
8228 gimple_seq_add_stmt (&cleanup, g);
8229 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8230 body = NULL;
8231 gimple_seq_add_stmt (&body, g);
8232 g = gimple_build_omp_taskgroup (body);
8234 break;
8235 case OMP_ORDERED:
8236 g = gimple_build_omp_ordered (body);
8237 break;
8238 case OMP_CRITICAL:
8239 g = gimple_build_omp_critical (body,
8240 OMP_CRITICAL_NAME (*expr_p));
8241 break;
8242 default:
8243 gcc_unreachable ();
8245 gimplify_seq_add_stmt (pre_p, g);
8246 ret = GS_ALL_DONE;
8247 break;
8250 case OMP_ATOMIC:
8251 case OMP_ATOMIC_READ:
8252 case OMP_ATOMIC_CAPTURE_OLD:
8253 case OMP_ATOMIC_CAPTURE_NEW:
8254 ret = gimplify_omp_atomic (expr_p, pre_p);
8255 break;
8257 case TRANSACTION_EXPR:
8258 ret = gimplify_transaction (expr_p, pre_p);
8259 break;
8261 case TRUTH_AND_EXPR:
8262 case TRUTH_OR_EXPR:
8263 case TRUTH_XOR_EXPR:
8265 tree orig_type = TREE_TYPE (*expr_p);
8266 tree new_type, xop0, xop1;
8267 *expr_p = gimple_boolify (*expr_p);
8268 new_type = TREE_TYPE (*expr_p);
8269 if (!useless_type_conversion_p (orig_type, new_type))
8271 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8272 ret = GS_OK;
8273 break;
8276 /* Boolified binary truth expressions are semantically equivalent
8277 to bitwise binary expressions. Canonicalize them to the
8278 bitwise variant. */
8279 switch (TREE_CODE (*expr_p))
8281 case TRUTH_AND_EXPR:
8282 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8283 break;
8284 case TRUTH_OR_EXPR:
8285 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8286 break;
8287 case TRUTH_XOR_EXPR:
8288 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8289 break;
8290 default:
8291 break;
8293 /* Now make sure that operands have compatible type to
8294 expression's new_type. */
8295 xop0 = TREE_OPERAND (*expr_p, 0);
8296 xop1 = TREE_OPERAND (*expr_p, 1);
8297 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8298 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8299 new_type,
8300 xop0);
8301 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8302 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8303 new_type,
8304 xop1);
8305 /* Continue classified as tcc_binary. */
8306 goto expr_2;
8309 case FMA_EXPR:
8310 case VEC_COND_EXPR:
8311 case VEC_PERM_EXPR:
8312 /* Classified as tcc_expression. */
8313 goto expr_3;
8315 case POINTER_PLUS_EXPR:
8317 enum gimplify_status r0, r1;
8318 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8319 post_p, is_gimple_val, fb_rvalue);
8320 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8321 post_p, is_gimple_val, fb_rvalue);
8322 recalculate_side_effects (*expr_p);
8323 ret = MIN (r0, r1);
8324 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8325 after gimplifying operands - this is similar to how
8326 it would be folding all gimplified stmts on creation
8327 to have them canonicalized, which is what we eventually
8328 should do anyway. */
8329 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8330 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8332 *expr_p = build_fold_addr_expr_with_type_loc
8333 (input_location,
8334 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8335 TREE_OPERAND (*expr_p, 0),
8336 fold_convert (ptr_type_node,
8337 TREE_OPERAND (*expr_p, 1))),
8338 TREE_TYPE (*expr_p));
8339 ret = MIN (ret, GS_OK);
8341 break;
8344 case CILK_SYNC_STMT:
8346 if (!fn_contains_cilk_spawn_p (cfun))
8348 error_at (EXPR_LOCATION (*expr_p),
8349 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8350 ret = GS_ERROR;
8352 else
8354 gimplify_cilk_sync (expr_p, pre_p);
8355 ret = GS_ALL_DONE;
8357 break;
8360 default:
8361 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8363 case tcc_comparison:
8364 /* Handle comparison of objects of non scalar mode aggregates
8365 with a call to memcmp. It would be nice to only have to do
8366 this for variable-sized objects, but then we'd have to allow
8367 the same nest of reference nodes we allow for MODIFY_EXPR and
8368 that's too complex.
8370 Compare scalar mode aggregates as scalar mode values. Using
8371 memcmp for them would be very inefficient at best, and is
8372 plain wrong if bitfields are involved. */
8374 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8376 /* Vector comparisons need no boolification. */
8377 if (TREE_CODE (type) == VECTOR_TYPE)
8378 goto expr_2;
8379 else if (!AGGREGATE_TYPE_P (type))
8381 tree org_type = TREE_TYPE (*expr_p);
8382 *expr_p = gimple_boolify (*expr_p);
8383 if (!useless_type_conversion_p (org_type,
8384 TREE_TYPE (*expr_p)))
8386 *expr_p = fold_convert_loc (input_location,
8387 org_type, *expr_p);
8388 ret = GS_OK;
8390 else
8391 goto expr_2;
8393 else if (TYPE_MODE (type) != BLKmode)
8394 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8395 else
8396 ret = gimplify_variable_sized_compare (expr_p);
8398 break;
8401 /* If *EXPR_P does not need to be special-cased, handle it
8402 according to its class. */
8403 case tcc_unary:
8404 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8405 post_p, is_gimple_val, fb_rvalue);
8406 break;
8408 case tcc_binary:
8409 expr_2:
8411 enum gimplify_status r0, r1;
8413 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8414 post_p, is_gimple_val, fb_rvalue);
8415 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8416 post_p, is_gimple_val, fb_rvalue);
8418 ret = MIN (r0, r1);
8419 break;
8422 expr_3:
8424 enum gimplify_status r0, r1, r2;
8426 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8427 post_p, is_gimple_val, fb_rvalue);
8428 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8429 post_p, is_gimple_val, fb_rvalue);
8430 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8431 post_p, is_gimple_val, fb_rvalue);
8433 ret = MIN (MIN (r0, r1), r2);
8434 break;
8437 case tcc_declaration:
8438 case tcc_constant:
8439 ret = GS_ALL_DONE;
8440 goto dont_recalculate;
8442 default:
8443 gcc_unreachable ();
8446 recalculate_side_effects (*expr_p);
8448 dont_recalculate:
8449 break;
8452 gcc_assert (*expr_p || ret != GS_OK);
8454 while (ret == GS_OK);
8456 /* If we encountered an error_mark somewhere nested inside, either
8457 stub out the statement or propagate the error back out. */
8458 if (ret == GS_ERROR)
8460 if (is_statement)
8461 *expr_p = NULL;
8462 goto out;
8465 /* This was only valid as a return value from the langhook, which
8466 we handled. Make sure it doesn't escape from any other context. */
8467 gcc_assert (ret != GS_UNHANDLED);
8469 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8471 /* We aren't looking for a value, and we don't have a valid
8472 statement. If it doesn't have side-effects, throw it away. */
8473 if (!TREE_SIDE_EFFECTS (*expr_p))
8474 *expr_p = NULL;
8475 else if (!TREE_THIS_VOLATILE (*expr_p))
8477 /* This is probably a _REF that contains something nested that
8478 has side effects. Recurse through the operands to find it. */
8479 enum tree_code code = TREE_CODE (*expr_p);
8481 switch (code)
8483 case COMPONENT_REF:
8484 case REALPART_EXPR:
8485 case IMAGPART_EXPR:
8486 case VIEW_CONVERT_EXPR:
8487 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8488 gimple_test_f, fallback);
8489 break;
8491 case ARRAY_REF:
8492 case ARRAY_RANGE_REF:
8493 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8494 gimple_test_f, fallback);
8495 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8496 gimple_test_f, fallback);
8497 break;
8499 default:
8500 /* Anything else with side-effects must be converted to
8501 a valid statement before we get here. */
8502 gcc_unreachable ();
8505 *expr_p = NULL;
8507 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8508 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8510 /* Historically, the compiler has treated a bare reference
8511 to a non-BLKmode volatile lvalue as forcing a load. */
8512 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8514 /* Normally, we do not want to create a temporary for a
8515 TREE_ADDRESSABLE type because such a type should not be
8516 copied by bitwise-assignment. However, we make an
8517 exception here, as all we are doing here is ensuring that
8518 we read the bytes that make up the type. We use
8519 create_tmp_var_raw because create_tmp_var will abort when
8520 given a TREE_ADDRESSABLE type. */
8521 tree tmp = create_tmp_var_raw (type, "vol");
8522 gimple_add_tmp_var (tmp);
8523 gimplify_assign (tmp, *expr_p, pre_p);
8524 *expr_p = NULL;
8526 else
8527 /* We can't do anything useful with a volatile reference to
8528 an incomplete type, so just throw it away. Likewise for
8529 a BLKmode type, since any implicit inner load should
8530 already have been turned into an explicit one by the
8531 gimplification process. */
8532 *expr_p = NULL;
8535 /* If we are gimplifying at the statement level, we're done. Tack
8536 everything together and return. */
8537 if (fallback == fb_none || is_statement)
8539 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8540 it out for GC to reclaim it. */
8541 *expr_p = NULL_TREE;
8543 if (!gimple_seq_empty_p (internal_pre)
8544 || !gimple_seq_empty_p (internal_post))
8546 gimplify_seq_add_seq (&internal_pre, internal_post);
8547 gimplify_seq_add_seq (pre_p, internal_pre);
8550 /* The result of gimplifying *EXPR_P is going to be the last few
8551 statements in *PRE_P and *POST_P. Add location information
8552 to all the statements that were added by the gimplification
8553 helpers. */
8554 if (!gimple_seq_empty_p (*pre_p))
8555 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8557 if (!gimple_seq_empty_p (*post_p))
8558 annotate_all_with_location_after (*post_p, post_last_gsi,
8559 input_location);
8561 goto out;
8564 #ifdef ENABLE_GIMPLE_CHECKING
8565 if (*expr_p)
8567 enum tree_code code = TREE_CODE (*expr_p);
8568 /* These expressions should already be in gimple IR form. */
8569 gcc_assert (code != MODIFY_EXPR
8570 && code != ASM_EXPR
8571 && code != BIND_EXPR
8572 && code != CATCH_EXPR
8573 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8574 && code != EH_FILTER_EXPR
8575 && code != GOTO_EXPR
8576 && code != LABEL_EXPR
8577 && code != LOOP_EXPR
8578 && code != SWITCH_EXPR
8579 && code != TRY_FINALLY_EXPR
8580 && code != OMP_CRITICAL
8581 && code != OMP_FOR
8582 && code != OMP_MASTER
8583 && code != OMP_TASKGROUP
8584 && code != OMP_ORDERED
8585 && code != OMP_PARALLEL
8586 && code != OMP_SECTIONS
8587 && code != OMP_SECTION
8588 && code != OMP_SINGLE);
8590 #endif
8592 /* Otherwise we're gimplifying a subexpression, so the resulting
8593 value is interesting. If it's a valid operand that matches
8594 GIMPLE_TEST_F, we're done. Unless we are handling some
8595 post-effects internally; if that's the case, we need to copy into
8596 a temporary before adding the post-effects to POST_P. */
8597 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8598 goto out;
8600 /* Otherwise, we need to create a new temporary for the gimplified
8601 expression. */
8603 /* We can't return an lvalue if we have an internal postqueue. The
8604 object the lvalue refers to would (probably) be modified by the
8605 postqueue; we need to copy the value out first, which means an
8606 rvalue. */
8607 if ((fallback & fb_lvalue)
8608 && gimple_seq_empty_p (internal_post)
8609 && is_gimple_addressable (*expr_p))
8611 /* An lvalue will do. Take the address of the expression, store it
8612 in a temporary, and replace the expression with an INDIRECT_REF of
8613 that temporary. */
8614 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8615 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8616 *expr_p = build_simple_mem_ref (tmp);
8618 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8620 /* An rvalue will do. Assign the gimplified expression into a
8621 new temporary TMP and replace the original expression with
8622 TMP. First, make sure that the expression has a type so that
8623 it can be assigned into a temporary. */
8624 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8625 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8627 else
8629 #ifdef ENABLE_GIMPLE_CHECKING
8630 if (!(fallback & fb_mayfail))
8632 fprintf (stderr, "gimplification failed:\n");
8633 print_generic_expr (stderr, *expr_p, 0);
8634 debug_tree (*expr_p);
8635 internal_error ("gimplification failed");
8637 #endif
8638 gcc_assert (fallback & fb_mayfail);
8640 /* If this is an asm statement, and the user asked for the
8641 impossible, don't die. Fail and let gimplify_asm_expr
8642 issue an error. */
8643 ret = GS_ERROR;
8644 goto out;
8647 /* Make sure the temporary matches our predicate. */
8648 gcc_assert ((*gimple_test_f) (*expr_p));
8650 if (!gimple_seq_empty_p (internal_post))
8652 annotate_all_with_location (internal_post, input_location);
8653 gimplify_seq_add_seq (pre_p, internal_post);
8656 out:
8657 input_location = saved_location;
8658 return ret;
8661 /* Look through TYPE for variable-sized objects and gimplify each such
8662 size that we find. Add to LIST_P any statements generated. */
8664 void
8665 gimplify_type_sizes (tree type, gimple_seq *list_p)
8667 tree field, t;
8669 if (type == NULL || type == error_mark_node)
8670 return;
8672 /* We first do the main variant, then copy into any other variants. */
8673 type = TYPE_MAIN_VARIANT (type);
8675 /* Avoid infinite recursion. */
8676 if (TYPE_SIZES_GIMPLIFIED (type))
8677 return;
8679 TYPE_SIZES_GIMPLIFIED (type) = 1;
8681 switch (TREE_CODE (type))
8683 case INTEGER_TYPE:
8684 case ENUMERAL_TYPE:
8685 case BOOLEAN_TYPE:
8686 case REAL_TYPE:
8687 case FIXED_POINT_TYPE:
8688 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8689 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8691 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8693 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8694 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8696 break;
8698 case ARRAY_TYPE:
8699 /* These types may not have declarations, so handle them here. */
8700 gimplify_type_sizes (TREE_TYPE (type), list_p);
8701 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8702 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8703 with assigned stack slots, for -O1+ -g they should be tracked
8704 by VTA. */
8705 if (!(TYPE_NAME (type)
8706 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8707 && DECL_IGNORED_P (TYPE_NAME (type)))
8708 && TYPE_DOMAIN (type)
8709 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8711 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8712 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8713 DECL_IGNORED_P (t) = 0;
8714 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8715 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8716 DECL_IGNORED_P (t) = 0;
8718 break;
8720 case RECORD_TYPE:
8721 case UNION_TYPE:
8722 case QUAL_UNION_TYPE:
8723 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8724 if (TREE_CODE (field) == FIELD_DECL)
8726 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8727 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8728 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8729 gimplify_type_sizes (TREE_TYPE (field), list_p);
8731 break;
8733 case POINTER_TYPE:
8734 case REFERENCE_TYPE:
8735 /* We used to recurse on the pointed-to type here, which turned out to
8736 be incorrect because its definition might refer to variables not
8737 yet initialized at this point if a forward declaration is involved.
8739 It was actually useful for anonymous pointed-to types to ensure
8740 that the sizes evaluation dominates every possible later use of the
8741 values. Restricting to such types here would be safe since there
8742 is no possible forward declaration around, but would introduce an
8743 undesirable middle-end semantic to anonymity. We then defer to
8744 front-ends the responsibility of ensuring that the sizes are
8745 evaluated both early and late enough, e.g. by attaching artificial
8746 type declarations to the tree. */
8747 break;
8749 default:
8750 break;
8753 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8754 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8756 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8758 TYPE_SIZE (t) = TYPE_SIZE (type);
8759 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8760 TYPE_SIZES_GIMPLIFIED (t) = 1;
8764 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8765 a size or position, has had all of its SAVE_EXPRs evaluated.
8766 We add any required statements to *STMT_P. */
8768 void
8769 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8771 tree expr = *expr_p;
8773 /* We don't do anything if the value isn't there, is constant, or contains
8774 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8775 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8776 will want to replace it with a new variable, but that will cause problems
8777 if this type is from outside the function. It's OK to have that here. */
8778 if (is_gimple_sizepos (expr))
8779 return;
8781 *expr_p = unshare_expr (expr);
8783 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8786 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8787 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8788 is true, also gimplify the parameters. */
8790 gimple
8791 gimplify_body (tree fndecl, bool do_parms)
8793 location_t saved_location = input_location;
8794 gimple_seq parm_stmts, seq;
8795 gimple outer_bind;
8796 struct gimplify_ctx gctx;
8797 struct cgraph_node *cgn;
8799 timevar_push (TV_TREE_GIMPLIFY);
8801 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8802 gimplification. */
8803 default_rtl_profile ();
8805 gcc_assert (gimplify_ctxp == NULL);
8806 push_gimplify_context (&gctx);
8808 if (flag_openmp)
8810 gcc_assert (gimplify_omp_ctxp == NULL);
8811 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8812 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8815 /* Unshare most shared trees in the body and in that of any nested functions.
8816 It would seem we don't have to do this for nested functions because
8817 they are supposed to be output and then the outer function gimplified
8818 first, but the g++ front end doesn't always do it that way. */
8819 unshare_body (fndecl);
8820 unvisit_body (fndecl);
8822 cgn = cgraph_get_node (fndecl);
8823 if (cgn && cgn->origin)
8824 nonlocal_vlas = pointer_set_create ();
8826 /* Make sure input_location isn't set to something weird. */
8827 input_location = DECL_SOURCE_LOCATION (fndecl);
8829 /* Resolve callee-copies. This has to be done before processing
8830 the body so that DECL_VALUE_EXPR gets processed correctly. */
8831 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8833 /* Gimplify the function's body. */
8834 seq = NULL;
8835 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8836 outer_bind = gimple_seq_first_stmt (seq);
8837 if (!outer_bind)
8839 outer_bind = gimple_build_nop ();
8840 gimplify_seq_add_stmt (&seq, outer_bind);
8843 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8844 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8845 if (gimple_code (outer_bind) == GIMPLE_BIND
8846 && gimple_seq_first (seq) == gimple_seq_last (seq))
8848 else
8849 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8851 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8853 /* If we had callee-copies statements, insert them at the beginning
8854 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8855 if (!gimple_seq_empty_p (parm_stmts))
8857 tree parm;
8859 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8860 gimple_bind_set_body (outer_bind, parm_stmts);
8862 for (parm = DECL_ARGUMENTS (current_function_decl);
8863 parm; parm = DECL_CHAIN (parm))
8864 if (DECL_HAS_VALUE_EXPR_P (parm))
8866 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8867 DECL_IGNORED_P (parm) = 0;
8871 if (nonlocal_vlas)
8873 pointer_set_destroy (nonlocal_vlas);
8874 nonlocal_vlas = NULL;
8877 if (flag_openmp && gimplify_omp_ctxp)
8879 delete_omp_context (gimplify_omp_ctxp);
8880 gimplify_omp_ctxp = NULL;
8883 pop_gimplify_context (outer_bind);
8884 gcc_assert (gimplify_ctxp == NULL);
8886 #ifdef ENABLE_CHECKING
8887 if (!seen_error ())
8888 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8889 #endif
8891 timevar_pop (TV_TREE_GIMPLIFY);
8892 input_location = saved_location;
8894 return outer_bind;
8897 typedef char *char_p; /* For DEF_VEC_P. */
8899 /* Return whether we should exclude FNDECL from instrumentation. */
8901 bool
8902 flag_instrument_functions_exclude_p (tree fndecl)
8904 vec<char_p> *v;
8906 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8907 if (v && v->length () > 0)
8909 const char *name;
8910 int i;
8911 char *s;
8913 name = lang_hooks.decl_printable_name (fndecl, 0);
8914 FOR_EACH_VEC_ELT (*v, i, s)
8915 if (strstr (name, s) != NULL)
8916 return true;
8919 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8920 if (v && v->length () > 0)
8922 const char *name;
8923 int i;
8924 char *s;
8926 name = DECL_SOURCE_FILE (fndecl);
8927 FOR_EACH_VEC_ELT (*v, i, s)
8928 if (strstr (name, s) != NULL)
8929 return true;
8932 return false;
8935 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8936 node for the function we want to gimplify.
8938 Return the sequence of GIMPLE statements corresponding to the body
8939 of FNDECL. */
8941 void
8942 gimplify_function_tree (tree fndecl)
8944 tree parm, ret;
8945 gimple_seq seq;
8946 gimple bind;
8948 gcc_assert (!gimple_body (fndecl));
8950 if (DECL_STRUCT_FUNCTION (fndecl))
8951 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8952 else
8953 push_struct_function (fndecl);
8955 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8957 /* Preliminarily mark non-addressed complex variables as eligible
8958 for promotion to gimple registers. We'll transform their uses
8959 as we find them. */
8960 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8961 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8962 && !TREE_THIS_VOLATILE (parm)
8963 && !needs_to_live_in_memory (parm))
8964 DECL_GIMPLE_REG_P (parm) = 1;
8967 ret = DECL_RESULT (fndecl);
8968 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8969 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8970 && !needs_to_live_in_memory (ret))
8971 DECL_GIMPLE_REG_P (ret) = 1;
8973 bind = gimplify_body (fndecl, true);
8975 /* The tree body of the function is no longer needed, replace it
8976 with the new GIMPLE body. */
8977 seq = NULL;
8978 gimple_seq_add_stmt (&seq, bind);
8979 gimple_set_body (fndecl, seq);
8981 /* If we're instrumenting function entry/exit, then prepend the call to
8982 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8983 catch the exit hook. */
8984 /* ??? Add some way to ignore exceptions for this TFE. */
8985 if (flag_instrument_function_entry_exit
8986 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8987 && !flag_instrument_functions_exclude_p (fndecl))
8989 tree x;
8990 gimple new_bind;
8991 gimple tf;
8992 gimple_seq cleanup = NULL, body = NULL;
8993 tree tmp_var;
8994 gimple call;
8996 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8997 call = gimple_build_call (x, 1, integer_zero_node);
8998 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8999 gimple_call_set_lhs (call, tmp_var);
9000 gimplify_seq_add_stmt (&cleanup, call);
9001 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9002 call = gimple_build_call (x, 2,
9003 build_fold_addr_expr (current_function_decl),
9004 tmp_var);
9005 gimplify_seq_add_stmt (&cleanup, call);
9006 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9008 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9009 call = gimple_build_call (x, 1, integer_zero_node);
9010 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9011 gimple_call_set_lhs (call, tmp_var);
9012 gimplify_seq_add_stmt (&body, call);
9013 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9014 call = gimple_build_call (x, 2,
9015 build_fold_addr_expr (current_function_decl),
9016 tmp_var);
9017 gimplify_seq_add_stmt (&body, call);
9018 gimplify_seq_add_stmt (&body, tf);
9019 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9020 /* Clear the block for BIND, since it is no longer directly inside
9021 the function, but within a try block. */
9022 gimple_bind_set_block (bind, NULL);
9024 /* Replace the current function body with the body
9025 wrapped in the try/finally TF. */
9026 seq = NULL;
9027 gimple_seq_add_stmt (&seq, new_bind);
9028 gimple_set_body (fndecl, seq);
9031 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9032 cfun->curr_properties = PROP_gimple_any;
9034 pop_cfun ();
9037 /* Some transformations like inlining may invalidate the GIMPLE form
9038 for operands. This function traverses all the operands in STMT and
9039 gimplifies anything that is not a valid gimple operand. Any new
9040 GIMPLE statements are inserted before *GSI_P. */
9042 void
9043 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
9045 size_t i, num_ops;
9046 tree lhs;
9047 gimple_seq pre = NULL;
9048 gimple post_stmt = NULL;
9049 struct gimplify_ctx gctx;
9051 push_gimplify_context (&gctx);
9052 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
9054 switch (gimple_code (stmt))
9056 case GIMPLE_COND:
9057 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
9058 is_gimple_val, fb_rvalue);
9059 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
9060 is_gimple_val, fb_rvalue);
9061 break;
9062 case GIMPLE_SWITCH:
9063 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
9064 is_gimple_val, fb_rvalue);
9065 break;
9066 case GIMPLE_OMP_ATOMIC_LOAD:
9067 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
9068 is_gimple_val, fb_rvalue);
9069 break;
9070 case GIMPLE_ASM:
9072 size_t i, noutputs = gimple_asm_noutputs (stmt);
9073 const char *constraint, **oconstraints;
9074 bool allows_mem, allows_reg, is_inout;
9076 oconstraints
9077 = (const char **) alloca ((noutputs) * sizeof (const char *));
9078 for (i = 0; i < noutputs; i++)
9080 tree op = gimple_asm_output_op (stmt, i);
9081 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
9082 oconstraints[i] = constraint;
9083 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
9084 &allows_reg, &is_inout);
9085 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9086 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
9087 fb_lvalue | fb_mayfail);
9089 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
9091 tree op = gimple_asm_input_op (stmt, i);
9092 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
9093 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
9094 oconstraints, &allows_mem, &allows_reg);
9095 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
9096 allows_reg = 0;
9097 if (!allows_reg && allows_mem)
9098 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9099 is_gimple_lvalue, fb_lvalue | fb_mayfail);
9100 else
9101 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9102 is_gimple_asm_val, fb_rvalue);
9105 break;
9106 default:
9107 /* NOTE: We start gimplifying operands from last to first to
9108 make sure that side-effects on the RHS of calls, assignments
9109 and ASMs are executed before the LHS. The ordering is not
9110 important for other statements. */
9111 num_ops = gimple_num_ops (stmt);
9112 for (i = num_ops; i > 0; i--)
9114 tree op = gimple_op (stmt, i - 1);
9115 if (op == NULL_TREE)
9116 continue;
9117 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
9118 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
9119 else if (i == 2
9120 && is_gimple_assign (stmt)
9121 && num_ops == 2
9122 && get_gimple_rhs_class (gimple_expr_code (stmt))
9123 == GIMPLE_SINGLE_RHS)
9124 gimplify_expr (&op, &pre, NULL,
9125 rhs_predicate_for (gimple_assign_lhs (stmt)),
9126 fb_rvalue);
9127 else if (i == 2 && is_gimple_call (stmt))
9129 if (TREE_CODE (op) == FUNCTION_DECL)
9130 continue;
9131 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
9133 else
9134 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
9135 gimple_set_op (stmt, i - 1, op);
9138 lhs = gimple_get_lhs (stmt);
9139 /* If the LHS changed it in a way that requires a simple RHS,
9140 create temporary. */
9141 if (lhs && !is_gimple_reg (lhs))
9143 bool need_temp = false;
9145 if (is_gimple_assign (stmt)
9146 && num_ops == 2
9147 && get_gimple_rhs_class (gimple_expr_code (stmt))
9148 == GIMPLE_SINGLE_RHS)
9149 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
9150 rhs_predicate_for (gimple_assign_lhs (stmt)),
9151 fb_rvalue);
9152 else if (is_gimple_reg (lhs))
9154 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9156 if (is_gimple_call (stmt))
9158 i = gimple_call_flags (stmt);
9159 if ((i & ECF_LOOPING_CONST_OR_PURE)
9160 || !(i & (ECF_CONST | ECF_PURE)))
9161 need_temp = true;
9163 if (stmt_can_throw_internal (stmt))
9164 need_temp = true;
9167 else
9169 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9170 need_temp = true;
9171 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
9173 if (is_gimple_call (stmt))
9175 tree fndecl = gimple_call_fndecl (stmt);
9177 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
9178 && !(fndecl && DECL_RESULT (fndecl)
9179 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
9180 need_temp = true;
9182 else
9183 need_temp = true;
9186 if (need_temp)
9188 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
9189 if (gimple_in_ssa_p (cfun))
9190 temp = make_ssa_name (temp, NULL);
9191 gimple_set_lhs (stmt, temp);
9192 post_stmt = gimple_build_assign (lhs, temp);
9193 if (TREE_CODE (lhs) == SSA_NAME)
9194 SSA_NAME_DEF_STMT (lhs) = post_stmt;
9197 break;
9200 if (!gimple_seq_empty_p (pre))
9201 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
9202 if (post_stmt)
9203 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
9205 pop_gimplify_context (NULL);
9208 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
9209 the predicate that will hold for the result. If VAR is not NULL, make the
9210 base variable of the final destination be VAR if suitable. */
9212 tree
9213 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
9214 gimple_predicate gimple_test_f, tree var)
9216 enum gimplify_status ret;
9217 struct gimplify_ctx gctx;
9218 location_t saved_location;
9220 *stmts = NULL;
9222 /* gimple_test_f might be more strict than is_gimple_val, make
9223 sure we pass both. Just checking gimple_test_f doesn't work
9224 because most gimple predicates do not work recursively. */
9225 if (is_gimple_val (expr)
9226 && (*gimple_test_f) (expr))
9227 return expr;
9229 push_gimplify_context (&gctx);
9230 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
9231 gimplify_ctxp->allow_rhs_cond_expr = true;
9232 saved_location = input_location;
9233 input_location = UNKNOWN_LOCATION;
9235 if (var)
9237 if (gimplify_ctxp->into_ssa
9238 && is_gimple_reg (var))
9239 var = make_ssa_name (var, NULL);
9240 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
9243 if (TREE_CODE (expr) != MODIFY_EXPR
9244 && TREE_TYPE (expr) == void_type_node)
9246 gimplify_and_add (expr, stmts);
9247 expr = NULL_TREE;
9249 else
9251 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
9252 gcc_assert (ret != GS_ERROR);
9255 input_location = saved_location;
9256 pop_gimplify_context (NULL);
9258 return expr;
9261 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
9262 force the result to be either ssa_name or an invariant, otherwise
9263 just force it to be a rhs expression. If VAR is not NULL, make the
9264 base variable of the final destination be VAR if suitable. */
9266 tree
9267 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
9269 return force_gimple_operand_1 (expr, stmts,
9270 simple ? is_gimple_val : is_gimple_reg_rhs,
9271 var);
9274 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
9275 and VAR. If some statements are produced, emits them at GSI.
9276 If BEFORE is true. the statements are appended before GSI, otherwise
9277 they are appended after it. M specifies the way GSI moves after
9278 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
9280 tree
9281 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
9282 gimple_predicate gimple_test_f,
9283 tree var, bool before,
9284 enum gsi_iterator_update m)
9286 gimple_seq stmts;
9288 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
9290 if (!gimple_seq_empty_p (stmts))
9292 if (before)
9293 gsi_insert_seq_before (gsi, stmts, m);
9294 else
9295 gsi_insert_seq_after (gsi, stmts, m);
9298 return expr;
9301 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
9302 If SIMPLE is true, force the result to be either ssa_name or an invariant,
9303 otherwise just force it to be a rhs expression. If some statements are
9304 produced, emits them at GSI. If BEFORE is true, the statements are
9305 appended before GSI, otherwise they are appended after it. M specifies
9306 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
9307 are the usual values). */
9309 tree
9310 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
9311 bool simple_p, tree var, bool before,
9312 enum gsi_iterator_update m)
9314 return force_gimple_operand_gsi_1 (gsi, expr,
9315 simple_p
9316 ? is_gimple_val : is_gimple_reg_rhs,
9317 var, before, m);
9320 /* Return a dummy expression of type TYPE in order to keep going after an
9321 error. */
9323 static tree
9324 dummy_object (tree type)
9326 tree t = build_int_cst (build_pointer_type (type), 0);
9327 return build2 (MEM_REF, type, t, t);
9330 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9331 builtin function, but a very special sort of operator. */
9333 enum gimplify_status
9334 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9336 tree promoted_type, have_va_type;
9337 tree valist = TREE_OPERAND (*expr_p, 0);
9338 tree type = TREE_TYPE (*expr_p);
9339 tree t;
9340 location_t loc = EXPR_LOCATION (*expr_p);
9342 /* Verify that valist is of the proper type. */
9343 have_va_type = TREE_TYPE (valist);
9344 if (have_va_type == error_mark_node)
9345 return GS_ERROR;
9346 have_va_type = targetm.canonical_va_list_type (have_va_type);
9348 if (have_va_type == NULL_TREE)
9350 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9351 return GS_ERROR;
9354 /* Generate a diagnostic for requesting data of a type that cannot
9355 be passed through `...' due to type promotion at the call site. */
9356 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9357 != type)
9359 static bool gave_help;
9360 bool warned;
9362 /* Unfortunately, this is merely undefined, rather than a constraint
9363 violation, so we cannot make this an error. If this call is never
9364 executed, the program is still strictly conforming. */
9365 warned = warning_at (loc, 0,
9366 "%qT is promoted to %qT when passed through %<...%>",
9367 type, promoted_type);
9368 if (!gave_help && warned)
9370 gave_help = true;
9371 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9372 promoted_type, type);
9375 /* We can, however, treat "undefined" any way we please.
9376 Call abort to encourage the user to fix the program. */
9377 if (warned)
9378 inform (loc, "if this code is reached, the program will abort");
9379 /* Before the abort, allow the evaluation of the va_list
9380 expression to exit or longjmp. */
9381 gimplify_and_add (valist, pre_p);
9382 t = build_call_expr_loc (loc,
9383 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9384 gimplify_and_add (t, pre_p);
9386 /* This is dead code, but go ahead and finish so that the
9387 mode of the result comes out right. */
9388 *expr_p = dummy_object (type);
9389 return GS_ALL_DONE;
9391 else
9393 /* Make it easier for the backends by protecting the valist argument
9394 from multiple evaluations. */
9395 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9397 /* For this case, the backends will be expecting a pointer to
9398 TREE_TYPE (abi), but it's possible we've
9399 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9400 So fix it. */
9401 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9403 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9404 valist = fold_convert_loc (loc, p1,
9405 build_fold_addr_expr_loc (loc, valist));
9408 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9410 else
9411 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9413 if (!targetm.gimplify_va_arg_expr)
9414 /* FIXME: Once most targets are converted we should merely
9415 assert this is non-null. */
9416 return GS_ALL_DONE;
9418 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9419 return GS_OK;
9423 #include "gt-gimplify.h"