Check for Altivec mode when returning altivec register.
[official-gcc.git] / gcc / gimplify.c
blob21d8f453e22101b4c9171582cd9a29187108b4ae
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 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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, 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 "errors.h"
31 #include "varray.h"
32 #include "tree-gimple.h"
33 #include "tree-inline.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "tree-flow.h"
38 #include "cgraph.h"
39 #include "timevar.h"
40 #include "except.h"
41 #include "hashtab.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "function.h"
45 #include "output.h"
46 #include "expr.h"
47 #include "ggc.h"
48 #include "target.h"
50 static struct gimplify_ctx
52 tree current_bind_expr;
53 bool save_stack;
54 tree temps;
55 tree conditional_cleanups;
56 int conditions;
57 tree exit_label;
58 tree return_temp;
59 varray_type case_labels;
60 /* The formal temporary table. Should this be persistent? */
61 htab_t temp_htab;
62 } *gimplify_ctxp;
65 /* Formal (expression) temporary table handling: Multiple occurrences of
66 the same scalar expression are evaluated into the same temporary. */
68 typedef struct gimple_temp_hash_elt
70 tree val; /* Key */
71 tree temp; /* Value */
72 } elt_t;
74 /* Forward declarations. */
75 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
78 /* Return a hash value for a formal temporary table entry. */
80 static hashval_t
81 gimple_tree_hash (const void *p)
83 tree t = ((const elt_t *) p)->val;
84 return iterative_hash_expr (t, 0);
87 /* Compare two formal temporary table entries. */
89 static int
90 gimple_tree_eq (const void *p1, const void *p2)
92 tree t1 = ((const elt_t *) p1)->val;
93 tree t2 = ((const elt_t *) p2)->val;
94 enum tree_code code = TREE_CODE (t1);
96 if (TREE_CODE (t2) != code
97 || TREE_TYPE (t1) != TREE_TYPE (t2))
98 return 0;
100 if (!operand_equal_p (t1, t2, 0))
101 return 0;
103 /* Only allow them to compare equal if they also hash equal; otherwise
104 results are nondeterminate, and we fail bootstrap comparison. */
105 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
106 abort ();
108 return 1;
111 /* Set up a context for the gimplifier. */
113 void
114 push_gimplify_context (void)
116 if (gimplify_ctxp)
117 abort ();
118 gimplify_ctxp
119 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
120 gimplify_ctxp->temp_htab
121 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
124 /* Tear down a context for the gimplifier. If BODY is non-null, then
125 put the temporaries into the outer BIND_EXPR. Otherwise, put them
126 in the unexpanded_var_list. */
128 void
129 pop_gimplify_context (tree body)
131 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
132 abort ();
134 if (body)
135 declare_tmp_vars (gimplify_ctxp->temps, body);
136 else
137 record_vars (gimplify_ctxp->temps);
139 #if 0
140 if (!quiet_flag)
141 fprintf (stderr, " collisions: %f ",
142 htab_collisions (gimplify_ctxp->temp_htab));
143 #endif
145 htab_delete (gimplify_ctxp->temp_htab);
146 free (gimplify_ctxp);
147 gimplify_ctxp = NULL;
150 void
151 gimple_push_bind_expr (tree bind)
153 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
154 gimplify_ctxp->current_bind_expr = bind;
157 void
158 gimple_pop_bind_expr (void)
160 gimplify_ctxp->current_bind_expr
161 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
164 tree
165 gimple_current_bind_expr (void)
167 return gimplify_ctxp->current_bind_expr;
170 /* Returns true iff there is a COND_EXPR between us and the innermost
171 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
173 static bool
174 gimple_conditional_context (void)
176 return gimplify_ctxp->conditions > 0;
179 /* Note that we've entered a COND_EXPR. */
181 static void
182 gimple_push_condition (void)
184 ++(gimplify_ctxp->conditions);
187 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
188 now, add any conditional cleanups we've seen to the prequeue. */
190 static void
191 gimple_pop_condition (tree *pre_p)
193 int conds = --(gimplify_ctxp->conditions);
195 if (conds == 0)
197 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
198 gimplify_ctxp->conditional_cleanups = NULL_TREE;
200 else if (conds < 0)
201 abort ();
204 /* A subroutine of append_to_statement_list{,_force}. */
206 static void
207 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
209 tree list = *list_p;
210 tree_stmt_iterator i;
212 if (!side_effects)
213 return;
215 if (!list)
217 if (t && TREE_CODE (t) == STATEMENT_LIST)
219 *list_p = t;
220 return;
222 *list_p = list = alloc_stmt_list ();
225 i = tsi_last (list);
226 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
229 /* Add T to the end of the list container pointed by LIST_P.
230 If T is an expression with no effects, it is ignored. */
232 void
233 append_to_statement_list (tree t, tree *list_p)
235 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
238 /* Similar, but the statement is always added, regardless of side effects. */
240 void
241 append_to_statement_list_force (tree t, tree *list_p)
243 append_to_statement_list_1 (t, list_p, t != NULL);
246 /* Both gimplify the statement T and append it to LIST_P. */
248 void
249 gimplify_and_add (tree t, tree *list_p)
251 gimplify_stmt (&t);
252 append_to_statement_list (t, list_p);
255 /* Strip off a legitimate source ending from the input string NAME of
256 length LEN. Rather than having to know the names used by all of
257 our front ends, we strip off an ending of a period followed by
258 up to five characters. (Java uses ".class".) */
260 static inline void
261 remove_suffix (char *name, int len)
263 int i;
265 for (i = 2; i < 8 && len > i; i++)
267 if (name[len - i] == '.')
269 name[len - i] = '\0';
270 break;
275 /* Create a nameless artificial label and put it in the current function
276 context. Returns the newly created label. */
278 tree
279 create_artificial_label (void)
281 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
283 DECL_ARTIFICIAL (lab) = 1;
284 DECL_CONTEXT (lab) = current_function_decl;
285 return lab;
288 /* Create a new temporary name with PREFIX. Returns an identifier. */
290 static GTY(()) unsigned int tmp_var_id_num;
292 tree
293 create_tmp_var_name (const char *prefix)
295 char *tmp_name;
297 if (prefix)
299 char *preftmp = ASTRDUP (prefix);
301 remove_suffix (preftmp, strlen (preftmp));
302 prefix = preftmp;
305 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
306 return get_identifier (tmp_name);
310 /* Create a new temporary variable declaration of type TYPE.
311 Does NOT push it into the current binding. */
313 tree
314 create_tmp_var_raw (tree type, const char *prefix)
316 tree tmp_var;
317 tree new_type;
319 /* Make the type of the variable writable. */
320 new_type = build_type_variant (type, 0, 0);
321 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
323 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
325 /* The variable was declared by the compiler. */
326 DECL_ARTIFICIAL (tmp_var) = 1;
327 /* And we don't want debug info for it. */
328 DECL_IGNORED_P (tmp_var) = 1;
330 /* Make the variable writable. */
331 TREE_READONLY (tmp_var) = 0;
333 DECL_EXTERNAL (tmp_var) = 0;
334 TREE_STATIC (tmp_var) = 0;
335 TREE_USED (tmp_var) = 1;
337 return tmp_var;
340 /* Create a new temporary variable declaration of type TYPE. DOES push the
341 variable into the current binding. Further, assume that this is called
342 only from gimplification or optimization, at which point the creation of
343 certain types are bugs. */
345 tree
346 create_tmp_var (tree type, const char *prefix)
348 tree tmp_var;
350 #if defined ENABLE_CHECKING
351 /* We don't allow types that are addressable (meaning we can't make copies),
352 incomplete, or of variable size. */
353 if (TREE_ADDRESSABLE (type)
354 || !COMPLETE_TYPE_P (type)
355 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
356 abort ();
357 #endif
359 tmp_var = create_tmp_var_raw (type, prefix);
360 gimple_add_tmp_var (tmp_var);
361 return tmp_var;
364 /* Given a tree, try to return a useful variable name that we can use
365 to prefix a temporary that is being assigned the value of the tree.
366 I.E. given <temp> = &A, return A. */
368 const char *
369 get_name (tree t)
371 tree stripped_decl;
373 stripped_decl = t;
374 STRIP_NOPS (stripped_decl);
375 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
376 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
377 else
379 switch (TREE_CODE (stripped_decl))
381 case ADDR_EXPR:
382 return get_name (TREE_OPERAND (stripped_decl, 0));
383 break;
384 default:
385 return NULL;
390 /* Create a temporary with a name derived from VAL. Subroutine of
391 lookup_tmp_var; nobody else should call this function. */
393 static inline tree
394 create_tmp_from_val (tree val)
396 return create_tmp_var (TREE_TYPE (val), get_name (val));
399 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
400 an existing expression temporary. */
402 static tree
403 lookup_tmp_var (tree val, bool is_formal)
405 if (!is_formal || TREE_SIDE_EFFECTS (val))
406 return create_tmp_from_val (val);
407 else
409 elt_t elt, *elt_p;
410 void **slot;
412 elt.val = val;
413 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
414 if (*slot == NULL)
416 elt_p = xmalloc (sizeof (*elt_p));
417 elt_p->val = val;
418 elt_p->temp = create_tmp_from_val (val);
419 TREE_READONLY (elt_p->temp) = 1;
420 *slot = (void *) elt_p;
422 else
423 elt_p = (elt_t *) *slot;
425 return elt_p->temp;
429 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
430 in gimplify_expr. Only use this function if:
432 1) The value of the unfactored expression represented by VAL will not
433 change between the initialization and use of the temporary, and
434 2) The temporary will not be otherwise modified.
436 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
437 and #2 means it is inappropriate for && temps.
439 For other cases, use get_initialized_tmp_var instead. */
441 static tree
442 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
444 tree t, mod;
445 char class;
447 gimplify_expr (&val, pre_p, post_p, is_gimple_tmp_rhs, fb_rvalue);
449 t = lookup_tmp_var (val, is_formal);
451 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
453 class = TREE_CODE_CLASS (TREE_CODE (val));
454 if (EXPR_HAS_LOCATION (val))
455 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
456 else
457 SET_EXPR_LOCATION (mod, input_location);
459 /* gimplify_modify_expr might want to reduce this further. */
460 gimplify_and_add (mod, pre_p);
461 return t;
464 tree
465 get_formal_tmp_var (tree val, tree *pre_p)
467 return internal_get_tmp_var (val, pre_p, NULL, true);
470 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
471 are as in gimplify_expr. */
473 tree
474 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
476 return internal_get_tmp_var (val, pre_p, post_p, false);
479 /* Declares all the variables in VARS in SCOPE. */
481 void
482 declare_tmp_vars (tree vars, tree scope)
484 tree last = vars;
485 if (last)
487 tree temps;
489 /* C99 mode puts the default 'return 0;' for main outside the outer
490 braces. So drill down until we find an actual scope. */
491 while (TREE_CODE (scope) == COMPOUND_EXPR)
492 scope = TREE_OPERAND (scope, 0);
494 if (TREE_CODE (scope) != BIND_EXPR)
495 abort ();
497 temps = nreverse (last);
498 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
499 BIND_EXPR_VARS (scope) = temps;
503 void
504 gimple_add_tmp_var (tree tmp)
506 if (TREE_CHAIN (tmp) || DECL_SEEN_IN_BIND_EXPR_P (tmp))
507 abort ();
509 DECL_CONTEXT (tmp) = current_function_decl;
510 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
512 if (gimplify_ctxp)
514 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
515 gimplify_ctxp->temps = tmp;
517 else if (cfun)
518 record_vars (tmp);
519 else
520 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
523 /* Determines whether to assign a locus to the statement STMT. */
525 static bool
526 should_carry_locus_p (tree stmt)
528 /* Don't emit a line note for a label. We particularly don't want to
529 emit one for the break label, since it doesn't actually correspond
530 to the beginning of the loop/switch. */
531 if (TREE_CODE (stmt) == LABEL_EXPR)
532 return false;
534 /* Do not annotate empty statements, since it confuses gcov. */
535 if (!TREE_SIDE_EFFECTS (stmt))
536 return false;
538 return true;
541 static void
542 annotate_one_with_locus (tree t, location_t locus)
544 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
545 && ! EXPR_HAS_LOCATION (t)
546 && should_carry_locus_p (t))
547 SET_EXPR_LOCATION (t, locus);
550 void
551 annotate_all_with_locus (tree *stmt_p, location_t locus)
553 tree_stmt_iterator i;
555 if (!*stmt_p)
556 return;
558 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
560 tree t = tsi_stmt (i);
562 #ifdef ENABLE_CHECKING
563 /* Assuming we've already been gimplified, we shouldn't
564 see nested chaining constructs anymore. */
565 if (TREE_CODE (t) == STATEMENT_LIST
566 || TREE_CODE (t) == COMPOUND_EXPR)
567 abort ();
568 #endif
570 annotate_one_with_locus (t, locus);
574 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
575 These nodes model computations that should only be done once. If we
576 were to unshare something like SAVE_EXPR(i++), the gimplification
577 process would create wrong code. */
579 static tree
580 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
582 enum tree_code code = TREE_CODE (*tp);
583 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
584 if (TREE_CODE_CLASS (code) == 't'
585 || TREE_CODE_CLASS (code) == 'd'
586 || TREE_CODE_CLASS (code) == 'c'
587 || code == SAVE_EXPR || code == TARGET_EXPR
588 /* We can't do anything sensible with a BLOCK used as an expression,
589 but we also can't abort when we see it because of non-expression
590 uses. So just avert our eyes and cross our fingers. Silly Java. */
591 || code == BLOCK)
592 *walk_subtrees = 0;
593 else if (code == BIND_EXPR)
594 abort ();
595 else
596 copy_tree_r (tp, walk_subtrees, data);
598 return NULL_TREE;
601 /* Callback for walk_tree to unshare most of the shared trees rooted at
602 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
603 then *TP is deep copied by calling copy_tree_r.
605 This unshares the same trees as copy_tree_r with the exception of
606 SAVE_EXPR nodes. These nodes model computations that should only be
607 done once. If we were to unshare something like SAVE_EXPR(i++), the
608 gimplification process would create wrong code. */
610 static tree
611 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
612 void *data ATTRIBUTE_UNUSED)
614 tree t = *tp;
615 enum tree_code code = TREE_CODE (t);
617 /* Skip types, decls, and constants. But we do want to look at their
618 types and the bounds of types. Mark them as visited so we properly
619 unmark their subtrees on the unmark pass. If we've already seen them,
620 don't look down further. */
621 if (TREE_CODE_CLASS (code) == 't'
622 || TREE_CODE_CLASS (code) == 'd'
623 || TREE_CODE_CLASS (code) == 'c')
625 if (TREE_VISITED (t))
626 *walk_subtrees = 0;
627 else
628 TREE_VISITED (t) = 1;
631 /* If this node has been visited already, unshare it and don't look
632 any deeper. */
633 else if (TREE_VISITED (t))
635 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
636 *walk_subtrees = 0;
639 /* Otherwise, mark the tree as visited and keep looking. */
640 else
641 TREE_VISITED (t) = 1;
643 return NULL_TREE;
646 static tree
647 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
648 void *data ATTRIBUTE_UNUSED)
650 if (TREE_VISITED (*tp))
651 TREE_VISITED (*tp) = 0;
652 else
653 *walk_subtrees = 0;
655 return NULL_TREE;
658 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
659 bodies of any nested functions if we are unsharing the entire body of
660 FNDECL. */
662 static void
663 unshare_body (tree *body_p, tree fndecl)
665 struct cgraph_node *cgn = cgraph_node (fndecl);
667 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
668 if (body_p == &DECL_SAVED_TREE (fndecl))
669 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
670 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
673 /* Likewise, but mark all trees as not visited. */
675 static void
676 unvisit_body (tree *body_p, tree fndecl)
678 struct cgraph_node *cgn = cgraph_node (fndecl);
680 walk_tree (body_p, unmark_visited_r, NULL, NULL);
681 if (body_p == &DECL_SAVED_TREE (fndecl))
682 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
683 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
686 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
688 void
689 unshare_all_trees (tree t)
691 walk_tree (&t, copy_if_shared_r, NULL, NULL);
692 walk_tree (&t, unmark_visited_r, NULL, NULL);
695 /* Unconditionally make an unshared copy of EXPR. This is used when using
696 stored expressions which span multiple functions, such as BINFO_VTABLE,
697 as the normal unsharing process can't tell that they're shared. */
699 tree
700 unshare_expr (tree expr)
702 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
703 return expr;
706 /* A terser interface for building a representation of a exception
707 specification. */
709 tree
710 gimple_build_eh_filter (tree body, tree allowed, tree failure)
712 tree t;
714 /* FIXME should the allowed types go in TREE_TYPE? */
715 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
716 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
718 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
719 append_to_statement_list (body, &TREE_OPERAND (t, 0));
721 return t;
725 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
726 contain statements and have a value. Assign its value to a temporary
727 and give it void_type_node. Returns the temporary, or NULL_TREE if
728 WRAPPER was already void. */
730 tree
731 voidify_wrapper_expr (tree wrapper, tree temp)
733 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
735 tree *p, sub = wrapper;
737 restart:
738 /* Set p to point to the body of the wrapper. */
739 switch (TREE_CODE (sub))
741 case BIND_EXPR:
742 /* For a BIND_EXPR, the body is operand 1. */
743 p = &BIND_EXPR_BODY (sub);
744 break;
746 default:
747 p = &TREE_OPERAND (sub, 0);
748 break;
751 /* Advance to the last statement. Set all container types to void. */
752 if (TREE_CODE (*p) == STATEMENT_LIST)
754 tree_stmt_iterator i = tsi_last (*p);
755 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
757 else
759 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
761 TREE_SIDE_EFFECTS (*p) = 1;
762 TREE_TYPE (*p) = void_type_node;
766 if (p == NULL || IS_EMPTY_STMT (*p))
768 /* Look through exception handling. */
769 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
770 || TREE_CODE (*p) == TRY_CATCH_EXPR)
772 sub = *p;
773 goto restart;
775 /* The C++ frontend already did this for us. */
776 else if (TREE_CODE (*p) == INIT_EXPR
777 || TREE_CODE (*p) == TARGET_EXPR)
778 temp = TREE_OPERAND (*p, 0);
779 /* If we're returning a dereference, move the dereference
780 outside the wrapper. */
781 else if (TREE_CODE (*p) == INDIRECT_REF)
783 tree ptr = TREE_OPERAND (*p, 0);
784 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
785 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
786 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
787 /* If this is a BIND_EXPR for a const inline function, it might not
788 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
789 TREE_SIDE_EFFECTS (wrapper) = 1;
791 else
793 if (!temp)
794 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
795 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
796 TREE_SIDE_EFFECTS (wrapper) = 1;
799 TREE_TYPE (wrapper) = void_type_node;
800 return temp;
803 return NULL_TREE;
806 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
807 a temporary through which they communicate. */
809 static void
810 build_stack_save_restore (tree *save, tree *restore)
812 tree save_call, tmp_var;
814 save_call =
815 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
816 NULL_TREE);
817 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
819 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
820 *restore =
821 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
822 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
825 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
827 static enum gimplify_status
828 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
830 tree bind_expr = *expr_p;
831 bool old_save_stack = gimplify_ctxp->save_stack;
832 tree t;
834 temp = voidify_wrapper_expr (bind_expr, temp);
836 /* Mark variables seen in this bind expr. */
837 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
838 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
840 gimple_push_bind_expr (bind_expr);
841 gimplify_ctxp->save_stack = false;
843 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
845 if (gimplify_ctxp->save_stack)
847 tree stack_save, stack_restore;
849 /* Save stack on entry and restore it on exit. Add a try_finally
850 block to achieve this. Note that mudflap depends on the
851 format of the emitted code: see mx_register_decls(). */
852 build_stack_save_restore (&stack_save, &stack_restore);
854 t = build (TRY_FINALLY_EXPR, void_type_node,
855 BIND_EXPR_BODY (bind_expr), NULL_TREE);
856 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
858 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
859 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
860 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
863 gimplify_ctxp->save_stack = old_save_stack;
864 gimple_pop_bind_expr ();
866 if (temp)
868 *expr_p = temp;
869 append_to_statement_list (bind_expr, pre_p);
870 return GS_OK;
872 else
873 return GS_ALL_DONE;
876 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
877 GIMPLE value, it is assigned to a new temporary and the statement is
878 re-written to return the temporary.
880 PRE_P points to the list where side effects that must happen before
881 STMT should be stored. */
883 static enum gimplify_status
884 gimplify_return_expr (tree stmt, tree *pre_p)
886 tree ret_expr = TREE_OPERAND (stmt, 0);
887 tree result_decl, result;
889 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
890 || ret_expr == error_mark_node)
891 return GS_ALL_DONE;
893 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
894 result_decl = NULL_TREE;
895 else
897 result_decl = TREE_OPERAND (ret_expr, 0);
898 if (TREE_CODE (result_decl) == INDIRECT_REF)
899 /* See through a return by reference. */
900 result_decl = TREE_OPERAND (result_decl, 0);
901 #ifdef ENABLE_CHECKING
902 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
903 && TREE_CODE (ret_expr) != INIT_EXPR)
904 || TREE_CODE (result_decl) != RESULT_DECL)
905 abort ();
906 #endif
909 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
910 Recall that aggregate_value_p is FALSE for any aggregate type that is
911 returned in registers. If we're returning values in registers, then
912 we don't want to extend the lifetime of the RESULT_DECL, particularly
913 across another call. In addition, for those aggregates for which
914 hard_function_value generates a PARALLEL, we'll abort during normal
915 expansion of structure assignments; there's special code in expand_return
916 to handle this case that does not exist in expand_expr. */
917 if (!result_decl
918 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
919 result = result_decl;
920 else if (gimplify_ctxp->return_temp)
921 result = gimplify_ctxp->return_temp;
922 else
924 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
926 /* ??? With complex control flow (usually involving abnormal edges),
927 we can wind up warning about an uninitialized value for this. Due
928 to how this variable is constructed and initialized, this is never
929 true. Give up and never warn. */
930 TREE_NO_WARNING (result) = 1;
932 gimplify_ctxp->return_temp = result;
935 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
936 Then gimplify the whole thing. */
937 if (result != result_decl)
938 TREE_OPERAND (ret_expr, 0) = result;
940 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
942 /* If we didn't use a temporary, then the result is just the result_decl.
943 Otherwise we need a simple copy. This should already be gimple. */
944 if (result == result_decl)
945 ret_expr = result;
946 else
947 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
948 TREE_OPERAND (stmt, 0) = ret_expr;
950 return GS_ALL_DONE;
953 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
954 and initialization explicit. */
956 static enum gimplify_status
957 gimplify_decl_expr (tree *stmt_p)
959 tree stmt = *stmt_p;
960 tree decl = DECL_EXPR_DECL (stmt);
962 *stmt_p = NULL_TREE;
964 if (TREE_TYPE (decl) == error_mark_node)
965 return GS_ERROR;
967 else if (TREE_CODE (decl) == TYPE_DECL)
968 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
970 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
972 tree init = DECL_INITIAL (decl);
974 if (!TREE_CONSTANT (DECL_SIZE (decl)))
976 /* This is a variable-sized decl. Simplify its size and mark it
977 for deferred expansion. Note that mudflap depends on the format
978 of the emitted code: see mx_register_decls(). */
979 tree t, args, addr, ptr_type;
981 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
982 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
983 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
985 /* All occurences of this decl in final gimplified code will be
986 replaced by indirection. Setting DECL_VALUE_EXPR does two
987 things: First, it lets the rest of the gimplifier know what
988 replacement to use. Second, it lets the debug info know
989 where to find the value. */
990 ptr_type = build_pointer_type (TREE_TYPE (decl));
991 addr = create_tmp_var (ptr_type, get_name (decl));
992 DECL_IGNORED_P (addr) = 0;
993 t = build_fold_indirect_ref (addr);
994 DECL_VALUE_EXPR (decl) = t;
996 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
997 t = built_in_decls[BUILT_IN_ALLOCA];
998 t = build_function_call_expr (t, args);
999 t = fold_convert (ptr_type, t);
1000 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1002 gimplify_and_add (t, stmt_p);
1004 /* Indicate that we need to restore the stack level when the
1005 enclosing BIND_EXPR is exited. */
1006 gimplify_ctxp->save_stack = true;
1009 if (init && init != error_mark_node)
1011 if (!TREE_STATIC (decl))
1013 DECL_INITIAL (decl) = NULL_TREE;
1014 init = build (MODIFY_EXPR, void_type_node, decl, init);
1015 gimplify_and_add (init, stmt_p);
1017 else
1018 /* We must still examine initializers for static variables
1019 as they may contain a label address. */
1020 walk_tree (&init, force_labels_r, NULL, NULL);
1023 /* This decl isn't mentioned in the enclosing block, so add it to the
1024 list of temps. FIXME it seems a bit of a kludge to say that
1025 anonymous artificial vars aren't pushed, but everything else is. */
1026 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1027 gimple_add_tmp_var (decl);
1030 return GS_ALL_DONE;
1033 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1034 and replacing the LOOP_EXPR with goto, but if the loop contains an
1035 EXIT_EXPR, we need to append a label for it to jump to. */
1037 static enum gimplify_status
1038 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1040 tree saved_label = gimplify_ctxp->exit_label;
1041 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1042 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1044 append_to_statement_list (start_label, pre_p);
1046 gimplify_ctxp->exit_label = NULL_TREE;
1048 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1050 if (gimplify_ctxp->exit_label)
1052 append_to_statement_list (jump_stmt, pre_p);
1053 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1055 else
1056 *expr_p = jump_stmt;
1058 gimplify_ctxp->exit_label = saved_label;
1060 return GS_ALL_DONE;
1063 /* Compare two case labels. Because the front end should already have
1064 made sure that case ranges do not overlap, it is enough to only compare
1065 the CASE_LOW values of each case label. */
1067 static int
1068 compare_case_labels (const void *p1, const void *p2)
1070 tree case1 = *(tree *)p1;
1071 tree case2 = *(tree *)p2;
1073 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1076 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1078 void
1079 sort_case_labels (tree label_vec)
1081 size_t len = TREE_VEC_LENGTH (label_vec);
1082 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1084 if (CASE_LOW (default_case))
1086 size_t i;
1088 /* The last label in the vector should be the default case
1089 but it is not. */
1090 for (i = 0; i < len; ++i)
1092 tree t = TREE_VEC_ELT (label_vec, i);
1093 if (!CASE_LOW (t))
1095 default_case = t;
1096 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1097 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1098 break;
1103 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1104 compare_case_labels);
1107 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1108 branch to. */
1110 static enum gimplify_status
1111 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1113 tree switch_expr = *expr_p;
1114 enum gimplify_status ret;
1116 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1117 is_gimple_val, fb_rvalue);
1119 if (SWITCH_BODY (switch_expr))
1121 varray_type labels, saved_labels;
1122 tree label_vec, default_case = NULL_TREE;
1123 size_t i, len;
1125 /* If someone can be bothered to fill in the labels, they can
1126 be bothered to null out the body too. */
1127 if (SWITCH_LABELS (switch_expr))
1128 abort ();
1130 saved_labels = gimplify_ctxp->case_labels;
1131 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1133 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1135 labels = gimplify_ctxp->case_labels;
1136 gimplify_ctxp->case_labels = saved_labels;
1138 len = VARRAY_ACTIVE_SIZE (labels);
1140 for (i = 0; i < len; ++i)
1142 tree t = VARRAY_TREE (labels, i);
1143 if (!CASE_LOW (t))
1145 /* The default case must be the last label in the list. */
1146 default_case = t;
1147 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1148 len--;
1149 break;
1153 label_vec = make_tree_vec (len + 1);
1154 SWITCH_LABELS (*expr_p) = label_vec;
1155 append_to_statement_list (switch_expr, pre_p);
1157 if (! default_case)
1159 /* If the switch has no default label, add one, so that we jump
1160 around the switch body. */
1161 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1162 NULL_TREE, create_artificial_label ());
1163 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1164 *expr_p = build (LABEL_EXPR, void_type_node,
1165 CASE_LABEL (default_case));
1167 else
1168 *expr_p = SWITCH_BODY (switch_expr);
1170 for (i = 0; i < len; ++i)
1171 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1172 TREE_VEC_ELT (label_vec, len) = default_case;
1174 sort_case_labels (label_vec);
1176 SWITCH_BODY (switch_expr) = NULL;
1178 else if (!SWITCH_LABELS (switch_expr))
1179 abort ();
1181 return ret;
1184 static enum gimplify_status
1185 gimplify_case_label_expr (tree *expr_p)
1187 tree expr = *expr_p;
1188 if (gimplify_ctxp->case_labels)
1189 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1190 else
1191 abort ();
1192 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1193 return GS_ALL_DONE;
1196 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1197 a (possibly empty) body. */
1199 static enum gimplify_status
1200 gimplify_labeled_block_expr (tree *expr_p)
1202 tree body = LABELED_BLOCK_BODY (*expr_p);
1203 tree label = LABELED_BLOCK_LABEL (*expr_p);
1204 tree t;
1206 DECL_CONTEXT (label) = current_function_decl;
1207 t = build (LABEL_EXPR, void_type_node, label);
1208 if (body != NULL_TREE)
1209 t = build (COMPOUND_EXPR, void_type_node, body, t);
1210 *expr_p = t;
1212 return GS_OK;
1215 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1217 static enum gimplify_status
1218 gimplify_exit_block_expr (tree *expr_p)
1220 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1221 tree label;
1223 /* First operand must be a LABELED_BLOCK_EXPR, which should
1224 already be lowered (or partially lowered) when we get here. */
1225 #if defined ENABLE_CHECKING
1226 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1227 abort ();
1228 #endif
1230 label = LABELED_BLOCK_LABEL (labeled_block);
1231 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1233 return GS_OK;
1236 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1237 if necessary. */
1239 tree
1240 build_and_jump (tree *label_p)
1242 if (label_p == NULL)
1243 /* If there's nowhere to jump, just fall through. */
1244 return NULL_TREE;
1246 if (*label_p == NULL_TREE)
1248 tree label = create_artificial_label ();
1249 *label_p = label;
1252 return build1 (GOTO_EXPR, void_type_node, *label_p);
1255 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1256 This also involves building a label to jump to and communicating it to
1257 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1259 static enum gimplify_status
1260 gimplify_exit_expr (tree *expr_p)
1262 tree cond = TREE_OPERAND (*expr_p, 0);
1263 tree expr;
1265 expr = build_and_jump (&gimplify_ctxp->exit_label);
1266 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1267 *expr_p = expr;
1269 return GS_OK;
1272 /* A helper function to be called via walk_tree. Mark all labels under *TP
1273 as being forced. To be called for DECL_INITIAL of static variables. */
1275 tree
1276 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1278 if (TYPE_P (*tp))
1279 *walk_subtrees = 0;
1280 if (TREE_CODE (*tp) == LABEL_DECL)
1281 FORCED_LABEL (*tp) = 1;
1283 return NULL_TREE;
1286 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1287 different from its canonical type, wrap the whole thing inside a
1288 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1289 type.
1291 The canonical type of a COMPONENT_REF is the type of the field being
1292 referenced--unless the field is a bit-field which can be read directly
1293 in a smaller mode, in which case the canonical type is the
1294 sign-appropriate type corresponding to that mode. */
1296 static void
1297 canonicalize_component_ref (tree *expr_p)
1299 tree expr = *expr_p;
1300 tree type;
1302 if (TREE_CODE (expr) != COMPONENT_REF)
1303 abort ();
1305 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1306 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1307 else
1308 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1310 if (TREE_TYPE (expr) != type)
1312 tree old_type = TREE_TYPE (expr);
1314 /* Set the type of the COMPONENT_REF to the underlying type. */
1315 TREE_TYPE (expr) = type;
1317 /* And wrap the whole thing inside a NOP_EXPR. */
1318 expr = build1 (NOP_EXPR, old_type, expr);
1320 *expr_p = expr;
1324 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1325 to foo, embed that change in the ADDR_EXPR by converting
1326 T array[U];
1327 (T *)&array
1329 &array[L]
1330 where L is the lower bound. For simplicity, only do this for constant
1331 lower bound. */
1333 static void
1334 canonicalize_addr_expr (tree *expr_p)
1336 tree expr = *expr_p;
1337 tree ctype = TREE_TYPE (expr);
1338 tree addr_expr = TREE_OPERAND (expr, 0);
1339 tree atype = TREE_TYPE (addr_expr);
1340 tree dctype, datype, ddatype, otype, obj_expr;
1342 /* Both cast and addr_expr types should be pointers. */
1343 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1344 return;
1346 /* The addr_expr type should be a pointer to an array. */
1347 datype = TREE_TYPE (atype);
1348 if (TREE_CODE (datype) != ARRAY_TYPE)
1349 return;
1351 /* Both cast and addr_expr types should address the same object type. */
1352 dctype = TREE_TYPE (ctype);
1353 ddatype = TREE_TYPE (datype);
1354 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1355 return;
1357 /* The addr_expr and the object type should match. */
1358 obj_expr = TREE_OPERAND (addr_expr, 0);
1359 otype = TREE_TYPE (obj_expr);
1360 if (!lang_hooks.types_compatible_p (otype, datype))
1361 return;
1363 /* The lower bound and element sizes must be constant. */
1364 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1365 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1366 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1367 return;
1369 /* All checks succeeded. Build a new node to merge the cast. */
1370 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1371 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1372 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1373 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1374 size_int (TYPE_ALIGN (dctype)
1375 / BITS_PER_UNIT)));
1376 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1379 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1380 underneath as appropriate. */
1382 static enum gimplify_status
1383 gimplify_conversion (tree *expr_p)
1385 /* If we still have a conversion at the toplevel, then strip
1386 away all but the outermost conversion. */
1387 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1389 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1391 /* And remove the outermost conversion if it's useless. */
1392 if (tree_ssa_useless_type_conversion (*expr_p))
1393 *expr_p = TREE_OPERAND (*expr_p, 0);
1396 /* If we still have a conversion at the toplevel,
1397 then canonicalize some constructs. */
1398 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1400 tree sub = TREE_OPERAND (*expr_p, 0);
1402 /* If a NOP conversion is changing the type of a COMPONENT_REF
1403 expression, then canonicalize its type now in order to expose more
1404 redundant conversions. */
1405 if (TREE_CODE (sub) == COMPONENT_REF)
1406 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1408 /* If a NOP conversion is changing a pointer to array of foo
1409 to a pointer to foo, embed that change in the ADDR_EXPR. */
1410 else if (TREE_CODE (sub) == ADDR_EXPR)
1411 canonicalize_addr_expr (expr_p);
1414 return GS_OK;
1417 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1419 static enum gimplify_status
1420 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1422 tree op1 = TREE_OPERAND (*expr_p, 0);
1423 tree op2 = TREE_OPERAND (*expr_p, 1);
1424 enum tree_code code;
1425 enum gimplify_status r0, r1;
1427 if (TREE_CODE (*expr_p) == MIN_EXPR)
1428 code = LE_EXPR;
1429 else
1430 code = GE_EXPR;
1432 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1433 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1435 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1436 build (code, boolean_type_node, op1, op2),
1437 op1, op2);
1439 if (r0 == GS_ERROR || r1 == GS_ERROR)
1440 return GS_ERROR;
1441 else
1442 return GS_OK;
1445 /* Subroutine of gimplify_compound_lval.
1446 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1448 static enum gimplify_status
1449 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1451 tree array = TREE_OPERAND (*expr_p, 0);
1452 tree arrtype = TREE_TYPE (array);
1453 tree elttype = TREE_TYPE (arrtype);
1454 tree size = array_ref_element_size (*expr_p);
1455 tree ptrtype = build_pointer_type (elttype);
1456 enum tree_code add_code = PLUS_EXPR;
1457 tree idx = TREE_OPERAND (*expr_p, 1);
1458 tree minidx = unshare_expr (array_ref_low_bound (*expr_p));
1459 tree offset, addr, result;
1460 enum gimplify_status ret;
1462 /* If the array domain does not start at zero, apply the offset. */
1463 if (!integer_zerop (minidx))
1465 idx = convert (TREE_TYPE (minidx), idx);
1466 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1469 /* If the index is negative -- a technically invalid situation now
1470 that we've biased the index back to zero -- then casting it to
1471 unsigned has ill effects. In particular, -1*4U/4U != -1.
1472 Represent this as a subtraction of a positive rather than addition
1473 of a negative. This will prevent any conversion back to ARRAY_REF
1474 from getting the wrong results from the division. */
1475 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1477 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1478 add_code = MINUS_EXPR;
1481 /* Pointer arithmetic must be done in sizetype. */
1482 idx = fold_convert (sizetype, idx);
1484 /* Convert the index to a byte offset. */
1485 offset = size_binop (MULT_EXPR, size, idx);
1487 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1488 if (ret == GS_ERROR)
1489 return ret;
1491 addr = build_fold_addr_expr_with_type (array, ptrtype);
1492 result = fold (build (add_code, ptrtype, addr, offset));
1493 *expr_p = build1 (INDIRECT_REF, elttype, result);
1495 return GS_OK;
1498 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1499 node pointed by EXPR_P.
1501 compound_lval
1502 : min_lval '[' val ']'
1503 | min_lval '.' ID
1504 | compound_lval '[' val ']'
1505 | compound_lval '.' ID
1507 This is not part of the original SIMPLE definition, which separates
1508 array and member references, but it seems reasonable to handle them
1509 together. Also, this way we don't run into problems with union
1510 aliasing; gcc requires that for accesses through a union to alias, the
1511 union reference must be explicit, which was not always the case when we
1512 were splitting up array and member refs.
1514 PRE_P points to the list where side effects that must happen before
1515 *EXPR_P should be stored.
1517 POST_P points to the list where side effects that must happen after
1518 *EXPR_P should be stored. */
1520 static enum gimplify_status
1521 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1522 tree *post_p, fallback_t fallback)
1524 tree *p;
1525 varray_type stack;
1526 enum gimplify_status ret = GS_OK, tret;
1527 int i;
1529 /* Create a stack of the subexpressions so later we can walk them in
1530 order from inner to outer. */
1531 VARRAY_TREE_INIT (stack, 10, "stack");
1533 /* We can either handle REALPART_EXPR, IMAGEPART_EXPR anything that
1534 handled_components can deal with. */
1535 for (p = expr_p;
1536 (handled_component_p (*p)
1537 || TREE_CODE (*p) == REALPART_EXPR || TREE_CODE (*p) == IMAGPART_EXPR);
1538 p = &TREE_OPERAND (*p, 0))
1539 VARRAY_PUSH_TREE (stack, *p);
1541 #if defined ENABLE_CHECKING
1542 if (VARRAY_ACTIVE_SIZE (stack) == 0)
1543 abort ();
1544 #endif
1546 /* Now STACK is a stack of pointers to all the refs we've walked through
1547 and P points to the innermost expression.
1549 Java requires that we elaborated nodes in source order. That
1550 means we must gimplify the inner expression followed by each of
1551 the indices, in order. But we can't gimplify the inner
1552 expression until we deal with any variable bounds, sizes, or
1553 positions in order to deal with PLACEHOLDER_EXPRs.
1555 So we do this in three steps. First we deal with the annotations
1556 for any variables in the components, then we gimplify the base,
1557 then we gimplify any indices, from left to right. */
1558 for (i = VARRAY_ACTIVE_SIZE (stack) - 1; i >= 0; i--)
1560 tree t = VARRAY_TREE (stack, i);
1562 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1564 /* Gimplify the low bound and element type size and put them into
1565 the ARRAY_REF. If these values are set, they have already been
1566 gimplified. */
1567 if (!TREE_OPERAND (t, 2))
1569 tree low = unshare_expr (array_ref_low_bound (t));
1570 if (!is_gimple_min_invariant (low))
1572 TREE_OPERAND (t, 2) = low;
1573 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1574 is_gimple_tmp_reg, fb_rvalue);
1575 ret = MIN (ret, tret);
1579 if (!TREE_OPERAND (t, 3))
1581 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1582 tree elmt_size = unshare_expr (array_ref_element_size (t));
1583 tree factor = size_int (TYPE_ALIGN (elmt_type) / BITS_PER_UNIT);
1585 /* Divide the element size by the alignment of the element
1586 type (above). */
1587 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1589 if (!is_gimple_min_invariant (elmt_size))
1591 TREE_OPERAND (t, 3) = elmt_size;
1592 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1593 is_gimple_tmp_reg, fb_rvalue);
1594 ret = MIN (ret, tret);
1598 else if (TREE_CODE (t) == COMPONENT_REF)
1600 /* Set the field offset into T and gimplify it. */
1601 if (!TREE_OPERAND (t, 2))
1603 tree offset = unshare_expr (component_ref_field_offset (t));
1604 tree field = TREE_OPERAND (t, 1);
1605 tree factor
1606 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1608 /* Divide the offset by its alignment. */
1609 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1611 if (!is_gimple_min_invariant (offset))
1613 TREE_OPERAND (t, 2) = offset;
1614 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1615 is_gimple_tmp_reg, fb_rvalue);
1616 ret = MIN (ret, tret);
1622 /* Step 2 is to gimplify the base expression. */
1623 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1624 ret = MIN (ret, tret);
1626 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1627 loop we also remove any useless conversions. */
1628 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1630 tree t = VARRAY_TOP_TREE (stack);
1632 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1634 /* Gimplify the dimension.
1635 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1636 Gimplify non-constant array indices into a temporary
1637 variable.
1638 FIXME - The real fix is to gimplify post-modify
1639 expressions into a minimal gimple lvalue. However, that
1640 exposes bugs in alias analysis. The alias analyzer does
1641 not handle &PTR->FIELD very well. Will fix after the
1642 branch is merged into mainline (dnovillo 2004-05-03). */
1643 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1645 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1646 is_gimple_tmp_reg, fb_rvalue);
1647 ret = MIN (ret, tret);
1650 else if (TREE_CODE (t) == BIT_FIELD_REF)
1652 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1653 is_gimple_val, fb_rvalue);
1654 ret = MIN (ret, tret);
1655 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1656 is_gimple_val, fb_rvalue);
1657 ret = MIN (ret, tret);
1660 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1662 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1663 set which would have caused all the outer expressions in EXPR_P
1664 leading to P to also have had TREE_SIDE_EFFECTS set. */
1665 recalculate_side_effects (t);
1666 VARRAY_POP (stack);
1669 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1670 ret = MIN (ret, tret);
1672 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1673 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1675 canonicalize_component_ref (expr_p);
1676 ret = MIN (ret, GS_OK);
1679 return ret;
1682 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1684 PRE_P points to the list where side effects that must happen before
1685 *EXPR_P should be stored.
1687 POST_P points to the list where side effects that must happen after
1688 *EXPR_P should be stored.
1690 WANT_VALUE is nonzero iff we want to use the value of this expression
1691 in another expression. */
1693 static enum gimplify_status
1694 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1695 bool want_value)
1697 enum tree_code code;
1698 tree lhs, lvalue, rhs, t1;
1699 bool postfix;
1700 enum tree_code arith_code;
1701 enum gimplify_status ret;
1703 code = TREE_CODE (*expr_p);
1705 #if defined ENABLE_CHECKING
1706 if (code != POSTINCREMENT_EXPR
1707 && code != POSTDECREMENT_EXPR
1708 && code != PREINCREMENT_EXPR
1709 && code != PREDECREMENT_EXPR)
1710 abort ();
1711 #endif
1713 /* Prefix or postfix? */
1714 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1715 /* Faster to treat as prefix if result is not used. */
1716 postfix = want_value;
1717 else
1718 postfix = false;
1720 /* Add or subtract? */
1721 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1722 arith_code = PLUS_EXPR;
1723 else
1724 arith_code = MINUS_EXPR;
1726 /* Gimplify the LHS into a GIMPLE lvalue. */
1727 lvalue = TREE_OPERAND (*expr_p, 0);
1728 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1729 if (ret == GS_ERROR)
1730 return ret;
1732 /* Extract the operands to the arithmetic operation. */
1733 lhs = lvalue;
1734 rhs = TREE_OPERAND (*expr_p, 1);
1736 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1737 that as the result value and in the postqueue operation. */
1738 if (postfix)
1740 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1741 if (ret == GS_ERROR)
1742 return ret;
1745 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1746 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1748 if (postfix)
1750 gimplify_and_add (t1, post_p);
1751 *expr_p = lhs;
1752 return GS_ALL_DONE;
1754 else
1756 *expr_p = t1;
1757 return GS_OK;
1761 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1763 static void
1764 maybe_with_size_expr (tree *expr_p)
1766 tree expr = *expr_p;
1767 tree type = TREE_TYPE (expr);
1768 tree size;
1770 /* If we've already wrapped this or the type is error_mark_node, we can't do
1771 anything. */
1772 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1773 || type == error_mark_node)
1774 return;
1776 /* If the size isn't known or is a constant, we have nothing to do. */
1777 size = TYPE_SIZE_UNIT (type);
1778 if (!size || TREE_CODE (size) == INTEGER_CST)
1779 return;
1781 /* Otherwise, make a WITH_SIZE_EXPR. */
1782 size = unshare_expr (size);
1783 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1784 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1787 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1789 static enum gimplify_status
1790 gimplify_arg (tree *expr_p, tree *pre_p)
1792 bool (*test) (tree);
1793 fallback_t fb;
1795 /* In general, we allow lvalues for function arguments to avoid
1796 extra overhead of copying large aggregates out of even larger
1797 aggregates into temporaries only to copy the temporaries to
1798 the argument list. Make optimizers happy by pulling out to
1799 temporaries those types that fit in registers. */
1800 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
1801 test = is_gimple_val, fb = fb_rvalue;
1802 else
1803 test = is_gimple_lvalue, fb = fb_either;
1805 /* If this is a variable sized type, we must remember the size. */
1806 maybe_with_size_expr (expr_p);
1808 /* There is a sequence point before a function call. Side effects in
1809 the argument list must occur before the actual call. So, when
1810 gimplifying arguments, force gimplify_expr to use an internal
1811 post queue which is then appended to the end of PRE_P. */
1812 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
1815 /* Gimplify the CALL_EXPR node pointed by EXPR_P. PRE_P points to the
1816 list where side effects that must happen before *EXPR_P should be stored.
1817 WANT_VALUE is true if the result of the call is desired. */
1819 static enum gimplify_status
1820 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1822 tree decl;
1823 tree arglist;
1824 enum gimplify_status ret;
1826 #if defined ENABLE_CHECKING
1827 if (TREE_CODE (*expr_p) != CALL_EXPR)
1828 abort ();
1829 #endif
1831 /* For reliable diagnostics during inlining, it is necessary that
1832 every call_expr be annotated with file and line. */
1833 if (! EXPR_HAS_LOCATION (*expr_p))
1834 SET_EXPR_LOCATION (*expr_p, input_location);
1836 /* This may be a call to a builtin function.
1838 Builtin function calls may be transformed into different
1839 (and more efficient) builtin function calls under certain
1840 circumstances. Unfortunately, gimplification can muck things
1841 up enough that the builtin expanders are not aware that certain
1842 transformations are still valid.
1844 So we attempt transformation/gimplification of the call before
1845 we gimplify the CALL_EXPR. At this time we do not manage to
1846 transform all calls in the same manner as the expanders do, but
1847 we do transform most of them. */
1848 decl = get_callee_fndecl (*expr_p);
1849 if (decl && DECL_BUILT_IN (decl))
1851 tree new = simplify_builtin (*expr_p, !want_value);
1853 if (new && new != *expr_p)
1855 /* There was a transformation of this call which computes the
1856 same value, but in a more efficient way. Return and try
1857 again. */
1858 *expr_p = new;
1859 return GS_OK;
1862 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
1863 /* Avoid gimplifying the second argument to va_start, which needs
1864 to be the plain PARM_DECL. */
1865 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
1868 /* There is a sequence point before the call, so any side effects in
1869 the calling expression must occur before the actual call. Force
1870 gimplify_expr to use an internal post queue. */
1871 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1872 is_gimple_call_addr, fb_rvalue);
1874 if (PUSH_ARGS_REVERSED)
1875 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1876 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1877 arglist = TREE_CHAIN (arglist))
1879 enum gimplify_status t;
1881 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
1883 if (t == GS_ERROR)
1884 ret = GS_ERROR;
1886 if (PUSH_ARGS_REVERSED)
1887 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1889 /* Try this again in case gimplification exposed something. */
1890 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
1892 tree new = simplify_builtin (*expr_p, !want_value);
1894 if (new && new != *expr_p)
1896 /* There was a transformation of this call which computes the
1897 same value, but in a more efficient way. Return and try
1898 again. */
1899 *expr_p = new;
1900 return GS_OK;
1904 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1905 decl. This allows us to eliminate redundant or useless
1906 calls to "const" functions. */
1907 if (TREE_CODE (*expr_p) == CALL_EXPR
1908 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
1909 TREE_SIDE_EFFECTS (*expr_p) = 0;
1911 return ret;
1914 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
1915 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
1917 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
1918 condition is true or false, respectively. If null, we should generate
1919 our own to skip over the evaluation of this specific expression.
1921 This function is the tree equivalent of do_jump.
1923 shortcut_cond_r should only be called by shortcut_cond_expr. */
1925 static tree
1926 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
1928 tree local_label = NULL_TREE;
1929 tree t, expr = NULL;
1931 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
1932 retain the shortcut semantics. Just insert the gotos here;
1933 shortcut_cond_expr will append the real blocks later. */
1934 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1936 /* Turn if (a && b) into
1938 if (a); else goto no;
1939 if (b) goto yes; else goto no;
1940 (no:) */
1942 if (false_label_p == NULL)
1943 false_label_p = &local_label;
1945 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
1946 append_to_statement_list (t, &expr);
1948 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1949 false_label_p);
1950 append_to_statement_list (t, &expr);
1952 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1954 /* Turn if (a || b) into
1956 if (a) goto yes;
1957 if (b) goto yes; else goto no;
1958 (yes:) */
1960 if (true_label_p == NULL)
1961 true_label_p = &local_label;
1963 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
1964 append_to_statement_list (t, &expr);
1966 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1967 false_label_p);
1968 append_to_statement_list (t, &expr);
1970 else if (TREE_CODE (pred) == COND_EXPR)
1972 /* As long as we're messing with gotos, turn if (a ? b : c) into
1973 if (a)
1974 if (b) goto yes; else goto no;
1975 else
1976 if (c) goto yes; else goto no; */
1977 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
1978 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1979 false_label_p),
1980 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
1981 false_label_p));
1983 else
1985 expr = build (COND_EXPR, void_type_node, pred,
1986 build_and_jump (true_label_p),
1987 build_and_jump (false_label_p));
1990 if (local_label)
1992 t = build1 (LABEL_EXPR, void_type_node, local_label);
1993 append_to_statement_list (t, &expr);
1996 return expr;
1999 static tree
2000 shortcut_cond_expr (tree expr)
2002 tree pred = TREE_OPERAND (expr, 0);
2003 tree then_ = TREE_OPERAND (expr, 1);
2004 tree else_ = TREE_OPERAND (expr, 2);
2005 tree true_label, false_label, end_label, t;
2006 tree *true_label_p;
2007 tree *false_label_p;
2008 bool emit_end, emit_false;
2009 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2010 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2012 /* First do simple transformations. */
2013 if (!else_se)
2015 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2016 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2018 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2019 then_ = shortcut_cond_expr (expr);
2020 pred = TREE_OPERAND (pred, 0);
2021 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2024 if (!then_se)
2026 /* If there is no 'then', turn
2027 if (a || b); else d
2028 into
2029 if (a); else if (b); else d. */
2030 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2032 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2033 else_ = shortcut_cond_expr (expr);
2034 pred = TREE_OPERAND (pred, 0);
2035 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2039 /* If we're done, great. */
2040 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2041 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2042 return expr;
2044 /* Otherwise we need to mess with gotos. Change
2045 if (a) c; else d;
2047 if (a); else goto no;
2048 c; goto end;
2049 no: d; end:
2050 and recursively gimplify the condition. */
2052 true_label = false_label = end_label = NULL_TREE;
2054 /* If our arms just jump somewhere, hijack those labels so we don't
2055 generate jumps to jumps. */
2057 if (then_
2058 && TREE_CODE (then_) == GOTO_EXPR
2059 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2061 true_label = GOTO_DESTINATION (then_);
2062 then_ = NULL;
2063 then_se = false;
2066 if (else_
2067 && TREE_CODE (else_) == GOTO_EXPR
2068 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2070 false_label = GOTO_DESTINATION (else_);
2071 else_ = NULL;
2072 else_se = false;
2075 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2076 if (true_label)
2077 true_label_p = &true_label;
2078 else
2079 true_label_p = NULL;
2081 /* The 'else' branch also needs a label if it contains interesting code. */
2082 if (false_label || else_se)
2083 false_label_p = &false_label;
2084 else
2085 false_label_p = NULL;
2087 /* If there was nothing else in our arms, just forward the label(s). */
2088 if (!then_se && !else_se)
2089 return shortcut_cond_r (pred, true_label_p, false_label_p);
2091 /* If our last subexpression already has a terminal label, reuse it. */
2092 if (else_se)
2093 expr = expr_last (else_);
2094 else if (then_se)
2095 expr = expr_last (then_);
2096 else
2097 expr = NULL;
2098 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2099 end_label = LABEL_EXPR_LABEL (expr);
2101 /* If we don't care about jumping to the 'else' branch, jump to the end
2102 if the condition is false. */
2103 if (!false_label_p)
2104 false_label_p = &end_label;
2106 /* We only want to emit these labels if we aren't hijacking them. */
2107 emit_end = (end_label == NULL_TREE);
2108 emit_false = (false_label == NULL_TREE);
2110 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2112 expr = NULL;
2113 append_to_statement_list (pred, &expr);
2115 append_to_statement_list (then_, &expr);
2116 if (else_se)
2118 t = build_and_jump (&end_label);
2119 append_to_statement_list (t, &expr);
2120 if (emit_false)
2122 t = build1 (LABEL_EXPR, void_type_node, false_label);
2123 append_to_statement_list (t, &expr);
2125 append_to_statement_list (else_, &expr);
2127 if (emit_end && end_label)
2129 t = build1 (LABEL_EXPR, void_type_node, end_label);
2130 append_to_statement_list (t, &expr);
2133 return expr;
2136 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2138 static tree
2139 gimple_boolify (tree expr)
2141 tree type = TREE_TYPE (expr);
2143 if (TREE_CODE (type) == BOOLEAN_TYPE)
2144 return expr;
2146 /* If this is the predicate of a COND_EXPR, it might not even be a
2147 truthvalue yet. */
2148 expr = lang_hooks.truthvalue_conversion (expr);
2150 switch (TREE_CODE (expr))
2152 case TRUTH_AND_EXPR:
2153 case TRUTH_OR_EXPR:
2154 case TRUTH_XOR_EXPR:
2155 case TRUTH_ANDIF_EXPR:
2156 case TRUTH_ORIF_EXPR:
2157 /* Also boolify the arguments of truth exprs. */
2158 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2159 /* FALLTHRU */
2161 case TRUTH_NOT_EXPR:
2162 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2163 /* FALLTHRU */
2165 case EQ_EXPR: case NE_EXPR:
2166 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2167 /* These expressions always produce boolean results. */
2168 TREE_TYPE (expr) = boolean_type_node;
2169 return expr;
2171 default:
2172 /* Other expressions that get here must have boolean values, but
2173 might need to be converted to the appropriate mode. */
2174 return convert (boolean_type_node, expr);
2178 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2179 into
2181 if (p) if (p)
2182 t1 = a; a;
2183 else or else
2184 t1 = b; b;
2187 The second form is used when *EXPR_P is of type void.
2189 TARGET is the tree for T1 above.
2191 PRE_P points to the list where side effects that must happen before
2192 *EXPR_P should be stored. */
2194 static enum gimplify_status
2195 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2197 tree expr = *expr_p;
2198 tree tmp, tmp2, type;
2199 enum gimplify_status ret;
2201 type = TREE_TYPE (expr);
2202 if (!type)
2203 TREE_TYPE (expr) = void_type_node;
2205 /* If this COND_EXPR has a value, copy the values into a temporary within
2206 the arms. */
2207 else if (! VOID_TYPE_P (type))
2209 if (target)
2211 ret = gimplify_expr (&target, pre_p, NULL,
2212 is_gimple_min_lval, fb_lvalue);
2213 if (ret != GS_ERROR)
2214 ret = GS_OK;
2215 tmp = target;
2216 tmp2 = unshare_expr (target);
2218 else
2220 tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2221 ret = GS_ALL_DONE;
2224 /* Build the then clause, 't1 = a;'. But don't build an assignment
2225 if this branch is void; in C++ it can be, if it's a throw. */
2226 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2227 TREE_OPERAND (expr, 1)
2228 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2230 /* Build the else clause, 't1 = b;'. */
2231 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2232 TREE_OPERAND (expr, 2)
2233 = build (MODIFY_EXPR, void_type_node, tmp2, TREE_OPERAND (expr, 2));
2235 TREE_TYPE (expr) = void_type_node;
2236 recalculate_side_effects (expr);
2238 /* Move the COND_EXPR to the prequeue. */
2239 gimplify_and_add (expr, pre_p);
2241 *expr_p = tmp;
2242 return ret;
2245 /* Make sure the condition has BOOLEAN_TYPE. */
2246 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2248 /* Break apart && and || conditions. */
2249 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2250 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2252 expr = shortcut_cond_expr (expr);
2254 if (expr != *expr_p)
2256 *expr_p = expr;
2258 /* We can't rely on gimplify_expr to re-gimplify the expanded
2259 form properly, as cleanups might cause the target labels to be
2260 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2261 set up a conditional context. */
2262 gimple_push_condition ();
2263 gimplify_stmt (expr_p);
2264 gimple_pop_condition (pre_p);
2266 return GS_ALL_DONE;
2270 /* Now do the normal gimplification. */
2271 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2272 is_gimple_condexpr, fb_rvalue);
2274 gimple_push_condition ();
2276 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2277 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2278 recalculate_side_effects (expr);
2280 gimple_pop_condition (pre_p);
2282 if (ret == GS_ERROR)
2284 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2285 ret = GS_ALL_DONE;
2286 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2287 /* Rewrite "if (a); else b" to "if (!a) b" */
2289 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2290 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2291 is_gimple_condexpr, fb_rvalue);
2293 tmp = TREE_OPERAND (expr, 1);
2294 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2295 TREE_OPERAND (expr, 2) = tmp;
2297 else
2298 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2299 expr = TREE_OPERAND (expr, 0);
2301 *expr_p = expr;
2302 return ret;
2305 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2306 a call to __builtin_memcpy. */
2308 static enum gimplify_status
2309 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2311 tree args, t, to, to_ptr, from;
2313 to = TREE_OPERAND (*expr_p, 0);
2314 from = TREE_OPERAND (*expr_p, 1);
2316 args = tree_cons (NULL, size, NULL);
2318 t = build_fold_addr_expr (from);
2319 args = tree_cons (NULL, t, args);
2321 to_ptr = build_fold_addr_expr (to);
2322 args = tree_cons (NULL, to_ptr, args);
2323 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2324 t = build_function_call_expr (t, args);
2326 if (want_value)
2328 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2329 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2332 *expr_p = t;
2333 return GS_OK;
2336 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2337 a call to __builtin_memset. In this case we know that the RHS is
2338 a CONSTRUCTOR with an empty element list. */
2340 static enum gimplify_status
2341 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2343 tree args, t, to, to_ptr;
2345 to = TREE_OPERAND (*expr_p, 0);
2347 args = tree_cons (NULL, size, NULL);
2349 args = tree_cons (NULL, integer_zero_node, args);
2351 to_ptr = build_fold_addr_expr (to);
2352 args = tree_cons (NULL, to_ptr, args);
2353 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2354 t = build_function_call_expr (t, args);
2356 if (want_value)
2358 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2359 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2362 *expr_p = t;
2363 return GS_OK;
2366 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2367 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2368 assignment. Returns non-null if we detect a potential overlap. */
2370 struct gimplify_init_ctor_preeval_data
2372 /* The base decl of the lhs object. May be NULL, in which case we
2373 have to assume the lhs is indirect. */
2374 tree lhs_base_decl;
2376 /* The alias set of the lhs object. */
2377 int lhs_alias_set;
2380 static tree
2381 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2383 struct gimplify_init_ctor_preeval_data *data
2384 = (struct gimplify_init_ctor_preeval_data *) xdata;
2385 tree t = *tp;
2387 /* If we find the base object, obviously we have overlap. */
2388 if (data->lhs_base_decl == t)
2389 return t;
2391 /* If the constructor component is indirect, determine if we have a
2392 potential overlap with the lhs. The only bits of information we
2393 have to go on at this point are addressability and alias sets. */
2394 if (TREE_CODE (t) == INDIRECT_REF
2395 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2396 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2397 return t;
2399 if (DECL_P (t) || TYPE_P (t))
2400 *walk_subtrees = 0;
2401 return NULL;
2404 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2405 force values that overlap with the lhs (as described by *DATA)
2406 into temporaries. */
2408 static void
2409 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2410 struct gimplify_init_ctor_preeval_data *data)
2412 enum gimplify_status one;
2414 /* If the value is invariant, then there's nothing to pre-evaluate.
2415 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2416 invariant but has side effects and might contain a reference to
2417 the object we're initializing. */
2418 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2419 return;
2421 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2422 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2423 return;
2425 /* Recurse for nested constructors. */
2426 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2428 tree list;
2429 for (list = CONSTRUCTOR_ELTS (*expr_p); list ; list = TREE_CHAIN (list))
2430 gimplify_init_ctor_preeval (&TREE_VALUE (list), pre_p, post_p, data);
2431 return;
2434 /* We can't preevaluate if the type contains a placeholder. */
2435 if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
2436 return;
2438 /* Gimplify the constructor element to something appropriate for the rhs
2439 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2440 the gimplifier will consider this a store to memory. Doing this
2441 gimplification now means that we won't have to deal with complicated
2442 language-specific trees, nor trees like SAVE_EXPR that can induce
2443 exponential search behaviour. */
2444 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2445 if (one == GS_ERROR)
2447 *expr_p = NULL;
2448 return;
2451 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2452 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2453 always be true for all scalars, since is_gimple_mem_rhs insists on a
2454 temporary variable for them. */
2455 if (DECL_P (*expr_p))
2456 return;
2458 /* If this is of variable size, we have no choice but to assume it doesn't
2459 overlap since we can't make a temporary for it. */
2460 if (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p))))
2461 return;
2463 /* Otherwise, we must search for overlap ... */
2464 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2465 return;
2467 /* ... and if found, force the value into a temporary. */
2468 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2471 /* A subroutine of gimplify_init_constructor. Generate individual
2472 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2473 assignments should happen. LIST is the CONSTRUCTOR_ELTS of the
2474 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2475 zeroed first. */
2477 static void
2478 gimplify_init_ctor_eval (tree object, tree list, tree *pre_p, bool cleared)
2480 tree array_elt_type = NULL;
2482 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2483 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2485 for (; list; list = TREE_CHAIN (list))
2487 tree purpose, value, cref, init;
2489 purpose = TREE_PURPOSE (list);
2490 value = TREE_VALUE (list);
2492 /* NULL values are created above for gimplification errors. */
2493 if (value == NULL)
2494 continue;
2496 if (cleared && initializer_zerop (value))
2497 continue;
2499 if (array_elt_type)
2501 /* ??? Here's to hoping the front end fills in all of the indicies,
2502 so we don't have to figure out what's missing ourselves. */
2503 if (!purpose)
2504 abort ();
2505 /* ??? Need to handle this. */
2506 if (TREE_CODE (purpose) == RANGE_EXPR)
2507 abort ();
2509 cref = build (ARRAY_REF, array_elt_type, unshare_expr (object),
2510 purpose, NULL_TREE, NULL_TREE);
2512 else
2513 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
2514 unshare_expr (object), purpose, NULL_TREE);
2516 if (TREE_CODE (value) == CONSTRUCTOR)
2517 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2518 pre_p, cleared);
2519 else
2521 init = build (MODIFY_EXPR, TREE_TYPE (cref), cref, value);
2522 gimplify_and_add (init, pre_p);
2527 /* A subroutine of gimplify_modify_expr. Break out elements of a
2528 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2530 Note that we still need to clear any elements that don't have explicit
2531 initializers, so if not all elements are initialized we keep the
2532 original MODIFY_EXPR, we just remove all of the constructor elements. */
2534 static enum gimplify_status
2535 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2536 tree *post_p, bool want_value)
2538 tree object;
2539 tree ctor = TREE_OPERAND (*expr_p, 1);
2540 tree type = TREE_TYPE (ctor);
2541 enum gimplify_status ret;
2542 tree elt_list;
2544 if (TREE_CODE (ctor) != CONSTRUCTOR)
2545 return GS_UNHANDLED;
2547 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
2548 is_gimple_lvalue, fb_lvalue);
2549 if (ret == GS_ERROR)
2550 return ret;
2551 object = TREE_OPERAND (*expr_p, 0);
2553 elt_list = CONSTRUCTOR_ELTS (ctor);
2555 ret = GS_ALL_DONE;
2556 switch (TREE_CODE (type))
2558 case RECORD_TYPE:
2559 case UNION_TYPE:
2560 case QUAL_UNION_TYPE:
2561 case ARRAY_TYPE:
2563 struct gimplify_init_ctor_preeval_data preeval_data;
2564 HOST_WIDE_INT num_elements, num_nonzero_elements;
2565 HOST_WIDE_INT num_nonconstant_elements;
2566 bool cleared;
2568 /* Aggregate types must lower constructors to initialization of
2569 individual elements. The exception is that a CONSTRUCTOR node
2570 with no elements indicates zero-initialization of the whole. */
2571 if (elt_list == NULL)
2572 break;
2574 categorize_ctor_elements (ctor, &num_nonzero_elements,
2575 &num_nonconstant_elements);
2577 /* If a const aggregate variable is being initialized, then it
2578 should never be a lose to promote the variable to be static. */
2579 if (num_nonconstant_elements == 0
2580 && TREE_READONLY (object)
2581 && TREE_CODE (object) == VAR_DECL)
2583 DECL_INITIAL (object) = ctor;
2584 TREE_STATIC (object) = 1;
2585 if (!DECL_NAME (object))
2586 DECL_NAME (object) = create_tmp_var_name ("C");
2587 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2589 /* ??? C++ doesn't automatically append a .<number> to the
2590 assembler name, and even when it does, it looks a FE private
2591 data structures to figure out what that number should be,
2592 which are not set for this variable. I suppose this is
2593 important for local statics for inline functions, which aren't
2594 "local" in the object file sense. So in order to get a unique
2595 TU-local symbol, we must invoke the lhd version now. */
2596 lhd_set_decl_assembler_name (object);
2598 *expr_p = NULL_TREE;
2599 break;
2602 /* If there are "lots" of initialized elements, and all of them
2603 are valid address constants, then the entire initializer can
2604 be dropped to memory, and then memcpy'd out. */
2605 if (num_nonconstant_elements == 0)
2607 HOST_WIDE_INT size = int_size_in_bytes (type);
2608 unsigned int align;
2610 /* ??? We can still get unbounded array types, at least
2611 from the C++ front end. This seems wrong, but attempt
2612 to work around it for now. */
2613 if (size < 0)
2615 size = int_size_in_bytes (TREE_TYPE (object));
2616 if (size >= 0)
2617 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2620 /* Find the maximum alignment we can assume for the object. */
2621 /* ??? Make use of DECL_OFFSET_ALIGN. */
2622 if (DECL_P (object))
2623 align = DECL_ALIGN (object);
2624 else
2625 align = TYPE_ALIGN (type);
2627 if (size > 0 && !can_move_by_pieces (size, align))
2629 tree new = create_tmp_var_raw (type, "C");
2631 gimple_add_tmp_var (new);
2632 TREE_STATIC (new) = 1;
2633 TREE_READONLY (new) = 1;
2634 DECL_INITIAL (new) = ctor;
2635 if (align > DECL_ALIGN (new))
2637 DECL_ALIGN (new) = align;
2638 DECL_USER_ALIGN (new) = 1;
2640 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2642 TREE_OPERAND (*expr_p, 1) = new;
2644 /* This is no longer an assignment of a CONSTRUCTOR, but
2645 we still may have processing to do on the LHS. So
2646 pretend we didn't do anything here to let that happen. */
2647 return GS_UNHANDLED;
2651 /* If there are "lots" of initialized elements, even discounting
2652 those that are not address constants (and thus *must* be
2653 computed at runtime), then partition the constructor into
2654 constant and non-constant parts. Block copy the constant
2655 parts in, then generate code for the non-constant parts. */
2656 /* TODO. There's code in cp/typeck.c to do this. */
2658 num_elements = count_type_elements (TREE_TYPE (ctor));
2660 /* If there are "lots" of zeros, then block clear the object first. */
2661 cleared = false;
2662 if (num_elements - num_nonzero_elements > CLEAR_RATIO
2663 && num_nonzero_elements < num_elements/4)
2664 cleared = true;
2666 /* ??? This bit ought not be needed. For any element not present
2667 in the initializer, we should simply set them to zero. Except
2668 we'd need to *find* the elements that are not present, and that
2669 requires trickery to avoid quadratic compile-time behavior in
2670 large cases or excessive memory use in small cases. */
2671 else
2673 HOST_WIDE_INT len = list_length (elt_list);
2674 if (TREE_CODE (type) == ARRAY_TYPE)
2676 tree nelts = array_type_nelts (type);
2677 if (!host_integerp (nelts, 1)
2678 || tree_low_cst (nelts, 1) + 1 != len)
2679 cleared = true;
2681 else if (len != fields_length (type))
2682 cleared = true;
2685 if (cleared)
2687 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2688 Note that we still have to gimplify, in order to handle the
2689 case of variable sized types. Avoid shared tree structures. */
2690 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
2691 object = unshare_expr (object);
2692 gimplify_stmt (expr_p);
2693 append_to_statement_list (*expr_p, pre_p);
2696 preeval_data.lhs_base_decl = get_base_address (object);
2697 if (!DECL_P (preeval_data.lhs_base_decl))
2698 preeval_data.lhs_base_decl = NULL;
2699 preeval_data.lhs_alias_set = get_alias_set (object);
2701 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
2702 pre_p, post_p, &preeval_data);
2703 gimplify_init_ctor_eval (object, elt_list, pre_p, cleared);
2705 *expr_p = NULL_TREE;
2707 break;
2709 case COMPLEX_TYPE:
2711 tree r, i;
2713 /* Extract the real and imaginary parts out of the ctor. */
2714 r = i = NULL_TREE;
2715 if (elt_list)
2717 r = TREE_VALUE (elt_list);
2718 elt_list = TREE_CHAIN (elt_list);
2719 if (elt_list)
2721 i = TREE_VALUE (elt_list);
2722 if (TREE_CHAIN (elt_list))
2723 abort ();
2726 if (r == NULL || i == NULL)
2728 tree zero = convert (TREE_TYPE (type), integer_zero_node);
2729 if (r == NULL)
2730 r = zero;
2731 if (i == NULL)
2732 i = zero;
2735 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
2736 represent creation of a complex value. */
2737 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
2739 ctor = build_complex (type, r, i);
2740 TREE_OPERAND (*expr_p, 1) = ctor;
2742 else
2744 ctor = build (COMPLEX_EXPR, type, r, i);
2745 TREE_OPERAND (*expr_p, 1) = ctor;
2746 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
2747 is_gimple_tmp_rhs, fb_rvalue);
2750 break;
2752 case VECTOR_TYPE:
2753 /* Go ahead and simplify constant constructors to VECTOR_CST. */
2754 if (TREE_CONSTANT (ctor))
2755 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
2756 else
2758 /* Vector types use CONSTRUCTOR all the way through gimple
2759 compilation as a general initializer. */
2760 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
2762 enum gimplify_status tret;
2763 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
2764 is_gimple_constructor_elt, fb_rvalue);
2765 if (tret == GS_ERROR)
2766 ret = GS_ERROR;
2769 break;
2771 default:
2772 /* So how did we get a CONSTRUCTOR for a scalar type? */
2773 abort ();
2776 if (ret == GS_ERROR)
2777 return GS_ERROR;
2778 else if (want_value)
2780 append_to_statement_list (*expr_p, pre_p);
2781 *expr_p = object;
2782 return GS_OK;
2784 else
2785 return GS_ALL_DONE;
2788 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
2789 based on the code of the RHS. We loop for as long as something changes. */
2791 static enum gimplify_status
2792 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2793 tree *post_p, bool want_value)
2795 enum gimplify_status ret = GS_OK;
2797 while (ret != GS_UNHANDLED)
2798 switch (TREE_CODE (*from_p))
2800 case TARGET_EXPR:
2802 /* If we are initializing something from a TARGET_EXPR, strip the
2803 TARGET_EXPR and initialize it directly, if possible. This can't
2804 be done if the initializer is void, since that implies that the
2805 temporary is set in some non-trivial way.
2807 ??? What about code that pulls out the temp and uses it
2808 elsewhere? I think that such code never uses the TARGET_EXPR as
2809 an initializer. If I'm wrong, we'll abort because the temp won't
2810 have any RTL. In that case, I guess we'll need to replace
2811 references somehow. */
2812 tree init = TARGET_EXPR_INITIAL (*from_p);
2814 if (!VOID_TYPE_P (TREE_TYPE (init)))
2816 *from_p = init;
2817 ret = GS_OK;
2819 else
2820 ret = GS_UNHANDLED;
2822 break;
2824 case COMPOUND_EXPR:
2825 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2826 caught. */
2827 gimplify_compound_expr (from_p, pre_p, true);
2828 ret = GS_OK;
2829 break;
2831 case CONSTRUCTOR:
2832 /* If we're initializing from a CONSTRUCTOR, break this into
2833 individual MODIFY_EXPRs. */
2834 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2836 case COND_EXPR:
2837 /* If we're assigning to a non-register type, push the assignment
2838 down into the branches. This is mandatory for ADDRESSABLE types,
2839 since we cannot generate temporaries for such, but it saves a
2840 copy in other cases as well. */
2841 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
2843 *expr_p = *from_p;
2844 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2846 else
2847 ret = GS_UNHANDLED;
2848 break;
2850 default:
2851 ret = GS_UNHANDLED;
2852 break;
2855 return ret;
2858 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2860 modify_expr
2861 : varname '=' rhs
2862 | '*' ID '=' rhs
2864 PRE_P points to the list where side effects that must happen before
2865 *EXPR_P should be stored.
2867 POST_P points to the list where side effects that must happen after
2868 *EXPR_P should be stored.
2870 WANT_VALUE is nonzero iff we want to use the value of this expression
2871 in another expression. */
2873 static enum gimplify_status
2874 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2876 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2877 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2878 enum gimplify_status ret = GS_UNHANDLED;
2880 #if defined ENABLE_CHECKING
2881 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2882 abort ();
2883 #endif
2885 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2886 if (TREE_CODE (*expr_p) == INIT_EXPR)
2887 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2889 /* See if any simplifications can be done based on what the RHS is. */
2890 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2891 want_value);
2892 if (ret != GS_UNHANDLED)
2893 return ret;
2895 /* If the value being copied is of variable width, compute the length
2896 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
2897 before gimplifying any of the operands so that we can resolve any
2898 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
2899 the size of the expression to be copied, not of the destination, so
2900 that is what we must here. */
2901 maybe_with_size_expr (from_p);
2903 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2904 if (ret == GS_ERROR)
2905 return ret;
2907 ret = gimplify_expr (from_p, pre_p, post_p,
2908 rhs_predicate_for (*to_p), fb_rvalue);
2909 if (ret == GS_ERROR)
2910 return ret;
2912 /* Now see if the above changed *from_p to something we handle specially. */
2913 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2914 want_value);
2915 if (ret != GS_UNHANDLED)
2916 return ret;
2918 /* If we've got a variable sized assignment between two lvalues (i.e. does
2919 not involve a call), then we can make things a bit more straightforward
2920 by converting the assignment to memcpy or memset. */
2921 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
2923 tree from = TREE_OPERAND (*from_p, 0);
2924 tree size = TREE_OPERAND (*from_p, 1);
2926 if (TREE_CODE (from) == CONSTRUCTOR)
2927 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
2928 if (is_gimple_addressable (from))
2930 *from_p = from;
2931 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
2935 /* If the destination is already simple, nothing else needed. */
2936 if (is_gimple_tmp_var (*to_p) || !want_value)
2937 ret = GS_ALL_DONE;
2938 else
2939 ret = GS_OK;
2941 if (want_value)
2943 append_to_statement_list (*expr_p, pre_p);
2944 *expr_p = *to_p;
2947 return ret;
2950 /* Gimplify a comparison between two variable-sized objects. Do this
2951 with a call to BUILT_IN_MEMCMP. */
2953 static enum gimplify_status
2954 gimplify_variable_sized_compare (tree *expr_p)
2956 tree op0 = TREE_OPERAND (*expr_p, 0);
2957 tree op1 = TREE_OPERAND (*expr_p, 1);
2958 tree args, t, dest;
2960 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2961 t = unshare_expr (t);
2962 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2963 args = tree_cons (NULL, t, NULL);
2964 t = build_fold_addr_expr (op1);
2965 args = tree_cons (NULL, t, args);
2966 dest = build_fold_addr_expr (op0);
2967 args = tree_cons (NULL, dest, args);
2968 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2969 t = build_function_call_expr (t, args);
2970 *expr_p
2971 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2973 return GS_OK;
2976 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2977 points to the expression to gimplify.
2979 Expressions of the form 'a && b' are gimplified to:
2981 a && b ? true : false
2983 gimplify_cond_expr will do the rest.
2985 PRE_P points to the list where side effects that must happen before
2986 *EXPR_P should be stored. */
2988 static enum gimplify_status
2989 gimplify_boolean_expr (tree *expr_p)
2991 /* Preserve the original type of the expression. */
2992 tree type = TREE_TYPE (*expr_p);
2994 *expr_p = build (COND_EXPR, type, *expr_p,
2995 convert (type, boolean_true_node),
2996 convert (type, boolean_false_node));
2998 return GS_OK;
3001 /* Gimplifies an expression sequence. This function gimplifies each
3002 expression and re-writes the original expression with the last
3003 expression of the sequence in GIMPLE form.
3005 PRE_P points to the list where the side effects for all the
3006 expressions in the sequence will be emitted.
3008 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
3009 /* ??? Should rearrange to share the pre-queue with all the indirect
3010 invocations of gimplify_expr. Would probably save on creations
3011 of statement_list nodes. */
3013 static enum gimplify_status
3014 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
3016 tree t = *expr_p;
3020 tree *sub_p = &TREE_OPERAND (t, 0);
3022 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3023 gimplify_compound_expr (sub_p, pre_p, false);
3024 else
3025 gimplify_stmt (sub_p);
3026 append_to_statement_list (*sub_p, pre_p);
3028 t = TREE_OPERAND (t, 1);
3030 while (TREE_CODE (t) == COMPOUND_EXPR);
3032 *expr_p = t;
3033 if (want_value)
3034 return GS_OK;
3035 else
3037 gimplify_stmt (expr_p);
3038 return GS_ALL_DONE;
3042 /* Gimplifies a statement list. These may be created either by an
3043 enlightened front-end, or by shortcut_cond_expr. */
3045 static enum gimplify_status
3046 gimplify_statement_list (tree *expr_p)
3048 tree_stmt_iterator i = tsi_start (*expr_p);
3050 while (!tsi_end_p (i))
3052 tree t;
3054 gimplify_stmt (tsi_stmt_ptr (i));
3056 t = tsi_stmt (i);
3057 if (t == NULL)
3058 tsi_delink (&i);
3059 else if (TREE_CODE (t) == STATEMENT_LIST)
3061 tsi_link_before (&i, t, TSI_SAME_STMT);
3062 tsi_delink (&i);
3064 else
3065 tsi_next (&i);
3068 return GS_ALL_DONE;
3071 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
3072 gimplify. After gimplification, EXPR_P will point to a new temporary
3073 that holds the original value of the SAVE_EXPR node.
3075 PRE_P points to the list where side effects that must happen before
3076 *EXPR_P should be stored. */
3078 static enum gimplify_status
3079 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3081 enum gimplify_status ret = GS_ALL_DONE;
3082 tree val;
3084 #if defined ENABLE_CHECKING
3085 if (TREE_CODE (*expr_p) != SAVE_EXPR)
3086 abort ();
3087 #endif
3089 val = TREE_OPERAND (*expr_p, 0);
3091 /* If the operand is already a GIMPLE temporary, just re-write the
3092 SAVE_EXPR node. */
3093 if (is_gimple_tmp_var (val))
3094 *expr_p = val;
3095 /* The operand may be a void-valued expression such as SAVE_EXPRs
3096 generated by the Java frontend for class initialization. It is
3097 being executed only for its side-effects. */
3098 else if (TREE_TYPE (val) == void_type_node)
3100 tree body = TREE_OPERAND (*expr_p, 0);
3101 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
3102 append_to_statement_list (body, pre_p);
3103 *expr_p = NULL;
3105 else
3106 *expr_p = TREE_OPERAND (*expr_p, 0)
3107 = get_initialized_tmp_var (val, pre_p, post_p);
3109 return ret;
3112 /* Re-write the ADDR_EXPR node pointed by EXPR_P
3114 unary_expr
3115 : ...
3116 | '&' varname
3119 PRE_P points to the list where side effects that must happen before
3120 *EXPR_P should be stored.
3122 POST_P points to the list where side effects that must happen after
3123 *EXPR_P should be stored. */
3125 static enum gimplify_status
3126 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3128 tree expr = *expr_p;
3129 tree op0 = TREE_OPERAND (expr, 0);
3130 enum gimplify_status ret;
3132 switch (TREE_CODE (op0))
3134 case INDIRECT_REF:
3135 /* Check if we are dealing with an expression of the form '&*ptr'.
3136 While the front end folds away '&*ptr' into 'ptr', these
3137 expressions may be generated internally by the compiler (e.g.,
3138 builtins like __builtin_va_end). */
3139 *expr_p = TREE_OPERAND (op0, 0);
3140 ret = GS_OK;
3141 break;
3143 case ARRAY_REF:
3144 /* Fold &a[6] to (&a + 6). */
3145 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
3146 pre_p, post_p);
3148 /* This added an INDIRECT_REF. Fold it away. */
3149 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3150 break;
3152 case VIEW_CONVERT_EXPR:
3153 /* Take the address of our operand and then convert it to the type of
3154 this ADDR_EXPR.
3156 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3157 all clear. The impact of this transformation is even less clear. */
3158 *expr_p = fold_convert (TREE_TYPE (expr),
3159 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3160 ret = GS_OK;
3161 break;
3163 default:
3164 /* We use fb_either here because the C frontend sometimes takes
3165 the address of a call that returns a struct; see
3166 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3167 the implied temporary explicit. */
3168 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3169 is_gimple_addressable, fb_either);
3170 if (ret != GS_ERROR)
3172 /* The above may have made an INDIRECT_REF (e.g, Ada's NULL_EXPR),
3173 so check for it here. It's not worth checking for the other
3174 cases above. */
3175 if (TREE_CODE (TREE_OPERAND (expr, 0)) == INDIRECT_REF)
3177 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3178 break;
3181 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3182 is set properly. */
3183 recompute_tree_invarant_for_addr_expr (expr);
3185 /* Mark the RHS addressable. */
3186 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3188 break;
3191 return ret;
3194 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3195 value; output operands should be a gimple lvalue. */
3197 static enum gimplify_status
3198 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3200 tree expr = *expr_p;
3201 int noutputs = list_length (ASM_OUTPUTS (expr));
3202 const char **oconstraints
3203 = (const char **) alloca ((noutputs) * sizeof (const char *));
3204 int i;
3205 tree link;
3206 const char *constraint;
3207 bool allows_mem, allows_reg, is_inout;
3208 enum gimplify_status ret, tret;
3210 ASM_STRING (expr)
3211 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
3212 ASM_INPUTS (expr));
3214 ret = GS_ALL_DONE;
3215 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3217 oconstraints[i] = constraint
3218 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3220 parse_output_constraint (&constraint, i, 0, 0,
3221 &allows_mem, &allows_reg, &is_inout);
3223 if (!allows_reg && allows_mem)
3224 lang_hooks.mark_addressable (TREE_VALUE (link));
3226 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3227 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3228 fb_lvalue | fb_mayfail);
3229 if (tret == GS_ERROR)
3231 error ("invalid lvalue in asm output %d", i);
3232 ret = tret;
3235 if (is_inout)
3237 /* An input/output operand. To give the optimizers more
3238 flexibility, split it into separate input and output
3239 operands. */
3240 tree input;
3241 char buf[10];
3242 size_t constraint_len = strlen (constraint);
3244 /* Turn the in/out constraint into an output constraint. */
3245 char *p = xstrdup (constraint);
3246 p[0] = '=';
3247 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3248 free (p);
3250 /* And add a matching input constraint. */
3251 if (allows_reg)
3253 sprintf (buf, "%d", i);
3254 input = build_string (strlen (buf), buf);
3256 else
3257 input = build_string (constraint_len - 1, constraint + 1);
3258 input = build_tree_list (build_tree_list (NULL_TREE, input),
3259 unshare_expr (TREE_VALUE (link)));
3260 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3264 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3266 constraint
3267 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3268 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3269 oconstraints, &allows_mem, &allows_reg);
3271 /* If the operand is a memory input, it should be an lvalue. */
3272 if (!allows_reg && allows_mem)
3274 lang_hooks.mark_addressable (TREE_VALUE (link));
3275 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3276 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3277 if (tret == GS_ERROR)
3279 error ("memory input %d is not directly addressable", i);
3280 ret = tret;
3283 else
3285 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3286 is_gimple_val, fb_rvalue);
3287 if (tret == GS_ERROR)
3288 ret = tret;
3292 return ret;
3295 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3296 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3297 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3298 return to this function.
3300 FIXME should we complexify the prequeue handling instead? Or use flags
3301 for all the cleanups and let the optimizer tighten them up? The current
3302 code seems pretty fragile; it will break on a cleanup within any
3303 non-conditional nesting. But any such nesting would be broken, anyway;
3304 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3305 and continues out of it. We can do that at the RTL level, though, so
3306 having an optimizer to tighten up try/finally regions would be a Good
3307 Thing. */
3309 static enum gimplify_status
3310 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3312 tree_stmt_iterator iter;
3313 tree body;
3315 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3317 /* We only care about the number of conditions between the innermost
3318 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3319 int old_conds = gimplify_ctxp->conditions;
3320 gimplify_ctxp->conditions = 0;
3322 body = TREE_OPERAND (*expr_p, 0);
3323 gimplify_to_stmt_list (&body);
3325 gimplify_ctxp->conditions = old_conds;
3327 for (iter = tsi_start (body); !tsi_end_p (iter); )
3329 tree *wce_p = tsi_stmt_ptr (iter);
3330 tree wce = *wce_p;
3332 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3334 if (tsi_one_before_end_p (iter))
3336 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
3337 tsi_delink (&iter);
3338 break;
3340 else
3342 tree sl, tfe;
3344 sl = tsi_split_statement_list_after (&iter);
3345 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
3346 append_to_statement_list (TREE_OPERAND (wce, 0),
3347 &TREE_OPERAND (tfe, 1));
3348 *wce_p = tfe;
3349 iter = tsi_start (sl);
3352 else
3353 tsi_next (&iter);
3356 if (temp)
3358 *expr_p = temp;
3359 append_to_statement_list (body, pre_p);
3360 return GS_OK;
3362 else
3364 *expr_p = body;
3365 return GS_ALL_DONE;
3369 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3370 is the cleanup action required. */
3372 static void
3373 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
3375 tree wce;
3377 /* Errors can result in improperly nested cleanups. Which results in
3378 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3379 if (errorcount || sorrycount)
3380 return;
3382 if (gimple_conditional_context ())
3384 /* If we're in a conditional context, this is more complex. We only
3385 want to run the cleanup if we actually ran the initialization that
3386 necessitates it, but we want to run it after the end of the
3387 conditional context. So we wrap the try/finally around the
3388 condition and use a flag to determine whether or not to actually
3389 run the destructor. Thus
3391 test ? f(A()) : 0
3393 becomes (approximately)
3395 flag = 0;
3396 try {
3397 if (test) { A::A(temp); flag = 1; val = f(temp); }
3398 else { val = 0; }
3399 } finally {
3400 if (flag) A::~A(temp);
3405 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3406 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3407 boolean_false_node);
3408 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3409 boolean_true_node);
3410 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3411 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3412 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3413 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3414 append_to_statement_list (ftrue, pre_p);
3416 /* Because of this manipulation, and the EH edges that jump
3417 threading cannot redirect, the temporary (VAR) will appear
3418 to be used uninitialized. Don't warn. */
3419 TREE_NO_WARNING (var) = 1;
3421 else
3423 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3424 append_to_statement_list (wce, pre_p);
3427 gimplify_stmt (&TREE_OPERAND (wce, 0));
3430 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3432 static enum gimplify_status
3433 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3435 tree targ = *expr_p;
3436 tree temp = TARGET_EXPR_SLOT (targ);
3437 tree init = TARGET_EXPR_INITIAL (targ);
3438 enum gimplify_status ret;
3440 if (init)
3442 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3443 to the temps list. */
3444 gimple_add_tmp_var (temp);
3446 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3447 expression is supposed to initialize the slot. */
3448 if (VOID_TYPE_P (TREE_TYPE (init)))
3449 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3450 else
3452 /* Special handling for BIND_EXPR can result in fewer temps. */
3453 ret = GS_OK;
3454 if (TREE_CODE (init) == BIND_EXPR)
3455 gimplify_bind_expr (&init, temp, pre_p);
3456 if (init != temp)
3458 init = build (MODIFY_EXPR, void_type_node, temp, init);
3459 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3460 fb_none);
3463 if (ret == GS_ERROR)
3464 return GS_ERROR;
3465 append_to_statement_list (init, pre_p);
3467 /* If needed, push the cleanup for the temp. */
3468 if (TARGET_EXPR_CLEANUP (targ))
3470 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3471 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3474 /* Only expand this once. */
3475 TREE_OPERAND (targ, 3) = init;
3476 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3478 else if (!DECL_SEEN_IN_BIND_EXPR_P (temp))
3479 /* We should have expanded this before. */
3480 abort ();
3482 *expr_p = temp;
3483 return GS_OK;
3486 /* Gimplification of expression trees. */
3488 /* Gimplify an expression which appears at statement context; usually, this
3489 means replacing it with a suitably gimple STATEMENT_LIST. */
3491 void
3492 gimplify_stmt (tree *stmt_p)
3494 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3497 /* Similarly, but force the result to be a STATEMENT_LIST. */
3499 void
3500 gimplify_to_stmt_list (tree *stmt_p)
3502 gimplify_stmt (stmt_p);
3503 if (!*stmt_p)
3504 *stmt_p = alloc_stmt_list ();
3505 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3507 tree t = *stmt_p;
3508 *stmt_p = alloc_stmt_list ();
3509 append_to_statement_list (t, stmt_p);
3514 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3515 gimplification failed.
3517 PRE_P points to the list where side effects that must happen before
3518 EXPR should be stored.
3520 POST_P points to the list where side effects that must happen after
3521 EXPR should be stored, or NULL if there is no suitable list. In
3522 that case, we copy the result to a temporary, emit the
3523 post-effects, and then return the temporary.
3525 GIMPLE_TEST_F points to a function that takes a tree T and
3526 returns nonzero if T is in the GIMPLE form requested by the
3527 caller. The GIMPLE predicates are in tree-gimple.c.
3529 This test is used twice. Before gimplification, the test is
3530 invoked to determine whether *EXPR_P is already gimple enough. If
3531 that fails, *EXPR_P is gimplified according to its code and
3532 GIMPLE_TEST_F is called again. If the test still fails, then a new
3533 temporary variable is created and assigned the value of the
3534 gimplified expression.
3536 FALLBACK tells the function what sort of a temporary we want. If the 1
3537 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3538 If both are set, either is OK, but an lvalue is preferable.
3540 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3541 iterates until solution. */
3543 enum gimplify_status
3544 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3545 bool (* gimple_test_f) (tree), fallback_t fallback)
3547 tree tmp;
3548 tree internal_pre = NULL_TREE;
3549 tree internal_post = NULL_TREE;
3550 tree save_expr;
3551 int is_statement = (pre_p == NULL);
3552 location_t saved_location;
3553 enum gimplify_status ret;
3555 save_expr = *expr_p;
3556 if (save_expr == NULL_TREE)
3557 return GS_ALL_DONE;
3559 /* We used to check the predicate here and return immediately if it
3560 succeeds. This is wrong; the design is for gimplification to be
3561 idempotent, and for the predicates to only test for valid forms, not
3562 whether they are fully simplified. */
3564 /* Set up our internal queues if needed. */
3565 if (pre_p == NULL)
3566 pre_p = &internal_pre;
3567 if (post_p == NULL)
3568 post_p = &internal_post;
3570 saved_location = input_location;
3571 if (save_expr != error_mark_node
3572 && EXPR_HAS_LOCATION (*expr_p))
3573 input_location = EXPR_LOCATION (*expr_p);
3575 /* Loop over the specific gimplifiers until the toplevel node
3576 remains the same. */
3579 /* Strip away as many useless type conversions as possible
3580 at the toplevel. */
3581 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
3583 /* Remember the expr. */
3584 save_expr = *expr_p;
3586 /* Die, die, die, my darling. */
3587 if (save_expr == error_mark_node
3588 || (TREE_TYPE (save_expr)
3589 && TREE_TYPE (save_expr) == error_mark_node))
3591 ret = GS_ERROR;
3592 break;
3595 /* Do any language-specific gimplification. */
3596 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3597 if (ret == GS_OK)
3599 if (*expr_p == NULL_TREE)
3600 break;
3601 if (*expr_p != save_expr)
3602 continue;
3604 else if (ret != GS_UNHANDLED)
3605 break;
3607 ret = GS_OK;
3608 switch (TREE_CODE (*expr_p))
3610 /* First deal with the special cases. */
3612 case POSTINCREMENT_EXPR:
3613 case POSTDECREMENT_EXPR:
3614 case PREINCREMENT_EXPR:
3615 case PREDECREMENT_EXPR:
3616 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3617 fallback != fb_none);
3618 break;
3620 case ARRAY_REF:
3621 case ARRAY_RANGE_REF:
3622 case REALPART_EXPR:
3623 case IMAGPART_EXPR:
3624 case COMPONENT_REF:
3625 case VIEW_CONVERT_EXPR:
3626 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3627 fallback ? fallback : fb_rvalue);
3628 break;
3630 case COND_EXPR:
3631 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3632 break;
3634 case CALL_EXPR:
3635 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
3636 break;
3638 case TREE_LIST:
3639 abort ();
3641 case COMPOUND_EXPR:
3642 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3643 break;
3645 case MODIFY_EXPR:
3646 case INIT_EXPR:
3647 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3648 fallback != fb_none);
3649 break;
3651 case TRUTH_ANDIF_EXPR:
3652 case TRUTH_ORIF_EXPR:
3653 ret = gimplify_boolean_expr (expr_p);
3654 break;
3656 case TRUTH_NOT_EXPR:
3657 TREE_OPERAND (*expr_p, 0)
3658 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3659 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3660 is_gimple_val, fb_rvalue);
3661 recalculate_side_effects (*expr_p);
3662 break;
3664 case ADDR_EXPR:
3665 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3666 break;
3668 case VA_ARG_EXPR:
3669 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3670 break;
3672 case CONVERT_EXPR:
3673 case NOP_EXPR:
3674 if (IS_EMPTY_STMT (*expr_p))
3676 ret = GS_ALL_DONE;
3677 break;
3680 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3681 || fallback == fb_none)
3683 /* Just strip a conversion to void (or in void context) and
3684 try again. */
3685 *expr_p = TREE_OPERAND (*expr_p, 0);
3686 break;
3689 ret = gimplify_conversion (expr_p);
3690 if (ret == GS_ERROR)
3691 break;
3692 if (*expr_p != save_expr)
3693 break;
3694 /* FALLTHRU */
3696 case FIX_TRUNC_EXPR:
3697 case FIX_CEIL_EXPR:
3698 case FIX_FLOOR_EXPR:
3699 case FIX_ROUND_EXPR:
3700 /* unary_expr: ... | '(' cast ')' val | ... */
3701 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3702 is_gimple_val, fb_rvalue);
3703 recalculate_side_effects (*expr_p);
3704 break;
3706 case INDIRECT_REF:
3707 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3708 is_gimple_reg, fb_rvalue);
3709 recalculate_side_effects (*expr_p);
3710 break;
3712 /* Constants need not be gimplified. */
3713 case INTEGER_CST:
3714 case REAL_CST:
3715 case STRING_CST:
3716 case COMPLEX_CST:
3717 case VECTOR_CST:
3718 ret = GS_ALL_DONE;
3719 break;
3721 case CONST_DECL:
3722 /* If we require an lvalue, such as for ADDR_EXPR, retain the
3723 CONST_DECL node. Otherwise the decl is replacable by its
3724 value. */
3725 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
3726 if (fallback & fb_lvalue)
3727 ret = GS_ALL_DONE;
3728 else
3729 *expr_p = DECL_INITIAL (*expr_p);
3730 break;
3732 case DECL_EXPR:
3733 ret = gimplify_decl_expr (expr_p);
3734 break;
3736 case EXC_PTR_EXPR:
3737 /* FIXME make this a decl. */
3738 ret = GS_ALL_DONE;
3739 break;
3741 case BIND_EXPR:
3742 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3743 break;
3745 case LOOP_EXPR:
3746 ret = gimplify_loop_expr (expr_p, pre_p);
3747 break;
3749 case SWITCH_EXPR:
3750 ret = gimplify_switch_expr (expr_p, pre_p);
3751 break;
3753 case LABELED_BLOCK_EXPR:
3754 ret = gimplify_labeled_block_expr (expr_p);
3755 break;
3757 case EXIT_BLOCK_EXPR:
3758 ret = gimplify_exit_block_expr (expr_p);
3759 break;
3761 case EXIT_EXPR:
3762 ret = gimplify_exit_expr (expr_p);
3763 break;
3765 case GOTO_EXPR:
3766 /* If the target is not LABEL, then it is a computed jump
3767 and the target needs to be gimplified. */
3768 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3769 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3770 NULL, is_gimple_val, fb_rvalue);
3771 break;
3773 case LABEL_EXPR:
3774 ret = GS_ALL_DONE;
3775 #ifdef ENABLE_CHECKING
3776 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3777 abort ();
3778 #endif
3779 break;
3781 case CASE_LABEL_EXPR:
3782 ret = gimplify_case_label_expr (expr_p);
3783 break;
3785 case RETURN_EXPR:
3786 ret = gimplify_return_expr (*expr_p, pre_p);
3787 break;
3789 case CONSTRUCTOR:
3790 /* Don't reduce this in place; let gimplify_init_constructor work its
3791 magic. Buf if we're just elaborating this for side effects, just
3792 gimplify any element that has side-effects. */
3793 if (fallback == fb_none)
3795 for (tmp = CONSTRUCTOR_ELTS (*expr_p); tmp;
3796 tmp = TREE_CHAIN (tmp))
3797 if (TREE_SIDE_EFFECTS (TREE_VALUE (tmp)))
3798 gimplify_expr (&TREE_VALUE (tmp), pre_p, post_p,
3799 gimple_test_f, fallback);
3801 *expr_p = NULL_TREE;
3804 ret = GS_ALL_DONE;
3805 break;
3807 /* The following are special cases that are not handled by the
3808 original GIMPLE grammar. */
3810 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3811 eliminated. */
3812 case SAVE_EXPR:
3813 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3814 break;
3816 case BIT_FIELD_REF:
3818 enum gimplify_status r0, r1, r2;
3820 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3821 is_gimple_lvalue, fb_either);
3822 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3823 is_gimple_val, fb_rvalue);
3824 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3825 is_gimple_val, fb_rvalue);
3826 recalculate_side_effects (*expr_p);
3828 ret = MIN (r0, MIN (r1, r2));
3830 break;
3832 case NON_LVALUE_EXPR:
3833 /* This should have been stripped above. */
3834 abort ();
3835 break;
3837 case ASM_EXPR:
3838 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3839 break;
3841 case TRY_FINALLY_EXPR:
3842 case TRY_CATCH_EXPR:
3843 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3844 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3845 ret = GS_ALL_DONE;
3846 break;
3848 case CLEANUP_POINT_EXPR:
3849 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3850 break;
3852 case TARGET_EXPR:
3853 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3854 break;
3856 case CATCH_EXPR:
3857 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3858 ret = GS_ALL_DONE;
3859 break;
3861 case EH_FILTER_EXPR:
3862 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3863 ret = GS_ALL_DONE;
3864 break;
3866 case OBJ_TYPE_REF:
3868 enum gimplify_status r0, r1;
3869 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
3870 is_gimple_val, fb_rvalue);
3871 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
3872 is_gimple_val, fb_rvalue);
3873 ret = MIN (r0, r1);
3875 break;
3877 case MIN_EXPR:
3878 case MAX_EXPR:
3879 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3880 break;
3882 case LABEL_DECL:
3883 /* We get here when taking the address of a label. We mark
3884 the label as "forced"; meaning it can never be removed and
3885 it is a potential target for any computed goto. */
3886 FORCED_LABEL (*expr_p) = 1;
3887 ret = GS_ALL_DONE;
3888 break;
3890 case STATEMENT_LIST:
3891 ret = gimplify_statement_list (expr_p);
3892 break;
3894 case WITH_SIZE_EXPR:
3896 enum gimplify_status r0, r1;
3897 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3898 post_p == &internal_post ? NULL : post_p,
3899 gimple_test_f, fallback);
3900 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3901 is_gimple_val, fb_rvalue);
3903 break;
3905 case VAR_DECL:
3906 /* ??? If this is a local variable, and it has not been seen in any
3907 outer BIND_EXPR, then it's probably the result of a duplicate
3908 declaration, for which we've already issued an error. It would
3909 be really nice if the front end wouldn't leak these at all.
3910 Currently the only known culprit is C++ destructors, as seen
3911 in g++.old-deja/g++.jason/binding.C. */
3912 tmp = *expr_p;
3913 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3914 && decl_function_context (tmp) == current_function_decl
3915 && !DECL_SEEN_IN_BIND_EXPR_P (tmp))
3917 #ifdef ENABLE_CHECKING
3918 if (!errorcount && !sorrycount)
3919 abort ();
3920 #endif
3921 ret = GS_ERROR;
3922 break;
3925 /* If this is a local variable sized decl, it must be accessed
3926 indirectly. Perform that substitution. */
3927 if (DECL_VALUE_EXPR (tmp))
3929 *expr_p = unshare_expr (DECL_VALUE_EXPR (tmp));
3930 ret = GS_OK;
3931 break;
3934 ret = GS_ALL_DONE;
3935 break;
3937 case SSA_NAME:
3938 /* Allow callbacks into the gimplifier during optimization. */
3939 ret = GS_ALL_DONE;
3940 break;
3942 default:
3943 /* If this is a comparison of objects of aggregate type, handle
3944 it specially (by converting to a call to memcmp). It would be
3945 nice to only have to do this for variable-sized objects, but
3946 then we'd have to allow the same nest of reference nodes we
3947 allow for MODIFY_EXPR and that's too complex. */
3948 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3949 && (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1)))))
3950 ret = gimplify_variable_sized_compare (expr_p);
3952 /* If *EXPR_P does not need to be special-cased, handle it
3953 according to its class. */
3954 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3955 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3956 post_p, is_gimple_val, fb_rvalue);
3957 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3958 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3959 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3960 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3961 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3963 enum gimplify_status r0, r1;
3965 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3966 post_p, is_gimple_val, fb_rvalue);
3967 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3968 post_p, is_gimple_val, fb_rvalue);
3970 ret = MIN (r0, r1);
3972 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3973 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3975 ret = GS_ALL_DONE;
3976 break;
3978 else
3979 /* Fail if we don't know how to handle this tree code. */
3980 abort ();
3982 recalculate_side_effects (*expr_p);
3983 break;
3986 /* If we replaced *expr_p, gimplify again. */
3987 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3988 ret = GS_ALL_DONE;
3990 while (ret == GS_OK);
3992 /* If we encountered an error_mark somewhere nested inside, either
3993 stub out the statement or propagate the error back out. */
3994 if (ret == GS_ERROR)
3996 if (is_statement)
3997 *expr_p = NULL;
3998 goto out;
4001 #ifdef ENABLE_CHECKING
4002 /* This was only valid as a return value from the langhook, which
4003 we handled. Make sure it doesn't escape from any other context. */
4004 if (ret == GS_UNHANDLED)
4005 abort ();
4006 #endif
4008 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
4010 /* We aren't looking for a value, and we don't have a valid
4011 statement. If it doesn't have side-effects, throw it away. */
4012 if (!TREE_SIDE_EFFECTS (*expr_p))
4013 *expr_p = NULL;
4014 else if (!TREE_THIS_VOLATILE (*expr_p))
4016 /* This is probably a _REF that contains something nested that
4017 has side effects. Recurse through the operands to find it. */
4018 enum tree_code code = TREE_CODE (*expr_p);
4020 if (code == COMPONENT_REF
4021 || code == REALPART_EXPR || code == IMAGPART_EXPR)
4022 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4023 gimple_test_f, fallback);
4024 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
4026 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4027 gimple_test_f, fallback);
4028 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
4029 gimple_test_f, fallback);
4031 else
4032 /* Anything else with side-effects
4033 must be converted to a valid statement before we get here. */
4034 abort ();
4036 *expr_p = NULL;
4038 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
4040 /* Historically, the compiler has treated a bare
4041 reference to a volatile lvalue as forcing a load. */
4042 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
4043 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
4045 else
4046 /* We can't do anything useful with a volatile reference to
4047 incomplete type, so just throw it away. */
4048 *expr_p = NULL;
4051 /* If we are gimplifying at the statement level, we're done. Tack
4052 everything together and replace the original statement with the
4053 gimplified form. */
4054 if (fallback == fb_none || is_statement)
4056 if (internal_pre || internal_post)
4058 append_to_statement_list (*expr_p, &internal_pre);
4059 append_to_statement_list (internal_post, &internal_pre);
4060 annotate_all_with_locus (&internal_pre, input_location);
4061 *expr_p = internal_pre;
4063 else if (!*expr_p)
4065 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
4066 annotate_all_with_locus (expr_p, input_location);
4067 else
4068 annotate_one_with_locus (*expr_p, input_location);
4069 goto out;
4072 /* Otherwise we're gimplifying a subexpression, so the resulting value is
4073 interesting. */
4075 /* If it's sufficiently simple already, we're done. Unless we are
4076 handling some post-effects internally; if that's the case, we need to
4077 copy into a temp before adding the post-effects to the tree. */
4078 if (!internal_post && (*gimple_test_f) (*expr_p))
4079 goto out;
4081 /* Otherwise, we need to create a new temporary for the gimplified
4082 expression. */
4084 /* We can't return an lvalue if we have an internal postqueue. The
4085 object the lvalue refers to would (probably) be modified by the
4086 postqueue; we need to copy the value out first, which means an
4087 rvalue. */
4088 if ((fallback & fb_lvalue) && !internal_post
4089 && is_gimple_addressable (*expr_p))
4091 /* An lvalue will do. Take the address of the expression, store it
4092 in a temporary, and replace the expression with an INDIRECT_REF of
4093 that temporary. */
4094 tmp = build_fold_addr_expr (*expr_p);
4095 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
4096 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
4098 else if ((fallback & fb_rvalue) && is_gimple_tmp_rhs (*expr_p))
4100 #if defined ENABLE_CHECKING
4101 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
4102 abort ();
4103 #endif
4105 /* An rvalue will do. Assign the gimplified expression into a new
4106 temporary TMP and replace the original expression with TMP. */
4108 if (internal_post || (fallback & fb_lvalue))
4109 /* The postqueue might change the value of the expression between
4110 the initialization and use of the temporary, so we can't use a
4111 formal temp. FIXME do we care? */
4112 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
4113 else
4114 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4116 else if (fallback & fb_mayfail)
4118 /* If this is an asm statement, and the user asked for the impossible,
4119 don't abort. Fail and let gimplify_asm_expr issue an error. */
4120 ret = GS_ERROR;
4121 goto out;
4123 else
4125 fprintf (stderr, "gimplification failed:\n");
4126 print_generic_expr (stderr, *expr_p, 0);
4127 debug_tree (*expr_p);
4128 abort ();
4131 #if defined ENABLE_CHECKING
4132 /* Make sure the temporary matches our predicate. */
4133 if (!(*gimple_test_f) (*expr_p))
4134 abort ();
4135 #endif
4137 if (internal_post)
4139 annotate_all_with_locus (&internal_post, input_location);
4140 append_to_statement_list (internal_post, pre_p);
4143 out:
4144 input_location = saved_location;
4145 return ret;
4148 /* Look through TYPE for variable-sized objects and gimplify each such
4149 size that we find. Add to LIST_P any statements generated. */
4151 void
4152 gimplify_type_sizes (tree type, tree *list_p)
4154 tree field;
4156 switch (TREE_CODE (type))
4158 case ERROR_MARK:
4159 return;
4161 case INTEGER_TYPE:
4162 case ENUMERAL_TYPE:
4163 case BOOLEAN_TYPE:
4164 case CHAR_TYPE:
4165 case REAL_TYPE:
4166 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
4167 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
4168 break;
4170 case ARRAY_TYPE:
4171 /* These anonymous types don't have declarations, so handle them here. */
4172 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
4173 break;
4175 case RECORD_TYPE:
4176 case UNION_TYPE:
4177 case QUAL_UNION_TYPE:
4178 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4179 if (TREE_CODE (field) == FIELD_DECL)
4180 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
4181 break;
4183 default:
4184 break;
4187 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
4188 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
4191 /* Subroutine of the above to gimplify one size or position, *EXPR_P.
4192 We add any required statements to STMT_P. */
4194 void
4195 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
4197 /* We don't do anything if the value isn't there, is constant, or contains
4198 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
4199 a VAR_DECL. If it's a VAR_DECL from another function, the gimplfier
4200 will want to replace it with a new variable, but that will cause problems
4201 if this type is from outside the function. It's OK to have that here. */
4202 if (*expr_p == NULL_TREE || TREE_CONSTANT (*expr_p)
4203 || TREE_CODE (*expr_p) == VAR_DECL
4204 || CONTAINS_PLACEHOLDER_P (*expr_p))
4205 return;
4207 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
4210 #ifdef ENABLE_CHECKING
4211 /* Compare types A and B for a "close enough" match. */
4213 static bool
4214 cpt_same_type (tree a, tree b)
4216 if (lang_hooks.types_compatible_p (a, b))
4217 return true;
4219 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
4220 link them together. This routine is intended to catch type errors
4221 that will affect the optimizers, and the optimizers don't add new
4222 dereferences of function pointers, so ignore it. */
4223 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
4224 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
4225 return true;
4227 /* ??? The C FE pushes type qualifiers after the fact into the type of
4228 the element from the type of the array. See build_unary_op's handling
4229 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
4230 should have done it when creating the variable in the first place.
4231 Alternately, why aren't the two array types made variants? */
4232 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
4233 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4235 /* And because of those, we have to recurse down through pointers. */
4236 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
4237 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4239 return false;
4242 /* Check for some cases of the front end missing cast expressions.
4243 The type of a dereference should correspond to the pointer type;
4244 similarly the type of an address should match its object. */
4246 static tree
4247 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4248 void *data ATTRIBUTE_UNUSED)
4250 tree t = *tp;
4251 tree ptype, otype, dtype;
4253 switch (TREE_CODE (t))
4255 case INDIRECT_REF:
4256 case ARRAY_REF:
4257 otype = TREE_TYPE (t);
4258 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
4259 dtype = TREE_TYPE (ptype);
4260 if (!cpt_same_type (otype, dtype))
4261 abort ();
4262 break;
4264 case ADDR_EXPR:
4265 ptype = TREE_TYPE (t);
4266 otype = TREE_TYPE (TREE_OPERAND (t, 0));
4267 dtype = TREE_TYPE (ptype);
4268 if (!cpt_same_type (otype, dtype))
4270 /* &array is allowed to produce a pointer to the element, rather than
4271 a pointer to the array type. We must allow this in order to
4272 properly represent assigning the address of an array in C into
4273 pointer to the element type. */
4274 if (TREE_CODE (otype) == ARRAY_TYPE
4275 && POINTER_TYPE_P (ptype)
4276 && cpt_same_type (TREE_TYPE (otype), dtype))
4277 break;
4278 abort ();
4280 break;
4282 default:
4283 return NULL_TREE;
4287 return NULL_TREE;
4289 #endif
4291 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
4292 function decl containing BODY. */
4294 void
4295 gimplify_body (tree *body_p, tree fndecl)
4297 location_t saved_location = input_location;
4298 tree body;
4300 timevar_push (TV_TREE_GIMPLIFY);
4301 push_gimplify_context ();
4303 /* Unshare most shared trees in the body and in that of any nested functions.
4304 It would seem we don't have to do this for nested functions because
4305 they are supposed to be output and then the outer function gimplified
4306 first, but the g++ front end doesn't always do it that way. */
4307 unshare_body (body_p, fndecl);
4308 unvisit_body (body_p, fndecl);
4310 /* Make sure input_location isn't set to something wierd. */
4311 input_location = DECL_SOURCE_LOCATION (fndecl);
4313 /* Gimplify the function's body. */
4314 gimplify_stmt (body_p);
4315 body = *body_p;
4317 /* Unshare again, in case gimplification was sloppy. */
4318 unshare_all_trees (body);
4320 if (!body)
4321 body = alloc_stmt_list ();
4322 else if (TREE_CODE (body) == STATEMENT_LIST)
4324 tree t = expr_only (*body_p);
4325 if (t)
4326 body = t;
4329 /* If there isn't an outer BIND_EXPR, add one. */
4330 if (TREE_CODE (body) != BIND_EXPR)
4332 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
4333 NULL_TREE, NULL_TREE);
4334 TREE_SIDE_EFFECTS (b) = 1;
4335 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
4336 body = b;
4338 *body_p = body;
4340 pop_gimplify_context (body);
4342 #ifdef ENABLE_CHECKING
4343 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
4344 #endif
4346 timevar_pop (TV_TREE_GIMPLIFY);
4347 input_location = saved_location;
4350 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
4351 node for the function we want to gimplify. */
4353 void
4354 gimplify_function_tree (tree fndecl)
4356 tree oldfn;
4358 oldfn = current_function_decl;
4359 current_function_decl = fndecl;
4361 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
4363 /* If we're instrumenting function entry/exit, then prepend the call to
4364 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
4365 catch the exit hook. */
4366 /* ??? Add some way to ignore exceptions for this TFE. */
4367 if (flag_instrument_function_entry_exit
4368 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
4370 tree tf, x, bind;
4372 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4373 TREE_SIDE_EFFECTS (tf) = 1;
4374 x = DECL_SAVED_TREE (fndecl);
4375 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
4376 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
4377 x = build_function_call_expr (x, NULL);
4378 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
4380 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4381 TREE_SIDE_EFFECTS (bind) = 1;
4382 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
4383 x = build_function_call_expr (x, NULL);
4384 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
4385 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
4387 DECL_SAVED_TREE (fndecl) = bind;
4390 current_function_decl = oldfn;
4393 #include "gt-gimplify.h"