* config.gcc (cygwin tm_file): Add cygwin-stdint.h.
[official-gcc.git] / gcc / gimplify.c
blob515c58ed57e9ead1a1dcd66f262daa2d7bb08116
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
4 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 "rtl.h"
30 #include "varray.h"
31 #include "gimple.h"
32 #include "tree-iterator.h"
33 #include "tree-inline.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "tree-flow.h"
38 #include "cgraph.h"
39 #include "timevar.h"
40 #include "except.h"
41 #include "hashtab.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "function.h"
45 #include "output.h"
46 #include "expr.h"
47 #include "ggc.h"
48 #include "toplev.h"
49 #include "target.h"
50 #include "optabs.h"
51 #include "pointer-set.h"
52 #include "splay-tree.h"
53 #include "vec.h"
54 #include "gimple.h"
57 enum gimplify_omp_var_data
59 GOVD_SEEN = 1,
60 GOVD_EXPLICIT = 2,
61 GOVD_SHARED = 4,
62 GOVD_PRIVATE = 8,
63 GOVD_FIRSTPRIVATE = 16,
64 GOVD_LASTPRIVATE = 32,
65 GOVD_REDUCTION = 64,
66 GOVD_LOCAL = 128,
67 GOVD_DEBUG_PRIVATE = 256,
68 GOVD_PRIVATE_OUTER_REF = 512,
69 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
70 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
74 enum omp_region_type
76 ORT_WORKSHARE = 0,
77 ORT_TASK = 1,
78 ORT_PARALLEL = 2,
79 ORT_COMBINED_PARALLEL = 3
82 struct gimplify_omp_ctx
84 struct gimplify_omp_ctx *outer_context;
85 splay_tree variables;
86 struct pointer_set_t *privatized_types;
87 location_t location;
88 enum omp_clause_default_kind default_kind;
89 enum omp_region_type region_type;
92 static struct gimplify_ctx *gimplify_ctxp;
93 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
96 /* Formal (expression) temporary table handling: Multiple occurrences of
97 the same scalar expression are evaluated into the same temporary. */
99 typedef struct gimple_temp_hash_elt
101 tree val; /* Key */
102 tree temp; /* Value */
103 } elt_t;
105 /* Forward declarations. */
106 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
108 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
109 form and we don't do any syntax checking. */
110 void
111 mark_addressable (tree x)
113 while (handled_component_p (x))
114 x = TREE_OPERAND (x, 0);
115 if (TREE_CODE (x) != VAR_DECL
116 && TREE_CODE (x) != PARM_DECL
117 && TREE_CODE (x) != RESULT_DECL)
118 return ;
119 TREE_ADDRESSABLE (x) = 1;
122 /* Return a hash value for a formal temporary table entry. */
124 static hashval_t
125 gimple_tree_hash (const void *p)
127 tree t = ((const elt_t *) p)->val;
128 return iterative_hash_expr (t, 0);
131 /* Compare two formal temporary table entries. */
133 static int
134 gimple_tree_eq (const void *p1, const void *p2)
136 tree t1 = ((const elt_t *) p1)->val;
137 tree t2 = ((const elt_t *) p2)->val;
138 enum tree_code code = TREE_CODE (t1);
140 if (TREE_CODE (t2) != code
141 || TREE_TYPE (t1) != TREE_TYPE (t2))
142 return 0;
144 if (!operand_equal_p (t1, t2, 0))
145 return 0;
147 /* Only allow them to compare equal if they also hash equal; otherwise
148 results are nondeterminate, and we fail bootstrap comparison. */
149 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
151 return 1;
154 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
155 *SEQ_P is NULL, a new sequence is allocated. This function is
156 similar to gimple_seq_add_stmt, but does not scan the operands.
157 During gimplification, we need to manipulate statement sequences
158 before the def/use vectors have been constructed. */
160 static void
161 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
163 gimple_stmt_iterator si;
165 if (gs == NULL)
166 return;
168 if (*seq_p == NULL)
169 *seq_p = gimple_seq_alloc ();
171 si = gsi_last (*seq_p);
173 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
176 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
177 NULL, a new sequence is allocated. This function is
178 similar to gimple_seq_add_seq, but does not scan the operands.
179 During gimplification, we need to manipulate statement sequences
180 before the def/use vectors have been constructed. */
182 static void
183 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
185 gimple_stmt_iterator si;
187 if (src == NULL)
188 return;
190 if (*dst_p == NULL)
191 *dst_p = gimple_seq_alloc ();
193 si = gsi_last (*dst_p);
194 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
197 /* Set up a context for the gimplifier. */
199 void
200 push_gimplify_context (struct gimplify_ctx *c)
202 memset (c, '\0', sizeof (*c));
203 c->prev_context = gimplify_ctxp;
204 gimplify_ctxp = c;
207 /* Tear down a context for the gimplifier. If BODY is non-null, then
208 put the temporaries into the outer BIND_EXPR. Otherwise, put them
209 in the local_decls.
211 BODY is not a sequence, but the first tuple in a sequence. */
213 void
214 pop_gimplify_context (gimple body)
216 struct gimplify_ctx *c = gimplify_ctxp;
218 gcc_assert (c && (c->bind_expr_stack == NULL
219 || VEC_empty (gimple, c->bind_expr_stack)));
220 VEC_free (gimple, heap, c->bind_expr_stack);
221 gimplify_ctxp = c->prev_context;
223 if (body)
224 declare_vars (c->temps, body, false);
225 else
226 record_vars (c->temps);
228 if (c->temp_htab)
229 htab_delete (c->temp_htab);
232 static void
233 gimple_push_bind_expr (gimple gimple_bind)
235 if (gimplify_ctxp->bind_expr_stack == NULL)
236 gimplify_ctxp->bind_expr_stack = VEC_alloc (gimple, heap, 8);
237 VEC_safe_push (gimple, heap, gimplify_ctxp->bind_expr_stack, gimple_bind);
240 static void
241 gimple_pop_bind_expr (void)
243 VEC_pop (gimple, gimplify_ctxp->bind_expr_stack);
246 gimple
247 gimple_current_bind_expr (void)
249 return VEC_last (gimple, gimplify_ctxp->bind_expr_stack);
252 /* Return the stack GIMPLE_BINDs created during gimplification. */
254 VEC(gimple, heap) *
255 gimple_bind_expr_stack (void)
257 return gimplify_ctxp->bind_expr_stack;
260 /* Returns true iff there is a COND_EXPR between us and the innermost
261 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
263 static bool
264 gimple_conditional_context (void)
266 return gimplify_ctxp->conditions > 0;
269 /* Note that we've entered a COND_EXPR. */
271 static void
272 gimple_push_condition (void)
274 #ifdef ENABLE_GIMPLE_CHECKING
275 if (gimplify_ctxp->conditions == 0)
276 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
277 #endif
278 ++(gimplify_ctxp->conditions);
281 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
282 now, add any conditional cleanups we've seen to the prequeue. */
284 static void
285 gimple_pop_condition (gimple_seq *pre_p)
287 int conds = --(gimplify_ctxp->conditions);
289 gcc_assert (conds >= 0);
290 if (conds == 0)
292 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
293 gimplify_ctxp->conditional_cleanups = NULL;
297 /* A stable comparison routine for use with splay trees and DECLs. */
299 static int
300 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
302 tree a = (tree) xa;
303 tree b = (tree) xb;
305 return DECL_UID (a) - DECL_UID (b);
308 /* Create a new omp construct that deals with variable remapping. */
310 static struct gimplify_omp_ctx *
311 new_omp_context (enum omp_region_type region_type)
313 struct gimplify_omp_ctx *c;
315 c = XCNEW (struct gimplify_omp_ctx);
316 c->outer_context = gimplify_omp_ctxp;
317 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
318 c->privatized_types = pointer_set_create ();
319 c->location = input_location;
320 c->region_type = region_type;
321 if (region_type != ORT_TASK)
322 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
323 else
324 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
326 return c;
329 /* Destroy an omp construct that deals with variable remapping. */
331 static void
332 delete_omp_context (struct gimplify_omp_ctx *c)
334 splay_tree_delete (c->variables);
335 pointer_set_destroy (c->privatized_types);
336 XDELETE (c);
339 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
340 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
342 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
344 static void
345 append_to_statement_list_1 (tree t, tree *list_p)
347 tree list = *list_p;
348 tree_stmt_iterator i;
350 if (!list)
352 if (t && TREE_CODE (t) == STATEMENT_LIST)
354 *list_p = t;
355 return;
357 *list_p = list = alloc_stmt_list ();
360 i = tsi_last (list);
361 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
364 /* Add T to the end of the list container pointed to by LIST_P.
365 If T is an expression with no effects, it is ignored. */
367 void
368 append_to_statement_list (tree t, tree *list_p)
370 if (t && TREE_SIDE_EFFECTS (t))
371 append_to_statement_list_1 (t, list_p);
374 /* Similar, but the statement is always added, regardless of side effects. */
376 void
377 append_to_statement_list_force (tree t, tree *list_p)
379 if (t != NULL_TREE)
380 append_to_statement_list_1 (t, list_p);
383 /* Both gimplify the statement T and append it to *SEQ_P. This function
384 behaves exactly as gimplify_stmt, but you don't have to pass T as a
385 reference. */
387 void
388 gimplify_and_add (tree t, gimple_seq *seq_p)
390 gimplify_stmt (&t, seq_p);
393 /* Gimplify statement T into sequence *SEQ_P, and return the first
394 tuple in the sequence of generated tuples for this statement.
395 Return NULL if gimplifying T produced no tuples. */
397 static gimple
398 gimplify_and_return_first (tree t, gimple_seq *seq_p)
400 gimple_stmt_iterator last = gsi_last (*seq_p);
402 gimplify_and_add (t, seq_p);
404 if (!gsi_end_p (last))
406 gsi_next (&last);
407 return gsi_stmt (last);
409 else
410 return gimple_seq_first_stmt (*seq_p);
413 /* Strip off a legitimate source ending from the input string NAME of
414 length LEN. Rather than having to know the names used by all of
415 our front ends, we strip off an ending of a period followed by
416 up to five characters. (Java uses ".class".) */
418 static inline void
419 remove_suffix (char *name, int len)
421 int i;
423 for (i = 2; i < 8 && len > i; i++)
425 if (name[len - i] == '.')
427 name[len - i] = '\0';
428 break;
433 /* Subroutine for find_single_pointer_decl. */
435 static tree
436 find_single_pointer_decl_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
437 void *data)
439 tree *pdecl = (tree *) data;
441 /* We are only looking for pointers at the same level as the
442 original tree; we must not look through any indirections.
443 Returning anything other than NULL_TREE will cause the caller to
444 not find a base. */
445 if (REFERENCE_CLASS_P (*tp))
446 return *tp;
448 if (DECL_P (*tp) && POINTER_TYPE_P (TREE_TYPE (*tp)))
450 if (*pdecl)
452 /* We already found a pointer decl; return anything other
453 than NULL_TREE to unwind from walk_tree signalling that
454 we have a duplicate. */
455 return *tp;
457 *pdecl = *tp;
460 return NULL_TREE;
463 /* Find the single DECL of pointer type in the tree T, used directly
464 rather than via an indirection, and return it. If there are zero
465 or more than one such DECLs, return NULL. */
467 static tree
468 find_single_pointer_decl (tree t)
470 tree decl = NULL_TREE;
472 if (walk_tree (&t, find_single_pointer_decl_1, &decl, NULL))
474 /* find_single_pointer_decl_1 returns a nonzero value, causing
475 walk_tree to return a nonzero value, to indicate that it
476 found more than one pointer DECL or that it found an
477 indirection. */
478 return NULL_TREE;
481 return decl;
484 /* Create a new temporary name with PREFIX. Returns an identifier. */
486 static GTY(()) unsigned int tmp_var_id_num;
488 tree
489 create_tmp_var_name (const char *prefix)
491 char *tmp_name;
493 if (prefix)
495 char *preftmp = ASTRDUP (prefix);
497 remove_suffix (preftmp, strlen (preftmp));
498 prefix = preftmp;
501 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
502 return get_identifier (tmp_name);
506 /* Create a new temporary variable declaration of type TYPE.
507 Does NOT push it into the current binding. */
509 tree
510 create_tmp_var_raw (tree type, const char *prefix)
512 tree tmp_var;
513 tree new_type;
515 /* Make the type of the variable writable. */
516 new_type = build_type_variant (type, 0, 0);
517 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
519 tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
520 type);
522 /* The variable was declared by the compiler. */
523 DECL_ARTIFICIAL (tmp_var) = 1;
524 /* And we don't want debug info for it. */
525 DECL_IGNORED_P (tmp_var) = 1;
527 /* Make the variable writable. */
528 TREE_READONLY (tmp_var) = 0;
530 DECL_EXTERNAL (tmp_var) = 0;
531 TREE_STATIC (tmp_var) = 0;
532 TREE_USED (tmp_var) = 1;
534 return tmp_var;
537 /* Create a new temporary variable declaration of type TYPE. DOES push the
538 variable into the current binding. Further, assume that this is called
539 only from gimplification or optimization, at which point the creation of
540 certain types are bugs. */
542 tree
543 create_tmp_var (tree type, const char *prefix)
545 tree tmp_var;
547 /* We don't allow types that are addressable (meaning we can't make copies),
548 or incomplete. We also used to reject every variable size objects here,
549 but now support those for which a constant upper bound can be obtained.
550 The processing for variable sizes is performed in gimple_add_tmp_var,
551 point at which it really matters and possibly reached via paths not going
552 through this function, e.g. after direct calls to create_tmp_var_raw. */
553 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
555 tmp_var = create_tmp_var_raw (type, prefix);
556 gimple_add_tmp_var (tmp_var);
557 return tmp_var;
560 /* Create a temporary with a name derived from VAL. Subroutine of
561 lookup_tmp_var; nobody else should call this function. */
563 static inline tree
564 create_tmp_from_val (tree val)
566 return create_tmp_var (TREE_TYPE (val), get_name (val));
569 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
570 an existing expression temporary. */
572 static tree
573 lookup_tmp_var (tree val, bool is_formal)
575 tree ret;
577 /* If not optimizing, never really reuse a temporary. local-alloc
578 won't allocate any variable that is used in more than one basic
579 block, which means it will go into memory, causing much extra
580 work in reload and final and poorer code generation, outweighing
581 the extra memory allocation here. */
582 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
583 ret = create_tmp_from_val (val);
584 else
586 elt_t elt, *elt_p;
587 void **slot;
589 elt.val = val;
590 if (gimplify_ctxp->temp_htab == NULL)
591 gimplify_ctxp->temp_htab
592 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
593 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
594 if (*slot == NULL)
596 elt_p = XNEW (elt_t);
597 elt_p->val = val;
598 elt_p->temp = ret = create_tmp_from_val (val);
599 *slot = (void *) elt_p;
601 else
603 elt_p = (elt_t *) *slot;
604 ret = elt_p->temp;
608 return ret;
612 /* Return true if T is a CALL_EXPR or an expression that can be
613 assignmed to a temporary. Note that this predicate should only be
614 used during gimplification. See the rationale for this in
615 gimplify_modify_expr. */
617 static bool
618 is_gimple_reg_rhs_or_call (tree t)
620 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
621 || TREE_CODE (t) == CALL_EXPR);
624 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
625 this predicate should only be used during gimplification. See the
626 rationale for this in gimplify_modify_expr. */
628 static bool
629 is_gimple_mem_rhs_or_call (tree t)
631 /* If we're dealing with a renamable type, either source or dest must be
632 a renamed variable. */
633 if (is_gimple_reg_type (TREE_TYPE (t)))
634 return is_gimple_val (t);
635 else
636 return (is_gimple_val (t) || is_gimple_lvalue (t)
637 || TREE_CODE (t) == CALL_EXPR);
640 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
642 static tree
643 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
644 bool is_formal)
646 tree t, mod;
648 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
649 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
650 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
651 fb_rvalue);
653 t = lookup_tmp_var (val, is_formal);
655 if (is_formal)
657 tree u = find_single_pointer_decl (val);
659 if (u && TREE_CODE (u) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (u))
660 u = DECL_GET_RESTRICT_BASE (u);
661 if (u && TYPE_RESTRICT (TREE_TYPE (u)))
663 if (DECL_BASED_ON_RESTRICT_P (t))
664 gcc_assert (u == DECL_GET_RESTRICT_BASE (t));
665 else
667 DECL_BASED_ON_RESTRICT_P (t) = 1;
668 SET_DECL_RESTRICT_BASE (t, u);
672 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
673 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
674 DECL_GIMPLE_REG_P (t) = 1;
677 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
679 if (EXPR_HAS_LOCATION (val))
680 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
681 else
682 SET_EXPR_LOCATION (mod, input_location);
684 /* gimplify_modify_expr might want to reduce this further. */
685 gimplify_and_add (mod, pre_p);
686 ggc_free (mod);
688 /* If we're gimplifying into ssa, gimplify_modify_expr will have
689 given our temporary an SSA name. Find and return it. */
690 if (gimplify_ctxp->into_ssa)
692 gimple last = gimple_seq_last_stmt (*pre_p);
693 t = gimple_get_lhs (last);
696 return t;
699 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
700 in gimplify_expr. Only use this function if:
702 1) The value of the unfactored expression represented by VAL will not
703 change between the initialization and use of the temporary, and
704 2) The temporary will not be otherwise modified.
706 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
707 and #2 means it is inappropriate for && temps.
709 For other cases, use get_initialized_tmp_var instead. */
711 tree
712 get_formal_tmp_var (tree val, gimple_seq *pre_p)
714 return internal_get_tmp_var (val, pre_p, NULL, true);
717 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
718 are as in gimplify_expr. */
720 tree
721 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
723 return internal_get_tmp_var (val, pre_p, post_p, false);
726 /* Declares all the variables in VARS in SCOPE. If DEBUG_INFO is
727 true, generate debug info for them; otherwise don't. */
729 void
730 declare_vars (tree vars, gimple scope, bool debug_info)
732 tree last = vars;
733 if (last)
735 tree temps, block;
737 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
739 temps = nreverse (last);
741 block = gimple_bind_block (scope);
742 gcc_assert (!block || TREE_CODE (block) == BLOCK);
743 if (!block || !debug_info)
745 TREE_CHAIN (last) = gimple_bind_vars (scope);
746 gimple_bind_set_vars (scope, temps);
748 else
750 /* We need to attach the nodes both to the BIND_EXPR and to its
751 associated BLOCK for debugging purposes. The key point here
752 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
753 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
754 if (BLOCK_VARS (block))
755 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
756 else
758 gimple_bind_set_vars (scope,
759 chainon (gimple_bind_vars (scope), temps));
760 BLOCK_VARS (block) = temps;
766 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
767 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
768 no such upper bound can be obtained. */
770 static void
771 force_constant_size (tree var)
773 /* The only attempt we make is by querying the maximum size of objects
774 of the variable's type. */
776 HOST_WIDE_INT max_size;
778 gcc_assert (TREE_CODE (var) == VAR_DECL);
780 max_size = max_int_size_in_bytes (TREE_TYPE (var));
782 gcc_assert (max_size >= 0);
784 DECL_SIZE_UNIT (var)
785 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
786 DECL_SIZE (var)
787 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
790 void
791 gimple_add_tmp_var (tree tmp)
793 gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
795 /* Later processing assumes that the object size is constant, which might
796 not be true at this point. Force the use of a constant upper bound in
797 this case. */
798 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
799 force_constant_size (tmp);
801 DECL_CONTEXT (tmp) = current_function_decl;
802 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
804 if (gimplify_ctxp)
806 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
807 gimplify_ctxp->temps = tmp;
809 /* Mark temporaries local within the nearest enclosing parallel. */
810 if (gimplify_omp_ctxp)
812 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
813 while (ctx && ctx->region_type == ORT_WORKSHARE)
814 ctx = ctx->outer_context;
815 if (ctx)
816 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
819 else if (cfun)
820 record_vars (tmp);
821 else
823 gimple_seq body_seq;
825 /* This case is for nested functions. We need to expose the locals
826 they create. */
827 body_seq = gimple_body (current_function_decl);
828 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
832 /* Determines whether to assign a location to the statement GS. */
834 static bool
835 should_carry_location_p (gimple gs)
837 /* Don't emit a line note for a label. We particularly don't want to
838 emit one for the break label, since it doesn't actually correspond
839 to the beginning of the loop/switch. */
840 if (gimple_code (gs) == GIMPLE_LABEL)
841 return false;
843 return true;
846 /* Same, but for a tree. */
848 static bool
849 tree_should_carry_location_p (const_tree stmt)
851 /* Don't emit a line note for a label. We particularly don't want to
852 emit one for the break label, since it doesn't actually correspond
853 to the beginning of the loop/switch. */
854 if (TREE_CODE (stmt) == LABEL_EXPR)
855 return false;
857 /* Do not annotate empty statements, since it confuses gcov. */
858 if (!TREE_SIDE_EFFECTS (stmt))
859 return false;
861 return true;
864 /* Return true if a location should not be emitted for this statement
865 by annotate_one_with_location. */
867 static inline bool
868 gimple_do_not_emit_location_p (gimple g)
870 return gimple_plf (g, GF_PLF_1);
873 /* Mark statement G so a location will not be emitted by
874 annotate_one_with_location. */
876 static inline void
877 gimple_set_do_not_emit_location (gimple g)
879 /* The PLF flags are initialized to 0 when a new tuple is created,
880 so no need to initialize it anywhere. */
881 gimple_set_plf (g, GF_PLF_1, true);
884 /* Set the location for gimple statement GS to LOCUS. */
886 static void
887 annotate_one_with_location (gimple gs, location_t location)
889 if (!gimple_has_location (gs)
890 && !gimple_do_not_emit_location_p (gs)
891 && should_carry_location_p (gs))
892 gimple_set_location (gs, location);
895 /* Same, but for tree T. */
897 static void
898 tree_annotate_one_with_location (tree t, location_t location)
900 if (CAN_HAVE_LOCATION_P (t)
901 && ! EXPR_HAS_LOCATION (t) && tree_should_carry_location_p (t))
902 SET_EXPR_LOCATION (t, location);
906 /* Set LOCATION for all the statements after iterator GSI in sequence
907 SEQ. If GSI is pointing to the end of the sequence, start with the
908 first statement in SEQ. */
910 static void
911 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
912 location_t location)
914 if (gsi_end_p (gsi))
915 gsi = gsi_start (seq);
916 else
917 gsi_next (&gsi);
919 for (; !gsi_end_p (gsi); gsi_next (&gsi))
920 annotate_one_with_location (gsi_stmt (gsi), location);
924 /* Set the location for all the statements in a sequence STMT_P to LOCUS. */
926 void
927 annotate_all_with_location (gimple_seq stmt_p, location_t location)
929 gimple_stmt_iterator i;
931 if (gimple_seq_empty_p (stmt_p))
932 return;
934 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
936 gimple gs = gsi_stmt (i);
937 annotate_one_with_location (gs, location);
941 /* Same, but for statement or statement list in *STMT_P. */
943 void
944 tree_annotate_all_with_location (tree *stmt_p, location_t location)
946 tree_stmt_iterator i;
948 if (!*stmt_p)
949 return;
951 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
953 tree t = tsi_stmt (i);
955 /* Assuming we've already been gimplified, we shouldn't
956 see nested chaining constructs anymore. */
957 gcc_assert (TREE_CODE (t) != STATEMENT_LIST
958 && TREE_CODE (t) != COMPOUND_EXPR);
960 tree_annotate_one_with_location (t, location);
965 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
966 These nodes model computations that should only be done once. If we
967 were to unshare something like SAVE_EXPR(i++), the gimplification
968 process would create wrong code. */
970 static tree
971 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
973 enum tree_code code = TREE_CODE (*tp);
974 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
975 if (TREE_CODE_CLASS (code) == tcc_type
976 || TREE_CODE_CLASS (code) == tcc_declaration
977 || TREE_CODE_CLASS (code) == tcc_constant
978 || code == SAVE_EXPR || code == TARGET_EXPR
979 /* We can't do anything sensible with a BLOCK used as an expression,
980 but we also can't just die when we see it because of non-expression
981 uses. So just avert our eyes and cross our fingers. Silly Java. */
982 || code == BLOCK)
983 *walk_subtrees = 0;
984 else
986 gcc_assert (code != BIND_EXPR);
987 copy_tree_r (tp, walk_subtrees, data);
990 return NULL_TREE;
993 /* Callback for walk_tree to unshare most of the shared trees rooted at
994 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
995 then *TP is deep copied by calling copy_tree_r.
997 This unshares the same trees as copy_tree_r with the exception of
998 SAVE_EXPR nodes. These nodes model computations that should only be
999 done once. If we were to unshare something like SAVE_EXPR(i++), the
1000 gimplification process would create wrong code. */
1002 static tree
1003 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
1004 void *data ATTRIBUTE_UNUSED)
1006 tree t = *tp;
1007 enum tree_code code = TREE_CODE (t);
1009 /* Skip types, decls, and constants. But we do want to look at their
1010 types and the bounds of types. Mark them as visited so we properly
1011 unmark their subtrees on the unmark pass. If we've already seen them,
1012 don't look down further. */
1013 if (TREE_CODE_CLASS (code) == tcc_type
1014 || TREE_CODE_CLASS (code) == tcc_declaration
1015 || TREE_CODE_CLASS (code) == tcc_constant)
1017 if (TREE_VISITED (t))
1018 *walk_subtrees = 0;
1019 else
1020 TREE_VISITED (t) = 1;
1023 /* If this node has been visited already, unshare it and don't look
1024 any deeper. */
1025 else if (TREE_VISITED (t))
1027 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
1028 *walk_subtrees = 0;
1031 /* Otherwise, mark the tree as visited and keep looking. */
1032 else
1033 TREE_VISITED (t) = 1;
1035 return NULL_TREE;
1038 static tree
1039 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
1040 void *data ATTRIBUTE_UNUSED)
1042 if (TREE_VISITED (*tp))
1043 TREE_VISITED (*tp) = 0;
1044 else
1045 *walk_subtrees = 0;
1047 return NULL_TREE;
1050 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
1051 bodies of any nested functions if we are unsharing the entire body of
1052 FNDECL. */
1054 static void
1055 unshare_body (tree *body_p, tree fndecl)
1057 struct cgraph_node *cgn = cgraph_node (fndecl);
1059 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
1060 if (body_p == &DECL_SAVED_TREE (fndecl))
1061 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1062 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
1065 /* Likewise, but mark all trees as not visited. */
1067 static void
1068 unvisit_body (tree *body_p, tree fndecl)
1070 struct cgraph_node *cgn = cgraph_node (fndecl);
1072 walk_tree (body_p, unmark_visited_r, NULL, NULL);
1073 if (body_p == &DECL_SAVED_TREE (fndecl))
1074 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1075 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
1078 /* Unconditionally make an unshared copy of EXPR. This is used when using
1079 stored expressions which span multiple functions, such as BINFO_VTABLE,
1080 as the normal unsharing process can't tell that they're shared. */
1082 tree
1083 unshare_expr (tree expr)
1085 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1086 return expr;
1089 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1090 contain statements and have a value. Assign its value to a temporary
1091 and give it void_type_node. Returns the temporary, or NULL_TREE if
1092 WRAPPER was already void. */
1094 tree
1095 voidify_wrapper_expr (tree wrapper, tree temp)
1097 tree type = TREE_TYPE (wrapper);
1098 if (type && !VOID_TYPE_P (type))
1100 tree *p;
1102 /* Set p to point to the body of the wrapper. Loop until we find
1103 something that isn't a wrapper. */
1104 for (p = &wrapper; p && *p; )
1106 switch (TREE_CODE (*p))
1108 case BIND_EXPR:
1109 TREE_SIDE_EFFECTS (*p) = 1;
1110 TREE_TYPE (*p) = void_type_node;
1111 /* For a BIND_EXPR, the body is operand 1. */
1112 p = &BIND_EXPR_BODY (*p);
1113 break;
1115 case CLEANUP_POINT_EXPR:
1116 case TRY_FINALLY_EXPR:
1117 case TRY_CATCH_EXPR:
1118 TREE_SIDE_EFFECTS (*p) = 1;
1119 TREE_TYPE (*p) = void_type_node;
1120 p = &TREE_OPERAND (*p, 0);
1121 break;
1123 case STATEMENT_LIST:
1125 tree_stmt_iterator i = tsi_last (*p);
1126 TREE_SIDE_EFFECTS (*p) = 1;
1127 TREE_TYPE (*p) = void_type_node;
1128 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1130 break;
1132 case COMPOUND_EXPR:
1133 /* Advance to the last statement. Set all container types to void. */
1134 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1136 TREE_SIDE_EFFECTS (*p) = 1;
1137 TREE_TYPE (*p) = void_type_node;
1139 break;
1141 default:
1142 goto out;
1146 out:
1147 if (p == NULL || IS_EMPTY_STMT (*p))
1148 temp = NULL_TREE;
1149 else if (temp)
1151 /* The wrapper is on the RHS of an assignment that we're pushing
1152 down. */
1153 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1154 || TREE_CODE (temp) == MODIFY_EXPR);
1155 TREE_OPERAND (temp, 1) = *p;
1156 *p = temp;
1158 else
1160 temp = create_tmp_var (type, "retval");
1161 *p = build2 (INIT_EXPR, type, temp, *p);
1164 return temp;
1167 return NULL_TREE;
1170 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1171 a temporary through which they communicate. */
1173 static void
1174 build_stack_save_restore (gimple *save, gimple *restore)
1176 tree tmp_var;
1178 *save = gimple_build_call (implicit_built_in_decls[BUILT_IN_STACK_SAVE], 0);
1179 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1180 gimple_call_set_lhs (*save, tmp_var);
1182 *restore = gimple_build_call (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
1183 1, tmp_var);
1186 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1188 static enum gimplify_status
1189 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1191 tree bind_expr = *expr_p;
1192 bool old_save_stack = gimplify_ctxp->save_stack;
1193 tree t;
1194 gimple gimple_bind;
1195 gimple_seq body;
1197 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1199 /* Mark variables seen in this bind expr. */
1200 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1202 if (TREE_CODE (t) == VAR_DECL)
1204 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1206 /* Mark variable as local. */
1207 if (ctx && !is_global_var (t)
1208 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1209 || splay_tree_lookup (ctx->variables,
1210 (splay_tree_key) t) == NULL))
1211 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1213 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1215 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1216 cfun->has_local_explicit_reg_vars = true;
1219 /* Preliminarily mark non-addressed complex variables as eligible
1220 for promotion to gimple registers. We'll transform their uses
1221 as we find them. */
1222 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1223 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1224 && !TREE_THIS_VOLATILE (t)
1225 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1226 && !needs_to_live_in_memory (t))
1227 DECL_GIMPLE_REG_P (t) = 1;
1230 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1231 BIND_EXPR_BLOCK (bind_expr));
1232 gimple_push_bind_expr (gimple_bind);
1234 gimplify_ctxp->save_stack = false;
1236 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1237 body = NULL;
1238 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1239 gimple_bind_set_body (gimple_bind, body);
1241 if (gimplify_ctxp->save_stack)
1243 gimple stack_save, stack_restore, gs;
1244 gimple_seq cleanup, new_body;
1246 /* Save stack on entry and restore it on exit. Add a try_finally
1247 block to achieve this. Note that mudflap depends on the
1248 format of the emitted code: see mx_register_decls(). */
1249 build_stack_save_restore (&stack_save, &stack_restore);
1251 cleanup = new_body = NULL;
1252 gimplify_seq_add_stmt (&cleanup, stack_restore);
1253 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1254 GIMPLE_TRY_FINALLY);
1256 gimplify_seq_add_stmt (&new_body, stack_save);
1257 gimplify_seq_add_stmt (&new_body, gs);
1258 gimple_bind_set_body (gimple_bind, new_body);
1261 gimplify_ctxp->save_stack = old_save_stack;
1262 gimple_pop_bind_expr ();
1264 gimplify_seq_add_stmt (pre_p, gimple_bind);
1266 if (temp)
1268 *expr_p = temp;
1269 return GS_OK;
1272 *expr_p = NULL_TREE;
1273 return GS_ALL_DONE;
1276 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1277 GIMPLE value, it is assigned to a new temporary and the statement is
1278 re-written to return the temporary.
1280 PRE_P points to the sequence where side effects that must happen before
1281 STMT should be stored. */
1283 static enum gimplify_status
1284 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1286 gimple ret;
1287 tree ret_expr = TREE_OPERAND (stmt, 0);
1288 tree result_decl, result;
1290 if (ret_expr == error_mark_node)
1291 return GS_ERROR;
1293 if (!ret_expr
1294 || TREE_CODE (ret_expr) == RESULT_DECL
1295 || ret_expr == error_mark_node)
1297 gimple ret = gimple_build_return (ret_expr);
1298 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1299 gimplify_seq_add_stmt (pre_p, ret);
1300 return GS_ALL_DONE;
1303 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1304 result_decl = NULL_TREE;
1305 else
1307 result_decl = TREE_OPERAND (ret_expr, 0);
1309 /* See through a return by reference. */
1310 if (TREE_CODE (result_decl) == INDIRECT_REF)
1311 result_decl = TREE_OPERAND (result_decl, 0);
1313 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1314 || TREE_CODE (ret_expr) == INIT_EXPR)
1315 && TREE_CODE (result_decl) == RESULT_DECL);
1318 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1319 Recall that aggregate_value_p is FALSE for any aggregate type that is
1320 returned in registers. If we're returning values in registers, then
1321 we don't want to extend the lifetime of the RESULT_DECL, particularly
1322 across another call. In addition, for those aggregates for which
1323 hard_function_value generates a PARALLEL, we'll die during normal
1324 expansion of structure assignments; there's special code in expand_return
1325 to handle this case that does not exist in expand_expr. */
1326 if (!result_decl
1327 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1328 result = result_decl;
1329 else if (gimplify_ctxp->return_temp)
1330 result = gimplify_ctxp->return_temp;
1331 else
1333 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
1334 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1335 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1336 DECL_GIMPLE_REG_P (result) = 1;
1338 /* ??? With complex control flow (usually involving abnormal edges),
1339 we can wind up warning about an uninitialized value for this. Due
1340 to how this variable is constructed and initialized, this is never
1341 true. Give up and never warn. */
1342 TREE_NO_WARNING (result) = 1;
1344 gimplify_ctxp->return_temp = result;
1347 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1348 Then gimplify the whole thing. */
1349 if (result != result_decl)
1350 TREE_OPERAND (ret_expr, 0) = result;
1352 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1354 ret = gimple_build_return (result);
1355 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1356 gimplify_seq_add_stmt (pre_p, ret);
1358 return GS_ALL_DONE;
1361 static void
1362 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1364 /* This is a variable-sized decl. Simplify its size and mark it
1365 for deferred expansion. Note that mudflap depends on the format
1366 of the emitted code: see mx_register_decls(). */
1367 tree t, addr, ptr_type;
1369 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1370 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1372 /* All occurrences of this decl in final gimplified code will be
1373 replaced by indirection. Setting DECL_VALUE_EXPR does two
1374 things: First, it lets the rest of the gimplifier know what
1375 replacement to use. Second, it lets the debug info know
1376 where to find the value. */
1377 ptr_type = build_pointer_type (TREE_TYPE (decl));
1378 addr = create_tmp_var (ptr_type, get_name (decl));
1379 DECL_IGNORED_P (addr) = 0;
1380 t = build_fold_indirect_ref (addr);
1381 SET_DECL_VALUE_EXPR (decl, t);
1382 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1384 t = built_in_decls[BUILT_IN_ALLOCA];
1385 t = build_call_expr (t, 1, DECL_SIZE_UNIT (decl));
1386 t = fold_convert (ptr_type, t);
1387 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1389 gimplify_and_add (t, seq_p);
1391 /* Indicate that we need to restore the stack level when the
1392 enclosing BIND_EXPR is exited. */
1393 gimplify_ctxp->save_stack = true;
1397 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1398 and initialization explicit. */
1400 static enum gimplify_status
1401 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1403 tree stmt = *stmt_p;
1404 tree decl = DECL_EXPR_DECL (stmt);
1406 *stmt_p = NULL_TREE;
1408 if (TREE_TYPE (decl) == error_mark_node)
1409 return GS_ERROR;
1411 if ((TREE_CODE (decl) == TYPE_DECL
1412 || TREE_CODE (decl) == VAR_DECL)
1413 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1414 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1416 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1418 tree init = DECL_INITIAL (decl);
1420 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1421 || (!TREE_STATIC (decl)
1422 && flag_stack_check == GENERIC_STACK_CHECK
1423 && compare_tree_int (DECL_SIZE_UNIT (decl),
1424 STACK_CHECK_MAX_VAR_SIZE) > 0))
1425 gimplify_vla_decl (decl, seq_p);
1427 if (init && init != error_mark_node)
1429 if (!TREE_STATIC (decl))
1431 DECL_INITIAL (decl) = NULL_TREE;
1432 init = build2 (INIT_EXPR, void_type_node, decl, init);
1433 gimplify_and_add (init, seq_p);
1434 ggc_free (init);
1436 else
1437 /* We must still examine initializers for static variables
1438 as they may contain a label address. */
1439 walk_tree (&init, force_labels_r, NULL, NULL);
1442 /* Some front ends do not explicitly declare all anonymous
1443 artificial variables. We compensate here by declaring the
1444 variables, though it would be better if the front ends would
1445 explicitly declare them. */
1446 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1447 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1448 gimple_add_tmp_var (decl);
1451 return GS_ALL_DONE;
1454 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1455 and replacing the LOOP_EXPR with goto, but if the loop contains an
1456 EXIT_EXPR, we need to append a label for it to jump to. */
1458 static enum gimplify_status
1459 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1461 tree saved_label = gimplify_ctxp->exit_label;
1462 tree start_label = create_artificial_label ();
1464 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1466 gimplify_ctxp->exit_label = NULL_TREE;
1468 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1470 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1472 if (gimplify_ctxp->exit_label)
1473 gimplify_seq_add_stmt (pre_p, gimple_build_label (gimplify_ctxp->exit_label));
1475 gimplify_ctxp->exit_label = saved_label;
1477 *expr_p = NULL;
1478 return GS_ALL_DONE;
1481 /* Gimplifies a statement list onto a sequence. These may be created either
1482 by an enlightened front-end, or by shortcut_cond_expr. */
1484 static enum gimplify_status
1485 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1487 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1489 tree_stmt_iterator i = tsi_start (*expr_p);
1491 while (!tsi_end_p (i))
1493 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1494 tsi_delink (&i);
1497 if (temp)
1499 *expr_p = temp;
1500 return GS_OK;
1503 return GS_ALL_DONE;
1506 /* Compare two case labels. Because the front end should already have
1507 made sure that case ranges do not overlap, it is enough to only compare
1508 the CASE_LOW values of each case label. */
1510 static int
1511 compare_case_labels (const void *p1, const void *p2)
1513 const_tree const case1 = *(const_tree const*)p1;
1514 const_tree const case2 = *(const_tree const*)p2;
1516 /* The 'default' case label always goes first. */
1517 if (!CASE_LOW (case1))
1518 return -1;
1519 else if (!CASE_LOW (case2))
1520 return 1;
1521 else
1522 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1526 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1528 void
1529 sort_case_labels (VEC(tree,heap)* label_vec)
1531 size_t len = VEC_length (tree, label_vec);
1532 qsort (VEC_address (tree, label_vec), len, sizeof (tree),
1533 compare_case_labels);
1537 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1538 branch to. */
1540 static enum gimplify_status
1541 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1543 tree switch_expr = *expr_p;
1544 gimple_seq switch_body_seq = NULL;
1545 enum gimplify_status ret;
1547 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1548 fb_rvalue);
1549 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1550 return ret;
1552 if (SWITCH_BODY (switch_expr))
1554 VEC (tree,heap) *labels;
1555 VEC (tree,heap) *saved_labels;
1556 tree default_case = NULL_TREE;
1557 size_t i, len;
1558 gimple gimple_switch;
1560 /* If someone can be bothered to fill in the labels, they can
1561 be bothered to null out the body too. */
1562 gcc_assert (!SWITCH_LABELS (switch_expr));
1564 /* save old labels, get new ones from body, then restore the old
1565 labels. Save all the things from the switch body to append after. */
1566 saved_labels = gimplify_ctxp->case_labels;
1567 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1569 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1570 labels = gimplify_ctxp->case_labels;
1571 gimplify_ctxp->case_labels = saved_labels;
1573 i = 0;
1574 while (i < VEC_length (tree, labels))
1576 tree elt = VEC_index (tree, labels, i);
1577 tree low = CASE_LOW (elt);
1578 bool remove_element = FALSE;
1580 if (low)
1582 /* Discard empty ranges. */
1583 tree high = CASE_HIGH (elt);
1584 if (high && tree_int_cst_lt (high, low))
1585 remove_element = TRUE;
1587 else
1589 /* The default case must be the last label in the list. */
1590 gcc_assert (!default_case);
1591 default_case = elt;
1592 remove_element = TRUE;
1595 if (remove_element)
1596 VEC_ordered_remove (tree, labels, i);
1597 else
1598 i++;
1600 len = i;
1602 if (!default_case)
1604 gimple new_default;
1606 /* If the switch has no default label, add one, so that we jump
1607 around the switch body. */
1608 default_case = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1609 NULL_TREE, create_artificial_label ());
1610 new_default = gimple_build_label (CASE_LABEL (default_case));
1611 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1614 if (!VEC_empty (tree, labels))
1615 sort_case_labels (labels);
1617 gimple_switch = gimple_build_switch_vec (SWITCH_COND (switch_expr),
1618 default_case, labels);
1619 gimplify_seq_add_stmt (pre_p, gimple_switch);
1620 gimplify_seq_add_seq (pre_p, switch_body_seq);
1621 VEC_free(tree, heap, labels);
1623 else
1624 gcc_assert (SWITCH_LABELS (switch_expr));
1626 return GS_ALL_DONE;
1630 static enum gimplify_status
1631 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1633 struct gimplify_ctx *ctxp;
1634 gimple gimple_label;
1636 /* Invalid OpenMP programs can play Duff's Device type games with
1637 #pragma omp parallel. At least in the C front end, we don't
1638 detect such invalid branches until after gimplification. */
1639 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1640 if (ctxp->case_labels)
1641 break;
1643 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1644 VEC_safe_push (tree, heap, ctxp->case_labels, *expr_p);
1645 gimplify_seq_add_stmt (pre_p, gimple_label);
1647 return GS_ALL_DONE;
1650 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1651 if necessary. */
1653 tree
1654 build_and_jump (tree *label_p)
1656 if (label_p == NULL)
1657 /* If there's nowhere to jump, just fall through. */
1658 return NULL_TREE;
1660 if (*label_p == NULL_TREE)
1662 tree label = create_artificial_label ();
1663 *label_p = label;
1666 return build1 (GOTO_EXPR, void_type_node, *label_p);
1669 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1670 This also involves building a label to jump to and communicating it to
1671 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1673 static enum gimplify_status
1674 gimplify_exit_expr (tree *expr_p)
1676 tree cond = TREE_OPERAND (*expr_p, 0);
1677 tree expr;
1679 expr = build_and_jump (&gimplify_ctxp->exit_label);
1680 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1681 *expr_p = expr;
1683 return GS_OK;
1686 /* A helper function to be called via walk_tree. Mark all labels under *TP
1687 as being forced. To be called for DECL_INITIAL of static variables. */
1689 tree
1690 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1692 if (TYPE_P (*tp))
1693 *walk_subtrees = 0;
1694 if (TREE_CODE (*tp) == LABEL_DECL)
1695 FORCED_LABEL (*tp) = 1;
1697 return NULL_TREE;
1700 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1701 different from its canonical type, wrap the whole thing inside a
1702 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1703 type.
1705 The canonical type of a COMPONENT_REF is the type of the field being
1706 referenced--unless the field is a bit-field which can be read directly
1707 in a smaller mode, in which case the canonical type is the
1708 sign-appropriate type corresponding to that mode. */
1710 static void
1711 canonicalize_component_ref (tree *expr_p)
1713 tree expr = *expr_p;
1714 tree type;
1716 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1718 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1719 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1720 else
1721 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1723 /* One could argue that all the stuff below is not necessary for
1724 the non-bitfield case and declare it a FE error if type
1725 adjustment would be needed. */
1726 if (TREE_TYPE (expr) != type)
1728 #ifdef ENABLE_TYPES_CHECKING
1729 tree old_type = TREE_TYPE (expr);
1730 #endif
1731 int type_quals;
1733 /* We need to preserve qualifiers and propagate them from
1734 operand 0. */
1735 type_quals = TYPE_QUALS (type)
1736 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1737 if (TYPE_QUALS (type) != type_quals)
1738 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1740 /* Set the type of the COMPONENT_REF to the underlying type. */
1741 TREE_TYPE (expr) = type;
1743 #ifdef ENABLE_TYPES_CHECKING
1744 /* It is now a FE error, if the conversion from the canonical
1745 type to the original expression type is not useless. */
1746 gcc_assert (useless_type_conversion_p (old_type, type));
1747 #endif
1751 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1752 to foo, embed that change in the ADDR_EXPR by converting
1753 T array[U];
1754 (T *)&array
1756 &array[L]
1757 where L is the lower bound. For simplicity, only do this for constant
1758 lower bound.
1759 The constraint is that the type of &array[L] is trivially convertible
1760 to T *. */
1762 static void
1763 canonicalize_addr_expr (tree *expr_p)
1765 tree expr = *expr_p;
1766 tree addr_expr = TREE_OPERAND (expr, 0);
1767 tree datype, ddatype, pddatype;
1769 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1770 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1771 || TREE_CODE (addr_expr) != ADDR_EXPR)
1772 return;
1774 /* The addr_expr type should be a pointer to an array. */
1775 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1776 if (TREE_CODE (datype) != ARRAY_TYPE)
1777 return;
1779 /* The pointer to element type shall be trivially convertible to
1780 the expression pointer type. */
1781 ddatype = TREE_TYPE (datype);
1782 pddatype = build_pointer_type (ddatype);
1783 if (!useless_type_conversion_p (pddatype, ddatype))
1784 return;
1786 /* The lower bound and element sizes must be constant. */
1787 if (!TYPE_SIZE_UNIT (ddatype)
1788 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1789 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1790 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1791 return;
1793 /* All checks succeeded. Build a new node to merge the cast. */
1794 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1795 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1796 NULL_TREE, NULL_TREE);
1797 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1800 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1801 underneath as appropriate. */
1803 static enum gimplify_status
1804 gimplify_conversion (tree *expr_p)
1806 tree tem;
1807 gcc_assert (CONVERT_EXPR_P (*expr_p));
1809 /* Then strip away all but the outermost conversion. */
1810 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1812 /* And remove the outermost conversion if it's useless. */
1813 if (tree_ssa_useless_type_conversion (*expr_p))
1814 *expr_p = TREE_OPERAND (*expr_p, 0);
1816 /* Attempt to avoid NOP_EXPR by producing reference to a subtype.
1817 For example this fold (subclass *)&A into &A->subclass avoiding
1818 a need for statement. */
1819 if (CONVERT_EXPR_P (*expr_p)
1820 && POINTER_TYPE_P (TREE_TYPE (*expr_p))
1821 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 0)))
1822 && (tem = maybe_fold_offset_to_address
1823 (TREE_OPERAND (*expr_p, 0),
1824 integer_zero_node, TREE_TYPE (*expr_p))) != NULL_TREE)
1825 *expr_p = tem;
1827 /* If we still have a conversion at the toplevel,
1828 then canonicalize some constructs. */
1829 if (CONVERT_EXPR_P (*expr_p))
1831 tree sub = TREE_OPERAND (*expr_p, 0);
1833 /* If a NOP conversion is changing the type of a COMPONENT_REF
1834 expression, then canonicalize its type now in order to expose more
1835 redundant conversions. */
1836 if (TREE_CODE (sub) == COMPONENT_REF)
1837 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1839 /* If a NOP conversion is changing a pointer to array of foo
1840 to a pointer to foo, embed that change in the ADDR_EXPR. */
1841 else if (TREE_CODE (sub) == ADDR_EXPR)
1842 canonicalize_addr_expr (expr_p);
1845 /* If we have a conversion to a non-register type force the
1846 use of a VIEW_CONVERT_EXPR instead. */
1847 if (!is_gimple_reg_type (TREE_TYPE (*expr_p)))
1848 *expr_p = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1849 TREE_OPERAND (*expr_p, 0));
1851 return GS_OK;
1854 /* Gimplify a VAR_DECL or PARM_DECL. Returns GS_OK if we expanded a
1855 DECL_VALUE_EXPR, and it's worth re-examining things. */
1857 static enum gimplify_status
1858 gimplify_var_or_parm_decl (tree *expr_p)
1860 tree decl = *expr_p;
1862 /* ??? If this is a local variable, and it has not been seen in any
1863 outer BIND_EXPR, then it's probably the result of a duplicate
1864 declaration, for which we've already issued an error. It would
1865 be really nice if the front end wouldn't leak these at all.
1866 Currently the only known culprit is C++ destructors, as seen
1867 in g++.old-deja/g++.jason/binding.C. */
1868 if (TREE_CODE (decl) == VAR_DECL
1869 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1870 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1871 && decl_function_context (decl) == current_function_decl)
1873 gcc_assert (errorcount || sorrycount);
1874 return GS_ERROR;
1877 /* When within an OpenMP context, notice uses of variables. */
1878 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1879 return GS_ALL_DONE;
1881 /* If the decl is an alias for another expression, substitute it now. */
1882 if (DECL_HAS_VALUE_EXPR_P (decl))
1884 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
1885 return GS_OK;
1888 return GS_ALL_DONE;
1892 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1893 node *EXPR_P.
1895 compound_lval
1896 : min_lval '[' val ']'
1897 | min_lval '.' ID
1898 | compound_lval '[' val ']'
1899 | compound_lval '.' ID
1901 This is not part of the original SIMPLE definition, which separates
1902 array and member references, but it seems reasonable to handle them
1903 together. Also, this way we don't run into problems with union
1904 aliasing; gcc requires that for accesses through a union to alias, the
1905 union reference must be explicit, which was not always the case when we
1906 were splitting up array and member refs.
1908 PRE_P points to the sequence where side effects that must happen before
1909 *EXPR_P should be stored.
1911 POST_P points to the sequence where side effects that must happen after
1912 *EXPR_P should be stored. */
1914 static enum gimplify_status
1915 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1916 fallback_t fallback)
1918 tree *p;
1919 VEC(tree,heap) *stack;
1920 enum gimplify_status ret = GS_OK, tret;
1921 int i;
1923 /* Create a stack of the subexpressions so later we can walk them in
1924 order from inner to outer. */
1925 stack = VEC_alloc (tree, heap, 10);
1927 /* We can handle anything that get_inner_reference can deal with. */
1928 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1930 restart:
1931 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1932 if (TREE_CODE (*p) == INDIRECT_REF)
1933 *p = fold_indirect_ref (*p);
1935 if (handled_component_p (*p))
1937 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1938 additional COMPONENT_REFs. */
1939 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1940 && gimplify_var_or_parm_decl (p) == GS_OK)
1941 goto restart;
1942 else
1943 break;
1945 VEC_safe_push (tree, heap, stack, *p);
1948 gcc_assert (VEC_length (tree, stack));
1950 /* Now STACK is a stack of pointers to all the refs we've walked through
1951 and P points to the innermost expression.
1953 Java requires that we elaborated nodes in source order. That
1954 means we must gimplify the inner expression followed by each of
1955 the indices, in order. But we can't gimplify the inner
1956 expression until we deal with any variable bounds, sizes, or
1957 positions in order to deal with PLACEHOLDER_EXPRs.
1959 So we do this in three steps. First we deal with the annotations
1960 for any variables in the components, then we gimplify the base,
1961 then we gimplify any indices, from left to right. */
1962 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
1964 tree t = VEC_index (tree, stack, i);
1966 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1968 /* Gimplify the low bound and element type size and put them into
1969 the ARRAY_REF. If these values are set, they have already been
1970 gimplified. */
1971 if (TREE_OPERAND (t, 2) == NULL_TREE)
1973 tree low = unshare_expr (array_ref_low_bound (t));
1974 if (!is_gimple_min_invariant (low))
1976 TREE_OPERAND (t, 2) = low;
1977 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
1978 post_p, is_gimple_reg,
1979 fb_rvalue);
1980 ret = MIN (ret, tret);
1984 if (!TREE_OPERAND (t, 3))
1986 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1987 tree elmt_size = unshare_expr (array_ref_element_size (t));
1988 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1990 /* Divide the element size by the alignment of the element
1991 type (above). */
1992 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1994 if (!is_gimple_min_invariant (elmt_size))
1996 TREE_OPERAND (t, 3) = elmt_size;
1997 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
1998 post_p, is_gimple_reg,
1999 fb_rvalue);
2000 ret = MIN (ret, tret);
2004 else if (TREE_CODE (t) == COMPONENT_REF)
2006 /* Set the field offset into T and gimplify it. */
2007 if (!TREE_OPERAND (t, 2))
2009 tree offset = unshare_expr (component_ref_field_offset (t));
2010 tree field = TREE_OPERAND (t, 1);
2011 tree factor
2012 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2014 /* Divide the offset by its alignment. */
2015 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
2017 if (!is_gimple_min_invariant (offset))
2019 TREE_OPERAND (t, 2) = offset;
2020 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2021 post_p, is_gimple_reg,
2022 fb_rvalue);
2023 ret = MIN (ret, tret);
2029 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2030 so as to match the min_lval predicate. Failure to do so may result
2031 in the creation of large aggregate temporaries. */
2032 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2033 fallback | fb_lvalue);
2034 ret = MIN (ret, tret);
2036 /* And finally, the indices and operands to BIT_FIELD_REF. During this
2037 loop we also remove any useless conversions. */
2038 for (; VEC_length (tree, stack) > 0; )
2040 tree t = VEC_pop (tree, stack);
2042 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2044 /* Gimplify the dimension. */
2045 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2047 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2048 is_gimple_val, fb_rvalue);
2049 ret = MIN (ret, tret);
2052 else if (TREE_CODE (t) == BIT_FIELD_REF)
2054 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2055 is_gimple_val, fb_rvalue);
2056 ret = MIN (ret, tret);
2057 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2058 is_gimple_val, fb_rvalue);
2059 ret = MIN (ret, tret);
2062 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2064 /* The innermost expression P may have originally had
2065 TREE_SIDE_EFFECTS set which would have caused all the outer
2066 expressions in *EXPR_P leading to P to also have had
2067 TREE_SIDE_EFFECTS set. */
2068 recalculate_side_effects (t);
2071 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2072 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2074 canonicalize_component_ref (expr_p);
2075 ret = MIN (ret, GS_OK);
2078 VEC_free (tree, heap, stack);
2080 return ret;
2083 /* Gimplify the self modifying expression pointed to by EXPR_P
2084 (++, --, +=, -=).
2086 PRE_P points to the list where side effects that must happen before
2087 *EXPR_P should be stored.
2089 POST_P points to the list where side effects that must happen after
2090 *EXPR_P should be stored.
2092 WANT_VALUE is nonzero iff we want to use the value of this expression
2093 in another expression. */
2095 static enum gimplify_status
2096 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2097 bool want_value)
2099 enum tree_code code;
2100 tree lhs, lvalue, rhs, t1;
2101 gimple_seq post = NULL, *orig_post_p = post_p;
2102 bool postfix;
2103 enum tree_code arith_code;
2104 enum gimplify_status ret;
2106 code = TREE_CODE (*expr_p);
2108 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2109 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2111 /* Prefix or postfix? */
2112 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2113 /* Faster to treat as prefix if result is not used. */
2114 postfix = want_value;
2115 else
2116 postfix = false;
2118 /* For postfix, make sure the inner expression's post side effects
2119 are executed after side effects from this expression. */
2120 if (postfix)
2121 post_p = &post;
2123 /* Add or subtract? */
2124 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2125 arith_code = PLUS_EXPR;
2126 else
2127 arith_code = MINUS_EXPR;
2129 /* Gimplify the LHS into a GIMPLE lvalue. */
2130 lvalue = TREE_OPERAND (*expr_p, 0);
2131 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2132 if (ret == GS_ERROR)
2133 return ret;
2135 /* Extract the operands to the arithmetic operation. */
2136 lhs = lvalue;
2137 rhs = TREE_OPERAND (*expr_p, 1);
2139 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2140 that as the result value and in the postqueue operation. We also
2141 make sure to make lvalue a minimal lval, see
2142 gcc.c-torture/execute/20040313-1.c for an example where this matters. */
2143 if (postfix)
2145 if (!is_gimple_min_lval (lvalue))
2147 mark_addressable (lvalue);
2148 lvalue = build_fold_addr_expr (lvalue);
2149 gimplify_expr (&lvalue, pre_p, post_p, is_gimple_val, fb_rvalue);
2150 lvalue = build_fold_indirect_ref (lvalue);
2152 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2153 if (ret == GS_ERROR)
2154 return ret;
2157 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2158 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2160 rhs = fold_convert (sizetype, rhs);
2161 if (arith_code == MINUS_EXPR)
2162 rhs = fold_build1 (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2163 arith_code = POINTER_PLUS_EXPR;
2166 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
2168 if (postfix)
2170 gimplify_assign (lvalue, t1, orig_post_p);
2171 gimplify_seq_add_seq (orig_post_p, post);
2172 *expr_p = lhs;
2173 return GS_ALL_DONE;
2175 else
2177 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2178 return GS_OK;
2183 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2185 static void
2186 maybe_with_size_expr (tree *expr_p)
2188 tree expr = *expr_p;
2189 tree type = TREE_TYPE (expr);
2190 tree size;
2192 /* If we've already wrapped this or the type is error_mark_node, we can't do
2193 anything. */
2194 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2195 || type == error_mark_node)
2196 return;
2198 /* If the size isn't known or is a constant, we have nothing to do. */
2199 size = TYPE_SIZE_UNIT (type);
2200 if (!size || TREE_CODE (size) == INTEGER_CST)
2201 return;
2203 /* Otherwise, make a WITH_SIZE_EXPR. */
2204 size = unshare_expr (size);
2205 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2206 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2210 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2211 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2212 the CALL_EXPR. */
2214 static enum gimplify_status
2215 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2217 bool (*test) (tree);
2218 fallback_t fb;
2220 /* In general, we allow lvalues for function arguments to avoid
2221 extra overhead of copying large aggregates out of even larger
2222 aggregates into temporaries only to copy the temporaries to
2223 the argument list. Make optimizers happy by pulling out to
2224 temporaries those types that fit in registers. */
2225 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2226 test = is_gimple_val, fb = fb_rvalue;
2227 else
2228 test = is_gimple_lvalue, fb = fb_either;
2230 /* If this is a variable sized type, we must remember the size. */
2231 maybe_with_size_expr (arg_p);
2233 /* Make sure arguments have the same location as the function call
2234 itself. */
2235 protected_set_expr_location (*arg_p, call_location);
2237 /* There is a sequence point before a function call. Side effects in
2238 the argument list must occur before the actual call. So, when
2239 gimplifying arguments, force gimplify_expr to use an internal
2240 post queue which is then appended to the end of PRE_P. */
2241 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2245 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2246 WANT_VALUE is true if the result of the call is desired. */
2248 static enum gimplify_status
2249 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2251 tree fndecl, parms, p;
2252 enum gimplify_status ret;
2253 int i, nargs;
2254 gimple call;
2255 bool builtin_va_start_p = FALSE;
2257 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2259 /* For reliable diagnostics during inlining, it is necessary that
2260 every call_expr be annotated with file and line. */
2261 if (! EXPR_HAS_LOCATION (*expr_p))
2262 SET_EXPR_LOCATION (*expr_p, input_location);
2264 /* This may be a call to a builtin function.
2266 Builtin function calls may be transformed into different
2267 (and more efficient) builtin function calls under certain
2268 circumstances. Unfortunately, gimplification can muck things
2269 up enough that the builtin expanders are not aware that certain
2270 transformations are still valid.
2272 So we attempt transformation/gimplification of the call before
2273 we gimplify the CALL_EXPR. At this time we do not manage to
2274 transform all calls in the same manner as the expanders do, but
2275 we do transform most of them. */
2276 fndecl = get_callee_fndecl (*expr_p);
2277 if (fndecl && DECL_BUILT_IN (fndecl))
2279 tree new_tree = fold_call_expr (*expr_p, !want_value);
2281 if (new_tree && new_tree != *expr_p)
2283 /* There was a transformation of this call which computes the
2284 same value, but in a more efficient way. Return and try
2285 again. */
2286 *expr_p = new_tree;
2287 return GS_OK;
2290 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2291 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_VA_START)
2293 builtin_va_start_p = TRUE;
2294 if (call_expr_nargs (*expr_p) < 2)
2296 error ("too few arguments to function %<va_start%>");
2297 *expr_p = build_empty_stmt ();
2298 return GS_OK;
2301 if (fold_builtin_next_arg (*expr_p, true))
2303 *expr_p = build_empty_stmt ();
2304 return GS_OK;
2309 /* There is a sequence point before the call, so any side effects in
2310 the calling expression must occur before the actual call. Force
2311 gimplify_expr to use an internal post queue. */
2312 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2313 is_gimple_call_addr, fb_rvalue);
2315 nargs = call_expr_nargs (*expr_p);
2317 /* Get argument types for verification. */
2318 fndecl = get_callee_fndecl (*expr_p);
2319 parms = NULL_TREE;
2320 if (fndecl)
2321 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2322 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2323 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2325 if (fndecl && DECL_ARGUMENTS (fndecl))
2326 p = DECL_ARGUMENTS (fndecl);
2327 else if (parms)
2328 p = parms;
2329 else
2330 p = NULL_TREE;
2331 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2334 /* If the last argument is __builtin_va_arg_pack () and it is not
2335 passed as a named argument, decrease the number of CALL_EXPR
2336 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2337 if (!p
2338 && i < nargs
2339 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2341 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2342 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2344 if (last_arg_fndecl
2345 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2346 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2347 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2349 tree call = *expr_p;
2351 --nargs;
2352 *expr_p = build_call_array (TREE_TYPE (call), CALL_EXPR_FN (call),
2353 nargs, CALL_EXPR_ARGP (call));
2355 /* Copy all CALL_EXPR flags, location and block, except
2356 CALL_EXPR_VA_ARG_PACK flag. */
2357 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2358 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2359 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2360 = CALL_EXPR_RETURN_SLOT_OPT (call);
2361 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2362 CALL_CANNOT_INLINE_P (*expr_p) = CALL_CANNOT_INLINE_P (call);
2363 SET_EXPR_LOCUS (*expr_p, EXPR_LOCUS (call));
2364 TREE_BLOCK (*expr_p) = TREE_BLOCK (call);
2366 /* Set CALL_EXPR_VA_ARG_PACK. */
2367 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2371 /* Finally, gimplify the function arguments. */
2372 if (nargs > 0)
2374 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2375 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2376 PUSH_ARGS_REVERSED ? i-- : i++)
2378 enum gimplify_status t;
2380 /* Avoid gimplifying the second argument to va_start, which needs to
2381 be the plain PARM_DECL. */
2382 if ((i != 1) || !builtin_va_start_p)
2384 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2385 EXPR_LOCATION (*expr_p));
2387 if (t == GS_ERROR)
2388 ret = GS_ERROR;
2393 /* Try this again in case gimplification exposed something. */
2394 if (ret != GS_ERROR)
2396 tree new_tree = fold_call_expr (*expr_p, !want_value);
2398 if (new_tree && new_tree != *expr_p)
2400 /* There was a transformation of this call which computes the
2401 same value, but in a more efficient way. Return and try
2402 again. */
2403 *expr_p = new_tree;
2404 return GS_OK;
2407 else
2409 *expr_p = error_mark_node;
2410 return GS_ERROR;
2413 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2414 decl. This allows us to eliminate redundant or useless
2415 calls to "const" functions. */
2416 if (TREE_CODE (*expr_p) == CALL_EXPR)
2418 int flags = call_expr_flags (*expr_p);
2419 if (flags & (ECF_CONST | ECF_PURE)
2420 /* An infinite loop is considered a side effect. */
2421 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2422 TREE_SIDE_EFFECTS (*expr_p) = 0;
2425 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2426 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2427 form and delegate the creation of a GIMPLE_CALL to
2428 gimplify_modify_expr. This is always possible because when
2429 WANT_VALUE is true, the caller wants the result of this call into
2430 a temporary, which means that we will emit an INIT_EXPR in
2431 internal_get_tmp_var which will then be handled by
2432 gimplify_modify_expr. */
2433 if (!want_value)
2435 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2436 have to do is replicate it as a GIMPLE_CALL tuple. */
2437 call = gimple_build_call_from_tree (*expr_p);
2438 gimplify_seq_add_stmt (pre_p, call);
2439 *expr_p = NULL_TREE;
2442 return ret;
2445 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2446 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2448 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2449 condition is true or false, respectively. If null, we should generate
2450 our own to skip over the evaluation of this specific expression.
2452 This function is the tree equivalent of do_jump.
2454 shortcut_cond_r should only be called by shortcut_cond_expr. */
2456 static tree
2457 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2459 tree local_label = NULL_TREE;
2460 tree t, expr = NULL;
2462 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2463 retain the shortcut semantics. Just insert the gotos here;
2464 shortcut_cond_expr will append the real blocks later. */
2465 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2467 /* Turn if (a && b) into
2469 if (a); else goto no;
2470 if (b) goto yes; else goto no;
2471 (no:) */
2473 if (false_label_p == NULL)
2474 false_label_p = &local_label;
2476 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2477 append_to_statement_list (t, &expr);
2479 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2480 false_label_p);
2481 append_to_statement_list (t, &expr);
2483 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2485 /* Turn if (a || b) into
2487 if (a) goto yes;
2488 if (b) goto yes; else goto no;
2489 (yes:) */
2491 if (true_label_p == NULL)
2492 true_label_p = &local_label;
2494 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2495 append_to_statement_list (t, &expr);
2497 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2498 false_label_p);
2499 append_to_statement_list (t, &expr);
2501 else if (TREE_CODE (pred) == COND_EXPR)
2503 /* As long as we're messing with gotos, turn if (a ? b : c) into
2504 if (a)
2505 if (b) goto yes; else goto no;
2506 else
2507 if (c) goto yes; else goto no; */
2508 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2509 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2510 false_label_p),
2511 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2512 false_label_p));
2514 else
2516 expr = build3 (COND_EXPR, void_type_node, pred,
2517 build_and_jump (true_label_p),
2518 build_and_jump (false_label_p));
2521 if (local_label)
2523 t = build1 (LABEL_EXPR, void_type_node, local_label);
2524 append_to_statement_list (t, &expr);
2527 return expr;
2530 /* Given a conditional expression EXPR with short-circuit boolean
2531 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2532 predicate appart into the equivalent sequence of conditionals. */
2534 static tree
2535 shortcut_cond_expr (tree expr)
2537 tree pred = TREE_OPERAND (expr, 0);
2538 tree then_ = TREE_OPERAND (expr, 1);
2539 tree else_ = TREE_OPERAND (expr, 2);
2540 tree true_label, false_label, end_label, t;
2541 tree *true_label_p;
2542 tree *false_label_p;
2543 bool emit_end, emit_false, jump_over_else;
2544 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2545 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2547 /* First do simple transformations. */
2548 if (!else_se)
2550 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2551 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2553 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2554 then_ = shortcut_cond_expr (expr);
2555 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2556 pred = TREE_OPERAND (pred, 0);
2557 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2561 if (!then_se)
2563 /* If there is no 'then', turn
2564 if (a || b); else d
2565 into
2566 if (a); else if (b); else d. */
2567 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2569 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2570 else_ = shortcut_cond_expr (expr);
2571 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2572 pred = TREE_OPERAND (pred, 0);
2573 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2577 /* If we're done, great. */
2578 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2579 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2580 return expr;
2582 /* Otherwise we need to mess with gotos. Change
2583 if (a) c; else d;
2585 if (a); else goto no;
2586 c; goto end;
2587 no: d; end:
2588 and recursively gimplify the condition. */
2590 true_label = false_label = end_label = NULL_TREE;
2592 /* If our arms just jump somewhere, hijack those labels so we don't
2593 generate jumps to jumps. */
2595 if (then_
2596 && TREE_CODE (then_) == GOTO_EXPR
2597 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2599 true_label = GOTO_DESTINATION (then_);
2600 then_ = NULL;
2601 then_se = false;
2604 if (else_
2605 && TREE_CODE (else_) == GOTO_EXPR
2606 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2608 false_label = GOTO_DESTINATION (else_);
2609 else_ = NULL;
2610 else_se = false;
2613 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2614 if (true_label)
2615 true_label_p = &true_label;
2616 else
2617 true_label_p = NULL;
2619 /* The 'else' branch also needs a label if it contains interesting code. */
2620 if (false_label || else_se)
2621 false_label_p = &false_label;
2622 else
2623 false_label_p = NULL;
2625 /* If there was nothing else in our arms, just forward the label(s). */
2626 if (!then_se && !else_se)
2627 return shortcut_cond_r (pred, true_label_p, false_label_p);
2629 /* If our last subexpression already has a terminal label, reuse it. */
2630 if (else_se)
2631 expr = expr_last (else_);
2632 else if (then_se)
2633 expr = expr_last (then_);
2634 else
2635 expr = NULL;
2636 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2637 end_label = LABEL_EXPR_LABEL (expr);
2639 /* If we don't care about jumping to the 'else' branch, jump to the end
2640 if the condition is false. */
2641 if (!false_label_p)
2642 false_label_p = &end_label;
2644 /* We only want to emit these labels if we aren't hijacking them. */
2645 emit_end = (end_label == NULL_TREE);
2646 emit_false = (false_label == NULL_TREE);
2648 /* We only emit the jump over the else clause if we have to--if the
2649 then clause may fall through. Otherwise we can wind up with a
2650 useless jump and a useless label at the end of gimplified code,
2651 which will cause us to think that this conditional as a whole
2652 falls through even if it doesn't. If we then inline a function
2653 which ends with such a condition, that can cause us to issue an
2654 inappropriate warning about control reaching the end of a
2655 non-void function. */
2656 jump_over_else = block_may_fallthru (then_);
2658 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2660 expr = NULL;
2661 append_to_statement_list (pred, &expr);
2663 append_to_statement_list (then_, &expr);
2664 if (else_se)
2666 if (jump_over_else)
2668 t = build_and_jump (&end_label);
2669 append_to_statement_list (t, &expr);
2671 if (emit_false)
2673 t = build1 (LABEL_EXPR, void_type_node, false_label);
2674 append_to_statement_list (t, &expr);
2676 append_to_statement_list (else_, &expr);
2678 if (emit_end && end_label)
2680 t = build1 (LABEL_EXPR, void_type_node, end_label);
2681 append_to_statement_list (t, &expr);
2684 return expr;
2687 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2689 tree
2690 gimple_boolify (tree expr)
2692 tree type = TREE_TYPE (expr);
2694 if (TREE_CODE (type) == BOOLEAN_TYPE)
2695 return expr;
2697 switch (TREE_CODE (expr))
2699 case TRUTH_AND_EXPR:
2700 case TRUTH_OR_EXPR:
2701 case TRUTH_XOR_EXPR:
2702 case TRUTH_ANDIF_EXPR:
2703 case TRUTH_ORIF_EXPR:
2704 /* Also boolify the arguments of truth exprs. */
2705 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2706 /* FALLTHRU */
2708 case TRUTH_NOT_EXPR:
2709 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2710 /* FALLTHRU */
2712 case EQ_EXPR: case NE_EXPR:
2713 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2714 /* These expressions always produce boolean results. */
2715 TREE_TYPE (expr) = boolean_type_node;
2716 return expr;
2718 default:
2719 /* Other expressions that get here must have boolean values, but
2720 might need to be converted to the appropriate mode. */
2721 return fold_convert (boolean_type_node, expr);
2725 /* Given a conditional expression *EXPR_P without side effects, gimplify
2726 its operands. New statements are inserted to PRE_P. */
2728 static enum gimplify_status
2729 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2731 tree expr = *expr_p, cond;
2732 enum gimplify_status ret, tret;
2733 enum tree_code code;
2735 cond = gimple_boolify (COND_EXPR_COND (expr));
2737 /* We need to handle && and || specially, as their gimplification
2738 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2739 code = TREE_CODE (cond);
2740 if (code == TRUTH_ANDIF_EXPR)
2741 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2742 else if (code == TRUTH_ORIF_EXPR)
2743 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2744 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2745 COND_EXPR_COND (*expr_p) = cond;
2747 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2748 is_gimple_val, fb_rvalue);
2749 ret = MIN (ret, tret);
2750 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2751 is_gimple_val, fb_rvalue);
2753 return MIN (ret, tret);
2756 /* Returns true if evaluating EXPR could trap.
2757 EXPR is GENERIC, while tree_could_trap_p can be called
2758 only on GIMPLE. */
2760 static bool
2761 generic_expr_could_trap_p (tree expr)
2763 unsigned i, n;
2765 if (!expr || is_gimple_val (expr))
2766 return false;
2768 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2769 return true;
2771 n = TREE_OPERAND_LENGTH (expr);
2772 for (i = 0; i < n; i++)
2773 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2774 return true;
2776 return false;
2779 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2780 into
2782 if (p) if (p)
2783 t1 = a; a;
2784 else or else
2785 t1 = b; b;
2788 The second form is used when *EXPR_P is of type void.
2790 PRE_P points to the list where side effects that must happen before
2791 *EXPR_P should be stored. */
2793 static enum gimplify_status
2794 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
2796 tree expr = *expr_p;
2797 tree tmp, type, arm1, arm2;
2798 enum gimplify_status ret;
2799 tree label_true, label_false, label_cont;
2800 bool have_then_clause_p, have_else_clause_p;
2801 gimple gimple_cond;
2802 enum tree_code pred_code;
2803 gimple_seq seq = NULL;
2805 type = TREE_TYPE (expr);
2807 /* If this COND_EXPR has a value, copy the values into a temporary within
2808 the arms. */
2809 if (! VOID_TYPE_P (type))
2811 tree result;
2813 /* If an rvalue is ok or we do not require an lvalue, avoid creating
2814 an addressable temporary. */
2815 if (((fallback & fb_rvalue)
2816 || !(fallback & fb_lvalue))
2817 && !TREE_ADDRESSABLE (type))
2819 if (gimplify_ctxp->allow_rhs_cond_expr
2820 /* If either branch has side effects or could trap, it can't be
2821 evaluated unconditionally. */
2822 && !TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 1))
2823 && !generic_expr_could_trap_p (TREE_OPERAND (*expr_p, 1))
2824 && !TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p, 2))
2825 && !generic_expr_could_trap_p (TREE_OPERAND (*expr_p, 2)))
2826 return gimplify_pure_cond_expr (expr_p, pre_p);
2828 result = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2829 ret = GS_ALL_DONE;
2831 else
2833 tree type = build_pointer_type (TREE_TYPE (expr));
2835 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2836 TREE_OPERAND (expr, 1) =
2837 build_fold_addr_expr (TREE_OPERAND (expr, 1));
2839 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2840 TREE_OPERAND (expr, 2) =
2841 build_fold_addr_expr (TREE_OPERAND (expr, 2));
2843 tmp = create_tmp_var (type, "iftmp");
2845 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (expr, 0),
2846 TREE_OPERAND (expr, 1), TREE_OPERAND (expr, 2));
2848 result = build_fold_indirect_ref (tmp);
2851 /* Build the then clause, 't1 = a;'. But don't build an assignment
2852 if this branch is void; in C++ it can be, if it's a throw. */
2853 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2854 TREE_OPERAND (expr, 1)
2855 = build2 (MODIFY_EXPR, TREE_TYPE (tmp), tmp, TREE_OPERAND (expr, 1));
2857 /* Build the else clause, 't1 = b;'. */
2858 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2859 TREE_OPERAND (expr, 2)
2860 = build2 (MODIFY_EXPR, TREE_TYPE (tmp), tmp, TREE_OPERAND (expr, 2));
2862 TREE_TYPE (expr) = void_type_node;
2863 recalculate_side_effects (expr);
2865 /* Move the COND_EXPR to the prequeue. */
2866 gimplify_stmt (&expr, pre_p);
2868 *expr_p = result;
2869 return GS_ALL_DONE;
2872 /* Make sure the condition has BOOLEAN_TYPE. */
2873 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2875 /* Break apart && and || conditions. */
2876 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2877 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2879 expr = shortcut_cond_expr (expr);
2881 if (expr != *expr_p)
2883 *expr_p = expr;
2885 /* We can't rely on gimplify_expr to re-gimplify the expanded
2886 form properly, as cleanups might cause the target labels to be
2887 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2888 set up a conditional context. */
2889 gimple_push_condition ();
2890 gimplify_stmt (expr_p, &seq);
2891 gimple_pop_condition (pre_p);
2892 gimple_seq_add_seq (pre_p, seq);
2894 return GS_ALL_DONE;
2898 /* Now do the normal gimplification. */
2900 /* Gimplify condition. */
2901 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
2902 fb_rvalue);
2903 if (ret == GS_ERROR)
2904 return GS_ERROR;
2905 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
2907 gimple_push_condition ();
2909 have_then_clause_p = have_else_clause_p = false;
2910 if (TREE_OPERAND (expr, 1) != NULL
2911 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
2912 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
2913 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
2914 == current_function_decl)
2915 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
2916 have different locations, otherwise we end up with incorrect
2917 location information on the branches. */
2918 && (optimize
2919 || !EXPR_HAS_LOCATION (expr)
2920 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
2921 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
2923 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
2924 have_then_clause_p = true;
2926 else
2927 label_true = create_artificial_label ();
2928 if (TREE_OPERAND (expr, 2) != NULL
2929 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
2930 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
2931 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
2932 == current_function_decl)
2933 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
2934 have different locations, otherwise we end up with incorrect
2935 location information on the branches. */
2936 && (optimize
2937 || !EXPR_HAS_LOCATION (expr)
2938 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
2939 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
2941 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
2942 have_else_clause_p = true;
2944 else
2945 label_false = create_artificial_label ();
2947 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
2948 &arm2);
2950 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
2951 label_false);
2953 gimplify_seq_add_stmt (&seq, gimple_cond);
2954 label_cont = NULL_TREE;
2955 if (!have_then_clause_p)
2957 /* For if (...) {} else { code; } put label_true after
2958 the else block. */
2959 if (TREE_OPERAND (expr, 1) == NULL_TREE
2960 && !have_else_clause_p
2961 && TREE_OPERAND (expr, 2) != NULL_TREE)
2962 label_cont = label_true;
2963 else
2965 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
2966 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
2967 /* For if (...) { code; } else {} or
2968 if (...) { code; } else goto label; or
2969 if (...) { code; return; } else { ... }
2970 label_cont isn't needed. */
2971 if (!have_else_clause_p
2972 && TREE_OPERAND (expr, 2) != NULL_TREE
2973 && gimple_seq_may_fallthru (seq))
2975 gimple g;
2976 label_cont = create_artificial_label ();
2978 g = gimple_build_goto (label_cont);
2980 /* GIMPLE_COND's are very low level; they have embedded
2981 gotos. This particular embedded goto should not be marked
2982 with the location of the original COND_EXPR, as it would
2983 correspond to the COND_EXPR's condition, not the ELSE or the
2984 THEN arms. To avoid marking it with the wrong location, flag
2985 it as "no location". */
2986 gimple_set_do_not_emit_location (g);
2988 gimplify_seq_add_stmt (&seq, g);
2992 if (!have_else_clause_p)
2994 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
2995 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
2997 if (label_cont)
2998 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3000 gimple_pop_condition (pre_p);
3001 gimple_seq_add_seq (pre_p, seq);
3003 if (ret == GS_ERROR)
3004 ; /* Do nothing. */
3005 else if (have_then_clause_p || have_else_clause_p)
3006 ret = GS_ALL_DONE;
3007 else
3009 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3010 expr = TREE_OPERAND (expr, 0);
3011 gimplify_stmt (&expr, pre_p);
3014 *expr_p = NULL;
3015 return ret;
3018 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3019 a call to __builtin_memcpy. */
3021 static enum gimplify_status
3022 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3023 gimple_seq *seq_p)
3025 tree t, to, to_ptr, from, from_ptr;
3026 gimple gs;
3028 to = TREE_OPERAND (*expr_p, 0);
3029 from = TREE_OPERAND (*expr_p, 1);
3031 mark_addressable (from);
3032 from_ptr = build_fold_addr_expr (from);
3033 gimplify_arg (&from_ptr, seq_p, EXPR_LOCATION (*expr_p));
3035 mark_addressable (to);
3036 to_ptr = build_fold_addr_expr (to);
3037 gimplify_arg (&to_ptr, seq_p, EXPR_LOCATION (*expr_p));
3039 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
3041 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3043 if (want_value)
3045 /* tmp = memcpy() */
3046 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3047 gimple_call_set_lhs (gs, t);
3048 gimplify_seq_add_stmt (seq_p, gs);
3050 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3051 return GS_ALL_DONE;
3054 gimplify_seq_add_stmt (seq_p, gs);
3055 *expr_p = NULL;
3056 return GS_ALL_DONE;
3059 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3060 a call to __builtin_memset. In this case we know that the RHS is
3061 a CONSTRUCTOR with an empty element list. */
3063 static enum gimplify_status
3064 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3065 gimple_seq *seq_p)
3067 tree t, from, to, to_ptr;
3068 gimple gs;
3070 /* Assert our assumptions, to abort instead of producing wrong code
3071 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3072 not be immediately exposed. */
3073 from = TREE_OPERAND (*expr_p, 1);
3074 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3075 from = TREE_OPERAND (from, 0);
3077 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3078 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (from)));
3080 /* Now proceed. */
3081 to = TREE_OPERAND (*expr_p, 0);
3083 to_ptr = build_fold_addr_expr (to);
3084 gimplify_arg (&to_ptr, seq_p, EXPR_LOCATION (*expr_p));
3085 t = implicit_built_in_decls[BUILT_IN_MEMSET];
3087 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3089 if (want_value)
3091 /* tmp = memset() */
3092 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3093 gimple_call_set_lhs (gs, t);
3094 gimplify_seq_add_stmt (seq_p, gs);
3096 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3097 return GS_ALL_DONE;
3100 gimplify_seq_add_stmt (seq_p, gs);
3101 *expr_p = NULL;
3102 return GS_ALL_DONE;
3105 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3106 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3107 assignment. Returns non-null if we detect a potential overlap. */
3109 struct gimplify_init_ctor_preeval_data
3111 /* The base decl of the lhs object. May be NULL, in which case we
3112 have to assume the lhs is indirect. */
3113 tree lhs_base_decl;
3115 /* The alias set of the lhs object. */
3116 alias_set_type lhs_alias_set;
3119 static tree
3120 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3122 struct gimplify_init_ctor_preeval_data *data
3123 = (struct gimplify_init_ctor_preeval_data *) xdata;
3124 tree t = *tp;
3126 /* If we find the base object, obviously we have overlap. */
3127 if (data->lhs_base_decl == t)
3128 return t;
3130 /* If the constructor component is indirect, determine if we have a
3131 potential overlap with the lhs. The only bits of information we
3132 have to go on at this point are addressability and alias sets. */
3133 if (TREE_CODE (t) == INDIRECT_REF
3134 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3135 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3136 return t;
3138 /* If the constructor component is a call, determine if it can hide a
3139 potential overlap with the lhs through an INDIRECT_REF like above. */
3140 if (TREE_CODE (t) == CALL_EXPR)
3142 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3144 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3145 if (POINTER_TYPE_P (TREE_VALUE (type))
3146 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3147 && alias_sets_conflict_p (data->lhs_alias_set,
3148 get_alias_set
3149 (TREE_TYPE (TREE_VALUE (type)))))
3150 return t;
3153 if (IS_TYPE_OR_DECL_P (t))
3154 *walk_subtrees = 0;
3155 return NULL;
3158 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3159 force values that overlap with the lhs (as described by *DATA)
3160 into temporaries. */
3162 static void
3163 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3164 struct gimplify_init_ctor_preeval_data *data)
3166 enum gimplify_status one;
3168 /* If the value is constant, then there's nothing to pre-evaluate. */
3169 if (TREE_CONSTANT (*expr_p))
3171 /* Ensure it does not have side effects, it might contain a reference to
3172 the object we're initializing. */
3173 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3174 return;
3177 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3178 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3179 return;
3181 /* Recurse for nested constructors. */
3182 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3184 unsigned HOST_WIDE_INT ix;
3185 constructor_elt *ce;
3186 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
3188 for (ix = 0; VEC_iterate (constructor_elt, v, ix, ce); ix++)
3189 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3191 return;
3194 /* If this is a variable sized type, we must remember the size. */
3195 maybe_with_size_expr (expr_p);
3197 /* Gimplify the constructor element to something appropriate for the rhs
3198 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3199 the gimplifier will consider this a store to memory. Doing this
3200 gimplification now means that we won't have to deal with complicated
3201 language-specific trees, nor trees like SAVE_EXPR that can induce
3202 exponential search behavior. */
3203 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3204 if (one == GS_ERROR)
3206 *expr_p = NULL;
3207 return;
3210 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3211 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3212 always be true for all scalars, since is_gimple_mem_rhs insists on a
3213 temporary variable for them. */
3214 if (DECL_P (*expr_p))
3215 return;
3217 /* If this is of variable size, we have no choice but to assume it doesn't
3218 overlap since we can't make a temporary for it. */
3219 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3220 return;
3222 /* Otherwise, we must search for overlap ... */
3223 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3224 return;
3226 /* ... and if found, force the value into a temporary. */
3227 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3230 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3231 a RANGE_EXPR in a CONSTRUCTOR for an array.
3233 var = lower;
3234 loop_entry:
3235 object[var] = value;
3236 if (var == upper)
3237 goto loop_exit;
3238 var = var + 1;
3239 goto loop_entry;
3240 loop_exit:
3242 We increment var _after_ the loop exit check because we might otherwise
3243 fail if upper == TYPE_MAX_VALUE (type for upper).
3245 Note that we never have to deal with SAVE_EXPRs here, because this has
3246 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3248 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
3249 gimple_seq *, bool);
3251 static void
3252 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3253 tree value, tree array_elt_type,
3254 gimple_seq *pre_p, bool cleared)
3256 tree loop_entry_label, loop_exit_label, fall_thru_label;
3257 tree var, var_type, cref, tmp;
3259 loop_entry_label = create_artificial_label ();
3260 loop_exit_label = create_artificial_label ();
3261 fall_thru_label = create_artificial_label ();
3263 /* Create and initialize the index variable. */
3264 var_type = TREE_TYPE (upper);
3265 var = create_tmp_var (var_type, NULL);
3266 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3268 /* Add the loop entry label. */
3269 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3271 /* Build the reference. */
3272 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3273 var, NULL_TREE, NULL_TREE);
3275 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3276 the store. Otherwise just assign value to the reference. */
3278 if (TREE_CODE (value) == CONSTRUCTOR)
3279 /* NB we might have to call ourself recursively through
3280 gimplify_init_ctor_eval if the value is a constructor. */
3281 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3282 pre_p, cleared);
3283 else
3284 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3286 /* We exit the loop when the index var is equal to the upper bound. */
3287 gimplify_seq_add_stmt (pre_p,
3288 gimple_build_cond (EQ_EXPR, var, upper,
3289 loop_exit_label, fall_thru_label));
3291 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3293 /* Otherwise, increment the index var... */
3294 tmp = build2 (PLUS_EXPR, var_type, var,
3295 fold_convert (var_type, integer_one_node));
3296 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3298 /* ...and jump back to the loop entry. */
3299 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3301 /* Add the loop exit label. */
3302 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3305 /* Return true if FDECL is accessing a field that is zero sized. */
3307 static bool
3308 zero_sized_field_decl (const_tree fdecl)
3310 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3311 && integer_zerop (DECL_SIZE (fdecl)))
3312 return true;
3313 return false;
3316 /* Return true if TYPE is zero sized. */
3318 static bool
3319 zero_sized_type (const_tree type)
3321 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3322 && integer_zerop (TYPE_SIZE (type)))
3323 return true;
3324 return false;
3327 /* A subroutine of gimplify_init_constructor. Generate individual
3328 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3329 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3330 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3331 zeroed first. */
3333 static void
3334 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
3335 gimple_seq *pre_p, bool cleared)
3337 tree array_elt_type = NULL;
3338 unsigned HOST_WIDE_INT ix;
3339 tree purpose, value;
3341 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3342 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3344 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3346 tree cref;
3348 /* NULL values are created above for gimplification errors. */
3349 if (value == NULL)
3350 continue;
3352 if (cleared && initializer_zerop (value))
3353 continue;
3355 /* ??? Here's to hoping the front end fills in all of the indices,
3356 so we don't have to figure out what's missing ourselves. */
3357 gcc_assert (purpose);
3359 /* Skip zero-sized fields, unless value has side-effects. This can
3360 happen with calls to functions returning a zero-sized type, which
3361 we shouldn't discard. As a number of downstream passes don't
3362 expect sets of zero-sized fields, we rely on the gimplification of
3363 the MODIFY_EXPR we make below to drop the assignment statement. */
3364 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3365 continue;
3367 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3368 whole range. */
3369 if (TREE_CODE (purpose) == RANGE_EXPR)
3371 tree lower = TREE_OPERAND (purpose, 0);
3372 tree upper = TREE_OPERAND (purpose, 1);
3374 /* If the lower bound is equal to upper, just treat it as if
3375 upper was the index. */
3376 if (simple_cst_equal (lower, upper))
3377 purpose = upper;
3378 else
3380 gimplify_init_ctor_eval_range (object, lower, upper, value,
3381 array_elt_type, pre_p, cleared);
3382 continue;
3386 if (array_elt_type)
3388 /* Do not use bitsizetype for ARRAY_REF indices. */
3389 if (TYPE_DOMAIN (TREE_TYPE (object)))
3390 purpose = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3391 purpose);
3392 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3393 purpose, NULL_TREE, NULL_TREE);
3395 else
3397 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3398 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3399 unshare_expr (object), purpose, NULL_TREE);
3402 if (TREE_CODE (value) == CONSTRUCTOR
3403 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3404 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3405 pre_p, cleared);
3406 else
3408 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3409 gimplify_and_add (init, pre_p);
3410 ggc_free (init);
3416 /* Returns the appropriate RHS predicate for this LHS. */
3418 gimple_predicate
3419 rhs_predicate_for (tree lhs)
3421 if (is_gimple_reg (lhs))
3422 return is_gimple_reg_rhs_or_call;
3423 else
3424 return is_gimple_mem_rhs_or_call;
3427 /* Gimplify a C99 compound literal expression. This just means adding
3428 the DECL_EXPR before the current statement and using its anonymous
3429 decl instead. */
3431 static enum gimplify_status
3432 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p)
3434 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3435 tree decl = DECL_EXPR_DECL (decl_s);
3436 /* Mark the decl as addressable if the compound literal
3437 expression is addressable now, otherwise it is marked too late
3438 after we gimplify the initialization expression. */
3439 if (TREE_ADDRESSABLE (*expr_p))
3440 TREE_ADDRESSABLE (decl) = 1;
3442 /* Preliminarily mark non-addressed complex variables as eligible
3443 for promotion to gimple registers. We'll transform their uses
3444 as we find them. */
3445 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3446 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3447 && !TREE_THIS_VOLATILE (decl)
3448 && !needs_to_live_in_memory (decl))
3449 DECL_GIMPLE_REG_P (decl) = 1;
3451 /* This decl isn't mentioned in the enclosing block, so add it to the
3452 list of temps. FIXME it seems a bit of a kludge to say that
3453 anonymous artificial vars aren't pushed, but everything else is. */
3454 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3455 gimple_add_tmp_var (decl);
3457 gimplify_and_add (decl_s, pre_p);
3458 *expr_p = decl;
3459 return GS_OK;
3462 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3463 return a new CONSTRUCTOR if something changed. */
3465 static tree
3466 optimize_compound_literals_in_ctor (tree orig_ctor)
3468 tree ctor = orig_ctor;
3469 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
3470 unsigned int idx, num = VEC_length (constructor_elt, elts);
3472 for (idx = 0; idx < num; idx++)
3474 tree value = VEC_index (constructor_elt, elts, idx)->value;
3475 tree newval = value;
3476 if (TREE_CODE (value) == CONSTRUCTOR)
3477 newval = optimize_compound_literals_in_ctor (value);
3478 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3480 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3481 tree decl = DECL_EXPR_DECL (decl_s);
3482 tree init = DECL_INITIAL (decl);
3484 if (!TREE_ADDRESSABLE (value)
3485 && !TREE_ADDRESSABLE (decl)
3486 && init)
3487 newval = optimize_compound_literals_in_ctor (init);
3489 if (newval == value)
3490 continue;
3492 if (ctor == orig_ctor)
3494 ctor = copy_node (orig_ctor);
3495 CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
3496 elts = CONSTRUCTOR_ELTS (ctor);
3498 VEC_index (constructor_elt, elts, idx)->value = newval;
3500 return ctor;
3505 /* A subroutine of gimplify_modify_expr. Break out elements of a
3506 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3508 Note that we still need to clear any elements that don't have explicit
3509 initializers, so if not all elements are initialized we keep the
3510 original MODIFY_EXPR, we just remove all of the constructor elements.
3512 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3513 GS_ERROR if we would have to create a temporary when gimplifying
3514 this constructor. Otherwise, return GS_OK.
3516 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3518 static enum gimplify_status
3519 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3520 bool want_value, bool notify_temp_creation)
3522 tree object, new_ctor;
3523 tree ctor = TREE_OPERAND (*expr_p, 1);
3524 tree type = TREE_TYPE (ctor);
3525 enum gimplify_status ret;
3526 VEC(constructor_elt,gc) *elts;
3528 if (TREE_CODE (ctor) != CONSTRUCTOR)
3529 return GS_UNHANDLED;
3531 if (!notify_temp_creation)
3533 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3534 is_gimple_lvalue, fb_lvalue);
3535 if (ret == GS_ERROR)
3536 return ret;
3539 object = TREE_OPERAND (*expr_p, 0);
3540 new_ctor = optimize_compound_literals_in_ctor (ctor);
3541 elts = CONSTRUCTOR_ELTS (new_ctor);
3542 ret = GS_ALL_DONE;
3544 switch (TREE_CODE (type))
3546 case RECORD_TYPE:
3547 case UNION_TYPE:
3548 case QUAL_UNION_TYPE:
3549 case ARRAY_TYPE:
3551 struct gimplify_init_ctor_preeval_data preeval_data;
3552 HOST_WIDE_INT num_type_elements, num_ctor_elements;
3553 HOST_WIDE_INT num_nonzero_elements;
3554 bool cleared, valid_const_initializer;
3556 /* Aggregate types must lower constructors to initialization of
3557 individual elements. The exception is that a CONSTRUCTOR node
3558 with no elements indicates zero-initialization of the whole. */
3559 if (VEC_empty (constructor_elt, elts))
3561 if (notify_temp_creation)
3562 return GS_OK;
3563 break;
3566 /* Fetch information about the constructor to direct later processing.
3567 We might want to make static versions of it in various cases, and
3568 can only do so if it known to be a valid constant initializer. */
3569 valid_const_initializer
3570 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3571 &num_ctor_elements, &cleared);
3573 /* If a const aggregate variable is being initialized, then it
3574 should never be a lose to promote the variable to be static. */
3575 if (valid_const_initializer
3576 && num_nonzero_elements > 1
3577 && TREE_READONLY (object)
3578 && TREE_CODE (object) == VAR_DECL
3579 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3581 if (notify_temp_creation)
3582 return GS_ERROR;
3583 DECL_INITIAL (object) = ctor;
3584 TREE_STATIC (object) = 1;
3585 if (!DECL_NAME (object))
3586 DECL_NAME (object) = create_tmp_var_name ("C");
3587 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3589 /* ??? C++ doesn't automatically append a .<number> to the
3590 assembler name, and even when it does, it looks a FE private
3591 data structures to figure out what that number should be,
3592 which are not set for this variable. I suppose this is
3593 important for local statics for inline functions, which aren't
3594 "local" in the object file sense. So in order to get a unique
3595 TU-local symbol, we must invoke the lhd version now. */
3596 lhd_set_decl_assembler_name (object);
3598 *expr_p = NULL_TREE;
3599 break;
3602 /* If there are "lots" of initialized elements, even discounting
3603 those that are not address constants (and thus *must* be
3604 computed at runtime), then partition the constructor into
3605 constant and non-constant parts. Block copy the constant
3606 parts in, then generate code for the non-constant parts. */
3607 /* TODO. There's code in cp/typeck.c to do this. */
3609 num_type_elements = count_type_elements (type, true);
3611 /* If count_type_elements could not determine number of type elements
3612 for a constant-sized object, assume clearing is needed.
3613 Don't do this for variable-sized objects, as store_constructor
3614 will ignore the clearing of variable-sized objects. */
3615 if (num_type_elements < 0 && int_size_in_bytes (type) >= 0)
3616 cleared = true;
3617 /* If there are "lots" of zeros, then block clear the object first. */
3618 else if (num_type_elements - num_nonzero_elements
3619 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3620 && num_nonzero_elements < num_type_elements/4)
3621 cleared = true;
3622 /* ??? This bit ought not be needed. For any element not present
3623 in the initializer, we should simply set them to zero. Except
3624 we'd need to *find* the elements that are not present, and that
3625 requires trickery to avoid quadratic compile-time behavior in
3626 large cases or excessive memory use in small cases. */
3627 else if (num_ctor_elements < num_type_elements)
3628 cleared = true;
3630 /* If there are "lots" of initialized elements, and all of them
3631 are valid address constants, then the entire initializer can
3632 be dropped to memory, and then memcpy'd out. Don't do this
3633 for sparse arrays, though, as it's more efficient to follow
3634 the standard CONSTRUCTOR behavior of memset followed by
3635 individual element initialization. Also don't do this for small
3636 all-zero initializers (which aren't big enough to merit
3637 clearing), and don't try to make bitwise copies of
3638 TREE_ADDRESSABLE types. */
3639 if (valid_const_initializer
3640 && !(cleared || num_nonzero_elements == 0)
3641 && !TREE_ADDRESSABLE (type))
3643 HOST_WIDE_INT size = int_size_in_bytes (type);
3644 unsigned int align;
3646 /* ??? We can still get unbounded array types, at least
3647 from the C++ front end. This seems wrong, but attempt
3648 to work around it for now. */
3649 if (size < 0)
3651 size = int_size_in_bytes (TREE_TYPE (object));
3652 if (size >= 0)
3653 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3656 /* Find the maximum alignment we can assume for the object. */
3657 /* ??? Make use of DECL_OFFSET_ALIGN. */
3658 if (DECL_P (object))
3659 align = DECL_ALIGN (object);
3660 else
3661 align = TYPE_ALIGN (type);
3663 if (size > 0
3664 && num_nonzero_elements > 1
3665 && !can_move_by_pieces (size, align))
3667 tree new_tree;
3669 if (notify_temp_creation)
3670 return GS_ERROR;
3672 new_tree = create_tmp_var_raw (type, "C");
3674 gimple_add_tmp_var (new_tree);
3675 TREE_STATIC (new_tree) = 1;
3676 TREE_READONLY (new_tree) = 1;
3677 DECL_INITIAL (new_tree) = ctor;
3678 if (align > DECL_ALIGN (new_tree))
3680 DECL_ALIGN (new_tree) = align;
3681 DECL_USER_ALIGN (new_tree) = 1;
3683 walk_tree (&DECL_INITIAL (new_tree), force_labels_r, NULL, NULL);
3685 TREE_OPERAND (*expr_p, 1) = new_tree;
3687 /* This is no longer an assignment of a CONSTRUCTOR, but
3688 we still may have processing to do on the LHS. So
3689 pretend we didn't do anything here to let that happen. */
3690 return GS_UNHANDLED;
3694 if (notify_temp_creation)
3695 return GS_OK;
3697 /* If there are nonzero elements, pre-evaluate to capture elements
3698 overlapping with the lhs into temporaries. We must do this before
3699 clearing to fetch the values before they are zeroed-out. */
3700 if (num_nonzero_elements > 0)
3702 preeval_data.lhs_base_decl = get_base_address (object);
3703 if (!DECL_P (preeval_data.lhs_base_decl))
3704 preeval_data.lhs_base_decl = NULL;
3705 preeval_data.lhs_alias_set = get_alias_set (object);
3707 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3708 pre_p, post_p, &preeval_data);
3711 if (cleared)
3713 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3714 Note that we still have to gimplify, in order to handle the
3715 case of variable sized types. Avoid shared tree structures. */
3716 CONSTRUCTOR_ELTS (ctor) = NULL;
3717 TREE_SIDE_EFFECTS (ctor) = 0;
3718 object = unshare_expr (object);
3719 gimplify_stmt (expr_p, pre_p);
3722 /* If we have not block cleared the object, or if there are nonzero
3723 elements in the constructor, add assignments to the individual
3724 scalar fields of the object. */
3725 if (!cleared || num_nonzero_elements > 0)
3726 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3728 *expr_p = NULL_TREE;
3730 break;
3732 case COMPLEX_TYPE:
3734 tree r, i;
3736 if (notify_temp_creation)
3737 return GS_OK;
3739 /* Extract the real and imaginary parts out of the ctor. */
3740 gcc_assert (VEC_length (constructor_elt, elts) == 2);
3741 r = VEC_index (constructor_elt, elts, 0)->value;
3742 i = VEC_index (constructor_elt, elts, 1)->value;
3743 if (r == NULL || i == NULL)
3745 tree zero = fold_convert (TREE_TYPE (type), integer_zero_node);
3746 if (r == NULL)
3747 r = zero;
3748 if (i == NULL)
3749 i = zero;
3752 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3753 represent creation of a complex value. */
3754 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3756 ctor = build_complex (type, r, i);
3757 TREE_OPERAND (*expr_p, 1) = ctor;
3759 else
3761 ctor = build2 (COMPLEX_EXPR, type, r, i);
3762 TREE_OPERAND (*expr_p, 1) = ctor;
3763 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
3764 pre_p,
3765 post_p,
3766 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3767 fb_rvalue);
3770 break;
3772 case VECTOR_TYPE:
3774 unsigned HOST_WIDE_INT ix;
3775 constructor_elt *ce;
3777 if (notify_temp_creation)
3778 return GS_OK;
3780 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3781 if (TREE_CONSTANT (ctor))
3783 bool constant_p = true;
3784 tree value;
3786 /* Even when ctor is constant, it might contain non-*_CST
3787 elements, such as addresses or trapping values like
3788 1.0/0.0 - 1.0/0.0. Such expressions don't belong
3789 in VECTOR_CST nodes. */
3790 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3791 if (!CONSTANT_CLASS_P (value))
3793 constant_p = false;
3794 break;
3797 if (constant_p)
3799 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3800 break;
3803 /* Don't reduce an initializer constant even if we can't
3804 make a VECTOR_CST. It won't do anything for us, and it'll
3805 prevent us from representing it as a single constant. */
3806 if (initializer_constant_valid_p (ctor, type))
3807 break;
3809 TREE_CONSTANT (ctor) = 0;
3812 /* Vector types use CONSTRUCTOR all the way through gimple
3813 compilation as a general initializer. */
3814 for (ix = 0; VEC_iterate (constructor_elt, elts, ix, ce); ix++)
3816 enum gimplify_status tret;
3817 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
3818 fb_rvalue);
3819 if (tret == GS_ERROR)
3820 ret = GS_ERROR;
3822 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
3823 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3825 break;
3827 default:
3828 /* So how did we get a CONSTRUCTOR for a scalar type? */
3829 gcc_unreachable ();
3832 if (ret == GS_ERROR)
3833 return GS_ERROR;
3834 else if (want_value)
3836 *expr_p = object;
3837 return GS_OK;
3839 else
3841 /* If we have gimplified both sides of the initializer but have
3842 not emitted an assignment, do so now. */
3843 if (*expr_p)
3845 tree lhs = TREE_OPERAND (*expr_p, 0);
3846 tree rhs = TREE_OPERAND (*expr_p, 1);
3847 gimple init = gimple_build_assign (lhs, rhs);
3848 gimplify_seq_add_stmt (pre_p, init);
3849 *expr_p = NULL;
3852 return GS_ALL_DONE;
3856 /* Given a pointer value OP0, return a simplified version of an
3857 indirection through OP0, or NULL_TREE if no simplification is
3858 possible. Note that the resulting type may be different from
3859 the type pointed to in the sense that it is still compatible
3860 from the langhooks point of view. */
3862 tree
3863 gimple_fold_indirect_ref (tree t)
3865 tree type = TREE_TYPE (TREE_TYPE (t));
3866 tree sub = t;
3867 tree subtype;
3869 STRIP_USELESS_TYPE_CONVERSION (sub);
3870 subtype = TREE_TYPE (sub);
3871 if (!POINTER_TYPE_P (subtype))
3872 return NULL_TREE;
3874 if (TREE_CODE (sub) == ADDR_EXPR)
3876 tree op = TREE_OPERAND (sub, 0);
3877 tree optype = TREE_TYPE (op);
3878 /* *&p => p */
3879 if (useless_type_conversion_p (type, optype))
3880 return op;
3882 /* *(foo *)&fooarray => fooarray[0] */
3883 if (TREE_CODE (optype) == ARRAY_TYPE
3884 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3886 tree type_domain = TYPE_DOMAIN (optype);
3887 tree min_val = size_zero_node;
3888 if (type_domain && TYPE_MIN_VALUE (type_domain))
3889 min_val = TYPE_MIN_VALUE (type_domain);
3890 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3894 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3895 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3896 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3898 tree type_domain;
3899 tree min_val = size_zero_node;
3900 tree osub = sub;
3901 sub = gimple_fold_indirect_ref (sub);
3902 if (! sub)
3903 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3904 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3905 if (type_domain && TYPE_MIN_VALUE (type_domain))
3906 min_val = TYPE_MIN_VALUE (type_domain);
3907 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3910 return NULL_TREE;
3913 /* Given a pointer value OP0, return a simplified version of an
3914 indirection through OP0, or NULL_TREE if no simplification is
3915 possible. This may only be applied to a rhs of an expression.
3916 Note that the resulting type may be different from the type pointed
3917 to in the sense that it is still compatible from the langhooks
3918 point of view. */
3920 static tree
3921 gimple_fold_indirect_ref_rhs (tree t)
3923 return gimple_fold_indirect_ref (t);
3926 /* Subroutine of gimplify_modify_expr to do simplifications of
3927 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
3928 something changes. */
3930 static enum gimplify_status
3931 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
3932 gimple_seq *pre_p, gimple_seq *post_p,
3933 bool want_value)
3935 enum gimplify_status ret = GS_OK;
3937 while (ret != GS_UNHANDLED)
3938 switch (TREE_CODE (*from_p))
3940 case VAR_DECL:
3941 /* If we're assigning from a constant constructor, move the
3942 constructor expression to the RHS of the MODIFY_EXPR. */
3943 if (DECL_INITIAL (*from_p)
3944 && TREE_READONLY (*from_p)
3945 && !TREE_THIS_VOLATILE (*from_p)
3946 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
3948 tree old_from = *from_p;
3950 /* Move the constructor into the RHS. */
3951 *from_p = unshare_expr (DECL_INITIAL (*from_p));
3953 /* Let's see if gimplify_init_constructor will need to put
3954 it in memory. If so, revert the change. */
3955 ret = gimplify_init_constructor (expr_p, NULL, NULL, false, true);
3956 if (ret == GS_ERROR)
3958 *from_p = old_from;
3959 /* Fall through. */
3961 else
3963 ret = GS_OK;
3964 break;
3967 ret = GS_UNHANDLED;
3968 break;
3969 case INDIRECT_REF:
3971 /* If we have code like
3973 *(const A*)(A*)&x
3975 where the type of "x" is a (possibly cv-qualified variant
3976 of "A"), treat the entire expression as identical to "x".
3977 This kind of code arises in C++ when an object is bound
3978 to a const reference, and if "x" is a TARGET_EXPR we want
3979 to take advantage of the optimization below. */
3980 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
3981 if (t)
3983 *from_p = t;
3984 ret = GS_OK;
3986 else
3987 ret = GS_UNHANDLED;
3988 break;
3991 case TARGET_EXPR:
3993 /* If we are initializing something from a TARGET_EXPR, strip the
3994 TARGET_EXPR and initialize it directly, if possible. This can't
3995 be done if the initializer is void, since that implies that the
3996 temporary is set in some non-trivial way.
3998 ??? What about code that pulls out the temp and uses it
3999 elsewhere? I think that such code never uses the TARGET_EXPR as
4000 an initializer. If I'm wrong, we'll die because the temp won't
4001 have any RTL. In that case, I guess we'll need to replace
4002 references somehow. */
4003 tree init = TARGET_EXPR_INITIAL (*from_p);
4005 if (init
4006 && !VOID_TYPE_P (TREE_TYPE (init)))
4008 *from_p = init;
4009 ret = GS_OK;
4011 else
4012 ret = GS_UNHANDLED;
4014 break;
4016 case COMPOUND_EXPR:
4017 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4018 caught. */
4019 gimplify_compound_expr (from_p, pre_p, true);
4020 ret = GS_OK;
4021 break;
4023 case CONSTRUCTOR:
4024 /* If we're initializing from a CONSTRUCTOR, break this into
4025 individual MODIFY_EXPRs. */
4026 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4027 false);
4029 case COND_EXPR:
4030 /* If we're assigning to a non-register type, push the assignment
4031 down into the branches. This is mandatory for ADDRESSABLE types,
4032 since we cannot generate temporaries for such, but it saves a
4033 copy in other cases as well. */
4034 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4036 /* This code should mirror the code in gimplify_cond_expr. */
4037 enum tree_code code = TREE_CODE (*expr_p);
4038 tree cond = *from_p;
4039 tree result = *to_p;
4041 ret = gimplify_expr (&result, pre_p, post_p,
4042 is_gimple_lvalue, fb_lvalue);
4043 if (ret != GS_ERROR)
4044 ret = GS_OK;
4046 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4047 TREE_OPERAND (cond, 1)
4048 = build2 (code, void_type_node, result,
4049 TREE_OPERAND (cond, 1));
4050 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4051 TREE_OPERAND (cond, 2)
4052 = build2 (code, void_type_node, unshare_expr (result),
4053 TREE_OPERAND (cond, 2));
4055 TREE_TYPE (cond) = void_type_node;
4056 recalculate_side_effects (cond);
4058 if (want_value)
4060 gimplify_and_add (cond, pre_p);
4061 *expr_p = unshare_expr (result);
4063 else
4064 *expr_p = cond;
4065 return ret;
4067 else
4068 ret = GS_UNHANDLED;
4069 break;
4071 case CALL_EXPR:
4072 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4073 return slot so that we don't generate a temporary. */
4074 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4075 && aggregate_value_p (*from_p, *from_p))
4077 bool use_target;
4079 if (!(rhs_predicate_for (*to_p))(*from_p))
4080 /* If we need a temporary, *to_p isn't accurate. */
4081 use_target = false;
4082 else if (TREE_CODE (*to_p) == RESULT_DECL
4083 && DECL_NAME (*to_p) == NULL_TREE
4084 && needs_to_live_in_memory (*to_p))
4085 /* It's OK to use the return slot directly unless it's an NRV. */
4086 use_target = true;
4087 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4088 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4089 /* Don't force regs into memory. */
4090 use_target = false;
4091 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4092 /* It's OK to use the target directly if it's being
4093 initialized. */
4094 use_target = true;
4095 else if (!is_gimple_non_addressable (*to_p))
4096 /* Don't use the original target if it's already addressable;
4097 if its address escapes, and the called function uses the
4098 NRV optimization, a conforming program could see *to_p
4099 change before the called function returns; see c++/19317.
4100 When optimizing, the return_slot pass marks more functions
4101 as safe after we have escape info. */
4102 use_target = false;
4103 else
4104 use_target = true;
4106 if (use_target)
4108 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4109 mark_addressable (*to_p);
4113 ret = GS_UNHANDLED;
4114 break;
4116 /* If we're initializing from a container, push the initialization
4117 inside it. */
4118 case CLEANUP_POINT_EXPR:
4119 case BIND_EXPR:
4120 case STATEMENT_LIST:
4122 tree wrap = *from_p;
4123 tree t;
4125 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4126 fb_lvalue);
4127 if (ret != GS_ERROR)
4128 ret = GS_OK;
4130 t = voidify_wrapper_expr (wrap, *expr_p);
4131 gcc_assert (t == *expr_p);
4133 if (want_value)
4135 gimplify_and_add (wrap, pre_p);
4136 *expr_p = unshare_expr (*to_p);
4138 else
4139 *expr_p = wrap;
4140 return GS_OK;
4143 case COMPOUND_LITERAL_EXPR:
4145 tree complit = TREE_OPERAND (*expr_p, 1);
4146 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4147 tree decl = DECL_EXPR_DECL (decl_s);
4148 tree init = DECL_INITIAL (decl);
4150 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4151 into struct T x = { 0, 1, 2 } if the address of the
4152 compound literal has never been taken. */
4153 if (!TREE_ADDRESSABLE (complit)
4154 && !TREE_ADDRESSABLE (decl)
4155 && init)
4157 *expr_p = copy_node (*expr_p);
4158 TREE_OPERAND (*expr_p, 1) = init;
4159 return GS_OK;
4163 default:
4164 ret = GS_UNHANDLED;
4165 break;
4168 return ret;
4172 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4173 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4174 DECL_GIMPLE_REG_P set.
4176 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4177 other, unmodified part of the complex object just before the total store.
4178 As a consequence, if the object is still uninitialized, an undefined value
4179 will be loaded into a register, which may result in a spurious exception
4180 if the register is floating-point and the value happens to be a signaling
4181 NaN for example. Then the fully-fledged complex operations lowering pass
4182 followed by a DCE pass are necessary in order to fix things up. */
4184 static enum gimplify_status
4185 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4186 bool want_value)
4188 enum tree_code code, ocode;
4189 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4191 lhs = TREE_OPERAND (*expr_p, 0);
4192 rhs = TREE_OPERAND (*expr_p, 1);
4193 code = TREE_CODE (lhs);
4194 lhs = TREE_OPERAND (lhs, 0);
4196 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4197 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4198 other = get_formal_tmp_var (other, pre_p);
4200 realpart = code == REALPART_EXPR ? rhs : other;
4201 imagpart = code == REALPART_EXPR ? other : rhs;
4203 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4204 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4205 else
4206 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4208 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4209 *expr_p = (want_value) ? rhs : NULL_TREE;
4211 return GS_ALL_DONE;
4215 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4217 modify_expr
4218 : varname '=' rhs
4219 | '*' ID '=' rhs
4221 PRE_P points to the list where side effects that must happen before
4222 *EXPR_P should be stored.
4224 POST_P points to the list where side effects that must happen after
4225 *EXPR_P should be stored.
4227 WANT_VALUE is nonzero iff we want to use the value of this expression
4228 in another expression. */
4230 static enum gimplify_status
4231 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4232 bool want_value)
4234 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4235 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4236 enum gimplify_status ret = GS_UNHANDLED;
4237 gimple assign;
4239 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4240 || TREE_CODE (*expr_p) == INIT_EXPR);
4242 /* Insert pointer conversions required by the middle-end that are not
4243 required by the frontend. This fixes middle-end type checking for
4244 for example gcc.dg/redecl-6.c. */
4245 if (POINTER_TYPE_P (TREE_TYPE (*to_p))
4246 && lang_hooks.types_compatible_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4248 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4249 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4250 *from_p = fold_convert (TREE_TYPE (*to_p), *from_p);
4253 /* See if any simplifications can be done based on what the RHS is. */
4254 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4255 want_value);
4256 if (ret != GS_UNHANDLED)
4257 return ret;
4259 /* For zero sized types only gimplify the left hand side and right hand
4260 side as statements and throw away the assignment. Do this after
4261 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4262 types properly. */
4263 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4265 gimplify_stmt (from_p, pre_p);
4266 gimplify_stmt (to_p, pre_p);
4267 *expr_p = NULL_TREE;
4268 return GS_ALL_DONE;
4271 /* If the value being copied is of variable width, compute the length
4272 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4273 before gimplifying any of the operands so that we can resolve any
4274 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4275 the size of the expression to be copied, not of the destination, so
4276 that is what we must do here. */
4277 maybe_with_size_expr (from_p);
4279 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4280 if (ret == GS_ERROR)
4281 return ret;
4283 /* As a special case, we have to temporarily allow for assignments
4284 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4285 a toplevel statement, when gimplifying the GENERIC expression
4286 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4287 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4289 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4290 prevent gimplify_expr from trying to create a new temporary for
4291 foo's LHS, we tell it that it should only gimplify until it
4292 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4293 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4294 and all we need to do here is set 'a' to be its LHS. */
4295 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4296 fb_rvalue);
4297 if (ret == GS_ERROR)
4298 return ret;
4300 /* Now see if the above changed *from_p to something we handle specially. */
4301 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4302 want_value);
4303 if (ret != GS_UNHANDLED)
4304 return ret;
4306 /* If we've got a variable sized assignment between two lvalues (i.e. does
4307 not involve a call), then we can make things a bit more straightforward
4308 by converting the assignment to memcpy or memset. */
4309 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4311 tree from = TREE_OPERAND (*from_p, 0);
4312 tree size = TREE_OPERAND (*from_p, 1);
4314 if (TREE_CODE (from) == CONSTRUCTOR)
4315 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4317 if (is_gimple_addressable (from))
4319 *from_p = from;
4320 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4321 pre_p);
4325 /* Transform partial stores to non-addressable complex variables into
4326 total stores. This allows us to use real instead of virtual operands
4327 for these variables, which improves optimization. */
4328 if ((TREE_CODE (*to_p) == REALPART_EXPR
4329 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4330 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4331 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4333 /* Try to alleviate the effects of the gimplification creating artificial
4334 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4335 if (!gimplify_ctxp->into_ssa
4336 && DECL_P (*from_p)
4337 && DECL_IGNORED_P (*from_p)
4338 && DECL_P (*to_p)
4339 && !DECL_IGNORED_P (*to_p))
4341 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4342 DECL_NAME (*from_p)
4343 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4344 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4345 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4348 if (TREE_CODE (*from_p) == CALL_EXPR)
4350 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4351 instead of a GIMPLE_ASSIGN. */
4352 assign = gimple_build_call_from_tree (*from_p);
4353 gimple_call_set_lhs (assign, *to_p);
4355 else
4356 assign = gimple_build_assign (*to_p, *from_p);
4358 gimplify_seq_add_stmt (pre_p, assign);
4360 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4362 /* If we've somehow already got an SSA_NAME on the LHS, then
4363 we've probably modified it twice. Not good. */
4364 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
4365 *to_p = make_ssa_name (*to_p, assign);
4366 gimple_set_lhs (assign, *to_p);
4369 if (want_value)
4371 *expr_p = unshare_expr (*to_p);
4372 return GS_OK;
4374 else
4375 *expr_p = NULL;
4377 return GS_ALL_DONE;
4380 /* Gimplify a comparison between two variable-sized objects. Do this
4381 with a call to BUILT_IN_MEMCMP. */
4383 static enum gimplify_status
4384 gimplify_variable_sized_compare (tree *expr_p)
4386 tree op0 = TREE_OPERAND (*expr_p, 0);
4387 tree op1 = TREE_OPERAND (*expr_p, 1);
4388 tree t, arg, dest, src;
4390 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4391 arg = unshare_expr (arg);
4392 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4393 src = build_fold_addr_expr (op1);
4394 dest = build_fold_addr_expr (op0);
4395 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
4396 t = build_call_expr (t, 3, dest, src, arg);
4397 *expr_p
4398 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4400 return GS_OK;
4403 /* Gimplify a comparison between two aggregate objects of integral scalar
4404 mode as a comparison between the bitwise equivalent scalar values. */
4406 static enum gimplify_status
4407 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4409 tree op0 = TREE_OPERAND (*expr_p, 0);
4410 tree op1 = TREE_OPERAND (*expr_p, 1);
4412 tree type = TREE_TYPE (op0);
4413 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4415 op0 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op0);
4416 op1 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op1);
4418 *expr_p
4419 = fold_build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4421 return GS_OK;
4424 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
4425 points to the expression to gimplify.
4427 Expressions of the form 'a && b' are gimplified to:
4429 a && b ? true : false
4431 gimplify_cond_expr will do the rest.
4433 PRE_P points to the list where side effects that must happen before
4434 *EXPR_P should be stored. */
4436 static enum gimplify_status
4437 gimplify_boolean_expr (tree *expr_p)
4439 /* Preserve the original type of the expression. */
4440 tree type = TREE_TYPE (*expr_p);
4442 *expr_p = build3 (COND_EXPR, type, *expr_p,
4443 fold_convert (type, boolean_true_node),
4444 fold_convert (type, boolean_false_node));
4446 return GS_OK;
4449 /* Gimplifies an expression sequence. This function gimplifies each
4450 expression and re-writes the original expression with the last
4451 expression of the sequence in GIMPLE form.
4453 PRE_P points to the list where the side effects for all the
4454 expressions in the sequence will be emitted.
4456 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4458 static enum gimplify_status
4459 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4461 tree t = *expr_p;
4465 tree *sub_p = &TREE_OPERAND (t, 0);
4467 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4468 gimplify_compound_expr (sub_p, pre_p, false);
4469 else
4470 gimplify_stmt (sub_p, pre_p);
4472 t = TREE_OPERAND (t, 1);
4474 while (TREE_CODE (t) == COMPOUND_EXPR);
4476 *expr_p = t;
4477 if (want_value)
4478 return GS_OK;
4479 else
4481 gimplify_stmt (expr_p, pre_p);
4482 return GS_ALL_DONE;
4487 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4488 gimplify. After gimplification, EXPR_P will point to a new temporary
4489 that holds the original value of the SAVE_EXPR node.
4491 PRE_P points to the list where side effects that must happen before
4492 *EXPR_P should be stored. */
4494 static enum gimplify_status
4495 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4497 enum gimplify_status ret = GS_ALL_DONE;
4498 tree val;
4500 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4501 val = TREE_OPERAND (*expr_p, 0);
4503 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4504 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4506 /* The operand may be a void-valued expression such as SAVE_EXPRs
4507 generated by the Java frontend for class initialization. It is
4508 being executed only for its side-effects. */
4509 if (TREE_TYPE (val) == void_type_node)
4511 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4512 is_gimple_stmt, fb_none);
4513 val = NULL;
4515 else
4516 val = get_initialized_tmp_var (val, pre_p, post_p);
4518 TREE_OPERAND (*expr_p, 0) = val;
4519 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4522 *expr_p = val;
4524 return ret;
4527 /* Re-write the ADDR_EXPR node pointed to by EXPR_P
4529 unary_expr
4530 : ...
4531 | '&' varname
4534 PRE_P points to the list where side effects that must happen before
4535 *EXPR_P should be stored.
4537 POST_P points to the list where side effects that must happen after
4538 *EXPR_P should be stored. */
4540 static enum gimplify_status
4541 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4543 tree expr = *expr_p;
4544 tree op0 = TREE_OPERAND (expr, 0);
4545 enum gimplify_status ret;
4547 switch (TREE_CODE (op0))
4549 case INDIRECT_REF:
4550 case MISALIGNED_INDIRECT_REF:
4551 do_indirect_ref:
4552 /* Check if we are dealing with an expression of the form '&*ptr'.
4553 While the front end folds away '&*ptr' into 'ptr', these
4554 expressions may be generated internally by the compiler (e.g.,
4555 builtins like __builtin_va_end). */
4556 /* Caution: the silent array decomposition semantics we allow for
4557 ADDR_EXPR means we can't always discard the pair. */
4558 /* Gimplification of the ADDR_EXPR operand may drop
4559 cv-qualification conversions, so make sure we add them if
4560 needed. */
4562 tree op00 = TREE_OPERAND (op0, 0);
4563 tree t_expr = TREE_TYPE (expr);
4564 tree t_op00 = TREE_TYPE (op00);
4566 if (!useless_type_conversion_p (t_expr, t_op00))
4567 op00 = fold_convert (TREE_TYPE (expr), op00);
4568 *expr_p = op00;
4569 ret = GS_OK;
4571 break;
4573 case VIEW_CONVERT_EXPR:
4574 /* Take the address of our operand and then convert it to the type of
4575 this ADDR_EXPR.
4577 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4578 all clear. The impact of this transformation is even less clear. */
4580 /* If the operand is a useless conversion, look through it. Doing so
4581 guarantees that the ADDR_EXPR and its operand will remain of the
4582 same type. */
4583 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4584 op0 = TREE_OPERAND (op0, 0);
4586 *expr_p = fold_convert (TREE_TYPE (expr),
4587 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
4588 ret = GS_OK;
4589 break;
4591 default:
4592 /* We use fb_either here because the C frontend sometimes takes
4593 the address of a call that returns a struct; see
4594 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4595 the implied temporary explicit. */
4597 /* Mark the RHS addressable. */
4598 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4599 is_gimple_addressable, fb_either);
4600 if (ret == GS_ERROR)
4601 break;
4603 /* We cannot rely on making the RHS addressable if it is
4604 a temporary created by gimplification. In this case create a
4605 new temporary that is initialized by a copy (which will
4606 become a store after we mark it addressable).
4607 This mostly happens if the frontend passed us something that
4608 it could not mark addressable yet, like a fortran
4609 pass-by-reference parameter (int) floatvar. */
4610 if (is_gimple_reg (TREE_OPERAND (expr, 0)))
4611 TREE_OPERAND (expr, 0)
4612 = get_initialized_tmp_var (TREE_OPERAND (expr, 0), pre_p, post_p);
4614 op0 = TREE_OPERAND (expr, 0);
4616 /* For various reasons, the gimplification of the expression
4617 may have made a new INDIRECT_REF. */
4618 if (TREE_CODE (op0) == INDIRECT_REF)
4619 goto do_indirect_ref;
4621 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4622 recompute_tree_invariant_for_addr_expr (expr);
4624 mark_addressable (TREE_OPERAND (expr, 0));
4625 break;
4628 return ret;
4631 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4632 value; output operands should be a gimple lvalue. */
4634 static enum gimplify_status
4635 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4637 tree expr;
4638 int noutputs;
4639 const char **oconstraints;
4640 int i;
4641 tree link;
4642 const char *constraint;
4643 bool allows_mem, allows_reg, is_inout;
4644 enum gimplify_status ret, tret;
4645 gimple stmt;
4646 VEC(tree, gc) *inputs;
4647 VEC(tree, gc) *outputs;
4648 VEC(tree, gc) *clobbers;
4649 tree link_next;
4651 expr = *expr_p;
4652 noutputs = list_length (ASM_OUTPUTS (expr));
4653 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
4655 inputs = outputs = clobbers = NULL;
4657 ret = GS_ALL_DONE;
4658 link_next = NULL_TREE;
4659 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
4661 bool ok;
4662 size_t constraint_len;
4664 link_next = TREE_CHAIN (link);
4666 oconstraints[i]
4667 = constraint
4668 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4669 constraint_len = strlen (constraint);
4670 if (constraint_len == 0)
4671 continue;
4673 ok = parse_output_constraint (&constraint, i, 0, 0,
4674 &allows_mem, &allows_reg, &is_inout);
4675 if (!ok)
4677 ret = GS_ERROR;
4678 is_inout = false;
4681 if (!allows_reg && allows_mem)
4682 mark_addressable (TREE_VALUE (link));
4684 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4685 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4686 fb_lvalue | fb_mayfail);
4687 if (tret == GS_ERROR)
4689 error ("invalid lvalue in asm output %d", i);
4690 ret = tret;
4693 VEC_safe_push (tree, gc, outputs, link);
4694 TREE_CHAIN (link) = NULL_TREE;
4696 if (is_inout)
4698 /* An input/output operand. To give the optimizers more
4699 flexibility, split it into separate input and output
4700 operands. */
4701 tree input;
4702 char buf[10];
4704 /* Turn the in/out constraint into an output constraint. */
4705 char *p = xstrdup (constraint);
4706 p[0] = '=';
4707 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4709 /* And add a matching input constraint. */
4710 if (allows_reg)
4712 sprintf (buf, "%d", i);
4714 /* If there are multiple alternatives in the constraint,
4715 handle each of them individually. Those that allow register
4716 will be replaced with operand number, the others will stay
4717 unchanged. */
4718 if (strchr (p, ',') != NULL)
4720 size_t len = 0, buflen = strlen (buf);
4721 char *beg, *end, *str, *dst;
4723 for (beg = p + 1;;)
4725 end = strchr (beg, ',');
4726 if (end == NULL)
4727 end = strchr (beg, '\0');
4728 if ((size_t) (end - beg) < buflen)
4729 len += buflen + 1;
4730 else
4731 len += end - beg + 1;
4732 if (*end)
4733 beg = end + 1;
4734 else
4735 break;
4738 str = (char *) alloca (len);
4739 for (beg = p + 1, dst = str;;)
4741 const char *tem;
4742 bool mem_p, reg_p, inout_p;
4744 end = strchr (beg, ',');
4745 if (end)
4746 *end = '\0';
4747 beg[-1] = '=';
4748 tem = beg - 1;
4749 parse_output_constraint (&tem, i, 0, 0,
4750 &mem_p, &reg_p, &inout_p);
4751 if (dst != str)
4752 *dst++ = ',';
4753 if (reg_p)
4755 memcpy (dst, buf, buflen);
4756 dst += buflen;
4758 else
4760 if (end)
4761 len = end - beg;
4762 else
4763 len = strlen (beg);
4764 memcpy (dst, beg, len);
4765 dst += len;
4767 if (end)
4768 beg = end + 1;
4769 else
4770 break;
4772 *dst = '\0';
4773 input = build_string (dst - str, str);
4775 else
4776 input = build_string (strlen (buf), buf);
4778 else
4779 input = build_string (constraint_len - 1, constraint + 1);
4781 free (p);
4783 input = build_tree_list (build_tree_list (NULL_TREE, input),
4784 unshare_expr (TREE_VALUE (link)));
4785 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4789 link_next = NULL_TREE;
4790 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
4792 link_next = TREE_CHAIN (link);
4793 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4794 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4795 oconstraints, &allows_mem, &allows_reg);
4797 /* If we can't make copies, we can only accept memory. */
4798 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
4800 if (allows_mem)
4801 allows_reg = 0;
4802 else
4804 error ("impossible constraint in %<asm%>");
4805 error ("non-memory input %d must stay in memory", i);
4806 return GS_ERROR;
4810 /* If the operand is a memory input, it should be an lvalue. */
4811 if (!allows_reg && allows_mem)
4813 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4814 is_gimple_lvalue, fb_lvalue | fb_mayfail);
4815 mark_addressable (TREE_VALUE (link));
4816 if (tret == GS_ERROR)
4818 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
4819 input_location = EXPR_LOCATION (TREE_VALUE (link));
4820 error ("memory input %d is not directly addressable", i);
4821 ret = tret;
4824 else
4826 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4827 is_gimple_asm_val, fb_rvalue);
4828 if (tret == GS_ERROR)
4829 ret = tret;
4832 TREE_CHAIN (link) = NULL_TREE;
4833 VEC_safe_push (tree, gc, inputs, link);
4836 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
4837 VEC_safe_push (tree, gc, clobbers, link);
4839 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
4840 inputs, outputs, clobbers);
4842 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
4843 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
4845 gimplify_seq_add_stmt (pre_p, stmt);
4847 return ret;
4850 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
4851 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
4852 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
4853 return to this function.
4855 FIXME should we complexify the prequeue handling instead? Or use flags
4856 for all the cleanups and let the optimizer tighten them up? The current
4857 code seems pretty fragile; it will break on a cleanup within any
4858 non-conditional nesting. But any such nesting would be broken, anyway;
4859 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
4860 and continues out of it. We can do that at the RTL level, though, so
4861 having an optimizer to tighten up try/finally regions would be a Good
4862 Thing. */
4864 static enum gimplify_status
4865 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
4867 gimple_stmt_iterator iter;
4868 gimple_seq body_sequence = NULL;
4870 tree temp = voidify_wrapper_expr (*expr_p, NULL);
4872 /* We only care about the number of conditions between the innermost
4873 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
4874 any cleanups collected outside the CLEANUP_POINT_EXPR. */
4875 int old_conds = gimplify_ctxp->conditions;
4876 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
4877 gimplify_ctxp->conditions = 0;
4878 gimplify_ctxp->conditional_cleanups = NULL;
4880 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
4882 gimplify_ctxp->conditions = old_conds;
4883 gimplify_ctxp->conditional_cleanups = old_cleanups;
4885 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
4887 gimple wce = gsi_stmt (iter);
4889 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
4891 if (gsi_one_before_end_p (iter))
4893 /* Note that gsi_insert_seq_before and gsi_remove do not
4894 scan operands, unlike some other sequence mutators. */
4895 gsi_insert_seq_before_without_update (&iter,
4896 gimple_wce_cleanup (wce),
4897 GSI_SAME_STMT);
4898 gsi_remove (&iter, true);
4899 break;
4901 else
4903 gimple gtry;
4904 gimple_seq seq;
4905 enum gimple_try_flags kind;
4907 if (gimple_wce_cleanup_eh_only (wce))
4908 kind = GIMPLE_TRY_CATCH;
4909 else
4910 kind = GIMPLE_TRY_FINALLY;
4911 seq = gsi_split_seq_after (iter);
4913 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
4914 /* Do not use gsi_replace here, as it may scan operands.
4915 We want to do a simple structural modification only. */
4916 *gsi_stmt_ptr (&iter) = gtry;
4917 iter = gsi_start (seq);
4920 else
4921 gsi_next (&iter);
4924 gimplify_seq_add_seq (pre_p, body_sequence);
4925 if (temp)
4927 *expr_p = temp;
4928 return GS_OK;
4930 else
4932 *expr_p = NULL;
4933 return GS_ALL_DONE;
4937 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
4938 is the cleanup action required. EH_ONLY is true if the cleanup should
4939 only be executed if an exception is thrown, not on normal exit. */
4941 static void
4942 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
4944 gimple wce;
4945 gimple_seq cleanup_stmts = NULL;
4947 /* Errors can result in improperly nested cleanups. Which results in
4948 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
4949 if (errorcount || sorrycount)
4950 return;
4952 if (gimple_conditional_context ())
4954 /* If we're in a conditional context, this is more complex. We only
4955 want to run the cleanup if we actually ran the initialization that
4956 necessitates it, but we want to run it after the end of the
4957 conditional context. So we wrap the try/finally around the
4958 condition and use a flag to determine whether or not to actually
4959 run the destructor. Thus
4961 test ? f(A()) : 0
4963 becomes (approximately)
4965 flag = 0;
4966 try {
4967 if (test) { A::A(temp); flag = 1; val = f(temp); }
4968 else { val = 0; }
4969 } finally {
4970 if (flag) A::~A(temp);
4974 tree flag = create_tmp_var (boolean_type_node, "cleanup");
4975 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
4976 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
4978 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
4979 gimplify_stmt (&cleanup, &cleanup_stmts);
4980 wce = gimple_build_wce (cleanup_stmts);
4982 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
4983 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
4984 gimplify_seq_add_stmt (pre_p, ftrue);
4986 /* Because of this manipulation, and the EH edges that jump
4987 threading cannot redirect, the temporary (VAR) will appear
4988 to be used uninitialized. Don't warn. */
4989 TREE_NO_WARNING (var) = 1;
4991 else
4993 gimplify_stmt (&cleanup, &cleanup_stmts);
4994 wce = gimple_build_wce (cleanup_stmts);
4995 gimple_wce_set_cleanup_eh_only (wce, eh_only);
4996 gimplify_seq_add_stmt (pre_p, wce);
5000 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5002 static enum gimplify_status
5003 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5005 tree targ = *expr_p;
5006 tree temp = TARGET_EXPR_SLOT (targ);
5007 tree init = TARGET_EXPR_INITIAL (targ);
5008 enum gimplify_status ret;
5010 if (init)
5012 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5013 to the temps list. Handle also variable length TARGET_EXPRs. */
5014 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5016 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5017 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5018 gimplify_vla_decl (temp, pre_p);
5020 else
5021 gimple_add_tmp_var (temp);
5023 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5024 expression is supposed to initialize the slot. */
5025 if (VOID_TYPE_P (TREE_TYPE (init)))
5026 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5027 else
5029 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5030 init = init_expr;
5031 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5032 init = NULL;
5033 ggc_free (init_expr);
5035 if (ret == GS_ERROR)
5037 /* PR c++/28266 Make sure this is expanded only once. */
5038 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5039 return GS_ERROR;
5041 if (init)
5042 gimplify_and_add (init, pre_p);
5044 /* If needed, push the cleanup for the temp. */
5045 if (TARGET_EXPR_CLEANUP (targ))
5046 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5047 CLEANUP_EH_ONLY (targ), pre_p);
5049 /* Only expand this once. */
5050 TREE_OPERAND (targ, 3) = init;
5051 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5053 else
5054 /* We should have expanded this before. */
5055 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5057 *expr_p = temp;
5058 return GS_OK;
5061 /* Gimplification of expression trees. */
5063 /* Gimplify an expression which appears at statement context. The
5064 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5065 NULL, a new sequence is allocated.
5067 Return true if we actually added a statement to the queue. */
5069 bool
5070 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5072 gimple_seq_node last;
5074 if (!*seq_p)
5075 *seq_p = gimple_seq_alloc ();
5077 last = gimple_seq_last (*seq_p);
5078 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5079 return last != gimple_seq_last (*seq_p);
5083 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5084 to CTX. If entries already exist, force them to be some flavor of private.
5085 If there is no enclosing parallel, do nothing. */
5087 void
5088 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5090 splay_tree_node n;
5092 if (decl == NULL || !DECL_P (decl))
5093 return;
5097 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5098 if (n != NULL)
5100 if (n->value & GOVD_SHARED)
5101 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5102 else
5103 return;
5105 else if (ctx->region_type != ORT_WORKSHARE)
5106 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5108 ctx = ctx->outer_context;
5110 while (ctx);
5113 /* Similarly for each of the type sizes of TYPE. */
5115 static void
5116 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5118 if (type == NULL || type == error_mark_node)
5119 return;
5120 type = TYPE_MAIN_VARIANT (type);
5122 if (pointer_set_insert (ctx->privatized_types, type))
5123 return;
5125 switch (TREE_CODE (type))
5127 case INTEGER_TYPE:
5128 case ENUMERAL_TYPE:
5129 case BOOLEAN_TYPE:
5130 case REAL_TYPE:
5131 case FIXED_POINT_TYPE:
5132 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5133 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5134 break;
5136 case ARRAY_TYPE:
5137 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5138 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5139 break;
5141 case RECORD_TYPE:
5142 case UNION_TYPE:
5143 case QUAL_UNION_TYPE:
5145 tree field;
5146 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5147 if (TREE_CODE (field) == FIELD_DECL)
5149 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5150 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5153 break;
5155 case POINTER_TYPE:
5156 case REFERENCE_TYPE:
5157 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5158 break;
5160 default:
5161 break;
5164 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5165 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5166 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5169 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5171 static void
5172 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5174 splay_tree_node n;
5175 unsigned int nflags;
5176 tree t;
5178 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
5179 return;
5181 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5182 there are constructors involved somewhere. */
5183 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5184 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5185 flags |= GOVD_SEEN;
5187 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5188 if (n != NULL)
5190 /* We shouldn't be re-adding the decl with the same data
5191 sharing class. */
5192 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5193 /* The only combination of data sharing classes we should see is
5194 FIRSTPRIVATE and LASTPRIVATE. */
5195 nflags = n->value | flags;
5196 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5197 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5198 n->value = nflags;
5199 return;
5202 /* When adding a variable-sized variable, we have to handle all sorts
5203 of additional bits of data: the pointer replacement variable, and
5204 the parameters of the type. */
5205 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5207 /* Add the pointer replacement variable as PRIVATE if the variable
5208 replacement is private, else FIRSTPRIVATE since we'll need the
5209 address of the original variable either for SHARED, or for the
5210 copy into or out of the context. */
5211 if (!(flags & GOVD_LOCAL))
5213 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5214 nflags |= flags & GOVD_SEEN;
5215 t = DECL_VALUE_EXPR (decl);
5216 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5217 t = TREE_OPERAND (t, 0);
5218 gcc_assert (DECL_P (t));
5219 omp_add_variable (ctx, t, nflags);
5222 /* Add all of the variable and type parameters (which should have
5223 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5224 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5225 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5226 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5228 /* The variable-sized variable itself is never SHARED, only some form
5229 of PRIVATE. The sharing would take place via the pointer variable
5230 which we remapped above. */
5231 if (flags & GOVD_SHARED)
5232 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5233 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5235 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5236 alloca statement we generate for the variable, so make sure it
5237 is available. This isn't automatically needed for the SHARED
5238 case, since we won't be allocating local storage then.
5239 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5240 in this case omp_notice_variable will be called later
5241 on when it is gimplified. */
5242 else if (! (flags & GOVD_LOCAL))
5243 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5245 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5247 gcc_assert ((flags & GOVD_LOCAL) == 0);
5248 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5250 /* Similar to the direct variable sized case above, we'll need the
5251 size of references being privatized. */
5252 if ((flags & GOVD_SHARED) == 0)
5254 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5255 if (TREE_CODE (t) != INTEGER_CST)
5256 omp_notice_variable (ctx, t, true);
5260 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5263 /* Record the fact that DECL was used within the OpenMP context CTX.
5264 IN_CODE is true when real code uses DECL, and false when we should
5265 merely emit default(none) errors. Return true if DECL is going to
5266 be remapped and thus DECL shouldn't be gimplified into its
5267 DECL_VALUE_EXPR (if any). */
5269 static bool
5270 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5272 splay_tree_node n;
5273 unsigned flags = in_code ? GOVD_SEEN : 0;
5274 bool ret = false, shared;
5276 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
5277 return false;
5279 /* Threadprivate variables are predetermined. */
5280 if (is_global_var (decl))
5282 if (DECL_THREAD_LOCAL_P (decl))
5283 return false;
5285 if (DECL_HAS_VALUE_EXPR_P (decl))
5287 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5289 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5290 return false;
5294 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5295 if (n == NULL)
5297 enum omp_clause_default_kind default_kind, kind;
5298 struct gimplify_omp_ctx *octx;
5300 if (ctx->region_type == ORT_WORKSHARE)
5301 goto do_outer;
5303 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5304 remapped firstprivate instead of shared. To some extent this is
5305 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5306 default_kind = ctx->default_kind;
5307 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5308 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5309 default_kind = kind;
5311 switch (default_kind)
5313 case OMP_CLAUSE_DEFAULT_NONE:
5314 error ("%qs not specified in enclosing parallel",
5315 IDENTIFIER_POINTER (DECL_NAME (decl)));
5316 error ("%Henclosing parallel", &ctx->location);
5317 /* FALLTHRU */
5318 case OMP_CLAUSE_DEFAULT_SHARED:
5319 flags |= GOVD_SHARED;
5320 break;
5321 case OMP_CLAUSE_DEFAULT_PRIVATE:
5322 flags |= GOVD_PRIVATE;
5323 break;
5324 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5325 flags |= GOVD_FIRSTPRIVATE;
5326 break;
5327 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5328 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5329 gcc_assert (ctx->region_type == ORT_TASK);
5330 if (ctx->outer_context)
5331 omp_notice_variable (ctx->outer_context, decl, in_code);
5332 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5334 splay_tree_node n2;
5336 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5337 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5339 flags |= GOVD_FIRSTPRIVATE;
5340 break;
5342 if ((octx->region_type & ORT_PARALLEL) != 0)
5343 break;
5345 if (flags & GOVD_FIRSTPRIVATE)
5346 break;
5347 if (octx == NULL
5348 && (TREE_CODE (decl) == PARM_DECL
5349 || (!is_global_var (decl)
5350 && DECL_CONTEXT (decl) == current_function_decl)))
5352 flags |= GOVD_FIRSTPRIVATE;
5353 break;
5355 flags |= GOVD_SHARED;
5356 break;
5357 default:
5358 gcc_unreachable ();
5361 if ((flags & GOVD_PRIVATE)
5362 && lang_hooks.decls.omp_private_outer_ref (decl))
5363 flags |= GOVD_PRIVATE_OUTER_REF;
5365 omp_add_variable (ctx, decl, flags);
5367 shared = (flags & GOVD_SHARED) != 0;
5368 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5369 goto do_outer;
5372 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5373 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5374 && DECL_SIZE (decl)
5375 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5377 splay_tree_node n2;
5378 tree t = DECL_VALUE_EXPR (decl);
5379 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5380 t = TREE_OPERAND (t, 0);
5381 gcc_assert (DECL_P (t));
5382 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5383 n2->value |= GOVD_SEEN;
5386 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5387 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5389 /* If nothing changed, there's nothing left to do. */
5390 if ((n->value & flags) == flags)
5391 return ret;
5392 flags |= n->value;
5393 n->value = flags;
5395 do_outer:
5396 /* If the variable is private in the current context, then we don't
5397 need to propagate anything to an outer context. */
5398 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5399 return ret;
5400 if (ctx->outer_context
5401 && omp_notice_variable (ctx->outer_context, decl, in_code))
5402 return true;
5403 return ret;
5406 /* Verify that DECL is private within CTX. If there's specific information
5407 to the contrary in the innermost scope, generate an error. */
5409 static bool
5410 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
5412 splay_tree_node n;
5414 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5415 if (n != NULL)
5417 if (n->value & GOVD_SHARED)
5419 if (ctx == gimplify_omp_ctxp)
5421 error ("iteration variable %qs should be private",
5422 IDENTIFIER_POINTER (DECL_NAME (decl)));
5423 n->value = GOVD_PRIVATE;
5424 return true;
5426 else
5427 return false;
5429 else if ((n->value & GOVD_EXPLICIT) != 0
5430 && (ctx == gimplify_omp_ctxp
5431 || (ctx->region_type == ORT_COMBINED_PARALLEL
5432 && gimplify_omp_ctxp->outer_context == ctx)))
5434 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5435 error ("iteration variable %qs should not be firstprivate",
5436 IDENTIFIER_POINTER (DECL_NAME (decl)));
5437 else if ((n->value & GOVD_REDUCTION) != 0)
5438 error ("iteration variable %qs should not be reduction",
5439 IDENTIFIER_POINTER (DECL_NAME (decl)));
5441 return (ctx == gimplify_omp_ctxp
5442 || (ctx->region_type == ORT_COMBINED_PARALLEL
5443 && gimplify_omp_ctxp->outer_context == ctx));
5446 if (ctx->region_type != ORT_WORKSHARE)
5447 return false;
5448 else if (ctx->outer_context)
5449 return omp_is_private (ctx->outer_context, decl);
5450 return false;
5453 /* Return true if DECL is private within a parallel region
5454 that binds to the current construct's context or in parallel
5455 region's REDUCTION clause. */
5457 static bool
5458 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
5460 splay_tree_node n;
5464 ctx = ctx->outer_context;
5465 if (ctx == NULL)
5466 return !(is_global_var (decl)
5467 /* References might be private, but might be shared too. */
5468 || lang_hooks.decls.omp_privatize_by_reference (decl));
5470 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5471 if (n != NULL)
5472 return (n->value & GOVD_SHARED) == 0;
5474 while (ctx->region_type == ORT_WORKSHARE);
5475 return false;
5478 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5479 and previous omp contexts. */
5481 static void
5482 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5483 enum omp_region_type region_type)
5485 struct gimplify_omp_ctx *ctx, *outer_ctx;
5486 struct gimplify_ctx gctx;
5487 tree c;
5489 ctx = new_omp_context (region_type);
5490 outer_ctx = ctx->outer_context;
5492 while ((c = *list_p) != NULL)
5494 bool remove = false;
5495 bool notice_outer = true;
5496 const char *check_non_private = NULL;
5497 unsigned int flags;
5498 tree decl;
5500 switch (OMP_CLAUSE_CODE (c))
5502 case OMP_CLAUSE_PRIVATE:
5503 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5504 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5506 flags |= GOVD_PRIVATE_OUTER_REF;
5507 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5509 else
5510 notice_outer = false;
5511 goto do_add;
5512 case OMP_CLAUSE_SHARED:
5513 flags = GOVD_SHARED | GOVD_EXPLICIT;
5514 goto do_add;
5515 case OMP_CLAUSE_FIRSTPRIVATE:
5516 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5517 check_non_private = "firstprivate";
5518 goto do_add;
5519 case OMP_CLAUSE_LASTPRIVATE:
5520 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5521 check_non_private = "lastprivate";
5522 goto do_add;
5523 case OMP_CLAUSE_REDUCTION:
5524 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5525 check_non_private = "reduction";
5526 goto do_add;
5528 do_add:
5529 decl = OMP_CLAUSE_DECL (c);
5530 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
5532 remove = true;
5533 break;
5535 omp_add_variable (ctx, decl, flags);
5536 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5537 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5539 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
5540 GOVD_LOCAL | GOVD_SEEN);
5541 gimplify_omp_ctxp = ctx;
5542 push_gimplify_context (&gctx);
5544 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = gimple_seq_alloc ();
5545 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = gimple_seq_alloc ();
5547 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
5548 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
5549 pop_gimplify_context
5550 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
5551 push_gimplify_context (&gctx);
5552 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
5553 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
5554 pop_gimplify_context
5555 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
5556 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
5557 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
5559 gimplify_omp_ctxp = outer_ctx;
5561 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5562 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
5564 gimplify_omp_ctxp = ctx;
5565 push_gimplify_context (&gctx);
5566 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
5568 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
5569 NULL, NULL);
5570 TREE_SIDE_EFFECTS (bind) = 1;
5571 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
5572 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
5574 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
5575 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
5576 pop_gimplify_context
5577 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
5578 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
5580 gimplify_omp_ctxp = outer_ctx;
5582 if (notice_outer)
5583 goto do_notice;
5584 break;
5586 case OMP_CLAUSE_COPYIN:
5587 case OMP_CLAUSE_COPYPRIVATE:
5588 decl = OMP_CLAUSE_DECL (c);
5589 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
5591 remove = true;
5592 break;
5594 do_notice:
5595 if (outer_ctx)
5596 omp_notice_variable (outer_ctx, decl, true);
5597 if (check_non_private
5598 && region_type == ORT_WORKSHARE
5599 && omp_check_private (ctx, decl))
5601 error ("%s variable %qs is private in outer context",
5602 check_non_private, IDENTIFIER_POINTER (DECL_NAME (decl)));
5603 remove = true;
5605 break;
5607 case OMP_CLAUSE_IF:
5608 OMP_CLAUSE_OPERAND (c, 0)
5609 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
5610 /* Fall through. */
5612 case OMP_CLAUSE_SCHEDULE:
5613 case OMP_CLAUSE_NUM_THREADS:
5614 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
5615 is_gimple_val, fb_rvalue) == GS_ERROR)
5616 remove = true;
5617 break;
5619 case OMP_CLAUSE_NOWAIT:
5620 case OMP_CLAUSE_ORDERED:
5621 case OMP_CLAUSE_UNTIED:
5622 case OMP_CLAUSE_COLLAPSE:
5623 break;
5625 case OMP_CLAUSE_DEFAULT:
5626 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
5627 break;
5629 default:
5630 gcc_unreachable ();
5633 if (remove)
5634 *list_p = OMP_CLAUSE_CHAIN (c);
5635 else
5636 list_p = &OMP_CLAUSE_CHAIN (c);
5639 gimplify_omp_ctxp = ctx;
5642 /* For all variables that were not actually used within the context,
5643 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
5645 static int
5646 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
5648 tree *list_p = (tree *) data;
5649 tree decl = (tree) n->key;
5650 unsigned flags = n->value;
5651 enum omp_clause_code code;
5652 tree clause;
5653 bool private_debug;
5655 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
5656 return 0;
5657 if ((flags & GOVD_SEEN) == 0)
5658 return 0;
5659 if (flags & GOVD_DEBUG_PRIVATE)
5661 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
5662 private_debug = true;
5664 else
5665 private_debug
5666 = lang_hooks.decls.omp_private_debug_clause (decl,
5667 !!(flags & GOVD_SHARED));
5668 if (private_debug)
5669 code = OMP_CLAUSE_PRIVATE;
5670 else if (flags & GOVD_SHARED)
5672 if (is_global_var (decl))
5674 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
5675 while (ctx != NULL)
5677 splay_tree_node on
5678 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5679 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
5680 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
5681 break;
5682 ctx = ctx->outer_context;
5684 if (ctx == NULL)
5685 return 0;
5687 code = OMP_CLAUSE_SHARED;
5689 else if (flags & GOVD_PRIVATE)
5690 code = OMP_CLAUSE_PRIVATE;
5691 else if (flags & GOVD_FIRSTPRIVATE)
5692 code = OMP_CLAUSE_FIRSTPRIVATE;
5693 else
5694 gcc_unreachable ();
5696 clause = build_omp_clause (code);
5697 OMP_CLAUSE_DECL (clause) = decl;
5698 OMP_CLAUSE_CHAIN (clause) = *list_p;
5699 if (private_debug)
5700 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
5701 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
5702 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
5703 *list_p = clause;
5704 lang_hooks.decls.omp_finish_clause (clause);
5706 return 0;
5709 static void
5710 gimplify_adjust_omp_clauses (tree *list_p)
5712 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
5713 tree c, decl;
5715 while ((c = *list_p) != NULL)
5717 splay_tree_node n;
5718 bool remove = false;
5720 switch (OMP_CLAUSE_CODE (c))
5722 case OMP_CLAUSE_PRIVATE:
5723 case OMP_CLAUSE_SHARED:
5724 case OMP_CLAUSE_FIRSTPRIVATE:
5725 decl = OMP_CLAUSE_DECL (c);
5726 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5727 remove = !(n->value & GOVD_SEEN);
5728 if (! remove)
5730 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
5731 if ((n->value & GOVD_DEBUG_PRIVATE)
5732 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
5734 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
5735 || ((n->value & GOVD_DATA_SHARE_CLASS)
5736 == GOVD_PRIVATE));
5737 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
5738 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
5741 break;
5743 case OMP_CLAUSE_LASTPRIVATE:
5744 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
5745 accurately reflect the presence of a FIRSTPRIVATE clause. */
5746 decl = OMP_CLAUSE_DECL (c);
5747 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5748 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
5749 = (n->value & GOVD_FIRSTPRIVATE) != 0;
5750 break;
5752 case OMP_CLAUSE_REDUCTION:
5753 case OMP_CLAUSE_COPYIN:
5754 case OMP_CLAUSE_COPYPRIVATE:
5755 case OMP_CLAUSE_IF:
5756 case OMP_CLAUSE_NUM_THREADS:
5757 case OMP_CLAUSE_SCHEDULE:
5758 case OMP_CLAUSE_NOWAIT:
5759 case OMP_CLAUSE_ORDERED:
5760 case OMP_CLAUSE_DEFAULT:
5761 case OMP_CLAUSE_UNTIED:
5762 case OMP_CLAUSE_COLLAPSE:
5763 break;
5765 default:
5766 gcc_unreachable ();
5769 if (remove)
5770 *list_p = OMP_CLAUSE_CHAIN (c);
5771 else
5772 list_p = &OMP_CLAUSE_CHAIN (c);
5775 /* Add in any implicit data sharing. */
5776 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
5778 gimplify_omp_ctxp = ctx->outer_context;
5779 delete_omp_context (ctx);
5782 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
5783 gimplification of the body, as well as scanning the body for used
5784 variables. We need to do this scan now, because variable-sized
5785 decls will be decomposed during gimplification. */
5787 static void
5788 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
5790 tree expr = *expr_p;
5791 gimple g;
5792 gimple_seq body = NULL;
5793 struct gimplify_ctx gctx;
5795 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
5796 OMP_PARALLEL_COMBINED (expr)
5797 ? ORT_COMBINED_PARALLEL
5798 : ORT_PARALLEL);
5800 push_gimplify_context (&gctx);
5802 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
5803 if (gimple_code (g) == GIMPLE_BIND)
5804 pop_gimplify_context (g);
5805 else
5806 pop_gimplify_context (NULL);
5808 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
5810 g = gimple_build_omp_parallel (body,
5811 OMP_PARALLEL_CLAUSES (expr),
5812 NULL_TREE, NULL_TREE);
5813 if (OMP_PARALLEL_COMBINED (expr))
5814 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
5815 gimplify_seq_add_stmt (pre_p, g);
5816 *expr_p = NULL_TREE;
5819 /* Gimplify the contents of an OMP_TASK statement. This involves
5820 gimplification of the body, as well as scanning the body for used
5821 variables. We need to do this scan now, because variable-sized
5822 decls will be decomposed during gimplification. */
5824 static void
5825 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
5827 tree expr = *expr_p;
5828 gimple g;
5829 gimple_seq body = NULL;
5830 struct gimplify_ctx gctx;
5832 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p, ORT_TASK);
5834 push_gimplify_context (&gctx);
5836 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
5837 if (gimple_code (g) == GIMPLE_BIND)
5838 pop_gimplify_context (g);
5839 else
5840 pop_gimplify_context (NULL);
5842 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
5844 g = gimple_build_omp_task (body,
5845 OMP_TASK_CLAUSES (expr),
5846 NULL_TREE, NULL_TREE,
5847 NULL_TREE, NULL_TREE, NULL_TREE);
5848 gimplify_seq_add_stmt (pre_p, g);
5849 *expr_p = NULL_TREE;
5852 /* Gimplify the gross structure of an OMP_FOR statement. */
5854 static enum gimplify_status
5855 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
5857 tree for_stmt, decl, var, t;
5858 enum gimplify_status ret = GS_OK;
5859 gimple gfor;
5860 gimple_seq for_body, for_pre_body;
5861 int i;
5863 for_stmt = *expr_p;
5865 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
5866 ORT_WORKSHARE);
5868 /* Handle OMP_FOR_INIT. */
5869 for_pre_body = NULL;
5870 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
5871 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
5873 for_body = gimple_seq_alloc ();
5874 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
5875 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
5876 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
5877 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
5878 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
5880 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
5881 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
5882 decl = TREE_OPERAND (t, 0);
5883 gcc_assert (DECL_P (decl));
5884 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
5885 || POINTER_TYPE_P (TREE_TYPE (decl)));
5887 /* Make sure the iteration variable is private. */
5888 if (omp_is_private (gimplify_omp_ctxp, decl))
5889 omp_notice_variable (gimplify_omp_ctxp, decl, true);
5890 else
5891 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
5893 /* If DECL is not a gimple register, create a temporary variable to act
5894 as an iteration counter. This is valid, since DECL cannot be
5895 modified in the body of the loop. */
5896 if (!is_gimple_reg (decl))
5898 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
5899 TREE_OPERAND (t, 0) = var;
5901 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
5903 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
5905 else
5906 var = decl;
5908 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
5909 is_gimple_val, fb_rvalue);
5910 if (ret == GS_ERROR)
5911 return ret;
5913 /* Handle OMP_FOR_COND. */
5914 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
5915 gcc_assert (COMPARISON_CLASS_P (t));
5916 gcc_assert (TREE_OPERAND (t, 0) == decl);
5918 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
5919 is_gimple_val, fb_rvalue);
5921 /* Handle OMP_FOR_INCR. */
5922 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
5923 switch (TREE_CODE (t))
5925 case PREINCREMENT_EXPR:
5926 case POSTINCREMENT_EXPR:
5927 t = build_int_cst (TREE_TYPE (decl), 1);
5928 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
5929 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
5930 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
5931 break;
5933 case PREDECREMENT_EXPR:
5934 case POSTDECREMENT_EXPR:
5935 t = build_int_cst (TREE_TYPE (decl), -1);
5936 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
5937 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
5938 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
5939 break;
5941 case MODIFY_EXPR:
5942 gcc_assert (TREE_OPERAND (t, 0) == decl);
5943 TREE_OPERAND (t, 0) = var;
5945 t = TREE_OPERAND (t, 1);
5946 switch (TREE_CODE (t))
5948 case PLUS_EXPR:
5949 if (TREE_OPERAND (t, 1) == decl)
5951 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
5952 TREE_OPERAND (t, 0) = var;
5953 break;
5956 /* Fallthru. */
5957 case MINUS_EXPR:
5958 case POINTER_PLUS_EXPR:
5959 gcc_assert (TREE_OPERAND (t, 0) == decl);
5960 TREE_OPERAND (t, 0) = var;
5961 break;
5962 default:
5963 gcc_unreachable ();
5966 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
5967 is_gimple_val, fb_rvalue);
5968 break;
5970 default:
5971 gcc_unreachable ();
5974 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
5976 tree c;
5977 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
5978 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5979 && OMP_CLAUSE_DECL (c) == decl
5980 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
5982 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
5983 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
5984 gcc_assert (TREE_OPERAND (t, 0) == var);
5985 t = TREE_OPERAND (t, 1);
5986 gcc_assert (TREE_CODE (t) == PLUS_EXPR
5987 || TREE_CODE (t) == MINUS_EXPR
5988 || TREE_CODE (t) == POINTER_PLUS_EXPR);
5989 gcc_assert (TREE_OPERAND (t, 0) == var);
5990 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
5991 TREE_OPERAND (t, 1));
5992 gimplify_assign (decl, t,
5993 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
5998 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6000 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6002 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6003 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6004 for_pre_body);
6006 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6008 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6009 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6010 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6011 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6012 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6013 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6014 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6015 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6018 gimplify_seq_add_stmt (pre_p, gfor);
6019 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6022 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6023 In particular, OMP_SECTIONS and OMP_SINGLE. */
6025 static void
6026 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6028 tree expr = *expr_p;
6029 gimple stmt;
6030 gimple_seq body = NULL;
6032 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6033 gimplify_and_add (OMP_BODY (expr), &body);
6034 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6036 if (TREE_CODE (expr) == OMP_SECTIONS)
6037 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6038 else if (TREE_CODE (expr) == OMP_SINGLE)
6039 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6040 else
6041 gcc_unreachable ();
6043 gimplify_seq_add_stmt (pre_p, stmt);
6046 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6047 stabilized the lhs of the atomic operation as *ADDR. Return true if
6048 EXPR is this stabilized form. */
6050 static bool
6051 goa_lhs_expr_p (tree expr, tree addr)
6053 /* Also include casts to other type variants. The C front end is fond
6054 of adding these for e.g. volatile variables. This is like
6055 STRIP_TYPE_NOPS but includes the main variant lookup. */
6056 while ((CONVERT_EXPR_P (expr)
6057 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6058 && TREE_OPERAND (expr, 0) != error_mark_node
6059 && (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
6060 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))))
6061 expr = TREE_OPERAND (expr, 0);
6063 if (TREE_CODE (expr) == INDIRECT_REF)
6065 expr = TREE_OPERAND (expr, 0);
6066 while (expr != addr
6067 && (CONVERT_EXPR_P (expr)
6068 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6069 && TREE_CODE (expr) == TREE_CODE (addr)
6070 && TYPE_MAIN_VARIANT (TREE_TYPE (expr))
6071 == TYPE_MAIN_VARIANT (TREE_TYPE (addr)))
6073 expr = TREE_OPERAND (expr, 0);
6074 addr = TREE_OPERAND (addr, 0);
6076 if (expr == addr)
6077 return true;
6078 return (TREE_CODE (addr) == ADDR_EXPR
6079 && TREE_CODE (expr) == ADDR_EXPR
6080 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6082 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6083 return true;
6084 return false;
6087 /* Walk *EXPR_P and replace
6088 appearances of *LHS_ADDR with LHS_VAR. If an expression does not involve
6089 the lhs, evaluate it into a temporary. Return 1 if the lhs appeared as
6090 a subexpression, 0 if it did not, or -1 if an error was encountered. */
6092 static int
6093 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6094 tree lhs_var)
6096 tree expr = *expr_p;
6097 int saw_lhs;
6099 if (goa_lhs_expr_p (expr, lhs_addr))
6101 *expr_p = lhs_var;
6102 return 1;
6104 if (is_gimple_val (expr))
6105 return 0;
6107 saw_lhs = 0;
6108 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6110 case tcc_binary:
6111 case tcc_comparison:
6112 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6113 lhs_var);
6114 case tcc_unary:
6115 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6116 lhs_var);
6117 break;
6118 case tcc_expression:
6119 switch (TREE_CODE (expr))
6121 case TRUTH_ANDIF_EXPR:
6122 case TRUTH_ORIF_EXPR:
6123 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6124 lhs_addr, lhs_var);
6125 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6126 lhs_addr, lhs_var);
6127 break;
6128 default:
6129 break;
6131 break;
6132 default:
6133 break;
6136 if (saw_lhs == 0)
6138 enum gimplify_status gs;
6139 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6140 if (gs != GS_ALL_DONE)
6141 saw_lhs = -1;
6144 return saw_lhs;
6148 /* Gimplify an OMP_ATOMIC statement. */
6150 static enum gimplify_status
6151 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6153 tree addr = TREE_OPERAND (*expr_p, 0);
6154 tree rhs = TREE_OPERAND (*expr_p, 1);
6155 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6156 tree tmp_load;
6158 tmp_load = create_tmp_var (type, NULL);
6159 if (goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6160 return GS_ERROR;
6162 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6163 != GS_ALL_DONE)
6164 return GS_ERROR;
6166 gimplify_seq_add_stmt (pre_p, gimple_build_omp_atomic_load (tmp_load, addr));
6167 if (gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6168 != GS_ALL_DONE)
6169 return GS_ERROR;
6170 gimplify_seq_add_stmt (pre_p, gimple_build_omp_atomic_store (rhs));
6171 *expr_p = NULL;
6173 return GS_ALL_DONE;
6177 /* Converts the GENERIC expression tree *EXPR_P to GIMPLE. If the
6178 expression produces a value to be used as an operand inside a GIMPLE
6179 statement, the value will be stored back in *EXPR_P. This value will
6180 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6181 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6182 emitted in PRE_P and POST_P.
6184 Additionally, this process may overwrite parts of the input
6185 expression during gimplification. Ideally, it should be
6186 possible to do non-destructive gimplification.
6188 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6189 the expression needs to evaluate to a value to be used as
6190 an operand in a GIMPLE statement, this value will be stored in
6191 *EXPR_P on exit. This happens when the caller specifies one
6192 of fb_lvalue or fb_rvalue fallback flags.
6194 PRE_P will contain the sequence of GIMPLE statements corresponding
6195 to the evaluation of EXPR and all the side-effects that must
6196 be executed before the main expression. On exit, the last
6197 statement of PRE_P is the core statement being gimplified. For
6198 instance, when gimplifying 'if (++a)' the last statement in
6199 PRE_P will be 'if (t.1)' where t.1 is the result of
6200 pre-incrementing 'a'.
6202 POST_P will contain the sequence of GIMPLE statements corresponding
6203 to the evaluation of all the side-effects that must be executed
6204 after the main expression. If this is NULL, the post
6205 side-effects are stored at the end of PRE_P.
6207 The reason why the output is split in two is to handle post
6208 side-effects explicitly. In some cases, an expression may have
6209 inner and outer post side-effects which need to be emitted in
6210 an order different from the one given by the recursive
6211 traversal. For instance, for the expression (*p--)++ the post
6212 side-effects of '--' must actually occur *after* the post
6213 side-effects of '++'. However, gimplification will first visit
6214 the inner expression, so if a separate POST sequence was not
6215 used, the resulting sequence would be:
6217 1 t.1 = *p
6218 2 p = p - 1
6219 3 t.2 = t.1 + 1
6220 4 *p = t.2
6222 However, the post-decrement operation in line #2 must not be
6223 evaluated until after the store to *p at line #4, so the
6224 correct sequence should be:
6226 1 t.1 = *p
6227 2 t.2 = t.1 + 1
6228 3 *p = t.2
6229 4 p = p - 1
6231 So, by specifying a separate post queue, it is possible
6232 to emit the post side-effects in the correct order.
6233 If POST_P is NULL, an internal queue will be used. Before
6234 returning to the caller, the sequence POST_P is appended to
6235 the main output sequence PRE_P.
6237 GIMPLE_TEST_F points to a function that takes a tree T and
6238 returns nonzero if T is in the GIMPLE form requested by the
6239 caller. The GIMPLE predicates are in tree-gimple.c.
6241 FALLBACK tells the function what sort of a temporary we want if
6242 gimplification cannot produce an expression that complies with
6243 GIMPLE_TEST_F.
6245 fb_none means that no temporary should be generated
6246 fb_rvalue means that an rvalue is OK to generate
6247 fb_lvalue means that an lvalue is OK to generate
6248 fb_either means that either is OK, but an lvalue is preferable.
6249 fb_mayfail means that gimplification may fail (in which case
6250 GS_ERROR will be returned)
6252 The return value is either GS_ERROR or GS_ALL_DONE, since this
6253 function iterates until EXPR is completely gimplified or an error
6254 occurs. */
6256 enum gimplify_status
6257 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6258 bool (*gimple_test_f) (tree), fallback_t fallback)
6260 tree tmp;
6261 gimple_seq internal_pre = NULL;
6262 gimple_seq internal_post = NULL;
6263 tree save_expr;
6264 bool is_statement;
6265 location_t saved_location;
6266 enum gimplify_status ret;
6267 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6269 save_expr = *expr_p;
6270 if (save_expr == NULL_TREE)
6271 return GS_ALL_DONE;
6273 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6274 is_statement = gimple_test_f == is_gimple_stmt;
6275 if (is_statement)
6276 gcc_assert (pre_p);
6278 /* Consistency checks. */
6279 if (gimple_test_f == is_gimple_reg)
6280 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
6281 else if (gimple_test_f == is_gimple_val
6282 || gimple_test_f == is_gimple_call_addr
6283 || gimple_test_f == is_gimple_condexpr
6284 || gimple_test_f == is_gimple_mem_rhs
6285 || gimple_test_f == is_gimple_mem_rhs_or_call
6286 || gimple_test_f == is_gimple_reg_rhs
6287 || gimple_test_f == is_gimple_reg_rhs_or_call
6288 || gimple_test_f == is_gimple_asm_val)
6289 gcc_assert (fallback & fb_rvalue);
6290 else if (gimple_test_f == is_gimple_min_lval
6291 || gimple_test_f == is_gimple_lvalue)
6292 gcc_assert (fallback & fb_lvalue);
6293 else if (gimple_test_f == is_gimple_addressable)
6294 gcc_assert (fallback & fb_either);
6295 else if (gimple_test_f == is_gimple_stmt)
6296 gcc_assert (fallback == fb_none);
6297 else
6299 /* We should have recognized the GIMPLE_TEST_F predicate to
6300 know what kind of fallback to use in case a temporary is
6301 needed to hold the value or address of *EXPR_P. */
6302 gcc_unreachable ();
6305 /* We used to check the predicate here and return immediately if it
6306 succeeds. This is wrong; the design is for gimplification to be
6307 idempotent, and for the predicates to only test for valid forms, not
6308 whether they are fully simplified. */
6309 if (pre_p == NULL)
6310 pre_p = &internal_pre;
6312 if (post_p == NULL)
6313 post_p = &internal_post;
6315 /* Remember the last statements added to PRE_P and POST_P. Every
6316 new statement added by the gimplification helpers needs to be
6317 annotated with location information. To centralize the
6318 responsibility, we remember the last statement that had been
6319 added to both queues before gimplifying *EXPR_P. If
6320 gimplification produces new statements in PRE_P and POST_P, those
6321 statements will be annotated with the same location information
6322 as *EXPR_P. */
6323 pre_last_gsi = gsi_last (*pre_p);
6324 post_last_gsi = gsi_last (*post_p);
6326 saved_location = input_location;
6327 if (save_expr != error_mark_node
6328 && EXPR_HAS_LOCATION (*expr_p))
6329 input_location = EXPR_LOCATION (*expr_p);
6331 /* Loop over the specific gimplifiers until the toplevel node
6332 remains the same. */
6335 /* Strip away as many useless type conversions as possible
6336 at the toplevel. */
6337 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
6339 /* Remember the expr. */
6340 save_expr = *expr_p;
6342 /* Die, die, die, my darling. */
6343 if (save_expr == error_mark_node
6344 || (TREE_TYPE (save_expr)
6345 && TREE_TYPE (save_expr) == error_mark_node))
6347 ret = GS_ERROR;
6348 break;
6351 /* Do any language-specific gimplification. */
6352 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
6353 if (ret == GS_OK)
6355 if (*expr_p == NULL_TREE)
6356 break;
6357 if (*expr_p != save_expr)
6358 continue;
6360 else if (ret != GS_UNHANDLED)
6361 break;
6363 ret = GS_OK;
6364 switch (TREE_CODE (*expr_p))
6366 /* First deal with the special cases. */
6368 case POSTINCREMENT_EXPR:
6369 case POSTDECREMENT_EXPR:
6370 case PREINCREMENT_EXPR:
6371 case PREDECREMENT_EXPR:
6372 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
6373 fallback != fb_none);
6374 break;
6376 case ARRAY_REF:
6377 case ARRAY_RANGE_REF:
6378 case REALPART_EXPR:
6379 case IMAGPART_EXPR:
6380 case COMPONENT_REF:
6381 case VIEW_CONVERT_EXPR:
6382 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
6383 fallback ? fallback : fb_rvalue);
6384 break;
6386 case COND_EXPR:
6387 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
6389 /* C99 code may assign to an array in a structure value of a
6390 conditional expression, and this has undefined behavior
6391 only on execution, so create a temporary if an lvalue is
6392 required. */
6393 if (fallback == fb_lvalue)
6395 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6396 mark_addressable (*expr_p);
6398 break;
6400 case CALL_EXPR:
6401 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
6403 /* C99 code may assign to an array in a structure returned
6404 from a function, and this has undefined behavior only on
6405 execution, so create a temporary if an lvalue is
6406 required. */
6407 if (fallback == fb_lvalue)
6409 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6410 mark_addressable (*expr_p);
6412 break;
6414 case TREE_LIST:
6415 gcc_unreachable ();
6417 case COMPOUND_EXPR:
6418 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
6419 break;
6421 case COMPOUND_LITERAL_EXPR:
6422 ret = gimplify_compound_literal_expr (expr_p, pre_p);
6423 break;
6425 case MODIFY_EXPR:
6426 case INIT_EXPR:
6427 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
6428 fallback != fb_none);
6429 break;
6431 case TRUTH_ANDIF_EXPR:
6432 case TRUTH_ORIF_EXPR:
6433 ret = gimplify_boolean_expr (expr_p);
6434 break;
6436 case TRUTH_NOT_EXPR:
6437 if (TREE_CODE (TREE_TYPE (*expr_p)) != BOOLEAN_TYPE)
6439 tree type = TREE_TYPE (*expr_p);
6440 *expr_p = fold_convert (type, gimple_boolify (*expr_p));
6441 ret = GS_OK;
6442 break;
6445 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6446 is_gimple_val, fb_rvalue);
6447 recalculate_side_effects (*expr_p);
6448 break;
6450 case ADDR_EXPR:
6451 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
6452 break;
6454 case VA_ARG_EXPR:
6455 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
6456 break;
6458 CASE_CONVERT:
6459 if (IS_EMPTY_STMT (*expr_p))
6461 ret = GS_ALL_DONE;
6462 break;
6465 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
6466 || fallback == fb_none)
6468 /* Just strip a conversion to void (or in void context) and
6469 try again. */
6470 *expr_p = TREE_OPERAND (*expr_p, 0);
6471 break;
6474 ret = gimplify_conversion (expr_p);
6475 if (ret == GS_ERROR)
6476 break;
6477 if (*expr_p != save_expr)
6478 break;
6479 /* FALLTHRU */
6481 case FIX_TRUNC_EXPR:
6482 /* unary_expr: ... | '(' cast ')' val | ... */
6483 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6484 is_gimple_val, fb_rvalue);
6485 recalculate_side_effects (*expr_p);
6486 break;
6488 case INDIRECT_REF:
6489 *expr_p = fold_indirect_ref (*expr_p);
6490 if (*expr_p != save_expr)
6491 break;
6492 /* else fall through. */
6493 case ALIGN_INDIRECT_REF:
6494 case MISALIGNED_INDIRECT_REF:
6495 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6496 is_gimple_reg, fb_rvalue);
6497 recalculate_side_effects (*expr_p);
6498 break;
6500 /* Constants need not be gimplified. */
6501 case INTEGER_CST:
6502 case REAL_CST:
6503 case FIXED_CST:
6504 case STRING_CST:
6505 case COMPLEX_CST:
6506 case VECTOR_CST:
6507 ret = GS_ALL_DONE;
6508 break;
6510 case CONST_DECL:
6511 /* If we require an lvalue, such as for ADDR_EXPR, retain the
6512 CONST_DECL node. Otherwise the decl is replaceable by its
6513 value. */
6514 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
6515 if (fallback & fb_lvalue)
6516 ret = GS_ALL_DONE;
6517 else
6518 *expr_p = DECL_INITIAL (*expr_p);
6519 break;
6521 case DECL_EXPR:
6522 ret = gimplify_decl_expr (expr_p, pre_p);
6523 break;
6525 case EXC_PTR_EXPR:
6526 /* FIXME make this a decl. */
6527 ret = GS_ALL_DONE;
6528 break;
6530 case BIND_EXPR:
6531 ret = gimplify_bind_expr (expr_p, pre_p);
6532 break;
6534 case LOOP_EXPR:
6535 ret = gimplify_loop_expr (expr_p, pre_p);
6536 break;
6538 case SWITCH_EXPR:
6539 ret = gimplify_switch_expr (expr_p, pre_p);
6540 break;
6542 case EXIT_EXPR:
6543 ret = gimplify_exit_expr (expr_p);
6544 break;
6546 case GOTO_EXPR:
6547 /* If the target is not LABEL, then it is a computed jump
6548 and the target needs to be gimplified. */
6549 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
6551 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
6552 NULL, is_gimple_val, fb_rvalue);
6553 if (ret == GS_ERROR)
6554 break;
6556 gimplify_seq_add_stmt (pre_p,
6557 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
6558 break;
6560 case PREDICT_EXPR:
6561 gimplify_seq_add_stmt (pre_p,
6562 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
6563 PREDICT_EXPR_OUTCOME (*expr_p)));
6564 ret = GS_ALL_DONE;
6565 break;
6567 case LABEL_EXPR:
6568 ret = GS_ALL_DONE;
6569 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
6570 == current_function_decl);
6571 gimplify_seq_add_stmt (pre_p,
6572 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
6573 break;
6575 case CASE_LABEL_EXPR:
6576 ret = gimplify_case_label_expr (expr_p, pre_p);
6577 break;
6579 case RETURN_EXPR:
6580 ret = gimplify_return_expr (*expr_p, pre_p);
6581 break;
6583 case CONSTRUCTOR:
6584 /* Don't reduce this in place; let gimplify_init_constructor work its
6585 magic. Buf if we're just elaborating this for side effects, just
6586 gimplify any element that has side-effects. */
6587 if (fallback == fb_none)
6589 unsigned HOST_WIDE_INT ix;
6590 constructor_elt *ce;
6591 tree temp = NULL_TREE;
6592 for (ix = 0;
6593 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (*expr_p),
6594 ix, ce);
6595 ix++)
6596 if (TREE_SIDE_EFFECTS (ce->value))
6597 append_to_statement_list (ce->value, &temp);
6599 *expr_p = temp;
6600 ret = GS_OK;
6602 /* C99 code may assign to an array in a constructed
6603 structure or union, and this has undefined behavior only
6604 on execution, so create a temporary if an lvalue is
6605 required. */
6606 else if (fallback == fb_lvalue)
6608 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6609 mark_addressable (*expr_p);
6611 else
6612 ret = GS_ALL_DONE;
6613 break;
6615 /* The following are special cases that are not handled by the
6616 original GIMPLE grammar. */
6618 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
6619 eliminated. */
6620 case SAVE_EXPR:
6621 ret = gimplify_save_expr (expr_p, pre_p, post_p);
6622 break;
6624 case BIT_FIELD_REF:
6626 enum gimplify_status r0, r1, r2;
6628 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6629 post_p, is_gimple_lvalue, fb_either);
6630 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
6631 post_p, is_gimple_val, fb_rvalue);
6632 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
6633 post_p, is_gimple_val, fb_rvalue);
6634 recalculate_side_effects (*expr_p);
6636 ret = MIN (r0, MIN (r1, r2));
6638 break;
6640 case NON_LVALUE_EXPR:
6641 /* This should have been stripped above. */
6642 gcc_unreachable ();
6644 case ASM_EXPR:
6645 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
6646 break;
6648 case TRY_FINALLY_EXPR:
6649 case TRY_CATCH_EXPR:
6651 gimple_seq eval, cleanup;
6652 gimple try_;
6654 eval = cleanup = NULL;
6655 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
6656 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
6657 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
6658 if (gimple_seq_empty_p (cleanup))
6660 gimple_seq_add_seq (pre_p, eval);
6661 ret = GS_ALL_DONE;
6662 break;
6664 try_ = gimple_build_try (eval, cleanup,
6665 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
6666 ? GIMPLE_TRY_FINALLY
6667 : GIMPLE_TRY_CATCH);
6668 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
6669 gimple_try_set_catch_is_cleanup (try_,
6670 TRY_CATCH_IS_CLEANUP (*expr_p));
6671 gimplify_seq_add_stmt (pre_p, try_);
6672 ret = GS_ALL_DONE;
6673 break;
6676 case CLEANUP_POINT_EXPR:
6677 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
6678 break;
6680 case TARGET_EXPR:
6681 ret = gimplify_target_expr (expr_p, pre_p, post_p);
6682 break;
6684 case CATCH_EXPR:
6686 gimple c;
6687 gimple_seq handler = NULL;
6688 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
6689 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
6690 gimplify_seq_add_stmt (pre_p, c);
6691 ret = GS_ALL_DONE;
6692 break;
6695 case EH_FILTER_EXPR:
6697 gimple ehf;
6698 gimple_seq failure = NULL;
6700 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
6701 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
6702 gimple_eh_filter_set_must_not_throw
6703 (ehf, EH_FILTER_MUST_NOT_THROW (*expr_p));
6704 gimplify_seq_add_stmt (pre_p, ehf);
6705 ret = GS_ALL_DONE;
6706 break;
6709 case CHANGE_DYNAMIC_TYPE_EXPR:
6711 gimple cdt;
6713 ret = gimplify_expr (&CHANGE_DYNAMIC_TYPE_LOCATION (*expr_p),
6714 pre_p, post_p, is_gimple_reg, fb_lvalue);
6715 cdt = gimple_build_cdt (CHANGE_DYNAMIC_TYPE_NEW_TYPE (*expr_p),
6716 CHANGE_DYNAMIC_TYPE_LOCATION (*expr_p));
6717 gimplify_seq_add_stmt (pre_p, cdt);
6718 ret = GS_ALL_DONE;
6720 break;
6722 case OBJ_TYPE_REF:
6724 enum gimplify_status r0, r1;
6725 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
6726 post_p, is_gimple_val, fb_rvalue);
6727 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
6728 post_p, is_gimple_val, fb_rvalue);
6729 TREE_SIDE_EFFECTS (*expr_p) = 0;
6730 ret = MIN (r0, r1);
6732 break;
6734 case LABEL_DECL:
6735 /* We get here when taking the address of a label. We mark
6736 the label as "forced"; meaning it can never be removed and
6737 it is a potential target for any computed goto. */
6738 FORCED_LABEL (*expr_p) = 1;
6739 ret = GS_ALL_DONE;
6740 break;
6742 case STATEMENT_LIST:
6743 ret = gimplify_statement_list (expr_p, pre_p);
6744 break;
6746 case WITH_SIZE_EXPR:
6748 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6749 post_p == &internal_post ? NULL : post_p,
6750 gimple_test_f, fallback);
6751 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
6752 is_gimple_val, fb_rvalue);
6754 break;
6756 case VAR_DECL:
6757 case PARM_DECL:
6758 ret = gimplify_var_or_parm_decl (expr_p);
6759 break;
6761 case RESULT_DECL:
6762 /* When within an OpenMP context, notice uses of variables. */
6763 if (gimplify_omp_ctxp)
6764 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
6765 ret = GS_ALL_DONE;
6766 break;
6768 case SSA_NAME:
6769 /* Allow callbacks into the gimplifier during optimization. */
6770 ret = GS_ALL_DONE;
6771 break;
6773 case OMP_PARALLEL:
6774 gimplify_omp_parallel (expr_p, pre_p);
6775 ret = GS_ALL_DONE;
6776 break;
6778 case OMP_TASK:
6779 gimplify_omp_task (expr_p, pre_p);
6780 ret = GS_ALL_DONE;
6781 break;
6783 case OMP_FOR:
6784 ret = gimplify_omp_for (expr_p, pre_p);
6785 break;
6787 case OMP_SECTIONS:
6788 case OMP_SINGLE:
6789 gimplify_omp_workshare (expr_p, pre_p);
6790 ret = GS_ALL_DONE;
6791 break;
6793 case OMP_SECTION:
6794 case OMP_MASTER:
6795 case OMP_ORDERED:
6796 case OMP_CRITICAL:
6798 gimple_seq body = NULL;
6799 gimple g;
6801 gimplify_and_add (OMP_BODY (*expr_p), &body);
6802 switch (TREE_CODE (*expr_p))
6804 case OMP_SECTION:
6805 g = gimple_build_omp_section (body);
6806 break;
6807 case OMP_MASTER:
6808 g = gimple_build_omp_master (body);
6809 break;
6810 case OMP_ORDERED:
6811 g = gimple_build_omp_ordered (body);
6812 break;
6813 case OMP_CRITICAL:
6814 g = gimple_build_omp_critical (body,
6815 OMP_CRITICAL_NAME (*expr_p));
6816 break;
6817 default:
6818 gcc_unreachable ();
6820 gimplify_seq_add_stmt (pre_p, g);
6821 ret = GS_ALL_DONE;
6822 break;
6825 case OMP_ATOMIC:
6826 ret = gimplify_omp_atomic (expr_p, pre_p);
6827 break;
6829 case POINTER_PLUS_EXPR:
6830 /* Convert ((type *)A)+offset into &A->field_of_type_and_offset.
6831 The second is gimple immediate saving a need for extra statement.
6833 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
6834 && (tmp = maybe_fold_offset_to_address
6835 (TREE_OPERAND (*expr_p, 0), TREE_OPERAND (*expr_p, 1),
6836 TREE_TYPE (*expr_p))))
6838 *expr_p = tmp;
6839 break;
6841 /* Convert (void *)&a + 4 into (void *)&a[1]. */
6842 if (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == NOP_EXPR
6843 && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
6844 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*expr_p,
6845 0),0)))
6846 && (tmp = maybe_fold_offset_to_address
6847 (TREE_OPERAND (TREE_OPERAND (*expr_p, 0), 0),
6848 TREE_OPERAND (*expr_p, 1),
6849 TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*expr_p, 0),
6850 0)))))
6852 *expr_p = fold_convert (TREE_TYPE (*expr_p), tmp);
6853 break;
6855 /* FALLTHRU */
6857 default:
6858 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
6860 case tcc_comparison:
6861 /* Handle comparison of objects of non scalar mode aggregates
6862 with a call to memcmp. It would be nice to only have to do
6863 this for variable-sized objects, but then we'd have to allow
6864 the same nest of reference nodes we allow for MODIFY_EXPR and
6865 that's too complex.
6867 Compare scalar mode aggregates as scalar mode values. Using
6868 memcmp for them would be very inefficient at best, and is
6869 plain wrong if bitfields are involved. */
6871 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
6873 if (!AGGREGATE_TYPE_P (type))
6874 goto expr_2;
6875 else if (TYPE_MODE (type) != BLKmode)
6876 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
6877 else
6878 ret = gimplify_variable_sized_compare (expr_p);
6880 break;
6883 /* If *EXPR_P does not need to be special-cased, handle it
6884 according to its class. */
6885 case tcc_unary:
6886 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6887 post_p, is_gimple_val, fb_rvalue);
6888 break;
6890 case tcc_binary:
6891 expr_2:
6893 enum gimplify_status r0, r1;
6895 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6896 post_p, is_gimple_val, fb_rvalue);
6897 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
6898 post_p, is_gimple_val, fb_rvalue);
6900 ret = MIN (r0, r1);
6901 break;
6904 case tcc_declaration:
6905 case tcc_constant:
6906 ret = GS_ALL_DONE;
6907 goto dont_recalculate;
6909 default:
6910 gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
6911 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
6912 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
6913 goto expr_2;
6916 recalculate_side_effects (*expr_p);
6918 dont_recalculate:
6919 break;
6922 /* If we replaced *expr_p, gimplify again. */
6923 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
6924 ret = GS_ALL_DONE;
6926 while (ret == GS_OK);
6928 /* If we encountered an error_mark somewhere nested inside, either
6929 stub out the statement or propagate the error back out. */
6930 if (ret == GS_ERROR)
6932 if (is_statement)
6933 *expr_p = NULL;
6934 goto out;
6937 /* This was only valid as a return value from the langhook, which
6938 we handled. Make sure it doesn't escape from any other context. */
6939 gcc_assert (ret != GS_UNHANDLED);
6941 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
6943 /* We aren't looking for a value, and we don't have a valid
6944 statement. If it doesn't have side-effects, throw it away. */
6945 if (!TREE_SIDE_EFFECTS (*expr_p))
6946 *expr_p = NULL;
6947 else if (!TREE_THIS_VOLATILE (*expr_p))
6949 /* This is probably a _REF that contains something nested that
6950 has side effects. Recurse through the operands to find it. */
6951 enum tree_code code = TREE_CODE (*expr_p);
6953 switch (code)
6955 case COMPONENT_REF:
6956 case REALPART_EXPR:
6957 case IMAGPART_EXPR:
6958 case VIEW_CONVERT_EXPR:
6959 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6960 gimple_test_f, fallback);
6961 break;
6963 case ARRAY_REF:
6964 case ARRAY_RANGE_REF:
6965 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6966 gimple_test_f, fallback);
6967 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
6968 gimple_test_f, fallback);
6969 break;
6971 default:
6972 /* Anything else with side-effects must be converted to
6973 a valid statement before we get here. */
6974 gcc_unreachable ();
6977 *expr_p = NULL;
6979 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
6980 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
6982 /* Historically, the compiler has treated a bare reference
6983 to a non-BLKmode volatile lvalue as forcing a load. */
6984 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
6986 /* Normally, we do not want to create a temporary for a
6987 TREE_ADDRESSABLE type because such a type should not be
6988 copied by bitwise-assignment. However, we make an
6989 exception here, as all we are doing here is ensuring that
6990 we read the bytes that make up the type. We use
6991 create_tmp_var_raw because create_tmp_var will abort when
6992 given a TREE_ADDRESSABLE type. */
6993 tree tmp = create_tmp_var_raw (type, "vol");
6994 gimple_add_tmp_var (tmp);
6995 gimplify_assign (tmp, *expr_p, pre_p);
6996 *expr_p = NULL;
6998 else
6999 /* We can't do anything useful with a volatile reference to
7000 an incomplete type, so just throw it away. Likewise for
7001 a BLKmode type, since any implicit inner load should
7002 already have been turned into an explicit one by the
7003 gimplification process. */
7004 *expr_p = NULL;
7007 /* If we are gimplifying at the statement level, we're done. Tack
7008 everything together and return. */
7009 if (fallback == fb_none || is_statement)
7011 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7012 it out for GC to reclaim it. */
7013 *expr_p = NULL_TREE;
7015 if (!gimple_seq_empty_p (internal_pre)
7016 || !gimple_seq_empty_p (internal_post))
7018 gimplify_seq_add_seq (&internal_pre, internal_post);
7019 gimplify_seq_add_seq (pre_p, internal_pre);
7022 /* The result of gimplifying *EXPR_P is going to be the last few
7023 statements in *PRE_P and *POST_P. Add location information
7024 to all the statements that were added by the gimplification
7025 helpers. */
7026 if (!gimple_seq_empty_p (*pre_p))
7027 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7029 if (!gimple_seq_empty_p (*post_p))
7030 annotate_all_with_location_after (*post_p, post_last_gsi,
7031 input_location);
7033 goto out;
7036 #ifdef ENABLE_GIMPLE_CHECKING
7037 if (*expr_p)
7039 enum tree_code code = TREE_CODE (*expr_p);
7040 /* These expressions should already be in gimple IR form. */
7041 gcc_assert (code != MODIFY_EXPR
7042 && code != ASM_EXPR
7043 && code != BIND_EXPR
7044 && code != CATCH_EXPR
7045 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7046 && code != EH_FILTER_EXPR
7047 && code != GOTO_EXPR
7048 && code != LABEL_EXPR
7049 && code != LOOP_EXPR
7050 && code != RESX_EXPR
7051 && code != SWITCH_EXPR
7052 && code != TRY_FINALLY_EXPR
7053 && code != OMP_CRITICAL
7054 && code != OMP_FOR
7055 && code != OMP_MASTER
7056 && code != OMP_ORDERED
7057 && code != OMP_PARALLEL
7058 && code != OMP_SECTIONS
7059 && code != OMP_SECTION
7060 && code != OMP_SINGLE);
7062 #endif
7064 /* Otherwise we're gimplifying a subexpression, so the resulting
7065 value is interesting. If it's a valid operand that matches
7066 GIMPLE_TEST_F, we're done. Unless we are handling some
7067 post-effects internally; if that's the case, we need to copy into
7068 a temporary before adding the post-effects to POST_P. */
7069 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7070 goto out;
7072 /* Otherwise, we need to create a new temporary for the gimplified
7073 expression. */
7075 /* We can't return an lvalue if we have an internal postqueue. The
7076 object the lvalue refers to would (probably) be modified by the
7077 postqueue; we need to copy the value out first, which means an
7078 rvalue. */
7079 if ((fallback & fb_lvalue)
7080 && gimple_seq_empty_p (internal_post)
7081 && is_gimple_addressable (*expr_p))
7083 /* An lvalue will do. Take the address of the expression, store it
7084 in a temporary, and replace the expression with an INDIRECT_REF of
7085 that temporary. */
7086 tmp = build_fold_addr_expr (*expr_p);
7087 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7088 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
7090 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7092 /* An rvalue will do. Assign the gimplified expression into a
7093 new temporary TMP and replace the original expression with
7094 TMP. First, make sure that the expression has a type so that
7095 it can be assigned into a temporary. */
7096 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7098 if (!gimple_seq_empty_p (internal_post) || (fallback & fb_lvalue))
7099 /* The postqueue might change the value of the expression between
7100 the initialization and use of the temporary, so we can't use a
7101 formal temp. FIXME do we care? */
7102 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7103 else
7104 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7106 else
7108 #ifdef ENABLE_GIMPLE_CHECKING
7109 if (!(fallback & fb_mayfail))
7111 fprintf (stderr, "gimplification failed:\n");
7112 print_generic_expr (stderr, *expr_p, 0);
7113 debug_tree (*expr_p);
7114 internal_error ("gimplification failed");
7116 #endif
7117 gcc_assert (fallback & fb_mayfail);
7119 /* If this is an asm statement, and the user asked for the
7120 impossible, don't die. Fail and let gimplify_asm_expr
7121 issue an error. */
7122 ret = GS_ERROR;
7123 goto out;
7126 /* Make sure the temporary matches our predicate. */
7127 gcc_assert ((*gimple_test_f) (*expr_p));
7129 if (!gimple_seq_empty_p (internal_post))
7131 annotate_all_with_location (internal_post, input_location);
7132 gimplify_seq_add_seq (pre_p, internal_post);
7135 out:
7136 input_location = saved_location;
7137 return ret;
7140 /* Look through TYPE for variable-sized objects and gimplify each such
7141 size that we find. Add to LIST_P any statements generated. */
7143 void
7144 gimplify_type_sizes (tree type, gimple_seq *list_p)
7146 tree field, t;
7148 if (type == NULL || type == error_mark_node)
7149 return;
7151 /* We first do the main variant, then copy into any other variants. */
7152 type = TYPE_MAIN_VARIANT (type);
7154 /* Avoid infinite recursion. */
7155 if (TYPE_SIZES_GIMPLIFIED (type))
7156 return;
7158 TYPE_SIZES_GIMPLIFIED (type) = 1;
7160 switch (TREE_CODE (type))
7162 case INTEGER_TYPE:
7163 case ENUMERAL_TYPE:
7164 case BOOLEAN_TYPE:
7165 case REAL_TYPE:
7166 case FIXED_POINT_TYPE:
7167 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
7168 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
7170 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7172 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
7173 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
7175 break;
7177 case ARRAY_TYPE:
7178 /* These types may not have declarations, so handle them here. */
7179 gimplify_type_sizes (TREE_TYPE (type), list_p);
7180 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
7181 /* When not optimizing, ensure VLA bounds aren't removed. */
7182 if (!optimize
7183 && TYPE_DOMAIN (type)
7184 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
7186 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7187 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7188 DECL_IGNORED_P (t) = 0;
7189 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7190 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7191 DECL_IGNORED_P (t) = 0;
7193 break;
7195 case RECORD_TYPE:
7196 case UNION_TYPE:
7197 case QUAL_UNION_TYPE:
7198 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7199 if (TREE_CODE (field) == FIELD_DECL)
7201 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
7202 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
7203 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
7204 gimplify_type_sizes (TREE_TYPE (field), list_p);
7206 break;
7208 case POINTER_TYPE:
7209 case REFERENCE_TYPE:
7210 /* We used to recurse on the pointed-to type here, which turned out to
7211 be incorrect because its definition might refer to variables not
7212 yet initialized at this point if a forward declaration is involved.
7214 It was actually useful for anonymous pointed-to types to ensure
7215 that the sizes evaluation dominates every possible later use of the
7216 values. Restricting to such types here would be safe since there
7217 is no possible forward declaration around, but would introduce an
7218 undesirable middle-end semantic to anonymity. We then defer to
7219 front-ends the responsibility of ensuring that the sizes are
7220 evaluated both early and late enough, e.g. by attaching artificial
7221 type declarations to the tree. */
7222 break;
7224 default:
7225 break;
7228 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
7229 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
7231 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7233 TYPE_SIZE (t) = TYPE_SIZE (type);
7234 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
7235 TYPE_SIZES_GIMPLIFIED (t) = 1;
7239 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
7240 a size or position, has had all of its SAVE_EXPRs evaluated.
7241 We add any required statements to *STMT_P. */
7243 void
7244 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
7246 tree type, expr = *expr_p;
7248 /* We don't do anything if the value isn't there, is constant, or contains
7249 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
7250 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
7251 will want to replace it with a new variable, but that will cause problems
7252 if this type is from outside the function. It's OK to have that here. */
7253 if (expr == NULL_TREE || TREE_CONSTANT (expr)
7254 || TREE_CODE (expr) == VAR_DECL
7255 || CONTAINS_PLACEHOLDER_P (expr))
7256 return;
7258 type = TREE_TYPE (expr);
7259 *expr_p = unshare_expr (expr);
7261 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
7262 expr = *expr_p;
7264 /* Verify that we've an exact type match with the original expression.
7265 In particular, we do not wish to drop a "sizetype" in favour of a
7266 type of similar dimensions. We don't want to pollute the generic
7267 type-stripping code with this knowledge because it doesn't matter
7268 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
7269 and friends retain their "sizetype-ness". */
7270 if (TREE_TYPE (expr) != type
7271 && TREE_CODE (type) == INTEGER_TYPE
7272 && TYPE_IS_SIZETYPE (type))
7274 tree tmp;
7275 gimple stmt;
7277 *expr_p = create_tmp_var (type, NULL);
7278 tmp = build1 (NOP_EXPR, type, expr);
7279 stmt = gimplify_assign (*expr_p, tmp, stmt_p);
7280 if (EXPR_HAS_LOCATION (expr))
7281 gimple_set_location (stmt, *EXPR_LOCUS (expr));
7282 else
7283 gimple_set_location (stmt, input_location);
7288 /* Gimplify the body of statements pointed to by BODY_P and return a
7289 GIMPLE_BIND containing the sequence of GIMPLE statements
7290 corresponding to BODY_P. FNDECL is the function decl containing
7291 *BODY_P. */
7293 gimple
7294 gimplify_body (tree *body_p, tree fndecl, bool do_parms)
7296 location_t saved_location = input_location;
7297 gimple_seq parm_stmts, seq;
7298 gimple outer_bind;
7299 struct gimplify_ctx gctx;
7301 timevar_push (TV_TREE_GIMPLIFY);
7303 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
7304 gimplification. */
7305 default_rtl_profile ();
7307 gcc_assert (gimplify_ctxp == NULL);
7308 push_gimplify_context (&gctx);
7310 /* Unshare most shared trees in the body and in that of any nested functions.
7311 It would seem we don't have to do this for nested functions because
7312 they are supposed to be output and then the outer function gimplified
7313 first, but the g++ front end doesn't always do it that way. */
7314 unshare_body (body_p, fndecl);
7315 unvisit_body (body_p, fndecl);
7317 /* Make sure input_location isn't set to something weird. */
7318 input_location = DECL_SOURCE_LOCATION (fndecl);
7320 /* Resolve callee-copies. This has to be done before processing
7321 the body so that DECL_VALUE_EXPR gets processed correctly. */
7322 parm_stmts = (do_parms) ? gimplify_parameters () : NULL;
7324 /* Gimplify the function's body. */
7325 seq = NULL;
7326 gimplify_stmt (body_p, &seq);
7327 outer_bind = gimple_seq_first_stmt (seq);
7328 if (!outer_bind)
7330 outer_bind = gimple_build_nop ();
7331 gimplify_seq_add_stmt (&seq, outer_bind);
7334 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
7335 not the case, wrap everything in a GIMPLE_BIND to make it so. */
7336 if (gimple_code (outer_bind) == GIMPLE_BIND
7337 && gimple_seq_first (seq) == gimple_seq_last (seq))
7339 else
7340 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
7342 *body_p = NULL_TREE;
7344 /* If we had callee-copies statements, insert them at the beginning
7345 of the function. */
7346 if (!gimple_seq_empty_p (parm_stmts))
7348 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
7349 gimple_bind_set_body (outer_bind, parm_stmts);
7352 pop_gimplify_context (outer_bind);
7353 gcc_assert (gimplify_ctxp == NULL);
7355 #ifdef ENABLE_TYPES_CHECKING
7356 if (!errorcount && !sorrycount)
7357 verify_types_in_gimple_seq (gimple_bind_body (outer_bind));
7358 #endif
7360 timevar_pop (TV_TREE_GIMPLIFY);
7361 input_location = saved_location;
7363 return outer_bind;
7366 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
7367 node for the function we want to gimplify.
7369 Returns the sequence of GIMPLE statements corresponding to the body
7370 of FNDECL. */
7372 void
7373 gimplify_function_tree (tree fndecl)
7375 tree oldfn, parm, ret;
7376 gimple_seq seq;
7377 gimple bind;
7379 oldfn = current_function_decl;
7380 current_function_decl = fndecl;
7381 if (DECL_STRUCT_FUNCTION (fndecl))
7382 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
7383 else
7384 push_struct_function (fndecl);
7386 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = TREE_CHAIN (parm))
7388 /* Preliminarily mark non-addressed complex variables as eligible
7389 for promotion to gimple registers. We'll transform their uses
7390 as we find them. */
7391 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
7392 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
7393 && !TREE_THIS_VOLATILE (parm)
7394 && !needs_to_live_in_memory (parm))
7395 DECL_GIMPLE_REG_P (parm) = 1;
7398 ret = DECL_RESULT (fndecl);
7399 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
7400 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
7401 && !needs_to_live_in_memory (ret))
7402 DECL_GIMPLE_REG_P (ret) = 1;
7404 bind = gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl, true);
7406 /* The tree body of the function is no longer needed, replace it
7407 with the new GIMPLE body. */
7408 seq = gimple_seq_alloc ();
7409 gimple_seq_add_stmt (&seq, bind);
7410 gimple_set_body (fndecl, seq);
7412 /* If we're instrumenting function entry/exit, then prepend the call to
7413 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
7414 catch the exit hook. */
7415 /* ??? Add some way to ignore exceptions for this TFE. */
7416 if (flag_instrument_function_entry_exit
7417 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
7418 && !flag_instrument_functions_exclude_p (fndecl))
7420 tree x;
7421 gimple new_bind;
7422 gimple tf;
7423 gimple_seq cleanup = NULL, body = NULL;
7425 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
7426 gimplify_seq_add_stmt (&cleanup, gimple_build_call (x, 0));
7427 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
7429 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
7430 gimplify_seq_add_stmt (&body, gimple_build_call (x, 0));
7431 gimplify_seq_add_stmt (&body, tf);
7432 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
7433 /* Clear the block for BIND, since it is no longer directly inside
7434 the function, but within a try block. */
7435 gimple_bind_set_block (bind, NULL);
7437 /* Replace the current function body with the body
7438 wrapped in the try/finally TF. */
7439 seq = gimple_seq_alloc ();
7440 gimple_seq_add_stmt (&seq, new_bind);
7441 gimple_set_body (fndecl, seq);
7444 DECL_SAVED_TREE (fndecl) = NULL_TREE;
7446 current_function_decl = oldfn;
7447 pop_cfun ();
7451 /* Some transformations like inlining may invalidate the GIMPLE form
7452 for operands. This function traverses all the operands in STMT and
7453 gimplifies anything that is not a valid gimple operand. Any new
7454 GIMPLE statements are inserted before *GSI_P. */
7456 void
7457 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
7459 size_t i, num_ops;
7460 tree orig_lhs = NULL_TREE, lhs, t;
7461 gimple_seq pre = NULL;
7462 gimple post_stmt = NULL;
7463 struct gimplify_ctx gctx;
7465 push_gimplify_context (&gctx);
7466 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
7468 switch (gimple_code (stmt))
7470 case GIMPLE_COND:
7471 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
7472 is_gimple_val, fb_rvalue);
7473 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
7474 is_gimple_val, fb_rvalue);
7475 break;
7476 case GIMPLE_SWITCH:
7477 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
7478 is_gimple_val, fb_rvalue);
7479 break;
7480 case GIMPLE_OMP_ATOMIC_LOAD:
7481 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
7482 is_gimple_val, fb_rvalue);
7483 break;
7484 case GIMPLE_ASM:
7486 size_t i, noutputs = gimple_asm_noutputs (stmt);
7487 const char *constraint, **oconstraints;
7488 bool allows_mem, allows_reg, is_inout;
7490 oconstraints
7491 = (const char **) alloca ((noutputs) * sizeof (const char *));
7492 for (i = 0; i < noutputs; i++)
7494 tree op = gimple_asm_output_op (stmt, i);
7495 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
7496 oconstraints[i] = constraint;
7497 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
7498 &allows_reg, &is_inout);
7499 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
7500 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
7501 fb_lvalue | fb_mayfail);
7503 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
7505 tree op = gimple_asm_input_op (stmt, i);
7506 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
7507 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
7508 oconstraints, &allows_mem, &allows_reg);
7509 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
7510 allows_reg = 0;
7511 if (!allows_reg && allows_mem)
7512 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
7513 is_gimple_lvalue, fb_lvalue | fb_mayfail);
7514 else
7515 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
7516 is_gimple_asm_val, fb_rvalue);
7519 break;
7520 default:
7521 /* NOTE: We start gimplifying operands from last to first to
7522 make sure that side-effects on the RHS of calls, assignments
7523 and ASMs are executed before the LHS. The ordering is not
7524 important for other statements. */
7525 num_ops = gimple_num_ops (stmt);
7526 orig_lhs = gimple_get_lhs (stmt);
7527 for (i = num_ops; i > 0; i--)
7529 tree op = gimple_op (stmt, i - 1);
7530 if (op == NULL_TREE)
7531 continue;
7532 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
7533 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
7534 else if (i == 2
7535 && is_gimple_assign (stmt)
7536 && num_ops == 2
7537 && get_gimple_rhs_class (gimple_expr_code (stmt))
7538 == GIMPLE_SINGLE_RHS)
7539 gimplify_expr (&op, &pre, NULL,
7540 rhs_predicate_for (gimple_assign_lhs (stmt)),
7541 fb_rvalue);
7542 else if (i == 2 && is_gimple_call (stmt))
7544 if (TREE_CODE (op) == FUNCTION_DECL)
7545 continue;
7546 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
7548 else
7549 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
7550 gimple_set_op (stmt, i - 1, op);
7553 lhs = gimple_get_lhs (stmt);
7554 /* If the LHS changed it in a way that requires a simple RHS,
7555 create temporary. */
7556 if (lhs && !is_gimple_reg (lhs))
7558 bool need_temp = false;
7560 if (is_gimple_assign (stmt)
7561 && num_ops == 2
7562 && get_gimple_rhs_class (gimple_expr_code (stmt))
7563 == GIMPLE_SINGLE_RHS)
7564 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
7565 rhs_predicate_for (gimple_assign_lhs (stmt)),
7566 fb_rvalue);
7567 else if (is_gimple_reg (lhs))
7569 if (is_gimple_reg_type (TREE_TYPE (lhs)))
7571 if (is_gimple_call (stmt))
7573 i = gimple_call_flags (stmt);
7574 if ((i & ECF_LOOPING_CONST_OR_PURE)
7575 || !(i & (ECF_CONST | ECF_PURE)))
7576 need_temp = true;
7578 if (stmt_can_throw_internal (stmt))
7579 need_temp = true;
7582 else
7584 if (is_gimple_reg_type (TREE_TYPE (lhs)))
7585 need_temp = true;
7586 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
7588 if (is_gimple_call (stmt))
7590 tree fndecl = gimple_call_fndecl (stmt);
7592 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
7593 && !(fndecl && DECL_RESULT (fndecl)
7594 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
7595 need_temp = true;
7597 else
7598 need_temp = true;
7601 if (need_temp)
7603 tree temp = create_tmp_var (TREE_TYPE (lhs), NULL);
7605 if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
7606 || TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE)
7607 DECL_GIMPLE_REG_P (temp) = 1;
7608 if (TREE_CODE (orig_lhs) == SSA_NAME)
7609 orig_lhs = SSA_NAME_VAR (orig_lhs);
7610 if (TREE_CODE (orig_lhs) == VAR_DECL
7611 && DECL_BASED_ON_RESTRICT_P (orig_lhs))
7613 DECL_BASED_ON_RESTRICT_P (temp) = 1;
7614 SET_DECL_RESTRICT_BASE (temp,
7615 DECL_GET_RESTRICT_BASE (orig_lhs));
7618 if (gimple_in_ssa_p (cfun))
7619 temp = make_ssa_name (temp, NULL);
7620 gimple_set_lhs (stmt, temp);
7621 post_stmt = gimple_build_assign (lhs, temp);
7622 if (TREE_CODE (lhs) == SSA_NAME)
7623 SSA_NAME_DEF_STMT (lhs) = post_stmt;
7626 break;
7629 if (gimple_referenced_vars (cfun))
7630 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
7631 add_referenced_var (t);
7633 if (!gimple_seq_empty_p (pre))
7635 if (gimple_in_ssa_p (cfun))
7637 gimple_stmt_iterator i;
7639 for (i = gsi_start (pre); !gsi_end_p (i); gsi_next (&i))
7640 mark_symbols_for_renaming (gsi_stmt (i));
7642 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
7644 if (post_stmt)
7645 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
7647 pop_gimplify_context (NULL);
7651 /* Expands EXPR to list of gimple statements STMTS. If SIMPLE is true,
7652 force the result to be either ssa_name or an invariant, otherwise
7653 just force it to be a rhs expression. If VAR is not NULL, make the
7654 base variable of the final destination be VAR if suitable. */
7656 tree
7657 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
7659 tree t;
7660 enum gimplify_status ret;
7661 gimple_predicate gimple_test_f;
7662 struct gimplify_ctx gctx;
7664 *stmts = NULL;
7666 if (is_gimple_val (expr))
7667 return expr;
7669 gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
7671 push_gimplify_context (&gctx);
7672 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
7673 gimplify_ctxp->allow_rhs_cond_expr = true;
7675 if (var)
7676 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
7678 if (TREE_CODE (expr) != MODIFY_EXPR
7679 && TREE_TYPE (expr) == void_type_node)
7681 gimplify_and_add (expr, stmts);
7682 expr = NULL_TREE;
7684 else
7686 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
7687 gcc_assert (ret != GS_ERROR);
7690 if (gimple_referenced_vars (cfun))
7691 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
7692 add_referenced_var (t);
7694 pop_gimplify_context (NULL);
7696 return expr;
7699 /* Invokes force_gimple_operand for EXPR with parameters SIMPLE_P and VAR. If
7700 some statements are produced, emits them at GSI. If BEFORE is true.
7701 the statements are appended before GSI, otherwise they are appended after
7702 it. M specifies the way GSI moves after insertion (GSI_SAME_STMT or
7703 GSI_CONTINUE_LINKING are the usual values). */
7705 tree
7706 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
7707 bool simple_p, tree var, bool before,
7708 enum gsi_iterator_update m)
7710 gimple_seq stmts;
7712 expr = force_gimple_operand (expr, &stmts, simple_p, var);
7714 if (!gimple_seq_empty_p (stmts))
7716 if (gimple_in_ssa_p (cfun))
7718 gimple_stmt_iterator i;
7720 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
7721 mark_symbols_for_renaming (gsi_stmt (i));
7724 if (before)
7725 gsi_insert_seq_before (gsi, stmts, m);
7726 else
7727 gsi_insert_seq_after (gsi, stmts, m);
7730 return expr;
7733 #include "gt-gimplify.h"