* gcc.dg/pthread-init-2.c (dg-options): Define _XOPEN_SOURCE=500
[official-gcc.git] / gcc / gimplify.c
blobe79d06397e0672a4c298bae4365604c445344129
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "tree-pretty-print.h"
33 #include "langhooks.h"
34 #include "tree-flow.h"
35 #include "cgraph.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "diagnostic-core.h"
42 #include "target.h"
43 #include "pointer-set.h"
44 #include "splay-tree.h"
45 #include "vec.h"
46 #include "gimple.h"
48 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
49 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
51 enum gimplify_omp_var_data
53 GOVD_SEEN = 1,
54 GOVD_EXPLICIT = 2,
55 GOVD_SHARED = 4,
56 GOVD_PRIVATE = 8,
57 GOVD_FIRSTPRIVATE = 16,
58 GOVD_LASTPRIVATE = 32,
59 GOVD_REDUCTION = 64,
60 GOVD_LOCAL = 128,
61 GOVD_DEBUG_PRIVATE = 256,
62 GOVD_PRIVATE_OUTER_REF = 512,
63 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
64 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
68 enum omp_region_type
70 ORT_WORKSHARE = 0,
71 ORT_PARALLEL = 2,
72 ORT_COMBINED_PARALLEL = 3,
73 ORT_TASK = 4,
74 ORT_UNTIED_TASK = 5
77 struct gimplify_omp_ctx
79 struct gimplify_omp_ctx *outer_context;
80 splay_tree variables;
81 struct pointer_set_t *privatized_types;
82 location_t location;
83 enum omp_clause_default_kind default_kind;
84 enum omp_region_type region_type;
87 static struct gimplify_ctx *gimplify_ctxp;
88 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
91 /* Formal (expression) temporary table handling: multiple occurrences of
92 the same scalar expression are evaluated into the same temporary. */
94 typedef struct gimple_temp_hash_elt
96 tree val; /* Key */
97 tree temp; /* Value */
98 } elt_t;
100 /* Forward declaration. */
101 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
103 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
104 form and we don't do any syntax checking. */
106 void
107 mark_addressable (tree x)
109 while (handled_component_p (x))
110 x = TREE_OPERAND (x, 0);
111 if (TREE_CODE (x) == MEM_REF
112 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
113 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
114 if (TREE_CODE (x) != VAR_DECL
115 && TREE_CODE (x) != PARM_DECL
116 && TREE_CODE (x) != RESULT_DECL)
117 return;
118 TREE_ADDRESSABLE (x) = 1;
120 /* Also mark the artificial SSA_NAME that points to the partition of X. */
121 if (TREE_CODE (x) == VAR_DECL
122 && !DECL_EXTERNAL (x)
123 && !TREE_STATIC (x)
124 && cfun->gimple_df != NULL
125 && cfun->gimple_df->decls_to_pointers != NULL)
127 void *namep
128 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
129 if (namep)
130 TREE_ADDRESSABLE (*(tree *)namep) = 1;
134 /* Return a hash value for a formal temporary table entry. */
136 static hashval_t
137 gimple_tree_hash (const void *p)
139 tree t = ((const elt_t *) p)->val;
140 return iterative_hash_expr (t, 0);
143 /* Compare two formal temporary table entries. */
145 static int
146 gimple_tree_eq (const void *p1, const void *p2)
148 tree t1 = ((const elt_t *) p1)->val;
149 tree t2 = ((const elt_t *) p2)->val;
150 enum tree_code code = TREE_CODE (t1);
152 if (TREE_CODE (t2) != code
153 || TREE_TYPE (t1) != TREE_TYPE (t2))
154 return 0;
156 if (!operand_equal_p (t1, t2, 0))
157 return 0;
159 #ifdef ENABLE_CHECKING
160 /* Only allow them to compare equal if they also hash equal; otherwise
161 results are nondeterminate, and we fail bootstrap comparison. */
162 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
163 #endif
165 return 1;
168 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
169 *SEQ_P is NULL, a new sequence is allocated. This function is
170 similar to gimple_seq_add_stmt, but does not scan the operands.
171 During gimplification, we need to manipulate statement sequences
172 before the def/use vectors have been constructed. */
174 void
175 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
177 gimple_stmt_iterator si;
179 if (gs == NULL)
180 return;
182 si = gsi_last (*seq_p);
183 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
186 /* Shorter alias name for the above function for use in gimplify.c
187 only. */
189 static inline void
190 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
192 gimple_seq_add_stmt_without_update (seq_p, gs);
195 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
196 NULL, a new sequence is allocated. This function is
197 similar to gimple_seq_add_seq, but does not scan the operands.
198 During gimplification, we need to manipulate statement sequences
199 before the def/use vectors have been constructed. */
201 static void
202 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
204 gimple_stmt_iterator si;
206 if (src == NULL)
207 return;
209 si = gsi_last (*dst_p);
210 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
213 /* Set up a context for the gimplifier. */
215 void
216 push_gimplify_context (struct gimplify_ctx *c)
218 memset (c, '\0', sizeof (*c));
219 c->prev_context = gimplify_ctxp;
220 gimplify_ctxp = c;
223 /* Tear down a context for the gimplifier. If BODY is non-null, then
224 put the temporaries into the outer BIND_EXPR. Otherwise, put them
225 in the local_decls.
227 BODY is not a sequence, but the first tuple in a sequence. */
229 void
230 pop_gimplify_context (gimple body)
232 struct gimplify_ctx *c = gimplify_ctxp;
234 gcc_assert (c
235 && (!c->bind_expr_stack.exists ()
236 || c->bind_expr_stack.is_empty ()));
237 c->bind_expr_stack.release ();
238 gimplify_ctxp = c->prev_context;
240 if (body)
241 declare_vars (c->temps, body, false);
242 else
243 record_vars (c->temps);
245 if (c->temp_htab)
246 htab_delete (c->temp_htab);
249 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
251 static void
252 gimple_push_bind_expr (gimple gimple_bind)
254 gimplify_ctxp->bind_expr_stack.reserve (8);
255 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
258 /* Pop the first element off the stack of bindings. */
260 static void
261 gimple_pop_bind_expr (void)
263 gimplify_ctxp->bind_expr_stack.pop ();
266 /* Return the first element of the stack of bindings. */
268 gimple
269 gimple_current_bind_expr (void)
271 return gimplify_ctxp->bind_expr_stack.last ();
274 /* Return the stack of bindings created during gimplification. */
276 vec<gimple>
277 gimple_bind_expr_stack (void)
279 return gimplify_ctxp->bind_expr_stack;
282 /* Return true iff there is a COND_EXPR between us and the innermost
283 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
285 static bool
286 gimple_conditional_context (void)
288 return gimplify_ctxp->conditions > 0;
291 /* Note that we've entered a COND_EXPR. */
293 static void
294 gimple_push_condition (void)
296 #ifdef ENABLE_GIMPLE_CHECKING
297 if (gimplify_ctxp->conditions == 0)
298 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
299 #endif
300 ++(gimplify_ctxp->conditions);
303 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
304 now, add any conditional cleanups we've seen to the prequeue. */
306 static void
307 gimple_pop_condition (gimple_seq *pre_p)
309 int conds = --(gimplify_ctxp->conditions);
311 gcc_assert (conds >= 0);
312 if (conds == 0)
314 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
315 gimplify_ctxp->conditional_cleanups = NULL;
319 /* A stable comparison routine for use with splay trees and DECLs. */
321 static int
322 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
324 tree a = (tree) xa;
325 tree b = (tree) xb;
327 return DECL_UID (a) - DECL_UID (b);
330 /* Create a new omp construct that deals with variable remapping. */
332 static struct gimplify_omp_ctx *
333 new_omp_context (enum omp_region_type region_type)
335 struct gimplify_omp_ctx *c;
337 c = XCNEW (struct gimplify_omp_ctx);
338 c->outer_context = gimplify_omp_ctxp;
339 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
340 c->privatized_types = pointer_set_create ();
341 c->location = input_location;
342 c->region_type = region_type;
343 if ((region_type & ORT_TASK) == 0)
344 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
345 else
346 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
348 return c;
351 /* Destroy an omp construct that deals with variable remapping. */
353 static void
354 delete_omp_context (struct gimplify_omp_ctx *c)
356 splay_tree_delete (c->variables);
357 pointer_set_destroy (c->privatized_types);
358 XDELETE (c);
361 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
362 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
364 /* Both gimplify the statement T and append it to *SEQ_P. This function
365 behaves exactly as gimplify_stmt, but you don't have to pass T as a
366 reference. */
368 void
369 gimplify_and_add (tree t, gimple_seq *seq_p)
371 gimplify_stmt (&t, seq_p);
374 /* Gimplify statement T into sequence *SEQ_P, and return the first
375 tuple in the sequence of generated tuples for this statement.
376 Return NULL if gimplifying T produced no tuples. */
378 static gimple
379 gimplify_and_return_first (tree t, gimple_seq *seq_p)
381 gimple_stmt_iterator last = gsi_last (*seq_p);
383 gimplify_and_add (t, seq_p);
385 if (!gsi_end_p (last))
387 gsi_next (&last);
388 return gsi_stmt (last);
390 else
391 return gimple_seq_first_stmt (*seq_p);
394 /* Strip off a legitimate source ending from the input string NAME of
395 length LEN. Rather than having to know the names used by all of
396 our front ends, we strip off an ending of a period followed by
397 up to five characters. (Java uses ".class".) */
399 static inline void
400 remove_suffix (char *name, int len)
402 int i;
404 for (i = 2; i < 8 && len > i; i++)
406 if (name[len - i] == '.')
408 name[len - i] = '\0';
409 break;
414 /* Create a new temporary name with PREFIX. Return an identifier. */
416 static GTY(()) unsigned int tmp_var_id_num;
418 tree
419 create_tmp_var_name (const char *prefix)
421 char *tmp_name;
423 if (prefix)
425 char *preftmp = ASTRDUP (prefix);
427 remove_suffix (preftmp, strlen (preftmp));
428 clean_symbol_name (preftmp);
430 prefix = preftmp;
433 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
434 return get_identifier (tmp_name);
437 /* Create a new temporary variable declaration of type TYPE.
438 Do NOT push it into the current binding. */
440 tree
441 create_tmp_var_raw (tree type, const char *prefix)
443 tree tmp_var;
445 tmp_var = build_decl (input_location,
446 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
447 type);
449 /* The variable was declared by the compiler. */
450 DECL_ARTIFICIAL (tmp_var) = 1;
451 /* And we don't want debug info for it. */
452 DECL_IGNORED_P (tmp_var) = 1;
454 /* Make the variable writable. */
455 TREE_READONLY (tmp_var) = 0;
457 DECL_EXTERNAL (tmp_var) = 0;
458 TREE_STATIC (tmp_var) = 0;
459 TREE_USED (tmp_var) = 1;
461 return tmp_var;
464 /* Create a new temporary variable declaration of type TYPE. DO push the
465 variable into the current binding. Further, assume that this is called
466 only from gimplification or optimization, at which point the creation of
467 certain types are bugs. */
469 tree
470 create_tmp_var (tree type, const char *prefix)
472 tree tmp_var;
474 /* We don't allow types that are addressable (meaning we can't make copies),
475 or incomplete. We also used to reject every variable size objects here,
476 but now support those for which a constant upper bound can be obtained.
477 The processing for variable sizes is performed in gimple_add_tmp_var,
478 point at which it really matters and possibly reached via paths not going
479 through this function, e.g. after direct calls to create_tmp_var_raw. */
480 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
482 tmp_var = create_tmp_var_raw (type, prefix);
483 gimple_add_tmp_var (tmp_var);
484 return tmp_var;
487 /* Create a new temporary variable declaration of type TYPE by calling
488 create_tmp_var and if TYPE is a vector or a complex number, mark the new
489 temporary as gimple register. */
491 tree
492 create_tmp_reg (tree type, const char *prefix)
494 tree tmp;
496 tmp = create_tmp_var (type, prefix);
497 if (TREE_CODE (type) == COMPLEX_TYPE
498 || TREE_CODE (type) == VECTOR_TYPE)
499 DECL_GIMPLE_REG_P (tmp) = 1;
501 return tmp;
504 /* Returns true iff T is a valid RHS for an assignment to a renamed
505 user -- or front-end generated artificial -- variable. */
507 static bool
508 is_gimple_reg_rhs (tree t)
510 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
513 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
514 LHS, or for a call argument. */
516 static bool
517 is_gimple_mem_rhs (tree t)
519 /* If we're dealing with a renamable type, either source or dest must be
520 a renamed variable. */
521 if (is_gimple_reg_type (TREE_TYPE (t)))
522 return is_gimple_val (t);
523 else
524 return is_gimple_val (t) || is_gimple_lvalue (t);
527 /* Return true if T is a CALL_EXPR or an expression that can be
528 assigned to a temporary. Note that this predicate should only be
529 used during gimplification. See the rationale for this in
530 gimplify_modify_expr. */
532 static bool
533 is_gimple_reg_rhs_or_call (tree t)
535 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
536 || TREE_CODE (t) == CALL_EXPR);
539 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
540 this predicate should only be used during gimplification. See the
541 rationale for this in gimplify_modify_expr. */
543 static bool
544 is_gimple_mem_rhs_or_call (tree t)
546 /* If we're dealing with a renamable type, either source or dest must be
547 a renamed variable. */
548 if (is_gimple_reg_type (TREE_TYPE (t)))
549 return is_gimple_val (t);
550 else
551 return (is_gimple_val (t) || is_gimple_lvalue (t)
552 || TREE_CODE (t) == CALL_EXPR);
555 /* Create a temporary with a name derived from VAL. Subroutine of
556 lookup_tmp_var; nobody else should call this function. */
558 static inline tree
559 create_tmp_from_val (tree val, bool is_formal)
561 /* Drop all qualifiers and address-space information from the value type. */
562 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
563 tree var = create_tmp_var (type, get_name (val));
564 if (is_formal
565 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
566 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
567 DECL_GIMPLE_REG_P (var) = 1;
568 return var;
571 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
572 an existing expression temporary. */
574 static tree
575 lookup_tmp_var (tree val, bool is_formal)
577 tree ret;
579 /* If not optimizing, never really reuse a temporary. local-alloc
580 won't allocate any variable that is used in more than one basic
581 block, which means it will go into memory, causing much extra
582 work in reload and final and poorer code generation, outweighing
583 the extra memory allocation here. */
584 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
585 ret = create_tmp_from_val (val, is_formal);
586 else
588 elt_t elt, *elt_p;
589 void **slot;
591 elt.val = val;
592 if (gimplify_ctxp->temp_htab == NULL)
593 gimplify_ctxp->temp_htab
594 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
595 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
596 if (*slot == NULL)
598 elt_p = XNEW (elt_t);
599 elt_p->val = val;
600 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
601 *slot = (void *) elt_p;
603 else
605 elt_p = (elt_t *) *slot;
606 ret = elt_p->temp;
610 return ret;
613 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
615 static tree
616 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
617 bool is_formal)
619 tree t, mod;
621 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
622 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
623 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
624 fb_rvalue);
626 if (gimplify_ctxp->into_ssa
627 && is_gimple_reg_type (TREE_TYPE (val)))
628 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
629 else
630 t = lookup_tmp_var (val, is_formal);
632 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
634 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
636 /* gimplify_modify_expr might want to reduce this further. */
637 gimplify_and_add (mod, pre_p);
638 ggc_free (mod);
640 return t;
643 /* Return a formal temporary variable initialized with VAL. PRE_P is as
644 in gimplify_expr. Only use this function if:
646 1) The value of the unfactored expression represented by VAL will not
647 change between the initialization and use of the temporary, and
648 2) The temporary will not be otherwise modified.
650 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
651 and #2 means it is inappropriate for && temps.
653 For other cases, use get_initialized_tmp_var instead. */
655 tree
656 get_formal_tmp_var (tree val, gimple_seq *pre_p)
658 return internal_get_tmp_var (val, pre_p, NULL, true);
661 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
662 are as in gimplify_expr. */
664 tree
665 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
667 return internal_get_tmp_var (val, pre_p, post_p, false);
670 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
671 generate debug info for them; otherwise don't. */
673 void
674 declare_vars (tree vars, gimple scope, bool debug_info)
676 tree last = vars;
677 if (last)
679 tree temps, block;
681 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
683 temps = nreverse (last);
685 block = gimple_bind_block (scope);
686 gcc_assert (!block || TREE_CODE (block) == BLOCK);
687 if (!block || !debug_info)
689 DECL_CHAIN (last) = gimple_bind_vars (scope);
690 gimple_bind_set_vars (scope, temps);
692 else
694 /* We need to attach the nodes both to the BIND_EXPR and to its
695 associated BLOCK for debugging purposes. The key point here
696 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
697 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
698 if (BLOCK_VARS (block))
699 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
700 else
702 gimple_bind_set_vars (scope,
703 chainon (gimple_bind_vars (scope), temps));
704 BLOCK_VARS (block) = temps;
710 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
711 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
712 no such upper bound can be obtained. */
714 static void
715 force_constant_size (tree var)
717 /* The only attempt we make is by querying the maximum size of objects
718 of the variable's type. */
720 HOST_WIDE_INT max_size;
722 gcc_assert (TREE_CODE (var) == VAR_DECL);
724 max_size = max_int_size_in_bytes (TREE_TYPE (var));
726 gcc_assert (max_size >= 0);
728 DECL_SIZE_UNIT (var)
729 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
730 DECL_SIZE (var)
731 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
734 /* Push the temporary variable TMP into the current binding. */
736 void
737 gimple_add_tmp_var (tree tmp)
739 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
741 /* Later processing assumes that the object size is constant, which might
742 not be true at this point. Force the use of a constant upper bound in
743 this case. */
744 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
745 force_constant_size (tmp);
747 DECL_CONTEXT (tmp) = current_function_decl;
748 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
750 if (gimplify_ctxp)
752 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
753 gimplify_ctxp->temps = tmp;
755 /* Mark temporaries local within the nearest enclosing parallel. */
756 if (gimplify_omp_ctxp)
758 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
759 while (ctx && ctx->region_type == ORT_WORKSHARE)
760 ctx = ctx->outer_context;
761 if (ctx)
762 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
765 else if (cfun)
766 record_vars (tmp);
767 else
769 gimple_seq body_seq;
771 /* This case is for nested functions. We need to expose the locals
772 they create. */
773 body_seq = gimple_body (current_function_decl);
774 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
778 /* Determine whether to assign a location to the statement GS. */
780 static bool
781 should_carry_location_p (gimple gs)
783 /* Don't emit a line note for a label. We particularly don't want to
784 emit one for the break label, since it doesn't actually correspond
785 to the beginning of the loop/switch. */
786 if (gimple_code (gs) == GIMPLE_LABEL)
787 return false;
789 return true;
792 /* Return true if a location should not be emitted for this statement
793 by annotate_one_with_location. */
795 static inline bool
796 gimple_do_not_emit_location_p (gimple g)
798 return gimple_plf (g, GF_PLF_1);
801 /* Mark statement G so a location will not be emitted by
802 annotate_one_with_location. */
804 static inline void
805 gimple_set_do_not_emit_location (gimple g)
807 /* The PLF flags are initialized to 0 when a new tuple is created,
808 so no need to initialize it anywhere. */
809 gimple_set_plf (g, GF_PLF_1, true);
812 /* Set the location for gimple statement GS to LOCATION. */
814 static void
815 annotate_one_with_location (gimple gs, location_t location)
817 if (!gimple_has_location (gs)
818 && !gimple_do_not_emit_location_p (gs)
819 && should_carry_location_p (gs))
820 gimple_set_location (gs, location);
823 /* Set LOCATION for all the statements after iterator GSI in sequence
824 SEQ. If GSI is pointing to the end of the sequence, start with the
825 first statement in SEQ. */
827 static void
828 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
829 location_t location)
831 if (gsi_end_p (gsi))
832 gsi = gsi_start (seq);
833 else
834 gsi_next (&gsi);
836 for (; !gsi_end_p (gsi); gsi_next (&gsi))
837 annotate_one_with_location (gsi_stmt (gsi), location);
840 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
842 void
843 annotate_all_with_location (gimple_seq stmt_p, location_t location)
845 gimple_stmt_iterator i;
847 if (gimple_seq_empty_p (stmt_p))
848 return;
850 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
852 gimple gs = gsi_stmt (i);
853 annotate_one_with_location (gs, location);
857 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
858 nodes that are referenced more than once in GENERIC functions. This is
859 necessary because gimplification (translation into GIMPLE) is performed
860 by modifying tree nodes in-place, so gimplication of a shared node in a
861 first context could generate an invalid GIMPLE form in a second context.
863 This is achieved with a simple mark/copy/unmark algorithm that walks the
864 GENERIC representation top-down, marks nodes with TREE_VISITED the first
865 time it encounters them, duplicates them if they already have TREE_VISITED
866 set, and finally removes the TREE_VISITED marks it has set.
868 The algorithm works only at the function level, i.e. it generates a GENERIC
869 representation of a function with no nodes shared within the function when
870 passed a GENERIC function (except for nodes that are allowed to be shared).
872 At the global level, it is also necessary to unshare tree nodes that are
873 referenced in more than one function, for the same aforementioned reason.
874 This requires some cooperation from the front-end. There are 2 strategies:
876 1. Manual unsharing. The front-end needs to call unshare_expr on every
877 expression that might end up being shared across functions.
879 2. Deep unsharing. This is an extension of regular unsharing. Instead
880 of calling unshare_expr on expressions that might be shared across
881 functions, the front-end pre-marks them with TREE_VISITED. This will
882 ensure that they are unshared on the first reference within functions
883 when the regular unsharing algorithm runs. The counterpart is that
884 this algorithm must look deeper than for manual unsharing, which is
885 specified by LANG_HOOKS_DEEP_UNSHARING.
887 If there are only few specific cases of node sharing across functions, it is
888 probably easier for a front-end to unshare the expressions manually. On the
889 contrary, if the expressions generated at the global level are as widespread
890 as expressions generated within functions, deep unsharing is very likely the
891 way to go. */
893 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
894 These nodes model computations that must be done once. If we were to
895 unshare something like SAVE_EXPR(i++), the gimplification process would
896 create wrong code. However, if DATA is non-null, it must hold a pointer
897 set that is used to unshare the subtrees of these nodes. */
899 static tree
900 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
902 tree t = *tp;
903 enum tree_code code = TREE_CODE (t);
905 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
906 copy their subtrees if we can make sure to do it only once. */
907 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
909 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
911 else
912 *walk_subtrees = 0;
915 /* Stop at types, decls, constants like copy_tree_r. */
916 else if (TREE_CODE_CLASS (code) == tcc_type
917 || TREE_CODE_CLASS (code) == tcc_declaration
918 || TREE_CODE_CLASS (code) == tcc_constant
919 /* We can't do anything sensible with a BLOCK used as an
920 expression, but we also can't just die when we see it
921 because of non-expression uses. So we avert our eyes
922 and cross our fingers. Silly Java. */
923 || code == BLOCK)
924 *walk_subtrees = 0;
926 /* Cope with the statement expression extension. */
927 else if (code == STATEMENT_LIST)
930 /* Leave the bulk of the work to copy_tree_r itself. */
931 else
932 copy_tree_r (tp, walk_subtrees, NULL);
934 return NULL_TREE;
937 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
938 If *TP has been visited already, then *TP is deeply copied by calling
939 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
941 static tree
942 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
944 tree t = *tp;
945 enum tree_code code = TREE_CODE (t);
947 /* Skip types, decls, and constants. But we do want to look at their
948 types and the bounds of types. Mark them as visited so we properly
949 unmark their subtrees on the unmark pass. If we've already seen them,
950 don't look down further. */
951 if (TREE_CODE_CLASS (code) == tcc_type
952 || TREE_CODE_CLASS (code) == tcc_declaration
953 || TREE_CODE_CLASS (code) == tcc_constant)
955 if (TREE_VISITED (t))
956 *walk_subtrees = 0;
957 else
958 TREE_VISITED (t) = 1;
961 /* If this node has been visited already, unshare it and don't look
962 any deeper. */
963 else if (TREE_VISITED (t))
965 walk_tree (tp, mostly_copy_tree_r, data, NULL);
966 *walk_subtrees = 0;
969 /* Otherwise, mark the node as visited and keep looking. */
970 else
971 TREE_VISITED (t) = 1;
973 return NULL_TREE;
976 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
977 copy_if_shared_r callback unmodified. */
979 static inline void
980 copy_if_shared (tree *tp, void *data)
982 walk_tree (tp, copy_if_shared_r, data, NULL);
985 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
986 any nested functions. */
988 static void
989 unshare_body (tree fndecl)
991 struct cgraph_node *cgn = cgraph_get_node (fndecl);
992 /* If the language requires deep unsharing, we need a pointer set to make
993 sure we don't repeatedly unshare subtrees of unshareable nodes. */
994 struct pointer_set_t *visited
995 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
997 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
998 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
999 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1001 if (visited)
1002 pointer_set_destroy (visited);
1004 if (cgn)
1005 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1006 unshare_body (cgn->symbol.decl);
1009 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1010 Subtrees are walked until the first unvisited node is encountered. */
1012 static tree
1013 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1015 tree t = *tp;
1017 /* If this node has been visited, unmark it and keep looking. */
1018 if (TREE_VISITED (t))
1019 TREE_VISITED (t) = 0;
1021 /* Otherwise, don't look any deeper. */
1022 else
1023 *walk_subtrees = 0;
1025 return NULL_TREE;
1028 /* Unmark the visited trees rooted at *TP. */
1030 static inline void
1031 unmark_visited (tree *tp)
1033 walk_tree (tp, unmark_visited_r, NULL, NULL);
1036 /* Likewise, but mark all trees as not visited. */
1038 static void
1039 unvisit_body (tree fndecl)
1041 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1043 unmark_visited (&DECL_SAVED_TREE (fndecl));
1044 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1045 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1047 if (cgn)
1048 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1049 unvisit_body (cgn->symbol.decl);
1052 /* Unconditionally make an unshared copy of EXPR. This is used when using
1053 stored expressions which span multiple functions, such as BINFO_VTABLE,
1054 as the normal unsharing process can't tell that they're shared. */
1056 tree
1057 unshare_expr (tree expr)
1059 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1060 return expr;
1063 /* Worker for unshare_expr_without_location. */
1065 static tree
1066 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1068 if (EXPR_P (*tp))
1069 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1070 else
1071 *walk_subtrees = 0;
1072 return NULL_TREE;
1075 /* Similar to unshare_expr but also prune all expression locations
1076 from EXPR. */
1078 tree
1079 unshare_expr_without_location (tree expr)
1081 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1082 if (EXPR_P (expr))
1083 walk_tree (&expr, prune_expr_location, NULL, NULL);
1084 return expr;
1087 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1088 contain statements and have a value. Assign its value to a temporary
1089 and give it void_type_node. Return the temporary, or NULL_TREE if
1090 WRAPPER was already void. */
1092 tree
1093 voidify_wrapper_expr (tree wrapper, tree temp)
1095 tree type = TREE_TYPE (wrapper);
1096 if (type && !VOID_TYPE_P (type))
1098 tree *p;
1100 /* Set p to point to the body of the wrapper. Loop until we find
1101 something that isn't a wrapper. */
1102 for (p = &wrapper; p && *p; )
1104 switch (TREE_CODE (*p))
1106 case BIND_EXPR:
1107 TREE_SIDE_EFFECTS (*p) = 1;
1108 TREE_TYPE (*p) = void_type_node;
1109 /* For a BIND_EXPR, the body is operand 1. */
1110 p = &BIND_EXPR_BODY (*p);
1111 break;
1113 case CLEANUP_POINT_EXPR:
1114 case TRY_FINALLY_EXPR:
1115 case TRY_CATCH_EXPR:
1116 TREE_SIDE_EFFECTS (*p) = 1;
1117 TREE_TYPE (*p) = void_type_node;
1118 p = &TREE_OPERAND (*p, 0);
1119 break;
1121 case STATEMENT_LIST:
1123 tree_stmt_iterator i = tsi_last (*p);
1124 TREE_SIDE_EFFECTS (*p) = 1;
1125 TREE_TYPE (*p) = void_type_node;
1126 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1128 break;
1130 case COMPOUND_EXPR:
1131 /* Advance to the last statement. Set all container types to
1132 void. */
1133 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1135 TREE_SIDE_EFFECTS (*p) = 1;
1136 TREE_TYPE (*p) = void_type_node;
1138 break;
1140 case TRANSACTION_EXPR:
1141 TREE_SIDE_EFFECTS (*p) = 1;
1142 TREE_TYPE (*p) = void_type_node;
1143 p = &TRANSACTION_EXPR_BODY (*p);
1144 break;
1146 default:
1147 /* Assume that any tree upon which voidify_wrapper_expr is
1148 directly called is a wrapper, and that its body is op0. */
1149 if (p == &wrapper)
1151 TREE_SIDE_EFFECTS (*p) = 1;
1152 TREE_TYPE (*p) = void_type_node;
1153 p = &TREE_OPERAND (*p, 0);
1154 break;
1156 goto out;
1160 out:
1161 if (p == NULL || IS_EMPTY_STMT (*p))
1162 temp = NULL_TREE;
1163 else if (temp)
1165 /* The wrapper is on the RHS of an assignment that we're pushing
1166 down. */
1167 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1168 || TREE_CODE (temp) == MODIFY_EXPR);
1169 TREE_OPERAND (temp, 1) = *p;
1170 *p = temp;
1172 else
1174 temp = create_tmp_var (type, "retval");
1175 *p = build2 (INIT_EXPR, type, temp, *p);
1178 return temp;
1181 return NULL_TREE;
1184 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1185 a temporary through which they communicate. */
1187 static void
1188 build_stack_save_restore (gimple *save, gimple *restore)
1190 tree tmp_var;
1192 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1193 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1194 gimple_call_set_lhs (*save, tmp_var);
1196 *restore
1197 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1198 1, tmp_var);
1201 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1203 static enum gimplify_status
1204 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1206 tree bind_expr = *expr_p;
1207 bool old_save_stack = gimplify_ctxp->save_stack;
1208 tree t;
1209 gimple gimple_bind;
1210 gimple_seq body, cleanup;
1211 gimple stack_save;
1213 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1215 /* Mark variables seen in this bind expr. */
1216 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1218 if (TREE_CODE (t) == VAR_DECL)
1220 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1222 /* Mark variable as local. */
1223 if (ctx && !DECL_EXTERNAL (t)
1224 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1225 || splay_tree_lookup (ctx->variables,
1226 (splay_tree_key) t) == NULL))
1227 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1229 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1231 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1232 cfun->has_local_explicit_reg_vars = true;
1235 /* Preliminarily mark non-addressed complex variables as eligible
1236 for promotion to gimple registers. We'll transform their uses
1237 as we find them. */
1238 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1239 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1240 && !TREE_THIS_VOLATILE (t)
1241 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1242 && !needs_to_live_in_memory (t))
1243 DECL_GIMPLE_REG_P (t) = 1;
1246 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1247 BIND_EXPR_BLOCK (bind_expr));
1248 gimple_push_bind_expr (gimple_bind);
1250 gimplify_ctxp->save_stack = false;
1252 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1253 body = NULL;
1254 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1255 gimple_bind_set_body (gimple_bind, body);
1257 cleanup = NULL;
1258 stack_save = NULL;
1259 if (gimplify_ctxp->save_stack)
1261 gimple stack_restore;
1263 /* Save stack on entry and restore it on exit. Add a try_finally
1264 block to achieve this. Note that mudflap depends on the
1265 format of the emitted code: see mx_register_decls(). */
1266 build_stack_save_restore (&stack_save, &stack_restore);
1268 gimplify_seq_add_stmt (&cleanup, stack_restore);
1271 /* Add clobbers for all variables that go out of scope. */
1272 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1274 if (TREE_CODE (t) == VAR_DECL
1275 && !is_global_var (t)
1276 && DECL_CONTEXT (t) == current_function_decl
1277 && !DECL_HARD_REGISTER (t)
1278 && !TREE_THIS_VOLATILE (t)
1279 && !DECL_HAS_VALUE_EXPR_P (t)
1280 /* Only care for variables that have to be in memory. Others
1281 will be rewritten into SSA names, hence moved to the top-level. */
1282 && !is_gimple_reg (t)
1283 && flag_stack_reuse != SR_NONE)
1285 tree clobber = build_constructor (TREE_TYPE (t),
1286 NULL);
1287 TREE_THIS_VOLATILE (clobber) = 1;
1288 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1292 if (cleanup)
1294 gimple gs;
1295 gimple_seq new_body;
1297 new_body = NULL;
1298 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1299 GIMPLE_TRY_FINALLY);
1301 if (stack_save)
1302 gimplify_seq_add_stmt (&new_body, stack_save);
1303 gimplify_seq_add_stmt (&new_body, gs);
1304 gimple_bind_set_body (gimple_bind, new_body);
1307 gimplify_ctxp->save_stack = old_save_stack;
1308 gimple_pop_bind_expr ();
1310 gimplify_seq_add_stmt (pre_p, gimple_bind);
1312 if (temp)
1314 *expr_p = temp;
1315 return GS_OK;
1318 *expr_p = NULL_TREE;
1319 return GS_ALL_DONE;
1322 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1323 GIMPLE value, it is assigned to a new temporary and the statement is
1324 re-written to return the temporary.
1326 PRE_P points to the sequence where side effects that must happen before
1327 STMT should be stored. */
1329 static enum gimplify_status
1330 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1332 gimple ret;
1333 tree ret_expr = TREE_OPERAND (stmt, 0);
1334 tree result_decl, result;
1336 if (ret_expr == error_mark_node)
1337 return GS_ERROR;
1339 if (!ret_expr
1340 || TREE_CODE (ret_expr) == RESULT_DECL
1341 || ret_expr == error_mark_node)
1343 gimple ret = gimple_build_return (ret_expr);
1344 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1345 gimplify_seq_add_stmt (pre_p, ret);
1346 return GS_ALL_DONE;
1349 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1350 result_decl = NULL_TREE;
1351 else
1353 result_decl = TREE_OPERAND (ret_expr, 0);
1355 /* See through a return by reference. */
1356 if (TREE_CODE (result_decl) == INDIRECT_REF)
1357 result_decl = TREE_OPERAND (result_decl, 0);
1359 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1360 || TREE_CODE (ret_expr) == INIT_EXPR)
1361 && TREE_CODE (result_decl) == RESULT_DECL);
1364 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1365 Recall that aggregate_value_p is FALSE for any aggregate type that is
1366 returned in registers. If we're returning values in registers, then
1367 we don't want to extend the lifetime of the RESULT_DECL, particularly
1368 across another call. In addition, for those aggregates for which
1369 hard_function_value generates a PARALLEL, we'll die during normal
1370 expansion of structure assignments; there's special code in expand_return
1371 to handle this case that does not exist in expand_expr. */
1372 if (!result_decl)
1373 result = NULL_TREE;
1374 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1376 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1378 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1379 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1380 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1381 should be effectively allocated by the caller, i.e. all calls to
1382 this function must be subject to the Return Slot Optimization. */
1383 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1384 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1386 result = result_decl;
1388 else if (gimplify_ctxp->return_temp)
1389 result = gimplify_ctxp->return_temp;
1390 else
1392 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1394 /* ??? With complex control flow (usually involving abnormal edges),
1395 we can wind up warning about an uninitialized value for this. Due
1396 to how this variable is constructed and initialized, this is never
1397 true. Give up and never warn. */
1398 TREE_NO_WARNING (result) = 1;
1400 gimplify_ctxp->return_temp = result;
1403 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1404 Then gimplify the whole thing. */
1405 if (result != result_decl)
1406 TREE_OPERAND (ret_expr, 0) = result;
1408 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1410 ret = gimple_build_return (result);
1411 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1412 gimplify_seq_add_stmt (pre_p, ret);
1414 return GS_ALL_DONE;
1417 /* Gimplify a variable-length array DECL. */
1419 static void
1420 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1422 /* This is a variable-sized decl. Simplify its size and mark it
1423 for deferred expansion. Note that mudflap depends on the format
1424 of the emitted code: see mx_register_decls(). */
1425 tree t, addr, ptr_type;
1427 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1428 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1430 /* All occurrences of this decl in final gimplified code will be
1431 replaced by indirection. Setting DECL_VALUE_EXPR does two
1432 things: First, it lets the rest of the gimplifier know what
1433 replacement to use. Second, it lets the debug info know
1434 where to find the value. */
1435 ptr_type = build_pointer_type (TREE_TYPE (decl));
1436 addr = create_tmp_var (ptr_type, get_name (decl));
1437 DECL_IGNORED_P (addr) = 0;
1438 t = build_fold_indirect_ref (addr);
1439 TREE_THIS_NOTRAP (t) = 1;
1440 SET_DECL_VALUE_EXPR (decl, t);
1441 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1443 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1444 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1445 size_int (DECL_ALIGN (decl)));
1446 /* The call has been built for a variable-sized object. */
1447 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1448 t = fold_convert (ptr_type, t);
1449 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1451 gimplify_and_add (t, seq_p);
1453 /* Indicate that we need to restore the stack level when the
1454 enclosing BIND_EXPR is exited. */
1455 gimplify_ctxp->save_stack = true;
1458 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1459 and initialization explicit. */
1461 static enum gimplify_status
1462 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1464 tree stmt = *stmt_p;
1465 tree decl = DECL_EXPR_DECL (stmt);
1467 *stmt_p = NULL_TREE;
1469 if (TREE_TYPE (decl) == error_mark_node)
1470 return GS_ERROR;
1472 if ((TREE_CODE (decl) == TYPE_DECL
1473 || TREE_CODE (decl) == VAR_DECL)
1474 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1475 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1477 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1478 in case its size expressions contain problematic nodes like CALL_EXPR. */
1479 if (TREE_CODE (decl) == TYPE_DECL
1480 && DECL_ORIGINAL_TYPE (decl)
1481 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1482 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1484 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1486 tree init = DECL_INITIAL (decl);
1488 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1489 || (!TREE_STATIC (decl)
1490 && flag_stack_check == GENERIC_STACK_CHECK
1491 && compare_tree_int (DECL_SIZE_UNIT (decl),
1492 STACK_CHECK_MAX_VAR_SIZE) > 0))
1493 gimplify_vla_decl (decl, seq_p);
1495 /* Some front ends do not explicitly declare all anonymous
1496 artificial variables. We compensate here by declaring the
1497 variables, though it would be better if the front ends would
1498 explicitly declare them. */
1499 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1500 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1501 gimple_add_tmp_var (decl);
1503 if (init && init != error_mark_node)
1505 if (!TREE_STATIC (decl))
1507 DECL_INITIAL (decl) = NULL_TREE;
1508 init = build2 (INIT_EXPR, void_type_node, decl, init);
1509 gimplify_and_add (init, seq_p);
1510 ggc_free (init);
1512 else
1513 /* We must still examine initializers for static variables
1514 as they may contain a label address. */
1515 walk_tree (&init, force_labels_r, NULL, NULL);
1519 return GS_ALL_DONE;
1522 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1523 and replacing the LOOP_EXPR with goto, but if the loop contains an
1524 EXIT_EXPR, we need to append a label for it to jump to. */
1526 static enum gimplify_status
1527 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1529 tree saved_label = gimplify_ctxp->exit_label;
1530 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1532 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1534 gimplify_ctxp->exit_label = NULL_TREE;
1536 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1538 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1540 if (gimplify_ctxp->exit_label)
1541 gimplify_seq_add_stmt (pre_p,
1542 gimple_build_label (gimplify_ctxp->exit_label));
1544 gimplify_ctxp->exit_label = saved_label;
1546 *expr_p = NULL;
1547 return GS_ALL_DONE;
1550 /* Gimplify a statement list onto a sequence. These may be created either
1551 by an enlightened front-end, or by shortcut_cond_expr. */
1553 static enum gimplify_status
1554 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1556 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1558 tree_stmt_iterator i = tsi_start (*expr_p);
1560 while (!tsi_end_p (i))
1562 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1563 tsi_delink (&i);
1566 if (temp)
1568 *expr_p = temp;
1569 return GS_OK;
1572 return GS_ALL_DONE;
1575 /* Compare two case labels. Because the front end should already have
1576 made sure that case ranges do not overlap, it is enough to only compare
1577 the CASE_LOW values of each case label. */
1579 static int
1580 compare_case_labels (const void *p1, const void *p2)
1582 const_tree const case1 = *(const_tree const*)p1;
1583 const_tree const case2 = *(const_tree const*)p2;
1585 /* The 'default' case label always goes first. */
1586 if (!CASE_LOW (case1))
1587 return -1;
1588 else if (!CASE_LOW (case2))
1589 return 1;
1590 else
1591 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1594 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1596 void
1597 sort_case_labels (vec<tree> label_vec)
1599 label_vec.qsort (compare_case_labels);
1602 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1604 LABELS is a vector that contains all case labels to look at.
1606 INDEX_TYPE is the type of the switch index expression. Case labels
1607 in LABELS are discarded if their values are not in the value range
1608 covered by INDEX_TYPE. The remaining case label values are folded
1609 to INDEX_TYPE.
1611 If a default case exists in LABELS, it is removed from LABELS and
1612 returned in DEFAULT_CASEP. If no default case exists, but the
1613 case labels already cover the whole range of INDEX_TYPE, a default
1614 case is returned pointing to one of the existing case labels.
1615 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1617 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1618 apply and no action is taken regardless of whether a default case is
1619 found or not. */
1621 void
1622 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1623 tree index_type,
1624 tree *default_casep)
1626 tree min_value, max_value;
1627 tree default_case = NULL_TREE;
1628 size_t i, len;
1630 i = 0;
1631 min_value = TYPE_MIN_VALUE (index_type);
1632 max_value = TYPE_MAX_VALUE (index_type);
1633 while (i < labels.length ())
1635 tree elt = labels[i];
1636 tree low = CASE_LOW (elt);
1637 tree high = CASE_HIGH (elt);
1638 bool remove_element = FALSE;
1640 if (low)
1642 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1643 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1645 /* This is a non-default case label, i.e. it has a value.
1647 See if the case label is reachable within the range of
1648 the index type. Remove out-of-range case values. Turn
1649 case ranges into a canonical form (high > low strictly)
1650 and convert the case label values to the index type.
1652 NB: The type of gimple_switch_index() may be the promoted
1653 type, but the case labels retain the original type. */
1655 if (high)
1657 /* This is a case range. Discard empty ranges.
1658 If the bounds or the range are equal, turn this
1659 into a simple (one-value) case. */
1660 int cmp = tree_int_cst_compare (high, low);
1661 if (cmp < 0)
1662 remove_element = TRUE;
1663 else if (cmp == 0)
1664 high = NULL_TREE;
1667 if (! high)
1669 /* If the simple case value is unreachable, ignore it. */
1670 if ((TREE_CODE (min_value) == INTEGER_CST
1671 && tree_int_cst_compare (low, 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
1676 low = fold_convert (index_type, low);
1678 else
1680 /* If the entire case range is unreachable, ignore it. */
1681 if ((TREE_CODE (min_value) == INTEGER_CST
1682 && tree_int_cst_compare (high, min_value) < 0)
1683 || (TREE_CODE (max_value) == INTEGER_CST
1684 && tree_int_cst_compare (low, max_value) > 0))
1685 remove_element = TRUE;
1686 else
1688 /* If the lower bound is less than the index type's
1689 minimum value, truncate the range bounds. */
1690 if (TREE_CODE (min_value) == INTEGER_CST
1691 && tree_int_cst_compare (low, min_value) < 0)
1692 low = min_value;
1693 low = fold_convert (index_type, low);
1695 /* If the upper bound is greater than the index type's
1696 maximum value, truncate the range bounds. */
1697 if (TREE_CODE (max_value) == INTEGER_CST
1698 && tree_int_cst_compare (high, max_value) > 0)
1699 high = max_value;
1700 high = fold_convert (index_type, high);
1702 /* We may have folded a case range to a one-value case. */
1703 if (tree_int_cst_equal (low, high))
1704 high = NULL_TREE;
1708 CASE_LOW (elt) = low;
1709 CASE_HIGH (elt) = high;
1711 else
1713 gcc_assert (!default_case);
1714 default_case = elt;
1715 /* The default case must be passed separately to the
1716 gimple_build_switch routine. But if DEFAULT_CASEP
1717 is NULL, we do not remove the default case (it would
1718 be completely lost). */
1719 if (default_casep)
1720 remove_element = TRUE;
1723 if (remove_element)
1724 labels.ordered_remove (i);
1725 else
1726 i++;
1728 len = i;
1730 if (!labels.is_empty ())
1731 sort_case_labels (labels);
1733 if (default_casep && !default_case)
1735 /* If the switch has no default label, add one, so that we jump
1736 around the switch body. If the labels already cover the whole
1737 range of the switch index_type, add the default label pointing
1738 to one of the existing labels. */
1739 if (len
1740 && TYPE_MIN_VALUE (index_type)
1741 && TYPE_MAX_VALUE (index_type)
1742 && tree_int_cst_equal (CASE_LOW (labels[0]),
1743 TYPE_MIN_VALUE (index_type)))
1745 tree low, high = CASE_HIGH (labels[len - 1]);
1746 if (!high)
1747 high = CASE_LOW (labels[len - 1]);
1748 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1750 for (i = 1; i < len; i++)
1752 high = CASE_LOW (labels[i]);
1753 low = CASE_HIGH (labels[i - 1]);
1754 if (!low)
1755 low = CASE_LOW (labels[i - 1]);
1756 if ((TREE_INT_CST_LOW (low) + 1
1757 != TREE_INT_CST_LOW (high))
1758 || (TREE_INT_CST_HIGH (low)
1759 + (TREE_INT_CST_LOW (high) == 0)
1760 != TREE_INT_CST_HIGH (high)))
1761 break;
1763 if (i == len)
1765 tree label = CASE_LABEL (labels[0]);
1766 default_case = build_case_label (NULL_TREE, NULL_TREE,
1767 label);
1773 if (default_casep)
1774 *default_casep = default_case;
1777 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1778 branch to. */
1780 static enum gimplify_status
1781 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1783 tree switch_expr = *expr_p;
1784 gimple_seq switch_body_seq = NULL;
1785 enum gimplify_status ret;
1786 tree index_type = TREE_TYPE (switch_expr);
1787 if (index_type == NULL_TREE)
1788 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1790 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1791 fb_rvalue);
1792 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1793 return ret;
1795 if (SWITCH_BODY (switch_expr))
1797 vec<tree> labels;
1798 vec<tree> saved_labels;
1799 tree default_case = NULL_TREE;
1800 gimple gimple_switch;
1802 /* If someone can be bothered to fill in the labels, they can
1803 be bothered to null out the body too. */
1804 gcc_assert (!SWITCH_LABELS (switch_expr));
1806 /* Save old labels, get new ones from body, then restore the old
1807 labels. Save all the things from the switch body to append after. */
1808 saved_labels = gimplify_ctxp->case_labels;
1809 gimplify_ctxp->case_labels.create (8);
1811 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1812 labels = gimplify_ctxp->case_labels;
1813 gimplify_ctxp->case_labels = saved_labels;
1815 preprocess_case_label_vec_for_gimple (labels, index_type,
1816 &default_case);
1818 if (!default_case)
1820 gimple new_default;
1822 default_case
1823 = build_case_label (NULL_TREE, NULL_TREE,
1824 create_artificial_label (UNKNOWN_LOCATION));
1825 new_default = gimple_build_label (CASE_LABEL (default_case));
1826 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1829 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1830 default_case, labels);
1831 gimplify_seq_add_stmt (pre_p, gimple_switch);
1832 gimplify_seq_add_seq (pre_p, switch_body_seq);
1833 labels.release ();
1835 else
1836 gcc_assert (SWITCH_LABELS (switch_expr));
1838 return GS_ALL_DONE;
1841 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1843 static enum gimplify_status
1844 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1846 struct gimplify_ctx *ctxp;
1847 gimple gimple_label;
1849 /* Invalid OpenMP programs can play Duff's Device type games with
1850 #pragma omp parallel. At least in the C front end, we don't
1851 detect such invalid branches until after gimplification. */
1852 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1853 if (ctxp->case_labels.exists ())
1854 break;
1856 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1857 ctxp->case_labels.safe_push (*expr_p);
1858 gimplify_seq_add_stmt (pre_p, gimple_label);
1860 return GS_ALL_DONE;
1863 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1864 if necessary. */
1866 tree
1867 build_and_jump (tree *label_p)
1869 if (label_p == NULL)
1870 /* If there's nowhere to jump, just fall through. */
1871 return NULL_TREE;
1873 if (*label_p == NULL_TREE)
1875 tree label = create_artificial_label (UNKNOWN_LOCATION);
1876 *label_p = label;
1879 return build1 (GOTO_EXPR, void_type_node, *label_p);
1882 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1883 This also involves building a label to jump to and communicating it to
1884 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1886 static enum gimplify_status
1887 gimplify_exit_expr (tree *expr_p)
1889 tree cond = TREE_OPERAND (*expr_p, 0);
1890 tree expr;
1892 expr = build_and_jump (&gimplify_ctxp->exit_label);
1893 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1894 *expr_p = expr;
1896 return GS_OK;
1899 /* A helper function to be called via walk_tree. Mark all labels under *TP
1900 as being forced. To be called for DECL_INITIAL of static variables. */
1902 tree
1903 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1905 if (TYPE_P (*tp))
1906 *walk_subtrees = 0;
1907 if (TREE_CODE (*tp) == LABEL_DECL)
1908 FORCED_LABEL (*tp) = 1;
1910 return NULL_TREE;
1913 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1914 different from its canonical type, wrap the whole thing inside a
1915 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1916 type.
1918 The canonical type of a COMPONENT_REF is the type of the field being
1919 referenced--unless the field is a bit-field which can be read directly
1920 in a smaller mode, in which case the canonical type is the
1921 sign-appropriate type corresponding to that mode. */
1923 static void
1924 canonicalize_component_ref (tree *expr_p)
1926 tree expr = *expr_p;
1927 tree type;
1929 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1931 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1932 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1933 else
1934 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1936 /* One could argue that all the stuff below is not necessary for
1937 the non-bitfield case and declare it a FE error if type
1938 adjustment would be needed. */
1939 if (TREE_TYPE (expr) != type)
1941 #ifdef ENABLE_TYPES_CHECKING
1942 tree old_type = TREE_TYPE (expr);
1943 #endif
1944 int type_quals;
1946 /* We need to preserve qualifiers and propagate them from
1947 operand 0. */
1948 type_quals = TYPE_QUALS (type)
1949 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1950 if (TYPE_QUALS (type) != type_quals)
1951 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1953 /* Set the type of the COMPONENT_REF to the underlying type. */
1954 TREE_TYPE (expr) = type;
1956 #ifdef ENABLE_TYPES_CHECKING
1957 /* It is now a FE error, if the conversion from the canonical
1958 type to the original expression type is not useless. */
1959 gcc_assert (useless_type_conversion_p (old_type, type));
1960 #endif
1964 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1965 to foo, embed that change in the ADDR_EXPR by converting
1966 T array[U];
1967 (T *)&array
1969 &array[L]
1970 where L is the lower bound. For simplicity, only do this for constant
1971 lower bound.
1972 The constraint is that the type of &array[L] is trivially convertible
1973 to T *. */
1975 static void
1976 canonicalize_addr_expr (tree *expr_p)
1978 tree expr = *expr_p;
1979 tree addr_expr = TREE_OPERAND (expr, 0);
1980 tree datype, ddatype, pddatype;
1982 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1983 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1984 || TREE_CODE (addr_expr) != ADDR_EXPR)
1985 return;
1987 /* The addr_expr type should be a pointer to an array. */
1988 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1989 if (TREE_CODE (datype) != ARRAY_TYPE)
1990 return;
1992 /* The pointer to element type shall be trivially convertible to
1993 the expression pointer type. */
1994 ddatype = TREE_TYPE (datype);
1995 pddatype = build_pointer_type (ddatype);
1996 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1997 pddatype))
1998 return;
2000 /* The lower bound and element sizes must be constant. */
2001 if (!TYPE_SIZE_UNIT (ddatype)
2002 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2003 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2004 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2005 return;
2007 /* All checks succeeded. Build a new node to merge the cast. */
2008 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2009 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2010 NULL_TREE, NULL_TREE);
2011 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2013 /* We can have stripped a required restrict qualifier above. */
2014 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2015 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2018 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2019 underneath as appropriate. */
2021 static enum gimplify_status
2022 gimplify_conversion (tree *expr_p)
2024 location_t loc = EXPR_LOCATION (*expr_p);
2025 gcc_assert (CONVERT_EXPR_P (*expr_p));
2027 /* Then strip away all but the outermost conversion. */
2028 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2030 /* And remove the outermost conversion if it's useless. */
2031 if (tree_ssa_useless_type_conversion (*expr_p))
2032 *expr_p = TREE_OPERAND (*expr_p, 0);
2034 /* If we still have a conversion at the toplevel,
2035 then canonicalize some constructs. */
2036 if (CONVERT_EXPR_P (*expr_p))
2038 tree sub = TREE_OPERAND (*expr_p, 0);
2040 /* If a NOP conversion is changing the type of a COMPONENT_REF
2041 expression, then canonicalize its type now in order to expose more
2042 redundant conversions. */
2043 if (TREE_CODE (sub) == COMPONENT_REF)
2044 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2046 /* If a NOP conversion is changing a pointer to array of foo
2047 to a pointer to foo, embed that change in the ADDR_EXPR. */
2048 else if (TREE_CODE (sub) == ADDR_EXPR)
2049 canonicalize_addr_expr (expr_p);
2052 /* If we have a conversion to a non-register type force the
2053 use of a VIEW_CONVERT_EXPR instead. */
2054 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2055 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2056 TREE_OPERAND (*expr_p, 0));
2058 return GS_OK;
2061 /* Nonlocal VLAs seen in the current function. */
2062 static struct pointer_set_t *nonlocal_vlas;
2064 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2065 DECL_VALUE_EXPR, and it's worth re-examining things. */
2067 static enum gimplify_status
2068 gimplify_var_or_parm_decl (tree *expr_p)
2070 tree decl = *expr_p;
2072 /* ??? If this is a local variable, and it has not been seen in any
2073 outer BIND_EXPR, then it's probably the result of a duplicate
2074 declaration, for which we've already issued an error. It would
2075 be really nice if the front end wouldn't leak these at all.
2076 Currently the only known culprit is C++ destructors, as seen
2077 in g++.old-deja/g++.jason/binding.C. */
2078 if (TREE_CODE (decl) == VAR_DECL
2079 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2080 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2081 && decl_function_context (decl) == current_function_decl)
2083 gcc_assert (seen_error ());
2084 return GS_ERROR;
2087 /* When within an OpenMP context, notice uses of variables. */
2088 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2089 return GS_ALL_DONE;
2091 /* If the decl is an alias for another expression, substitute it now. */
2092 if (DECL_HAS_VALUE_EXPR_P (decl))
2094 tree value_expr = DECL_VALUE_EXPR (decl);
2096 /* For referenced nonlocal VLAs add a decl for debugging purposes
2097 to the current function. */
2098 if (TREE_CODE (decl) == VAR_DECL
2099 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2100 && nonlocal_vlas != NULL
2101 && TREE_CODE (value_expr) == INDIRECT_REF
2102 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2103 && decl_function_context (decl) != current_function_decl)
2105 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2106 while (ctx && ctx->region_type == ORT_WORKSHARE)
2107 ctx = ctx->outer_context;
2108 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2110 tree copy = copy_node (decl), block;
2112 lang_hooks.dup_lang_specific_decl (copy);
2113 SET_DECL_RTL (copy, 0);
2114 TREE_USED (copy) = 1;
2115 block = DECL_INITIAL (current_function_decl);
2116 DECL_CHAIN (copy) = BLOCK_VARS (block);
2117 BLOCK_VARS (block) = copy;
2118 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2119 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2123 *expr_p = unshare_expr (value_expr);
2124 return GS_OK;
2127 return GS_ALL_DONE;
2130 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2131 node *EXPR_P.
2133 compound_lval
2134 : min_lval '[' val ']'
2135 | min_lval '.' ID
2136 | compound_lval '[' val ']'
2137 | compound_lval '.' ID
2139 This is not part of the original SIMPLE definition, which separates
2140 array and member references, but it seems reasonable to handle them
2141 together. Also, this way we don't run into problems with union
2142 aliasing; gcc requires that for accesses through a union to alias, the
2143 union reference must be explicit, which was not always the case when we
2144 were splitting up array and member refs.
2146 PRE_P points to the sequence where side effects that must happen before
2147 *EXPR_P should be stored.
2149 POST_P points to the sequence where side effects that must happen after
2150 *EXPR_P should be stored. */
2152 static enum gimplify_status
2153 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2154 fallback_t fallback)
2156 tree *p;
2157 vec<tree> expr_stack;
2158 enum gimplify_status ret = GS_ALL_DONE, tret;
2159 int i;
2160 location_t loc = EXPR_LOCATION (*expr_p);
2161 tree expr = *expr_p;
2163 /* Create a stack of the subexpressions so later we can walk them in
2164 order from inner to outer. */
2165 expr_stack.create (10);
2167 /* We can handle anything that get_inner_reference can deal with. */
2168 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2170 restart:
2171 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2172 if (TREE_CODE (*p) == INDIRECT_REF)
2173 *p = fold_indirect_ref_loc (loc, *p);
2175 if (handled_component_p (*p))
2177 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2178 additional COMPONENT_REFs. */
2179 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2180 && gimplify_var_or_parm_decl (p) == GS_OK)
2181 goto restart;
2182 else
2183 break;
2185 expr_stack.safe_push (*p);
2188 gcc_assert (expr_stack.length ());
2190 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2191 walked through and P points to the innermost expression.
2193 Java requires that we elaborated nodes in source order. That
2194 means we must gimplify the inner expression followed by each of
2195 the indices, in order. But we can't gimplify the inner
2196 expression until we deal with any variable bounds, sizes, or
2197 positions in order to deal with PLACEHOLDER_EXPRs.
2199 So we do this in three steps. First we deal with the annotations
2200 for any variables in the components, then we gimplify the base,
2201 then we gimplify any indices, from left to right. */
2202 for (i = expr_stack.length () - 1; i >= 0; i--)
2204 tree t = expr_stack[i];
2206 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2208 /* Gimplify the low bound and element type size and put them into
2209 the ARRAY_REF. If these values are set, they have already been
2210 gimplified. */
2211 if (TREE_OPERAND (t, 2) == NULL_TREE)
2213 tree low = unshare_expr (array_ref_low_bound (t));
2214 if (!is_gimple_min_invariant (low))
2216 TREE_OPERAND (t, 2) = low;
2217 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2218 post_p, is_gimple_reg,
2219 fb_rvalue);
2220 ret = MIN (ret, tret);
2223 else
2225 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2226 is_gimple_reg, fb_rvalue);
2227 ret = MIN (ret, tret);
2230 if (TREE_OPERAND (t, 3) == NULL_TREE)
2232 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2233 tree elmt_size = unshare_expr (array_ref_element_size (t));
2234 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2236 /* Divide the element size by the alignment of the element
2237 type (above). */
2238 elmt_size
2239 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2241 if (!is_gimple_min_invariant (elmt_size))
2243 TREE_OPERAND (t, 3) = elmt_size;
2244 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2245 post_p, is_gimple_reg,
2246 fb_rvalue);
2247 ret = MIN (ret, tret);
2250 else
2252 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2253 is_gimple_reg, fb_rvalue);
2254 ret = MIN (ret, tret);
2257 else if (TREE_CODE (t) == COMPONENT_REF)
2259 /* Set the field offset into T and gimplify it. */
2260 if (TREE_OPERAND (t, 2) == NULL_TREE)
2262 tree offset = unshare_expr (component_ref_field_offset (t));
2263 tree field = TREE_OPERAND (t, 1);
2264 tree factor
2265 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2267 /* Divide the offset by its alignment. */
2268 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2270 if (!is_gimple_min_invariant (offset))
2272 TREE_OPERAND (t, 2) = offset;
2273 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2274 post_p, is_gimple_reg,
2275 fb_rvalue);
2276 ret = MIN (ret, tret);
2279 else
2281 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2282 is_gimple_reg, fb_rvalue);
2283 ret = MIN (ret, tret);
2288 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2289 so as to match the min_lval predicate. Failure to do so may result
2290 in the creation of large aggregate temporaries. */
2291 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2292 fallback | fb_lvalue);
2293 ret = MIN (ret, tret);
2295 /* And finally, the indices and operands of ARRAY_REF. During this
2296 loop we also remove any useless conversions. */
2297 for (; expr_stack.length () > 0; )
2299 tree t = expr_stack.pop ();
2301 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2303 /* Gimplify the dimension. */
2304 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2306 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2307 is_gimple_val, fb_rvalue);
2308 ret = MIN (ret, tret);
2312 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2314 /* The innermost expression P may have originally had
2315 TREE_SIDE_EFFECTS set which would have caused all the outer
2316 expressions in *EXPR_P leading to P to also have had
2317 TREE_SIDE_EFFECTS set. */
2318 recalculate_side_effects (t);
2321 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2322 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2324 canonicalize_component_ref (expr_p);
2327 expr_stack.release ();
2329 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2331 return ret;
2334 /* Gimplify the self modifying expression pointed to by EXPR_P
2335 (++, --, +=, -=).
2337 PRE_P points to the list where side effects that must happen before
2338 *EXPR_P should be stored.
2340 POST_P points to the list where side effects that must happen after
2341 *EXPR_P should be stored.
2343 WANT_VALUE is nonzero iff we want to use the value of this expression
2344 in another expression.
2346 ARITH_TYPE is the type the computation should be performed in. */
2348 enum gimplify_status
2349 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2350 bool want_value, tree arith_type)
2352 enum tree_code code;
2353 tree lhs, lvalue, rhs, t1;
2354 gimple_seq post = NULL, *orig_post_p = post_p;
2355 bool postfix;
2356 enum tree_code arith_code;
2357 enum gimplify_status ret;
2358 location_t loc = EXPR_LOCATION (*expr_p);
2360 code = TREE_CODE (*expr_p);
2362 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2363 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2365 /* Prefix or postfix? */
2366 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2367 /* Faster to treat as prefix if result is not used. */
2368 postfix = want_value;
2369 else
2370 postfix = false;
2372 /* For postfix, make sure the inner expression's post side effects
2373 are executed after side effects from this expression. */
2374 if (postfix)
2375 post_p = &post;
2377 /* Add or subtract? */
2378 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2379 arith_code = PLUS_EXPR;
2380 else
2381 arith_code = MINUS_EXPR;
2383 /* Gimplify the LHS into a GIMPLE lvalue. */
2384 lvalue = TREE_OPERAND (*expr_p, 0);
2385 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2386 if (ret == GS_ERROR)
2387 return ret;
2389 /* Extract the operands to the arithmetic operation. */
2390 lhs = lvalue;
2391 rhs = TREE_OPERAND (*expr_p, 1);
2393 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2394 that as the result value and in the postqueue operation. */
2395 if (postfix)
2397 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2398 if (ret == GS_ERROR)
2399 return ret;
2401 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2404 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2405 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2407 rhs = convert_to_ptrofftype_loc (loc, rhs);
2408 if (arith_code == MINUS_EXPR)
2409 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2410 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2412 else
2413 t1 = fold_convert (TREE_TYPE (*expr_p),
2414 fold_build2 (arith_code, arith_type,
2415 fold_convert (arith_type, lhs),
2416 fold_convert (arith_type, rhs)));
2418 if (postfix)
2420 gimplify_assign (lvalue, t1, pre_p);
2421 gimplify_seq_add_seq (orig_post_p, post);
2422 *expr_p = lhs;
2423 return GS_ALL_DONE;
2425 else
2427 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2428 return GS_OK;
2432 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2434 static void
2435 maybe_with_size_expr (tree *expr_p)
2437 tree expr = *expr_p;
2438 tree type = TREE_TYPE (expr);
2439 tree size;
2441 /* If we've already wrapped this or the type is error_mark_node, we can't do
2442 anything. */
2443 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2444 || type == error_mark_node)
2445 return;
2447 /* If the size isn't known or is a constant, we have nothing to do. */
2448 size = TYPE_SIZE_UNIT (type);
2449 if (!size || TREE_CODE (size) == INTEGER_CST)
2450 return;
2452 /* Otherwise, make a WITH_SIZE_EXPR. */
2453 size = unshare_expr (size);
2454 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2455 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2458 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2459 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2460 the CALL_EXPR. */
2462 static enum gimplify_status
2463 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2465 bool (*test) (tree);
2466 fallback_t fb;
2468 /* In general, we allow lvalues for function arguments to avoid
2469 extra overhead of copying large aggregates out of even larger
2470 aggregates into temporaries only to copy the temporaries to
2471 the argument list. Make optimizers happy by pulling out to
2472 temporaries those types that fit in registers. */
2473 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2474 test = is_gimple_val, fb = fb_rvalue;
2475 else
2477 test = is_gimple_lvalue, fb = fb_either;
2478 /* Also strip a TARGET_EXPR that would force an extra copy. */
2479 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2481 tree init = TARGET_EXPR_INITIAL (*arg_p);
2482 if (init
2483 && !VOID_TYPE_P (TREE_TYPE (init)))
2484 *arg_p = init;
2488 /* If this is a variable sized type, we must remember the size. */
2489 maybe_with_size_expr (arg_p);
2491 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2492 /* Make sure arguments have the same location as the function call
2493 itself. */
2494 protected_set_expr_location (*arg_p, call_location);
2496 /* There is a sequence point before a function call. Side effects in
2497 the argument list must occur before the actual call. So, when
2498 gimplifying arguments, force gimplify_expr to use an internal
2499 post queue which is then appended to the end of PRE_P. */
2500 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2503 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2504 WANT_VALUE is true if the result of the call is desired. */
2506 static enum gimplify_status
2507 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2509 tree fndecl, parms, p, fnptrtype;
2510 enum gimplify_status ret;
2511 int i, nargs;
2512 gimple call;
2513 bool builtin_va_start_p = FALSE;
2514 location_t loc = EXPR_LOCATION (*expr_p);
2516 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2518 /* For reliable diagnostics during inlining, it is necessary that
2519 every call_expr be annotated with file and line. */
2520 if (! EXPR_HAS_LOCATION (*expr_p))
2521 SET_EXPR_LOCATION (*expr_p, input_location);
2523 /* This may be a call to a builtin function.
2525 Builtin function calls may be transformed into different
2526 (and more efficient) builtin function calls under certain
2527 circumstances. Unfortunately, gimplification can muck things
2528 up enough that the builtin expanders are not aware that certain
2529 transformations are still valid.
2531 So we attempt transformation/gimplification of the call before
2532 we gimplify the CALL_EXPR. At this time we do not manage to
2533 transform all calls in the same manner as the expanders do, but
2534 we do transform most of them. */
2535 fndecl = get_callee_fndecl (*expr_p);
2536 if (fndecl
2537 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2538 switch (DECL_FUNCTION_CODE (fndecl))
2540 case BUILT_IN_VA_START:
2542 builtin_va_start_p = TRUE;
2543 if (call_expr_nargs (*expr_p) < 2)
2545 error ("too few arguments to function %<va_start%>");
2546 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2547 return GS_OK;
2550 if (fold_builtin_next_arg (*expr_p, true))
2552 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2553 return GS_OK;
2555 break;
2557 case BUILT_IN_LINE:
2559 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2560 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2561 return GS_OK;
2563 case BUILT_IN_FILE:
2565 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2566 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2567 return GS_OK;
2569 case BUILT_IN_FUNCTION:
2571 const char *function;
2572 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2573 *expr_p = build_string_literal (strlen (function) + 1, function);
2574 return GS_OK;
2576 default:
2579 if (fndecl && DECL_BUILT_IN (fndecl))
2581 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2582 if (new_tree && new_tree != *expr_p)
2584 /* There was a transformation of this call which computes the
2585 same value, but in a more efficient way. Return and try
2586 again. */
2587 *expr_p = new_tree;
2588 return GS_OK;
2592 /* Remember the original function pointer type. */
2593 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2595 /* There is a sequence point before the call, so any side effects in
2596 the calling expression must occur before the actual call. Force
2597 gimplify_expr to use an internal post queue. */
2598 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2599 is_gimple_call_addr, fb_rvalue);
2601 nargs = call_expr_nargs (*expr_p);
2603 /* Get argument types for verification. */
2604 fndecl = get_callee_fndecl (*expr_p);
2605 parms = NULL_TREE;
2606 if (fndecl)
2607 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2608 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2609 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2611 if (fndecl && DECL_ARGUMENTS (fndecl))
2612 p = DECL_ARGUMENTS (fndecl);
2613 else if (parms)
2614 p = parms;
2615 else
2616 p = NULL_TREE;
2617 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2620 /* If the last argument is __builtin_va_arg_pack () and it is not
2621 passed as a named argument, decrease the number of CALL_EXPR
2622 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2623 if (!p
2624 && i < nargs
2625 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2627 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2628 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2630 if (last_arg_fndecl
2631 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2632 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2633 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2635 tree call = *expr_p;
2637 --nargs;
2638 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2639 CALL_EXPR_FN (call),
2640 nargs, CALL_EXPR_ARGP (call));
2642 /* Copy all CALL_EXPR flags, location and block, except
2643 CALL_EXPR_VA_ARG_PACK flag. */
2644 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2645 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2646 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2647 = CALL_EXPR_RETURN_SLOT_OPT (call);
2648 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2649 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2651 /* Set CALL_EXPR_VA_ARG_PACK. */
2652 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2656 /* Finally, gimplify the function arguments. */
2657 if (nargs > 0)
2659 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2660 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2661 PUSH_ARGS_REVERSED ? i-- : i++)
2663 enum gimplify_status t;
2665 /* Avoid gimplifying the second argument to va_start, which needs to
2666 be the plain PARM_DECL. */
2667 if ((i != 1) || !builtin_va_start_p)
2669 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2670 EXPR_LOCATION (*expr_p));
2672 if (t == GS_ERROR)
2673 ret = GS_ERROR;
2678 /* Verify the function result. */
2679 if (want_value && fndecl
2680 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2682 error_at (loc, "using result of function returning %<void%>");
2683 ret = GS_ERROR;
2686 /* Try this again in case gimplification exposed something. */
2687 if (ret != GS_ERROR)
2689 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2691 if (new_tree && new_tree != *expr_p)
2693 /* There was a transformation of this call which computes the
2694 same value, but in a more efficient way. Return and try
2695 again. */
2696 *expr_p = new_tree;
2697 return GS_OK;
2700 else
2702 *expr_p = error_mark_node;
2703 return GS_ERROR;
2706 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2707 decl. This allows us to eliminate redundant or useless
2708 calls to "const" functions. */
2709 if (TREE_CODE (*expr_p) == CALL_EXPR)
2711 int flags = call_expr_flags (*expr_p);
2712 if (flags & (ECF_CONST | ECF_PURE)
2713 /* An infinite loop is considered a side effect. */
2714 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2715 TREE_SIDE_EFFECTS (*expr_p) = 0;
2718 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2719 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2720 form and delegate the creation of a GIMPLE_CALL to
2721 gimplify_modify_expr. This is always possible because when
2722 WANT_VALUE is true, the caller wants the result of this call into
2723 a temporary, which means that we will emit an INIT_EXPR in
2724 internal_get_tmp_var which will then be handled by
2725 gimplify_modify_expr. */
2726 if (!want_value)
2728 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2729 have to do is replicate it as a GIMPLE_CALL tuple. */
2730 gimple_stmt_iterator gsi;
2731 call = gimple_build_call_from_tree (*expr_p);
2732 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2733 gimplify_seq_add_stmt (pre_p, call);
2734 gsi = gsi_last (*pre_p);
2735 fold_stmt (&gsi);
2736 *expr_p = NULL_TREE;
2738 else
2739 /* Remember the original function type. */
2740 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2741 CALL_EXPR_FN (*expr_p));
2743 return ret;
2746 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2747 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2749 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2750 condition is true or false, respectively. If null, we should generate
2751 our own to skip over the evaluation of this specific expression.
2753 LOCUS is the source location of the COND_EXPR.
2755 This function is the tree equivalent of do_jump.
2757 shortcut_cond_r should only be called by shortcut_cond_expr. */
2759 static tree
2760 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2761 location_t locus)
2763 tree local_label = NULL_TREE;
2764 tree t, expr = NULL;
2766 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2767 retain the shortcut semantics. Just insert the gotos here;
2768 shortcut_cond_expr will append the real blocks later. */
2769 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2771 location_t new_locus;
2773 /* Turn if (a && b) into
2775 if (a); else goto no;
2776 if (b) goto yes; else goto no;
2777 (no:) */
2779 if (false_label_p == NULL)
2780 false_label_p = &local_label;
2782 /* Keep the original source location on the first 'if'. */
2783 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2784 append_to_statement_list (t, &expr);
2786 /* Set the source location of the && on the second 'if'. */
2787 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2788 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2789 new_locus);
2790 append_to_statement_list (t, &expr);
2792 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2794 location_t new_locus;
2796 /* Turn if (a || b) into
2798 if (a) goto yes;
2799 if (b) goto yes; else goto no;
2800 (yes:) */
2802 if (true_label_p == NULL)
2803 true_label_p = &local_label;
2805 /* Keep the original source location on the first 'if'. */
2806 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2807 append_to_statement_list (t, &expr);
2809 /* Set the source location of the || on the second 'if'. */
2810 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2811 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2812 new_locus);
2813 append_to_statement_list (t, &expr);
2815 else if (TREE_CODE (pred) == COND_EXPR
2816 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2817 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2819 location_t new_locus;
2821 /* As long as we're messing with gotos, turn if (a ? b : c) into
2822 if (a)
2823 if (b) goto yes; else goto no;
2824 else
2825 if (c) goto yes; else goto no;
2827 Don't do this if one of the arms has void type, which can happen
2828 in C++ when the arm is throw. */
2830 /* Keep the original source location on the first 'if'. Set the source
2831 location of the ? on the second 'if'. */
2832 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2833 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2834 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2835 false_label_p, locus),
2836 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2837 false_label_p, new_locus));
2839 else
2841 expr = build3 (COND_EXPR, void_type_node, pred,
2842 build_and_jump (true_label_p),
2843 build_and_jump (false_label_p));
2844 SET_EXPR_LOCATION (expr, locus);
2847 if (local_label)
2849 t = build1 (LABEL_EXPR, void_type_node, local_label);
2850 append_to_statement_list (t, &expr);
2853 return expr;
2856 /* Given a conditional expression EXPR with short-circuit boolean
2857 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2858 predicate apart into the equivalent sequence of conditionals. */
2860 static tree
2861 shortcut_cond_expr (tree expr)
2863 tree pred = TREE_OPERAND (expr, 0);
2864 tree then_ = TREE_OPERAND (expr, 1);
2865 tree else_ = TREE_OPERAND (expr, 2);
2866 tree true_label, false_label, end_label, t;
2867 tree *true_label_p;
2868 tree *false_label_p;
2869 bool emit_end, emit_false, jump_over_else;
2870 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2871 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2873 /* First do simple transformations. */
2874 if (!else_se)
2876 /* If there is no 'else', turn
2877 if (a && b) then c
2878 into
2879 if (a) if (b) then c. */
2880 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2882 /* Keep the original source location on the first 'if'. */
2883 location_t locus = EXPR_LOC_OR_HERE (expr);
2884 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2885 /* Set the source location of the && on the second 'if'. */
2886 if (EXPR_HAS_LOCATION (pred))
2887 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2888 then_ = shortcut_cond_expr (expr);
2889 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2890 pred = TREE_OPERAND (pred, 0);
2891 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2892 SET_EXPR_LOCATION (expr, locus);
2896 if (!then_se)
2898 /* If there is no 'then', turn
2899 if (a || b); else d
2900 into
2901 if (a); else if (b); else d. */
2902 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2904 /* Keep the original source location on the first 'if'. */
2905 location_t locus = EXPR_LOC_OR_HERE (expr);
2906 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2907 /* Set the source location of the || on the second 'if'. */
2908 if (EXPR_HAS_LOCATION (pred))
2909 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2910 else_ = shortcut_cond_expr (expr);
2911 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2912 pred = TREE_OPERAND (pred, 0);
2913 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2914 SET_EXPR_LOCATION (expr, locus);
2918 /* If we're done, great. */
2919 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2920 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2921 return expr;
2923 /* Otherwise we need to mess with gotos. Change
2924 if (a) c; else d;
2926 if (a); else goto no;
2927 c; goto end;
2928 no: d; end:
2929 and recursively gimplify the condition. */
2931 true_label = false_label = end_label = NULL_TREE;
2933 /* If our arms just jump somewhere, hijack those labels so we don't
2934 generate jumps to jumps. */
2936 if (then_
2937 && TREE_CODE (then_) == GOTO_EXPR
2938 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2940 true_label = GOTO_DESTINATION (then_);
2941 then_ = NULL;
2942 then_se = false;
2945 if (else_
2946 && TREE_CODE (else_) == GOTO_EXPR
2947 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2949 false_label = GOTO_DESTINATION (else_);
2950 else_ = NULL;
2951 else_se = false;
2954 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2955 if (true_label)
2956 true_label_p = &true_label;
2957 else
2958 true_label_p = NULL;
2960 /* The 'else' branch also needs a label if it contains interesting code. */
2961 if (false_label || else_se)
2962 false_label_p = &false_label;
2963 else
2964 false_label_p = NULL;
2966 /* If there was nothing else in our arms, just forward the label(s). */
2967 if (!then_se && !else_se)
2968 return shortcut_cond_r (pred, true_label_p, false_label_p,
2969 EXPR_LOC_OR_HERE (expr));
2971 /* If our last subexpression already has a terminal label, reuse it. */
2972 if (else_se)
2973 t = expr_last (else_);
2974 else if (then_se)
2975 t = expr_last (then_);
2976 else
2977 t = NULL;
2978 if (t && TREE_CODE (t) == LABEL_EXPR)
2979 end_label = LABEL_EXPR_LABEL (t);
2981 /* If we don't care about jumping to the 'else' branch, jump to the end
2982 if the condition is false. */
2983 if (!false_label_p)
2984 false_label_p = &end_label;
2986 /* We only want to emit these labels if we aren't hijacking them. */
2987 emit_end = (end_label == NULL_TREE);
2988 emit_false = (false_label == NULL_TREE);
2990 /* We only emit the jump over the else clause if we have to--if the
2991 then clause may fall through. Otherwise we can wind up with a
2992 useless jump and a useless label at the end of gimplified code,
2993 which will cause us to think that this conditional as a whole
2994 falls through even if it doesn't. If we then inline a function
2995 which ends with such a condition, that can cause us to issue an
2996 inappropriate warning about control reaching the end of a
2997 non-void function. */
2998 jump_over_else = block_may_fallthru (then_);
3000 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3001 EXPR_LOC_OR_HERE (expr));
3003 expr = NULL;
3004 append_to_statement_list (pred, &expr);
3006 append_to_statement_list (then_, &expr);
3007 if (else_se)
3009 if (jump_over_else)
3011 tree last = expr_last (expr);
3012 t = build_and_jump (&end_label);
3013 if (EXPR_HAS_LOCATION (last))
3014 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3015 append_to_statement_list (t, &expr);
3017 if (emit_false)
3019 t = build1 (LABEL_EXPR, void_type_node, false_label);
3020 append_to_statement_list (t, &expr);
3022 append_to_statement_list (else_, &expr);
3024 if (emit_end && end_label)
3026 t = build1 (LABEL_EXPR, void_type_node, end_label);
3027 append_to_statement_list (t, &expr);
3030 return expr;
3033 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3035 tree
3036 gimple_boolify (tree expr)
3038 tree type = TREE_TYPE (expr);
3039 location_t loc = EXPR_LOCATION (expr);
3041 if (TREE_CODE (expr) == NE_EXPR
3042 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3043 && integer_zerop (TREE_OPERAND (expr, 1)))
3045 tree call = TREE_OPERAND (expr, 0);
3046 tree fn = get_callee_fndecl (call);
3048 /* For __builtin_expect ((long) (x), y) recurse into x as well
3049 if x is truth_value_p. */
3050 if (fn
3051 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3052 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3053 && call_expr_nargs (call) == 2)
3055 tree arg = CALL_EXPR_ARG (call, 0);
3056 if (arg)
3058 if (TREE_CODE (arg) == NOP_EXPR
3059 && TREE_TYPE (arg) == TREE_TYPE (call))
3060 arg = TREE_OPERAND (arg, 0);
3061 if (truth_value_p (TREE_CODE (arg)))
3063 arg = gimple_boolify (arg);
3064 CALL_EXPR_ARG (call, 0)
3065 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3071 switch (TREE_CODE (expr))
3073 case TRUTH_AND_EXPR:
3074 case TRUTH_OR_EXPR:
3075 case TRUTH_XOR_EXPR:
3076 case TRUTH_ANDIF_EXPR:
3077 case TRUTH_ORIF_EXPR:
3078 /* Also boolify the arguments of truth exprs. */
3079 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3080 /* FALLTHRU */
3082 case TRUTH_NOT_EXPR:
3083 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3085 /* These expressions always produce boolean results. */
3086 if (TREE_CODE (type) != BOOLEAN_TYPE)
3087 TREE_TYPE (expr) = boolean_type_node;
3088 return expr;
3090 default:
3091 if (COMPARISON_CLASS_P (expr))
3093 /* There expressions always prduce boolean results. */
3094 if (TREE_CODE (type) != BOOLEAN_TYPE)
3095 TREE_TYPE (expr) = boolean_type_node;
3096 return expr;
3098 /* Other expressions that get here must have boolean values, but
3099 might need to be converted to the appropriate mode. */
3100 if (TREE_CODE (type) == BOOLEAN_TYPE)
3101 return expr;
3102 return fold_convert_loc (loc, boolean_type_node, expr);
3106 /* Given a conditional expression *EXPR_P without side effects, gimplify
3107 its operands. New statements are inserted to PRE_P. */
3109 static enum gimplify_status
3110 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3112 tree expr = *expr_p, cond;
3113 enum gimplify_status ret, tret;
3114 enum tree_code code;
3116 cond = gimple_boolify (COND_EXPR_COND (expr));
3118 /* We need to handle && and || specially, as their gimplification
3119 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3120 code = TREE_CODE (cond);
3121 if (code == TRUTH_ANDIF_EXPR)
3122 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3123 else if (code == TRUTH_ORIF_EXPR)
3124 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3125 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3126 COND_EXPR_COND (*expr_p) = cond;
3128 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3129 is_gimple_val, fb_rvalue);
3130 ret = MIN (ret, tret);
3131 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3132 is_gimple_val, fb_rvalue);
3134 return MIN (ret, tret);
3137 /* Return true if evaluating EXPR could trap.
3138 EXPR is GENERIC, while tree_could_trap_p can be called
3139 only on GIMPLE. */
3141 static bool
3142 generic_expr_could_trap_p (tree expr)
3144 unsigned i, n;
3146 if (!expr || is_gimple_val (expr))
3147 return false;
3149 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3150 return true;
3152 n = TREE_OPERAND_LENGTH (expr);
3153 for (i = 0; i < n; i++)
3154 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3155 return true;
3157 return false;
3160 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3161 into
3163 if (p) if (p)
3164 t1 = a; a;
3165 else or else
3166 t1 = b; b;
3169 The second form is used when *EXPR_P is of type void.
3171 PRE_P points to the list where side effects that must happen before
3172 *EXPR_P should be stored. */
3174 static enum gimplify_status
3175 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3177 tree expr = *expr_p;
3178 tree type = TREE_TYPE (expr);
3179 location_t loc = EXPR_LOCATION (expr);
3180 tree tmp, arm1, arm2;
3181 enum gimplify_status ret;
3182 tree label_true, label_false, label_cont;
3183 bool have_then_clause_p, have_else_clause_p;
3184 gimple gimple_cond;
3185 enum tree_code pred_code;
3186 gimple_seq seq = NULL;
3188 /* If this COND_EXPR has a value, copy the values into a temporary within
3189 the arms. */
3190 if (!VOID_TYPE_P (type))
3192 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3193 tree result;
3195 /* If either an rvalue is ok or we do not require an lvalue, create the
3196 temporary. But we cannot do that if the type is addressable. */
3197 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3198 && !TREE_ADDRESSABLE (type))
3200 if (gimplify_ctxp->allow_rhs_cond_expr
3201 /* If either branch has side effects or could trap, it can't be
3202 evaluated unconditionally. */
3203 && !TREE_SIDE_EFFECTS (then_)
3204 && !generic_expr_could_trap_p (then_)
3205 && !TREE_SIDE_EFFECTS (else_)
3206 && !generic_expr_could_trap_p (else_))
3207 return gimplify_pure_cond_expr (expr_p, pre_p);
3209 tmp = create_tmp_var (type, "iftmp");
3210 result = tmp;
3213 /* Otherwise, only create and copy references to the values. */
3214 else
3216 type = build_pointer_type (type);
3218 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3219 then_ = build_fold_addr_expr_loc (loc, then_);
3221 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3222 else_ = build_fold_addr_expr_loc (loc, else_);
3224 expr
3225 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3227 tmp = create_tmp_var (type, "iftmp");
3228 result = build_simple_mem_ref_loc (loc, tmp);
3231 /* Build the new then clause, `tmp = then_;'. But don't build the
3232 assignment if the value is void; in C++ it can be if it's a throw. */
3233 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3234 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3236 /* Similarly, build the new else clause, `tmp = else_;'. */
3237 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3238 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3240 TREE_TYPE (expr) = void_type_node;
3241 recalculate_side_effects (expr);
3243 /* Move the COND_EXPR to the prequeue. */
3244 gimplify_stmt (&expr, pre_p);
3246 *expr_p = result;
3247 return GS_ALL_DONE;
3250 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3251 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3252 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3253 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3255 /* Make sure the condition has BOOLEAN_TYPE. */
3256 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3258 /* Break apart && and || conditions. */
3259 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3260 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3262 expr = shortcut_cond_expr (expr);
3264 if (expr != *expr_p)
3266 *expr_p = expr;
3268 /* We can't rely on gimplify_expr to re-gimplify the expanded
3269 form properly, as cleanups might cause the target labels to be
3270 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3271 set up a conditional context. */
3272 gimple_push_condition ();
3273 gimplify_stmt (expr_p, &seq);
3274 gimple_pop_condition (pre_p);
3275 gimple_seq_add_seq (pre_p, seq);
3277 return GS_ALL_DONE;
3281 /* Now do the normal gimplification. */
3283 /* Gimplify condition. */
3284 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3285 fb_rvalue);
3286 if (ret == GS_ERROR)
3287 return GS_ERROR;
3288 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3290 gimple_push_condition ();
3292 have_then_clause_p = have_else_clause_p = false;
3293 if (TREE_OPERAND (expr, 1) != NULL
3294 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3295 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3296 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3297 == current_function_decl)
3298 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3299 have different locations, otherwise we end up with incorrect
3300 location information on the branches. */
3301 && (optimize
3302 || !EXPR_HAS_LOCATION (expr)
3303 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3304 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3306 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3307 have_then_clause_p = true;
3309 else
3310 label_true = create_artificial_label (UNKNOWN_LOCATION);
3311 if (TREE_OPERAND (expr, 2) != NULL
3312 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3313 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3314 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3315 == current_function_decl)
3316 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3317 have different locations, otherwise we end up with incorrect
3318 location information on the branches. */
3319 && (optimize
3320 || !EXPR_HAS_LOCATION (expr)
3321 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3322 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3324 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3325 have_else_clause_p = true;
3327 else
3328 label_false = create_artificial_label (UNKNOWN_LOCATION);
3330 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3331 &arm2);
3333 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3334 label_false);
3336 gimplify_seq_add_stmt (&seq, gimple_cond);
3337 label_cont = NULL_TREE;
3338 if (!have_then_clause_p)
3340 /* For if (...) {} else { code; } put label_true after
3341 the else block. */
3342 if (TREE_OPERAND (expr, 1) == NULL_TREE
3343 && !have_else_clause_p
3344 && TREE_OPERAND (expr, 2) != NULL_TREE)
3345 label_cont = label_true;
3346 else
3348 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3349 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3350 /* For if (...) { code; } else {} or
3351 if (...) { code; } else goto label; or
3352 if (...) { code; return; } else { ... }
3353 label_cont isn't needed. */
3354 if (!have_else_clause_p
3355 && TREE_OPERAND (expr, 2) != NULL_TREE
3356 && gimple_seq_may_fallthru (seq))
3358 gimple g;
3359 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3361 g = gimple_build_goto (label_cont);
3363 /* GIMPLE_COND's are very low level; they have embedded
3364 gotos. This particular embedded goto should not be marked
3365 with the location of the original COND_EXPR, as it would
3366 correspond to the COND_EXPR's condition, not the ELSE or the
3367 THEN arms. To avoid marking it with the wrong location, flag
3368 it as "no location". */
3369 gimple_set_do_not_emit_location (g);
3371 gimplify_seq_add_stmt (&seq, g);
3375 if (!have_else_clause_p)
3377 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3378 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3380 if (label_cont)
3381 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3383 gimple_pop_condition (pre_p);
3384 gimple_seq_add_seq (pre_p, seq);
3386 if (ret == GS_ERROR)
3387 ; /* Do nothing. */
3388 else if (have_then_clause_p || have_else_clause_p)
3389 ret = GS_ALL_DONE;
3390 else
3392 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3393 expr = TREE_OPERAND (expr, 0);
3394 gimplify_stmt (&expr, pre_p);
3397 *expr_p = NULL;
3398 return ret;
3401 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3402 to be marked addressable.
3404 We cannot rely on such an expression being directly markable if a temporary
3405 has been created by the gimplification. In this case, we create another
3406 temporary and initialize it with a copy, which will become a store after we
3407 mark it addressable. This can happen if the front-end passed us something
3408 that it could not mark addressable yet, like a Fortran pass-by-reference
3409 parameter (int) floatvar. */
3411 static void
3412 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3414 while (handled_component_p (*expr_p))
3415 expr_p = &TREE_OPERAND (*expr_p, 0);
3416 if (is_gimple_reg (*expr_p))
3417 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3420 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3421 a call to __builtin_memcpy. */
3423 static enum gimplify_status
3424 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3425 gimple_seq *seq_p)
3427 tree t, to, to_ptr, from, from_ptr;
3428 gimple gs;
3429 location_t loc = EXPR_LOCATION (*expr_p);
3431 to = TREE_OPERAND (*expr_p, 0);
3432 from = TREE_OPERAND (*expr_p, 1);
3434 /* Mark the RHS addressable. Beware that it may not be possible to do so
3435 directly if a temporary has been created by the gimplification. */
3436 prepare_gimple_addressable (&from, seq_p);
3438 mark_addressable (from);
3439 from_ptr = build_fold_addr_expr_loc (loc, from);
3440 gimplify_arg (&from_ptr, seq_p, loc);
3442 mark_addressable (to);
3443 to_ptr = build_fold_addr_expr_loc (loc, to);
3444 gimplify_arg (&to_ptr, seq_p, loc);
3446 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3448 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3450 if (want_value)
3452 /* tmp = memcpy() */
3453 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3454 gimple_call_set_lhs (gs, t);
3455 gimplify_seq_add_stmt (seq_p, gs);
3457 *expr_p = build_simple_mem_ref (t);
3458 return GS_ALL_DONE;
3461 gimplify_seq_add_stmt (seq_p, gs);
3462 *expr_p = NULL;
3463 return GS_ALL_DONE;
3466 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3467 a call to __builtin_memset. In this case we know that the RHS is
3468 a CONSTRUCTOR with an empty element list. */
3470 static enum gimplify_status
3471 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3472 gimple_seq *seq_p)
3474 tree t, from, to, to_ptr;
3475 gimple gs;
3476 location_t loc = EXPR_LOCATION (*expr_p);
3478 /* Assert our assumptions, to abort instead of producing wrong code
3479 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3480 not be immediately exposed. */
3481 from = TREE_OPERAND (*expr_p, 1);
3482 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3483 from = TREE_OPERAND (from, 0);
3485 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3486 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3488 /* Now proceed. */
3489 to = TREE_OPERAND (*expr_p, 0);
3491 to_ptr = build_fold_addr_expr_loc (loc, to);
3492 gimplify_arg (&to_ptr, seq_p, loc);
3493 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3495 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3497 if (want_value)
3499 /* tmp = memset() */
3500 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3501 gimple_call_set_lhs (gs, t);
3502 gimplify_seq_add_stmt (seq_p, gs);
3504 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3505 return GS_ALL_DONE;
3508 gimplify_seq_add_stmt (seq_p, gs);
3509 *expr_p = NULL;
3510 return GS_ALL_DONE;
3513 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3514 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3515 assignment. Return non-null if we detect a potential overlap. */
3517 struct gimplify_init_ctor_preeval_data
3519 /* The base decl of the lhs object. May be NULL, in which case we
3520 have to assume the lhs is indirect. */
3521 tree lhs_base_decl;
3523 /* The alias set of the lhs object. */
3524 alias_set_type lhs_alias_set;
3527 static tree
3528 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3530 struct gimplify_init_ctor_preeval_data *data
3531 = (struct gimplify_init_ctor_preeval_data *) xdata;
3532 tree t = *tp;
3534 /* If we find the base object, obviously we have overlap. */
3535 if (data->lhs_base_decl == t)
3536 return t;
3538 /* If the constructor component is indirect, determine if we have a
3539 potential overlap with the lhs. The only bits of information we
3540 have to go on at this point are addressability and alias sets. */
3541 if ((INDIRECT_REF_P (t)
3542 || TREE_CODE (t) == MEM_REF)
3543 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3544 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3545 return t;
3547 /* If the constructor component is a call, determine if it can hide a
3548 potential overlap with the lhs through an INDIRECT_REF like above.
3549 ??? Ugh - this is completely broken. In fact this whole analysis
3550 doesn't look conservative. */
3551 if (TREE_CODE (t) == CALL_EXPR)
3553 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3555 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3556 if (POINTER_TYPE_P (TREE_VALUE (type))
3557 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3558 && alias_sets_conflict_p (data->lhs_alias_set,
3559 get_alias_set
3560 (TREE_TYPE (TREE_VALUE (type)))))
3561 return t;
3564 if (IS_TYPE_OR_DECL_P (t))
3565 *walk_subtrees = 0;
3566 return NULL;
3569 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3570 force values that overlap with the lhs (as described by *DATA)
3571 into temporaries. */
3573 static void
3574 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3575 struct gimplify_init_ctor_preeval_data *data)
3577 enum gimplify_status one;
3579 /* If the value is constant, then there's nothing to pre-evaluate. */
3580 if (TREE_CONSTANT (*expr_p))
3582 /* Ensure it does not have side effects, it might contain a reference to
3583 the object we're initializing. */
3584 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3585 return;
3588 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3589 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3590 return;
3592 /* Recurse for nested constructors. */
3593 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3595 unsigned HOST_WIDE_INT ix;
3596 constructor_elt *ce;
3597 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3599 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3600 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3602 return;
3605 /* If this is a variable sized type, we must remember the size. */
3606 maybe_with_size_expr (expr_p);
3608 /* Gimplify the constructor element to something appropriate for the rhs
3609 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3610 the gimplifier will consider this a store to memory. Doing this
3611 gimplification now means that we won't have to deal with complicated
3612 language-specific trees, nor trees like SAVE_EXPR that can induce
3613 exponential search behavior. */
3614 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3615 if (one == GS_ERROR)
3617 *expr_p = NULL;
3618 return;
3621 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3622 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3623 always be true for all scalars, since is_gimple_mem_rhs insists on a
3624 temporary variable for them. */
3625 if (DECL_P (*expr_p))
3626 return;
3628 /* If this is of variable size, we have no choice but to assume it doesn't
3629 overlap since we can't make a temporary for it. */
3630 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3631 return;
3633 /* Otherwise, we must search for overlap ... */
3634 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3635 return;
3637 /* ... and if found, force the value into a temporary. */
3638 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3641 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3642 a RANGE_EXPR in a CONSTRUCTOR for an array.
3644 var = lower;
3645 loop_entry:
3646 object[var] = value;
3647 if (var == upper)
3648 goto loop_exit;
3649 var = var + 1;
3650 goto loop_entry;
3651 loop_exit:
3653 We increment var _after_ the loop exit check because we might otherwise
3654 fail if upper == TYPE_MAX_VALUE (type for upper).
3656 Note that we never have to deal with SAVE_EXPRs here, because this has
3657 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3659 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3660 gimple_seq *, bool);
3662 static void
3663 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3664 tree value, tree array_elt_type,
3665 gimple_seq *pre_p, bool cleared)
3667 tree loop_entry_label, loop_exit_label, fall_thru_label;
3668 tree var, var_type, cref, tmp;
3670 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3671 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3672 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3674 /* Create and initialize the index variable. */
3675 var_type = TREE_TYPE (upper);
3676 var = create_tmp_var (var_type, NULL);
3677 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3679 /* Add the loop entry label. */
3680 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3682 /* Build the reference. */
3683 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3684 var, NULL_TREE, NULL_TREE);
3686 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3687 the store. Otherwise just assign value to the reference. */
3689 if (TREE_CODE (value) == CONSTRUCTOR)
3690 /* NB we might have to call ourself recursively through
3691 gimplify_init_ctor_eval if the value is a constructor. */
3692 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3693 pre_p, cleared);
3694 else
3695 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3697 /* We exit the loop when the index var is equal to the upper bound. */
3698 gimplify_seq_add_stmt (pre_p,
3699 gimple_build_cond (EQ_EXPR, var, upper,
3700 loop_exit_label, fall_thru_label));
3702 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3704 /* Otherwise, increment the index var... */
3705 tmp = build2 (PLUS_EXPR, var_type, var,
3706 fold_convert (var_type, integer_one_node));
3707 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3709 /* ...and jump back to the loop entry. */
3710 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3712 /* Add the loop exit label. */
3713 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3716 /* Return true if FDECL is accessing a field that is zero sized. */
3718 static bool
3719 zero_sized_field_decl (const_tree fdecl)
3721 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3722 && integer_zerop (DECL_SIZE (fdecl)))
3723 return true;
3724 return false;
3727 /* Return true if TYPE is zero sized. */
3729 static bool
3730 zero_sized_type (const_tree type)
3732 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3733 && integer_zerop (TYPE_SIZE (type)))
3734 return true;
3735 return false;
3738 /* A subroutine of gimplify_init_constructor. Generate individual
3739 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3740 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3741 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3742 zeroed first. */
3744 static void
3745 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3746 gimple_seq *pre_p, bool cleared)
3748 tree array_elt_type = NULL;
3749 unsigned HOST_WIDE_INT ix;
3750 tree purpose, value;
3752 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3753 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3755 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3757 tree cref;
3759 /* NULL values are created above for gimplification errors. */
3760 if (value == NULL)
3761 continue;
3763 if (cleared && initializer_zerop (value))
3764 continue;
3766 /* ??? Here's to hoping the front end fills in all of the indices,
3767 so we don't have to figure out what's missing ourselves. */
3768 gcc_assert (purpose);
3770 /* Skip zero-sized fields, unless value has side-effects. This can
3771 happen with calls to functions returning a zero-sized type, which
3772 we shouldn't discard. As a number of downstream passes don't
3773 expect sets of zero-sized fields, we rely on the gimplification of
3774 the MODIFY_EXPR we make below to drop the assignment statement. */
3775 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3776 continue;
3778 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3779 whole range. */
3780 if (TREE_CODE (purpose) == RANGE_EXPR)
3782 tree lower = TREE_OPERAND (purpose, 0);
3783 tree upper = TREE_OPERAND (purpose, 1);
3785 /* If the lower bound is equal to upper, just treat it as if
3786 upper was the index. */
3787 if (simple_cst_equal (lower, upper))
3788 purpose = upper;
3789 else
3791 gimplify_init_ctor_eval_range (object, lower, upper, value,
3792 array_elt_type, pre_p, cleared);
3793 continue;
3797 if (array_elt_type)
3799 /* Do not use bitsizetype for ARRAY_REF indices. */
3800 if (TYPE_DOMAIN (TREE_TYPE (object)))
3801 purpose
3802 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3803 purpose);
3804 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3805 purpose, NULL_TREE, NULL_TREE);
3807 else
3809 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3810 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3811 unshare_expr (object), purpose, NULL_TREE);
3814 if (TREE_CODE (value) == CONSTRUCTOR
3815 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3816 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3817 pre_p, cleared);
3818 else
3820 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3821 gimplify_and_add (init, pre_p);
3822 ggc_free (init);
3827 /* Return the appropriate RHS predicate for this LHS. */
3829 gimple_predicate
3830 rhs_predicate_for (tree lhs)
3832 if (is_gimple_reg (lhs))
3833 return is_gimple_reg_rhs_or_call;
3834 else
3835 return is_gimple_mem_rhs_or_call;
3838 /* Gimplify a C99 compound literal expression. This just means adding
3839 the DECL_EXPR before the current statement and using its anonymous
3840 decl instead. */
3842 static enum gimplify_status
3843 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3844 bool (*gimple_test_f) (tree),
3845 fallback_t fallback)
3847 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3848 tree decl = DECL_EXPR_DECL (decl_s);
3849 tree init = DECL_INITIAL (decl);
3850 /* Mark the decl as addressable if the compound literal
3851 expression is addressable now, otherwise it is marked too late
3852 after we gimplify the initialization expression. */
3853 if (TREE_ADDRESSABLE (*expr_p))
3854 TREE_ADDRESSABLE (decl) = 1;
3855 /* Otherwise, if we don't need an lvalue and have a literal directly
3856 substitute it. Check if it matches the gimple predicate, as
3857 otherwise we'd generate a new temporary, and we can as well just
3858 use the decl we already have. */
3859 else if (!TREE_ADDRESSABLE (decl)
3860 && init
3861 && (fallback & fb_lvalue) == 0
3862 && gimple_test_f (init))
3864 *expr_p = init;
3865 return GS_OK;
3868 /* Preliminarily mark non-addressed complex variables as eligible
3869 for promotion to gimple registers. We'll transform their uses
3870 as we find them. */
3871 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3872 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3873 && !TREE_THIS_VOLATILE (decl)
3874 && !needs_to_live_in_memory (decl))
3875 DECL_GIMPLE_REG_P (decl) = 1;
3877 /* If the decl is not addressable, then it is being used in some
3878 expression or on the right hand side of a statement, and it can
3879 be put into a readonly data section. */
3880 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3881 TREE_READONLY (decl) = 1;
3883 /* This decl isn't mentioned in the enclosing block, so add it to the
3884 list of temps. FIXME it seems a bit of a kludge to say that
3885 anonymous artificial vars aren't pushed, but everything else is. */
3886 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3887 gimple_add_tmp_var (decl);
3889 gimplify_and_add (decl_s, pre_p);
3890 *expr_p = decl;
3891 return GS_OK;
3894 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3895 return a new CONSTRUCTOR if something changed. */
3897 static tree
3898 optimize_compound_literals_in_ctor (tree orig_ctor)
3900 tree ctor = orig_ctor;
3901 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3902 unsigned int idx, num = vec_safe_length (elts);
3904 for (idx = 0; idx < num; idx++)
3906 tree value = (*elts)[idx].value;
3907 tree newval = value;
3908 if (TREE_CODE (value) == CONSTRUCTOR)
3909 newval = optimize_compound_literals_in_ctor (value);
3910 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3912 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3913 tree decl = DECL_EXPR_DECL (decl_s);
3914 tree init = DECL_INITIAL (decl);
3916 if (!TREE_ADDRESSABLE (value)
3917 && !TREE_ADDRESSABLE (decl)
3918 && init
3919 && TREE_CODE (init) == CONSTRUCTOR)
3920 newval = optimize_compound_literals_in_ctor (init);
3922 if (newval == value)
3923 continue;
3925 if (ctor == orig_ctor)
3927 ctor = copy_node (orig_ctor);
3928 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3929 elts = CONSTRUCTOR_ELTS (ctor);
3931 (*elts)[idx].value = newval;
3933 return ctor;
3936 /* A subroutine of gimplify_modify_expr. Break out elements of a
3937 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3939 Note that we still need to clear any elements that don't have explicit
3940 initializers, so if not all elements are initialized we keep the
3941 original MODIFY_EXPR, we just remove all of the constructor elements.
3943 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3944 GS_ERROR if we would have to create a temporary when gimplifying
3945 this constructor. Otherwise, return GS_OK.
3947 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3949 static enum gimplify_status
3950 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3951 bool want_value, bool notify_temp_creation)
3953 tree object, ctor, type;
3954 enum gimplify_status ret;
3955 vec<constructor_elt, va_gc> *elts;
3957 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3959 if (!notify_temp_creation)
3961 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3962 is_gimple_lvalue, fb_lvalue);
3963 if (ret == GS_ERROR)
3964 return ret;
3967 object = TREE_OPERAND (*expr_p, 0);
3968 ctor = TREE_OPERAND (*expr_p, 1) =
3969 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3970 type = TREE_TYPE (ctor);
3971 elts = CONSTRUCTOR_ELTS (ctor);
3972 ret = GS_ALL_DONE;
3974 switch (TREE_CODE (type))
3976 case RECORD_TYPE:
3977 case UNION_TYPE:
3978 case QUAL_UNION_TYPE:
3979 case ARRAY_TYPE:
3981 struct gimplify_init_ctor_preeval_data preeval_data;
3982 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3983 bool cleared, complete_p, valid_const_initializer;
3985 /* Aggregate types must lower constructors to initialization of
3986 individual elements. The exception is that a CONSTRUCTOR node
3987 with no elements indicates zero-initialization of the whole. */
3988 if (vec_safe_is_empty (elts))
3990 if (notify_temp_creation)
3991 return GS_OK;
3992 break;
3995 /* Fetch information about the constructor to direct later processing.
3996 We might want to make static versions of it in various cases, and
3997 can only do so if it known to be a valid constant initializer. */
3998 valid_const_initializer
3999 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4000 &num_ctor_elements, &complete_p);
4002 /* If a const aggregate variable is being initialized, then it
4003 should never be a lose to promote the variable to be static. */
4004 if (valid_const_initializer
4005 && num_nonzero_elements > 1
4006 && TREE_READONLY (object)
4007 && TREE_CODE (object) == VAR_DECL
4008 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4010 if (notify_temp_creation)
4011 return GS_ERROR;
4012 DECL_INITIAL (object) = ctor;
4013 TREE_STATIC (object) = 1;
4014 if (!DECL_NAME (object))
4015 DECL_NAME (object) = create_tmp_var_name ("C");
4016 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4018 /* ??? C++ doesn't automatically append a .<number> to the
4019 assembler name, and even when it does, it looks at FE private
4020 data structures to figure out what that number should be,
4021 which are not set for this variable. I suppose this is
4022 important for local statics for inline functions, which aren't
4023 "local" in the object file sense. So in order to get a unique
4024 TU-local symbol, we must invoke the lhd version now. */
4025 lhd_set_decl_assembler_name (object);
4027 *expr_p = NULL_TREE;
4028 break;
4031 /* If there are "lots" of initialized elements, even discounting
4032 those that are not address constants (and thus *must* be
4033 computed at runtime), then partition the constructor into
4034 constant and non-constant parts. Block copy the constant
4035 parts in, then generate code for the non-constant parts. */
4036 /* TODO. There's code in cp/typeck.c to do this. */
4038 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4039 /* store_constructor will ignore the clearing of variable-sized
4040 objects. Initializers for such objects must explicitly set
4041 every field that needs to be set. */
4042 cleared = false;
4043 else if (!complete_p)
4044 /* If the constructor isn't complete, clear the whole object
4045 beforehand.
4047 ??? This ought not to be needed. For any element not present
4048 in the initializer, we should simply set them to zero. Except
4049 we'd need to *find* the elements that are not present, and that
4050 requires trickery to avoid quadratic compile-time behavior in
4051 large cases or excessive memory use in small cases. */
4052 cleared = true;
4053 else if (num_ctor_elements - num_nonzero_elements
4054 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4055 && num_nonzero_elements < num_ctor_elements / 4)
4056 /* If there are "lots" of zeros, it's more efficient to clear
4057 the memory and then set the nonzero elements. */
4058 cleared = true;
4059 else
4060 cleared = false;
4062 /* If there are "lots" of initialized elements, and all of them
4063 are valid address constants, then the entire initializer can
4064 be dropped to memory, and then memcpy'd out. Don't do this
4065 for sparse arrays, though, as it's more efficient to follow
4066 the standard CONSTRUCTOR behavior of memset followed by
4067 individual element initialization. Also don't do this for small
4068 all-zero initializers (which aren't big enough to merit
4069 clearing), and don't try to make bitwise copies of
4070 TREE_ADDRESSABLE types. */
4071 if (valid_const_initializer
4072 && !(cleared || num_nonzero_elements == 0)
4073 && !TREE_ADDRESSABLE (type))
4075 HOST_WIDE_INT size = int_size_in_bytes (type);
4076 unsigned int align;
4078 /* ??? We can still get unbounded array types, at least
4079 from the C++ front end. This seems wrong, but attempt
4080 to work around it for now. */
4081 if (size < 0)
4083 size = int_size_in_bytes (TREE_TYPE (object));
4084 if (size >= 0)
4085 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4088 /* Find the maximum alignment we can assume for the object. */
4089 /* ??? Make use of DECL_OFFSET_ALIGN. */
4090 if (DECL_P (object))
4091 align = DECL_ALIGN (object);
4092 else
4093 align = TYPE_ALIGN (type);
4095 /* Do a block move either if the size is so small as to make
4096 each individual move a sub-unit move on average, or if it
4097 is so large as to make individual moves inefficient. */
4098 if (size > 0
4099 && num_nonzero_elements > 1
4100 && (size < num_nonzero_elements
4101 || !can_move_by_pieces (size, align)))
4103 if (notify_temp_creation)
4104 return GS_ERROR;
4106 walk_tree (&ctor, force_labels_r, NULL, NULL);
4107 ctor = tree_output_constant_def (ctor);
4108 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4109 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4110 TREE_OPERAND (*expr_p, 1) = ctor;
4112 /* This is no longer an assignment of a CONSTRUCTOR, but
4113 we still may have processing to do on the LHS. So
4114 pretend we didn't do anything here to let that happen. */
4115 return GS_UNHANDLED;
4119 /* If the target is volatile, we have non-zero elements and more than
4120 one field to assign, initialize the target from a temporary. */
4121 if (TREE_THIS_VOLATILE (object)
4122 && !TREE_ADDRESSABLE (type)
4123 && num_nonzero_elements > 0
4124 && vec_safe_length (elts) > 1)
4126 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4127 TREE_OPERAND (*expr_p, 0) = temp;
4128 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4129 *expr_p,
4130 build2 (MODIFY_EXPR, void_type_node,
4131 object, temp));
4132 return GS_OK;
4135 if (notify_temp_creation)
4136 return GS_OK;
4138 /* If there are nonzero elements and if needed, pre-evaluate to capture
4139 elements overlapping with the lhs into temporaries. We must do this
4140 before clearing to fetch the values before they are zeroed-out. */
4141 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4143 preeval_data.lhs_base_decl = get_base_address (object);
4144 if (!DECL_P (preeval_data.lhs_base_decl))
4145 preeval_data.lhs_base_decl = NULL;
4146 preeval_data.lhs_alias_set = get_alias_set (object);
4148 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4149 pre_p, post_p, &preeval_data);
4152 if (cleared)
4154 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4155 Note that we still have to gimplify, in order to handle the
4156 case of variable sized types. Avoid shared tree structures. */
4157 CONSTRUCTOR_ELTS (ctor) = NULL;
4158 TREE_SIDE_EFFECTS (ctor) = 0;
4159 object = unshare_expr (object);
4160 gimplify_stmt (expr_p, pre_p);
4163 /* If we have not block cleared the object, or if there are nonzero
4164 elements in the constructor, add assignments to the individual
4165 scalar fields of the object. */
4166 if (!cleared || num_nonzero_elements > 0)
4167 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4169 *expr_p = NULL_TREE;
4171 break;
4173 case COMPLEX_TYPE:
4175 tree r, i;
4177 if (notify_temp_creation)
4178 return GS_OK;
4180 /* Extract the real and imaginary parts out of the ctor. */
4181 gcc_assert (elts->length () == 2);
4182 r = (*elts)[0].value;
4183 i = (*elts)[1].value;
4184 if (r == NULL || i == NULL)
4186 tree zero = build_zero_cst (TREE_TYPE (type));
4187 if (r == NULL)
4188 r = zero;
4189 if (i == NULL)
4190 i = zero;
4193 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4194 represent creation of a complex value. */
4195 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4197 ctor = build_complex (type, r, i);
4198 TREE_OPERAND (*expr_p, 1) = ctor;
4200 else
4202 ctor = build2 (COMPLEX_EXPR, type, r, i);
4203 TREE_OPERAND (*expr_p, 1) = ctor;
4204 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4205 pre_p,
4206 post_p,
4207 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4208 fb_rvalue);
4211 break;
4213 case VECTOR_TYPE:
4215 unsigned HOST_WIDE_INT ix;
4216 constructor_elt *ce;
4218 if (notify_temp_creation)
4219 return GS_OK;
4221 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4222 if (TREE_CONSTANT (ctor))
4224 bool constant_p = true;
4225 tree value;
4227 /* Even when ctor is constant, it might contain non-*_CST
4228 elements, such as addresses or trapping values like
4229 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4230 in VECTOR_CST nodes. */
4231 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4232 if (!CONSTANT_CLASS_P (value))
4234 constant_p = false;
4235 break;
4238 if (constant_p)
4240 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4241 break;
4244 /* Don't reduce an initializer constant even if we can't
4245 make a VECTOR_CST. It won't do anything for us, and it'll
4246 prevent us from representing it as a single constant. */
4247 if (initializer_constant_valid_p (ctor, type))
4248 break;
4250 TREE_CONSTANT (ctor) = 0;
4253 /* Vector types use CONSTRUCTOR all the way through gimple
4254 compilation as a general initializer. */
4255 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4257 enum gimplify_status tret;
4258 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4259 fb_rvalue);
4260 if (tret == GS_ERROR)
4261 ret = GS_ERROR;
4263 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4264 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4266 break;
4268 default:
4269 /* So how did we get a CONSTRUCTOR for a scalar type? */
4270 gcc_unreachable ();
4273 if (ret == GS_ERROR)
4274 return GS_ERROR;
4275 else if (want_value)
4277 *expr_p = object;
4278 return GS_OK;
4280 else
4282 /* If we have gimplified both sides of the initializer but have
4283 not emitted an assignment, do so now. */
4284 if (*expr_p)
4286 tree lhs = TREE_OPERAND (*expr_p, 0);
4287 tree rhs = TREE_OPERAND (*expr_p, 1);
4288 gimple init = gimple_build_assign (lhs, rhs);
4289 gimplify_seq_add_stmt (pre_p, init);
4290 *expr_p = NULL;
4293 return GS_ALL_DONE;
4297 /* Given a pointer value OP0, return a simplified version of an
4298 indirection through OP0, or NULL_TREE if no simplification is
4299 possible. Note that the resulting type may be different from
4300 the type pointed to in the sense that it is still compatible
4301 from the langhooks point of view. */
4303 tree
4304 gimple_fold_indirect_ref (tree t)
4306 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4307 tree sub = t;
4308 tree subtype;
4310 STRIP_NOPS (sub);
4311 subtype = TREE_TYPE (sub);
4312 if (!POINTER_TYPE_P (subtype))
4313 return NULL_TREE;
4315 if (TREE_CODE (sub) == ADDR_EXPR)
4317 tree op = TREE_OPERAND (sub, 0);
4318 tree optype = TREE_TYPE (op);
4319 /* *&p => p */
4320 if (useless_type_conversion_p (type, optype))
4321 return op;
4323 /* *(foo *)&fooarray => fooarray[0] */
4324 if (TREE_CODE (optype) == ARRAY_TYPE
4325 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4326 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4328 tree type_domain = TYPE_DOMAIN (optype);
4329 tree min_val = size_zero_node;
4330 if (type_domain && TYPE_MIN_VALUE (type_domain))
4331 min_val = TYPE_MIN_VALUE (type_domain);
4332 if (TREE_CODE (min_val) == INTEGER_CST)
4333 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4335 /* *(foo *)&complexfoo => __real__ complexfoo */
4336 else if (TREE_CODE (optype) == COMPLEX_TYPE
4337 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4338 return fold_build1 (REALPART_EXPR, type, op);
4339 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4340 else if (TREE_CODE (optype) == VECTOR_TYPE
4341 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4343 tree part_width = TYPE_SIZE (type);
4344 tree index = bitsize_int (0);
4345 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4349 /* *(p + CST) -> ... */
4350 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4351 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4353 tree addr = TREE_OPERAND (sub, 0);
4354 tree off = TREE_OPERAND (sub, 1);
4355 tree addrtype;
4357 STRIP_NOPS (addr);
4358 addrtype = TREE_TYPE (addr);
4360 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4361 if (TREE_CODE (addr) == ADDR_EXPR
4362 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4363 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4364 && host_integerp (off, 1))
4366 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4367 tree part_width = TYPE_SIZE (type);
4368 unsigned HOST_WIDE_INT part_widthi
4369 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4370 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4371 tree index = bitsize_int (indexi);
4372 if (offset / part_widthi
4373 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4374 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4375 part_width, index);
4378 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4379 if (TREE_CODE (addr) == ADDR_EXPR
4380 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4381 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4383 tree size = TYPE_SIZE_UNIT (type);
4384 if (tree_int_cst_equal (size, off))
4385 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4388 /* *(p + CST) -> MEM_REF <p, CST>. */
4389 if (TREE_CODE (addr) != ADDR_EXPR
4390 || DECL_P (TREE_OPERAND (addr, 0)))
4391 return fold_build2 (MEM_REF, type,
4392 addr,
4393 build_int_cst_wide (ptype,
4394 TREE_INT_CST_LOW (off),
4395 TREE_INT_CST_HIGH (off)));
4398 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4399 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4400 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4401 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4403 tree type_domain;
4404 tree min_val = size_zero_node;
4405 tree osub = sub;
4406 sub = gimple_fold_indirect_ref (sub);
4407 if (! sub)
4408 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4409 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4410 if (type_domain && TYPE_MIN_VALUE (type_domain))
4411 min_val = TYPE_MIN_VALUE (type_domain);
4412 if (TREE_CODE (min_val) == INTEGER_CST)
4413 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4416 return NULL_TREE;
4419 /* Given a pointer value OP0, return a simplified version of an
4420 indirection through OP0, or NULL_TREE if no simplification is
4421 possible. This may only be applied to a rhs of an expression.
4422 Note that the resulting type may be different from the type pointed
4423 to in the sense that it is still compatible from the langhooks
4424 point of view. */
4426 static tree
4427 gimple_fold_indirect_ref_rhs (tree t)
4429 return gimple_fold_indirect_ref (t);
4432 /* Subroutine of gimplify_modify_expr to do simplifications of
4433 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4434 something changes. */
4436 static enum gimplify_status
4437 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4438 gimple_seq *pre_p, gimple_seq *post_p,
4439 bool want_value)
4441 enum gimplify_status ret = GS_UNHANDLED;
4442 bool changed;
4446 changed = false;
4447 switch (TREE_CODE (*from_p))
4449 case VAR_DECL:
4450 /* If we're assigning from a read-only variable initialized with
4451 a constructor, do the direct assignment from the constructor,
4452 but only if neither source nor target are volatile since this
4453 latter assignment might end up being done on a per-field basis. */
4454 if (DECL_INITIAL (*from_p)
4455 && TREE_READONLY (*from_p)
4456 && !TREE_THIS_VOLATILE (*from_p)
4457 && !TREE_THIS_VOLATILE (*to_p)
4458 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4460 tree old_from = *from_p;
4461 enum gimplify_status subret;
4463 /* Move the constructor into the RHS. */
4464 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4466 /* Let's see if gimplify_init_constructor will need to put
4467 it in memory. */
4468 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4469 false, true);
4470 if (subret == GS_ERROR)
4472 /* If so, revert the change. */
4473 *from_p = old_from;
4475 else
4477 ret = GS_OK;
4478 changed = true;
4481 break;
4482 case INDIRECT_REF:
4484 /* If we have code like
4486 *(const A*)(A*)&x
4488 where the type of "x" is a (possibly cv-qualified variant
4489 of "A"), treat the entire expression as identical to "x".
4490 This kind of code arises in C++ when an object is bound
4491 to a const reference, and if "x" is a TARGET_EXPR we want
4492 to take advantage of the optimization below. */
4493 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4494 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4495 if (t)
4497 if (TREE_THIS_VOLATILE (t) != volatile_p)
4499 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4500 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4501 build_fold_addr_expr (t));
4502 if (REFERENCE_CLASS_P (t))
4503 TREE_THIS_VOLATILE (t) = volatile_p;
4505 *from_p = t;
4506 ret = GS_OK;
4507 changed = true;
4509 break;
4512 case TARGET_EXPR:
4514 /* If we are initializing something from a TARGET_EXPR, strip the
4515 TARGET_EXPR and initialize it directly, if possible. This can't
4516 be done if the initializer is void, since that implies that the
4517 temporary is set in some non-trivial way.
4519 ??? What about code that pulls out the temp and uses it
4520 elsewhere? I think that such code never uses the TARGET_EXPR as
4521 an initializer. If I'm wrong, we'll die because the temp won't
4522 have any RTL. In that case, I guess we'll need to replace
4523 references somehow. */
4524 tree init = TARGET_EXPR_INITIAL (*from_p);
4526 if (init
4527 && !VOID_TYPE_P (TREE_TYPE (init)))
4529 *from_p = init;
4530 ret = GS_OK;
4531 changed = true;
4534 break;
4536 case COMPOUND_EXPR:
4537 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4538 caught. */
4539 gimplify_compound_expr (from_p, pre_p, true);
4540 ret = GS_OK;
4541 changed = true;
4542 break;
4544 case CONSTRUCTOR:
4545 /* If we already made some changes, let the front end have a
4546 crack at this before we break it down. */
4547 if (ret != GS_UNHANDLED)
4548 break;
4549 /* If we're initializing from a CONSTRUCTOR, break this into
4550 individual MODIFY_EXPRs. */
4551 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4552 false);
4554 case COND_EXPR:
4555 /* If we're assigning to a non-register type, push the assignment
4556 down into the branches. This is mandatory for ADDRESSABLE types,
4557 since we cannot generate temporaries for such, but it saves a
4558 copy in other cases as well. */
4559 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4561 /* This code should mirror the code in gimplify_cond_expr. */
4562 enum tree_code code = TREE_CODE (*expr_p);
4563 tree cond = *from_p;
4564 tree result = *to_p;
4566 ret = gimplify_expr (&result, pre_p, post_p,
4567 is_gimple_lvalue, fb_lvalue);
4568 if (ret != GS_ERROR)
4569 ret = GS_OK;
4571 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4572 TREE_OPERAND (cond, 1)
4573 = build2 (code, void_type_node, result,
4574 TREE_OPERAND (cond, 1));
4575 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4576 TREE_OPERAND (cond, 2)
4577 = build2 (code, void_type_node, unshare_expr (result),
4578 TREE_OPERAND (cond, 2));
4580 TREE_TYPE (cond) = void_type_node;
4581 recalculate_side_effects (cond);
4583 if (want_value)
4585 gimplify_and_add (cond, pre_p);
4586 *expr_p = unshare_expr (result);
4588 else
4589 *expr_p = cond;
4590 return ret;
4592 break;
4594 case CALL_EXPR:
4595 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4596 return slot so that we don't generate a temporary. */
4597 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4598 && aggregate_value_p (*from_p, *from_p))
4600 bool use_target;
4602 if (!(rhs_predicate_for (*to_p))(*from_p))
4603 /* If we need a temporary, *to_p isn't accurate. */
4604 use_target = false;
4605 /* It's OK to use the return slot directly unless it's an NRV. */
4606 else if (TREE_CODE (*to_p) == RESULT_DECL
4607 && DECL_NAME (*to_p) == NULL_TREE
4608 && needs_to_live_in_memory (*to_p))
4609 use_target = true;
4610 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4611 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4612 /* Don't force regs into memory. */
4613 use_target = false;
4614 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4615 /* It's OK to use the target directly if it's being
4616 initialized. */
4617 use_target = true;
4618 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4619 /* Always use the target and thus RSO for variable-sized types.
4620 GIMPLE cannot deal with a variable-sized assignment
4621 embedded in a call statement. */
4622 use_target = true;
4623 else if (TREE_CODE (*to_p) != SSA_NAME
4624 && (!is_gimple_variable (*to_p)
4625 || needs_to_live_in_memory (*to_p)))
4626 /* Don't use the original target if it's already addressable;
4627 if its address escapes, and the called function uses the
4628 NRV optimization, a conforming program could see *to_p
4629 change before the called function returns; see c++/19317.
4630 When optimizing, the return_slot pass marks more functions
4631 as safe after we have escape info. */
4632 use_target = false;
4633 else
4634 use_target = true;
4636 if (use_target)
4638 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4639 mark_addressable (*to_p);
4642 break;
4644 case WITH_SIZE_EXPR:
4645 /* Likewise for calls that return an aggregate of non-constant size,
4646 since we would not be able to generate a temporary at all. */
4647 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4649 *from_p = TREE_OPERAND (*from_p, 0);
4650 /* We don't change ret in this case because the
4651 WITH_SIZE_EXPR might have been added in
4652 gimplify_modify_expr, so returning GS_OK would lead to an
4653 infinite loop. */
4654 changed = true;
4656 break;
4658 /* If we're initializing from a container, push the initialization
4659 inside it. */
4660 case CLEANUP_POINT_EXPR:
4661 case BIND_EXPR:
4662 case STATEMENT_LIST:
4664 tree wrap = *from_p;
4665 tree t;
4667 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4668 fb_lvalue);
4669 if (ret != GS_ERROR)
4670 ret = GS_OK;
4672 t = voidify_wrapper_expr (wrap, *expr_p);
4673 gcc_assert (t == *expr_p);
4675 if (want_value)
4677 gimplify_and_add (wrap, pre_p);
4678 *expr_p = unshare_expr (*to_p);
4680 else
4681 *expr_p = wrap;
4682 return GS_OK;
4685 case COMPOUND_LITERAL_EXPR:
4687 tree complit = TREE_OPERAND (*expr_p, 1);
4688 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4689 tree decl = DECL_EXPR_DECL (decl_s);
4690 tree init = DECL_INITIAL (decl);
4692 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4693 into struct T x = { 0, 1, 2 } if the address of the
4694 compound literal has never been taken. */
4695 if (!TREE_ADDRESSABLE (complit)
4696 && !TREE_ADDRESSABLE (decl)
4697 && init)
4699 *expr_p = copy_node (*expr_p);
4700 TREE_OPERAND (*expr_p, 1) = init;
4701 return GS_OK;
4705 default:
4706 break;
4709 while (changed);
4711 return ret;
4715 /* Return true if T looks like a valid GIMPLE statement. */
4717 static bool
4718 is_gimple_stmt (tree t)
4720 const enum tree_code code = TREE_CODE (t);
4722 switch (code)
4724 case NOP_EXPR:
4725 /* The only valid NOP_EXPR is the empty statement. */
4726 return IS_EMPTY_STMT (t);
4728 case BIND_EXPR:
4729 case COND_EXPR:
4730 /* These are only valid if they're void. */
4731 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4733 case SWITCH_EXPR:
4734 case GOTO_EXPR:
4735 case RETURN_EXPR:
4736 case LABEL_EXPR:
4737 case CASE_LABEL_EXPR:
4738 case TRY_CATCH_EXPR:
4739 case TRY_FINALLY_EXPR:
4740 case EH_FILTER_EXPR:
4741 case CATCH_EXPR:
4742 case ASM_EXPR:
4743 case STATEMENT_LIST:
4744 case OMP_PARALLEL:
4745 case OMP_FOR:
4746 case OMP_SECTIONS:
4747 case OMP_SECTION:
4748 case OMP_SINGLE:
4749 case OMP_MASTER:
4750 case OMP_ORDERED:
4751 case OMP_CRITICAL:
4752 case OMP_TASK:
4753 /* These are always void. */
4754 return true;
4756 case CALL_EXPR:
4757 case MODIFY_EXPR:
4758 case PREDICT_EXPR:
4759 /* These are valid regardless of their type. */
4760 return true;
4762 default:
4763 return false;
4768 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4769 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4770 DECL_GIMPLE_REG_P set.
4772 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4773 other, unmodified part of the complex object just before the total store.
4774 As a consequence, if the object is still uninitialized, an undefined value
4775 will be loaded into a register, which may result in a spurious exception
4776 if the register is floating-point and the value happens to be a signaling
4777 NaN for example. Then the fully-fledged complex operations lowering pass
4778 followed by a DCE pass are necessary in order to fix things up. */
4780 static enum gimplify_status
4781 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4782 bool want_value)
4784 enum tree_code code, ocode;
4785 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4787 lhs = TREE_OPERAND (*expr_p, 0);
4788 rhs = TREE_OPERAND (*expr_p, 1);
4789 code = TREE_CODE (lhs);
4790 lhs = TREE_OPERAND (lhs, 0);
4792 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4793 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4794 TREE_NO_WARNING (other) = 1;
4795 other = get_formal_tmp_var (other, pre_p);
4797 realpart = code == REALPART_EXPR ? rhs : other;
4798 imagpart = code == REALPART_EXPR ? other : rhs;
4800 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4801 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4802 else
4803 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4805 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4806 *expr_p = (want_value) ? rhs : NULL_TREE;
4808 return GS_ALL_DONE;
4811 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4813 modify_expr
4814 : varname '=' rhs
4815 | '*' ID '=' rhs
4817 PRE_P points to the list where side effects that must happen before
4818 *EXPR_P should be stored.
4820 POST_P points to the list where side effects that must happen after
4821 *EXPR_P should be stored.
4823 WANT_VALUE is nonzero iff we want to use the value of this expression
4824 in another expression. */
4826 static enum gimplify_status
4827 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4828 bool want_value)
4830 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4831 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4832 enum gimplify_status ret = GS_UNHANDLED;
4833 gimple assign;
4834 location_t loc = EXPR_LOCATION (*expr_p);
4835 gimple_stmt_iterator gsi;
4837 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4838 || TREE_CODE (*expr_p) == INIT_EXPR);
4840 /* Trying to simplify a clobber using normal logic doesn't work,
4841 so handle it here. */
4842 if (TREE_CLOBBER_P (*from_p))
4844 gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4845 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4846 *expr_p = NULL;
4847 return GS_ALL_DONE;
4850 /* Insert pointer conversions required by the middle-end that are not
4851 required by the frontend. This fixes middle-end type checking for
4852 for example gcc.dg/redecl-6.c. */
4853 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4855 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4856 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4857 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4860 /* See if any simplifications can be done based on what the RHS is. */
4861 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4862 want_value);
4863 if (ret != GS_UNHANDLED)
4864 return ret;
4866 /* For zero sized types only gimplify the left hand side and right hand
4867 side as statements and throw away the assignment. Do this after
4868 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4869 types properly. */
4870 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4872 gimplify_stmt (from_p, pre_p);
4873 gimplify_stmt (to_p, pre_p);
4874 *expr_p = NULL_TREE;
4875 return GS_ALL_DONE;
4878 /* If the value being copied is of variable width, compute the length
4879 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4880 before gimplifying any of the operands so that we can resolve any
4881 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4882 the size of the expression to be copied, not of the destination, so
4883 that is what we must do here. */
4884 maybe_with_size_expr (from_p);
4886 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4887 if (ret == GS_ERROR)
4888 return ret;
4890 /* As a special case, we have to temporarily allow for assignments
4891 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4892 a toplevel statement, when gimplifying the GENERIC expression
4893 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4894 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4896 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4897 prevent gimplify_expr from trying to create a new temporary for
4898 foo's LHS, we tell it that it should only gimplify until it
4899 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4900 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4901 and all we need to do here is set 'a' to be its LHS. */
4902 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4903 fb_rvalue);
4904 if (ret == GS_ERROR)
4905 return ret;
4907 /* Now see if the above changed *from_p to something we handle specially. */
4908 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4909 want_value);
4910 if (ret != GS_UNHANDLED)
4911 return ret;
4913 /* If we've got a variable sized assignment between two lvalues (i.e. does
4914 not involve a call), then we can make things a bit more straightforward
4915 by converting the assignment to memcpy or memset. */
4916 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4918 tree from = TREE_OPERAND (*from_p, 0);
4919 tree size = TREE_OPERAND (*from_p, 1);
4921 if (TREE_CODE (from) == CONSTRUCTOR)
4922 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4924 if (is_gimple_addressable (from))
4926 *from_p = from;
4927 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4928 pre_p);
4932 /* Transform partial stores to non-addressable complex variables into
4933 total stores. This allows us to use real instead of virtual operands
4934 for these variables, which improves optimization. */
4935 if ((TREE_CODE (*to_p) == REALPART_EXPR
4936 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4937 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4938 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4940 /* Try to alleviate the effects of the gimplification creating artificial
4941 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4942 if (!gimplify_ctxp->into_ssa
4943 && TREE_CODE (*from_p) == VAR_DECL
4944 && DECL_IGNORED_P (*from_p)
4945 && DECL_P (*to_p)
4946 && !DECL_IGNORED_P (*to_p))
4948 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4949 DECL_NAME (*from_p)
4950 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4951 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4952 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4955 if (want_value && TREE_THIS_VOLATILE (*to_p))
4956 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4958 if (TREE_CODE (*from_p) == CALL_EXPR)
4960 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4961 instead of a GIMPLE_ASSIGN. */
4962 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4963 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4964 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4965 assign = gimple_build_call_from_tree (*from_p);
4966 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4967 if (!gimple_call_noreturn_p (assign))
4968 gimple_call_set_lhs (assign, *to_p);
4970 else
4972 assign = gimple_build_assign (*to_p, *from_p);
4973 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4976 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4978 /* We should have got an SSA name from the start. */
4979 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4982 gimplify_seq_add_stmt (pre_p, assign);
4983 gsi = gsi_last (*pre_p);
4984 fold_stmt (&gsi);
4986 if (want_value)
4988 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4989 return GS_OK;
4991 else
4992 *expr_p = NULL;
4994 return GS_ALL_DONE;
4997 /* Gimplify a comparison between two variable-sized objects. Do this
4998 with a call to BUILT_IN_MEMCMP. */
5000 static enum gimplify_status
5001 gimplify_variable_sized_compare (tree *expr_p)
5003 location_t loc = EXPR_LOCATION (*expr_p);
5004 tree op0 = TREE_OPERAND (*expr_p, 0);
5005 tree op1 = TREE_OPERAND (*expr_p, 1);
5006 tree t, arg, dest, src, expr;
5008 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5009 arg = unshare_expr (arg);
5010 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5011 src = build_fold_addr_expr_loc (loc, op1);
5012 dest = build_fold_addr_expr_loc (loc, op0);
5013 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5014 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5016 expr
5017 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5018 SET_EXPR_LOCATION (expr, loc);
5019 *expr_p = expr;
5021 return GS_OK;
5024 /* Gimplify a comparison between two aggregate objects of integral scalar
5025 mode as a comparison between the bitwise equivalent scalar values. */
5027 static enum gimplify_status
5028 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5030 location_t loc = EXPR_LOCATION (*expr_p);
5031 tree op0 = TREE_OPERAND (*expr_p, 0);
5032 tree op1 = TREE_OPERAND (*expr_p, 1);
5034 tree type = TREE_TYPE (op0);
5035 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5037 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5038 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5040 *expr_p
5041 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5043 return GS_OK;
5046 /* Gimplify an expression sequence. This function gimplifies each
5047 expression and rewrites the original expression with the last
5048 expression of the sequence in GIMPLE form.
5050 PRE_P points to the list where the side effects for all the
5051 expressions in the sequence will be emitted.
5053 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5055 static enum gimplify_status
5056 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5058 tree t = *expr_p;
5062 tree *sub_p = &TREE_OPERAND (t, 0);
5064 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5065 gimplify_compound_expr (sub_p, pre_p, false);
5066 else
5067 gimplify_stmt (sub_p, pre_p);
5069 t = TREE_OPERAND (t, 1);
5071 while (TREE_CODE (t) == COMPOUND_EXPR);
5073 *expr_p = t;
5074 if (want_value)
5075 return GS_OK;
5076 else
5078 gimplify_stmt (expr_p, pre_p);
5079 return GS_ALL_DONE;
5083 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5084 gimplify. After gimplification, EXPR_P will point to a new temporary
5085 that holds the original value of the SAVE_EXPR node.
5087 PRE_P points to the list where side effects that must happen before
5088 *EXPR_P should be stored. */
5090 static enum gimplify_status
5091 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5093 enum gimplify_status ret = GS_ALL_DONE;
5094 tree val;
5096 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5097 val = TREE_OPERAND (*expr_p, 0);
5099 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5100 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5102 /* The operand may be a void-valued expression such as SAVE_EXPRs
5103 generated by the Java frontend for class initialization. It is
5104 being executed only for its side-effects. */
5105 if (TREE_TYPE (val) == void_type_node)
5107 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5108 is_gimple_stmt, fb_none);
5109 val = NULL;
5111 else
5112 val = get_initialized_tmp_var (val, pre_p, post_p);
5114 TREE_OPERAND (*expr_p, 0) = val;
5115 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5118 *expr_p = val;
5120 return ret;
5123 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5125 unary_expr
5126 : ...
5127 | '&' varname
5130 PRE_P points to the list where side effects that must happen before
5131 *EXPR_P should be stored.
5133 POST_P points to the list where side effects that must happen after
5134 *EXPR_P should be stored. */
5136 static enum gimplify_status
5137 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5139 tree expr = *expr_p;
5140 tree op0 = TREE_OPERAND (expr, 0);
5141 enum gimplify_status ret;
5142 location_t loc = EXPR_LOCATION (*expr_p);
5144 switch (TREE_CODE (op0))
5146 case INDIRECT_REF:
5147 do_indirect_ref:
5148 /* Check if we are dealing with an expression of the form '&*ptr'.
5149 While the front end folds away '&*ptr' into 'ptr', these
5150 expressions may be generated internally by the compiler (e.g.,
5151 builtins like __builtin_va_end). */
5152 /* Caution: the silent array decomposition semantics we allow for
5153 ADDR_EXPR means we can't always discard the pair. */
5154 /* Gimplification of the ADDR_EXPR operand may drop
5155 cv-qualification conversions, so make sure we add them if
5156 needed. */
5158 tree op00 = TREE_OPERAND (op0, 0);
5159 tree t_expr = TREE_TYPE (expr);
5160 tree t_op00 = TREE_TYPE (op00);
5162 if (!useless_type_conversion_p (t_expr, t_op00))
5163 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5164 *expr_p = op00;
5165 ret = GS_OK;
5167 break;
5169 case VIEW_CONVERT_EXPR:
5170 /* Take the address of our operand and then convert it to the type of
5171 this ADDR_EXPR.
5173 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5174 all clear. The impact of this transformation is even less clear. */
5176 /* If the operand is a useless conversion, look through it. Doing so
5177 guarantees that the ADDR_EXPR and its operand will remain of the
5178 same type. */
5179 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5180 op0 = TREE_OPERAND (op0, 0);
5182 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5183 build_fold_addr_expr_loc (loc,
5184 TREE_OPERAND (op0, 0)));
5185 ret = GS_OK;
5186 break;
5188 default:
5189 /* We use fb_either here because the C frontend sometimes takes
5190 the address of a call that returns a struct; see
5191 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5192 the implied temporary explicit. */
5194 /* Make the operand addressable. */
5195 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5196 is_gimple_addressable, fb_either);
5197 if (ret == GS_ERROR)
5198 break;
5200 /* Then mark it. Beware that it may not be possible to do so directly
5201 if a temporary has been created by the gimplification. */
5202 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5204 op0 = TREE_OPERAND (expr, 0);
5206 /* For various reasons, the gimplification of the expression
5207 may have made a new INDIRECT_REF. */
5208 if (TREE_CODE (op0) == INDIRECT_REF)
5209 goto do_indirect_ref;
5211 mark_addressable (TREE_OPERAND (expr, 0));
5213 /* The FEs may end up building ADDR_EXPRs early on a decl with
5214 an incomplete type. Re-build ADDR_EXPRs in canonical form
5215 here. */
5216 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5217 *expr_p = build_fold_addr_expr (op0);
5219 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5220 recompute_tree_invariant_for_addr_expr (*expr_p);
5222 /* If we re-built the ADDR_EXPR add a conversion to the original type
5223 if required. */
5224 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5225 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5227 break;
5230 return ret;
5233 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5234 value; output operands should be a gimple lvalue. */
5236 static enum gimplify_status
5237 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5239 tree expr;
5240 int noutputs;
5241 const char **oconstraints;
5242 int i;
5243 tree link;
5244 const char *constraint;
5245 bool allows_mem, allows_reg, is_inout;
5246 enum gimplify_status ret, tret;
5247 gimple stmt;
5248 vec<tree, va_gc> *inputs;
5249 vec<tree, va_gc> *outputs;
5250 vec<tree, va_gc> *clobbers;
5251 vec<tree, va_gc> *labels;
5252 tree link_next;
5254 expr = *expr_p;
5255 noutputs = list_length (ASM_OUTPUTS (expr));
5256 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5258 inputs = NULL;
5259 outputs = NULL;
5260 clobbers = NULL;
5261 labels = NULL;
5263 ret = GS_ALL_DONE;
5264 link_next = NULL_TREE;
5265 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5267 bool ok;
5268 size_t constraint_len;
5270 link_next = TREE_CHAIN (link);
5272 oconstraints[i]
5273 = constraint
5274 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5275 constraint_len = strlen (constraint);
5276 if (constraint_len == 0)
5277 continue;
5279 ok = parse_output_constraint (&constraint, i, 0, 0,
5280 &allows_mem, &allows_reg, &is_inout);
5281 if (!ok)
5283 ret = GS_ERROR;
5284 is_inout = false;
5287 if (!allows_reg && allows_mem)
5288 mark_addressable (TREE_VALUE (link));
5290 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5291 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5292 fb_lvalue | fb_mayfail);
5293 if (tret == GS_ERROR)
5295 error ("invalid lvalue in asm output %d", i);
5296 ret = tret;
5299 vec_safe_push (outputs, link);
5300 TREE_CHAIN (link) = NULL_TREE;
5302 if (is_inout)
5304 /* An input/output operand. To give the optimizers more
5305 flexibility, split it into separate input and output
5306 operands. */
5307 tree input;
5308 char buf[10];
5310 /* Turn the in/out constraint into an output constraint. */
5311 char *p = xstrdup (constraint);
5312 p[0] = '=';
5313 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5315 /* And add a matching input constraint. */
5316 if (allows_reg)
5318 sprintf (buf, "%d", i);
5320 /* If there are multiple alternatives in the constraint,
5321 handle each of them individually. Those that allow register
5322 will be replaced with operand number, the others will stay
5323 unchanged. */
5324 if (strchr (p, ',') != NULL)
5326 size_t len = 0, buflen = strlen (buf);
5327 char *beg, *end, *str, *dst;
5329 for (beg = p + 1;;)
5331 end = strchr (beg, ',');
5332 if (end == NULL)
5333 end = strchr (beg, '\0');
5334 if ((size_t) (end - beg) < buflen)
5335 len += buflen + 1;
5336 else
5337 len += end - beg + 1;
5338 if (*end)
5339 beg = end + 1;
5340 else
5341 break;
5344 str = (char *) alloca (len);
5345 for (beg = p + 1, dst = str;;)
5347 const char *tem;
5348 bool mem_p, reg_p, inout_p;
5350 end = strchr (beg, ',');
5351 if (end)
5352 *end = '\0';
5353 beg[-1] = '=';
5354 tem = beg - 1;
5355 parse_output_constraint (&tem, i, 0, 0,
5356 &mem_p, &reg_p, &inout_p);
5357 if (dst != str)
5358 *dst++ = ',';
5359 if (reg_p)
5361 memcpy (dst, buf, buflen);
5362 dst += buflen;
5364 else
5366 if (end)
5367 len = end - beg;
5368 else
5369 len = strlen (beg);
5370 memcpy (dst, beg, len);
5371 dst += len;
5373 if (end)
5374 beg = end + 1;
5375 else
5376 break;
5378 *dst = '\0';
5379 input = build_string (dst - str, str);
5381 else
5382 input = build_string (strlen (buf), buf);
5384 else
5385 input = build_string (constraint_len - 1, constraint + 1);
5387 free (p);
5389 input = build_tree_list (build_tree_list (NULL_TREE, input),
5390 unshare_expr (TREE_VALUE (link)));
5391 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5395 link_next = NULL_TREE;
5396 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5398 link_next = TREE_CHAIN (link);
5399 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5400 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5401 oconstraints, &allows_mem, &allows_reg);
5403 /* If we can't make copies, we can only accept memory. */
5404 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5406 if (allows_mem)
5407 allows_reg = 0;
5408 else
5410 error ("impossible constraint in %<asm%>");
5411 error ("non-memory input %d must stay in memory", i);
5412 return GS_ERROR;
5416 /* If the operand is a memory input, it should be an lvalue. */
5417 if (!allows_reg && allows_mem)
5419 tree inputv = TREE_VALUE (link);
5420 STRIP_NOPS (inputv);
5421 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5422 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5423 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5424 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5425 TREE_VALUE (link) = error_mark_node;
5426 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5427 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5428 mark_addressable (TREE_VALUE (link));
5429 if (tret == GS_ERROR)
5431 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5432 input_location = EXPR_LOCATION (TREE_VALUE (link));
5433 error ("memory input %d is not directly addressable", i);
5434 ret = tret;
5437 else
5439 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5440 is_gimple_asm_val, fb_rvalue);
5441 if (tret == GS_ERROR)
5442 ret = tret;
5445 TREE_CHAIN (link) = NULL_TREE;
5446 vec_safe_push (inputs, link);
5449 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5450 vec_safe_push (clobbers, link);
5452 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5453 vec_safe_push (labels, link);
5455 /* Do not add ASMs with errors to the gimple IL stream. */
5456 if (ret != GS_ERROR)
5458 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5459 inputs, outputs, clobbers, labels);
5461 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5462 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5464 gimplify_seq_add_stmt (pre_p, stmt);
5467 return ret;
5470 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5471 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5472 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5473 return to this function.
5475 FIXME should we complexify the prequeue handling instead? Or use flags
5476 for all the cleanups and let the optimizer tighten them up? The current
5477 code seems pretty fragile; it will break on a cleanup within any
5478 non-conditional nesting. But any such nesting would be broken, anyway;
5479 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5480 and continues out of it. We can do that at the RTL level, though, so
5481 having an optimizer to tighten up try/finally regions would be a Good
5482 Thing. */
5484 static enum gimplify_status
5485 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5487 gimple_stmt_iterator iter;
5488 gimple_seq body_sequence = NULL;
5490 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5492 /* We only care about the number of conditions between the innermost
5493 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5494 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5495 int old_conds = gimplify_ctxp->conditions;
5496 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5497 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5498 gimplify_ctxp->conditions = 0;
5499 gimplify_ctxp->conditional_cleanups = NULL;
5500 gimplify_ctxp->in_cleanup_point_expr = true;
5502 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5504 gimplify_ctxp->conditions = old_conds;
5505 gimplify_ctxp->conditional_cleanups = old_cleanups;
5506 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5508 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5510 gimple wce = gsi_stmt (iter);
5512 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5514 if (gsi_one_before_end_p (iter))
5516 /* Note that gsi_insert_seq_before and gsi_remove do not
5517 scan operands, unlike some other sequence mutators. */
5518 if (!gimple_wce_cleanup_eh_only (wce))
5519 gsi_insert_seq_before_without_update (&iter,
5520 gimple_wce_cleanup (wce),
5521 GSI_SAME_STMT);
5522 gsi_remove (&iter, true);
5523 break;
5525 else
5527 gimple gtry;
5528 gimple_seq seq;
5529 enum gimple_try_flags kind;
5531 if (gimple_wce_cleanup_eh_only (wce))
5532 kind = GIMPLE_TRY_CATCH;
5533 else
5534 kind = GIMPLE_TRY_FINALLY;
5535 seq = gsi_split_seq_after (iter);
5537 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5538 /* Do not use gsi_replace here, as it may scan operands.
5539 We want to do a simple structural modification only. */
5540 gsi_set_stmt (&iter, gtry);
5541 iter = gsi_start (gtry->gimple_try.eval);
5544 else
5545 gsi_next (&iter);
5548 gimplify_seq_add_seq (pre_p, body_sequence);
5549 if (temp)
5551 *expr_p = temp;
5552 return GS_OK;
5554 else
5556 *expr_p = NULL;
5557 return GS_ALL_DONE;
5561 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5562 is the cleanup action required. EH_ONLY is true if the cleanup should
5563 only be executed if an exception is thrown, not on normal exit. */
5565 static void
5566 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5568 gimple wce;
5569 gimple_seq cleanup_stmts = NULL;
5571 /* Errors can result in improperly nested cleanups. Which results in
5572 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5573 if (seen_error ())
5574 return;
5576 if (gimple_conditional_context ())
5578 /* If we're in a conditional context, this is more complex. We only
5579 want to run the cleanup if we actually ran the initialization that
5580 necessitates it, but we want to run it after the end of the
5581 conditional context. So we wrap the try/finally around the
5582 condition and use a flag to determine whether or not to actually
5583 run the destructor. Thus
5585 test ? f(A()) : 0
5587 becomes (approximately)
5589 flag = 0;
5590 try {
5591 if (test) { A::A(temp); flag = 1; val = f(temp); }
5592 else { val = 0; }
5593 } finally {
5594 if (flag) A::~A(temp);
5598 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5599 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5600 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5602 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5603 gimplify_stmt (&cleanup, &cleanup_stmts);
5604 wce = gimple_build_wce (cleanup_stmts);
5606 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5607 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5608 gimplify_seq_add_stmt (pre_p, ftrue);
5610 /* Because of this manipulation, and the EH edges that jump
5611 threading cannot redirect, the temporary (VAR) will appear
5612 to be used uninitialized. Don't warn. */
5613 TREE_NO_WARNING (var) = 1;
5615 else
5617 gimplify_stmt (&cleanup, &cleanup_stmts);
5618 wce = gimple_build_wce (cleanup_stmts);
5619 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5620 gimplify_seq_add_stmt (pre_p, wce);
5624 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5626 static enum gimplify_status
5627 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5629 tree targ = *expr_p;
5630 tree temp = TARGET_EXPR_SLOT (targ);
5631 tree init = TARGET_EXPR_INITIAL (targ);
5632 enum gimplify_status ret;
5634 if (init)
5636 tree cleanup = NULL_TREE;
5638 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5639 to the temps list. Handle also variable length TARGET_EXPRs. */
5640 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5642 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5643 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5644 gimplify_vla_decl (temp, pre_p);
5646 else
5647 gimple_add_tmp_var (temp);
5649 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5650 expression is supposed to initialize the slot. */
5651 if (VOID_TYPE_P (TREE_TYPE (init)))
5652 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5653 else
5655 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5656 init = init_expr;
5657 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5658 init = NULL;
5659 ggc_free (init_expr);
5661 if (ret == GS_ERROR)
5663 /* PR c++/28266 Make sure this is expanded only once. */
5664 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5665 return GS_ERROR;
5667 if (init)
5668 gimplify_and_add (init, pre_p);
5670 /* If needed, push the cleanup for the temp. */
5671 if (TARGET_EXPR_CLEANUP (targ))
5673 if (CLEANUP_EH_ONLY (targ))
5674 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5675 CLEANUP_EH_ONLY (targ), pre_p);
5676 else
5677 cleanup = TARGET_EXPR_CLEANUP (targ);
5680 /* Add a clobber for the temporary going out of scope, like
5681 gimplify_bind_expr. */
5682 if (gimplify_ctxp->in_cleanup_point_expr
5683 && needs_to_live_in_memory (temp)
5684 && flag_stack_reuse == SR_ALL)
5686 tree clobber = build_constructor (TREE_TYPE (temp),
5687 NULL);
5688 TREE_THIS_VOLATILE (clobber) = true;
5689 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5690 if (cleanup)
5691 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5692 clobber);
5693 else
5694 cleanup = clobber;
5697 if (cleanup)
5698 gimple_push_cleanup (temp, cleanup, false, pre_p);
5700 /* Only expand this once. */
5701 TREE_OPERAND (targ, 3) = init;
5702 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5704 else
5705 /* We should have expanded this before. */
5706 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5708 *expr_p = temp;
5709 return GS_OK;
5712 /* Gimplification of expression trees. */
5714 /* Gimplify an expression which appears at statement context. The
5715 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5716 NULL, a new sequence is allocated.
5718 Return true if we actually added a statement to the queue. */
5720 bool
5721 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5723 gimple_seq_node last;
5725 last = gimple_seq_last (*seq_p);
5726 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5727 return last != gimple_seq_last (*seq_p);
5730 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5731 to CTX. If entries already exist, force them to be some flavor of private.
5732 If there is no enclosing parallel, do nothing. */
5734 void
5735 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5737 splay_tree_node n;
5739 if (decl == NULL || !DECL_P (decl))
5740 return;
5744 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5745 if (n != NULL)
5747 if (n->value & GOVD_SHARED)
5748 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5749 else
5750 return;
5752 else if (ctx->region_type != ORT_WORKSHARE)
5753 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5755 ctx = ctx->outer_context;
5757 while (ctx);
5760 /* Similarly for each of the type sizes of TYPE. */
5762 static void
5763 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5765 if (type == NULL || type == error_mark_node)
5766 return;
5767 type = TYPE_MAIN_VARIANT (type);
5769 if (pointer_set_insert (ctx->privatized_types, type))
5770 return;
5772 switch (TREE_CODE (type))
5774 case INTEGER_TYPE:
5775 case ENUMERAL_TYPE:
5776 case BOOLEAN_TYPE:
5777 case REAL_TYPE:
5778 case FIXED_POINT_TYPE:
5779 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5780 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5781 break;
5783 case ARRAY_TYPE:
5784 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5785 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5786 break;
5788 case RECORD_TYPE:
5789 case UNION_TYPE:
5790 case QUAL_UNION_TYPE:
5792 tree field;
5793 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5794 if (TREE_CODE (field) == FIELD_DECL)
5796 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5797 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5800 break;
5802 case POINTER_TYPE:
5803 case REFERENCE_TYPE:
5804 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5805 break;
5807 default:
5808 break;
5811 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5812 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5813 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5816 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5818 static void
5819 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5821 splay_tree_node n;
5822 unsigned int nflags;
5823 tree t;
5825 if (error_operand_p (decl))
5826 return;
5828 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5829 there are constructors involved somewhere. */
5830 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5831 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5832 flags |= GOVD_SEEN;
5834 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5835 if (n != NULL)
5837 /* We shouldn't be re-adding the decl with the same data
5838 sharing class. */
5839 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5840 /* The only combination of data sharing classes we should see is
5841 FIRSTPRIVATE and LASTPRIVATE. */
5842 nflags = n->value | flags;
5843 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5844 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5845 n->value = nflags;
5846 return;
5849 /* When adding a variable-sized variable, we have to handle all sorts
5850 of additional bits of data: the pointer replacement variable, and
5851 the parameters of the type. */
5852 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5854 /* Add the pointer replacement variable as PRIVATE if the variable
5855 replacement is private, else FIRSTPRIVATE since we'll need the
5856 address of the original variable either for SHARED, or for the
5857 copy into or out of the context. */
5858 if (!(flags & GOVD_LOCAL))
5860 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5861 nflags |= flags & GOVD_SEEN;
5862 t = DECL_VALUE_EXPR (decl);
5863 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5864 t = TREE_OPERAND (t, 0);
5865 gcc_assert (DECL_P (t));
5866 omp_add_variable (ctx, t, nflags);
5869 /* Add all of the variable and type parameters (which should have
5870 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5871 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5872 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5873 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5875 /* The variable-sized variable itself is never SHARED, only some form
5876 of PRIVATE. The sharing would take place via the pointer variable
5877 which we remapped above. */
5878 if (flags & GOVD_SHARED)
5879 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5880 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5882 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5883 alloca statement we generate for the variable, so make sure it
5884 is available. This isn't automatically needed for the SHARED
5885 case, since we won't be allocating local storage then.
5886 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5887 in this case omp_notice_variable will be called later
5888 on when it is gimplified. */
5889 else if (! (flags & GOVD_LOCAL)
5890 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5891 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5893 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5895 gcc_assert ((flags & GOVD_LOCAL) == 0);
5896 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5898 /* Similar to the direct variable sized case above, we'll need the
5899 size of references being privatized. */
5900 if ((flags & GOVD_SHARED) == 0)
5902 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5903 if (TREE_CODE (t) != INTEGER_CST)
5904 omp_notice_variable (ctx, t, true);
5908 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5911 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5912 This just prints out diagnostics about threadprivate variable uses
5913 in untied tasks. If DECL2 is non-NULL, prevent this warning
5914 on that variable. */
5916 static bool
5917 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5918 tree decl2)
5920 splay_tree_node n;
5922 if (ctx->region_type != ORT_UNTIED_TASK)
5923 return false;
5924 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5925 if (n == NULL)
5927 error ("threadprivate variable %qE used in untied task",
5928 DECL_NAME (decl));
5929 error_at (ctx->location, "enclosing task");
5930 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5932 if (decl2)
5933 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5934 return false;
5937 /* Record the fact that DECL was used within the OpenMP context CTX.
5938 IN_CODE is true when real code uses DECL, and false when we should
5939 merely emit default(none) errors. Return true if DECL is going to
5940 be remapped and thus DECL shouldn't be gimplified into its
5941 DECL_VALUE_EXPR (if any). */
5943 static bool
5944 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5946 splay_tree_node n;
5947 unsigned flags = in_code ? GOVD_SEEN : 0;
5948 bool ret = false, shared;
5950 if (error_operand_p (decl))
5951 return false;
5953 /* Threadprivate variables are predetermined. */
5954 if (is_global_var (decl))
5956 if (DECL_THREAD_LOCAL_P (decl))
5957 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5959 if (DECL_HAS_VALUE_EXPR_P (decl))
5961 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5963 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5964 return omp_notice_threadprivate_variable (ctx, decl, value);
5968 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5969 if (n == NULL)
5971 enum omp_clause_default_kind default_kind, kind;
5972 struct gimplify_omp_ctx *octx;
5974 if (ctx->region_type == ORT_WORKSHARE)
5975 goto do_outer;
5977 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5978 remapped firstprivate instead of shared. To some extent this is
5979 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5980 default_kind = ctx->default_kind;
5981 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5982 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5983 default_kind = kind;
5985 switch (default_kind)
5987 case OMP_CLAUSE_DEFAULT_NONE:
5988 error ("%qE not specified in enclosing parallel",
5989 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5990 if ((ctx->region_type & ORT_TASK) != 0)
5991 error_at (ctx->location, "enclosing task");
5992 else
5993 error_at (ctx->location, "enclosing parallel");
5994 /* FALLTHRU */
5995 case OMP_CLAUSE_DEFAULT_SHARED:
5996 flags |= GOVD_SHARED;
5997 break;
5998 case OMP_CLAUSE_DEFAULT_PRIVATE:
5999 flags |= GOVD_PRIVATE;
6000 break;
6001 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6002 flags |= GOVD_FIRSTPRIVATE;
6003 break;
6004 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6005 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6006 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6007 if (ctx->outer_context)
6008 omp_notice_variable (ctx->outer_context, decl, in_code);
6009 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
6011 splay_tree_node n2;
6013 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6014 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6016 flags |= GOVD_FIRSTPRIVATE;
6017 break;
6019 if ((octx->region_type & ORT_PARALLEL) != 0)
6020 break;
6022 if (flags & GOVD_FIRSTPRIVATE)
6023 break;
6024 if (octx == NULL
6025 && (TREE_CODE (decl) == PARM_DECL
6026 || (!is_global_var (decl)
6027 && DECL_CONTEXT (decl) == current_function_decl)))
6029 flags |= GOVD_FIRSTPRIVATE;
6030 break;
6032 flags |= GOVD_SHARED;
6033 break;
6034 default:
6035 gcc_unreachable ();
6038 if ((flags & GOVD_PRIVATE)
6039 && lang_hooks.decls.omp_private_outer_ref (decl))
6040 flags |= GOVD_PRIVATE_OUTER_REF;
6042 omp_add_variable (ctx, decl, flags);
6044 shared = (flags & GOVD_SHARED) != 0;
6045 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6046 goto do_outer;
6049 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6050 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6051 && DECL_SIZE (decl)
6052 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6054 splay_tree_node n2;
6055 tree t = DECL_VALUE_EXPR (decl);
6056 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6057 t = TREE_OPERAND (t, 0);
6058 gcc_assert (DECL_P (t));
6059 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6060 n2->value |= GOVD_SEEN;
6063 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6064 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6066 /* If nothing changed, there's nothing left to do. */
6067 if ((n->value & flags) == flags)
6068 return ret;
6069 flags |= n->value;
6070 n->value = flags;
6072 do_outer:
6073 /* If the variable is private in the current context, then we don't
6074 need to propagate anything to an outer context. */
6075 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6076 return ret;
6077 if (ctx->outer_context
6078 && omp_notice_variable (ctx->outer_context, decl, in_code))
6079 return true;
6080 return ret;
6083 /* Verify that DECL is private within CTX. If there's specific information
6084 to the contrary in the innermost scope, generate an error. */
6086 static bool
6087 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
6089 splay_tree_node n;
6091 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6092 if (n != NULL)
6094 if (n->value & GOVD_SHARED)
6096 if (ctx == gimplify_omp_ctxp)
6098 error ("iteration variable %qE should be private",
6099 DECL_NAME (decl));
6100 n->value = GOVD_PRIVATE;
6101 return true;
6103 else
6104 return false;
6106 else if ((n->value & GOVD_EXPLICIT) != 0
6107 && (ctx == gimplify_omp_ctxp
6108 || (ctx->region_type == ORT_COMBINED_PARALLEL
6109 && gimplify_omp_ctxp->outer_context == ctx)))
6111 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6112 error ("iteration variable %qE should not be firstprivate",
6113 DECL_NAME (decl));
6114 else if ((n->value & GOVD_REDUCTION) != 0)
6115 error ("iteration variable %qE should not be reduction",
6116 DECL_NAME (decl));
6118 return (ctx == gimplify_omp_ctxp
6119 || (ctx->region_type == ORT_COMBINED_PARALLEL
6120 && gimplify_omp_ctxp->outer_context == ctx));
6123 if (ctx->region_type != ORT_WORKSHARE)
6124 return false;
6125 else if (ctx->outer_context)
6126 return omp_is_private (ctx->outer_context, decl);
6127 return false;
6130 /* Return true if DECL is private within a parallel region
6131 that binds to the current construct's context or in parallel
6132 region's REDUCTION clause. */
6134 static bool
6135 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6137 splay_tree_node n;
6141 ctx = ctx->outer_context;
6142 if (ctx == NULL)
6143 return !(is_global_var (decl)
6144 /* References might be private, but might be shared too. */
6145 || lang_hooks.decls.omp_privatize_by_reference (decl));
6147 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6148 if (n != NULL)
6149 return (n->value & GOVD_SHARED) == 0;
6151 while (ctx->region_type == ORT_WORKSHARE);
6152 return false;
6155 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6156 and previous omp contexts. */
6158 static void
6159 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6160 enum omp_region_type region_type)
6162 struct gimplify_omp_ctx *ctx, *outer_ctx;
6163 struct gimplify_ctx gctx;
6164 tree c;
6166 ctx = new_omp_context (region_type);
6167 outer_ctx = ctx->outer_context;
6169 while ((c = *list_p) != NULL)
6171 bool remove = false;
6172 bool notice_outer = true;
6173 const char *check_non_private = NULL;
6174 unsigned int flags;
6175 tree decl;
6177 switch (OMP_CLAUSE_CODE (c))
6179 case OMP_CLAUSE_PRIVATE:
6180 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6181 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6183 flags |= GOVD_PRIVATE_OUTER_REF;
6184 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6186 else
6187 notice_outer = false;
6188 goto do_add;
6189 case OMP_CLAUSE_SHARED:
6190 flags = GOVD_SHARED | GOVD_EXPLICIT;
6191 goto do_add;
6192 case OMP_CLAUSE_FIRSTPRIVATE:
6193 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6194 check_non_private = "firstprivate";
6195 goto do_add;
6196 case OMP_CLAUSE_LASTPRIVATE:
6197 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6198 check_non_private = "lastprivate";
6199 goto do_add;
6200 case OMP_CLAUSE_REDUCTION:
6201 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6202 check_non_private = "reduction";
6203 goto do_add;
6205 do_add:
6206 decl = OMP_CLAUSE_DECL (c);
6207 if (error_operand_p (decl))
6209 remove = true;
6210 break;
6212 omp_add_variable (ctx, decl, flags);
6213 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6214 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6216 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6217 GOVD_LOCAL | GOVD_SEEN);
6218 gimplify_omp_ctxp = ctx;
6219 push_gimplify_context (&gctx);
6221 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6222 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6224 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6225 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6226 pop_gimplify_context
6227 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6228 push_gimplify_context (&gctx);
6229 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6230 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6231 pop_gimplify_context
6232 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6233 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6234 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6236 gimplify_omp_ctxp = outer_ctx;
6238 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6239 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6241 gimplify_omp_ctxp = ctx;
6242 push_gimplify_context (&gctx);
6243 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6245 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6246 NULL, NULL);
6247 TREE_SIDE_EFFECTS (bind) = 1;
6248 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6249 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6251 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6252 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6253 pop_gimplify_context
6254 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6255 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6257 gimplify_omp_ctxp = outer_ctx;
6259 if (notice_outer)
6260 goto do_notice;
6261 break;
6263 case OMP_CLAUSE_COPYIN:
6264 case OMP_CLAUSE_COPYPRIVATE:
6265 decl = OMP_CLAUSE_DECL (c);
6266 if (error_operand_p (decl))
6268 remove = true;
6269 break;
6271 do_notice:
6272 if (outer_ctx)
6273 omp_notice_variable (outer_ctx, decl, true);
6274 if (check_non_private
6275 && region_type == ORT_WORKSHARE
6276 && omp_check_private (ctx, decl))
6278 error ("%s variable %qE is private in outer context",
6279 check_non_private, DECL_NAME (decl));
6280 remove = true;
6282 break;
6284 case OMP_CLAUSE_FINAL:
6285 case OMP_CLAUSE_IF:
6286 OMP_CLAUSE_OPERAND (c, 0)
6287 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6288 /* Fall through. */
6290 case OMP_CLAUSE_SCHEDULE:
6291 case OMP_CLAUSE_NUM_THREADS:
6292 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6293 is_gimple_val, fb_rvalue) == GS_ERROR)
6294 remove = true;
6295 break;
6297 case OMP_CLAUSE_NOWAIT:
6298 case OMP_CLAUSE_ORDERED:
6299 case OMP_CLAUSE_UNTIED:
6300 case OMP_CLAUSE_COLLAPSE:
6301 case OMP_CLAUSE_MERGEABLE:
6302 break;
6304 case OMP_CLAUSE_DEFAULT:
6305 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6306 break;
6308 default:
6309 gcc_unreachable ();
6312 if (remove)
6313 *list_p = OMP_CLAUSE_CHAIN (c);
6314 else
6315 list_p = &OMP_CLAUSE_CHAIN (c);
6318 gimplify_omp_ctxp = ctx;
6321 /* For all variables that were not actually used within the context,
6322 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6324 static int
6325 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6327 tree *list_p = (tree *) data;
6328 tree decl = (tree) n->key;
6329 unsigned flags = n->value;
6330 enum omp_clause_code code;
6331 tree clause;
6332 bool private_debug;
6334 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6335 return 0;
6336 if ((flags & GOVD_SEEN) == 0)
6337 return 0;
6338 if (flags & GOVD_DEBUG_PRIVATE)
6340 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6341 private_debug = true;
6343 else
6344 private_debug
6345 = lang_hooks.decls.omp_private_debug_clause (decl,
6346 !!(flags & GOVD_SHARED));
6347 if (private_debug)
6348 code = OMP_CLAUSE_PRIVATE;
6349 else if (flags & GOVD_SHARED)
6351 if (is_global_var (decl))
6353 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6354 while (ctx != NULL)
6356 splay_tree_node on
6357 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6358 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6359 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6360 break;
6361 ctx = ctx->outer_context;
6363 if (ctx == NULL)
6364 return 0;
6366 code = OMP_CLAUSE_SHARED;
6368 else if (flags & GOVD_PRIVATE)
6369 code = OMP_CLAUSE_PRIVATE;
6370 else if (flags & GOVD_FIRSTPRIVATE)
6371 code = OMP_CLAUSE_FIRSTPRIVATE;
6372 else
6373 gcc_unreachable ();
6375 clause = build_omp_clause (input_location, code);
6376 OMP_CLAUSE_DECL (clause) = decl;
6377 OMP_CLAUSE_CHAIN (clause) = *list_p;
6378 if (private_debug)
6379 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6380 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6381 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6382 *list_p = clause;
6383 lang_hooks.decls.omp_finish_clause (clause);
6385 return 0;
6388 static void
6389 gimplify_adjust_omp_clauses (tree *list_p)
6391 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6392 tree c, decl;
6394 while ((c = *list_p) != NULL)
6396 splay_tree_node n;
6397 bool remove = false;
6399 switch (OMP_CLAUSE_CODE (c))
6401 case OMP_CLAUSE_PRIVATE:
6402 case OMP_CLAUSE_SHARED:
6403 case OMP_CLAUSE_FIRSTPRIVATE:
6404 decl = OMP_CLAUSE_DECL (c);
6405 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6406 remove = !(n->value & GOVD_SEEN);
6407 if (! remove)
6409 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6410 if ((n->value & GOVD_DEBUG_PRIVATE)
6411 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6413 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6414 || ((n->value & GOVD_DATA_SHARE_CLASS)
6415 == GOVD_PRIVATE));
6416 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6417 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6420 break;
6422 case OMP_CLAUSE_LASTPRIVATE:
6423 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6424 accurately reflect the presence of a FIRSTPRIVATE clause. */
6425 decl = OMP_CLAUSE_DECL (c);
6426 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6427 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6428 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6429 break;
6431 case OMP_CLAUSE_REDUCTION:
6432 case OMP_CLAUSE_COPYIN:
6433 case OMP_CLAUSE_COPYPRIVATE:
6434 case OMP_CLAUSE_IF:
6435 case OMP_CLAUSE_NUM_THREADS:
6436 case OMP_CLAUSE_SCHEDULE:
6437 case OMP_CLAUSE_NOWAIT:
6438 case OMP_CLAUSE_ORDERED:
6439 case OMP_CLAUSE_DEFAULT:
6440 case OMP_CLAUSE_UNTIED:
6441 case OMP_CLAUSE_COLLAPSE:
6442 case OMP_CLAUSE_FINAL:
6443 case OMP_CLAUSE_MERGEABLE:
6444 break;
6446 default:
6447 gcc_unreachable ();
6450 if (remove)
6451 *list_p = OMP_CLAUSE_CHAIN (c);
6452 else
6453 list_p = &OMP_CLAUSE_CHAIN (c);
6456 /* Add in any implicit data sharing. */
6457 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6459 gimplify_omp_ctxp = ctx->outer_context;
6460 delete_omp_context (ctx);
6463 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6464 gimplification of the body, as well as scanning the body for used
6465 variables. We need to do this scan now, because variable-sized
6466 decls will be decomposed during gimplification. */
6468 static void
6469 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6471 tree expr = *expr_p;
6472 gimple g;
6473 gimple_seq body = NULL;
6474 struct gimplify_ctx gctx;
6476 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6477 OMP_PARALLEL_COMBINED (expr)
6478 ? ORT_COMBINED_PARALLEL
6479 : ORT_PARALLEL);
6481 push_gimplify_context (&gctx);
6483 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6484 if (gimple_code (g) == GIMPLE_BIND)
6485 pop_gimplify_context (g);
6486 else
6487 pop_gimplify_context (NULL);
6489 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6491 g = gimple_build_omp_parallel (body,
6492 OMP_PARALLEL_CLAUSES (expr),
6493 NULL_TREE, NULL_TREE);
6494 if (OMP_PARALLEL_COMBINED (expr))
6495 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6496 gimplify_seq_add_stmt (pre_p, g);
6497 *expr_p = NULL_TREE;
6500 /* Gimplify the contents of an OMP_TASK statement. This involves
6501 gimplification of the body, as well as scanning the body for used
6502 variables. We need to do this scan now, because variable-sized
6503 decls will be decomposed during gimplification. */
6505 static void
6506 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6508 tree expr = *expr_p;
6509 gimple g;
6510 gimple_seq body = NULL;
6511 struct gimplify_ctx gctx;
6513 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6514 find_omp_clause (OMP_TASK_CLAUSES (expr),
6515 OMP_CLAUSE_UNTIED)
6516 ? ORT_UNTIED_TASK : ORT_TASK);
6518 push_gimplify_context (&gctx);
6520 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6521 if (gimple_code (g) == GIMPLE_BIND)
6522 pop_gimplify_context (g);
6523 else
6524 pop_gimplify_context (NULL);
6526 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6528 g = gimple_build_omp_task (body,
6529 OMP_TASK_CLAUSES (expr),
6530 NULL_TREE, NULL_TREE,
6531 NULL_TREE, NULL_TREE, NULL_TREE);
6532 gimplify_seq_add_stmt (pre_p, g);
6533 *expr_p = NULL_TREE;
6536 /* Gimplify the gross structure of an OMP_FOR statement. */
6538 static enum gimplify_status
6539 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6541 tree for_stmt, decl, var, t;
6542 enum gimplify_status ret = GS_ALL_DONE;
6543 enum gimplify_status tret;
6544 gimple gfor;
6545 gimple_seq for_body, for_pre_body;
6546 int i;
6548 for_stmt = *expr_p;
6550 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6551 ORT_WORKSHARE);
6553 /* Handle OMP_FOR_INIT. */
6554 for_pre_body = NULL;
6555 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6556 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6558 for_body = NULL;
6559 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6560 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6561 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6562 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6563 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6565 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6566 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6567 decl = TREE_OPERAND (t, 0);
6568 gcc_assert (DECL_P (decl));
6569 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6570 || POINTER_TYPE_P (TREE_TYPE (decl)));
6572 /* Make sure the iteration variable is private. */
6573 if (omp_is_private (gimplify_omp_ctxp, decl))
6574 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6575 else
6576 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6578 /* If DECL is not a gimple register, create a temporary variable to act
6579 as an iteration counter. This is valid, since DECL cannot be
6580 modified in the body of the loop. */
6581 if (!is_gimple_reg (decl))
6583 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6584 TREE_OPERAND (t, 0) = var;
6586 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6588 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6590 else
6591 var = decl;
6593 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6594 is_gimple_val, fb_rvalue);
6595 ret = MIN (ret, tret);
6596 if (ret == GS_ERROR)
6597 return ret;
6599 /* Handle OMP_FOR_COND. */
6600 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6601 gcc_assert (COMPARISON_CLASS_P (t));
6602 gcc_assert (TREE_OPERAND (t, 0) == decl);
6604 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6605 is_gimple_val, fb_rvalue);
6606 ret = MIN (ret, tret);
6608 /* Handle OMP_FOR_INCR. */
6609 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6610 switch (TREE_CODE (t))
6612 case PREINCREMENT_EXPR:
6613 case POSTINCREMENT_EXPR:
6614 t = build_int_cst (TREE_TYPE (decl), 1);
6615 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6616 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6617 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6618 break;
6620 case PREDECREMENT_EXPR:
6621 case POSTDECREMENT_EXPR:
6622 t = build_int_cst (TREE_TYPE (decl), -1);
6623 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6624 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6625 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6626 break;
6628 case MODIFY_EXPR:
6629 gcc_assert (TREE_OPERAND (t, 0) == decl);
6630 TREE_OPERAND (t, 0) = var;
6632 t = TREE_OPERAND (t, 1);
6633 switch (TREE_CODE (t))
6635 case PLUS_EXPR:
6636 if (TREE_OPERAND (t, 1) == decl)
6638 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6639 TREE_OPERAND (t, 0) = var;
6640 break;
6643 /* Fallthru. */
6644 case MINUS_EXPR:
6645 case POINTER_PLUS_EXPR:
6646 gcc_assert (TREE_OPERAND (t, 0) == decl);
6647 TREE_OPERAND (t, 0) = var;
6648 break;
6649 default:
6650 gcc_unreachable ();
6653 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6654 is_gimple_val, fb_rvalue);
6655 ret = MIN (ret, tret);
6656 break;
6658 default:
6659 gcc_unreachable ();
6662 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6664 tree c;
6665 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6666 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6667 && OMP_CLAUSE_DECL (c) == decl
6668 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6670 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6671 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6672 gcc_assert (TREE_OPERAND (t, 0) == var);
6673 t = TREE_OPERAND (t, 1);
6674 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6675 || TREE_CODE (t) == MINUS_EXPR
6676 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6677 gcc_assert (TREE_OPERAND (t, 0) == var);
6678 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6679 TREE_OPERAND (t, 1));
6680 gimplify_assign (decl, t,
6681 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6686 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6688 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6690 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6691 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6692 for_pre_body);
6694 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6696 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6697 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6698 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6699 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6700 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6701 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6702 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6703 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6706 gimplify_seq_add_stmt (pre_p, gfor);
6707 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6710 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6711 In particular, OMP_SECTIONS and OMP_SINGLE. */
6713 static void
6714 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6716 tree expr = *expr_p;
6717 gimple stmt;
6718 gimple_seq body = NULL;
6720 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6721 gimplify_and_add (OMP_BODY (expr), &body);
6722 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6724 if (TREE_CODE (expr) == OMP_SECTIONS)
6725 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6726 else if (TREE_CODE (expr) == OMP_SINGLE)
6727 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6728 else
6729 gcc_unreachable ();
6731 gimplify_seq_add_stmt (pre_p, stmt);
6734 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6735 stabilized the lhs of the atomic operation as *ADDR. Return true if
6736 EXPR is this stabilized form. */
6738 static bool
6739 goa_lhs_expr_p (tree expr, tree addr)
6741 /* Also include casts to other type variants. The C front end is fond
6742 of adding these for e.g. volatile variables. This is like
6743 STRIP_TYPE_NOPS but includes the main variant lookup. */
6744 STRIP_USELESS_TYPE_CONVERSION (expr);
6746 if (TREE_CODE (expr) == INDIRECT_REF)
6748 expr = TREE_OPERAND (expr, 0);
6749 while (expr != addr
6750 && (CONVERT_EXPR_P (expr)
6751 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6752 && TREE_CODE (expr) == TREE_CODE (addr)
6753 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6755 expr = TREE_OPERAND (expr, 0);
6756 addr = TREE_OPERAND (addr, 0);
6758 if (expr == addr)
6759 return true;
6760 return (TREE_CODE (addr) == ADDR_EXPR
6761 && TREE_CODE (expr) == ADDR_EXPR
6762 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6764 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6765 return true;
6766 return false;
6769 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6770 expression does not involve the lhs, evaluate it into a temporary.
6771 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6772 or -1 if an error was encountered. */
6774 static int
6775 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6776 tree lhs_var)
6778 tree expr = *expr_p;
6779 int saw_lhs;
6781 if (goa_lhs_expr_p (expr, lhs_addr))
6783 *expr_p = lhs_var;
6784 return 1;
6786 if (is_gimple_val (expr))
6787 return 0;
6789 saw_lhs = 0;
6790 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6792 case tcc_binary:
6793 case tcc_comparison:
6794 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6795 lhs_var);
6796 case tcc_unary:
6797 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6798 lhs_var);
6799 break;
6800 case tcc_expression:
6801 switch (TREE_CODE (expr))
6803 case TRUTH_ANDIF_EXPR:
6804 case TRUTH_ORIF_EXPR:
6805 case TRUTH_AND_EXPR:
6806 case TRUTH_OR_EXPR:
6807 case TRUTH_XOR_EXPR:
6808 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6809 lhs_addr, lhs_var);
6810 case TRUTH_NOT_EXPR:
6811 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6812 lhs_addr, lhs_var);
6813 break;
6814 case COMPOUND_EXPR:
6815 /* Break out any preevaluations from cp_build_modify_expr. */
6816 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6817 expr = TREE_OPERAND (expr, 1))
6818 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6819 *expr_p = expr;
6820 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6821 default:
6822 break;
6824 break;
6825 default:
6826 break;
6829 if (saw_lhs == 0)
6831 enum gimplify_status gs;
6832 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6833 if (gs != GS_ALL_DONE)
6834 saw_lhs = -1;
6837 return saw_lhs;
6840 /* Gimplify an OMP_ATOMIC statement. */
6842 static enum gimplify_status
6843 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6845 tree addr = TREE_OPERAND (*expr_p, 0);
6846 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6847 ? NULL : TREE_OPERAND (*expr_p, 1);
6848 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6849 tree tmp_load;
6850 gimple loadstmt, storestmt;
6852 tmp_load = create_tmp_reg (type, NULL);
6853 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6854 return GS_ERROR;
6856 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6857 != GS_ALL_DONE)
6858 return GS_ERROR;
6860 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6861 gimplify_seq_add_stmt (pre_p, loadstmt);
6862 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6863 != GS_ALL_DONE)
6864 return GS_ERROR;
6866 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6867 rhs = tmp_load;
6868 storestmt = gimple_build_omp_atomic_store (rhs);
6869 gimplify_seq_add_stmt (pre_p, storestmt);
6870 switch (TREE_CODE (*expr_p))
6872 case OMP_ATOMIC_READ:
6873 case OMP_ATOMIC_CAPTURE_OLD:
6874 *expr_p = tmp_load;
6875 gimple_omp_atomic_set_need_value (loadstmt);
6876 break;
6877 case OMP_ATOMIC_CAPTURE_NEW:
6878 *expr_p = rhs;
6879 gimple_omp_atomic_set_need_value (storestmt);
6880 break;
6881 default:
6882 *expr_p = NULL;
6883 break;
6886 return GS_ALL_DONE;
6889 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6890 body, and adding some EH bits. */
6892 static enum gimplify_status
6893 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6895 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6896 gimple g;
6897 gimple_seq body = NULL;
6898 struct gimplify_ctx gctx;
6899 int subcode = 0;
6901 /* Wrap the transaction body in a BIND_EXPR so we have a context
6902 where to put decls for OpenMP. */
6903 if (TREE_CODE (tbody) != BIND_EXPR)
6905 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6906 TREE_SIDE_EFFECTS (bind) = 1;
6907 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6908 TRANSACTION_EXPR_BODY (expr) = bind;
6911 push_gimplify_context (&gctx);
6912 temp = voidify_wrapper_expr (*expr_p, NULL);
6914 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6915 pop_gimplify_context (g);
6917 g = gimple_build_transaction (body, NULL);
6918 if (TRANSACTION_EXPR_OUTER (expr))
6919 subcode = GTMA_IS_OUTER;
6920 else if (TRANSACTION_EXPR_RELAXED (expr))
6921 subcode = GTMA_IS_RELAXED;
6922 gimple_transaction_set_subcode (g, subcode);
6924 gimplify_seq_add_stmt (pre_p, g);
6926 if (temp)
6928 *expr_p = temp;
6929 return GS_OK;
6932 *expr_p = NULL_TREE;
6933 return GS_ALL_DONE;
6936 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6937 expression produces a value to be used as an operand inside a GIMPLE
6938 statement, the value will be stored back in *EXPR_P. This value will
6939 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6940 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6941 emitted in PRE_P and POST_P.
6943 Additionally, this process may overwrite parts of the input
6944 expression during gimplification. Ideally, it should be
6945 possible to do non-destructive gimplification.
6947 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6948 the expression needs to evaluate to a value to be used as
6949 an operand in a GIMPLE statement, this value will be stored in
6950 *EXPR_P on exit. This happens when the caller specifies one
6951 of fb_lvalue or fb_rvalue fallback flags.
6953 PRE_P will contain the sequence of GIMPLE statements corresponding
6954 to the evaluation of EXPR and all the side-effects that must
6955 be executed before the main expression. On exit, the last
6956 statement of PRE_P is the core statement being gimplified. For
6957 instance, when gimplifying 'if (++a)' the last statement in
6958 PRE_P will be 'if (t.1)' where t.1 is the result of
6959 pre-incrementing 'a'.
6961 POST_P will contain the sequence of GIMPLE statements corresponding
6962 to the evaluation of all the side-effects that must be executed
6963 after the main expression. If this is NULL, the post
6964 side-effects are stored at the end of PRE_P.
6966 The reason why the output is split in two is to handle post
6967 side-effects explicitly. In some cases, an expression may have
6968 inner and outer post side-effects which need to be emitted in
6969 an order different from the one given by the recursive
6970 traversal. For instance, for the expression (*p--)++ the post
6971 side-effects of '--' must actually occur *after* the post
6972 side-effects of '++'. However, gimplification will first visit
6973 the inner expression, so if a separate POST sequence was not
6974 used, the resulting sequence would be:
6976 1 t.1 = *p
6977 2 p = p - 1
6978 3 t.2 = t.1 + 1
6979 4 *p = t.2
6981 However, the post-decrement operation in line #2 must not be
6982 evaluated until after the store to *p at line #4, so the
6983 correct sequence should be:
6985 1 t.1 = *p
6986 2 t.2 = t.1 + 1
6987 3 *p = t.2
6988 4 p = p - 1
6990 So, by specifying a separate post queue, it is possible
6991 to emit the post side-effects in the correct order.
6992 If POST_P is NULL, an internal queue will be used. Before
6993 returning to the caller, the sequence POST_P is appended to
6994 the main output sequence PRE_P.
6996 GIMPLE_TEST_F points to a function that takes a tree T and
6997 returns nonzero if T is in the GIMPLE form requested by the
6998 caller. The GIMPLE predicates are in gimple.c.
7000 FALLBACK tells the function what sort of a temporary we want if
7001 gimplification cannot produce an expression that complies with
7002 GIMPLE_TEST_F.
7004 fb_none means that no temporary should be generated
7005 fb_rvalue means that an rvalue is OK to generate
7006 fb_lvalue means that an lvalue is OK to generate
7007 fb_either means that either is OK, but an lvalue is preferable.
7008 fb_mayfail means that gimplification may fail (in which case
7009 GS_ERROR will be returned)
7011 The return value is either GS_ERROR or GS_ALL_DONE, since this
7012 function iterates until EXPR is completely gimplified or an error
7013 occurs. */
7015 enum gimplify_status
7016 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7017 bool (*gimple_test_f) (tree), fallback_t fallback)
7019 tree tmp;
7020 gimple_seq internal_pre = NULL;
7021 gimple_seq internal_post = NULL;
7022 tree save_expr;
7023 bool is_statement;
7024 location_t saved_location;
7025 enum gimplify_status ret;
7026 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7028 save_expr = *expr_p;
7029 if (save_expr == NULL_TREE)
7030 return GS_ALL_DONE;
7032 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7033 is_statement = gimple_test_f == is_gimple_stmt;
7034 if (is_statement)
7035 gcc_assert (pre_p);
7037 /* Consistency checks. */
7038 if (gimple_test_f == is_gimple_reg)
7039 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7040 else if (gimple_test_f == is_gimple_val
7041 || gimple_test_f == is_gimple_call_addr
7042 || gimple_test_f == is_gimple_condexpr
7043 || gimple_test_f == is_gimple_mem_rhs
7044 || gimple_test_f == is_gimple_mem_rhs_or_call
7045 || gimple_test_f == is_gimple_reg_rhs
7046 || gimple_test_f == is_gimple_reg_rhs_or_call
7047 || gimple_test_f == is_gimple_asm_val
7048 || gimple_test_f == is_gimple_mem_ref_addr)
7049 gcc_assert (fallback & fb_rvalue);
7050 else if (gimple_test_f == is_gimple_min_lval
7051 || gimple_test_f == is_gimple_lvalue)
7052 gcc_assert (fallback & fb_lvalue);
7053 else if (gimple_test_f == is_gimple_addressable)
7054 gcc_assert (fallback & fb_either);
7055 else if (gimple_test_f == is_gimple_stmt)
7056 gcc_assert (fallback == fb_none);
7057 else
7059 /* We should have recognized the GIMPLE_TEST_F predicate to
7060 know what kind of fallback to use in case a temporary is
7061 needed to hold the value or address of *EXPR_P. */
7062 gcc_unreachable ();
7065 /* We used to check the predicate here and return immediately if it
7066 succeeds. This is wrong; the design is for gimplification to be
7067 idempotent, and for the predicates to only test for valid forms, not
7068 whether they are fully simplified. */
7069 if (pre_p == NULL)
7070 pre_p = &internal_pre;
7072 if (post_p == NULL)
7073 post_p = &internal_post;
7075 /* Remember the last statements added to PRE_P and POST_P. Every
7076 new statement added by the gimplification helpers needs to be
7077 annotated with location information. To centralize the
7078 responsibility, we remember the last statement that had been
7079 added to both queues before gimplifying *EXPR_P. If
7080 gimplification produces new statements in PRE_P and POST_P, those
7081 statements will be annotated with the same location information
7082 as *EXPR_P. */
7083 pre_last_gsi = gsi_last (*pre_p);
7084 post_last_gsi = gsi_last (*post_p);
7086 saved_location = input_location;
7087 if (save_expr != error_mark_node
7088 && EXPR_HAS_LOCATION (*expr_p))
7089 input_location = EXPR_LOCATION (*expr_p);
7091 /* Loop over the specific gimplifiers until the toplevel node
7092 remains the same. */
7095 /* Strip away as many useless type conversions as possible
7096 at the toplevel. */
7097 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7099 /* Remember the expr. */
7100 save_expr = *expr_p;
7102 /* Die, die, die, my darling. */
7103 if (save_expr == error_mark_node
7104 || (TREE_TYPE (save_expr)
7105 && TREE_TYPE (save_expr) == error_mark_node))
7107 ret = GS_ERROR;
7108 break;
7111 /* Do any language-specific gimplification. */
7112 ret = ((enum gimplify_status)
7113 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7114 if (ret == GS_OK)
7116 if (*expr_p == NULL_TREE)
7117 break;
7118 if (*expr_p != save_expr)
7119 continue;
7121 else if (ret != GS_UNHANDLED)
7122 break;
7124 /* Make sure that all the cases set 'ret' appropriately. */
7125 ret = GS_UNHANDLED;
7126 switch (TREE_CODE (*expr_p))
7128 /* First deal with the special cases. */
7130 case POSTINCREMENT_EXPR:
7131 case POSTDECREMENT_EXPR:
7132 case PREINCREMENT_EXPR:
7133 case PREDECREMENT_EXPR:
7134 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7135 fallback != fb_none,
7136 TREE_TYPE (*expr_p));
7137 break;
7139 case ARRAY_REF:
7140 case ARRAY_RANGE_REF:
7141 case REALPART_EXPR:
7142 case IMAGPART_EXPR:
7143 case COMPONENT_REF:
7144 case VIEW_CONVERT_EXPR:
7145 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7146 fallback ? fallback : fb_rvalue);
7147 break;
7149 case COND_EXPR:
7150 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7152 /* C99 code may assign to an array in a structure value of a
7153 conditional expression, and this has undefined behavior
7154 only on execution, so create a temporary if an lvalue is
7155 required. */
7156 if (fallback == fb_lvalue)
7158 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7159 mark_addressable (*expr_p);
7160 ret = GS_OK;
7162 break;
7164 case CALL_EXPR:
7165 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7167 /* C99 code may assign to an array in a structure returned
7168 from a function, and this has undefined behavior only on
7169 execution, so create a temporary if an lvalue is
7170 required. */
7171 if (fallback == fb_lvalue)
7173 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7174 mark_addressable (*expr_p);
7175 ret = GS_OK;
7177 break;
7179 case TREE_LIST:
7180 gcc_unreachable ();
7182 case COMPOUND_EXPR:
7183 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7184 break;
7186 case COMPOUND_LITERAL_EXPR:
7187 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7188 gimple_test_f, fallback);
7189 break;
7191 case MODIFY_EXPR:
7192 case INIT_EXPR:
7193 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7194 fallback != fb_none);
7195 break;
7197 case TRUTH_ANDIF_EXPR:
7198 case TRUTH_ORIF_EXPR:
7200 /* Preserve the original type of the expression and the
7201 source location of the outer expression. */
7202 tree org_type = TREE_TYPE (*expr_p);
7203 *expr_p = gimple_boolify (*expr_p);
7204 *expr_p = build3_loc (input_location, COND_EXPR,
7205 org_type, *expr_p,
7206 fold_convert_loc
7207 (input_location,
7208 org_type, boolean_true_node),
7209 fold_convert_loc
7210 (input_location,
7211 org_type, boolean_false_node));
7212 ret = GS_OK;
7213 break;
7216 case TRUTH_NOT_EXPR:
7218 tree type = TREE_TYPE (*expr_p);
7219 /* The parsers are careful to generate TRUTH_NOT_EXPR
7220 only with operands that are always zero or one.
7221 We do not fold here but handle the only interesting case
7222 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7223 *expr_p = gimple_boolify (*expr_p);
7224 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7225 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7226 TREE_TYPE (*expr_p),
7227 TREE_OPERAND (*expr_p, 0));
7228 else
7229 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7230 TREE_TYPE (*expr_p),
7231 TREE_OPERAND (*expr_p, 0),
7232 build_int_cst (TREE_TYPE (*expr_p), 1));
7233 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7234 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7235 ret = GS_OK;
7236 break;
7239 case ADDR_EXPR:
7240 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7241 break;
7243 case VA_ARG_EXPR:
7244 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7245 break;
7247 CASE_CONVERT:
7248 if (IS_EMPTY_STMT (*expr_p))
7250 ret = GS_ALL_DONE;
7251 break;
7254 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7255 || fallback == fb_none)
7257 /* Just strip a conversion to void (or in void context) and
7258 try again. */
7259 *expr_p = TREE_OPERAND (*expr_p, 0);
7260 ret = GS_OK;
7261 break;
7264 ret = gimplify_conversion (expr_p);
7265 if (ret == GS_ERROR)
7266 break;
7267 if (*expr_p != save_expr)
7268 break;
7269 /* FALLTHRU */
7271 case FIX_TRUNC_EXPR:
7272 /* unary_expr: ... | '(' cast ')' val | ... */
7273 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7274 is_gimple_val, fb_rvalue);
7275 recalculate_side_effects (*expr_p);
7276 break;
7278 case INDIRECT_REF:
7280 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7281 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7282 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7284 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7285 if (*expr_p != save_expr)
7287 ret = GS_OK;
7288 break;
7291 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7292 is_gimple_reg, fb_rvalue);
7293 if (ret == GS_ERROR)
7294 break;
7296 recalculate_side_effects (*expr_p);
7297 *expr_p = fold_build2_loc (input_location, MEM_REF,
7298 TREE_TYPE (*expr_p),
7299 TREE_OPERAND (*expr_p, 0),
7300 build_int_cst (saved_ptr_type, 0));
7301 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7302 TREE_THIS_NOTRAP (*expr_p) = notrap;
7303 ret = GS_OK;
7304 break;
7307 /* We arrive here through the various re-gimplifcation paths. */
7308 case MEM_REF:
7309 /* First try re-folding the whole thing. */
7310 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7311 TREE_OPERAND (*expr_p, 0),
7312 TREE_OPERAND (*expr_p, 1));
7313 if (tmp)
7315 *expr_p = tmp;
7316 recalculate_side_effects (*expr_p);
7317 ret = GS_OK;
7318 break;
7320 /* Avoid re-gimplifying the address operand if it is already
7321 in suitable form. Re-gimplifying would mark the address
7322 operand addressable. Always gimplify when not in SSA form
7323 as we still may have to gimplify decls with value-exprs. */
7324 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7325 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7327 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7328 is_gimple_mem_ref_addr, fb_rvalue);
7329 if (ret == GS_ERROR)
7330 break;
7332 recalculate_side_effects (*expr_p);
7333 ret = GS_ALL_DONE;
7334 break;
7336 /* Constants need not be gimplified. */
7337 case INTEGER_CST:
7338 case REAL_CST:
7339 case FIXED_CST:
7340 case STRING_CST:
7341 case COMPLEX_CST:
7342 case VECTOR_CST:
7343 ret = GS_ALL_DONE;
7344 break;
7346 case CONST_DECL:
7347 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7348 CONST_DECL node. Otherwise the decl is replaceable by its
7349 value. */
7350 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7351 if (fallback & fb_lvalue)
7352 ret = GS_ALL_DONE;
7353 else
7355 *expr_p = DECL_INITIAL (*expr_p);
7356 ret = GS_OK;
7358 break;
7360 case DECL_EXPR:
7361 ret = gimplify_decl_expr (expr_p, pre_p);
7362 break;
7364 case BIND_EXPR:
7365 ret = gimplify_bind_expr (expr_p, pre_p);
7366 break;
7368 case LOOP_EXPR:
7369 ret = gimplify_loop_expr (expr_p, pre_p);
7370 break;
7372 case SWITCH_EXPR:
7373 ret = gimplify_switch_expr (expr_p, pre_p);
7374 break;
7376 case EXIT_EXPR:
7377 ret = gimplify_exit_expr (expr_p);
7378 break;
7380 case GOTO_EXPR:
7381 /* If the target is not LABEL, then it is a computed jump
7382 and the target needs to be gimplified. */
7383 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7385 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7386 NULL, is_gimple_val, fb_rvalue);
7387 if (ret == GS_ERROR)
7388 break;
7390 gimplify_seq_add_stmt (pre_p,
7391 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7392 ret = GS_ALL_DONE;
7393 break;
7395 case PREDICT_EXPR:
7396 gimplify_seq_add_stmt (pre_p,
7397 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7398 PREDICT_EXPR_OUTCOME (*expr_p)));
7399 ret = GS_ALL_DONE;
7400 break;
7402 case LABEL_EXPR:
7403 ret = GS_ALL_DONE;
7404 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7405 == current_function_decl);
7406 gimplify_seq_add_stmt (pre_p,
7407 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7408 break;
7410 case CASE_LABEL_EXPR:
7411 ret = gimplify_case_label_expr (expr_p, pre_p);
7412 break;
7414 case RETURN_EXPR:
7415 ret = gimplify_return_expr (*expr_p, pre_p);
7416 break;
7418 case CONSTRUCTOR:
7419 /* Don't reduce this in place; let gimplify_init_constructor work its
7420 magic. Buf if we're just elaborating this for side effects, just
7421 gimplify any element that has side-effects. */
7422 if (fallback == fb_none)
7424 unsigned HOST_WIDE_INT ix;
7425 tree val;
7426 tree temp = NULL_TREE;
7427 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7428 if (TREE_SIDE_EFFECTS (val))
7429 append_to_statement_list (val, &temp);
7431 *expr_p = temp;
7432 ret = temp ? GS_OK : GS_ALL_DONE;
7434 /* C99 code may assign to an array in a constructed
7435 structure or union, and this has undefined behavior only
7436 on execution, so create a temporary if an lvalue is
7437 required. */
7438 else if (fallback == fb_lvalue)
7440 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7441 mark_addressable (*expr_p);
7442 ret = GS_OK;
7444 else
7445 ret = GS_ALL_DONE;
7446 break;
7448 /* The following are special cases that are not handled by the
7449 original GIMPLE grammar. */
7451 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7452 eliminated. */
7453 case SAVE_EXPR:
7454 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7455 break;
7457 case BIT_FIELD_REF:
7458 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7459 post_p, is_gimple_lvalue, fb_either);
7460 recalculate_side_effects (*expr_p);
7461 break;
7463 case TARGET_MEM_REF:
7465 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7467 if (TMR_BASE (*expr_p))
7468 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7469 post_p, is_gimple_mem_ref_addr, fb_either);
7470 if (TMR_INDEX (*expr_p))
7471 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7472 post_p, is_gimple_val, fb_rvalue);
7473 if (TMR_INDEX2 (*expr_p))
7474 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7475 post_p, is_gimple_val, fb_rvalue);
7476 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7477 ret = MIN (r0, r1);
7479 break;
7481 case NON_LVALUE_EXPR:
7482 /* This should have been stripped above. */
7483 gcc_unreachable ();
7485 case ASM_EXPR:
7486 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7487 break;
7489 case TRY_FINALLY_EXPR:
7490 case TRY_CATCH_EXPR:
7492 gimple_seq eval, cleanup;
7493 gimple try_;
7495 /* Calls to destructors are generated automatically in FINALLY/CATCH
7496 block. They should have location as UNKNOWN_LOCATION. However,
7497 gimplify_call_expr will reset these call stmts to input_location
7498 if it finds stmt's location is unknown. To prevent resetting for
7499 destructors, we set the input_location to unknown.
7500 Note that this only affects the destructor calls in FINALLY/CATCH
7501 block, and will automatically reset to its original value by the
7502 end of gimplify_expr. */
7503 input_location = UNKNOWN_LOCATION;
7504 eval = cleanup = NULL;
7505 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7506 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7507 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7508 if (gimple_seq_empty_p (cleanup))
7510 gimple_seq_add_seq (pre_p, eval);
7511 ret = GS_ALL_DONE;
7512 break;
7514 try_ = gimple_build_try (eval, cleanup,
7515 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7516 ? GIMPLE_TRY_FINALLY
7517 : GIMPLE_TRY_CATCH);
7518 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7519 gimple_set_location (try_, saved_location);
7520 else
7521 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7522 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7523 gimple_try_set_catch_is_cleanup (try_,
7524 TRY_CATCH_IS_CLEANUP (*expr_p));
7525 gimplify_seq_add_stmt (pre_p, try_);
7526 ret = GS_ALL_DONE;
7527 break;
7530 case CLEANUP_POINT_EXPR:
7531 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7532 break;
7534 case TARGET_EXPR:
7535 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7536 break;
7538 case CATCH_EXPR:
7540 gimple c;
7541 gimple_seq handler = NULL;
7542 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7543 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7544 gimplify_seq_add_stmt (pre_p, c);
7545 ret = GS_ALL_DONE;
7546 break;
7549 case EH_FILTER_EXPR:
7551 gimple ehf;
7552 gimple_seq failure = NULL;
7554 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7555 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7556 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7557 gimplify_seq_add_stmt (pre_p, ehf);
7558 ret = GS_ALL_DONE;
7559 break;
7562 case OBJ_TYPE_REF:
7564 enum gimplify_status r0, r1;
7565 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7566 post_p, is_gimple_val, fb_rvalue);
7567 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7568 post_p, is_gimple_val, fb_rvalue);
7569 TREE_SIDE_EFFECTS (*expr_p) = 0;
7570 ret = MIN (r0, r1);
7572 break;
7574 case LABEL_DECL:
7575 /* We get here when taking the address of a label. We mark
7576 the label as "forced"; meaning it can never be removed and
7577 it is a potential target for any computed goto. */
7578 FORCED_LABEL (*expr_p) = 1;
7579 ret = GS_ALL_DONE;
7580 break;
7582 case STATEMENT_LIST:
7583 ret = gimplify_statement_list (expr_p, pre_p);
7584 break;
7586 case WITH_SIZE_EXPR:
7588 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7589 post_p == &internal_post ? NULL : post_p,
7590 gimple_test_f, fallback);
7591 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7592 is_gimple_val, fb_rvalue);
7593 ret = GS_ALL_DONE;
7595 break;
7597 case VAR_DECL:
7598 case PARM_DECL:
7599 ret = gimplify_var_or_parm_decl (expr_p);
7600 break;
7602 case RESULT_DECL:
7603 /* When within an OpenMP context, notice uses of variables. */
7604 if (gimplify_omp_ctxp)
7605 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7606 ret = GS_ALL_DONE;
7607 break;
7609 case SSA_NAME:
7610 /* Allow callbacks into the gimplifier during optimization. */
7611 ret = GS_ALL_DONE;
7612 break;
7614 case OMP_PARALLEL:
7615 gimplify_omp_parallel (expr_p, pre_p);
7616 ret = GS_ALL_DONE;
7617 break;
7619 case OMP_TASK:
7620 gimplify_omp_task (expr_p, pre_p);
7621 ret = GS_ALL_DONE;
7622 break;
7624 case OMP_FOR:
7625 ret = gimplify_omp_for (expr_p, pre_p);
7626 break;
7628 case OMP_SECTIONS:
7629 case OMP_SINGLE:
7630 gimplify_omp_workshare (expr_p, pre_p);
7631 ret = GS_ALL_DONE;
7632 break;
7634 case OMP_SECTION:
7635 case OMP_MASTER:
7636 case OMP_ORDERED:
7637 case OMP_CRITICAL:
7639 gimple_seq body = NULL;
7640 gimple g;
7642 gimplify_and_add (OMP_BODY (*expr_p), &body);
7643 switch (TREE_CODE (*expr_p))
7645 case OMP_SECTION:
7646 g = gimple_build_omp_section (body);
7647 break;
7648 case OMP_MASTER:
7649 g = gimple_build_omp_master (body);
7650 break;
7651 case OMP_ORDERED:
7652 g = gimple_build_omp_ordered (body);
7653 break;
7654 case OMP_CRITICAL:
7655 g = gimple_build_omp_critical (body,
7656 OMP_CRITICAL_NAME (*expr_p));
7657 break;
7658 default:
7659 gcc_unreachable ();
7661 gimplify_seq_add_stmt (pre_p, g);
7662 ret = GS_ALL_DONE;
7663 break;
7666 case OMP_ATOMIC:
7667 case OMP_ATOMIC_READ:
7668 case OMP_ATOMIC_CAPTURE_OLD:
7669 case OMP_ATOMIC_CAPTURE_NEW:
7670 ret = gimplify_omp_atomic (expr_p, pre_p);
7671 break;
7673 case TRANSACTION_EXPR:
7674 ret = gimplify_transaction (expr_p, pre_p);
7675 break;
7677 case TRUTH_AND_EXPR:
7678 case TRUTH_OR_EXPR:
7679 case TRUTH_XOR_EXPR:
7681 tree orig_type = TREE_TYPE (*expr_p);
7682 tree new_type, xop0, xop1;
7683 *expr_p = gimple_boolify (*expr_p);
7684 new_type = TREE_TYPE (*expr_p);
7685 if (!useless_type_conversion_p (orig_type, new_type))
7687 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7688 ret = GS_OK;
7689 break;
7692 /* Boolified binary truth expressions are semantically equivalent
7693 to bitwise binary expressions. Canonicalize them to the
7694 bitwise variant. */
7695 switch (TREE_CODE (*expr_p))
7697 case TRUTH_AND_EXPR:
7698 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7699 break;
7700 case TRUTH_OR_EXPR:
7701 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7702 break;
7703 case TRUTH_XOR_EXPR:
7704 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7705 break;
7706 default:
7707 break;
7709 /* Now make sure that operands have compatible type to
7710 expression's new_type. */
7711 xop0 = TREE_OPERAND (*expr_p, 0);
7712 xop1 = TREE_OPERAND (*expr_p, 1);
7713 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7714 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7715 new_type,
7716 xop0);
7717 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7718 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7719 new_type,
7720 xop1);
7721 /* Continue classified as tcc_binary. */
7722 goto expr_2;
7725 case FMA_EXPR:
7726 case VEC_COND_EXPR:
7727 case VEC_PERM_EXPR:
7728 /* Classified as tcc_expression. */
7729 goto expr_3;
7731 case POINTER_PLUS_EXPR:
7733 enum gimplify_status r0, r1;
7734 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7735 post_p, is_gimple_val, fb_rvalue);
7736 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7737 post_p, is_gimple_val, fb_rvalue);
7738 recalculate_side_effects (*expr_p);
7739 ret = MIN (r0, r1);
7740 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7741 after gimplifying operands - this is similar to how
7742 it would be folding all gimplified stmts on creation
7743 to have them canonicalized, which is what we eventually
7744 should do anyway. */
7745 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7746 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7748 *expr_p = build_fold_addr_expr_with_type_loc
7749 (input_location,
7750 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7751 TREE_OPERAND (*expr_p, 0),
7752 fold_convert (ptr_type_node,
7753 TREE_OPERAND (*expr_p, 1))),
7754 TREE_TYPE (*expr_p));
7755 ret = MIN (ret, GS_OK);
7757 break;
7760 default:
7761 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7763 case tcc_comparison:
7764 /* Handle comparison of objects of non scalar mode aggregates
7765 with a call to memcmp. It would be nice to only have to do
7766 this for variable-sized objects, but then we'd have to allow
7767 the same nest of reference nodes we allow for MODIFY_EXPR and
7768 that's too complex.
7770 Compare scalar mode aggregates as scalar mode values. Using
7771 memcmp for them would be very inefficient at best, and is
7772 plain wrong if bitfields are involved. */
7774 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7776 /* Vector comparisons need no boolification. */
7777 if (TREE_CODE (type) == VECTOR_TYPE)
7778 goto expr_2;
7779 else if (!AGGREGATE_TYPE_P (type))
7781 tree org_type = TREE_TYPE (*expr_p);
7782 *expr_p = gimple_boolify (*expr_p);
7783 if (!useless_type_conversion_p (org_type,
7784 TREE_TYPE (*expr_p)))
7786 *expr_p = fold_convert_loc (input_location,
7787 org_type, *expr_p);
7788 ret = GS_OK;
7790 else
7791 goto expr_2;
7793 else if (TYPE_MODE (type) != BLKmode)
7794 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7795 else
7796 ret = gimplify_variable_sized_compare (expr_p);
7798 break;
7801 /* If *EXPR_P does not need to be special-cased, handle it
7802 according to its class. */
7803 case tcc_unary:
7804 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7805 post_p, is_gimple_val, fb_rvalue);
7806 break;
7808 case tcc_binary:
7809 expr_2:
7811 enum gimplify_status r0, r1;
7813 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7814 post_p, is_gimple_val, fb_rvalue);
7815 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7816 post_p, is_gimple_val, fb_rvalue);
7818 ret = MIN (r0, r1);
7819 break;
7822 expr_3:
7824 enum gimplify_status r0, r1, r2;
7826 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7827 post_p, is_gimple_val, fb_rvalue);
7828 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7829 post_p, is_gimple_val, fb_rvalue);
7830 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7831 post_p, is_gimple_val, fb_rvalue);
7833 ret = MIN (MIN (r0, r1), r2);
7834 break;
7837 case tcc_declaration:
7838 case tcc_constant:
7839 ret = GS_ALL_DONE;
7840 goto dont_recalculate;
7842 default:
7843 gcc_unreachable ();
7846 recalculate_side_effects (*expr_p);
7848 dont_recalculate:
7849 break;
7852 gcc_assert (*expr_p || ret != GS_OK);
7854 while (ret == GS_OK);
7856 /* If we encountered an error_mark somewhere nested inside, either
7857 stub out the statement or propagate the error back out. */
7858 if (ret == GS_ERROR)
7860 if (is_statement)
7861 *expr_p = NULL;
7862 goto out;
7865 /* This was only valid as a return value from the langhook, which
7866 we handled. Make sure it doesn't escape from any other context. */
7867 gcc_assert (ret != GS_UNHANDLED);
7869 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7871 /* We aren't looking for a value, and we don't have a valid
7872 statement. If it doesn't have side-effects, throw it away. */
7873 if (!TREE_SIDE_EFFECTS (*expr_p))
7874 *expr_p = NULL;
7875 else if (!TREE_THIS_VOLATILE (*expr_p))
7877 /* This is probably a _REF that contains something nested that
7878 has side effects. Recurse through the operands to find it. */
7879 enum tree_code code = TREE_CODE (*expr_p);
7881 switch (code)
7883 case COMPONENT_REF:
7884 case REALPART_EXPR:
7885 case IMAGPART_EXPR:
7886 case VIEW_CONVERT_EXPR:
7887 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7888 gimple_test_f, fallback);
7889 break;
7891 case ARRAY_REF:
7892 case ARRAY_RANGE_REF:
7893 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7894 gimple_test_f, fallback);
7895 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7896 gimple_test_f, fallback);
7897 break;
7899 default:
7900 /* Anything else with side-effects must be converted to
7901 a valid statement before we get here. */
7902 gcc_unreachable ();
7905 *expr_p = NULL;
7907 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7908 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7910 /* Historically, the compiler has treated a bare reference
7911 to a non-BLKmode volatile lvalue as forcing a load. */
7912 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7914 /* Normally, we do not want to create a temporary for a
7915 TREE_ADDRESSABLE type because such a type should not be
7916 copied by bitwise-assignment. However, we make an
7917 exception here, as all we are doing here is ensuring that
7918 we read the bytes that make up the type. We use
7919 create_tmp_var_raw because create_tmp_var will abort when
7920 given a TREE_ADDRESSABLE type. */
7921 tree tmp = create_tmp_var_raw (type, "vol");
7922 gimple_add_tmp_var (tmp);
7923 gimplify_assign (tmp, *expr_p, pre_p);
7924 *expr_p = NULL;
7926 else
7927 /* We can't do anything useful with a volatile reference to
7928 an incomplete type, so just throw it away. Likewise for
7929 a BLKmode type, since any implicit inner load should
7930 already have been turned into an explicit one by the
7931 gimplification process. */
7932 *expr_p = NULL;
7935 /* If we are gimplifying at the statement level, we're done. Tack
7936 everything together and return. */
7937 if (fallback == fb_none || is_statement)
7939 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7940 it out for GC to reclaim it. */
7941 *expr_p = NULL_TREE;
7943 if (!gimple_seq_empty_p (internal_pre)
7944 || !gimple_seq_empty_p (internal_post))
7946 gimplify_seq_add_seq (&internal_pre, internal_post);
7947 gimplify_seq_add_seq (pre_p, internal_pre);
7950 /* The result of gimplifying *EXPR_P is going to be the last few
7951 statements in *PRE_P and *POST_P. Add location information
7952 to all the statements that were added by the gimplification
7953 helpers. */
7954 if (!gimple_seq_empty_p (*pre_p))
7955 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7957 if (!gimple_seq_empty_p (*post_p))
7958 annotate_all_with_location_after (*post_p, post_last_gsi,
7959 input_location);
7961 goto out;
7964 #ifdef ENABLE_GIMPLE_CHECKING
7965 if (*expr_p)
7967 enum tree_code code = TREE_CODE (*expr_p);
7968 /* These expressions should already be in gimple IR form. */
7969 gcc_assert (code != MODIFY_EXPR
7970 && code != ASM_EXPR
7971 && code != BIND_EXPR
7972 && code != CATCH_EXPR
7973 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7974 && code != EH_FILTER_EXPR
7975 && code != GOTO_EXPR
7976 && code != LABEL_EXPR
7977 && code != LOOP_EXPR
7978 && code != SWITCH_EXPR
7979 && code != TRY_FINALLY_EXPR
7980 && code != OMP_CRITICAL
7981 && code != OMP_FOR
7982 && code != OMP_MASTER
7983 && code != OMP_ORDERED
7984 && code != OMP_PARALLEL
7985 && code != OMP_SECTIONS
7986 && code != OMP_SECTION
7987 && code != OMP_SINGLE);
7989 #endif
7991 /* Otherwise we're gimplifying a subexpression, so the resulting
7992 value is interesting. If it's a valid operand that matches
7993 GIMPLE_TEST_F, we're done. Unless we are handling some
7994 post-effects internally; if that's the case, we need to copy into
7995 a temporary before adding the post-effects to POST_P. */
7996 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7997 goto out;
7999 /* Otherwise, we need to create a new temporary for the gimplified
8000 expression. */
8002 /* We can't return an lvalue if we have an internal postqueue. The
8003 object the lvalue refers to would (probably) be modified by the
8004 postqueue; we need to copy the value out first, which means an
8005 rvalue. */
8006 if ((fallback & fb_lvalue)
8007 && gimple_seq_empty_p (internal_post)
8008 && is_gimple_addressable (*expr_p))
8010 /* An lvalue will do. Take the address of the expression, store it
8011 in a temporary, and replace the expression with an INDIRECT_REF of
8012 that temporary. */
8013 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8014 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8015 *expr_p = build_simple_mem_ref (tmp);
8017 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8019 /* An rvalue will do. Assign the gimplified expression into a
8020 new temporary TMP and replace the original expression with
8021 TMP. First, make sure that the expression has a type so that
8022 it can be assigned into a temporary. */
8023 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8024 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8026 else
8028 #ifdef ENABLE_GIMPLE_CHECKING
8029 if (!(fallback & fb_mayfail))
8031 fprintf (stderr, "gimplification failed:\n");
8032 print_generic_expr (stderr, *expr_p, 0);
8033 debug_tree (*expr_p);
8034 internal_error ("gimplification failed");
8036 #endif
8037 gcc_assert (fallback & fb_mayfail);
8039 /* If this is an asm statement, and the user asked for the
8040 impossible, don't die. Fail and let gimplify_asm_expr
8041 issue an error. */
8042 ret = GS_ERROR;
8043 goto out;
8046 /* Make sure the temporary matches our predicate. */
8047 gcc_assert ((*gimple_test_f) (*expr_p));
8049 if (!gimple_seq_empty_p (internal_post))
8051 annotate_all_with_location (internal_post, input_location);
8052 gimplify_seq_add_seq (pre_p, internal_post);
8055 out:
8056 input_location = saved_location;
8057 return ret;
8060 /* Look through TYPE for variable-sized objects and gimplify each such
8061 size that we find. Add to LIST_P any statements generated. */
8063 void
8064 gimplify_type_sizes (tree type, gimple_seq *list_p)
8066 tree field, t;
8068 if (type == NULL || type == error_mark_node)
8069 return;
8071 /* We first do the main variant, then copy into any other variants. */
8072 type = TYPE_MAIN_VARIANT (type);
8074 /* Avoid infinite recursion. */
8075 if (TYPE_SIZES_GIMPLIFIED (type))
8076 return;
8078 TYPE_SIZES_GIMPLIFIED (type) = 1;
8080 switch (TREE_CODE (type))
8082 case INTEGER_TYPE:
8083 case ENUMERAL_TYPE:
8084 case BOOLEAN_TYPE:
8085 case REAL_TYPE:
8086 case FIXED_POINT_TYPE:
8087 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8088 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8090 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8092 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8093 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8095 break;
8097 case ARRAY_TYPE:
8098 /* These types may not have declarations, so handle them here. */
8099 gimplify_type_sizes (TREE_TYPE (type), list_p);
8100 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8101 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8102 with assigned stack slots, for -O1+ -g they should be tracked
8103 by VTA. */
8104 if (!(TYPE_NAME (type)
8105 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8106 && DECL_IGNORED_P (TYPE_NAME (type)))
8107 && TYPE_DOMAIN (type)
8108 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8110 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8111 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8112 DECL_IGNORED_P (t) = 0;
8113 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8114 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8115 DECL_IGNORED_P (t) = 0;
8117 break;
8119 case RECORD_TYPE:
8120 case UNION_TYPE:
8121 case QUAL_UNION_TYPE:
8122 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8123 if (TREE_CODE (field) == FIELD_DECL)
8125 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8126 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8127 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8128 gimplify_type_sizes (TREE_TYPE (field), list_p);
8130 break;
8132 case POINTER_TYPE:
8133 case REFERENCE_TYPE:
8134 /* We used to recurse on the pointed-to type here, which turned out to
8135 be incorrect because its definition might refer to variables not
8136 yet initialized at this point if a forward declaration is involved.
8138 It was actually useful for anonymous pointed-to types to ensure
8139 that the sizes evaluation dominates every possible later use of the
8140 values. Restricting to such types here would be safe since there
8141 is no possible forward declaration around, but would introduce an
8142 undesirable middle-end semantic to anonymity. We then defer to
8143 front-ends the responsibility of ensuring that the sizes are
8144 evaluated both early and late enough, e.g. by attaching artificial
8145 type declarations to the tree. */
8146 break;
8148 default:
8149 break;
8152 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8153 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8155 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8157 TYPE_SIZE (t) = TYPE_SIZE (type);
8158 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8159 TYPE_SIZES_GIMPLIFIED (t) = 1;
8163 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8164 a size or position, has had all of its SAVE_EXPRs evaluated.
8165 We add any required statements to *STMT_P. */
8167 void
8168 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8170 tree expr = *expr_p;
8172 /* We don't do anything if the value isn't there, is constant, or contains
8173 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8174 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8175 will want to replace it with a new variable, but that will cause problems
8176 if this type is from outside the function. It's OK to have that here. */
8177 if (is_gimple_sizepos (expr))
8178 return;
8180 *expr_p = unshare_expr (expr);
8182 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8185 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8186 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8187 is true, also gimplify the parameters. */
8189 gimple
8190 gimplify_body (tree fndecl, bool do_parms)
8192 location_t saved_location = input_location;
8193 gimple_seq parm_stmts, seq;
8194 gimple outer_bind;
8195 struct gimplify_ctx gctx;
8196 struct cgraph_node *cgn;
8198 timevar_push (TV_TREE_GIMPLIFY);
8200 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8201 gimplification. */
8202 default_rtl_profile ();
8204 gcc_assert (gimplify_ctxp == NULL);
8205 push_gimplify_context (&gctx);
8207 /* Unshare most shared trees in the body and in that of any nested functions.
8208 It would seem we don't have to do this for nested functions because
8209 they are supposed to be output and then the outer function gimplified
8210 first, but the g++ front end doesn't always do it that way. */
8211 unshare_body (fndecl);
8212 unvisit_body (fndecl);
8214 cgn = cgraph_get_node (fndecl);
8215 if (cgn && cgn->origin)
8216 nonlocal_vlas = pointer_set_create ();
8218 /* Make sure input_location isn't set to something weird. */
8219 input_location = DECL_SOURCE_LOCATION (fndecl);
8221 /* Resolve callee-copies. This has to be done before processing
8222 the body so that DECL_VALUE_EXPR gets processed correctly. */
8223 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8225 /* Gimplify the function's body. */
8226 seq = NULL;
8227 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8228 outer_bind = gimple_seq_first_stmt (seq);
8229 if (!outer_bind)
8231 outer_bind = gimple_build_nop ();
8232 gimplify_seq_add_stmt (&seq, outer_bind);
8235 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8236 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8237 if (gimple_code (outer_bind) == GIMPLE_BIND
8238 && gimple_seq_first (seq) == gimple_seq_last (seq))
8240 else
8241 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8243 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8245 /* If we had callee-copies statements, insert them at the beginning
8246 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8247 if (!gimple_seq_empty_p (parm_stmts))
8249 tree parm;
8251 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8252 gimple_bind_set_body (outer_bind, parm_stmts);
8254 for (parm = DECL_ARGUMENTS (current_function_decl);
8255 parm; parm = DECL_CHAIN (parm))
8256 if (DECL_HAS_VALUE_EXPR_P (parm))
8258 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8259 DECL_IGNORED_P (parm) = 0;
8263 if (nonlocal_vlas)
8265 pointer_set_destroy (nonlocal_vlas);
8266 nonlocal_vlas = NULL;
8269 pop_gimplify_context (outer_bind);
8270 gcc_assert (gimplify_ctxp == NULL);
8272 #ifdef ENABLE_CHECKING
8273 if (!seen_error ())
8274 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8275 #endif
8277 timevar_pop (TV_TREE_GIMPLIFY);
8278 input_location = saved_location;
8280 return outer_bind;
8283 typedef char *char_p; /* For DEF_VEC_P. */
8285 /* Return whether we should exclude FNDECL from instrumentation. */
8287 static bool
8288 flag_instrument_functions_exclude_p (tree fndecl)
8290 vec<char_p> *v;
8292 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8293 if (v && v->length () > 0)
8295 const char *name;
8296 int i;
8297 char *s;
8299 name = lang_hooks.decl_printable_name (fndecl, 0);
8300 FOR_EACH_VEC_ELT (*v, i, s)
8301 if (strstr (name, s) != NULL)
8302 return true;
8305 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8306 if (v && v->length () > 0)
8308 const char *name;
8309 int i;
8310 char *s;
8312 name = DECL_SOURCE_FILE (fndecl);
8313 FOR_EACH_VEC_ELT (*v, i, s)
8314 if (strstr (name, s) != NULL)
8315 return true;
8318 return false;
8321 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8322 node for the function we want to gimplify.
8324 Return the sequence of GIMPLE statements corresponding to the body
8325 of FNDECL. */
8327 void
8328 gimplify_function_tree (tree fndecl)
8330 tree parm, ret;
8331 gimple_seq seq;
8332 gimple bind;
8334 gcc_assert (!gimple_body (fndecl));
8336 if (DECL_STRUCT_FUNCTION (fndecl))
8337 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8338 else
8339 push_struct_function (fndecl);
8341 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8343 /* Preliminarily mark non-addressed complex variables as eligible
8344 for promotion to gimple registers. We'll transform their uses
8345 as we find them. */
8346 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8347 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8348 && !TREE_THIS_VOLATILE (parm)
8349 && !needs_to_live_in_memory (parm))
8350 DECL_GIMPLE_REG_P (parm) = 1;
8353 ret = DECL_RESULT (fndecl);
8354 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8355 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8356 && !needs_to_live_in_memory (ret))
8357 DECL_GIMPLE_REG_P (ret) = 1;
8359 bind = gimplify_body (fndecl, true);
8361 /* The tree body of the function is no longer needed, replace it
8362 with the new GIMPLE body. */
8363 seq = NULL;
8364 gimple_seq_add_stmt (&seq, bind);
8365 gimple_set_body (fndecl, seq);
8367 /* If we're instrumenting function entry/exit, then prepend the call to
8368 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8369 catch the exit hook. */
8370 /* ??? Add some way to ignore exceptions for this TFE. */
8371 if (flag_instrument_function_entry_exit
8372 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8373 && !flag_instrument_functions_exclude_p (fndecl))
8375 tree x;
8376 gimple new_bind;
8377 gimple tf;
8378 gimple_seq cleanup = NULL, body = NULL;
8379 tree tmp_var;
8380 gimple call;
8382 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8383 call = gimple_build_call (x, 1, integer_zero_node);
8384 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8385 gimple_call_set_lhs (call, tmp_var);
8386 gimplify_seq_add_stmt (&cleanup, call);
8387 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8388 call = gimple_build_call (x, 2,
8389 build_fold_addr_expr (current_function_decl),
8390 tmp_var);
8391 gimplify_seq_add_stmt (&cleanup, call);
8392 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8394 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8395 call = gimple_build_call (x, 1, integer_zero_node);
8396 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8397 gimple_call_set_lhs (call, tmp_var);
8398 gimplify_seq_add_stmt (&body, call);
8399 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8400 call = gimple_build_call (x, 2,
8401 build_fold_addr_expr (current_function_decl),
8402 tmp_var);
8403 gimplify_seq_add_stmt (&body, call);
8404 gimplify_seq_add_stmt (&body, tf);
8405 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8406 /* Clear the block for BIND, since it is no longer directly inside
8407 the function, but within a try block. */
8408 gimple_bind_set_block (bind, NULL);
8410 /* Replace the current function body with the body
8411 wrapped in the try/finally TF. */
8412 seq = NULL;
8413 gimple_seq_add_stmt (&seq, new_bind);
8414 gimple_set_body (fndecl, seq);
8417 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8418 cfun->curr_properties = PROP_gimple_any;
8420 pop_cfun ();
8423 /* Some transformations like inlining may invalidate the GIMPLE form
8424 for operands. This function traverses all the operands in STMT and
8425 gimplifies anything that is not a valid gimple operand. Any new
8426 GIMPLE statements are inserted before *GSI_P. */
8428 void
8429 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8431 size_t i, num_ops;
8432 tree lhs;
8433 gimple_seq pre = NULL;
8434 gimple post_stmt = NULL;
8435 struct gimplify_ctx gctx;
8437 push_gimplify_context (&gctx);
8438 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8440 switch (gimple_code (stmt))
8442 case GIMPLE_COND:
8443 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8444 is_gimple_val, fb_rvalue);
8445 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8446 is_gimple_val, fb_rvalue);
8447 break;
8448 case GIMPLE_SWITCH:
8449 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8450 is_gimple_val, fb_rvalue);
8451 break;
8452 case GIMPLE_OMP_ATOMIC_LOAD:
8453 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8454 is_gimple_val, fb_rvalue);
8455 break;
8456 case GIMPLE_ASM:
8458 size_t i, noutputs = gimple_asm_noutputs (stmt);
8459 const char *constraint, **oconstraints;
8460 bool allows_mem, allows_reg, is_inout;
8462 oconstraints
8463 = (const char **) alloca ((noutputs) * sizeof (const char *));
8464 for (i = 0; i < noutputs; i++)
8466 tree op = gimple_asm_output_op (stmt, i);
8467 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8468 oconstraints[i] = constraint;
8469 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8470 &allows_reg, &is_inout);
8471 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8472 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8473 fb_lvalue | fb_mayfail);
8475 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8477 tree op = gimple_asm_input_op (stmt, i);
8478 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8479 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8480 oconstraints, &allows_mem, &allows_reg);
8481 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8482 allows_reg = 0;
8483 if (!allows_reg && allows_mem)
8484 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8485 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8486 else
8487 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8488 is_gimple_asm_val, fb_rvalue);
8491 break;
8492 default:
8493 /* NOTE: We start gimplifying operands from last to first to
8494 make sure that side-effects on the RHS of calls, assignments
8495 and ASMs are executed before the LHS. The ordering is not
8496 important for other statements. */
8497 num_ops = gimple_num_ops (stmt);
8498 for (i = num_ops; i > 0; i--)
8500 tree op = gimple_op (stmt, i - 1);
8501 if (op == NULL_TREE)
8502 continue;
8503 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8504 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8505 else if (i == 2
8506 && is_gimple_assign (stmt)
8507 && num_ops == 2
8508 && get_gimple_rhs_class (gimple_expr_code (stmt))
8509 == GIMPLE_SINGLE_RHS)
8510 gimplify_expr (&op, &pre, NULL,
8511 rhs_predicate_for (gimple_assign_lhs (stmt)),
8512 fb_rvalue);
8513 else if (i == 2 && is_gimple_call (stmt))
8515 if (TREE_CODE (op) == FUNCTION_DECL)
8516 continue;
8517 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8519 else
8520 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8521 gimple_set_op (stmt, i - 1, op);
8524 lhs = gimple_get_lhs (stmt);
8525 /* If the LHS changed it in a way that requires a simple RHS,
8526 create temporary. */
8527 if (lhs && !is_gimple_reg (lhs))
8529 bool need_temp = false;
8531 if (is_gimple_assign (stmt)
8532 && num_ops == 2
8533 && get_gimple_rhs_class (gimple_expr_code (stmt))
8534 == GIMPLE_SINGLE_RHS)
8535 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8536 rhs_predicate_for (gimple_assign_lhs (stmt)),
8537 fb_rvalue);
8538 else if (is_gimple_reg (lhs))
8540 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8542 if (is_gimple_call (stmt))
8544 i = gimple_call_flags (stmt);
8545 if ((i & ECF_LOOPING_CONST_OR_PURE)
8546 || !(i & (ECF_CONST | ECF_PURE)))
8547 need_temp = true;
8549 if (stmt_can_throw_internal (stmt))
8550 need_temp = true;
8553 else
8555 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8556 need_temp = true;
8557 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8559 if (is_gimple_call (stmt))
8561 tree fndecl = gimple_call_fndecl (stmt);
8563 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8564 && !(fndecl && DECL_RESULT (fndecl)
8565 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8566 need_temp = true;
8568 else
8569 need_temp = true;
8572 if (need_temp)
8574 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8575 if (gimple_in_ssa_p (cfun))
8576 temp = make_ssa_name (temp, NULL);
8577 gimple_set_lhs (stmt, temp);
8578 post_stmt = gimple_build_assign (lhs, temp);
8579 if (TREE_CODE (lhs) == SSA_NAME)
8580 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8583 break;
8586 if (!gimple_seq_empty_p (pre))
8587 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8588 if (post_stmt)
8589 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8591 pop_gimplify_context (NULL);
8594 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8595 the predicate that will hold for the result. If VAR is not NULL, make the
8596 base variable of the final destination be VAR if suitable. */
8598 tree
8599 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8600 gimple_predicate gimple_test_f, tree var)
8602 enum gimplify_status ret;
8603 struct gimplify_ctx gctx;
8605 *stmts = NULL;
8607 /* gimple_test_f might be more strict than is_gimple_val, make
8608 sure we pass both. Just checking gimple_test_f doesn't work
8609 because most gimple predicates do not work recursively. */
8610 if (is_gimple_val (expr)
8611 && (*gimple_test_f) (expr))
8612 return expr;
8614 push_gimplify_context (&gctx);
8615 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8616 gimplify_ctxp->allow_rhs_cond_expr = true;
8618 if (var)
8620 if (gimplify_ctxp->into_ssa
8621 && is_gimple_reg (var))
8622 var = make_ssa_name (var, NULL);
8623 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8626 if (TREE_CODE (expr) != MODIFY_EXPR
8627 && TREE_TYPE (expr) == void_type_node)
8629 gimplify_and_add (expr, stmts);
8630 expr = NULL_TREE;
8632 else
8634 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8635 gcc_assert (ret != GS_ERROR);
8638 pop_gimplify_context (NULL);
8640 return expr;
8643 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8644 force the result to be either ssa_name or an invariant, otherwise
8645 just force it to be a rhs expression. If VAR is not NULL, make the
8646 base variable of the final destination be VAR if suitable. */
8648 tree
8649 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8651 return force_gimple_operand_1 (expr, stmts,
8652 simple ? is_gimple_val : is_gimple_reg_rhs,
8653 var);
8656 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8657 and VAR. If some statements are produced, emits them at GSI.
8658 If BEFORE is true. the statements are appended before GSI, otherwise
8659 they are appended after it. M specifies the way GSI moves after
8660 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8662 tree
8663 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8664 gimple_predicate gimple_test_f,
8665 tree var, bool before,
8666 enum gsi_iterator_update m)
8668 gimple_seq stmts;
8670 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8672 if (!gimple_seq_empty_p (stmts))
8674 if (before)
8675 gsi_insert_seq_before (gsi, stmts, m);
8676 else
8677 gsi_insert_seq_after (gsi, stmts, m);
8680 return expr;
8683 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8684 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8685 otherwise just force it to be a rhs expression. If some statements are
8686 produced, emits them at GSI. If BEFORE is true, the statements are
8687 appended before GSI, otherwise they are appended after it. M specifies
8688 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8689 are the usual values). */
8691 tree
8692 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8693 bool simple_p, tree var, bool before,
8694 enum gsi_iterator_update m)
8696 return force_gimple_operand_gsi_1 (gsi, expr,
8697 simple_p
8698 ? is_gimple_val : is_gimple_reg_rhs,
8699 var, before, m);
8703 #include "gt-gimplify.h"