2013-06-06 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / gimplify.c
blobe2ae8932dfae45f35c5efc45e1b789ee49d87e0a
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2013 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "tree-iterator.h"
30 #include "tree-inline.h"
31 #include "tree-pretty-print.h"
32 #include "langhooks.h"
33 #include "tree-flow.h"
34 #include "cgraph.h"
35 #include "timevar.h"
36 #include "hashtab.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "target.h"
42 #include "pointer-set.h"
43 #include "splay-tree.h"
44 #include "vec.h"
45 #include "gimple.h"
47 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
48 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
50 enum gimplify_omp_var_data
52 GOVD_SEEN = 1,
53 GOVD_EXPLICIT = 2,
54 GOVD_SHARED = 4,
55 GOVD_PRIVATE = 8,
56 GOVD_FIRSTPRIVATE = 16,
57 GOVD_LASTPRIVATE = 32,
58 GOVD_REDUCTION = 64,
59 GOVD_LOCAL = 128,
60 GOVD_DEBUG_PRIVATE = 256,
61 GOVD_PRIVATE_OUTER_REF = 512,
62 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
63 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
67 enum omp_region_type
69 ORT_WORKSHARE = 0,
70 ORT_PARALLEL = 2,
71 ORT_COMBINED_PARALLEL = 3,
72 ORT_TASK = 4,
73 ORT_UNTIED_TASK = 5
76 struct gimplify_omp_ctx
78 struct gimplify_omp_ctx *outer_context;
79 splay_tree variables;
80 struct pointer_set_t *privatized_types;
81 location_t location;
82 enum omp_clause_default_kind default_kind;
83 enum omp_region_type region_type;
86 static struct gimplify_ctx *gimplify_ctxp;
87 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
90 /* Forward declaration. */
91 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
93 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
94 form and we don't do any syntax checking. */
96 void
97 mark_addressable (tree x)
99 while (handled_component_p (x))
100 x = TREE_OPERAND (x, 0);
101 if (TREE_CODE (x) == MEM_REF
102 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
103 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
104 if (TREE_CODE (x) != VAR_DECL
105 && TREE_CODE (x) != PARM_DECL
106 && TREE_CODE (x) != RESULT_DECL)
107 return;
108 TREE_ADDRESSABLE (x) = 1;
110 /* Also mark the artificial SSA_NAME that points to the partition of X. */
111 if (TREE_CODE (x) == VAR_DECL
112 && !DECL_EXTERNAL (x)
113 && !TREE_STATIC (x)
114 && cfun->gimple_df != NULL
115 && cfun->gimple_df->decls_to_pointers != NULL)
117 void *namep
118 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
119 if (namep)
120 TREE_ADDRESSABLE (*(tree *)namep) = 1;
124 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
125 *SEQ_P is NULL, a new sequence is allocated. This function is
126 similar to gimple_seq_add_stmt, but does not scan the operands.
127 During gimplification, we need to manipulate statement sequences
128 before the def/use vectors have been constructed. */
130 void
131 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
133 gimple_stmt_iterator si;
135 if (gs == NULL)
136 return;
138 si = gsi_last (*seq_p);
139 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
142 /* Shorter alias name for the above function for use in gimplify.c
143 only. */
145 static inline void
146 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
148 gimple_seq_add_stmt_without_update (seq_p, gs);
151 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
152 NULL, a new sequence is allocated. This function is
153 similar to gimple_seq_add_seq, but does not scan the operands.
154 During gimplification, we need to manipulate statement sequences
155 before the def/use vectors have been constructed. */
157 static void
158 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
160 gimple_stmt_iterator si;
162 if (src == NULL)
163 return;
165 si = gsi_last (*dst_p);
166 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
169 /* Set up a context for the gimplifier. */
171 void
172 push_gimplify_context (struct gimplify_ctx *c)
174 memset (c, '\0', sizeof (*c));
175 c->prev_context = gimplify_ctxp;
176 gimplify_ctxp = c;
179 /* Tear down a context for the gimplifier. If BODY is non-null, then
180 put the temporaries into the outer BIND_EXPR. Otherwise, put them
181 in the local_decls.
183 BODY is not a sequence, but the first tuple in a sequence. */
185 void
186 pop_gimplify_context (gimple body)
188 struct gimplify_ctx *c = gimplify_ctxp;
190 gcc_assert (c
191 && (!c->bind_expr_stack.exists ()
192 || c->bind_expr_stack.is_empty ()));
193 c->bind_expr_stack.release ();
194 gimplify_ctxp = c->prev_context;
196 if (body)
197 declare_vars (c->temps, body, false);
198 else
199 record_vars (c->temps);
201 if (c->temp_htab.is_created ())
202 c->temp_htab.dispose ();
205 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
207 static void
208 gimple_push_bind_expr (gimple gimple_bind)
210 gimplify_ctxp->bind_expr_stack.reserve (8);
211 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
214 /* Pop the first element off the stack of bindings. */
216 static void
217 gimple_pop_bind_expr (void)
219 gimplify_ctxp->bind_expr_stack.pop ();
222 /* Return the first element of the stack of bindings. */
224 gimple
225 gimple_current_bind_expr (void)
227 return gimplify_ctxp->bind_expr_stack.last ();
230 /* Return the stack of bindings created during gimplification. */
232 vec<gimple>
233 gimple_bind_expr_stack (void)
235 return gimplify_ctxp->bind_expr_stack;
238 /* Return true iff there is a COND_EXPR between us and the innermost
239 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
241 static bool
242 gimple_conditional_context (void)
244 return gimplify_ctxp->conditions > 0;
247 /* Note that we've entered a COND_EXPR. */
249 static void
250 gimple_push_condition (void)
252 #ifdef ENABLE_GIMPLE_CHECKING
253 if (gimplify_ctxp->conditions == 0)
254 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
255 #endif
256 ++(gimplify_ctxp->conditions);
259 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
260 now, add any conditional cleanups we've seen to the prequeue. */
262 static void
263 gimple_pop_condition (gimple_seq *pre_p)
265 int conds = --(gimplify_ctxp->conditions);
267 gcc_assert (conds >= 0);
268 if (conds == 0)
270 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
271 gimplify_ctxp->conditional_cleanups = NULL;
275 /* A stable comparison routine for use with splay trees and DECLs. */
277 static int
278 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
280 tree a = (tree) xa;
281 tree b = (tree) xb;
283 return DECL_UID (a) - DECL_UID (b);
286 /* Create a new omp construct that deals with variable remapping. */
288 static struct gimplify_omp_ctx *
289 new_omp_context (enum omp_region_type region_type)
291 struct gimplify_omp_ctx *c;
293 c = XCNEW (struct gimplify_omp_ctx);
294 c->outer_context = gimplify_omp_ctxp;
295 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
296 c->privatized_types = pointer_set_create ();
297 c->location = input_location;
298 c->region_type = region_type;
299 if ((region_type & ORT_TASK) == 0)
300 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
301 else
302 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
304 return c;
307 /* Destroy an omp construct that deals with variable remapping. */
309 static void
310 delete_omp_context (struct gimplify_omp_ctx *c)
312 splay_tree_delete (c->variables);
313 pointer_set_destroy (c->privatized_types);
314 XDELETE (c);
317 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
318 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
320 /* Both gimplify the statement T and append it to *SEQ_P. This function
321 behaves exactly as gimplify_stmt, but you don't have to pass T as a
322 reference. */
324 void
325 gimplify_and_add (tree t, gimple_seq *seq_p)
327 gimplify_stmt (&t, seq_p);
330 /* Gimplify statement T into sequence *SEQ_P, and return the first
331 tuple in the sequence of generated tuples for this statement.
332 Return NULL if gimplifying T produced no tuples. */
334 static gimple
335 gimplify_and_return_first (tree t, gimple_seq *seq_p)
337 gimple_stmt_iterator last = gsi_last (*seq_p);
339 gimplify_and_add (t, seq_p);
341 if (!gsi_end_p (last))
343 gsi_next (&last);
344 return gsi_stmt (last);
346 else
347 return gimple_seq_first_stmt (*seq_p);
350 /* Strip off a legitimate source ending from the input string NAME of
351 length LEN. Rather than having to know the names used by all of
352 our front ends, we strip off an ending of a period followed by
353 up to five characters. (Java uses ".class".) */
355 static inline void
356 remove_suffix (char *name, int len)
358 int i;
360 for (i = 2; i < 8 && len > i; i++)
362 if (name[len - i] == '.')
364 name[len - i] = '\0';
365 break;
370 /* Create a new temporary name with PREFIX. Return an identifier. */
372 static GTY(()) unsigned int tmp_var_id_num;
374 tree
375 create_tmp_var_name (const char *prefix)
377 char *tmp_name;
379 if (prefix)
381 char *preftmp = ASTRDUP (prefix);
383 remove_suffix (preftmp, strlen (preftmp));
384 clean_symbol_name (preftmp);
386 prefix = preftmp;
389 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
390 return get_identifier (tmp_name);
393 /* Create a new temporary variable declaration of type TYPE.
394 Do NOT push it into the current binding. */
396 tree
397 create_tmp_var_raw (tree type, const char *prefix)
399 tree tmp_var;
401 tmp_var = build_decl (input_location,
402 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
403 type);
405 /* The variable was declared by the compiler. */
406 DECL_ARTIFICIAL (tmp_var) = 1;
407 /* And we don't want debug info for it. */
408 DECL_IGNORED_P (tmp_var) = 1;
410 /* Make the variable writable. */
411 TREE_READONLY (tmp_var) = 0;
413 DECL_EXTERNAL (tmp_var) = 0;
414 TREE_STATIC (tmp_var) = 0;
415 TREE_USED (tmp_var) = 1;
417 return tmp_var;
420 /* Create a new temporary variable declaration of type TYPE. DO push the
421 variable into the current binding. Further, assume that this is called
422 only from gimplification or optimization, at which point the creation of
423 certain types are bugs. */
425 tree
426 create_tmp_var (tree type, const char *prefix)
428 tree tmp_var;
430 /* We don't allow types that are addressable (meaning we can't make copies),
431 or incomplete. We also used to reject every variable size objects here,
432 but now support those for which a constant upper bound can be obtained.
433 The processing for variable sizes is performed in gimple_add_tmp_var,
434 point at which it really matters and possibly reached via paths not going
435 through this function, e.g. after direct calls to create_tmp_var_raw. */
436 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
438 tmp_var = create_tmp_var_raw (type, prefix);
439 gimple_add_tmp_var (tmp_var);
440 return tmp_var;
443 /* Create a new temporary variable declaration of type TYPE by calling
444 create_tmp_var and if TYPE is a vector or a complex number, mark the new
445 temporary as gimple register. */
447 tree
448 create_tmp_reg (tree type, const char *prefix)
450 tree tmp;
452 tmp = create_tmp_var (type, prefix);
453 if (TREE_CODE (type) == COMPLEX_TYPE
454 || TREE_CODE (type) == VECTOR_TYPE)
455 DECL_GIMPLE_REG_P (tmp) = 1;
457 return tmp;
460 /* Returns true iff T is a valid RHS for an assignment to a renamed
461 user -- or front-end generated artificial -- variable. */
463 static bool
464 is_gimple_reg_rhs (tree t)
466 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
469 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
470 LHS, or for a call argument. */
472 static bool
473 is_gimple_mem_rhs (tree t)
475 /* If we're dealing with a renamable type, either source or dest must be
476 a renamed variable. */
477 if (is_gimple_reg_type (TREE_TYPE (t)))
478 return is_gimple_val (t);
479 else
480 return is_gimple_val (t) || is_gimple_lvalue (t);
483 /* Return true if T is a CALL_EXPR or an expression that can be
484 assigned to a temporary. Note that this predicate should only be
485 used during gimplification. See the rationale for this in
486 gimplify_modify_expr. */
488 static bool
489 is_gimple_reg_rhs_or_call (tree t)
491 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
492 || TREE_CODE (t) == CALL_EXPR);
495 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
496 this predicate should only be used during gimplification. See the
497 rationale for this in gimplify_modify_expr. */
499 static bool
500 is_gimple_mem_rhs_or_call (tree t)
502 /* If we're dealing with a renamable type, either source or dest must be
503 a renamed variable. */
504 if (is_gimple_reg_type (TREE_TYPE (t)))
505 return is_gimple_val (t);
506 else
507 return (is_gimple_val (t) || is_gimple_lvalue (t)
508 || TREE_CODE (t) == CALL_EXPR);
511 /* Create a temporary with a name derived from VAL. Subroutine of
512 lookup_tmp_var; nobody else should call this function. */
514 static inline tree
515 create_tmp_from_val (tree val, bool is_formal)
517 /* Drop all qualifiers and address-space information from the value type. */
518 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
519 tree var = create_tmp_var (type, get_name (val));
520 if (is_formal
521 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
522 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
523 DECL_GIMPLE_REG_P (var) = 1;
524 return var;
527 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
528 an existing expression temporary. */
530 static tree
531 lookup_tmp_var (tree val, bool is_formal)
533 tree ret;
535 /* If not optimizing, never really reuse a temporary. local-alloc
536 won't allocate any variable that is used in more than one basic
537 block, which means it will go into memory, causing much extra
538 work in reload and final and poorer code generation, outweighing
539 the extra memory allocation here. */
540 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
541 ret = create_tmp_from_val (val, is_formal);
542 else
544 elt_t elt, *elt_p;
545 elt_t **slot;
547 elt.val = val;
548 if (!gimplify_ctxp->temp_htab.is_created ())
549 gimplify_ctxp->temp_htab.create (1000);
550 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
551 if (*slot == NULL)
553 elt_p = XNEW (elt_t);
554 elt_p->val = val;
555 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
556 *slot = elt_p;
558 else
560 elt_p = *slot;
561 ret = elt_p->temp;
565 return ret;
568 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
570 static tree
571 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
572 bool is_formal)
574 tree t, mod;
576 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
577 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
578 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
579 fb_rvalue);
581 if (gimplify_ctxp->into_ssa
582 && is_gimple_reg_type (TREE_TYPE (val)))
583 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
584 else
585 t = lookup_tmp_var (val, is_formal);
587 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
589 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
591 /* gimplify_modify_expr might want to reduce this further. */
592 gimplify_and_add (mod, pre_p);
593 ggc_free (mod);
595 return t;
598 /* Return a formal temporary variable initialized with VAL. PRE_P is as
599 in gimplify_expr. Only use this function if:
601 1) The value of the unfactored expression represented by VAL will not
602 change between the initialization and use of the temporary, and
603 2) The temporary will not be otherwise modified.
605 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
606 and #2 means it is inappropriate for && temps.
608 For other cases, use get_initialized_tmp_var instead. */
610 tree
611 get_formal_tmp_var (tree val, gimple_seq *pre_p)
613 return internal_get_tmp_var (val, pre_p, NULL, true);
616 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
617 are as in gimplify_expr. */
619 tree
620 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
622 return internal_get_tmp_var (val, pre_p, post_p, false);
625 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
626 generate debug info for them; otherwise don't. */
628 void
629 declare_vars (tree vars, gimple scope, bool debug_info)
631 tree last = vars;
632 if (last)
634 tree temps, block;
636 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
638 temps = nreverse (last);
640 block = gimple_bind_block (scope);
641 gcc_assert (!block || TREE_CODE (block) == BLOCK);
642 if (!block || !debug_info)
644 DECL_CHAIN (last) = gimple_bind_vars (scope);
645 gimple_bind_set_vars (scope, temps);
647 else
649 /* We need to attach the nodes both to the BIND_EXPR and to its
650 associated BLOCK for debugging purposes. The key point here
651 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
652 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
653 if (BLOCK_VARS (block))
654 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
655 else
657 gimple_bind_set_vars (scope,
658 chainon (gimple_bind_vars (scope), temps));
659 BLOCK_VARS (block) = temps;
665 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
666 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
667 no such upper bound can be obtained. */
669 static void
670 force_constant_size (tree var)
672 /* The only attempt we make is by querying the maximum size of objects
673 of the variable's type. */
675 HOST_WIDE_INT max_size;
677 gcc_assert (TREE_CODE (var) == VAR_DECL);
679 max_size = max_int_size_in_bytes (TREE_TYPE (var));
681 gcc_assert (max_size >= 0);
683 DECL_SIZE_UNIT (var)
684 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
685 DECL_SIZE (var)
686 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
689 /* Push the temporary variable TMP into the current binding. */
691 void
692 gimple_add_tmp_var (tree tmp)
694 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
696 /* Later processing assumes that the object size is constant, which might
697 not be true at this point. Force the use of a constant upper bound in
698 this case. */
699 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
700 force_constant_size (tmp);
702 DECL_CONTEXT (tmp) = current_function_decl;
703 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
705 if (gimplify_ctxp)
707 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
708 gimplify_ctxp->temps = tmp;
710 /* Mark temporaries local within the nearest enclosing parallel. */
711 if (gimplify_omp_ctxp)
713 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
714 while (ctx && ctx->region_type == ORT_WORKSHARE)
715 ctx = ctx->outer_context;
716 if (ctx)
717 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
720 else if (cfun)
721 record_vars (tmp);
722 else
724 gimple_seq body_seq;
726 /* This case is for nested functions. We need to expose the locals
727 they create. */
728 body_seq = gimple_body (current_function_decl);
729 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
733 /* Determine whether to assign a location to the statement GS. */
735 static bool
736 should_carry_location_p (gimple gs)
738 /* Don't emit a line note for a label. We particularly don't want to
739 emit one for the break label, since it doesn't actually correspond
740 to the beginning of the loop/switch. */
741 if (gimple_code (gs) == GIMPLE_LABEL)
742 return false;
744 return true;
747 /* Return true if a location should not be emitted for this statement
748 by annotate_one_with_location. */
750 static inline bool
751 gimple_do_not_emit_location_p (gimple g)
753 return gimple_plf (g, GF_PLF_1);
756 /* Mark statement G so a location will not be emitted by
757 annotate_one_with_location. */
759 static inline void
760 gimple_set_do_not_emit_location (gimple g)
762 /* The PLF flags are initialized to 0 when a new tuple is created,
763 so no need to initialize it anywhere. */
764 gimple_set_plf (g, GF_PLF_1, true);
767 /* Set the location for gimple statement GS to LOCATION. */
769 static void
770 annotate_one_with_location (gimple gs, location_t location)
772 if (!gimple_has_location (gs)
773 && !gimple_do_not_emit_location_p (gs)
774 && should_carry_location_p (gs))
775 gimple_set_location (gs, location);
778 /* Set LOCATION for all the statements after iterator GSI in sequence
779 SEQ. If GSI is pointing to the end of the sequence, start with the
780 first statement in SEQ. */
782 static void
783 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
784 location_t location)
786 if (gsi_end_p (gsi))
787 gsi = gsi_start (seq);
788 else
789 gsi_next (&gsi);
791 for (; !gsi_end_p (gsi); gsi_next (&gsi))
792 annotate_one_with_location (gsi_stmt (gsi), location);
795 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
797 void
798 annotate_all_with_location (gimple_seq stmt_p, location_t location)
800 gimple_stmt_iterator i;
802 if (gimple_seq_empty_p (stmt_p))
803 return;
805 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
807 gimple gs = gsi_stmt (i);
808 annotate_one_with_location (gs, location);
812 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
813 nodes that are referenced more than once in GENERIC functions. This is
814 necessary because gimplification (translation into GIMPLE) is performed
815 by modifying tree nodes in-place, so gimplication of a shared node in a
816 first context could generate an invalid GIMPLE form in a second context.
818 This is achieved with a simple mark/copy/unmark algorithm that walks the
819 GENERIC representation top-down, marks nodes with TREE_VISITED the first
820 time it encounters them, duplicates them if they already have TREE_VISITED
821 set, and finally removes the TREE_VISITED marks it has set.
823 The algorithm works only at the function level, i.e. it generates a GENERIC
824 representation of a function with no nodes shared within the function when
825 passed a GENERIC function (except for nodes that are allowed to be shared).
827 At the global level, it is also necessary to unshare tree nodes that are
828 referenced in more than one function, for the same aforementioned reason.
829 This requires some cooperation from the front-end. There are 2 strategies:
831 1. Manual unsharing. The front-end needs to call unshare_expr on every
832 expression that might end up being shared across functions.
834 2. Deep unsharing. This is an extension of regular unsharing. Instead
835 of calling unshare_expr on expressions that might be shared across
836 functions, the front-end pre-marks them with TREE_VISITED. This will
837 ensure that they are unshared on the first reference within functions
838 when the regular unsharing algorithm runs. The counterpart is that
839 this algorithm must look deeper than for manual unsharing, which is
840 specified by LANG_HOOKS_DEEP_UNSHARING.
842 If there are only few specific cases of node sharing across functions, it is
843 probably easier for a front-end to unshare the expressions manually. On the
844 contrary, if the expressions generated at the global level are as widespread
845 as expressions generated within functions, deep unsharing is very likely the
846 way to go. */
848 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
849 These nodes model computations that must be done once. If we were to
850 unshare something like SAVE_EXPR(i++), the gimplification process would
851 create wrong code. However, if DATA is non-null, it must hold a pointer
852 set that is used to unshare the subtrees of these nodes. */
854 static tree
855 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
857 tree t = *tp;
858 enum tree_code code = TREE_CODE (t);
860 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
861 copy their subtrees if we can make sure to do it only once. */
862 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
864 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
866 else
867 *walk_subtrees = 0;
870 /* Stop at types, decls, constants like copy_tree_r. */
871 else if (TREE_CODE_CLASS (code) == tcc_type
872 || TREE_CODE_CLASS (code) == tcc_declaration
873 || TREE_CODE_CLASS (code) == tcc_constant
874 /* We can't do anything sensible with a BLOCK used as an
875 expression, but we also can't just die when we see it
876 because of non-expression uses. So we avert our eyes
877 and cross our fingers. Silly Java. */
878 || code == BLOCK)
879 *walk_subtrees = 0;
881 /* Cope with the statement expression extension. */
882 else if (code == STATEMENT_LIST)
885 /* Leave the bulk of the work to copy_tree_r itself. */
886 else
887 copy_tree_r (tp, walk_subtrees, NULL);
889 return NULL_TREE;
892 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
893 If *TP has been visited already, then *TP is deeply copied by calling
894 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
896 static tree
897 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
899 tree t = *tp;
900 enum tree_code code = TREE_CODE (t);
902 /* Skip types, decls, and constants. But we do want to look at their
903 types and the bounds of types. Mark them as visited so we properly
904 unmark their subtrees on the unmark pass. If we've already seen them,
905 don't look down further. */
906 if (TREE_CODE_CLASS (code) == tcc_type
907 || TREE_CODE_CLASS (code) == tcc_declaration
908 || TREE_CODE_CLASS (code) == tcc_constant)
910 if (TREE_VISITED (t))
911 *walk_subtrees = 0;
912 else
913 TREE_VISITED (t) = 1;
916 /* If this node has been visited already, unshare it and don't look
917 any deeper. */
918 else if (TREE_VISITED (t))
920 walk_tree (tp, mostly_copy_tree_r, data, NULL);
921 *walk_subtrees = 0;
924 /* Otherwise, mark the node as visited and keep looking. */
925 else
926 TREE_VISITED (t) = 1;
928 return NULL_TREE;
931 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
932 copy_if_shared_r callback unmodified. */
934 static inline void
935 copy_if_shared (tree *tp, void *data)
937 walk_tree (tp, copy_if_shared_r, data, NULL);
940 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
941 any nested functions. */
943 static void
944 unshare_body (tree fndecl)
946 struct cgraph_node *cgn = cgraph_get_node (fndecl);
947 /* If the language requires deep unsharing, we need a pointer set to make
948 sure we don't repeatedly unshare subtrees of unshareable nodes. */
949 struct pointer_set_t *visited
950 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
952 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
953 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
954 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
956 if (visited)
957 pointer_set_destroy (visited);
959 if (cgn)
960 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
961 unshare_body (cgn->symbol.decl);
964 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
965 Subtrees are walked until the first unvisited node is encountered. */
967 static tree
968 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
970 tree t = *tp;
972 /* If this node has been visited, unmark it and keep looking. */
973 if (TREE_VISITED (t))
974 TREE_VISITED (t) = 0;
976 /* Otherwise, don't look any deeper. */
977 else
978 *walk_subtrees = 0;
980 return NULL_TREE;
983 /* Unmark the visited trees rooted at *TP. */
985 static inline void
986 unmark_visited (tree *tp)
988 walk_tree (tp, unmark_visited_r, NULL, NULL);
991 /* Likewise, but mark all trees as not visited. */
993 static void
994 unvisit_body (tree fndecl)
996 struct cgraph_node *cgn = cgraph_get_node (fndecl);
998 unmark_visited (&DECL_SAVED_TREE (fndecl));
999 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1000 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1002 if (cgn)
1003 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1004 unvisit_body (cgn->symbol.decl);
1007 /* Unconditionally make an unshared copy of EXPR. This is used when using
1008 stored expressions which span multiple functions, such as BINFO_VTABLE,
1009 as the normal unsharing process can't tell that they're shared. */
1011 tree
1012 unshare_expr (tree expr)
1014 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1015 return expr;
1018 /* Worker for unshare_expr_without_location. */
1020 static tree
1021 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1023 if (EXPR_P (*tp))
1024 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1025 else
1026 *walk_subtrees = 0;
1027 return NULL_TREE;
1030 /* Similar to unshare_expr but also prune all expression locations
1031 from EXPR. */
1033 tree
1034 unshare_expr_without_location (tree expr)
1036 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1037 if (EXPR_P (expr))
1038 walk_tree (&expr, prune_expr_location, NULL, NULL);
1039 return expr;
1042 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1043 contain statements and have a value. Assign its value to a temporary
1044 and give it void_type_node. Return the temporary, or NULL_TREE if
1045 WRAPPER was already void. */
1047 tree
1048 voidify_wrapper_expr (tree wrapper, tree temp)
1050 tree type = TREE_TYPE (wrapper);
1051 if (type && !VOID_TYPE_P (type))
1053 tree *p;
1055 /* Set p to point to the body of the wrapper. Loop until we find
1056 something that isn't a wrapper. */
1057 for (p = &wrapper; p && *p; )
1059 switch (TREE_CODE (*p))
1061 case BIND_EXPR:
1062 TREE_SIDE_EFFECTS (*p) = 1;
1063 TREE_TYPE (*p) = void_type_node;
1064 /* For a BIND_EXPR, the body is operand 1. */
1065 p = &BIND_EXPR_BODY (*p);
1066 break;
1068 case CLEANUP_POINT_EXPR:
1069 case TRY_FINALLY_EXPR:
1070 case TRY_CATCH_EXPR:
1071 TREE_SIDE_EFFECTS (*p) = 1;
1072 TREE_TYPE (*p) = void_type_node;
1073 p = &TREE_OPERAND (*p, 0);
1074 break;
1076 case STATEMENT_LIST:
1078 tree_stmt_iterator i = tsi_last (*p);
1079 TREE_SIDE_EFFECTS (*p) = 1;
1080 TREE_TYPE (*p) = void_type_node;
1081 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1083 break;
1085 case COMPOUND_EXPR:
1086 /* Advance to the last statement. Set all container types to
1087 void. */
1088 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1090 TREE_SIDE_EFFECTS (*p) = 1;
1091 TREE_TYPE (*p) = void_type_node;
1093 break;
1095 case TRANSACTION_EXPR:
1096 TREE_SIDE_EFFECTS (*p) = 1;
1097 TREE_TYPE (*p) = void_type_node;
1098 p = &TRANSACTION_EXPR_BODY (*p);
1099 break;
1101 default:
1102 /* Assume that any tree upon which voidify_wrapper_expr is
1103 directly called is a wrapper, and that its body is op0. */
1104 if (p == &wrapper)
1106 TREE_SIDE_EFFECTS (*p) = 1;
1107 TREE_TYPE (*p) = void_type_node;
1108 p = &TREE_OPERAND (*p, 0);
1109 break;
1111 goto out;
1115 out:
1116 if (p == NULL || IS_EMPTY_STMT (*p))
1117 temp = NULL_TREE;
1118 else if (temp)
1120 /* The wrapper is on the RHS of an assignment that we're pushing
1121 down. */
1122 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1123 || TREE_CODE (temp) == MODIFY_EXPR);
1124 TREE_OPERAND (temp, 1) = *p;
1125 *p = temp;
1127 else
1129 temp = create_tmp_var (type, "retval");
1130 *p = build2 (INIT_EXPR, type, temp, *p);
1133 return temp;
1136 return NULL_TREE;
1139 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1140 a temporary through which they communicate. */
1142 static void
1143 build_stack_save_restore (gimple *save, gimple *restore)
1145 tree tmp_var;
1147 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1148 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1149 gimple_call_set_lhs (*save, tmp_var);
1151 *restore
1152 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1153 1, tmp_var);
1156 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1158 static enum gimplify_status
1159 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1161 tree bind_expr = *expr_p;
1162 bool old_save_stack = gimplify_ctxp->save_stack;
1163 tree t;
1164 gimple gimple_bind;
1165 gimple_seq body, cleanup;
1166 gimple stack_save;
1168 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1170 /* Mark variables seen in this bind expr. */
1171 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1173 if (TREE_CODE (t) == VAR_DECL)
1175 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1177 /* Mark variable as local. */
1178 if (ctx && !DECL_EXTERNAL (t)
1179 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1180 || splay_tree_lookup (ctx->variables,
1181 (splay_tree_key) t) == NULL))
1182 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1184 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1186 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1187 cfun->has_local_explicit_reg_vars = true;
1190 /* Preliminarily mark non-addressed complex variables as eligible
1191 for promotion to gimple registers. We'll transform their uses
1192 as we find them. */
1193 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1194 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1195 && !TREE_THIS_VOLATILE (t)
1196 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1197 && !needs_to_live_in_memory (t))
1198 DECL_GIMPLE_REG_P (t) = 1;
1201 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1202 BIND_EXPR_BLOCK (bind_expr));
1203 gimple_push_bind_expr (gimple_bind);
1205 gimplify_ctxp->save_stack = false;
1207 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1208 body = NULL;
1209 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1210 gimple_bind_set_body (gimple_bind, body);
1212 cleanup = NULL;
1213 stack_save = NULL;
1214 if (gimplify_ctxp->save_stack)
1216 gimple stack_restore;
1218 /* Save stack on entry and restore it on exit. Add a try_finally
1219 block to achieve this. Note that mudflap depends on the
1220 format of the emitted code: see mx_register_decls(). */
1221 build_stack_save_restore (&stack_save, &stack_restore);
1223 gimplify_seq_add_stmt (&cleanup, stack_restore);
1226 /* Add clobbers for all variables that go out of scope. */
1227 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1229 if (TREE_CODE (t) == VAR_DECL
1230 && !is_global_var (t)
1231 && DECL_CONTEXT (t) == current_function_decl
1232 && !DECL_HARD_REGISTER (t)
1233 && !TREE_THIS_VOLATILE (t)
1234 && !DECL_HAS_VALUE_EXPR_P (t)
1235 /* Only care for variables that have to be in memory. Others
1236 will be rewritten into SSA names, hence moved to the top-level. */
1237 && !is_gimple_reg (t)
1238 && flag_stack_reuse != SR_NONE)
1240 tree clobber = build_constructor (TREE_TYPE (t),
1241 NULL);
1242 TREE_THIS_VOLATILE (clobber) = 1;
1243 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1247 if (cleanup)
1249 gimple gs;
1250 gimple_seq new_body;
1252 new_body = NULL;
1253 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1254 GIMPLE_TRY_FINALLY);
1256 if (stack_save)
1257 gimplify_seq_add_stmt (&new_body, stack_save);
1258 gimplify_seq_add_stmt (&new_body, gs);
1259 gimple_bind_set_body (gimple_bind, new_body);
1262 gimplify_ctxp->save_stack = old_save_stack;
1263 gimple_pop_bind_expr ();
1265 gimplify_seq_add_stmt (pre_p, gimple_bind);
1267 if (temp)
1269 *expr_p = temp;
1270 return GS_OK;
1273 *expr_p = NULL_TREE;
1274 return GS_ALL_DONE;
1277 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1278 GIMPLE value, it is assigned to a new temporary and the statement is
1279 re-written to return the temporary.
1281 PRE_P points to the sequence where side effects that must happen before
1282 STMT should be stored. */
1284 static enum gimplify_status
1285 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1287 gimple ret;
1288 tree ret_expr = TREE_OPERAND (stmt, 0);
1289 tree result_decl, result;
1291 if (ret_expr == error_mark_node)
1292 return GS_ERROR;
1294 if (!ret_expr
1295 || TREE_CODE (ret_expr) == RESULT_DECL
1296 || ret_expr == error_mark_node)
1298 gimple ret = gimple_build_return (ret_expr);
1299 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1300 gimplify_seq_add_stmt (pre_p, ret);
1301 return GS_ALL_DONE;
1304 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1305 result_decl = NULL_TREE;
1306 else
1308 result_decl = TREE_OPERAND (ret_expr, 0);
1310 /* See through a return by reference. */
1311 if (TREE_CODE (result_decl) == INDIRECT_REF)
1312 result_decl = TREE_OPERAND (result_decl, 0);
1314 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1315 || TREE_CODE (ret_expr) == INIT_EXPR)
1316 && TREE_CODE (result_decl) == RESULT_DECL);
1319 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1320 Recall that aggregate_value_p is FALSE for any aggregate type that is
1321 returned in registers. If we're returning values in registers, then
1322 we don't want to extend the lifetime of the RESULT_DECL, particularly
1323 across another call. In addition, for those aggregates for which
1324 hard_function_value generates a PARALLEL, we'll die during normal
1325 expansion of structure assignments; there's special code in expand_return
1326 to handle this case that does not exist in expand_expr. */
1327 if (!result_decl)
1328 result = NULL_TREE;
1329 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1331 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1333 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1334 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1335 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1336 should be effectively allocated by the caller, i.e. all calls to
1337 this function must be subject to the Return Slot Optimization. */
1338 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1339 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1341 result = result_decl;
1343 else if (gimplify_ctxp->return_temp)
1344 result = gimplify_ctxp->return_temp;
1345 else
1347 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1349 /* ??? With complex control flow (usually involving abnormal edges),
1350 we can wind up warning about an uninitialized value for this. Due
1351 to how this variable is constructed and initialized, this is never
1352 true. Give up and never warn. */
1353 TREE_NO_WARNING (result) = 1;
1355 gimplify_ctxp->return_temp = result;
1358 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1359 Then gimplify the whole thing. */
1360 if (result != result_decl)
1361 TREE_OPERAND (ret_expr, 0) = result;
1363 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1365 ret = gimple_build_return (result);
1366 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1367 gimplify_seq_add_stmt (pre_p, ret);
1369 return GS_ALL_DONE;
1372 /* Gimplify a variable-length array DECL. */
1374 static void
1375 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1377 /* This is a variable-sized decl. Simplify its size and mark it
1378 for deferred expansion. Note that mudflap depends on the format
1379 of the emitted code: see mx_register_decls(). */
1380 tree t, addr, ptr_type;
1382 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1383 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1385 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1386 if (DECL_HAS_VALUE_EXPR_P (decl))
1387 return;
1389 /* All occurrences of this decl in final gimplified code will be
1390 replaced by indirection. Setting DECL_VALUE_EXPR does two
1391 things: First, it lets the rest of the gimplifier know what
1392 replacement to use. Second, it lets the debug info know
1393 where to find the value. */
1394 ptr_type = build_pointer_type (TREE_TYPE (decl));
1395 addr = create_tmp_var (ptr_type, get_name (decl));
1396 DECL_IGNORED_P (addr) = 0;
1397 t = build_fold_indirect_ref (addr);
1398 TREE_THIS_NOTRAP (t) = 1;
1399 SET_DECL_VALUE_EXPR (decl, t);
1400 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1402 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1403 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1404 size_int (DECL_ALIGN (decl)));
1405 /* The call has been built for a variable-sized object. */
1406 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1407 t = fold_convert (ptr_type, t);
1408 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1410 gimplify_and_add (t, seq_p);
1412 /* Indicate that we need to restore the stack level when the
1413 enclosing BIND_EXPR is exited. */
1414 gimplify_ctxp->save_stack = true;
1417 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1418 and initialization explicit. */
1420 static enum gimplify_status
1421 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1423 tree stmt = *stmt_p;
1424 tree decl = DECL_EXPR_DECL (stmt);
1426 *stmt_p = NULL_TREE;
1428 if (TREE_TYPE (decl) == error_mark_node)
1429 return GS_ERROR;
1431 if ((TREE_CODE (decl) == TYPE_DECL
1432 || TREE_CODE (decl) == VAR_DECL)
1433 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1434 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1436 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1437 in case its size expressions contain problematic nodes like CALL_EXPR. */
1438 if (TREE_CODE (decl) == TYPE_DECL
1439 && DECL_ORIGINAL_TYPE (decl)
1440 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1441 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1443 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1445 tree init = DECL_INITIAL (decl);
1447 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1448 || (!TREE_STATIC (decl)
1449 && flag_stack_check == GENERIC_STACK_CHECK
1450 && compare_tree_int (DECL_SIZE_UNIT (decl),
1451 STACK_CHECK_MAX_VAR_SIZE) > 0))
1452 gimplify_vla_decl (decl, seq_p);
1454 /* Some front ends do not explicitly declare all anonymous
1455 artificial variables. We compensate here by declaring the
1456 variables, though it would be better if the front ends would
1457 explicitly declare them. */
1458 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1459 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1460 gimple_add_tmp_var (decl);
1462 if (init && init != error_mark_node)
1464 if (!TREE_STATIC (decl))
1466 DECL_INITIAL (decl) = NULL_TREE;
1467 init = build2 (INIT_EXPR, void_type_node, decl, init);
1468 gimplify_and_add (init, seq_p);
1469 ggc_free (init);
1471 else
1472 /* We must still examine initializers for static variables
1473 as they may contain a label address. */
1474 walk_tree (&init, force_labels_r, NULL, NULL);
1478 return GS_ALL_DONE;
1481 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1482 and replacing the LOOP_EXPR with goto, but if the loop contains an
1483 EXIT_EXPR, we need to append a label for it to jump to. */
1485 static enum gimplify_status
1486 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1488 tree saved_label = gimplify_ctxp->exit_label;
1489 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1491 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1493 gimplify_ctxp->exit_label = NULL_TREE;
1495 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1497 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1499 if (gimplify_ctxp->exit_label)
1500 gimplify_seq_add_stmt (pre_p,
1501 gimple_build_label (gimplify_ctxp->exit_label));
1503 gimplify_ctxp->exit_label = saved_label;
1505 *expr_p = NULL;
1506 return GS_ALL_DONE;
1509 /* Gimplify a statement list onto a sequence. These may be created either
1510 by an enlightened front-end, or by shortcut_cond_expr. */
1512 static enum gimplify_status
1513 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1515 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1517 tree_stmt_iterator i = tsi_start (*expr_p);
1519 while (!tsi_end_p (i))
1521 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1522 tsi_delink (&i);
1525 if (temp)
1527 *expr_p = temp;
1528 return GS_OK;
1531 return GS_ALL_DONE;
1534 /* Compare two case labels. Because the front end should already have
1535 made sure that case ranges do not overlap, it is enough to only compare
1536 the CASE_LOW values of each case label. */
1538 static int
1539 compare_case_labels (const void *p1, const void *p2)
1541 const_tree const case1 = *(const_tree const*)p1;
1542 const_tree const case2 = *(const_tree const*)p2;
1544 /* The 'default' case label always goes first. */
1545 if (!CASE_LOW (case1))
1546 return -1;
1547 else if (!CASE_LOW (case2))
1548 return 1;
1549 else
1550 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1553 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1555 void
1556 sort_case_labels (vec<tree> label_vec)
1558 label_vec.qsort (compare_case_labels);
1561 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1563 LABELS is a vector that contains all case labels to look at.
1565 INDEX_TYPE is the type of the switch index expression. Case labels
1566 in LABELS are discarded if their values are not in the value range
1567 covered by INDEX_TYPE. The remaining case label values are folded
1568 to INDEX_TYPE.
1570 If a default case exists in LABELS, it is removed from LABELS and
1571 returned in DEFAULT_CASEP. If no default case exists, but the
1572 case labels already cover the whole range of INDEX_TYPE, a default
1573 case is returned pointing to one of the existing case labels.
1574 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1576 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1577 apply and no action is taken regardless of whether a default case is
1578 found or not. */
1580 void
1581 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1582 tree index_type,
1583 tree *default_casep)
1585 tree min_value, max_value;
1586 tree default_case = NULL_TREE;
1587 size_t i, len;
1589 i = 0;
1590 min_value = TYPE_MIN_VALUE (index_type);
1591 max_value = TYPE_MAX_VALUE (index_type);
1592 while (i < labels.length ())
1594 tree elt = labels[i];
1595 tree low = CASE_LOW (elt);
1596 tree high = CASE_HIGH (elt);
1597 bool remove_element = FALSE;
1599 if (low)
1601 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1602 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1604 /* This is a non-default case label, i.e. it has a value.
1606 See if the case label is reachable within the range of
1607 the index type. Remove out-of-range case values. Turn
1608 case ranges into a canonical form (high > low strictly)
1609 and convert the case label values to the index type.
1611 NB: The type of gimple_switch_index() may be the promoted
1612 type, but the case labels retain the original type. */
1614 if (high)
1616 /* This is a case range. Discard empty ranges.
1617 If the bounds or the range are equal, turn this
1618 into a simple (one-value) case. */
1619 int cmp = tree_int_cst_compare (high, low);
1620 if (cmp < 0)
1621 remove_element = TRUE;
1622 else if (cmp == 0)
1623 high = NULL_TREE;
1626 if (! high)
1628 /* If the simple case value is unreachable, ignore it. */
1629 if ((TREE_CODE (min_value) == INTEGER_CST
1630 && tree_int_cst_compare (low, min_value) < 0)
1631 || (TREE_CODE (max_value) == INTEGER_CST
1632 && tree_int_cst_compare (low, max_value) > 0))
1633 remove_element = TRUE;
1634 else
1635 low = fold_convert (index_type, low);
1637 else
1639 /* If the entire case range is unreachable, ignore it. */
1640 if ((TREE_CODE (min_value) == INTEGER_CST
1641 && tree_int_cst_compare (high, min_value) < 0)
1642 || (TREE_CODE (max_value) == INTEGER_CST
1643 && tree_int_cst_compare (low, max_value) > 0))
1644 remove_element = TRUE;
1645 else
1647 /* If the lower bound is less than the index type's
1648 minimum value, truncate the range bounds. */
1649 if (TREE_CODE (min_value) == INTEGER_CST
1650 && tree_int_cst_compare (low, min_value) < 0)
1651 low = min_value;
1652 low = fold_convert (index_type, low);
1654 /* If the upper bound is greater than the index type's
1655 maximum value, truncate the range bounds. */
1656 if (TREE_CODE (max_value) == INTEGER_CST
1657 && tree_int_cst_compare (high, max_value) > 0)
1658 high = max_value;
1659 high = fold_convert (index_type, high);
1661 /* We may have folded a case range to a one-value case. */
1662 if (tree_int_cst_equal (low, high))
1663 high = NULL_TREE;
1667 CASE_LOW (elt) = low;
1668 CASE_HIGH (elt) = high;
1670 else
1672 gcc_assert (!default_case);
1673 default_case = elt;
1674 /* The default case must be passed separately to the
1675 gimple_build_switch routine. But if DEFAULT_CASEP
1676 is NULL, we do not remove the default case (it would
1677 be completely lost). */
1678 if (default_casep)
1679 remove_element = TRUE;
1682 if (remove_element)
1683 labels.ordered_remove (i);
1684 else
1685 i++;
1687 len = i;
1689 if (!labels.is_empty ())
1690 sort_case_labels (labels);
1692 if (default_casep && !default_case)
1694 /* If the switch has no default label, add one, so that we jump
1695 around the switch body. If the labels already cover the whole
1696 range of the switch index_type, add the default label pointing
1697 to one of the existing labels. */
1698 if (len
1699 && TYPE_MIN_VALUE (index_type)
1700 && TYPE_MAX_VALUE (index_type)
1701 && tree_int_cst_equal (CASE_LOW (labels[0]),
1702 TYPE_MIN_VALUE (index_type)))
1704 tree low, high = CASE_HIGH (labels[len - 1]);
1705 if (!high)
1706 high = CASE_LOW (labels[len - 1]);
1707 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1709 for (i = 1; i < len; i++)
1711 high = CASE_LOW (labels[i]);
1712 low = CASE_HIGH (labels[i - 1]);
1713 if (!low)
1714 low = CASE_LOW (labels[i - 1]);
1715 if ((TREE_INT_CST_LOW (low) + 1
1716 != TREE_INT_CST_LOW (high))
1717 || (TREE_INT_CST_HIGH (low)
1718 + (TREE_INT_CST_LOW (high) == 0)
1719 != TREE_INT_CST_HIGH (high)))
1720 break;
1722 if (i == len)
1724 tree label = CASE_LABEL (labels[0]);
1725 default_case = build_case_label (NULL_TREE, NULL_TREE,
1726 label);
1732 if (default_casep)
1733 *default_casep = default_case;
1736 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1737 branch to. */
1739 static enum gimplify_status
1740 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1742 tree switch_expr = *expr_p;
1743 gimple_seq switch_body_seq = NULL;
1744 enum gimplify_status ret;
1745 tree index_type = TREE_TYPE (switch_expr);
1746 if (index_type == NULL_TREE)
1747 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1749 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1750 fb_rvalue);
1751 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1752 return ret;
1754 if (SWITCH_BODY (switch_expr))
1756 vec<tree> labels;
1757 vec<tree> saved_labels;
1758 tree default_case = NULL_TREE;
1759 gimple gimple_switch;
1761 /* If someone can be bothered to fill in the labels, they can
1762 be bothered to null out the body too. */
1763 gcc_assert (!SWITCH_LABELS (switch_expr));
1765 /* Save old labels, get new ones from body, then restore the old
1766 labels. Save all the things from the switch body to append after. */
1767 saved_labels = gimplify_ctxp->case_labels;
1768 gimplify_ctxp->case_labels.create (8);
1770 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1771 labels = gimplify_ctxp->case_labels;
1772 gimplify_ctxp->case_labels = saved_labels;
1774 preprocess_case_label_vec_for_gimple (labels, index_type,
1775 &default_case);
1777 if (!default_case)
1779 gimple new_default;
1781 default_case
1782 = build_case_label (NULL_TREE, NULL_TREE,
1783 create_artificial_label (UNKNOWN_LOCATION));
1784 new_default = gimple_build_label (CASE_LABEL (default_case));
1785 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1788 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1789 default_case, labels);
1790 gimplify_seq_add_stmt (pre_p, gimple_switch);
1791 gimplify_seq_add_seq (pre_p, switch_body_seq);
1792 labels.release ();
1794 else
1795 gcc_assert (SWITCH_LABELS (switch_expr));
1797 return GS_ALL_DONE;
1800 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1802 static enum gimplify_status
1803 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1805 struct gimplify_ctx *ctxp;
1806 gimple gimple_label;
1808 /* Invalid OpenMP programs can play Duff's Device type games with
1809 #pragma omp parallel. At least in the C front end, we don't
1810 detect such invalid branches until after gimplification. */
1811 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1812 if (ctxp->case_labels.exists ())
1813 break;
1815 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1816 ctxp->case_labels.safe_push (*expr_p);
1817 gimplify_seq_add_stmt (pre_p, gimple_label);
1819 return GS_ALL_DONE;
1822 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1823 if necessary. */
1825 tree
1826 build_and_jump (tree *label_p)
1828 if (label_p == NULL)
1829 /* If there's nowhere to jump, just fall through. */
1830 return NULL_TREE;
1832 if (*label_p == NULL_TREE)
1834 tree label = create_artificial_label (UNKNOWN_LOCATION);
1835 *label_p = label;
1838 return build1 (GOTO_EXPR, void_type_node, *label_p);
1841 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1842 This also involves building a label to jump to and communicating it to
1843 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1845 static enum gimplify_status
1846 gimplify_exit_expr (tree *expr_p)
1848 tree cond = TREE_OPERAND (*expr_p, 0);
1849 tree expr;
1851 expr = build_and_jump (&gimplify_ctxp->exit_label);
1852 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1853 *expr_p = expr;
1855 return GS_OK;
1858 /* A helper function to be called via walk_tree. Mark all labels under *TP
1859 as being forced. To be called for DECL_INITIAL of static variables. */
1861 tree
1862 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1864 if (TYPE_P (*tp))
1865 *walk_subtrees = 0;
1866 if (TREE_CODE (*tp) == LABEL_DECL)
1867 FORCED_LABEL (*tp) = 1;
1869 return NULL_TREE;
1872 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1873 different from its canonical type, wrap the whole thing inside a
1874 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1875 type.
1877 The canonical type of a COMPONENT_REF is the type of the field being
1878 referenced--unless the field is a bit-field which can be read directly
1879 in a smaller mode, in which case the canonical type is the
1880 sign-appropriate type corresponding to that mode. */
1882 static void
1883 canonicalize_component_ref (tree *expr_p)
1885 tree expr = *expr_p;
1886 tree type;
1888 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1890 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1891 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1892 else
1893 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1895 /* One could argue that all the stuff below is not necessary for
1896 the non-bitfield case and declare it a FE error if type
1897 adjustment would be needed. */
1898 if (TREE_TYPE (expr) != type)
1900 #ifdef ENABLE_TYPES_CHECKING
1901 tree old_type = TREE_TYPE (expr);
1902 #endif
1903 int type_quals;
1905 /* We need to preserve qualifiers and propagate them from
1906 operand 0. */
1907 type_quals = TYPE_QUALS (type)
1908 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1909 if (TYPE_QUALS (type) != type_quals)
1910 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1912 /* Set the type of the COMPONENT_REF to the underlying type. */
1913 TREE_TYPE (expr) = type;
1915 #ifdef ENABLE_TYPES_CHECKING
1916 /* It is now a FE error, if the conversion from the canonical
1917 type to the original expression type is not useless. */
1918 gcc_assert (useless_type_conversion_p (old_type, type));
1919 #endif
1923 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1924 to foo, embed that change in the ADDR_EXPR by converting
1925 T array[U];
1926 (T *)&array
1928 &array[L]
1929 where L is the lower bound. For simplicity, only do this for constant
1930 lower bound.
1931 The constraint is that the type of &array[L] is trivially convertible
1932 to T *. */
1934 static void
1935 canonicalize_addr_expr (tree *expr_p)
1937 tree expr = *expr_p;
1938 tree addr_expr = TREE_OPERAND (expr, 0);
1939 tree datype, ddatype, pddatype;
1941 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1942 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1943 || TREE_CODE (addr_expr) != ADDR_EXPR)
1944 return;
1946 /* The addr_expr type should be a pointer to an array. */
1947 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1948 if (TREE_CODE (datype) != ARRAY_TYPE)
1949 return;
1951 /* The pointer to element type shall be trivially convertible to
1952 the expression pointer type. */
1953 ddatype = TREE_TYPE (datype);
1954 pddatype = build_pointer_type (ddatype);
1955 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1956 pddatype))
1957 return;
1959 /* The lower bound and element sizes must be constant. */
1960 if (!TYPE_SIZE_UNIT (ddatype)
1961 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1962 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1963 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1964 return;
1966 /* All checks succeeded. Build a new node to merge the cast. */
1967 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1968 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1969 NULL_TREE, NULL_TREE);
1970 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1972 /* We can have stripped a required restrict qualifier above. */
1973 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1974 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1977 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1978 underneath as appropriate. */
1980 static enum gimplify_status
1981 gimplify_conversion (tree *expr_p)
1983 location_t loc = EXPR_LOCATION (*expr_p);
1984 gcc_assert (CONVERT_EXPR_P (*expr_p));
1986 /* Then strip away all but the outermost conversion. */
1987 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1989 /* And remove the outermost conversion if it's useless. */
1990 if (tree_ssa_useless_type_conversion (*expr_p))
1991 *expr_p = TREE_OPERAND (*expr_p, 0);
1993 /* If we still have a conversion at the toplevel,
1994 then canonicalize some constructs. */
1995 if (CONVERT_EXPR_P (*expr_p))
1997 tree sub = TREE_OPERAND (*expr_p, 0);
1999 /* If a NOP conversion is changing the type of a COMPONENT_REF
2000 expression, then canonicalize its type now in order to expose more
2001 redundant conversions. */
2002 if (TREE_CODE (sub) == COMPONENT_REF)
2003 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2005 /* If a NOP conversion is changing a pointer to array of foo
2006 to a pointer to foo, embed that change in the ADDR_EXPR. */
2007 else if (TREE_CODE (sub) == ADDR_EXPR)
2008 canonicalize_addr_expr (expr_p);
2011 /* If we have a conversion to a non-register type force the
2012 use of a VIEW_CONVERT_EXPR instead. */
2013 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2014 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2015 TREE_OPERAND (*expr_p, 0));
2017 return GS_OK;
2020 /* Nonlocal VLAs seen in the current function. */
2021 static struct pointer_set_t *nonlocal_vlas;
2023 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2024 DECL_VALUE_EXPR, and it's worth re-examining things. */
2026 static enum gimplify_status
2027 gimplify_var_or_parm_decl (tree *expr_p)
2029 tree decl = *expr_p;
2031 /* ??? If this is a local variable, and it has not been seen in any
2032 outer BIND_EXPR, then it's probably the result of a duplicate
2033 declaration, for which we've already issued an error. It would
2034 be really nice if the front end wouldn't leak these at all.
2035 Currently the only known culprit is C++ destructors, as seen
2036 in g++.old-deja/g++.jason/binding.C. */
2037 if (TREE_CODE (decl) == VAR_DECL
2038 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2039 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2040 && decl_function_context (decl) == current_function_decl)
2042 gcc_assert (seen_error ());
2043 return GS_ERROR;
2046 /* When within an OpenMP context, notice uses of variables. */
2047 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2048 return GS_ALL_DONE;
2050 /* If the decl is an alias for another expression, substitute it now. */
2051 if (DECL_HAS_VALUE_EXPR_P (decl))
2053 tree value_expr = DECL_VALUE_EXPR (decl);
2055 /* For referenced nonlocal VLAs add a decl for debugging purposes
2056 to the current function. */
2057 if (TREE_CODE (decl) == VAR_DECL
2058 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2059 && nonlocal_vlas != NULL
2060 && TREE_CODE (value_expr) == INDIRECT_REF
2061 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2062 && decl_function_context (decl) != current_function_decl)
2064 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2065 while (ctx && ctx->region_type == ORT_WORKSHARE)
2066 ctx = ctx->outer_context;
2067 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2069 tree copy = copy_node (decl), block;
2071 lang_hooks.dup_lang_specific_decl (copy);
2072 SET_DECL_RTL (copy, 0);
2073 TREE_USED (copy) = 1;
2074 block = DECL_INITIAL (current_function_decl);
2075 DECL_CHAIN (copy) = BLOCK_VARS (block);
2076 BLOCK_VARS (block) = copy;
2077 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2078 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2082 *expr_p = unshare_expr (value_expr);
2083 return GS_OK;
2086 return GS_ALL_DONE;
2089 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2090 node *EXPR_P.
2092 compound_lval
2093 : min_lval '[' val ']'
2094 | min_lval '.' ID
2095 | compound_lval '[' val ']'
2096 | compound_lval '.' ID
2098 This is not part of the original SIMPLE definition, which separates
2099 array and member references, but it seems reasonable to handle them
2100 together. Also, this way we don't run into problems with union
2101 aliasing; gcc requires that for accesses through a union to alias, the
2102 union reference must be explicit, which was not always the case when we
2103 were splitting up array and member refs.
2105 PRE_P points to the sequence where side effects that must happen before
2106 *EXPR_P should be stored.
2108 POST_P points to the sequence where side effects that must happen after
2109 *EXPR_P should be stored. */
2111 static enum gimplify_status
2112 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2113 fallback_t fallback)
2115 tree *p;
2116 vec<tree> expr_stack;
2117 enum gimplify_status ret = GS_ALL_DONE, tret;
2118 int i;
2119 location_t loc = EXPR_LOCATION (*expr_p);
2120 tree expr = *expr_p;
2122 /* Create a stack of the subexpressions so later we can walk them in
2123 order from inner to outer. */
2124 expr_stack.create (10);
2126 /* We can handle anything that get_inner_reference can deal with. */
2127 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2129 restart:
2130 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2131 if (TREE_CODE (*p) == INDIRECT_REF)
2132 *p = fold_indirect_ref_loc (loc, *p);
2134 if (handled_component_p (*p))
2136 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2137 additional COMPONENT_REFs. */
2138 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2139 && gimplify_var_or_parm_decl (p) == GS_OK)
2140 goto restart;
2141 else
2142 break;
2144 expr_stack.safe_push (*p);
2147 gcc_assert (expr_stack.length ());
2149 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2150 walked through and P points to the innermost expression.
2152 Java requires that we elaborated nodes in source order. That
2153 means we must gimplify the inner expression followed by each of
2154 the indices, in order. But we can't gimplify the inner
2155 expression until we deal with any variable bounds, sizes, or
2156 positions in order to deal with PLACEHOLDER_EXPRs.
2158 So we do this in three steps. First we deal with the annotations
2159 for any variables in the components, then we gimplify the base,
2160 then we gimplify any indices, from left to right. */
2161 for (i = expr_stack.length () - 1; i >= 0; i--)
2163 tree t = expr_stack[i];
2165 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2167 /* Gimplify the low bound and element type size and put them into
2168 the ARRAY_REF. If these values are set, they have already been
2169 gimplified. */
2170 if (TREE_OPERAND (t, 2) == NULL_TREE)
2172 tree low = unshare_expr (array_ref_low_bound (t));
2173 if (!is_gimple_min_invariant (low))
2175 TREE_OPERAND (t, 2) = low;
2176 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2177 post_p, is_gimple_reg,
2178 fb_rvalue);
2179 ret = MIN (ret, tret);
2182 else
2184 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2185 is_gimple_reg, fb_rvalue);
2186 ret = MIN (ret, tret);
2189 if (TREE_OPERAND (t, 3) == NULL_TREE)
2191 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2192 tree elmt_size = unshare_expr (array_ref_element_size (t));
2193 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2195 /* Divide the element size by the alignment of the element
2196 type (above). */
2197 elmt_size
2198 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2200 if (!is_gimple_min_invariant (elmt_size))
2202 TREE_OPERAND (t, 3) = elmt_size;
2203 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2204 post_p, is_gimple_reg,
2205 fb_rvalue);
2206 ret = MIN (ret, tret);
2209 else
2211 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2212 is_gimple_reg, fb_rvalue);
2213 ret = MIN (ret, tret);
2216 else if (TREE_CODE (t) == COMPONENT_REF)
2218 /* Set the field offset into T and gimplify it. */
2219 if (TREE_OPERAND (t, 2) == NULL_TREE)
2221 tree offset = unshare_expr (component_ref_field_offset (t));
2222 tree field = TREE_OPERAND (t, 1);
2223 tree factor
2224 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2226 /* Divide the offset by its alignment. */
2227 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2229 if (!is_gimple_min_invariant (offset))
2231 TREE_OPERAND (t, 2) = offset;
2232 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2233 post_p, is_gimple_reg,
2234 fb_rvalue);
2235 ret = MIN (ret, tret);
2238 else
2240 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2241 is_gimple_reg, fb_rvalue);
2242 ret = MIN (ret, tret);
2247 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2248 so as to match the min_lval predicate. Failure to do so may result
2249 in the creation of large aggregate temporaries. */
2250 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2251 fallback | fb_lvalue);
2252 ret = MIN (ret, tret);
2254 /* And finally, the indices and operands of ARRAY_REF. During this
2255 loop we also remove any useless conversions. */
2256 for (; expr_stack.length () > 0; )
2258 tree t = expr_stack.pop ();
2260 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2262 /* Gimplify the dimension. */
2263 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2265 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2266 is_gimple_val, fb_rvalue);
2267 ret = MIN (ret, tret);
2271 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2273 /* The innermost expression P may have originally had
2274 TREE_SIDE_EFFECTS set which would have caused all the outer
2275 expressions in *EXPR_P leading to P to also have had
2276 TREE_SIDE_EFFECTS set. */
2277 recalculate_side_effects (t);
2280 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2281 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2283 canonicalize_component_ref (expr_p);
2286 expr_stack.release ();
2288 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2290 return ret;
2293 /* Gimplify the self modifying expression pointed to by EXPR_P
2294 (++, --, +=, -=).
2296 PRE_P points to the list where side effects that must happen before
2297 *EXPR_P should be stored.
2299 POST_P points to the list where side effects that must happen after
2300 *EXPR_P should be stored.
2302 WANT_VALUE is nonzero iff we want to use the value of this expression
2303 in another expression.
2305 ARITH_TYPE is the type the computation should be performed in. */
2307 enum gimplify_status
2308 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2309 bool want_value, tree arith_type)
2311 enum tree_code code;
2312 tree lhs, lvalue, rhs, t1;
2313 gimple_seq post = NULL, *orig_post_p = post_p;
2314 bool postfix;
2315 enum tree_code arith_code;
2316 enum gimplify_status ret;
2317 location_t loc = EXPR_LOCATION (*expr_p);
2319 code = TREE_CODE (*expr_p);
2321 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2322 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2324 /* Prefix or postfix? */
2325 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2326 /* Faster to treat as prefix if result is not used. */
2327 postfix = want_value;
2328 else
2329 postfix = false;
2331 /* For postfix, make sure the inner expression's post side effects
2332 are executed after side effects from this expression. */
2333 if (postfix)
2334 post_p = &post;
2336 /* Add or subtract? */
2337 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2338 arith_code = PLUS_EXPR;
2339 else
2340 arith_code = MINUS_EXPR;
2342 /* Gimplify the LHS into a GIMPLE lvalue. */
2343 lvalue = TREE_OPERAND (*expr_p, 0);
2344 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2345 if (ret == GS_ERROR)
2346 return ret;
2348 /* Extract the operands to the arithmetic operation. */
2349 lhs = lvalue;
2350 rhs = TREE_OPERAND (*expr_p, 1);
2352 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2353 that as the result value and in the postqueue operation. */
2354 if (postfix)
2356 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2357 if (ret == GS_ERROR)
2358 return ret;
2360 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2363 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2364 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2366 rhs = convert_to_ptrofftype_loc (loc, rhs);
2367 if (arith_code == MINUS_EXPR)
2368 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2369 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2371 else
2372 t1 = fold_convert (TREE_TYPE (*expr_p),
2373 fold_build2 (arith_code, arith_type,
2374 fold_convert (arith_type, lhs),
2375 fold_convert (arith_type, rhs)));
2377 if (postfix)
2379 gimplify_assign (lvalue, t1, pre_p);
2380 gimplify_seq_add_seq (orig_post_p, post);
2381 *expr_p = lhs;
2382 return GS_ALL_DONE;
2384 else
2386 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2387 return GS_OK;
2391 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2393 static void
2394 maybe_with_size_expr (tree *expr_p)
2396 tree expr = *expr_p;
2397 tree type = TREE_TYPE (expr);
2398 tree size;
2400 /* If we've already wrapped this or the type is error_mark_node, we can't do
2401 anything. */
2402 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2403 || type == error_mark_node)
2404 return;
2406 /* If the size isn't known or is a constant, we have nothing to do. */
2407 size = TYPE_SIZE_UNIT (type);
2408 if (!size || TREE_CODE (size) == INTEGER_CST)
2409 return;
2411 /* Otherwise, make a WITH_SIZE_EXPR. */
2412 size = unshare_expr (size);
2413 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2414 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2417 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2418 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2419 the CALL_EXPR. */
2421 static enum gimplify_status
2422 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2424 bool (*test) (tree);
2425 fallback_t fb;
2427 /* In general, we allow lvalues for function arguments to avoid
2428 extra overhead of copying large aggregates out of even larger
2429 aggregates into temporaries only to copy the temporaries to
2430 the argument list. Make optimizers happy by pulling out to
2431 temporaries those types that fit in registers. */
2432 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2433 test = is_gimple_val, fb = fb_rvalue;
2434 else
2436 test = is_gimple_lvalue, fb = fb_either;
2437 /* Also strip a TARGET_EXPR that would force an extra copy. */
2438 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2440 tree init = TARGET_EXPR_INITIAL (*arg_p);
2441 if (init
2442 && !VOID_TYPE_P (TREE_TYPE (init)))
2443 *arg_p = init;
2447 /* If this is a variable sized type, we must remember the size. */
2448 maybe_with_size_expr (arg_p);
2450 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2451 /* Make sure arguments have the same location as the function call
2452 itself. */
2453 protected_set_expr_location (*arg_p, call_location);
2455 /* There is a sequence point before a function call. Side effects in
2456 the argument list must occur before the actual call. So, when
2457 gimplifying arguments, force gimplify_expr to use an internal
2458 post queue which is then appended to the end of PRE_P. */
2459 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2462 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2463 WANT_VALUE is true if the result of the call is desired. */
2465 static enum gimplify_status
2466 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2468 tree fndecl, parms, p, fnptrtype;
2469 enum gimplify_status ret;
2470 int i, nargs;
2471 gimple call;
2472 bool builtin_va_start_p = FALSE;
2473 location_t loc = EXPR_LOCATION (*expr_p);
2475 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2477 /* For reliable diagnostics during inlining, it is necessary that
2478 every call_expr be annotated with file and line. */
2479 if (! EXPR_HAS_LOCATION (*expr_p))
2480 SET_EXPR_LOCATION (*expr_p, input_location);
2482 /* This may be a call to a builtin function.
2484 Builtin function calls may be transformed into different
2485 (and more efficient) builtin function calls under certain
2486 circumstances. Unfortunately, gimplification can muck things
2487 up enough that the builtin expanders are not aware that certain
2488 transformations are still valid.
2490 So we attempt transformation/gimplification of the call before
2491 we gimplify the CALL_EXPR. At this time we do not manage to
2492 transform all calls in the same manner as the expanders do, but
2493 we do transform most of them. */
2494 fndecl = get_callee_fndecl (*expr_p);
2495 if (fndecl
2496 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2497 switch (DECL_FUNCTION_CODE (fndecl))
2499 case BUILT_IN_VA_START:
2501 builtin_va_start_p = TRUE;
2502 if (call_expr_nargs (*expr_p) < 2)
2504 error ("too few arguments to function %<va_start%>");
2505 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2506 return GS_OK;
2509 if (fold_builtin_next_arg (*expr_p, true))
2511 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2512 return GS_OK;
2514 break;
2516 case BUILT_IN_LINE:
2518 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2519 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2520 return GS_OK;
2522 case BUILT_IN_FILE:
2524 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2525 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2526 return GS_OK;
2528 case BUILT_IN_FUNCTION:
2530 const char *function;
2531 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2532 *expr_p = build_string_literal (strlen (function) + 1, function);
2533 return GS_OK;
2535 default:
2538 if (fndecl && DECL_BUILT_IN (fndecl))
2540 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2541 if (new_tree && new_tree != *expr_p)
2543 /* There was a transformation of this call which computes the
2544 same value, but in a more efficient way. Return and try
2545 again. */
2546 *expr_p = new_tree;
2547 return GS_OK;
2551 /* Remember the original function pointer type. */
2552 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2554 /* There is a sequence point before the call, so any side effects in
2555 the calling expression must occur before the actual call. Force
2556 gimplify_expr to use an internal post queue. */
2557 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2558 is_gimple_call_addr, fb_rvalue);
2560 nargs = call_expr_nargs (*expr_p);
2562 /* Get argument types for verification. */
2563 fndecl = get_callee_fndecl (*expr_p);
2564 parms = NULL_TREE;
2565 if (fndecl)
2566 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2567 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2568 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2570 if (fndecl && DECL_ARGUMENTS (fndecl))
2571 p = DECL_ARGUMENTS (fndecl);
2572 else if (parms)
2573 p = parms;
2574 else
2575 p = NULL_TREE;
2576 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2579 /* If the last argument is __builtin_va_arg_pack () and it is not
2580 passed as a named argument, decrease the number of CALL_EXPR
2581 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2582 if (!p
2583 && i < nargs
2584 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2586 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2587 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2589 if (last_arg_fndecl
2590 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2591 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2592 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2594 tree call = *expr_p;
2596 --nargs;
2597 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2598 CALL_EXPR_FN (call),
2599 nargs, CALL_EXPR_ARGP (call));
2601 /* Copy all CALL_EXPR flags, location and block, except
2602 CALL_EXPR_VA_ARG_PACK flag. */
2603 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2604 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2605 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2606 = CALL_EXPR_RETURN_SLOT_OPT (call);
2607 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2608 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2610 /* Set CALL_EXPR_VA_ARG_PACK. */
2611 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2615 /* Finally, gimplify the function arguments. */
2616 if (nargs > 0)
2618 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2619 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2620 PUSH_ARGS_REVERSED ? i-- : i++)
2622 enum gimplify_status t;
2624 /* Avoid gimplifying the second argument to va_start, which needs to
2625 be the plain PARM_DECL. */
2626 if ((i != 1) || !builtin_va_start_p)
2628 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2629 EXPR_LOCATION (*expr_p));
2631 if (t == GS_ERROR)
2632 ret = GS_ERROR;
2637 /* Verify the function result. */
2638 if (want_value && fndecl
2639 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2641 error_at (loc, "using result of function returning %<void%>");
2642 ret = GS_ERROR;
2645 /* Try this again in case gimplification exposed something. */
2646 if (ret != GS_ERROR)
2648 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2650 if (new_tree && new_tree != *expr_p)
2652 /* There was a transformation of this call which computes the
2653 same value, but in a more efficient way. Return and try
2654 again. */
2655 *expr_p = new_tree;
2656 return GS_OK;
2659 else
2661 *expr_p = error_mark_node;
2662 return GS_ERROR;
2665 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2666 decl. This allows us to eliminate redundant or useless
2667 calls to "const" functions. */
2668 if (TREE_CODE (*expr_p) == CALL_EXPR)
2670 int flags = call_expr_flags (*expr_p);
2671 if (flags & (ECF_CONST | ECF_PURE)
2672 /* An infinite loop is considered a side effect. */
2673 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2674 TREE_SIDE_EFFECTS (*expr_p) = 0;
2677 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2678 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2679 form and delegate the creation of a GIMPLE_CALL to
2680 gimplify_modify_expr. This is always possible because when
2681 WANT_VALUE is true, the caller wants the result of this call into
2682 a temporary, which means that we will emit an INIT_EXPR in
2683 internal_get_tmp_var which will then be handled by
2684 gimplify_modify_expr. */
2685 if (!want_value)
2687 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2688 have to do is replicate it as a GIMPLE_CALL tuple. */
2689 gimple_stmt_iterator gsi;
2690 call = gimple_build_call_from_tree (*expr_p);
2691 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2692 notice_special_calls (call);
2693 gimplify_seq_add_stmt (pre_p, call);
2694 gsi = gsi_last (*pre_p);
2695 fold_stmt (&gsi);
2696 *expr_p = NULL_TREE;
2698 else
2699 /* Remember the original function type. */
2700 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2701 CALL_EXPR_FN (*expr_p));
2703 return ret;
2706 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2707 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2709 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2710 condition is true or false, respectively. If null, we should generate
2711 our own to skip over the evaluation of this specific expression.
2713 LOCUS is the source location of the COND_EXPR.
2715 This function is the tree equivalent of do_jump.
2717 shortcut_cond_r should only be called by shortcut_cond_expr. */
2719 static tree
2720 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2721 location_t locus)
2723 tree local_label = NULL_TREE;
2724 tree t, expr = NULL;
2726 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2727 retain the shortcut semantics. Just insert the gotos here;
2728 shortcut_cond_expr will append the real blocks later. */
2729 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2731 location_t new_locus;
2733 /* Turn if (a && b) into
2735 if (a); else goto no;
2736 if (b) goto yes; else goto no;
2737 (no:) */
2739 if (false_label_p == NULL)
2740 false_label_p = &local_label;
2742 /* Keep the original source location on the first 'if'. */
2743 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2744 append_to_statement_list (t, &expr);
2746 /* Set the source location of the && on the second 'if'. */
2747 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2748 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2749 new_locus);
2750 append_to_statement_list (t, &expr);
2752 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2754 location_t new_locus;
2756 /* Turn if (a || b) into
2758 if (a) goto yes;
2759 if (b) goto yes; else goto no;
2760 (yes:) */
2762 if (true_label_p == NULL)
2763 true_label_p = &local_label;
2765 /* Keep the original source location on the first 'if'. */
2766 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2767 append_to_statement_list (t, &expr);
2769 /* Set the source location of the || on the second 'if'. */
2770 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2771 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2772 new_locus);
2773 append_to_statement_list (t, &expr);
2775 else if (TREE_CODE (pred) == COND_EXPR
2776 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2777 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2779 location_t new_locus;
2781 /* As long as we're messing with gotos, turn if (a ? b : c) into
2782 if (a)
2783 if (b) goto yes; else goto no;
2784 else
2785 if (c) goto yes; else goto no;
2787 Don't do this if one of the arms has void type, which can happen
2788 in C++ when the arm is throw. */
2790 /* Keep the original source location on the first 'if'. Set the source
2791 location of the ? on the second 'if'. */
2792 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2793 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2794 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2795 false_label_p, locus),
2796 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2797 false_label_p, new_locus));
2799 else
2801 expr = build3 (COND_EXPR, void_type_node, pred,
2802 build_and_jump (true_label_p),
2803 build_and_jump (false_label_p));
2804 SET_EXPR_LOCATION (expr, locus);
2807 if (local_label)
2809 t = build1 (LABEL_EXPR, void_type_node, local_label);
2810 append_to_statement_list (t, &expr);
2813 return expr;
2816 /* Given a conditional expression EXPR with short-circuit boolean
2817 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2818 predicate apart into the equivalent sequence of conditionals. */
2820 static tree
2821 shortcut_cond_expr (tree expr)
2823 tree pred = TREE_OPERAND (expr, 0);
2824 tree then_ = TREE_OPERAND (expr, 1);
2825 tree else_ = TREE_OPERAND (expr, 2);
2826 tree true_label, false_label, end_label, t;
2827 tree *true_label_p;
2828 tree *false_label_p;
2829 bool emit_end, emit_false, jump_over_else;
2830 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2831 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2833 /* First do simple transformations. */
2834 if (!else_se)
2836 /* If there is no 'else', turn
2837 if (a && b) then c
2838 into
2839 if (a) if (b) then c. */
2840 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2842 /* Keep the original source location on the first 'if'. */
2843 location_t locus = EXPR_LOC_OR_HERE (expr);
2844 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2845 /* Set the source location of the && on the second 'if'. */
2846 if (EXPR_HAS_LOCATION (pred))
2847 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2848 then_ = shortcut_cond_expr (expr);
2849 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2850 pred = TREE_OPERAND (pred, 0);
2851 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2852 SET_EXPR_LOCATION (expr, locus);
2856 if (!then_se)
2858 /* If there is no 'then', turn
2859 if (a || b); else d
2860 into
2861 if (a); else if (b); else d. */
2862 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2864 /* Keep the original source location on the first 'if'. */
2865 location_t locus = EXPR_LOC_OR_HERE (expr);
2866 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2867 /* Set the source location of the || on the second 'if'. */
2868 if (EXPR_HAS_LOCATION (pred))
2869 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2870 else_ = shortcut_cond_expr (expr);
2871 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2872 pred = TREE_OPERAND (pred, 0);
2873 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2874 SET_EXPR_LOCATION (expr, locus);
2878 /* If we're done, great. */
2879 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2880 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2881 return expr;
2883 /* Otherwise we need to mess with gotos. Change
2884 if (a) c; else d;
2886 if (a); else goto no;
2887 c; goto end;
2888 no: d; end:
2889 and recursively gimplify the condition. */
2891 true_label = false_label = end_label = NULL_TREE;
2893 /* If our arms just jump somewhere, hijack those labels so we don't
2894 generate jumps to jumps. */
2896 if (then_
2897 && TREE_CODE (then_) == GOTO_EXPR
2898 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2900 true_label = GOTO_DESTINATION (then_);
2901 then_ = NULL;
2902 then_se = false;
2905 if (else_
2906 && TREE_CODE (else_) == GOTO_EXPR
2907 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2909 false_label = GOTO_DESTINATION (else_);
2910 else_ = NULL;
2911 else_se = false;
2914 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2915 if (true_label)
2916 true_label_p = &true_label;
2917 else
2918 true_label_p = NULL;
2920 /* The 'else' branch also needs a label if it contains interesting code. */
2921 if (false_label || else_se)
2922 false_label_p = &false_label;
2923 else
2924 false_label_p = NULL;
2926 /* If there was nothing else in our arms, just forward the label(s). */
2927 if (!then_se && !else_se)
2928 return shortcut_cond_r (pred, true_label_p, false_label_p,
2929 EXPR_LOC_OR_HERE (expr));
2931 /* If our last subexpression already has a terminal label, reuse it. */
2932 if (else_se)
2933 t = expr_last (else_);
2934 else if (then_se)
2935 t = expr_last (then_);
2936 else
2937 t = NULL;
2938 if (t && TREE_CODE (t) == LABEL_EXPR)
2939 end_label = LABEL_EXPR_LABEL (t);
2941 /* If we don't care about jumping to the 'else' branch, jump to the end
2942 if the condition is false. */
2943 if (!false_label_p)
2944 false_label_p = &end_label;
2946 /* We only want to emit these labels if we aren't hijacking them. */
2947 emit_end = (end_label == NULL_TREE);
2948 emit_false = (false_label == NULL_TREE);
2950 /* We only emit the jump over the else clause if we have to--if the
2951 then clause may fall through. Otherwise we can wind up with a
2952 useless jump and a useless label at the end of gimplified code,
2953 which will cause us to think that this conditional as a whole
2954 falls through even if it doesn't. If we then inline a function
2955 which ends with such a condition, that can cause us to issue an
2956 inappropriate warning about control reaching the end of a
2957 non-void function. */
2958 jump_over_else = block_may_fallthru (then_);
2960 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2961 EXPR_LOC_OR_HERE (expr));
2963 expr = NULL;
2964 append_to_statement_list (pred, &expr);
2966 append_to_statement_list (then_, &expr);
2967 if (else_se)
2969 if (jump_over_else)
2971 tree last = expr_last (expr);
2972 t = build_and_jump (&end_label);
2973 if (EXPR_HAS_LOCATION (last))
2974 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2975 append_to_statement_list (t, &expr);
2977 if (emit_false)
2979 t = build1 (LABEL_EXPR, void_type_node, false_label);
2980 append_to_statement_list (t, &expr);
2982 append_to_statement_list (else_, &expr);
2984 if (emit_end && end_label)
2986 t = build1 (LABEL_EXPR, void_type_node, end_label);
2987 append_to_statement_list (t, &expr);
2990 return expr;
2993 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2995 tree
2996 gimple_boolify (tree expr)
2998 tree type = TREE_TYPE (expr);
2999 location_t loc = EXPR_LOCATION (expr);
3001 if (TREE_CODE (expr) == NE_EXPR
3002 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3003 && integer_zerop (TREE_OPERAND (expr, 1)))
3005 tree call = TREE_OPERAND (expr, 0);
3006 tree fn = get_callee_fndecl (call);
3008 /* For __builtin_expect ((long) (x), y) recurse into x as well
3009 if x is truth_value_p. */
3010 if (fn
3011 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3012 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3013 && call_expr_nargs (call) == 2)
3015 tree arg = CALL_EXPR_ARG (call, 0);
3016 if (arg)
3018 if (TREE_CODE (arg) == NOP_EXPR
3019 && TREE_TYPE (arg) == TREE_TYPE (call))
3020 arg = TREE_OPERAND (arg, 0);
3021 if (truth_value_p (TREE_CODE (arg)))
3023 arg = gimple_boolify (arg);
3024 CALL_EXPR_ARG (call, 0)
3025 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3031 switch (TREE_CODE (expr))
3033 case TRUTH_AND_EXPR:
3034 case TRUTH_OR_EXPR:
3035 case TRUTH_XOR_EXPR:
3036 case TRUTH_ANDIF_EXPR:
3037 case TRUTH_ORIF_EXPR:
3038 /* Also boolify the arguments of truth exprs. */
3039 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3040 /* FALLTHRU */
3042 case TRUTH_NOT_EXPR:
3043 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3045 /* These expressions always produce boolean results. */
3046 if (TREE_CODE (type) != BOOLEAN_TYPE)
3047 TREE_TYPE (expr) = boolean_type_node;
3048 return expr;
3050 default:
3051 if (COMPARISON_CLASS_P (expr))
3053 /* There expressions always prduce boolean results. */
3054 if (TREE_CODE (type) != BOOLEAN_TYPE)
3055 TREE_TYPE (expr) = boolean_type_node;
3056 return expr;
3058 /* Other expressions that get here must have boolean values, but
3059 might need to be converted to the appropriate mode. */
3060 if (TREE_CODE (type) == BOOLEAN_TYPE)
3061 return expr;
3062 return fold_convert_loc (loc, boolean_type_node, expr);
3066 /* Given a conditional expression *EXPR_P without side effects, gimplify
3067 its operands. New statements are inserted to PRE_P. */
3069 static enum gimplify_status
3070 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3072 tree expr = *expr_p, cond;
3073 enum gimplify_status ret, tret;
3074 enum tree_code code;
3076 cond = gimple_boolify (COND_EXPR_COND (expr));
3078 /* We need to handle && and || specially, as their gimplification
3079 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3080 code = TREE_CODE (cond);
3081 if (code == TRUTH_ANDIF_EXPR)
3082 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3083 else if (code == TRUTH_ORIF_EXPR)
3084 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3085 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3086 COND_EXPR_COND (*expr_p) = cond;
3088 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3089 is_gimple_val, fb_rvalue);
3090 ret = MIN (ret, tret);
3091 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3092 is_gimple_val, fb_rvalue);
3094 return MIN (ret, tret);
3097 /* Return true if evaluating EXPR could trap.
3098 EXPR is GENERIC, while tree_could_trap_p can be called
3099 only on GIMPLE. */
3101 static bool
3102 generic_expr_could_trap_p (tree expr)
3104 unsigned i, n;
3106 if (!expr || is_gimple_val (expr))
3107 return false;
3109 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3110 return true;
3112 n = TREE_OPERAND_LENGTH (expr);
3113 for (i = 0; i < n; i++)
3114 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3115 return true;
3117 return false;
3120 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3121 into
3123 if (p) if (p)
3124 t1 = a; a;
3125 else or else
3126 t1 = b; b;
3129 The second form is used when *EXPR_P is of type void.
3131 PRE_P points to the list where side effects that must happen before
3132 *EXPR_P should be stored. */
3134 static enum gimplify_status
3135 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3137 tree expr = *expr_p;
3138 tree type = TREE_TYPE (expr);
3139 location_t loc = EXPR_LOCATION (expr);
3140 tree tmp, arm1, arm2;
3141 enum gimplify_status ret;
3142 tree label_true, label_false, label_cont;
3143 bool have_then_clause_p, have_else_clause_p;
3144 gimple gimple_cond;
3145 enum tree_code pred_code;
3146 gimple_seq seq = NULL;
3148 /* If this COND_EXPR has a value, copy the values into a temporary within
3149 the arms. */
3150 if (!VOID_TYPE_P (type))
3152 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3153 tree result;
3155 /* If either an rvalue is ok or we do not require an lvalue, create the
3156 temporary. But we cannot do that if the type is addressable. */
3157 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3158 && !TREE_ADDRESSABLE (type))
3160 if (gimplify_ctxp->allow_rhs_cond_expr
3161 /* If either branch has side effects or could trap, it can't be
3162 evaluated unconditionally. */
3163 && !TREE_SIDE_EFFECTS (then_)
3164 && !generic_expr_could_trap_p (then_)
3165 && !TREE_SIDE_EFFECTS (else_)
3166 && !generic_expr_could_trap_p (else_))
3167 return gimplify_pure_cond_expr (expr_p, pre_p);
3169 tmp = create_tmp_var (type, "iftmp");
3170 result = tmp;
3173 /* Otherwise, only create and copy references to the values. */
3174 else
3176 type = build_pointer_type (type);
3178 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3179 then_ = build_fold_addr_expr_loc (loc, then_);
3181 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3182 else_ = build_fold_addr_expr_loc (loc, else_);
3184 expr
3185 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3187 tmp = create_tmp_var (type, "iftmp");
3188 result = build_simple_mem_ref_loc (loc, tmp);
3191 /* Build the new then clause, `tmp = then_;'. But don't build the
3192 assignment if the value is void; in C++ it can be if it's a throw. */
3193 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3194 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3196 /* Similarly, build the new else clause, `tmp = else_;'. */
3197 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3198 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3200 TREE_TYPE (expr) = void_type_node;
3201 recalculate_side_effects (expr);
3203 /* Move the COND_EXPR to the prequeue. */
3204 gimplify_stmt (&expr, pre_p);
3206 *expr_p = result;
3207 return GS_ALL_DONE;
3210 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3211 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3212 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3213 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3215 /* Make sure the condition has BOOLEAN_TYPE. */
3216 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3218 /* Break apart && and || conditions. */
3219 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3220 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3222 expr = shortcut_cond_expr (expr);
3224 if (expr != *expr_p)
3226 *expr_p = expr;
3228 /* We can't rely on gimplify_expr to re-gimplify the expanded
3229 form properly, as cleanups might cause the target labels to be
3230 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3231 set up a conditional context. */
3232 gimple_push_condition ();
3233 gimplify_stmt (expr_p, &seq);
3234 gimple_pop_condition (pre_p);
3235 gimple_seq_add_seq (pre_p, seq);
3237 return GS_ALL_DONE;
3241 /* Now do the normal gimplification. */
3243 /* Gimplify condition. */
3244 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3245 fb_rvalue);
3246 if (ret == GS_ERROR)
3247 return GS_ERROR;
3248 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3250 gimple_push_condition ();
3252 have_then_clause_p = have_else_clause_p = false;
3253 if (TREE_OPERAND (expr, 1) != NULL
3254 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3255 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3256 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3257 == current_function_decl)
3258 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3259 have different locations, otherwise we end up with incorrect
3260 location information on the branches. */
3261 && (optimize
3262 || !EXPR_HAS_LOCATION (expr)
3263 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3264 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3266 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3267 have_then_clause_p = true;
3269 else
3270 label_true = create_artificial_label (UNKNOWN_LOCATION);
3271 if (TREE_OPERAND (expr, 2) != NULL
3272 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3273 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3274 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3275 == current_function_decl)
3276 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3277 have different locations, otherwise we end up with incorrect
3278 location information on the branches. */
3279 && (optimize
3280 || !EXPR_HAS_LOCATION (expr)
3281 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3282 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3284 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3285 have_else_clause_p = true;
3287 else
3288 label_false = create_artificial_label (UNKNOWN_LOCATION);
3290 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3291 &arm2);
3293 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3294 label_false);
3296 gimplify_seq_add_stmt (&seq, gimple_cond);
3297 label_cont = NULL_TREE;
3298 if (!have_then_clause_p)
3300 /* For if (...) {} else { code; } put label_true after
3301 the else block. */
3302 if (TREE_OPERAND (expr, 1) == NULL_TREE
3303 && !have_else_clause_p
3304 && TREE_OPERAND (expr, 2) != NULL_TREE)
3305 label_cont = label_true;
3306 else
3308 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3309 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3310 /* For if (...) { code; } else {} or
3311 if (...) { code; } else goto label; or
3312 if (...) { code; return; } else { ... }
3313 label_cont isn't needed. */
3314 if (!have_else_clause_p
3315 && TREE_OPERAND (expr, 2) != NULL_TREE
3316 && gimple_seq_may_fallthru (seq))
3318 gimple g;
3319 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3321 g = gimple_build_goto (label_cont);
3323 /* GIMPLE_COND's are very low level; they have embedded
3324 gotos. This particular embedded goto should not be marked
3325 with the location of the original COND_EXPR, as it would
3326 correspond to the COND_EXPR's condition, not the ELSE or the
3327 THEN arms. To avoid marking it with the wrong location, flag
3328 it as "no location". */
3329 gimple_set_do_not_emit_location (g);
3331 gimplify_seq_add_stmt (&seq, g);
3335 if (!have_else_clause_p)
3337 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3338 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3340 if (label_cont)
3341 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3343 gimple_pop_condition (pre_p);
3344 gimple_seq_add_seq (pre_p, seq);
3346 if (ret == GS_ERROR)
3347 ; /* Do nothing. */
3348 else if (have_then_clause_p || have_else_clause_p)
3349 ret = GS_ALL_DONE;
3350 else
3352 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3353 expr = TREE_OPERAND (expr, 0);
3354 gimplify_stmt (&expr, pre_p);
3357 *expr_p = NULL;
3358 return ret;
3361 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3362 to be marked addressable.
3364 We cannot rely on such an expression being directly markable if a temporary
3365 has been created by the gimplification. In this case, we create another
3366 temporary and initialize it with a copy, which will become a store after we
3367 mark it addressable. This can happen if the front-end passed us something
3368 that it could not mark addressable yet, like a Fortran pass-by-reference
3369 parameter (int) floatvar. */
3371 static void
3372 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3374 while (handled_component_p (*expr_p))
3375 expr_p = &TREE_OPERAND (*expr_p, 0);
3376 if (is_gimple_reg (*expr_p))
3377 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3380 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3381 a call to __builtin_memcpy. */
3383 static enum gimplify_status
3384 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3385 gimple_seq *seq_p)
3387 tree t, to, to_ptr, from, from_ptr;
3388 gimple gs;
3389 location_t loc = EXPR_LOCATION (*expr_p);
3391 to = TREE_OPERAND (*expr_p, 0);
3392 from = TREE_OPERAND (*expr_p, 1);
3394 /* Mark the RHS addressable. Beware that it may not be possible to do so
3395 directly if a temporary has been created by the gimplification. */
3396 prepare_gimple_addressable (&from, seq_p);
3398 mark_addressable (from);
3399 from_ptr = build_fold_addr_expr_loc (loc, from);
3400 gimplify_arg (&from_ptr, seq_p, loc);
3402 mark_addressable (to);
3403 to_ptr = build_fold_addr_expr_loc (loc, to);
3404 gimplify_arg (&to_ptr, seq_p, loc);
3406 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3408 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3410 if (want_value)
3412 /* tmp = memcpy() */
3413 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3414 gimple_call_set_lhs (gs, t);
3415 gimplify_seq_add_stmt (seq_p, gs);
3417 *expr_p = build_simple_mem_ref (t);
3418 return GS_ALL_DONE;
3421 gimplify_seq_add_stmt (seq_p, gs);
3422 *expr_p = NULL;
3423 return GS_ALL_DONE;
3426 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3427 a call to __builtin_memset. In this case we know that the RHS is
3428 a CONSTRUCTOR with an empty element list. */
3430 static enum gimplify_status
3431 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3432 gimple_seq *seq_p)
3434 tree t, from, to, to_ptr;
3435 gimple gs;
3436 location_t loc = EXPR_LOCATION (*expr_p);
3438 /* Assert our assumptions, to abort instead of producing wrong code
3439 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3440 not be immediately exposed. */
3441 from = TREE_OPERAND (*expr_p, 1);
3442 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3443 from = TREE_OPERAND (from, 0);
3445 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3446 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3448 /* Now proceed. */
3449 to = TREE_OPERAND (*expr_p, 0);
3451 to_ptr = build_fold_addr_expr_loc (loc, to);
3452 gimplify_arg (&to_ptr, seq_p, loc);
3453 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3455 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3457 if (want_value)
3459 /* tmp = memset() */
3460 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3461 gimple_call_set_lhs (gs, t);
3462 gimplify_seq_add_stmt (seq_p, gs);
3464 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3465 return GS_ALL_DONE;
3468 gimplify_seq_add_stmt (seq_p, gs);
3469 *expr_p = NULL;
3470 return GS_ALL_DONE;
3473 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3474 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3475 assignment. Return non-null if we detect a potential overlap. */
3477 struct gimplify_init_ctor_preeval_data
3479 /* The base decl of the lhs object. May be NULL, in which case we
3480 have to assume the lhs is indirect. */
3481 tree lhs_base_decl;
3483 /* The alias set of the lhs object. */
3484 alias_set_type lhs_alias_set;
3487 static tree
3488 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3490 struct gimplify_init_ctor_preeval_data *data
3491 = (struct gimplify_init_ctor_preeval_data *) xdata;
3492 tree t = *tp;
3494 /* If we find the base object, obviously we have overlap. */
3495 if (data->lhs_base_decl == t)
3496 return t;
3498 /* If the constructor component is indirect, determine if we have a
3499 potential overlap with the lhs. The only bits of information we
3500 have to go on at this point are addressability and alias sets. */
3501 if ((INDIRECT_REF_P (t)
3502 || TREE_CODE (t) == MEM_REF)
3503 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3504 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3505 return t;
3507 /* If the constructor component is a call, determine if it can hide a
3508 potential overlap with the lhs through an INDIRECT_REF like above.
3509 ??? Ugh - this is completely broken. In fact this whole analysis
3510 doesn't look conservative. */
3511 if (TREE_CODE (t) == CALL_EXPR)
3513 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3515 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3516 if (POINTER_TYPE_P (TREE_VALUE (type))
3517 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3518 && alias_sets_conflict_p (data->lhs_alias_set,
3519 get_alias_set
3520 (TREE_TYPE (TREE_VALUE (type)))))
3521 return t;
3524 if (IS_TYPE_OR_DECL_P (t))
3525 *walk_subtrees = 0;
3526 return NULL;
3529 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3530 force values that overlap with the lhs (as described by *DATA)
3531 into temporaries. */
3533 static void
3534 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3535 struct gimplify_init_ctor_preeval_data *data)
3537 enum gimplify_status one;
3539 /* If the value is constant, then there's nothing to pre-evaluate. */
3540 if (TREE_CONSTANT (*expr_p))
3542 /* Ensure it does not have side effects, it might contain a reference to
3543 the object we're initializing. */
3544 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3545 return;
3548 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3549 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3550 return;
3552 /* Recurse for nested constructors. */
3553 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3555 unsigned HOST_WIDE_INT ix;
3556 constructor_elt *ce;
3557 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3559 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3560 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3562 return;
3565 /* If this is a variable sized type, we must remember the size. */
3566 maybe_with_size_expr (expr_p);
3568 /* Gimplify the constructor element to something appropriate for the rhs
3569 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3570 the gimplifier will consider this a store to memory. Doing this
3571 gimplification now means that we won't have to deal with complicated
3572 language-specific trees, nor trees like SAVE_EXPR that can induce
3573 exponential search behavior. */
3574 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3575 if (one == GS_ERROR)
3577 *expr_p = NULL;
3578 return;
3581 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3582 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3583 always be true for all scalars, since is_gimple_mem_rhs insists on a
3584 temporary variable for them. */
3585 if (DECL_P (*expr_p))
3586 return;
3588 /* If this is of variable size, we have no choice but to assume it doesn't
3589 overlap since we can't make a temporary for it. */
3590 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3591 return;
3593 /* Otherwise, we must search for overlap ... */
3594 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3595 return;
3597 /* ... and if found, force the value into a temporary. */
3598 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3601 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3602 a RANGE_EXPR in a CONSTRUCTOR for an array.
3604 var = lower;
3605 loop_entry:
3606 object[var] = value;
3607 if (var == upper)
3608 goto loop_exit;
3609 var = var + 1;
3610 goto loop_entry;
3611 loop_exit:
3613 We increment var _after_ the loop exit check because we might otherwise
3614 fail if upper == TYPE_MAX_VALUE (type for upper).
3616 Note that we never have to deal with SAVE_EXPRs here, because this has
3617 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3619 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3620 gimple_seq *, bool);
3622 static void
3623 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3624 tree value, tree array_elt_type,
3625 gimple_seq *pre_p, bool cleared)
3627 tree loop_entry_label, loop_exit_label, fall_thru_label;
3628 tree var, var_type, cref, tmp;
3630 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3631 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3632 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3634 /* Create and initialize the index variable. */
3635 var_type = TREE_TYPE (upper);
3636 var = create_tmp_var (var_type, NULL);
3637 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3639 /* Add the loop entry label. */
3640 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3642 /* Build the reference. */
3643 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3644 var, NULL_TREE, NULL_TREE);
3646 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3647 the store. Otherwise just assign value to the reference. */
3649 if (TREE_CODE (value) == CONSTRUCTOR)
3650 /* NB we might have to call ourself recursively through
3651 gimplify_init_ctor_eval if the value is a constructor. */
3652 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3653 pre_p, cleared);
3654 else
3655 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3657 /* We exit the loop when the index var is equal to the upper bound. */
3658 gimplify_seq_add_stmt (pre_p,
3659 gimple_build_cond (EQ_EXPR, var, upper,
3660 loop_exit_label, fall_thru_label));
3662 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3664 /* Otherwise, increment the index var... */
3665 tmp = build2 (PLUS_EXPR, var_type, var,
3666 fold_convert (var_type, integer_one_node));
3667 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3669 /* ...and jump back to the loop entry. */
3670 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3672 /* Add the loop exit label. */
3673 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3676 /* Return true if FDECL is accessing a field that is zero sized. */
3678 static bool
3679 zero_sized_field_decl (const_tree fdecl)
3681 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3682 && integer_zerop (DECL_SIZE (fdecl)))
3683 return true;
3684 return false;
3687 /* Return true if TYPE is zero sized. */
3689 static bool
3690 zero_sized_type (const_tree type)
3692 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3693 && integer_zerop (TYPE_SIZE (type)))
3694 return true;
3695 return false;
3698 /* A subroutine of gimplify_init_constructor. Generate individual
3699 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3700 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3701 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3702 zeroed first. */
3704 static void
3705 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3706 gimple_seq *pre_p, bool cleared)
3708 tree array_elt_type = NULL;
3709 unsigned HOST_WIDE_INT ix;
3710 tree purpose, value;
3712 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3713 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3715 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3717 tree cref;
3719 /* NULL values are created above for gimplification errors. */
3720 if (value == NULL)
3721 continue;
3723 if (cleared && initializer_zerop (value))
3724 continue;
3726 /* ??? Here's to hoping the front end fills in all of the indices,
3727 so we don't have to figure out what's missing ourselves. */
3728 gcc_assert (purpose);
3730 /* Skip zero-sized fields, unless value has side-effects. This can
3731 happen with calls to functions returning a zero-sized type, which
3732 we shouldn't discard. As a number of downstream passes don't
3733 expect sets of zero-sized fields, we rely on the gimplification of
3734 the MODIFY_EXPR we make below to drop the assignment statement. */
3735 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3736 continue;
3738 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3739 whole range. */
3740 if (TREE_CODE (purpose) == RANGE_EXPR)
3742 tree lower = TREE_OPERAND (purpose, 0);
3743 tree upper = TREE_OPERAND (purpose, 1);
3745 /* If the lower bound is equal to upper, just treat it as if
3746 upper was the index. */
3747 if (simple_cst_equal (lower, upper))
3748 purpose = upper;
3749 else
3751 gimplify_init_ctor_eval_range (object, lower, upper, value,
3752 array_elt_type, pre_p, cleared);
3753 continue;
3757 if (array_elt_type)
3759 /* Do not use bitsizetype for ARRAY_REF indices. */
3760 if (TYPE_DOMAIN (TREE_TYPE (object)))
3761 purpose
3762 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3763 purpose);
3764 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3765 purpose, NULL_TREE, NULL_TREE);
3767 else
3769 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3770 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3771 unshare_expr (object), purpose, NULL_TREE);
3774 if (TREE_CODE (value) == CONSTRUCTOR
3775 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3776 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3777 pre_p, cleared);
3778 else
3780 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3781 gimplify_and_add (init, pre_p);
3782 ggc_free (init);
3787 /* Return the appropriate RHS predicate for this LHS. */
3789 gimple_predicate
3790 rhs_predicate_for (tree lhs)
3792 if (is_gimple_reg (lhs))
3793 return is_gimple_reg_rhs_or_call;
3794 else
3795 return is_gimple_mem_rhs_or_call;
3798 /* Gimplify a C99 compound literal expression. This just means adding
3799 the DECL_EXPR before the current statement and using its anonymous
3800 decl instead. */
3802 static enum gimplify_status
3803 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3804 bool (*gimple_test_f) (tree),
3805 fallback_t fallback)
3807 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3808 tree decl = DECL_EXPR_DECL (decl_s);
3809 tree init = DECL_INITIAL (decl);
3810 /* Mark the decl as addressable if the compound literal
3811 expression is addressable now, otherwise it is marked too late
3812 after we gimplify the initialization expression. */
3813 if (TREE_ADDRESSABLE (*expr_p))
3814 TREE_ADDRESSABLE (decl) = 1;
3815 /* Otherwise, if we don't need an lvalue and have a literal directly
3816 substitute it. Check if it matches the gimple predicate, as
3817 otherwise we'd generate a new temporary, and we can as well just
3818 use the decl we already have. */
3819 else if (!TREE_ADDRESSABLE (decl)
3820 && init
3821 && (fallback & fb_lvalue) == 0
3822 && gimple_test_f (init))
3824 *expr_p = init;
3825 return GS_OK;
3828 /* Preliminarily mark non-addressed complex variables as eligible
3829 for promotion to gimple registers. We'll transform their uses
3830 as we find them. */
3831 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3832 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3833 && !TREE_THIS_VOLATILE (decl)
3834 && !needs_to_live_in_memory (decl))
3835 DECL_GIMPLE_REG_P (decl) = 1;
3837 /* If the decl is not addressable, then it is being used in some
3838 expression or on the right hand side of a statement, and it can
3839 be put into a readonly data section. */
3840 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3841 TREE_READONLY (decl) = 1;
3843 /* This decl isn't mentioned in the enclosing block, so add it to the
3844 list of temps. FIXME it seems a bit of a kludge to say that
3845 anonymous artificial vars aren't pushed, but everything else is. */
3846 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3847 gimple_add_tmp_var (decl);
3849 gimplify_and_add (decl_s, pre_p);
3850 *expr_p = decl;
3851 return GS_OK;
3854 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3855 return a new CONSTRUCTOR if something changed. */
3857 static tree
3858 optimize_compound_literals_in_ctor (tree orig_ctor)
3860 tree ctor = orig_ctor;
3861 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3862 unsigned int idx, num = vec_safe_length (elts);
3864 for (idx = 0; idx < num; idx++)
3866 tree value = (*elts)[idx].value;
3867 tree newval = value;
3868 if (TREE_CODE (value) == CONSTRUCTOR)
3869 newval = optimize_compound_literals_in_ctor (value);
3870 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3872 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3873 tree decl = DECL_EXPR_DECL (decl_s);
3874 tree init = DECL_INITIAL (decl);
3876 if (!TREE_ADDRESSABLE (value)
3877 && !TREE_ADDRESSABLE (decl)
3878 && init
3879 && TREE_CODE (init) == CONSTRUCTOR)
3880 newval = optimize_compound_literals_in_ctor (init);
3882 if (newval == value)
3883 continue;
3885 if (ctor == orig_ctor)
3887 ctor = copy_node (orig_ctor);
3888 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3889 elts = CONSTRUCTOR_ELTS (ctor);
3891 (*elts)[idx].value = newval;
3893 return ctor;
3896 /* A subroutine of gimplify_modify_expr. Break out elements of a
3897 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3899 Note that we still need to clear any elements that don't have explicit
3900 initializers, so if not all elements are initialized we keep the
3901 original MODIFY_EXPR, we just remove all of the constructor elements.
3903 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3904 GS_ERROR if we would have to create a temporary when gimplifying
3905 this constructor. Otherwise, return GS_OK.
3907 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3909 static enum gimplify_status
3910 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3911 bool want_value, bool notify_temp_creation)
3913 tree object, ctor, type;
3914 enum gimplify_status ret;
3915 vec<constructor_elt, va_gc> *elts;
3917 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3919 if (!notify_temp_creation)
3921 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3922 is_gimple_lvalue, fb_lvalue);
3923 if (ret == GS_ERROR)
3924 return ret;
3927 object = TREE_OPERAND (*expr_p, 0);
3928 ctor = TREE_OPERAND (*expr_p, 1) =
3929 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3930 type = TREE_TYPE (ctor);
3931 elts = CONSTRUCTOR_ELTS (ctor);
3932 ret = GS_ALL_DONE;
3934 switch (TREE_CODE (type))
3936 case RECORD_TYPE:
3937 case UNION_TYPE:
3938 case QUAL_UNION_TYPE:
3939 case ARRAY_TYPE:
3941 struct gimplify_init_ctor_preeval_data preeval_data;
3942 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3943 bool cleared, complete_p, valid_const_initializer;
3945 /* Aggregate types must lower constructors to initialization of
3946 individual elements. The exception is that a CONSTRUCTOR node
3947 with no elements indicates zero-initialization of the whole. */
3948 if (vec_safe_is_empty (elts))
3950 if (notify_temp_creation)
3951 return GS_OK;
3952 break;
3955 /* Fetch information about the constructor to direct later processing.
3956 We might want to make static versions of it in various cases, and
3957 can only do so if it known to be a valid constant initializer. */
3958 valid_const_initializer
3959 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3960 &num_ctor_elements, &complete_p);
3962 /* If a const aggregate variable is being initialized, then it
3963 should never be a lose to promote the variable to be static. */
3964 if (valid_const_initializer
3965 && num_nonzero_elements > 1
3966 && TREE_READONLY (object)
3967 && TREE_CODE (object) == VAR_DECL
3968 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3970 if (notify_temp_creation)
3971 return GS_ERROR;
3972 DECL_INITIAL (object) = ctor;
3973 TREE_STATIC (object) = 1;
3974 if (!DECL_NAME (object))
3975 DECL_NAME (object) = create_tmp_var_name ("C");
3976 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3978 /* ??? C++ doesn't automatically append a .<number> to the
3979 assembler name, and even when it does, it looks at FE private
3980 data structures to figure out what that number should be,
3981 which are not set for this variable. I suppose this is
3982 important for local statics for inline functions, which aren't
3983 "local" in the object file sense. So in order to get a unique
3984 TU-local symbol, we must invoke the lhd version now. */
3985 lhd_set_decl_assembler_name (object);
3987 *expr_p = NULL_TREE;
3988 break;
3991 /* If there are "lots" of initialized elements, even discounting
3992 those that are not address constants (and thus *must* be
3993 computed at runtime), then partition the constructor into
3994 constant and non-constant parts. Block copy the constant
3995 parts in, then generate code for the non-constant parts. */
3996 /* TODO. There's code in cp/typeck.c to do this. */
3998 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3999 /* store_constructor will ignore the clearing of variable-sized
4000 objects. Initializers for such objects must explicitly set
4001 every field that needs to be set. */
4002 cleared = false;
4003 else if (!complete_p)
4004 /* If the constructor isn't complete, clear the whole object
4005 beforehand.
4007 ??? This ought not to be needed. For any element not present
4008 in the initializer, we should simply set them to zero. Except
4009 we'd need to *find* the elements that are not present, and that
4010 requires trickery to avoid quadratic compile-time behavior in
4011 large cases or excessive memory use in small cases. */
4012 cleared = true;
4013 else if (num_ctor_elements - num_nonzero_elements
4014 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4015 && num_nonzero_elements < num_ctor_elements / 4)
4016 /* If there are "lots" of zeros, it's more efficient to clear
4017 the memory and then set the nonzero elements. */
4018 cleared = true;
4019 else
4020 cleared = false;
4022 /* If there are "lots" of initialized elements, and all of them
4023 are valid address constants, then the entire initializer can
4024 be dropped to memory, and then memcpy'd out. Don't do this
4025 for sparse arrays, though, as it's more efficient to follow
4026 the standard CONSTRUCTOR behavior of memset followed by
4027 individual element initialization. Also don't do this for small
4028 all-zero initializers (which aren't big enough to merit
4029 clearing), and don't try to make bitwise copies of
4030 TREE_ADDRESSABLE types. */
4031 if (valid_const_initializer
4032 && !(cleared || num_nonzero_elements == 0)
4033 && !TREE_ADDRESSABLE (type))
4035 HOST_WIDE_INT size = int_size_in_bytes (type);
4036 unsigned int align;
4038 /* ??? We can still get unbounded array types, at least
4039 from the C++ front end. This seems wrong, but attempt
4040 to work around it for now. */
4041 if (size < 0)
4043 size = int_size_in_bytes (TREE_TYPE (object));
4044 if (size >= 0)
4045 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4048 /* Find the maximum alignment we can assume for the object. */
4049 /* ??? Make use of DECL_OFFSET_ALIGN. */
4050 if (DECL_P (object))
4051 align = DECL_ALIGN (object);
4052 else
4053 align = TYPE_ALIGN (type);
4055 /* Do a block move either if the size is so small as to make
4056 each individual move a sub-unit move on average, or if it
4057 is so large as to make individual moves inefficient. */
4058 if (size > 0
4059 && num_nonzero_elements > 1
4060 && (size < num_nonzero_elements
4061 || !can_move_by_pieces (size, align)))
4063 if (notify_temp_creation)
4064 return GS_ERROR;
4066 walk_tree (&ctor, force_labels_r, NULL, NULL);
4067 ctor = tree_output_constant_def (ctor);
4068 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4069 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4070 TREE_OPERAND (*expr_p, 1) = ctor;
4072 /* This is no longer an assignment of a CONSTRUCTOR, but
4073 we still may have processing to do on the LHS. So
4074 pretend we didn't do anything here to let that happen. */
4075 return GS_UNHANDLED;
4079 /* If the target is volatile, we have non-zero elements and more than
4080 one field to assign, initialize the target from a temporary. */
4081 if (TREE_THIS_VOLATILE (object)
4082 && !TREE_ADDRESSABLE (type)
4083 && num_nonzero_elements > 0
4084 && vec_safe_length (elts) > 1)
4086 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4087 TREE_OPERAND (*expr_p, 0) = temp;
4088 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4089 *expr_p,
4090 build2 (MODIFY_EXPR, void_type_node,
4091 object, temp));
4092 return GS_OK;
4095 if (notify_temp_creation)
4096 return GS_OK;
4098 /* If there are nonzero elements and if needed, pre-evaluate to capture
4099 elements overlapping with the lhs into temporaries. We must do this
4100 before clearing to fetch the values before they are zeroed-out. */
4101 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4103 preeval_data.lhs_base_decl = get_base_address (object);
4104 if (!DECL_P (preeval_data.lhs_base_decl))
4105 preeval_data.lhs_base_decl = NULL;
4106 preeval_data.lhs_alias_set = get_alias_set (object);
4108 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4109 pre_p, post_p, &preeval_data);
4112 if (cleared)
4114 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4115 Note that we still have to gimplify, in order to handle the
4116 case of variable sized types. Avoid shared tree structures. */
4117 CONSTRUCTOR_ELTS (ctor) = NULL;
4118 TREE_SIDE_EFFECTS (ctor) = 0;
4119 object = unshare_expr (object);
4120 gimplify_stmt (expr_p, pre_p);
4123 /* If we have not block cleared the object, or if there are nonzero
4124 elements in the constructor, add assignments to the individual
4125 scalar fields of the object. */
4126 if (!cleared || num_nonzero_elements > 0)
4127 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4129 *expr_p = NULL_TREE;
4131 break;
4133 case COMPLEX_TYPE:
4135 tree r, i;
4137 if (notify_temp_creation)
4138 return GS_OK;
4140 /* Extract the real and imaginary parts out of the ctor. */
4141 gcc_assert (elts->length () == 2);
4142 r = (*elts)[0].value;
4143 i = (*elts)[1].value;
4144 if (r == NULL || i == NULL)
4146 tree zero = build_zero_cst (TREE_TYPE (type));
4147 if (r == NULL)
4148 r = zero;
4149 if (i == NULL)
4150 i = zero;
4153 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4154 represent creation of a complex value. */
4155 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4157 ctor = build_complex (type, r, i);
4158 TREE_OPERAND (*expr_p, 1) = ctor;
4160 else
4162 ctor = build2 (COMPLEX_EXPR, type, r, i);
4163 TREE_OPERAND (*expr_p, 1) = ctor;
4164 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4165 pre_p,
4166 post_p,
4167 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4168 fb_rvalue);
4171 break;
4173 case VECTOR_TYPE:
4175 unsigned HOST_WIDE_INT ix;
4176 constructor_elt *ce;
4178 if (notify_temp_creation)
4179 return GS_OK;
4181 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4182 if (TREE_CONSTANT (ctor))
4184 bool constant_p = true;
4185 tree value;
4187 /* Even when ctor is constant, it might contain non-*_CST
4188 elements, such as addresses or trapping values like
4189 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4190 in VECTOR_CST nodes. */
4191 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4192 if (!CONSTANT_CLASS_P (value))
4194 constant_p = false;
4195 break;
4198 if (constant_p)
4200 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4201 break;
4204 /* Don't reduce an initializer constant even if we can't
4205 make a VECTOR_CST. It won't do anything for us, and it'll
4206 prevent us from representing it as a single constant. */
4207 if (initializer_constant_valid_p (ctor, type))
4208 break;
4210 TREE_CONSTANT (ctor) = 0;
4213 /* Vector types use CONSTRUCTOR all the way through gimple
4214 compilation as a general initializer. */
4215 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4217 enum gimplify_status tret;
4218 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4219 fb_rvalue);
4220 if (tret == GS_ERROR)
4221 ret = GS_ERROR;
4223 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4224 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4226 break;
4228 default:
4229 /* So how did we get a CONSTRUCTOR for a scalar type? */
4230 gcc_unreachable ();
4233 if (ret == GS_ERROR)
4234 return GS_ERROR;
4235 else if (want_value)
4237 *expr_p = object;
4238 return GS_OK;
4240 else
4242 /* If we have gimplified both sides of the initializer but have
4243 not emitted an assignment, do so now. */
4244 if (*expr_p)
4246 tree lhs = TREE_OPERAND (*expr_p, 0);
4247 tree rhs = TREE_OPERAND (*expr_p, 1);
4248 gimple init = gimple_build_assign (lhs, rhs);
4249 gimplify_seq_add_stmt (pre_p, init);
4250 *expr_p = NULL;
4253 return GS_ALL_DONE;
4257 /* Given a pointer value OP0, return a simplified version of an
4258 indirection through OP0, or NULL_TREE if no simplification is
4259 possible. Note that the resulting type may be different from
4260 the type pointed to in the sense that it is still compatible
4261 from the langhooks point of view. */
4263 tree
4264 gimple_fold_indirect_ref (tree t)
4266 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4267 tree sub = t;
4268 tree subtype;
4270 STRIP_NOPS (sub);
4271 subtype = TREE_TYPE (sub);
4272 if (!POINTER_TYPE_P (subtype))
4273 return NULL_TREE;
4275 if (TREE_CODE (sub) == ADDR_EXPR)
4277 tree op = TREE_OPERAND (sub, 0);
4278 tree optype = TREE_TYPE (op);
4279 /* *&p => p */
4280 if (useless_type_conversion_p (type, optype))
4281 return op;
4283 /* *(foo *)&fooarray => fooarray[0] */
4284 if (TREE_CODE (optype) == ARRAY_TYPE
4285 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4286 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4288 tree type_domain = TYPE_DOMAIN (optype);
4289 tree min_val = size_zero_node;
4290 if (type_domain && TYPE_MIN_VALUE (type_domain))
4291 min_val = TYPE_MIN_VALUE (type_domain);
4292 if (TREE_CODE (min_val) == INTEGER_CST)
4293 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4295 /* *(foo *)&complexfoo => __real__ complexfoo */
4296 else if (TREE_CODE (optype) == COMPLEX_TYPE
4297 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4298 return fold_build1 (REALPART_EXPR, type, op);
4299 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4300 else if (TREE_CODE (optype) == VECTOR_TYPE
4301 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4303 tree part_width = TYPE_SIZE (type);
4304 tree index = bitsize_int (0);
4305 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4309 /* *(p + CST) -> ... */
4310 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4311 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4313 tree addr = TREE_OPERAND (sub, 0);
4314 tree off = TREE_OPERAND (sub, 1);
4315 tree addrtype;
4317 STRIP_NOPS (addr);
4318 addrtype = TREE_TYPE (addr);
4320 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4321 if (TREE_CODE (addr) == ADDR_EXPR
4322 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4323 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4324 && host_integerp (off, 1))
4326 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4327 tree part_width = TYPE_SIZE (type);
4328 unsigned HOST_WIDE_INT part_widthi
4329 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4330 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4331 tree index = bitsize_int (indexi);
4332 if (offset / part_widthi
4333 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4334 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4335 part_width, index);
4338 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4339 if (TREE_CODE (addr) == ADDR_EXPR
4340 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4341 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4343 tree size = TYPE_SIZE_UNIT (type);
4344 if (tree_int_cst_equal (size, off))
4345 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4348 /* *(p + CST) -> MEM_REF <p, CST>. */
4349 if (TREE_CODE (addr) != ADDR_EXPR
4350 || DECL_P (TREE_OPERAND (addr, 0)))
4351 return fold_build2 (MEM_REF, type,
4352 addr,
4353 build_int_cst_wide (ptype,
4354 TREE_INT_CST_LOW (off),
4355 TREE_INT_CST_HIGH (off)));
4358 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4359 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4360 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4361 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4363 tree type_domain;
4364 tree min_val = size_zero_node;
4365 tree osub = sub;
4366 sub = gimple_fold_indirect_ref (sub);
4367 if (! sub)
4368 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4369 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4370 if (type_domain && TYPE_MIN_VALUE (type_domain))
4371 min_val = TYPE_MIN_VALUE (type_domain);
4372 if (TREE_CODE (min_val) == INTEGER_CST)
4373 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4376 return NULL_TREE;
4379 /* Given a pointer value OP0, return a simplified version of an
4380 indirection through OP0, or NULL_TREE if no simplification is
4381 possible. This may only be applied to a rhs of an expression.
4382 Note that the resulting type may be different from the type pointed
4383 to in the sense that it is still compatible from the langhooks
4384 point of view. */
4386 static tree
4387 gimple_fold_indirect_ref_rhs (tree t)
4389 return gimple_fold_indirect_ref (t);
4392 /* Subroutine of gimplify_modify_expr to do simplifications of
4393 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4394 something changes. */
4396 static enum gimplify_status
4397 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4398 gimple_seq *pre_p, gimple_seq *post_p,
4399 bool want_value)
4401 enum gimplify_status ret = GS_UNHANDLED;
4402 bool changed;
4406 changed = false;
4407 switch (TREE_CODE (*from_p))
4409 case VAR_DECL:
4410 /* If we're assigning from a read-only variable initialized with
4411 a constructor, do the direct assignment from the constructor,
4412 but only if neither source nor target are volatile since this
4413 latter assignment might end up being done on a per-field basis. */
4414 if (DECL_INITIAL (*from_p)
4415 && TREE_READONLY (*from_p)
4416 && !TREE_THIS_VOLATILE (*from_p)
4417 && !TREE_THIS_VOLATILE (*to_p)
4418 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4420 tree old_from = *from_p;
4421 enum gimplify_status subret;
4423 /* Move the constructor into the RHS. */
4424 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4426 /* Let's see if gimplify_init_constructor will need to put
4427 it in memory. */
4428 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4429 false, true);
4430 if (subret == GS_ERROR)
4432 /* If so, revert the change. */
4433 *from_p = old_from;
4435 else
4437 ret = GS_OK;
4438 changed = true;
4441 break;
4442 case INDIRECT_REF:
4444 /* If we have code like
4446 *(const A*)(A*)&x
4448 where the type of "x" is a (possibly cv-qualified variant
4449 of "A"), treat the entire expression as identical to "x".
4450 This kind of code arises in C++ when an object is bound
4451 to a const reference, and if "x" is a TARGET_EXPR we want
4452 to take advantage of the optimization below. */
4453 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4454 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4455 if (t)
4457 if (TREE_THIS_VOLATILE (t) != volatile_p)
4459 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4460 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4461 build_fold_addr_expr (t));
4462 if (REFERENCE_CLASS_P (t))
4463 TREE_THIS_VOLATILE (t) = volatile_p;
4465 *from_p = t;
4466 ret = GS_OK;
4467 changed = true;
4469 break;
4472 case TARGET_EXPR:
4474 /* If we are initializing something from a TARGET_EXPR, strip the
4475 TARGET_EXPR and initialize it directly, if possible. This can't
4476 be done if the initializer is void, since that implies that the
4477 temporary is set in some non-trivial way.
4479 ??? What about code that pulls out the temp and uses it
4480 elsewhere? I think that such code never uses the TARGET_EXPR as
4481 an initializer. If I'm wrong, we'll die because the temp won't
4482 have any RTL. In that case, I guess we'll need to replace
4483 references somehow. */
4484 tree init = TARGET_EXPR_INITIAL (*from_p);
4486 if (init
4487 && !VOID_TYPE_P (TREE_TYPE (init)))
4489 *from_p = init;
4490 ret = GS_OK;
4491 changed = true;
4494 break;
4496 case COMPOUND_EXPR:
4497 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4498 caught. */
4499 gimplify_compound_expr (from_p, pre_p, true);
4500 ret = GS_OK;
4501 changed = true;
4502 break;
4504 case CONSTRUCTOR:
4505 /* If we already made some changes, let the front end have a
4506 crack at this before we break it down. */
4507 if (ret != GS_UNHANDLED)
4508 break;
4509 /* If we're initializing from a CONSTRUCTOR, break this into
4510 individual MODIFY_EXPRs. */
4511 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4512 false);
4514 case COND_EXPR:
4515 /* If we're assigning to a non-register type, push the assignment
4516 down into the branches. This is mandatory for ADDRESSABLE types,
4517 since we cannot generate temporaries for such, but it saves a
4518 copy in other cases as well. */
4519 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4521 /* This code should mirror the code in gimplify_cond_expr. */
4522 enum tree_code code = TREE_CODE (*expr_p);
4523 tree cond = *from_p;
4524 tree result = *to_p;
4526 ret = gimplify_expr (&result, pre_p, post_p,
4527 is_gimple_lvalue, fb_lvalue);
4528 if (ret != GS_ERROR)
4529 ret = GS_OK;
4531 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4532 TREE_OPERAND (cond, 1)
4533 = build2 (code, void_type_node, result,
4534 TREE_OPERAND (cond, 1));
4535 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4536 TREE_OPERAND (cond, 2)
4537 = build2 (code, void_type_node, unshare_expr (result),
4538 TREE_OPERAND (cond, 2));
4540 TREE_TYPE (cond) = void_type_node;
4541 recalculate_side_effects (cond);
4543 if (want_value)
4545 gimplify_and_add (cond, pre_p);
4546 *expr_p = unshare_expr (result);
4548 else
4549 *expr_p = cond;
4550 return ret;
4552 break;
4554 case CALL_EXPR:
4555 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4556 return slot so that we don't generate a temporary. */
4557 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4558 && aggregate_value_p (*from_p, *from_p))
4560 bool use_target;
4562 if (!(rhs_predicate_for (*to_p))(*from_p))
4563 /* If we need a temporary, *to_p isn't accurate. */
4564 use_target = false;
4565 /* It's OK to use the return slot directly unless it's an NRV. */
4566 else if (TREE_CODE (*to_p) == RESULT_DECL
4567 && DECL_NAME (*to_p) == NULL_TREE
4568 && needs_to_live_in_memory (*to_p))
4569 use_target = true;
4570 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4571 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4572 /* Don't force regs into memory. */
4573 use_target = false;
4574 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4575 /* It's OK to use the target directly if it's being
4576 initialized. */
4577 use_target = true;
4578 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4579 /* Always use the target and thus RSO for variable-sized types.
4580 GIMPLE cannot deal with a variable-sized assignment
4581 embedded in a call statement. */
4582 use_target = true;
4583 else if (TREE_CODE (*to_p) != SSA_NAME
4584 && (!is_gimple_variable (*to_p)
4585 || needs_to_live_in_memory (*to_p)))
4586 /* Don't use the original target if it's already addressable;
4587 if its address escapes, and the called function uses the
4588 NRV optimization, a conforming program could see *to_p
4589 change before the called function returns; see c++/19317.
4590 When optimizing, the return_slot pass marks more functions
4591 as safe after we have escape info. */
4592 use_target = false;
4593 else
4594 use_target = true;
4596 if (use_target)
4598 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4599 mark_addressable (*to_p);
4602 break;
4604 case WITH_SIZE_EXPR:
4605 /* Likewise for calls that return an aggregate of non-constant size,
4606 since we would not be able to generate a temporary at all. */
4607 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4609 *from_p = TREE_OPERAND (*from_p, 0);
4610 /* We don't change ret in this case because the
4611 WITH_SIZE_EXPR might have been added in
4612 gimplify_modify_expr, so returning GS_OK would lead to an
4613 infinite loop. */
4614 changed = true;
4616 break;
4618 /* If we're initializing from a container, push the initialization
4619 inside it. */
4620 case CLEANUP_POINT_EXPR:
4621 case BIND_EXPR:
4622 case STATEMENT_LIST:
4624 tree wrap = *from_p;
4625 tree t;
4627 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4628 fb_lvalue);
4629 if (ret != GS_ERROR)
4630 ret = GS_OK;
4632 t = voidify_wrapper_expr (wrap, *expr_p);
4633 gcc_assert (t == *expr_p);
4635 if (want_value)
4637 gimplify_and_add (wrap, pre_p);
4638 *expr_p = unshare_expr (*to_p);
4640 else
4641 *expr_p = wrap;
4642 return GS_OK;
4645 case COMPOUND_LITERAL_EXPR:
4647 tree complit = TREE_OPERAND (*expr_p, 1);
4648 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4649 tree decl = DECL_EXPR_DECL (decl_s);
4650 tree init = DECL_INITIAL (decl);
4652 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4653 into struct T x = { 0, 1, 2 } if the address of the
4654 compound literal has never been taken. */
4655 if (!TREE_ADDRESSABLE (complit)
4656 && !TREE_ADDRESSABLE (decl)
4657 && init)
4659 *expr_p = copy_node (*expr_p);
4660 TREE_OPERAND (*expr_p, 1) = init;
4661 return GS_OK;
4665 default:
4666 break;
4669 while (changed);
4671 return ret;
4675 /* Return true if T looks like a valid GIMPLE statement. */
4677 static bool
4678 is_gimple_stmt (tree t)
4680 const enum tree_code code = TREE_CODE (t);
4682 switch (code)
4684 case NOP_EXPR:
4685 /* The only valid NOP_EXPR is the empty statement. */
4686 return IS_EMPTY_STMT (t);
4688 case BIND_EXPR:
4689 case COND_EXPR:
4690 /* These are only valid if they're void. */
4691 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4693 case SWITCH_EXPR:
4694 case GOTO_EXPR:
4695 case RETURN_EXPR:
4696 case LABEL_EXPR:
4697 case CASE_LABEL_EXPR:
4698 case TRY_CATCH_EXPR:
4699 case TRY_FINALLY_EXPR:
4700 case EH_FILTER_EXPR:
4701 case CATCH_EXPR:
4702 case ASM_EXPR:
4703 case STATEMENT_LIST:
4704 case OMP_PARALLEL:
4705 case OMP_FOR:
4706 case OMP_SECTIONS:
4707 case OMP_SECTION:
4708 case OMP_SINGLE:
4709 case OMP_MASTER:
4710 case OMP_ORDERED:
4711 case OMP_CRITICAL:
4712 case OMP_TASK:
4713 /* These are always void. */
4714 return true;
4716 case CALL_EXPR:
4717 case MODIFY_EXPR:
4718 case PREDICT_EXPR:
4719 /* These are valid regardless of their type. */
4720 return true;
4722 default:
4723 return false;
4728 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4729 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4730 DECL_GIMPLE_REG_P set.
4732 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4733 other, unmodified part of the complex object just before the total store.
4734 As a consequence, if the object is still uninitialized, an undefined value
4735 will be loaded into a register, which may result in a spurious exception
4736 if the register is floating-point and the value happens to be a signaling
4737 NaN for example. Then the fully-fledged complex operations lowering pass
4738 followed by a DCE pass are necessary in order to fix things up. */
4740 static enum gimplify_status
4741 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4742 bool want_value)
4744 enum tree_code code, ocode;
4745 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4747 lhs = TREE_OPERAND (*expr_p, 0);
4748 rhs = TREE_OPERAND (*expr_p, 1);
4749 code = TREE_CODE (lhs);
4750 lhs = TREE_OPERAND (lhs, 0);
4752 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4753 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4754 TREE_NO_WARNING (other) = 1;
4755 other = get_formal_tmp_var (other, pre_p);
4757 realpart = code == REALPART_EXPR ? rhs : other;
4758 imagpart = code == REALPART_EXPR ? other : rhs;
4760 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4761 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4762 else
4763 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4765 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4766 *expr_p = (want_value) ? rhs : NULL_TREE;
4768 return GS_ALL_DONE;
4771 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4773 modify_expr
4774 : varname '=' rhs
4775 | '*' ID '=' rhs
4777 PRE_P points to the list where side effects that must happen before
4778 *EXPR_P should be stored.
4780 POST_P points to the list where side effects that must happen after
4781 *EXPR_P should be stored.
4783 WANT_VALUE is nonzero iff we want to use the value of this expression
4784 in another expression. */
4786 static enum gimplify_status
4787 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4788 bool want_value)
4790 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4791 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4792 enum gimplify_status ret = GS_UNHANDLED;
4793 gimple assign;
4794 location_t loc = EXPR_LOCATION (*expr_p);
4795 gimple_stmt_iterator gsi;
4797 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4798 || TREE_CODE (*expr_p) == INIT_EXPR);
4800 /* Trying to simplify a clobber using normal logic doesn't work,
4801 so handle it here. */
4802 if (TREE_CLOBBER_P (*from_p))
4804 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4805 if (ret == GS_ERROR)
4806 return ret;
4807 gcc_assert (!want_value
4808 && (TREE_CODE (*to_p) == VAR_DECL
4809 || TREE_CODE (*to_p) == MEM_REF));
4810 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4811 *expr_p = NULL;
4812 return GS_ALL_DONE;
4815 /* Insert pointer conversions required by the middle-end that are not
4816 required by the frontend. This fixes middle-end type checking for
4817 for example gcc.dg/redecl-6.c. */
4818 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4820 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4821 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4822 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4825 /* See if any simplifications can be done based on what the RHS is. */
4826 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4827 want_value);
4828 if (ret != GS_UNHANDLED)
4829 return ret;
4831 /* For zero sized types only gimplify the left hand side and right hand
4832 side as statements and throw away the assignment. Do this after
4833 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4834 types properly. */
4835 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4837 gimplify_stmt (from_p, pre_p);
4838 gimplify_stmt (to_p, pre_p);
4839 *expr_p = NULL_TREE;
4840 return GS_ALL_DONE;
4843 /* If the value being copied is of variable width, compute the length
4844 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4845 before gimplifying any of the operands so that we can resolve any
4846 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4847 the size of the expression to be copied, not of the destination, so
4848 that is what we must do here. */
4849 maybe_with_size_expr (from_p);
4851 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4852 if (ret == GS_ERROR)
4853 return ret;
4855 /* As a special case, we have to temporarily allow for assignments
4856 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4857 a toplevel statement, when gimplifying the GENERIC expression
4858 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4859 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4861 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4862 prevent gimplify_expr from trying to create a new temporary for
4863 foo's LHS, we tell it that it should only gimplify until it
4864 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4865 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4866 and all we need to do here is set 'a' to be its LHS. */
4867 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4868 fb_rvalue);
4869 if (ret == GS_ERROR)
4870 return ret;
4872 /* Now see if the above changed *from_p to something we handle specially. */
4873 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4874 want_value);
4875 if (ret != GS_UNHANDLED)
4876 return ret;
4878 /* If we've got a variable sized assignment between two lvalues (i.e. does
4879 not involve a call), then we can make things a bit more straightforward
4880 by converting the assignment to memcpy or memset. */
4881 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4883 tree from = TREE_OPERAND (*from_p, 0);
4884 tree size = TREE_OPERAND (*from_p, 1);
4886 if (TREE_CODE (from) == CONSTRUCTOR)
4887 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4889 if (is_gimple_addressable (from))
4891 *from_p = from;
4892 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4893 pre_p);
4897 /* Transform partial stores to non-addressable complex variables into
4898 total stores. This allows us to use real instead of virtual operands
4899 for these variables, which improves optimization. */
4900 if ((TREE_CODE (*to_p) == REALPART_EXPR
4901 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4902 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4903 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4905 /* Try to alleviate the effects of the gimplification creating artificial
4906 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4907 if (!gimplify_ctxp->into_ssa
4908 && TREE_CODE (*from_p) == VAR_DECL
4909 && DECL_IGNORED_P (*from_p)
4910 && DECL_P (*to_p)
4911 && !DECL_IGNORED_P (*to_p))
4913 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4914 DECL_NAME (*from_p)
4915 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4916 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4917 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4920 if (want_value && TREE_THIS_VOLATILE (*to_p))
4921 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4923 if (TREE_CODE (*from_p) == CALL_EXPR)
4925 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4926 instead of a GIMPLE_ASSIGN. */
4927 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4928 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4929 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4930 assign = gimple_build_call_from_tree (*from_p);
4931 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4932 notice_special_calls (assign);
4933 if (!gimple_call_noreturn_p (assign))
4934 gimple_call_set_lhs (assign, *to_p);
4936 else
4938 assign = gimple_build_assign (*to_p, *from_p);
4939 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4942 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4944 /* We should have got an SSA name from the start. */
4945 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4948 gimplify_seq_add_stmt (pre_p, assign);
4949 gsi = gsi_last (*pre_p);
4950 fold_stmt (&gsi);
4952 if (want_value)
4954 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4955 return GS_OK;
4957 else
4958 *expr_p = NULL;
4960 return GS_ALL_DONE;
4963 /* Gimplify a comparison between two variable-sized objects. Do this
4964 with a call to BUILT_IN_MEMCMP. */
4966 static enum gimplify_status
4967 gimplify_variable_sized_compare (tree *expr_p)
4969 location_t loc = EXPR_LOCATION (*expr_p);
4970 tree op0 = TREE_OPERAND (*expr_p, 0);
4971 tree op1 = TREE_OPERAND (*expr_p, 1);
4972 tree t, arg, dest, src, expr;
4974 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4975 arg = unshare_expr (arg);
4976 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4977 src = build_fold_addr_expr_loc (loc, op1);
4978 dest = build_fold_addr_expr_loc (loc, op0);
4979 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4980 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4982 expr
4983 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4984 SET_EXPR_LOCATION (expr, loc);
4985 *expr_p = expr;
4987 return GS_OK;
4990 /* Gimplify a comparison between two aggregate objects of integral scalar
4991 mode as a comparison between the bitwise equivalent scalar values. */
4993 static enum gimplify_status
4994 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4996 location_t loc = EXPR_LOCATION (*expr_p);
4997 tree op0 = TREE_OPERAND (*expr_p, 0);
4998 tree op1 = TREE_OPERAND (*expr_p, 1);
5000 tree type = TREE_TYPE (op0);
5001 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5003 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5004 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5006 *expr_p
5007 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5009 return GS_OK;
5012 /* Gimplify an expression sequence. This function gimplifies each
5013 expression and rewrites the original expression with the last
5014 expression of the sequence in GIMPLE form.
5016 PRE_P points to the list where the side effects for all the
5017 expressions in the sequence will be emitted.
5019 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5021 static enum gimplify_status
5022 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5024 tree t = *expr_p;
5028 tree *sub_p = &TREE_OPERAND (t, 0);
5030 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5031 gimplify_compound_expr (sub_p, pre_p, false);
5032 else
5033 gimplify_stmt (sub_p, pre_p);
5035 t = TREE_OPERAND (t, 1);
5037 while (TREE_CODE (t) == COMPOUND_EXPR);
5039 *expr_p = t;
5040 if (want_value)
5041 return GS_OK;
5042 else
5044 gimplify_stmt (expr_p, pre_p);
5045 return GS_ALL_DONE;
5049 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5050 gimplify. After gimplification, EXPR_P will point to a new temporary
5051 that holds the original value of the SAVE_EXPR node.
5053 PRE_P points to the list where side effects that must happen before
5054 *EXPR_P should be stored. */
5056 static enum gimplify_status
5057 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5059 enum gimplify_status ret = GS_ALL_DONE;
5060 tree val;
5062 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5063 val = TREE_OPERAND (*expr_p, 0);
5065 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5066 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5068 /* The operand may be a void-valued expression such as SAVE_EXPRs
5069 generated by the Java frontend for class initialization. It is
5070 being executed only for its side-effects. */
5071 if (TREE_TYPE (val) == void_type_node)
5073 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5074 is_gimple_stmt, fb_none);
5075 val = NULL;
5077 else
5078 val = get_initialized_tmp_var (val, pre_p, post_p);
5080 TREE_OPERAND (*expr_p, 0) = val;
5081 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5084 *expr_p = val;
5086 return ret;
5089 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5091 unary_expr
5092 : ...
5093 | '&' varname
5096 PRE_P points to the list where side effects that must happen before
5097 *EXPR_P should be stored.
5099 POST_P points to the list where side effects that must happen after
5100 *EXPR_P should be stored. */
5102 static enum gimplify_status
5103 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5105 tree expr = *expr_p;
5106 tree op0 = TREE_OPERAND (expr, 0);
5107 enum gimplify_status ret;
5108 location_t loc = EXPR_LOCATION (*expr_p);
5110 switch (TREE_CODE (op0))
5112 case INDIRECT_REF:
5113 do_indirect_ref:
5114 /* Check if we are dealing with an expression of the form '&*ptr'.
5115 While the front end folds away '&*ptr' into 'ptr', these
5116 expressions may be generated internally by the compiler (e.g.,
5117 builtins like __builtin_va_end). */
5118 /* Caution: the silent array decomposition semantics we allow for
5119 ADDR_EXPR means we can't always discard the pair. */
5120 /* Gimplification of the ADDR_EXPR operand may drop
5121 cv-qualification conversions, so make sure we add them if
5122 needed. */
5124 tree op00 = TREE_OPERAND (op0, 0);
5125 tree t_expr = TREE_TYPE (expr);
5126 tree t_op00 = TREE_TYPE (op00);
5128 if (!useless_type_conversion_p (t_expr, t_op00))
5129 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5130 *expr_p = op00;
5131 ret = GS_OK;
5133 break;
5135 case VIEW_CONVERT_EXPR:
5136 /* Take the address of our operand and then convert it to the type of
5137 this ADDR_EXPR.
5139 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5140 all clear. The impact of this transformation is even less clear. */
5142 /* If the operand is a useless conversion, look through it. Doing so
5143 guarantees that the ADDR_EXPR and its operand will remain of the
5144 same type. */
5145 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5146 op0 = TREE_OPERAND (op0, 0);
5148 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5149 build_fold_addr_expr_loc (loc,
5150 TREE_OPERAND (op0, 0)));
5151 ret = GS_OK;
5152 break;
5154 default:
5155 /* We use fb_either here because the C frontend sometimes takes
5156 the address of a call that returns a struct; see
5157 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5158 the implied temporary explicit. */
5160 /* Make the operand addressable. */
5161 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5162 is_gimple_addressable, fb_either);
5163 if (ret == GS_ERROR)
5164 break;
5166 /* Then mark it. Beware that it may not be possible to do so directly
5167 if a temporary has been created by the gimplification. */
5168 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5170 op0 = TREE_OPERAND (expr, 0);
5172 /* For various reasons, the gimplification of the expression
5173 may have made a new INDIRECT_REF. */
5174 if (TREE_CODE (op0) == INDIRECT_REF)
5175 goto do_indirect_ref;
5177 mark_addressable (TREE_OPERAND (expr, 0));
5179 /* The FEs may end up building ADDR_EXPRs early on a decl with
5180 an incomplete type. Re-build ADDR_EXPRs in canonical form
5181 here. */
5182 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5183 *expr_p = build_fold_addr_expr (op0);
5185 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5186 recompute_tree_invariant_for_addr_expr (*expr_p);
5188 /* If we re-built the ADDR_EXPR add a conversion to the original type
5189 if required. */
5190 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5191 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5193 break;
5196 return ret;
5199 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5200 value; output operands should be a gimple lvalue. */
5202 static enum gimplify_status
5203 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5205 tree expr;
5206 int noutputs;
5207 const char **oconstraints;
5208 int i;
5209 tree link;
5210 const char *constraint;
5211 bool allows_mem, allows_reg, is_inout;
5212 enum gimplify_status ret, tret;
5213 gimple stmt;
5214 vec<tree, va_gc> *inputs;
5215 vec<tree, va_gc> *outputs;
5216 vec<tree, va_gc> *clobbers;
5217 vec<tree, va_gc> *labels;
5218 tree link_next;
5220 expr = *expr_p;
5221 noutputs = list_length (ASM_OUTPUTS (expr));
5222 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5224 inputs = NULL;
5225 outputs = NULL;
5226 clobbers = NULL;
5227 labels = NULL;
5229 ret = GS_ALL_DONE;
5230 link_next = NULL_TREE;
5231 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5233 bool ok;
5234 size_t constraint_len;
5236 link_next = TREE_CHAIN (link);
5238 oconstraints[i]
5239 = constraint
5240 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5241 constraint_len = strlen (constraint);
5242 if (constraint_len == 0)
5243 continue;
5245 ok = parse_output_constraint (&constraint, i, 0, 0,
5246 &allows_mem, &allows_reg, &is_inout);
5247 if (!ok)
5249 ret = GS_ERROR;
5250 is_inout = false;
5253 if (!allows_reg && allows_mem)
5254 mark_addressable (TREE_VALUE (link));
5256 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5257 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5258 fb_lvalue | fb_mayfail);
5259 if (tret == GS_ERROR)
5261 error ("invalid lvalue in asm output %d", i);
5262 ret = tret;
5265 vec_safe_push (outputs, link);
5266 TREE_CHAIN (link) = NULL_TREE;
5268 if (is_inout)
5270 /* An input/output operand. To give the optimizers more
5271 flexibility, split it into separate input and output
5272 operands. */
5273 tree input;
5274 char buf[10];
5276 /* Turn the in/out constraint into an output constraint. */
5277 char *p = xstrdup (constraint);
5278 p[0] = '=';
5279 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5281 /* And add a matching input constraint. */
5282 if (allows_reg)
5284 sprintf (buf, "%d", i);
5286 /* If there are multiple alternatives in the constraint,
5287 handle each of them individually. Those that allow register
5288 will be replaced with operand number, the others will stay
5289 unchanged. */
5290 if (strchr (p, ',') != NULL)
5292 size_t len = 0, buflen = strlen (buf);
5293 char *beg, *end, *str, *dst;
5295 for (beg = p + 1;;)
5297 end = strchr (beg, ',');
5298 if (end == NULL)
5299 end = strchr (beg, '\0');
5300 if ((size_t) (end - beg) < buflen)
5301 len += buflen + 1;
5302 else
5303 len += end - beg + 1;
5304 if (*end)
5305 beg = end + 1;
5306 else
5307 break;
5310 str = (char *) alloca (len);
5311 for (beg = p + 1, dst = str;;)
5313 const char *tem;
5314 bool mem_p, reg_p, inout_p;
5316 end = strchr (beg, ',');
5317 if (end)
5318 *end = '\0';
5319 beg[-1] = '=';
5320 tem = beg - 1;
5321 parse_output_constraint (&tem, i, 0, 0,
5322 &mem_p, &reg_p, &inout_p);
5323 if (dst != str)
5324 *dst++ = ',';
5325 if (reg_p)
5327 memcpy (dst, buf, buflen);
5328 dst += buflen;
5330 else
5332 if (end)
5333 len = end - beg;
5334 else
5335 len = strlen (beg);
5336 memcpy (dst, beg, len);
5337 dst += len;
5339 if (end)
5340 beg = end + 1;
5341 else
5342 break;
5344 *dst = '\0';
5345 input = build_string (dst - str, str);
5347 else
5348 input = build_string (strlen (buf), buf);
5350 else
5351 input = build_string (constraint_len - 1, constraint + 1);
5353 free (p);
5355 input = build_tree_list (build_tree_list (NULL_TREE, input),
5356 unshare_expr (TREE_VALUE (link)));
5357 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5361 link_next = NULL_TREE;
5362 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5364 link_next = TREE_CHAIN (link);
5365 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5366 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5367 oconstraints, &allows_mem, &allows_reg);
5369 /* If we can't make copies, we can only accept memory. */
5370 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5372 if (allows_mem)
5373 allows_reg = 0;
5374 else
5376 error ("impossible constraint in %<asm%>");
5377 error ("non-memory input %d must stay in memory", i);
5378 return GS_ERROR;
5382 /* If the operand is a memory input, it should be an lvalue. */
5383 if (!allows_reg && allows_mem)
5385 tree inputv = TREE_VALUE (link);
5386 STRIP_NOPS (inputv);
5387 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5388 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5389 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5390 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5391 TREE_VALUE (link) = error_mark_node;
5392 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5393 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5394 mark_addressable (TREE_VALUE (link));
5395 if (tret == GS_ERROR)
5397 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5398 input_location = EXPR_LOCATION (TREE_VALUE (link));
5399 error ("memory input %d is not directly addressable", i);
5400 ret = tret;
5403 else
5405 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5406 is_gimple_asm_val, fb_rvalue);
5407 if (tret == GS_ERROR)
5408 ret = tret;
5411 TREE_CHAIN (link) = NULL_TREE;
5412 vec_safe_push (inputs, link);
5415 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5416 vec_safe_push (clobbers, link);
5418 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5419 vec_safe_push (labels, link);
5421 /* Do not add ASMs with errors to the gimple IL stream. */
5422 if (ret != GS_ERROR)
5424 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5425 inputs, outputs, clobbers, labels);
5427 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5428 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5430 gimplify_seq_add_stmt (pre_p, stmt);
5433 return ret;
5436 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5437 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5438 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5439 return to this function.
5441 FIXME should we complexify the prequeue handling instead? Or use flags
5442 for all the cleanups and let the optimizer tighten them up? The current
5443 code seems pretty fragile; it will break on a cleanup within any
5444 non-conditional nesting. But any such nesting would be broken, anyway;
5445 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5446 and continues out of it. We can do that at the RTL level, though, so
5447 having an optimizer to tighten up try/finally regions would be a Good
5448 Thing. */
5450 static enum gimplify_status
5451 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5453 gimple_stmt_iterator iter;
5454 gimple_seq body_sequence = NULL;
5456 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5458 /* We only care about the number of conditions between the innermost
5459 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5460 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5461 int old_conds = gimplify_ctxp->conditions;
5462 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5463 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5464 gimplify_ctxp->conditions = 0;
5465 gimplify_ctxp->conditional_cleanups = NULL;
5466 gimplify_ctxp->in_cleanup_point_expr = true;
5468 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5470 gimplify_ctxp->conditions = old_conds;
5471 gimplify_ctxp->conditional_cleanups = old_cleanups;
5472 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5474 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5476 gimple wce = gsi_stmt (iter);
5478 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5480 if (gsi_one_before_end_p (iter))
5482 /* Note that gsi_insert_seq_before and gsi_remove do not
5483 scan operands, unlike some other sequence mutators. */
5484 if (!gimple_wce_cleanup_eh_only (wce))
5485 gsi_insert_seq_before_without_update (&iter,
5486 gimple_wce_cleanup (wce),
5487 GSI_SAME_STMT);
5488 gsi_remove (&iter, true);
5489 break;
5491 else
5493 gimple gtry;
5494 gimple_seq seq;
5495 enum gimple_try_flags kind;
5497 if (gimple_wce_cleanup_eh_only (wce))
5498 kind = GIMPLE_TRY_CATCH;
5499 else
5500 kind = GIMPLE_TRY_FINALLY;
5501 seq = gsi_split_seq_after (iter);
5503 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5504 /* Do not use gsi_replace here, as it may scan operands.
5505 We want to do a simple structural modification only. */
5506 gsi_set_stmt (&iter, gtry);
5507 iter = gsi_start (gtry->gimple_try.eval);
5510 else
5511 gsi_next (&iter);
5514 gimplify_seq_add_seq (pre_p, body_sequence);
5515 if (temp)
5517 *expr_p = temp;
5518 return GS_OK;
5520 else
5522 *expr_p = NULL;
5523 return GS_ALL_DONE;
5527 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5528 is the cleanup action required. EH_ONLY is true if the cleanup should
5529 only be executed if an exception is thrown, not on normal exit. */
5531 static void
5532 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5534 gimple wce;
5535 gimple_seq cleanup_stmts = NULL;
5537 /* Errors can result in improperly nested cleanups. Which results in
5538 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5539 if (seen_error ())
5540 return;
5542 if (gimple_conditional_context ())
5544 /* If we're in a conditional context, this is more complex. We only
5545 want to run the cleanup if we actually ran the initialization that
5546 necessitates it, but we want to run it after the end of the
5547 conditional context. So we wrap the try/finally around the
5548 condition and use a flag to determine whether or not to actually
5549 run the destructor. Thus
5551 test ? f(A()) : 0
5553 becomes (approximately)
5555 flag = 0;
5556 try {
5557 if (test) { A::A(temp); flag = 1; val = f(temp); }
5558 else { val = 0; }
5559 } finally {
5560 if (flag) A::~A(temp);
5564 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5565 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5566 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5568 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5569 gimplify_stmt (&cleanup, &cleanup_stmts);
5570 wce = gimple_build_wce (cleanup_stmts);
5572 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5573 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5574 gimplify_seq_add_stmt (pre_p, ftrue);
5576 /* Because of this manipulation, and the EH edges that jump
5577 threading cannot redirect, the temporary (VAR) will appear
5578 to be used uninitialized. Don't warn. */
5579 TREE_NO_WARNING (var) = 1;
5581 else
5583 gimplify_stmt (&cleanup, &cleanup_stmts);
5584 wce = gimple_build_wce (cleanup_stmts);
5585 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5586 gimplify_seq_add_stmt (pre_p, wce);
5590 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5592 static enum gimplify_status
5593 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5595 tree targ = *expr_p;
5596 tree temp = TARGET_EXPR_SLOT (targ);
5597 tree init = TARGET_EXPR_INITIAL (targ);
5598 enum gimplify_status ret;
5600 if (init)
5602 tree cleanup = NULL_TREE;
5604 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5605 to the temps list. Handle also variable length TARGET_EXPRs. */
5606 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5608 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5609 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5610 gimplify_vla_decl (temp, pre_p);
5612 else
5613 gimple_add_tmp_var (temp);
5615 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5616 expression is supposed to initialize the slot. */
5617 if (VOID_TYPE_P (TREE_TYPE (init)))
5618 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5619 else
5621 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5622 init = init_expr;
5623 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5624 init = NULL;
5625 ggc_free (init_expr);
5627 if (ret == GS_ERROR)
5629 /* PR c++/28266 Make sure this is expanded only once. */
5630 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5631 return GS_ERROR;
5633 if (init)
5634 gimplify_and_add (init, pre_p);
5636 /* If needed, push the cleanup for the temp. */
5637 if (TARGET_EXPR_CLEANUP (targ))
5639 if (CLEANUP_EH_ONLY (targ))
5640 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5641 CLEANUP_EH_ONLY (targ), pre_p);
5642 else
5643 cleanup = TARGET_EXPR_CLEANUP (targ);
5646 /* Add a clobber for the temporary going out of scope, like
5647 gimplify_bind_expr. */
5648 if (gimplify_ctxp->in_cleanup_point_expr
5649 && needs_to_live_in_memory (temp)
5650 && flag_stack_reuse == SR_ALL)
5652 tree clobber = build_constructor (TREE_TYPE (temp),
5653 NULL);
5654 TREE_THIS_VOLATILE (clobber) = true;
5655 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5656 if (cleanup)
5657 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5658 clobber);
5659 else
5660 cleanup = clobber;
5663 if (cleanup)
5664 gimple_push_cleanup (temp, cleanup, false, pre_p);
5666 /* Only expand this once. */
5667 TREE_OPERAND (targ, 3) = init;
5668 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5670 else
5671 /* We should have expanded this before. */
5672 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5674 *expr_p = temp;
5675 return GS_OK;
5678 /* Gimplification of expression trees. */
5680 /* Gimplify an expression which appears at statement context. The
5681 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5682 NULL, a new sequence is allocated.
5684 Return true if we actually added a statement to the queue. */
5686 bool
5687 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5689 gimple_seq_node last;
5691 last = gimple_seq_last (*seq_p);
5692 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5693 return last != gimple_seq_last (*seq_p);
5696 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5697 to CTX. If entries already exist, force them to be some flavor of private.
5698 If there is no enclosing parallel, do nothing. */
5700 void
5701 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5703 splay_tree_node n;
5705 if (decl == NULL || !DECL_P (decl))
5706 return;
5710 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5711 if (n != NULL)
5713 if (n->value & GOVD_SHARED)
5714 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5715 else
5716 return;
5718 else if (ctx->region_type != ORT_WORKSHARE)
5719 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5721 ctx = ctx->outer_context;
5723 while (ctx);
5726 /* Similarly for each of the type sizes of TYPE. */
5728 static void
5729 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5731 if (type == NULL || type == error_mark_node)
5732 return;
5733 type = TYPE_MAIN_VARIANT (type);
5735 if (pointer_set_insert (ctx->privatized_types, type))
5736 return;
5738 switch (TREE_CODE (type))
5740 case INTEGER_TYPE:
5741 case ENUMERAL_TYPE:
5742 case BOOLEAN_TYPE:
5743 case REAL_TYPE:
5744 case FIXED_POINT_TYPE:
5745 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5746 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5747 break;
5749 case ARRAY_TYPE:
5750 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5751 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5752 break;
5754 case RECORD_TYPE:
5755 case UNION_TYPE:
5756 case QUAL_UNION_TYPE:
5758 tree field;
5759 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5760 if (TREE_CODE (field) == FIELD_DECL)
5762 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5763 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5766 break;
5768 case POINTER_TYPE:
5769 case REFERENCE_TYPE:
5770 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5771 break;
5773 default:
5774 break;
5777 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5778 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5779 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5782 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5784 static void
5785 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5787 splay_tree_node n;
5788 unsigned int nflags;
5789 tree t;
5791 if (error_operand_p (decl))
5792 return;
5794 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5795 there are constructors involved somewhere. */
5796 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5797 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5798 flags |= GOVD_SEEN;
5800 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5801 if (n != NULL)
5803 /* We shouldn't be re-adding the decl with the same data
5804 sharing class. */
5805 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5806 /* The only combination of data sharing classes we should see is
5807 FIRSTPRIVATE and LASTPRIVATE. */
5808 nflags = n->value | flags;
5809 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5810 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5811 n->value = nflags;
5812 return;
5815 /* When adding a variable-sized variable, we have to handle all sorts
5816 of additional bits of data: the pointer replacement variable, and
5817 the parameters of the type. */
5818 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5820 /* Add the pointer replacement variable as PRIVATE if the variable
5821 replacement is private, else FIRSTPRIVATE since we'll need the
5822 address of the original variable either for SHARED, or for the
5823 copy into or out of the context. */
5824 if (!(flags & GOVD_LOCAL))
5826 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5827 nflags |= flags & GOVD_SEEN;
5828 t = DECL_VALUE_EXPR (decl);
5829 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5830 t = TREE_OPERAND (t, 0);
5831 gcc_assert (DECL_P (t));
5832 omp_add_variable (ctx, t, nflags);
5835 /* Add all of the variable and type parameters (which should have
5836 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5837 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5838 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5839 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5841 /* The variable-sized variable itself is never SHARED, only some form
5842 of PRIVATE. The sharing would take place via the pointer variable
5843 which we remapped above. */
5844 if (flags & GOVD_SHARED)
5845 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5846 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5848 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5849 alloca statement we generate for the variable, so make sure it
5850 is available. This isn't automatically needed for the SHARED
5851 case, since we won't be allocating local storage then.
5852 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5853 in this case omp_notice_variable will be called later
5854 on when it is gimplified. */
5855 else if (! (flags & GOVD_LOCAL)
5856 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5857 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5859 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5861 gcc_assert ((flags & GOVD_LOCAL) == 0);
5862 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5864 /* Similar to the direct variable sized case above, we'll need the
5865 size of references being privatized. */
5866 if ((flags & GOVD_SHARED) == 0)
5868 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5869 if (TREE_CODE (t) != INTEGER_CST)
5870 omp_notice_variable (ctx, t, true);
5874 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5877 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5878 This just prints out diagnostics about threadprivate variable uses
5879 in untied tasks. If DECL2 is non-NULL, prevent this warning
5880 on that variable. */
5882 static bool
5883 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5884 tree decl2)
5886 splay_tree_node n;
5888 if (ctx->region_type != ORT_UNTIED_TASK)
5889 return false;
5890 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5891 if (n == NULL)
5893 error ("threadprivate variable %qE used in untied task",
5894 DECL_NAME (decl));
5895 error_at (ctx->location, "enclosing task");
5896 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5898 if (decl2)
5899 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5900 return false;
5903 /* Record the fact that DECL was used within the OpenMP context CTX.
5904 IN_CODE is true when real code uses DECL, and false when we should
5905 merely emit default(none) errors. Return true if DECL is going to
5906 be remapped and thus DECL shouldn't be gimplified into its
5907 DECL_VALUE_EXPR (if any). */
5909 static bool
5910 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5912 splay_tree_node n;
5913 unsigned flags = in_code ? GOVD_SEEN : 0;
5914 bool ret = false, shared;
5916 if (error_operand_p (decl))
5917 return false;
5919 /* Threadprivate variables are predetermined. */
5920 if (is_global_var (decl))
5922 if (DECL_THREAD_LOCAL_P (decl))
5923 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5925 if (DECL_HAS_VALUE_EXPR_P (decl))
5927 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5929 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5930 return omp_notice_threadprivate_variable (ctx, decl, value);
5934 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5935 if (n == NULL)
5937 enum omp_clause_default_kind default_kind, kind;
5938 struct gimplify_omp_ctx *octx;
5940 if (ctx->region_type == ORT_WORKSHARE)
5941 goto do_outer;
5943 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5944 remapped firstprivate instead of shared. To some extent this is
5945 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5946 default_kind = ctx->default_kind;
5947 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5948 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5949 default_kind = kind;
5951 switch (default_kind)
5953 case OMP_CLAUSE_DEFAULT_NONE:
5954 error ("%qE not specified in enclosing parallel",
5955 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5956 if ((ctx->region_type & ORT_TASK) != 0)
5957 error_at (ctx->location, "enclosing task");
5958 else
5959 error_at (ctx->location, "enclosing parallel");
5960 /* FALLTHRU */
5961 case OMP_CLAUSE_DEFAULT_SHARED:
5962 flags |= GOVD_SHARED;
5963 break;
5964 case OMP_CLAUSE_DEFAULT_PRIVATE:
5965 flags |= GOVD_PRIVATE;
5966 break;
5967 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5968 flags |= GOVD_FIRSTPRIVATE;
5969 break;
5970 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5971 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5972 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5973 if (ctx->outer_context)
5974 omp_notice_variable (ctx->outer_context, decl, in_code);
5975 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5977 splay_tree_node n2;
5979 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5980 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5982 flags |= GOVD_FIRSTPRIVATE;
5983 break;
5985 if ((octx->region_type & ORT_PARALLEL) != 0)
5986 break;
5988 if (flags & GOVD_FIRSTPRIVATE)
5989 break;
5990 if (octx == NULL
5991 && (TREE_CODE (decl) == PARM_DECL
5992 || (!is_global_var (decl)
5993 && DECL_CONTEXT (decl) == current_function_decl)))
5995 flags |= GOVD_FIRSTPRIVATE;
5996 break;
5998 flags |= GOVD_SHARED;
5999 break;
6000 default:
6001 gcc_unreachable ();
6004 if ((flags & GOVD_PRIVATE)
6005 && lang_hooks.decls.omp_private_outer_ref (decl))
6006 flags |= GOVD_PRIVATE_OUTER_REF;
6008 omp_add_variable (ctx, decl, flags);
6010 shared = (flags & GOVD_SHARED) != 0;
6011 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6012 goto do_outer;
6015 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6016 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6017 && DECL_SIZE (decl)
6018 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6020 splay_tree_node n2;
6021 tree t = DECL_VALUE_EXPR (decl);
6022 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6023 t = TREE_OPERAND (t, 0);
6024 gcc_assert (DECL_P (t));
6025 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6026 n2->value |= GOVD_SEEN;
6029 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6030 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6032 /* If nothing changed, there's nothing left to do. */
6033 if ((n->value & flags) == flags)
6034 return ret;
6035 flags |= n->value;
6036 n->value = flags;
6038 do_outer:
6039 /* If the variable is private in the current context, then we don't
6040 need to propagate anything to an outer context. */
6041 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6042 return ret;
6043 if (ctx->outer_context
6044 && omp_notice_variable (ctx->outer_context, decl, in_code))
6045 return true;
6046 return ret;
6049 /* Verify that DECL is private within CTX. If there's specific information
6050 to the contrary in the innermost scope, generate an error. */
6052 static bool
6053 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
6055 splay_tree_node n;
6057 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6058 if (n != NULL)
6060 if (n->value & GOVD_SHARED)
6062 if (ctx == gimplify_omp_ctxp)
6064 error ("iteration variable %qE should be private",
6065 DECL_NAME (decl));
6066 n->value = GOVD_PRIVATE;
6067 return true;
6069 else
6070 return false;
6072 else if ((n->value & GOVD_EXPLICIT) != 0
6073 && (ctx == gimplify_omp_ctxp
6074 || (ctx->region_type == ORT_COMBINED_PARALLEL
6075 && gimplify_omp_ctxp->outer_context == ctx)))
6077 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6078 error ("iteration variable %qE should not be firstprivate",
6079 DECL_NAME (decl));
6080 else if ((n->value & GOVD_REDUCTION) != 0)
6081 error ("iteration variable %qE should not be reduction",
6082 DECL_NAME (decl));
6084 return (ctx == gimplify_omp_ctxp
6085 || (ctx->region_type == ORT_COMBINED_PARALLEL
6086 && gimplify_omp_ctxp->outer_context == ctx));
6089 if (ctx->region_type != ORT_WORKSHARE)
6090 return false;
6091 else if (ctx->outer_context)
6092 return omp_is_private (ctx->outer_context, decl);
6093 return false;
6096 /* Return true if DECL is private within a parallel region
6097 that binds to the current construct's context or in parallel
6098 region's REDUCTION clause. */
6100 static bool
6101 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6103 splay_tree_node n;
6107 ctx = ctx->outer_context;
6108 if (ctx == NULL)
6109 return !(is_global_var (decl)
6110 /* References might be private, but might be shared too. */
6111 || lang_hooks.decls.omp_privatize_by_reference (decl));
6113 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6114 if (n != NULL)
6115 return (n->value & GOVD_SHARED) == 0;
6117 while (ctx->region_type == ORT_WORKSHARE);
6118 return false;
6121 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6122 and previous omp contexts. */
6124 static void
6125 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6126 enum omp_region_type region_type)
6128 struct gimplify_omp_ctx *ctx, *outer_ctx;
6129 struct gimplify_ctx gctx;
6130 tree c;
6132 ctx = new_omp_context (region_type);
6133 outer_ctx = ctx->outer_context;
6135 while ((c = *list_p) != NULL)
6137 bool remove = false;
6138 bool notice_outer = true;
6139 const char *check_non_private = NULL;
6140 unsigned int flags;
6141 tree decl;
6143 switch (OMP_CLAUSE_CODE (c))
6145 case OMP_CLAUSE_PRIVATE:
6146 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6147 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6149 flags |= GOVD_PRIVATE_OUTER_REF;
6150 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6152 else
6153 notice_outer = false;
6154 goto do_add;
6155 case OMP_CLAUSE_SHARED:
6156 flags = GOVD_SHARED | GOVD_EXPLICIT;
6157 goto do_add;
6158 case OMP_CLAUSE_FIRSTPRIVATE:
6159 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6160 check_non_private = "firstprivate";
6161 goto do_add;
6162 case OMP_CLAUSE_LASTPRIVATE:
6163 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6164 check_non_private = "lastprivate";
6165 goto do_add;
6166 case OMP_CLAUSE_REDUCTION:
6167 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6168 check_non_private = "reduction";
6169 goto do_add;
6171 do_add:
6172 decl = OMP_CLAUSE_DECL (c);
6173 if (error_operand_p (decl))
6175 remove = true;
6176 break;
6178 omp_add_variable (ctx, decl, flags);
6179 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6180 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6182 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6183 GOVD_LOCAL | GOVD_SEEN);
6184 gimplify_omp_ctxp = ctx;
6185 push_gimplify_context (&gctx);
6187 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6188 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6190 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6191 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6192 pop_gimplify_context
6193 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6194 push_gimplify_context (&gctx);
6195 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6196 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6197 pop_gimplify_context
6198 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6199 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6200 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6202 gimplify_omp_ctxp = outer_ctx;
6204 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6205 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6207 gimplify_omp_ctxp = ctx;
6208 push_gimplify_context (&gctx);
6209 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6211 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6212 NULL, NULL);
6213 TREE_SIDE_EFFECTS (bind) = 1;
6214 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6215 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6217 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6218 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6219 pop_gimplify_context
6220 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6221 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6223 gimplify_omp_ctxp = outer_ctx;
6225 if (notice_outer)
6226 goto do_notice;
6227 break;
6229 case OMP_CLAUSE_COPYIN:
6230 case OMP_CLAUSE_COPYPRIVATE:
6231 decl = OMP_CLAUSE_DECL (c);
6232 if (error_operand_p (decl))
6234 remove = true;
6235 break;
6237 do_notice:
6238 if (outer_ctx)
6239 omp_notice_variable (outer_ctx, decl, true);
6240 if (check_non_private
6241 && region_type == ORT_WORKSHARE
6242 && omp_check_private (ctx, decl))
6244 error ("%s variable %qE is private in outer context",
6245 check_non_private, DECL_NAME (decl));
6246 remove = true;
6248 break;
6250 case OMP_CLAUSE_FINAL:
6251 case OMP_CLAUSE_IF:
6252 OMP_CLAUSE_OPERAND (c, 0)
6253 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6254 /* Fall through. */
6256 case OMP_CLAUSE_SCHEDULE:
6257 case OMP_CLAUSE_NUM_THREADS:
6258 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6259 is_gimple_val, fb_rvalue) == GS_ERROR)
6260 remove = true;
6261 break;
6263 case OMP_CLAUSE_NOWAIT:
6264 case OMP_CLAUSE_ORDERED:
6265 case OMP_CLAUSE_UNTIED:
6266 case OMP_CLAUSE_COLLAPSE:
6267 case OMP_CLAUSE_MERGEABLE:
6268 break;
6270 case OMP_CLAUSE_DEFAULT:
6271 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6272 break;
6274 default:
6275 gcc_unreachable ();
6278 if (remove)
6279 *list_p = OMP_CLAUSE_CHAIN (c);
6280 else
6281 list_p = &OMP_CLAUSE_CHAIN (c);
6284 gimplify_omp_ctxp = ctx;
6287 /* For all variables that were not actually used within the context,
6288 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6290 static int
6291 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6293 tree *list_p = (tree *) data;
6294 tree decl = (tree) n->key;
6295 unsigned flags = n->value;
6296 enum omp_clause_code code;
6297 tree clause;
6298 bool private_debug;
6300 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6301 return 0;
6302 if ((flags & GOVD_SEEN) == 0)
6303 return 0;
6304 if (flags & GOVD_DEBUG_PRIVATE)
6306 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6307 private_debug = true;
6309 else
6310 private_debug
6311 = lang_hooks.decls.omp_private_debug_clause (decl,
6312 !!(flags & GOVD_SHARED));
6313 if (private_debug)
6314 code = OMP_CLAUSE_PRIVATE;
6315 else if (flags & GOVD_SHARED)
6317 if (is_global_var (decl))
6319 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6320 while (ctx != NULL)
6322 splay_tree_node on
6323 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6324 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6325 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6326 break;
6327 ctx = ctx->outer_context;
6329 if (ctx == NULL)
6330 return 0;
6332 code = OMP_CLAUSE_SHARED;
6334 else if (flags & GOVD_PRIVATE)
6335 code = OMP_CLAUSE_PRIVATE;
6336 else if (flags & GOVD_FIRSTPRIVATE)
6337 code = OMP_CLAUSE_FIRSTPRIVATE;
6338 else
6339 gcc_unreachable ();
6341 clause = build_omp_clause (input_location, code);
6342 OMP_CLAUSE_DECL (clause) = decl;
6343 OMP_CLAUSE_CHAIN (clause) = *list_p;
6344 if (private_debug)
6345 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6346 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6347 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6348 *list_p = clause;
6349 lang_hooks.decls.omp_finish_clause (clause);
6351 return 0;
6354 static void
6355 gimplify_adjust_omp_clauses (tree *list_p)
6357 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6358 tree c, decl;
6360 while ((c = *list_p) != NULL)
6362 splay_tree_node n;
6363 bool remove = false;
6365 switch (OMP_CLAUSE_CODE (c))
6367 case OMP_CLAUSE_PRIVATE:
6368 case OMP_CLAUSE_SHARED:
6369 case OMP_CLAUSE_FIRSTPRIVATE:
6370 decl = OMP_CLAUSE_DECL (c);
6371 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6372 remove = !(n->value & GOVD_SEEN);
6373 if (! remove)
6375 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6376 if ((n->value & GOVD_DEBUG_PRIVATE)
6377 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6379 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6380 || ((n->value & GOVD_DATA_SHARE_CLASS)
6381 == GOVD_PRIVATE));
6382 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6383 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6386 break;
6388 case OMP_CLAUSE_LASTPRIVATE:
6389 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6390 accurately reflect the presence of a FIRSTPRIVATE clause. */
6391 decl = OMP_CLAUSE_DECL (c);
6392 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6393 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6394 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6395 break;
6397 case OMP_CLAUSE_REDUCTION:
6398 case OMP_CLAUSE_COPYIN:
6399 case OMP_CLAUSE_COPYPRIVATE:
6400 case OMP_CLAUSE_IF:
6401 case OMP_CLAUSE_NUM_THREADS:
6402 case OMP_CLAUSE_SCHEDULE:
6403 case OMP_CLAUSE_NOWAIT:
6404 case OMP_CLAUSE_ORDERED:
6405 case OMP_CLAUSE_DEFAULT:
6406 case OMP_CLAUSE_UNTIED:
6407 case OMP_CLAUSE_COLLAPSE:
6408 case OMP_CLAUSE_FINAL:
6409 case OMP_CLAUSE_MERGEABLE:
6410 break;
6412 default:
6413 gcc_unreachable ();
6416 if (remove)
6417 *list_p = OMP_CLAUSE_CHAIN (c);
6418 else
6419 list_p = &OMP_CLAUSE_CHAIN (c);
6422 /* Add in any implicit data sharing. */
6423 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6425 gimplify_omp_ctxp = ctx->outer_context;
6426 delete_omp_context (ctx);
6429 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6430 gimplification of the body, as well as scanning the body for used
6431 variables. We need to do this scan now, because variable-sized
6432 decls will be decomposed during gimplification. */
6434 static void
6435 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6437 tree expr = *expr_p;
6438 gimple g;
6439 gimple_seq body = NULL;
6440 struct gimplify_ctx gctx;
6442 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6443 OMP_PARALLEL_COMBINED (expr)
6444 ? ORT_COMBINED_PARALLEL
6445 : ORT_PARALLEL);
6447 push_gimplify_context (&gctx);
6449 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6450 if (gimple_code (g) == GIMPLE_BIND)
6451 pop_gimplify_context (g);
6452 else
6453 pop_gimplify_context (NULL);
6455 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6457 g = gimple_build_omp_parallel (body,
6458 OMP_PARALLEL_CLAUSES (expr),
6459 NULL_TREE, NULL_TREE);
6460 if (OMP_PARALLEL_COMBINED (expr))
6461 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6462 gimplify_seq_add_stmt (pre_p, g);
6463 *expr_p = NULL_TREE;
6466 /* Gimplify the contents of an OMP_TASK statement. This involves
6467 gimplification of the body, as well as scanning the body for used
6468 variables. We need to do this scan now, because variable-sized
6469 decls will be decomposed during gimplification. */
6471 static void
6472 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6474 tree expr = *expr_p;
6475 gimple g;
6476 gimple_seq body = NULL;
6477 struct gimplify_ctx gctx;
6479 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6480 find_omp_clause (OMP_TASK_CLAUSES (expr),
6481 OMP_CLAUSE_UNTIED)
6482 ? ORT_UNTIED_TASK : ORT_TASK);
6484 push_gimplify_context (&gctx);
6486 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6487 if (gimple_code (g) == GIMPLE_BIND)
6488 pop_gimplify_context (g);
6489 else
6490 pop_gimplify_context (NULL);
6492 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6494 g = gimple_build_omp_task (body,
6495 OMP_TASK_CLAUSES (expr),
6496 NULL_TREE, NULL_TREE,
6497 NULL_TREE, NULL_TREE, NULL_TREE);
6498 gimplify_seq_add_stmt (pre_p, g);
6499 *expr_p = NULL_TREE;
6502 /* Gimplify the gross structure of an OMP_FOR statement. */
6504 static enum gimplify_status
6505 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6507 tree for_stmt, decl, var, t;
6508 enum gimplify_status ret = GS_ALL_DONE;
6509 enum gimplify_status tret;
6510 gimple gfor;
6511 gimple_seq for_body, for_pre_body;
6512 int i;
6514 for_stmt = *expr_p;
6516 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6517 ORT_WORKSHARE);
6519 /* Handle OMP_FOR_INIT. */
6520 for_pre_body = NULL;
6521 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6522 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6524 for_body = NULL;
6525 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6526 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6527 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6528 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6529 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6531 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6532 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6533 decl = TREE_OPERAND (t, 0);
6534 gcc_assert (DECL_P (decl));
6535 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6536 || POINTER_TYPE_P (TREE_TYPE (decl)));
6538 /* Make sure the iteration variable is private. */
6539 if (omp_is_private (gimplify_omp_ctxp, decl))
6540 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6541 else
6542 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6544 /* If DECL is not a gimple register, create a temporary variable to act
6545 as an iteration counter. This is valid, since DECL cannot be
6546 modified in the body of the loop. */
6547 if (!is_gimple_reg (decl))
6549 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6550 TREE_OPERAND (t, 0) = var;
6552 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6554 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6556 else
6557 var = decl;
6559 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6560 is_gimple_val, fb_rvalue);
6561 ret = MIN (ret, tret);
6562 if (ret == GS_ERROR)
6563 return ret;
6565 /* Handle OMP_FOR_COND. */
6566 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6567 gcc_assert (COMPARISON_CLASS_P (t));
6568 gcc_assert (TREE_OPERAND (t, 0) == decl);
6570 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6571 is_gimple_val, fb_rvalue);
6572 ret = MIN (ret, tret);
6574 /* Handle OMP_FOR_INCR. */
6575 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6576 switch (TREE_CODE (t))
6578 case PREINCREMENT_EXPR:
6579 case POSTINCREMENT_EXPR:
6580 t = build_int_cst (TREE_TYPE (decl), 1);
6581 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6582 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6583 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6584 break;
6586 case PREDECREMENT_EXPR:
6587 case POSTDECREMENT_EXPR:
6588 t = build_int_cst (TREE_TYPE (decl), -1);
6589 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6590 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6591 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6592 break;
6594 case MODIFY_EXPR:
6595 gcc_assert (TREE_OPERAND (t, 0) == decl);
6596 TREE_OPERAND (t, 0) = var;
6598 t = TREE_OPERAND (t, 1);
6599 switch (TREE_CODE (t))
6601 case PLUS_EXPR:
6602 if (TREE_OPERAND (t, 1) == decl)
6604 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6605 TREE_OPERAND (t, 0) = var;
6606 break;
6609 /* Fallthru. */
6610 case MINUS_EXPR:
6611 case POINTER_PLUS_EXPR:
6612 gcc_assert (TREE_OPERAND (t, 0) == decl);
6613 TREE_OPERAND (t, 0) = var;
6614 break;
6615 default:
6616 gcc_unreachable ();
6619 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6620 is_gimple_val, fb_rvalue);
6621 ret = MIN (ret, tret);
6622 break;
6624 default:
6625 gcc_unreachable ();
6628 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6630 tree c;
6631 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6632 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6633 && OMP_CLAUSE_DECL (c) == decl
6634 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6636 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6637 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6638 gcc_assert (TREE_OPERAND (t, 0) == var);
6639 t = TREE_OPERAND (t, 1);
6640 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6641 || TREE_CODE (t) == MINUS_EXPR
6642 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6643 gcc_assert (TREE_OPERAND (t, 0) == var);
6644 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6645 TREE_OPERAND (t, 1));
6646 gimplify_assign (decl, t,
6647 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6652 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6654 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6656 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6657 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6658 for_pre_body);
6660 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6662 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6663 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6664 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6665 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6666 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6667 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6668 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6669 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6672 gimplify_seq_add_stmt (pre_p, gfor);
6673 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6676 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6677 In particular, OMP_SECTIONS and OMP_SINGLE. */
6679 static void
6680 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6682 tree expr = *expr_p;
6683 gimple stmt;
6684 gimple_seq body = NULL;
6686 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6687 gimplify_and_add (OMP_BODY (expr), &body);
6688 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6690 if (TREE_CODE (expr) == OMP_SECTIONS)
6691 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6692 else if (TREE_CODE (expr) == OMP_SINGLE)
6693 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6694 else
6695 gcc_unreachable ();
6697 gimplify_seq_add_stmt (pre_p, stmt);
6700 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6701 stabilized the lhs of the atomic operation as *ADDR. Return true if
6702 EXPR is this stabilized form. */
6704 static bool
6705 goa_lhs_expr_p (tree expr, tree addr)
6707 /* Also include casts to other type variants. The C front end is fond
6708 of adding these for e.g. volatile variables. This is like
6709 STRIP_TYPE_NOPS but includes the main variant lookup. */
6710 STRIP_USELESS_TYPE_CONVERSION (expr);
6712 if (TREE_CODE (expr) == INDIRECT_REF)
6714 expr = TREE_OPERAND (expr, 0);
6715 while (expr != addr
6716 && (CONVERT_EXPR_P (expr)
6717 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6718 && TREE_CODE (expr) == TREE_CODE (addr)
6719 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6721 expr = TREE_OPERAND (expr, 0);
6722 addr = TREE_OPERAND (addr, 0);
6724 if (expr == addr)
6725 return true;
6726 return (TREE_CODE (addr) == ADDR_EXPR
6727 && TREE_CODE (expr) == ADDR_EXPR
6728 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6730 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6731 return true;
6732 return false;
6735 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6736 expression does not involve the lhs, evaluate it into a temporary.
6737 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6738 or -1 if an error was encountered. */
6740 static int
6741 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6742 tree lhs_var)
6744 tree expr = *expr_p;
6745 int saw_lhs;
6747 if (goa_lhs_expr_p (expr, lhs_addr))
6749 *expr_p = lhs_var;
6750 return 1;
6752 if (is_gimple_val (expr))
6753 return 0;
6755 saw_lhs = 0;
6756 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6758 case tcc_binary:
6759 case tcc_comparison:
6760 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6761 lhs_var);
6762 case tcc_unary:
6763 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6764 lhs_var);
6765 break;
6766 case tcc_expression:
6767 switch (TREE_CODE (expr))
6769 case TRUTH_ANDIF_EXPR:
6770 case TRUTH_ORIF_EXPR:
6771 case TRUTH_AND_EXPR:
6772 case TRUTH_OR_EXPR:
6773 case TRUTH_XOR_EXPR:
6774 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6775 lhs_addr, lhs_var);
6776 case TRUTH_NOT_EXPR:
6777 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6778 lhs_addr, lhs_var);
6779 break;
6780 case COMPOUND_EXPR:
6781 /* Break out any preevaluations from cp_build_modify_expr. */
6782 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6783 expr = TREE_OPERAND (expr, 1))
6784 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6785 *expr_p = expr;
6786 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6787 default:
6788 break;
6790 break;
6791 default:
6792 break;
6795 if (saw_lhs == 0)
6797 enum gimplify_status gs;
6798 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6799 if (gs != GS_ALL_DONE)
6800 saw_lhs = -1;
6803 return saw_lhs;
6806 /* Gimplify an OMP_ATOMIC statement. */
6808 static enum gimplify_status
6809 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6811 tree addr = TREE_OPERAND (*expr_p, 0);
6812 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6813 ? NULL : TREE_OPERAND (*expr_p, 1);
6814 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6815 tree tmp_load;
6816 gimple loadstmt, storestmt;
6818 tmp_load = create_tmp_reg (type, NULL);
6819 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6820 return GS_ERROR;
6822 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6823 != GS_ALL_DONE)
6824 return GS_ERROR;
6826 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6827 gimplify_seq_add_stmt (pre_p, loadstmt);
6828 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6829 != GS_ALL_DONE)
6830 return GS_ERROR;
6832 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6833 rhs = tmp_load;
6834 storestmt = gimple_build_omp_atomic_store (rhs);
6835 gimplify_seq_add_stmt (pre_p, storestmt);
6836 switch (TREE_CODE (*expr_p))
6838 case OMP_ATOMIC_READ:
6839 case OMP_ATOMIC_CAPTURE_OLD:
6840 *expr_p = tmp_load;
6841 gimple_omp_atomic_set_need_value (loadstmt);
6842 break;
6843 case OMP_ATOMIC_CAPTURE_NEW:
6844 *expr_p = rhs;
6845 gimple_omp_atomic_set_need_value (storestmt);
6846 break;
6847 default:
6848 *expr_p = NULL;
6849 break;
6852 return GS_ALL_DONE;
6855 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6856 body, and adding some EH bits. */
6858 static enum gimplify_status
6859 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6861 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6862 gimple g;
6863 gimple_seq body = NULL;
6864 struct gimplify_ctx gctx;
6865 int subcode = 0;
6867 /* Wrap the transaction body in a BIND_EXPR so we have a context
6868 where to put decls for OpenMP. */
6869 if (TREE_CODE (tbody) != BIND_EXPR)
6871 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6872 TREE_SIDE_EFFECTS (bind) = 1;
6873 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6874 TRANSACTION_EXPR_BODY (expr) = bind;
6877 push_gimplify_context (&gctx);
6878 temp = voidify_wrapper_expr (*expr_p, NULL);
6880 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6881 pop_gimplify_context (g);
6883 g = gimple_build_transaction (body, NULL);
6884 if (TRANSACTION_EXPR_OUTER (expr))
6885 subcode = GTMA_IS_OUTER;
6886 else if (TRANSACTION_EXPR_RELAXED (expr))
6887 subcode = GTMA_IS_RELAXED;
6888 gimple_transaction_set_subcode (g, subcode);
6890 gimplify_seq_add_stmt (pre_p, g);
6892 if (temp)
6894 *expr_p = temp;
6895 return GS_OK;
6898 *expr_p = NULL_TREE;
6899 return GS_ALL_DONE;
6902 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6903 expression produces a value to be used as an operand inside a GIMPLE
6904 statement, the value will be stored back in *EXPR_P. This value will
6905 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6906 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6907 emitted in PRE_P and POST_P.
6909 Additionally, this process may overwrite parts of the input
6910 expression during gimplification. Ideally, it should be
6911 possible to do non-destructive gimplification.
6913 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6914 the expression needs to evaluate to a value to be used as
6915 an operand in a GIMPLE statement, this value will be stored in
6916 *EXPR_P on exit. This happens when the caller specifies one
6917 of fb_lvalue or fb_rvalue fallback flags.
6919 PRE_P will contain the sequence of GIMPLE statements corresponding
6920 to the evaluation of EXPR and all the side-effects that must
6921 be executed before the main expression. On exit, the last
6922 statement of PRE_P is the core statement being gimplified. For
6923 instance, when gimplifying 'if (++a)' the last statement in
6924 PRE_P will be 'if (t.1)' where t.1 is the result of
6925 pre-incrementing 'a'.
6927 POST_P will contain the sequence of GIMPLE statements corresponding
6928 to the evaluation of all the side-effects that must be executed
6929 after the main expression. If this is NULL, the post
6930 side-effects are stored at the end of PRE_P.
6932 The reason why the output is split in two is to handle post
6933 side-effects explicitly. In some cases, an expression may have
6934 inner and outer post side-effects which need to be emitted in
6935 an order different from the one given by the recursive
6936 traversal. For instance, for the expression (*p--)++ the post
6937 side-effects of '--' must actually occur *after* the post
6938 side-effects of '++'. However, gimplification will first visit
6939 the inner expression, so if a separate POST sequence was not
6940 used, the resulting sequence would be:
6942 1 t.1 = *p
6943 2 p = p - 1
6944 3 t.2 = t.1 + 1
6945 4 *p = t.2
6947 However, the post-decrement operation in line #2 must not be
6948 evaluated until after the store to *p at line #4, so the
6949 correct sequence should be:
6951 1 t.1 = *p
6952 2 t.2 = t.1 + 1
6953 3 *p = t.2
6954 4 p = p - 1
6956 So, by specifying a separate post queue, it is possible
6957 to emit the post side-effects in the correct order.
6958 If POST_P is NULL, an internal queue will be used. Before
6959 returning to the caller, the sequence POST_P is appended to
6960 the main output sequence PRE_P.
6962 GIMPLE_TEST_F points to a function that takes a tree T and
6963 returns nonzero if T is in the GIMPLE form requested by the
6964 caller. The GIMPLE predicates are in gimple.c.
6966 FALLBACK tells the function what sort of a temporary we want if
6967 gimplification cannot produce an expression that complies with
6968 GIMPLE_TEST_F.
6970 fb_none means that no temporary should be generated
6971 fb_rvalue means that an rvalue is OK to generate
6972 fb_lvalue means that an lvalue is OK to generate
6973 fb_either means that either is OK, but an lvalue is preferable.
6974 fb_mayfail means that gimplification may fail (in which case
6975 GS_ERROR will be returned)
6977 The return value is either GS_ERROR or GS_ALL_DONE, since this
6978 function iterates until EXPR is completely gimplified or an error
6979 occurs. */
6981 enum gimplify_status
6982 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6983 bool (*gimple_test_f) (tree), fallback_t fallback)
6985 tree tmp;
6986 gimple_seq internal_pre = NULL;
6987 gimple_seq internal_post = NULL;
6988 tree save_expr;
6989 bool is_statement;
6990 location_t saved_location;
6991 enum gimplify_status ret;
6992 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6994 save_expr = *expr_p;
6995 if (save_expr == NULL_TREE)
6996 return GS_ALL_DONE;
6998 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6999 is_statement = gimple_test_f == is_gimple_stmt;
7000 if (is_statement)
7001 gcc_assert (pre_p);
7003 /* Consistency checks. */
7004 if (gimple_test_f == is_gimple_reg)
7005 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7006 else if (gimple_test_f == is_gimple_val
7007 || gimple_test_f == is_gimple_call_addr
7008 || gimple_test_f == is_gimple_condexpr
7009 || gimple_test_f == is_gimple_mem_rhs
7010 || gimple_test_f == is_gimple_mem_rhs_or_call
7011 || gimple_test_f == is_gimple_reg_rhs
7012 || gimple_test_f == is_gimple_reg_rhs_or_call
7013 || gimple_test_f == is_gimple_asm_val
7014 || gimple_test_f == is_gimple_mem_ref_addr)
7015 gcc_assert (fallback & fb_rvalue);
7016 else if (gimple_test_f == is_gimple_min_lval
7017 || gimple_test_f == is_gimple_lvalue)
7018 gcc_assert (fallback & fb_lvalue);
7019 else if (gimple_test_f == is_gimple_addressable)
7020 gcc_assert (fallback & fb_either);
7021 else if (gimple_test_f == is_gimple_stmt)
7022 gcc_assert (fallback == fb_none);
7023 else
7025 /* We should have recognized the GIMPLE_TEST_F predicate to
7026 know what kind of fallback to use in case a temporary is
7027 needed to hold the value or address of *EXPR_P. */
7028 gcc_unreachable ();
7031 /* We used to check the predicate here and return immediately if it
7032 succeeds. This is wrong; the design is for gimplification to be
7033 idempotent, and for the predicates to only test for valid forms, not
7034 whether they are fully simplified. */
7035 if (pre_p == NULL)
7036 pre_p = &internal_pre;
7038 if (post_p == NULL)
7039 post_p = &internal_post;
7041 /* Remember the last statements added to PRE_P and POST_P. Every
7042 new statement added by the gimplification helpers needs to be
7043 annotated with location information. To centralize the
7044 responsibility, we remember the last statement that had been
7045 added to both queues before gimplifying *EXPR_P. If
7046 gimplification produces new statements in PRE_P and POST_P, those
7047 statements will be annotated with the same location information
7048 as *EXPR_P. */
7049 pre_last_gsi = gsi_last (*pre_p);
7050 post_last_gsi = gsi_last (*post_p);
7052 saved_location = input_location;
7053 if (save_expr != error_mark_node
7054 && EXPR_HAS_LOCATION (*expr_p))
7055 input_location = EXPR_LOCATION (*expr_p);
7057 /* Loop over the specific gimplifiers until the toplevel node
7058 remains the same. */
7061 /* Strip away as many useless type conversions as possible
7062 at the toplevel. */
7063 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7065 /* Remember the expr. */
7066 save_expr = *expr_p;
7068 /* Die, die, die, my darling. */
7069 if (save_expr == error_mark_node
7070 || (TREE_TYPE (save_expr)
7071 && TREE_TYPE (save_expr) == error_mark_node))
7073 ret = GS_ERROR;
7074 break;
7077 /* Do any language-specific gimplification. */
7078 ret = ((enum gimplify_status)
7079 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7080 if (ret == GS_OK)
7082 if (*expr_p == NULL_TREE)
7083 break;
7084 if (*expr_p != save_expr)
7085 continue;
7087 else if (ret != GS_UNHANDLED)
7088 break;
7090 /* Make sure that all the cases set 'ret' appropriately. */
7091 ret = GS_UNHANDLED;
7092 switch (TREE_CODE (*expr_p))
7094 /* First deal with the special cases. */
7096 case POSTINCREMENT_EXPR:
7097 case POSTDECREMENT_EXPR:
7098 case PREINCREMENT_EXPR:
7099 case PREDECREMENT_EXPR:
7100 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7101 fallback != fb_none,
7102 TREE_TYPE (*expr_p));
7103 break;
7105 case ARRAY_REF:
7106 case ARRAY_RANGE_REF:
7107 case REALPART_EXPR:
7108 case IMAGPART_EXPR:
7109 case COMPONENT_REF:
7110 case VIEW_CONVERT_EXPR:
7111 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7112 fallback ? fallback : fb_rvalue);
7113 break;
7115 case COND_EXPR:
7116 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7118 /* C99 code may assign to an array in a structure value of a
7119 conditional expression, and this has undefined behavior
7120 only on execution, so create a temporary if an lvalue is
7121 required. */
7122 if (fallback == fb_lvalue)
7124 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7125 mark_addressable (*expr_p);
7126 ret = GS_OK;
7128 break;
7130 case CALL_EXPR:
7131 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7133 /* C99 code may assign to an array in a structure returned
7134 from a function, and this has undefined behavior only on
7135 execution, so create a temporary if an lvalue is
7136 required. */
7137 if (fallback == fb_lvalue)
7139 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7140 mark_addressable (*expr_p);
7141 ret = GS_OK;
7143 break;
7145 case TREE_LIST:
7146 gcc_unreachable ();
7148 case COMPOUND_EXPR:
7149 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7150 break;
7152 case COMPOUND_LITERAL_EXPR:
7153 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7154 gimple_test_f, fallback);
7155 break;
7157 case MODIFY_EXPR:
7158 case INIT_EXPR:
7159 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7160 fallback != fb_none);
7161 break;
7163 case TRUTH_ANDIF_EXPR:
7164 case TRUTH_ORIF_EXPR:
7166 /* Preserve the original type of the expression and the
7167 source location of the outer expression. */
7168 tree org_type = TREE_TYPE (*expr_p);
7169 *expr_p = gimple_boolify (*expr_p);
7170 *expr_p = build3_loc (input_location, COND_EXPR,
7171 org_type, *expr_p,
7172 fold_convert_loc
7173 (input_location,
7174 org_type, boolean_true_node),
7175 fold_convert_loc
7176 (input_location,
7177 org_type, boolean_false_node));
7178 ret = GS_OK;
7179 break;
7182 case TRUTH_NOT_EXPR:
7184 tree type = TREE_TYPE (*expr_p);
7185 /* The parsers are careful to generate TRUTH_NOT_EXPR
7186 only with operands that are always zero or one.
7187 We do not fold here but handle the only interesting case
7188 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7189 *expr_p = gimple_boolify (*expr_p);
7190 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7191 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7192 TREE_TYPE (*expr_p),
7193 TREE_OPERAND (*expr_p, 0));
7194 else
7195 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7196 TREE_TYPE (*expr_p),
7197 TREE_OPERAND (*expr_p, 0),
7198 build_int_cst (TREE_TYPE (*expr_p), 1));
7199 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7200 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7201 ret = GS_OK;
7202 break;
7205 case ADDR_EXPR:
7206 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7207 break;
7209 case VA_ARG_EXPR:
7210 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7211 break;
7213 CASE_CONVERT:
7214 if (IS_EMPTY_STMT (*expr_p))
7216 ret = GS_ALL_DONE;
7217 break;
7220 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7221 || fallback == fb_none)
7223 /* Just strip a conversion to void (or in void context) and
7224 try again. */
7225 *expr_p = TREE_OPERAND (*expr_p, 0);
7226 ret = GS_OK;
7227 break;
7230 ret = gimplify_conversion (expr_p);
7231 if (ret == GS_ERROR)
7232 break;
7233 if (*expr_p != save_expr)
7234 break;
7235 /* FALLTHRU */
7237 case FIX_TRUNC_EXPR:
7238 /* unary_expr: ... | '(' cast ')' val | ... */
7239 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7240 is_gimple_val, fb_rvalue);
7241 recalculate_side_effects (*expr_p);
7242 break;
7244 case INDIRECT_REF:
7246 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7247 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7248 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7250 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7251 if (*expr_p != save_expr)
7253 ret = GS_OK;
7254 break;
7257 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7258 is_gimple_reg, fb_rvalue);
7259 if (ret == GS_ERROR)
7260 break;
7262 recalculate_side_effects (*expr_p);
7263 *expr_p = fold_build2_loc (input_location, MEM_REF,
7264 TREE_TYPE (*expr_p),
7265 TREE_OPERAND (*expr_p, 0),
7266 build_int_cst (saved_ptr_type, 0));
7267 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7268 TREE_THIS_NOTRAP (*expr_p) = notrap;
7269 ret = GS_OK;
7270 break;
7273 /* We arrive here through the various re-gimplifcation paths. */
7274 case MEM_REF:
7275 /* First try re-folding the whole thing. */
7276 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7277 TREE_OPERAND (*expr_p, 0),
7278 TREE_OPERAND (*expr_p, 1));
7279 if (tmp)
7281 *expr_p = tmp;
7282 recalculate_side_effects (*expr_p);
7283 ret = GS_OK;
7284 break;
7286 /* Avoid re-gimplifying the address operand if it is already
7287 in suitable form. Re-gimplifying would mark the address
7288 operand addressable. Always gimplify when not in SSA form
7289 as we still may have to gimplify decls with value-exprs. */
7290 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7291 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7293 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7294 is_gimple_mem_ref_addr, fb_rvalue);
7295 if (ret == GS_ERROR)
7296 break;
7298 recalculate_side_effects (*expr_p);
7299 ret = GS_ALL_DONE;
7300 break;
7302 /* Constants need not be gimplified. */
7303 case INTEGER_CST:
7304 case REAL_CST:
7305 case FIXED_CST:
7306 case STRING_CST:
7307 case COMPLEX_CST:
7308 case VECTOR_CST:
7309 ret = GS_ALL_DONE;
7310 break;
7312 case CONST_DECL:
7313 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7314 CONST_DECL node. Otherwise the decl is replaceable by its
7315 value. */
7316 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7317 if (fallback & fb_lvalue)
7318 ret = GS_ALL_DONE;
7319 else
7321 *expr_p = DECL_INITIAL (*expr_p);
7322 ret = GS_OK;
7324 break;
7326 case DECL_EXPR:
7327 ret = gimplify_decl_expr (expr_p, pre_p);
7328 break;
7330 case BIND_EXPR:
7331 ret = gimplify_bind_expr (expr_p, pre_p);
7332 break;
7334 case LOOP_EXPR:
7335 ret = gimplify_loop_expr (expr_p, pre_p);
7336 break;
7338 case SWITCH_EXPR:
7339 ret = gimplify_switch_expr (expr_p, pre_p);
7340 break;
7342 case EXIT_EXPR:
7343 ret = gimplify_exit_expr (expr_p);
7344 break;
7346 case GOTO_EXPR:
7347 /* If the target is not LABEL, then it is a computed jump
7348 and the target needs to be gimplified. */
7349 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7351 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7352 NULL, is_gimple_val, fb_rvalue);
7353 if (ret == GS_ERROR)
7354 break;
7356 gimplify_seq_add_stmt (pre_p,
7357 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7358 ret = GS_ALL_DONE;
7359 break;
7361 case PREDICT_EXPR:
7362 gimplify_seq_add_stmt (pre_p,
7363 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7364 PREDICT_EXPR_OUTCOME (*expr_p)));
7365 ret = GS_ALL_DONE;
7366 break;
7368 case LABEL_EXPR:
7369 ret = GS_ALL_DONE;
7370 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7371 == current_function_decl);
7372 gimplify_seq_add_stmt (pre_p,
7373 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7374 break;
7376 case CASE_LABEL_EXPR:
7377 ret = gimplify_case_label_expr (expr_p, pre_p);
7378 break;
7380 case RETURN_EXPR:
7381 ret = gimplify_return_expr (*expr_p, pre_p);
7382 break;
7384 case CONSTRUCTOR:
7385 /* Don't reduce this in place; let gimplify_init_constructor work its
7386 magic. Buf if we're just elaborating this for side effects, just
7387 gimplify any element that has side-effects. */
7388 if (fallback == fb_none)
7390 unsigned HOST_WIDE_INT ix;
7391 tree val;
7392 tree temp = NULL_TREE;
7393 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7394 if (TREE_SIDE_EFFECTS (val))
7395 append_to_statement_list (val, &temp);
7397 *expr_p = temp;
7398 ret = temp ? GS_OK : GS_ALL_DONE;
7400 /* C99 code may assign to an array in a constructed
7401 structure or union, and this has undefined behavior only
7402 on execution, so create a temporary if an lvalue is
7403 required. */
7404 else if (fallback == fb_lvalue)
7406 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7407 mark_addressable (*expr_p);
7408 ret = GS_OK;
7410 else
7411 ret = GS_ALL_DONE;
7412 break;
7414 /* The following are special cases that are not handled by the
7415 original GIMPLE grammar. */
7417 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7418 eliminated. */
7419 case SAVE_EXPR:
7420 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7421 break;
7423 case BIT_FIELD_REF:
7424 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7425 post_p, is_gimple_lvalue, fb_either);
7426 recalculate_side_effects (*expr_p);
7427 break;
7429 case TARGET_MEM_REF:
7431 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7433 if (TMR_BASE (*expr_p))
7434 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7435 post_p, is_gimple_mem_ref_addr, fb_either);
7436 if (TMR_INDEX (*expr_p))
7437 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7438 post_p, is_gimple_val, fb_rvalue);
7439 if (TMR_INDEX2 (*expr_p))
7440 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7441 post_p, is_gimple_val, fb_rvalue);
7442 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7443 ret = MIN (r0, r1);
7445 break;
7447 case NON_LVALUE_EXPR:
7448 /* This should have been stripped above. */
7449 gcc_unreachable ();
7451 case ASM_EXPR:
7452 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7453 break;
7455 case TRY_FINALLY_EXPR:
7456 case TRY_CATCH_EXPR:
7458 gimple_seq eval, cleanup;
7459 gimple try_;
7461 /* Calls to destructors are generated automatically in FINALLY/CATCH
7462 block. They should have location as UNKNOWN_LOCATION. However,
7463 gimplify_call_expr will reset these call stmts to input_location
7464 if it finds stmt's location is unknown. To prevent resetting for
7465 destructors, we set the input_location to unknown.
7466 Note that this only affects the destructor calls in FINALLY/CATCH
7467 block, and will automatically reset to its original value by the
7468 end of gimplify_expr. */
7469 input_location = UNKNOWN_LOCATION;
7470 eval = cleanup = NULL;
7471 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7472 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7473 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7474 if (gimple_seq_empty_p (cleanup))
7476 gimple_seq_add_seq (pre_p, eval);
7477 ret = GS_ALL_DONE;
7478 break;
7480 try_ = gimple_build_try (eval, cleanup,
7481 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7482 ? GIMPLE_TRY_FINALLY
7483 : GIMPLE_TRY_CATCH);
7484 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7485 gimple_set_location (try_, saved_location);
7486 else
7487 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7488 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7489 gimple_try_set_catch_is_cleanup (try_,
7490 TRY_CATCH_IS_CLEANUP (*expr_p));
7491 gimplify_seq_add_stmt (pre_p, try_);
7492 ret = GS_ALL_DONE;
7493 break;
7496 case CLEANUP_POINT_EXPR:
7497 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7498 break;
7500 case TARGET_EXPR:
7501 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7502 break;
7504 case CATCH_EXPR:
7506 gimple c;
7507 gimple_seq handler = NULL;
7508 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7509 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7510 gimplify_seq_add_stmt (pre_p, c);
7511 ret = GS_ALL_DONE;
7512 break;
7515 case EH_FILTER_EXPR:
7517 gimple ehf;
7518 gimple_seq failure = NULL;
7520 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7521 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7522 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7523 gimplify_seq_add_stmt (pre_p, ehf);
7524 ret = GS_ALL_DONE;
7525 break;
7528 case OBJ_TYPE_REF:
7530 enum gimplify_status r0, r1;
7531 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7532 post_p, is_gimple_val, fb_rvalue);
7533 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7534 post_p, is_gimple_val, fb_rvalue);
7535 TREE_SIDE_EFFECTS (*expr_p) = 0;
7536 ret = MIN (r0, r1);
7538 break;
7540 case LABEL_DECL:
7541 /* We get here when taking the address of a label. We mark
7542 the label as "forced"; meaning it can never be removed and
7543 it is a potential target for any computed goto. */
7544 FORCED_LABEL (*expr_p) = 1;
7545 ret = GS_ALL_DONE;
7546 break;
7548 case STATEMENT_LIST:
7549 ret = gimplify_statement_list (expr_p, pre_p);
7550 break;
7552 case WITH_SIZE_EXPR:
7554 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7555 post_p == &internal_post ? NULL : post_p,
7556 gimple_test_f, fallback);
7557 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7558 is_gimple_val, fb_rvalue);
7559 ret = GS_ALL_DONE;
7561 break;
7563 case VAR_DECL:
7564 case PARM_DECL:
7565 ret = gimplify_var_or_parm_decl (expr_p);
7566 break;
7568 case RESULT_DECL:
7569 /* When within an OpenMP context, notice uses of variables. */
7570 if (gimplify_omp_ctxp)
7571 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7572 ret = GS_ALL_DONE;
7573 break;
7575 case SSA_NAME:
7576 /* Allow callbacks into the gimplifier during optimization. */
7577 ret = GS_ALL_DONE;
7578 break;
7580 case OMP_PARALLEL:
7581 gimplify_omp_parallel (expr_p, pre_p);
7582 ret = GS_ALL_DONE;
7583 break;
7585 case OMP_TASK:
7586 gimplify_omp_task (expr_p, pre_p);
7587 ret = GS_ALL_DONE;
7588 break;
7590 case OMP_FOR:
7591 ret = gimplify_omp_for (expr_p, pre_p);
7592 break;
7594 case OMP_SECTIONS:
7595 case OMP_SINGLE:
7596 gimplify_omp_workshare (expr_p, pre_p);
7597 ret = GS_ALL_DONE;
7598 break;
7600 case OMP_SECTION:
7601 case OMP_MASTER:
7602 case OMP_ORDERED:
7603 case OMP_CRITICAL:
7605 gimple_seq body = NULL;
7606 gimple g;
7608 gimplify_and_add (OMP_BODY (*expr_p), &body);
7609 switch (TREE_CODE (*expr_p))
7611 case OMP_SECTION:
7612 g = gimple_build_omp_section (body);
7613 break;
7614 case OMP_MASTER:
7615 g = gimple_build_omp_master (body);
7616 break;
7617 case OMP_ORDERED:
7618 g = gimple_build_omp_ordered (body);
7619 break;
7620 case OMP_CRITICAL:
7621 g = gimple_build_omp_critical (body,
7622 OMP_CRITICAL_NAME (*expr_p));
7623 break;
7624 default:
7625 gcc_unreachable ();
7627 gimplify_seq_add_stmt (pre_p, g);
7628 ret = GS_ALL_DONE;
7629 break;
7632 case OMP_ATOMIC:
7633 case OMP_ATOMIC_READ:
7634 case OMP_ATOMIC_CAPTURE_OLD:
7635 case OMP_ATOMIC_CAPTURE_NEW:
7636 ret = gimplify_omp_atomic (expr_p, pre_p);
7637 break;
7639 case TRANSACTION_EXPR:
7640 ret = gimplify_transaction (expr_p, pre_p);
7641 break;
7643 case TRUTH_AND_EXPR:
7644 case TRUTH_OR_EXPR:
7645 case TRUTH_XOR_EXPR:
7647 tree orig_type = TREE_TYPE (*expr_p);
7648 tree new_type, xop0, xop1;
7649 *expr_p = gimple_boolify (*expr_p);
7650 new_type = TREE_TYPE (*expr_p);
7651 if (!useless_type_conversion_p (orig_type, new_type))
7653 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7654 ret = GS_OK;
7655 break;
7658 /* Boolified binary truth expressions are semantically equivalent
7659 to bitwise binary expressions. Canonicalize them to the
7660 bitwise variant. */
7661 switch (TREE_CODE (*expr_p))
7663 case TRUTH_AND_EXPR:
7664 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7665 break;
7666 case TRUTH_OR_EXPR:
7667 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7668 break;
7669 case TRUTH_XOR_EXPR:
7670 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7671 break;
7672 default:
7673 break;
7675 /* Now make sure that operands have compatible type to
7676 expression's new_type. */
7677 xop0 = TREE_OPERAND (*expr_p, 0);
7678 xop1 = TREE_OPERAND (*expr_p, 1);
7679 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7680 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7681 new_type,
7682 xop0);
7683 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7684 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7685 new_type,
7686 xop1);
7687 /* Continue classified as tcc_binary. */
7688 goto expr_2;
7691 case FMA_EXPR:
7692 case VEC_COND_EXPR:
7693 case VEC_PERM_EXPR:
7694 /* Classified as tcc_expression. */
7695 goto expr_3;
7697 case POINTER_PLUS_EXPR:
7699 enum gimplify_status r0, r1;
7700 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7701 post_p, is_gimple_val, fb_rvalue);
7702 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7703 post_p, is_gimple_val, fb_rvalue);
7704 recalculate_side_effects (*expr_p);
7705 ret = MIN (r0, r1);
7706 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7707 after gimplifying operands - this is similar to how
7708 it would be folding all gimplified stmts on creation
7709 to have them canonicalized, which is what we eventually
7710 should do anyway. */
7711 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7712 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7714 *expr_p = build_fold_addr_expr_with_type_loc
7715 (input_location,
7716 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7717 TREE_OPERAND (*expr_p, 0),
7718 fold_convert (ptr_type_node,
7719 TREE_OPERAND (*expr_p, 1))),
7720 TREE_TYPE (*expr_p));
7721 ret = MIN (ret, GS_OK);
7723 break;
7726 default:
7727 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7729 case tcc_comparison:
7730 /* Handle comparison of objects of non scalar mode aggregates
7731 with a call to memcmp. It would be nice to only have to do
7732 this for variable-sized objects, but then we'd have to allow
7733 the same nest of reference nodes we allow for MODIFY_EXPR and
7734 that's too complex.
7736 Compare scalar mode aggregates as scalar mode values. Using
7737 memcmp for them would be very inefficient at best, and is
7738 plain wrong if bitfields are involved. */
7740 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7742 /* Vector comparisons need no boolification. */
7743 if (TREE_CODE (type) == VECTOR_TYPE)
7744 goto expr_2;
7745 else if (!AGGREGATE_TYPE_P (type))
7747 tree org_type = TREE_TYPE (*expr_p);
7748 *expr_p = gimple_boolify (*expr_p);
7749 if (!useless_type_conversion_p (org_type,
7750 TREE_TYPE (*expr_p)))
7752 *expr_p = fold_convert_loc (input_location,
7753 org_type, *expr_p);
7754 ret = GS_OK;
7756 else
7757 goto expr_2;
7759 else if (TYPE_MODE (type) != BLKmode)
7760 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7761 else
7762 ret = gimplify_variable_sized_compare (expr_p);
7764 break;
7767 /* If *EXPR_P does not need to be special-cased, handle it
7768 according to its class. */
7769 case tcc_unary:
7770 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7771 post_p, is_gimple_val, fb_rvalue);
7772 break;
7774 case tcc_binary:
7775 expr_2:
7777 enum gimplify_status r0, r1;
7779 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7780 post_p, is_gimple_val, fb_rvalue);
7781 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7782 post_p, is_gimple_val, fb_rvalue);
7784 ret = MIN (r0, r1);
7785 break;
7788 expr_3:
7790 enum gimplify_status r0, r1, r2;
7792 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7793 post_p, is_gimple_val, fb_rvalue);
7794 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7795 post_p, is_gimple_val, fb_rvalue);
7796 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7797 post_p, is_gimple_val, fb_rvalue);
7799 ret = MIN (MIN (r0, r1), r2);
7800 break;
7803 case tcc_declaration:
7804 case tcc_constant:
7805 ret = GS_ALL_DONE;
7806 goto dont_recalculate;
7808 default:
7809 gcc_unreachable ();
7812 recalculate_side_effects (*expr_p);
7814 dont_recalculate:
7815 break;
7818 gcc_assert (*expr_p || ret != GS_OK);
7820 while (ret == GS_OK);
7822 /* If we encountered an error_mark somewhere nested inside, either
7823 stub out the statement or propagate the error back out. */
7824 if (ret == GS_ERROR)
7826 if (is_statement)
7827 *expr_p = NULL;
7828 goto out;
7831 /* This was only valid as a return value from the langhook, which
7832 we handled. Make sure it doesn't escape from any other context. */
7833 gcc_assert (ret != GS_UNHANDLED);
7835 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7837 /* We aren't looking for a value, and we don't have a valid
7838 statement. If it doesn't have side-effects, throw it away. */
7839 if (!TREE_SIDE_EFFECTS (*expr_p))
7840 *expr_p = NULL;
7841 else if (!TREE_THIS_VOLATILE (*expr_p))
7843 /* This is probably a _REF that contains something nested that
7844 has side effects. Recurse through the operands to find it. */
7845 enum tree_code code = TREE_CODE (*expr_p);
7847 switch (code)
7849 case COMPONENT_REF:
7850 case REALPART_EXPR:
7851 case IMAGPART_EXPR:
7852 case VIEW_CONVERT_EXPR:
7853 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7854 gimple_test_f, fallback);
7855 break;
7857 case ARRAY_REF:
7858 case ARRAY_RANGE_REF:
7859 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7860 gimple_test_f, fallback);
7861 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7862 gimple_test_f, fallback);
7863 break;
7865 default:
7866 /* Anything else with side-effects must be converted to
7867 a valid statement before we get here. */
7868 gcc_unreachable ();
7871 *expr_p = NULL;
7873 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7874 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7876 /* Historically, the compiler has treated a bare reference
7877 to a non-BLKmode volatile lvalue as forcing a load. */
7878 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7880 /* Normally, we do not want to create a temporary for a
7881 TREE_ADDRESSABLE type because such a type should not be
7882 copied by bitwise-assignment. However, we make an
7883 exception here, as all we are doing here is ensuring that
7884 we read the bytes that make up the type. We use
7885 create_tmp_var_raw because create_tmp_var will abort when
7886 given a TREE_ADDRESSABLE type. */
7887 tree tmp = create_tmp_var_raw (type, "vol");
7888 gimple_add_tmp_var (tmp);
7889 gimplify_assign (tmp, *expr_p, pre_p);
7890 *expr_p = NULL;
7892 else
7893 /* We can't do anything useful with a volatile reference to
7894 an incomplete type, so just throw it away. Likewise for
7895 a BLKmode type, since any implicit inner load should
7896 already have been turned into an explicit one by the
7897 gimplification process. */
7898 *expr_p = NULL;
7901 /* If we are gimplifying at the statement level, we're done. Tack
7902 everything together and return. */
7903 if (fallback == fb_none || is_statement)
7905 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7906 it out for GC to reclaim it. */
7907 *expr_p = NULL_TREE;
7909 if (!gimple_seq_empty_p (internal_pre)
7910 || !gimple_seq_empty_p (internal_post))
7912 gimplify_seq_add_seq (&internal_pre, internal_post);
7913 gimplify_seq_add_seq (pre_p, internal_pre);
7916 /* The result of gimplifying *EXPR_P is going to be the last few
7917 statements in *PRE_P and *POST_P. Add location information
7918 to all the statements that were added by the gimplification
7919 helpers. */
7920 if (!gimple_seq_empty_p (*pre_p))
7921 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7923 if (!gimple_seq_empty_p (*post_p))
7924 annotate_all_with_location_after (*post_p, post_last_gsi,
7925 input_location);
7927 goto out;
7930 #ifdef ENABLE_GIMPLE_CHECKING
7931 if (*expr_p)
7933 enum tree_code code = TREE_CODE (*expr_p);
7934 /* These expressions should already be in gimple IR form. */
7935 gcc_assert (code != MODIFY_EXPR
7936 && code != ASM_EXPR
7937 && code != BIND_EXPR
7938 && code != CATCH_EXPR
7939 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7940 && code != EH_FILTER_EXPR
7941 && code != GOTO_EXPR
7942 && code != LABEL_EXPR
7943 && code != LOOP_EXPR
7944 && code != SWITCH_EXPR
7945 && code != TRY_FINALLY_EXPR
7946 && code != OMP_CRITICAL
7947 && code != OMP_FOR
7948 && code != OMP_MASTER
7949 && code != OMP_ORDERED
7950 && code != OMP_PARALLEL
7951 && code != OMP_SECTIONS
7952 && code != OMP_SECTION
7953 && code != OMP_SINGLE);
7955 #endif
7957 /* Otherwise we're gimplifying a subexpression, so the resulting
7958 value is interesting. If it's a valid operand that matches
7959 GIMPLE_TEST_F, we're done. Unless we are handling some
7960 post-effects internally; if that's the case, we need to copy into
7961 a temporary before adding the post-effects to POST_P. */
7962 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7963 goto out;
7965 /* Otherwise, we need to create a new temporary for the gimplified
7966 expression. */
7968 /* We can't return an lvalue if we have an internal postqueue. The
7969 object the lvalue refers to would (probably) be modified by the
7970 postqueue; we need to copy the value out first, which means an
7971 rvalue. */
7972 if ((fallback & fb_lvalue)
7973 && gimple_seq_empty_p (internal_post)
7974 && is_gimple_addressable (*expr_p))
7976 /* An lvalue will do. Take the address of the expression, store it
7977 in a temporary, and replace the expression with an INDIRECT_REF of
7978 that temporary. */
7979 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
7980 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7981 *expr_p = build_simple_mem_ref (tmp);
7983 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7985 /* An rvalue will do. Assign the gimplified expression into a
7986 new temporary TMP and replace the original expression with
7987 TMP. First, make sure that the expression has a type so that
7988 it can be assigned into a temporary. */
7989 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7990 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7992 else
7994 #ifdef ENABLE_GIMPLE_CHECKING
7995 if (!(fallback & fb_mayfail))
7997 fprintf (stderr, "gimplification failed:\n");
7998 print_generic_expr (stderr, *expr_p, 0);
7999 debug_tree (*expr_p);
8000 internal_error ("gimplification failed");
8002 #endif
8003 gcc_assert (fallback & fb_mayfail);
8005 /* If this is an asm statement, and the user asked for the
8006 impossible, don't die. Fail and let gimplify_asm_expr
8007 issue an error. */
8008 ret = GS_ERROR;
8009 goto out;
8012 /* Make sure the temporary matches our predicate. */
8013 gcc_assert ((*gimple_test_f) (*expr_p));
8015 if (!gimple_seq_empty_p (internal_post))
8017 annotate_all_with_location (internal_post, input_location);
8018 gimplify_seq_add_seq (pre_p, internal_post);
8021 out:
8022 input_location = saved_location;
8023 return ret;
8026 /* Look through TYPE for variable-sized objects and gimplify each such
8027 size that we find. Add to LIST_P any statements generated. */
8029 void
8030 gimplify_type_sizes (tree type, gimple_seq *list_p)
8032 tree field, t;
8034 if (type == NULL || type == error_mark_node)
8035 return;
8037 /* We first do the main variant, then copy into any other variants. */
8038 type = TYPE_MAIN_VARIANT (type);
8040 /* Avoid infinite recursion. */
8041 if (TYPE_SIZES_GIMPLIFIED (type))
8042 return;
8044 TYPE_SIZES_GIMPLIFIED (type) = 1;
8046 switch (TREE_CODE (type))
8048 case INTEGER_TYPE:
8049 case ENUMERAL_TYPE:
8050 case BOOLEAN_TYPE:
8051 case REAL_TYPE:
8052 case FIXED_POINT_TYPE:
8053 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8054 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8056 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8058 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8059 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8061 break;
8063 case ARRAY_TYPE:
8064 /* These types may not have declarations, so handle them here. */
8065 gimplify_type_sizes (TREE_TYPE (type), list_p);
8066 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8067 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8068 with assigned stack slots, for -O1+ -g they should be tracked
8069 by VTA. */
8070 if (!(TYPE_NAME (type)
8071 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8072 && DECL_IGNORED_P (TYPE_NAME (type)))
8073 && TYPE_DOMAIN (type)
8074 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8076 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8077 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8078 DECL_IGNORED_P (t) = 0;
8079 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8080 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8081 DECL_IGNORED_P (t) = 0;
8083 break;
8085 case RECORD_TYPE:
8086 case UNION_TYPE:
8087 case QUAL_UNION_TYPE:
8088 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8089 if (TREE_CODE (field) == FIELD_DECL)
8091 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8092 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8093 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8094 gimplify_type_sizes (TREE_TYPE (field), list_p);
8096 break;
8098 case POINTER_TYPE:
8099 case REFERENCE_TYPE:
8100 /* We used to recurse on the pointed-to type here, which turned out to
8101 be incorrect because its definition might refer to variables not
8102 yet initialized at this point if a forward declaration is involved.
8104 It was actually useful for anonymous pointed-to types to ensure
8105 that the sizes evaluation dominates every possible later use of the
8106 values. Restricting to such types here would be safe since there
8107 is no possible forward declaration around, but would introduce an
8108 undesirable middle-end semantic to anonymity. We then defer to
8109 front-ends the responsibility of ensuring that the sizes are
8110 evaluated both early and late enough, e.g. by attaching artificial
8111 type declarations to the tree. */
8112 break;
8114 default:
8115 break;
8118 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8119 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8121 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8123 TYPE_SIZE (t) = TYPE_SIZE (type);
8124 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8125 TYPE_SIZES_GIMPLIFIED (t) = 1;
8129 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8130 a size or position, has had all of its SAVE_EXPRs evaluated.
8131 We add any required statements to *STMT_P. */
8133 void
8134 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8136 tree expr = *expr_p;
8138 /* We don't do anything if the value isn't there, is constant, or contains
8139 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8140 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8141 will want to replace it with a new variable, but that will cause problems
8142 if this type is from outside the function. It's OK to have that here. */
8143 if (is_gimple_sizepos (expr))
8144 return;
8146 *expr_p = unshare_expr (expr);
8148 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8151 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8152 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8153 is true, also gimplify the parameters. */
8155 gimple
8156 gimplify_body (tree fndecl, bool do_parms)
8158 location_t saved_location = input_location;
8159 gimple_seq parm_stmts, seq;
8160 gimple outer_bind;
8161 struct gimplify_ctx gctx;
8162 struct cgraph_node *cgn;
8164 timevar_push (TV_TREE_GIMPLIFY);
8166 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8167 gimplification. */
8168 default_rtl_profile ();
8170 gcc_assert (gimplify_ctxp == NULL);
8171 push_gimplify_context (&gctx);
8173 /* Unshare most shared trees in the body and in that of any nested functions.
8174 It would seem we don't have to do this for nested functions because
8175 they are supposed to be output and then the outer function gimplified
8176 first, but the g++ front end doesn't always do it that way. */
8177 unshare_body (fndecl);
8178 unvisit_body (fndecl);
8180 cgn = cgraph_get_node (fndecl);
8181 if (cgn && cgn->origin)
8182 nonlocal_vlas = pointer_set_create ();
8184 /* Make sure input_location isn't set to something weird. */
8185 input_location = DECL_SOURCE_LOCATION (fndecl);
8187 /* Resolve callee-copies. This has to be done before processing
8188 the body so that DECL_VALUE_EXPR gets processed correctly. */
8189 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8191 /* Gimplify the function's body. */
8192 seq = NULL;
8193 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8194 outer_bind = gimple_seq_first_stmt (seq);
8195 if (!outer_bind)
8197 outer_bind = gimple_build_nop ();
8198 gimplify_seq_add_stmt (&seq, outer_bind);
8201 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8202 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8203 if (gimple_code (outer_bind) == GIMPLE_BIND
8204 && gimple_seq_first (seq) == gimple_seq_last (seq))
8206 else
8207 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8209 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8211 /* If we had callee-copies statements, insert them at the beginning
8212 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8213 if (!gimple_seq_empty_p (parm_stmts))
8215 tree parm;
8217 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8218 gimple_bind_set_body (outer_bind, parm_stmts);
8220 for (parm = DECL_ARGUMENTS (current_function_decl);
8221 parm; parm = DECL_CHAIN (parm))
8222 if (DECL_HAS_VALUE_EXPR_P (parm))
8224 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8225 DECL_IGNORED_P (parm) = 0;
8229 if (nonlocal_vlas)
8231 pointer_set_destroy (nonlocal_vlas);
8232 nonlocal_vlas = NULL;
8235 pop_gimplify_context (outer_bind);
8236 gcc_assert (gimplify_ctxp == NULL);
8238 #ifdef ENABLE_CHECKING
8239 if (!seen_error ())
8240 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8241 #endif
8243 timevar_pop (TV_TREE_GIMPLIFY);
8244 input_location = saved_location;
8246 return outer_bind;
8249 typedef char *char_p; /* For DEF_VEC_P. */
8251 /* Return whether we should exclude FNDECL from instrumentation. */
8253 static bool
8254 flag_instrument_functions_exclude_p (tree fndecl)
8256 vec<char_p> *v;
8258 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8259 if (v && v->length () > 0)
8261 const char *name;
8262 int i;
8263 char *s;
8265 name = lang_hooks.decl_printable_name (fndecl, 0);
8266 FOR_EACH_VEC_ELT (*v, i, s)
8267 if (strstr (name, s) != NULL)
8268 return true;
8271 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8272 if (v && v->length () > 0)
8274 const char *name;
8275 int i;
8276 char *s;
8278 name = DECL_SOURCE_FILE (fndecl);
8279 FOR_EACH_VEC_ELT (*v, i, s)
8280 if (strstr (name, s) != NULL)
8281 return true;
8284 return false;
8287 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8288 node for the function we want to gimplify.
8290 Return the sequence of GIMPLE statements corresponding to the body
8291 of FNDECL. */
8293 void
8294 gimplify_function_tree (tree fndecl)
8296 tree parm, ret;
8297 gimple_seq seq;
8298 gimple bind;
8300 gcc_assert (!gimple_body (fndecl));
8302 if (DECL_STRUCT_FUNCTION (fndecl))
8303 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8304 else
8305 push_struct_function (fndecl);
8307 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8309 /* Preliminarily mark non-addressed complex variables as eligible
8310 for promotion to gimple registers. We'll transform their uses
8311 as we find them. */
8312 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8313 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8314 && !TREE_THIS_VOLATILE (parm)
8315 && !needs_to_live_in_memory (parm))
8316 DECL_GIMPLE_REG_P (parm) = 1;
8319 ret = DECL_RESULT (fndecl);
8320 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8321 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8322 && !needs_to_live_in_memory (ret))
8323 DECL_GIMPLE_REG_P (ret) = 1;
8325 bind = gimplify_body (fndecl, true);
8327 /* The tree body of the function is no longer needed, replace it
8328 with the new GIMPLE body. */
8329 seq = NULL;
8330 gimple_seq_add_stmt (&seq, bind);
8331 gimple_set_body (fndecl, seq);
8333 /* If we're instrumenting function entry/exit, then prepend the call to
8334 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8335 catch the exit hook. */
8336 /* ??? Add some way to ignore exceptions for this TFE. */
8337 if (flag_instrument_function_entry_exit
8338 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8339 && !flag_instrument_functions_exclude_p (fndecl))
8341 tree x;
8342 gimple new_bind;
8343 gimple tf;
8344 gimple_seq cleanup = NULL, body = NULL;
8345 tree tmp_var;
8346 gimple call;
8348 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8349 call = gimple_build_call (x, 1, integer_zero_node);
8350 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8351 gimple_call_set_lhs (call, tmp_var);
8352 gimplify_seq_add_stmt (&cleanup, call);
8353 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8354 call = gimple_build_call (x, 2,
8355 build_fold_addr_expr (current_function_decl),
8356 tmp_var);
8357 gimplify_seq_add_stmt (&cleanup, call);
8358 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8360 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8361 call = gimple_build_call (x, 1, integer_zero_node);
8362 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8363 gimple_call_set_lhs (call, tmp_var);
8364 gimplify_seq_add_stmt (&body, call);
8365 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8366 call = gimple_build_call (x, 2,
8367 build_fold_addr_expr (current_function_decl),
8368 tmp_var);
8369 gimplify_seq_add_stmt (&body, call);
8370 gimplify_seq_add_stmt (&body, tf);
8371 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8372 /* Clear the block for BIND, since it is no longer directly inside
8373 the function, but within a try block. */
8374 gimple_bind_set_block (bind, NULL);
8376 /* Replace the current function body with the body
8377 wrapped in the try/finally TF. */
8378 seq = NULL;
8379 gimple_seq_add_stmt (&seq, new_bind);
8380 gimple_set_body (fndecl, seq);
8383 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8384 cfun->curr_properties = PROP_gimple_any;
8386 pop_cfun ();
8389 /* Some transformations like inlining may invalidate the GIMPLE form
8390 for operands. This function traverses all the operands in STMT and
8391 gimplifies anything that is not a valid gimple operand. Any new
8392 GIMPLE statements are inserted before *GSI_P. */
8394 void
8395 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8397 size_t i, num_ops;
8398 tree lhs;
8399 gimple_seq pre = NULL;
8400 gimple post_stmt = NULL;
8401 struct gimplify_ctx gctx;
8403 push_gimplify_context (&gctx);
8404 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8406 switch (gimple_code (stmt))
8408 case GIMPLE_COND:
8409 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8410 is_gimple_val, fb_rvalue);
8411 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8412 is_gimple_val, fb_rvalue);
8413 break;
8414 case GIMPLE_SWITCH:
8415 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8416 is_gimple_val, fb_rvalue);
8417 break;
8418 case GIMPLE_OMP_ATOMIC_LOAD:
8419 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8420 is_gimple_val, fb_rvalue);
8421 break;
8422 case GIMPLE_ASM:
8424 size_t i, noutputs = gimple_asm_noutputs (stmt);
8425 const char *constraint, **oconstraints;
8426 bool allows_mem, allows_reg, is_inout;
8428 oconstraints
8429 = (const char **) alloca ((noutputs) * sizeof (const char *));
8430 for (i = 0; i < noutputs; i++)
8432 tree op = gimple_asm_output_op (stmt, i);
8433 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8434 oconstraints[i] = constraint;
8435 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8436 &allows_reg, &is_inout);
8437 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8438 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8439 fb_lvalue | fb_mayfail);
8441 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8443 tree op = gimple_asm_input_op (stmt, i);
8444 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8445 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8446 oconstraints, &allows_mem, &allows_reg);
8447 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8448 allows_reg = 0;
8449 if (!allows_reg && allows_mem)
8450 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8451 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8452 else
8453 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8454 is_gimple_asm_val, fb_rvalue);
8457 break;
8458 default:
8459 /* NOTE: We start gimplifying operands from last to first to
8460 make sure that side-effects on the RHS of calls, assignments
8461 and ASMs are executed before the LHS. The ordering is not
8462 important for other statements. */
8463 num_ops = gimple_num_ops (stmt);
8464 for (i = num_ops; i > 0; i--)
8466 tree op = gimple_op (stmt, i - 1);
8467 if (op == NULL_TREE)
8468 continue;
8469 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8470 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8471 else if (i == 2
8472 && is_gimple_assign (stmt)
8473 && num_ops == 2
8474 && get_gimple_rhs_class (gimple_expr_code (stmt))
8475 == GIMPLE_SINGLE_RHS)
8476 gimplify_expr (&op, &pre, NULL,
8477 rhs_predicate_for (gimple_assign_lhs (stmt)),
8478 fb_rvalue);
8479 else if (i == 2 && is_gimple_call (stmt))
8481 if (TREE_CODE (op) == FUNCTION_DECL)
8482 continue;
8483 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8485 else
8486 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8487 gimple_set_op (stmt, i - 1, op);
8490 lhs = gimple_get_lhs (stmt);
8491 /* If the LHS changed it in a way that requires a simple RHS,
8492 create temporary. */
8493 if (lhs && !is_gimple_reg (lhs))
8495 bool need_temp = false;
8497 if (is_gimple_assign (stmt)
8498 && num_ops == 2
8499 && get_gimple_rhs_class (gimple_expr_code (stmt))
8500 == GIMPLE_SINGLE_RHS)
8501 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8502 rhs_predicate_for (gimple_assign_lhs (stmt)),
8503 fb_rvalue);
8504 else if (is_gimple_reg (lhs))
8506 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8508 if (is_gimple_call (stmt))
8510 i = gimple_call_flags (stmt);
8511 if ((i & ECF_LOOPING_CONST_OR_PURE)
8512 || !(i & (ECF_CONST | ECF_PURE)))
8513 need_temp = true;
8515 if (stmt_can_throw_internal (stmt))
8516 need_temp = true;
8519 else
8521 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8522 need_temp = true;
8523 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8525 if (is_gimple_call (stmt))
8527 tree fndecl = gimple_call_fndecl (stmt);
8529 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8530 && !(fndecl && DECL_RESULT (fndecl)
8531 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8532 need_temp = true;
8534 else
8535 need_temp = true;
8538 if (need_temp)
8540 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8541 if (gimple_in_ssa_p (cfun))
8542 temp = make_ssa_name (temp, NULL);
8543 gimple_set_lhs (stmt, temp);
8544 post_stmt = gimple_build_assign (lhs, temp);
8545 if (TREE_CODE (lhs) == SSA_NAME)
8546 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8549 break;
8552 if (!gimple_seq_empty_p (pre))
8553 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8554 if (post_stmt)
8555 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8557 pop_gimplify_context (NULL);
8560 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8561 the predicate that will hold for the result. If VAR is not NULL, make the
8562 base variable of the final destination be VAR if suitable. */
8564 tree
8565 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8566 gimple_predicate gimple_test_f, tree var)
8568 enum gimplify_status ret;
8569 struct gimplify_ctx gctx;
8570 location_t saved_location;
8572 *stmts = NULL;
8574 /* gimple_test_f might be more strict than is_gimple_val, make
8575 sure we pass both. Just checking gimple_test_f doesn't work
8576 because most gimple predicates do not work recursively. */
8577 if (is_gimple_val (expr)
8578 && (*gimple_test_f) (expr))
8579 return expr;
8581 push_gimplify_context (&gctx);
8582 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8583 gimplify_ctxp->allow_rhs_cond_expr = true;
8584 saved_location = input_location;
8585 input_location = UNKNOWN_LOCATION;
8587 if (var)
8589 if (gimplify_ctxp->into_ssa
8590 && is_gimple_reg (var))
8591 var = make_ssa_name (var, NULL);
8592 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8595 if (TREE_CODE (expr) != MODIFY_EXPR
8596 && TREE_TYPE (expr) == void_type_node)
8598 gimplify_and_add (expr, stmts);
8599 expr = NULL_TREE;
8601 else
8603 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8604 gcc_assert (ret != GS_ERROR);
8607 input_location = saved_location;
8608 pop_gimplify_context (NULL);
8610 return expr;
8613 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8614 force the result to be either ssa_name or an invariant, otherwise
8615 just force it to be a rhs expression. If VAR is not NULL, make the
8616 base variable of the final destination be VAR if suitable. */
8618 tree
8619 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8621 return force_gimple_operand_1 (expr, stmts,
8622 simple ? is_gimple_val : is_gimple_reg_rhs,
8623 var);
8626 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8627 and VAR. If some statements are produced, emits them at GSI.
8628 If BEFORE is true. the statements are appended before GSI, otherwise
8629 they are appended after it. M specifies the way GSI moves after
8630 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8632 tree
8633 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8634 gimple_predicate gimple_test_f,
8635 tree var, bool before,
8636 enum gsi_iterator_update m)
8638 gimple_seq stmts;
8640 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8642 if (!gimple_seq_empty_p (stmts))
8644 if (before)
8645 gsi_insert_seq_before (gsi, stmts, m);
8646 else
8647 gsi_insert_seq_after (gsi, stmts, m);
8650 return expr;
8653 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8654 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8655 otherwise just force it to be a rhs expression. If some statements are
8656 produced, emits them at GSI. If BEFORE is true, the statements are
8657 appended before GSI, otherwise they are appended after it. M specifies
8658 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8659 are the usual values). */
8661 tree
8662 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8663 bool simple_p, tree var, bool before,
8664 enum gsi_iterator_update m)
8666 return force_gimple_operand_gsi_1 (gsi, expr,
8667 simple_p
8668 ? is_gimple_val : is_gimple_reg_rhs,
8669 var, before, m);
8673 #include "gt-gimplify.h"