2004-08-19 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / gcc / gimplify.c
blob3dba42d9b8564781eb4b2144b2d8b5bb67bec7a1
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 tree t;
133 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
134 abort ();
136 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
137 DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
139 if (body)
140 declare_tmp_vars (gimplify_ctxp->temps, body);
141 else
142 record_vars (gimplify_ctxp->temps);
144 #if 0
145 if (!quiet_flag)
146 fprintf (stderr, " collisions: %f ",
147 htab_collisions (gimplify_ctxp->temp_htab));
148 #endif
150 htab_delete (gimplify_ctxp->temp_htab);
151 free (gimplify_ctxp);
152 gimplify_ctxp = NULL;
155 void
156 gimple_push_bind_expr (tree bind)
158 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
159 gimplify_ctxp->current_bind_expr = bind;
162 void
163 gimple_pop_bind_expr (void)
165 gimplify_ctxp->current_bind_expr
166 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
169 tree
170 gimple_current_bind_expr (void)
172 return gimplify_ctxp->current_bind_expr;
175 /* Returns true iff there is a COND_EXPR between us and the innermost
176 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
178 static bool
179 gimple_conditional_context (void)
181 return gimplify_ctxp->conditions > 0;
184 /* Note that we've entered a COND_EXPR. */
186 static void
187 gimple_push_condition (void)
189 ++(gimplify_ctxp->conditions);
192 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
193 now, add any conditional cleanups we've seen to the prequeue. */
195 static void
196 gimple_pop_condition (tree *pre_p)
198 int conds = --(gimplify_ctxp->conditions);
200 if (conds == 0)
202 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
203 gimplify_ctxp->conditional_cleanups = NULL_TREE;
205 else if (conds < 0)
206 abort ();
209 /* A subroutine of append_to_statement_list{,_force}. */
211 static void
212 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
214 tree list = *list_p;
215 tree_stmt_iterator i;
217 if (!side_effects)
218 return;
220 if (!list)
222 if (t && TREE_CODE (t) == STATEMENT_LIST)
224 *list_p = t;
225 return;
227 *list_p = list = alloc_stmt_list ();
230 i = tsi_last (list);
231 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
234 /* Add T to the end of the list container pointed by LIST_P.
235 If T is an expression with no effects, it is ignored. */
237 void
238 append_to_statement_list (tree t, tree *list_p)
240 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
243 /* Similar, but the statement is always added, regardless of side effects. */
245 void
246 append_to_statement_list_force (tree t, tree *list_p)
248 append_to_statement_list_1 (t, list_p, t != NULL);
251 /* Both gimplify the statement T and append it to LIST_P. */
253 void
254 gimplify_and_add (tree t, tree *list_p)
256 gimplify_stmt (&t);
257 append_to_statement_list (t, list_p);
260 /* Strip off a legitimate source ending from the input string NAME of
261 length LEN. Rather than having to know the names used by all of
262 our front ends, we strip off an ending of a period followed by
263 up to five characters. (Java uses ".class".) */
265 static inline void
266 remove_suffix (char *name, int len)
268 int i;
270 for (i = 2; i < 8 && len > i; i++)
272 if (name[len - i] == '.')
274 name[len - i] = '\0';
275 break;
280 /* Create a nameless artificial label and put it in the current function
281 context. Returns the newly created label. */
283 tree
284 create_artificial_label (void)
286 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
288 DECL_ARTIFICIAL (lab) = 1;
289 DECL_CONTEXT (lab) = current_function_decl;
290 return lab;
293 /* Create a new temporary name with PREFIX. Returns an identifier. */
295 static GTY(()) unsigned int tmp_var_id_num;
297 tree
298 create_tmp_var_name (const char *prefix)
300 char *tmp_name;
302 if (prefix)
304 char *preftmp = ASTRDUP (prefix);
306 remove_suffix (preftmp, strlen (preftmp));
307 prefix = preftmp;
310 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
311 return get_identifier (tmp_name);
315 /* Create a new temporary variable declaration of type TYPE.
316 Does NOT push it into the current binding. */
318 tree
319 create_tmp_var_raw (tree type, const char *prefix)
321 tree tmp_var;
322 tree new_type;
324 /* Make the type of the variable writable. */
325 new_type = build_type_variant (type, 0, 0);
326 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
328 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
330 /* The variable was declared by the compiler. */
331 DECL_ARTIFICIAL (tmp_var) = 1;
332 /* And we don't want debug info for it. */
333 DECL_IGNORED_P (tmp_var) = 1;
335 /* Make the variable writable. */
336 TREE_READONLY (tmp_var) = 0;
338 DECL_EXTERNAL (tmp_var) = 0;
339 TREE_STATIC (tmp_var) = 0;
340 TREE_USED (tmp_var) = 1;
342 return tmp_var;
345 /* Create a new temporary variable declaration of type TYPE. DOES push the
346 variable into the current binding. Further, assume that this is called
347 only from gimplification or optimization, at which point the creation of
348 certain types are bugs. */
350 tree
351 create_tmp_var (tree type, const char *prefix)
353 tree tmp_var;
355 #if defined ENABLE_CHECKING
356 /* We don't allow types that are addressable (meaning we can't make copies),
357 incomplete, or of variable size. */
358 if (TREE_ADDRESSABLE (type)
359 || !COMPLETE_TYPE_P (type)
360 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
361 abort ();
362 #endif
364 tmp_var = create_tmp_var_raw (type, prefix);
365 gimple_add_tmp_var (tmp_var);
366 return tmp_var;
369 /* Given a tree, try to return a useful variable name that we can use
370 to prefix a temporary that is being assigned the value of the tree.
371 I.E. given <temp> = &A, return A. */
373 const char *
374 get_name (tree t)
376 tree stripped_decl;
378 stripped_decl = t;
379 STRIP_NOPS (stripped_decl);
380 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
381 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
382 else
384 switch (TREE_CODE (stripped_decl))
386 case ADDR_EXPR:
387 return get_name (TREE_OPERAND (stripped_decl, 0));
388 break;
389 default:
390 return NULL;
395 /* Create a temporary with a name derived from VAL. Subroutine of
396 lookup_tmp_var; nobody else should call this function. */
398 static inline tree
399 create_tmp_from_val (tree val)
401 return create_tmp_var (TREE_TYPE (val), get_name (val));
404 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
405 an existing expression temporary. */
407 static tree
408 lookup_tmp_var (tree val, bool is_formal)
410 tree ret;
412 if (!is_formal || TREE_SIDE_EFFECTS (val))
413 ret = create_tmp_from_val (val);
414 else
416 elt_t elt, *elt_p;
417 void **slot;
419 elt.val = val;
420 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
421 if (*slot == NULL)
423 elt_p = xmalloc (sizeof (*elt_p));
424 elt_p->val = val;
425 elt_p->temp = ret = create_tmp_from_val (val);
426 *slot = (void *) elt_p;
428 else
430 elt_p = (elt_t *) *slot;
431 ret = elt_p->temp;
435 if (is_formal)
436 DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
438 return ret;
441 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
442 in gimplify_expr. Only use this function if:
444 1) The value of the unfactored expression represented by VAL will not
445 change between the initialization and use of the temporary, and
446 2) The temporary will not be otherwise modified.
448 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
449 and #2 means it is inappropriate for && temps.
451 For other cases, use get_initialized_tmp_var instead. */
453 static tree
454 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
456 tree t, mod;
457 char class;
459 gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
461 t = lookup_tmp_var (val, is_formal);
463 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
465 class = TREE_CODE_CLASS (TREE_CODE (val));
466 if (EXPR_HAS_LOCATION (val))
467 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
468 else
469 SET_EXPR_LOCATION (mod, input_location);
471 /* gimplify_modify_expr might want to reduce this further. */
472 gimplify_and_add (mod, pre_p);
473 return t;
476 tree
477 get_formal_tmp_var (tree val, tree *pre_p)
479 return internal_get_tmp_var (val, pre_p, NULL, true);
482 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
483 are as in gimplify_expr. */
485 tree
486 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
488 return internal_get_tmp_var (val, pre_p, post_p, false);
491 /* Declares all the variables in VARS in SCOPE. */
493 void
494 declare_tmp_vars (tree vars, tree scope)
496 tree last = vars;
497 if (last)
499 tree temps;
501 /* C99 mode puts the default 'return 0;' for main outside the outer
502 braces. So drill down until we find an actual scope. */
503 while (TREE_CODE (scope) == COMPOUND_EXPR)
504 scope = TREE_OPERAND (scope, 0);
506 if (TREE_CODE (scope) != BIND_EXPR)
507 abort ();
509 temps = nreverse (last);
510 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
511 BIND_EXPR_VARS (scope) = temps;
515 void
516 gimple_add_tmp_var (tree tmp)
518 if (TREE_CHAIN (tmp) || DECL_SEEN_IN_BIND_EXPR_P (tmp))
519 abort ();
521 DECL_CONTEXT (tmp) = current_function_decl;
522 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
524 if (gimplify_ctxp)
526 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
527 gimplify_ctxp->temps = tmp;
529 else if (cfun)
530 record_vars (tmp);
531 else
532 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
535 /* Determines whether to assign a locus to the statement STMT. */
537 static bool
538 should_carry_locus_p (tree stmt)
540 /* Don't emit a line note for a label. We particularly don't want to
541 emit one for the break label, since it doesn't actually correspond
542 to the beginning of the loop/switch. */
543 if (TREE_CODE (stmt) == LABEL_EXPR)
544 return false;
546 /* Do not annotate empty statements, since it confuses gcov. */
547 if (!TREE_SIDE_EFFECTS (stmt))
548 return false;
550 return true;
553 static void
554 annotate_one_with_locus (tree t, location_t locus)
556 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
557 && ! EXPR_HAS_LOCATION (t)
558 && should_carry_locus_p (t))
559 SET_EXPR_LOCATION (t, locus);
562 void
563 annotate_all_with_locus (tree *stmt_p, location_t locus)
565 tree_stmt_iterator i;
567 if (!*stmt_p)
568 return;
570 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
572 tree t = tsi_stmt (i);
574 #ifdef ENABLE_CHECKING
575 /* Assuming we've already been gimplified, we shouldn't
576 see nested chaining constructs anymore. */
577 if (TREE_CODE (t) == STATEMENT_LIST
578 || TREE_CODE (t) == COMPOUND_EXPR)
579 abort ();
580 #endif
582 annotate_one_with_locus (t, locus);
586 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
587 These nodes model computations that should only be done once. If we
588 were to unshare something like SAVE_EXPR(i++), the gimplification
589 process would create wrong code. */
591 static tree
592 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
594 enum tree_code code = TREE_CODE (*tp);
595 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
596 if (TREE_CODE_CLASS (code) == 't'
597 || TREE_CODE_CLASS (code) == 'd'
598 || TREE_CODE_CLASS (code) == 'c'
599 || code == SAVE_EXPR || code == TARGET_EXPR
600 /* We can't do anything sensible with a BLOCK used as an expression,
601 but we also can't abort when we see it because of non-expression
602 uses. So just avert our eyes and cross our fingers. Silly Java. */
603 || code == BLOCK)
604 *walk_subtrees = 0;
605 else if (code == BIND_EXPR)
606 abort ();
607 else
608 copy_tree_r (tp, walk_subtrees, data);
610 return NULL_TREE;
613 /* Callback for walk_tree to unshare most of the shared trees rooted at
614 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
615 then *TP is deep copied by calling copy_tree_r.
617 This unshares the same trees as copy_tree_r with the exception of
618 SAVE_EXPR nodes. These nodes model computations that should only be
619 done once. If we were to unshare something like SAVE_EXPR(i++), the
620 gimplification process would create wrong code. */
622 static tree
623 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
624 void *data ATTRIBUTE_UNUSED)
626 tree t = *tp;
627 enum tree_code code = TREE_CODE (t);
629 /* Skip types, decls, and constants. But we do want to look at their
630 types and the bounds of types. Mark them as visited so we properly
631 unmark their subtrees on the unmark pass. If we've already seen them,
632 don't look down further. */
633 if (TREE_CODE_CLASS (code) == 't'
634 || TREE_CODE_CLASS (code) == 'd'
635 || TREE_CODE_CLASS (code) == 'c')
637 if (TREE_VISITED (t))
638 *walk_subtrees = 0;
639 else
640 TREE_VISITED (t) = 1;
643 /* If this node has been visited already, unshare it and don't look
644 any deeper. */
645 else if (TREE_VISITED (t))
647 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
648 *walk_subtrees = 0;
651 /* Otherwise, mark the tree as visited and keep looking. */
652 else
653 TREE_VISITED (t) = 1;
655 return NULL_TREE;
658 static tree
659 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
660 void *data ATTRIBUTE_UNUSED)
662 if (TREE_VISITED (*tp))
663 TREE_VISITED (*tp) = 0;
664 else
665 *walk_subtrees = 0;
667 return NULL_TREE;
670 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
671 bodies of any nested functions if we are unsharing the entire body of
672 FNDECL. */
674 static void
675 unshare_body (tree *body_p, tree fndecl)
677 struct cgraph_node *cgn = cgraph_node (fndecl);
679 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
680 if (body_p == &DECL_SAVED_TREE (fndecl))
681 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
682 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
685 /* Likewise, but mark all trees as not visited. */
687 static void
688 unvisit_body (tree *body_p, tree fndecl)
690 struct cgraph_node *cgn = cgraph_node (fndecl);
692 walk_tree (body_p, unmark_visited_r, NULL, NULL);
693 if (body_p == &DECL_SAVED_TREE (fndecl))
694 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
695 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
698 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
700 void
701 unshare_all_trees (tree t)
703 walk_tree (&t, copy_if_shared_r, NULL, NULL);
704 walk_tree (&t, unmark_visited_r, NULL, NULL);
707 /* Unconditionally make an unshared copy of EXPR. This is used when using
708 stored expressions which span multiple functions, such as BINFO_VTABLE,
709 as the normal unsharing process can't tell that they're shared. */
711 tree
712 unshare_expr (tree expr)
714 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
715 return expr;
718 /* A terser interface for building a representation of a exception
719 specification. */
721 tree
722 gimple_build_eh_filter (tree body, tree allowed, tree failure)
724 tree t;
726 /* FIXME should the allowed types go in TREE_TYPE? */
727 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
728 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
730 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
731 append_to_statement_list (body, &TREE_OPERAND (t, 0));
733 return t;
737 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
738 contain statements and have a value. Assign its value to a temporary
739 and give it void_type_node. Returns the temporary, or NULL_TREE if
740 WRAPPER was already void. */
742 tree
743 voidify_wrapper_expr (tree wrapper, tree temp)
745 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
747 tree *p, sub = wrapper;
749 restart:
750 /* Set p to point to the body of the wrapper. */
751 switch (TREE_CODE (sub))
753 case BIND_EXPR:
754 /* For a BIND_EXPR, the body is operand 1. */
755 p = &BIND_EXPR_BODY (sub);
756 break;
758 default:
759 p = &TREE_OPERAND (sub, 0);
760 break;
763 /* Advance to the last statement. Set all container types to void. */
764 if (TREE_CODE (*p) == STATEMENT_LIST)
766 tree_stmt_iterator i = tsi_last (*p);
767 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
769 else
771 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
773 TREE_SIDE_EFFECTS (*p) = 1;
774 TREE_TYPE (*p) = void_type_node;
778 if (p == NULL || IS_EMPTY_STMT (*p))
780 /* Look through exception handling. */
781 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
782 || TREE_CODE (*p) == TRY_CATCH_EXPR)
784 sub = *p;
785 goto restart;
787 /* The C++ frontend already did this for us. */
788 else if (TREE_CODE (*p) == INIT_EXPR
789 || TREE_CODE (*p) == TARGET_EXPR)
790 temp = TREE_OPERAND (*p, 0);
791 /* If we're returning a dereference, move the dereference
792 outside the wrapper. */
793 else if (TREE_CODE (*p) == INDIRECT_REF)
795 tree ptr = TREE_OPERAND (*p, 0);
796 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
797 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
798 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
799 /* If this is a BIND_EXPR for a const inline function, it might not
800 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
801 TREE_SIDE_EFFECTS (wrapper) = 1;
803 else
805 if (!temp)
806 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
807 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
808 TREE_SIDE_EFFECTS (wrapper) = 1;
811 TREE_TYPE (wrapper) = void_type_node;
812 return temp;
815 return NULL_TREE;
818 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
819 a temporary through which they communicate. */
821 static void
822 build_stack_save_restore (tree *save, tree *restore)
824 tree save_call, tmp_var;
826 save_call =
827 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
828 NULL_TREE);
829 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
831 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
832 *restore =
833 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
834 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
837 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
839 static enum gimplify_status
840 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
842 tree bind_expr = *expr_p;
843 bool old_save_stack = gimplify_ctxp->save_stack;
844 tree t;
846 temp = voidify_wrapper_expr (bind_expr, temp);
848 /* Mark variables seen in this bind expr. */
849 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
850 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
852 gimple_push_bind_expr (bind_expr);
853 gimplify_ctxp->save_stack = false;
855 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
857 if (gimplify_ctxp->save_stack)
859 tree stack_save, stack_restore;
861 /* Save stack on entry and restore it on exit. Add a try_finally
862 block to achieve this. Note that mudflap depends on the
863 format of the emitted code: see mx_register_decls(). */
864 build_stack_save_restore (&stack_save, &stack_restore);
866 t = build (TRY_FINALLY_EXPR, void_type_node,
867 BIND_EXPR_BODY (bind_expr), NULL_TREE);
868 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
870 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
871 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
872 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
875 gimplify_ctxp->save_stack = old_save_stack;
876 gimple_pop_bind_expr ();
878 if (temp)
880 *expr_p = temp;
881 append_to_statement_list (bind_expr, pre_p);
882 return GS_OK;
884 else
885 return GS_ALL_DONE;
888 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
889 GIMPLE value, it is assigned to a new temporary and the statement is
890 re-written to return the temporary.
892 PRE_P points to the list where side effects that must happen before
893 STMT should be stored. */
895 static enum gimplify_status
896 gimplify_return_expr (tree stmt, tree *pre_p)
898 tree ret_expr = TREE_OPERAND (stmt, 0);
899 tree result_decl, result;
901 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
902 || ret_expr == error_mark_node)
903 return GS_ALL_DONE;
905 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
906 result_decl = NULL_TREE;
907 else
909 result_decl = TREE_OPERAND (ret_expr, 0);
910 if (TREE_CODE (result_decl) == INDIRECT_REF)
911 /* See through a return by reference. */
912 result_decl = TREE_OPERAND (result_decl, 0);
913 #ifdef ENABLE_CHECKING
914 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
915 && TREE_CODE (ret_expr) != INIT_EXPR)
916 || TREE_CODE (result_decl) != RESULT_DECL)
917 abort ();
918 #endif
921 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
922 Recall that aggregate_value_p is FALSE for any aggregate type that is
923 returned in registers. If we're returning values in registers, then
924 we don't want to extend the lifetime of the RESULT_DECL, particularly
925 across another call. In addition, for those aggregates for which
926 hard_function_value generates a PARALLEL, we'll abort during normal
927 expansion of structure assignments; there's special code in expand_return
928 to handle this case that does not exist in expand_expr. */
929 if (!result_decl
930 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
931 result = result_decl;
932 else if (gimplify_ctxp->return_temp)
933 result = gimplify_ctxp->return_temp;
934 else
936 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
938 /* ??? With complex control flow (usually involving abnormal edges),
939 we can wind up warning about an uninitialized value for this. Due
940 to how this variable is constructed and initialized, this is never
941 true. Give up and never warn. */
942 TREE_NO_WARNING (result) = 1;
944 gimplify_ctxp->return_temp = result;
947 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
948 Then gimplify the whole thing. */
949 if (result != result_decl)
950 TREE_OPERAND (ret_expr, 0) = result;
952 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
954 /* If we didn't use a temporary, then the result is just the result_decl.
955 Otherwise we need a simple copy. This should already be gimple. */
956 if (result == result_decl)
957 ret_expr = result;
958 else
959 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
960 TREE_OPERAND (stmt, 0) = ret_expr;
962 return GS_ALL_DONE;
965 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
966 and initialization explicit. */
968 static enum gimplify_status
969 gimplify_decl_expr (tree *stmt_p)
971 tree stmt = *stmt_p;
972 tree decl = DECL_EXPR_DECL (stmt);
974 *stmt_p = NULL_TREE;
976 if (TREE_TYPE (decl) == error_mark_node)
977 return GS_ERROR;
979 else if (TREE_CODE (decl) == TYPE_DECL)
980 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
982 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
984 tree init = DECL_INITIAL (decl);
986 if (!TREE_CONSTANT (DECL_SIZE (decl)))
988 /* This is a variable-sized decl. Simplify its size and mark it
989 for deferred expansion. Note that mudflap depends on the format
990 of the emitted code: see mx_register_decls(). */
991 tree t, args, addr, ptr_type;
993 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
994 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
995 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
997 /* All occurences of this decl in final gimplified code will be
998 replaced by indirection. Setting DECL_VALUE_EXPR does two
999 things: First, it lets the rest of the gimplifier know what
1000 replacement to use. Second, it lets the debug info know
1001 where to find the value. */
1002 ptr_type = build_pointer_type (TREE_TYPE (decl));
1003 addr = create_tmp_var (ptr_type, get_name (decl));
1004 DECL_IGNORED_P (addr) = 0;
1005 t = build_fold_indirect_ref (addr);
1006 DECL_VALUE_EXPR (decl) = t;
1008 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
1009 t = built_in_decls[BUILT_IN_ALLOCA];
1010 t = build_function_call_expr (t, args);
1011 t = fold_convert (ptr_type, t);
1012 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1014 gimplify_and_add (t, stmt_p);
1016 /* Indicate that we need to restore the stack level when the
1017 enclosing BIND_EXPR is exited. */
1018 gimplify_ctxp->save_stack = true;
1021 if (init && init != error_mark_node)
1023 if (!TREE_STATIC (decl))
1025 DECL_INITIAL (decl) = NULL_TREE;
1026 init = build (MODIFY_EXPR, void_type_node, decl, init);
1027 gimplify_and_add (init, stmt_p);
1029 else
1030 /* We must still examine initializers for static variables
1031 as they may contain a label address. */
1032 walk_tree (&init, force_labels_r, NULL, NULL);
1035 /* This decl isn't mentioned in the enclosing block, so add it to the
1036 list of temps. FIXME it seems a bit of a kludge to say that
1037 anonymous artificial vars aren't pushed, but everything else is. */
1038 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1039 gimple_add_tmp_var (decl);
1042 return GS_ALL_DONE;
1045 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1046 and replacing the LOOP_EXPR with goto, but if the loop contains an
1047 EXIT_EXPR, we need to append a label for it to jump to. */
1049 static enum gimplify_status
1050 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1052 tree saved_label = gimplify_ctxp->exit_label;
1053 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1054 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1056 append_to_statement_list (start_label, pre_p);
1058 gimplify_ctxp->exit_label = NULL_TREE;
1060 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1062 if (gimplify_ctxp->exit_label)
1064 append_to_statement_list (jump_stmt, pre_p);
1065 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1067 else
1068 *expr_p = jump_stmt;
1070 gimplify_ctxp->exit_label = saved_label;
1072 return GS_ALL_DONE;
1075 /* Compare two case labels. Because the front end should already have
1076 made sure that case ranges do not overlap, it is enough to only compare
1077 the CASE_LOW values of each case label. */
1079 static int
1080 compare_case_labels (const void *p1, const void *p2)
1082 tree case1 = *(tree *)p1;
1083 tree case2 = *(tree *)p2;
1085 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1088 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1090 void
1091 sort_case_labels (tree label_vec)
1093 size_t len = TREE_VEC_LENGTH (label_vec);
1094 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1096 if (CASE_LOW (default_case))
1098 size_t i;
1100 /* The last label in the vector should be the default case
1101 but it is not. */
1102 for (i = 0; i < len; ++i)
1104 tree t = TREE_VEC_ELT (label_vec, i);
1105 if (!CASE_LOW (t))
1107 default_case = t;
1108 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1109 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1110 break;
1115 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1116 compare_case_labels);
1119 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1120 branch to. */
1122 static enum gimplify_status
1123 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1125 tree switch_expr = *expr_p;
1126 enum gimplify_status ret;
1128 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1129 is_gimple_val, fb_rvalue);
1131 if (SWITCH_BODY (switch_expr))
1133 varray_type labels, saved_labels;
1134 tree label_vec, default_case = NULL_TREE;
1135 size_t i, len;
1137 /* If someone can be bothered to fill in the labels, they can
1138 be bothered to null out the body too. */
1139 if (SWITCH_LABELS (switch_expr))
1140 abort ();
1142 saved_labels = gimplify_ctxp->case_labels;
1143 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1145 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1147 labels = gimplify_ctxp->case_labels;
1148 gimplify_ctxp->case_labels = saved_labels;
1150 len = VARRAY_ACTIVE_SIZE (labels);
1152 for (i = 0; i < len; ++i)
1154 tree t = VARRAY_TREE (labels, i);
1155 if (!CASE_LOW (t))
1157 /* The default case must be the last label in the list. */
1158 default_case = t;
1159 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1160 len--;
1161 break;
1165 label_vec = make_tree_vec (len + 1);
1166 SWITCH_LABELS (*expr_p) = label_vec;
1167 append_to_statement_list (switch_expr, pre_p);
1169 if (! default_case)
1171 /* If the switch has no default label, add one, so that we jump
1172 around the switch body. */
1173 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1174 NULL_TREE, create_artificial_label ());
1175 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1176 *expr_p = build (LABEL_EXPR, void_type_node,
1177 CASE_LABEL (default_case));
1179 else
1180 *expr_p = SWITCH_BODY (switch_expr);
1182 for (i = 0; i < len; ++i)
1183 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1184 TREE_VEC_ELT (label_vec, len) = default_case;
1186 sort_case_labels (label_vec);
1188 SWITCH_BODY (switch_expr) = NULL;
1190 else if (!SWITCH_LABELS (switch_expr))
1191 abort ();
1193 return ret;
1196 static enum gimplify_status
1197 gimplify_case_label_expr (tree *expr_p)
1199 tree expr = *expr_p;
1200 if (gimplify_ctxp->case_labels)
1201 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1202 else
1203 abort ();
1204 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1205 return GS_ALL_DONE;
1208 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1209 a (possibly empty) body. */
1211 static enum gimplify_status
1212 gimplify_labeled_block_expr (tree *expr_p)
1214 tree body = LABELED_BLOCK_BODY (*expr_p);
1215 tree label = LABELED_BLOCK_LABEL (*expr_p);
1216 tree t;
1218 DECL_CONTEXT (label) = current_function_decl;
1219 t = build (LABEL_EXPR, void_type_node, label);
1220 if (body != NULL_TREE)
1221 t = build (COMPOUND_EXPR, void_type_node, body, t);
1222 *expr_p = t;
1224 return GS_OK;
1227 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1229 static enum gimplify_status
1230 gimplify_exit_block_expr (tree *expr_p)
1232 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1233 tree label;
1235 /* First operand must be a LABELED_BLOCK_EXPR, which should
1236 already be lowered (or partially lowered) when we get here. */
1237 #if defined ENABLE_CHECKING
1238 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1239 abort ();
1240 #endif
1242 label = LABELED_BLOCK_LABEL (labeled_block);
1243 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1245 return GS_OK;
1248 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1249 if necessary. */
1251 tree
1252 build_and_jump (tree *label_p)
1254 if (label_p == NULL)
1255 /* If there's nowhere to jump, just fall through. */
1256 return NULL_TREE;
1258 if (*label_p == NULL_TREE)
1260 tree label = create_artificial_label ();
1261 *label_p = label;
1264 return build1 (GOTO_EXPR, void_type_node, *label_p);
1267 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1268 This also involves building a label to jump to and communicating it to
1269 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1271 static enum gimplify_status
1272 gimplify_exit_expr (tree *expr_p)
1274 tree cond = TREE_OPERAND (*expr_p, 0);
1275 tree expr;
1277 expr = build_and_jump (&gimplify_ctxp->exit_label);
1278 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1279 *expr_p = expr;
1281 return GS_OK;
1284 /* A helper function to be called via walk_tree. Mark all labels under *TP
1285 as being forced. To be called for DECL_INITIAL of static variables. */
1287 tree
1288 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1290 if (TYPE_P (*tp))
1291 *walk_subtrees = 0;
1292 if (TREE_CODE (*tp) == LABEL_DECL)
1293 FORCED_LABEL (*tp) = 1;
1295 return NULL_TREE;
1298 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1299 different from its canonical type, wrap the whole thing inside a
1300 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1301 type.
1303 The canonical type of a COMPONENT_REF is the type of the field being
1304 referenced--unless the field is a bit-field which can be read directly
1305 in a smaller mode, in which case the canonical type is the
1306 sign-appropriate type corresponding to that mode. */
1308 static void
1309 canonicalize_component_ref (tree *expr_p)
1311 tree expr = *expr_p;
1312 tree type;
1314 if (TREE_CODE (expr) != COMPONENT_REF)
1315 abort ();
1317 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1318 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1319 else
1320 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1322 if (TREE_TYPE (expr) != type)
1324 tree old_type = TREE_TYPE (expr);
1326 /* Set the type of the COMPONENT_REF to the underlying type. */
1327 TREE_TYPE (expr) = type;
1329 /* And wrap the whole thing inside a NOP_EXPR. */
1330 expr = build1 (NOP_EXPR, old_type, expr);
1332 *expr_p = expr;
1336 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1337 to foo, embed that change in the ADDR_EXPR by converting
1338 T array[U];
1339 (T *)&array
1341 &array[L]
1342 where L is the lower bound. For simplicity, only do this for constant
1343 lower bound. */
1345 static void
1346 canonicalize_addr_expr (tree *expr_p)
1348 tree expr = *expr_p;
1349 tree ctype = TREE_TYPE (expr);
1350 tree addr_expr = TREE_OPERAND (expr, 0);
1351 tree atype = TREE_TYPE (addr_expr);
1352 tree dctype, datype, ddatype, otype, obj_expr;
1354 /* Both cast and addr_expr types should be pointers. */
1355 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1356 return;
1358 /* The addr_expr type should be a pointer to an array. */
1359 datype = TREE_TYPE (atype);
1360 if (TREE_CODE (datype) != ARRAY_TYPE)
1361 return;
1363 /* Both cast and addr_expr types should address the same object type. */
1364 dctype = TREE_TYPE (ctype);
1365 ddatype = TREE_TYPE (datype);
1366 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1367 return;
1369 /* The addr_expr and the object type should match. */
1370 obj_expr = TREE_OPERAND (addr_expr, 0);
1371 otype = TREE_TYPE (obj_expr);
1372 if (!lang_hooks.types_compatible_p (otype, datype))
1373 return;
1375 /* The lower bound and element sizes must be constant. */
1376 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1377 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1378 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1379 return;
1381 /* All checks succeeded. Build a new node to merge the cast. */
1382 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1383 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1384 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1385 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1386 size_int (TYPE_ALIGN (dctype)
1387 / BITS_PER_UNIT)));
1388 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1391 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1392 underneath as appropriate. */
1394 static enum gimplify_status
1395 gimplify_conversion (tree *expr_p)
1397 /* If we still have a conversion at the toplevel, then strip
1398 away all but the outermost conversion. */
1399 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1401 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1403 /* And remove the outermost conversion if it's useless. */
1404 if (tree_ssa_useless_type_conversion (*expr_p))
1405 *expr_p = TREE_OPERAND (*expr_p, 0);
1408 /* If we still have a conversion at the toplevel,
1409 then canonicalize some constructs. */
1410 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1412 tree sub = TREE_OPERAND (*expr_p, 0);
1414 /* If a NOP conversion is changing the type of a COMPONENT_REF
1415 expression, then canonicalize its type now in order to expose more
1416 redundant conversions. */
1417 if (TREE_CODE (sub) == COMPONENT_REF)
1418 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1420 /* If a NOP conversion is changing a pointer to array of foo
1421 to a pointer to foo, embed that change in the ADDR_EXPR. */
1422 else if (TREE_CODE (sub) == ADDR_EXPR)
1423 canonicalize_addr_expr (expr_p);
1426 return GS_OK;
1429 /* Subroutine of gimplify_compound_lval.
1430 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1432 static enum gimplify_status
1433 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1435 tree array = TREE_OPERAND (*expr_p, 0);
1436 tree arrtype = TREE_TYPE (array);
1437 tree elttype = TREE_TYPE (arrtype);
1438 tree size = array_ref_element_size (*expr_p);
1439 tree ptrtype = build_pointer_type (elttype);
1440 enum tree_code add_code = PLUS_EXPR;
1441 tree idx = TREE_OPERAND (*expr_p, 1);
1442 tree minidx = unshare_expr (array_ref_low_bound (*expr_p));
1443 tree offset, addr, result;
1444 enum gimplify_status ret;
1446 /* If the array domain does not start at zero, apply the offset. */
1447 if (!integer_zerop (minidx))
1449 idx = convert (TREE_TYPE (minidx), idx);
1450 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1453 /* If the index is negative -- a technically invalid situation now
1454 that we've biased the index back to zero -- then casting it to
1455 unsigned has ill effects. In particular, -1*4U/4U != -1.
1456 Represent this as a subtraction of a positive rather than addition
1457 of a negative. This will prevent any conversion back to ARRAY_REF
1458 from getting the wrong results from the division. */
1459 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1461 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1462 add_code = MINUS_EXPR;
1465 /* Pointer arithmetic must be done in sizetype. */
1466 idx = fold_convert (sizetype, idx);
1468 /* Convert the index to a byte offset. */
1469 offset = size_binop (MULT_EXPR, size, idx);
1471 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1472 if (ret == GS_ERROR)
1473 return ret;
1475 addr = build_fold_addr_expr_with_type (array, ptrtype);
1476 result = fold (build (add_code, ptrtype, addr, offset));
1477 *expr_p = build1 (INDIRECT_REF, elttype, result);
1479 return GS_OK;
1482 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1483 node pointed by EXPR_P.
1485 compound_lval
1486 : min_lval '[' val ']'
1487 | min_lval '.' ID
1488 | compound_lval '[' val ']'
1489 | compound_lval '.' ID
1491 This is not part of the original SIMPLE definition, which separates
1492 array and member references, but it seems reasonable to handle them
1493 together. Also, this way we don't run into problems with union
1494 aliasing; gcc requires that for accesses through a union to alias, the
1495 union reference must be explicit, which was not always the case when we
1496 were splitting up array and member refs.
1498 PRE_P points to the list where side effects that must happen before
1499 *EXPR_P should be stored.
1501 POST_P points to the list where side effects that must happen after
1502 *EXPR_P should be stored. */
1504 static enum gimplify_status
1505 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1506 tree *post_p, fallback_t fallback)
1508 tree *p;
1509 varray_type stack;
1510 enum gimplify_status ret = GS_OK, tret;
1511 int i;
1513 /* Create a stack of the subexpressions so later we can walk them in
1514 order from inner to outer. */
1515 VARRAY_TREE_INIT (stack, 10, "stack");
1517 /* We can either handle REALPART_EXPR, IMAGEPART_EXPR anything that
1518 handled_components can deal with. */
1519 for (p = expr_p;
1520 (handled_component_p (*p)
1521 || TREE_CODE (*p) == REALPART_EXPR || TREE_CODE (*p) == IMAGPART_EXPR);
1522 p = &TREE_OPERAND (*p, 0))
1523 VARRAY_PUSH_TREE (stack, *p);
1525 #if defined ENABLE_CHECKING
1526 if (VARRAY_ACTIVE_SIZE (stack) == 0)
1527 abort ();
1528 #endif
1530 /* Now STACK is a stack of pointers to all the refs we've walked through
1531 and P points to the innermost expression.
1533 Java requires that we elaborated nodes in source order. That
1534 means we must gimplify the inner expression followed by each of
1535 the indices, in order. But we can't gimplify the inner
1536 expression until we deal with any variable bounds, sizes, or
1537 positions in order to deal with PLACEHOLDER_EXPRs.
1539 So we do this in three steps. First we deal with the annotations
1540 for any variables in the components, then we gimplify the base,
1541 then we gimplify any indices, from left to right. */
1542 for (i = VARRAY_ACTIVE_SIZE (stack) - 1; i >= 0; i--)
1544 tree t = VARRAY_TREE (stack, i);
1546 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1548 /* Gimplify the low bound and element type size and put them into
1549 the ARRAY_REF. If these values are set, they have already been
1550 gimplified. */
1551 if (!TREE_OPERAND (t, 2))
1553 tree low = unshare_expr (array_ref_low_bound (t));
1554 if (!is_gimple_min_invariant (low))
1556 TREE_OPERAND (t, 2) = low;
1557 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1558 is_gimple_formal_tmp_reg, fb_rvalue);
1559 ret = MIN (ret, tret);
1563 if (!TREE_OPERAND (t, 3))
1565 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1566 tree elmt_size = unshare_expr (array_ref_element_size (t));
1567 tree factor = size_int (TYPE_ALIGN (elmt_type) / BITS_PER_UNIT);
1569 /* Divide the element size by the alignment of the element
1570 type (above). */
1571 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1573 if (!is_gimple_min_invariant (elmt_size))
1575 TREE_OPERAND (t, 3) = elmt_size;
1576 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1577 is_gimple_formal_tmp_reg, fb_rvalue);
1578 ret = MIN (ret, tret);
1582 else if (TREE_CODE (t) == COMPONENT_REF)
1584 /* Set the field offset into T and gimplify it. */
1585 if (!TREE_OPERAND (t, 2))
1587 tree offset = unshare_expr (component_ref_field_offset (t));
1588 tree field = TREE_OPERAND (t, 1);
1589 tree factor
1590 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1592 /* Divide the offset by its alignment. */
1593 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1595 if (!is_gimple_min_invariant (offset))
1597 TREE_OPERAND (t, 2) = offset;
1598 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1599 is_gimple_formal_tmp_reg, fb_rvalue);
1600 ret = MIN (ret, tret);
1606 /* Step 2 is to gimplify the base expression. */
1607 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1608 ret = MIN (ret, tret);
1610 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1611 loop we also remove any useless conversions. */
1612 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1614 tree t = VARRAY_TOP_TREE (stack);
1616 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1618 /* Gimplify the dimension.
1619 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1620 Gimplify non-constant array indices into a temporary
1621 variable.
1622 FIXME - The real fix is to gimplify post-modify
1623 expressions into a minimal gimple lvalue. However, that
1624 exposes bugs in alias analysis. The alias analyzer does
1625 not handle &PTR->FIELD very well. Will fix after the
1626 branch is merged into mainline (dnovillo 2004-05-03). */
1627 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1629 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1630 is_gimple_formal_tmp_reg, fb_rvalue);
1631 ret = MIN (ret, tret);
1634 else if (TREE_CODE (t) == BIT_FIELD_REF)
1636 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1637 is_gimple_val, fb_rvalue);
1638 ret = MIN (ret, tret);
1639 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1640 is_gimple_val, fb_rvalue);
1641 ret = MIN (ret, tret);
1644 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1646 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1647 set which would have caused all the outer expressions in EXPR_P
1648 leading to P to also have had TREE_SIDE_EFFECTS set. */
1649 recalculate_side_effects (t);
1650 VARRAY_POP (stack);
1653 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1654 ret = MIN (ret, tret);
1656 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1657 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1659 canonicalize_component_ref (expr_p);
1660 ret = MIN (ret, GS_OK);
1663 return ret;
1666 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1668 PRE_P points to the list where side effects that must happen before
1669 *EXPR_P should be stored.
1671 POST_P points to the list where side effects that must happen after
1672 *EXPR_P should be stored.
1674 WANT_VALUE is nonzero iff we want to use the value of this expression
1675 in another expression. */
1677 static enum gimplify_status
1678 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1679 bool want_value)
1681 enum tree_code code;
1682 tree lhs, lvalue, rhs, t1;
1683 bool postfix;
1684 enum tree_code arith_code;
1685 enum gimplify_status ret;
1687 code = TREE_CODE (*expr_p);
1689 #if defined ENABLE_CHECKING
1690 if (code != POSTINCREMENT_EXPR
1691 && code != POSTDECREMENT_EXPR
1692 && code != PREINCREMENT_EXPR
1693 && code != PREDECREMENT_EXPR)
1694 abort ();
1695 #endif
1697 /* Prefix or postfix? */
1698 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1699 /* Faster to treat as prefix if result is not used. */
1700 postfix = want_value;
1701 else
1702 postfix = false;
1704 /* Add or subtract? */
1705 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1706 arith_code = PLUS_EXPR;
1707 else
1708 arith_code = MINUS_EXPR;
1710 /* Gimplify the LHS into a GIMPLE lvalue. */
1711 lvalue = TREE_OPERAND (*expr_p, 0);
1712 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1713 if (ret == GS_ERROR)
1714 return ret;
1716 /* Extract the operands to the arithmetic operation. */
1717 lhs = lvalue;
1718 rhs = TREE_OPERAND (*expr_p, 1);
1720 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1721 that as the result value and in the postqueue operation. */
1722 if (postfix)
1724 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1725 if (ret == GS_ERROR)
1726 return ret;
1729 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1730 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1732 if (postfix)
1734 gimplify_and_add (t1, post_p);
1735 *expr_p = lhs;
1736 return GS_ALL_DONE;
1738 else
1740 *expr_p = t1;
1741 return GS_OK;
1745 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1747 static void
1748 maybe_with_size_expr (tree *expr_p)
1750 tree expr = *expr_p;
1751 tree type = TREE_TYPE (expr);
1752 tree size;
1754 /* If we've already wrapped this or the type is error_mark_node, we can't do
1755 anything. */
1756 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1757 || type == error_mark_node)
1758 return;
1760 /* If the size isn't known or is a constant, we have nothing to do. */
1761 size = TYPE_SIZE_UNIT (type);
1762 if (!size || TREE_CODE (size) == INTEGER_CST)
1763 return;
1765 /* Otherwise, make a WITH_SIZE_EXPR. */
1766 size = unshare_expr (size);
1767 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1768 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1771 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1773 static enum gimplify_status
1774 gimplify_arg (tree *expr_p, tree *pre_p)
1776 bool (*test) (tree);
1777 fallback_t fb;
1779 /* In general, we allow lvalues for function arguments to avoid
1780 extra overhead of copying large aggregates out of even larger
1781 aggregates into temporaries only to copy the temporaries to
1782 the argument list. Make optimizers happy by pulling out to
1783 temporaries those types that fit in registers. */
1784 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
1785 test = is_gimple_val, fb = fb_rvalue;
1786 else
1787 test = is_gimple_lvalue, fb = fb_either;
1789 /* If this is a variable sized type, we must remember the size. */
1790 maybe_with_size_expr (expr_p);
1792 /* There is a sequence point before a function call. Side effects in
1793 the argument list must occur before the actual call. So, when
1794 gimplifying arguments, force gimplify_expr to use an internal
1795 post queue which is then appended to the end of PRE_P. */
1796 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
1799 /* Gimplify the CALL_EXPR node pointed by EXPR_P. PRE_P points to the
1800 list where side effects that must happen before *EXPR_P should be stored.
1801 WANT_VALUE is true if the result of the call is desired. */
1803 static enum gimplify_status
1804 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1806 tree decl;
1807 tree arglist;
1808 enum gimplify_status ret;
1810 #if defined ENABLE_CHECKING
1811 if (TREE_CODE (*expr_p) != CALL_EXPR)
1812 abort ();
1813 #endif
1815 /* For reliable diagnostics during inlining, it is necessary that
1816 every call_expr be annotated with file and line. */
1817 if (! EXPR_HAS_LOCATION (*expr_p))
1818 SET_EXPR_LOCATION (*expr_p, input_location);
1820 /* This may be a call to a builtin function.
1822 Builtin function calls may be transformed into different
1823 (and more efficient) builtin function calls under certain
1824 circumstances. Unfortunately, gimplification can muck things
1825 up enough that the builtin expanders are not aware that certain
1826 transformations are still valid.
1828 So we attempt transformation/gimplification of the call before
1829 we gimplify the CALL_EXPR. At this time we do not manage to
1830 transform all calls in the same manner as the expanders do, but
1831 we do transform most of them. */
1832 decl = get_callee_fndecl (*expr_p);
1833 if (decl && DECL_BUILT_IN (decl))
1835 tree new = simplify_builtin (*expr_p, !want_value);
1837 if (new && new != *expr_p)
1839 /* There was a transformation of this call which computes the
1840 same value, but in a more efficient way. Return and try
1841 again. */
1842 *expr_p = new;
1843 return GS_OK;
1846 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
1847 /* Avoid gimplifying the second argument to va_start, which needs
1848 to be the plain PARM_DECL. */
1849 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
1852 /* There is a sequence point before the call, so any side effects in
1853 the calling expression must occur before the actual call. Force
1854 gimplify_expr to use an internal post queue. */
1855 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1856 is_gimple_call_addr, fb_rvalue);
1858 if (PUSH_ARGS_REVERSED)
1859 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1860 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1861 arglist = TREE_CHAIN (arglist))
1863 enum gimplify_status t;
1865 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
1867 if (t == GS_ERROR)
1868 ret = GS_ERROR;
1870 if (PUSH_ARGS_REVERSED)
1871 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1873 /* Try this again in case gimplification exposed something. */
1874 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
1876 tree new = simplify_builtin (*expr_p, !want_value);
1878 if (new && new != *expr_p)
1880 /* There was a transformation of this call which computes the
1881 same value, but in a more efficient way. Return and try
1882 again. */
1883 *expr_p = new;
1884 return GS_OK;
1888 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1889 decl. This allows us to eliminate redundant or useless
1890 calls to "const" functions. */
1891 if (TREE_CODE (*expr_p) == CALL_EXPR
1892 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
1893 TREE_SIDE_EFFECTS (*expr_p) = 0;
1895 return ret;
1898 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
1899 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
1901 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
1902 condition is true or false, respectively. If null, we should generate
1903 our own to skip over the evaluation of this specific expression.
1905 This function is the tree equivalent of do_jump.
1907 shortcut_cond_r should only be called by shortcut_cond_expr. */
1909 static tree
1910 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
1912 tree local_label = NULL_TREE;
1913 tree t, expr = NULL;
1915 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
1916 retain the shortcut semantics. Just insert the gotos here;
1917 shortcut_cond_expr will append the real blocks later. */
1918 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1920 /* Turn if (a && b) into
1922 if (a); else goto no;
1923 if (b) goto yes; else goto no;
1924 (no:) */
1926 if (false_label_p == NULL)
1927 false_label_p = &local_label;
1929 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
1930 append_to_statement_list (t, &expr);
1932 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1933 false_label_p);
1934 append_to_statement_list (t, &expr);
1936 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1938 /* Turn if (a || b) into
1940 if (a) goto yes;
1941 if (b) goto yes; else goto no;
1942 (yes:) */
1944 if (true_label_p == NULL)
1945 true_label_p = &local_label;
1947 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
1948 append_to_statement_list (t, &expr);
1950 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1951 false_label_p);
1952 append_to_statement_list (t, &expr);
1954 else if (TREE_CODE (pred) == COND_EXPR)
1956 /* As long as we're messing with gotos, turn if (a ? b : c) into
1957 if (a)
1958 if (b) goto yes; else goto no;
1959 else
1960 if (c) goto yes; else goto no; */
1961 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
1962 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1963 false_label_p),
1964 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
1965 false_label_p));
1967 else
1969 expr = build (COND_EXPR, void_type_node, pred,
1970 build_and_jump (true_label_p),
1971 build_and_jump (false_label_p));
1974 if (local_label)
1976 t = build1 (LABEL_EXPR, void_type_node, local_label);
1977 append_to_statement_list (t, &expr);
1980 return expr;
1983 static tree
1984 shortcut_cond_expr (tree expr)
1986 tree pred = TREE_OPERAND (expr, 0);
1987 tree then_ = TREE_OPERAND (expr, 1);
1988 tree else_ = TREE_OPERAND (expr, 2);
1989 tree true_label, false_label, end_label, t;
1990 tree *true_label_p;
1991 tree *false_label_p;
1992 bool emit_end, emit_false;
1993 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
1994 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
1996 /* First do simple transformations. */
1997 if (!else_se)
1999 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2000 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2002 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2003 then_ = shortcut_cond_expr (expr);
2004 pred = TREE_OPERAND (pred, 0);
2005 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2008 if (!then_se)
2010 /* If there is no 'then', turn
2011 if (a || b); else d
2012 into
2013 if (a); else if (b); else d. */
2014 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2016 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2017 else_ = shortcut_cond_expr (expr);
2018 pred = TREE_OPERAND (pred, 0);
2019 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2023 /* If we're done, great. */
2024 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2025 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2026 return expr;
2028 /* Otherwise we need to mess with gotos. Change
2029 if (a) c; else d;
2031 if (a); else goto no;
2032 c; goto end;
2033 no: d; end:
2034 and recursively gimplify the condition. */
2036 true_label = false_label = end_label = NULL_TREE;
2038 /* If our arms just jump somewhere, hijack those labels so we don't
2039 generate jumps to jumps. */
2041 if (then_
2042 && TREE_CODE (then_) == GOTO_EXPR
2043 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2045 true_label = GOTO_DESTINATION (then_);
2046 then_ = NULL;
2047 then_se = false;
2050 if (else_
2051 && TREE_CODE (else_) == GOTO_EXPR
2052 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2054 false_label = GOTO_DESTINATION (else_);
2055 else_ = NULL;
2056 else_se = false;
2059 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2060 if (true_label)
2061 true_label_p = &true_label;
2062 else
2063 true_label_p = NULL;
2065 /* The 'else' branch also needs a label if it contains interesting code. */
2066 if (false_label || else_se)
2067 false_label_p = &false_label;
2068 else
2069 false_label_p = NULL;
2071 /* If there was nothing else in our arms, just forward the label(s). */
2072 if (!then_se && !else_se)
2073 return shortcut_cond_r (pred, true_label_p, false_label_p);
2075 /* If our last subexpression already has a terminal label, reuse it. */
2076 if (else_se)
2077 expr = expr_last (else_);
2078 else if (then_se)
2079 expr = expr_last (then_);
2080 else
2081 expr = NULL;
2082 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2083 end_label = LABEL_EXPR_LABEL (expr);
2085 /* If we don't care about jumping to the 'else' branch, jump to the end
2086 if the condition is false. */
2087 if (!false_label_p)
2088 false_label_p = &end_label;
2090 /* We only want to emit these labels if we aren't hijacking them. */
2091 emit_end = (end_label == NULL_TREE);
2092 emit_false = (false_label == NULL_TREE);
2094 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2096 expr = NULL;
2097 append_to_statement_list (pred, &expr);
2099 append_to_statement_list (then_, &expr);
2100 if (else_se)
2102 t = build_and_jump (&end_label);
2103 append_to_statement_list (t, &expr);
2104 if (emit_false)
2106 t = build1 (LABEL_EXPR, void_type_node, false_label);
2107 append_to_statement_list (t, &expr);
2109 append_to_statement_list (else_, &expr);
2111 if (emit_end && end_label)
2113 t = build1 (LABEL_EXPR, void_type_node, end_label);
2114 append_to_statement_list (t, &expr);
2117 return expr;
2120 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2122 static tree
2123 gimple_boolify (tree expr)
2125 tree type = TREE_TYPE (expr);
2127 if (TREE_CODE (type) == BOOLEAN_TYPE)
2128 return expr;
2130 /* If this is the predicate of a COND_EXPR, it might not even be a
2131 truthvalue yet. */
2132 expr = lang_hooks.truthvalue_conversion (expr);
2134 switch (TREE_CODE (expr))
2136 case TRUTH_AND_EXPR:
2137 case TRUTH_OR_EXPR:
2138 case TRUTH_XOR_EXPR:
2139 case TRUTH_ANDIF_EXPR:
2140 case TRUTH_ORIF_EXPR:
2141 /* Also boolify the arguments of truth exprs. */
2142 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2143 /* FALLTHRU */
2145 case TRUTH_NOT_EXPR:
2146 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2147 /* FALLTHRU */
2149 case EQ_EXPR: case NE_EXPR:
2150 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2151 /* These expressions always produce boolean results. */
2152 TREE_TYPE (expr) = boolean_type_node;
2153 return expr;
2155 default:
2156 /* Other expressions that get here must have boolean values, but
2157 might need to be converted to the appropriate mode. */
2158 return convert (boolean_type_node, expr);
2162 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2163 into
2165 if (p) if (p)
2166 t1 = a; a;
2167 else or else
2168 t1 = b; b;
2171 The second form is used when *EXPR_P is of type void.
2173 TARGET is the tree for T1 above.
2175 PRE_P points to the list where side effects that must happen before
2176 *EXPR_P should be stored. */
2178 static enum gimplify_status
2179 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2181 tree expr = *expr_p;
2182 tree tmp, tmp2, type;
2183 enum gimplify_status ret;
2185 type = TREE_TYPE (expr);
2186 if (!type)
2187 TREE_TYPE (expr) = void_type_node;
2189 /* If this COND_EXPR has a value, copy the values into a temporary within
2190 the arms. */
2191 else if (! VOID_TYPE_P (type))
2193 if (target)
2195 ret = gimplify_expr (&target, pre_p, NULL,
2196 is_gimple_min_lval, fb_lvalue);
2197 if (ret != GS_ERROR)
2198 ret = GS_OK;
2199 tmp = target;
2200 tmp2 = unshare_expr (target);
2202 else
2204 tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2205 ret = GS_ALL_DONE;
2208 /* Build the then clause, 't1 = a;'. But don't build an assignment
2209 if this branch is void; in C++ it can be, if it's a throw. */
2210 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2211 TREE_OPERAND (expr, 1)
2212 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2214 /* Build the else clause, 't1 = b;'. */
2215 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2216 TREE_OPERAND (expr, 2)
2217 = build (MODIFY_EXPR, void_type_node, tmp2, TREE_OPERAND (expr, 2));
2219 TREE_TYPE (expr) = void_type_node;
2220 recalculate_side_effects (expr);
2222 /* Move the COND_EXPR to the prequeue. */
2223 gimplify_and_add (expr, pre_p);
2225 *expr_p = tmp;
2226 return ret;
2229 /* Make sure the condition has BOOLEAN_TYPE. */
2230 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2232 /* Break apart && and || conditions. */
2233 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2234 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2236 expr = shortcut_cond_expr (expr);
2238 if (expr != *expr_p)
2240 *expr_p = expr;
2242 /* We can't rely on gimplify_expr to re-gimplify the expanded
2243 form properly, as cleanups might cause the target labels to be
2244 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2245 set up a conditional context. */
2246 gimple_push_condition ();
2247 gimplify_stmt (expr_p);
2248 gimple_pop_condition (pre_p);
2250 return GS_ALL_DONE;
2254 /* Now do the normal gimplification. */
2255 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2256 is_gimple_condexpr, fb_rvalue);
2258 gimple_push_condition ();
2260 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2261 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2262 recalculate_side_effects (expr);
2264 gimple_pop_condition (pre_p);
2266 if (ret == GS_ERROR)
2268 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2269 ret = GS_ALL_DONE;
2270 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2271 /* Rewrite "if (a); else b" to "if (!a) b" */
2273 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2274 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2275 is_gimple_condexpr, fb_rvalue);
2277 tmp = TREE_OPERAND (expr, 1);
2278 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2279 TREE_OPERAND (expr, 2) = tmp;
2281 else
2282 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2283 expr = TREE_OPERAND (expr, 0);
2285 *expr_p = expr;
2286 return ret;
2289 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2290 a call to __builtin_memcpy. */
2292 static enum gimplify_status
2293 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2295 tree args, t, to, to_ptr, from;
2297 to = TREE_OPERAND (*expr_p, 0);
2298 from = TREE_OPERAND (*expr_p, 1);
2300 args = tree_cons (NULL, size, NULL);
2302 t = build_fold_addr_expr (from);
2303 args = tree_cons (NULL, t, args);
2305 to_ptr = build_fold_addr_expr (to);
2306 args = tree_cons (NULL, to_ptr, args);
2307 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2308 t = build_function_call_expr (t, args);
2310 if (want_value)
2312 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2313 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2316 *expr_p = t;
2317 return GS_OK;
2320 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2321 a call to __builtin_memset. In this case we know that the RHS is
2322 a CONSTRUCTOR with an empty element list. */
2324 static enum gimplify_status
2325 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2327 tree args, t, to, to_ptr;
2329 to = TREE_OPERAND (*expr_p, 0);
2331 args = tree_cons (NULL, size, NULL);
2333 args = tree_cons (NULL, integer_zero_node, args);
2335 to_ptr = build_fold_addr_expr (to);
2336 args = tree_cons (NULL, to_ptr, args);
2337 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2338 t = build_function_call_expr (t, args);
2340 if (want_value)
2342 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2343 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2346 *expr_p = t;
2347 return GS_OK;
2350 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2351 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2352 assignment. Returns non-null if we detect a potential overlap. */
2354 struct gimplify_init_ctor_preeval_data
2356 /* The base decl of the lhs object. May be NULL, in which case we
2357 have to assume the lhs is indirect. */
2358 tree lhs_base_decl;
2360 /* The alias set of the lhs object. */
2361 int lhs_alias_set;
2364 static tree
2365 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2367 struct gimplify_init_ctor_preeval_data *data
2368 = (struct gimplify_init_ctor_preeval_data *) xdata;
2369 tree t = *tp;
2371 /* If we find the base object, obviously we have overlap. */
2372 if (data->lhs_base_decl == t)
2373 return t;
2375 /* If the constructor component is indirect, determine if we have a
2376 potential overlap with the lhs. The only bits of information we
2377 have to go on at this point are addressability and alias sets. */
2378 if (TREE_CODE (t) == INDIRECT_REF
2379 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2380 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2381 return t;
2383 if (DECL_P (t) || TYPE_P (t))
2384 *walk_subtrees = 0;
2385 return NULL;
2388 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2389 force values that overlap with the lhs (as described by *DATA)
2390 into temporaries. */
2392 static void
2393 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2394 struct gimplify_init_ctor_preeval_data *data)
2396 enum gimplify_status one;
2398 /* If the value is invariant, then there's nothing to pre-evaluate.
2399 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2400 invariant but has side effects and might contain a reference to
2401 the object we're initializing. */
2402 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2403 return;
2405 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2406 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2407 return;
2409 /* Recurse for nested constructors. */
2410 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2412 tree list;
2413 for (list = CONSTRUCTOR_ELTS (*expr_p); list ; list = TREE_CHAIN (list))
2414 gimplify_init_ctor_preeval (&TREE_VALUE (list), pre_p, post_p, data);
2415 return;
2418 /* We can't preevaluate if the type contains a placeholder. */
2419 if (type_contains_placeholder_p (TREE_TYPE (*expr_p)))
2420 return;
2422 /* Gimplify the constructor element to something appropriate for the rhs
2423 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2424 the gimplifier will consider this a store to memory. Doing this
2425 gimplification now means that we won't have to deal with complicated
2426 language-specific trees, nor trees like SAVE_EXPR that can induce
2427 exponential search behaviour. */
2428 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2429 if (one == GS_ERROR)
2431 *expr_p = NULL;
2432 return;
2435 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2436 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2437 always be true for all scalars, since is_gimple_mem_rhs insists on a
2438 temporary variable for them. */
2439 if (DECL_P (*expr_p))
2440 return;
2442 /* If this is of variable size, we have no choice but to assume it doesn't
2443 overlap since we can't make a temporary for it. */
2444 if (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p))))
2445 return;
2447 /* Otherwise, we must search for overlap ... */
2448 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2449 return;
2451 /* ... and if found, force the value into a temporary. */
2452 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2455 /* A subroutine of gimplify_init_constructor. Generate individual
2456 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2457 assignments should happen. LIST is the CONSTRUCTOR_ELTS of the
2458 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2459 zeroed first. */
2461 static void
2462 gimplify_init_ctor_eval (tree object, tree list, tree *pre_p, bool cleared)
2464 tree array_elt_type = NULL;
2466 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2467 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2469 for (; list; list = TREE_CHAIN (list))
2471 tree purpose, value, cref, init;
2473 purpose = TREE_PURPOSE (list);
2474 value = TREE_VALUE (list);
2476 /* NULL values are created above for gimplification errors. */
2477 if (value == NULL)
2478 continue;
2480 if (cleared && initializer_zerop (value))
2481 continue;
2483 if (array_elt_type)
2485 /* ??? Here's to hoping the front end fills in all of the indicies,
2486 so we don't have to figure out what's missing ourselves. */
2487 if (!purpose)
2488 abort ();
2489 /* ??? Need to handle this. */
2490 if (TREE_CODE (purpose) == RANGE_EXPR)
2491 abort ();
2493 cref = build (ARRAY_REF, array_elt_type, unshare_expr (object),
2494 purpose, NULL_TREE, NULL_TREE);
2496 else
2497 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
2498 unshare_expr (object), purpose, NULL_TREE);
2500 if (TREE_CODE (value) == CONSTRUCTOR)
2501 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2502 pre_p, cleared);
2503 else
2505 init = build (MODIFY_EXPR, TREE_TYPE (cref), cref, value);
2506 gimplify_and_add (init, pre_p);
2511 /* A subroutine of gimplify_modify_expr. Break out elements of a
2512 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2514 Note that we still need to clear any elements that don't have explicit
2515 initializers, so if not all elements are initialized we keep the
2516 original MODIFY_EXPR, we just remove all of the constructor elements. */
2518 static enum gimplify_status
2519 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2520 tree *post_p, bool want_value)
2522 tree object;
2523 tree ctor = TREE_OPERAND (*expr_p, 1);
2524 tree type = TREE_TYPE (ctor);
2525 enum gimplify_status ret;
2526 tree elt_list;
2528 if (TREE_CODE (ctor) != CONSTRUCTOR)
2529 return GS_UNHANDLED;
2531 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
2532 is_gimple_lvalue, fb_lvalue);
2533 if (ret == GS_ERROR)
2534 return ret;
2535 object = TREE_OPERAND (*expr_p, 0);
2537 elt_list = CONSTRUCTOR_ELTS (ctor);
2539 ret = GS_ALL_DONE;
2540 switch (TREE_CODE (type))
2542 case RECORD_TYPE:
2543 case UNION_TYPE:
2544 case QUAL_UNION_TYPE:
2545 case ARRAY_TYPE:
2547 struct gimplify_init_ctor_preeval_data preeval_data;
2548 HOST_WIDE_INT num_elements, num_nonzero_elements;
2549 HOST_WIDE_INT num_nonconstant_elements;
2550 bool cleared;
2552 /* Aggregate types must lower constructors to initialization of
2553 individual elements. The exception is that a CONSTRUCTOR node
2554 with no elements indicates zero-initialization of the whole. */
2555 if (elt_list == NULL)
2556 break;
2558 categorize_ctor_elements (ctor, &num_nonzero_elements,
2559 &num_nonconstant_elements);
2561 /* If a const aggregate variable is being initialized, then it
2562 should never be a lose to promote the variable to be static. */
2563 if (num_nonconstant_elements == 0
2564 && TREE_READONLY (object)
2565 && TREE_CODE (object) == VAR_DECL)
2567 DECL_INITIAL (object) = ctor;
2568 TREE_STATIC (object) = 1;
2569 if (!DECL_NAME (object))
2570 DECL_NAME (object) = create_tmp_var_name ("C");
2571 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2573 /* ??? C++ doesn't automatically append a .<number> to the
2574 assembler name, and even when it does, it looks a FE private
2575 data structures to figure out what that number should be,
2576 which are not set for this variable. I suppose this is
2577 important for local statics for inline functions, which aren't
2578 "local" in the object file sense. So in order to get a unique
2579 TU-local symbol, we must invoke the lhd version now. */
2580 lhd_set_decl_assembler_name (object);
2582 *expr_p = NULL_TREE;
2583 break;
2586 /* If there are "lots" of initialized elements, and all of them
2587 are valid address constants, then the entire initializer can
2588 be dropped to memory, and then memcpy'd out. */
2589 if (num_nonconstant_elements == 0)
2591 HOST_WIDE_INT size = int_size_in_bytes (type);
2592 unsigned int align;
2594 /* ??? We can still get unbounded array types, at least
2595 from the C++ front end. This seems wrong, but attempt
2596 to work around it for now. */
2597 if (size < 0)
2599 size = int_size_in_bytes (TREE_TYPE (object));
2600 if (size >= 0)
2601 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2604 /* Find the maximum alignment we can assume for the object. */
2605 /* ??? Make use of DECL_OFFSET_ALIGN. */
2606 if (DECL_P (object))
2607 align = DECL_ALIGN (object);
2608 else
2609 align = TYPE_ALIGN (type);
2611 if (size > 0 && !can_move_by_pieces (size, align))
2613 tree new = create_tmp_var_raw (type, "C");
2615 gimple_add_tmp_var (new);
2616 TREE_STATIC (new) = 1;
2617 TREE_READONLY (new) = 1;
2618 DECL_INITIAL (new) = ctor;
2619 if (align > DECL_ALIGN (new))
2621 DECL_ALIGN (new) = align;
2622 DECL_USER_ALIGN (new) = 1;
2624 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2626 TREE_OPERAND (*expr_p, 1) = new;
2628 /* This is no longer an assignment of a CONSTRUCTOR, but
2629 we still may have processing to do on the LHS. So
2630 pretend we didn't do anything here to let that happen. */
2631 return GS_UNHANDLED;
2635 /* If there are "lots" of initialized elements, even discounting
2636 those that are not address constants (and thus *must* be
2637 computed at runtime), then partition the constructor into
2638 constant and non-constant parts. Block copy the constant
2639 parts in, then generate code for the non-constant parts. */
2640 /* TODO. There's code in cp/typeck.c to do this. */
2642 num_elements = count_type_elements (TREE_TYPE (ctor));
2644 /* If there are "lots" of zeros, then block clear the object first. */
2645 cleared = false;
2646 if (num_elements - num_nonzero_elements > CLEAR_RATIO
2647 && num_nonzero_elements < num_elements/4)
2648 cleared = true;
2650 /* ??? This bit ought not be needed. For any element not present
2651 in the initializer, we should simply set them to zero. Except
2652 we'd need to *find* the elements that are not present, and that
2653 requires trickery to avoid quadratic compile-time behavior in
2654 large cases or excessive memory use in small cases. */
2655 else
2657 HOST_WIDE_INT len = list_length (elt_list);
2658 if (TREE_CODE (type) == ARRAY_TYPE)
2660 tree nelts = array_type_nelts (type);
2661 if (!host_integerp (nelts, 1)
2662 || tree_low_cst (nelts, 1) + 1 != len)
2663 cleared = true;
2665 else if (len != fields_length (type))
2666 cleared = true;
2669 if (cleared)
2671 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2672 Note that we still have to gimplify, in order to handle the
2673 case of variable sized types. Avoid shared tree structures. */
2674 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
2675 object = unshare_expr (object);
2676 gimplify_stmt (expr_p);
2677 append_to_statement_list (*expr_p, pre_p);
2680 preeval_data.lhs_base_decl = get_base_address (object);
2681 if (!DECL_P (preeval_data.lhs_base_decl))
2682 preeval_data.lhs_base_decl = NULL;
2683 preeval_data.lhs_alias_set = get_alias_set (object);
2685 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
2686 pre_p, post_p, &preeval_data);
2687 gimplify_init_ctor_eval (object, elt_list, pre_p, cleared);
2689 *expr_p = NULL_TREE;
2691 break;
2693 case COMPLEX_TYPE:
2695 tree r, i;
2697 /* Extract the real and imaginary parts out of the ctor. */
2698 r = i = NULL_TREE;
2699 if (elt_list)
2701 r = TREE_VALUE (elt_list);
2702 elt_list = TREE_CHAIN (elt_list);
2703 if (elt_list)
2705 i = TREE_VALUE (elt_list);
2706 if (TREE_CHAIN (elt_list))
2707 abort ();
2710 if (r == NULL || i == NULL)
2712 tree zero = convert (TREE_TYPE (type), integer_zero_node);
2713 if (r == NULL)
2714 r = zero;
2715 if (i == NULL)
2716 i = zero;
2719 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
2720 represent creation of a complex value. */
2721 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
2723 ctor = build_complex (type, r, i);
2724 TREE_OPERAND (*expr_p, 1) = ctor;
2726 else
2728 ctor = build (COMPLEX_EXPR, type, r, i);
2729 TREE_OPERAND (*expr_p, 1) = ctor;
2730 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
2731 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
2732 fb_rvalue);
2735 break;
2737 case VECTOR_TYPE:
2738 /* Go ahead and simplify constant constructors to VECTOR_CST. */
2739 if (TREE_CONSTANT (ctor))
2740 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
2741 else
2743 /* Vector types use CONSTRUCTOR all the way through gimple
2744 compilation as a general initializer. */
2745 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
2747 enum gimplify_status tret;
2748 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
2749 is_gimple_val, fb_rvalue);
2750 if (tret == GS_ERROR)
2751 ret = GS_ERROR;
2754 break;
2756 default:
2757 /* So how did we get a CONSTRUCTOR for a scalar type? */
2758 abort ();
2761 if (ret == GS_ERROR)
2762 return GS_ERROR;
2763 else if (want_value)
2765 append_to_statement_list (*expr_p, pre_p);
2766 *expr_p = object;
2767 return GS_OK;
2769 else
2770 return GS_ALL_DONE;
2773 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
2774 based on the code of the RHS. We loop for as long as something changes. */
2776 static enum gimplify_status
2777 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2778 tree *post_p, bool want_value)
2780 enum gimplify_status ret = GS_OK;
2782 while (ret != GS_UNHANDLED)
2783 switch (TREE_CODE (*from_p))
2785 case TARGET_EXPR:
2787 /* If we are initializing something from a TARGET_EXPR, strip the
2788 TARGET_EXPR and initialize it directly, if possible. This can't
2789 be done if the initializer is void, since that implies that the
2790 temporary is set in some non-trivial way.
2792 ??? What about code that pulls out the temp and uses it
2793 elsewhere? I think that such code never uses the TARGET_EXPR as
2794 an initializer. If I'm wrong, we'll abort because the temp won't
2795 have any RTL. In that case, I guess we'll need to replace
2796 references somehow. */
2797 tree init = TARGET_EXPR_INITIAL (*from_p);
2799 if (!VOID_TYPE_P (TREE_TYPE (init)))
2801 *from_p = init;
2802 ret = GS_OK;
2804 else
2805 ret = GS_UNHANDLED;
2807 break;
2809 case COMPOUND_EXPR:
2810 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2811 caught. */
2812 gimplify_compound_expr (from_p, pre_p, true);
2813 ret = GS_OK;
2814 break;
2816 case CONSTRUCTOR:
2817 /* If we're initializing from a CONSTRUCTOR, break this into
2818 individual MODIFY_EXPRs. */
2819 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2821 case COND_EXPR:
2822 /* If we're assigning to a non-register type, push the assignment
2823 down into the branches. This is mandatory for ADDRESSABLE types,
2824 since we cannot generate temporaries for such, but it saves a
2825 copy in other cases as well. */
2826 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
2828 *expr_p = *from_p;
2829 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2831 else
2832 ret = GS_UNHANDLED;
2833 break;
2835 default:
2836 ret = GS_UNHANDLED;
2837 break;
2840 return ret;
2843 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2845 modify_expr
2846 : varname '=' rhs
2847 | '*' ID '=' rhs
2849 PRE_P points to the list where side effects that must happen before
2850 *EXPR_P should be stored.
2852 POST_P points to the list where side effects that must happen after
2853 *EXPR_P should be stored.
2855 WANT_VALUE is nonzero iff we want to use the value of this expression
2856 in another expression. */
2858 static enum gimplify_status
2859 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2861 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2862 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2863 enum gimplify_status ret = GS_UNHANDLED;
2865 #if defined ENABLE_CHECKING
2866 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2867 abort ();
2868 #endif
2870 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2871 if (TREE_CODE (*expr_p) == INIT_EXPR)
2872 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2874 /* See if any simplifications can be done based on what the RHS is. */
2875 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2876 want_value);
2877 if (ret != GS_UNHANDLED)
2878 return ret;
2880 /* If the value being copied is of variable width, compute the length
2881 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
2882 before gimplifying any of the operands so that we can resolve any
2883 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
2884 the size of the expression to be copied, not of the destination, so
2885 that is what we must here. */
2886 maybe_with_size_expr (from_p);
2888 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2889 if (ret == GS_ERROR)
2890 return ret;
2892 ret = gimplify_expr (from_p, pre_p, post_p,
2893 rhs_predicate_for (*to_p), fb_rvalue);
2894 if (ret == GS_ERROR)
2895 return ret;
2897 /* Now see if the above changed *from_p to something we handle specially. */
2898 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2899 want_value);
2900 if (ret != GS_UNHANDLED)
2901 return ret;
2903 /* If we've got a variable sized assignment between two lvalues (i.e. does
2904 not involve a call), then we can make things a bit more straightforward
2905 by converting the assignment to memcpy or memset. */
2906 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
2908 tree from = TREE_OPERAND (*from_p, 0);
2909 tree size = TREE_OPERAND (*from_p, 1);
2911 if (TREE_CODE (from) == CONSTRUCTOR)
2912 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
2913 if (is_gimple_addressable (from))
2915 *from_p = from;
2916 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
2920 if (want_value)
2922 append_to_statement_list (*expr_p, pre_p);
2923 *expr_p = *to_p;
2924 return GS_OK;
2927 return GS_ALL_DONE;
2930 /* Gimplify a comparison between two variable-sized objects. Do this
2931 with a call to BUILT_IN_MEMCMP. */
2933 static enum gimplify_status
2934 gimplify_variable_sized_compare (tree *expr_p)
2936 tree op0 = TREE_OPERAND (*expr_p, 0);
2937 tree op1 = TREE_OPERAND (*expr_p, 1);
2938 tree args, t, dest;
2940 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2941 t = unshare_expr (t);
2942 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2943 args = tree_cons (NULL, t, NULL);
2944 t = build_fold_addr_expr (op1);
2945 args = tree_cons (NULL, t, args);
2946 dest = build_fold_addr_expr (op0);
2947 args = tree_cons (NULL, dest, args);
2948 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2949 t = build_function_call_expr (t, args);
2950 *expr_p
2951 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2953 return GS_OK;
2956 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2957 points to the expression to gimplify.
2959 Expressions of the form 'a && b' are gimplified to:
2961 a && b ? true : false
2963 gimplify_cond_expr will do the rest.
2965 PRE_P points to the list where side effects that must happen before
2966 *EXPR_P should be stored. */
2968 static enum gimplify_status
2969 gimplify_boolean_expr (tree *expr_p)
2971 /* Preserve the original type of the expression. */
2972 tree type = TREE_TYPE (*expr_p);
2974 *expr_p = build (COND_EXPR, type, *expr_p,
2975 convert (type, boolean_true_node),
2976 convert (type, boolean_false_node));
2978 return GS_OK;
2981 /* Gimplifies an expression sequence. This function gimplifies each
2982 expression and re-writes the original expression with the last
2983 expression of the sequence in GIMPLE form.
2985 PRE_P points to the list where the side effects for all the
2986 expressions in the sequence will be emitted.
2988 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2989 /* ??? Should rearrange to share the pre-queue with all the indirect
2990 invocations of gimplify_expr. Would probably save on creations
2991 of statement_list nodes. */
2993 static enum gimplify_status
2994 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2996 tree t = *expr_p;
3000 tree *sub_p = &TREE_OPERAND (t, 0);
3002 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3003 gimplify_compound_expr (sub_p, pre_p, false);
3004 else
3005 gimplify_stmt (sub_p);
3006 append_to_statement_list (*sub_p, pre_p);
3008 t = TREE_OPERAND (t, 1);
3010 while (TREE_CODE (t) == COMPOUND_EXPR);
3012 *expr_p = t;
3013 if (want_value)
3014 return GS_OK;
3015 else
3017 gimplify_stmt (expr_p);
3018 return GS_ALL_DONE;
3022 /* Gimplifies a statement list. These may be created either by an
3023 enlightened front-end, or by shortcut_cond_expr. */
3025 static enum gimplify_status
3026 gimplify_statement_list (tree *expr_p)
3028 tree_stmt_iterator i = tsi_start (*expr_p);
3030 while (!tsi_end_p (i))
3032 tree t;
3034 gimplify_stmt (tsi_stmt_ptr (i));
3036 t = tsi_stmt (i);
3037 if (t == NULL)
3038 tsi_delink (&i);
3039 else if (TREE_CODE (t) == STATEMENT_LIST)
3041 tsi_link_before (&i, t, TSI_SAME_STMT);
3042 tsi_delink (&i);
3044 else
3045 tsi_next (&i);
3048 return GS_ALL_DONE;
3051 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
3052 gimplify. After gimplification, EXPR_P will point to a new temporary
3053 that holds the original value of the SAVE_EXPR node.
3055 PRE_P points to the list where side effects that must happen before
3056 *EXPR_P should be stored. */
3058 static enum gimplify_status
3059 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3061 enum gimplify_status ret = GS_ALL_DONE;
3062 tree val;
3064 #if defined ENABLE_CHECKING
3065 if (TREE_CODE (*expr_p) != SAVE_EXPR)
3066 abort ();
3067 #endif
3069 val = TREE_OPERAND (*expr_p, 0);
3071 /* If the operand is already a GIMPLE temporary, just re-write the
3072 SAVE_EXPR node. */
3073 if (TREE_CODE (val) == VAR_DECL && DECL_GIMPLE_FORMAL_TEMP_P (val))
3074 *expr_p = val;
3075 /* The operand may be a void-valued expression such as SAVE_EXPRs
3076 generated by the Java frontend for class initialization. It is
3077 being executed only for its side-effects. */
3078 else if (TREE_TYPE (val) == void_type_node)
3080 tree body = TREE_OPERAND (*expr_p, 0);
3081 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
3082 append_to_statement_list (body, pre_p);
3083 *expr_p = NULL;
3085 else
3087 val = get_initialized_tmp_var (val, pre_p, post_p);
3088 DECL_GIMPLE_FORMAL_TEMP_P (val) = 1;
3089 *expr_p = TREE_OPERAND (*expr_p, 0) = val;
3092 return ret;
3095 /* Re-write the ADDR_EXPR node pointed by EXPR_P
3097 unary_expr
3098 : ...
3099 | '&' varname
3102 PRE_P points to the list where side effects that must happen before
3103 *EXPR_P should be stored.
3105 POST_P points to the list where side effects that must happen after
3106 *EXPR_P should be stored. */
3108 static enum gimplify_status
3109 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3111 tree expr = *expr_p;
3112 tree op0 = TREE_OPERAND (expr, 0);
3113 enum gimplify_status ret;
3115 switch (TREE_CODE (op0))
3117 case INDIRECT_REF:
3118 /* Check if we are dealing with an expression of the form '&*ptr'.
3119 While the front end folds away '&*ptr' into 'ptr', these
3120 expressions may be generated internally by the compiler (e.g.,
3121 builtins like __builtin_va_end). */
3122 *expr_p = TREE_OPERAND (op0, 0);
3123 ret = GS_OK;
3124 break;
3126 case ARRAY_REF:
3127 /* Fold &a[6] to (&a + 6). */
3128 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
3129 pre_p, post_p);
3131 /* This added an INDIRECT_REF. Fold it away. */
3132 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3133 break;
3135 case VIEW_CONVERT_EXPR:
3136 /* Take the address of our operand and then convert it to the type of
3137 this ADDR_EXPR.
3139 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3140 all clear. The impact of this transformation is even less clear. */
3141 *expr_p = fold_convert (TREE_TYPE (expr),
3142 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3143 ret = GS_OK;
3144 break;
3146 default:
3147 /* We use fb_either here because the C frontend sometimes takes
3148 the address of a call that returns a struct; see
3149 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3150 the implied temporary explicit. */
3151 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3152 is_gimple_addressable, fb_either);
3153 if (ret != GS_ERROR)
3155 /* The above may have made an INDIRECT_REF (e.g, Ada's NULL_EXPR),
3156 so check for it here. It's not worth checking for the other
3157 cases above. */
3158 if (TREE_CODE (TREE_OPERAND (expr, 0)) == INDIRECT_REF)
3160 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3161 break;
3164 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3165 is set properly. */
3166 recompute_tree_invarant_for_addr_expr (expr);
3168 /* Mark the RHS addressable. */
3169 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3171 break;
3174 return ret;
3177 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3178 value; output operands should be a gimple lvalue. */
3180 static enum gimplify_status
3181 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3183 tree expr = *expr_p;
3184 int noutputs = list_length (ASM_OUTPUTS (expr));
3185 const char **oconstraints
3186 = (const char **) alloca ((noutputs) * sizeof (const char *));
3187 int i;
3188 tree link;
3189 const char *constraint;
3190 bool allows_mem, allows_reg, is_inout;
3191 enum gimplify_status ret, tret;
3193 ASM_STRING (expr)
3194 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
3195 ASM_INPUTS (expr));
3197 ret = GS_ALL_DONE;
3198 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3200 oconstraints[i] = constraint
3201 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3203 parse_output_constraint (&constraint, i, 0, 0,
3204 &allows_mem, &allows_reg, &is_inout);
3206 if (!allows_reg && allows_mem)
3207 lang_hooks.mark_addressable (TREE_VALUE (link));
3209 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3210 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3211 fb_lvalue | fb_mayfail);
3212 if (tret == GS_ERROR)
3214 error ("invalid lvalue in asm output %d", i);
3215 ret = tret;
3218 if (is_inout)
3220 /* An input/output operand. To give the optimizers more
3221 flexibility, split it into separate input and output
3222 operands. */
3223 tree input;
3224 char buf[10];
3225 size_t constraint_len = strlen (constraint);
3227 /* Turn the in/out constraint into an output constraint. */
3228 char *p = xstrdup (constraint);
3229 p[0] = '=';
3230 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3231 free (p);
3233 /* And add a matching input constraint. */
3234 if (allows_reg)
3236 sprintf (buf, "%d", i);
3237 input = build_string (strlen (buf), buf);
3239 else
3240 input = build_string (constraint_len - 1, constraint + 1);
3241 input = build_tree_list (build_tree_list (NULL_TREE, input),
3242 unshare_expr (TREE_VALUE (link)));
3243 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3247 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3249 constraint
3250 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3251 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3252 oconstraints, &allows_mem, &allows_reg);
3254 /* If the operand is a memory input, it should be an lvalue. */
3255 if (!allows_reg && allows_mem)
3257 lang_hooks.mark_addressable (TREE_VALUE (link));
3258 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3259 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3260 if (tret == GS_ERROR)
3262 error ("memory input %d is not directly addressable", i);
3263 ret = tret;
3266 else
3268 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3269 is_gimple_val, fb_rvalue);
3270 if (tret == GS_ERROR)
3271 ret = tret;
3275 return ret;
3278 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3279 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3280 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3281 return to this function.
3283 FIXME should we complexify the prequeue handling instead? Or use flags
3284 for all the cleanups and let the optimizer tighten them up? The current
3285 code seems pretty fragile; it will break on a cleanup within any
3286 non-conditional nesting. But any such nesting would be broken, anyway;
3287 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3288 and continues out of it. We can do that at the RTL level, though, so
3289 having an optimizer to tighten up try/finally regions would be a Good
3290 Thing. */
3292 static enum gimplify_status
3293 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3295 tree_stmt_iterator iter;
3296 tree body;
3298 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3300 /* We only care about the number of conditions between the innermost
3301 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3302 int old_conds = gimplify_ctxp->conditions;
3303 gimplify_ctxp->conditions = 0;
3305 body = TREE_OPERAND (*expr_p, 0);
3306 gimplify_to_stmt_list (&body);
3308 gimplify_ctxp->conditions = old_conds;
3310 for (iter = tsi_start (body); !tsi_end_p (iter); )
3312 tree *wce_p = tsi_stmt_ptr (iter);
3313 tree wce = *wce_p;
3315 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3317 if (tsi_one_before_end_p (iter))
3319 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
3320 tsi_delink (&iter);
3321 break;
3323 else
3325 tree sl, tfe;
3327 sl = tsi_split_statement_list_after (&iter);
3328 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
3329 append_to_statement_list (TREE_OPERAND (wce, 0),
3330 &TREE_OPERAND (tfe, 1));
3331 *wce_p = tfe;
3332 iter = tsi_start (sl);
3335 else
3336 tsi_next (&iter);
3339 if (temp)
3341 *expr_p = temp;
3342 append_to_statement_list (body, pre_p);
3343 return GS_OK;
3345 else
3347 *expr_p = body;
3348 return GS_ALL_DONE;
3352 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3353 is the cleanup action required. */
3355 static void
3356 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
3358 tree wce;
3360 /* Errors can result in improperly nested cleanups. Which results in
3361 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3362 if (errorcount || sorrycount)
3363 return;
3365 if (gimple_conditional_context ())
3367 /* If we're in a conditional context, this is more complex. We only
3368 want to run the cleanup if we actually ran the initialization that
3369 necessitates it, but we want to run it after the end of the
3370 conditional context. So we wrap the try/finally around the
3371 condition and use a flag to determine whether or not to actually
3372 run the destructor. Thus
3374 test ? f(A()) : 0
3376 becomes (approximately)
3378 flag = 0;
3379 try {
3380 if (test) { A::A(temp); flag = 1; val = f(temp); }
3381 else { val = 0; }
3382 } finally {
3383 if (flag) A::~A(temp);
3388 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3389 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3390 boolean_false_node);
3391 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3392 boolean_true_node);
3393 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3394 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3395 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3396 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3397 append_to_statement_list (ftrue, pre_p);
3399 /* Because of this manipulation, and the EH edges that jump
3400 threading cannot redirect, the temporary (VAR) will appear
3401 to be used uninitialized. Don't warn. */
3402 TREE_NO_WARNING (var) = 1;
3404 else
3406 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3407 append_to_statement_list (wce, pre_p);
3410 gimplify_stmt (&TREE_OPERAND (wce, 0));
3413 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3415 static enum gimplify_status
3416 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3418 tree targ = *expr_p;
3419 tree temp = TARGET_EXPR_SLOT (targ);
3420 tree init = TARGET_EXPR_INITIAL (targ);
3421 enum gimplify_status ret;
3423 if (init)
3425 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3426 to the temps list. */
3427 gimple_add_tmp_var (temp);
3429 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3430 expression is supposed to initialize the slot. */
3431 if (VOID_TYPE_P (TREE_TYPE (init)))
3432 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3433 else
3435 /* Special handling for BIND_EXPR can result in fewer temps. */
3436 ret = GS_OK;
3437 if (TREE_CODE (init) == BIND_EXPR)
3438 gimplify_bind_expr (&init, temp, pre_p);
3439 if (init != temp)
3441 init = build (MODIFY_EXPR, void_type_node, temp, init);
3442 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3443 fb_none);
3446 if (ret == GS_ERROR)
3447 return GS_ERROR;
3448 append_to_statement_list (init, pre_p);
3450 /* If needed, push the cleanup for the temp. */
3451 if (TARGET_EXPR_CLEANUP (targ))
3453 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3454 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3457 /* Only expand this once. */
3458 TREE_OPERAND (targ, 3) = init;
3459 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3461 else if (!DECL_SEEN_IN_BIND_EXPR_P (temp))
3462 /* We should have expanded this before. */
3463 abort ();
3465 *expr_p = temp;
3466 return GS_OK;
3469 /* Gimplification of expression trees. */
3471 /* Gimplify an expression which appears at statement context; usually, this
3472 means replacing it with a suitably gimple STATEMENT_LIST. */
3474 void
3475 gimplify_stmt (tree *stmt_p)
3477 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3480 /* Similarly, but force the result to be a STATEMENT_LIST. */
3482 void
3483 gimplify_to_stmt_list (tree *stmt_p)
3485 gimplify_stmt (stmt_p);
3486 if (!*stmt_p)
3487 *stmt_p = alloc_stmt_list ();
3488 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3490 tree t = *stmt_p;
3491 *stmt_p = alloc_stmt_list ();
3492 append_to_statement_list (t, stmt_p);
3497 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3498 gimplification failed.
3500 PRE_P points to the list where side effects that must happen before
3501 EXPR should be stored.
3503 POST_P points to the list where side effects that must happen after
3504 EXPR should be stored, or NULL if there is no suitable list. In
3505 that case, we copy the result to a temporary, emit the
3506 post-effects, and then return the temporary.
3508 GIMPLE_TEST_F points to a function that takes a tree T and
3509 returns nonzero if T is in the GIMPLE form requested by the
3510 caller. The GIMPLE predicates are in tree-gimple.c.
3512 This test is used twice. Before gimplification, the test is
3513 invoked to determine whether *EXPR_P is already gimple enough. If
3514 that fails, *EXPR_P is gimplified according to its code and
3515 GIMPLE_TEST_F is called again. If the test still fails, then a new
3516 temporary variable is created and assigned the value of the
3517 gimplified expression.
3519 FALLBACK tells the function what sort of a temporary we want. If the 1
3520 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3521 If both are set, either is OK, but an lvalue is preferable.
3523 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3524 iterates until solution. */
3526 enum gimplify_status
3527 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3528 bool (* gimple_test_f) (tree), fallback_t fallback)
3530 tree tmp;
3531 tree internal_pre = NULL_TREE;
3532 tree internal_post = NULL_TREE;
3533 tree save_expr;
3534 int is_statement = (pre_p == NULL);
3535 location_t saved_location;
3536 enum gimplify_status ret;
3538 save_expr = *expr_p;
3539 if (save_expr == NULL_TREE)
3540 return GS_ALL_DONE;
3542 /* We used to check the predicate here and return immediately if it
3543 succeeds. This is wrong; the design is for gimplification to be
3544 idempotent, and for the predicates to only test for valid forms, not
3545 whether they are fully simplified. */
3547 /* Set up our internal queues if needed. */
3548 if (pre_p == NULL)
3549 pre_p = &internal_pre;
3550 if (post_p == NULL)
3551 post_p = &internal_post;
3553 saved_location = input_location;
3554 if (save_expr != error_mark_node
3555 && EXPR_HAS_LOCATION (*expr_p))
3556 input_location = EXPR_LOCATION (*expr_p);
3558 /* Loop over the specific gimplifiers until the toplevel node
3559 remains the same. */
3562 /* Strip away as many useless type conversions as possible
3563 at the toplevel. */
3564 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
3566 /* Remember the expr. */
3567 save_expr = *expr_p;
3569 /* Die, die, die, my darling. */
3570 if (save_expr == error_mark_node
3571 || (TREE_TYPE (save_expr)
3572 && TREE_TYPE (save_expr) == error_mark_node))
3574 ret = GS_ERROR;
3575 break;
3578 /* Do any language-specific gimplification. */
3579 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3580 if (ret == GS_OK)
3582 if (*expr_p == NULL_TREE)
3583 break;
3584 if (*expr_p != save_expr)
3585 continue;
3587 else if (ret != GS_UNHANDLED)
3588 break;
3590 ret = GS_OK;
3591 switch (TREE_CODE (*expr_p))
3593 /* First deal with the special cases. */
3595 case POSTINCREMENT_EXPR:
3596 case POSTDECREMENT_EXPR:
3597 case PREINCREMENT_EXPR:
3598 case PREDECREMENT_EXPR:
3599 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3600 fallback != fb_none);
3601 break;
3603 case ARRAY_REF:
3604 case ARRAY_RANGE_REF:
3605 case REALPART_EXPR:
3606 case IMAGPART_EXPR:
3607 case COMPONENT_REF:
3608 case VIEW_CONVERT_EXPR:
3609 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3610 fallback ? fallback : fb_rvalue);
3611 break;
3613 case COND_EXPR:
3614 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3615 break;
3617 case CALL_EXPR:
3618 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
3619 break;
3621 case TREE_LIST:
3622 abort ();
3624 case COMPOUND_EXPR:
3625 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3626 break;
3628 case MODIFY_EXPR:
3629 case INIT_EXPR:
3630 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3631 fallback != fb_none);
3632 break;
3634 case TRUTH_ANDIF_EXPR:
3635 case TRUTH_ORIF_EXPR:
3636 ret = gimplify_boolean_expr (expr_p);
3637 break;
3639 case TRUTH_NOT_EXPR:
3640 TREE_OPERAND (*expr_p, 0)
3641 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3642 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3643 is_gimple_val, fb_rvalue);
3644 recalculate_side_effects (*expr_p);
3645 break;
3647 case ADDR_EXPR:
3648 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3649 break;
3651 case VA_ARG_EXPR:
3652 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3653 break;
3655 case CONVERT_EXPR:
3656 case NOP_EXPR:
3657 if (IS_EMPTY_STMT (*expr_p))
3659 ret = GS_ALL_DONE;
3660 break;
3663 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3664 || fallback == fb_none)
3666 /* Just strip a conversion to void (or in void context) and
3667 try again. */
3668 *expr_p = TREE_OPERAND (*expr_p, 0);
3669 break;
3672 ret = gimplify_conversion (expr_p);
3673 if (ret == GS_ERROR)
3674 break;
3675 if (*expr_p != save_expr)
3676 break;
3677 /* FALLTHRU */
3679 case FIX_TRUNC_EXPR:
3680 case FIX_CEIL_EXPR:
3681 case FIX_FLOOR_EXPR:
3682 case FIX_ROUND_EXPR:
3683 /* unary_expr: ... | '(' cast ')' val | ... */
3684 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3685 is_gimple_val, fb_rvalue);
3686 recalculate_side_effects (*expr_p);
3687 break;
3689 case INDIRECT_REF:
3690 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3691 is_gimple_reg, fb_rvalue);
3692 recalculate_side_effects (*expr_p);
3693 break;
3695 /* Constants need not be gimplified. */
3696 case INTEGER_CST:
3697 case REAL_CST:
3698 case STRING_CST:
3699 case COMPLEX_CST:
3700 case VECTOR_CST:
3701 ret = GS_ALL_DONE;
3702 break;
3704 case CONST_DECL:
3705 /* If we require an lvalue, such as for ADDR_EXPR, retain the
3706 CONST_DECL node. Otherwise the decl is replacable by its
3707 value. */
3708 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
3709 if (fallback & fb_lvalue)
3710 ret = GS_ALL_DONE;
3711 else
3712 *expr_p = DECL_INITIAL (*expr_p);
3713 break;
3715 case DECL_EXPR:
3716 ret = gimplify_decl_expr (expr_p);
3717 break;
3719 case EXC_PTR_EXPR:
3720 /* FIXME make this a decl. */
3721 ret = GS_ALL_DONE;
3722 break;
3724 case BIND_EXPR:
3725 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3726 break;
3728 case LOOP_EXPR:
3729 ret = gimplify_loop_expr (expr_p, pre_p);
3730 break;
3732 case SWITCH_EXPR:
3733 ret = gimplify_switch_expr (expr_p, pre_p);
3734 break;
3736 case LABELED_BLOCK_EXPR:
3737 ret = gimplify_labeled_block_expr (expr_p);
3738 break;
3740 case EXIT_BLOCK_EXPR:
3741 ret = gimplify_exit_block_expr (expr_p);
3742 break;
3744 case EXIT_EXPR:
3745 ret = gimplify_exit_expr (expr_p);
3746 break;
3748 case GOTO_EXPR:
3749 /* If the target is not LABEL, then it is a computed jump
3750 and the target needs to be gimplified. */
3751 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3752 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3753 NULL, is_gimple_val, fb_rvalue);
3754 break;
3756 case LABEL_EXPR:
3757 ret = GS_ALL_DONE;
3758 #ifdef ENABLE_CHECKING
3759 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3760 abort ();
3761 #endif
3762 break;
3764 case CASE_LABEL_EXPR:
3765 ret = gimplify_case_label_expr (expr_p);
3766 break;
3768 case RETURN_EXPR:
3769 ret = gimplify_return_expr (*expr_p, pre_p);
3770 break;
3772 case CONSTRUCTOR:
3773 /* Don't reduce this in place; let gimplify_init_constructor work its
3774 magic. Buf if we're just elaborating this for side effects, just
3775 gimplify any element that has side-effects. */
3776 if (fallback == fb_none)
3778 for (tmp = CONSTRUCTOR_ELTS (*expr_p); tmp;
3779 tmp = TREE_CHAIN (tmp))
3780 if (TREE_SIDE_EFFECTS (TREE_VALUE (tmp)))
3781 gimplify_expr (&TREE_VALUE (tmp), pre_p, post_p,
3782 gimple_test_f, fallback);
3784 *expr_p = NULL_TREE;
3787 ret = GS_ALL_DONE;
3788 break;
3790 /* The following are special cases that are not handled by the
3791 original GIMPLE grammar. */
3793 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3794 eliminated. */
3795 case SAVE_EXPR:
3796 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3797 break;
3799 case BIT_FIELD_REF:
3801 enum gimplify_status r0, r1, r2;
3803 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3804 is_gimple_lvalue, fb_either);
3805 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3806 is_gimple_val, fb_rvalue);
3807 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3808 is_gimple_val, fb_rvalue);
3809 recalculate_side_effects (*expr_p);
3811 ret = MIN (r0, MIN (r1, r2));
3813 break;
3815 case NON_LVALUE_EXPR:
3816 /* This should have been stripped above. */
3817 abort ();
3818 break;
3820 case ASM_EXPR:
3821 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3822 break;
3824 case TRY_FINALLY_EXPR:
3825 case TRY_CATCH_EXPR:
3826 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3827 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3828 ret = GS_ALL_DONE;
3829 break;
3831 case CLEANUP_POINT_EXPR:
3832 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3833 break;
3835 case TARGET_EXPR:
3836 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3837 break;
3839 case CATCH_EXPR:
3840 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3841 ret = GS_ALL_DONE;
3842 break;
3844 case EH_FILTER_EXPR:
3845 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3846 ret = GS_ALL_DONE;
3847 break;
3849 case OBJ_TYPE_REF:
3851 enum gimplify_status r0, r1;
3852 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
3853 is_gimple_val, fb_rvalue);
3854 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
3855 is_gimple_val, fb_rvalue);
3856 ret = MIN (r0, r1);
3858 break;
3860 case LABEL_DECL:
3861 /* We get here when taking the address of a label. We mark
3862 the label as "forced"; meaning it can never be removed and
3863 it is a potential target for any computed goto. */
3864 FORCED_LABEL (*expr_p) = 1;
3865 ret = GS_ALL_DONE;
3866 break;
3868 case STATEMENT_LIST:
3869 ret = gimplify_statement_list (expr_p);
3870 break;
3872 case WITH_SIZE_EXPR:
3874 enum gimplify_status r0, r1;
3875 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3876 post_p == &internal_post ? NULL : post_p,
3877 gimple_test_f, fallback);
3878 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3879 is_gimple_val, fb_rvalue);
3881 break;
3883 case VAR_DECL:
3884 /* ??? If this is a local variable, and it has not been seen in any
3885 outer BIND_EXPR, then it's probably the result of a duplicate
3886 declaration, for which we've already issued an error. It would
3887 be really nice if the front end wouldn't leak these at all.
3888 Currently the only known culprit is C++ destructors, as seen
3889 in g++.old-deja/g++.jason/binding.C. */
3890 tmp = *expr_p;
3891 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3892 && decl_function_context (tmp) == current_function_decl
3893 && !DECL_SEEN_IN_BIND_EXPR_P (tmp))
3895 #ifdef ENABLE_CHECKING
3896 if (!errorcount && !sorrycount)
3897 abort ();
3898 #endif
3899 ret = GS_ERROR;
3900 break;
3903 /* If this is a local variable sized decl, it must be accessed
3904 indirectly. Perform that substitution. */
3905 if (DECL_VALUE_EXPR (tmp))
3907 *expr_p = unshare_expr (DECL_VALUE_EXPR (tmp));
3908 ret = GS_OK;
3909 break;
3912 ret = GS_ALL_DONE;
3913 break;
3915 case SSA_NAME:
3916 /* Allow callbacks into the gimplifier during optimization. */
3917 ret = GS_ALL_DONE;
3918 break;
3920 default:
3921 /* If this is a comparison of objects of aggregate type, handle
3922 it specially (by converting to a call to memcmp). It would be
3923 nice to only have to do this for variable-sized objects, but
3924 then we'd have to allow the same nest of reference nodes we
3925 allow for MODIFY_EXPR and that's too complex. */
3926 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3927 && (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1)))))
3928 ret = gimplify_variable_sized_compare (expr_p);
3930 /* If *EXPR_P does not need to be special-cased, handle it
3931 according to its class. */
3932 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3933 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3934 post_p, is_gimple_val, fb_rvalue);
3935 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3936 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3937 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3938 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3939 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3941 enum gimplify_status r0, r1;
3943 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3944 post_p, is_gimple_val, fb_rvalue);
3945 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3946 post_p, is_gimple_val, fb_rvalue);
3948 ret = MIN (r0, r1);
3950 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3951 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3953 ret = GS_ALL_DONE;
3954 break;
3956 else
3957 /* Fail if we don't know how to handle this tree code. */
3958 abort ();
3960 recalculate_side_effects (*expr_p);
3961 break;
3964 /* If we replaced *expr_p, gimplify again. */
3965 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3966 ret = GS_ALL_DONE;
3968 while (ret == GS_OK);
3970 /* If we encountered an error_mark somewhere nested inside, either
3971 stub out the statement or propagate the error back out. */
3972 if (ret == GS_ERROR)
3974 if (is_statement)
3975 *expr_p = NULL;
3976 goto out;
3979 #ifdef ENABLE_CHECKING
3980 /* This was only valid as a return value from the langhook, which
3981 we handled. Make sure it doesn't escape from any other context. */
3982 if (ret == GS_UNHANDLED)
3983 abort ();
3984 #endif
3986 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
3988 /* We aren't looking for a value, and we don't have a valid
3989 statement. If it doesn't have side-effects, throw it away. */
3990 if (!TREE_SIDE_EFFECTS (*expr_p))
3991 *expr_p = NULL;
3992 else if (!TREE_THIS_VOLATILE (*expr_p))
3994 /* This is probably a _REF that contains something nested that
3995 has side effects. Recurse through the operands to find it. */
3996 enum tree_code code = TREE_CODE (*expr_p);
3998 if (code == COMPONENT_REF
3999 || code == REALPART_EXPR || code == IMAGPART_EXPR)
4000 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4001 gimple_test_f, fallback);
4002 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
4004 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4005 gimple_test_f, fallback);
4006 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
4007 gimple_test_f, fallback);
4009 else
4010 /* Anything else with side-effects
4011 must be converted to a valid statement before we get here. */
4012 abort ();
4014 *expr_p = NULL;
4016 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
4018 /* Historically, the compiler has treated a bare
4019 reference to a volatile lvalue as forcing a load. */
4020 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
4021 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
4023 else
4024 /* We can't do anything useful with a volatile reference to
4025 incomplete type, so just throw it away. */
4026 *expr_p = NULL;
4029 /* If we are gimplifying at the statement level, we're done. Tack
4030 everything together and replace the original statement with the
4031 gimplified form. */
4032 if (fallback == fb_none || is_statement)
4034 if (internal_pre || internal_post)
4036 append_to_statement_list (*expr_p, &internal_pre);
4037 append_to_statement_list (internal_post, &internal_pre);
4038 annotate_all_with_locus (&internal_pre, input_location);
4039 *expr_p = internal_pre;
4041 else if (!*expr_p)
4043 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
4044 annotate_all_with_locus (expr_p, input_location);
4045 else
4046 annotate_one_with_locus (*expr_p, input_location);
4047 goto out;
4050 /* Otherwise we're gimplifying a subexpression, so the resulting value is
4051 interesting. */
4053 /* If it's sufficiently simple already, we're done. Unless we are
4054 handling some post-effects internally; if that's the case, we need to
4055 copy into a temp before adding the post-effects to the tree. */
4056 if (!internal_post && (*gimple_test_f) (*expr_p))
4057 goto out;
4059 /* Otherwise, we need to create a new temporary for the gimplified
4060 expression. */
4062 /* We can't return an lvalue if we have an internal postqueue. The
4063 object the lvalue refers to would (probably) be modified by the
4064 postqueue; we need to copy the value out first, which means an
4065 rvalue. */
4066 if ((fallback & fb_lvalue) && !internal_post
4067 && is_gimple_addressable (*expr_p))
4069 /* An lvalue will do. Take the address of the expression, store it
4070 in a temporary, and replace the expression with an INDIRECT_REF of
4071 that temporary. */
4072 tmp = build_fold_addr_expr (*expr_p);
4073 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
4074 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
4076 else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
4078 #if defined ENABLE_CHECKING
4079 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
4080 abort ();
4081 #endif
4083 /* An rvalue will do. Assign the gimplified expression into a new
4084 temporary TMP and replace the original expression with TMP. */
4086 if (internal_post || (fallback & fb_lvalue))
4087 /* The postqueue might change the value of the expression between
4088 the initialization and use of the temporary, so we can't use a
4089 formal temp. FIXME do we care? */
4090 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
4091 else
4092 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
4093 DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
4095 else if (fallback & fb_mayfail)
4097 /* If this is an asm statement, and the user asked for the impossible,
4098 don't abort. Fail and let gimplify_asm_expr issue an error. */
4099 ret = GS_ERROR;
4100 goto out;
4102 else
4104 fprintf (stderr, "gimplification failed:\n");
4105 print_generic_expr (stderr, *expr_p, 0);
4106 debug_tree (*expr_p);
4107 abort ();
4110 #if defined ENABLE_CHECKING
4111 /* Make sure the temporary matches our predicate. */
4112 if (!(*gimple_test_f) (*expr_p))
4113 abort ();
4114 #endif
4116 if (internal_post)
4118 annotate_all_with_locus (&internal_post, input_location);
4119 append_to_statement_list (internal_post, pre_p);
4122 out:
4123 input_location = saved_location;
4124 return ret;
4127 /* Look through TYPE for variable-sized objects and gimplify each such
4128 size that we find. Add to LIST_P any statements generated. */
4130 void
4131 gimplify_type_sizes (tree type, tree *list_p)
4133 tree field;
4135 switch (TREE_CODE (type))
4137 case ERROR_MARK:
4138 return;
4140 case INTEGER_TYPE:
4141 case ENUMERAL_TYPE:
4142 case BOOLEAN_TYPE:
4143 case CHAR_TYPE:
4144 case REAL_TYPE:
4145 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
4146 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
4147 break;
4149 case ARRAY_TYPE:
4150 /* These anonymous types don't have declarations, so handle them here. */
4151 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
4152 break;
4154 case RECORD_TYPE:
4155 case UNION_TYPE:
4156 case QUAL_UNION_TYPE:
4157 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4158 if (TREE_CODE (field) == FIELD_DECL)
4159 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
4160 break;
4162 default:
4163 break;
4166 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
4167 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
4170 /* Subroutine of the above to gimplify one size or position, *EXPR_P.
4171 We add any required statements to STMT_P. */
4173 void
4174 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
4176 /* We don't do anything if the value isn't there, is constant, or contains
4177 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
4178 a VAR_DECL. If it's a VAR_DECL from another function, the gimplfier
4179 will want to replace it with a new variable, but that will cause problems
4180 if this type is from outside the function. It's OK to have that here. */
4181 if (*expr_p == NULL_TREE || TREE_CONSTANT (*expr_p)
4182 || TREE_CODE (*expr_p) == VAR_DECL
4183 || CONTAINS_PLACEHOLDER_P (*expr_p))
4184 return;
4186 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
4189 #ifdef ENABLE_CHECKING
4190 /* Compare types A and B for a "close enough" match. */
4192 static bool
4193 cpt_same_type (tree a, tree b)
4195 if (lang_hooks.types_compatible_p (a, b))
4196 return true;
4198 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
4199 link them together. This routine is intended to catch type errors
4200 that will affect the optimizers, and the optimizers don't add new
4201 dereferences of function pointers, so ignore it. */
4202 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
4203 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
4204 return true;
4206 /* ??? The C FE pushes type qualifiers after the fact into the type of
4207 the element from the type of the array. See build_unary_op's handling
4208 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
4209 should have done it when creating the variable in the first place.
4210 Alternately, why aren't the two array types made variants? */
4211 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
4212 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4214 /* And because of those, we have to recurse down through pointers. */
4215 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
4216 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4218 return false;
4221 /* Check for some cases of the front end missing cast expressions.
4222 The type of a dereference should correspond to the pointer type;
4223 similarly the type of an address should match its object. */
4225 static tree
4226 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4227 void *data ATTRIBUTE_UNUSED)
4229 tree t = *tp;
4230 tree ptype, otype, dtype;
4232 switch (TREE_CODE (t))
4234 case INDIRECT_REF:
4235 case ARRAY_REF:
4236 otype = TREE_TYPE (t);
4237 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
4238 dtype = TREE_TYPE (ptype);
4239 if (!cpt_same_type (otype, dtype))
4240 abort ();
4241 break;
4243 case ADDR_EXPR:
4244 ptype = TREE_TYPE (t);
4245 otype = TREE_TYPE (TREE_OPERAND (t, 0));
4246 dtype = TREE_TYPE (ptype);
4247 if (!cpt_same_type (otype, dtype))
4249 /* &array is allowed to produce a pointer to the element, rather than
4250 a pointer to the array type. We must allow this in order to
4251 properly represent assigning the address of an array in C into
4252 pointer to the element type. */
4253 if (TREE_CODE (otype) == ARRAY_TYPE
4254 && POINTER_TYPE_P (ptype)
4255 && cpt_same_type (TREE_TYPE (otype), dtype))
4256 break;
4257 abort ();
4259 break;
4261 default:
4262 return NULL_TREE;
4266 return NULL_TREE;
4268 #endif
4270 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
4271 function decl containing BODY. */
4273 void
4274 gimplify_body (tree *body_p, tree fndecl)
4276 location_t saved_location = input_location;
4277 tree body;
4279 timevar_push (TV_TREE_GIMPLIFY);
4280 push_gimplify_context ();
4282 /* Unshare most shared trees in the body and in that of any nested functions.
4283 It would seem we don't have to do this for nested functions because
4284 they are supposed to be output and then the outer function gimplified
4285 first, but the g++ front end doesn't always do it that way. */
4286 unshare_body (body_p, fndecl);
4287 unvisit_body (body_p, fndecl);
4289 /* Make sure input_location isn't set to something wierd. */
4290 input_location = DECL_SOURCE_LOCATION (fndecl);
4292 /* Gimplify the function's body. */
4293 gimplify_stmt (body_p);
4294 body = *body_p;
4296 /* Unshare again, in case gimplification was sloppy. */
4297 unshare_all_trees (body);
4299 if (!body)
4300 body = alloc_stmt_list ();
4301 else if (TREE_CODE (body) == STATEMENT_LIST)
4303 tree t = expr_only (*body_p);
4304 if (t)
4305 body = t;
4308 /* If there isn't an outer BIND_EXPR, add one. */
4309 if (TREE_CODE (body) != BIND_EXPR)
4311 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
4312 NULL_TREE, NULL_TREE);
4313 TREE_SIDE_EFFECTS (b) = 1;
4314 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
4315 body = b;
4317 *body_p = body;
4319 pop_gimplify_context (body);
4321 #ifdef ENABLE_CHECKING
4322 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
4323 #endif
4325 timevar_pop (TV_TREE_GIMPLIFY);
4326 input_location = saved_location;
4329 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
4330 node for the function we want to gimplify. */
4332 void
4333 gimplify_function_tree (tree fndecl)
4335 tree oldfn;
4337 oldfn = current_function_decl;
4338 current_function_decl = fndecl;
4340 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
4342 /* If we're instrumenting function entry/exit, then prepend the call to
4343 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
4344 catch the exit hook. */
4345 /* ??? Add some way to ignore exceptions for this TFE. */
4346 if (flag_instrument_function_entry_exit
4347 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
4349 tree tf, x, bind;
4351 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4352 TREE_SIDE_EFFECTS (tf) = 1;
4353 x = DECL_SAVED_TREE (fndecl);
4354 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
4355 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
4356 x = build_function_call_expr (x, NULL);
4357 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
4359 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4360 TREE_SIDE_EFFECTS (bind) = 1;
4361 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
4362 x = build_function_call_expr (x, NULL);
4363 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
4364 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
4366 DECL_SAVED_TREE (fndecl) = bind;
4369 current_function_decl = oldfn;
4372 #include "gt-gimplify.h"