Implement -freuse-stack= option
[official-gcc.git] / gcc / gimplify.c
blobbd3642c8c41f37483053427ac4a80e1a0a92e622
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "tree-pretty-print.h"
33 #include "langhooks.h"
34 #include "tree-flow.h"
35 #include "cgraph.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "diagnostic-core.h"
42 #include "target.h"
43 #include "pointer-set.h"
44 #include "splay-tree.h"
45 #include "vec.h"
46 #include "gimple.h"
47 #include "tree-pass.h"
49 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name. */
50 #include "expr.h" /* FIXME: for can_move_by_pieces
51 and STACK_CHECK_MAX_VAR_SIZE. */
53 enum gimplify_omp_var_data
55 GOVD_SEEN = 1,
56 GOVD_EXPLICIT = 2,
57 GOVD_SHARED = 4,
58 GOVD_PRIVATE = 8,
59 GOVD_FIRSTPRIVATE = 16,
60 GOVD_LASTPRIVATE = 32,
61 GOVD_REDUCTION = 64,
62 GOVD_LOCAL = 128,
63 GOVD_DEBUG_PRIVATE = 256,
64 GOVD_PRIVATE_OUTER_REF = 512,
65 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
66 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
70 enum omp_region_type
72 ORT_WORKSHARE = 0,
73 ORT_PARALLEL = 2,
74 ORT_COMBINED_PARALLEL = 3,
75 ORT_TASK = 4,
76 ORT_UNTIED_TASK = 5
79 struct gimplify_omp_ctx
81 struct gimplify_omp_ctx *outer_context;
82 splay_tree variables;
83 struct pointer_set_t *privatized_types;
84 location_t location;
85 enum omp_clause_default_kind default_kind;
86 enum omp_region_type region_type;
89 static struct gimplify_ctx *gimplify_ctxp;
90 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
93 /* Formal (expression) temporary table handling: multiple occurrences of
94 the same scalar expression are evaluated into the same temporary. */
96 typedef struct gimple_temp_hash_elt
98 tree val; /* Key */
99 tree temp; /* Value */
100 } elt_t;
102 /* Forward declaration. */
103 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
105 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
106 form and we don't do any syntax checking. */
108 void
109 mark_addressable (tree x)
111 while (handled_component_p (x))
112 x = TREE_OPERAND (x, 0);
113 if (TREE_CODE (x) == MEM_REF
114 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
115 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
116 if (TREE_CODE (x) != VAR_DECL
117 && TREE_CODE (x) != PARM_DECL
118 && TREE_CODE (x) != RESULT_DECL)
119 return;
120 TREE_ADDRESSABLE (x) = 1;
123 /* Return a hash value for a formal temporary table entry. */
125 static hashval_t
126 gimple_tree_hash (const void *p)
128 tree t = ((const elt_t *) p)->val;
129 return iterative_hash_expr (t, 0);
132 /* Compare two formal temporary table entries. */
134 static int
135 gimple_tree_eq (const void *p1, const void *p2)
137 tree t1 = ((const elt_t *) p1)->val;
138 tree t2 = ((const elt_t *) p2)->val;
139 enum tree_code code = TREE_CODE (t1);
141 if (TREE_CODE (t2) != code
142 || TREE_TYPE (t1) != TREE_TYPE (t2))
143 return 0;
145 if (!operand_equal_p (t1, t2, 0))
146 return 0;
148 #ifdef ENABLE_CHECKING
149 /* Only allow them to compare equal if they also hash equal; otherwise
150 results are nondeterminate, and we fail bootstrap comparison. */
151 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
152 #endif
154 return 1;
157 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
158 *SEQ_P is NULL, a new sequence is allocated. This function is
159 similar to gimple_seq_add_stmt, but does not scan the operands.
160 During gimplification, we need to manipulate statement sequences
161 before the def/use vectors have been constructed. */
163 void
164 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
166 gimple_stmt_iterator si;
168 if (gs == NULL)
169 return;
171 si = gsi_last (*seq_p);
172 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
175 /* Shorter alias name for the above function for use in gimplify.c
176 only. */
178 static inline void
179 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
181 gimple_seq_add_stmt_without_update (seq_p, gs);
184 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
185 NULL, a new sequence is allocated. This function is
186 similar to gimple_seq_add_seq, but does not scan the operands.
187 During gimplification, we need to manipulate statement sequences
188 before the def/use vectors have been constructed. */
190 static void
191 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
193 gimple_stmt_iterator si;
195 if (src == NULL)
196 return;
198 si = gsi_last (*dst_p);
199 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
202 /* Set up a context for the gimplifier. */
204 void
205 push_gimplify_context (struct gimplify_ctx *c)
207 memset (c, '\0', sizeof (*c));
208 c->prev_context = gimplify_ctxp;
209 gimplify_ctxp = c;
212 /* Tear down a context for the gimplifier. If BODY is non-null, then
213 put the temporaries into the outer BIND_EXPR. Otherwise, put them
214 in the local_decls.
216 BODY is not a sequence, but the first tuple in a sequence. */
218 void
219 pop_gimplify_context (gimple body)
221 struct gimplify_ctx *c = gimplify_ctxp;
223 gcc_assert (c && (c->bind_expr_stack == NULL
224 || VEC_empty (gimple, c->bind_expr_stack)));
225 VEC_free (gimple, heap, c->bind_expr_stack);
226 gimplify_ctxp = c->prev_context;
228 if (body)
229 declare_vars (c->temps, body, false);
230 else
231 record_vars (c->temps);
233 if (c->temp_htab)
234 htab_delete (c->temp_htab);
237 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
239 static void
240 gimple_push_bind_expr (gimple gimple_bind)
242 if (gimplify_ctxp->bind_expr_stack == NULL)
243 gimplify_ctxp->bind_expr_stack = VEC_alloc (gimple, heap, 8);
244 VEC_safe_push (gimple, heap, gimplify_ctxp->bind_expr_stack, gimple_bind);
247 /* Pop the first element off the stack of bindings. */
249 static void
250 gimple_pop_bind_expr (void)
252 VEC_pop (gimple, gimplify_ctxp->bind_expr_stack);
255 /* Return the first element of the stack of bindings. */
257 gimple
258 gimple_current_bind_expr (void)
260 return VEC_last (gimple, gimplify_ctxp->bind_expr_stack);
263 /* Return the stack of bindings created during gimplification. */
265 VEC(gimple, heap) *
266 gimple_bind_expr_stack (void)
268 return gimplify_ctxp->bind_expr_stack;
271 /* Return true iff there is a COND_EXPR between us and the innermost
272 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
274 static bool
275 gimple_conditional_context (void)
277 return gimplify_ctxp->conditions > 0;
280 /* Note that we've entered a COND_EXPR. */
282 static void
283 gimple_push_condition (void)
285 #ifdef ENABLE_GIMPLE_CHECKING
286 if (gimplify_ctxp->conditions == 0)
287 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
288 #endif
289 ++(gimplify_ctxp->conditions);
292 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
293 now, add any conditional cleanups we've seen to the prequeue. */
295 static void
296 gimple_pop_condition (gimple_seq *pre_p)
298 int conds = --(gimplify_ctxp->conditions);
300 gcc_assert (conds >= 0);
301 if (conds == 0)
303 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
304 gimplify_ctxp->conditional_cleanups = NULL;
308 /* A stable comparison routine for use with splay trees and DECLs. */
310 static int
311 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
313 tree a = (tree) xa;
314 tree b = (tree) xb;
316 return DECL_UID (a) - DECL_UID (b);
319 /* Create a new omp construct that deals with variable remapping. */
321 static struct gimplify_omp_ctx *
322 new_omp_context (enum omp_region_type region_type)
324 struct gimplify_omp_ctx *c;
326 c = XCNEW (struct gimplify_omp_ctx);
327 c->outer_context = gimplify_omp_ctxp;
328 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
329 c->privatized_types = pointer_set_create ();
330 c->location = input_location;
331 c->region_type = region_type;
332 if ((region_type & ORT_TASK) == 0)
333 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
334 else
335 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
337 return c;
340 /* Destroy an omp construct that deals with variable remapping. */
342 static void
343 delete_omp_context (struct gimplify_omp_ctx *c)
345 splay_tree_delete (c->variables);
346 pointer_set_destroy (c->privatized_types);
347 XDELETE (c);
350 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
351 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
353 /* Both gimplify the statement T and append it to *SEQ_P. This function
354 behaves exactly as gimplify_stmt, but you don't have to pass T as a
355 reference. */
357 void
358 gimplify_and_add (tree t, gimple_seq *seq_p)
360 gimplify_stmt (&t, seq_p);
363 /* Gimplify statement T into sequence *SEQ_P, and return the first
364 tuple in the sequence of generated tuples for this statement.
365 Return NULL if gimplifying T produced no tuples. */
367 static gimple
368 gimplify_and_return_first (tree t, gimple_seq *seq_p)
370 gimple_stmt_iterator last = gsi_last (*seq_p);
372 gimplify_and_add (t, seq_p);
374 if (!gsi_end_p (last))
376 gsi_next (&last);
377 return gsi_stmt (last);
379 else
380 return gimple_seq_first_stmt (*seq_p);
383 /* Strip off a legitimate source ending from the input string NAME of
384 length LEN. Rather than having to know the names used by all of
385 our front ends, we strip off an ending of a period followed by
386 up to five characters. (Java uses ".class".) */
388 static inline void
389 remove_suffix (char *name, int len)
391 int i;
393 for (i = 2; i < 8 && len > i; i++)
395 if (name[len - i] == '.')
397 name[len - i] = '\0';
398 break;
403 /* Create a new temporary name with PREFIX. Return an identifier. */
405 static GTY(()) unsigned int tmp_var_id_num;
407 tree
408 create_tmp_var_name (const char *prefix)
410 char *tmp_name;
412 if (prefix)
414 char *preftmp = ASTRDUP (prefix);
416 remove_suffix (preftmp, strlen (preftmp));
417 clean_symbol_name (preftmp);
419 prefix = preftmp;
422 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
423 return get_identifier (tmp_name);
426 /* Create a new temporary variable declaration of type TYPE.
427 Do NOT push it into the current binding. */
429 tree
430 create_tmp_var_raw (tree type, const char *prefix)
432 tree tmp_var;
434 tmp_var = build_decl (input_location,
435 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
436 type);
438 /* The variable was declared by the compiler. */
439 DECL_ARTIFICIAL (tmp_var) = 1;
440 /* And we don't want debug info for it. */
441 DECL_IGNORED_P (tmp_var) = 1;
443 /* Make the variable writable. */
444 TREE_READONLY (tmp_var) = 0;
446 DECL_EXTERNAL (tmp_var) = 0;
447 TREE_STATIC (tmp_var) = 0;
448 TREE_USED (tmp_var) = 1;
450 return tmp_var;
453 /* Create a new temporary variable declaration of type TYPE. DO push the
454 variable into the current binding. Further, assume that this is called
455 only from gimplification or optimization, at which point the creation of
456 certain types are bugs. */
458 tree
459 create_tmp_var (tree type, const char *prefix)
461 tree tmp_var;
463 /* We don't allow types that are addressable (meaning we can't make copies),
464 or incomplete. We also used to reject every variable size objects here,
465 but now support those for which a constant upper bound can be obtained.
466 The processing for variable sizes is performed in gimple_add_tmp_var,
467 point at which it really matters and possibly reached via paths not going
468 through this function, e.g. after direct calls to create_tmp_var_raw. */
469 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
471 tmp_var = create_tmp_var_raw (type, prefix);
472 gimple_add_tmp_var (tmp_var);
473 return tmp_var;
476 /* Create a new temporary variable declaration of type TYPE by calling
477 create_tmp_var and if TYPE is a vector or a complex number, mark the new
478 temporary as gimple register. */
480 tree
481 create_tmp_reg (tree type, const char *prefix)
483 tree tmp;
485 tmp = create_tmp_var (type, prefix);
486 if (TREE_CODE (type) == COMPLEX_TYPE
487 || TREE_CODE (type) == VECTOR_TYPE)
488 DECL_GIMPLE_REG_P (tmp) = 1;
490 return tmp;
493 /* Create a temporary with a name derived from VAL. Subroutine of
494 lookup_tmp_var; nobody else should call this function. */
496 static inline tree
497 create_tmp_from_val (tree val)
499 /* Drop all qualifiers and address-space information from the value type. */
500 return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
503 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
504 an existing expression temporary. */
506 static tree
507 lookup_tmp_var (tree val, bool is_formal)
509 tree ret;
511 /* If not optimizing, never really reuse a temporary. local-alloc
512 won't allocate any variable that is used in more than one basic
513 block, which means it will go into memory, causing much extra
514 work in reload and final and poorer code generation, outweighing
515 the extra memory allocation here. */
516 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
517 ret = create_tmp_from_val (val);
518 else
520 elt_t elt, *elt_p;
521 void **slot;
523 elt.val = val;
524 if (gimplify_ctxp->temp_htab == NULL)
525 gimplify_ctxp->temp_htab
526 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
527 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
528 if (*slot == NULL)
530 elt_p = XNEW (elt_t);
531 elt_p->val = val;
532 elt_p->temp = ret = create_tmp_from_val (val);
533 *slot = (void *) elt_p;
535 else
537 elt_p = (elt_t *) *slot;
538 ret = elt_p->temp;
542 return ret;
545 /* Returns true iff T is a valid RHS for an assignment to a renamed
546 user -- or front-end generated artificial -- variable. */
548 static bool
549 is_gimple_reg_rhs (tree t)
551 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
554 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
555 LHS, or for a call argument. */
557 static bool
558 is_gimple_mem_rhs (tree t)
560 /* If we're dealing with a renamable type, either source or dest must be
561 a renamed variable. */
562 if (is_gimple_reg_type (TREE_TYPE (t)))
563 return is_gimple_val (t);
564 else
565 return is_gimple_val (t) || is_gimple_lvalue (t);
568 /* Return true if T is a CALL_EXPR or an expression that can be
569 assigned to a temporary. Note that this predicate should only be
570 used during gimplification. See the rationale for this in
571 gimplify_modify_expr. */
573 static bool
574 is_gimple_reg_rhs_or_call (tree t)
576 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
577 || TREE_CODE (t) == CALL_EXPR);
580 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
581 this predicate should only be used during gimplification. See the
582 rationale for this in gimplify_modify_expr. */
584 static bool
585 is_gimple_mem_rhs_or_call (tree t)
587 /* If we're dealing with a renamable type, either source or dest must be
588 a renamed variable. */
589 if (is_gimple_reg_type (TREE_TYPE (t)))
590 return is_gimple_val (t);
591 else
592 return (is_gimple_val (t) || is_gimple_lvalue (t)
593 || TREE_CODE (t) == CALL_EXPR);
596 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
598 static tree
599 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
600 bool is_formal)
602 tree t, mod;
604 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
605 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
606 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
607 fb_rvalue);
609 t = lookup_tmp_var (val, is_formal);
611 if (is_formal
612 && (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
613 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE))
614 DECL_GIMPLE_REG_P (t) = 1;
616 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
618 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
620 /* gimplify_modify_expr might want to reduce this further. */
621 gimplify_and_add (mod, pre_p);
622 ggc_free (mod);
624 /* If we're gimplifying into ssa, gimplify_modify_expr will have
625 given our temporary an SSA name. Find and return it. */
626 if (gimplify_ctxp->into_ssa)
628 gimple last = gimple_seq_last_stmt (*pre_p);
629 t = gimple_get_lhs (last);
632 return t;
635 /* Return a formal temporary variable initialized with VAL. PRE_P is as
636 in gimplify_expr. Only use this function if:
638 1) The value of the unfactored expression represented by VAL will not
639 change between the initialization and use of the temporary, and
640 2) The temporary will not be otherwise modified.
642 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
643 and #2 means it is inappropriate for && temps.
645 For other cases, use get_initialized_tmp_var instead. */
647 tree
648 get_formal_tmp_var (tree val, gimple_seq *pre_p)
650 return internal_get_tmp_var (val, pre_p, NULL, true);
653 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
654 are as in gimplify_expr. */
656 tree
657 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
659 return internal_get_tmp_var (val, pre_p, post_p, false);
662 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
663 generate debug info for them; otherwise don't. */
665 void
666 declare_vars (tree vars, gimple scope, bool debug_info)
668 tree last = vars;
669 if (last)
671 tree temps, block;
673 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
675 temps = nreverse (last);
677 block = gimple_bind_block (scope);
678 gcc_assert (!block || TREE_CODE (block) == BLOCK);
679 if (!block || !debug_info)
681 DECL_CHAIN (last) = gimple_bind_vars (scope);
682 gimple_bind_set_vars (scope, temps);
684 else
686 /* We need to attach the nodes both to the BIND_EXPR and to its
687 associated BLOCK for debugging purposes. The key point here
688 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
689 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
690 if (BLOCK_VARS (block))
691 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
692 else
694 gimple_bind_set_vars (scope,
695 chainon (gimple_bind_vars (scope), temps));
696 BLOCK_VARS (block) = temps;
702 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
703 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
704 no such upper bound can be obtained. */
706 static void
707 force_constant_size (tree var)
709 /* The only attempt we make is by querying the maximum size of objects
710 of the variable's type. */
712 HOST_WIDE_INT max_size;
714 gcc_assert (TREE_CODE (var) == VAR_DECL);
716 max_size = max_int_size_in_bytes (TREE_TYPE (var));
718 gcc_assert (max_size >= 0);
720 DECL_SIZE_UNIT (var)
721 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
722 DECL_SIZE (var)
723 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
726 /* Push the temporary variable TMP into the current binding. */
728 void
729 gimple_add_tmp_var (tree tmp)
731 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
733 /* Later processing assumes that the object size is constant, which might
734 not be true at this point. Force the use of a constant upper bound in
735 this case. */
736 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
737 force_constant_size (tmp);
739 DECL_CONTEXT (tmp) = current_function_decl;
740 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
742 if (gimplify_ctxp)
744 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
745 gimplify_ctxp->temps = tmp;
747 /* Mark temporaries local within the nearest enclosing parallel. */
748 if (gimplify_omp_ctxp)
750 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
751 while (ctx && ctx->region_type == ORT_WORKSHARE)
752 ctx = ctx->outer_context;
753 if (ctx)
754 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
757 else if (cfun)
758 record_vars (tmp);
759 else
761 gimple_seq body_seq;
763 /* This case is for nested functions. We need to expose the locals
764 they create. */
765 body_seq = gimple_body (current_function_decl);
766 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
770 /* Determine whether to assign a location to the statement GS. */
772 static bool
773 should_carry_location_p (gimple gs)
775 /* Don't emit a line note for a label. We particularly don't want to
776 emit one for the break label, since it doesn't actually correspond
777 to the beginning of the loop/switch. */
778 if (gimple_code (gs) == GIMPLE_LABEL)
779 return false;
781 return true;
784 /* Return true if a location should not be emitted for this statement
785 by annotate_one_with_location. */
787 static inline bool
788 gimple_do_not_emit_location_p (gimple g)
790 return gimple_plf (g, GF_PLF_1);
793 /* Mark statement G so a location will not be emitted by
794 annotate_one_with_location. */
796 static inline void
797 gimple_set_do_not_emit_location (gimple g)
799 /* The PLF flags are initialized to 0 when a new tuple is created,
800 so no need to initialize it anywhere. */
801 gimple_set_plf (g, GF_PLF_1, true);
804 /* Set the location for gimple statement GS to LOCATION. */
806 static void
807 annotate_one_with_location (gimple gs, location_t location)
809 if (!gimple_has_location (gs)
810 && !gimple_do_not_emit_location_p (gs)
811 && should_carry_location_p (gs))
812 gimple_set_location (gs, location);
815 /* Set LOCATION for all the statements after iterator GSI in sequence
816 SEQ. If GSI is pointing to the end of the sequence, start with the
817 first statement in SEQ. */
819 static void
820 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
821 location_t location)
823 if (gsi_end_p (gsi))
824 gsi = gsi_start (seq);
825 else
826 gsi_next (&gsi);
828 for (; !gsi_end_p (gsi); gsi_next (&gsi))
829 annotate_one_with_location (gsi_stmt (gsi), location);
832 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
834 void
835 annotate_all_with_location (gimple_seq stmt_p, location_t location)
837 gimple_stmt_iterator i;
839 if (gimple_seq_empty_p (stmt_p))
840 return;
842 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
844 gimple gs = gsi_stmt (i);
845 annotate_one_with_location (gs, location);
849 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
850 nodes that are referenced more than once in GENERIC functions. This is
851 necessary because gimplification (translation into GIMPLE) is performed
852 by modifying tree nodes in-place, so gimplication of a shared node in a
853 first context could generate an invalid GIMPLE form in a second context.
855 This is achieved with a simple mark/copy/unmark algorithm that walks the
856 GENERIC representation top-down, marks nodes with TREE_VISITED the first
857 time it encounters them, duplicates them if they already have TREE_VISITED
858 set, and finally removes the TREE_VISITED marks it has set.
860 The algorithm works only at the function level, i.e. it generates a GENERIC
861 representation of a function with no nodes shared within the function when
862 passed a GENERIC function (except for nodes that are allowed to be shared).
864 At the global level, it is also necessary to unshare tree nodes that are
865 referenced in more than one function, for the same aforementioned reason.
866 This requires some cooperation from the front-end. There are 2 strategies:
868 1. Manual unsharing. The front-end needs to call unshare_expr on every
869 expression that might end up being shared across functions.
871 2. Deep unsharing. This is an extension of regular unsharing. Instead
872 of calling unshare_expr on expressions that might be shared across
873 functions, the front-end pre-marks them with TREE_VISITED. This will
874 ensure that they are unshared on the first reference within functions
875 when the regular unsharing algorithm runs. The counterpart is that
876 this algorithm must look deeper than for manual unsharing, which is
877 specified by LANG_HOOKS_DEEP_UNSHARING.
879 If there are only few specific cases of node sharing across functions, it is
880 probably easier for a front-end to unshare the expressions manually. On the
881 contrary, if the expressions generated at the global level are as widespread
882 as expressions generated within functions, deep unsharing is very likely the
883 way to go. */
885 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
886 These nodes model computations that must be done once. If we were to
887 unshare something like SAVE_EXPR(i++), the gimplification process would
888 create wrong code. However, if DATA is non-null, it must hold a pointer
889 set that is used to unshare the subtrees of these nodes. */
891 static tree
892 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
894 tree t = *tp;
895 enum tree_code code = TREE_CODE (t);
897 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
898 copy their subtrees if we can make sure to do it only once. */
899 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
901 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
903 else
904 *walk_subtrees = 0;
907 /* Stop at types, decls, constants like copy_tree_r. */
908 else if (TREE_CODE_CLASS (code) == tcc_type
909 || TREE_CODE_CLASS (code) == tcc_declaration
910 || TREE_CODE_CLASS (code) == tcc_constant
911 /* We can't do anything sensible with a BLOCK used as an
912 expression, but we also can't just die when we see it
913 because of non-expression uses. So we avert our eyes
914 and cross our fingers. Silly Java. */
915 || code == BLOCK)
916 *walk_subtrees = 0;
918 /* Cope with the statement expression extension. */
919 else if (code == STATEMENT_LIST)
922 /* Leave the bulk of the work to copy_tree_r itself. */
923 else
924 copy_tree_r (tp, walk_subtrees, NULL);
926 return NULL_TREE;
929 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
930 If *TP has been visited already, then *TP is deeply copied by calling
931 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
933 static tree
934 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
936 tree t = *tp;
937 enum tree_code code = TREE_CODE (t);
939 /* Skip types, decls, and constants. But we do want to look at their
940 types and the bounds of types. Mark them as visited so we properly
941 unmark their subtrees on the unmark pass. If we've already seen them,
942 don't look down further. */
943 if (TREE_CODE_CLASS (code) == tcc_type
944 || TREE_CODE_CLASS (code) == tcc_declaration
945 || TREE_CODE_CLASS (code) == tcc_constant)
947 if (TREE_VISITED (t))
948 *walk_subtrees = 0;
949 else
950 TREE_VISITED (t) = 1;
953 /* If this node has been visited already, unshare it and don't look
954 any deeper. */
955 else if (TREE_VISITED (t))
957 walk_tree (tp, mostly_copy_tree_r, data, NULL);
958 *walk_subtrees = 0;
961 /* Otherwise, mark the node as visited and keep looking. */
962 else
963 TREE_VISITED (t) = 1;
965 return NULL_TREE;
968 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
969 copy_if_shared_r callback unmodified. */
971 static inline void
972 copy_if_shared (tree *tp, void *data)
974 walk_tree (tp, copy_if_shared_r, data, NULL);
977 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
978 any nested functions. */
980 static void
981 unshare_body (tree fndecl)
983 struct cgraph_node *cgn = cgraph_get_node (fndecl);
984 /* If the language requires deep unsharing, we need a pointer set to make
985 sure we don't repeatedly unshare subtrees of unshareable nodes. */
986 struct pointer_set_t *visited
987 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
989 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
990 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
991 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
993 if (visited)
994 pointer_set_destroy (visited);
996 if (cgn)
997 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
998 unshare_body (cgn->symbol.decl);
1001 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1002 Subtrees are walked until the first unvisited node is encountered. */
1004 static tree
1005 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1007 tree t = *tp;
1009 /* If this node has been visited, unmark it and keep looking. */
1010 if (TREE_VISITED (t))
1011 TREE_VISITED (t) = 0;
1013 /* Otherwise, don't look any deeper. */
1014 else
1015 *walk_subtrees = 0;
1017 return NULL_TREE;
1020 /* Unmark the visited trees rooted at *TP. */
1022 static inline void
1023 unmark_visited (tree *tp)
1025 walk_tree (tp, unmark_visited_r, NULL, NULL);
1028 /* Likewise, but mark all trees as not visited. */
1030 static void
1031 unvisit_body (tree fndecl)
1033 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1035 unmark_visited (&DECL_SAVED_TREE (fndecl));
1036 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1037 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1039 if (cgn)
1040 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1041 unvisit_body (cgn->symbol.decl);
1044 /* Unconditionally make an unshared copy of EXPR. This is used when using
1045 stored expressions which span multiple functions, such as BINFO_VTABLE,
1046 as the normal unsharing process can't tell that they're shared. */
1048 tree
1049 unshare_expr (tree expr)
1051 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1052 return expr;
1055 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1056 contain statements and have a value. Assign its value to a temporary
1057 and give it void_type_node. Return the temporary, or NULL_TREE if
1058 WRAPPER was already void. */
1060 tree
1061 voidify_wrapper_expr (tree wrapper, tree temp)
1063 tree type = TREE_TYPE (wrapper);
1064 if (type && !VOID_TYPE_P (type))
1066 tree *p;
1068 /* Set p to point to the body of the wrapper. Loop until we find
1069 something that isn't a wrapper. */
1070 for (p = &wrapper; p && *p; )
1072 switch (TREE_CODE (*p))
1074 case BIND_EXPR:
1075 TREE_SIDE_EFFECTS (*p) = 1;
1076 TREE_TYPE (*p) = void_type_node;
1077 /* For a BIND_EXPR, the body is operand 1. */
1078 p = &BIND_EXPR_BODY (*p);
1079 break;
1081 case CLEANUP_POINT_EXPR:
1082 case TRY_FINALLY_EXPR:
1083 case TRY_CATCH_EXPR:
1084 TREE_SIDE_EFFECTS (*p) = 1;
1085 TREE_TYPE (*p) = void_type_node;
1086 p = &TREE_OPERAND (*p, 0);
1087 break;
1089 case STATEMENT_LIST:
1091 tree_stmt_iterator i = tsi_last (*p);
1092 TREE_SIDE_EFFECTS (*p) = 1;
1093 TREE_TYPE (*p) = void_type_node;
1094 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1096 break;
1098 case COMPOUND_EXPR:
1099 /* Advance to the last statement. Set all container types to
1100 void. */
1101 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1103 TREE_SIDE_EFFECTS (*p) = 1;
1104 TREE_TYPE (*p) = void_type_node;
1106 break;
1108 case TRANSACTION_EXPR:
1109 TREE_SIDE_EFFECTS (*p) = 1;
1110 TREE_TYPE (*p) = void_type_node;
1111 p = &TRANSACTION_EXPR_BODY (*p);
1112 break;
1114 default:
1115 /* Assume that any tree upon which voidify_wrapper_expr is
1116 directly called is a wrapper, and that its body is op0. */
1117 if (p == &wrapper)
1119 TREE_SIDE_EFFECTS (*p) = 1;
1120 TREE_TYPE (*p) = void_type_node;
1121 p = &TREE_OPERAND (*p, 0);
1122 break;
1124 goto out;
1128 out:
1129 if (p == NULL || IS_EMPTY_STMT (*p))
1130 temp = NULL_TREE;
1131 else if (temp)
1133 /* The wrapper is on the RHS of an assignment that we're pushing
1134 down. */
1135 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1136 || TREE_CODE (temp) == MODIFY_EXPR);
1137 TREE_OPERAND (temp, 1) = *p;
1138 *p = temp;
1140 else
1142 temp = create_tmp_var (type, "retval");
1143 *p = build2 (INIT_EXPR, type, temp, *p);
1146 return temp;
1149 return NULL_TREE;
1152 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1153 a temporary through which they communicate. */
1155 static void
1156 build_stack_save_restore (gimple *save, gimple *restore)
1158 tree tmp_var;
1160 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1161 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1162 gimple_call_set_lhs (*save, tmp_var);
1164 *restore
1165 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1166 1, tmp_var);
1169 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1171 static enum gimplify_status
1172 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1174 tree bind_expr = *expr_p;
1175 bool old_save_stack = gimplify_ctxp->save_stack;
1176 tree t;
1177 gimple gimple_bind;
1178 gimple_seq body, cleanup;
1179 gimple stack_save;
1181 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1183 /* Mark variables seen in this bind expr. */
1184 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1186 if (TREE_CODE (t) == VAR_DECL)
1188 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1190 /* Mark variable as local. */
1191 if (ctx && !DECL_EXTERNAL (t)
1192 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1193 || splay_tree_lookup (ctx->variables,
1194 (splay_tree_key) t) == NULL))
1195 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1197 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1199 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1200 cfun->has_local_explicit_reg_vars = true;
1203 /* Preliminarily mark non-addressed complex variables as eligible
1204 for promotion to gimple registers. We'll transform their uses
1205 as we find them. */
1206 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1207 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1208 && !TREE_THIS_VOLATILE (t)
1209 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1210 && !needs_to_live_in_memory (t))
1211 DECL_GIMPLE_REG_P (t) = 1;
1214 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1215 BIND_EXPR_BLOCK (bind_expr));
1216 gimple_push_bind_expr (gimple_bind);
1218 gimplify_ctxp->save_stack = false;
1220 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1221 body = NULL;
1222 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1223 gimple_bind_set_body (gimple_bind, body);
1225 cleanup = NULL;
1226 stack_save = NULL;
1227 if (gimplify_ctxp->save_stack)
1229 gimple stack_restore;
1231 /* Save stack on entry and restore it on exit. Add a try_finally
1232 block to achieve this. Note that mudflap depends on the
1233 format of the emitted code: see mx_register_decls(). */
1234 build_stack_save_restore (&stack_save, &stack_restore);
1236 gimplify_seq_add_stmt (&cleanup, stack_restore);
1239 /* Add clobbers for all variables that go out of scope. */
1240 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1242 if (TREE_CODE (t) == VAR_DECL
1243 && !is_global_var (t)
1244 && DECL_CONTEXT (t) == current_function_decl
1245 && !DECL_HARD_REGISTER (t)
1246 && !TREE_THIS_VOLATILE (t)
1247 && !DECL_HAS_VALUE_EXPR_P (t)
1248 /* Only care for variables that have to be in memory. Others
1249 will be rewritten into SSA names, hence moved to the top-level. */
1250 && !is_gimple_reg (t)
1251 && flag_stack_reuse != SR_NONE)
1253 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1254 TREE_THIS_VOLATILE (clobber) = 1;
1255 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1259 if (cleanup)
1261 gimple gs;
1262 gimple_seq new_body;
1264 new_body = NULL;
1265 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1266 GIMPLE_TRY_FINALLY);
1268 if (stack_save)
1269 gimplify_seq_add_stmt (&new_body, stack_save);
1270 gimplify_seq_add_stmt (&new_body, gs);
1271 gimple_bind_set_body (gimple_bind, new_body);
1274 gimplify_ctxp->save_stack = old_save_stack;
1275 gimple_pop_bind_expr ();
1277 gimplify_seq_add_stmt (pre_p, gimple_bind);
1279 if (temp)
1281 *expr_p = temp;
1282 return GS_OK;
1285 *expr_p = NULL_TREE;
1286 return GS_ALL_DONE;
1289 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1290 GIMPLE value, it is assigned to a new temporary and the statement is
1291 re-written to return the temporary.
1293 PRE_P points to the sequence where side effects that must happen before
1294 STMT should be stored. */
1296 static enum gimplify_status
1297 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1299 gimple ret;
1300 tree ret_expr = TREE_OPERAND (stmt, 0);
1301 tree result_decl, result;
1303 if (ret_expr == error_mark_node)
1304 return GS_ERROR;
1306 if (!ret_expr
1307 || TREE_CODE (ret_expr) == RESULT_DECL
1308 || ret_expr == error_mark_node)
1310 gimple ret = gimple_build_return (ret_expr);
1311 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1312 gimplify_seq_add_stmt (pre_p, ret);
1313 return GS_ALL_DONE;
1316 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1317 result_decl = NULL_TREE;
1318 else
1320 result_decl = TREE_OPERAND (ret_expr, 0);
1322 /* See through a return by reference. */
1323 if (TREE_CODE (result_decl) == INDIRECT_REF)
1324 result_decl = TREE_OPERAND (result_decl, 0);
1326 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1327 || TREE_CODE (ret_expr) == INIT_EXPR)
1328 && TREE_CODE (result_decl) == RESULT_DECL);
1331 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1332 Recall that aggregate_value_p is FALSE for any aggregate type that is
1333 returned in registers. If we're returning values in registers, then
1334 we don't want to extend the lifetime of the RESULT_DECL, particularly
1335 across another call. In addition, for those aggregates for which
1336 hard_function_value generates a PARALLEL, we'll die during normal
1337 expansion of structure assignments; there's special code in expand_return
1338 to handle this case that does not exist in expand_expr. */
1339 if (!result_decl)
1340 result = NULL_TREE;
1341 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1343 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1345 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1346 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1347 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1348 should be effectively allocated by the caller, i.e. all calls to
1349 this function must be subject to the Return Slot Optimization. */
1350 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1351 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1353 result = result_decl;
1355 else if (gimplify_ctxp->return_temp)
1356 result = gimplify_ctxp->return_temp;
1357 else
1359 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1361 /* ??? With complex control flow (usually involving abnormal edges),
1362 we can wind up warning about an uninitialized value for this. Due
1363 to how this variable is constructed and initialized, this is never
1364 true. Give up and never warn. */
1365 TREE_NO_WARNING (result) = 1;
1367 gimplify_ctxp->return_temp = result;
1370 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1371 Then gimplify the whole thing. */
1372 if (result != result_decl)
1373 TREE_OPERAND (ret_expr, 0) = result;
1375 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1377 ret = gimple_build_return (result);
1378 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1379 gimplify_seq_add_stmt (pre_p, ret);
1381 return GS_ALL_DONE;
1384 /* Gimplify a variable-length array DECL. */
1386 static void
1387 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1389 /* This is a variable-sized decl. Simplify its size and mark it
1390 for deferred expansion. Note that mudflap depends on the format
1391 of the emitted code: see mx_register_decls(). */
1392 tree t, addr, ptr_type;
1394 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1395 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1397 /* All occurrences of this decl in final gimplified code will be
1398 replaced by indirection. Setting DECL_VALUE_EXPR does two
1399 things: First, it lets the rest of the gimplifier know what
1400 replacement to use. Second, it lets the debug info know
1401 where to find the value. */
1402 ptr_type = build_pointer_type (TREE_TYPE (decl));
1403 addr = create_tmp_var (ptr_type, get_name (decl));
1404 DECL_IGNORED_P (addr) = 0;
1405 t = build_fold_indirect_ref (addr);
1406 TREE_THIS_NOTRAP (t) = 1;
1407 SET_DECL_VALUE_EXPR (decl, t);
1408 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1410 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1411 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1412 size_int (DECL_ALIGN (decl)));
1413 /* The call has been built for a variable-sized object. */
1414 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1415 t = fold_convert (ptr_type, t);
1416 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1418 gimplify_and_add (t, seq_p);
1420 /* Indicate that we need to restore the stack level when the
1421 enclosing BIND_EXPR is exited. */
1422 gimplify_ctxp->save_stack = true;
1425 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1426 and initialization explicit. */
1428 static enum gimplify_status
1429 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1431 tree stmt = *stmt_p;
1432 tree decl = DECL_EXPR_DECL (stmt);
1434 *stmt_p = NULL_TREE;
1436 if (TREE_TYPE (decl) == error_mark_node)
1437 return GS_ERROR;
1439 if ((TREE_CODE (decl) == TYPE_DECL
1440 || TREE_CODE (decl) == VAR_DECL)
1441 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1442 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1444 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1445 in case its size expressions contain problematic nodes like CALL_EXPR. */
1446 if (TREE_CODE (decl) == TYPE_DECL
1447 && DECL_ORIGINAL_TYPE (decl)
1448 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1449 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1451 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1453 tree init = DECL_INITIAL (decl);
1455 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1456 || (!TREE_STATIC (decl)
1457 && flag_stack_check == GENERIC_STACK_CHECK
1458 && compare_tree_int (DECL_SIZE_UNIT (decl),
1459 STACK_CHECK_MAX_VAR_SIZE) > 0))
1460 gimplify_vla_decl (decl, seq_p);
1462 /* Some front ends do not explicitly declare all anonymous
1463 artificial variables. We compensate here by declaring the
1464 variables, though it would be better if the front ends would
1465 explicitly declare them. */
1466 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1467 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1468 gimple_add_tmp_var (decl);
1470 if (init && init != error_mark_node)
1472 if (!TREE_STATIC (decl))
1474 DECL_INITIAL (decl) = NULL_TREE;
1475 init = build2 (INIT_EXPR, void_type_node, decl, init);
1476 gimplify_and_add (init, seq_p);
1477 ggc_free (init);
1479 else
1480 /* We must still examine initializers for static variables
1481 as they may contain a label address. */
1482 walk_tree (&init, force_labels_r, NULL, NULL);
1486 return GS_ALL_DONE;
1489 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1490 and replacing the LOOP_EXPR with goto, but if the loop contains an
1491 EXIT_EXPR, we need to append a label for it to jump to. */
1493 static enum gimplify_status
1494 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1496 tree saved_label = gimplify_ctxp->exit_label;
1497 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1499 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1501 gimplify_ctxp->exit_label = NULL_TREE;
1503 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1505 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1507 if (gimplify_ctxp->exit_label)
1508 gimplify_seq_add_stmt (pre_p,
1509 gimple_build_label (gimplify_ctxp->exit_label));
1511 gimplify_ctxp->exit_label = saved_label;
1513 *expr_p = NULL;
1514 return GS_ALL_DONE;
1517 /* Gimplify a statement list onto a sequence. These may be created either
1518 by an enlightened front-end, or by shortcut_cond_expr. */
1520 static enum gimplify_status
1521 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1523 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1525 tree_stmt_iterator i = tsi_start (*expr_p);
1527 while (!tsi_end_p (i))
1529 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1530 tsi_delink (&i);
1533 if (temp)
1535 *expr_p = temp;
1536 return GS_OK;
1539 return GS_ALL_DONE;
1542 /* Compare two case labels. Because the front end should already have
1543 made sure that case ranges do not overlap, it is enough to only compare
1544 the CASE_LOW values of each case label. */
1546 static int
1547 compare_case_labels (const void *p1, const void *p2)
1549 const_tree const case1 = *(const_tree const*)p1;
1550 const_tree const case2 = *(const_tree const*)p2;
1552 /* The 'default' case label always goes first. */
1553 if (!CASE_LOW (case1))
1554 return -1;
1555 else if (!CASE_LOW (case2))
1556 return 1;
1557 else
1558 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1561 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1563 void
1564 sort_case_labels (VEC(tree,heap)* label_vec)
1566 VEC_qsort (tree, label_vec, compare_case_labels);
1569 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1571 LABELS is a vector that contains all case labels to look at.
1573 INDEX_TYPE is the type of the switch index expression. Case labels
1574 in LABELS are discarded if their values are not in the value range
1575 covered by INDEX_TYPE. The remaining case label values are folded
1576 to INDEX_TYPE.
1578 If a default case exists in LABELS, it is removed from LABELS and
1579 returned in DEFAULT_CASEP. If no default case exists, but the
1580 case labels already cover the whole range of INDEX_TYPE, a default
1581 case is returned pointing to one of the existing case labels.
1582 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1584 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1585 apply and no action is taken regardless of whether a default case is
1586 found or not. */
1588 void
1589 preprocess_case_label_vec_for_gimple (VEC(tree,heap) *labels,
1590 tree index_type,
1591 tree *default_casep)
1593 tree min_value, max_value;
1594 tree default_case = NULL_TREE;
1595 size_t i, len;
1597 i = 0;
1598 min_value = TYPE_MIN_VALUE (index_type);
1599 max_value = TYPE_MAX_VALUE (index_type);
1600 while (i < VEC_length (tree, labels))
1602 tree elt = VEC_index (tree, labels, i);
1603 tree low = CASE_LOW (elt);
1604 tree high = CASE_HIGH (elt);
1605 bool remove_element = FALSE;
1607 if (low)
1609 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1610 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1612 /* This is a non-default case label, i.e. it has a value.
1614 See if the case label is reachable within the range of
1615 the index type. Remove out-of-range case values. Turn
1616 case ranges into a canonical form (high > low strictly)
1617 and convert the case label values to the index type.
1619 NB: The type of gimple_switch_index() may be the promoted
1620 type, but the case labels retain the original type. */
1622 if (high)
1624 /* This is a case range. Discard empty ranges.
1625 If the bounds or the range are equal, turn this
1626 into a simple (one-value) case. */
1627 int cmp = tree_int_cst_compare (high, low);
1628 if (cmp < 0)
1629 remove_element = TRUE;
1630 else if (cmp == 0)
1631 high = NULL_TREE;
1634 if (! high)
1636 /* If the simple case value is unreachable, ignore it. */
1637 if ((TREE_CODE (min_value) == INTEGER_CST
1638 && tree_int_cst_compare (low, min_value) < 0)
1639 || (TREE_CODE (max_value) == INTEGER_CST
1640 && tree_int_cst_compare (low, max_value) > 0))
1641 remove_element = TRUE;
1642 else
1643 low = fold_convert (index_type, low);
1645 else
1647 /* If the entire case range is unreachable, ignore it. */
1648 if ((TREE_CODE (min_value) == INTEGER_CST
1649 && tree_int_cst_compare (high, min_value) < 0)
1650 || (TREE_CODE (max_value) == INTEGER_CST
1651 && tree_int_cst_compare (low, max_value) > 0))
1652 remove_element = TRUE;
1653 else
1655 /* If the lower bound is less than the index type's
1656 minimum value, truncate the range bounds. */
1657 if (TREE_CODE (min_value) == INTEGER_CST
1658 && tree_int_cst_compare (low, min_value) < 0)
1659 low = min_value;
1660 low = fold_convert (index_type, low);
1662 /* If the upper bound is greater than the index type's
1663 maximum value, truncate the range bounds. */
1664 if (TREE_CODE (max_value) == INTEGER_CST
1665 && tree_int_cst_compare (high, max_value) > 0)
1666 high = max_value;
1667 high = fold_convert (index_type, high);
1669 /* We may have folded a case range to a one-value case. */
1670 if (tree_int_cst_equal (low, high))
1671 high = NULL_TREE;
1675 CASE_LOW (elt) = low;
1676 CASE_HIGH (elt) = high;
1678 else
1680 gcc_assert (!default_case);
1681 default_case = elt;
1682 /* The default case must be passed separately to the
1683 gimple_build_switch routines. But if DEFAULT_CASEP
1684 is NULL, we do not remove the default case (it would
1685 be completely lost). */
1686 if (default_casep)
1687 remove_element = TRUE;
1690 if (remove_element)
1691 VEC_ordered_remove (tree, labels, i);
1692 else
1693 i++;
1695 len = i;
1697 if (!VEC_empty (tree, labels))
1698 sort_case_labels (labels);
1700 if (default_casep && !default_case)
1702 /* If the switch has no default label, add one, so that we jump
1703 around the switch body. If the labels already cover the whole
1704 range of the switch index_type, add the default label pointing
1705 to one of the existing labels. */
1706 if (len
1707 && TYPE_MIN_VALUE (index_type)
1708 && TYPE_MAX_VALUE (index_type)
1709 && tree_int_cst_equal (CASE_LOW (VEC_index (tree, labels, 0)),
1710 TYPE_MIN_VALUE (index_type)))
1712 tree low, high = CASE_HIGH (VEC_index (tree, labels, len - 1));
1713 if (!high)
1714 high = CASE_LOW (VEC_index (tree, labels, len - 1));
1715 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1717 for (i = 1; i < len; i++)
1719 high = CASE_LOW (VEC_index (tree, labels, i));
1720 low = CASE_HIGH (VEC_index (tree, labels, i - 1));
1721 if (!low)
1722 low = CASE_LOW (VEC_index (tree, labels, i - 1));
1723 if ((TREE_INT_CST_LOW (low) + 1
1724 != TREE_INT_CST_LOW (high))
1725 || (TREE_INT_CST_HIGH (low)
1726 + (TREE_INT_CST_LOW (high) == 0)
1727 != TREE_INT_CST_HIGH (high)))
1728 break;
1730 if (i == len)
1732 tree label = CASE_LABEL (VEC_index (tree, labels, 0));
1733 default_case = build_case_label (NULL_TREE, NULL_TREE,
1734 label);
1740 if (default_casep)
1741 *default_casep = default_case;
1744 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1745 branch to. */
1747 static enum gimplify_status
1748 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1750 tree switch_expr = *expr_p;
1751 gimple_seq switch_body_seq = NULL;
1752 enum gimplify_status ret;
1753 tree index_type = TREE_TYPE (switch_expr);
1754 if (index_type == NULL_TREE)
1755 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1757 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1758 fb_rvalue);
1759 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1760 return ret;
1762 if (SWITCH_BODY (switch_expr))
1764 VEC (tree,heap) *labels;
1765 VEC (tree,heap) *saved_labels;
1766 tree default_case = NULL_TREE;
1767 gimple gimple_switch;
1769 /* If someone can be bothered to fill in the labels, they can
1770 be bothered to null out the body too. */
1771 gcc_assert (!SWITCH_LABELS (switch_expr));
1773 /* Save old labels, get new ones from body, then restore the old
1774 labels. Save all the things from the switch body to append after. */
1775 saved_labels = gimplify_ctxp->case_labels;
1776 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1778 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1779 labels = gimplify_ctxp->case_labels;
1780 gimplify_ctxp->case_labels = saved_labels;
1782 preprocess_case_label_vec_for_gimple (labels, index_type,
1783 &default_case);
1785 if (!default_case)
1787 gimple new_default;
1789 default_case
1790 = build_case_label (NULL_TREE, NULL_TREE,
1791 create_artificial_label (UNKNOWN_LOCATION));
1792 new_default = gimple_build_label (CASE_LABEL (default_case));
1793 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1796 gimple_switch = gimple_build_switch_vec (SWITCH_COND (switch_expr),
1797 default_case, labels);
1798 gimplify_seq_add_stmt (pre_p, gimple_switch);
1799 gimplify_seq_add_seq (pre_p, switch_body_seq);
1800 VEC_free(tree, heap, labels);
1802 else
1803 gcc_assert (SWITCH_LABELS (switch_expr));
1805 return GS_ALL_DONE;
1808 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1810 static enum gimplify_status
1811 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1813 struct gimplify_ctx *ctxp;
1814 gimple gimple_label;
1816 /* Invalid OpenMP programs can play Duff's Device type games with
1817 #pragma omp parallel. At least in the C front end, we don't
1818 detect such invalid branches until after gimplification. */
1819 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1820 if (ctxp->case_labels)
1821 break;
1823 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1824 VEC_safe_push (tree, heap, ctxp->case_labels, *expr_p);
1825 gimplify_seq_add_stmt (pre_p, gimple_label);
1827 return GS_ALL_DONE;
1830 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1831 if necessary. */
1833 tree
1834 build_and_jump (tree *label_p)
1836 if (label_p == NULL)
1837 /* If there's nowhere to jump, just fall through. */
1838 return NULL_TREE;
1840 if (*label_p == NULL_TREE)
1842 tree label = create_artificial_label (UNKNOWN_LOCATION);
1843 *label_p = label;
1846 return build1 (GOTO_EXPR, void_type_node, *label_p);
1849 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1850 This also involves building a label to jump to and communicating it to
1851 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1853 static enum gimplify_status
1854 gimplify_exit_expr (tree *expr_p)
1856 tree cond = TREE_OPERAND (*expr_p, 0);
1857 tree expr;
1859 expr = build_and_jump (&gimplify_ctxp->exit_label);
1860 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1861 *expr_p = expr;
1863 return GS_OK;
1866 /* A helper function to be called via walk_tree. Mark all labels under *TP
1867 as being forced. To be called for DECL_INITIAL of static variables. */
1869 tree
1870 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1872 if (TYPE_P (*tp))
1873 *walk_subtrees = 0;
1874 if (TREE_CODE (*tp) == LABEL_DECL)
1875 FORCED_LABEL (*tp) = 1;
1877 return NULL_TREE;
1880 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1881 different from its canonical type, wrap the whole thing inside a
1882 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1883 type.
1885 The canonical type of a COMPONENT_REF is the type of the field being
1886 referenced--unless the field is a bit-field which can be read directly
1887 in a smaller mode, in which case the canonical type is the
1888 sign-appropriate type corresponding to that mode. */
1890 static void
1891 canonicalize_component_ref (tree *expr_p)
1893 tree expr = *expr_p;
1894 tree type;
1896 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1898 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1899 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1900 else
1901 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1903 /* One could argue that all the stuff below is not necessary for
1904 the non-bitfield case and declare it a FE error if type
1905 adjustment would be needed. */
1906 if (TREE_TYPE (expr) != type)
1908 #ifdef ENABLE_TYPES_CHECKING
1909 tree old_type = TREE_TYPE (expr);
1910 #endif
1911 int type_quals;
1913 /* We need to preserve qualifiers and propagate them from
1914 operand 0. */
1915 type_quals = TYPE_QUALS (type)
1916 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1917 if (TYPE_QUALS (type) != type_quals)
1918 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1920 /* Set the type of the COMPONENT_REF to the underlying type. */
1921 TREE_TYPE (expr) = type;
1923 #ifdef ENABLE_TYPES_CHECKING
1924 /* It is now a FE error, if the conversion from the canonical
1925 type to the original expression type is not useless. */
1926 gcc_assert (useless_type_conversion_p (old_type, type));
1927 #endif
1931 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1932 to foo, embed that change in the ADDR_EXPR by converting
1933 T array[U];
1934 (T *)&array
1936 &array[L]
1937 where L is the lower bound. For simplicity, only do this for constant
1938 lower bound.
1939 The constraint is that the type of &array[L] is trivially convertible
1940 to T *. */
1942 static void
1943 canonicalize_addr_expr (tree *expr_p)
1945 tree expr = *expr_p;
1946 tree addr_expr = TREE_OPERAND (expr, 0);
1947 tree datype, ddatype, pddatype;
1949 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1950 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1951 || TREE_CODE (addr_expr) != ADDR_EXPR)
1952 return;
1954 /* The addr_expr type should be a pointer to an array. */
1955 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1956 if (TREE_CODE (datype) != ARRAY_TYPE)
1957 return;
1959 /* The pointer to element type shall be trivially convertible to
1960 the expression pointer type. */
1961 ddatype = TREE_TYPE (datype);
1962 pddatype = build_pointer_type (ddatype);
1963 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1964 pddatype))
1965 return;
1967 /* The lower bound and element sizes must be constant. */
1968 if (!TYPE_SIZE_UNIT (ddatype)
1969 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1970 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1971 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1972 return;
1974 /* All checks succeeded. Build a new node to merge the cast. */
1975 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1976 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1977 NULL_TREE, NULL_TREE);
1978 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1980 /* We can have stripped a required restrict qualifier above. */
1981 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1982 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1985 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1986 underneath as appropriate. */
1988 static enum gimplify_status
1989 gimplify_conversion (tree *expr_p)
1991 location_t loc = EXPR_LOCATION (*expr_p);
1992 gcc_assert (CONVERT_EXPR_P (*expr_p));
1994 /* Then strip away all but the outermost conversion. */
1995 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1997 /* And remove the outermost conversion if it's useless. */
1998 if (tree_ssa_useless_type_conversion (*expr_p))
1999 *expr_p = TREE_OPERAND (*expr_p, 0);
2001 /* If we still have a conversion at the toplevel,
2002 then canonicalize some constructs. */
2003 if (CONVERT_EXPR_P (*expr_p))
2005 tree sub = TREE_OPERAND (*expr_p, 0);
2007 /* If a NOP conversion is changing the type of a COMPONENT_REF
2008 expression, then canonicalize its type now in order to expose more
2009 redundant conversions. */
2010 if (TREE_CODE (sub) == COMPONENT_REF)
2011 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2013 /* If a NOP conversion is changing a pointer to array of foo
2014 to a pointer to foo, embed that change in the ADDR_EXPR. */
2015 else if (TREE_CODE (sub) == ADDR_EXPR)
2016 canonicalize_addr_expr (expr_p);
2019 /* If we have a conversion to a non-register type force the
2020 use of a VIEW_CONVERT_EXPR instead. */
2021 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2022 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2023 TREE_OPERAND (*expr_p, 0));
2025 return GS_OK;
2028 /* Nonlocal VLAs seen in the current function. */
2029 static struct pointer_set_t *nonlocal_vlas;
2031 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2032 DECL_VALUE_EXPR, and it's worth re-examining things. */
2034 static enum gimplify_status
2035 gimplify_var_or_parm_decl (tree *expr_p)
2037 tree decl = *expr_p;
2039 /* ??? If this is a local variable, and it has not been seen in any
2040 outer BIND_EXPR, then it's probably the result of a duplicate
2041 declaration, for which we've already issued an error. It would
2042 be really nice if the front end wouldn't leak these at all.
2043 Currently the only known culprit is C++ destructors, as seen
2044 in g++.old-deja/g++.jason/binding.C. */
2045 if (TREE_CODE (decl) == VAR_DECL
2046 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2047 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2048 && decl_function_context (decl) == current_function_decl)
2050 gcc_assert (seen_error ());
2051 return GS_ERROR;
2054 /* When within an OpenMP context, notice uses of variables. */
2055 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2056 return GS_ALL_DONE;
2058 /* If the decl is an alias for another expression, substitute it now. */
2059 if (DECL_HAS_VALUE_EXPR_P (decl))
2061 tree value_expr = DECL_VALUE_EXPR (decl);
2063 /* For referenced nonlocal VLAs add a decl for debugging purposes
2064 to the current function. */
2065 if (TREE_CODE (decl) == VAR_DECL
2066 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2067 && nonlocal_vlas != NULL
2068 && TREE_CODE (value_expr) == INDIRECT_REF
2069 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2070 && decl_function_context (decl) != current_function_decl)
2072 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2073 while (ctx && ctx->region_type == ORT_WORKSHARE)
2074 ctx = ctx->outer_context;
2075 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2077 tree copy = copy_node (decl), block;
2079 lang_hooks.dup_lang_specific_decl (copy);
2080 SET_DECL_RTL (copy, 0);
2081 TREE_USED (copy) = 1;
2082 block = DECL_INITIAL (current_function_decl);
2083 DECL_CHAIN (copy) = BLOCK_VARS (block);
2084 BLOCK_VARS (block) = copy;
2085 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2086 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2090 *expr_p = unshare_expr (value_expr);
2091 return GS_OK;
2094 return GS_ALL_DONE;
2097 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2098 node *EXPR_P.
2100 compound_lval
2101 : min_lval '[' val ']'
2102 | min_lval '.' ID
2103 | compound_lval '[' val ']'
2104 | compound_lval '.' ID
2106 This is not part of the original SIMPLE definition, which separates
2107 array and member references, but it seems reasonable to handle them
2108 together. Also, this way we don't run into problems with union
2109 aliasing; gcc requires that for accesses through a union to alias, the
2110 union reference must be explicit, which was not always the case when we
2111 were splitting up array and member refs.
2113 PRE_P points to the sequence where side effects that must happen before
2114 *EXPR_P should be stored.
2116 POST_P points to the sequence where side effects that must happen after
2117 *EXPR_P should be stored. */
2119 static enum gimplify_status
2120 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2121 fallback_t fallback)
2123 tree *p;
2124 VEC(tree,heap) *stack;
2125 enum gimplify_status ret = GS_ALL_DONE, tret;
2126 int i;
2127 location_t loc = EXPR_LOCATION (*expr_p);
2128 tree expr = *expr_p;
2130 /* Create a stack of the subexpressions so later we can walk them in
2131 order from inner to outer. */
2132 stack = VEC_alloc (tree, heap, 10);
2134 /* We can handle anything that get_inner_reference can deal with. */
2135 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2137 restart:
2138 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2139 if (TREE_CODE (*p) == INDIRECT_REF)
2140 *p = fold_indirect_ref_loc (loc, *p);
2142 if (handled_component_p (*p))
2144 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2145 additional COMPONENT_REFs. */
2146 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2147 && gimplify_var_or_parm_decl (p) == GS_OK)
2148 goto restart;
2149 else
2150 break;
2152 VEC_safe_push (tree, heap, stack, *p);
2155 gcc_assert (VEC_length (tree, stack));
2157 /* Now STACK is a stack of pointers to all the refs we've walked through
2158 and P points to the innermost expression.
2160 Java requires that we elaborated nodes in source order. That
2161 means we must gimplify the inner expression followed by each of
2162 the indices, in order. But we can't gimplify the inner
2163 expression until we deal with any variable bounds, sizes, or
2164 positions in order to deal with PLACEHOLDER_EXPRs.
2166 So we do this in three steps. First we deal with the annotations
2167 for any variables in the components, then we gimplify the base,
2168 then we gimplify any indices, from left to right. */
2169 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
2171 tree t = VEC_index (tree, stack, i);
2173 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2175 /* Gimplify the low bound and element type size and put them into
2176 the ARRAY_REF. If these values are set, they have already been
2177 gimplified. */
2178 if (TREE_OPERAND (t, 2) == NULL_TREE)
2180 tree low = unshare_expr (array_ref_low_bound (t));
2181 if (!is_gimple_min_invariant (low))
2183 TREE_OPERAND (t, 2) = low;
2184 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2185 post_p, is_gimple_reg,
2186 fb_rvalue);
2187 ret = MIN (ret, tret);
2190 else
2192 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2193 is_gimple_reg, fb_rvalue);
2194 ret = MIN (ret, tret);
2197 if (TREE_OPERAND (t, 3) == NULL_TREE)
2199 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2200 tree elmt_size = unshare_expr (array_ref_element_size (t));
2201 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2203 /* Divide the element size by the alignment of the element
2204 type (above). */
2205 elmt_size
2206 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2208 if (!is_gimple_min_invariant (elmt_size))
2210 TREE_OPERAND (t, 3) = elmt_size;
2211 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2212 post_p, is_gimple_reg,
2213 fb_rvalue);
2214 ret = MIN (ret, tret);
2217 else
2219 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2220 is_gimple_reg, fb_rvalue);
2221 ret = MIN (ret, tret);
2224 else if (TREE_CODE (t) == COMPONENT_REF)
2226 /* Set the field offset into T and gimplify it. */
2227 if (TREE_OPERAND (t, 2) == NULL_TREE)
2229 tree offset = unshare_expr (component_ref_field_offset (t));
2230 tree field = TREE_OPERAND (t, 1);
2231 tree factor
2232 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2234 /* Divide the offset by its alignment. */
2235 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2237 if (!is_gimple_min_invariant (offset))
2239 TREE_OPERAND (t, 2) = offset;
2240 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2241 post_p, is_gimple_reg,
2242 fb_rvalue);
2243 ret = MIN (ret, tret);
2246 else
2248 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2249 is_gimple_reg, fb_rvalue);
2250 ret = MIN (ret, tret);
2255 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2256 so as to match the min_lval predicate. Failure to do so may result
2257 in the creation of large aggregate temporaries. */
2258 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2259 fallback | fb_lvalue);
2260 ret = MIN (ret, tret);
2262 /* And finally, the indices and operands to BIT_FIELD_REF. During this
2263 loop we also remove any useless conversions. */
2264 for (; VEC_length (tree, stack) > 0; )
2266 tree t = VEC_pop (tree, stack);
2268 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2270 /* Gimplify the dimension. */
2271 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2273 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2274 is_gimple_val, fb_rvalue);
2275 ret = MIN (ret, tret);
2278 else if (TREE_CODE (t) == BIT_FIELD_REF)
2280 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2281 is_gimple_val, fb_rvalue);
2282 ret = MIN (ret, tret);
2283 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2284 is_gimple_val, fb_rvalue);
2285 ret = MIN (ret, tret);
2288 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2290 /* The innermost expression P may have originally had
2291 TREE_SIDE_EFFECTS set which would have caused all the outer
2292 expressions in *EXPR_P leading to P to also have had
2293 TREE_SIDE_EFFECTS set. */
2294 recalculate_side_effects (t);
2297 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2298 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2300 canonicalize_component_ref (expr_p);
2303 VEC_free (tree, heap, stack);
2305 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2307 return ret;
2310 /* Gimplify the self modifying expression pointed to by EXPR_P
2311 (++, --, +=, -=).
2313 PRE_P points to the list where side effects that must happen before
2314 *EXPR_P should be stored.
2316 POST_P points to the list where side effects that must happen after
2317 *EXPR_P should be stored.
2319 WANT_VALUE is nonzero iff we want to use the value of this expression
2320 in another expression. */
2322 static enum gimplify_status
2323 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2324 bool want_value)
2326 enum tree_code code;
2327 tree lhs, lvalue, rhs, t1;
2328 gimple_seq post = NULL, *orig_post_p = post_p;
2329 bool postfix;
2330 enum tree_code arith_code;
2331 enum gimplify_status ret;
2332 location_t loc = EXPR_LOCATION (*expr_p);
2334 code = TREE_CODE (*expr_p);
2336 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2337 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2339 /* Prefix or postfix? */
2340 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2341 /* Faster to treat as prefix if result is not used. */
2342 postfix = want_value;
2343 else
2344 postfix = false;
2346 /* For postfix, make sure the inner expression's post side effects
2347 are executed after side effects from this expression. */
2348 if (postfix)
2349 post_p = &post;
2351 /* Add or subtract? */
2352 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2353 arith_code = PLUS_EXPR;
2354 else
2355 arith_code = MINUS_EXPR;
2357 /* Gimplify the LHS into a GIMPLE lvalue. */
2358 lvalue = TREE_OPERAND (*expr_p, 0);
2359 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2360 if (ret == GS_ERROR)
2361 return ret;
2363 /* Extract the operands to the arithmetic operation. */
2364 lhs = lvalue;
2365 rhs = TREE_OPERAND (*expr_p, 1);
2367 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2368 that as the result value and in the postqueue operation. We also
2369 make sure to make lvalue a minimal lval, see
2370 gcc.c-torture/execute/20040313-1.c for an example where this matters. */
2371 if (postfix)
2373 if (!is_gimple_min_lval (lvalue))
2375 mark_addressable (lvalue);
2376 lvalue = build_fold_addr_expr_loc (input_location, lvalue);
2377 gimplify_expr (&lvalue, pre_p, post_p, is_gimple_val, fb_rvalue);
2378 lvalue = build_fold_indirect_ref_loc (input_location, lvalue);
2380 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2381 if (ret == GS_ERROR)
2382 return ret;
2385 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2386 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2388 rhs = convert_to_ptrofftype_loc (loc, rhs);
2389 if (arith_code == MINUS_EXPR)
2390 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2391 arith_code = POINTER_PLUS_EXPR;
2394 if (postfix)
2396 tree t2 = get_initialized_tmp_var (lhs, pre_p, NULL);
2397 t1 = build2 (arith_code, TREE_TYPE (*expr_p), t2, rhs);
2398 gimplify_assign (lvalue, t1, pre_p);
2399 gimplify_seq_add_seq (orig_post_p, post);
2400 *expr_p = t2;
2401 return GS_ALL_DONE;
2403 else
2405 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
2406 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2407 return GS_OK;
2411 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2413 static void
2414 maybe_with_size_expr (tree *expr_p)
2416 tree expr = *expr_p;
2417 tree type = TREE_TYPE (expr);
2418 tree size;
2420 /* If we've already wrapped this or the type is error_mark_node, we can't do
2421 anything. */
2422 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2423 || type == error_mark_node)
2424 return;
2426 /* If the size isn't known or is a constant, we have nothing to do. */
2427 size = TYPE_SIZE_UNIT (type);
2428 if (!size || TREE_CODE (size) == INTEGER_CST)
2429 return;
2431 /* Otherwise, make a WITH_SIZE_EXPR. */
2432 size = unshare_expr (size);
2433 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2434 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2437 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2438 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2439 the CALL_EXPR. */
2441 static enum gimplify_status
2442 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2444 bool (*test) (tree);
2445 fallback_t fb;
2447 /* In general, we allow lvalues for function arguments to avoid
2448 extra overhead of copying large aggregates out of even larger
2449 aggregates into temporaries only to copy the temporaries to
2450 the argument list. Make optimizers happy by pulling out to
2451 temporaries those types that fit in registers. */
2452 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2453 test = is_gimple_val, fb = fb_rvalue;
2454 else
2456 test = is_gimple_lvalue, fb = fb_either;
2457 /* Also strip a TARGET_EXPR that would force an extra copy. */
2458 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2460 tree init = TARGET_EXPR_INITIAL (*arg_p);
2461 if (init
2462 && !VOID_TYPE_P (TREE_TYPE (init)))
2463 *arg_p = init;
2467 /* If this is a variable sized type, we must remember the size. */
2468 maybe_with_size_expr (arg_p);
2470 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2471 /* Make sure arguments have the same location as the function call
2472 itself. */
2473 protected_set_expr_location (*arg_p, call_location);
2475 /* There is a sequence point before a function call. Side effects in
2476 the argument list must occur before the actual call. So, when
2477 gimplifying arguments, force gimplify_expr to use an internal
2478 post queue which is then appended to the end of PRE_P. */
2479 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2482 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2483 WANT_VALUE is true if the result of the call is desired. */
2485 static enum gimplify_status
2486 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2488 tree fndecl, parms, p, fnptrtype;
2489 enum gimplify_status ret;
2490 int i, nargs;
2491 gimple call;
2492 bool builtin_va_start_p = FALSE;
2493 location_t loc = EXPR_LOCATION (*expr_p);
2495 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2497 /* For reliable diagnostics during inlining, it is necessary that
2498 every call_expr be annotated with file and line. */
2499 if (! EXPR_HAS_LOCATION (*expr_p))
2500 SET_EXPR_LOCATION (*expr_p, input_location);
2502 /* This may be a call to a builtin function.
2504 Builtin function calls may be transformed into different
2505 (and more efficient) builtin function calls under certain
2506 circumstances. Unfortunately, gimplification can muck things
2507 up enough that the builtin expanders are not aware that certain
2508 transformations are still valid.
2510 So we attempt transformation/gimplification of the call before
2511 we gimplify the CALL_EXPR. At this time we do not manage to
2512 transform all calls in the same manner as the expanders do, but
2513 we do transform most of them. */
2514 fndecl = get_callee_fndecl (*expr_p);
2515 if (fndecl && DECL_BUILT_IN (fndecl))
2517 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2519 if (new_tree && new_tree != *expr_p)
2521 /* There was a transformation of this call which computes the
2522 same value, but in a more efficient way. Return and try
2523 again. */
2524 *expr_p = new_tree;
2525 return GS_OK;
2528 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2529 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_VA_START)
2531 builtin_va_start_p = TRUE;
2532 if (call_expr_nargs (*expr_p) < 2)
2534 error ("too few arguments to function %<va_start%>");
2535 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2536 return GS_OK;
2539 if (fold_builtin_next_arg (*expr_p, true))
2541 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2542 return GS_OK;
2547 /* Remember the original function pointer type. */
2548 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2550 /* There is a sequence point before the call, so any side effects in
2551 the calling expression must occur before the actual call. Force
2552 gimplify_expr to use an internal post queue. */
2553 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2554 is_gimple_call_addr, fb_rvalue);
2556 nargs = call_expr_nargs (*expr_p);
2558 /* Get argument types for verification. */
2559 fndecl = get_callee_fndecl (*expr_p);
2560 parms = NULL_TREE;
2561 if (fndecl)
2562 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2563 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2564 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2566 if (fndecl && DECL_ARGUMENTS (fndecl))
2567 p = DECL_ARGUMENTS (fndecl);
2568 else if (parms)
2569 p = parms;
2570 else
2571 p = NULL_TREE;
2572 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2575 /* If the last argument is __builtin_va_arg_pack () and it is not
2576 passed as a named argument, decrease the number of CALL_EXPR
2577 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2578 if (!p
2579 && i < nargs
2580 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2582 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2583 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2585 if (last_arg_fndecl
2586 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2587 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2588 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2590 tree call = *expr_p;
2592 --nargs;
2593 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2594 CALL_EXPR_FN (call),
2595 nargs, CALL_EXPR_ARGP (call));
2597 /* Copy all CALL_EXPR flags, location and block, except
2598 CALL_EXPR_VA_ARG_PACK flag. */
2599 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2600 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2601 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2602 = CALL_EXPR_RETURN_SLOT_OPT (call);
2603 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2604 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2605 TREE_BLOCK (*expr_p) = TREE_BLOCK (call);
2607 /* Set CALL_EXPR_VA_ARG_PACK. */
2608 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2612 /* Finally, gimplify the function arguments. */
2613 if (nargs > 0)
2615 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2616 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2617 PUSH_ARGS_REVERSED ? i-- : i++)
2619 enum gimplify_status t;
2621 /* Avoid gimplifying the second argument to va_start, which needs to
2622 be the plain PARM_DECL. */
2623 if ((i != 1) || !builtin_va_start_p)
2625 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2626 EXPR_LOCATION (*expr_p));
2628 if (t == GS_ERROR)
2629 ret = GS_ERROR;
2634 /* Verify the function result. */
2635 if (want_value && fndecl
2636 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2638 error_at (loc, "using result of function returning %<void%>");
2639 ret = GS_ERROR;
2642 /* Try this again in case gimplification exposed something. */
2643 if (ret != GS_ERROR)
2645 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2647 if (new_tree && new_tree != *expr_p)
2649 /* There was a transformation of this call which computes the
2650 same value, but in a more efficient way. Return and try
2651 again. */
2652 *expr_p = new_tree;
2653 return GS_OK;
2656 else
2658 *expr_p = error_mark_node;
2659 return GS_ERROR;
2662 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2663 decl. This allows us to eliminate redundant or useless
2664 calls to "const" functions. */
2665 if (TREE_CODE (*expr_p) == CALL_EXPR)
2667 int flags = call_expr_flags (*expr_p);
2668 if (flags & (ECF_CONST | ECF_PURE)
2669 /* An infinite loop is considered a side effect. */
2670 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2671 TREE_SIDE_EFFECTS (*expr_p) = 0;
2674 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2675 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2676 form and delegate the creation of a GIMPLE_CALL to
2677 gimplify_modify_expr. This is always possible because when
2678 WANT_VALUE is true, the caller wants the result of this call into
2679 a temporary, which means that we will emit an INIT_EXPR in
2680 internal_get_tmp_var which will then be handled by
2681 gimplify_modify_expr. */
2682 if (!want_value)
2684 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2685 have to do is replicate it as a GIMPLE_CALL tuple. */
2686 gimple_stmt_iterator gsi;
2687 call = gimple_build_call_from_tree (*expr_p);
2688 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2689 gimplify_seq_add_stmt (pre_p, call);
2690 gsi = gsi_last (*pre_p);
2691 fold_stmt (&gsi);
2692 *expr_p = NULL_TREE;
2694 else
2695 /* Remember the original function type. */
2696 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2697 CALL_EXPR_FN (*expr_p));
2699 return ret;
2702 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2703 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2705 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2706 condition is true or false, respectively. If null, we should generate
2707 our own to skip over the evaluation of this specific expression.
2709 LOCUS is the source location of the COND_EXPR.
2711 This function is the tree equivalent of do_jump.
2713 shortcut_cond_r should only be called by shortcut_cond_expr. */
2715 static tree
2716 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2717 location_t locus)
2719 tree local_label = NULL_TREE;
2720 tree t, expr = NULL;
2722 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2723 retain the shortcut semantics. Just insert the gotos here;
2724 shortcut_cond_expr will append the real blocks later. */
2725 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2727 location_t new_locus;
2729 /* Turn if (a && b) into
2731 if (a); else goto no;
2732 if (b) goto yes; else goto no;
2733 (no:) */
2735 if (false_label_p == NULL)
2736 false_label_p = &local_label;
2738 /* Keep the original source location on the first 'if'. */
2739 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2740 append_to_statement_list (t, &expr);
2742 /* Set the source location of the && on the second 'if'. */
2743 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2744 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2745 new_locus);
2746 append_to_statement_list (t, &expr);
2748 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2750 location_t new_locus;
2752 /* Turn if (a || b) into
2754 if (a) goto yes;
2755 if (b) goto yes; else goto no;
2756 (yes:) */
2758 if (true_label_p == NULL)
2759 true_label_p = &local_label;
2761 /* Keep the original source location on the first 'if'. */
2762 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2763 append_to_statement_list (t, &expr);
2765 /* Set the source location of the || on the second 'if'. */
2766 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2767 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2768 new_locus);
2769 append_to_statement_list (t, &expr);
2771 else if (TREE_CODE (pred) == COND_EXPR
2772 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2773 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2775 location_t new_locus;
2777 /* As long as we're messing with gotos, turn if (a ? b : c) into
2778 if (a)
2779 if (b) goto yes; else goto no;
2780 else
2781 if (c) goto yes; else goto no;
2783 Don't do this if one of the arms has void type, which can happen
2784 in C++ when the arm is throw. */
2786 /* Keep the original source location on the first 'if'. Set the source
2787 location of the ? on the second 'if'. */
2788 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2789 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2790 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2791 false_label_p, locus),
2792 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2793 false_label_p, new_locus));
2795 else
2797 expr = build3 (COND_EXPR, void_type_node, pred,
2798 build_and_jump (true_label_p),
2799 build_and_jump (false_label_p));
2800 SET_EXPR_LOCATION (expr, locus);
2803 if (local_label)
2805 t = build1 (LABEL_EXPR, void_type_node, local_label);
2806 append_to_statement_list (t, &expr);
2809 return expr;
2812 /* Given a conditional expression EXPR with short-circuit boolean
2813 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2814 predicate apart into the equivalent sequence of conditionals. */
2816 static tree
2817 shortcut_cond_expr (tree expr)
2819 tree pred = TREE_OPERAND (expr, 0);
2820 tree then_ = TREE_OPERAND (expr, 1);
2821 tree else_ = TREE_OPERAND (expr, 2);
2822 tree true_label, false_label, end_label, t;
2823 tree *true_label_p;
2824 tree *false_label_p;
2825 bool emit_end, emit_false, jump_over_else;
2826 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2827 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2829 /* First do simple transformations. */
2830 if (!else_se)
2832 /* If there is no 'else', turn
2833 if (a && b) then c
2834 into
2835 if (a) if (b) then c. */
2836 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2838 /* Keep the original source location on the first 'if'. */
2839 location_t locus = EXPR_LOC_OR_HERE (expr);
2840 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2841 /* Set the source location of the && on the second 'if'. */
2842 if (EXPR_HAS_LOCATION (pred))
2843 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2844 then_ = shortcut_cond_expr (expr);
2845 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2846 pred = TREE_OPERAND (pred, 0);
2847 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2848 SET_EXPR_LOCATION (expr, locus);
2852 if (!then_se)
2854 /* If there is no 'then', turn
2855 if (a || b); else d
2856 into
2857 if (a); else if (b); else d. */
2858 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2860 /* Keep the original source location on the first 'if'. */
2861 location_t locus = EXPR_LOC_OR_HERE (expr);
2862 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2863 /* Set the source location of the || on the second 'if'. */
2864 if (EXPR_HAS_LOCATION (pred))
2865 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2866 else_ = shortcut_cond_expr (expr);
2867 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2868 pred = TREE_OPERAND (pred, 0);
2869 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2870 SET_EXPR_LOCATION (expr, locus);
2874 /* If we're done, great. */
2875 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2876 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2877 return expr;
2879 /* Otherwise we need to mess with gotos. Change
2880 if (a) c; else d;
2882 if (a); else goto no;
2883 c; goto end;
2884 no: d; end:
2885 and recursively gimplify the condition. */
2887 true_label = false_label = end_label = NULL_TREE;
2889 /* If our arms just jump somewhere, hijack those labels so we don't
2890 generate jumps to jumps. */
2892 if (then_
2893 && TREE_CODE (then_) == GOTO_EXPR
2894 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2896 true_label = GOTO_DESTINATION (then_);
2897 then_ = NULL;
2898 then_se = false;
2901 if (else_
2902 && TREE_CODE (else_) == GOTO_EXPR
2903 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2905 false_label = GOTO_DESTINATION (else_);
2906 else_ = NULL;
2907 else_se = false;
2910 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2911 if (true_label)
2912 true_label_p = &true_label;
2913 else
2914 true_label_p = NULL;
2916 /* The 'else' branch also needs a label if it contains interesting code. */
2917 if (false_label || else_se)
2918 false_label_p = &false_label;
2919 else
2920 false_label_p = NULL;
2922 /* If there was nothing else in our arms, just forward the label(s). */
2923 if (!then_se && !else_se)
2924 return shortcut_cond_r (pred, true_label_p, false_label_p,
2925 EXPR_LOC_OR_HERE (expr));
2927 /* If our last subexpression already has a terminal label, reuse it. */
2928 if (else_se)
2929 t = expr_last (else_);
2930 else if (then_se)
2931 t = expr_last (then_);
2932 else
2933 t = NULL;
2934 if (t && TREE_CODE (t) == LABEL_EXPR)
2935 end_label = LABEL_EXPR_LABEL (t);
2937 /* If we don't care about jumping to the 'else' branch, jump to the end
2938 if the condition is false. */
2939 if (!false_label_p)
2940 false_label_p = &end_label;
2942 /* We only want to emit these labels if we aren't hijacking them. */
2943 emit_end = (end_label == NULL_TREE);
2944 emit_false = (false_label == NULL_TREE);
2946 /* We only emit the jump over the else clause if we have to--if the
2947 then clause may fall through. Otherwise we can wind up with a
2948 useless jump and a useless label at the end of gimplified code,
2949 which will cause us to think that this conditional as a whole
2950 falls through even if it doesn't. If we then inline a function
2951 which ends with such a condition, that can cause us to issue an
2952 inappropriate warning about control reaching the end of a
2953 non-void function. */
2954 jump_over_else = block_may_fallthru (then_);
2956 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2957 EXPR_LOC_OR_HERE (expr));
2959 expr = NULL;
2960 append_to_statement_list (pred, &expr);
2962 append_to_statement_list (then_, &expr);
2963 if (else_se)
2965 if (jump_over_else)
2967 tree last = expr_last (expr);
2968 t = build_and_jump (&end_label);
2969 if (EXPR_HAS_LOCATION (last))
2970 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2971 append_to_statement_list (t, &expr);
2973 if (emit_false)
2975 t = build1 (LABEL_EXPR, void_type_node, false_label);
2976 append_to_statement_list (t, &expr);
2978 append_to_statement_list (else_, &expr);
2980 if (emit_end && end_label)
2982 t = build1 (LABEL_EXPR, void_type_node, end_label);
2983 append_to_statement_list (t, &expr);
2986 return expr;
2989 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2991 tree
2992 gimple_boolify (tree expr)
2994 tree type = TREE_TYPE (expr);
2995 location_t loc = EXPR_LOCATION (expr);
2997 if (TREE_CODE (expr) == NE_EXPR
2998 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2999 && integer_zerop (TREE_OPERAND (expr, 1)))
3001 tree call = TREE_OPERAND (expr, 0);
3002 tree fn = get_callee_fndecl (call);
3004 /* For __builtin_expect ((long) (x), y) recurse into x as well
3005 if x is truth_value_p. */
3006 if (fn
3007 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3008 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3009 && call_expr_nargs (call) == 2)
3011 tree arg = CALL_EXPR_ARG (call, 0);
3012 if (arg)
3014 if (TREE_CODE (arg) == NOP_EXPR
3015 && TREE_TYPE (arg) == TREE_TYPE (call))
3016 arg = TREE_OPERAND (arg, 0);
3017 if (truth_value_p (TREE_CODE (arg)))
3019 arg = gimple_boolify (arg);
3020 CALL_EXPR_ARG (call, 0)
3021 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3027 switch (TREE_CODE (expr))
3029 case TRUTH_AND_EXPR:
3030 case TRUTH_OR_EXPR:
3031 case TRUTH_XOR_EXPR:
3032 case TRUTH_ANDIF_EXPR:
3033 case TRUTH_ORIF_EXPR:
3034 /* Also boolify the arguments of truth exprs. */
3035 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3036 /* FALLTHRU */
3038 case TRUTH_NOT_EXPR:
3039 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3041 /* These expressions always produce boolean results. */
3042 if (TREE_CODE (type) != BOOLEAN_TYPE)
3043 TREE_TYPE (expr) = boolean_type_node;
3044 return expr;
3046 default:
3047 if (COMPARISON_CLASS_P (expr))
3049 /* There expressions always prduce boolean results. */
3050 if (TREE_CODE (type) != BOOLEAN_TYPE)
3051 TREE_TYPE (expr) = boolean_type_node;
3052 return expr;
3054 /* Other expressions that get here must have boolean values, but
3055 might need to be converted to the appropriate mode. */
3056 if (TREE_CODE (type) == BOOLEAN_TYPE)
3057 return expr;
3058 return fold_convert_loc (loc, boolean_type_node, expr);
3062 /* Given a conditional expression *EXPR_P without side effects, gimplify
3063 its operands. New statements are inserted to PRE_P. */
3065 static enum gimplify_status
3066 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3068 tree expr = *expr_p, cond;
3069 enum gimplify_status ret, tret;
3070 enum tree_code code;
3072 cond = gimple_boolify (COND_EXPR_COND (expr));
3074 /* We need to handle && and || specially, as their gimplification
3075 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3076 code = TREE_CODE (cond);
3077 if (code == TRUTH_ANDIF_EXPR)
3078 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3079 else if (code == TRUTH_ORIF_EXPR)
3080 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3081 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3082 COND_EXPR_COND (*expr_p) = cond;
3084 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3085 is_gimple_val, fb_rvalue);
3086 ret = MIN (ret, tret);
3087 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3088 is_gimple_val, fb_rvalue);
3090 return MIN (ret, tret);
3093 /* Return true if evaluating EXPR could trap.
3094 EXPR is GENERIC, while tree_could_trap_p can be called
3095 only on GIMPLE. */
3097 static bool
3098 generic_expr_could_trap_p (tree expr)
3100 unsigned i, n;
3102 if (!expr || is_gimple_val (expr))
3103 return false;
3105 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3106 return true;
3108 n = TREE_OPERAND_LENGTH (expr);
3109 for (i = 0; i < n; i++)
3110 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3111 return true;
3113 return false;
3116 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3117 into
3119 if (p) if (p)
3120 t1 = a; a;
3121 else or else
3122 t1 = b; b;
3125 The second form is used when *EXPR_P is of type void.
3127 PRE_P points to the list where side effects that must happen before
3128 *EXPR_P should be stored. */
3130 static enum gimplify_status
3131 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3133 tree expr = *expr_p;
3134 tree type = TREE_TYPE (expr);
3135 location_t loc = EXPR_LOCATION (expr);
3136 tree tmp, arm1, arm2;
3137 enum gimplify_status ret;
3138 tree label_true, label_false, label_cont;
3139 bool have_then_clause_p, have_else_clause_p;
3140 gimple gimple_cond;
3141 enum tree_code pred_code;
3142 gimple_seq seq = NULL;
3144 /* If this COND_EXPR has a value, copy the values into a temporary within
3145 the arms. */
3146 if (!VOID_TYPE_P (type))
3148 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3149 tree result;
3151 /* If either an rvalue is ok or we do not require an lvalue, create the
3152 temporary. But we cannot do that if the type is addressable. */
3153 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3154 && !TREE_ADDRESSABLE (type))
3156 if (gimplify_ctxp->allow_rhs_cond_expr
3157 /* If either branch has side effects or could trap, it can't be
3158 evaluated unconditionally. */
3159 && !TREE_SIDE_EFFECTS (then_)
3160 && !generic_expr_could_trap_p (then_)
3161 && !TREE_SIDE_EFFECTS (else_)
3162 && !generic_expr_could_trap_p (else_))
3163 return gimplify_pure_cond_expr (expr_p, pre_p);
3165 tmp = create_tmp_var (type, "iftmp");
3166 result = tmp;
3169 /* Otherwise, only create and copy references to the values. */
3170 else
3172 type = build_pointer_type (type);
3174 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3175 then_ = build_fold_addr_expr_loc (loc, then_);
3177 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3178 else_ = build_fold_addr_expr_loc (loc, else_);
3180 expr
3181 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3183 tmp = create_tmp_var (type, "iftmp");
3184 result = build_simple_mem_ref_loc (loc, tmp);
3187 /* Build the new then clause, `tmp = then_;'. But don't build the
3188 assignment if the value is void; in C++ it can be if it's a throw. */
3189 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3190 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3192 /* Similarly, build the new else clause, `tmp = else_;'. */
3193 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3194 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3196 TREE_TYPE (expr) = void_type_node;
3197 recalculate_side_effects (expr);
3199 /* Move the COND_EXPR to the prequeue. */
3200 gimplify_stmt (&expr, pre_p);
3202 *expr_p = result;
3203 return GS_ALL_DONE;
3206 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3207 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3208 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3209 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3211 /* Make sure the condition has BOOLEAN_TYPE. */
3212 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3214 /* Break apart && and || conditions. */
3215 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3216 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3218 expr = shortcut_cond_expr (expr);
3220 if (expr != *expr_p)
3222 *expr_p = expr;
3224 /* We can't rely on gimplify_expr to re-gimplify the expanded
3225 form properly, as cleanups might cause the target labels to be
3226 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3227 set up a conditional context. */
3228 gimple_push_condition ();
3229 gimplify_stmt (expr_p, &seq);
3230 gimple_pop_condition (pre_p);
3231 gimple_seq_add_seq (pre_p, seq);
3233 return GS_ALL_DONE;
3237 /* Now do the normal gimplification. */
3239 /* Gimplify condition. */
3240 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3241 fb_rvalue);
3242 if (ret == GS_ERROR)
3243 return GS_ERROR;
3244 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3246 gimple_push_condition ();
3248 have_then_clause_p = have_else_clause_p = false;
3249 if (TREE_OPERAND (expr, 1) != NULL
3250 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3251 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3252 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3253 == current_function_decl)
3254 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3255 have different locations, otherwise we end up with incorrect
3256 location information on the branches. */
3257 && (optimize
3258 || !EXPR_HAS_LOCATION (expr)
3259 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3260 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3262 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3263 have_then_clause_p = true;
3265 else
3266 label_true = create_artificial_label (UNKNOWN_LOCATION);
3267 if (TREE_OPERAND (expr, 2) != NULL
3268 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3269 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3270 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3271 == current_function_decl)
3272 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3273 have different locations, otherwise we end up with incorrect
3274 location information on the branches. */
3275 && (optimize
3276 || !EXPR_HAS_LOCATION (expr)
3277 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3278 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3280 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3281 have_else_clause_p = true;
3283 else
3284 label_false = create_artificial_label (UNKNOWN_LOCATION);
3286 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3287 &arm2);
3289 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3290 label_false);
3292 gimplify_seq_add_stmt (&seq, gimple_cond);
3293 label_cont = NULL_TREE;
3294 if (!have_then_clause_p)
3296 /* For if (...) {} else { code; } put label_true after
3297 the else block. */
3298 if (TREE_OPERAND (expr, 1) == NULL_TREE
3299 && !have_else_clause_p
3300 && TREE_OPERAND (expr, 2) != NULL_TREE)
3301 label_cont = label_true;
3302 else
3304 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3305 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3306 /* For if (...) { code; } else {} or
3307 if (...) { code; } else goto label; or
3308 if (...) { code; return; } else { ... }
3309 label_cont isn't needed. */
3310 if (!have_else_clause_p
3311 && TREE_OPERAND (expr, 2) != NULL_TREE
3312 && gimple_seq_may_fallthru (seq))
3314 gimple g;
3315 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3317 g = gimple_build_goto (label_cont);
3319 /* GIMPLE_COND's are very low level; they have embedded
3320 gotos. This particular embedded goto should not be marked
3321 with the location of the original COND_EXPR, as it would
3322 correspond to the COND_EXPR's condition, not the ELSE or the
3323 THEN arms. To avoid marking it with the wrong location, flag
3324 it as "no location". */
3325 gimple_set_do_not_emit_location (g);
3327 gimplify_seq_add_stmt (&seq, g);
3331 if (!have_else_clause_p)
3333 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3334 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3336 if (label_cont)
3337 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3339 gimple_pop_condition (pre_p);
3340 gimple_seq_add_seq (pre_p, seq);
3342 if (ret == GS_ERROR)
3343 ; /* Do nothing. */
3344 else if (have_then_clause_p || have_else_clause_p)
3345 ret = GS_ALL_DONE;
3346 else
3348 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3349 expr = TREE_OPERAND (expr, 0);
3350 gimplify_stmt (&expr, pre_p);
3353 *expr_p = NULL;
3354 return ret;
3357 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3358 to be marked addressable.
3360 We cannot rely on such an expression being directly markable if a temporary
3361 has been created by the gimplification. In this case, we create another
3362 temporary and initialize it with a copy, which will become a store after we
3363 mark it addressable. This can happen if the front-end passed us something
3364 that it could not mark addressable yet, like a Fortran pass-by-reference
3365 parameter (int) floatvar. */
3367 static void
3368 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3370 while (handled_component_p (*expr_p))
3371 expr_p = &TREE_OPERAND (*expr_p, 0);
3372 if (is_gimple_reg (*expr_p))
3373 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3376 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3377 a call to __builtin_memcpy. */
3379 static enum gimplify_status
3380 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3381 gimple_seq *seq_p)
3383 tree t, to, to_ptr, from, from_ptr;
3384 gimple gs;
3385 location_t loc = EXPR_LOCATION (*expr_p);
3387 to = TREE_OPERAND (*expr_p, 0);
3388 from = TREE_OPERAND (*expr_p, 1);
3390 /* Mark the RHS addressable. Beware that it may not be possible to do so
3391 directly if a temporary has been created by the gimplification. */
3392 prepare_gimple_addressable (&from, seq_p);
3394 mark_addressable (from);
3395 from_ptr = build_fold_addr_expr_loc (loc, from);
3396 gimplify_arg (&from_ptr, seq_p, loc);
3398 mark_addressable (to);
3399 to_ptr = build_fold_addr_expr_loc (loc, to);
3400 gimplify_arg (&to_ptr, seq_p, loc);
3402 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3404 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3406 if (want_value)
3408 /* tmp = memcpy() */
3409 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3410 gimple_call_set_lhs (gs, t);
3411 gimplify_seq_add_stmt (seq_p, gs);
3413 *expr_p = build_simple_mem_ref (t);
3414 return GS_ALL_DONE;
3417 gimplify_seq_add_stmt (seq_p, gs);
3418 *expr_p = NULL;
3419 return GS_ALL_DONE;
3422 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3423 a call to __builtin_memset. In this case we know that the RHS is
3424 a CONSTRUCTOR with an empty element list. */
3426 static enum gimplify_status
3427 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3428 gimple_seq *seq_p)
3430 tree t, from, to, to_ptr;
3431 gimple gs;
3432 location_t loc = EXPR_LOCATION (*expr_p);
3434 /* Assert our assumptions, to abort instead of producing wrong code
3435 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3436 not be immediately exposed. */
3437 from = TREE_OPERAND (*expr_p, 1);
3438 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3439 from = TREE_OPERAND (from, 0);
3441 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3442 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (from)));
3444 /* Now proceed. */
3445 to = TREE_OPERAND (*expr_p, 0);
3447 to_ptr = build_fold_addr_expr_loc (loc, to);
3448 gimplify_arg (&to_ptr, seq_p, loc);
3449 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3451 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3453 if (want_value)
3455 /* tmp = memset() */
3456 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3457 gimple_call_set_lhs (gs, t);
3458 gimplify_seq_add_stmt (seq_p, gs);
3460 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3461 return GS_ALL_DONE;
3464 gimplify_seq_add_stmt (seq_p, gs);
3465 *expr_p = NULL;
3466 return GS_ALL_DONE;
3469 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3470 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3471 assignment. Return non-null if we detect a potential overlap. */
3473 struct gimplify_init_ctor_preeval_data
3475 /* The base decl of the lhs object. May be NULL, in which case we
3476 have to assume the lhs is indirect. */
3477 tree lhs_base_decl;
3479 /* The alias set of the lhs object. */
3480 alias_set_type lhs_alias_set;
3483 static tree
3484 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3486 struct gimplify_init_ctor_preeval_data *data
3487 = (struct gimplify_init_ctor_preeval_data *) xdata;
3488 tree t = *tp;
3490 /* If we find the base object, obviously we have overlap. */
3491 if (data->lhs_base_decl == t)
3492 return t;
3494 /* If the constructor component is indirect, determine if we have a
3495 potential overlap with the lhs. The only bits of information we
3496 have to go on at this point are addressability and alias sets. */
3497 if ((INDIRECT_REF_P (t)
3498 || TREE_CODE (t) == MEM_REF)
3499 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3500 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3501 return t;
3503 /* If the constructor component is a call, determine if it can hide a
3504 potential overlap with the lhs through an INDIRECT_REF like above.
3505 ??? Ugh - this is completely broken. In fact this whole analysis
3506 doesn't look conservative. */
3507 if (TREE_CODE (t) == CALL_EXPR)
3509 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3511 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3512 if (POINTER_TYPE_P (TREE_VALUE (type))
3513 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3514 && alias_sets_conflict_p (data->lhs_alias_set,
3515 get_alias_set
3516 (TREE_TYPE (TREE_VALUE (type)))))
3517 return t;
3520 if (IS_TYPE_OR_DECL_P (t))
3521 *walk_subtrees = 0;
3522 return NULL;
3525 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3526 force values that overlap with the lhs (as described by *DATA)
3527 into temporaries. */
3529 static void
3530 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3531 struct gimplify_init_ctor_preeval_data *data)
3533 enum gimplify_status one;
3535 /* If the value is constant, then there's nothing to pre-evaluate. */
3536 if (TREE_CONSTANT (*expr_p))
3538 /* Ensure it does not have side effects, it might contain a reference to
3539 the object we're initializing. */
3540 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3541 return;
3544 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3545 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3546 return;
3548 /* Recurse for nested constructors. */
3549 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3551 unsigned HOST_WIDE_INT ix;
3552 constructor_elt *ce;
3553 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
3555 FOR_EACH_VEC_ELT (constructor_elt, v, ix, ce)
3556 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3558 return;
3561 /* If this is a variable sized type, we must remember the size. */
3562 maybe_with_size_expr (expr_p);
3564 /* Gimplify the constructor element to something appropriate for the rhs
3565 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3566 the gimplifier will consider this a store to memory. Doing this
3567 gimplification now means that we won't have to deal with complicated
3568 language-specific trees, nor trees like SAVE_EXPR that can induce
3569 exponential search behavior. */
3570 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3571 if (one == GS_ERROR)
3573 *expr_p = NULL;
3574 return;
3577 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3578 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3579 always be true for all scalars, since is_gimple_mem_rhs insists on a
3580 temporary variable for them. */
3581 if (DECL_P (*expr_p))
3582 return;
3584 /* If this is of variable size, we have no choice but to assume it doesn't
3585 overlap since we can't make a temporary for it. */
3586 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3587 return;
3589 /* Otherwise, we must search for overlap ... */
3590 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3591 return;
3593 /* ... and if found, force the value into a temporary. */
3594 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3597 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3598 a RANGE_EXPR in a CONSTRUCTOR for an array.
3600 var = lower;
3601 loop_entry:
3602 object[var] = value;
3603 if (var == upper)
3604 goto loop_exit;
3605 var = var + 1;
3606 goto loop_entry;
3607 loop_exit:
3609 We increment var _after_ the loop exit check because we might otherwise
3610 fail if upper == TYPE_MAX_VALUE (type for upper).
3612 Note that we never have to deal with SAVE_EXPRs here, because this has
3613 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3615 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
3616 gimple_seq *, bool);
3618 static void
3619 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3620 tree value, tree array_elt_type,
3621 gimple_seq *pre_p, bool cleared)
3623 tree loop_entry_label, loop_exit_label, fall_thru_label;
3624 tree var, var_type, cref, tmp;
3626 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3627 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3628 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3630 /* Create and initialize the index variable. */
3631 var_type = TREE_TYPE (upper);
3632 var = create_tmp_var (var_type, NULL);
3633 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3635 /* Add the loop entry label. */
3636 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3638 /* Build the reference. */
3639 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3640 var, NULL_TREE, NULL_TREE);
3642 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3643 the store. Otherwise just assign value to the reference. */
3645 if (TREE_CODE (value) == CONSTRUCTOR)
3646 /* NB we might have to call ourself recursively through
3647 gimplify_init_ctor_eval if the value is a constructor. */
3648 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3649 pre_p, cleared);
3650 else
3651 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3653 /* We exit the loop when the index var is equal to the upper bound. */
3654 gimplify_seq_add_stmt (pre_p,
3655 gimple_build_cond (EQ_EXPR, var, upper,
3656 loop_exit_label, fall_thru_label));
3658 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3660 /* Otherwise, increment the index var... */
3661 tmp = build2 (PLUS_EXPR, var_type, var,
3662 fold_convert (var_type, integer_one_node));
3663 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3665 /* ...and jump back to the loop entry. */
3666 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3668 /* Add the loop exit label. */
3669 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3672 /* Return true if FDECL is accessing a field that is zero sized. */
3674 static bool
3675 zero_sized_field_decl (const_tree fdecl)
3677 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3678 && integer_zerop (DECL_SIZE (fdecl)))
3679 return true;
3680 return false;
3683 /* Return true if TYPE is zero sized. */
3685 static bool
3686 zero_sized_type (const_tree type)
3688 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3689 && integer_zerop (TYPE_SIZE (type)))
3690 return true;
3691 return false;
3694 /* A subroutine of gimplify_init_constructor. Generate individual
3695 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3696 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3697 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3698 zeroed first. */
3700 static void
3701 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
3702 gimple_seq *pre_p, bool cleared)
3704 tree array_elt_type = NULL;
3705 unsigned HOST_WIDE_INT ix;
3706 tree purpose, value;
3708 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3709 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3711 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3713 tree cref;
3715 /* NULL values are created above for gimplification errors. */
3716 if (value == NULL)
3717 continue;
3719 if (cleared && initializer_zerop (value))
3720 continue;
3722 /* ??? Here's to hoping the front end fills in all of the indices,
3723 so we don't have to figure out what's missing ourselves. */
3724 gcc_assert (purpose);
3726 /* Skip zero-sized fields, unless value has side-effects. This can
3727 happen with calls to functions returning a zero-sized type, which
3728 we shouldn't discard. As a number of downstream passes don't
3729 expect sets of zero-sized fields, we rely on the gimplification of
3730 the MODIFY_EXPR we make below to drop the assignment statement. */
3731 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3732 continue;
3734 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3735 whole range. */
3736 if (TREE_CODE (purpose) == RANGE_EXPR)
3738 tree lower = TREE_OPERAND (purpose, 0);
3739 tree upper = TREE_OPERAND (purpose, 1);
3741 /* If the lower bound is equal to upper, just treat it as if
3742 upper was the index. */
3743 if (simple_cst_equal (lower, upper))
3744 purpose = upper;
3745 else
3747 gimplify_init_ctor_eval_range (object, lower, upper, value,
3748 array_elt_type, pre_p, cleared);
3749 continue;
3753 if (array_elt_type)
3755 /* Do not use bitsizetype for ARRAY_REF indices. */
3756 if (TYPE_DOMAIN (TREE_TYPE (object)))
3757 purpose
3758 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3759 purpose);
3760 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3761 purpose, NULL_TREE, NULL_TREE);
3763 else
3765 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3766 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3767 unshare_expr (object), purpose, NULL_TREE);
3770 if (TREE_CODE (value) == CONSTRUCTOR
3771 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3772 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3773 pre_p, cleared);
3774 else
3776 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3777 gimplify_and_add (init, pre_p);
3778 ggc_free (init);
3783 /* Return the appropriate RHS predicate for this LHS. */
3785 gimple_predicate
3786 rhs_predicate_for (tree lhs)
3788 if (is_gimple_reg (lhs))
3789 return is_gimple_reg_rhs_or_call;
3790 else
3791 return is_gimple_mem_rhs_or_call;
3794 /* Gimplify a C99 compound literal expression. This just means adding
3795 the DECL_EXPR before the current statement and using its anonymous
3796 decl instead. */
3798 static enum gimplify_status
3799 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3800 bool (*gimple_test_f) (tree),
3801 fallback_t fallback)
3803 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3804 tree decl = DECL_EXPR_DECL (decl_s);
3805 tree init = DECL_INITIAL (decl);
3806 /* Mark the decl as addressable if the compound literal
3807 expression is addressable now, otherwise it is marked too late
3808 after we gimplify the initialization expression. */
3809 if (TREE_ADDRESSABLE (*expr_p))
3810 TREE_ADDRESSABLE (decl) = 1;
3811 /* Otherwise, if we don't need an lvalue and have a literal directly
3812 substitute it. Check if it matches the gimple predicate, as
3813 otherwise we'd generate a new temporary, and we can as well just
3814 use the decl we already have. */
3815 else if (!TREE_ADDRESSABLE (decl)
3816 && init
3817 && (fallback & fb_lvalue) == 0
3818 && gimple_test_f (init))
3820 *expr_p = init;
3821 return GS_OK;
3824 /* Preliminarily mark non-addressed complex variables as eligible
3825 for promotion to gimple registers. We'll transform their uses
3826 as we find them. */
3827 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3828 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3829 && !TREE_THIS_VOLATILE (decl)
3830 && !needs_to_live_in_memory (decl))
3831 DECL_GIMPLE_REG_P (decl) = 1;
3833 /* If the decl is not addressable, then it is being used in some
3834 expression or on the right hand side of a statement, and it can
3835 be put into a readonly data section. */
3836 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3837 TREE_READONLY (decl) = 1;
3839 /* This decl isn't mentioned in the enclosing block, so add it to the
3840 list of temps. FIXME it seems a bit of a kludge to say that
3841 anonymous artificial vars aren't pushed, but everything else is. */
3842 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3843 gimple_add_tmp_var (decl);
3845 gimplify_and_add (decl_s, pre_p);
3846 *expr_p = decl;
3847 return GS_OK;
3850 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3851 return a new CONSTRUCTOR if something changed. */
3853 static tree
3854 optimize_compound_literals_in_ctor (tree orig_ctor)
3856 tree ctor = orig_ctor;
3857 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
3858 unsigned int idx, num = VEC_length (constructor_elt, elts);
3860 for (idx = 0; idx < num; idx++)
3862 tree value = VEC_index (constructor_elt, elts, idx)->value;
3863 tree newval = value;
3864 if (TREE_CODE (value) == CONSTRUCTOR)
3865 newval = optimize_compound_literals_in_ctor (value);
3866 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3868 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3869 tree decl = DECL_EXPR_DECL (decl_s);
3870 tree init = DECL_INITIAL (decl);
3872 if (!TREE_ADDRESSABLE (value)
3873 && !TREE_ADDRESSABLE (decl)
3874 && init)
3875 newval = optimize_compound_literals_in_ctor (init);
3877 if (newval == value)
3878 continue;
3880 if (ctor == orig_ctor)
3882 ctor = copy_node (orig_ctor);
3883 CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
3884 elts = CONSTRUCTOR_ELTS (ctor);
3886 VEC_index (constructor_elt, elts, idx)->value = newval;
3888 return ctor;
3891 /* A subroutine of gimplify_modify_expr. Break out elements of a
3892 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3894 Note that we still need to clear any elements that don't have explicit
3895 initializers, so if not all elements are initialized we keep the
3896 original MODIFY_EXPR, we just remove all of the constructor elements.
3898 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3899 GS_ERROR if we would have to create a temporary when gimplifying
3900 this constructor. Otherwise, return GS_OK.
3902 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3904 static enum gimplify_status
3905 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3906 bool want_value, bool notify_temp_creation)
3908 tree object, ctor, type;
3909 enum gimplify_status ret;
3910 VEC(constructor_elt,gc) *elts;
3912 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3914 if (!notify_temp_creation)
3916 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3917 is_gimple_lvalue, fb_lvalue);
3918 if (ret == GS_ERROR)
3919 return ret;
3922 object = TREE_OPERAND (*expr_p, 0);
3923 ctor = TREE_OPERAND (*expr_p, 1) =
3924 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3925 type = TREE_TYPE (ctor);
3926 elts = CONSTRUCTOR_ELTS (ctor);
3927 ret = GS_ALL_DONE;
3929 switch (TREE_CODE (type))
3931 case RECORD_TYPE:
3932 case UNION_TYPE:
3933 case QUAL_UNION_TYPE:
3934 case ARRAY_TYPE:
3936 struct gimplify_init_ctor_preeval_data preeval_data;
3937 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3938 bool cleared, complete_p, valid_const_initializer;
3940 /* Aggregate types must lower constructors to initialization of
3941 individual elements. The exception is that a CONSTRUCTOR node
3942 with no elements indicates zero-initialization of the whole. */
3943 if (VEC_empty (constructor_elt, elts))
3945 if (notify_temp_creation)
3946 return GS_OK;
3947 break;
3950 /* Fetch information about the constructor to direct later processing.
3951 We might want to make static versions of it in various cases, and
3952 can only do so if it known to be a valid constant initializer. */
3953 valid_const_initializer
3954 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3955 &num_ctor_elements, &complete_p);
3957 /* If a const aggregate variable is being initialized, then it
3958 should never be a lose to promote the variable to be static. */
3959 if (valid_const_initializer
3960 && num_nonzero_elements > 1
3961 && TREE_READONLY (object)
3962 && TREE_CODE (object) == VAR_DECL
3963 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3965 if (notify_temp_creation)
3966 return GS_ERROR;
3967 DECL_INITIAL (object) = ctor;
3968 TREE_STATIC (object) = 1;
3969 if (!DECL_NAME (object))
3970 DECL_NAME (object) = create_tmp_var_name ("C");
3971 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3973 /* ??? C++ doesn't automatically append a .<number> to the
3974 assembler name, and even when it does, it looks a FE private
3975 data structures to figure out what that number should be,
3976 which are not set for this variable. I suppose this is
3977 important for local statics for inline functions, which aren't
3978 "local" in the object file sense. So in order to get a unique
3979 TU-local symbol, we must invoke the lhd version now. */
3980 lhd_set_decl_assembler_name (object);
3982 *expr_p = NULL_TREE;
3983 break;
3986 /* If there are "lots" of initialized elements, even discounting
3987 those that are not address constants (and thus *must* be
3988 computed at runtime), then partition the constructor into
3989 constant and non-constant parts. Block copy the constant
3990 parts in, then generate code for the non-constant parts. */
3991 /* TODO. There's code in cp/typeck.c to do this. */
3993 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3994 /* store_constructor will ignore the clearing of variable-sized
3995 objects. Initializers for such objects must explicitly set
3996 every field that needs to be set. */
3997 cleared = false;
3998 else if (!complete_p)
3999 /* If the constructor isn't complete, clear the whole object
4000 beforehand.
4002 ??? This ought not to be needed. For any element not present
4003 in the initializer, we should simply set them to zero. Except
4004 we'd need to *find* the elements that are not present, and that
4005 requires trickery to avoid quadratic compile-time behavior in
4006 large cases or excessive memory use in small cases. */
4007 cleared = true;
4008 else if (num_ctor_elements - num_nonzero_elements
4009 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4010 && num_nonzero_elements < num_ctor_elements / 4)
4011 /* If there are "lots" of zeros, it's more efficient to clear
4012 the memory and then set the nonzero elements. */
4013 cleared = true;
4014 else
4015 cleared = false;
4017 /* If there are "lots" of initialized elements, and all of them
4018 are valid address constants, then the entire initializer can
4019 be dropped to memory, and then memcpy'd out. Don't do this
4020 for sparse arrays, though, as it's more efficient to follow
4021 the standard CONSTRUCTOR behavior of memset followed by
4022 individual element initialization. Also don't do this for small
4023 all-zero initializers (which aren't big enough to merit
4024 clearing), and don't try to make bitwise copies of
4025 TREE_ADDRESSABLE types. */
4026 if (valid_const_initializer
4027 && !(cleared || num_nonzero_elements == 0)
4028 && !TREE_ADDRESSABLE (type))
4030 HOST_WIDE_INT size = int_size_in_bytes (type);
4031 unsigned int align;
4033 /* ??? We can still get unbounded array types, at least
4034 from the C++ front end. This seems wrong, but attempt
4035 to work around it for now. */
4036 if (size < 0)
4038 size = int_size_in_bytes (TREE_TYPE (object));
4039 if (size >= 0)
4040 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4043 /* Find the maximum alignment we can assume for the object. */
4044 /* ??? Make use of DECL_OFFSET_ALIGN. */
4045 if (DECL_P (object))
4046 align = DECL_ALIGN (object);
4047 else
4048 align = TYPE_ALIGN (type);
4050 /* Do a block move either if the size is so small as to make
4051 each individual move a sub-unit move on average, or if it
4052 is so large as to make individual moves inefficient. */
4053 if (size > 0
4054 && num_nonzero_elements > 1
4055 && (size < num_nonzero_elements
4056 || !can_move_by_pieces (size, align)))
4058 if (notify_temp_creation)
4059 return GS_ERROR;
4061 walk_tree (&ctor, force_labels_r, NULL, NULL);
4062 ctor = tree_output_constant_def (ctor);
4063 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4064 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4065 TREE_OPERAND (*expr_p, 1) = ctor;
4067 /* This is no longer an assignment of a CONSTRUCTOR, but
4068 we still may have processing to do on the LHS. So
4069 pretend we didn't do anything here to let that happen. */
4070 return GS_UNHANDLED;
4074 /* If the target is volatile, we have non-zero elements and more than
4075 one field to assign, initialize the target from a temporary. */
4076 if (TREE_THIS_VOLATILE (object)
4077 && !TREE_ADDRESSABLE (type)
4078 && num_nonzero_elements > 0
4079 && VEC_length (constructor_elt, elts) > 1)
4081 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4082 TREE_OPERAND (*expr_p, 0) = temp;
4083 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4084 *expr_p,
4085 build2 (MODIFY_EXPR, void_type_node,
4086 object, temp));
4087 return GS_OK;
4090 if (notify_temp_creation)
4091 return GS_OK;
4093 /* If there are nonzero elements and if needed, pre-evaluate to capture
4094 elements overlapping with the lhs into temporaries. We must do this
4095 before clearing to fetch the values before they are zeroed-out. */
4096 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4098 preeval_data.lhs_base_decl = get_base_address (object);
4099 if (!DECL_P (preeval_data.lhs_base_decl))
4100 preeval_data.lhs_base_decl = NULL;
4101 preeval_data.lhs_alias_set = get_alias_set (object);
4103 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4104 pre_p, post_p, &preeval_data);
4107 if (cleared)
4109 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4110 Note that we still have to gimplify, in order to handle the
4111 case of variable sized types. Avoid shared tree structures. */
4112 CONSTRUCTOR_ELTS (ctor) = NULL;
4113 TREE_SIDE_EFFECTS (ctor) = 0;
4114 object = unshare_expr (object);
4115 gimplify_stmt (expr_p, pre_p);
4118 /* If we have not block cleared the object, or if there are nonzero
4119 elements in the constructor, add assignments to the individual
4120 scalar fields of the object. */
4121 if (!cleared || num_nonzero_elements > 0)
4122 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4124 *expr_p = NULL_TREE;
4126 break;
4128 case COMPLEX_TYPE:
4130 tree r, i;
4132 if (notify_temp_creation)
4133 return GS_OK;
4135 /* Extract the real and imaginary parts out of the ctor. */
4136 gcc_assert (VEC_length (constructor_elt, elts) == 2);
4137 r = VEC_index (constructor_elt, elts, 0)->value;
4138 i = VEC_index (constructor_elt, elts, 1)->value;
4139 if (r == NULL || i == NULL)
4141 tree zero = build_zero_cst (TREE_TYPE (type));
4142 if (r == NULL)
4143 r = zero;
4144 if (i == NULL)
4145 i = zero;
4148 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4149 represent creation of a complex value. */
4150 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4152 ctor = build_complex (type, r, i);
4153 TREE_OPERAND (*expr_p, 1) = ctor;
4155 else
4157 ctor = build2 (COMPLEX_EXPR, type, r, i);
4158 TREE_OPERAND (*expr_p, 1) = ctor;
4159 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4160 pre_p,
4161 post_p,
4162 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4163 fb_rvalue);
4166 break;
4168 case VECTOR_TYPE:
4170 unsigned HOST_WIDE_INT ix;
4171 constructor_elt *ce;
4173 if (notify_temp_creation)
4174 return GS_OK;
4176 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4177 if (TREE_CONSTANT (ctor))
4179 bool constant_p = true;
4180 tree value;
4182 /* Even when ctor is constant, it might contain non-*_CST
4183 elements, such as addresses or trapping values like
4184 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4185 in VECTOR_CST nodes. */
4186 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4187 if (!CONSTANT_CLASS_P (value))
4189 constant_p = false;
4190 break;
4193 if (constant_p)
4195 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4196 break;
4199 /* Don't reduce an initializer constant even if we can't
4200 make a VECTOR_CST. It won't do anything for us, and it'll
4201 prevent us from representing it as a single constant. */
4202 if (initializer_constant_valid_p (ctor, type))
4203 break;
4205 TREE_CONSTANT (ctor) = 0;
4208 /* Vector types use CONSTRUCTOR all the way through gimple
4209 compilation as a general initializer. */
4210 FOR_EACH_VEC_ELT (constructor_elt, elts, ix, ce)
4212 enum gimplify_status tret;
4213 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4214 fb_rvalue);
4215 if (tret == GS_ERROR)
4216 ret = GS_ERROR;
4218 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4219 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4221 break;
4223 default:
4224 /* So how did we get a CONSTRUCTOR for a scalar type? */
4225 gcc_unreachable ();
4228 if (ret == GS_ERROR)
4229 return GS_ERROR;
4230 else if (want_value)
4232 *expr_p = object;
4233 return GS_OK;
4235 else
4237 /* If we have gimplified both sides of the initializer but have
4238 not emitted an assignment, do so now. */
4239 if (*expr_p)
4241 tree lhs = TREE_OPERAND (*expr_p, 0);
4242 tree rhs = TREE_OPERAND (*expr_p, 1);
4243 gimple init = gimple_build_assign (lhs, rhs);
4244 gimplify_seq_add_stmt (pre_p, init);
4245 *expr_p = NULL;
4248 return GS_ALL_DONE;
4252 /* Given a pointer value OP0, return a simplified version of an
4253 indirection through OP0, or NULL_TREE if no simplification is
4254 possible. Note that the resulting type may be different from
4255 the type pointed to in the sense that it is still compatible
4256 from the langhooks point of view. */
4258 tree
4259 gimple_fold_indirect_ref (tree t)
4261 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4262 tree sub = t;
4263 tree subtype;
4265 STRIP_NOPS (sub);
4266 subtype = TREE_TYPE (sub);
4267 if (!POINTER_TYPE_P (subtype))
4268 return NULL_TREE;
4270 if (TREE_CODE (sub) == ADDR_EXPR)
4272 tree op = TREE_OPERAND (sub, 0);
4273 tree optype = TREE_TYPE (op);
4274 /* *&p => p */
4275 if (useless_type_conversion_p (type, optype))
4276 return op;
4278 /* *(foo *)&fooarray => fooarray[0] */
4279 if (TREE_CODE (optype) == ARRAY_TYPE
4280 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4281 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4283 tree type_domain = TYPE_DOMAIN (optype);
4284 tree min_val = size_zero_node;
4285 if (type_domain && TYPE_MIN_VALUE (type_domain))
4286 min_val = TYPE_MIN_VALUE (type_domain);
4287 if (TREE_CODE (min_val) == INTEGER_CST)
4288 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4290 /* *(foo *)&complexfoo => __real__ complexfoo */
4291 else if (TREE_CODE (optype) == COMPLEX_TYPE
4292 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4293 return fold_build1 (REALPART_EXPR, type, op);
4294 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4295 else if (TREE_CODE (optype) == VECTOR_TYPE
4296 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4298 tree part_width = TYPE_SIZE (type);
4299 tree index = bitsize_int (0);
4300 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4304 /* *(p + CST) -> ... */
4305 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4306 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4308 tree addr = TREE_OPERAND (sub, 0);
4309 tree off = TREE_OPERAND (sub, 1);
4310 tree addrtype;
4312 STRIP_NOPS (addr);
4313 addrtype = TREE_TYPE (addr);
4315 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4316 if (TREE_CODE (addr) == ADDR_EXPR
4317 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4318 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4319 && host_integerp (off, 1))
4321 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4322 tree part_width = TYPE_SIZE (type);
4323 unsigned HOST_WIDE_INT part_widthi
4324 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4325 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4326 tree index = bitsize_int (indexi);
4327 if (offset / part_widthi
4328 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4329 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4330 part_width, index);
4333 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4334 if (TREE_CODE (addr) == ADDR_EXPR
4335 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4336 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4338 tree size = TYPE_SIZE_UNIT (type);
4339 if (tree_int_cst_equal (size, off))
4340 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4343 /* *(p + CST) -> MEM_REF <p, CST>. */
4344 if (TREE_CODE (addr) != ADDR_EXPR
4345 || DECL_P (TREE_OPERAND (addr, 0)))
4346 return fold_build2 (MEM_REF, type,
4347 addr,
4348 build_int_cst_wide (ptype,
4349 TREE_INT_CST_LOW (off),
4350 TREE_INT_CST_HIGH (off)));
4353 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4354 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4355 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4356 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4358 tree type_domain;
4359 tree min_val = size_zero_node;
4360 tree osub = sub;
4361 sub = gimple_fold_indirect_ref (sub);
4362 if (! sub)
4363 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4364 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4365 if (type_domain && TYPE_MIN_VALUE (type_domain))
4366 min_val = TYPE_MIN_VALUE (type_domain);
4367 if (TREE_CODE (min_val) == INTEGER_CST)
4368 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4371 return NULL_TREE;
4374 /* Given a pointer value OP0, return a simplified version of an
4375 indirection through OP0, or NULL_TREE if no simplification is
4376 possible. This may only be applied to a rhs of an expression.
4377 Note that the resulting type may be different from the type pointed
4378 to in the sense that it is still compatible from the langhooks
4379 point of view. */
4381 static tree
4382 gimple_fold_indirect_ref_rhs (tree t)
4384 return gimple_fold_indirect_ref (t);
4387 /* Subroutine of gimplify_modify_expr to do simplifications of
4388 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4389 something changes. */
4391 static enum gimplify_status
4392 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4393 gimple_seq *pre_p, gimple_seq *post_p,
4394 bool want_value)
4396 enum gimplify_status ret = GS_UNHANDLED;
4397 bool changed;
4401 changed = false;
4402 switch (TREE_CODE (*from_p))
4404 case VAR_DECL:
4405 /* If we're assigning from a read-only variable initialized with
4406 a constructor, do the direct assignment from the constructor,
4407 but only if neither source nor target are volatile since this
4408 latter assignment might end up being done on a per-field basis. */
4409 if (DECL_INITIAL (*from_p)
4410 && TREE_READONLY (*from_p)
4411 && !TREE_THIS_VOLATILE (*from_p)
4412 && !TREE_THIS_VOLATILE (*to_p)
4413 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4415 tree old_from = *from_p;
4416 enum gimplify_status subret;
4418 /* Move the constructor into the RHS. */
4419 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4421 /* Let's see if gimplify_init_constructor will need to put
4422 it in memory. */
4423 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4424 false, true);
4425 if (subret == GS_ERROR)
4427 /* If so, revert the change. */
4428 *from_p = old_from;
4430 else
4432 ret = GS_OK;
4433 changed = true;
4436 break;
4437 case INDIRECT_REF:
4439 /* If we have code like
4441 *(const A*)(A*)&x
4443 where the type of "x" is a (possibly cv-qualified variant
4444 of "A"), treat the entire expression as identical to "x".
4445 This kind of code arises in C++ when an object is bound
4446 to a const reference, and if "x" is a TARGET_EXPR we want
4447 to take advantage of the optimization below. */
4448 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4449 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4450 if (t)
4452 if (TREE_THIS_VOLATILE (t) != volatile_p)
4454 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4455 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4456 build_fold_addr_expr (t));
4457 if (REFERENCE_CLASS_P (t))
4458 TREE_THIS_VOLATILE (t) = volatile_p;
4460 *from_p = t;
4461 ret = GS_OK;
4462 changed = true;
4464 break;
4467 case TARGET_EXPR:
4469 /* If we are initializing something from a TARGET_EXPR, strip the
4470 TARGET_EXPR and initialize it directly, if possible. This can't
4471 be done if the initializer is void, since that implies that the
4472 temporary is set in some non-trivial way.
4474 ??? What about code that pulls out the temp and uses it
4475 elsewhere? I think that such code never uses the TARGET_EXPR as
4476 an initializer. If I'm wrong, we'll die because the temp won't
4477 have any RTL. In that case, I guess we'll need to replace
4478 references somehow. */
4479 tree init = TARGET_EXPR_INITIAL (*from_p);
4481 if (init
4482 && !VOID_TYPE_P (TREE_TYPE (init)))
4484 *from_p = init;
4485 ret = GS_OK;
4486 changed = true;
4489 break;
4491 case COMPOUND_EXPR:
4492 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4493 caught. */
4494 gimplify_compound_expr (from_p, pre_p, true);
4495 ret = GS_OK;
4496 changed = true;
4497 break;
4499 case CONSTRUCTOR:
4500 /* If we already made some changes, let the front end have a
4501 crack at this before we break it down. */
4502 if (ret != GS_UNHANDLED)
4503 break;
4504 /* If we're initializing from a CONSTRUCTOR, break this into
4505 individual MODIFY_EXPRs. */
4506 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4507 false);
4509 case COND_EXPR:
4510 /* If we're assigning to a non-register type, push the assignment
4511 down into the branches. This is mandatory for ADDRESSABLE types,
4512 since we cannot generate temporaries for such, but it saves a
4513 copy in other cases as well. */
4514 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4516 /* This code should mirror the code in gimplify_cond_expr. */
4517 enum tree_code code = TREE_CODE (*expr_p);
4518 tree cond = *from_p;
4519 tree result = *to_p;
4521 ret = gimplify_expr (&result, pre_p, post_p,
4522 is_gimple_lvalue, fb_lvalue);
4523 if (ret != GS_ERROR)
4524 ret = GS_OK;
4526 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4527 TREE_OPERAND (cond, 1)
4528 = build2 (code, void_type_node, result,
4529 TREE_OPERAND (cond, 1));
4530 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4531 TREE_OPERAND (cond, 2)
4532 = build2 (code, void_type_node, unshare_expr (result),
4533 TREE_OPERAND (cond, 2));
4535 TREE_TYPE (cond) = void_type_node;
4536 recalculate_side_effects (cond);
4538 if (want_value)
4540 gimplify_and_add (cond, pre_p);
4541 *expr_p = unshare_expr (result);
4543 else
4544 *expr_p = cond;
4545 return ret;
4547 break;
4549 case CALL_EXPR:
4550 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4551 return slot so that we don't generate a temporary. */
4552 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4553 && aggregate_value_p (*from_p, *from_p))
4555 bool use_target;
4557 if (!(rhs_predicate_for (*to_p))(*from_p))
4558 /* If we need a temporary, *to_p isn't accurate. */
4559 use_target = false;
4560 /* It's OK to use the return slot directly unless it's an NRV. */
4561 else if (TREE_CODE (*to_p) == RESULT_DECL
4562 && DECL_NAME (*to_p) == NULL_TREE
4563 && needs_to_live_in_memory (*to_p))
4564 use_target = true;
4565 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4566 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4567 /* Don't force regs into memory. */
4568 use_target = false;
4569 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4570 /* It's OK to use the target directly if it's being
4571 initialized. */
4572 use_target = true;
4573 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4574 /* Always use the target and thus RSO for variable-sized types.
4575 GIMPLE cannot deal with a variable-sized assignment
4576 embedded in a call statement. */
4577 use_target = true;
4578 else if (TREE_CODE (*to_p) != SSA_NAME
4579 && (!is_gimple_variable (*to_p)
4580 || needs_to_live_in_memory (*to_p)))
4581 /* Don't use the original target if it's already addressable;
4582 if its address escapes, and the called function uses the
4583 NRV optimization, a conforming program could see *to_p
4584 change before the called function returns; see c++/19317.
4585 When optimizing, the return_slot pass marks more functions
4586 as safe after we have escape info. */
4587 use_target = false;
4588 else
4589 use_target = true;
4591 if (use_target)
4593 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4594 mark_addressable (*to_p);
4597 break;
4599 case WITH_SIZE_EXPR:
4600 /* Likewise for calls that return an aggregate of non-constant size,
4601 since we would not be able to generate a temporary at all. */
4602 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4604 *from_p = TREE_OPERAND (*from_p, 0);
4605 /* We don't change ret in this case because the
4606 WITH_SIZE_EXPR might have been added in
4607 gimplify_modify_expr, so returning GS_OK would lead to an
4608 infinite loop. */
4609 changed = true;
4611 break;
4613 /* If we're initializing from a container, push the initialization
4614 inside it. */
4615 case CLEANUP_POINT_EXPR:
4616 case BIND_EXPR:
4617 case STATEMENT_LIST:
4619 tree wrap = *from_p;
4620 tree t;
4622 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4623 fb_lvalue);
4624 if (ret != GS_ERROR)
4625 ret = GS_OK;
4627 t = voidify_wrapper_expr (wrap, *expr_p);
4628 gcc_assert (t == *expr_p);
4630 if (want_value)
4632 gimplify_and_add (wrap, pre_p);
4633 *expr_p = unshare_expr (*to_p);
4635 else
4636 *expr_p = wrap;
4637 return GS_OK;
4640 case COMPOUND_LITERAL_EXPR:
4642 tree complit = TREE_OPERAND (*expr_p, 1);
4643 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4644 tree decl = DECL_EXPR_DECL (decl_s);
4645 tree init = DECL_INITIAL (decl);
4647 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4648 into struct T x = { 0, 1, 2 } if the address of the
4649 compound literal has never been taken. */
4650 if (!TREE_ADDRESSABLE (complit)
4651 && !TREE_ADDRESSABLE (decl)
4652 && init)
4654 *expr_p = copy_node (*expr_p);
4655 TREE_OPERAND (*expr_p, 1) = init;
4656 return GS_OK;
4660 default:
4661 break;
4664 while (changed);
4666 return ret;
4670 /* Return true if T looks like a valid GIMPLE statement. */
4672 static bool
4673 is_gimple_stmt (tree t)
4675 const enum tree_code code = TREE_CODE (t);
4677 switch (code)
4679 case NOP_EXPR:
4680 /* The only valid NOP_EXPR is the empty statement. */
4681 return IS_EMPTY_STMT (t);
4683 case BIND_EXPR:
4684 case COND_EXPR:
4685 /* These are only valid if they're void. */
4686 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4688 case SWITCH_EXPR:
4689 case GOTO_EXPR:
4690 case RETURN_EXPR:
4691 case LABEL_EXPR:
4692 case CASE_LABEL_EXPR:
4693 case TRY_CATCH_EXPR:
4694 case TRY_FINALLY_EXPR:
4695 case EH_FILTER_EXPR:
4696 case CATCH_EXPR:
4697 case ASM_EXPR:
4698 case STATEMENT_LIST:
4699 case OMP_PARALLEL:
4700 case OMP_FOR:
4701 case OMP_SECTIONS:
4702 case OMP_SECTION:
4703 case OMP_SINGLE:
4704 case OMP_MASTER:
4705 case OMP_ORDERED:
4706 case OMP_CRITICAL:
4707 case OMP_TASK:
4708 /* These are always void. */
4709 return true;
4711 case CALL_EXPR:
4712 case MODIFY_EXPR:
4713 case PREDICT_EXPR:
4714 /* These are valid regardless of their type. */
4715 return true;
4717 default:
4718 return false;
4723 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4724 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4725 DECL_GIMPLE_REG_P set.
4727 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4728 other, unmodified part of the complex object just before the total store.
4729 As a consequence, if the object is still uninitialized, an undefined value
4730 will be loaded into a register, which may result in a spurious exception
4731 if the register is floating-point and the value happens to be a signaling
4732 NaN for example. Then the fully-fledged complex operations lowering pass
4733 followed by a DCE pass are necessary in order to fix things up. */
4735 static enum gimplify_status
4736 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4737 bool want_value)
4739 enum tree_code code, ocode;
4740 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4742 lhs = TREE_OPERAND (*expr_p, 0);
4743 rhs = TREE_OPERAND (*expr_p, 1);
4744 code = TREE_CODE (lhs);
4745 lhs = TREE_OPERAND (lhs, 0);
4747 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4748 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4749 TREE_NO_WARNING (other) = 1;
4750 other = get_formal_tmp_var (other, pre_p);
4752 realpart = code == REALPART_EXPR ? rhs : other;
4753 imagpart = code == REALPART_EXPR ? other : rhs;
4755 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4756 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4757 else
4758 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4760 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4761 *expr_p = (want_value) ? rhs : NULL_TREE;
4763 return GS_ALL_DONE;
4766 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4768 modify_expr
4769 : varname '=' rhs
4770 | '*' ID '=' rhs
4772 PRE_P points to the list where side effects that must happen before
4773 *EXPR_P should be stored.
4775 POST_P points to the list where side effects that must happen after
4776 *EXPR_P should be stored.
4778 WANT_VALUE is nonzero iff we want to use the value of this expression
4779 in another expression. */
4781 static enum gimplify_status
4782 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4783 bool want_value)
4785 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4786 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4787 enum gimplify_status ret = GS_UNHANDLED;
4788 gimple assign;
4789 location_t loc = EXPR_LOCATION (*expr_p);
4790 gimple_stmt_iterator gsi;
4792 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4793 || TREE_CODE (*expr_p) == INIT_EXPR);
4795 /* Trying to simplify a clobber using normal logic doesn't work,
4796 so handle it here. */
4797 if (TREE_CLOBBER_P (*from_p))
4799 gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4800 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4801 *expr_p = NULL;
4802 return GS_ALL_DONE;
4805 /* Insert pointer conversions required by the middle-end that are not
4806 required by the frontend. This fixes middle-end type checking for
4807 for example gcc.dg/redecl-6.c. */
4808 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4810 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4811 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4812 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4815 /* See if any simplifications can be done based on what the RHS is. */
4816 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4817 want_value);
4818 if (ret != GS_UNHANDLED)
4819 return ret;
4821 /* For zero sized types only gimplify the left hand side and right hand
4822 side as statements and throw away the assignment. Do this after
4823 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4824 types properly. */
4825 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4827 gimplify_stmt (from_p, pre_p);
4828 gimplify_stmt (to_p, pre_p);
4829 *expr_p = NULL_TREE;
4830 return GS_ALL_DONE;
4833 /* If the value being copied is of variable width, compute the length
4834 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4835 before gimplifying any of the operands so that we can resolve any
4836 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4837 the size of the expression to be copied, not of the destination, so
4838 that is what we must do here. */
4839 maybe_with_size_expr (from_p);
4841 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4842 if (ret == GS_ERROR)
4843 return ret;
4845 /* As a special case, we have to temporarily allow for assignments
4846 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4847 a toplevel statement, when gimplifying the GENERIC expression
4848 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4849 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4851 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4852 prevent gimplify_expr from trying to create a new temporary for
4853 foo's LHS, we tell it that it should only gimplify until it
4854 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4855 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4856 and all we need to do here is set 'a' to be its LHS. */
4857 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4858 fb_rvalue);
4859 if (ret == GS_ERROR)
4860 return ret;
4862 /* Now see if the above changed *from_p to something we handle specially. */
4863 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4864 want_value);
4865 if (ret != GS_UNHANDLED)
4866 return ret;
4868 /* If we've got a variable sized assignment between two lvalues (i.e. does
4869 not involve a call), then we can make things a bit more straightforward
4870 by converting the assignment to memcpy or memset. */
4871 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4873 tree from = TREE_OPERAND (*from_p, 0);
4874 tree size = TREE_OPERAND (*from_p, 1);
4876 if (TREE_CODE (from) == CONSTRUCTOR)
4877 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4879 if (is_gimple_addressable (from))
4881 *from_p = from;
4882 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4883 pre_p);
4887 /* Transform partial stores to non-addressable complex variables into
4888 total stores. This allows us to use real instead of virtual operands
4889 for these variables, which improves optimization. */
4890 if ((TREE_CODE (*to_p) == REALPART_EXPR
4891 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4892 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4893 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4895 /* Try to alleviate the effects of the gimplification creating artificial
4896 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4897 if (!gimplify_ctxp->into_ssa
4898 && TREE_CODE (*from_p) == VAR_DECL
4899 && DECL_IGNORED_P (*from_p)
4900 && DECL_P (*to_p)
4901 && !DECL_IGNORED_P (*to_p))
4903 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4904 DECL_NAME (*from_p)
4905 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4906 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4907 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4910 if (want_value && TREE_THIS_VOLATILE (*to_p))
4911 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4913 if (TREE_CODE (*from_p) == CALL_EXPR)
4915 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4916 instead of a GIMPLE_ASSIGN. */
4917 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4918 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4919 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4920 assign = gimple_build_call_from_tree (*from_p);
4921 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4922 if (!gimple_call_noreturn_p (assign))
4923 gimple_call_set_lhs (assign, *to_p);
4925 else
4927 assign = gimple_build_assign (*to_p, *from_p);
4928 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4931 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4933 /* If we've somehow already got an SSA_NAME on the LHS, then
4934 we've probably modified it twice. Not good. */
4935 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
4936 *to_p = make_ssa_name (*to_p, assign);
4937 gimple_set_lhs (assign, *to_p);
4940 gimplify_seq_add_stmt (pre_p, assign);
4941 gsi = gsi_last (*pre_p);
4942 fold_stmt (&gsi);
4944 if (want_value)
4946 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4947 return GS_OK;
4949 else
4950 *expr_p = NULL;
4952 return GS_ALL_DONE;
4955 /* Gimplify a comparison between two variable-sized objects. Do this
4956 with a call to BUILT_IN_MEMCMP. */
4958 static enum gimplify_status
4959 gimplify_variable_sized_compare (tree *expr_p)
4961 location_t loc = EXPR_LOCATION (*expr_p);
4962 tree op0 = TREE_OPERAND (*expr_p, 0);
4963 tree op1 = TREE_OPERAND (*expr_p, 1);
4964 tree t, arg, dest, src, expr;
4966 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4967 arg = unshare_expr (arg);
4968 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4969 src = build_fold_addr_expr_loc (loc, op1);
4970 dest = build_fold_addr_expr_loc (loc, op0);
4971 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4972 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4974 expr
4975 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4976 SET_EXPR_LOCATION (expr, loc);
4977 *expr_p = expr;
4979 return GS_OK;
4982 /* Gimplify a comparison between two aggregate objects of integral scalar
4983 mode as a comparison between the bitwise equivalent scalar values. */
4985 static enum gimplify_status
4986 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4988 location_t loc = EXPR_LOCATION (*expr_p);
4989 tree op0 = TREE_OPERAND (*expr_p, 0);
4990 tree op1 = TREE_OPERAND (*expr_p, 1);
4992 tree type = TREE_TYPE (op0);
4993 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4995 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4996 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4998 *expr_p
4999 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5001 return GS_OK;
5004 /* Gimplify an expression sequence. This function gimplifies each
5005 expression and rewrites the original expression with the last
5006 expression of the sequence in GIMPLE form.
5008 PRE_P points to the list where the side effects for all the
5009 expressions in the sequence will be emitted.
5011 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5013 static enum gimplify_status
5014 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5016 tree t = *expr_p;
5020 tree *sub_p = &TREE_OPERAND (t, 0);
5022 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5023 gimplify_compound_expr (sub_p, pre_p, false);
5024 else
5025 gimplify_stmt (sub_p, pre_p);
5027 t = TREE_OPERAND (t, 1);
5029 while (TREE_CODE (t) == COMPOUND_EXPR);
5031 *expr_p = t;
5032 if (want_value)
5033 return GS_OK;
5034 else
5036 gimplify_stmt (expr_p, pre_p);
5037 return GS_ALL_DONE;
5041 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5042 gimplify. After gimplification, EXPR_P will point to a new temporary
5043 that holds the original value of the SAVE_EXPR node.
5045 PRE_P points to the list where side effects that must happen before
5046 *EXPR_P should be stored. */
5048 static enum gimplify_status
5049 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5051 enum gimplify_status ret = GS_ALL_DONE;
5052 tree val;
5054 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5055 val = TREE_OPERAND (*expr_p, 0);
5057 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5058 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5060 /* The operand may be a void-valued expression such as SAVE_EXPRs
5061 generated by the Java frontend for class initialization. It is
5062 being executed only for its side-effects. */
5063 if (TREE_TYPE (val) == void_type_node)
5065 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5066 is_gimple_stmt, fb_none);
5067 val = NULL;
5069 else
5070 val = get_initialized_tmp_var (val, pre_p, post_p);
5072 TREE_OPERAND (*expr_p, 0) = val;
5073 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5076 *expr_p = val;
5078 return ret;
5081 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5083 unary_expr
5084 : ...
5085 | '&' varname
5088 PRE_P points to the list where side effects that must happen before
5089 *EXPR_P should be stored.
5091 POST_P points to the list where side effects that must happen after
5092 *EXPR_P should be stored. */
5094 static enum gimplify_status
5095 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5097 tree expr = *expr_p;
5098 tree op0 = TREE_OPERAND (expr, 0);
5099 enum gimplify_status ret;
5100 location_t loc = EXPR_LOCATION (*expr_p);
5102 switch (TREE_CODE (op0))
5104 case INDIRECT_REF:
5105 do_indirect_ref:
5106 /* Check if we are dealing with an expression of the form '&*ptr'.
5107 While the front end folds away '&*ptr' into 'ptr', these
5108 expressions may be generated internally by the compiler (e.g.,
5109 builtins like __builtin_va_end). */
5110 /* Caution: the silent array decomposition semantics we allow for
5111 ADDR_EXPR means we can't always discard the pair. */
5112 /* Gimplification of the ADDR_EXPR operand may drop
5113 cv-qualification conversions, so make sure we add them if
5114 needed. */
5116 tree op00 = TREE_OPERAND (op0, 0);
5117 tree t_expr = TREE_TYPE (expr);
5118 tree t_op00 = TREE_TYPE (op00);
5120 if (!useless_type_conversion_p (t_expr, t_op00))
5121 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5122 *expr_p = op00;
5123 ret = GS_OK;
5125 break;
5127 case VIEW_CONVERT_EXPR:
5128 /* Take the address of our operand and then convert it to the type of
5129 this ADDR_EXPR.
5131 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5132 all clear. The impact of this transformation is even less clear. */
5134 /* If the operand is a useless conversion, look through it. Doing so
5135 guarantees that the ADDR_EXPR and its operand will remain of the
5136 same type. */
5137 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5138 op0 = TREE_OPERAND (op0, 0);
5140 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5141 build_fold_addr_expr_loc (loc,
5142 TREE_OPERAND (op0, 0)));
5143 ret = GS_OK;
5144 break;
5146 default:
5147 /* We use fb_either here because the C frontend sometimes takes
5148 the address of a call that returns a struct; see
5149 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5150 the implied temporary explicit. */
5152 /* Make the operand addressable. */
5153 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5154 is_gimple_addressable, fb_either);
5155 if (ret == GS_ERROR)
5156 break;
5158 /* Then mark it. Beware that it may not be possible to do so directly
5159 if a temporary has been created by the gimplification. */
5160 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5162 op0 = TREE_OPERAND (expr, 0);
5164 /* For various reasons, the gimplification of the expression
5165 may have made a new INDIRECT_REF. */
5166 if (TREE_CODE (op0) == INDIRECT_REF)
5167 goto do_indirect_ref;
5169 mark_addressable (TREE_OPERAND (expr, 0));
5171 /* The FEs may end up building ADDR_EXPRs early on a decl with
5172 an incomplete type. Re-build ADDR_EXPRs in canonical form
5173 here. */
5174 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5175 *expr_p = build_fold_addr_expr (op0);
5177 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5178 recompute_tree_invariant_for_addr_expr (*expr_p);
5180 /* If we re-built the ADDR_EXPR add a conversion to the original type
5181 if required. */
5182 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5183 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5185 break;
5188 return ret;
5191 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5192 value; output operands should be a gimple lvalue. */
5194 static enum gimplify_status
5195 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5197 tree expr;
5198 int noutputs;
5199 const char **oconstraints;
5200 int i;
5201 tree link;
5202 const char *constraint;
5203 bool allows_mem, allows_reg, is_inout;
5204 enum gimplify_status ret, tret;
5205 gimple stmt;
5206 VEC(tree, gc) *inputs;
5207 VEC(tree, gc) *outputs;
5208 VEC(tree, gc) *clobbers;
5209 VEC(tree, gc) *labels;
5210 tree link_next;
5212 expr = *expr_p;
5213 noutputs = list_length (ASM_OUTPUTS (expr));
5214 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5216 inputs = outputs = clobbers = labels = NULL;
5218 ret = GS_ALL_DONE;
5219 link_next = NULL_TREE;
5220 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5222 bool ok;
5223 size_t constraint_len;
5225 link_next = TREE_CHAIN (link);
5227 oconstraints[i]
5228 = constraint
5229 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5230 constraint_len = strlen (constraint);
5231 if (constraint_len == 0)
5232 continue;
5234 ok = parse_output_constraint (&constraint, i, 0, 0,
5235 &allows_mem, &allows_reg, &is_inout);
5236 if (!ok)
5238 ret = GS_ERROR;
5239 is_inout = false;
5242 if (!allows_reg && allows_mem)
5243 mark_addressable (TREE_VALUE (link));
5245 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5246 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5247 fb_lvalue | fb_mayfail);
5248 if (tret == GS_ERROR)
5250 error ("invalid lvalue in asm output %d", i);
5251 ret = tret;
5254 VEC_safe_push (tree, gc, outputs, link);
5255 TREE_CHAIN (link) = NULL_TREE;
5257 if (is_inout)
5259 /* An input/output operand. To give the optimizers more
5260 flexibility, split it into separate input and output
5261 operands. */
5262 tree input;
5263 char buf[10];
5265 /* Turn the in/out constraint into an output constraint. */
5266 char *p = xstrdup (constraint);
5267 p[0] = '=';
5268 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5270 /* And add a matching input constraint. */
5271 if (allows_reg)
5273 sprintf (buf, "%d", i);
5275 /* If there are multiple alternatives in the constraint,
5276 handle each of them individually. Those that allow register
5277 will be replaced with operand number, the others will stay
5278 unchanged. */
5279 if (strchr (p, ',') != NULL)
5281 size_t len = 0, buflen = strlen (buf);
5282 char *beg, *end, *str, *dst;
5284 for (beg = p + 1;;)
5286 end = strchr (beg, ',');
5287 if (end == NULL)
5288 end = strchr (beg, '\0');
5289 if ((size_t) (end - beg) < buflen)
5290 len += buflen + 1;
5291 else
5292 len += end - beg + 1;
5293 if (*end)
5294 beg = end + 1;
5295 else
5296 break;
5299 str = (char *) alloca (len);
5300 for (beg = p + 1, dst = str;;)
5302 const char *tem;
5303 bool mem_p, reg_p, inout_p;
5305 end = strchr (beg, ',');
5306 if (end)
5307 *end = '\0';
5308 beg[-1] = '=';
5309 tem = beg - 1;
5310 parse_output_constraint (&tem, i, 0, 0,
5311 &mem_p, &reg_p, &inout_p);
5312 if (dst != str)
5313 *dst++ = ',';
5314 if (reg_p)
5316 memcpy (dst, buf, buflen);
5317 dst += buflen;
5319 else
5321 if (end)
5322 len = end - beg;
5323 else
5324 len = strlen (beg);
5325 memcpy (dst, beg, len);
5326 dst += len;
5328 if (end)
5329 beg = end + 1;
5330 else
5331 break;
5333 *dst = '\0';
5334 input = build_string (dst - str, str);
5336 else
5337 input = build_string (strlen (buf), buf);
5339 else
5340 input = build_string (constraint_len - 1, constraint + 1);
5342 free (p);
5344 input = build_tree_list (build_tree_list (NULL_TREE, input),
5345 unshare_expr (TREE_VALUE (link)));
5346 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5350 link_next = NULL_TREE;
5351 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5353 link_next = TREE_CHAIN (link);
5354 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5355 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5356 oconstraints, &allows_mem, &allows_reg);
5358 /* If we can't make copies, we can only accept memory. */
5359 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5361 if (allows_mem)
5362 allows_reg = 0;
5363 else
5365 error ("impossible constraint in %<asm%>");
5366 error ("non-memory input %d must stay in memory", i);
5367 return GS_ERROR;
5371 /* If the operand is a memory input, it should be an lvalue. */
5372 if (!allows_reg && allows_mem)
5374 tree inputv = TREE_VALUE (link);
5375 STRIP_NOPS (inputv);
5376 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5377 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5378 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5379 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5380 TREE_VALUE (link) = error_mark_node;
5381 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5382 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5383 mark_addressable (TREE_VALUE (link));
5384 if (tret == GS_ERROR)
5386 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5387 input_location = EXPR_LOCATION (TREE_VALUE (link));
5388 error ("memory input %d is not directly addressable", i);
5389 ret = tret;
5392 else
5394 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5395 is_gimple_asm_val, fb_rvalue);
5396 if (tret == GS_ERROR)
5397 ret = tret;
5400 TREE_CHAIN (link) = NULL_TREE;
5401 VEC_safe_push (tree, gc, inputs, link);
5404 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5405 VEC_safe_push (tree, gc, clobbers, link);
5407 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5408 VEC_safe_push (tree, gc, labels, link);
5410 /* Do not add ASMs with errors to the gimple IL stream. */
5411 if (ret != GS_ERROR)
5413 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5414 inputs, outputs, clobbers, labels);
5416 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5417 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5419 gimplify_seq_add_stmt (pre_p, stmt);
5422 return ret;
5425 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5426 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5427 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5428 return to this function.
5430 FIXME should we complexify the prequeue handling instead? Or use flags
5431 for all the cleanups and let the optimizer tighten them up? The current
5432 code seems pretty fragile; it will break on a cleanup within any
5433 non-conditional nesting. But any such nesting would be broken, anyway;
5434 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5435 and continues out of it. We can do that at the RTL level, though, so
5436 having an optimizer to tighten up try/finally regions would be a Good
5437 Thing. */
5439 static enum gimplify_status
5440 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5442 gimple_stmt_iterator iter;
5443 gimple_seq body_sequence = NULL;
5445 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5447 /* We only care about the number of conditions between the innermost
5448 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5449 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5450 int old_conds = gimplify_ctxp->conditions;
5451 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5452 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5453 gimplify_ctxp->conditions = 0;
5454 gimplify_ctxp->conditional_cleanups = NULL;
5455 gimplify_ctxp->in_cleanup_point_expr = true;
5457 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5459 gimplify_ctxp->conditions = old_conds;
5460 gimplify_ctxp->conditional_cleanups = old_cleanups;
5461 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5463 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5465 gimple wce = gsi_stmt (iter);
5467 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5469 if (gsi_one_before_end_p (iter))
5471 /* Note that gsi_insert_seq_before and gsi_remove do not
5472 scan operands, unlike some other sequence mutators. */
5473 if (!gimple_wce_cleanup_eh_only (wce))
5474 gsi_insert_seq_before_without_update (&iter,
5475 gimple_wce_cleanup (wce),
5476 GSI_SAME_STMT);
5477 gsi_remove (&iter, true);
5478 break;
5480 else
5482 gimple gtry;
5483 gimple_seq seq;
5484 enum gimple_try_flags kind;
5486 if (gimple_wce_cleanup_eh_only (wce))
5487 kind = GIMPLE_TRY_CATCH;
5488 else
5489 kind = GIMPLE_TRY_FINALLY;
5490 seq = gsi_split_seq_after (iter);
5492 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5493 /* Do not use gsi_replace here, as it may scan operands.
5494 We want to do a simple structural modification only. */
5495 gsi_set_stmt (&iter, gtry);
5496 iter = gsi_start (gtry->gimple_try.eval);
5499 else
5500 gsi_next (&iter);
5503 gimplify_seq_add_seq (pre_p, body_sequence);
5504 if (temp)
5506 *expr_p = temp;
5507 return GS_OK;
5509 else
5511 *expr_p = NULL;
5512 return GS_ALL_DONE;
5516 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5517 is the cleanup action required. EH_ONLY is true if the cleanup should
5518 only be executed if an exception is thrown, not on normal exit. */
5520 static void
5521 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5523 gimple wce;
5524 gimple_seq cleanup_stmts = NULL;
5526 /* Errors can result in improperly nested cleanups. Which results in
5527 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5528 if (seen_error ())
5529 return;
5531 if (gimple_conditional_context ())
5533 /* If we're in a conditional context, this is more complex. We only
5534 want to run the cleanup if we actually ran the initialization that
5535 necessitates it, but we want to run it after the end of the
5536 conditional context. So we wrap the try/finally around the
5537 condition and use a flag to determine whether or not to actually
5538 run the destructor. Thus
5540 test ? f(A()) : 0
5542 becomes (approximately)
5544 flag = 0;
5545 try {
5546 if (test) { A::A(temp); flag = 1; val = f(temp); }
5547 else { val = 0; }
5548 } finally {
5549 if (flag) A::~A(temp);
5553 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5554 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5555 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5557 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5558 gimplify_stmt (&cleanup, &cleanup_stmts);
5559 wce = gimple_build_wce (cleanup_stmts);
5561 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5562 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5563 gimplify_seq_add_stmt (pre_p, ftrue);
5565 /* Because of this manipulation, and the EH edges that jump
5566 threading cannot redirect, the temporary (VAR) will appear
5567 to be used uninitialized. Don't warn. */
5568 TREE_NO_WARNING (var) = 1;
5570 else
5572 gimplify_stmt (&cleanup, &cleanup_stmts);
5573 wce = gimple_build_wce (cleanup_stmts);
5574 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5575 gimplify_seq_add_stmt (pre_p, wce);
5579 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5581 static enum gimplify_status
5582 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5584 tree targ = *expr_p;
5585 tree temp = TARGET_EXPR_SLOT (targ);
5586 tree init = TARGET_EXPR_INITIAL (targ);
5587 enum gimplify_status ret;
5589 if (init)
5591 tree cleanup = NULL_TREE;
5593 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5594 to the temps list. Handle also variable length TARGET_EXPRs. */
5595 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5597 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5598 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5599 gimplify_vla_decl (temp, pre_p);
5601 else
5602 gimple_add_tmp_var (temp);
5604 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5605 expression is supposed to initialize the slot. */
5606 if (VOID_TYPE_P (TREE_TYPE (init)))
5607 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5608 else
5610 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5611 init = init_expr;
5612 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5613 init = NULL;
5614 ggc_free (init_expr);
5616 if (ret == GS_ERROR)
5618 /* PR c++/28266 Make sure this is expanded only once. */
5619 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5620 return GS_ERROR;
5622 if (init)
5623 gimplify_and_add (init, pre_p);
5625 /* If needed, push the cleanup for the temp. */
5626 if (TARGET_EXPR_CLEANUP (targ))
5628 if (CLEANUP_EH_ONLY (targ))
5629 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5630 CLEANUP_EH_ONLY (targ), pre_p);
5631 else
5632 cleanup = TARGET_EXPR_CLEANUP (targ);
5635 /* Add a clobber for the temporary going out of scope, like
5636 gimplify_bind_expr. */
5637 if (gimplify_ctxp->in_cleanup_point_expr
5638 && needs_to_live_in_memory (temp)
5639 && flag_stack_reuse == SR_ALL)
5641 tree clobber = build_constructor (TREE_TYPE (temp), NULL);
5642 TREE_THIS_VOLATILE (clobber) = true;
5643 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5644 if (cleanup)
5645 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5646 clobber);
5647 else
5648 cleanup = clobber;
5651 if (cleanup)
5652 gimple_push_cleanup (temp, cleanup, false, pre_p);
5654 /* Only expand this once. */
5655 TREE_OPERAND (targ, 3) = init;
5656 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5658 else
5659 /* We should have expanded this before. */
5660 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5662 *expr_p = temp;
5663 return GS_OK;
5666 /* Gimplification of expression trees. */
5668 /* Gimplify an expression which appears at statement context. The
5669 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5670 NULL, a new sequence is allocated.
5672 Return true if we actually added a statement to the queue. */
5674 bool
5675 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5677 gimple_seq_node last;
5679 last = gimple_seq_last (*seq_p);
5680 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5681 return last != gimple_seq_last (*seq_p);
5684 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5685 to CTX. If entries already exist, force them to be some flavor of private.
5686 If there is no enclosing parallel, do nothing. */
5688 void
5689 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5691 splay_tree_node n;
5693 if (decl == NULL || !DECL_P (decl))
5694 return;
5698 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5699 if (n != NULL)
5701 if (n->value & GOVD_SHARED)
5702 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5703 else
5704 return;
5706 else if (ctx->region_type != ORT_WORKSHARE)
5707 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5709 ctx = ctx->outer_context;
5711 while (ctx);
5714 /* Similarly for each of the type sizes of TYPE. */
5716 static void
5717 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5719 if (type == NULL || type == error_mark_node)
5720 return;
5721 type = TYPE_MAIN_VARIANT (type);
5723 if (pointer_set_insert (ctx->privatized_types, type))
5724 return;
5726 switch (TREE_CODE (type))
5728 case INTEGER_TYPE:
5729 case ENUMERAL_TYPE:
5730 case BOOLEAN_TYPE:
5731 case REAL_TYPE:
5732 case FIXED_POINT_TYPE:
5733 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5734 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5735 break;
5737 case ARRAY_TYPE:
5738 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5739 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5740 break;
5742 case RECORD_TYPE:
5743 case UNION_TYPE:
5744 case QUAL_UNION_TYPE:
5746 tree field;
5747 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5748 if (TREE_CODE (field) == FIELD_DECL)
5750 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5751 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5754 break;
5756 case POINTER_TYPE:
5757 case REFERENCE_TYPE:
5758 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5759 break;
5761 default:
5762 break;
5765 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5766 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5767 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5770 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5772 static void
5773 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5775 splay_tree_node n;
5776 unsigned int nflags;
5777 tree t;
5779 if (error_operand_p (decl))
5780 return;
5782 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5783 there are constructors involved somewhere. */
5784 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5785 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5786 flags |= GOVD_SEEN;
5788 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5789 if (n != NULL)
5791 /* We shouldn't be re-adding the decl with the same data
5792 sharing class. */
5793 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5794 /* The only combination of data sharing classes we should see is
5795 FIRSTPRIVATE and LASTPRIVATE. */
5796 nflags = n->value | flags;
5797 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5798 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5799 n->value = nflags;
5800 return;
5803 /* When adding a variable-sized variable, we have to handle all sorts
5804 of additional bits of data: the pointer replacement variable, and
5805 the parameters of the type. */
5806 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5808 /* Add the pointer replacement variable as PRIVATE if the variable
5809 replacement is private, else FIRSTPRIVATE since we'll need the
5810 address of the original variable either for SHARED, or for the
5811 copy into or out of the context. */
5812 if (!(flags & GOVD_LOCAL))
5814 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5815 nflags |= flags & GOVD_SEEN;
5816 t = DECL_VALUE_EXPR (decl);
5817 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5818 t = TREE_OPERAND (t, 0);
5819 gcc_assert (DECL_P (t));
5820 omp_add_variable (ctx, t, nflags);
5823 /* Add all of the variable and type parameters (which should have
5824 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5825 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5826 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5827 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5829 /* The variable-sized variable itself is never SHARED, only some form
5830 of PRIVATE. The sharing would take place via the pointer variable
5831 which we remapped above. */
5832 if (flags & GOVD_SHARED)
5833 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5834 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5836 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5837 alloca statement we generate for the variable, so make sure it
5838 is available. This isn't automatically needed for the SHARED
5839 case, since we won't be allocating local storage then.
5840 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5841 in this case omp_notice_variable will be called later
5842 on when it is gimplified. */
5843 else if (! (flags & GOVD_LOCAL)
5844 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5845 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5847 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5849 gcc_assert ((flags & GOVD_LOCAL) == 0);
5850 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5852 /* Similar to the direct variable sized case above, we'll need the
5853 size of references being privatized. */
5854 if ((flags & GOVD_SHARED) == 0)
5856 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5857 if (TREE_CODE (t) != INTEGER_CST)
5858 omp_notice_variable (ctx, t, true);
5862 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5865 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5866 This just prints out diagnostics about threadprivate variable uses
5867 in untied tasks. If DECL2 is non-NULL, prevent this warning
5868 on that variable. */
5870 static bool
5871 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5872 tree decl2)
5874 splay_tree_node n;
5876 if (ctx->region_type != ORT_UNTIED_TASK)
5877 return false;
5878 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5879 if (n == NULL)
5881 error ("threadprivate variable %qE used in untied task",
5882 DECL_NAME (decl));
5883 error_at (ctx->location, "enclosing task");
5884 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5886 if (decl2)
5887 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5888 return false;
5891 /* Record the fact that DECL was used within the OpenMP context CTX.
5892 IN_CODE is true when real code uses DECL, and false when we should
5893 merely emit default(none) errors. Return true if DECL is going to
5894 be remapped and thus DECL shouldn't be gimplified into its
5895 DECL_VALUE_EXPR (if any). */
5897 static bool
5898 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5900 splay_tree_node n;
5901 unsigned flags = in_code ? GOVD_SEEN : 0;
5902 bool ret = false, shared;
5904 if (error_operand_p (decl))
5905 return false;
5907 /* Threadprivate variables are predetermined. */
5908 if (is_global_var (decl))
5910 if (DECL_THREAD_LOCAL_P (decl))
5911 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5913 if (DECL_HAS_VALUE_EXPR_P (decl))
5915 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5917 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5918 return omp_notice_threadprivate_variable (ctx, decl, value);
5922 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5923 if (n == NULL)
5925 enum omp_clause_default_kind default_kind, kind;
5926 struct gimplify_omp_ctx *octx;
5928 if (ctx->region_type == ORT_WORKSHARE)
5929 goto do_outer;
5931 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5932 remapped firstprivate instead of shared. To some extent this is
5933 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5934 default_kind = ctx->default_kind;
5935 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5936 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5937 default_kind = kind;
5939 switch (default_kind)
5941 case OMP_CLAUSE_DEFAULT_NONE:
5942 error ("%qE not specified in enclosing parallel",
5943 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5944 if ((ctx->region_type & ORT_TASK) != 0)
5945 error_at (ctx->location, "enclosing task");
5946 else
5947 error_at (ctx->location, "enclosing parallel");
5948 /* FALLTHRU */
5949 case OMP_CLAUSE_DEFAULT_SHARED:
5950 flags |= GOVD_SHARED;
5951 break;
5952 case OMP_CLAUSE_DEFAULT_PRIVATE:
5953 flags |= GOVD_PRIVATE;
5954 break;
5955 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5956 flags |= GOVD_FIRSTPRIVATE;
5957 break;
5958 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5959 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5960 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5961 if (ctx->outer_context)
5962 omp_notice_variable (ctx->outer_context, decl, in_code);
5963 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5965 splay_tree_node n2;
5967 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5968 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5970 flags |= GOVD_FIRSTPRIVATE;
5971 break;
5973 if ((octx->region_type & ORT_PARALLEL) != 0)
5974 break;
5976 if (flags & GOVD_FIRSTPRIVATE)
5977 break;
5978 if (octx == NULL
5979 && (TREE_CODE (decl) == PARM_DECL
5980 || (!is_global_var (decl)
5981 && DECL_CONTEXT (decl) == current_function_decl)))
5983 flags |= GOVD_FIRSTPRIVATE;
5984 break;
5986 flags |= GOVD_SHARED;
5987 break;
5988 default:
5989 gcc_unreachable ();
5992 if ((flags & GOVD_PRIVATE)
5993 && lang_hooks.decls.omp_private_outer_ref (decl))
5994 flags |= GOVD_PRIVATE_OUTER_REF;
5996 omp_add_variable (ctx, decl, flags);
5998 shared = (flags & GOVD_SHARED) != 0;
5999 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6000 goto do_outer;
6003 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6004 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6005 && DECL_SIZE (decl)
6006 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6008 splay_tree_node n2;
6009 tree t = DECL_VALUE_EXPR (decl);
6010 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6011 t = TREE_OPERAND (t, 0);
6012 gcc_assert (DECL_P (t));
6013 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6014 n2->value |= GOVD_SEEN;
6017 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6018 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6020 /* If nothing changed, there's nothing left to do. */
6021 if ((n->value & flags) == flags)
6022 return ret;
6023 flags |= n->value;
6024 n->value = flags;
6026 do_outer:
6027 /* If the variable is private in the current context, then we don't
6028 need to propagate anything to an outer context. */
6029 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6030 return ret;
6031 if (ctx->outer_context
6032 && omp_notice_variable (ctx->outer_context, decl, in_code))
6033 return true;
6034 return ret;
6037 /* Verify that DECL is private within CTX. If there's specific information
6038 to the contrary in the innermost scope, generate an error. */
6040 static bool
6041 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
6043 splay_tree_node n;
6045 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6046 if (n != NULL)
6048 if (n->value & GOVD_SHARED)
6050 if (ctx == gimplify_omp_ctxp)
6052 error ("iteration variable %qE should be private",
6053 DECL_NAME (decl));
6054 n->value = GOVD_PRIVATE;
6055 return true;
6057 else
6058 return false;
6060 else if ((n->value & GOVD_EXPLICIT) != 0
6061 && (ctx == gimplify_omp_ctxp
6062 || (ctx->region_type == ORT_COMBINED_PARALLEL
6063 && gimplify_omp_ctxp->outer_context == ctx)))
6065 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6066 error ("iteration variable %qE should not be firstprivate",
6067 DECL_NAME (decl));
6068 else if ((n->value & GOVD_REDUCTION) != 0)
6069 error ("iteration variable %qE should not be reduction",
6070 DECL_NAME (decl));
6072 return (ctx == gimplify_omp_ctxp
6073 || (ctx->region_type == ORT_COMBINED_PARALLEL
6074 && gimplify_omp_ctxp->outer_context == ctx));
6077 if (ctx->region_type != ORT_WORKSHARE)
6078 return false;
6079 else if (ctx->outer_context)
6080 return omp_is_private (ctx->outer_context, decl);
6081 return false;
6084 /* Return true if DECL is private within a parallel region
6085 that binds to the current construct's context or in parallel
6086 region's REDUCTION clause. */
6088 static bool
6089 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6091 splay_tree_node n;
6095 ctx = ctx->outer_context;
6096 if (ctx == NULL)
6097 return !(is_global_var (decl)
6098 /* References might be private, but might be shared too. */
6099 || lang_hooks.decls.omp_privatize_by_reference (decl));
6101 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6102 if (n != NULL)
6103 return (n->value & GOVD_SHARED) == 0;
6105 while (ctx->region_type == ORT_WORKSHARE);
6106 return false;
6109 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6110 and previous omp contexts. */
6112 static void
6113 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6114 enum omp_region_type region_type)
6116 struct gimplify_omp_ctx *ctx, *outer_ctx;
6117 struct gimplify_ctx gctx;
6118 tree c;
6120 ctx = new_omp_context (region_type);
6121 outer_ctx = ctx->outer_context;
6123 while ((c = *list_p) != NULL)
6125 bool remove = false;
6126 bool notice_outer = true;
6127 const char *check_non_private = NULL;
6128 unsigned int flags;
6129 tree decl;
6131 switch (OMP_CLAUSE_CODE (c))
6133 case OMP_CLAUSE_PRIVATE:
6134 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6135 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6137 flags |= GOVD_PRIVATE_OUTER_REF;
6138 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6140 else
6141 notice_outer = false;
6142 goto do_add;
6143 case OMP_CLAUSE_SHARED:
6144 flags = GOVD_SHARED | GOVD_EXPLICIT;
6145 goto do_add;
6146 case OMP_CLAUSE_FIRSTPRIVATE:
6147 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6148 check_non_private = "firstprivate";
6149 goto do_add;
6150 case OMP_CLAUSE_LASTPRIVATE:
6151 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6152 check_non_private = "lastprivate";
6153 goto do_add;
6154 case OMP_CLAUSE_REDUCTION:
6155 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6156 check_non_private = "reduction";
6157 goto do_add;
6159 do_add:
6160 decl = OMP_CLAUSE_DECL (c);
6161 if (error_operand_p (decl))
6163 remove = true;
6164 break;
6166 omp_add_variable (ctx, decl, flags);
6167 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6168 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6170 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6171 GOVD_LOCAL | GOVD_SEEN);
6172 gimplify_omp_ctxp = ctx;
6173 push_gimplify_context (&gctx);
6175 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6176 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6178 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6179 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6180 pop_gimplify_context
6181 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6182 push_gimplify_context (&gctx);
6183 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6184 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6185 pop_gimplify_context
6186 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6187 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6188 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6190 gimplify_omp_ctxp = outer_ctx;
6192 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6193 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6195 gimplify_omp_ctxp = ctx;
6196 push_gimplify_context (&gctx);
6197 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6199 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6200 NULL, NULL);
6201 TREE_SIDE_EFFECTS (bind) = 1;
6202 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6203 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6205 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6206 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6207 pop_gimplify_context
6208 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6209 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6211 gimplify_omp_ctxp = outer_ctx;
6213 if (notice_outer)
6214 goto do_notice;
6215 break;
6217 case OMP_CLAUSE_COPYIN:
6218 case OMP_CLAUSE_COPYPRIVATE:
6219 decl = OMP_CLAUSE_DECL (c);
6220 if (error_operand_p (decl))
6222 remove = true;
6223 break;
6225 do_notice:
6226 if (outer_ctx)
6227 omp_notice_variable (outer_ctx, decl, true);
6228 if (check_non_private
6229 && region_type == ORT_WORKSHARE
6230 && omp_check_private (ctx, decl))
6232 error ("%s variable %qE is private in outer context",
6233 check_non_private, DECL_NAME (decl));
6234 remove = true;
6236 break;
6238 case OMP_CLAUSE_FINAL:
6239 case OMP_CLAUSE_IF:
6240 OMP_CLAUSE_OPERAND (c, 0)
6241 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6242 /* Fall through. */
6244 case OMP_CLAUSE_SCHEDULE:
6245 case OMP_CLAUSE_NUM_THREADS:
6246 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6247 is_gimple_val, fb_rvalue) == GS_ERROR)
6248 remove = true;
6249 break;
6251 case OMP_CLAUSE_NOWAIT:
6252 case OMP_CLAUSE_ORDERED:
6253 case OMP_CLAUSE_UNTIED:
6254 case OMP_CLAUSE_COLLAPSE:
6255 case OMP_CLAUSE_MERGEABLE:
6256 break;
6258 case OMP_CLAUSE_DEFAULT:
6259 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6260 break;
6262 default:
6263 gcc_unreachable ();
6266 if (remove)
6267 *list_p = OMP_CLAUSE_CHAIN (c);
6268 else
6269 list_p = &OMP_CLAUSE_CHAIN (c);
6272 gimplify_omp_ctxp = ctx;
6275 /* For all variables that were not actually used within the context,
6276 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6278 static int
6279 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6281 tree *list_p = (tree *) data;
6282 tree decl = (tree) n->key;
6283 unsigned flags = n->value;
6284 enum omp_clause_code code;
6285 tree clause;
6286 bool private_debug;
6288 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6289 return 0;
6290 if ((flags & GOVD_SEEN) == 0)
6291 return 0;
6292 if (flags & GOVD_DEBUG_PRIVATE)
6294 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6295 private_debug = true;
6297 else
6298 private_debug
6299 = lang_hooks.decls.omp_private_debug_clause (decl,
6300 !!(flags & GOVD_SHARED));
6301 if (private_debug)
6302 code = OMP_CLAUSE_PRIVATE;
6303 else if (flags & GOVD_SHARED)
6305 if (is_global_var (decl))
6307 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6308 while (ctx != NULL)
6310 splay_tree_node on
6311 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6312 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6313 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6314 break;
6315 ctx = ctx->outer_context;
6317 if (ctx == NULL)
6318 return 0;
6320 code = OMP_CLAUSE_SHARED;
6322 else if (flags & GOVD_PRIVATE)
6323 code = OMP_CLAUSE_PRIVATE;
6324 else if (flags & GOVD_FIRSTPRIVATE)
6325 code = OMP_CLAUSE_FIRSTPRIVATE;
6326 else
6327 gcc_unreachable ();
6329 clause = build_omp_clause (input_location, code);
6330 OMP_CLAUSE_DECL (clause) = decl;
6331 OMP_CLAUSE_CHAIN (clause) = *list_p;
6332 if (private_debug)
6333 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6334 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6335 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6336 *list_p = clause;
6337 lang_hooks.decls.omp_finish_clause (clause);
6339 return 0;
6342 static void
6343 gimplify_adjust_omp_clauses (tree *list_p)
6345 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6346 tree c, decl;
6348 while ((c = *list_p) != NULL)
6350 splay_tree_node n;
6351 bool remove = false;
6353 switch (OMP_CLAUSE_CODE (c))
6355 case OMP_CLAUSE_PRIVATE:
6356 case OMP_CLAUSE_SHARED:
6357 case OMP_CLAUSE_FIRSTPRIVATE:
6358 decl = OMP_CLAUSE_DECL (c);
6359 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6360 remove = !(n->value & GOVD_SEEN);
6361 if (! remove)
6363 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6364 if ((n->value & GOVD_DEBUG_PRIVATE)
6365 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6367 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6368 || ((n->value & GOVD_DATA_SHARE_CLASS)
6369 == GOVD_PRIVATE));
6370 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6371 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6374 break;
6376 case OMP_CLAUSE_LASTPRIVATE:
6377 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6378 accurately reflect the presence of a FIRSTPRIVATE clause. */
6379 decl = OMP_CLAUSE_DECL (c);
6380 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6381 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6382 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6383 break;
6385 case OMP_CLAUSE_REDUCTION:
6386 case OMP_CLAUSE_COPYIN:
6387 case OMP_CLAUSE_COPYPRIVATE:
6388 case OMP_CLAUSE_IF:
6389 case OMP_CLAUSE_NUM_THREADS:
6390 case OMP_CLAUSE_SCHEDULE:
6391 case OMP_CLAUSE_NOWAIT:
6392 case OMP_CLAUSE_ORDERED:
6393 case OMP_CLAUSE_DEFAULT:
6394 case OMP_CLAUSE_UNTIED:
6395 case OMP_CLAUSE_COLLAPSE:
6396 case OMP_CLAUSE_FINAL:
6397 case OMP_CLAUSE_MERGEABLE:
6398 break;
6400 default:
6401 gcc_unreachable ();
6404 if (remove)
6405 *list_p = OMP_CLAUSE_CHAIN (c);
6406 else
6407 list_p = &OMP_CLAUSE_CHAIN (c);
6410 /* Add in any implicit data sharing. */
6411 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6413 gimplify_omp_ctxp = ctx->outer_context;
6414 delete_omp_context (ctx);
6417 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6418 gimplification of the body, as well as scanning the body for used
6419 variables. We need to do this scan now, because variable-sized
6420 decls will be decomposed during gimplification. */
6422 static void
6423 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6425 tree expr = *expr_p;
6426 gimple g;
6427 gimple_seq body = NULL;
6428 struct gimplify_ctx gctx;
6430 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6431 OMP_PARALLEL_COMBINED (expr)
6432 ? ORT_COMBINED_PARALLEL
6433 : ORT_PARALLEL);
6435 push_gimplify_context (&gctx);
6437 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6438 if (gimple_code (g) == GIMPLE_BIND)
6439 pop_gimplify_context (g);
6440 else
6441 pop_gimplify_context (NULL);
6443 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6445 g = gimple_build_omp_parallel (body,
6446 OMP_PARALLEL_CLAUSES (expr),
6447 NULL_TREE, NULL_TREE);
6448 if (OMP_PARALLEL_COMBINED (expr))
6449 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6450 gimplify_seq_add_stmt (pre_p, g);
6451 *expr_p = NULL_TREE;
6454 /* Gimplify the contents of an OMP_TASK statement. This involves
6455 gimplification of the body, as well as scanning the body for used
6456 variables. We need to do this scan now, because variable-sized
6457 decls will be decomposed during gimplification. */
6459 static void
6460 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6462 tree expr = *expr_p;
6463 gimple g;
6464 gimple_seq body = NULL;
6465 struct gimplify_ctx gctx;
6467 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6468 find_omp_clause (OMP_TASK_CLAUSES (expr),
6469 OMP_CLAUSE_UNTIED)
6470 ? ORT_UNTIED_TASK : ORT_TASK);
6472 push_gimplify_context (&gctx);
6474 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6475 if (gimple_code (g) == GIMPLE_BIND)
6476 pop_gimplify_context (g);
6477 else
6478 pop_gimplify_context (NULL);
6480 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6482 g = gimple_build_omp_task (body,
6483 OMP_TASK_CLAUSES (expr),
6484 NULL_TREE, NULL_TREE,
6485 NULL_TREE, NULL_TREE, NULL_TREE);
6486 gimplify_seq_add_stmt (pre_p, g);
6487 *expr_p = NULL_TREE;
6490 /* Gimplify the gross structure of an OMP_FOR statement. */
6492 static enum gimplify_status
6493 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6495 tree for_stmt, decl, var, t;
6496 enum gimplify_status ret = GS_ALL_DONE;
6497 enum gimplify_status tret;
6498 gimple gfor;
6499 gimple_seq for_body, for_pre_body;
6500 int i;
6502 for_stmt = *expr_p;
6504 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6505 ORT_WORKSHARE);
6507 /* Handle OMP_FOR_INIT. */
6508 for_pre_body = NULL;
6509 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6510 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6512 for_body = NULL;
6513 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6514 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6515 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6516 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6517 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6519 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6520 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6521 decl = TREE_OPERAND (t, 0);
6522 gcc_assert (DECL_P (decl));
6523 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6524 || POINTER_TYPE_P (TREE_TYPE (decl)));
6526 /* Make sure the iteration variable is private. */
6527 if (omp_is_private (gimplify_omp_ctxp, decl))
6528 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6529 else
6530 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6532 /* If DECL is not a gimple register, create a temporary variable to act
6533 as an iteration counter. This is valid, since DECL cannot be
6534 modified in the body of the loop. */
6535 if (!is_gimple_reg (decl))
6537 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6538 TREE_OPERAND (t, 0) = var;
6540 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6542 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6544 else
6545 var = decl;
6547 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6548 is_gimple_val, fb_rvalue);
6549 ret = MIN (ret, tret);
6550 if (ret == GS_ERROR)
6551 return ret;
6553 /* Handle OMP_FOR_COND. */
6554 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6555 gcc_assert (COMPARISON_CLASS_P (t));
6556 gcc_assert (TREE_OPERAND (t, 0) == decl);
6558 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6559 is_gimple_val, fb_rvalue);
6560 ret = MIN (ret, tret);
6562 /* Handle OMP_FOR_INCR. */
6563 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6564 switch (TREE_CODE (t))
6566 case PREINCREMENT_EXPR:
6567 case POSTINCREMENT_EXPR:
6568 t = build_int_cst (TREE_TYPE (decl), 1);
6569 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6570 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6571 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6572 break;
6574 case PREDECREMENT_EXPR:
6575 case POSTDECREMENT_EXPR:
6576 t = build_int_cst (TREE_TYPE (decl), -1);
6577 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6578 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6579 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6580 break;
6582 case MODIFY_EXPR:
6583 gcc_assert (TREE_OPERAND (t, 0) == decl);
6584 TREE_OPERAND (t, 0) = var;
6586 t = TREE_OPERAND (t, 1);
6587 switch (TREE_CODE (t))
6589 case PLUS_EXPR:
6590 if (TREE_OPERAND (t, 1) == decl)
6592 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6593 TREE_OPERAND (t, 0) = var;
6594 break;
6597 /* Fallthru. */
6598 case MINUS_EXPR:
6599 case POINTER_PLUS_EXPR:
6600 gcc_assert (TREE_OPERAND (t, 0) == decl);
6601 TREE_OPERAND (t, 0) = var;
6602 break;
6603 default:
6604 gcc_unreachable ();
6607 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6608 is_gimple_val, fb_rvalue);
6609 ret = MIN (ret, tret);
6610 break;
6612 default:
6613 gcc_unreachable ();
6616 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6618 tree c;
6619 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6620 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6621 && OMP_CLAUSE_DECL (c) == decl
6622 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6624 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6625 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6626 gcc_assert (TREE_OPERAND (t, 0) == var);
6627 t = TREE_OPERAND (t, 1);
6628 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6629 || TREE_CODE (t) == MINUS_EXPR
6630 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6631 gcc_assert (TREE_OPERAND (t, 0) == var);
6632 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6633 TREE_OPERAND (t, 1));
6634 gimplify_assign (decl, t,
6635 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6640 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6642 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6644 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6645 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6646 for_pre_body);
6648 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6650 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6651 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6652 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6653 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6654 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6655 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6656 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6657 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6660 gimplify_seq_add_stmt (pre_p, gfor);
6661 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6664 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6665 In particular, OMP_SECTIONS and OMP_SINGLE. */
6667 static void
6668 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6670 tree expr = *expr_p;
6671 gimple stmt;
6672 gimple_seq body = NULL;
6674 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6675 gimplify_and_add (OMP_BODY (expr), &body);
6676 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6678 if (TREE_CODE (expr) == OMP_SECTIONS)
6679 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6680 else if (TREE_CODE (expr) == OMP_SINGLE)
6681 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6682 else
6683 gcc_unreachable ();
6685 gimplify_seq_add_stmt (pre_p, stmt);
6688 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6689 stabilized the lhs of the atomic operation as *ADDR. Return true if
6690 EXPR is this stabilized form. */
6692 static bool
6693 goa_lhs_expr_p (tree expr, tree addr)
6695 /* Also include casts to other type variants. The C front end is fond
6696 of adding these for e.g. volatile variables. This is like
6697 STRIP_TYPE_NOPS but includes the main variant lookup. */
6698 STRIP_USELESS_TYPE_CONVERSION (expr);
6700 if (TREE_CODE (expr) == INDIRECT_REF)
6702 expr = TREE_OPERAND (expr, 0);
6703 while (expr != addr
6704 && (CONVERT_EXPR_P (expr)
6705 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6706 && TREE_CODE (expr) == TREE_CODE (addr)
6707 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6709 expr = TREE_OPERAND (expr, 0);
6710 addr = TREE_OPERAND (addr, 0);
6712 if (expr == addr)
6713 return true;
6714 return (TREE_CODE (addr) == ADDR_EXPR
6715 && TREE_CODE (expr) == ADDR_EXPR
6716 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6718 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6719 return true;
6720 return false;
6723 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6724 expression does not involve the lhs, evaluate it into a temporary.
6725 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6726 or -1 if an error was encountered. */
6728 static int
6729 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6730 tree lhs_var)
6732 tree expr = *expr_p;
6733 int saw_lhs;
6735 if (goa_lhs_expr_p (expr, lhs_addr))
6737 *expr_p = lhs_var;
6738 return 1;
6740 if (is_gimple_val (expr))
6741 return 0;
6743 saw_lhs = 0;
6744 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6746 case tcc_binary:
6747 case tcc_comparison:
6748 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6749 lhs_var);
6750 case tcc_unary:
6751 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6752 lhs_var);
6753 break;
6754 case tcc_expression:
6755 switch (TREE_CODE (expr))
6757 case TRUTH_ANDIF_EXPR:
6758 case TRUTH_ORIF_EXPR:
6759 case TRUTH_AND_EXPR:
6760 case TRUTH_OR_EXPR:
6761 case TRUTH_XOR_EXPR:
6762 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6763 lhs_addr, lhs_var);
6764 case TRUTH_NOT_EXPR:
6765 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6766 lhs_addr, lhs_var);
6767 break;
6768 case COMPOUND_EXPR:
6769 /* Break out any preevaluations from cp_build_modify_expr. */
6770 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6771 expr = TREE_OPERAND (expr, 1))
6772 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6773 *expr_p = expr;
6774 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6775 default:
6776 break;
6778 break;
6779 default:
6780 break;
6783 if (saw_lhs == 0)
6785 enum gimplify_status gs;
6786 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6787 if (gs != GS_ALL_DONE)
6788 saw_lhs = -1;
6791 return saw_lhs;
6794 /* Gimplify an OMP_ATOMIC statement. */
6796 static enum gimplify_status
6797 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6799 tree addr = TREE_OPERAND (*expr_p, 0);
6800 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6801 ? NULL : TREE_OPERAND (*expr_p, 1);
6802 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6803 tree tmp_load;
6804 gimple loadstmt, storestmt;
6806 tmp_load = create_tmp_reg (type, NULL);
6807 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6808 return GS_ERROR;
6810 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6811 != GS_ALL_DONE)
6812 return GS_ERROR;
6814 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6815 gimplify_seq_add_stmt (pre_p, loadstmt);
6816 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6817 != GS_ALL_DONE)
6818 return GS_ERROR;
6820 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6821 rhs = tmp_load;
6822 storestmt = gimple_build_omp_atomic_store (rhs);
6823 gimplify_seq_add_stmt (pre_p, storestmt);
6824 switch (TREE_CODE (*expr_p))
6826 case OMP_ATOMIC_READ:
6827 case OMP_ATOMIC_CAPTURE_OLD:
6828 *expr_p = tmp_load;
6829 gimple_omp_atomic_set_need_value (loadstmt);
6830 break;
6831 case OMP_ATOMIC_CAPTURE_NEW:
6832 *expr_p = rhs;
6833 gimple_omp_atomic_set_need_value (storestmt);
6834 break;
6835 default:
6836 *expr_p = NULL;
6837 break;
6840 return GS_ALL_DONE;
6843 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6844 body, and adding some EH bits. */
6846 static enum gimplify_status
6847 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6849 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6850 gimple g;
6851 gimple_seq body = NULL;
6852 struct gimplify_ctx gctx;
6853 int subcode = 0;
6855 /* Wrap the transaction body in a BIND_EXPR so we have a context
6856 where to put decls for OpenMP. */
6857 if (TREE_CODE (tbody) != BIND_EXPR)
6859 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6860 TREE_SIDE_EFFECTS (bind) = 1;
6861 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6862 TRANSACTION_EXPR_BODY (expr) = bind;
6865 push_gimplify_context (&gctx);
6866 temp = voidify_wrapper_expr (*expr_p, NULL);
6868 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6869 pop_gimplify_context (g);
6871 g = gimple_build_transaction (body, NULL);
6872 if (TRANSACTION_EXPR_OUTER (expr))
6873 subcode = GTMA_IS_OUTER;
6874 else if (TRANSACTION_EXPR_RELAXED (expr))
6875 subcode = GTMA_IS_RELAXED;
6876 gimple_transaction_set_subcode (g, subcode);
6878 gimplify_seq_add_stmt (pre_p, g);
6880 if (temp)
6882 *expr_p = temp;
6883 return GS_OK;
6886 *expr_p = NULL_TREE;
6887 return GS_ALL_DONE;
6890 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6891 expression produces a value to be used as an operand inside a GIMPLE
6892 statement, the value will be stored back in *EXPR_P. This value will
6893 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6894 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6895 emitted in PRE_P and POST_P.
6897 Additionally, this process may overwrite parts of the input
6898 expression during gimplification. Ideally, it should be
6899 possible to do non-destructive gimplification.
6901 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6902 the expression needs to evaluate to a value to be used as
6903 an operand in a GIMPLE statement, this value will be stored in
6904 *EXPR_P on exit. This happens when the caller specifies one
6905 of fb_lvalue or fb_rvalue fallback flags.
6907 PRE_P will contain the sequence of GIMPLE statements corresponding
6908 to the evaluation of EXPR and all the side-effects that must
6909 be executed before the main expression. On exit, the last
6910 statement of PRE_P is the core statement being gimplified. For
6911 instance, when gimplifying 'if (++a)' the last statement in
6912 PRE_P will be 'if (t.1)' where t.1 is the result of
6913 pre-incrementing 'a'.
6915 POST_P will contain the sequence of GIMPLE statements corresponding
6916 to the evaluation of all the side-effects that must be executed
6917 after the main expression. If this is NULL, the post
6918 side-effects are stored at the end of PRE_P.
6920 The reason why the output is split in two is to handle post
6921 side-effects explicitly. In some cases, an expression may have
6922 inner and outer post side-effects which need to be emitted in
6923 an order different from the one given by the recursive
6924 traversal. For instance, for the expression (*p--)++ the post
6925 side-effects of '--' must actually occur *after* the post
6926 side-effects of '++'. However, gimplification will first visit
6927 the inner expression, so if a separate POST sequence was not
6928 used, the resulting sequence would be:
6930 1 t.1 = *p
6931 2 p = p - 1
6932 3 t.2 = t.1 + 1
6933 4 *p = t.2
6935 However, the post-decrement operation in line #2 must not be
6936 evaluated until after the store to *p at line #4, so the
6937 correct sequence should be:
6939 1 t.1 = *p
6940 2 t.2 = t.1 + 1
6941 3 *p = t.2
6942 4 p = p - 1
6944 So, by specifying a separate post queue, it is possible
6945 to emit the post side-effects in the correct order.
6946 If POST_P is NULL, an internal queue will be used. Before
6947 returning to the caller, the sequence POST_P is appended to
6948 the main output sequence PRE_P.
6950 GIMPLE_TEST_F points to a function that takes a tree T and
6951 returns nonzero if T is in the GIMPLE form requested by the
6952 caller. The GIMPLE predicates are in gimple.c.
6954 FALLBACK tells the function what sort of a temporary we want if
6955 gimplification cannot produce an expression that complies with
6956 GIMPLE_TEST_F.
6958 fb_none means that no temporary should be generated
6959 fb_rvalue means that an rvalue is OK to generate
6960 fb_lvalue means that an lvalue is OK to generate
6961 fb_either means that either is OK, but an lvalue is preferable.
6962 fb_mayfail means that gimplification may fail (in which case
6963 GS_ERROR will be returned)
6965 The return value is either GS_ERROR or GS_ALL_DONE, since this
6966 function iterates until EXPR is completely gimplified or an error
6967 occurs. */
6969 enum gimplify_status
6970 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6971 bool (*gimple_test_f) (tree), fallback_t fallback)
6973 tree tmp;
6974 gimple_seq internal_pre = NULL;
6975 gimple_seq internal_post = NULL;
6976 tree save_expr;
6977 bool is_statement;
6978 location_t saved_location;
6979 enum gimplify_status ret;
6980 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6982 save_expr = *expr_p;
6983 if (save_expr == NULL_TREE)
6984 return GS_ALL_DONE;
6986 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6987 is_statement = gimple_test_f == is_gimple_stmt;
6988 if (is_statement)
6989 gcc_assert (pre_p);
6991 /* Consistency checks. */
6992 if (gimple_test_f == is_gimple_reg)
6993 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
6994 else if (gimple_test_f == is_gimple_val
6995 || gimple_test_f == is_gimple_call_addr
6996 || gimple_test_f == is_gimple_condexpr
6997 || gimple_test_f == is_gimple_mem_rhs
6998 || gimple_test_f == is_gimple_mem_rhs_or_call
6999 || gimple_test_f == is_gimple_reg_rhs
7000 || gimple_test_f == is_gimple_reg_rhs_or_call
7001 || gimple_test_f == is_gimple_asm_val
7002 || gimple_test_f == is_gimple_mem_ref_addr)
7003 gcc_assert (fallback & fb_rvalue);
7004 else if (gimple_test_f == is_gimple_min_lval
7005 || gimple_test_f == is_gimple_lvalue)
7006 gcc_assert (fallback & fb_lvalue);
7007 else if (gimple_test_f == is_gimple_addressable)
7008 gcc_assert (fallback & fb_either);
7009 else if (gimple_test_f == is_gimple_stmt)
7010 gcc_assert (fallback == fb_none);
7011 else
7013 /* We should have recognized the GIMPLE_TEST_F predicate to
7014 know what kind of fallback to use in case a temporary is
7015 needed to hold the value or address of *EXPR_P. */
7016 gcc_unreachable ();
7019 /* We used to check the predicate here and return immediately if it
7020 succeeds. This is wrong; the design is for gimplification to be
7021 idempotent, and for the predicates to only test for valid forms, not
7022 whether they are fully simplified. */
7023 if (pre_p == NULL)
7024 pre_p = &internal_pre;
7026 if (post_p == NULL)
7027 post_p = &internal_post;
7029 /* Remember the last statements added to PRE_P and POST_P. Every
7030 new statement added by the gimplification helpers needs to be
7031 annotated with location information. To centralize the
7032 responsibility, we remember the last statement that had been
7033 added to both queues before gimplifying *EXPR_P. If
7034 gimplification produces new statements in PRE_P and POST_P, those
7035 statements will be annotated with the same location information
7036 as *EXPR_P. */
7037 pre_last_gsi = gsi_last (*pre_p);
7038 post_last_gsi = gsi_last (*post_p);
7040 saved_location = input_location;
7041 if (save_expr != error_mark_node
7042 && EXPR_HAS_LOCATION (*expr_p))
7043 input_location = EXPR_LOCATION (*expr_p);
7045 /* Loop over the specific gimplifiers until the toplevel node
7046 remains the same. */
7049 /* Strip away as many useless type conversions as possible
7050 at the toplevel. */
7051 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7053 /* Remember the expr. */
7054 save_expr = *expr_p;
7056 /* Die, die, die, my darling. */
7057 if (save_expr == error_mark_node
7058 || (TREE_TYPE (save_expr)
7059 && TREE_TYPE (save_expr) == error_mark_node))
7061 ret = GS_ERROR;
7062 break;
7065 /* Do any language-specific gimplification. */
7066 ret = ((enum gimplify_status)
7067 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7068 if (ret == GS_OK)
7070 if (*expr_p == NULL_TREE)
7071 break;
7072 if (*expr_p != save_expr)
7073 continue;
7075 else if (ret != GS_UNHANDLED)
7076 break;
7078 /* Make sure that all the cases set 'ret' appropriately. */
7079 ret = GS_UNHANDLED;
7080 switch (TREE_CODE (*expr_p))
7082 /* First deal with the special cases. */
7084 case POSTINCREMENT_EXPR:
7085 case POSTDECREMENT_EXPR:
7086 case PREINCREMENT_EXPR:
7087 case PREDECREMENT_EXPR:
7088 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7089 fallback != fb_none);
7090 break;
7092 case ARRAY_REF:
7093 case ARRAY_RANGE_REF:
7094 case REALPART_EXPR:
7095 case IMAGPART_EXPR:
7096 case COMPONENT_REF:
7097 case VIEW_CONVERT_EXPR:
7098 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7099 fallback ? fallback : fb_rvalue);
7100 break;
7102 case COND_EXPR:
7103 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7105 /* C99 code may assign to an array in a structure value of a
7106 conditional expression, and this has undefined behavior
7107 only on execution, so create a temporary if an lvalue is
7108 required. */
7109 if (fallback == fb_lvalue)
7111 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7112 mark_addressable (*expr_p);
7113 ret = GS_OK;
7115 break;
7117 case CALL_EXPR:
7118 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7120 /* C99 code may assign to an array in a structure returned
7121 from a function, and this has undefined behavior only on
7122 execution, so create a temporary if an lvalue is
7123 required. */
7124 if (fallback == fb_lvalue)
7126 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7127 mark_addressable (*expr_p);
7128 ret = GS_OK;
7130 break;
7132 case TREE_LIST:
7133 gcc_unreachable ();
7135 case COMPOUND_EXPR:
7136 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7137 break;
7139 case COMPOUND_LITERAL_EXPR:
7140 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7141 gimple_test_f, fallback);
7142 break;
7144 case MODIFY_EXPR:
7145 case INIT_EXPR:
7146 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7147 fallback != fb_none);
7148 break;
7150 case TRUTH_ANDIF_EXPR:
7151 case TRUTH_ORIF_EXPR:
7153 /* Preserve the original type of the expression and the
7154 source location of the outer expression. */
7155 tree org_type = TREE_TYPE (*expr_p);
7156 *expr_p = gimple_boolify (*expr_p);
7157 *expr_p = build3_loc (input_location, COND_EXPR,
7158 org_type, *expr_p,
7159 fold_convert_loc
7160 (input_location,
7161 org_type, boolean_true_node),
7162 fold_convert_loc
7163 (input_location,
7164 org_type, boolean_false_node));
7165 ret = GS_OK;
7166 break;
7169 case TRUTH_NOT_EXPR:
7171 tree type = TREE_TYPE (*expr_p);
7172 /* The parsers are careful to generate TRUTH_NOT_EXPR
7173 only with operands that are always zero or one.
7174 We do not fold here but handle the only interesting case
7175 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7176 *expr_p = gimple_boolify (*expr_p);
7177 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7178 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7179 TREE_TYPE (*expr_p),
7180 TREE_OPERAND (*expr_p, 0));
7181 else
7182 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7183 TREE_TYPE (*expr_p),
7184 TREE_OPERAND (*expr_p, 0),
7185 build_int_cst (TREE_TYPE (*expr_p), 1));
7186 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7187 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7188 ret = GS_OK;
7189 break;
7192 case ADDR_EXPR:
7193 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7194 break;
7196 case VA_ARG_EXPR:
7197 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7198 break;
7200 CASE_CONVERT:
7201 if (IS_EMPTY_STMT (*expr_p))
7203 ret = GS_ALL_DONE;
7204 break;
7207 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7208 || fallback == fb_none)
7210 /* Just strip a conversion to void (or in void context) and
7211 try again. */
7212 *expr_p = TREE_OPERAND (*expr_p, 0);
7213 ret = GS_OK;
7214 break;
7217 ret = gimplify_conversion (expr_p);
7218 if (ret == GS_ERROR)
7219 break;
7220 if (*expr_p != save_expr)
7221 break;
7222 /* FALLTHRU */
7224 case FIX_TRUNC_EXPR:
7225 /* unary_expr: ... | '(' cast ')' val | ... */
7226 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7227 is_gimple_val, fb_rvalue);
7228 recalculate_side_effects (*expr_p);
7229 break;
7231 case INDIRECT_REF:
7233 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7234 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7235 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7237 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7238 if (*expr_p != save_expr)
7240 ret = GS_OK;
7241 break;
7244 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7245 is_gimple_reg, fb_rvalue);
7246 if (ret == GS_ERROR)
7247 break;
7249 recalculate_side_effects (*expr_p);
7250 *expr_p = fold_build2_loc (input_location, MEM_REF,
7251 TREE_TYPE (*expr_p),
7252 TREE_OPERAND (*expr_p, 0),
7253 build_int_cst (saved_ptr_type, 0));
7254 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7255 TREE_THIS_NOTRAP (*expr_p) = notrap;
7256 ret = GS_OK;
7257 break;
7260 /* We arrive here through the various re-gimplifcation paths. */
7261 case MEM_REF:
7262 /* First try re-folding the whole thing. */
7263 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7264 TREE_OPERAND (*expr_p, 0),
7265 TREE_OPERAND (*expr_p, 1));
7266 if (tmp)
7268 *expr_p = tmp;
7269 recalculate_side_effects (*expr_p);
7270 ret = GS_OK;
7271 break;
7273 /* Avoid re-gimplifying the address operand if it is already
7274 in suitable form. Re-gimplifying would mark the address
7275 operand addressable. Always gimplify when not in SSA form
7276 as we still may have to gimplify decls with value-exprs. */
7277 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7278 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7280 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7281 is_gimple_mem_ref_addr, fb_rvalue);
7282 if (ret == GS_ERROR)
7283 break;
7285 recalculate_side_effects (*expr_p);
7286 ret = GS_ALL_DONE;
7287 break;
7289 /* Constants need not be gimplified. */
7290 case INTEGER_CST:
7291 case REAL_CST:
7292 case FIXED_CST:
7293 case STRING_CST:
7294 case COMPLEX_CST:
7295 case VECTOR_CST:
7296 ret = GS_ALL_DONE;
7297 break;
7299 case CONST_DECL:
7300 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7301 CONST_DECL node. Otherwise the decl is replaceable by its
7302 value. */
7303 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7304 if (fallback & fb_lvalue)
7305 ret = GS_ALL_DONE;
7306 else
7308 *expr_p = DECL_INITIAL (*expr_p);
7309 ret = GS_OK;
7311 break;
7313 case DECL_EXPR:
7314 ret = gimplify_decl_expr (expr_p, pre_p);
7315 break;
7317 case BIND_EXPR:
7318 ret = gimplify_bind_expr (expr_p, pre_p);
7319 break;
7321 case LOOP_EXPR:
7322 ret = gimplify_loop_expr (expr_p, pre_p);
7323 break;
7325 case SWITCH_EXPR:
7326 ret = gimplify_switch_expr (expr_p, pre_p);
7327 break;
7329 case EXIT_EXPR:
7330 ret = gimplify_exit_expr (expr_p);
7331 break;
7333 case GOTO_EXPR:
7334 /* If the target is not LABEL, then it is a computed jump
7335 and the target needs to be gimplified. */
7336 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7338 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7339 NULL, is_gimple_val, fb_rvalue);
7340 if (ret == GS_ERROR)
7341 break;
7343 gimplify_seq_add_stmt (pre_p,
7344 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7345 ret = GS_ALL_DONE;
7346 break;
7348 case PREDICT_EXPR:
7349 gimplify_seq_add_stmt (pre_p,
7350 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7351 PREDICT_EXPR_OUTCOME (*expr_p)));
7352 ret = GS_ALL_DONE;
7353 break;
7355 case LABEL_EXPR:
7356 ret = GS_ALL_DONE;
7357 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7358 == current_function_decl);
7359 gimplify_seq_add_stmt (pre_p,
7360 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7361 break;
7363 case CASE_LABEL_EXPR:
7364 ret = gimplify_case_label_expr (expr_p, pre_p);
7365 break;
7367 case RETURN_EXPR:
7368 ret = gimplify_return_expr (*expr_p, pre_p);
7369 break;
7371 case CONSTRUCTOR:
7372 /* Don't reduce this in place; let gimplify_init_constructor work its
7373 magic. Buf if we're just elaborating this for side effects, just
7374 gimplify any element that has side-effects. */
7375 if (fallback == fb_none)
7377 unsigned HOST_WIDE_INT ix;
7378 tree val;
7379 tree temp = NULL_TREE;
7380 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7381 if (TREE_SIDE_EFFECTS (val))
7382 append_to_statement_list (val, &temp);
7384 *expr_p = temp;
7385 ret = temp ? GS_OK : GS_ALL_DONE;
7387 /* C99 code may assign to an array in a constructed
7388 structure or union, and this has undefined behavior only
7389 on execution, so create a temporary if an lvalue is
7390 required. */
7391 else if (fallback == fb_lvalue)
7393 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7394 mark_addressable (*expr_p);
7395 ret = GS_OK;
7397 else
7398 ret = GS_ALL_DONE;
7399 break;
7401 /* The following are special cases that are not handled by the
7402 original GIMPLE grammar. */
7404 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7405 eliminated. */
7406 case SAVE_EXPR:
7407 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7408 break;
7410 case BIT_FIELD_REF:
7412 enum gimplify_status r0, r1, r2;
7414 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7415 post_p, is_gimple_lvalue, fb_either);
7416 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7417 post_p, is_gimple_val, fb_rvalue);
7418 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7419 post_p, is_gimple_val, fb_rvalue);
7420 recalculate_side_effects (*expr_p);
7422 ret = MIN (r0, MIN (r1, r2));
7424 break;
7426 case TARGET_MEM_REF:
7428 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7430 if (TMR_BASE (*expr_p))
7431 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7432 post_p, is_gimple_mem_ref_addr, fb_either);
7433 if (TMR_INDEX (*expr_p))
7434 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7435 post_p, is_gimple_val, fb_rvalue);
7436 if (TMR_INDEX2 (*expr_p))
7437 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7438 post_p, is_gimple_val, fb_rvalue);
7439 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7440 ret = MIN (r0, r1);
7442 break;
7444 case NON_LVALUE_EXPR:
7445 /* This should have been stripped above. */
7446 gcc_unreachable ();
7448 case ASM_EXPR:
7449 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7450 break;
7452 case TRY_FINALLY_EXPR:
7453 case TRY_CATCH_EXPR:
7455 gimple_seq eval, cleanup;
7456 gimple try_;
7458 eval = cleanup = NULL;
7459 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7460 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7461 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7462 if (gimple_seq_empty_p (cleanup))
7464 gimple_seq_add_seq (pre_p, eval);
7465 ret = GS_ALL_DONE;
7466 break;
7468 try_ = gimple_build_try (eval, cleanup,
7469 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7470 ? GIMPLE_TRY_FINALLY
7471 : GIMPLE_TRY_CATCH);
7472 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7473 gimple_try_set_catch_is_cleanup (try_,
7474 TRY_CATCH_IS_CLEANUP (*expr_p));
7475 gimplify_seq_add_stmt (pre_p, try_);
7476 ret = GS_ALL_DONE;
7477 break;
7480 case CLEANUP_POINT_EXPR:
7481 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7482 break;
7484 case TARGET_EXPR:
7485 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7486 break;
7488 case CATCH_EXPR:
7490 gimple c;
7491 gimple_seq handler = NULL;
7492 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7493 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7494 gimplify_seq_add_stmt (pre_p, c);
7495 ret = GS_ALL_DONE;
7496 break;
7499 case EH_FILTER_EXPR:
7501 gimple ehf;
7502 gimple_seq failure = NULL;
7504 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7505 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7506 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7507 gimplify_seq_add_stmt (pre_p, ehf);
7508 ret = GS_ALL_DONE;
7509 break;
7512 case OBJ_TYPE_REF:
7514 enum gimplify_status r0, r1;
7515 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7516 post_p, is_gimple_val, fb_rvalue);
7517 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7518 post_p, is_gimple_val, fb_rvalue);
7519 TREE_SIDE_EFFECTS (*expr_p) = 0;
7520 ret = MIN (r0, r1);
7522 break;
7524 case LABEL_DECL:
7525 /* We get here when taking the address of a label. We mark
7526 the label as "forced"; meaning it can never be removed and
7527 it is a potential target for any computed goto. */
7528 FORCED_LABEL (*expr_p) = 1;
7529 ret = GS_ALL_DONE;
7530 break;
7532 case STATEMENT_LIST:
7533 ret = gimplify_statement_list (expr_p, pre_p);
7534 break;
7536 case WITH_SIZE_EXPR:
7538 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7539 post_p == &internal_post ? NULL : post_p,
7540 gimple_test_f, fallback);
7541 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7542 is_gimple_val, fb_rvalue);
7543 ret = GS_ALL_DONE;
7545 break;
7547 case VAR_DECL:
7548 case PARM_DECL:
7549 ret = gimplify_var_or_parm_decl (expr_p);
7550 break;
7552 case RESULT_DECL:
7553 /* When within an OpenMP context, notice uses of variables. */
7554 if (gimplify_omp_ctxp)
7555 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7556 ret = GS_ALL_DONE;
7557 break;
7559 case SSA_NAME:
7560 /* Allow callbacks into the gimplifier during optimization. */
7561 ret = GS_ALL_DONE;
7562 break;
7564 case OMP_PARALLEL:
7565 gimplify_omp_parallel (expr_p, pre_p);
7566 ret = GS_ALL_DONE;
7567 break;
7569 case OMP_TASK:
7570 gimplify_omp_task (expr_p, pre_p);
7571 ret = GS_ALL_DONE;
7572 break;
7574 case OMP_FOR:
7575 ret = gimplify_omp_for (expr_p, pre_p);
7576 break;
7578 case OMP_SECTIONS:
7579 case OMP_SINGLE:
7580 gimplify_omp_workshare (expr_p, pre_p);
7581 ret = GS_ALL_DONE;
7582 break;
7584 case OMP_SECTION:
7585 case OMP_MASTER:
7586 case OMP_ORDERED:
7587 case OMP_CRITICAL:
7589 gimple_seq body = NULL;
7590 gimple g;
7592 gimplify_and_add (OMP_BODY (*expr_p), &body);
7593 switch (TREE_CODE (*expr_p))
7595 case OMP_SECTION:
7596 g = gimple_build_omp_section (body);
7597 break;
7598 case OMP_MASTER:
7599 g = gimple_build_omp_master (body);
7600 break;
7601 case OMP_ORDERED:
7602 g = gimple_build_omp_ordered (body);
7603 break;
7604 case OMP_CRITICAL:
7605 g = gimple_build_omp_critical (body,
7606 OMP_CRITICAL_NAME (*expr_p));
7607 break;
7608 default:
7609 gcc_unreachable ();
7611 gimplify_seq_add_stmt (pre_p, g);
7612 ret = GS_ALL_DONE;
7613 break;
7616 case OMP_ATOMIC:
7617 case OMP_ATOMIC_READ:
7618 case OMP_ATOMIC_CAPTURE_OLD:
7619 case OMP_ATOMIC_CAPTURE_NEW:
7620 ret = gimplify_omp_atomic (expr_p, pre_p);
7621 break;
7623 case TRANSACTION_EXPR:
7624 ret = gimplify_transaction (expr_p, pre_p);
7625 break;
7627 case TRUTH_AND_EXPR:
7628 case TRUTH_OR_EXPR:
7629 case TRUTH_XOR_EXPR:
7631 tree orig_type = TREE_TYPE (*expr_p);
7632 tree new_type, xop0, xop1;
7633 *expr_p = gimple_boolify (*expr_p);
7634 new_type = TREE_TYPE (*expr_p);
7635 if (!useless_type_conversion_p (orig_type, new_type))
7637 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7638 ret = GS_OK;
7639 break;
7642 /* Boolified binary truth expressions are semantically equivalent
7643 to bitwise binary expressions. Canonicalize them to the
7644 bitwise variant. */
7645 switch (TREE_CODE (*expr_p))
7647 case TRUTH_AND_EXPR:
7648 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7649 break;
7650 case TRUTH_OR_EXPR:
7651 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7652 break;
7653 case TRUTH_XOR_EXPR:
7654 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7655 break;
7656 default:
7657 break;
7659 /* Now make sure that operands have compatible type to
7660 expression's new_type. */
7661 xop0 = TREE_OPERAND (*expr_p, 0);
7662 xop1 = TREE_OPERAND (*expr_p, 1);
7663 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7664 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7665 new_type,
7666 xop0);
7667 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7668 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7669 new_type,
7670 xop1);
7671 /* Continue classified as tcc_binary. */
7672 goto expr_2;
7675 case FMA_EXPR:
7676 case VEC_PERM_EXPR:
7677 /* Classified as tcc_expression. */
7678 goto expr_3;
7680 case POINTER_PLUS_EXPR:
7682 enum gimplify_status r0, r1;
7683 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7684 post_p, is_gimple_val, fb_rvalue);
7685 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7686 post_p, is_gimple_val, fb_rvalue);
7687 recalculate_side_effects (*expr_p);
7688 ret = MIN (r0, r1);
7689 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7690 after gimplifying operands - this is similar to how
7691 it would be folding all gimplified stmts on creation
7692 to have them canonicalized, which is what we eventually
7693 should do anyway. */
7694 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7695 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7697 *expr_p = build_fold_addr_expr_with_type_loc
7698 (input_location,
7699 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7700 TREE_OPERAND (*expr_p, 0),
7701 fold_convert (ptr_type_node,
7702 TREE_OPERAND (*expr_p, 1))),
7703 TREE_TYPE (*expr_p));
7704 ret = MIN (ret, GS_OK);
7706 break;
7709 default:
7710 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7712 case tcc_comparison:
7713 /* Handle comparison of objects of non scalar mode aggregates
7714 with a call to memcmp. It would be nice to only have to do
7715 this for variable-sized objects, but then we'd have to allow
7716 the same nest of reference nodes we allow for MODIFY_EXPR and
7717 that's too complex.
7719 Compare scalar mode aggregates as scalar mode values. Using
7720 memcmp for them would be very inefficient at best, and is
7721 plain wrong if bitfields are involved. */
7723 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7725 /* Vector comparisons need no boolification. */
7726 if (TREE_CODE (type) == VECTOR_TYPE)
7727 goto expr_2;
7728 else if (!AGGREGATE_TYPE_P (type))
7730 tree org_type = TREE_TYPE (*expr_p);
7731 *expr_p = gimple_boolify (*expr_p);
7732 if (!useless_type_conversion_p (org_type,
7733 TREE_TYPE (*expr_p)))
7735 *expr_p = fold_convert_loc (input_location,
7736 org_type, *expr_p);
7737 ret = GS_OK;
7739 else
7740 goto expr_2;
7742 else if (TYPE_MODE (type) != BLKmode)
7743 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7744 else
7745 ret = gimplify_variable_sized_compare (expr_p);
7747 break;
7750 /* If *EXPR_P does not need to be special-cased, handle it
7751 according to its class. */
7752 case tcc_unary:
7753 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7754 post_p, is_gimple_val, fb_rvalue);
7755 break;
7757 case tcc_binary:
7758 expr_2:
7760 enum gimplify_status r0, r1;
7762 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7763 post_p, is_gimple_val, fb_rvalue);
7764 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7765 post_p, is_gimple_val, fb_rvalue);
7767 ret = MIN (r0, r1);
7768 break;
7771 expr_3:
7773 enum gimplify_status r0, r1, r2;
7775 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7776 post_p, is_gimple_val, fb_rvalue);
7777 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7778 post_p, is_gimple_val, fb_rvalue);
7779 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7780 post_p, is_gimple_val, fb_rvalue);
7782 ret = MIN (MIN (r0, r1), r2);
7783 break;
7786 case tcc_declaration:
7787 case tcc_constant:
7788 ret = GS_ALL_DONE;
7789 goto dont_recalculate;
7791 default:
7792 gcc_unreachable ();
7795 recalculate_side_effects (*expr_p);
7797 dont_recalculate:
7798 break;
7801 gcc_assert (*expr_p || ret != GS_OK);
7803 while (ret == GS_OK);
7805 /* If we encountered an error_mark somewhere nested inside, either
7806 stub out the statement or propagate the error back out. */
7807 if (ret == GS_ERROR)
7809 if (is_statement)
7810 *expr_p = NULL;
7811 goto out;
7814 /* This was only valid as a return value from the langhook, which
7815 we handled. Make sure it doesn't escape from any other context. */
7816 gcc_assert (ret != GS_UNHANDLED);
7818 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7820 /* We aren't looking for a value, and we don't have a valid
7821 statement. If it doesn't have side-effects, throw it away. */
7822 if (!TREE_SIDE_EFFECTS (*expr_p))
7823 *expr_p = NULL;
7824 else if (!TREE_THIS_VOLATILE (*expr_p))
7826 /* This is probably a _REF that contains something nested that
7827 has side effects. Recurse through the operands to find it. */
7828 enum tree_code code = TREE_CODE (*expr_p);
7830 switch (code)
7832 case COMPONENT_REF:
7833 case REALPART_EXPR:
7834 case IMAGPART_EXPR:
7835 case VIEW_CONVERT_EXPR:
7836 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7837 gimple_test_f, fallback);
7838 break;
7840 case ARRAY_REF:
7841 case ARRAY_RANGE_REF:
7842 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7843 gimple_test_f, fallback);
7844 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7845 gimple_test_f, fallback);
7846 break;
7848 default:
7849 /* Anything else with side-effects must be converted to
7850 a valid statement before we get here. */
7851 gcc_unreachable ();
7854 *expr_p = NULL;
7856 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7857 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7859 /* Historically, the compiler has treated a bare reference
7860 to a non-BLKmode volatile lvalue as forcing a load. */
7861 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7863 /* Normally, we do not want to create a temporary for a
7864 TREE_ADDRESSABLE type because such a type should not be
7865 copied by bitwise-assignment. However, we make an
7866 exception here, as all we are doing here is ensuring that
7867 we read the bytes that make up the type. We use
7868 create_tmp_var_raw because create_tmp_var will abort when
7869 given a TREE_ADDRESSABLE type. */
7870 tree tmp = create_tmp_var_raw (type, "vol");
7871 gimple_add_tmp_var (tmp);
7872 gimplify_assign (tmp, *expr_p, pre_p);
7873 *expr_p = NULL;
7875 else
7876 /* We can't do anything useful with a volatile reference to
7877 an incomplete type, so just throw it away. Likewise for
7878 a BLKmode type, since any implicit inner load should
7879 already have been turned into an explicit one by the
7880 gimplification process. */
7881 *expr_p = NULL;
7884 /* If we are gimplifying at the statement level, we're done. Tack
7885 everything together and return. */
7886 if (fallback == fb_none || is_statement)
7888 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7889 it out for GC to reclaim it. */
7890 *expr_p = NULL_TREE;
7892 if (!gimple_seq_empty_p (internal_pre)
7893 || !gimple_seq_empty_p (internal_post))
7895 gimplify_seq_add_seq (&internal_pre, internal_post);
7896 gimplify_seq_add_seq (pre_p, internal_pre);
7899 /* The result of gimplifying *EXPR_P is going to be the last few
7900 statements in *PRE_P and *POST_P. Add location information
7901 to all the statements that were added by the gimplification
7902 helpers. */
7903 if (!gimple_seq_empty_p (*pre_p))
7904 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7906 if (!gimple_seq_empty_p (*post_p))
7907 annotate_all_with_location_after (*post_p, post_last_gsi,
7908 input_location);
7910 goto out;
7913 #ifdef ENABLE_GIMPLE_CHECKING
7914 if (*expr_p)
7916 enum tree_code code = TREE_CODE (*expr_p);
7917 /* These expressions should already be in gimple IR form. */
7918 gcc_assert (code != MODIFY_EXPR
7919 && code != ASM_EXPR
7920 && code != BIND_EXPR
7921 && code != CATCH_EXPR
7922 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7923 && code != EH_FILTER_EXPR
7924 && code != GOTO_EXPR
7925 && code != LABEL_EXPR
7926 && code != LOOP_EXPR
7927 && code != SWITCH_EXPR
7928 && code != TRY_FINALLY_EXPR
7929 && code != OMP_CRITICAL
7930 && code != OMP_FOR
7931 && code != OMP_MASTER
7932 && code != OMP_ORDERED
7933 && code != OMP_PARALLEL
7934 && code != OMP_SECTIONS
7935 && code != OMP_SECTION
7936 && code != OMP_SINGLE);
7938 #endif
7940 /* Otherwise we're gimplifying a subexpression, so the resulting
7941 value is interesting. If it's a valid operand that matches
7942 GIMPLE_TEST_F, we're done. Unless we are handling some
7943 post-effects internally; if that's the case, we need to copy into
7944 a temporary before adding the post-effects to POST_P. */
7945 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7946 goto out;
7948 /* Otherwise, we need to create a new temporary for the gimplified
7949 expression. */
7951 /* We can't return an lvalue if we have an internal postqueue. The
7952 object the lvalue refers to would (probably) be modified by the
7953 postqueue; we need to copy the value out first, which means an
7954 rvalue. */
7955 if ((fallback & fb_lvalue)
7956 && gimple_seq_empty_p (internal_post)
7957 && is_gimple_addressable (*expr_p))
7959 /* An lvalue will do. Take the address of the expression, store it
7960 in a temporary, and replace the expression with an INDIRECT_REF of
7961 that temporary. */
7962 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
7963 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7964 *expr_p = build_simple_mem_ref (tmp);
7966 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7968 /* An rvalue will do. Assign the gimplified expression into a
7969 new temporary TMP and replace the original expression with
7970 TMP. First, make sure that the expression has a type so that
7971 it can be assigned into a temporary. */
7972 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7973 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7975 else
7977 #ifdef ENABLE_GIMPLE_CHECKING
7978 if (!(fallback & fb_mayfail))
7980 fprintf (stderr, "gimplification failed:\n");
7981 print_generic_expr (stderr, *expr_p, 0);
7982 debug_tree (*expr_p);
7983 internal_error ("gimplification failed");
7985 #endif
7986 gcc_assert (fallback & fb_mayfail);
7988 /* If this is an asm statement, and the user asked for the
7989 impossible, don't die. Fail and let gimplify_asm_expr
7990 issue an error. */
7991 ret = GS_ERROR;
7992 goto out;
7995 /* Make sure the temporary matches our predicate. */
7996 gcc_assert ((*gimple_test_f) (*expr_p));
7998 if (!gimple_seq_empty_p (internal_post))
8000 annotate_all_with_location (internal_post, input_location);
8001 gimplify_seq_add_seq (pre_p, internal_post);
8004 out:
8005 input_location = saved_location;
8006 return ret;
8009 /* Look through TYPE for variable-sized objects and gimplify each such
8010 size that we find. Add to LIST_P any statements generated. */
8012 void
8013 gimplify_type_sizes (tree type, gimple_seq *list_p)
8015 tree field, t;
8017 if (type == NULL || type == error_mark_node)
8018 return;
8020 /* We first do the main variant, then copy into any other variants. */
8021 type = TYPE_MAIN_VARIANT (type);
8023 /* Avoid infinite recursion. */
8024 if (TYPE_SIZES_GIMPLIFIED (type))
8025 return;
8027 TYPE_SIZES_GIMPLIFIED (type) = 1;
8029 switch (TREE_CODE (type))
8031 case INTEGER_TYPE:
8032 case ENUMERAL_TYPE:
8033 case BOOLEAN_TYPE:
8034 case REAL_TYPE:
8035 case FIXED_POINT_TYPE:
8036 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8037 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8039 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8041 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8042 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8044 break;
8046 case ARRAY_TYPE:
8047 /* These types may not have declarations, so handle them here. */
8048 gimplify_type_sizes (TREE_TYPE (type), list_p);
8049 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8050 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8051 with assigned stack slots, for -O1+ -g they should be tracked
8052 by VTA. */
8053 if (!(TYPE_NAME (type)
8054 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8055 && DECL_IGNORED_P (TYPE_NAME (type)))
8056 && TYPE_DOMAIN (type)
8057 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8059 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8060 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8061 DECL_IGNORED_P (t) = 0;
8062 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8063 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8064 DECL_IGNORED_P (t) = 0;
8066 break;
8068 case RECORD_TYPE:
8069 case UNION_TYPE:
8070 case QUAL_UNION_TYPE:
8071 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8072 if (TREE_CODE (field) == FIELD_DECL)
8074 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8075 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8076 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8077 gimplify_type_sizes (TREE_TYPE (field), list_p);
8079 break;
8081 case POINTER_TYPE:
8082 case REFERENCE_TYPE:
8083 /* We used to recurse on the pointed-to type here, which turned out to
8084 be incorrect because its definition might refer to variables not
8085 yet initialized at this point if a forward declaration is involved.
8087 It was actually useful for anonymous pointed-to types to ensure
8088 that the sizes evaluation dominates every possible later use of the
8089 values. Restricting to such types here would be safe since there
8090 is no possible forward declaration around, but would introduce an
8091 undesirable middle-end semantic to anonymity. We then defer to
8092 front-ends the responsibility of ensuring that the sizes are
8093 evaluated both early and late enough, e.g. by attaching artificial
8094 type declarations to the tree. */
8095 break;
8097 default:
8098 break;
8101 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8102 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8104 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8106 TYPE_SIZE (t) = TYPE_SIZE (type);
8107 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8108 TYPE_SIZES_GIMPLIFIED (t) = 1;
8112 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8113 a size or position, has had all of its SAVE_EXPRs evaluated.
8114 We add any required statements to *STMT_P. */
8116 void
8117 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8119 tree expr = *expr_p;
8121 /* We don't do anything if the value isn't there, is constant, or contains
8122 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8123 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8124 will want to replace it with a new variable, but that will cause problems
8125 if this type is from outside the function. It's OK to have that here. */
8126 if (expr == NULL_TREE || TREE_CONSTANT (expr)
8127 || TREE_CODE (expr) == VAR_DECL
8128 || CONTAINS_PLACEHOLDER_P (expr))
8129 return;
8131 *expr_p = unshare_expr (expr);
8133 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8134 expr = *expr_p;
8137 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8138 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8139 is true, also gimplify the parameters. */
8141 gimple
8142 gimplify_body (tree fndecl, bool do_parms)
8144 location_t saved_location = input_location;
8145 gimple_seq parm_stmts, seq;
8146 gimple outer_bind;
8147 struct gimplify_ctx gctx;
8148 struct cgraph_node *cgn;
8150 timevar_push (TV_TREE_GIMPLIFY);
8152 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8153 gimplification. */
8154 default_rtl_profile ();
8156 gcc_assert (gimplify_ctxp == NULL);
8157 push_gimplify_context (&gctx);
8159 /* Unshare most shared trees in the body and in that of any nested functions.
8160 It would seem we don't have to do this for nested functions because
8161 they are supposed to be output and then the outer function gimplified
8162 first, but the g++ front end doesn't always do it that way. */
8163 unshare_body (fndecl);
8164 unvisit_body (fndecl);
8166 cgn = cgraph_get_node (fndecl);
8167 if (cgn && cgn->origin)
8168 nonlocal_vlas = pointer_set_create ();
8170 /* Make sure input_location isn't set to something weird. */
8171 input_location = DECL_SOURCE_LOCATION (fndecl);
8173 /* Resolve callee-copies. This has to be done before processing
8174 the body so that DECL_VALUE_EXPR gets processed correctly. */
8175 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8177 /* Gimplify the function's body. */
8178 seq = NULL;
8179 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8180 outer_bind = gimple_seq_first_stmt (seq);
8181 if (!outer_bind)
8183 outer_bind = gimple_build_nop ();
8184 gimplify_seq_add_stmt (&seq, outer_bind);
8187 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8188 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8189 if (gimple_code (outer_bind) == GIMPLE_BIND
8190 && gimple_seq_first (seq) == gimple_seq_last (seq))
8192 else
8193 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8195 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8197 /* If we had callee-copies statements, insert them at the beginning
8198 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8199 if (!gimple_seq_empty_p (parm_stmts))
8201 tree parm;
8203 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8204 gimple_bind_set_body (outer_bind, parm_stmts);
8206 for (parm = DECL_ARGUMENTS (current_function_decl);
8207 parm; parm = DECL_CHAIN (parm))
8208 if (DECL_HAS_VALUE_EXPR_P (parm))
8210 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8211 DECL_IGNORED_P (parm) = 0;
8215 if (nonlocal_vlas)
8217 pointer_set_destroy (nonlocal_vlas);
8218 nonlocal_vlas = NULL;
8221 pop_gimplify_context (outer_bind);
8222 gcc_assert (gimplify_ctxp == NULL);
8224 if (!seen_error ())
8225 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8227 timevar_pop (TV_TREE_GIMPLIFY);
8228 input_location = saved_location;
8230 return outer_bind;
8233 typedef char *char_p; /* For DEF_VEC_P. */
8234 DEF_VEC_P(char_p);
8235 DEF_VEC_ALLOC_P(char_p,heap);
8237 /* Return whether we should exclude FNDECL from instrumentation. */
8239 static bool
8240 flag_instrument_functions_exclude_p (tree fndecl)
8242 VEC(char_p,heap) *vec;
8244 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_functions;
8245 if (VEC_length (char_p, vec) > 0)
8247 const char *name;
8248 int i;
8249 char *s;
8251 name = lang_hooks.decl_printable_name (fndecl, 0);
8252 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8253 if (strstr (name, s) != NULL)
8254 return true;
8257 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_files;
8258 if (VEC_length (char_p, vec) > 0)
8260 const char *name;
8261 int i;
8262 char *s;
8264 name = DECL_SOURCE_FILE (fndecl);
8265 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8266 if (strstr (name, s) != NULL)
8267 return true;
8270 return false;
8273 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8274 node for the function we want to gimplify.
8276 Return the sequence of GIMPLE statements corresponding to the body
8277 of FNDECL. */
8279 void
8280 gimplify_function_tree (tree fndecl)
8282 tree oldfn, parm, ret;
8283 gimple_seq seq;
8284 gimple bind;
8286 gcc_assert (!gimple_body (fndecl));
8288 oldfn = current_function_decl;
8289 current_function_decl = fndecl;
8290 if (DECL_STRUCT_FUNCTION (fndecl))
8291 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8292 else
8293 push_struct_function (fndecl);
8295 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8297 /* Preliminarily mark non-addressed complex variables as eligible
8298 for promotion to gimple registers. We'll transform their uses
8299 as we find them. */
8300 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8301 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8302 && !TREE_THIS_VOLATILE (parm)
8303 && !needs_to_live_in_memory (parm))
8304 DECL_GIMPLE_REG_P (parm) = 1;
8307 ret = DECL_RESULT (fndecl);
8308 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8309 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8310 && !needs_to_live_in_memory (ret))
8311 DECL_GIMPLE_REG_P (ret) = 1;
8313 bind = gimplify_body (fndecl, true);
8315 /* The tree body of the function is no longer needed, replace it
8316 with the new GIMPLE body. */
8317 seq = NULL;
8318 gimple_seq_add_stmt (&seq, bind);
8319 gimple_set_body (fndecl, seq);
8321 /* If we're instrumenting function entry/exit, then prepend the call to
8322 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8323 catch the exit hook. */
8324 /* ??? Add some way to ignore exceptions for this TFE. */
8325 if (flag_instrument_function_entry_exit
8326 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8327 && !flag_instrument_functions_exclude_p (fndecl))
8329 tree x;
8330 gimple new_bind;
8331 gimple tf;
8332 gimple_seq cleanup = NULL, body = NULL;
8333 tree tmp_var;
8334 gimple call;
8336 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8337 call = gimple_build_call (x, 1, integer_zero_node);
8338 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8339 gimple_call_set_lhs (call, tmp_var);
8340 gimplify_seq_add_stmt (&cleanup, call);
8341 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8342 call = gimple_build_call (x, 2,
8343 build_fold_addr_expr (current_function_decl),
8344 tmp_var);
8345 gimplify_seq_add_stmt (&cleanup, call);
8346 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
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 (&body, call);
8353 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8354 call = gimple_build_call (x, 2,
8355 build_fold_addr_expr (current_function_decl),
8356 tmp_var);
8357 gimplify_seq_add_stmt (&body, call);
8358 gimplify_seq_add_stmt (&body, tf);
8359 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8360 /* Clear the block for BIND, since it is no longer directly inside
8361 the function, but within a try block. */
8362 gimple_bind_set_block (bind, NULL);
8364 /* Replace the current function body with the body
8365 wrapped in the try/finally TF. */
8366 seq = NULL;
8367 gimple_seq_add_stmt (&seq, new_bind);
8368 gimple_set_body (fndecl, seq);
8371 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8372 cfun->curr_properties = PROP_gimple_any;
8374 current_function_decl = oldfn;
8375 pop_cfun ();
8378 /* Some transformations like inlining may invalidate the GIMPLE form
8379 for operands. This function traverses all the operands in STMT and
8380 gimplifies anything that is not a valid gimple operand. Any new
8381 GIMPLE statements are inserted before *GSI_P. */
8383 void
8384 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8386 size_t i, num_ops;
8387 tree orig_lhs = NULL_TREE, lhs, t;
8388 gimple_seq pre = NULL;
8389 gimple post_stmt = NULL;
8390 struct gimplify_ctx gctx;
8392 push_gimplify_context (&gctx);
8393 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8395 switch (gimple_code (stmt))
8397 case GIMPLE_COND:
8398 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8399 is_gimple_val, fb_rvalue);
8400 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8401 is_gimple_val, fb_rvalue);
8402 break;
8403 case GIMPLE_SWITCH:
8404 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8405 is_gimple_val, fb_rvalue);
8406 break;
8407 case GIMPLE_OMP_ATOMIC_LOAD:
8408 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8409 is_gimple_val, fb_rvalue);
8410 break;
8411 case GIMPLE_ASM:
8413 size_t i, noutputs = gimple_asm_noutputs (stmt);
8414 const char *constraint, **oconstraints;
8415 bool allows_mem, allows_reg, is_inout;
8417 oconstraints
8418 = (const char **) alloca ((noutputs) * sizeof (const char *));
8419 for (i = 0; i < noutputs; i++)
8421 tree op = gimple_asm_output_op (stmt, i);
8422 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8423 oconstraints[i] = constraint;
8424 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8425 &allows_reg, &is_inout);
8426 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8427 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8428 fb_lvalue | fb_mayfail);
8430 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8432 tree op = gimple_asm_input_op (stmt, i);
8433 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8434 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8435 oconstraints, &allows_mem, &allows_reg);
8436 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8437 allows_reg = 0;
8438 if (!allows_reg && allows_mem)
8439 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8440 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8441 else
8442 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8443 is_gimple_asm_val, fb_rvalue);
8446 break;
8447 default:
8448 /* NOTE: We start gimplifying operands from last to first to
8449 make sure that side-effects on the RHS of calls, assignments
8450 and ASMs are executed before the LHS. The ordering is not
8451 important for other statements. */
8452 num_ops = gimple_num_ops (stmt);
8453 orig_lhs = gimple_get_lhs (stmt);
8454 for (i = num_ops; i > 0; i--)
8456 tree op = gimple_op (stmt, i - 1);
8457 if (op == NULL_TREE)
8458 continue;
8459 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8460 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8461 else if (i == 2
8462 && is_gimple_assign (stmt)
8463 && num_ops == 2
8464 && get_gimple_rhs_class (gimple_expr_code (stmt))
8465 == GIMPLE_SINGLE_RHS)
8466 gimplify_expr (&op, &pre, NULL,
8467 rhs_predicate_for (gimple_assign_lhs (stmt)),
8468 fb_rvalue);
8469 else if (i == 2 && is_gimple_call (stmt))
8471 if (TREE_CODE (op) == FUNCTION_DECL)
8472 continue;
8473 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8475 else
8476 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8477 gimple_set_op (stmt, i - 1, op);
8480 lhs = gimple_get_lhs (stmt);
8481 /* If the LHS changed it in a way that requires a simple RHS,
8482 create temporary. */
8483 if (lhs && !is_gimple_reg (lhs))
8485 bool need_temp = false;
8487 if (is_gimple_assign (stmt)
8488 && num_ops == 2
8489 && get_gimple_rhs_class (gimple_expr_code (stmt))
8490 == GIMPLE_SINGLE_RHS)
8491 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8492 rhs_predicate_for (gimple_assign_lhs (stmt)),
8493 fb_rvalue);
8494 else if (is_gimple_reg (lhs))
8496 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8498 if (is_gimple_call (stmt))
8500 i = gimple_call_flags (stmt);
8501 if ((i & ECF_LOOPING_CONST_OR_PURE)
8502 || !(i & (ECF_CONST | ECF_PURE)))
8503 need_temp = true;
8505 if (stmt_can_throw_internal (stmt))
8506 need_temp = true;
8509 else
8511 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8512 need_temp = true;
8513 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8515 if (is_gimple_call (stmt))
8517 tree fndecl = gimple_call_fndecl (stmt);
8519 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8520 && !(fndecl && DECL_RESULT (fndecl)
8521 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8522 need_temp = true;
8524 else
8525 need_temp = true;
8528 if (need_temp)
8530 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8532 if (TREE_CODE (orig_lhs) == SSA_NAME)
8533 orig_lhs = SSA_NAME_VAR (orig_lhs);
8535 if (gimple_in_ssa_p (cfun))
8536 temp = make_ssa_name (temp, NULL);
8537 gimple_set_lhs (stmt, temp);
8538 post_stmt = gimple_build_assign (lhs, temp);
8539 if (TREE_CODE (lhs) == SSA_NAME)
8540 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8543 break;
8546 if (gimple_referenced_vars (cfun))
8547 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
8548 add_referenced_var (t);
8550 if (!gimple_seq_empty_p (pre))
8552 if (gimple_in_ssa_p (cfun))
8554 gimple_stmt_iterator i;
8556 for (i = gsi_start (pre); !gsi_end_p (i); gsi_next (&i))
8558 tree lhs = gimple_get_lhs (gsi_stmt (i));
8559 if (lhs
8560 && TREE_CODE (lhs) != SSA_NAME
8561 && is_gimple_reg (lhs))
8562 mark_sym_for_renaming (lhs);
8565 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8567 if (post_stmt)
8568 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8570 pop_gimplify_context (NULL);
8573 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8574 the predicate that will hold for the result. If VAR is not NULL, make the
8575 base variable of the final destination be VAR if suitable. */
8577 tree
8578 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8579 gimple_predicate gimple_test_f, tree var)
8581 tree t;
8582 enum gimplify_status ret;
8583 struct gimplify_ctx gctx;
8585 *stmts = NULL;
8587 /* gimple_test_f might be more strict than is_gimple_val, make
8588 sure we pass both. Just checking gimple_test_f doesn't work
8589 because most gimple predicates do not work recursively. */
8590 if (is_gimple_val (expr)
8591 && (*gimple_test_f) (expr))
8592 return expr;
8594 push_gimplify_context (&gctx);
8595 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8596 gimplify_ctxp->allow_rhs_cond_expr = true;
8598 if (var)
8599 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8601 if (TREE_CODE (expr) != MODIFY_EXPR
8602 && TREE_TYPE (expr) == void_type_node)
8604 gimplify_and_add (expr, stmts);
8605 expr = NULL_TREE;
8607 else
8609 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8610 gcc_assert (ret != GS_ERROR);
8613 if (gimple_referenced_vars (cfun))
8614 for (t = gimplify_ctxp->temps; t ; t = DECL_CHAIN (t))
8615 add_referenced_var (t);
8617 if (!gimple_seq_empty_p (*stmts)
8618 && gimplify_ctxp->into_ssa)
8620 gimple_stmt_iterator i;
8622 for (i = gsi_start (*stmts); !gsi_end_p (i); gsi_next (&i))
8624 tree lhs = gimple_get_lhs (gsi_stmt (i));
8625 if (lhs
8626 && TREE_CODE (lhs) != SSA_NAME
8627 && is_gimple_reg (lhs))
8628 mark_sym_for_renaming (lhs);
8632 pop_gimplify_context (NULL);
8634 return expr;
8637 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8638 force the result to be either ssa_name or an invariant, otherwise
8639 just force it to be a rhs expression. If VAR is not NULL, make the
8640 base variable of the final destination be VAR if suitable. */
8642 tree
8643 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8645 return force_gimple_operand_1 (expr, stmts,
8646 simple ? is_gimple_val : is_gimple_reg_rhs,
8647 var);
8650 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8651 and VAR. If some statements are produced, emits them at GSI.
8652 If BEFORE is true. the statements are appended before GSI, otherwise
8653 they are appended after it. M specifies the way GSI moves after
8654 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8656 tree
8657 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8658 gimple_predicate gimple_test_f,
8659 tree var, bool before,
8660 enum gsi_iterator_update m)
8662 gimple_seq stmts;
8664 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8666 if (!gimple_seq_empty_p (stmts))
8668 if (before)
8669 gsi_insert_seq_before (gsi, stmts, m);
8670 else
8671 gsi_insert_seq_after (gsi, stmts, m);
8674 return expr;
8677 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8678 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8679 otherwise just force it to be a rhs expression. If some statements are
8680 produced, emits them at GSI. If BEFORE is true, the statements are
8681 appended before GSI, otherwise they are appended after it. M specifies
8682 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8683 are the usual values). */
8685 tree
8686 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8687 bool simple_p, tree var, bool before,
8688 enum gsi_iterator_update m)
8690 return force_gimple_operand_gsi_1 (gsi, expr,
8691 simple_p
8692 ? is_gimple_val : is_gimple_reg_rhs,
8693 var, before, m);
8697 #include "gt-gimplify.h"