* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / gcc / gimplify.c
blob82e747f00709d7e6fc97c5deb56c028980262f3e
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 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
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 "tree-gimple.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "tree-flow.h"
37 #include "cgraph.h"
38 #include "timevar.h"
39 #include "except.h"
40 #include "hashtab.h"
41 #include "flags.h"
42 #include "real.h"
43 #include "function.h"
44 #include "output.h"
45 #include "expr.h"
46 #include "ggc.h"
47 #include "toplev.h"
48 #include "target.h"
49 #include "optabs.h"
50 #include "pointer-set.h"
53 enum gimplify_omp_var_data
55 GOVD_SEEN = 1,
56 GOVD_EXPLICIT = 2,
57 GOVD_SHARED = 4,
58 GOVD_PRIVATE = 8,
59 GOVD_FIRSTPRIVATE = 16,
60 GOVD_LASTPRIVATE = 32,
61 GOVD_REDUCTION = 64,
62 GOVD_LOCAL = 128,
63 GOVD_DEBUG_PRIVATE = 256,
64 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
65 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
68 struct gimplify_omp_ctx
70 struct gimplify_omp_ctx *outer_context;
71 splay_tree variables;
72 struct pointer_set_t *privatized_types;
73 location_t location;
74 enum omp_clause_default_kind default_kind;
75 bool is_parallel;
78 struct gimplify_ctx
80 struct gimplify_ctx *prev_context;
82 tree current_bind_expr;
83 tree temps;
84 tree conditional_cleanups;
85 tree exit_label;
86 tree return_temp;
88 VEC(tree,heap) *case_labels;
89 /* The formal temporary table. Should this be persistent? */
90 htab_t temp_htab;
92 int conditions;
93 bool save_stack;
94 bool into_ssa;
97 static struct gimplify_ctx *gimplify_ctxp;
98 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
102 /* Formal (expression) temporary table handling: Multiple occurrences of
103 the same scalar expression are evaluated into the same temporary. */
105 typedef struct gimple_temp_hash_elt
107 tree val; /* Key */
108 tree temp; /* Value */
109 } elt_t;
111 /* Forward declarations. */
112 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
113 #ifdef ENABLE_CHECKING
114 static bool cpt_same_type (tree a, tree b);
115 #endif
118 /* Return a hash value for a formal temporary table entry. */
120 static hashval_t
121 gimple_tree_hash (const void *p)
123 tree t = ((const elt_t *) p)->val;
124 return iterative_hash_expr (t, 0);
127 /* Compare two formal temporary table entries. */
129 static int
130 gimple_tree_eq (const void *p1, const void *p2)
132 tree t1 = ((const elt_t *) p1)->val;
133 tree t2 = ((const elt_t *) p2)->val;
134 enum tree_code code = TREE_CODE (t1);
136 if (TREE_CODE (t2) != code
137 || TREE_TYPE (t1) != TREE_TYPE (t2))
138 return 0;
140 if (!operand_equal_p (t1, t2, 0))
141 return 0;
143 /* Only allow them to compare equal if they also hash equal; otherwise
144 results are nondeterminate, and we fail bootstrap comparison. */
145 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
147 return 1;
150 /* Set up a context for the gimplifier. */
152 void
153 push_gimplify_context (void)
155 struct gimplify_ctx *c;
157 c = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
158 c->prev_context = gimplify_ctxp;
159 if (optimize)
160 c->temp_htab = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
162 gimplify_ctxp = c;
165 /* Tear down a context for the gimplifier. If BODY is non-null, then
166 put the temporaries into the outer BIND_EXPR. Otherwise, put them
167 in the unexpanded_var_list. */
169 void
170 pop_gimplify_context (tree body)
172 struct gimplify_ctx *c = gimplify_ctxp;
173 tree t;
175 gcc_assert (c && !c->current_bind_expr);
176 gimplify_ctxp = c->prev_context;
178 for (t = c->temps; t ; t = TREE_CHAIN (t))
179 DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
181 if (body)
182 declare_tmp_vars (c->temps, body);
183 else
184 record_vars (c->temps);
186 if (optimize)
187 htab_delete (c->temp_htab);
188 free (c);
191 static void
192 gimple_push_bind_expr (tree bind)
194 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
195 gimplify_ctxp->current_bind_expr = bind;
198 static void
199 gimple_pop_bind_expr (void)
201 gimplify_ctxp->current_bind_expr
202 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
205 tree
206 gimple_current_bind_expr (void)
208 return gimplify_ctxp->current_bind_expr;
211 /* Returns true iff there is a COND_EXPR between us and the innermost
212 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
214 static bool
215 gimple_conditional_context (void)
217 return gimplify_ctxp->conditions > 0;
220 /* Note that we've entered a COND_EXPR. */
222 static void
223 gimple_push_condition (void)
225 #ifdef ENABLE_CHECKING
226 if (gimplify_ctxp->conditions == 0)
227 gcc_assert (!gimplify_ctxp->conditional_cleanups);
228 #endif
229 ++(gimplify_ctxp->conditions);
232 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
233 now, add any conditional cleanups we've seen to the prequeue. */
235 static void
236 gimple_pop_condition (tree *pre_p)
238 int conds = --(gimplify_ctxp->conditions);
240 gcc_assert (conds >= 0);
241 if (conds == 0)
243 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
244 gimplify_ctxp->conditional_cleanups = NULL_TREE;
248 /* A stable comparison routine for use with splay trees and DECLs. */
250 static int
251 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
253 tree a = (tree) xa;
254 tree b = (tree) xb;
256 return DECL_UID (a) - DECL_UID (b);
259 /* Create a new omp construct that deals with variable remapping. */
261 static struct gimplify_omp_ctx *
262 new_omp_context (bool is_parallel)
264 struct gimplify_omp_ctx *c;
266 c = XCNEW (struct gimplify_omp_ctx);
267 c->outer_context = gimplify_omp_ctxp;
268 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
269 c->privatized_types = pointer_set_create ();
270 c->location = input_location;
271 c->is_parallel = is_parallel;
272 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
274 return c;
277 /* Destroy an omp construct that deals with variable remapping. */
279 static void
280 delete_omp_context (struct gimplify_omp_ctx *c)
282 splay_tree_delete (c->variables);
283 pointer_set_destroy (c->privatized_types);
284 XDELETE (c);
287 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
288 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
290 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
292 static void
293 append_to_statement_list_1 (tree t, tree *list_p)
295 tree list = *list_p;
296 tree_stmt_iterator i;
298 if (!list)
300 if (t && TREE_CODE (t) == STATEMENT_LIST)
302 *list_p = t;
303 return;
305 *list_p = list = alloc_stmt_list ();
308 i = tsi_last (list);
309 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
312 /* Add T to the end of the list container pointed to by LIST_P.
313 If T is an expression with no effects, it is ignored. */
315 void
316 append_to_statement_list (tree t, tree *list_p)
318 if (t && TREE_SIDE_EFFECTS (t))
319 append_to_statement_list_1 (t, list_p);
322 /* Similar, but the statement is always added, regardless of side effects. */
324 void
325 append_to_statement_list_force (tree t, tree *list_p)
327 if (t != NULL_TREE)
328 append_to_statement_list_1 (t, list_p);
331 /* Both gimplify the statement T and append it to LIST_P. */
333 void
334 gimplify_and_add (tree t, tree *list_p)
336 gimplify_stmt (&t);
337 append_to_statement_list (t, list_p);
340 /* Strip off a legitimate source ending from the input string NAME of
341 length LEN. Rather than having to know the names used by all of
342 our front ends, we strip off an ending of a period followed by
343 up to five characters. (Java uses ".class".) */
345 static inline void
346 remove_suffix (char *name, int len)
348 int i;
350 for (i = 2; i < 8 && len > i; i++)
352 if (name[len - i] == '.')
354 name[len - i] = '\0';
355 break;
360 /* Create a nameless artificial label and put it in the current function
361 context. Returns the newly created label. */
363 tree
364 create_artificial_label (void)
366 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
368 DECL_ARTIFICIAL (lab) = 1;
369 DECL_IGNORED_P (lab) = 1;
370 DECL_CONTEXT (lab) = current_function_decl;
371 return lab;
374 /* Subroutine for find_single_pointer_decl. */
376 static tree
377 find_single_pointer_decl_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
378 void *data)
380 tree *pdecl = (tree *) data;
382 if (DECL_P (*tp) && POINTER_TYPE_P (TREE_TYPE (*tp)))
384 if (*pdecl)
386 /* We already found a pointer decl; return anything other
387 than NULL_TREE to unwind from walk_tree signalling that
388 we have a duplicate. */
389 return *tp;
391 *pdecl = *tp;
394 return NULL_TREE;
397 /* Find the single DECL of pointer type in the tree T and return it.
398 If there are zero or more than one such DECLs, return NULL. */
400 static tree
401 find_single_pointer_decl (tree t)
403 tree decl = NULL_TREE;
405 if (walk_tree (&t, find_single_pointer_decl_1, &decl, NULL))
407 /* find_single_pointer_decl_1 returns a non-zero value, causing
408 walk_tree to return a non-zero value, to indicate that it
409 found more than one pointer DECL. */
410 return NULL_TREE;
413 return decl;
416 /* Create a new temporary name with PREFIX. Returns an identifier. */
418 static GTY(()) unsigned int tmp_var_id_num;
420 tree
421 create_tmp_var_name (const char *prefix)
423 char *tmp_name;
425 if (prefix)
427 char *preftmp = ASTRDUP (prefix);
429 remove_suffix (preftmp, strlen (preftmp));
430 prefix = preftmp;
433 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
434 return get_identifier (tmp_name);
438 /* Create a new temporary variable declaration of type TYPE.
439 Does NOT push it into the current binding. */
441 tree
442 create_tmp_var_raw (tree type, const char *prefix)
444 tree tmp_var;
445 tree new_type;
447 /* Make the type of the variable writable. */
448 new_type = build_type_variant (type, 0, 0);
449 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
451 tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
452 type);
454 /* The variable was declared by the compiler. */
455 DECL_ARTIFICIAL (tmp_var) = 1;
456 /* And we don't want debug info for it. */
457 DECL_IGNORED_P (tmp_var) = 1;
459 /* Make the variable writable. */
460 TREE_READONLY (tmp_var) = 0;
462 DECL_EXTERNAL (tmp_var) = 0;
463 TREE_STATIC (tmp_var) = 0;
464 TREE_USED (tmp_var) = 1;
466 return tmp_var;
469 /* Create a new temporary variable declaration of type TYPE. DOES push the
470 variable into the current binding. Further, assume that this is called
471 only from gimplification or optimization, at which point the creation of
472 certain types are bugs. */
474 tree
475 create_tmp_var (tree type, const char *prefix)
477 tree tmp_var;
479 /* We don't allow types that are addressable (meaning we can't make copies),
480 incomplete, or of variable size. */
481 gcc_assert (!TREE_ADDRESSABLE (type)
482 && COMPLETE_TYPE_P (type)
483 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
485 tmp_var = create_tmp_var_raw (type, prefix);
486 gimple_add_tmp_var (tmp_var);
487 return tmp_var;
490 /* Given a tree, try to return a useful variable name that we can use
491 to prefix a temporary that is being assigned the value of the tree.
492 I.E. given <temp> = &A, return A. */
494 const char *
495 get_name (tree t)
497 tree stripped_decl;
499 stripped_decl = t;
500 STRIP_NOPS (stripped_decl);
501 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
502 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
503 else
505 switch (TREE_CODE (stripped_decl))
507 case ADDR_EXPR:
508 return get_name (TREE_OPERAND (stripped_decl, 0));
509 break;
510 default:
511 return NULL;
516 /* Create a temporary with a name derived from VAL. Subroutine of
517 lookup_tmp_var; nobody else should call this function. */
519 static inline tree
520 create_tmp_from_val (tree val)
522 return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
525 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
526 an existing expression temporary. */
528 static tree
529 lookup_tmp_var (tree val, bool is_formal)
531 tree ret;
533 /* If not optimizing, never really reuse a temporary. local-alloc
534 won't allocate any variable that is used in more than one basic
535 block, which means it will go into memory, causing much extra
536 work in reload and final and poorer code generation, outweighing
537 the extra memory allocation here. */
538 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
539 ret = create_tmp_from_val (val);
540 else
542 elt_t elt, *elt_p;
543 void **slot;
545 elt.val = val;
546 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
547 if (*slot == NULL)
549 elt_p = XNEW (elt_t);
550 elt_p->val = val;
551 elt_p->temp = ret = create_tmp_from_val (val);
552 *slot = (void *) elt_p;
554 else
556 elt_p = (elt_t *) *slot;
557 ret = elt_p->temp;
561 if (is_formal)
562 DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
564 return ret;
567 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
568 in gimplify_expr. Only use this function if:
570 1) The value of the unfactored expression represented by VAL will not
571 change between the initialization and use of the temporary, and
572 2) The temporary will not be otherwise modified.
574 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
575 and #2 means it is inappropriate for && temps.
577 For other cases, use get_initialized_tmp_var instead. */
579 static tree
580 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
582 tree t, mod;
584 gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
586 t = lookup_tmp_var (val, is_formal);
588 if (is_formal)
590 tree u = find_single_pointer_decl (val);
592 if (u && TREE_CODE (u) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (u))
593 u = DECL_GET_RESTRICT_BASE (u);
594 if (u && TYPE_RESTRICT (TREE_TYPE (u)))
596 if (DECL_BASED_ON_RESTRICT_P (t))
597 gcc_assert (u == DECL_GET_RESTRICT_BASE (t));
598 else
600 DECL_BASED_ON_RESTRICT_P (t) = 1;
601 SET_DECL_RESTRICT_BASE (t, u);
606 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
607 DECL_COMPLEX_GIMPLE_REG_P (t) = 1;
609 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, val);
611 if (EXPR_HAS_LOCATION (val))
612 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
613 else
614 SET_EXPR_LOCATION (mod, input_location);
616 /* gimplify_modify_expr might want to reduce this further. */
617 gimplify_and_add (mod, pre_p);
619 /* If we're gimplifying into ssa, gimplify_modify_expr will have
620 given our temporary an ssa name. Find and return it. */
621 if (gimplify_ctxp->into_ssa)
622 t = TREE_OPERAND (mod, 0);
624 return t;
627 /* Returns a formal temporary variable initialized with VAL. PRE_P
628 points to a statement list where side-effects needed to compute VAL
629 should be stored. */
631 tree
632 get_formal_tmp_var (tree val, tree *pre_p)
634 return internal_get_tmp_var (val, pre_p, NULL, true);
637 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
638 are as in gimplify_expr. */
640 tree
641 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
643 return internal_get_tmp_var (val, pre_p, post_p, false);
646 /* Declares all the variables in VARS in SCOPE. */
648 void
649 declare_tmp_vars (tree vars, tree scope)
651 tree last = vars;
652 if (last)
654 tree temps;
656 /* C99 mode puts the default 'return 0;' for main outside the outer
657 braces. So drill down until we find an actual scope. */
658 while (TREE_CODE (scope) == COMPOUND_EXPR)
659 scope = TREE_OPERAND (scope, 0);
661 gcc_assert (TREE_CODE (scope) == BIND_EXPR);
663 temps = nreverse (last);
664 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
665 BIND_EXPR_VARS (scope) = temps;
669 void
670 gimple_add_tmp_var (tree tmp)
672 gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
674 DECL_CONTEXT (tmp) = current_function_decl;
675 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
677 if (gimplify_ctxp)
679 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
680 gimplify_ctxp->temps = tmp;
682 /* Mark temporaries local within the nearest enclosing parallel. */
683 if (gimplify_omp_ctxp)
685 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
686 while (ctx && !ctx->is_parallel)
687 ctx = ctx->outer_context;
688 if (ctx)
689 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
692 else if (cfun)
693 record_vars (tmp);
694 else
695 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
698 /* Determines whether to assign a locus to the statement STMT. */
700 static bool
701 should_carry_locus_p (tree stmt)
703 /* Don't emit a line note for a label. We particularly don't want to
704 emit one for the break label, since it doesn't actually correspond
705 to the beginning of the loop/switch. */
706 if (TREE_CODE (stmt) == LABEL_EXPR)
707 return false;
709 /* Do not annotate empty statements, since it confuses gcov. */
710 if (!TREE_SIDE_EFFECTS (stmt))
711 return false;
713 return true;
716 static void
717 annotate_one_with_locus (tree t, location_t locus)
719 if (EXPR_P (t) && ! EXPR_HAS_LOCATION (t) && should_carry_locus_p (t))
720 SET_EXPR_LOCATION (t, locus);
723 void
724 annotate_all_with_locus (tree *stmt_p, location_t locus)
726 tree_stmt_iterator i;
728 if (!*stmt_p)
729 return;
731 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
733 tree t = tsi_stmt (i);
735 /* Assuming we've already been gimplified, we shouldn't
736 see nested chaining constructs anymore. */
737 gcc_assert (TREE_CODE (t) != STATEMENT_LIST
738 && TREE_CODE (t) != COMPOUND_EXPR);
740 annotate_one_with_locus (t, locus);
744 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
745 These nodes model computations that should only be done once. If we
746 were to unshare something like SAVE_EXPR(i++), the gimplification
747 process would create wrong code. */
749 static tree
750 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
752 enum tree_code code = TREE_CODE (*tp);
753 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
754 if (TREE_CODE_CLASS (code) == tcc_type
755 || TREE_CODE_CLASS (code) == tcc_declaration
756 || TREE_CODE_CLASS (code) == tcc_constant
757 || code == SAVE_EXPR || code == TARGET_EXPR
758 /* We can't do anything sensible with a BLOCK used as an expression,
759 but we also can't just die when we see it because of non-expression
760 uses. So just avert our eyes and cross our fingers. Silly Java. */
761 || code == BLOCK)
762 *walk_subtrees = 0;
763 else
765 gcc_assert (code != BIND_EXPR);
766 copy_tree_r (tp, walk_subtrees, data);
769 return NULL_TREE;
772 /* Callback for walk_tree to unshare most of the shared trees rooted at
773 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
774 then *TP is deep copied by calling copy_tree_r.
776 This unshares the same trees as copy_tree_r with the exception of
777 SAVE_EXPR nodes. These nodes model computations that should only be
778 done once. If we were to unshare something like SAVE_EXPR(i++), the
779 gimplification process would create wrong code. */
781 static tree
782 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
783 void *data ATTRIBUTE_UNUSED)
785 tree t = *tp;
786 enum tree_code code = TREE_CODE (t);
788 /* Skip types, decls, and constants. But we do want to look at their
789 types and the bounds of types. Mark them as visited so we properly
790 unmark their subtrees on the unmark pass. If we've already seen them,
791 don't look down further. */
792 if (TREE_CODE_CLASS (code) == tcc_type
793 || TREE_CODE_CLASS (code) == tcc_declaration
794 || TREE_CODE_CLASS (code) == tcc_constant)
796 if (TREE_VISITED (t))
797 *walk_subtrees = 0;
798 else
799 TREE_VISITED (t) = 1;
802 /* If this node has been visited already, unshare it and don't look
803 any deeper. */
804 else if (TREE_VISITED (t))
806 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
807 *walk_subtrees = 0;
810 /* Otherwise, mark the tree as visited and keep looking. */
811 else
812 TREE_VISITED (t) = 1;
814 return NULL_TREE;
817 static tree
818 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
819 void *data ATTRIBUTE_UNUSED)
821 if (TREE_VISITED (*tp))
822 TREE_VISITED (*tp) = 0;
823 else
824 *walk_subtrees = 0;
826 return NULL_TREE;
829 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
830 bodies of any nested functions if we are unsharing the entire body of
831 FNDECL. */
833 static void
834 unshare_body (tree *body_p, tree fndecl)
836 struct cgraph_node *cgn = cgraph_node (fndecl);
838 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
839 if (body_p == &DECL_SAVED_TREE (fndecl))
840 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
841 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
844 /* Likewise, but mark all trees as not visited. */
846 static void
847 unvisit_body (tree *body_p, tree fndecl)
849 struct cgraph_node *cgn = cgraph_node (fndecl);
851 walk_tree (body_p, unmark_visited_r, NULL, NULL);
852 if (body_p == &DECL_SAVED_TREE (fndecl))
853 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
854 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
857 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
859 static void
860 unshare_all_trees (tree t)
862 walk_tree (&t, copy_if_shared_r, NULL, NULL);
863 walk_tree (&t, unmark_visited_r, NULL, NULL);
866 /* Unconditionally make an unshared copy of EXPR. This is used when using
867 stored expressions which span multiple functions, such as BINFO_VTABLE,
868 as the normal unsharing process can't tell that they're shared. */
870 tree
871 unshare_expr (tree expr)
873 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
874 return expr;
877 /* A terser interface for building a representation of an exception
878 specification. */
880 tree
881 gimple_build_eh_filter (tree body, tree allowed, tree failure)
883 tree t;
885 /* FIXME should the allowed types go in TREE_TYPE? */
886 t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
887 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
889 t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
890 append_to_statement_list (body, &TREE_OPERAND (t, 0));
892 return t;
896 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
897 contain statements and have a value. Assign its value to a temporary
898 and give it void_type_node. Returns the temporary, or NULL_TREE if
899 WRAPPER was already void. */
901 tree
902 voidify_wrapper_expr (tree wrapper, tree temp)
904 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
906 tree *p, sub = wrapper;
908 restart:
909 /* Set p to point to the body of the wrapper. */
910 switch (TREE_CODE (sub))
912 case BIND_EXPR:
913 /* For a BIND_EXPR, the body is operand 1. */
914 p = &BIND_EXPR_BODY (sub);
915 break;
917 default:
918 p = &TREE_OPERAND (sub, 0);
919 break;
922 /* Advance to the last statement. Set all container types to void. */
923 if (TREE_CODE (*p) == STATEMENT_LIST)
925 tree_stmt_iterator i = tsi_last (*p);
926 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
928 else
930 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
932 TREE_SIDE_EFFECTS (*p) = 1;
933 TREE_TYPE (*p) = void_type_node;
937 if (p == NULL || IS_EMPTY_STMT (*p))
939 /* Look through exception handling. */
940 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
941 || TREE_CODE (*p) == TRY_CATCH_EXPR)
943 sub = *p;
944 goto restart;
946 /* The C++ frontend already did this for us. */
947 else if (TREE_CODE (*p) == INIT_EXPR
948 || TREE_CODE (*p) == TARGET_EXPR)
949 temp = TREE_OPERAND (*p, 0);
950 /* If we're returning a dereference, move the dereference
951 outside the wrapper. */
952 else if (TREE_CODE (*p) == INDIRECT_REF)
954 tree ptr = TREE_OPERAND (*p, 0);
955 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
956 *p = build2 (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
957 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
958 /* If this is a BIND_EXPR for a const inline function, it might not
959 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
960 TREE_SIDE_EFFECTS (wrapper) = 1;
962 else
964 if (!temp)
965 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
966 *p = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
967 TREE_SIDE_EFFECTS (wrapper) = 1;
970 TREE_TYPE (wrapper) = void_type_node;
971 return temp;
974 return NULL_TREE;
977 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
978 a temporary through which they communicate. */
980 static void
981 build_stack_save_restore (tree *save, tree *restore)
983 tree save_call, tmp_var;
985 save_call =
986 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
987 NULL_TREE);
988 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
990 *save = build2 (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
991 *restore =
992 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
993 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
996 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
998 static enum gimplify_status
999 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
1001 tree bind_expr = *expr_p;
1002 bool old_save_stack = gimplify_ctxp->save_stack;
1003 tree t;
1005 temp = voidify_wrapper_expr (bind_expr, temp);
1007 /* Mark variables seen in this bind expr. */
1008 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1010 if (TREE_CODE (t) == VAR_DECL)
1011 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1013 /* Preliminarily mark non-addressed complex variables as eligible
1014 for promotion to gimple registers. We'll transform their uses
1015 as we find them. */
1016 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1017 && !TREE_THIS_VOLATILE (t)
1018 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1019 && !needs_to_live_in_memory (t))
1020 DECL_COMPLEX_GIMPLE_REG_P (t) = 1;
1023 /* Mark variables seen in this bind expr as locals. */
1024 if (gimplify_omp_ctxp)
1026 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1028 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1029 if (TREE_CODE (t) == VAR_DECL && !is_global_var (t))
1030 omp_add_variable (ctx, t, GOVD_LOCAL | GOVD_SEEN);
1033 gimple_push_bind_expr (bind_expr);
1034 gimplify_ctxp->save_stack = false;
1036 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
1038 if (gimplify_ctxp->save_stack)
1040 tree stack_save, stack_restore;
1042 /* Save stack on entry and restore it on exit. Add a try_finally
1043 block to achieve this. Note that mudflap depends on the
1044 format of the emitted code: see mx_register_decls(). */
1045 build_stack_save_restore (&stack_save, &stack_restore);
1047 t = build2 (TRY_FINALLY_EXPR, void_type_node,
1048 BIND_EXPR_BODY (bind_expr), NULL_TREE);
1049 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
1051 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
1052 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
1053 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
1056 gimplify_ctxp->save_stack = old_save_stack;
1057 gimple_pop_bind_expr ();
1059 if (temp)
1061 *expr_p = temp;
1062 append_to_statement_list (bind_expr, pre_p);
1063 return GS_OK;
1065 else
1066 return GS_ALL_DONE;
1069 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1070 GIMPLE value, it is assigned to a new temporary and the statement is
1071 re-written to return the temporary.
1073 PRE_P points to the list where side effects that must happen before
1074 STMT should be stored. */
1076 static enum gimplify_status
1077 gimplify_return_expr (tree stmt, tree *pre_p)
1079 tree ret_expr = TREE_OPERAND (stmt, 0);
1080 tree result_decl, result;
1082 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
1083 || ret_expr == error_mark_node)
1084 return GS_ALL_DONE;
1086 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1087 result_decl = NULL_TREE;
1088 else
1090 result_decl = TREE_OPERAND (ret_expr, 0);
1091 if (TREE_CODE (result_decl) == INDIRECT_REF)
1092 /* See through a return by reference. */
1093 result_decl = TREE_OPERAND (result_decl, 0);
1095 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1096 || TREE_CODE (ret_expr) == INIT_EXPR)
1097 && TREE_CODE (result_decl) == RESULT_DECL);
1100 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1101 Recall that aggregate_value_p is FALSE for any aggregate type that is
1102 returned in registers. If we're returning values in registers, then
1103 we don't want to extend the lifetime of the RESULT_DECL, particularly
1104 across another call. In addition, for those aggregates for which
1105 hard_function_value generates a PARALLEL, we'll die during normal
1106 expansion of structure assignments; there's special code in expand_return
1107 to handle this case that does not exist in expand_expr. */
1108 if (!result_decl
1109 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1110 result = result_decl;
1111 else if (gimplify_ctxp->return_temp)
1112 result = gimplify_ctxp->return_temp;
1113 else
1115 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
1117 /* ??? With complex control flow (usually involving abnormal edges),
1118 we can wind up warning about an uninitialized value for this. Due
1119 to how this variable is constructed and initialized, this is never
1120 true. Give up and never warn. */
1121 TREE_NO_WARNING (result) = 1;
1123 gimplify_ctxp->return_temp = result;
1126 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1127 Then gimplify the whole thing. */
1128 if (result != result_decl)
1129 TREE_OPERAND (ret_expr, 0) = result;
1131 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1133 /* If we didn't use a temporary, then the result is just the result_decl.
1134 Otherwise we need a simple copy. This should already be gimple. */
1135 if (result == result_decl)
1136 ret_expr = result;
1137 else
1138 ret_expr = build2 (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
1139 TREE_OPERAND (stmt, 0) = ret_expr;
1141 return GS_ALL_DONE;
1144 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1145 and initialization explicit. */
1147 static enum gimplify_status
1148 gimplify_decl_expr (tree *stmt_p)
1150 tree stmt = *stmt_p;
1151 tree decl = DECL_EXPR_DECL (stmt);
1153 *stmt_p = NULL_TREE;
1155 if (TREE_TYPE (decl) == error_mark_node)
1156 return GS_ERROR;
1158 if ((TREE_CODE (decl) == TYPE_DECL
1159 || TREE_CODE (decl) == VAR_DECL)
1160 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1161 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1163 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1165 tree init = DECL_INITIAL (decl);
1167 if (!TREE_CONSTANT (DECL_SIZE (decl)))
1169 /* This is a variable-sized decl. Simplify its size and mark it
1170 for deferred expansion. Note that mudflap depends on the format
1171 of the emitted code: see mx_register_decls(). */
1172 tree t, args, addr, ptr_type;
1174 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
1175 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
1177 /* All occurrences of this decl in final gimplified code will be
1178 replaced by indirection. Setting DECL_VALUE_EXPR does two
1179 things: First, it lets the rest of the gimplifier know what
1180 replacement to use. Second, it lets the debug info know
1181 where to find the value. */
1182 ptr_type = build_pointer_type (TREE_TYPE (decl));
1183 addr = create_tmp_var (ptr_type, get_name (decl));
1184 DECL_IGNORED_P (addr) = 0;
1185 t = build_fold_indirect_ref (addr);
1186 SET_DECL_VALUE_EXPR (decl, t);
1187 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1189 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
1190 t = built_in_decls[BUILT_IN_ALLOCA];
1191 t = build_function_call_expr (t, args);
1192 t = fold_convert (ptr_type, t);
1193 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1195 gimplify_and_add (t, stmt_p);
1197 /* Indicate that we need to restore the stack level when the
1198 enclosing BIND_EXPR is exited. */
1199 gimplify_ctxp->save_stack = true;
1202 if (init && init != error_mark_node)
1204 if (!TREE_STATIC (decl))
1206 DECL_INITIAL (decl) = NULL_TREE;
1207 init = build2 (INIT_EXPR, void_type_node, decl, init);
1208 gimplify_and_add (init, stmt_p);
1210 else
1211 /* We must still examine initializers for static variables
1212 as they may contain a label address. */
1213 walk_tree (&init, force_labels_r, NULL, NULL);
1216 /* This decl isn't mentioned in the enclosing block, so add it to the
1217 list of temps. FIXME it seems a bit of a kludge to say that
1218 anonymous artificial vars aren't pushed, but everything else is. */
1219 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1220 gimple_add_tmp_var (decl);
1223 return GS_ALL_DONE;
1226 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1227 and replacing the LOOP_EXPR with goto, but if the loop contains an
1228 EXIT_EXPR, we need to append a label for it to jump to. */
1230 static enum gimplify_status
1231 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1233 tree saved_label = gimplify_ctxp->exit_label;
1234 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1235 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1237 append_to_statement_list (start_label, pre_p);
1239 gimplify_ctxp->exit_label = NULL_TREE;
1241 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1243 if (gimplify_ctxp->exit_label)
1245 append_to_statement_list (jump_stmt, pre_p);
1246 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1248 else
1249 *expr_p = jump_stmt;
1251 gimplify_ctxp->exit_label = saved_label;
1253 return GS_ALL_DONE;
1256 /* Compare two case labels. Because the front end should already have
1257 made sure that case ranges do not overlap, it is enough to only compare
1258 the CASE_LOW values of each case label. */
1260 static int
1261 compare_case_labels (const void *p1, const void *p2)
1263 tree case1 = *(tree *)p1;
1264 tree case2 = *(tree *)p2;
1266 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1269 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1271 void
1272 sort_case_labels (tree label_vec)
1274 size_t len = TREE_VEC_LENGTH (label_vec);
1275 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1277 if (CASE_LOW (default_case))
1279 size_t i;
1281 /* The last label in the vector should be the default case
1282 but it is not. */
1283 for (i = 0; i < len; ++i)
1285 tree t = TREE_VEC_ELT (label_vec, i);
1286 if (!CASE_LOW (t))
1288 default_case = t;
1289 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1290 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1291 break;
1296 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1297 compare_case_labels);
1300 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1301 branch to. */
1303 static enum gimplify_status
1304 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1306 tree switch_expr = *expr_p;
1307 enum gimplify_status ret;
1309 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1310 is_gimple_val, fb_rvalue);
1312 if (SWITCH_BODY (switch_expr))
1314 VEC(tree,heap) *labels, *saved_labels;
1315 tree label_vec, default_case = NULL_TREE;
1316 size_t i, len;
1318 /* If someone can be bothered to fill in the labels, they can
1319 be bothered to null out the body too. */
1320 gcc_assert (!SWITCH_LABELS (switch_expr));
1322 saved_labels = gimplify_ctxp->case_labels;
1323 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1325 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1327 labels = gimplify_ctxp->case_labels;
1328 gimplify_ctxp->case_labels = saved_labels;
1330 len = VEC_length (tree, labels);
1332 for (i = 0; i < len; ++i)
1334 tree t = VEC_index (tree, labels, i);
1335 if (!CASE_LOW (t))
1337 /* The default case must be the last label in the list. */
1338 default_case = t;
1339 VEC_replace (tree, labels, i, VEC_index (tree, labels, len - 1));
1340 len--;
1341 break;
1345 label_vec = make_tree_vec (len + 1);
1346 SWITCH_LABELS (*expr_p) = label_vec;
1347 append_to_statement_list (switch_expr, pre_p);
1349 if (! default_case)
1351 /* If the switch has no default label, add one, so that we jump
1352 around the switch body. */
1353 default_case = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1354 NULL_TREE, create_artificial_label ());
1355 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1356 *expr_p = build1 (LABEL_EXPR, void_type_node,
1357 CASE_LABEL (default_case));
1359 else
1360 *expr_p = SWITCH_BODY (switch_expr);
1362 for (i = 0; i < len; ++i)
1363 TREE_VEC_ELT (label_vec, i) = VEC_index (tree, labels, i);
1364 TREE_VEC_ELT (label_vec, len) = default_case;
1366 VEC_free (tree, heap, labels);
1368 sort_case_labels (label_vec);
1370 SWITCH_BODY (switch_expr) = NULL;
1372 else
1373 gcc_assert (SWITCH_LABELS (switch_expr));
1375 return ret;
1378 static enum gimplify_status
1379 gimplify_case_label_expr (tree *expr_p)
1381 tree expr = *expr_p;
1382 struct gimplify_ctx *ctxp;
1384 /* Invalid OpenMP programs can play Duff's Device type games with
1385 #pragma omp parallel. At least in the C front end, we don't
1386 detect such invalid branches until after gimplification. */
1387 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1388 if (ctxp->case_labels)
1389 break;
1391 VEC_safe_push (tree, heap, ctxp->case_labels, expr);
1392 *expr_p = build1 (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1393 return GS_ALL_DONE;
1396 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1397 if necessary. */
1399 tree
1400 build_and_jump (tree *label_p)
1402 if (label_p == NULL)
1403 /* If there's nowhere to jump, just fall through. */
1404 return NULL_TREE;
1406 if (*label_p == NULL_TREE)
1408 tree label = create_artificial_label ();
1409 *label_p = label;
1412 return build1 (GOTO_EXPR, void_type_node, *label_p);
1415 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1416 This also involves building a label to jump to and communicating it to
1417 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1419 static enum gimplify_status
1420 gimplify_exit_expr (tree *expr_p)
1422 tree cond = TREE_OPERAND (*expr_p, 0);
1423 tree expr;
1425 expr = build_and_jump (&gimplify_ctxp->exit_label);
1426 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1427 *expr_p = expr;
1429 return GS_OK;
1432 /* A helper function to be called via walk_tree. Mark all labels under *TP
1433 as being forced. To be called for DECL_INITIAL of static variables. */
1435 tree
1436 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1438 if (TYPE_P (*tp))
1439 *walk_subtrees = 0;
1440 if (TREE_CODE (*tp) == LABEL_DECL)
1441 FORCED_LABEL (*tp) = 1;
1443 return NULL_TREE;
1446 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1447 different from its canonical type, wrap the whole thing inside a
1448 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1449 type.
1451 The canonical type of a COMPONENT_REF is the type of the field being
1452 referenced--unless the field is a bit-field which can be read directly
1453 in a smaller mode, in which case the canonical type is the
1454 sign-appropriate type corresponding to that mode. */
1456 static void
1457 canonicalize_component_ref (tree *expr_p)
1459 tree expr = *expr_p;
1460 tree type;
1462 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1464 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1465 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1466 else
1467 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1469 if (TREE_TYPE (expr) != type)
1471 tree old_type = TREE_TYPE (expr);
1473 /* Set the type of the COMPONENT_REF to the underlying type. */
1474 TREE_TYPE (expr) = type;
1476 /* And wrap the whole thing inside a NOP_EXPR. */
1477 expr = build1 (NOP_EXPR, old_type, expr);
1479 *expr_p = expr;
1483 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1484 to foo, embed that change in the ADDR_EXPR by converting
1485 T array[U];
1486 (T *)&array
1488 &array[L]
1489 where L is the lower bound. For simplicity, only do this for constant
1490 lower bound. */
1492 static void
1493 canonicalize_addr_expr (tree *expr_p)
1495 tree expr = *expr_p;
1496 tree ctype = TREE_TYPE (expr);
1497 tree addr_expr = TREE_OPERAND (expr, 0);
1498 tree atype = TREE_TYPE (addr_expr);
1499 tree dctype, datype, ddatype, otype, obj_expr;
1501 /* Both cast and addr_expr types should be pointers. */
1502 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1503 return;
1505 /* The addr_expr type should be a pointer to an array. */
1506 datype = TREE_TYPE (atype);
1507 if (TREE_CODE (datype) != ARRAY_TYPE)
1508 return;
1510 /* Both cast and addr_expr types should address the same object type. */
1511 dctype = TREE_TYPE (ctype);
1512 ddatype = TREE_TYPE (datype);
1513 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1514 return;
1516 /* The addr_expr and the object type should match. */
1517 obj_expr = TREE_OPERAND (addr_expr, 0);
1518 otype = TREE_TYPE (obj_expr);
1519 if (!lang_hooks.types_compatible_p (otype, datype))
1520 return;
1522 /* The lower bound and element sizes must be constant. */
1523 if (!TYPE_SIZE_UNIT (dctype)
1524 || TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1525 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1526 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1527 return;
1529 /* All checks succeeded. Build a new node to merge the cast. */
1530 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1531 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1532 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1533 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1534 size_int (TYPE_ALIGN_UNIT (dctype))));
1535 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1538 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1539 underneath as appropriate. */
1541 static enum gimplify_status
1542 gimplify_conversion (tree *expr_p)
1544 gcc_assert (TREE_CODE (*expr_p) == NOP_EXPR
1545 || TREE_CODE (*expr_p) == CONVERT_EXPR);
1547 /* Then strip away all but the outermost conversion. */
1548 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1550 /* And remove the outermost conversion if it's useless. */
1551 if (tree_ssa_useless_type_conversion (*expr_p))
1552 *expr_p = TREE_OPERAND (*expr_p, 0);
1554 /* If we still have a conversion at the toplevel,
1555 then canonicalize some constructs. */
1556 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1558 tree sub = TREE_OPERAND (*expr_p, 0);
1560 /* If a NOP conversion is changing the type of a COMPONENT_REF
1561 expression, then canonicalize its type now in order to expose more
1562 redundant conversions. */
1563 if (TREE_CODE (sub) == COMPONENT_REF)
1564 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1566 /* If a NOP conversion is changing a pointer to array of foo
1567 to a pointer to foo, embed that change in the ADDR_EXPR. */
1568 else if (TREE_CODE (sub) == ADDR_EXPR)
1569 canonicalize_addr_expr (expr_p);
1572 return GS_OK;
1575 /* Gimplify a VAR_DECL or PARM_DECL. Returns GS_OK if we expanded a
1576 DECL_VALUE_EXPR, and it's worth re-examining things. */
1578 static enum gimplify_status
1579 gimplify_var_or_parm_decl (tree *expr_p)
1581 tree decl = *expr_p;
1583 /* ??? If this is a local variable, and it has not been seen in any
1584 outer BIND_EXPR, then it's probably the result of a duplicate
1585 declaration, for which we've already issued an error. It would
1586 be really nice if the front end wouldn't leak these at all.
1587 Currently the only known culprit is C++ destructors, as seen
1588 in g++.old-deja/g++.jason/binding.C. */
1589 if (TREE_CODE (decl) == VAR_DECL
1590 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1591 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1592 && decl_function_context (decl) == current_function_decl)
1594 gcc_assert (errorcount || sorrycount);
1595 return GS_ERROR;
1598 /* When within an OpenMP context, notice uses of variables. */
1599 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1600 return GS_ALL_DONE;
1602 /* If the decl is an alias for another expression, substitute it now. */
1603 if (DECL_HAS_VALUE_EXPR_P (decl))
1605 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
1606 return GS_OK;
1609 return GS_ALL_DONE;
1613 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1614 node pointed to by EXPR_P.
1616 compound_lval
1617 : min_lval '[' val ']'
1618 | min_lval '.' ID
1619 | compound_lval '[' val ']'
1620 | compound_lval '.' ID
1622 This is not part of the original SIMPLE definition, which separates
1623 array and member references, but it seems reasonable to handle them
1624 together. Also, this way we don't run into problems with union
1625 aliasing; gcc requires that for accesses through a union to alias, the
1626 union reference must be explicit, which was not always the case when we
1627 were splitting up array and member refs.
1629 PRE_P points to the list where side effects that must happen before
1630 *EXPR_P should be stored.
1632 POST_P points to the list where side effects that must happen after
1633 *EXPR_P should be stored. */
1635 static enum gimplify_status
1636 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1637 tree *post_p, fallback_t fallback)
1639 tree *p;
1640 VEC(tree,heap) *stack;
1641 enum gimplify_status ret = GS_OK, tret;
1642 int i;
1644 /* Create a stack of the subexpressions so later we can walk them in
1645 order from inner to outer. */
1646 stack = VEC_alloc (tree, heap, 10);
1648 /* We can handle anything that get_inner_reference can deal with. */
1649 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1651 restart:
1652 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1653 if (TREE_CODE (*p) == INDIRECT_REF)
1654 *p = fold_indirect_ref (*p);
1656 if (handled_component_p (*p))
1658 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1659 additional COMPONENT_REFs. */
1660 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1661 && gimplify_var_or_parm_decl (p) == GS_OK)
1662 goto restart;
1663 else
1664 break;
1666 VEC_safe_push (tree, heap, stack, *p);
1669 gcc_assert (VEC_length (tree, stack));
1671 /* Now STACK is a stack of pointers to all the refs we've walked through
1672 and P points to the innermost expression.
1674 Java requires that we elaborated nodes in source order. That
1675 means we must gimplify the inner expression followed by each of
1676 the indices, in order. But we can't gimplify the inner
1677 expression until we deal with any variable bounds, sizes, or
1678 positions in order to deal with PLACEHOLDER_EXPRs.
1680 So we do this in three steps. First we deal with the annotations
1681 for any variables in the components, then we gimplify the base,
1682 then we gimplify any indices, from left to right. */
1683 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
1685 tree t = VEC_index (tree, stack, i);
1687 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1689 /* Gimplify the low bound and element type size and put them into
1690 the ARRAY_REF. If these values are set, they have already been
1691 gimplified. */
1692 if (!TREE_OPERAND (t, 2))
1694 tree low = unshare_expr (array_ref_low_bound (t));
1695 if (!is_gimple_min_invariant (low))
1697 TREE_OPERAND (t, 2) = low;
1698 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1699 is_gimple_formal_tmp_reg, fb_rvalue);
1700 ret = MIN (ret, tret);
1704 if (!TREE_OPERAND (t, 3))
1706 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1707 tree elmt_size = unshare_expr (array_ref_element_size (t));
1708 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1710 /* Divide the element size by the alignment of the element
1711 type (above). */
1712 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1714 if (!is_gimple_min_invariant (elmt_size))
1716 TREE_OPERAND (t, 3) = elmt_size;
1717 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1718 is_gimple_formal_tmp_reg, fb_rvalue);
1719 ret = MIN (ret, tret);
1723 else if (TREE_CODE (t) == COMPONENT_REF)
1725 /* Set the field offset into T and gimplify it. */
1726 if (!TREE_OPERAND (t, 2))
1728 tree offset = unshare_expr (component_ref_field_offset (t));
1729 tree field = TREE_OPERAND (t, 1);
1730 tree factor
1731 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1733 /* Divide the offset by its alignment. */
1734 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1736 if (!is_gimple_min_invariant (offset))
1738 TREE_OPERAND (t, 2) = offset;
1739 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1740 is_gimple_formal_tmp_reg, fb_rvalue);
1741 ret = MIN (ret, tret);
1747 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1748 so as to match the min_lval predicate. Failure to do so may result
1749 in the creation of large aggregate temporaries. */
1750 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1751 fallback | fb_lvalue);
1752 ret = MIN (ret, tret);
1754 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1755 loop we also remove any useless conversions. */
1756 for (; VEC_length (tree, stack) > 0; )
1758 tree t = VEC_pop (tree, stack);
1760 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1762 /* Gimplify the dimension.
1763 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1764 Gimplify non-constant array indices into a temporary
1765 variable.
1766 FIXME - The real fix is to gimplify post-modify
1767 expressions into a minimal gimple lvalue. However, that
1768 exposes bugs in alias analysis. The alias analyzer does
1769 not handle &PTR->FIELD very well. Will fix after the
1770 branch is merged into mainline (dnovillo 2004-05-03). */
1771 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1773 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1774 is_gimple_formal_tmp_reg, fb_rvalue);
1775 ret = MIN (ret, tret);
1778 else if (TREE_CODE (t) == BIT_FIELD_REF)
1780 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1781 is_gimple_val, fb_rvalue);
1782 ret = MIN (ret, tret);
1783 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1784 is_gimple_val, fb_rvalue);
1785 ret = MIN (ret, tret);
1788 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1790 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1791 set which would have caused all the outer expressions in EXPR_P
1792 leading to P to also have had TREE_SIDE_EFFECTS set. */
1793 recalculate_side_effects (t);
1796 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1797 ret = MIN (ret, tret);
1799 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1800 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1802 canonicalize_component_ref (expr_p);
1803 ret = MIN (ret, GS_OK);
1806 VEC_free (tree, heap, stack);
1808 return ret;
1811 /* Gimplify the self modifying expression pointed to by EXPR_P
1812 (++, --, +=, -=).
1814 PRE_P points to the list where side effects that must happen before
1815 *EXPR_P should be stored.
1817 POST_P points to the list where side effects that must happen after
1818 *EXPR_P should be stored.
1820 WANT_VALUE is nonzero iff we want to use the value of this expression
1821 in another expression. */
1823 static enum gimplify_status
1824 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1825 bool want_value)
1827 enum tree_code code;
1828 tree lhs, lvalue, rhs, t1;
1829 bool postfix;
1830 enum tree_code arith_code;
1831 enum gimplify_status ret;
1833 code = TREE_CODE (*expr_p);
1835 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1836 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1838 /* Prefix or postfix? */
1839 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1840 /* Faster to treat as prefix if result is not used. */
1841 postfix = want_value;
1842 else
1843 postfix = false;
1845 /* Add or subtract? */
1846 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1847 arith_code = PLUS_EXPR;
1848 else
1849 arith_code = MINUS_EXPR;
1851 /* Gimplify the LHS into a GIMPLE lvalue. */
1852 lvalue = TREE_OPERAND (*expr_p, 0);
1853 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1854 if (ret == GS_ERROR)
1855 return ret;
1857 /* Extract the operands to the arithmetic operation. */
1858 lhs = lvalue;
1859 rhs = TREE_OPERAND (*expr_p, 1);
1861 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1862 that as the result value and in the postqueue operation. */
1863 if (postfix)
1865 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1866 if (ret == GS_ERROR)
1867 return ret;
1870 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1871 t1 = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1873 if (postfix)
1875 gimplify_and_add (t1, post_p);
1876 *expr_p = lhs;
1877 return GS_ALL_DONE;
1879 else
1881 *expr_p = t1;
1882 return GS_OK;
1886 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1888 static void
1889 maybe_with_size_expr (tree *expr_p)
1891 tree expr = *expr_p;
1892 tree type = TREE_TYPE (expr);
1893 tree size;
1895 /* If we've already wrapped this or the type is error_mark_node, we can't do
1896 anything. */
1897 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1898 || type == error_mark_node)
1899 return;
1901 /* If the size isn't known or is a constant, we have nothing to do. */
1902 size = TYPE_SIZE_UNIT (type);
1903 if (!size || TREE_CODE (size) == INTEGER_CST)
1904 return;
1906 /* Otherwise, make a WITH_SIZE_EXPR. */
1907 size = unshare_expr (size);
1908 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1909 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1912 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1914 static enum gimplify_status
1915 gimplify_arg (tree *expr_p, tree *pre_p)
1917 bool (*test) (tree);
1918 fallback_t fb;
1920 /* In general, we allow lvalues for function arguments to avoid
1921 extra overhead of copying large aggregates out of even larger
1922 aggregates into temporaries only to copy the temporaries to
1923 the argument list. Make optimizers happy by pulling out to
1924 temporaries those types that fit in registers. */
1925 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
1926 test = is_gimple_val, fb = fb_rvalue;
1927 else
1928 test = is_gimple_lvalue, fb = fb_either;
1930 /* If this is a variable sized type, we must remember the size. */
1931 maybe_with_size_expr (expr_p);
1933 /* There is a sequence point before a function call. Side effects in
1934 the argument list must occur before the actual call. So, when
1935 gimplifying arguments, force gimplify_expr to use an internal
1936 post queue which is then appended to the end of PRE_P. */
1937 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
1940 /* Gimplify the CALL_EXPR node pointed to by EXPR_P. PRE_P points to the
1941 list where side effects that must happen before *EXPR_P should be stored.
1942 WANT_VALUE is true if the result of the call is desired. */
1944 static enum gimplify_status
1945 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1947 tree decl;
1948 tree arglist;
1949 enum gimplify_status ret;
1951 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
1953 /* For reliable diagnostics during inlining, it is necessary that
1954 every call_expr be annotated with file and line. */
1955 if (! EXPR_HAS_LOCATION (*expr_p))
1956 SET_EXPR_LOCATION (*expr_p, input_location);
1958 /* This may be a call to a builtin function.
1960 Builtin function calls may be transformed into different
1961 (and more efficient) builtin function calls under certain
1962 circumstances. Unfortunately, gimplification can muck things
1963 up enough that the builtin expanders are not aware that certain
1964 transformations are still valid.
1966 So we attempt transformation/gimplification of the call before
1967 we gimplify the CALL_EXPR. At this time we do not manage to
1968 transform all calls in the same manner as the expanders do, but
1969 we do transform most of them. */
1970 decl = get_callee_fndecl (*expr_p);
1971 if (decl && DECL_BUILT_IN (decl))
1973 tree arglist = TREE_OPERAND (*expr_p, 1);
1974 tree new = fold_builtin (decl, arglist, !want_value);
1976 if (new && new != *expr_p)
1978 /* There was a transformation of this call which computes the
1979 same value, but in a more efficient way. Return and try
1980 again. */
1981 *expr_p = new;
1982 return GS_OK;
1985 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1986 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
1988 if (!arglist || !TREE_CHAIN (arglist))
1990 error ("too few arguments to function %<va_start%>");
1991 *expr_p = build_empty_stmt ();
1992 return GS_OK;
1995 if (fold_builtin_next_arg (TREE_CHAIN (arglist)))
1997 *expr_p = build_empty_stmt ();
1998 return GS_OK;
2000 /* Avoid gimplifying the second argument to va_start, which needs
2001 to be the plain PARM_DECL. */
2002 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
2006 /* There is a sequence point before the call, so any side effects in
2007 the calling expression must occur before the actual call. Force
2008 gimplify_expr to use an internal post queue. */
2009 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2010 is_gimple_call_addr, fb_rvalue);
2012 if (PUSH_ARGS_REVERSED)
2013 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2014 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2015 arglist = TREE_CHAIN (arglist))
2017 enum gimplify_status t;
2019 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
2021 if (t == GS_ERROR)
2022 ret = GS_ERROR;
2024 if (PUSH_ARGS_REVERSED)
2025 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2027 /* Try this again in case gimplification exposed something. */
2028 if (ret != GS_ERROR)
2030 decl = get_callee_fndecl (*expr_p);
2031 if (decl && DECL_BUILT_IN (decl))
2033 tree arglist = TREE_OPERAND (*expr_p, 1);
2034 tree new = fold_builtin (decl, arglist, !want_value);
2036 if (new && new != *expr_p)
2038 /* There was a transformation of this call which computes the
2039 same value, but in a more efficient way. Return and try
2040 again. */
2041 *expr_p = new;
2042 return GS_OK;
2047 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2048 decl. This allows us to eliminate redundant or useless
2049 calls to "const" functions. */
2050 if (TREE_CODE (*expr_p) == CALL_EXPR
2051 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2052 TREE_SIDE_EFFECTS (*expr_p) = 0;
2054 return ret;
2057 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2058 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2060 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2061 condition is true or false, respectively. If null, we should generate
2062 our own to skip over the evaluation of this specific expression.
2064 This function is the tree equivalent of do_jump.
2066 shortcut_cond_r should only be called by shortcut_cond_expr. */
2068 static tree
2069 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2071 tree local_label = NULL_TREE;
2072 tree t, expr = NULL;
2074 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2075 retain the shortcut semantics. Just insert the gotos here;
2076 shortcut_cond_expr will append the real blocks later. */
2077 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2079 /* Turn if (a && b) into
2081 if (a); else goto no;
2082 if (b) goto yes; else goto no;
2083 (no:) */
2085 if (false_label_p == NULL)
2086 false_label_p = &local_label;
2088 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2089 append_to_statement_list (t, &expr);
2091 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2092 false_label_p);
2093 append_to_statement_list (t, &expr);
2095 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2097 /* Turn if (a || b) into
2099 if (a) goto yes;
2100 if (b) goto yes; else goto no;
2101 (yes:) */
2103 if (true_label_p == NULL)
2104 true_label_p = &local_label;
2106 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2107 append_to_statement_list (t, &expr);
2109 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2110 false_label_p);
2111 append_to_statement_list (t, &expr);
2113 else if (TREE_CODE (pred) == COND_EXPR)
2115 /* As long as we're messing with gotos, turn if (a ? b : c) into
2116 if (a)
2117 if (b) goto yes; else goto no;
2118 else
2119 if (c) goto yes; else goto no; */
2120 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2121 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2122 false_label_p),
2123 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2124 false_label_p));
2126 else
2128 expr = build3 (COND_EXPR, void_type_node, pred,
2129 build_and_jump (true_label_p),
2130 build_and_jump (false_label_p));
2133 if (local_label)
2135 t = build1 (LABEL_EXPR, void_type_node, local_label);
2136 append_to_statement_list (t, &expr);
2139 return expr;
2142 static tree
2143 shortcut_cond_expr (tree expr)
2145 tree pred = TREE_OPERAND (expr, 0);
2146 tree then_ = TREE_OPERAND (expr, 1);
2147 tree else_ = TREE_OPERAND (expr, 2);
2148 tree true_label, false_label, end_label, t;
2149 tree *true_label_p;
2150 tree *false_label_p;
2151 bool emit_end, emit_false, jump_over_else;
2152 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2153 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2155 /* First do simple transformations. */
2156 if (!else_se)
2158 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2159 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2161 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2162 then_ = shortcut_cond_expr (expr);
2163 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2164 pred = TREE_OPERAND (pred, 0);
2165 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2168 if (!then_se)
2170 /* If there is no 'then', turn
2171 if (a || b); else d
2172 into
2173 if (a); else if (b); else d. */
2174 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2176 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2177 else_ = shortcut_cond_expr (expr);
2178 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2179 pred = TREE_OPERAND (pred, 0);
2180 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2184 /* If we're done, great. */
2185 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2186 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2187 return expr;
2189 /* Otherwise we need to mess with gotos. Change
2190 if (a) c; else d;
2192 if (a); else goto no;
2193 c; goto end;
2194 no: d; end:
2195 and recursively gimplify the condition. */
2197 true_label = false_label = end_label = NULL_TREE;
2199 /* If our arms just jump somewhere, hijack those labels so we don't
2200 generate jumps to jumps. */
2202 if (then_
2203 && TREE_CODE (then_) == GOTO_EXPR
2204 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2206 true_label = GOTO_DESTINATION (then_);
2207 then_ = NULL;
2208 then_se = false;
2211 if (else_
2212 && TREE_CODE (else_) == GOTO_EXPR
2213 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2215 false_label = GOTO_DESTINATION (else_);
2216 else_ = NULL;
2217 else_se = false;
2220 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2221 if (true_label)
2222 true_label_p = &true_label;
2223 else
2224 true_label_p = NULL;
2226 /* The 'else' branch also needs a label if it contains interesting code. */
2227 if (false_label || else_se)
2228 false_label_p = &false_label;
2229 else
2230 false_label_p = NULL;
2232 /* If there was nothing else in our arms, just forward the label(s). */
2233 if (!then_se && !else_se)
2234 return shortcut_cond_r (pred, true_label_p, false_label_p);
2236 /* If our last subexpression already has a terminal label, reuse it. */
2237 if (else_se)
2238 expr = expr_last (else_);
2239 else if (then_se)
2240 expr = expr_last (then_);
2241 else
2242 expr = NULL;
2243 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2244 end_label = LABEL_EXPR_LABEL (expr);
2246 /* If we don't care about jumping to the 'else' branch, jump to the end
2247 if the condition is false. */
2248 if (!false_label_p)
2249 false_label_p = &end_label;
2251 /* We only want to emit these labels if we aren't hijacking them. */
2252 emit_end = (end_label == NULL_TREE);
2253 emit_false = (false_label == NULL_TREE);
2255 /* We only emit the jump over the else clause if we have to--if the
2256 then clause may fall through. Otherwise we can wind up with a
2257 useless jump and a useless label at the end of gimplified code,
2258 which will cause us to think that this conditional as a whole
2259 falls through even if it doesn't. If we then inline a function
2260 which ends with such a condition, that can cause us to issue an
2261 inappropriate warning about control reaching the end of a
2262 non-void function. */
2263 jump_over_else = block_may_fallthru (then_);
2265 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2267 expr = NULL;
2268 append_to_statement_list (pred, &expr);
2270 append_to_statement_list (then_, &expr);
2271 if (else_se)
2273 if (jump_over_else)
2275 t = build_and_jump (&end_label);
2276 append_to_statement_list (t, &expr);
2278 if (emit_false)
2280 t = build1 (LABEL_EXPR, void_type_node, false_label);
2281 append_to_statement_list (t, &expr);
2283 append_to_statement_list (else_, &expr);
2285 if (emit_end && end_label)
2287 t = build1 (LABEL_EXPR, void_type_node, end_label);
2288 append_to_statement_list (t, &expr);
2291 return expr;
2294 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2296 tree
2297 gimple_boolify (tree expr)
2299 tree type = TREE_TYPE (expr);
2301 if (TREE_CODE (type) == BOOLEAN_TYPE)
2302 return expr;
2304 switch (TREE_CODE (expr))
2306 case TRUTH_AND_EXPR:
2307 case TRUTH_OR_EXPR:
2308 case TRUTH_XOR_EXPR:
2309 case TRUTH_ANDIF_EXPR:
2310 case TRUTH_ORIF_EXPR:
2311 /* Also boolify the arguments of truth exprs. */
2312 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2313 /* FALLTHRU */
2315 case TRUTH_NOT_EXPR:
2316 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2317 /* FALLTHRU */
2319 case EQ_EXPR: case NE_EXPR:
2320 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2321 /* These expressions always produce boolean results. */
2322 TREE_TYPE (expr) = boolean_type_node;
2323 return expr;
2325 default:
2326 /* Other expressions that get here must have boolean values, but
2327 might need to be converted to the appropriate mode. */
2328 return convert (boolean_type_node, expr);
2332 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2333 into
2335 if (p) if (p)
2336 t1 = a; a;
2337 else or else
2338 t1 = b; b;
2341 The second form is used when *EXPR_P is of type void.
2343 TARGET is the tree for T1 above.
2345 PRE_P points to the list where side effects that must happen before
2346 *EXPR_P should be stored. */
2348 static enum gimplify_status
2349 gimplify_cond_expr (tree *expr_p, tree *pre_p, fallback_t fallback)
2351 tree expr = *expr_p;
2352 tree tmp, tmp2, type;
2353 enum gimplify_status ret;
2355 type = TREE_TYPE (expr);
2357 /* If this COND_EXPR has a value, copy the values into a temporary within
2358 the arms. */
2359 if (! VOID_TYPE_P (type))
2361 tree result;
2363 if ((fallback & fb_lvalue) == 0)
2365 result = tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2366 ret = GS_ALL_DONE;
2368 else
2370 tree type = build_pointer_type (TREE_TYPE (expr));
2372 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2373 TREE_OPERAND (expr, 1) =
2374 build_fold_addr_expr (TREE_OPERAND (expr, 1));
2376 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2377 TREE_OPERAND (expr, 2) =
2378 build_fold_addr_expr (TREE_OPERAND (expr, 2));
2380 tmp2 = tmp = create_tmp_var (type, "iftmp");
2382 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (expr, 0),
2383 TREE_OPERAND (expr, 1), TREE_OPERAND (expr, 2));
2385 result = build_fold_indirect_ref (tmp);
2386 ret = GS_ALL_DONE;
2389 /* Build the then clause, 't1 = a;'. But don't build an assignment
2390 if this branch is void; in C++ it can be, if it's a throw. */
2391 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2392 TREE_OPERAND (expr, 1)
2393 = build2 (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2395 /* Build the else clause, 't1 = b;'. */
2396 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2397 TREE_OPERAND (expr, 2)
2398 = build2 (MODIFY_EXPR, void_type_node, tmp2, TREE_OPERAND (expr, 2));
2400 TREE_TYPE (expr) = void_type_node;
2401 recalculate_side_effects (expr);
2403 /* Move the COND_EXPR to the prequeue. */
2404 gimplify_and_add (expr, pre_p);
2406 *expr_p = result;
2407 return ret;
2410 /* Make sure the condition has BOOLEAN_TYPE. */
2411 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2413 /* Break apart && and || conditions. */
2414 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2415 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2417 expr = shortcut_cond_expr (expr);
2419 if (expr != *expr_p)
2421 *expr_p = expr;
2423 /* We can't rely on gimplify_expr to re-gimplify the expanded
2424 form properly, as cleanups might cause the target labels to be
2425 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2426 set up a conditional context. */
2427 gimple_push_condition ();
2428 gimplify_stmt (expr_p);
2429 gimple_pop_condition (pre_p);
2431 return GS_ALL_DONE;
2435 /* Now do the normal gimplification. */
2436 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2437 is_gimple_condexpr, fb_rvalue);
2439 gimple_push_condition ();
2441 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2442 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2443 recalculate_side_effects (expr);
2445 gimple_pop_condition (pre_p);
2447 if (ret == GS_ERROR)
2449 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2450 ret = GS_ALL_DONE;
2451 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2452 /* Rewrite "if (a); else b" to "if (!a) b" */
2454 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2455 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2456 is_gimple_condexpr, fb_rvalue);
2458 tmp = TREE_OPERAND (expr, 1);
2459 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2460 TREE_OPERAND (expr, 2) = tmp;
2462 else
2463 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2464 expr = TREE_OPERAND (expr, 0);
2466 *expr_p = expr;
2467 return ret;
2470 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2471 a call to __builtin_memcpy. */
2473 static enum gimplify_status
2474 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2476 tree args, t, to, to_ptr, from;
2478 to = TREE_OPERAND (*expr_p, 0);
2479 from = TREE_OPERAND (*expr_p, 1);
2481 args = tree_cons (NULL, size, NULL);
2483 t = build_fold_addr_expr (from);
2484 args = tree_cons (NULL, t, args);
2486 to_ptr = build_fold_addr_expr (to);
2487 args = tree_cons (NULL, to_ptr, args);
2488 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2489 t = build_function_call_expr (t, args);
2491 if (want_value)
2493 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2494 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2497 *expr_p = t;
2498 return GS_OK;
2501 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2502 a call to __builtin_memset. In this case we know that the RHS is
2503 a CONSTRUCTOR with an empty element list. */
2505 static enum gimplify_status
2506 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2508 tree args, t, to, to_ptr;
2510 to = TREE_OPERAND (*expr_p, 0);
2512 args = tree_cons (NULL, size, NULL);
2514 args = tree_cons (NULL, integer_zero_node, args);
2516 to_ptr = build_fold_addr_expr (to);
2517 args = tree_cons (NULL, to_ptr, args);
2518 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2519 t = build_function_call_expr (t, args);
2521 if (want_value)
2523 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2524 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2527 *expr_p = t;
2528 return GS_OK;
2531 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2532 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2533 assignment. Returns non-null if we detect a potential overlap. */
2535 struct gimplify_init_ctor_preeval_data
2537 /* The base decl of the lhs object. May be NULL, in which case we
2538 have to assume the lhs is indirect. */
2539 tree lhs_base_decl;
2541 /* The alias set of the lhs object. */
2542 int lhs_alias_set;
2545 static tree
2546 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2548 struct gimplify_init_ctor_preeval_data *data
2549 = (struct gimplify_init_ctor_preeval_data *) xdata;
2550 tree t = *tp;
2552 /* If we find the base object, obviously we have overlap. */
2553 if (data->lhs_base_decl == t)
2554 return t;
2556 /* If the constructor component is indirect, determine if we have a
2557 potential overlap with the lhs. The only bits of information we
2558 have to go on at this point are addressability and alias sets. */
2559 if (TREE_CODE (t) == INDIRECT_REF
2560 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2561 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2562 return t;
2564 if (IS_TYPE_OR_DECL_P (t))
2565 *walk_subtrees = 0;
2566 return NULL;
2569 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2570 force values that overlap with the lhs (as described by *DATA)
2571 into temporaries. */
2573 static void
2574 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2575 struct gimplify_init_ctor_preeval_data *data)
2577 enum gimplify_status one;
2579 /* If the value is invariant, then there's nothing to pre-evaluate.
2580 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2581 invariant but has side effects and might contain a reference to
2582 the object we're initializing. */
2583 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2584 return;
2586 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2587 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2588 return;
2590 /* Recurse for nested constructors. */
2591 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2593 unsigned HOST_WIDE_INT ix;
2594 constructor_elt *ce;
2595 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
2597 for (ix = 0; VEC_iterate (constructor_elt, v, ix, ce); ix++)
2598 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
2599 return;
2602 /* We can't preevaluate if the type contains a placeholder. */
2603 if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
2604 return;
2606 /* Gimplify the constructor element to something appropriate for the rhs
2607 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2608 the gimplifier will consider this a store to memory. Doing this
2609 gimplification now means that we won't have to deal with complicated
2610 language-specific trees, nor trees like SAVE_EXPR that can induce
2611 exponential search behavior. */
2612 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2613 if (one == GS_ERROR)
2615 *expr_p = NULL;
2616 return;
2619 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2620 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2621 always be true for all scalars, since is_gimple_mem_rhs insists on a
2622 temporary variable for them. */
2623 if (DECL_P (*expr_p))
2624 return;
2626 /* If this is of variable size, we have no choice but to assume it doesn't
2627 overlap since we can't make a temporary for it. */
2628 if (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p))))
2629 return;
2631 /* Otherwise, we must search for overlap ... */
2632 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2633 return;
2635 /* ... and if found, force the value into a temporary. */
2636 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2639 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
2640 a RANGE_EXPR in a CONSTRUCTOR for an array.
2642 var = lower;
2643 loop_entry:
2644 object[var] = value;
2645 if (var == upper)
2646 goto loop_exit;
2647 var = var + 1;
2648 goto loop_entry;
2649 loop_exit:
2651 We increment var _after_ the loop exit check because we might otherwise
2652 fail if upper == TYPE_MAX_VALUE (type for upper).
2654 Note that we never have to deal with SAVE_EXPRs here, because this has
2655 already been taken care of for us, in gimplify_init_ctor_preeval(). */
2657 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
2658 tree *, bool);
2660 static void
2661 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
2662 tree value, tree array_elt_type,
2663 tree *pre_p, bool cleared)
2665 tree loop_entry_label, loop_exit_label;
2666 tree var, var_type, cref;
2668 loop_entry_label = create_artificial_label ();
2669 loop_exit_label = create_artificial_label ();
2671 /* Create and initialize the index variable. */
2672 var_type = TREE_TYPE (upper);
2673 var = create_tmp_var (var_type, NULL);
2674 append_to_statement_list (build2 (MODIFY_EXPR, var_type, var, lower), pre_p);
2676 /* Add the loop entry label. */
2677 append_to_statement_list (build1 (LABEL_EXPR,
2678 void_type_node,
2679 loop_entry_label),
2680 pre_p);
2682 /* Build the reference. */
2683 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2684 var, NULL_TREE, NULL_TREE);
2686 /* If we are a constructor, just call gimplify_init_ctor_eval to do
2687 the store. Otherwise just assign value to the reference. */
2689 if (TREE_CODE (value) == CONSTRUCTOR)
2690 /* NB we might have to call ourself recursively through
2691 gimplify_init_ctor_eval if the value is a constructor. */
2692 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2693 pre_p, cleared);
2694 else
2695 append_to_statement_list (build2 (MODIFY_EXPR, TREE_TYPE (cref),
2696 cref, value),
2697 pre_p);
2699 /* We exit the loop when the index var is equal to the upper bound. */
2700 gimplify_and_add (build3 (COND_EXPR, void_type_node,
2701 build2 (EQ_EXPR, boolean_type_node,
2702 var, upper),
2703 build1 (GOTO_EXPR,
2704 void_type_node,
2705 loop_exit_label),
2706 NULL_TREE),
2707 pre_p);
2709 /* Otherwise, increment the index var... */
2710 append_to_statement_list (build2 (MODIFY_EXPR, var_type, var,
2711 build2 (PLUS_EXPR, var_type, var,
2712 fold_convert (var_type,
2713 integer_one_node))),
2714 pre_p);
2716 /* ...and jump back to the loop entry. */
2717 append_to_statement_list (build1 (GOTO_EXPR,
2718 void_type_node,
2719 loop_entry_label),
2720 pre_p);
2722 /* Add the loop exit label. */
2723 append_to_statement_list (build1 (LABEL_EXPR,
2724 void_type_node,
2725 loop_exit_label),
2726 pre_p);
2729 /* Return true if FDECL is accessing a field that is zero sized. */
2731 static bool
2732 zero_sized_field_decl (tree fdecl)
2734 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
2735 && integer_zerop (DECL_SIZE (fdecl)))
2736 return true;
2737 return false;
2740 /* Return true if TYPE is zero sized. */
2742 static bool
2743 zero_sized_type (tree type)
2745 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
2746 && integer_zerop (TYPE_SIZE (type)))
2747 return true;
2748 return false;
2751 /* A subroutine of gimplify_init_constructor. Generate individual
2752 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2753 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
2754 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2755 zeroed first. */
2757 static void
2758 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
2759 tree *pre_p, bool cleared)
2761 tree array_elt_type = NULL;
2762 unsigned HOST_WIDE_INT ix;
2763 tree purpose, value;
2765 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2766 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2768 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
2770 tree cref, init;
2772 /* NULL values are created above for gimplification errors. */
2773 if (value == NULL)
2774 continue;
2776 if (cleared && initializer_zerop (value))
2777 continue;
2779 /* ??? Here's to hoping the front end fills in all of the indices,
2780 so we don't have to figure out what's missing ourselves. */
2781 gcc_assert (purpose);
2783 /* Skip zero-sized fields, unless value has side-effects. This can
2784 happen with calls to functions returning a zero-sized type, which
2785 we shouldn't discard. As a number of downstream passes don't
2786 expect sets of zero-sized fields, we rely on the gimplification of
2787 the MODIFY_EXPR we make below to drop the assignment statement. */
2788 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
2789 continue;
2791 /* If we have a RANGE_EXPR, we have to build a loop to assign the
2792 whole range. */
2793 if (TREE_CODE (purpose) == RANGE_EXPR)
2795 tree lower = TREE_OPERAND (purpose, 0);
2796 tree upper = TREE_OPERAND (purpose, 1);
2798 /* If the lower bound is equal to upper, just treat it as if
2799 upper was the index. */
2800 if (simple_cst_equal (lower, upper))
2801 purpose = upper;
2802 else
2804 gimplify_init_ctor_eval_range (object, lower, upper, value,
2805 array_elt_type, pre_p, cleared);
2806 continue;
2810 if (array_elt_type)
2812 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2813 purpose, NULL_TREE, NULL_TREE);
2815 else
2817 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
2818 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
2819 unshare_expr (object), purpose, NULL_TREE);
2822 if (TREE_CODE (value) == CONSTRUCTOR
2823 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
2824 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2825 pre_p, cleared);
2826 else
2828 init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
2829 gimplify_and_add (init, pre_p);
2834 /* A subroutine of gimplify_modify_expr. Break out elements of a
2835 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2837 Note that we still need to clear any elements that don't have explicit
2838 initializers, so if not all elements are initialized we keep the
2839 original MODIFY_EXPR, we just remove all of the constructor elements. */
2841 static enum gimplify_status
2842 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2843 tree *post_p, bool want_value)
2845 tree object;
2846 tree ctor = TREE_OPERAND (*expr_p, 1);
2847 tree type = TREE_TYPE (ctor);
2848 enum gimplify_status ret;
2849 VEC(constructor_elt,gc) *elts;
2851 if (TREE_CODE (ctor) != CONSTRUCTOR)
2852 return GS_UNHANDLED;
2854 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
2855 is_gimple_lvalue, fb_lvalue);
2856 if (ret == GS_ERROR)
2857 return ret;
2858 object = TREE_OPERAND (*expr_p, 0);
2860 elts = CONSTRUCTOR_ELTS (ctor);
2862 ret = GS_ALL_DONE;
2863 switch (TREE_CODE (type))
2865 case RECORD_TYPE:
2866 case UNION_TYPE:
2867 case QUAL_UNION_TYPE:
2868 case ARRAY_TYPE:
2870 struct gimplify_init_ctor_preeval_data preeval_data;
2871 HOST_WIDE_INT num_type_elements, num_ctor_elements;
2872 HOST_WIDE_INT num_nonzero_elements, num_nonconstant_elements;
2873 bool cleared;
2875 /* Aggregate types must lower constructors to initialization of
2876 individual elements. The exception is that a CONSTRUCTOR node
2877 with no elements indicates zero-initialization of the whole. */
2878 if (VEC_empty (constructor_elt, elts))
2879 break;
2881 categorize_ctor_elements (ctor, &num_nonzero_elements,
2882 &num_nonconstant_elements,
2883 &num_ctor_elements, &cleared);
2885 /* If a const aggregate variable is being initialized, then it
2886 should never be a lose to promote the variable to be static. */
2887 if (num_nonconstant_elements == 0
2888 && num_nonzero_elements > 1
2889 && TREE_READONLY (object)
2890 && TREE_CODE (object) == VAR_DECL)
2892 DECL_INITIAL (object) = ctor;
2893 TREE_STATIC (object) = 1;
2894 if (!DECL_NAME (object))
2895 DECL_NAME (object) = create_tmp_var_name ("C");
2896 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2898 /* ??? C++ doesn't automatically append a .<number> to the
2899 assembler name, and even when it does, it looks a FE private
2900 data structures to figure out what that number should be,
2901 which are not set for this variable. I suppose this is
2902 important for local statics for inline functions, which aren't
2903 "local" in the object file sense. So in order to get a unique
2904 TU-local symbol, we must invoke the lhd version now. */
2905 lhd_set_decl_assembler_name (object);
2907 *expr_p = NULL_TREE;
2908 break;
2911 /* If there are "lots" of initialized elements, even discounting
2912 those that are not address constants (and thus *must* be
2913 computed at runtime), then partition the constructor into
2914 constant and non-constant parts. Block copy the constant
2915 parts in, then generate code for the non-constant parts. */
2916 /* TODO. There's code in cp/typeck.c to do this. */
2918 num_type_elements = count_type_elements (type, true);
2920 /* If count_type_elements could not determine number of type elements
2921 for a constant-sized object, assume clearing is needed.
2922 Don't do this for variable-sized objects, as store_constructor
2923 will ignore the clearing of variable-sized objects. */
2924 if (num_type_elements < 0 && int_size_in_bytes (type) >= 0)
2925 cleared = true;
2926 /* If there are "lots" of zeros, then block clear the object first. */
2927 else if (num_type_elements - num_nonzero_elements > CLEAR_RATIO
2928 && num_nonzero_elements < num_type_elements/4)
2929 cleared = true;
2930 /* ??? This bit ought not be needed. For any element not present
2931 in the initializer, we should simply set them to zero. Except
2932 we'd need to *find* the elements that are not present, and that
2933 requires trickery to avoid quadratic compile-time behavior in
2934 large cases or excessive memory use in small cases. */
2935 else if (num_ctor_elements < num_type_elements)
2936 cleared = true;
2938 /* If there are "lots" of initialized elements, and all of them
2939 are valid address constants, then the entire initializer can
2940 be dropped to memory, and then memcpy'd out. Don't do this
2941 for sparse arrays, though, as it's more efficient to follow
2942 the standard CONSTRUCTOR behavior of memset followed by
2943 individual element initialization. */
2944 if (num_nonconstant_elements == 0 && !cleared)
2946 HOST_WIDE_INT size = int_size_in_bytes (type);
2947 unsigned int align;
2949 /* ??? We can still get unbounded array types, at least
2950 from the C++ front end. This seems wrong, but attempt
2951 to work around it for now. */
2952 if (size < 0)
2954 size = int_size_in_bytes (TREE_TYPE (object));
2955 if (size >= 0)
2956 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2959 /* Find the maximum alignment we can assume for the object. */
2960 /* ??? Make use of DECL_OFFSET_ALIGN. */
2961 if (DECL_P (object))
2962 align = DECL_ALIGN (object);
2963 else
2964 align = TYPE_ALIGN (type);
2966 if (size > 0 && !can_move_by_pieces (size, align))
2968 tree new = create_tmp_var_raw (type, "C");
2970 gimple_add_tmp_var (new);
2971 TREE_STATIC (new) = 1;
2972 TREE_READONLY (new) = 1;
2973 DECL_INITIAL (new) = ctor;
2974 if (align > DECL_ALIGN (new))
2976 DECL_ALIGN (new) = align;
2977 DECL_USER_ALIGN (new) = 1;
2979 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2981 TREE_OPERAND (*expr_p, 1) = new;
2983 /* This is no longer an assignment of a CONSTRUCTOR, but
2984 we still may have processing to do on the LHS. So
2985 pretend we didn't do anything here to let that happen. */
2986 return GS_UNHANDLED;
2990 if (cleared)
2992 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2993 Note that we still have to gimplify, in order to handle the
2994 case of variable sized types. Avoid shared tree structures. */
2995 CONSTRUCTOR_ELTS (ctor) = NULL;
2996 object = unshare_expr (object);
2997 gimplify_stmt (expr_p);
2998 append_to_statement_list (*expr_p, pre_p);
3001 /* If we have not block cleared the object, or if there are nonzero
3002 elements in the constructor, add assignments to the individual
3003 scalar fields of the object. */
3004 if (!cleared || num_nonzero_elements > 0)
3006 preeval_data.lhs_base_decl = get_base_address (object);
3007 if (!DECL_P (preeval_data.lhs_base_decl))
3008 preeval_data.lhs_base_decl = NULL;
3009 preeval_data.lhs_alias_set = get_alias_set (object);
3011 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3012 pre_p, post_p, &preeval_data);
3013 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3016 *expr_p = NULL_TREE;
3018 break;
3020 case COMPLEX_TYPE:
3022 tree r, i;
3024 /* Extract the real and imaginary parts out of the ctor. */
3025 gcc_assert (VEC_length (constructor_elt, elts) == 2);
3026 r = VEC_index (constructor_elt, elts, 0)->value;
3027 i = VEC_index (constructor_elt, elts, 1)->value;
3028 if (r == NULL || i == NULL)
3030 tree zero = convert (TREE_TYPE (type), integer_zero_node);
3031 if (r == NULL)
3032 r = zero;
3033 if (i == NULL)
3034 i = zero;
3037 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3038 represent creation of a complex value. */
3039 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3041 ctor = build_complex (type, r, i);
3042 TREE_OPERAND (*expr_p, 1) = ctor;
3044 else
3046 ctor = build2 (COMPLEX_EXPR, type, r, i);
3047 TREE_OPERAND (*expr_p, 1) = ctor;
3048 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3049 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3050 fb_rvalue);
3053 break;
3055 case VECTOR_TYPE:
3057 unsigned HOST_WIDE_INT ix;
3058 constructor_elt *ce;
3060 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3061 if (TREE_CONSTANT (ctor))
3063 bool constant_p = true;
3064 tree value;
3066 /* Even when ctor is constant, it might contain non-*_CST
3067 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
3068 belong into VECTOR_CST nodes. */
3069 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3070 if (!CONSTANT_CLASS_P (value))
3072 constant_p = false;
3073 break;
3076 if (constant_p)
3078 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3079 break;
3083 /* Vector types use CONSTRUCTOR all the way through gimple
3084 compilation as a general initializer. */
3085 for (ix = 0; VEC_iterate (constructor_elt, elts, ix, ce); ix++)
3087 enum gimplify_status tret;
3088 tret = gimplify_expr (&ce->value, pre_p, post_p,
3089 is_gimple_val, fb_rvalue);
3090 if (tret == GS_ERROR)
3091 ret = GS_ERROR;
3094 break;
3096 default:
3097 /* So how did we get a CONSTRUCTOR for a scalar type? */
3098 gcc_unreachable ();
3101 if (ret == GS_ERROR)
3102 return GS_ERROR;
3103 else if (want_value)
3105 append_to_statement_list (*expr_p, pre_p);
3106 *expr_p = object;
3107 return GS_OK;
3109 else
3110 return GS_ALL_DONE;
3113 /* Given a pointer value OP0, return a simplified version of an
3114 indirection through OP0, or NULL_TREE if no simplification is
3115 possible. This may only be applied to a rhs of an expression.
3116 Note that the resulting type may be different from the type pointed
3117 to in the sense that it is still compatible from the langhooks
3118 point of view. */
3120 static tree
3121 fold_indirect_ref_rhs (tree t)
3123 tree type = TREE_TYPE (TREE_TYPE (t));
3124 tree sub = t;
3125 tree subtype;
3127 STRIP_NOPS (sub);
3128 subtype = TREE_TYPE (sub);
3129 if (!POINTER_TYPE_P (subtype))
3130 return NULL_TREE;
3132 if (TREE_CODE (sub) == ADDR_EXPR)
3134 tree op = TREE_OPERAND (sub, 0);
3135 tree optype = TREE_TYPE (op);
3136 /* *&p => p */
3137 if (lang_hooks.types_compatible_p (type, optype))
3138 return op;
3139 /* *(foo *)&fooarray => fooarray[0] */
3140 else if (TREE_CODE (optype) == ARRAY_TYPE
3141 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
3143 tree type_domain = TYPE_DOMAIN (optype);
3144 tree min_val = size_zero_node;
3145 if (type_domain && TYPE_MIN_VALUE (type_domain))
3146 min_val = TYPE_MIN_VALUE (type_domain);
3147 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3151 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3152 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3153 && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3155 tree type_domain;
3156 tree min_val = size_zero_node;
3157 tree osub = sub;
3158 sub = fold_indirect_ref_rhs (sub);
3159 if (! sub)
3160 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3161 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3162 if (type_domain && TYPE_MIN_VALUE (type_domain))
3163 min_val = TYPE_MIN_VALUE (type_domain);
3164 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3167 return NULL_TREE;
3170 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
3171 based on the code of the RHS. We loop for as long as something changes. */
3173 static enum gimplify_status
3174 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
3175 tree *post_p, bool want_value)
3177 enum gimplify_status ret = GS_OK;
3179 while (ret != GS_UNHANDLED)
3180 switch (TREE_CODE (*from_p))
3182 case INDIRECT_REF:
3184 /* If we have code like
3186 *(const A*)(A*)&x
3188 where the type of "x" is a (possibly cv-qualified variant
3189 of "A"), treat the entire expression as identical to "x".
3190 This kind of code arises in C++ when an object is bound
3191 to a const reference, and if "x" is a TARGET_EXPR we want
3192 to take advantage of the optimization below. */
3193 tree t = fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
3194 if (t)
3196 *from_p = t;
3197 ret = GS_OK;
3199 else
3200 ret = GS_UNHANDLED;
3201 break;
3204 case TARGET_EXPR:
3206 /* If we are initializing something from a TARGET_EXPR, strip the
3207 TARGET_EXPR and initialize it directly, if possible. This can't
3208 be done if the initializer is void, since that implies that the
3209 temporary is set in some non-trivial way.
3211 ??? What about code that pulls out the temp and uses it
3212 elsewhere? I think that such code never uses the TARGET_EXPR as
3213 an initializer. If I'm wrong, we'll die because the temp won't
3214 have any RTL. In that case, I guess we'll need to replace
3215 references somehow. */
3216 tree init = TARGET_EXPR_INITIAL (*from_p);
3218 if (!VOID_TYPE_P (TREE_TYPE (init)))
3220 *from_p = init;
3221 ret = GS_OK;
3223 else
3224 ret = GS_UNHANDLED;
3226 break;
3228 case COMPOUND_EXPR:
3229 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
3230 caught. */
3231 gimplify_compound_expr (from_p, pre_p, true);
3232 ret = GS_OK;
3233 break;
3235 case CONSTRUCTOR:
3236 /* If we're initializing from a CONSTRUCTOR, break this into
3237 individual MODIFY_EXPRs. */
3238 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
3240 case COND_EXPR:
3241 /* If we're assigning to a non-register type, push the assignment
3242 down into the branches. This is mandatory for ADDRESSABLE types,
3243 since we cannot generate temporaries for such, but it saves a
3244 copy in other cases as well. */
3245 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
3247 /* This code should mirror the code in gimplify_cond_expr. */
3248 enum tree_code code = TREE_CODE (*expr_p);
3249 tree cond = *from_p;
3250 tree result = *to_p;
3252 ret = gimplify_expr (&result, pre_p, post_p,
3253 is_gimple_min_lval, fb_lvalue);
3254 if (ret != GS_ERROR)
3255 ret = GS_OK;
3257 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
3258 TREE_OPERAND (cond, 1)
3259 = build2 (code, void_type_node, result,
3260 TREE_OPERAND (cond, 1));
3261 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
3262 TREE_OPERAND (cond, 2)
3263 = build2 (code, void_type_node, unshare_expr (result),
3264 TREE_OPERAND (cond, 2));
3266 TREE_TYPE (cond) = void_type_node;
3267 recalculate_side_effects (cond);
3269 if (want_value)
3271 gimplify_and_add (cond, pre_p);
3272 *expr_p = unshare_expr (result);
3274 else
3275 *expr_p = cond;
3276 return ret;
3278 else
3279 ret = GS_UNHANDLED;
3280 break;
3282 case CALL_EXPR:
3283 /* For calls that return in memory, give *to_p as the CALL_EXPR's
3284 return slot so that we don't generate a temporary. */
3285 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
3286 && aggregate_value_p (*from_p, *from_p))
3288 bool use_target;
3290 if (!(rhs_predicate_for (*to_p))(*from_p))
3291 /* If we need a temporary, *to_p isn't accurate. */
3292 use_target = false;
3293 else if (TREE_CODE (*to_p) == RESULT_DECL
3294 && DECL_NAME (*to_p) == NULL_TREE
3295 && needs_to_live_in_memory (*to_p))
3296 /* It's OK to use the return slot directly unless it's an NRV. */
3297 use_target = true;
3298 else if (is_gimple_reg_type (TREE_TYPE (*to_p)))
3299 /* Don't force regs into memory. */
3300 use_target = false;
3301 else if (TREE_CODE (*to_p) == VAR_DECL
3302 && DECL_GIMPLE_FORMAL_TEMP_P (*to_p))
3303 /* Don't use the original target if it's a formal temp; we
3304 don't want to take their addresses. */
3305 use_target = false;
3306 else if (TREE_CODE (*expr_p) == INIT_EXPR)
3307 /* It's OK to use the target directly if it's being
3308 initialized. */
3309 use_target = true;
3310 else if (!is_gimple_non_addressable (*to_p))
3311 /* Don't use the original target if it's already addressable;
3312 if its address escapes, and the called function uses the
3313 NRV optimization, a conforming program could see *to_p
3314 change before the called function returns; see c++/19317.
3315 When optimizing, the return_slot pass marks more functions
3316 as safe after we have escape info. */
3317 use_target = false;
3318 else
3319 use_target = true;
3321 if (use_target)
3323 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
3324 lang_hooks.mark_addressable (*to_p);
3328 ret = GS_UNHANDLED;
3329 break;
3331 default:
3332 ret = GS_UNHANDLED;
3333 break;
3336 return ret;
3339 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
3340 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
3341 DECL_COMPLEX_GIMPLE_REG_P set. */
3343 static enum gimplify_status
3344 gimplify_modify_expr_complex_part (tree *expr_p, tree *pre_p, bool want_value)
3346 enum tree_code code, ocode;
3347 tree lhs, rhs, new_rhs, other, realpart, imagpart;
3349 lhs = TREE_OPERAND (*expr_p, 0);
3350 rhs = TREE_OPERAND (*expr_p, 1);
3351 code = TREE_CODE (lhs);
3352 lhs = TREE_OPERAND (lhs, 0);
3354 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
3355 other = build1 (ocode, TREE_TYPE (rhs), lhs);
3356 other = get_formal_tmp_var (other, pre_p);
3358 realpart = code == REALPART_EXPR ? rhs : other;
3359 imagpart = code == REALPART_EXPR ? other : rhs;
3361 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
3362 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
3363 else
3364 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
3366 TREE_OPERAND (*expr_p, 0) = lhs;
3367 TREE_OPERAND (*expr_p, 1) = new_rhs;
3369 if (want_value)
3371 append_to_statement_list (*expr_p, pre_p);
3372 *expr_p = rhs;
3375 return GS_ALL_DONE;
3378 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
3380 modify_expr
3381 : varname '=' rhs
3382 | '*' ID '=' rhs
3384 PRE_P points to the list where side effects that must happen before
3385 *EXPR_P should be stored.
3387 POST_P points to the list where side effects that must happen after
3388 *EXPR_P should be stored.
3390 WANT_VALUE is nonzero iff we want to use the value of this expression
3391 in another expression. */
3393 static enum gimplify_status
3394 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
3396 tree *from_p = &TREE_OPERAND (*expr_p, 1);
3397 tree *to_p = &TREE_OPERAND (*expr_p, 0);
3398 enum gimplify_status ret = GS_UNHANDLED;
3400 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
3401 || TREE_CODE (*expr_p) == INIT_EXPR);
3403 /* For zero sized types only gimplify the left hand side and right hand side
3404 as statements and throw away the assignment. */
3405 if (zero_sized_type (TREE_TYPE (*from_p)))
3407 gimplify_stmt (from_p);
3408 gimplify_stmt (to_p);
3409 append_to_statement_list (*from_p, pre_p);
3410 append_to_statement_list (*to_p, pre_p);
3411 *expr_p = NULL_TREE;
3412 return GS_ALL_DONE;
3415 /* See if any simplifications can be done based on what the RHS is. */
3416 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3417 want_value);
3418 if (ret != GS_UNHANDLED)
3419 return ret;
3421 /* If the value being copied is of variable width, compute the length
3422 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
3423 before gimplifying any of the operands so that we can resolve any
3424 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
3425 the size of the expression to be copied, not of the destination, so
3426 that is what we must here. */
3427 maybe_with_size_expr (from_p);
3429 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3430 if (ret == GS_ERROR)
3431 return ret;
3433 ret = gimplify_expr (from_p, pre_p, post_p,
3434 rhs_predicate_for (*to_p), fb_rvalue);
3435 if (ret == GS_ERROR)
3436 return ret;
3438 /* Now see if the above changed *from_p to something we handle specially. */
3439 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3440 want_value);
3441 if (ret != GS_UNHANDLED)
3442 return ret;
3444 /* If we've got a variable sized assignment between two lvalues (i.e. does
3445 not involve a call), then we can make things a bit more straightforward
3446 by converting the assignment to memcpy or memset. */
3447 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
3449 tree from = TREE_OPERAND (*from_p, 0);
3450 tree size = TREE_OPERAND (*from_p, 1);
3452 if (TREE_CODE (from) == CONSTRUCTOR)
3453 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
3454 if (is_gimple_addressable (from))
3456 *from_p = from;
3457 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
3461 /* Transform partial stores to non-addressable complex variables into
3462 total stores. This allows us to use real instead of virtual operands
3463 for these variables, which improves optimization. */
3464 if ((TREE_CODE (*to_p) == REALPART_EXPR
3465 || TREE_CODE (*to_p) == IMAGPART_EXPR)
3466 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
3467 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
3469 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
3471 /* If we've somehow already got an SSA_NAME on the LHS, then
3472 we're probably modified it twice. Not good. */
3473 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
3474 *to_p = make_ssa_name (*to_p, *expr_p);
3477 if (want_value)
3479 append_to_statement_list (*expr_p, pre_p);
3480 *expr_p = *to_p;
3481 return GS_OK;
3484 return GS_ALL_DONE;
3487 /* Gimplify a comparison between two variable-sized objects. Do this
3488 with a call to BUILT_IN_MEMCMP. */
3490 static enum gimplify_status
3491 gimplify_variable_sized_compare (tree *expr_p)
3493 tree op0 = TREE_OPERAND (*expr_p, 0);
3494 tree op1 = TREE_OPERAND (*expr_p, 1);
3495 tree args, t, dest;
3497 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
3498 t = unshare_expr (t);
3499 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
3500 args = tree_cons (NULL, t, NULL);
3501 t = build_fold_addr_expr (op1);
3502 args = tree_cons (NULL, t, args);
3503 dest = build_fold_addr_expr (op0);
3504 args = tree_cons (NULL, dest, args);
3505 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
3506 t = build_function_call_expr (t, args);
3507 *expr_p
3508 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
3510 return GS_OK;
3513 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
3514 points to the expression to gimplify.
3516 Expressions of the form 'a && b' are gimplified to:
3518 a && b ? true : false
3520 gimplify_cond_expr will do the rest.
3522 PRE_P points to the list where side effects that must happen before
3523 *EXPR_P should be stored. */
3525 static enum gimplify_status
3526 gimplify_boolean_expr (tree *expr_p)
3528 /* Preserve the original type of the expression. */
3529 tree type = TREE_TYPE (*expr_p);
3531 *expr_p = build3 (COND_EXPR, type, *expr_p,
3532 convert (type, boolean_true_node),
3533 convert (type, boolean_false_node));
3535 return GS_OK;
3538 /* Gimplifies an expression sequence. This function gimplifies each
3539 expression and re-writes the original expression with the last
3540 expression of the sequence in GIMPLE form.
3542 PRE_P points to the list where the side effects for all the
3543 expressions in the sequence will be emitted.
3545 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
3546 /* ??? Should rearrange to share the pre-queue with all the indirect
3547 invocations of gimplify_expr. Would probably save on creations
3548 of statement_list nodes. */
3550 static enum gimplify_status
3551 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
3553 tree t = *expr_p;
3557 tree *sub_p = &TREE_OPERAND (t, 0);
3559 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3560 gimplify_compound_expr (sub_p, pre_p, false);
3561 else
3562 gimplify_stmt (sub_p);
3563 append_to_statement_list (*sub_p, pre_p);
3565 t = TREE_OPERAND (t, 1);
3567 while (TREE_CODE (t) == COMPOUND_EXPR);
3569 *expr_p = t;
3570 if (want_value)
3571 return GS_OK;
3572 else
3574 gimplify_stmt (expr_p);
3575 return GS_ALL_DONE;
3579 /* Gimplifies a statement list. These may be created either by an
3580 enlightened front-end, or by shortcut_cond_expr. */
3582 static enum gimplify_status
3583 gimplify_statement_list (tree *expr_p)
3585 tree_stmt_iterator i = tsi_start (*expr_p);
3587 while (!tsi_end_p (i))
3589 tree t;
3591 gimplify_stmt (tsi_stmt_ptr (i));
3593 t = tsi_stmt (i);
3594 if (t == NULL)
3595 tsi_delink (&i);
3596 else if (TREE_CODE (t) == STATEMENT_LIST)
3598 tsi_link_before (&i, t, TSI_SAME_STMT);
3599 tsi_delink (&i);
3601 else
3602 tsi_next (&i);
3605 return GS_ALL_DONE;
3608 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
3609 gimplify. After gimplification, EXPR_P will point to a new temporary
3610 that holds the original value of the SAVE_EXPR node.
3612 PRE_P points to the list where side effects that must happen before
3613 *EXPR_P should be stored. */
3615 static enum gimplify_status
3616 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3618 enum gimplify_status ret = GS_ALL_DONE;
3619 tree val;
3621 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
3622 val = TREE_OPERAND (*expr_p, 0);
3624 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
3625 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
3627 /* The operand may be a void-valued expression such as SAVE_EXPRs
3628 generated by the Java frontend for class initialization. It is
3629 being executed only for its side-effects. */
3630 if (TREE_TYPE (val) == void_type_node)
3632 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3633 is_gimple_stmt, fb_none);
3634 append_to_statement_list (TREE_OPERAND (*expr_p, 0), pre_p);
3635 val = NULL;
3637 else
3638 val = get_initialized_tmp_var (val, pre_p, post_p);
3640 TREE_OPERAND (*expr_p, 0) = val;
3641 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
3644 *expr_p = val;
3646 return ret;
3649 /* Re-write the ADDR_EXPR node pointed to by EXPR_P
3651 unary_expr
3652 : ...
3653 | '&' varname
3656 PRE_P points to the list where side effects that must happen before
3657 *EXPR_P should be stored.
3659 POST_P points to the list where side effects that must happen after
3660 *EXPR_P should be stored. */
3662 static enum gimplify_status
3663 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3665 tree expr = *expr_p;
3666 tree op0 = TREE_OPERAND (expr, 0);
3667 enum gimplify_status ret;
3669 switch (TREE_CODE (op0))
3671 case INDIRECT_REF:
3672 case MISALIGNED_INDIRECT_REF:
3673 do_indirect_ref:
3674 /* Check if we are dealing with an expression of the form '&*ptr'.
3675 While the front end folds away '&*ptr' into 'ptr', these
3676 expressions may be generated internally by the compiler (e.g.,
3677 builtins like __builtin_va_end). */
3678 /* Caution: the silent array decomposition semantics we allow for
3679 ADDR_EXPR means we can't always discard the pair. */
3680 /* Gimplification of the ADDR_EXPR operand may drop
3681 cv-qualification conversions, so make sure we add them if
3682 needed. */
3684 tree op00 = TREE_OPERAND (op0, 0);
3685 tree t_expr = TREE_TYPE (expr);
3686 tree t_op00 = TREE_TYPE (op00);
3688 if (!lang_hooks.types_compatible_p (t_expr, t_op00))
3690 #ifdef ENABLE_CHECKING
3691 tree t_op0 = TREE_TYPE (op0);
3692 gcc_assert (POINTER_TYPE_P (t_expr)
3693 && cpt_same_type (TREE_CODE (t_op0) == ARRAY_TYPE
3694 ? TREE_TYPE (t_op0) : t_op0,
3695 TREE_TYPE (t_expr))
3696 && POINTER_TYPE_P (t_op00)
3697 && cpt_same_type (t_op0, TREE_TYPE (t_op00)));
3698 #endif
3699 op00 = fold_convert (TREE_TYPE (expr), op00);
3701 *expr_p = op00;
3702 ret = GS_OK;
3704 break;
3706 case VIEW_CONVERT_EXPR:
3707 /* Take the address of our operand and then convert it to the type of
3708 this ADDR_EXPR.
3710 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3711 all clear. The impact of this transformation is even less clear. */
3713 /* If the operand is a useless conversion, look through it. Doing so
3714 guarantees that the ADDR_EXPR and its operand will remain of the
3715 same type. */
3716 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
3717 op0 = TREE_OPERAND (op0, 0);
3719 *expr_p = fold_convert (TREE_TYPE (expr),
3720 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3721 ret = GS_OK;
3722 break;
3724 default:
3725 /* We use fb_either here because the C frontend sometimes takes
3726 the address of a call that returns a struct; see
3727 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3728 the implied temporary explicit. */
3729 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3730 is_gimple_addressable, fb_either);
3731 if (ret != GS_ERROR)
3733 op0 = TREE_OPERAND (expr, 0);
3735 /* For various reasons, the gimplification of the expression
3736 may have made a new INDIRECT_REF. */
3737 if (TREE_CODE (op0) == INDIRECT_REF)
3738 goto do_indirect_ref;
3740 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3741 is set properly. */
3742 recompute_tree_invariant_for_addr_expr (expr);
3744 /* Mark the RHS addressable. */
3745 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3747 break;
3750 return ret;
3753 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3754 value; output operands should be a gimple lvalue. */
3756 static enum gimplify_status
3757 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3759 tree expr = *expr_p;
3760 int noutputs = list_length (ASM_OUTPUTS (expr));
3761 const char **oconstraints
3762 = (const char **) alloca ((noutputs) * sizeof (const char *));
3763 int i;
3764 tree link;
3765 const char *constraint;
3766 bool allows_mem, allows_reg, is_inout;
3767 enum gimplify_status ret, tret;
3769 ret = GS_ALL_DONE;
3770 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3772 size_t constraint_len;
3773 oconstraints[i] = constraint
3774 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3775 constraint_len = strlen (constraint);
3776 if (constraint_len == 0)
3777 continue;
3779 parse_output_constraint (&constraint, i, 0, 0,
3780 &allows_mem, &allows_reg, &is_inout);
3782 if (!allows_reg && allows_mem)
3783 lang_hooks.mark_addressable (TREE_VALUE (link));
3785 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3786 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3787 fb_lvalue | fb_mayfail);
3788 if (tret == GS_ERROR)
3790 error ("invalid lvalue in asm output %d", i);
3791 ret = tret;
3794 if (is_inout)
3796 /* An input/output operand. To give the optimizers more
3797 flexibility, split it into separate input and output
3798 operands. */
3799 tree input;
3800 char buf[10];
3802 /* Turn the in/out constraint into an output constraint. */
3803 char *p = xstrdup (constraint);
3804 p[0] = '=';
3805 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3807 /* And add a matching input constraint. */
3808 if (allows_reg)
3810 sprintf (buf, "%d", i);
3812 /* If there are multiple alternatives in the constraint,
3813 handle each of them individually. Those that allow register
3814 will be replaced with operand number, the others will stay
3815 unchanged. */
3816 if (strchr (p, ',') != NULL)
3818 size_t len = 0, buflen = strlen (buf);
3819 char *beg, *end, *str, *dst;
3821 for (beg = p + 1;;)
3823 end = strchr (beg, ',');
3824 if (end == NULL)
3825 end = strchr (beg, '\0');
3826 if ((size_t) (end - beg) < buflen)
3827 len += buflen + 1;
3828 else
3829 len += end - beg + 1;
3830 if (*end)
3831 beg = end + 1;
3832 else
3833 break;
3836 str = (char *) alloca (len);
3837 for (beg = p + 1, dst = str;;)
3839 const char *tem;
3840 bool mem_p, reg_p, inout_p;
3842 end = strchr (beg, ',');
3843 if (end)
3844 *end = '\0';
3845 beg[-1] = '=';
3846 tem = beg - 1;
3847 parse_output_constraint (&tem, i, 0, 0,
3848 &mem_p, &reg_p, &inout_p);
3849 if (dst != str)
3850 *dst++ = ',';
3851 if (reg_p)
3853 memcpy (dst, buf, buflen);
3854 dst += buflen;
3856 else
3858 if (end)
3859 len = end - beg;
3860 else
3861 len = strlen (beg);
3862 memcpy (dst, beg, len);
3863 dst += len;
3865 if (end)
3866 beg = end + 1;
3867 else
3868 break;
3870 *dst = '\0';
3871 input = build_string (dst - str, str);
3873 else
3874 input = build_string (strlen (buf), buf);
3876 else
3877 input = build_string (constraint_len - 1, constraint + 1);
3879 free (p);
3881 input = build_tree_list (build_tree_list (NULL_TREE, input),
3882 unshare_expr (TREE_VALUE (link)));
3883 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3887 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3889 constraint
3890 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3891 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3892 oconstraints, &allows_mem, &allows_reg);
3894 /* If the operand is a memory input, it should be an lvalue. */
3895 if (!allows_reg && allows_mem)
3897 lang_hooks.mark_addressable (TREE_VALUE (link));
3898 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3899 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3900 if (tret == GS_ERROR)
3902 error ("memory input %d is not directly addressable", i);
3903 ret = tret;
3906 else
3908 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3909 is_gimple_asm_val, fb_rvalue);
3910 if (tret == GS_ERROR)
3911 ret = tret;
3915 return ret;
3918 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3919 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3920 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3921 return to this function.
3923 FIXME should we complexify the prequeue handling instead? Or use flags
3924 for all the cleanups and let the optimizer tighten them up? The current
3925 code seems pretty fragile; it will break on a cleanup within any
3926 non-conditional nesting. But any such nesting would be broken, anyway;
3927 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3928 and continues out of it. We can do that at the RTL level, though, so
3929 having an optimizer to tighten up try/finally regions would be a Good
3930 Thing. */
3932 static enum gimplify_status
3933 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3935 tree_stmt_iterator iter;
3936 tree body;
3938 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3940 /* We only care about the number of conditions between the innermost
3941 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
3942 any cleanups collected outside the CLEANUP_POINT_EXPR. */
3943 int old_conds = gimplify_ctxp->conditions;
3944 tree old_cleanups = gimplify_ctxp->conditional_cleanups;
3945 gimplify_ctxp->conditions = 0;
3946 gimplify_ctxp->conditional_cleanups = NULL_TREE;
3948 body = TREE_OPERAND (*expr_p, 0);
3949 gimplify_to_stmt_list (&body);
3951 gimplify_ctxp->conditions = old_conds;
3952 gimplify_ctxp->conditional_cleanups = old_cleanups;
3954 for (iter = tsi_start (body); !tsi_end_p (iter); )
3956 tree *wce_p = tsi_stmt_ptr (iter);
3957 tree wce = *wce_p;
3959 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3961 if (tsi_one_before_end_p (iter))
3963 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
3964 tsi_delink (&iter);
3965 break;
3967 else
3969 tree sl, tfe;
3970 enum tree_code code;
3972 if (CLEANUP_EH_ONLY (wce))
3973 code = TRY_CATCH_EXPR;
3974 else
3975 code = TRY_FINALLY_EXPR;
3977 sl = tsi_split_statement_list_after (&iter);
3978 tfe = build2 (code, void_type_node, sl, NULL_TREE);
3979 append_to_statement_list (TREE_OPERAND (wce, 0),
3980 &TREE_OPERAND (tfe, 1));
3981 *wce_p = tfe;
3982 iter = tsi_start (sl);
3985 else
3986 tsi_next (&iter);
3989 if (temp)
3991 *expr_p = temp;
3992 append_to_statement_list (body, pre_p);
3993 return GS_OK;
3995 else
3997 *expr_p = body;
3998 return GS_ALL_DONE;
4002 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
4003 is the cleanup action required. */
4005 static void
4006 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
4008 tree wce;
4010 /* Errors can result in improperly nested cleanups. Which results in
4011 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
4012 if (errorcount || sorrycount)
4013 return;
4015 if (gimple_conditional_context ())
4017 /* If we're in a conditional context, this is more complex. We only
4018 want to run the cleanup if we actually ran the initialization that
4019 necessitates it, but we want to run it after the end of the
4020 conditional context. So we wrap the try/finally around the
4021 condition and use a flag to determine whether or not to actually
4022 run the destructor. Thus
4024 test ? f(A()) : 0
4026 becomes (approximately)
4028 flag = 0;
4029 try {
4030 if (test) { A::A(temp); flag = 1; val = f(temp); }
4031 else { val = 0; }
4032 } finally {
4033 if (flag) A::~A(temp);
4038 tree flag = create_tmp_var (boolean_type_node, "cleanup");
4039 tree ffalse = build2 (MODIFY_EXPR, void_type_node, flag,
4040 boolean_false_node);
4041 tree ftrue = build2 (MODIFY_EXPR, void_type_node, flag,
4042 boolean_true_node);
4043 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
4044 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4045 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
4046 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
4047 append_to_statement_list (ftrue, pre_p);
4049 /* Because of this manipulation, and the EH edges that jump
4050 threading cannot redirect, the temporary (VAR) will appear
4051 to be used uninitialized. Don't warn. */
4052 TREE_NO_WARNING (var) = 1;
4054 else
4056 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4057 CLEANUP_EH_ONLY (wce) = eh_only;
4058 append_to_statement_list (wce, pre_p);
4061 gimplify_stmt (&TREE_OPERAND (wce, 0));
4064 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
4066 static enum gimplify_status
4067 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
4069 tree targ = *expr_p;
4070 tree temp = TARGET_EXPR_SLOT (targ);
4071 tree init = TARGET_EXPR_INITIAL (targ);
4072 enum gimplify_status ret;
4074 if (init)
4076 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
4077 to the temps list. */
4078 gimple_add_tmp_var (temp);
4080 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
4081 expression is supposed to initialize the slot. */
4082 if (VOID_TYPE_P (TREE_TYPE (init)))
4083 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
4084 else
4086 /* Special handling for BIND_EXPR can result in fewer temps. */
4087 ret = GS_OK;
4088 if (TREE_CODE (init) == BIND_EXPR)
4089 gimplify_bind_expr (&init, temp, pre_p);
4090 if (init != temp)
4092 init = build2 (INIT_EXPR, void_type_node, temp, init);
4093 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
4094 fb_none);
4097 if (ret == GS_ERROR)
4098 return GS_ERROR;
4099 append_to_statement_list (init, pre_p);
4101 /* If needed, push the cleanup for the temp. */
4102 if (TARGET_EXPR_CLEANUP (targ))
4104 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
4105 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
4106 CLEANUP_EH_ONLY (targ), pre_p);
4109 /* Only expand this once. */
4110 TREE_OPERAND (targ, 3) = init;
4111 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4113 else
4114 /* We should have expanded this before. */
4115 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
4117 *expr_p = temp;
4118 return GS_OK;
4121 /* Gimplification of expression trees. */
4123 /* Gimplify an expression which appears at statement context; usually, this
4124 means replacing it with a suitably gimple STATEMENT_LIST. */
4126 void
4127 gimplify_stmt (tree *stmt_p)
4129 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
4132 /* Similarly, but force the result to be a STATEMENT_LIST. */
4134 void
4135 gimplify_to_stmt_list (tree *stmt_p)
4137 gimplify_stmt (stmt_p);
4138 if (!*stmt_p)
4139 *stmt_p = alloc_stmt_list ();
4140 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
4142 tree t = *stmt_p;
4143 *stmt_p = alloc_stmt_list ();
4144 append_to_statement_list (t, stmt_p);
4149 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
4150 to CTX. If entries already exist, force them to be some flavor of private.
4151 If there is no enclosing parallel, do nothing. */
4153 void
4154 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
4156 splay_tree_node n;
4158 if (decl == NULL || !DECL_P (decl))
4159 return;
4163 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4164 if (n != NULL)
4166 if (n->value & GOVD_SHARED)
4167 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
4168 else
4169 return;
4171 else if (ctx->is_parallel)
4172 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
4174 ctx = ctx->outer_context;
4176 while (ctx);
4179 /* Similarly for each of the type sizes of TYPE. */
4181 static void
4182 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
4184 if (type == NULL || type == error_mark_node)
4185 return;
4186 type = TYPE_MAIN_VARIANT (type);
4188 if (pointer_set_insert (ctx->privatized_types, type))
4189 return;
4191 switch (TREE_CODE (type))
4193 case INTEGER_TYPE:
4194 case ENUMERAL_TYPE:
4195 case BOOLEAN_TYPE:
4196 case REAL_TYPE:
4197 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
4198 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
4199 break;
4201 case ARRAY_TYPE:
4202 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4203 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
4204 break;
4206 case RECORD_TYPE:
4207 case UNION_TYPE:
4208 case QUAL_UNION_TYPE:
4210 tree field;
4211 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4212 if (TREE_CODE (field) == FIELD_DECL)
4214 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
4215 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
4218 break;
4220 case POINTER_TYPE:
4221 case REFERENCE_TYPE:
4222 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4223 break;
4225 default:
4226 break;
4229 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
4230 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
4231 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
4234 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
4236 static void
4237 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
4239 splay_tree_node n;
4240 unsigned int nflags;
4241 tree t;
4243 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4244 return;
4246 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
4247 there are constructors involved somewhere. */
4248 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
4249 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
4250 flags |= GOVD_SEEN;
4252 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4253 if (n != NULL)
4255 /* We shouldn't be re-adding the decl with the same data
4256 sharing class. */
4257 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
4258 /* The only combination of data sharing classes we should see is
4259 FIRSTPRIVATE and LASTPRIVATE. */
4260 nflags = n->value | flags;
4261 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
4262 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
4263 n->value = nflags;
4264 return;
4267 /* When adding a variable-sized variable, we have to handle all sorts
4268 of additional bits of data: the pointer replacement variable, and
4269 the parameters of the type. */
4270 if (!TREE_CONSTANT (DECL_SIZE (decl)))
4272 /* Add the pointer replacement variable as PRIVATE if the variable
4273 replacement is private, else FIRSTPRIVATE since we'll need the
4274 address of the original variable either for SHARED, or for the
4275 copy into or out of the context. */
4276 if (!(flags & GOVD_LOCAL))
4278 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
4279 nflags |= flags & GOVD_SEEN;
4280 t = DECL_VALUE_EXPR (decl);
4281 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
4282 t = TREE_OPERAND (t, 0);
4283 gcc_assert (DECL_P (t));
4284 omp_add_variable (ctx, t, nflags);
4287 /* Add all of the variable and type parameters (which should have
4288 been gimplified to a formal temporary) as FIRSTPRIVATE. */
4289 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
4290 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
4291 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4293 /* The variable-sized variable itself is never SHARED, only some form
4294 of PRIVATE. The sharing would take place via the pointer variable
4295 which we remapped above. */
4296 if (flags & GOVD_SHARED)
4297 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
4298 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
4300 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
4301 alloca statement we generate for the variable, so make sure it
4302 is available. This isn't automatically needed for the SHARED
4303 case, since we won't be allocating local storage then. */
4304 else
4305 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
4307 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
4309 gcc_assert ((flags & GOVD_LOCAL) == 0);
4310 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4312 /* Similar to the direct variable sized case above, we'll need the
4313 size of references being privatized. */
4314 if ((flags & GOVD_SHARED) == 0)
4316 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
4317 if (!TREE_CONSTANT (t))
4318 omp_notice_variable (ctx, t, true);
4322 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
4325 /* Record the fact that DECL was used within the OpenMP context CTX.
4326 IN_CODE is true when real code uses DECL, and false when we should
4327 merely emit default(none) errors. Return true if DECL is going to
4328 be remapped and thus DECL shouldn't be gimplified into its
4329 DECL_VALUE_EXPR (if any). */
4331 static bool
4332 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
4334 splay_tree_node n;
4335 unsigned flags = in_code ? GOVD_SEEN : 0;
4336 bool ret = false, shared;
4338 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4339 return false;
4341 /* Threadprivate variables are predetermined. */
4342 if (is_global_var (decl))
4344 if (DECL_THREAD_LOCAL_P (decl))
4345 return false;
4347 if (DECL_HAS_VALUE_EXPR_P (decl))
4349 tree value = get_base_address (DECL_VALUE_EXPR (decl));
4351 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
4352 return false;
4356 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4357 if (n == NULL)
4359 enum omp_clause_default_kind default_kind, kind;
4361 if (!ctx->is_parallel)
4362 goto do_outer;
4364 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
4365 remapped firstprivate instead of shared. To some extent this is
4366 addressed in omp_firstprivatize_type_sizes, but not effectively. */
4367 default_kind = ctx->default_kind;
4368 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
4369 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
4370 default_kind = kind;
4372 switch (default_kind)
4374 case OMP_CLAUSE_DEFAULT_NONE:
4375 error ("%qs not specified in enclosing parallel",
4376 IDENTIFIER_POINTER (DECL_NAME (decl)));
4377 error ("%Henclosing parallel", &ctx->location);
4378 /* FALLTHRU */
4379 case OMP_CLAUSE_DEFAULT_SHARED:
4380 flags |= GOVD_SHARED;
4381 break;
4382 case OMP_CLAUSE_DEFAULT_PRIVATE:
4383 flags |= GOVD_PRIVATE;
4384 break;
4385 default:
4386 gcc_unreachable ();
4389 omp_add_variable (ctx, decl, flags);
4391 shared = (flags & GOVD_SHARED) != 0;
4392 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4393 goto do_outer;
4396 shared = ((flags | n->value) & GOVD_SHARED) != 0;
4397 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4399 /* If nothing changed, there's nothing left to do. */
4400 if ((n->value & flags) == flags)
4401 return ret;
4402 flags |= n->value;
4403 n->value = flags;
4405 do_outer:
4406 /* If the variable is private in the current context, then we don't
4407 need to propagate anything to an outer context. */
4408 if (flags & GOVD_PRIVATE)
4409 return ret;
4410 if (ctx->outer_context
4411 && omp_notice_variable (ctx->outer_context, decl, in_code))
4412 return true;
4413 return ret;
4416 /* Verify that DECL is private within CTX. If there's specific information
4417 to the contrary in the innermost scope, generate an error. */
4419 static bool
4420 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
4422 splay_tree_node n;
4424 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4425 if (n != NULL)
4427 if (n->value & GOVD_SHARED)
4429 if (ctx == gimplify_omp_ctxp)
4430 error ("iteration variable %qs should be private",
4431 IDENTIFIER_POINTER (DECL_NAME (decl)));
4432 n->value = GOVD_PRIVATE;
4434 return true;
4437 if (ctx->outer_context)
4438 return omp_is_private (ctx->outer_context, decl);
4439 else if (ctx->is_parallel)
4440 return false;
4441 else
4442 return !is_global_var (decl);
4445 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
4446 and previous omp contexts. */
4448 static void
4449 gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel)
4451 struct gimplify_omp_ctx *ctx, *outer_ctx;
4452 tree c;
4454 ctx = new_omp_context (in_parallel);
4455 outer_ctx = ctx->outer_context;
4457 while ((c = *list_p) != NULL)
4459 enum gimplify_status gs;
4460 bool remove = false;
4461 bool notice_outer = true;
4462 unsigned int flags;
4463 tree decl;
4465 switch (OMP_CLAUSE_CODE (c))
4467 case OMP_CLAUSE_PRIVATE:
4468 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
4469 notice_outer = false;
4470 goto do_add;
4471 case OMP_CLAUSE_SHARED:
4472 flags = GOVD_SHARED | GOVD_EXPLICIT;
4473 goto do_add;
4474 case OMP_CLAUSE_FIRSTPRIVATE:
4475 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
4476 goto do_add;
4477 case OMP_CLAUSE_LASTPRIVATE:
4478 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
4479 goto do_add;
4480 case OMP_CLAUSE_REDUCTION:
4481 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
4482 goto do_add;
4484 do_add:
4485 decl = OMP_CLAUSE_DECL (c);
4486 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4488 remove = true;
4489 break;
4491 omp_add_variable (ctx, decl, flags);
4492 if (TREE_CODE (c) == OMP_CLAUSE_REDUCTION
4493 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4495 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
4496 GOVD_LOCAL);
4497 gimplify_omp_ctxp = ctx;
4498 push_gimplify_context ();
4499 gimplify_stmt (&OMP_CLAUSE_REDUCTION_INIT (c));
4500 pop_gimplify_context (OMP_CLAUSE_REDUCTION_INIT (c));
4501 push_gimplify_context ();
4502 gimplify_stmt (&OMP_CLAUSE_REDUCTION_MERGE (c));
4503 pop_gimplify_context (OMP_CLAUSE_REDUCTION_MERGE (c));
4504 gimplify_omp_ctxp = outer_ctx;
4506 if (notice_outer)
4507 goto do_notice;
4508 break;
4510 case OMP_CLAUSE_COPYIN:
4511 case OMP_CLAUSE_COPYPRIVATE:
4512 decl = OMP_CLAUSE_DECL (c);
4513 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4515 remove = true;
4516 break;
4518 do_notice:
4519 if (outer_ctx)
4520 omp_notice_variable (outer_ctx, decl, true);
4521 break;
4523 case OMP_CLAUSE_SCHEDULE:
4524 case OMP_CLAUSE_IF:
4525 case OMP_CLAUSE_NUM_THREADS:
4526 gs = gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
4527 is_gimple_val, fb_rvalue);
4528 if (gs == GS_ERROR)
4529 remove = true;
4530 break;
4532 case OMP_CLAUSE_NOWAIT:
4533 case OMP_CLAUSE_ORDERED:
4534 break;
4536 case OMP_CLAUSE_DEFAULT:
4537 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
4538 break;
4540 default:
4541 gcc_unreachable ();
4544 if (remove)
4545 *list_p = OMP_CLAUSE_CHAIN (c);
4546 else
4547 list_p = &OMP_CLAUSE_CHAIN (c);
4550 gimplify_omp_ctxp = ctx;
4553 /* For all variables that were not actually used within the context,
4554 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
4556 static int
4557 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
4559 tree *list_p = (tree *) data;
4560 tree decl = (tree) n->key;
4561 unsigned flags = n->value;
4562 enum omp_clause_code code;
4563 tree clause;
4564 bool private_debug;
4566 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
4567 return 0;
4568 if ((flags & GOVD_SEEN) == 0)
4569 return 0;
4570 if (flags & GOVD_DEBUG_PRIVATE)
4572 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
4573 private_debug = true;
4575 else
4576 private_debug
4577 = lang_hooks.decls.omp_private_debug_clause (decl,
4578 !!(flags & GOVD_SHARED));
4579 if (private_debug)
4580 code = OMP_CLAUSE_PRIVATE;
4581 else if (flags & GOVD_SHARED)
4583 if (is_global_var (decl))
4584 return 0;
4585 code = OMP_CLAUSE_SHARED;
4587 else if (flags & GOVD_PRIVATE)
4588 code = OMP_CLAUSE_PRIVATE;
4589 else if (flags & GOVD_FIRSTPRIVATE)
4590 code = OMP_CLAUSE_FIRSTPRIVATE;
4591 else
4592 gcc_unreachable ();
4594 clause = build_omp_clause (code);
4595 OMP_CLAUSE_DECL (clause) = decl;
4596 OMP_CLAUSE_CHAIN (clause) = *list_p;
4597 if (private_debug)
4598 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
4599 *list_p = clause;
4601 return 0;
4604 static void
4605 gimplify_adjust_omp_clauses (tree *list_p)
4607 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
4608 tree c, decl;
4610 while ((c = *list_p) != NULL)
4612 splay_tree_node n;
4613 bool remove = false;
4615 switch (OMP_CLAUSE_CODE (c))
4617 case OMP_CLAUSE_PRIVATE:
4618 case OMP_CLAUSE_SHARED:
4619 case OMP_CLAUSE_FIRSTPRIVATE:
4620 decl = OMP_CLAUSE_DECL (c);
4621 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4622 remove = !(n->value & GOVD_SEEN);
4623 if (! remove)
4625 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
4626 if ((n->value & GOVD_DEBUG_PRIVATE)
4627 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
4629 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
4630 || ((n->value & GOVD_DATA_SHARE_CLASS)
4631 == GOVD_PRIVATE));
4632 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
4633 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
4636 break;
4638 case OMP_CLAUSE_LASTPRIVATE:
4639 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
4640 accurately reflect the presence of a FIRSTPRIVATE clause. */
4641 decl = OMP_CLAUSE_DECL (c);
4642 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4643 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
4644 = (n->value & GOVD_FIRSTPRIVATE) != 0;
4645 break;
4647 case OMP_CLAUSE_REDUCTION:
4648 case OMP_CLAUSE_COPYIN:
4649 case OMP_CLAUSE_COPYPRIVATE:
4650 case OMP_CLAUSE_IF:
4651 case OMP_CLAUSE_NUM_THREADS:
4652 case OMP_CLAUSE_SCHEDULE:
4653 case OMP_CLAUSE_NOWAIT:
4654 case OMP_CLAUSE_ORDERED:
4655 case OMP_CLAUSE_DEFAULT:
4656 break;
4658 default:
4659 gcc_unreachable ();
4662 if (remove)
4663 *list_p = OMP_CLAUSE_CHAIN (c);
4664 else
4665 list_p = &OMP_CLAUSE_CHAIN (c);
4668 /* Add in any implicit data sharing. */
4669 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
4671 gimplify_omp_ctxp = ctx->outer_context;
4672 delete_omp_context (ctx);
4675 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
4676 gimplification of the body, as well as scanning the body for used
4677 variables. We need to do this scan now, because variable-sized
4678 decls will be decomposed during gimplification. */
4680 static enum gimplify_status
4681 gimplify_omp_parallel (tree *expr_p, tree *pre_p)
4683 tree expr = *expr_p;
4685 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p, true);
4687 push_gimplify_context ();
4689 gimplify_stmt (&OMP_PARALLEL_BODY (expr));
4691 if (TREE_CODE (OMP_PARALLEL_BODY (expr)) == BIND_EXPR)
4692 pop_gimplify_context (OMP_PARALLEL_BODY (expr));
4693 else
4694 pop_gimplify_context (NULL_TREE);
4696 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
4698 return GS_ALL_DONE;
4701 /* Gimplify the gross structure of an OMP_FOR statement. */
4703 static enum gimplify_status
4704 gimplify_omp_for (tree *expr_p, tree *pre_p)
4706 tree for_stmt, decl, t;
4707 enum gimplify_status ret = 0;
4709 for_stmt = *expr_p;
4711 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, false);
4713 t = OMP_FOR_INIT (for_stmt);
4714 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
4715 decl = TREE_OPERAND (t, 0);
4716 gcc_assert (DECL_P (decl));
4717 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl)));
4718 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (decl)));
4720 /* Make sure the iteration variable is private. */
4721 if (omp_is_private (gimplify_omp_ctxp, decl))
4722 omp_notice_variable (gimplify_omp_ctxp, decl, true);
4723 else
4724 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
4726 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
4727 NULL, is_gimple_val, fb_rvalue);
4729 t = OMP_FOR_COND (for_stmt);
4730 gcc_assert (COMPARISON_CLASS_P (t));
4731 gcc_assert (TREE_OPERAND (t, 0) == decl);
4733 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
4734 NULL, is_gimple_val, fb_rvalue);
4736 t = OMP_FOR_INCR (for_stmt);
4737 switch (TREE_CODE (t))
4739 case PREINCREMENT_EXPR:
4740 case POSTINCREMENT_EXPR:
4741 t = build_int_cst (TREE_TYPE (decl), 1);
4742 goto build_modify;
4743 case PREDECREMENT_EXPR:
4744 case POSTDECREMENT_EXPR:
4745 t = build_int_cst (TREE_TYPE (decl), -1);
4746 goto build_modify;
4747 build_modify:
4748 t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
4749 t = build2 (MODIFY_EXPR, void_type_node, decl, t);
4750 OMP_FOR_INCR (for_stmt) = t;
4751 break;
4753 case MODIFY_EXPR:
4754 gcc_assert (TREE_OPERAND (t, 0) == decl);
4755 t = TREE_OPERAND (t, 1);
4756 switch (TREE_CODE (t))
4758 case PLUS_EXPR:
4759 if (TREE_OPERAND (t, 1) == decl)
4761 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
4762 TREE_OPERAND (t, 0) = decl;
4763 break;
4765 case MINUS_EXPR:
4766 gcc_assert (TREE_OPERAND (t, 0) == decl);
4767 break;
4768 default:
4769 gcc_unreachable ();
4772 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
4773 NULL, is_gimple_val, fb_rvalue);
4774 break;
4776 default:
4777 gcc_unreachable ();
4780 gimplify_to_stmt_list (&OMP_FOR_BODY (for_stmt));
4781 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
4783 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
4786 /* Gimplify the gross structure of other OpenMP worksharing constructs.
4787 In particular, OMP_SECTIONS and OMP_SINGLE. */
4789 static enum gimplify_status
4790 gimplify_omp_workshare (tree *expr_p, tree *pre_p)
4792 tree stmt = *expr_p;
4794 gimplify_scan_omp_clauses (&OMP_CLAUSES (stmt), pre_p, false);
4795 gimplify_to_stmt_list (&OMP_BODY (stmt));
4796 gimplify_adjust_omp_clauses (&OMP_CLAUSES (stmt));
4798 return GS_ALL_DONE;
4801 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
4802 stabilized the lhs of the atomic operation as *ADDR. Return true if
4803 EXPR is this stabilized form. */
4805 static bool
4806 goa_lhs_expr_p (tree expr, tree addr)
4808 /* Also include casts to other type variants. The C front end is fond
4809 of adding these for e.g. volatile variables. This is like
4810 STRIP_TYPE_NOPS but includes the main variant lookup. */
4811 while ((TREE_CODE (expr) == NOP_EXPR
4812 || TREE_CODE (expr) == CONVERT_EXPR
4813 || TREE_CODE (expr) == NON_LVALUE_EXPR)
4814 && TREE_OPERAND (expr, 0) != error_mark_node
4815 && (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
4816 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))))
4817 expr = TREE_OPERAND (expr, 0);
4819 if (TREE_CODE (expr) == INDIRECT_REF && TREE_OPERAND (expr, 0) == addr)
4820 return true;
4821 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
4822 return true;
4823 return false;
4826 /* A subroutine of gimplify_omp_atomic. Attempt to implement the atomic
4827 operation as a __sync_fetch_and_op builtin. INDEX is log2 of the
4828 size of the data type, and thus usable to find the index of the builtin
4829 decl. Returns GS_UNHANDLED if the expression is not of the proper form. */
4831 static enum gimplify_status
4832 gimplify_omp_atomic_fetch_op (tree *expr_p, tree addr, tree rhs, int index)
4834 enum built_in_function base;
4835 tree decl, args, itype;
4836 enum insn_code *optab;
4838 /* Check for one of the supported fetch-op operations. */
4839 switch (TREE_CODE (rhs))
4841 case PLUS_EXPR:
4842 base = BUILT_IN_FETCH_AND_ADD_N;
4843 optab = sync_add_optab;
4844 break;
4845 case MINUS_EXPR:
4846 base = BUILT_IN_FETCH_AND_SUB_N;
4847 optab = sync_add_optab;
4848 break;
4849 case BIT_AND_EXPR:
4850 base = BUILT_IN_FETCH_AND_AND_N;
4851 optab = sync_and_optab;
4852 break;
4853 case BIT_IOR_EXPR:
4854 base = BUILT_IN_FETCH_AND_OR_N;
4855 optab = sync_ior_optab;
4856 break;
4857 case BIT_XOR_EXPR:
4858 base = BUILT_IN_FETCH_AND_XOR_N;
4859 optab = sync_xor_optab;
4860 break;
4861 default:
4862 return GS_UNHANDLED;
4865 /* Make sure the expression is of the proper form. */
4866 if (goa_lhs_expr_p (TREE_OPERAND (rhs, 0), addr))
4867 rhs = TREE_OPERAND (rhs, 1);
4868 else if (commutative_tree_code (TREE_CODE (rhs))
4869 && goa_lhs_expr_p (TREE_OPERAND (rhs, 1), addr))
4870 rhs = TREE_OPERAND (rhs, 0);
4871 else
4872 return GS_UNHANDLED;
4874 decl = built_in_decls[base + index + 1];
4875 itype = TREE_TYPE (TREE_TYPE (decl));
4877 if (optab[TYPE_MODE (itype)] == CODE_FOR_nothing)
4878 return GS_UNHANDLED;
4880 args = tree_cons (NULL, fold_convert (itype, rhs), NULL);
4881 args = tree_cons (NULL, addr, args);
4882 *expr_p = build_function_call_expr (decl, args);
4883 return GS_OK;
4886 /* A subroutine of gimplify_omp_atomic_pipeline. Walk *EXPR_P and replace
4887 appearences of *LHS_ADDR with LHS_VAR. If an expression does not involve
4888 the lhs, evaluate it into a temporary. Return 1 if the lhs appeared as
4889 a subexpression, 0 if it did not, or -1 if an error was encountered. */
4891 static int
4892 goa_stabilize_expr (tree *expr_p, tree *pre_p, tree lhs_addr, tree lhs_var)
4894 tree expr = *expr_p;
4895 int saw_lhs;
4897 if (goa_lhs_expr_p (expr, lhs_addr))
4899 *expr_p = lhs_var;
4900 return 1;
4902 if (is_gimple_val (expr))
4903 return 0;
4905 saw_lhs = 0;
4906 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
4908 case tcc_binary:
4909 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
4910 lhs_addr, lhs_var);
4911 case tcc_unary:
4912 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
4913 lhs_addr, lhs_var);
4914 break;
4915 default:
4916 break;
4919 if (saw_lhs == 0)
4921 enum gimplify_status gs;
4922 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
4923 if (gs != GS_ALL_DONE)
4924 saw_lhs = -1;
4927 return saw_lhs;
4930 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
4932 oldval = *addr;
4933 repeat:
4934 newval = rhs; // with oldval replacing *addr in rhs
4935 oldval = __sync_val_compare_and_swap (addr, oldval, newval);
4936 if (oldval != newval)
4937 goto repeat;
4939 INDEX is log2 of the size of the data type, and thus usable to find the
4940 index of the builtin decl. */
4942 static enum gimplify_status
4943 gimplify_omp_atomic_pipeline (tree *expr_p, tree *pre_p, tree addr,
4944 tree rhs, int index)
4946 tree oldval, oldival, oldival2, newval, newival, label;
4947 tree type, itype, cmpxchg, args, x, iaddr;
4949 cmpxchg = built_in_decls[BUILT_IN_VAL_COMPARE_AND_SWAP_N + index + 1];
4950 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
4951 itype = TREE_TYPE (TREE_TYPE (cmpxchg));
4953 if (sync_compare_and_swap[TYPE_MODE (itype)] == CODE_FOR_nothing)
4954 return GS_UNHANDLED;
4956 oldval = create_tmp_var (type, NULL);
4957 newval = create_tmp_var (type, NULL);
4959 /* Precompute as much of RHS as possible. In the same walk, replace
4960 occurrences of the lhs value with our temporary. */
4961 if (goa_stabilize_expr (&rhs, pre_p, addr, oldval) < 0)
4962 return GS_ERROR;
4964 x = build_fold_indirect_ref (addr);
4965 x = build2 (MODIFY_EXPR, void_type_node, oldval, x);
4966 gimplify_and_add (x, pre_p);
4968 /* For floating-point values, we'll need to view-convert them to integers
4969 so that we can perform the atomic compare and swap. Simplify the
4970 following code by always setting up the "i"ntegral variables. */
4971 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4973 oldival = oldval;
4974 newival = newval;
4975 iaddr = addr;
4977 else
4979 oldival = create_tmp_var (itype, NULL);
4980 newival = create_tmp_var (itype, NULL);
4982 x = build1 (VIEW_CONVERT_EXPR, itype, oldval);
4983 x = build2 (MODIFY_EXPR, void_type_node, oldival, x);
4984 gimplify_and_add (x, pre_p);
4985 iaddr = fold_convert (build_pointer_type (itype), addr);
4988 oldival2 = create_tmp_var (itype, NULL);
4990 label = create_artificial_label ();
4991 x = build1 (LABEL_EXPR, void_type_node, label);
4992 gimplify_and_add (x, pre_p);
4994 x = build2 (MODIFY_EXPR, void_type_node, newval, rhs);
4995 gimplify_and_add (x, pre_p);
4997 if (newval != newival)
4999 x = build1 (VIEW_CONVERT_EXPR, itype, newval);
5000 x = build2 (MODIFY_EXPR, void_type_node, newival, x);
5001 gimplify_and_add (x, pre_p);
5004 x = build2 (MODIFY_EXPR, void_type_node, oldival2, oldival);
5005 gimplify_and_add (x, pre_p);
5007 args = tree_cons (NULL, fold_convert (itype, newival), NULL);
5008 args = tree_cons (NULL, fold_convert (itype, oldival), args);
5009 args = tree_cons (NULL, iaddr, args);
5010 x = build_function_call_expr (cmpxchg, args);
5011 if (oldval == oldival)
5012 x = fold_convert (type, x);
5013 x = build2 (MODIFY_EXPR, void_type_node, oldival, x);
5014 gimplify_and_add (x, pre_p);
5016 /* For floating point, be prepared for the loop backedge. */
5017 if (oldval != oldival)
5019 x = build1 (VIEW_CONVERT_EXPR, type, oldival);
5020 x = build2 (MODIFY_EXPR, void_type_node, oldval, x);
5021 gimplify_and_add (x, pre_p);
5024 /* Note that we always perform the comparison as an integer, even for
5025 floating point. This allows the atomic operation to properly
5026 succeed even with NaNs and -0.0. */
5027 x = build3 (COND_EXPR, void_type_node,
5028 build2 (NE_EXPR, boolean_type_node, oldival, oldival2),
5029 build1 (GOTO_EXPR, void_type_node, label), NULL);
5030 gimplify_and_add (x, pre_p);
5032 *expr_p = NULL;
5033 return GS_ALL_DONE;
5036 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
5038 GOMP_atomic_start ();
5039 *addr = rhs;
5040 GOMP_atomic_end ();
5042 The result is not globally atomic, but works so long as all parallel
5043 references are within #pragma omp atomic directives. According to
5044 responses received from omp@openmp.org, appears to be within spec.
5045 Which makes sense, since that's how several other compilers handle
5046 this situation as well. */
5048 static enum gimplify_status
5049 gimplify_omp_atomic_mutex (tree *expr_p, tree *pre_p, tree addr, tree rhs)
5051 tree t;
5053 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_START];
5054 t = build_function_call_expr (t, NULL);
5055 gimplify_and_add (t, pre_p);
5057 t = build_fold_indirect_ref (addr);
5058 t = build2 (MODIFY_EXPR, void_type_node, t, rhs);
5059 gimplify_and_add (t, pre_p);
5061 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_END];
5062 t = build_function_call_expr (t, NULL);
5063 gimplify_and_add (t, pre_p);
5065 *expr_p = NULL;
5066 return GS_ALL_DONE;
5069 /* Gimplify an OMP_ATOMIC statement. */
5071 static enum gimplify_status
5072 gimplify_omp_atomic (tree *expr_p, tree *pre_p)
5074 tree addr = TREE_OPERAND (*expr_p, 0);
5075 tree rhs = TREE_OPERAND (*expr_p, 1);
5076 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5077 HOST_WIDE_INT index;
5079 /* Make sure the type is one of the supported sizes. */
5080 index = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5081 index = exact_log2 (index);
5082 if (index >= 0 && index <= 4)
5084 enum gimplify_status gs;
5085 unsigned int align;
5087 if (DECL_P (TREE_OPERAND (addr, 0)))
5088 align = DECL_ALIGN_UNIT (TREE_OPERAND (addr, 0));
5089 else if (TREE_CODE (TREE_OPERAND (addr, 0)) == COMPONENT_REF
5090 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (addr, 0), 1))
5091 == FIELD_DECL)
5092 align = DECL_ALIGN_UNIT (TREE_OPERAND (TREE_OPERAND (addr, 0), 1));
5093 else
5094 align = TYPE_ALIGN_UNIT (type);
5096 /* __sync builtins require strict data alignment. */
5097 if (exact_log2 (align) >= index)
5099 /* When possible, use specialized atomic update functions. */
5100 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5102 gs = gimplify_omp_atomic_fetch_op (expr_p, addr, rhs, index);
5103 if (gs != GS_UNHANDLED)
5104 return gs;
5107 /* If we don't have specialized __sync builtins, try and implement
5108 as a compare and swap loop. */
5109 gs = gimplify_omp_atomic_pipeline (expr_p, pre_p, addr, rhs, index);
5110 if (gs != GS_UNHANDLED)
5111 return gs;
5115 /* The ultimate fallback is wrapping the operation in a mutex. */
5116 return gimplify_omp_atomic_mutex (expr_p, pre_p, addr, rhs);
5119 /* Gimplifies the expression tree pointed to by EXPR_P. Return 0 if
5120 gimplification failed.
5122 PRE_P points to the list where side effects that must happen before
5123 EXPR should be stored.
5125 POST_P points to the list where side effects that must happen after
5126 EXPR should be stored, or NULL if there is no suitable list. In
5127 that case, we copy the result to a temporary, emit the
5128 post-effects, and then return the temporary.
5130 GIMPLE_TEST_F points to a function that takes a tree T and
5131 returns nonzero if T is in the GIMPLE form requested by the
5132 caller. The GIMPLE predicates are in tree-gimple.c.
5134 This test is used twice. Before gimplification, the test is
5135 invoked to determine whether *EXPR_P is already gimple enough. If
5136 that fails, *EXPR_P is gimplified according to its code and
5137 GIMPLE_TEST_F is called again. If the test still fails, then a new
5138 temporary variable is created and assigned the value of the
5139 gimplified expression.
5141 FALLBACK tells the function what sort of a temporary we want. If the 1
5142 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
5143 If both are set, either is OK, but an lvalue is preferable.
5145 The return value is either GS_ERROR or GS_ALL_DONE, since this function
5146 iterates until solution. */
5148 enum gimplify_status
5149 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
5150 bool (* gimple_test_f) (tree), fallback_t fallback)
5152 tree tmp;
5153 tree internal_pre = NULL_TREE;
5154 tree internal_post = NULL_TREE;
5155 tree save_expr;
5156 int is_statement = (pre_p == NULL);
5157 location_t saved_location;
5158 enum gimplify_status ret;
5160 save_expr = *expr_p;
5161 if (save_expr == NULL_TREE)
5162 return GS_ALL_DONE;
5164 /* We used to check the predicate here and return immediately if it
5165 succeeds. This is wrong; the design is for gimplification to be
5166 idempotent, and for the predicates to only test for valid forms, not
5167 whether they are fully simplified. */
5169 /* Set up our internal queues if needed. */
5170 if (pre_p == NULL)
5171 pre_p = &internal_pre;
5172 if (post_p == NULL)
5173 post_p = &internal_post;
5175 saved_location = input_location;
5176 if (save_expr != error_mark_node
5177 && EXPR_HAS_LOCATION (*expr_p))
5178 input_location = EXPR_LOCATION (*expr_p);
5180 /* Loop over the specific gimplifiers until the toplevel node
5181 remains the same. */
5184 /* Strip away as many useless type conversions as possible
5185 at the toplevel. */
5186 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
5188 /* Remember the expr. */
5189 save_expr = *expr_p;
5191 /* Die, die, die, my darling. */
5192 if (save_expr == error_mark_node
5193 || (TREE_TYPE (save_expr)
5194 && TREE_TYPE (save_expr) == error_mark_node))
5196 ret = GS_ERROR;
5197 break;
5200 /* Do any language-specific gimplification. */
5201 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
5202 if (ret == GS_OK)
5204 if (*expr_p == NULL_TREE)
5205 break;
5206 if (*expr_p != save_expr)
5207 continue;
5209 else if (ret != GS_UNHANDLED)
5210 break;
5212 ret = GS_OK;
5213 switch (TREE_CODE (*expr_p))
5215 /* First deal with the special cases. */
5217 case POSTINCREMENT_EXPR:
5218 case POSTDECREMENT_EXPR:
5219 case PREINCREMENT_EXPR:
5220 case PREDECREMENT_EXPR:
5221 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
5222 fallback != fb_none);
5223 break;
5225 case ARRAY_REF:
5226 case ARRAY_RANGE_REF:
5227 case REALPART_EXPR:
5228 case IMAGPART_EXPR:
5229 case COMPONENT_REF:
5230 case VIEW_CONVERT_EXPR:
5231 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
5232 fallback ? fallback : fb_rvalue);
5233 break;
5235 case COND_EXPR:
5236 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
5237 /* C99 code may assign to an array in a structure value of a
5238 conditional expression, and this has undefined behavior
5239 only on execution, so create a temporary if an lvalue is
5240 required. */
5241 if (fallback == fb_lvalue)
5243 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5244 lang_hooks.mark_addressable (*expr_p);
5246 break;
5248 case CALL_EXPR:
5249 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
5250 /* C99 code may assign to an array in a structure returned
5251 from a function, and this has undefined behavior only on
5252 execution, so create a temporary if an lvalue is
5253 required. */
5254 if (fallback == fb_lvalue)
5256 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5257 lang_hooks.mark_addressable (*expr_p);
5259 break;
5261 case TREE_LIST:
5262 gcc_unreachable ();
5264 case COMPOUND_EXPR:
5265 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
5266 break;
5268 case MODIFY_EXPR:
5269 case INIT_EXPR:
5270 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
5271 fallback != fb_none);
5273 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer
5274 useful. */
5275 if (*expr_p && TREE_CODE (*expr_p) == INIT_EXPR)
5276 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
5277 break;
5279 case TRUTH_ANDIF_EXPR:
5280 case TRUTH_ORIF_EXPR:
5281 ret = gimplify_boolean_expr (expr_p);
5282 break;
5284 case TRUTH_NOT_EXPR:
5285 TREE_OPERAND (*expr_p, 0)
5286 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
5287 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5288 is_gimple_val, fb_rvalue);
5289 recalculate_side_effects (*expr_p);
5290 break;
5292 case ADDR_EXPR:
5293 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
5294 break;
5296 case VA_ARG_EXPR:
5297 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
5298 break;
5300 case CONVERT_EXPR:
5301 case NOP_EXPR:
5302 if (IS_EMPTY_STMT (*expr_p))
5304 ret = GS_ALL_DONE;
5305 break;
5308 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
5309 || fallback == fb_none)
5311 /* Just strip a conversion to void (or in void context) and
5312 try again. */
5313 *expr_p = TREE_OPERAND (*expr_p, 0);
5314 break;
5317 ret = gimplify_conversion (expr_p);
5318 if (ret == GS_ERROR)
5319 break;
5320 if (*expr_p != save_expr)
5321 break;
5322 /* FALLTHRU */
5324 case FIX_TRUNC_EXPR:
5325 case FIX_CEIL_EXPR:
5326 case FIX_FLOOR_EXPR:
5327 case FIX_ROUND_EXPR:
5328 /* unary_expr: ... | '(' cast ')' val | ... */
5329 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5330 is_gimple_val, fb_rvalue);
5331 recalculate_side_effects (*expr_p);
5332 break;
5334 case INDIRECT_REF:
5335 *expr_p = fold_indirect_ref (*expr_p);
5336 if (*expr_p != save_expr)
5337 break;
5338 /* else fall through. */
5339 case ALIGN_INDIRECT_REF:
5340 case MISALIGNED_INDIRECT_REF:
5341 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5342 is_gimple_reg, fb_rvalue);
5343 recalculate_side_effects (*expr_p);
5344 break;
5346 /* Constants need not be gimplified. */
5347 case INTEGER_CST:
5348 case REAL_CST:
5349 case STRING_CST:
5350 case COMPLEX_CST:
5351 case VECTOR_CST:
5352 ret = GS_ALL_DONE;
5353 break;
5355 case CONST_DECL:
5356 /* If we require an lvalue, such as for ADDR_EXPR, retain the
5357 CONST_DECL node. Otherwise the decl is replaceable by its
5358 value. */
5359 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
5360 if (fallback & fb_lvalue)
5361 ret = GS_ALL_DONE;
5362 else
5363 *expr_p = DECL_INITIAL (*expr_p);
5364 break;
5366 case DECL_EXPR:
5367 ret = gimplify_decl_expr (expr_p);
5368 break;
5370 case EXC_PTR_EXPR:
5371 /* FIXME make this a decl. */
5372 ret = GS_ALL_DONE;
5373 break;
5375 case BIND_EXPR:
5376 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
5377 break;
5379 case LOOP_EXPR:
5380 ret = gimplify_loop_expr (expr_p, pre_p);
5381 break;
5383 case SWITCH_EXPR:
5384 ret = gimplify_switch_expr (expr_p, pre_p);
5385 break;
5387 case EXIT_EXPR:
5388 ret = gimplify_exit_expr (expr_p);
5389 break;
5391 case GOTO_EXPR:
5392 /* If the target is not LABEL, then it is a computed jump
5393 and the target needs to be gimplified. */
5394 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
5395 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
5396 NULL, is_gimple_val, fb_rvalue);
5397 break;
5399 case LABEL_EXPR:
5400 ret = GS_ALL_DONE;
5401 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
5402 == current_function_decl);
5403 break;
5405 case CASE_LABEL_EXPR:
5406 ret = gimplify_case_label_expr (expr_p);
5407 break;
5409 case RETURN_EXPR:
5410 ret = gimplify_return_expr (*expr_p, pre_p);
5411 break;
5413 case CONSTRUCTOR:
5414 /* Don't reduce this in place; let gimplify_init_constructor work its
5415 magic. Buf if we're just elaborating this for side effects, just
5416 gimplify any element that has side-effects. */
5417 if (fallback == fb_none)
5419 unsigned HOST_WIDE_INT ix;
5420 constructor_elt *ce;
5421 tree temp = NULL_TREE;
5422 for (ix = 0;
5423 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (*expr_p),
5424 ix, ce);
5425 ix++)
5426 if (TREE_SIDE_EFFECTS (ce->value))
5427 append_to_statement_list (ce->value, &temp);
5429 *expr_p = temp;
5430 ret = GS_OK;
5432 /* C99 code may assign to an array in a constructed
5433 structure or union, and this has undefined behavior only
5434 on execution, so create a temporary if an lvalue is
5435 required. */
5436 else if (fallback == fb_lvalue)
5438 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5439 lang_hooks.mark_addressable (*expr_p);
5441 else
5442 ret = GS_ALL_DONE;
5443 break;
5445 /* The following are special cases that are not handled by the
5446 original GIMPLE grammar. */
5448 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
5449 eliminated. */
5450 case SAVE_EXPR:
5451 ret = gimplify_save_expr (expr_p, pre_p, post_p);
5452 break;
5454 case BIT_FIELD_REF:
5456 enum gimplify_status r0, r1, r2;
5458 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5459 is_gimple_lvalue, fb_either);
5460 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5461 is_gimple_val, fb_rvalue);
5462 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
5463 is_gimple_val, fb_rvalue);
5464 recalculate_side_effects (*expr_p);
5466 ret = MIN (r0, MIN (r1, r2));
5468 break;
5470 case NON_LVALUE_EXPR:
5471 /* This should have been stripped above. */
5472 gcc_unreachable ();
5474 case ASM_EXPR:
5475 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
5476 break;
5478 case TRY_FINALLY_EXPR:
5479 case TRY_CATCH_EXPR:
5480 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
5481 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
5482 ret = GS_ALL_DONE;
5483 break;
5485 case CLEANUP_POINT_EXPR:
5486 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
5487 break;
5489 case TARGET_EXPR:
5490 ret = gimplify_target_expr (expr_p, pre_p, post_p);
5491 break;
5493 case CATCH_EXPR:
5494 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
5495 ret = GS_ALL_DONE;
5496 break;
5498 case EH_FILTER_EXPR:
5499 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
5500 ret = GS_ALL_DONE;
5501 break;
5503 case OBJ_TYPE_REF:
5505 enum gimplify_status r0, r1;
5506 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
5507 is_gimple_val, fb_rvalue);
5508 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
5509 is_gimple_val, fb_rvalue);
5510 ret = MIN (r0, r1);
5512 break;
5514 case LABEL_DECL:
5515 /* We get here when taking the address of a label. We mark
5516 the label as "forced"; meaning it can never be removed and
5517 it is a potential target for any computed goto. */
5518 FORCED_LABEL (*expr_p) = 1;
5519 ret = GS_ALL_DONE;
5520 break;
5522 case STATEMENT_LIST:
5523 ret = gimplify_statement_list (expr_p);
5524 break;
5526 case WITH_SIZE_EXPR:
5528 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5529 post_p == &internal_post ? NULL : post_p,
5530 gimple_test_f, fallback);
5531 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5532 is_gimple_val, fb_rvalue);
5534 break;
5536 case VAR_DECL:
5537 case PARM_DECL:
5538 ret = gimplify_var_or_parm_decl (expr_p);
5539 break;
5541 case SSA_NAME:
5542 /* Allow callbacks into the gimplifier during optimization. */
5543 ret = GS_ALL_DONE;
5544 break;
5546 case OMP_PARALLEL:
5547 ret = gimplify_omp_parallel (expr_p, pre_p);
5548 break;
5550 case OMP_FOR:
5551 ret = gimplify_omp_for (expr_p, pre_p);
5552 break;
5554 case OMP_SECTIONS:
5555 case OMP_SINGLE:
5556 ret = gimplify_omp_workshare (expr_p, pre_p);
5557 break;
5559 case OMP_SECTION:
5560 case OMP_MASTER:
5561 case OMP_ORDERED:
5562 case OMP_CRITICAL:
5563 gimplify_to_stmt_list (&OMP_BODY (*expr_p));
5564 break;
5566 case OMP_ATOMIC:
5567 ret = gimplify_omp_atomic (expr_p, pre_p);
5568 break;
5570 case OMP_RETURN_EXPR:
5571 ret = GS_ALL_DONE;
5572 break;
5574 default:
5575 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
5577 case tcc_comparison:
5578 /* If this is a comparison of objects of aggregate type,
5579 handle it specially (by converting to a call to
5580 memcmp). It would be nice to only have to do this
5581 for variable-sized objects, but then we'd have to
5582 allow the same nest of reference nodes we allow for
5583 MODIFY_EXPR and that's too complex. */
5584 if (!AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1))))
5585 goto expr_2;
5586 ret = gimplify_variable_sized_compare (expr_p);
5587 break;
5589 /* If *EXPR_P does not need to be special-cased, handle it
5590 according to its class. */
5591 case tcc_unary:
5592 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5593 post_p, is_gimple_val, fb_rvalue);
5594 break;
5596 case tcc_binary:
5597 expr_2:
5599 enum gimplify_status r0, r1;
5601 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5602 post_p, is_gimple_val, fb_rvalue);
5603 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
5604 post_p, is_gimple_val, fb_rvalue);
5606 ret = MIN (r0, r1);
5607 break;
5610 case tcc_declaration:
5611 case tcc_constant:
5612 ret = GS_ALL_DONE;
5613 goto dont_recalculate;
5615 default:
5616 gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
5617 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
5618 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
5619 goto expr_2;
5622 recalculate_side_effects (*expr_p);
5623 dont_recalculate:
5624 break;
5627 /* If we replaced *expr_p, gimplify again. */
5628 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
5629 ret = GS_ALL_DONE;
5631 while (ret == GS_OK);
5633 /* If we encountered an error_mark somewhere nested inside, either
5634 stub out the statement or propagate the error back out. */
5635 if (ret == GS_ERROR)
5637 if (is_statement)
5638 *expr_p = NULL;
5639 goto out;
5642 /* This was only valid as a return value from the langhook, which
5643 we handled. Make sure it doesn't escape from any other context. */
5644 gcc_assert (ret != GS_UNHANDLED);
5646 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
5648 /* We aren't looking for a value, and we don't have a valid
5649 statement. If it doesn't have side-effects, throw it away. */
5650 if (!TREE_SIDE_EFFECTS (*expr_p))
5651 *expr_p = NULL;
5652 else if (!TREE_THIS_VOLATILE (*expr_p))
5654 /* This is probably a _REF that contains something nested that
5655 has side effects. Recurse through the operands to find it. */
5656 enum tree_code code = TREE_CODE (*expr_p);
5658 switch (code)
5660 case COMPONENT_REF:
5661 case REALPART_EXPR: case IMAGPART_EXPR:
5662 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5663 gimple_test_f, fallback);
5664 break;
5666 case ARRAY_REF: case ARRAY_RANGE_REF:
5667 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5668 gimple_test_f, fallback);
5669 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5670 gimple_test_f, fallback);
5671 break;
5673 default:
5674 /* Anything else with side-effects must be converted to
5675 a valid statement before we get here. */
5676 gcc_unreachable ();
5679 *expr_p = NULL;
5681 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
5683 /* Historically, the compiler has treated a bare
5684 reference to a volatile lvalue as forcing a load. */
5685 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
5686 /* Normally, we do want to create a temporary for a
5687 TREE_ADDRESSABLE type because such a type should not be
5688 copied by bitwise-assignment. However, we make an
5689 exception here, as all we are doing here is ensuring that
5690 we read the bytes that make up the type. We use
5691 create_tmp_var_raw because create_tmp_var will abort when
5692 given a TREE_ADDRESSABLE type. */
5693 tree tmp = create_tmp_var_raw (type, "vol");
5694 gimple_add_tmp_var (tmp);
5695 *expr_p = build2 (MODIFY_EXPR, type, tmp, *expr_p);
5697 else
5698 /* We can't do anything useful with a volatile reference to
5699 incomplete type, so just throw it away. */
5700 *expr_p = NULL;
5703 /* If we are gimplifying at the statement level, we're done. Tack
5704 everything together and replace the original statement with the
5705 gimplified form. */
5706 if (fallback == fb_none || is_statement)
5708 if (internal_pre || internal_post)
5710 append_to_statement_list (*expr_p, &internal_pre);
5711 append_to_statement_list (internal_post, &internal_pre);
5712 annotate_all_with_locus (&internal_pre, input_location);
5713 *expr_p = internal_pre;
5715 else if (!*expr_p)
5717 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
5718 annotate_all_with_locus (expr_p, input_location);
5719 else
5720 annotate_one_with_locus (*expr_p, input_location);
5721 goto out;
5724 /* Otherwise we're gimplifying a subexpression, so the resulting value is
5725 interesting. */
5727 /* If it's sufficiently simple already, we're done. Unless we are
5728 handling some post-effects internally; if that's the case, we need to
5729 copy into a temp before adding the post-effects to the tree. */
5730 if (!internal_post && (*gimple_test_f) (*expr_p))
5731 goto out;
5733 /* Otherwise, we need to create a new temporary for the gimplified
5734 expression. */
5736 /* We can't return an lvalue if we have an internal postqueue. The
5737 object the lvalue refers to would (probably) be modified by the
5738 postqueue; we need to copy the value out first, which means an
5739 rvalue. */
5740 if ((fallback & fb_lvalue) && !internal_post
5741 && is_gimple_addressable (*expr_p))
5743 /* An lvalue will do. Take the address of the expression, store it
5744 in a temporary, and replace the expression with an INDIRECT_REF of
5745 that temporary. */
5746 tmp = build_fold_addr_expr (*expr_p);
5747 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
5748 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
5750 else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
5752 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
5754 /* An rvalue will do. Assign the gimplified expression into a new
5755 temporary TMP and replace the original expression with TMP. */
5757 if (internal_post || (fallback & fb_lvalue))
5758 /* The postqueue might change the value of the expression between
5759 the initialization and use of the temporary, so we can't use a
5760 formal temp. FIXME do we care? */
5761 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5762 else
5763 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
5765 if (TREE_CODE (*expr_p) != SSA_NAME)
5766 DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
5768 else
5770 #ifdef ENABLE_CHECKING
5771 if (!(fallback & fb_mayfail))
5773 fprintf (stderr, "gimplification failed:\n");
5774 print_generic_expr (stderr, *expr_p, 0);
5775 debug_tree (*expr_p);
5776 internal_error ("gimplification failed");
5778 #endif
5779 gcc_assert (fallback & fb_mayfail);
5780 /* If this is an asm statement, and the user asked for the
5781 impossible, don't die. Fail and let gimplify_asm_expr
5782 issue an error. */
5783 ret = GS_ERROR;
5784 goto out;
5787 /* Make sure the temporary matches our predicate. */
5788 gcc_assert ((*gimple_test_f) (*expr_p));
5790 if (internal_post)
5792 annotate_all_with_locus (&internal_post, input_location);
5793 append_to_statement_list (internal_post, pre_p);
5796 out:
5797 input_location = saved_location;
5798 return ret;
5801 /* Look through TYPE for variable-sized objects and gimplify each such
5802 size that we find. Add to LIST_P any statements generated. */
5804 void
5805 gimplify_type_sizes (tree type, tree *list_p)
5807 tree field, t;
5809 if (type == NULL || type == error_mark_node)
5810 return;
5812 /* We first do the main variant, then copy into any other variants. */
5813 type = TYPE_MAIN_VARIANT (type);
5815 /* Avoid infinite recursion. */
5816 if (TYPE_SIZES_GIMPLIFIED (type))
5817 return;
5819 TYPE_SIZES_GIMPLIFIED (type) = 1;
5821 switch (TREE_CODE (type))
5823 case INTEGER_TYPE:
5824 case ENUMERAL_TYPE:
5825 case BOOLEAN_TYPE:
5826 case REAL_TYPE:
5827 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
5828 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
5830 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5832 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
5833 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
5835 break;
5837 case ARRAY_TYPE:
5838 /* These types may not have declarations, so handle them here. */
5839 gimplify_type_sizes (TREE_TYPE (type), list_p);
5840 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
5841 break;
5843 case RECORD_TYPE:
5844 case UNION_TYPE:
5845 case QUAL_UNION_TYPE:
5846 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5847 if (TREE_CODE (field) == FIELD_DECL)
5849 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
5850 gimplify_type_sizes (TREE_TYPE (field), list_p);
5852 break;
5854 case POINTER_TYPE:
5855 case REFERENCE_TYPE:
5856 gimplify_type_sizes (TREE_TYPE (type), list_p);
5857 break;
5859 default:
5860 break;
5863 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
5864 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
5866 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5868 TYPE_SIZE (t) = TYPE_SIZE (type);
5869 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
5870 TYPE_SIZES_GIMPLIFIED (t) = 1;
5874 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
5875 a size or position, has had all of its SAVE_EXPRs evaluated.
5876 We add any required statements to STMT_P. */
5878 void
5879 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
5881 tree type, expr = *expr_p;
5883 /* We don't do anything if the value isn't there, is constant, or contains
5884 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
5885 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
5886 will want to replace it with a new variable, but that will cause problems
5887 if this type is from outside the function. It's OK to have that here. */
5888 if (expr == NULL_TREE || TREE_CONSTANT (expr)
5889 || TREE_CODE (expr) == VAR_DECL
5890 || CONTAINS_PLACEHOLDER_P (expr))
5891 return;
5893 type = TREE_TYPE (expr);
5894 *expr_p = unshare_expr (expr);
5896 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
5897 expr = *expr_p;
5899 /* Verify that we've an exact type match with the original expression.
5900 In particular, we do not wish to drop a "sizetype" in favour of a
5901 type of similar dimensions. We don't want to pollute the generic
5902 type-stripping code with this knowledge because it doesn't matter
5903 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
5904 and friends retain their "sizetype-ness". */
5905 if (TREE_TYPE (expr) != type
5906 && TREE_CODE (type) == INTEGER_TYPE
5907 && TYPE_IS_SIZETYPE (type))
5909 tree tmp;
5911 *expr_p = create_tmp_var (type, NULL);
5912 tmp = build1 (NOP_EXPR, type, expr);
5913 tmp = build2 (MODIFY_EXPR, type, *expr_p, tmp);
5914 if (EXPR_HAS_LOCATION (expr))
5915 SET_EXPR_LOCUS (tmp, EXPR_LOCUS (expr));
5916 else
5917 SET_EXPR_LOCATION (tmp, input_location);
5919 gimplify_and_add (tmp, stmt_p);
5923 #ifdef ENABLE_CHECKING
5924 /* Compare types A and B for a "close enough" match. */
5926 static bool
5927 cpt_same_type (tree a, tree b)
5929 if (lang_hooks.types_compatible_p (a, b))
5930 return true;
5932 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
5933 link them together. This routine is intended to catch type errors
5934 that will affect the optimizers, and the optimizers don't add new
5935 dereferences of function pointers, so ignore it. */
5936 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
5937 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
5938 return true;
5940 /* ??? The C FE pushes type qualifiers after the fact into the type of
5941 the element from the type of the array. See build_unary_op's handling
5942 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
5943 should have done it when creating the variable in the first place.
5944 Alternately, why aren't the two array types made variants? */
5945 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
5946 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
5948 /* And because of those, we have to recurse down through pointers. */
5949 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
5950 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
5952 return false;
5955 /* Check for some cases of the front end missing cast expressions.
5956 The type of a dereference should correspond to the pointer type;
5957 similarly the type of an address should match its object. */
5959 static tree
5960 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
5961 void *data ATTRIBUTE_UNUSED)
5963 tree t = *tp;
5964 tree ptype, otype, dtype;
5966 switch (TREE_CODE (t))
5968 case INDIRECT_REF:
5969 case ARRAY_REF:
5970 otype = TREE_TYPE (t);
5971 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
5972 dtype = TREE_TYPE (ptype);
5973 gcc_assert (cpt_same_type (otype, dtype));
5974 break;
5976 case ADDR_EXPR:
5977 ptype = TREE_TYPE (t);
5978 otype = TREE_TYPE (TREE_OPERAND (t, 0));
5979 dtype = TREE_TYPE (ptype);
5980 if (!cpt_same_type (otype, dtype))
5982 /* &array is allowed to produce a pointer to the element, rather than
5983 a pointer to the array type. We must allow this in order to
5984 properly represent assigning the address of an array in C into
5985 pointer to the element type. */
5986 gcc_assert (TREE_CODE (otype) == ARRAY_TYPE
5987 && POINTER_TYPE_P (ptype)
5988 && cpt_same_type (TREE_TYPE (otype), dtype));
5989 break;
5991 break;
5993 default:
5994 return NULL_TREE;
5998 return NULL_TREE;
6000 #endif
6002 /* Gimplify the body of statements pointed to by BODY_P. FNDECL is the
6003 function decl containing BODY. */
6005 void
6006 gimplify_body (tree *body_p, tree fndecl, bool do_parms)
6008 location_t saved_location = input_location;
6009 tree body, parm_stmts;
6011 timevar_push (TV_TREE_GIMPLIFY);
6013 gcc_assert (gimplify_ctxp == NULL);
6014 push_gimplify_context ();
6016 /* Unshare most shared trees in the body and in that of any nested functions.
6017 It would seem we don't have to do this for nested functions because
6018 they are supposed to be output and then the outer function gimplified
6019 first, but the g++ front end doesn't always do it that way. */
6020 unshare_body (body_p, fndecl);
6021 unvisit_body (body_p, fndecl);
6023 /* Make sure input_location isn't set to something wierd. */
6024 input_location = DECL_SOURCE_LOCATION (fndecl);
6026 /* Resolve callee-copies. This has to be done before processing
6027 the body so that DECL_VALUE_EXPR gets processed correctly. */
6028 parm_stmts = do_parms ? gimplify_parameters () : NULL;
6030 /* Gimplify the function's body. */
6031 gimplify_stmt (body_p);
6032 body = *body_p;
6034 if (!body)
6035 body = alloc_stmt_list ();
6036 else if (TREE_CODE (body) == STATEMENT_LIST)
6038 tree t = expr_only (*body_p);
6039 if (t)
6040 body = t;
6043 /* If there isn't an outer BIND_EXPR, add one. */
6044 if (TREE_CODE (body) != BIND_EXPR)
6046 tree b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6047 NULL_TREE, NULL_TREE);
6048 TREE_SIDE_EFFECTS (b) = 1;
6049 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
6050 body = b;
6053 /* If we had callee-copies statements, insert them at the beginning
6054 of the function. */
6055 if (parm_stmts)
6057 append_to_statement_list_force (BIND_EXPR_BODY (body), &parm_stmts);
6058 BIND_EXPR_BODY (body) = parm_stmts;
6061 /* Unshare again, in case gimplification was sloppy. */
6062 unshare_all_trees (body);
6064 *body_p = body;
6066 pop_gimplify_context (body);
6067 gcc_assert (gimplify_ctxp == NULL);
6069 #ifdef ENABLE_CHECKING
6070 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
6071 #endif
6073 timevar_pop (TV_TREE_GIMPLIFY);
6074 input_location = saved_location;
6077 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
6078 node for the function we want to gimplify. */
6080 void
6081 gimplify_function_tree (tree fndecl)
6083 tree oldfn, parm, ret;
6085 oldfn = current_function_decl;
6086 current_function_decl = fndecl;
6087 cfun = DECL_STRUCT_FUNCTION (fndecl);
6088 if (cfun == NULL)
6089 allocate_struct_function (fndecl);
6091 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = TREE_CHAIN (parm))
6093 /* Preliminarily mark non-addressed complex variables as eligible
6094 for promotion to gimple registers. We'll transform their uses
6095 as we find them. */
6096 if (TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
6097 && !TREE_THIS_VOLATILE (parm)
6098 && !needs_to_live_in_memory (parm))
6099 DECL_COMPLEX_GIMPLE_REG_P (parm) = 1;
6102 ret = DECL_RESULT (fndecl);
6103 if (TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
6104 && !needs_to_live_in_memory (ret))
6105 DECL_COMPLEX_GIMPLE_REG_P (ret) = 1;
6107 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl, true);
6109 /* If we're instrumenting function entry/exit, then prepend the call to
6110 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
6111 catch the exit hook. */
6112 /* ??? Add some way to ignore exceptions for this TFE. */
6113 if (flag_instrument_function_entry_exit
6114 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
6116 tree tf, x, bind;
6118 tf = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
6119 TREE_SIDE_EFFECTS (tf) = 1;
6120 x = DECL_SAVED_TREE (fndecl);
6121 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
6122 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
6123 x = build_function_call_expr (x, NULL);
6124 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
6126 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6127 TREE_SIDE_EFFECTS (bind) = 1;
6128 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
6129 x = build_function_call_expr (x, NULL);
6130 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
6131 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
6133 DECL_SAVED_TREE (fndecl) = bind;
6136 current_function_decl = oldfn;
6137 cfun = oldfn ? DECL_STRUCT_FUNCTION (oldfn) : NULL;
6141 /* Expands EXPR to list of gimple statements STMTS. If SIMPLE is true,
6142 force the result to be either ssa_name or an invariant, otherwise
6143 just force it to be a rhs expression. If VAR is not NULL, make the
6144 base variable of the final destination be VAR if suitable. */
6146 tree
6147 force_gimple_operand (tree expr, tree *stmts, bool simple, tree var)
6149 tree t;
6150 enum gimplify_status ret;
6151 gimple_predicate gimple_test_f;
6153 *stmts = NULL_TREE;
6155 if (is_gimple_val (expr))
6156 return expr;
6158 gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
6160 push_gimplify_context ();
6161 gimplify_ctxp->into_ssa = in_ssa_p;
6163 if (var)
6164 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
6166 ret = gimplify_expr (&expr, stmts, NULL,
6167 gimple_test_f, fb_rvalue);
6168 gcc_assert (ret != GS_ERROR);
6170 if (referenced_vars)
6172 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
6173 add_referenced_tmp_var (t);
6176 pop_gimplify_context (NULL);
6178 return expr;
6181 /* Invokes force_gimple_operand for EXPR with parameters SIMPLE_P and VAR. If
6182 some statements are produced, emits them before BSI. */
6184 tree
6185 force_gimple_operand_bsi (block_stmt_iterator *bsi, tree expr,
6186 bool simple_p, tree var)
6188 tree stmts;
6190 expr = force_gimple_operand (expr, &stmts, simple_p, var);
6191 if (stmts)
6192 bsi_insert_before (bsi, stmts, BSI_SAME_STMT);
6194 return expr;
6197 #include "gt-gimplify.h"