* configure.in: Fix sed invocation for GFORTRAN_FOR_TARGET.
[official-gcc.git] / gcc / gimplify.c
blob621569bf5934ba0de6423c4264933c8e41a037dd
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
4 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "errors.h"
32 #include "varray.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "tree-flow.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"
49 static struct gimplify_ctx
51 tree current_bind_expr;
52 bool save_stack;
53 tree temps;
54 tree conditional_cleanups;
55 int conditions;
56 tree exit_label;
57 varray_type case_labels;
58 /* The formal temporary table. Should this be persistent? */
59 htab_t temp_htab;
60 } *gimplify_ctxp;
63 /* Formal (expression) temporary table handling: Multiple occurrences of
64 the same scalar expression are evaluated into the same temporary. */
66 typedef struct gimple_temp_hash_elt
68 tree val; /* Key */
69 tree temp; /* Value */
70 } elt_t;
72 /* Return a hash value for a formal temporary table entry. */
74 static hashval_t
75 gimple_tree_hash (const void *p)
77 tree t = ((const elt_t *)p)->val;
78 return iterative_hash_expr (t, 0);
81 /* Compare two formal temporary table entries. */
83 static int
84 gimple_tree_eq (const void *p1, const void *p2)
86 tree t1 = ((const elt_t *)p1)->val;
87 tree t2 = ((const elt_t *)p2)->val;
88 enum tree_code code = TREE_CODE (t1);
90 if (TREE_CODE (t2) != code
91 || TREE_TYPE (t1) != TREE_TYPE (t2))
92 return 0;
94 if (!operand_equal_p (t1, t2, 0))
95 return 0;
97 /* Only allow them to compare equal if they also hash equal; otherwise
98 results are nondeterminate, and we fail bootstrap comparison. */
99 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
100 abort ();
102 return 1;
105 /* Set up a context for the gimplifier. */
107 void
108 push_gimplify_context (void)
110 if (gimplify_ctxp)
111 abort ();
112 gimplify_ctxp
113 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
114 gimplify_ctxp->temp_htab
115 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
118 /* Tear down a context for the gimplifier. If BODY is non-null, then
119 put the temporaries into the outer BIND_EXPR. Otherwise, put them
120 in the unexpanded_var_list. */
122 void
123 pop_gimplify_context (tree body)
125 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
126 abort ();
128 if (body)
129 declare_tmp_vars (gimplify_ctxp->temps, body);
130 else
131 record_vars (gimplify_ctxp->temps);
133 #if 0
134 if (!quiet_flag)
135 fprintf (stderr, " collisions: %f ",
136 htab_collisions (gimplify_ctxp->temp_htab));
137 #endif
139 htab_delete (gimplify_ctxp->temp_htab);
140 free (gimplify_ctxp);
141 gimplify_ctxp = NULL;
144 void
145 gimple_push_bind_expr (tree bind)
147 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
148 gimplify_ctxp->current_bind_expr = bind;
151 void
152 gimple_pop_bind_expr (void)
154 gimplify_ctxp->current_bind_expr
155 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
158 tree
159 gimple_current_bind_expr (void)
161 return gimplify_ctxp->current_bind_expr;
164 /* Returns true iff there is a COND_EXPR between us and the innermost
165 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
167 static bool
168 gimple_conditional_context (void)
170 return gimplify_ctxp->conditions > 0;
173 /* Note that we've entered a COND_EXPR. */
175 static void
176 gimple_push_condition (void)
178 ++(gimplify_ctxp->conditions);
181 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
182 now, add any conditional cleanups we've seen to the prequeue. */
184 static void
185 gimple_pop_condition (tree *pre_p)
187 int conds = --(gimplify_ctxp->conditions);
188 if (conds == 0)
190 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
191 gimplify_ctxp->conditional_cleanups = NULL_TREE;
193 else if (conds < 0)
194 abort ();
197 /* A subroutine of append_to_statement_list{,_force}. */
199 static void
200 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
202 tree list = *list_p;
203 tree_stmt_iterator i;
205 if (!list)
207 if (t && TREE_CODE (t) == STATEMENT_LIST)
209 *list_p = t;
210 return;
212 *list_p = list = alloc_stmt_list ();
215 if (!side_effects)
216 return;
218 i = tsi_last (list);
219 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
222 /* Add T to the end of the list container pointed by LIST_P.
223 If T is an expression with no effects, it is ignored. */
225 void
226 append_to_statement_list (tree t, tree *list_p)
228 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
231 /* Similar, but the statement is always added, regardless of side effects. */
233 void
234 append_to_statement_list_force (tree t, tree *list_p)
236 append_to_statement_list_1 (t, list_p, t != NULL);
239 /* Add T to the end of a COMPOUND_EXPR pointed by LIST_P. The type
240 of the result is the type of T. */
242 void
243 append_to_compound_expr (tree t, tree *list_p)
245 if (!t)
246 return;
247 if (!*list_p)
248 *list_p = t;
249 else
250 *list_p = build (COMPOUND_EXPR, TREE_TYPE (t), *list_p, t);
253 /* Strip off a legitimate source ending from the input string NAME of
254 length LEN. Rather than having to know the names used by all of
255 our front ends, we strip off an ending of a period followed by
256 up to five characters. (Java uses ".class".) */
258 static inline void
259 remove_suffix (char *name, int len)
261 int i;
263 for (i = 2; i < 8 && len > i; i++)
265 if (name[len - i] == '.')
267 name[len - i] = '\0';
268 break;
273 /* Create a nameless artificial label and put it in the current function
274 context. Returns the newly created label. */
276 tree
277 create_artificial_label (void)
279 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
280 DECL_ARTIFICIAL (lab) = 1;
281 DECL_CONTEXT (lab) = current_function_decl;
282 return lab;
285 /* Create a new temporary name with PREFIX. Returns an indentifier. */
287 static GTY(()) unsigned int tmp_var_id_num;
289 tree
290 create_tmp_var_name (const char *prefix)
292 char *tmp_name;
294 if (prefix)
296 char *preftmp = ASTRDUP (prefix);
297 remove_suffix (preftmp, strlen (preftmp));
298 prefix = preftmp;
301 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
302 return get_identifier (tmp_name);
306 /* Create a new temporary variable declaration of type TYPE.
307 Does NOT push it into the current binding. */
309 tree
310 create_tmp_var_raw (tree type, const char *prefix)
312 tree tmp_var;
313 tree new_type;
315 /* Make the type of the variable writable. */
316 new_type = build_type_variant (type, 0, 0);
317 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
319 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
321 /* The variable was declared by the compiler. */
322 DECL_ARTIFICIAL (tmp_var) = 1;
323 /* And we don't want debug info for it. */
324 DECL_IGNORED_P (tmp_var) = 1;
326 /* Make the variable writable. */
327 TREE_READONLY (tmp_var) = 0;
329 DECL_EXTERNAL (tmp_var) = 0;
330 TREE_STATIC (tmp_var) = 0;
331 TREE_USED (tmp_var) = 1;
333 return tmp_var;
336 /* Create a new temporary variable declaration of type TYPE. DOES push the
337 variable into the current binding. Further, assume that this is called
338 only from gimplification or optimization, at which point the creation of
339 certain types are bugs. */
341 tree
342 create_tmp_var (tree type, const char *prefix)
344 tree tmp_var;
346 #if defined ENABLE_CHECKING
347 /* If the type is an array or a type which must be created by the
348 frontend, something is wrong. */
349 if (TREE_CODE (type) == ARRAY_TYPE || TREE_ADDRESSABLE (type))
350 abort ();
351 if (!COMPLETE_TYPE_P (type))
352 abort ();
353 /* Variable sized types require lots of machinery to create; the
354 optimizers shouldn't be doing anything of the sort. */
355 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
356 abort ();
357 #endif
359 tmp_var = create_tmp_var_raw (type, prefix);
360 gimple_add_tmp_var (tmp_var);
361 return tmp_var;
364 /* Given a tree, try to return a useful variable name that we can use
365 to prefix a temporary that is being assigned the value of the tree.
366 I.E. given <temp> = &A, return A. */
368 const char *
369 get_name (tree t)
371 tree stripped_decl;
373 stripped_decl = t;
374 STRIP_NOPS (stripped_decl);
375 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
376 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
377 else
379 switch (TREE_CODE (stripped_decl))
381 case ADDR_EXPR:
382 return get_name (TREE_OPERAND (stripped_decl, 0));
383 break;
384 default:
385 return NULL;
390 /* Create a temporary with a name derived from VAL. Subroutine of
391 lookup_tmp_var; nobody else should call this function. */
393 static inline tree
394 create_tmp_from_val (tree val)
396 return create_tmp_var (TREE_TYPE (val), get_name (val));
399 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
400 an existing expression temporary. */
402 static tree
403 lookup_tmp_var (tree val, bool is_formal)
405 if (!is_formal || TREE_SIDE_EFFECTS (val))
406 return create_tmp_from_val (val);
407 else
409 elt_t elt, *elt_p;
410 void **slot;
412 elt.val = val;
413 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
414 if (*slot == NULL)
416 elt_p = xmalloc (sizeof (*elt_p));
417 elt_p->val = val;
418 elt_p->temp = create_tmp_from_val (val);
419 *slot = (void *)elt_p;
421 else
422 elt_p = (elt_t *) *slot;
424 return elt_p->temp;
428 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
429 in gimplify_expr. Only use this function if:
431 1) The value of the unfactored expression represented by VAL will not
432 change between the initialization and use of the temporary, and
433 2) The temporary will not be otherwise modified.
435 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
436 and #2 means it is inappropriate for && temps.
438 For other cases, use get_initialized_tmp_var instead. */
440 static tree
441 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
443 tree t, mod;
444 char class;
446 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
448 t = lookup_tmp_var (val, is_formal);
450 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
452 class = TREE_CODE_CLASS (TREE_CODE (val));
453 if (EXPR_LOCUS (val))
454 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
455 else
456 annotate_with_locus (mod, input_location);
457 /* gimplify_modify_expr might want to reduce this further. */
458 gimplify_stmt (&mod);
459 append_to_statement_list (mod, pre_p);
461 return t;
464 tree
465 get_formal_tmp_var (tree val, tree *pre_p)
467 return internal_get_tmp_var (val, pre_p, NULL, true);
470 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
471 are as in gimplify_expr. */
473 tree
474 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
476 return internal_get_tmp_var (val, pre_p, post_p, false);
479 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
481 bool
482 is_gimple_tmp_var (tree t)
484 /* FIXME this could trigger for other local artificials, too. */
485 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
486 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
489 /* Declares all the variables in VARS in SCOPE. Returns the last
490 DECL_STMT emitted. */
492 void
493 declare_tmp_vars (tree vars, tree scope)
495 tree last = vars;
496 if (last)
498 tree temps;
500 /* C99 mode puts the default 'return 0;' for main() outside the outer
501 braces. So drill down until we find an actual scope. */
502 while (TREE_CODE (scope) == COMPOUND_EXPR)
503 scope = TREE_OPERAND (scope, 0);
505 if (TREE_CODE (scope) != BIND_EXPR)
506 abort ();
508 temps = nreverse (last);
509 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
510 BIND_EXPR_VARS (scope) = temps;
512 /* We don't add the temps to the block for this BIND_EXPR, as we're
513 not interested in debugging info for them. */
517 void
518 gimple_add_tmp_var (tree tmp)
520 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
521 abort ();
523 DECL_CONTEXT (tmp) = current_function_decl;
524 tmp->decl.seen_in_bind_expr = 1;
526 if (gimplify_ctxp)
528 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
529 gimplify_ctxp->temps = tmp;
531 else if (cfun)
532 record_vars (tmp);
533 else
534 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
537 /* Determines whether to assign a locus to the statement STMT. */
539 static bool
540 should_carry_locus_p (tree stmt)
542 /* Don't emit a line note for a label. We particularly don't want to
543 emit one for the break label, since it doesn't actually correspond
544 to the beginning of the loop/switch. */
545 if (TREE_CODE (stmt) == LABEL_EXPR)
546 return false;
548 /* Do not annotate empty statements, since it confuses gcov. */
549 if (!TREE_SIDE_EFFECTS (stmt))
550 return false;
552 return true;
555 void
556 annotate_all_with_locus (tree *stmt_p, location_t locus)
558 tree_stmt_iterator i;
560 if (!*stmt_p)
561 return;
563 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
565 tree t = tsi_stmt (i);
567 #ifdef ENABLE_CHECKING
568 /* Assuming we've already been gimplified, we shouldn't
569 see nested chaining constructs anymore. */
570 if (TREE_CODE (t) == STATEMENT_LIST
571 || TREE_CODE (t) == COMPOUND_EXPR)
572 abort ();
573 #endif
575 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
576 && ! EXPR_HAS_LOCATION (t)
577 && should_carry_locus_p (t))
578 annotate_with_locus (t, locus);
582 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
583 These nodes model computations that should only be done once. If we
584 were to unshare something like SAVE_EXPR(i++), the gimplification
585 process would create wrong code. */
587 static tree
588 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
590 enum tree_code code = TREE_CODE (*tp);
591 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
592 if (TREE_CODE_CLASS (code) == 't'
593 || TREE_CODE_CLASS (code) == 'd'
594 || TREE_CODE_CLASS (code) == 'c'
595 || code == SAVE_EXPR || code == TARGET_EXPR
596 /* We can't do anything sensible with a BLOCK used as an expression,
597 but we also can't abort when we see it because of non-expression
598 uses. So just avert our eyes and cross our fingers. Silly Java. */
599 || code == BLOCK)
600 *walk_subtrees = 0;
601 else if (code == BIND_EXPR)
602 abort ();
603 else
604 copy_tree_r (tp, walk_subtrees, data);
606 return NULL_TREE;
609 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
610 after VA_ARG_EXPRs are properly lowered. */
612 static tree
613 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
614 void *data ATTRIBUTE_UNUSED)
616 if (SSA_VAR_P (*tp))
617 TREE_THIS_VOLATILE (*tp) = 1;
619 return NULL_TREE;
623 /* Callback for walk_tree to unshare most of the shared trees rooted at
624 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
625 then *TP is deep copied by calling copy_tree_r.
627 This unshares the same trees as copy_tree_r with the exception of
628 SAVE_EXPR nodes. These nodes model computations that should only be
629 done once. If we were to unshare something like SAVE_EXPR(i++), the
630 gimplification process would create wrong code. */
632 static tree
633 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
634 void *data ATTRIBUTE_UNUSED)
636 tree t = *tp;
637 enum tree_code code = TREE_CODE (t);
639 /* Skip types, decls, and constants. */
640 if (TREE_CODE_CLASS (code) == 't'
641 || TREE_CODE_CLASS (code) == 'd'
642 || TREE_CODE_CLASS (code) == 'c')
643 *walk_subtrees = 0;
645 /* Special-case BIND_EXPR. We should never be copying these, therefore
646 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
647 double processing of the DECL_INITIAL, which could be seen via both
648 the BIND_EXPR_VARS and a DECL_STMT. */
649 else if (code == BIND_EXPR)
651 if (TREE_VISITED (t))
652 abort ();
653 TREE_VISITED (t) = 1;
654 *walk_subtrees = 0;
655 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
658 /* If this node has been visited already, unshare it and don't look
659 any deeper. */
660 else if (TREE_VISITED (t))
662 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
663 *walk_subtrees = 0;
666 /* Otherwise, mark the tree as visited and keep looking. */
667 else
668 TREE_VISITED (t) = 1;
670 return NULL_TREE;
673 static tree
674 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
675 void *data ATTRIBUTE_UNUSED)
677 if (TREE_VISITED (*tp))
678 TREE_VISITED (*tp) = 0;
679 else
680 *walk_subtrees = 0;
682 return NULL_TREE;
685 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
687 void
688 unshare_all_trees (tree t)
690 walk_tree (&t, copy_if_shared_r, NULL, NULL);
691 walk_tree (&t, unmark_visited_r, NULL, NULL);
694 /* Unconditionally make an unshared copy of EXPR. This is used when using
695 stored expressions which span multiple functions, such as BINFO_VTABLE,
696 as the normal unsharing process can't tell that they're shared. */
698 tree
699 unshare_expr (tree expr)
701 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
702 return expr;
705 /* A terser interface for building a representation of a exception
706 specification. */
708 tree
709 gimple_build_eh_filter (tree body, tree allowed, tree failure)
711 tree t;
713 /* FIXME should the allowed types go in TREE_TYPE? */
714 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
715 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
717 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
718 append_to_statement_list (body, &TREE_OPERAND (t, 0));
720 return t;
724 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
725 contain statements and have a value. Assign its value to a temporary
726 and give it void_type_node. Returns the temporary, or NULL_TREE if
727 WRAPPER was already void. */
729 tree
730 voidify_wrapper_expr (tree wrapper)
732 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
734 tree *p;
735 tree temp;
737 /* Set p to point to the body of the wrapper. */
738 switch (TREE_CODE (wrapper))
740 case BIND_EXPR:
741 /* For a BIND_EXPR, the body is operand 1. */
742 p = &BIND_EXPR_BODY (wrapper);
743 break;
745 default:
746 p = &TREE_OPERAND (wrapper, 0);
747 break;
750 /* Advance to the last statement. Set all container types to void. */
751 if (TREE_CODE (*p) == STATEMENT_LIST)
753 tree_stmt_iterator i = tsi_last (*p);
754 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
756 else
758 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
760 TREE_SIDE_EFFECTS (*p) = 1;
761 TREE_TYPE (*p) = void_type_node;
765 if (p && TREE_CODE (*p) == INIT_EXPR)
767 /* The C++ frontend already did this for us. */;
768 temp = TREE_OPERAND (*p, 0);
770 else if (p && TREE_CODE (*p) == INDIRECT_REF)
772 /* If we're returning a dereference, move the dereference outside
773 the wrapper. */
774 tree ptr = TREE_OPERAND (*p, 0);
775 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
776 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
777 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
778 /* If this is a BIND_EXPR for a const inline function, it might not
779 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
780 TREE_SIDE_EFFECTS (wrapper) = 1;
782 else
784 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
785 if (p && !IS_EMPTY_STMT (*p))
787 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
788 TREE_SIDE_EFFECTS (wrapper) = 1;
792 TREE_TYPE (wrapper) = void_type_node;
793 return temp;
796 return NULL_TREE;
799 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
800 temporary through that they comunicate. */
802 static void
803 build_stack_save_restore (tree *save, tree *restore)
805 tree save_call, tmp_var;
807 save_call =
808 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
809 NULL_TREE);
810 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
812 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
813 *restore =
814 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
815 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
818 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
820 static enum gimplify_status
821 gimplify_bind_expr (tree *expr_p, tree *pre_p)
823 tree bind_expr = *expr_p;
824 tree temp = voidify_wrapper_expr (bind_expr);
825 bool old_save_stack = gimplify_ctxp->save_stack;
826 tree t;
828 /* Mark variables seen in this bind expr. */
829 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
830 t->decl.seen_in_bind_expr = 1;
832 gimple_push_bind_expr (bind_expr);
833 gimplify_ctxp->save_stack = false;
835 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
837 if (gimplify_ctxp->save_stack)
839 tree stack_save, stack_restore;
841 /* Save stack on entry and restore it on exit. Add a try_finally
842 block to achieve this. Note that mudflap depends on the
843 format of the emitted code: see mx_register_decls(). */
844 build_stack_save_restore (&stack_save, &stack_restore);
846 t = build (TRY_FINALLY_EXPR, void_type_node,
847 BIND_EXPR_BODY (bind_expr), NULL_TREE);
848 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
850 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
851 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
852 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
855 gimplify_ctxp->save_stack = old_save_stack;
856 gimple_pop_bind_expr ();
858 if (temp)
860 *expr_p = temp;
861 append_to_statement_list (bind_expr, pre_p);
862 return GS_OK;
864 else
865 return GS_ALL_DONE;
868 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
869 GIMPLE value, it is assigned to a new temporary and the statement is
870 re-written to return the temporary.
872 PRE_P points to the list where side effects that must happen before
873 STMT should be stored. */
875 static enum gimplify_status
876 gimplify_return_expr (tree stmt, tree *pre_p)
878 tree ret_expr = TREE_OPERAND (stmt, 0);
879 tree result;
881 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
882 return GS_ALL_DONE;
884 if (ret_expr == error_mark_node)
885 return GS_ERROR;
887 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
888 result = NULL_TREE;
889 else
891 result = TREE_OPERAND (ret_expr, 0);
892 #ifdef ENABLE_CHECKING
893 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
894 && TREE_CODE (ret_expr) != INIT_EXPR)
895 || TREE_CODE (result) != RESULT_DECL)
896 abort ();
897 #endif
900 /* We need to pass the full MODIFY_EXPR down so that special handling
901 can replace it with something else. */
902 gimplify_stmt (&ret_expr);
904 if (result == NULL_TREE)
905 TREE_OPERAND (stmt, 0) = NULL_TREE;
906 else if (ret_expr == TREE_OPERAND (stmt, 0))
907 /* It was already GIMPLE. */
908 return GS_ALL_DONE;
909 else
911 /* If there's still a MODIFY_EXPR of the RESULT_DECL after
912 gimplification, find it so we can put it in the RETURN_EXPR. */
913 tree ret = NULL_TREE;
915 if (TREE_CODE (ret_expr) == STATEMENT_LIST)
917 tree_stmt_iterator si;
918 for (si = tsi_start (ret_expr); !tsi_end_p (si); tsi_next (&si))
920 tree sub = tsi_stmt (si);
921 if (TREE_CODE (sub) == MODIFY_EXPR
922 && TREE_OPERAND (sub, 0) == result)
924 ret = sub;
925 if (tsi_one_before_end_p (si))
926 tsi_delink (&si);
927 else
929 /* If there were posteffects after the MODIFY_EXPR,
930 we need a temporary. */
931 tree tmp = create_tmp_var (TREE_TYPE (result), "retval");
932 TREE_OPERAND (ret, 0) = tmp;
933 ret = build (MODIFY_EXPR, TREE_TYPE (result),
934 result, tmp);
936 break;
941 if (ret)
942 TREE_OPERAND (stmt, 0) = ret;
943 else
944 /* The return value must be set up some other way. Just tell
945 expand_return that we're returning the RESULT_DECL. */
946 TREE_OPERAND (stmt, 0) = result;
949 append_to_statement_list (ret_expr, pre_p);
950 return GS_ALL_DONE;
953 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
954 and replacing the LOOP_EXPR with goto, but if the loop contains an
955 EXIT_EXPR, we need to append a label for it to jump to. */
957 static enum gimplify_status
958 gimplify_loop_expr (tree *expr_p, tree *pre_p)
960 tree saved_label = gimplify_ctxp->exit_label;
961 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
962 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
964 append_to_statement_list (start_label, pre_p);
966 gimplify_ctxp->exit_label = NULL_TREE;
968 gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
969 append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
971 if (gimplify_ctxp->exit_label)
973 append_to_statement_list (jump_stmt, pre_p);
974 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
976 else
977 *expr_p = jump_stmt;
979 gimplify_ctxp->exit_label = saved_label;
981 return GS_ALL_DONE;
984 /* Compare two case labels. Because the front end should already have
985 made sure that case ranges do not overlap, it is enough to only compare
986 the CASE_LOW values of each case label. */
988 static int
989 compare_case_labels (const void *p1, const void *p2)
991 tree case1 = *(tree *)p1;
992 tree case2 = *(tree *)p2;
994 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
997 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
998 branch to. */
1000 static enum gimplify_status
1001 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1003 tree switch_expr = *expr_p;
1004 enum gimplify_status ret;
1006 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1007 is_gimple_val, fb_rvalue);
1009 if (SWITCH_BODY (switch_expr))
1011 varray_type labels, saved_labels;
1012 tree label_vec, default_case = NULL_TREE;
1013 size_t i, len;
1015 /* If someone can be bothered to fill in the labels, they can
1016 be bothered to null out the body too. */
1017 if (SWITCH_LABELS (switch_expr))
1018 abort ();
1020 saved_labels = gimplify_ctxp->case_labels;
1021 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1023 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1025 labels = gimplify_ctxp->case_labels;
1026 gimplify_ctxp->case_labels = saved_labels;
1028 len = VARRAY_ACTIVE_SIZE (labels);
1030 for (i = 0; i < len; ++i)
1032 tree t = VARRAY_TREE (labels, i);
1033 if (!CASE_LOW (t))
1035 /* The default case must be the last label in the list. */
1036 default_case = t;
1037 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1038 len--;
1039 break;
1043 label_vec = make_tree_vec (len + 1);
1044 SWITCH_LABELS (*expr_p) = label_vec;
1045 append_to_statement_list (switch_expr, pre_p);
1047 if (! default_case)
1049 /* If the switch has no default label, add one, so that we jump
1050 around the switch body. */
1051 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1052 NULL_TREE, create_artificial_label ());
1053 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1054 *expr_p = build (LABEL_EXPR, void_type_node,
1055 CASE_LABEL (default_case));
1057 else
1058 *expr_p = SWITCH_BODY (switch_expr);
1060 qsort (&VARRAY_TREE (labels, 0), len, sizeof (tree),
1061 compare_case_labels);
1062 for (i = 0; i < len; ++i)
1063 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1064 TREE_VEC_ELT (label_vec, len) = default_case;
1066 SWITCH_BODY (switch_expr) = NULL;
1068 else if (!SWITCH_LABELS (switch_expr))
1069 abort ();
1071 return ret;
1074 static enum gimplify_status
1075 gimplify_case_label_expr (tree *expr_p)
1077 tree expr = *expr_p;
1078 if (gimplify_ctxp->case_labels)
1079 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1080 else
1081 abort ();
1082 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1083 return GS_ALL_DONE;
1086 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1087 a (possibly empty) body. */
1089 static enum gimplify_status
1090 gimplify_labeled_block_expr (tree *expr_p)
1092 tree body = LABELED_BLOCK_BODY (*expr_p);
1093 tree label = LABELED_BLOCK_LABEL (*expr_p);
1094 tree t;
1096 DECL_CONTEXT (label) = current_function_decl;
1097 t = build (LABEL_EXPR, void_type_node, label);
1098 if (body != NULL_TREE)
1099 t = build (COMPOUND_EXPR, void_type_node, body, t);
1100 *expr_p = t;
1102 return GS_OK;
1105 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1107 static enum gimplify_status
1108 gimplify_exit_block_expr (tree *expr_p)
1110 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1111 tree label;
1113 /* First operand must be a LABELED_BLOCK_EXPR, which should
1114 already be lowered (or partially lowered) when we get here. */
1115 #if defined ENABLE_CHECKING
1116 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1117 abort ();
1118 #endif
1120 label = LABELED_BLOCK_LABEL (labeled_block);
1121 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1123 return GS_OK;
1126 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1127 if necessary. */
1129 tree
1130 build_and_jump (tree *label_p)
1132 if (label_p == NULL)
1133 /* If there's nowhere to jump, just fall through. */
1134 return build_empty_stmt ();
1136 if (*label_p == NULL_TREE)
1138 tree label = create_artificial_label ();
1139 *label_p = label;
1142 return build1 (GOTO_EXPR, void_type_node, *label_p);
1145 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1146 This also involves building a label to jump to and communicating it to
1147 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1149 static enum gimplify_status
1150 gimplify_exit_expr (tree *expr_p)
1152 tree cond = TREE_OPERAND (*expr_p, 0);
1153 tree expr;
1155 expr = build_and_jump (&gimplify_ctxp->exit_label);
1156 expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1157 *expr_p = expr;
1159 return GS_OK;
1162 /* A helper function to be called via walk_tree. Mark all labels under *TP
1163 as being forced. To be called for DECL_INITIAL of static variables. */
1165 tree
1166 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1168 if (TYPE_P (*tp))
1169 *walk_subtrees = 0;
1170 if (TREE_CODE (*tp) == LABEL_DECL)
1171 FORCED_LABEL (*tp) = 1;
1173 return NULL_TREE;
1176 /* Break out elements of a constructor used as an initializer into separate
1177 MODIFY_EXPRs.
1179 Note that we still need to clear any elements that don't have explicit
1180 initializers, so if not all elements are initialized we keep the
1181 original MODIFY_EXPR, we just remove all of the constructor elements. */
1183 static enum gimplify_status
1184 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1185 tree *post_p, int want_value)
1187 tree object = TREE_OPERAND (*expr_p, 0);
1188 tree ctor = TREE_OPERAND (*expr_p, 1);
1189 tree type = TREE_TYPE (ctor);
1190 enum gimplify_status ret;
1191 tree elt_list;
1193 if (TREE_CODE (ctor) != CONSTRUCTOR)
1194 return GS_UNHANDLED;
1196 elt_list = CONSTRUCTOR_ELTS (ctor);
1198 ret = GS_ALL_DONE;
1199 switch (TREE_CODE (type))
1201 case RECORD_TYPE:
1202 case UNION_TYPE:
1203 case QUAL_UNION_TYPE:
1204 case ARRAY_TYPE:
1206 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1207 HOST_WIDE_INT num_nonconstant_elements;
1208 bool cleared;
1210 /* Aggregate types must lower constructors to initialization of
1211 individual elements. The exception is that a CONSTRUCTOR node
1212 with no elements indicates zero-initialization of the whole. */
1213 if (elt_list == NULL)
1215 if (want_value)
1217 *expr_p = object;
1218 return GS_OK;
1220 else
1221 return GS_ALL_DONE;
1224 categorize_ctor_elements (ctor, &num_nonzero_elements,
1225 &num_nonconstant_elements);
1226 num_elements = count_type_elements (TREE_TYPE (ctor));
1228 /* If a const aggregate variable is being initialized, then it
1229 should never be a lose to promote the variable to be static. */
1230 if (num_nonconstant_elements == 0
1231 && TREE_READONLY (object)
1232 && TREE_CODE (object) == VAR_DECL)
1234 DECL_INITIAL (object) = ctor;
1235 TREE_STATIC (object) = 1;
1236 if (!DECL_NAME (object))
1237 DECL_NAME (object) = create_tmp_var_name ("C");
1238 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1240 /* ??? C++ doesn't automatically append a .<number> to the
1241 assembler name, and even when it does, it looks a FE private
1242 data structures to figure out what that number should be,
1243 which are not set for this variable. I suppose this is
1244 important for local statics for inline functions, which aren't
1245 "local" in the object file sense. So in order to get a unique
1246 TU-local symbol, we must invoke the lhd version now. */
1247 lhd_set_decl_assembler_name (object);
1249 *expr_p = build_empty_stmt ();
1250 break;
1253 /* If there are "lots" of initialized elements, and all of them
1254 are valid address constants, then the entire initializer can
1255 be dropped to memory, and then memcpy'd out. */
1256 if (num_nonconstant_elements == 0)
1258 HOST_WIDE_INT size = int_size_in_bytes (type);
1259 unsigned int align;
1261 /* ??? We can still get unbounded array types, at least
1262 from the C++ front end. This seems wrong, but attempt
1263 to work around it for now. */
1264 if (size < 0)
1266 size = int_size_in_bytes (TREE_TYPE (object));
1267 if (size >= 0)
1268 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1271 /* Find the maximum alignment we can assume for the object. */
1272 /* ??? Make use of DECL_OFFSET_ALIGN. */
1273 if (DECL_P (object))
1274 align = DECL_ALIGN (object);
1275 else
1276 align = TYPE_ALIGN (type);
1278 if (size > 0 && !can_move_by_pieces (size, align))
1280 tree new = create_tmp_var_raw (type, "C");
1281 gimple_add_tmp_var (new);
1282 TREE_STATIC (new) = 1;
1283 TREE_READONLY (new) = 1;
1284 DECL_INITIAL (new) = ctor;
1285 if (align > DECL_ALIGN (new))
1287 DECL_ALIGN (new) = align;
1288 DECL_USER_ALIGN (new) = 1;
1290 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1292 TREE_OPERAND (*expr_p, 1) = new;
1293 break;
1297 /* If there are "lots" of initialized elements, even discounting
1298 those that are not address constants (and thus *must* be
1299 computed at runtime), then partition the constructor into
1300 constant and non-constant parts. Block copy the constant
1301 parts in, then generate code for the non-constant parts. */
1302 /* TODO. There's code in cp/typeck.c to do this. */
1304 /* If there are "lots" of zeros, then block clear the object first. */
1305 cleared = false;
1306 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1307 && num_nonzero_elements < num_elements/4)
1308 cleared = true;
1310 /* ??? This bit ought not be needed. For any element not present
1311 in the initializer, we should simply set them to zero. Except
1312 we'd need to *find* the elements that are not present, and that
1313 requires trickery to avoid quadratic compile-time behaviour in
1314 large cases or excessive memory use in small cases. */
1315 else
1317 HOST_WIDE_INT len = list_length (elt_list);
1318 if (TREE_CODE (type) == ARRAY_TYPE)
1320 tree nelts = array_type_nelts (type);
1321 if (!host_integerp (nelts, 1)
1322 || tree_low_cst (nelts, 1) != len)
1323 cleared = 1;;
1325 else if (len != fields_length (type))
1326 cleared = 1;
1329 if (cleared)
1331 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1332 append_to_statement_list (*expr_p, pre_p);
1335 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1337 tree purpose, value, cref, init;
1339 purpose = TREE_PURPOSE (elt_list);
1340 value = TREE_VALUE (elt_list);
1342 if (cleared && initializer_zerop (value))
1343 continue;
1345 if (TREE_CODE (type) == ARRAY_TYPE)
1347 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1349 /* ??? Here's to hoping the front end fills in all of the
1350 indicies, so we don't have to figure out what's missing
1351 ourselves. */
1352 if (!purpose)
1353 abort ();
1354 /* ??? Need to handle this. */
1355 if (TREE_CODE (purpose) == RANGE_EXPR)
1356 abort ();
1358 cref = build (ARRAY_REF, t, object, purpose);
1360 else
1362 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1363 object, purpose);
1366 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1367 /* Each member initialization is a full-expression. */
1368 gimplify_stmt (&init);
1369 append_to_statement_list (init, pre_p);
1372 *expr_p = build_empty_stmt ();
1374 break;
1376 case COMPLEX_TYPE:
1378 tree r, i;
1380 /* Extract the real and imaginary parts out of the ctor. */
1381 r = i = NULL_TREE;
1382 if (elt_list)
1384 r = TREE_VALUE (elt_list);
1385 elt_list = TREE_CHAIN (elt_list);
1386 if (elt_list)
1388 i = TREE_VALUE (elt_list);
1389 if (TREE_CHAIN (elt_list))
1390 abort ();
1393 if (r == NULL || i == NULL)
1395 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1396 if (r == NULL)
1397 r = zero;
1398 if (i == NULL)
1399 i = zero;
1402 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1403 represent creation of a complex value. */
1404 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1406 ctor = build_complex (type, r, i);
1407 TREE_OPERAND (*expr_p, 1) = ctor;
1409 else
1411 ctor = build (COMPLEX_EXPR, type, r, i);
1412 TREE_OPERAND (*expr_p, 1) = ctor;
1413 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1414 is_gimple_rhs, fb_rvalue);
1417 break;
1419 case VECTOR_TYPE:
1420 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1421 if (TREE_CONSTANT (ctor))
1422 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1423 else
1425 /* Vector types use CONSTRUCTOR all the way through gimple
1426 compilation as a general initializer. */
1427 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1429 enum gimplify_status tret;
1430 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1431 is_gimple_constructor_elt, fb_rvalue);
1432 if (tret == GS_ERROR)
1433 ret = GS_ERROR;
1436 break;
1438 default:
1439 /* So how did we get a CONSTRUCTOR for a scalar type? */
1440 abort ();
1443 if (ret == GS_ERROR)
1444 return GS_ERROR;
1445 else if (want_value)
1447 append_to_statement_list (*expr_p, pre_p);
1448 *expr_p = object;
1449 return GS_OK;
1451 else
1452 return GS_ALL_DONE;
1455 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1456 different from its canonical type, wrap the whole thing inside a
1457 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1458 type.
1460 The canonical type of a COMPONENT_REF is the type of the field being
1461 referenced--unless the field is a bit-field which can be read directly
1462 in a smaller mode, in which case the canonical type is the
1463 sign-appropriate type corresponding to that mode. */
1465 static void
1466 canonicalize_component_ref (tree *expr_p)
1468 tree expr = *expr_p;
1469 tree type;
1471 if (TREE_CODE (expr) != COMPONENT_REF)
1472 abort ();
1474 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1475 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1476 else
1477 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1479 if (TREE_TYPE (expr) != type)
1481 tree old_type = TREE_TYPE (expr);
1483 /* Set the type of the COMPONENT_REF to the underlying type. */
1484 TREE_TYPE (expr) = type;
1486 /* And wrap the whole thing inside a NOP_EXPR. */
1487 expr = build1 (NOP_EXPR, old_type, expr);
1489 *expr_p = expr;
1493 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1494 to foo, embed that change in the ADDR_EXPR. Lest we perturb the type
1495 system too badly, we must take extra steps to ensure that the ADDR_EXPR
1496 and the addressed object continue to agree on types. */
1497 /* ??? We might could do better if we recognize
1498 T array[N][M];
1499 (T *)&array
1501 &array[0][0];
1504 static void
1505 canonicalize_addr_expr (tree* expr_p)
1507 tree expr = *expr_p;
1508 tree ctype = TREE_TYPE (expr);
1509 tree addr_expr = TREE_OPERAND (expr, 0);
1510 tree atype = TREE_TYPE (addr_expr);
1511 tree dctype, datype, ddatype, otype, obj_expr;
1513 /* Both cast and addr_expr types should be pointers. */
1514 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1515 return;
1517 /* The addr_expr type should be a pointer to an array. */
1518 datype = TREE_TYPE (atype);
1519 if (TREE_CODE (datype) != ARRAY_TYPE)
1520 return;
1522 /* Both cast and addr_expr types should address the same object type. */
1523 dctype = TREE_TYPE (ctype);
1524 ddatype = TREE_TYPE (datype);
1525 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1526 return;
1528 /* The addr_expr and the object type should match. */
1529 obj_expr = TREE_OPERAND (addr_expr, 0);
1530 otype = TREE_TYPE (obj_expr);
1531 if (!lang_hooks.types_compatible_p (otype, datype))
1532 return;
1534 /* All checks succeeded. Build a new node to merge the cast. */
1535 *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1538 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1539 underneath as appropriate. */
1541 static enum gimplify_status
1542 gimplify_conversion (tree *expr_p)
1544 /* Strip away as many useless type conversions as possible
1545 at the toplevel. */
1546 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1548 /* If we still have a conversion at the toplevel, then strip
1549 away all but the outermost conversion. */
1550 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1552 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1554 /* And remove the outermost conversion if it's useless. */
1555 if (tree_ssa_useless_type_conversion (*expr_p))
1556 *expr_p = TREE_OPERAND (*expr_p, 0);
1559 /* If we still have a conversion at the toplevel,
1560 then canonicalize some constructs. */
1561 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1563 tree sub = TREE_OPERAND (*expr_p, 0);
1565 /* If a NOP conversion is changing the type of a COMPONENT_REF
1566 expression, then canonicalize its type now in order to expose more
1567 redundant conversions. */
1568 if (TREE_CODE (sub) == COMPONENT_REF)
1569 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1571 /* If a NOP conversion is changing a pointer to array of foo
1572 to a pointer to foo, embed that change in the ADDR_EXPR. */
1573 else if (TREE_CODE (sub) == ADDR_EXPR)
1574 canonicalize_addr_expr (expr_p);
1577 return GS_OK;
1580 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1582 static enum gimplify_status
1583 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1585 tree op1 = TREE_OPERAND (*expr_p, 0);
1586 tree op2 = TREE_OPERAND (*expr_p, 1);
1587 enum tree_code code;
1588 enum gimplify_status r0, r1;
1590 if (TREE_CODE (*expr_p) == MIN_EXPR)
1591 code = LE_EXPR;
1592 else
1593 code = GE_EXPR;
1595 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1596 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1598 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1599 build (code, boolean_type_node, op1, op2),
1600 op1, op2);
1602 if (r0 == GS_ERROR || r1 == GS_ERROR)
1603 return GS_ERROR;
1604 else
1605 return GS_OK;
1608 /* Build an expression for the address of T. Folds away INDIRECT_REF to
1609 avoid confusing the gimplify process. */
1611 static tree
1612 build_addr_expr_with_type (tree t, tree ptrtype)
1614 if (TREE_CODE (t) == INDIRECT_REF)
1616 t = TREE_OPERAND (t, 0);
1617 if (TREE_TYPE (t) != ptrtype)
1618 t = build1 (NOP_EXPR, ptrtype, t);
1620 else
1622 tree base = t;
1623 while (TREE_CODE (base) == COMPONENT_REF
1624 || TREE_CODE (base) == ARRAY_REF)
1625 base = TREE_OPERAND (base, 0);
1626 if (DECL_P (base))
1627 TREE_ADDRESSABLE (base) = 1;
1629 t = build1 (ADDR_EXPR, ptrtype, t);
1632 return t;
1635 static tree
1636 build_addr_expr (tree t)
1638 return build_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
1641 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1642 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1644 static enum gimplify_status
1645 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1647 tree array = TREE_OPERAND (*expr_p, 0);
1648 tree arrtype = TREE_TYPE (array);
1649 tree elttype = TREE_TYPE (arrtype);
1650 tree size = size_in_bytes (elttype);
1651 tree ptrtype = build_pointer_type (elttype);
1652 enum tree_code add_code = PLUS_EXPR;
1653 tree idx = TREE_OPERAND (*expr_p, 1);
1654 tree minidx, offset, addr, result;
1655 enum gimplify_status ret;
1657 /* If the array domain does not start at zero, apply the offset. */
1658 minidx = TYPE_DOMAIN (arrtype);
1659 if (minidx)
1661 minidx = TYPE_MIN_VALUE (minidx);
1662 if (minidx && !integer_zerop (minidx))
1664 idx = convert (TREE_TYPE (minidx), idx);
1665 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1669 /* If the index is negative -- a technically invalid situation now
1670 that we've biased the index back to zero -- then casting it to
1671 unsigned has ill effects. In particular, -1*4U/4U != -1.
1672 Represent this as a subtraction of a positive rather than addition
1673 of a negative. This will prevent any conversion back to ARRAY_REF
1674 from getting the wrong results from the division. */
1675 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1677 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1678 add_code = MINUS_EXPR;
1681 /* Pointer arithmetic must be done in sizetype. */
1682 idx = convert (sizetype, idx);
1684 /* Convert the index to a byte offset. */
1685 offset = size_binop (MULT_EXPR, size, idx);
1687 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1688 if (ret == GS_ERROR)
1689 return ret;
1691 addr = build_addr_expr_with_type (array, ptrtype);
1692 result = fold (build (add_code, ptrtype, addr, offset));
1693 *expr_p = build1 (INDIRECT_REF, elttype, result);
1695 return GS_OK;
1698 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1699 node pointed by EXPR_P.
1701 compound_lval
1702 : min_lval '[' val ']'
1703 | min_lval '.' ID
1704 | compound_lval '[' val ']'
1705 | compound_lval '.' ID
1707 This is not part of the original SIMPLE definition, which separates
1708 array and member references, but it seems reasonable to handle them
1709 together. Also, this way we don't run into problems with union
1710 aliasing; gcc requires that for accesses through a union to alias, the
1711 union reference must be explicit, which was not always the case when we
1712 were splitting up array and member refs.
1714 PRE_P points to the list where side effects that must happen before
1715 *EXPR_P should be stored.
1717 POST_P points to the list where side effects that must happen after
1718 *EXPR_P should be stored. */
1720 static enum gimplify_status
1721 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1722 tree *post_p, int want_lvalue)
1724 tree *p;
1725 enum tree_code code;
1726 varray_type stack;
1727 enum gimplify_status ret;
1729 #if defined ENABLE_CHECKING
1730 if (TREE_CODE (*expr_p) != ARRAY_REF
1731 && TREE_CODE (*expr_p) != COMPONENT_REF
1732 && TREE_CODE (*expr_p) != REALPART_EXPR
1733 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1734 abort ();
1735 #endif
1737 code = ERROR_MARK; /* [GIMPLE] Avoid uninitialized use warning. */
1739 /* Create a stack of the subexpressions so later we can walk them in
1740 order from inner to outer. */
1741 VARRAY_TREE_INIT (stack, 10, "stack");
1743 for (p = expr_p;
1744 TREE_CODE (*p) == ARRAY_REF
1745 || TREE_CODE (*p) == COMPONENT_REF
1746 || TREE_CODE (*p) == REALPART_EXPR
1747 || TREE_CODE (*p) == IMAGPART_EXPR;
1748 p = &TREE_OPERAND (*p, 0))
1750 code = TREE_CODE (*p);
1751 if (code == ARRAY_REF)
1753 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1754 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1755 /* If the size of the array elements is not constant,
1756 computing the offset is non-trivial, so expose it. */
1757 break;
1759 VARRAY_PUSH_TREE (stack, *p);
1762 /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1763 TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1764 to all the refs we've walked through.
1766 Gimplify the base, and then process each of the outer nodes from left
1767 to right. */
1768 ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1769 code != ARRAY_REF ? fb_either : fb_lvalue);
1771 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1773 tree t = VARRAY_TOP_TREE (stack);
1774 if (TREE_CODE (t) == ARRAY_REF)
1776 /* Gimplify the dimension. */
1777 enum gimplify_status tret;
1778 /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1779 Gimplify non-constant array indices into a temporary
1780 variable.
1781 FIXME - The real fix is to gimplify post-modify
1782 expressions into a minimal gimple lvalue. However, that
1783 exposes bugs in alias analysis. The alias analyzer does
1784 not handle &PTR->FIELD very well. Will fix after the
1785 branch is merged into mainline (dnovillo 2004-05-03). */
1786 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1788 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1789 is_gimple_tmp_var, fb_rvalue);
1790 if (tret == GS_ERROR)
1791 ret = GS_ERROR;
1794 recalculate_side_effects (t);
1795 VARRAY_POP (stack);
1798 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1799 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1801 canonicalize_component_ref (expr_p);
1802 ret = MIN (ret, GS_OK);
1805 return ret;
1808 /* Re-write the ARRAY_REF node pointed by EXPR_P.
1810 PRE_P points to the list where side effects that must happen before
1811 *EXPR_P should be stored.
1813 POST_P points to the list where side effects that must happen after
1814 *EXPR_P should be stored.
1816 FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1817 argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1818 ARRAY_REF should be extended. */
1820 static enum gimplify_status
1821 gimplify_array_ref (tree *expr_p, tree *pre_p,
1822 tree *post_p, int want_lvalue)
1824 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1825 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1826 /* If the size of the array elements is not constant,
1827 computing the offset is non-trivial, so expose it. */
1828 return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1829 else
1830 /* Handle array and member refs together for now. When alias analysis
1831 improves, we may want to go back to handling them separately. */
1832 return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1835 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1837 PRE_P points to the list where side effects that must happen before
1838 *EXPR_P should be stored.
1840 POST_P points to the list where side effects that must happen after
1841 *EXPR_P should be stored.
1843 WANT_VALUE is nonzero iff we want to use the value of this expression
1844 in another expression. */
1846 static enum gimplify_status
1847 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1848 int want_value)
1850 enum tree_code code;
1851 tree lhs, lvalue, rhs, t1;
1852 bool postfix;
1853 enum tree_code arith_code;
1854 enum gimplify_status ret;
1856 code = TREE_CODE (*expr_p);
1858 #if defined ENABLE_CHECKING
1859 if (code != POSTINCREMENT_EXPR
1860 && code != POSTDECREMENT_EXPR
1861 && code != PREINCREMENT_EXPR
1862 && code != PREDECREMENT_EXPR)
1863 abort ();
1864 #endif
1866 /* Prefix or postfix? */
1867 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1868 /* Faster to treat as prefix if result is not used. */
1869 postfix = want_value;
1870 else
1871 postfix = false;
1873 /* Add or subtract? */
1874 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1875 arith_code = PLUS_EXPR;
1876 else
1877 arith_code = MINUS_EXPR;
1879 /* Gimplify the LHS into a GIMPLE lvalue. */
1880 lvalue = TREE_OPERAND (*expr_p, 0);
1881 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1882 if (ret == GS_ERROR)
1883 return ret;
1885 /* Extract the operands to the arithmetic operation. */
1886 lhs = lvalue;
1887 rhs = TREE_OPERAND (*expr_p, 1);
1889 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1890 that as the result value and in the postqueue operation. */
1891 if (postfix)
1893 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1894 if (ret == GS_ERROR)
1895 return ret;
1898 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1899 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1901 if (postfix)
1903 gimplify_stmt (&t1);
1904 append_to_statement_list (t1, post_p);
1905 *expr_p = lhs;
1906 return GS_ALL_DONE;
1908 else
1910 *expr_p = t1;
1911 return GS_OK;
1915 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
1917 call_expr
1918 : ID '(' arglist ')'
1920 arglist
1921 : arglist ',' val
1922 | val
1924 PRE_P points to the list where side effects that must happen before
1925 *EXPR_P should be stored. */
1927 static enum gimplify_status
1928 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1930 tree decl;
1931 tree arglist;
1932 enum gimplify_status ret;
1934 #if defined ENABLE_CHECKING
1935 if (TREE_CODE (*expr_p) != CALL_EXPR)
1936 abort ();
1937 #endif
1939 /* For reliable diagnostics during inlining, it is necessary that
1940 every call_expr be annotated with file and line. */
1941 if (!EXPR_LOCUS (*expr_p))
1942 annotate_with_locus (*expr_p, input_location);
1944 /* This may be a call to a builtin function.
1946 Builtin function calls may be transformed into different
1947 (and more efficient) builtin function calls under certain
1948 circumstances. Unfortunately, gimplification can muck things
1949 up enough that the builtin expanders are not aware that certain
1950 transformations are still valid.
1952 So we attempt transformation/gimplification of the call before
1953 we gimplify the CALL_EXPR. At this time we do not manage to
1954 transform all calls in the same manner as the expanders do, but
1955 we do transform most of them. */
1956 decl = get_callee_fndecl (*expr_p);
1957 if (decl && DECL_BUILT_IN (decl))
1959 tree new;
1961 /* If it is allocation of stack, record the need to restore the memory
1962 when the enclosing bind_expr is exited. */
1963 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1964 gimplify_ctxp->save_stack = true;
1966 /* If it is restore of the stack, reset it, since it means we are
1967 regimplifying the bind_expr. Note that we use the fact that
1968 for try_finally_expr, try part is processed first. */
1969 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1970 gimplify_ctxp->save_stack = false;
1972 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
1974 if (new && new != *expr_p)
1976 /* There was a transformation of this call which computes the
1977 same value, but in a more efficient way. Return and try
1978 again. */
1979 *expr_p = new;
1980 return GS_OK;
1984 /* There is a sequence point before the call, so any side effects in
1985 the calling expression must occur before the actual call. Force
1986 gimplify_expr to use an internal post queue. */
1987 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1988 is_gimple_val, fb_rvalue);
1990 if (PUSH_ARGS_REVERSED)
1991 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1992 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1993 arglist = TREE_CHAIN (arglist))
1995 enum gimplify_status t;
1997 /* There is a sequence point before a function call. Side effects in
1998 the argument list must occur before the actual call. So, when
1999 gimplifying arguments, force gimplify_expr to use an internal
2000 post queue which is then appended to the end of PRE_P. */
2001 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2002 fb_rvalue);
2004 if (t == GS_ERROR)
2005 ret = GS_ERROR;
2007 if (PUSH_ARGS_REVERSED)
2008 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2010 /* Try this again in case gimplification exposed something. */
2011 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2013 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2015 if (new && new != *expr_p)
2017 /* There was a transformation of this call which computes the
2018 same value, but in a more efficient way. Return and try
2019 again. */
2020 *expr_p = new;
2021 return GS_OK;
2025 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2026 decl. This allows us to eliminate redundant or useless
2027 calls to "const" functions. */
2028 if (TREE_CODE (*expr_p) == CALL_EXPR
2029 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2030 TREE_SIDE_EFFECTS (*expr_p) = 0;
2032 return ret;
2035 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2036 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2038 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2039 condition is true or false, respectively. If null, we should generate
2040 our own to skip over the evaluation of this specific expression.
2042 This function is the tree equivalent of do_jump.
2044 shortcut_cond_r should only be called by shortcut_cond_expr. */
2046 static tree
2047 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2049 tree local_label = NULL_TREE;
2050 tree t, expr = NULL;
2052 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2053 retain the shortcut semantics. Just insert the gotos here;
2054 shortcut_cond_expr will append the real blocks later. */
2055 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2057 /* Turn if (a && b) into
2059 if (a); else goto no;
2060 if (b) goto yes; else goto no;
2061 (no:) */
2063 if (false_label_p == NULL)
2064 false_label_p = &local_label;
2066 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2067 append_to_statement_list (t, &expr);
2069 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2070 false_label_p);
2071 append_to_statement_list (t, &expr);
2073 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2075 /* Turn if (a || b) into
2077 if (a) goto yes;
2078 if (b) goto yes; else goto no;
2079 (yes:) */
2081 if (true_label_p == NULL)
2082 true_label_p = &local_label;
2084 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2085 append_to_statement_list (t, &expr);
2087 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2088 false_label_p);
2089 append_to_statement_list (t, &expr);
2091 else if (TREE_CODE (pred) == COND_EXPR)
2093 /* As long as we're messing with gotos, turn if (a ? b : c) into
2094 if (a)
2095 if (b) goto yes; else goto no;
2096 else
2097 if (c) goto yes; else goto no; */
2098 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2099 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2100 false_label_p),
2101 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2102 false_label_p));
2104 else
2106 expr = build (COND_EXPR, void_type_node, pred,
2107 build_and_jump (true_label_p),
2108 build_and_jump (false_label_p));
2111 if (local_label)
2113 t = build1 (LABEL_EXPR, void_type_node, local_label);
2114 append_to_statement_list (t, &expr);
2117 return expr;
2120 static tree
2121 shortcut_cond_expr (tree expr)
2123 tree pred = TREE_OPERAND (expr, 0);
2124 tree then_ = TREE_OPERAND (expr, 1);
2125 tree else_ = TREE_OPERAND (expr, 2);
2126 tree true_label, false_label, end_label, t;
2127 tree *true_label_p;
2128 tree *false_label_p;
2129 bool emit_end, emit_false;
2131 /* First do simple transformations. */
2132 if (!TREE_SIDE_EFFECTS (else_))
2134 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2135 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2137 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2138 then_ = shortcut_cond_expr (expr);
2139 pred = TREE_OPERAND (pred, 0);
2140 expr = build (COND_EXPR, void_type_node, pred, then_,
2141 build_empty_stmt ());
2144 if (!TREE_SIDE_EFFECTS (then_))
2146 /* If there is no 'then', turn
2147 if (a || b); else d
2148 into
2149 if (a); else if (b); else d. */
2150 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2152 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2153 else_ = shortcut_cond_expr (expr);
2154 pred = TREE_OPERAND (pred, 0);
2155 expr = build (COND_EXPR, void_type_node, pred,
2156 build_empty_stmt (), else_);
2160 /* If we're done, great. */
2161 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2162 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2163 return expr;
2165 /* Otherwise we need to mess with gotos. Change
2166 if (a) c; else d;
2168 if (a); else goto no;
2169 c; goto end;
2170 no: d; end:
2171 and recursively gimplify the condition. */
2173 true_label = false_label = end_label = NULL_TREE;
2175 /* If our arms just jump somewhere, hijack those labels so we don't
2176 generate jumps to jumps. */
2178 if (TREE_CODE (then_) == GOTO_EXPR
2179 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2181 true_label = GOTO_DESTINATION (then_);
2182 then_ = build_empty_stmt ();
2185 if (TREE_CODE (else_) == GOTO_EXPR
2186 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2188 false_label = GOTO_DESTINATION (else_);
2189 else_ = build_empty_stmt ();
2192 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2193 if (true_label)
2194 true_label_p = &true_label;
2195 else
2196 true_label_p = NULL;
2198 /* The 'else' branch also needs a label if it contains interesting code. */
2199 if (false_label || TREE_SIDE_EFFECTS (else_))
2200 false_label_p = &false_label;
2201 else
2202 false_label_p = NULL;
2204 /* If there was nothing else in our arms, just forward the label(s). */
2205 if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2206 return shortcut_cond_r (pred, true_label_p, false_label_p);
2208 /* If our last subexpression already has a terminal label, reuse it. */
2209 if (TREE_SIDE_EFFECTS (else_))
2210 expr = expr_last (else_);
2211 else
2212 expr = expr_last (then_);
2213 if (TREE_CODE (expr) == LABEL_EXPR)
2214 end_label = LABEL_EXPR_LABEL (expr);
2216 /* If we don't care about jumping to the 'else' branch, jump to the end
2217 if the condition is false. */
2218 if (!false_label_p)
2219 false_label_p = &end_label;
2221 /* We only want to emit these labels if we aren't hijacking them. */
2222 emit_end = (end_label == NULL_TREE);
2223 emit_false = (false_label == NULL_TREE);
2225 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2227 expr = NULL;
2228 append_to_statement_list (pred, &expr);
2230 append_to_statement_list (then_, &expr);
2231 if (TREE_SIDE_EFFECTS (else_))
2233 t = build_and_jump (&end_label);
2234 append_to_statement_list (t, &expr);
2235 if (emit_false)
2237 t = build1 (LABEL_EXPR, void_type_node, false_label);
2238 append_to_statement_list (t, &expr);
2240 append_to_statement_list (else_, &expr);
2242 if (emit_end && end_label)
2244 t = build1 (LABEL_EXPR, void_type_node, end_label);
2245 append_to_statement_list (t, &expr);
2248 return expr;
2251 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2253 static tree
2254 gimple_boolify (tree expr)
2256 tree type = TREE_TYPE (expr);
2258 if (TREE_CODE (type) == BOOLEAN_TYPE)
2259 return expr;
2261 /* If this is the predicate of a COND_EXPR, it might not even be a
2262 truthvalue yet. */
2263 expr = lang_hooks.truthvalue_conversion (expr);
2265 switch (TREE_CODE (expr))
2267 case TRUTH_AND_EXPR:
2268 case TRUTH_OR_EXPR:
2269 case TRUTH_XOR_EXPR:
2270 case TRUTH_ANDIF_EXPR:
2271 case TRUTH_ORIF_EXPR:
2272 /* Also boolify the arguments of truth exprs. */
2273 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2274 /* FALLTHRU */
2276 case TRUTH_NOT_EXPR:
2277 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2278 /* FALLTHRU */
2280 case EQ_EXPR: case NE_EXPR:
2281 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2282 /* These expressions always produce boolean results. */
2283 TREE_TYPE (expr) = boolean_type_node;
2284 return expr;
2286 default:
2287 /* Other expressions that get here must have boolean values, but
2288 might need to be converted to the appropriate mode. */
2289 return convert (boolean_type_node, expr);
2293 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2294 into
2296 if (p) if (p)
2297 t1 = a; a;
2298 else or else
2299 t1 = b; b;
2302 The second form is used when *EXPR_P is of type void.
2304 PRE_P points to the list where side effects that must happen before
2305 *EXPR_P should be stored. */
2307 static enum gimplify_status
2308 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2310 tree expr = *expr_p;
2311 tree tmp;
2312 enum gimplify_status ret;
2314 /* If this COND_EXPR has a value, copy the values into a temporary within
2315 the arms. */
2316 if (! VOID_TYPE_P (TREE_TYPE (expr)))
2318 if (target)
2320 tmp = target;
2321 ret = GS_OK;
2323 else
2325 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2326 ret = GS_ALL_DONE;
2329 /* Build the then clause, 't1 = a;'. But don't build an assignment
2330 if this branch is void; in C++ it can be, if it's a throw. */
2331 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2332 TREE_OPERAND (expr, 1)
2333 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2335 /* Build the else clause, 't1 = b;'. */
2336 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2337 TREE_OPERAND (expr, 2)
2338 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2340 TREE_TYPE (expr) = void_type_node;
2341 recalculate_side_effects (expr);
2343 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2344 gimplify_stmt (&expr);
2345 append_to_statement_list (expr, pre_p);
2346 *expr_p = tmp;
2348 return ret;
2351 /* Make sure the condition has BOOLEAN_TYPE. */
2352 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2354 /* Break apart && and || conditions. */
2355 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2356 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2358 expr = shortcut_cond_expr (expr);
2360 if (expr != *expr_p)
2362 *expr_p = expr;
2364 /* We can't rely on gimplify_expr to re-gimplify the expanded
2365 form properly, as cleanups might cause the target labels to be
2366 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2367 set up a conditional context. */
2368 gimple_push_condition ();
2369 gimplify_stmt (expr_p);
2370 gimple_pop_condition (pre_p);
2372 return GS_ALL_DONE;
2376 /* Now do the normal gimplification. */
2377 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2378 is_gimple_condexpr, fb_rvalue);
2380 gimple_push_condition ();
2382 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2383 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2384 recalculate_side_effects (expr);
2386 gimple_pop_condition (pre_p);
2388 if (ret == GS_ERROR)
2390 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2391 ret = GS_ALL_DONE;
2392 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2393 /* Rewrite "if (a); else b" to "if (!a) b" */
2395 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2396 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2397 is_gimple_condexpr, fb_rvalue);
2399 tmp = TREE_OPERAND (expr, 1);
2400 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2401 TREE_OPERAND (expr, 2) = tmp;
2403 else
2404 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2405 expr = TREE_OPERAND (expr, 0);
2407 *expr_p = expr;
2408 return ret;
2411 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2413 modify_expr
2414 : varname '=' rhs
2415 | '*' ID '=' rhs
2417 PRE_P points to the list where side effects that must happen before
2418 *EXPR_P should be stored.
2420 POST_P points to the list where side effects that must happen after
2421 *EXPR_P should be stored.
2423 WANT_VALUE is nonzero iff we want to use the value of this expression
2424 in another expression. */
2426 static enum gimplify_status
2427 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2429 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2430 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2431 enum gimplify_status ret;
2433 #if defined ENABLE_CHECKING
2434 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2435 abort ();
2436 #endif
2438 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2439 if (TREE_CODE (*expr_p) == INIT_EXPR)
2440 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2442 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2443 if (ret == GS_ERROR)
2444 return ret;
2446 /* If we are initializing something from a TARGET_EXPR, strip the
2447 TARGET_EXPR and initialize it directly. */
2448 /* What about code that pulls out the temp and uses it elsewhere? I
2449 think that such code never uses the TARGET_EXPR as an initializer. If
2450 I'm wrong, we'll abort because the temp won't have any RTL. In that
2451 case, I guess we'll need to replace references somehow. */
2452 if (TREE_CODE (*from_p) == TARGET_EXPR)
2453 *from_p = TARGET_EXPR_INITIAL (*from_p);
2455 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2456 the assignment down into the branches, since we can't generate a
2457 temporary of such a type. */
2458 if (TREE_CODE (*from_p) == COND_EXPR
2459 && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2461 *expr_p = *from_p;
2462 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2465 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2466 if (ret == GS_ERROR)
2467 return ret;
2469 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2470 if (ret != GS_UNHANDLED)
2471 return ret;
2473 /* If the destination is already simple, nothing else needed. */
2474 if (is_gimple_tmp_var (*to_p))
2475 ret = GS_ALL_DONE;
2476 else
2478 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2479 the LHS is a user variable, then we need to introduce a temporary.
2480 ie temp = RHS; LHS = temp.
2482 This way the optimizers can determine that the user variable is
2483 only modified if evaluation of the RHS does not throw.
2485 FIXME this should be handled by the is_gimple_rhs predicate. */
2487 if (TREE_CODE (*from_p) == CALL_EXPR
2488 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2489 /* If we're dealing with a renamable type, either source or dest
2490 must be a renamed variable. */
2491 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2492 && !is_gimple_reg (*to_p)))
2493 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2495 /* If the value being copied is of variable width, expose the length
2496 if the copy by converting the whole thing to a memcpy. */
2497 /* ??? Except that we can't manage this with VA_ARG_EXPR. Yes, this
2498 does leave us with an edge condition that doesn't work. The only
2499 way out is to rearrange how VA_ARG_EXPR works. */
2500 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2501 && TREE_CODE (*from_p) != VA_ARG_EXPR)
2503 tree args, t, dest;
2505 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2506 t = unshare_expr (t);
2507 args = tree_cons (NULL, t, NULL);
2508 t = build_addr_expr (*from_p);
2509 args = tree_cons (NULL, t, args);
2510 dest = build_addr_expr (*to_p);
2511 args = tree_cons (NULL, dest, args);
2512 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2513 t = build_function_call_expr (t, args);
2514 if (want_value)
2516 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2517 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2519 *expr_p = t;
2521 return GS_OK;
2524 ret = want_value ? GS_OK : GS_ALL_DONE;
2527 if (want_value)
2529 append_to_statement_list (*expr_p, pre_p);
2530 *expr_p = *to_p;
2533 return ret;
2536 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2537 points to the expression to gimplify.
2539 Expressions of the form 'a && b' are gimplified to:
2541 a && b ? true : false
2543 gimplify_cond_expr will do the rest.
2545 PRE_P points to the list where side effects that must happen before
2546 *EXPR_P should be stored. */
2548 static enum gimplify_status
2549 gimplify_boolean_expr (tree *expr_p)
2551 /* Preserve the original type of the expression. */
2552 tree type = TREE_TYPE (*expr_p);
2554 *expr_p = build (COND_EXPR, type, *expr_p,
2555 convert (type, boolean_true_node),
2556 convert (type, boolean_false_node));
2558 return GS_OK;
2561 /* Gimplifies an expression sequence. This function gimplifies each
2562 expression and re-writes the original expression with the last
2563 expression of the sequence in GIMPLE form.
2565 PRE_P points to the list where the side effects for all the
2566 expressions in the sequence will be emitted.
2568 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2569 /* ??? Should rearrange to share the pre-queue with all the indirect
2570 invocations of gimplify_expr. Would probably save on creations
2571 of statement_list nodes. */
2573 static enum gimplify_status
2574 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2576 tree t = *expr_p;
2580 tree *sub_p = &TREE_OPERAND (t, 0);
2582 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2583 gimplify_compound_expr (sub_p, pre_p, false);
2584 else
2585 gimplify_stmt (sub_p);
2586 append_to_statement_list (*sub_p, pre_p);
2588 t = TREE_OPERAND (t, 1);
2590 while (TREE_CODE (t) == COMPOUND_EXPR);
2592 *expr_p = t;
2593 if (want_value)
2594 return GS_OK;
2595 else
2597 gimplify_stmt (expr_p);
2598 return GS_ALL_DONE;
2602 /* Gimplifies a statement list. These may be created either by an
2603 enlightend front-end, or by shortcut_cond_expr. */
2605 static enum gimplify_status
2606 gimplify_statement_list (tree *expr_p)
2608 tree_stmt_iterator i = tsi_start (*expr_p);
2610 while (!tsi_end_p (i))
2612 tree t;
2614 gimplify_stmt (tsi_stmt_ptr (i));
2616 t = tsi_stmt (i);
2617 if (TREE_CODE (t) == STATEMENT_LIST)
2619 tsi_link_before (&i, t, TSI_SAME_STMT);
2620 tsi_delink (&i);
2622 else
2623 tsi_next (&i);
2626 return GS_ALL_DONE;
2629 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2630 gimplify. After gimplification, EXPR_P will point to a new temporary
2631 that holds the original value of the SAVE_EXPR node.
2633 PRE_P points to the list where side effects that must happen before
2634 *EXPR_P should be stored. */
2636 static enum gimplify_status
2637 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2639 enum gimplify_status ret = GS_ALL_DONE;
2640 tree val;
2642 #if defined ENABLE_CHECKING
2643 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2644 abort ();
2645 #endif
2647 val = TREE_OPERAND (*expr_p, 0);
2649 /* If the operand is already a GIMPLE temporary, just re-write the
2650 SAVE_EXPR node. */
2651 if (is_gimple_tmp_var (val))
2652 *expr_p = val;
2653 /* The operand may be a void-valued expression such as SAVE_EXPRs
2654 generated by the Java frontend for class initialization. It is
2655 being executed only for its side-effects. */
2656 else if (TREE_TYPE (val) == void_type_node)
2658 tree body = TREE_OPERAND (*expr_p, 0);
2659 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2660 append_to_statement_list (body, pre_p);
2661 *expr_p = build_empty_stmt ();
2663 else
2664 *expr_p = TREE_OPERAND (*expr_p, 0)
2665 = get_initialized_tmp_var (val, pre_p, post_p);
2667 return ret;
2670 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2672 unary_expr
2673 : ...
2674 | '&' varname
2677 PRE_P points to the list where side effects that must happen before
2678 *EXPR_P should be stored.
2680 POST_P points to the list where side effects that must happen after
2681 *EXPR_P should be stored. */
2683 static enum gimplify_status
2684 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2686 tree expr = *expr_p;
2687 tree op0 = TREE_OPERAND (expr, 0);
2688 enum gimplify_status ret;
2690 switch (TREE_CODE (op0))
2692 case INDIRECT_REF:
2693 /* Check if we are dealing with an expression of the form '&*ptr'.
2694 While the front end folds away '&*ptr' into 'ptr', these
2695 expressions may be generated internally by the compiler (e.g.,
2696 builtins like __builtin_va_end). */
2697 *expr_p = TREE_OPERAND (op0, 0);
2698 ret = GS_OK;
2699 break;
2701 case ARRAY_REF:
2702 /* Fold &a[6] to (&a + 6). */
2703 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2704 pre_p, post_p);
2706 /* This added an INDIRECT_REF. Fold it away. */
2707 op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2709 *expr_p = op0;
2710 break;
2712 default:
2713 /* We use fb_either here because the C frontend sometimes takes
2714 the address of a call that returns a struct. */
2715 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2716 is_gimple_addr_expr_arg, fb_either);
2717 if (ret != GS_ERROR)
2719 /* At this point, the argument of the ADDR_EXPR should be
2720 sufficiently simple that there are never side effects. */
2721 /* ??? Could split out the decision code from build1 to verify. */
2722 TREE_SIDE_EFFECTS (expr) = 0;
2724 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2725 recompute_tree_invarant_for_addr_expr (expr);
2727 /* Mark the RHS addressable. */
2728 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2730 break;
2733 /* If the operand is gimplified into a _DECL, mark the address expression
2734 as TREE_INVARIANT. */
2735 if (DECL_P (TREE_OPERAND (expr, 0)))
2736 TREE_INVARIANT (expr) = 1;
2738 return ret;
2741 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2742 value; output operands should be a gimple lvalue. */
2744 static enum gimplify_status
2745 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2747 tree expr = *expr_p;
2748 int noutputs = list_length (ASM_OUTPUTS (expr));
2749 const char **oconstraints
2750 = (const char **) alloca ((noutputs) * sizeof (const char *));
2751 int i;
2752 tree link;
2753 const char *constraint;
2754 bool allows_mem, allows_reg, is_inout;
2755 enum gimplify_status ret, tret;
2757 ASM_STRING (expr)
2758 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2759 ASM_INPUTS (expr));
2761 ret = GS_ALL_DONE;
2762 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2764 oconstraints[i] = constraint
2765 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2767 parse_output_constraint (&constraint, i, 0, 0,
2768 &allows_mem, &allows_reg, &is_inout);
2770 if (!allows_reg && allows_mem)
2771 lang_hooks.mark_addressable (TREE_VALUE (link));
2773 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2774 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2775 fb_lvalue | fb_mayfail);
2776 if (tret == GS_ERROR)
2778 error ("invalid lvalue in asm output %d", i);
2779 ret = tret;
2782 if (is_inout)
2784 /* An input/output operand. To give the optimizers more
2785 flexibility, split it into separate input and output
2786 operands. */
2787 tree input;
2788 char buf[10];
2789 size_t constraint_len = strlen (constraint);
2791 /* Turn the in/out constraint into an output constraint. */
2792 char *p = xstrdup (constraint);
2793 p[0] = '=';
2794 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2795 free (p);
2797 /* And add a matching input constraint. */
2798 if (allows_reg)
2800 sprintf (buf, "%d", i);
2801 input = build_string (strlen (buf), buf);
2803 else
2804 input = build_string (constraint_len - 1, constraint + 1);
2805 input = build_tree_list (build_tree_list (NULL_TREE, input),
2806 unshare_expr (TREE_VALUE (link)));
2807 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2811 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2813 constraint
2814 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2815 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2816 oconstraints, &allows_mem, &allows_reg);
2818 /* If the operand is a memory input, it should be an lvalue. */
2819 if (!allows_reg && allows_mem)
2821 lang_hooks.mark_addressable (TREE_VALUE (link));
2822 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2823 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2824 if (tret == GS_ERROR)
2826 error ("memory input %d is not directly addressable", i);
2827 ret = tret;
2830 else
2832 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2833 is_gimple_val, fb_rvalue);
2834 if (tret == GS_ERROR)
2835 ret = tret;
2839 return ret;
2842 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
2843 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2844 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2845 return to this function.
2847 FIXME should we complexify the prequeue handling instead? Or use flags
2848 for all the cleanups and let the optimizer tighten them up? The current
2849 code seems pretty fragile; it will break on a cleanup within any
2850 non-conditional nesting. But any such nesting would be broken, anyway;
2851 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2852 and continues out of it. We can do that at the RTL level, though, so
2853 having an optimizer to tighten up try/finally regions would be a Good
2854 Thing. */
2856 static enum gimplify_status
2857 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2859 tree_stmt_iterator iter;
2860 tree body;
2862 tree temp = voidify_wrapper_expr (*expr_p);
2864 /* We only care about the number of conditions between the innermost
2865 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
2866 int old_conds = gimplify_ctxp->conditions;
2867 gimplify_ctxp->conditions = 0;
2869 body = TREE_OPERAND (*expr_p, 0);
2870 gimplify_to_stmt_list (&body);
2872 gimplify_ctxp->conditions = old_conds;
2874 for (iter = tsi_start (body); !tsi_end_p (iter); )
2876 tree *wce_p = tsi_stmt_ptr (iter);
2877 tree wce = *wce_p;
2879 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2881 if (tsi_one_before_end_p (iter))
2883 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2884 tsi_delink (&iter);
2885 break;
2887 else
2889 tree sl, tfe;
2891 sl = tsi_split_statement_list_after (&iter);
2892 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2893 append_to_statement_list (TREE_OPERAND (wce, 1),
2894 &TREE_OPERAND (tfe, 1));
2895 *wce_p = tfe;
2896 iter = tsi_start (sl);
2899 else
2900 tsi_next (&iter);
2903 if (temp)
2905 *expr_p = temp;
2906 append_to_statement_list (body, pre_p);
2907 return GS_OK;
2909 else
2911 *expr_p = body;
2912 return GS_ALL_DONE;
2916 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
2917 is the cleanup action required. */
2919 static void
2920 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2922 tree wce;
2924 /* Errors can result in improperly nested cleanups. Which results in
2925 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
2926 if (errorcount || sorrycount)
2927 return;
2929 if (gimple_conditional_context ())
2931 /* If we're in a conditional context, this is more complex. We only
2932 want to run the cleanup if we actually ran the initialization that
2933 necessitates it, but we want to run it after the end of the
2934 conditional context. So we wrap the try/finally around the
2935 condition and use a flag to determine whether or not to actually
2936 run the destructor. Thus
2938 test ? f(A()) : 0
2940 becomes (approximately)
2942 flag = 0;
2943 try {
2944 if (test) { A::A(temp); flag = 1; val = f(temp); }
2945 else { val = 0; }
2946 } finally {
2947 if (flag) A::~A(temp);
2952 tree flag = create_tmp_var (boolean_type_node, "cleanup");
2953 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2954 boolean_false_node);
2955 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2956 boolean_true_node);
2957 cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2958 build_empty_stmt ());
2959 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2960 cleanup, NULL_TREE);
2961 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2962 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2963 append_to_statement_list (ftrue, pre_p);
2965 /* Because of this manipulation, and the EH edges that jump
2966 threading cannot redirect, the temporary (VAR) will appear
2967 to be used uninitialized. Don't warn. */
2968 TREE_NO_WARNING (var) = 1;
2970 else
2972 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2973 cleanup, NULL_TREE);
2974 append_to_statement_list (wce, pre_p);
2977 gimplify_stmt (&TREE_OPERAND (wce, 1));
2980 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
2982 static enum gimplify_status
2983 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
2985 tree targ = *expr_p;
2986 tree temp = TARGET_EXPR_SLOT (targ);
2987 tree init = TARGET_EXPR_INITIAL (targ);
2988 enum gimplify_status ret;
2990 if (init)
2992 /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
2993 temps list. */
2994 gimple_add_tmp_var (temp);
2996 /* Build up the initialization and add it to pre_p. */
2997 init = build (MODIFY_EXPR, void_type_node, temp, init);
2998 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
2999 if (ret == GS_ERROR)
3000 return GS_ERROR;
3002 append_to_statement_list (init, pre_p);
3004 /* If needed, push the cleanup for the temp. */
3005 if (TARGET_EXPR_CLEANUP (targ))
3007 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3008 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3011 /* Only expand this once. */
3012 TREE_OPERAND (targ, 3) = init;
3013 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3015 else if (!temp->decl.seen_in_bind_expr)
3016 /* We should have expanded this before. */
3017 abort ();
3019 *expr_p = temp;
3020 return GS_OK;
3023 /* Gimplification of expression trees. */
3025 /* Gimplify an expression which appears at statement context; usually, this
3026 means replacing it with a suitably gimple COMPOUND_EXPR. */
3028 void
3029 gimplify_stmt (tree *stmt_p)
3031 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3032 if (!*stmt_p)
3033 *stmt_p = alloc_stmt_list ();
3036 /* Similarly, but force the result to be a STATEMENT_LIST. */
3038 void
3039 gimplify_to_stmt_list (tree *stmt_p)
3041 gimplify_stmt (stmt_p);
3042 if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3044 tree t = *stmt_p;
3045 *stmt_p = NULL;
3046 append_to_statement_list (t, stmt_p);
3051 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3052 gimplification failed.
3054 PRE_P points to the list where side effects that must happen before
3055 EXPR should be stored.
3057 POST_P points to the list where side effects that must happen after
3058 EXPR should be stored, or NULL if there is no suitable list. In
3059 that case, we copy the result to a temporary, emit the
3060 post-effects, and then return the temporary.
3062 GIMPLE_TEST_F points to a function that takes a tree T and
3063 returns nonzero if T is in the GIMPLE form requested by the
3064 caller. The GIMPLE predicates are in tree-gimple.c.
3066 This test is used twice. Before gimplification, the test is
3067 invoked to determine whether *EXPR_P is already gimple enough. If
3068 that fails, *EXPR_P is gimplified according to its code and
3069 GIMPLE_TEST_F is called again. If the test still fails, then a new
3070 temporary variable is created and assigned the value of the
3071 gimplified expression.
3073 FALLBACK tells the function what sort of a temporary we want. If the 1
3074 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3075 If both are set, either is OK, but an lvalue is preferable.
3077 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3078 iterates until solution. */
3080 enum gimplify_status
3081 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3082 bool (* gimple_test_f) (tree), fallback_t fallback)
3084 tree tmp;
3085 tree internal_pre = NULL_TREE;
3086 tree internal_post = NULL_TREE;
3087 tree save_expr;
3088 int is_statement = (pre_p == NULL);
3089 location_t *locus;
3090 location_t saved_location;
3091 enum gimplify_status ret;
3093 save_expr = *expr_p;
3094 if (save_expr == NULL_TREE)
3095 return GS_ALL_DONE;
3097 /* We used to check the predicate here and return immediately if it
3098 succeeds. This is wrong; the design is for gimplification to be
3099 idempotent, and for the predicates to only test for valid forms, not
3100 whether they are fully simplified. */
3102 /* Set up our internal queues if needed. */
3103 if (pre_p == NULL)
3104 pre_p = &internal_pre;
3105 if (post_p == NULL)
3106 post_p = &internal_post;
3108 saved_location = input_location;
3109 if (save_expr == error_mark_node)
3110 locus = NULL;
3111 else
3112 locus = EXPR_LOCUS (save_expr);
3113 if (locus)
3114 input_location = *locus;
3116 /* Loop over the specific gimplifiers until the toplevel node
3117 remains the same. */
3120 /* Strip any uselessness. */
3121 STRIP_MAIN_TYPE_NOPS (*expr_p);
3123 /* Remember the expr. */
3124 save_expr = *expr_p;
3126 /* Die, die, die, my darling. */
3127 if (save_expr == error_mark_node
3128 || TREE_TYPE (save_expr) == error_mark_node)
3130 ret = GS_ERROR;
3131 break;
3134 /* Do any language-specific gimplification. */
3135 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3136 if (ret == GS_OK)
3138 if (*expr_p == NULL_TREE)
3139 break;
3140 if (*expr_p != save_expr)
3141 continue;
3143 else if (ret != GS_UNHANDLED)
3144 break;
3146 ret = GS_OK;
3147 switch (TREE_CODE (*expr_p))
3149 /* First deal with the special cases. */
3151 case POSTINCREMENT_EXPR:
3152 case POSTDECREMENT_EXPR:
3153 case PREINCREMENT_EXPR:
3154 case PREDECREMENT_EXPR:
3155 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3156 fallback != fb_none);
3157 break;
3159 case ARRAY_REF:
3160 ret = gimplify_array_ref (expr_p, pre_p, post_p,
3161 fallback & fb_lvalue);
3162 break;
3164 case COMPONENT_REF:
3165 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3166 fallback & fb_lvalue);
3167 break;
3169 case COND_EXPR:
3170 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3171 break;
3173 case CALL_EXPR:
3174 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3175 break;
3177 case TREE_LIST:
3178 abort ();
3180 case COMPOUND_EXPR:
3181 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3182 break;
3184 case REALPART_EXPR:
3185 case IMAGPART_EXPR:
3186 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3187 fallback & fb_lvalue);
3188 break;
3190 case MODIFY_EXPR:
3191 case INIT_EXPR:
3192 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3193 fallback != fb_none);
3194 break;
3196 case TRUTH_ANDIF_EXPR:
3197 case TRUTH_ORIF_EXPR:
3198 ret = gimplify_boolean_expr (expr_p);
3199 break;
3201 case TRUTH_NOT_EXPR:
3202 TREE_OPERAND (*expr_p, 0)
3203 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3204 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3205 is_gimple_val, fb_rvalue);
3206 recalculate_side_effects (*expr_p);
3207 break;
3209 case ADDR_EXPR:
3210 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3211 break;
3213 case VA_ARG_EXPR:
3214 /* Mark any _DECL inside the operand as volatile to avoid the
3215 optimizers messing around with it. FIXME: Remove this once
3216 VA_ARG_EXPRs are properly lowered. */
3217 walk_tree (&TREE_OPERAND (*expr_p, 0), mark_decls_volatile_r,
3218 NULL, NULL);
3220 /* va_arg expressions are in GIMPLE form already. */
3221 ret = GS_ALL_DONE;
3222 break;
3224 case CONVERT_EXPR:
3225 case NOP_EXPR:
3226 if (IS_EMPTY_STMT (*expr_p))
3228 ret = GS_ALL_DONE;
3229 break;
3232 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3233 || fallback == fb_none)
3235 /* Just strip a conversion to void (or in void context) and
3236 try again. */
3237 *expr_p = TREE_OPERAND (*expr_p, 0);
3238 break;
3241 ret = gimplify_conversion (expr_p);
3242 if (ret == GS_ERROR)
3243 break;
3244 if (*expr_p != save_expr)
3245 break;
3246 /* FALLTHRU */
3248 case FIX_TRUNC_EXPR:
3249 case FIX_CEIL_EXPR:
3250 case FIX_FLOOR_EXPR:
3251 case FIX_ROUND_EXPR:
3252 /* unary_expr: ... | '(' cast ')' val | ... */
3253 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3254 is_gimple_val, fb_rvalue);
3255 recalculate_side_effects (*expr_p);
3256 break;
3258 case INDIRECT_REF:
3259 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3260 is_gimple_reg, fb_rvalue);
3261 recalculate_side_effects (*expr_p);
3262 break;
3264 /* Constants need not be gimplified. */
3265 case INTEGER_CST:
3266 case REAL_CST:
3267 case STRING_CST:
3268 case COMPLEX_CST:
3269 case VECTOR_CST:
3270 ret = GS_ALL_DONE;
3271 break;
3273 case CONST_DECL:
3274 *expr_p = DECL_INITIAL (*expr_p);
3275 break;
3277 case EXC_PTR_EXPR:
3278 /* FIXME make this a decl. */
3279 ret = GS_ALL_DONE;
3280 break;
3282 case BIND_EXPR:
3283 ret = gimplify_bind_expr (expr_p, pre_p);
3284 break;
3286 case LOOP_EXPR:
3287 ret = gimplify_loop_expr (expr_p, pre_p);
3288 break;
3290 case SWITCH_EXPR:
3291 ret = gimplify_switch_expr (expr_p, pre_p);
3292 break;
3294 case LABELED_BLOCK_EXPR:
3295 ret = gimplify_labeled_block_expr (expr_p);
3296 break;
3298 case EXIT_BLOCK_EXPR:
3299 ret = gimplify_exit_block_expr (expr_p);
3300 break;
3302 case EXIT_EXPR:
3303 ret = gimplify_exit_expr (expr_p);
3304 break;
3306 case GOTO_EXPR:
3307 /* If the target is not LABEL, then it is a computed jump
3308 and the target needs to be gimplified. */
3309 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3310 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3311 NULL, is_gimple_val, fb_rvalue);
3312 break;
3314 case LABEL_EXPR:
3315 ret = GS_ALL_DONE;
3316 #ifdef ENABLE_CHECKING
3317 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3318 abort ();
3319 #endif
3320 break;
3322 case CASE_LABEL_EXPR:
3323 ret = gimplify_case_label_expr (expr_p);
3324 break;
3326 case RETURN_EXPR:
3327 ret = gimplify_return_expr (*expr_p, pre_p);
3328 break;
3330 case CONSTRUCTOR:
3331 /* Don't reduce this in place; let gimplify_init_constructor work
3332 its magic. */
3333 ret = GS_ALL_DONE;
3334 break;
3336 /* The following are special cases that are not handled by the
3337 original GIMPLE grammar. */
3339 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3340 eliminated. */
3341 case SAVE_EXPR:
3342 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3343 break;
3345 case BIT_FIELD_REF:
3347 enum gimplify_status r0, r1, r2;
3349 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3350 is_gimple_min_lval, fb_either);
3351 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3352 is_gimple_val, fb_rvalue);
3353 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3354 is_gimple_val, fb_rvalue);
3355 recalculate_side_effects (*expr_p);
3357 ret = MIN (r0, MIN (r1, r2));
3359 break;
3361 case NON_LVALUE_EXPR:
3362 /* This should have been stripped above. */
3363 abort ();
3364 break;
3366 case ASM_EXPR:
3367 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3368 break;
3370 case TRY_FINALLY_EXPR:
3371 case TRY_CATCH_EXPR:
3372 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3373 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3374 ret = GS_ALL_DONE;
3375 break;
3377 case CLEANUP_POINT_EXPR:
3378 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3379 break;
3381 case TARGET_EXPR:
3382 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3383 break;
3385 case CATCH_EXPR:
3386 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3387 ret = GS_ALL_DONE;
3388 break;
3390 case EH_FILTER_EXPR:
3391 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3392 ret = GS_ALL_DONE;
3393 break;
3395 case VTABLE_REF:
3396 /* This moves much of the actual computation out of the
3397 VTABLE_REF. Perhaps this should be revisited once we want to
3398 do clever things with VTABLE_REFs. */
3399 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3400 is_gimple_min_lval, fb_lvalue);
3401 break;
3403 case MIN_EXPR:
3404 case MAX_EXPR:
3405 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3406 break;
3408 case LABEL_DECL:
3409 /* We get here when taking the address of a label. We mark
3410 the label as "forced"; meaning it can never be removed and
3411 it is a potential target for any computed goto. */
3412 FORCED_LABEL (*expr_p) = 1;
3413 ret = GS_ALL_DONE;
3414 break;
3416 case STATEMENT_LIST:
3417 ret = gimplify_statement_list (expr_p);
3418 break;
3420 case VAR_DECL:
3421 /* ??? If this is a local variable, and it has not been seen in any
3422 outer BIND_EXPR, then it's probably the result of a duplicate
3423 declaration, for which we've already issued an error. It would
3424 be really nice if the front end wouldn't leak these at all.
3425 Currently the only known culprit is C++ destructors, as seen
3426 in g++.old-deja/g++.jason/binding.C. */
3427 tmp = *expr_p;
3428 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3429 && decl_function_context (tmp) == current_function_decl
3430 && !tmp->decl.seen_in_bind_expr)
3432 #ifdef ENABLE_CHECKING
3433 if (!errorcount && !sorrycount)
3434 abort ();
3435 #endif
3436 ret = GS_ERROR;
3438 else
3439 ret = GS_ALL_DONE;
3440 break;
3442 default:
3443 /* If *EXPR_P does not need to be special-cased, handle it
3444 according to its class. */
3445 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3446 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3447 post_p, is_gimple_val, fb_rvalue);
3448 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3449 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3450 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3451 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3452 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3454 enum gimplify_status r0, r1;
3456 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3457 post_p, is_gimple_val, fb_rvalue);
3458 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3459 post_p, is_gimple_val, fb_rvalue);
3461 ret = MIN (r0, r1);
3463 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3464 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3466 ret = GS_ALL_DONE;
3467 break;
3469 else
3470 /* Fail if we don't know how to handle this tree code. */
3471 abort ();
3473 recalculate_side_effects (*expr_p);
3474 break;
3477 /* If we replaced *expr_p, gimplify again. */
3478 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3479 ret = GS_ALL_DONE;
3481 while (ret == GS_OK);
3483 /* If we encountered an error_mark somewhere nested inside, either
3484 stub out the statement or propagate the error back out. */
3485 if (ret == GS_ERROR)
3487 if (is_statement)
3488 *expr_p = build_empty_stmt ();
3489 goto out;
3492 #ifdef ENABLE_CHECKING
3493 /* This was only valid as a return value from the langhook, which
3494 we handled. Make sure it doesn't escape from any other context. */
3495 if (ret == GS_UNHANDLED)
3496 abort ();
3497 #endif
3499 if (!*expr_p)
3500 *expr_p = build_empty_stmt ();
3501 if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3503 /* We aren't looking for a value, and we don't have a valid
3504 statement. If it doesn't have side-effects, throw it away. */
3505 if (!TREE_SIDE_EFFECTS (*expr_p))
3506 *expr_p = build_empty_stmt ();
3507 else if (!TREE_THIS_VOLATILE (*expr_p))
3508 /* We only handle volatiles here; anything else with side-effects
3509 must be converted to a valid statement before we get here. */
3510 abort ();
3511 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3513 /* Historically, the compiler has treated a bare
3514 reference to a volatile lvalue as forcing a load. */
3515 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3516 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3518 else
3519 /* We can't do anything useful with a volatile reference to
3520 incomplete type, so just throw it away. */
3521 *expr_p = build_empty_stmt ();
3524 /* If we are gimplifying at the statement level, we're done. Tack
3525 everything together and replace the original statement with the
3526 gimplified form. */
3527 if (is_statement)
3529 append_to_statement_list (*expr_p, &internal_pre);
3530 append_to_statement_list (internal_post, &internal_pre);
3531 annotate_all_with_locus (&internal_pre, input_location);
3532 *expr_p = internal_pre;
3533 goto out;
3536 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3537 interesting. */
3539 /* If it's sufficiently simple already, we're done. Unless we are
3540 handling some post-effects internally; if that's the case, we need to
3541 copy into a temp before adding the post-effects to the tree. */
3542 if (!internal_post && (*gimple_test_f) (*expr_p))
3543 goto out;
3545 /* Otherwise, we need to create a new temporary for the gimplified
3546 expression. */
3548 /* We can't return an lvalue if we have an internal postqueue. The
3549 object the lvalue refers to would (probably) be modified by the
3550 postqueue; we need to copy the value out first, which means an
3551 rvalue. */
3552 if ((fallback & fb_lvalue) && !internal_post
3553 && is_gimple_addr_expr_arg (*expr_p))
3555 /* An lvalue will do. Take the address of the expression, store it
3556 in a temporary, and replace the expression with an INDIRECT_REF of
3557 that temporary. */
3558 tmp = build_addr_expr (*expr_p);
3559 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3560 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3562 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3564 #if defined ENABLE_CHECKING
3565 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3566 abort ();
3567 #endif
3569 /* An rvalue will do. Assign the gimplified expression into a new
3570 temporary TMP and replace the original expression with TMP. */
3572 if (internal_post || (fallback & fb_lvalue))
3573 /* The postqueue might change the value of the expression between
3574 the initialization and use of the temporary, so we can't use a
3575 formal temp. FIXME do we care? */
3576 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3577 else
3578 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3580 else if (fallback & fb_mayfail)
3582 /* If this is an asm statement, and the user asked for the impossible,
3583 don't abort. Fail and let gimplify_asm_expr issue an error. */
3584 ret = GS_ERROR;
3585 goto out;
3587 else
3589 fprintf (stderr, "gimplification failed:\n");
3590 print_generic_expr (stderr, *expr_p, 0);
3591 debug_tree (*expr_p);
3592 abort ();
3595 #if defined ENABLE_CHECKING
3596 /* Make sure the temporary matches our predicate. */
3597 if (!(*gimple_test_f) (*expr_p))
3598 abort ();
3599 #endif
3601 if (internal_post)
3603 annotate_all_with_locus (&internal_post, input_location);
3604 append_to_statement_list (internal_post, pre_p);
3607 out:
3608 input_location = saved_location;
3609 return ret;
3612 #ifdef ENABLE_CHECKING
3613 /* Compare types A and B for a "close enough" match. */
3615 static bool
3616 cpt_same_type (tree a, tree b)
3618 if (lang_hooks.types_compatible_p (a, b))
3619 return true;
3621 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3622 link them together. This routine is intended to catch type errors
3623 that will affect the optimizers, and the optimizers don't add new
3624 dereferences of function pointers, so ignore it. */
3625 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3626 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3627 return true;
3629 /* ??? The C FE pushes type qualifiers after the fact into the type of
3630 the element from the type of the array. See build_unary_op's handling
3631 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3632 should have done it when creating the variable in the first place.
3633 Alternately, why aren't the two array types made variants? */
3634 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3635 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3637 /* And because of those, we have to recurse down through pointers. */
3638 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3639 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3641 return false;
3644 /* Check for some cases of the front end missing cast expressions.
3645 The type of a dereference should correspond to the pointer type;
3646 similarly the type of an address should match its object. */
3648 static tree
3649 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3650 void *data ATTRIBUTE_UNUSED)
3652 tree t = *tp;
3653 tree ptype, otype, dtype;
3655 switch (TREE_CODE (t))
3657 case INDIRECT_REF:
3658 case ARRAY_REF:
3659 otype = TREE_TYPE (t);
3660 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3661 dtype = TREE_TYPE (ptype);
3662 if (!cpt_same_type (otype, dtype))
3663 abort ();
3664 break;
3666 case ADDR_EXPR:
3667 ptype = TREE_TYPE (t);
3668 otype = TREE_TYPE (TREE_OPERAND (t, 0));
3669 dtype = TREE_TYPE (ptype);
3670 if (!cpt_same_type (otype, dtype))
3672 /* &array is allowed to produce a pointer to the element,
3673 rather than a pointer to the array type. */
3674 if (TREE_CODE (otype) == ARRAY_TYPE
3675 && POINTER_TYPE_P (ptype)
3676 && cpt_same_type (TREE_TYPE (otype), dtype))
3677 break;
3678 abort ();
3680 break;
3682 default:
3683 return NULL_TREE;
3687 return NULL_TREE;
3689 #endif
3691 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
3692 function decl containing BODY. */
3694 void
3695 gimplify_body (tree *body_p, tree fndecl)
3697 location_t saved_location = input_location;
3698 tree body;
3700 timevar_push (TV_TREE_GIMPLIFY);
3701 push_gimplify_context ();
3703 /* Unshare most shared trees in the body. */
3704 unshare_all_trees (*body_p);
3706 /* Make sure input_location isn't set to something wierd. */
3707 input_location = DECL_SOURCE_LOCATION (fndecl);
3709 /* Gimplify the function's body. */
3710 gimplify_stmt (body_p);
3711 body = *body_p;
3713 /* Unshare again, in case gimplification was sloppy. */
3714 unshare_all_trees (body);
3716 /* If there isn't an outer BIND_EXPR, add one. */
3717 if (TREE_CODE (body) == STATEMENT_LIST)
3719 tree t = expr_only (*body_p);
3720 if (t)
3721 body = t;
3723 if (TREE_CODE (body) != BIND_EXPR)
3725 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3726 NULL_TREE, NULL_TREE);
3727 TREE_SIDE_EFFECTS (b) = 1;
3728 append_to_statement_list (body, &BIND_EXPR_BODY (b));
3729 body = b;
3731 *body_p = body;
3733 pop_gimplify_context (body);
3735 #ifdef ENABLE_CHECKING
3736 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3737 #endif
3739 timevar_pop (TV_TREE_GIMPLIFY);
3740 input_location = saved_location;
3743 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
3744 node for the function we want to gimplify. */
3746 void
3747 gimplify_function_tree (tree fndecl)
3749 tree oldfn;
3751 oldfn = current_function_decl;
3752 current_function_decl = fndecl;
3754 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3756 /* If we're instrumenting function entry/exit, then prepend the call to
3757 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3758 catch the exit hook. */
3759 /* ??? Add some way to ignore exceptions for this TFE. */
3760 if (flag_instrument_function_entry_exit
3761 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3763 tree tf, x, bind;
3765 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3766 TREE_SIDE_EFFECTS (tf) = 1;
3767 x = DECL_SAVED_TREE (fndecl);
3768 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3769 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3770 x = build_function_call_expr (x, NULL);
3771 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3773 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3774 TREE_SIDE_EFFECTS (bind) = 1;
3775 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3776 x = build_function_call_expr (x, NULL);
3777 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3778 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3780 DECL_SAVED_TREE (fndecl) = bind;
3783 current_function_decl = oldfn;
3786 #include "gt-gimplify.h"