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
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
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
27 #include "coretypes.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"
49 static struct gimplify_ctx
51 tree current_bind_expr;
54 tree conditional_cleanups;
57 varray_type case_labels;
58 /* The formal temporary table. Should this be persistent? */
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
69 tree temp; /* Value */
72 /* Return a hash value for a formal temporary table entry. */
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. */
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))
94 if (!operand_equal_p (t1, t2, 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))
105 /* Set up a context for the gimplifier. */
108 push_gimplify_context (void)
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. */
123 pop_gimplify_context (tree body)
125 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
129 declare_tmp_vars (gimplify_ctxp->temps, body);
131 record_vars (gimplify_ctxp->temps);
135 fprintf (stderr, " collisions: %f ",
136 htab_collisions (gimplify_ctxp->temp_htab));
139 htab_delete (gimplify_ctxp->temp_htab);
140 free (gimplify_ctxp);
141 gimplify_ctxp = NULL;
145 gimple_push_bind_expr (tree bind)
147 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
148 gimplify_ctxp->current_bind_expr = bind;
152 gimple_pop_bind_expr (void)
154 gimplify_ctxp->current_bind_expr
155 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
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. */
168 gimple_conditional_context (void)
170 return gimplify_ctxp->conditions > 0;
173 /* Note that we've entered a COND_EXPR. */
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. */
185 gimple_pop_condition (tree *pre_p)
187 int conds = --(gimplify_ctxp->conditions);
190 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
191 gimplify_ctxp->conditional_cleanups = NULL_TREE;
197 /* A subroutine of append_to_statement_list{,_force}. */
200 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
203 tree_stmt_iterator i;
210 if (t && TREE_CODE (t) == STATEMENT_LIST)
215 *list_p = list = alloc_stmt_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. */
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. */
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. */
243 append_to_compound_expr (tree t, tree *list_p)
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".) */
259 remove_suffix (char *name, int len)
263 for (i = 2; i < 8 && len > i; i++)
265 if (name[len - i] == '.')
267 name[len - i] = '\0';
273 /* Create a nameless artificial label and put it in the current function
274 context. Returns the newly created label. */
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;
285 /* Create a new temporary name with PREFIX. Returns an identifier. */
287 static GTY(()) unsigned int tmp_var_id_num;
290 create_tmp_var_name (const char *prefix)
296 char *preftmp = ASTRDUP (prefix);
297 remove_suffix (preftmp, strlen (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. */
310 create_tmp_var_raw (tree type, const char *prefix)
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;
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. */
342 create_tmp_var (tree type, const char *prefix)
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))
351 if (!COMPLETE_TYPE_P (type))
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)
359 tmp_var = create_tmp_var_raw (type, prefix);
360 gimple_add_tmp_var (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. */
374 STRIP_NOPS (stripped_decl);
375 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
376 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
379 switch (TREE_CODE (stripped_decl))
382 return get_name (TREE_OPERAND (stripped_decl, 0));
390 /* Create a temporary with a name derived from VAL. Subroutine of
391 lookup_tmp_var; nobody else should call this function. */
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. */
403 lookup_tmp_var (tree val, bool is_formal)
405 if (!is_formal || TREE_SIDE_EFFECTS (val))
406 return create_tmp_from_val (val);
413 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
416 elt_p = xmalloc (sizeof (*elt_p));
418 elt_p->temp = create_tmp_from_val (val);
419 *slot = (void *)elt_p;
422 elt_p = (elt_t *) *slot;
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. */
441 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
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));
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);
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. */
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. */
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. */
493 declare_tmp_vars (tree vars, tree scope)
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)
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. */
518 gimple_add_tmp_var (tree tmp)
520 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
523 DECL_CONTEXT (tmp) = current_function_decl;
524 tmp->decl.seen_in_bind_expr = 1;
528 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
529 gimplify_ctxp->temps = tmp;
534 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
537 /* Determines whether to assign a locus to the statement STMT. */
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)
548 /* Do not annotate empty statements, since it confuses gcov. */
549 if (!TREE_SIDE_EFFECTS (stmt))
556 annotate_all_with_locus (tree *stmt_p, location_t locus)
558 tree_stmt_iterator i;
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)
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. */
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. */
601 else if (code == BIND_EXPR)
604 copy_tree_r (tp, walk_subtrees, data);
609 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
610 after VA_ARG_EXPRs are properly lowered. */
613 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
614 void *data ATTRIBUTE_UNUSED)
617 TREE_THIS_VOLATILE (*tp) = 1;
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. */
633 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
634 void *data ATTRIBUTE_UNUSED)
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')
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))
653 TREE_VISITED (t) = 1;
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
660 else if (TREE_VISITED (t))
662 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
666 /* Otherwise, mark the tree as visited and keep looking. */
668 TREE_VISITED (t) = 1;
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;
685 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
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. */
699 unshare_expr (tree expr)
701 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
705 /* A terser interface for building a representation of a exception
709 gimple_build_eh_filter (tree body, tree allowed, tree failure)
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));
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. */
730 voidify_wrapper_expr (tree wrapper)
732 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
737 /* Set p to point to the body of the wrapper. */
738 switch (TREE_CODE (wrapper))
741 /* For a BIND_EXPR, the body is operand 1. */
742 p = &BIND_EXPR_BODY (wrapper);
746 p = &TREE_OPERAND (wrapper, 0);
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);
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
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;
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;
799 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
800 a temporary through which they communicate. */
803 build_stack_save_restore (tree *save, tree *restore)
805 tree save_call, tmp_var;
808 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
810 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
812 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
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;
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 ();
861 append_to_statement_list (bind_expr, pre_p);
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);
881 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
884 if (ret_expr == error_mark_node)
887 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
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)
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. */
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)
925 if (tsi_one_before_end_p (si))
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),
942 TREE_OPERAND (stmt, 0) = ret;
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);
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);
979 gimplify_ctxp->exit_label = saved_label;
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. */
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 /* Sort the case labels in LABEL_VEC in ascending order. */
1000 sort_case_labels (tree label_vec)
1002 size_t len = TREE_VEC_LENGTH (label_vec);
1003 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1005 if (CASE_LOW (default_case))
1009 /* The last label in the vector should be the default case
1011 for (i = 0; i < len; ++i)
1013 tree t = TREE_VEC_ELT (label_vec, i);
1017 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1018 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1024 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1025 compare_case_labels);
1028 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1031 static enum gimplify_status
1032 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1034 tree switch_expr = *expr_p;
1035 enum gimplify_status ret;
1037 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1038 is_gimple_val, fb_rvalue);
1040 if (SWITCH_BODY (switch_expr))
1042 varray_type labels, saved_labels;
1043 tree label_vec, default_case = NULL_TREE;
1046 /* If someone can be bothered to fill in the labels, they can
1047 be bothered to null out the body too. */
1048 if (SWITCH_LABELS (switch_expr))
1051 saved_labels = gimplify_ctxp->case_labels;
1052 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1054 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1056 labels = gimplify_ctxp->case_labels;
1057 gimplify_ctxp->case_labels = saved_labels;
1059 len = VARRAY_ACTIVE_SIZE (labels);
1061 for (i = 0; i < len; ++i)
1063 tree t = VARRAY_TREE (labels, i);
1066 /* The default case must be the last label in the list. */
1068 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1074 label_vec = make_tree_vec (len + 1);
1075 SWITCH_LABELS (*expr_p) = label_vec;
1076 append_to_statement_list (switch_expr, pre_p);
1080 /* If the switch has no default label, add one, so that we jump
1081 around the switch body. */
1082 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1083 NULL_TREE, create_artificial_label ());
1084 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1085 *expr_p = build (LABEL_EXPR, void_type_node,
1086 CASE_LABEL (default_case));
1089 *expr_p = SWITCH_BODY (switch_expr);
1091 for (i = 0; i < len; ++i)
1092 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1093 TREE_VEC_ELT (label_vec, len) = default_case;
1095 sort_case_labels (label_vec);
1097 SWITCH_BODY (switch_expr) = NULL;
1099 else if (!SWITCH_LABELS (switch_expr))
1105 static enum gimplify_status
1106 gimplify_case_label_expr (tree *expr_p)
1108 tree expr = *expr_p;
1109 if (gimplify_ctxp->case_labels)
1110 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1113 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1117 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1118 a (possibly empty) body. */
1120 static enum gimplify_status
1121 gimplify_labeled_block_expr (tree *expr_p)
1123 tree body = LABELED_BLOCK_BODY (*expr_p);
1124 tree label = LABELED_BLOCK_LABEL (*expr_p);
1127 DECL_CONTEXT (label) = current_function_decl;
1128 t = build (LABEL_EXPR, void_type_node, label);
1129 if (body != NULL_TREE)
1130 t = build (COMPOUND_EXPR, void_type_node, body, t);
1136 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1138 static enum gimplify_status
1139 gimplify_exit_block_expr (tree *expr_p)
1141 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1144 /* First operand must be a LABELED_BLOCK_EXPR, which should
1145 already be lowered (or partially lowered) when we get here. */
1146 #if defined ENABLE_CHECKING
1147 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1151 label = LABELED_BLOCK_LABEL (labeled_block);
1152 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1157 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1161 build_and_jump (tree *label_p)
1163 if (label_p == NULL)
1164 /* If there's nowhere to jump, just fall through. */
1165 return build_empty_stmt ();
1167 if (*label_p == NULL_TREE)
1169 tree label = create_artificial_label ();
1173 return build1 (GOTO_EXPR, void_type_node, *label_p);
1176 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1177 This also involves building a label to jump to and communicating it to
1178 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1180 static enum gimplify_status
1181 gimplify_exit_expr (tree *expr_p)
1183 tree cond = TREE_OPERAND (*expr_p, 0);
1186 expr = build_and_jump (&gimplify_ctxp->exit_label);
1187 expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1193 /* A helper function to be called via walk_tree. Mark all labels under *TP
1194 as being forced. To be called for DECL_INITIAL of static variables. */
1197 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1201 if (TREE_CODE (*tp) == LABEL_DECL)
1202 FORCED_LABEL (*tp) = 1;
1207 /* Break out elements of a constructor used as an initializer into separate
1210 Note that we still need to clear any elements that don't have explicit
1211 initializers, so if not all elements are initialized we keep the
1212 original MODIFY_EXPR, we just remove all of the constructor elements. */
1214 static enum gimplify_status
1215 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1216 tree *post_p, int want_value)
1218 tree object = TREE_OPERAND (*expr_p, 0);
1219 tree ctor = TREE_OPERAND (*expr_p, 1);
1220 tree type = TREE_TYPE (ctor);
1221 enum gimplify_status ret;
1224 if (TREE_CODE (ctor) != CONSTRUCTOR)
1225 return GS_UNHANDLED;
1227 elt_list = CONSTRUCTOR_ELTS (ctor);
1230 switch (TREE_CODE (type))
1234 case QUAL_UNION_TYPE:
1237 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1238 HOST_WIDE_INT num_nonconstant_elements;
1241 /* Aggregate types must lower constructors to initialization of
1242 individual elements. The exception is that a CONSTRUCTOR node
1243 with no elements indicates zero-initialization of the whole. */
1244 if (elt_list == NULL)
1255 categorize_ctor_elements (ctor, &num_nonzero_elements,
1256 &num_nonconstant_elements);
1257 num_elements = count_type_elements (TREE_TYPE (ctor));
1259 /* If a const aggregate variable is being initialized, then it
1260 should never be a lose to promote the variable to be static. */
1261 if (num_nonconstant_elements == 0
1262 && TREE_READONLY (object)
1263 && TREE_CODE (object) == VAR_DECL)
1265 DECL_INITIAL (object) = ctor;
1266 TREE_STATIC (object) = 1;
1267 if (!DECL_NAME (object))
1268 DECL_NAME (object) = create_tmp_var_name ("C");
1269 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1271 /* ??? C++ doesn't automatically append a .<number> to the
1272 assembler name, and even when it does, it looks a FE private
1273 data structures to figure out what that number should be,
1274 which are not set for this variable. I suppose this is
1275 important for local statics for inline functions, which aren't
1276 "local" in the object file sense. So in order to get a unique
1277 TU-local symbol, we must invoke the lhd version now. */
1278 lhd_set_decl_assembler_name (object);
1280 *expr_p = build_empty_stmt ();
1284 /* If there are "lots" of initialized elements, and all of them
1285 are valid address constants, then the entire initializer can
1286 be dropped to memory, and then memcpy'd out. */
1287 if (num_nonconstant_elements == 0)
1289 HOST_WIDE_INT size = int_size_in_bytes (type);
1292 /* ??? We can still get unbounded array types, at least
1293 from the C++ front end. This seems wrong, but attempt
1294 to work around it for now. */
1297 size = int_size_in_bytes (TREE_TYPE (object));
1299 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1302 /* Find the maximum alignment we can assume for the object. */
1303 /* ??? Make use of DECL_OFFSET_ALIGN. */
1304 if (DECL_P (object))
1305 align = DECL_ALIGN (object);
1307 align = TYPE_ALIGN (type);
1309 if (size > 0 && !can_move_by_pieces (size, align))
1311 tree new = create_tmp_var_raw (type, "C");
1312 gimple_add_tmp_var (new);
1313 TREE_STATIC (new) = 1;
1314 TREE_READONLY (new) = 1;
1315 DECL_INITIAL (new) = ctor;
1316 if (align > DECL_ALIGN (new))
1318 DECL_ALIGN (new) = align;
1319 DECL_USER_ALIGN (new) = 1;
1321 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1323 TREE_OPERAND (*expr_p, 1) = new;
1328 /* If there are "lots" of initialized elements, even discounting
1329 those that are not address constants (and thus *must* be
1330 computed at runtime), then partition the constructor into
1331 constant and non-constant parts. Block copy the constant
1332 parts in, then generate code for the non-constant parts. */
1333 /* TODO. There's code in cp/typeck.c to do this. */
1335 /* If there are "lots" of zeros, then block clear the object first. */
1337 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1338 && num_nonzero_elements < num_elements/4)
1341 /* ??? This bit ought not be needed. For any element not present
1342 in the initializer, we should simply set them to zero. Except
1343 we'd need to *find* the elements that are not present, and that
1344 requires trickery to avoid quadratic compile-time behavior in
1345 large cases or excessive memory use in small cases. */
1348 HOST_WIDE_INT len = list_length (elt_list);
1349 if (TREE_CODE (type) == ARRAY_TYPE)
1351 tree nelts = array_type_nelts (type);
1352 if (!host_integerp (nelts, 1)
1353 || tree_low_cst (nelts, 1) != len)
1356 else if (len != fields_length (type))
1362 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1363 append_to_statement_list (*expr_p, pre_p);
1366 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1368 tree purpose, value, cref, init;
1370 purpose = TREE_PURPOSE (elt_list);
1371 value = TREE_VALUE (elt_list);
1373 if (cleared && initializer_zerop (value))
1376 if (TREE_CODE (type) == ARRAY_TYPE)
1378 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1380 /* ??? Here's to hoping the front end fills in all of the
1381 indicies, so we don't have to figure out what's missing
1385 /* ??? Need to handle this. */
1386 if (TREE_CODE (purpose) == RANGE_EXPR)
1389 cref = build (ARRAY_REF, t, object, purpose);
1393 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1397 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1398 /* Each member initialization is a full-expression. */
1399 gimplify_stmt (&init);
1400 append_to_statement_list (init, pre_p);
1403 *expr_p = build_empty_stmt ();
1411 /* Extract the real and imaginary parts out of the ctor. */
1415 r = TREE_VALUE (elt_list);
1416 elt_list = TREE_CHAIN (elt_list);
1419 i = TREE_VALUE (elt_list);
1420 if (TREE_CHAIN (elt_list))
1424 if (r == NULL || i == NULL)
1426 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1433 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1434 represent creation of a complex value. */
1435 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1437 ctor = build_complex (type, r, i);
1438 TREE_OPERAND (*expr_p, 1) = ctor;
1442 ctor = build (COMPLEX_EXPR, type, r, i);
1443 TREE_OPERAND (*expr_p, 1) = ctor;
1444 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1445 is_gimple_rhs, fb_rvalue);
1451 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1452 if (TREE_CONSTANT (ctor))
1453 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1456 /* Vector types use CONSTRUCTOR all the way through gimple
1457 compilation as a general initializer. */
1458 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1460 enum gimplify_status tret;
1461 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1462 is_gimple_constructor_elt, fb_rvalue);
1463 if (tret == GS_ERROR)
1470 /* So how did we get a CONSTRUCTOR for a scalar type? */
1474 if (ret == GS_ERROR)
1476 else if (want_value)
1478 append_to_statement_list (*expr_p, pre_p);
1486 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1487 different from its canonical type, wrap the whole thing inside a
1488 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1491 The canonical type of a COMPONENT_REF is the type of the field being
1492 referenced--unless the field is a bit-field which can be read directly
1493 in a smaller mode, in which case the canonical type is the
1494 sign-appropriate type corresponding to that mode. */
1497 canonicalize_component_ref (tree *expr_p)
1499 tree expr = *expr_p;
1502 if (TREE_CODE (expr) != COMPONENT_REF)
1505 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1506 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1508 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1510 if (TREE_TYPE (expr) != type)
1512 tree old_type = TREE_TYPE (expr);
1514 /* Set the type of the COMPONENT_REF to the underlying type. */
1515 TREE_TYPE (expr) = type;
1517 /* And wrap the whole thing inside a NOP_EXPR. */
1518 expr = build1 (NOP_EXPR, old_type, expr);
1524 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1525 to foo, embed that change in the ADDR_EXPR. Lest we perturb the type
1526 system too badly, we must take extra steps to ensure that the ADDR_EXPR
1527 and the addressed object continue to agree on types. */
1528 /* ??? We might could do better if we recognize
1536 canonicalize_addr_expr (tree* expr_p)
1538 tree expr = *expr_p;
1539 tree ctype = TREE_TYPE (expr);
1540 tree addr_expr = TREE_OPERAND (expr, 0);
1541 tree atype = TREE_TYPE (addr_expr);
1542 tree dctype, datype, ddatype, otype, obj_expr;
1544 /* Both cast and addr_expr types should be pointers. */
1545 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1548 /* The addr_expr type should be a pointer to an array. */
1549 datype = TREE_TYPE (atype);
1550 if (TREE_CODE (datype) != ARRAY_TYPE)
1553 /* Both cast and addr_expr types should address the same object type. */
1554 dctype = TREE_TYPE (ctype);
1555 ddatype = TREE_TYPE (datype);
1556 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1559 /* The addr_expr and the object type should match. */
1560 obj_expr = TREE_OPERAND (addr_expr, 0);
1561 otype = TREE_TYPE (obj_expr);
1562 if (!lang_hooks.types_compatible_p (otype, datype))
1565 /* All checks succeeded. Build a new node to merge the cast. */
1566 *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1569 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1570 underneath as appropriate. */
1572 static enum gimplify_status
1573 gimplify_conversion (tree *expr_p)
1575 /* Strip away as many useless type conversions as possible
1577 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1579 /* If we still have a conversion at the toplevel, then strip
1580 away all but the outermost conversion. */
1581 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1583 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1585 /* And remove the outermost conversion if it's useless. */
1586 if (tree_ssa_useless_type_conversion (*expr_p))
1587 *expr_p = TREE_OPERAND (*expr_p, 0);
1590 /* If we still have a conversion at the toplevel,
1591 then canonicalize some constructs. */
1592 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1594 tree sub = TREE_OPERAND (*expr_p, 0);
1596 /* If a NOP conversion is changing the type of a COMPONENT_REF
1597 expression, then canonicalize its type now in order to expose more
1598 redundant conversions. */
1599 if (TREE_CODE (sub) == COMPONENT_REF)
1600 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1602 /* If a NOP conversion is changing a pointer to array of foo
1603 to a pointer to foo, embed that change in the ADDR_EXPR. */
1604 else if (TREE_CODE (sub) == ADDR_EXPR)
1605 canonicalize_addr_expr (expr_p);
1611 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1613 static enum gimplify_status
1614 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1616 tree op1 = TREE_OPERAND (*expr_p, 0);
1617 tree op2 = TREE_OPERAND (*expr_p, 1);
1618 enum tree_code code;
1619 enum gimplify_status r0, r1;
1621 if (TREE_CODE (*expr_p) == MIN_EXPR)
1626 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1627 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1629 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1630 build (code, boolean_type_node, op1, op2),
1633 if (r0 == GS_ERROR || r1 == GS_ERROR)
1639 /* Build an expression for the address of T. Folds away INDIRECT_REF to
1640 avoid confusing the gimplify process. */
1643 build_addr_expr_with_type (tree t, tree ptrtype)
1645 if (TREE_CODE (t) == INDIRECT_REF)
1647 t = TREE_OPERAND (t, 0);
1648 if (TREE_TYPE (t) != ptrtype)
1649 t = build1 (NOP_EXPR, ptrtype, t);
1654 while (TREE_CODE (base) == COMPONENT_REF
1655 || TREE_CODE (base) == ARRAY_REF)
1656 base = TREE_OPERAND (base, 0);
1658 TREE_ADDRESSABLE (base) = 1;
1660 t = build1 (ADDR_EXPR, ptrtype, t);
1667 build_addr_expr (tree t)
1669 return build_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
1672 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1673 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1675 static enum gimplify_status
1676 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1678 tree array = TREE_OPERAND (*expr_p, 0);
1679 tree arrtype = TREE_TYPE (array);
1680 tree elttype = TREE_TYPE (arrtype);
1681 tree size = size_in_bytes (elttype);
1682 tree ptrtype = build_pointer_type (elttype);
1683 enum tree_code add_code = PLUS_EXPR;
1684 tree idx = TREE_OPERAND (*expr_p, 1);
1685 tree minidx, offset, addr, result;
1686 enum gimplify_status ret;
1688 /* If the array domain does not start at zero, apply the offset. */
1689 minidx = TYPE_DOMAIN (arrtype);
1692 minidx = TYPE_MIN_VALUE (minidx);
1693 if (minidx && !integer_zerop (minidx))
1695 idx = convert (TREE_TYPE (minidx), idx);
1696 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1700 /* If the index is negative -- a technically invalid situation now
1701 that we've biased the index back to zero -- then casting it to
1702 unsigned has ill effects. In particular, -1*4U/4U != -1.
1703 Represent this as a subtraction of a positive rather than addition
1704 of a negative. This will prevent any conversion back to ARRAY_REF
1705 from getting the wrong results from the division. */
1706 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1708 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1709 add_code = MINUS_EXPR;
1712 /* Pointer arithmetic must be done in sizetype. */
1713 idx = convert (sizetype, idx);
1715 /* Convert the index to a byte offset. */
1716 offset = size_binop (MULT_EXPR, size, idx);
1718 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1719 if (ret == GS_ERROR)
1722 addr = build_addr_expr_with_type (array, ptrtype);
1723 result = fold (build (add_code, ptrtype, addr, offset));
1724 *expr_p = build1 (INDIRECT_REF, elttype, result);
1729 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1730 node pointed by EXPR_P.
1733 : min_lval '[' val ']'
1735 | compound_lval '[' val ']'
1736 | compound_lval '.' ID
1738 This is not part of the original SIMPLE definition, which separates
1739 array and member references, but it seems reasonable to handle them
1740 together. Also, this way we don't run into problems with union
1741 aliasing; gcc requires that for accesses through a union to alias, the
1742 union reference must be explicit, which was not always the case when we
1743 were splitting up array and member refs.
1745 PRE_P points to the list where side effects that must happen before
1746 *EXPR_P should be stored.
1748 POST_P points to the list where side effects that must happen after
1749 *EXPR_P should be stored. */
1751 static enum gimplify_status
1752 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1753 tree *post_p, int want_lvalue)
1756 enum tree_code code;
1758 enum gimplify_status ret;
1760 #if defined ENABLE_CHECKING
1761 if (TREE_CODE (*expr_p) != ARRAY_REF
1762 && TREE_CODE (*expr_p) != COMPONENT_REF
1763 && TREE_CODE (*expr_p) != REALPART_EXPR
1764 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1768 code = ERROR_MARK; /* [GIMPLE] Avoid uninitialized use warning. */
1770 /* Create a stack of the subexpressions so later we can walk them in
1771 order from inner to outer. */
1772 VARRAY_TREE_INIT (stack, 10, "stack");
1775 TREE_CODE (*p) == ARRAY_REF
1776 || TREE_CODE (*p) == COMPONENT_REF
1777 || TREE_CODE (*p) == REALPART_EXPR
1778 || TREE_CODE (*p) == IMAGPART_EXPR;
1779 p = &TREE_OPERAND (*p, 0))
1781 code = TREE_CODE (*p);
1782 if (code == ARRAY_REF)
1784 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1785 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1786 /* If the size of the array elements is not constant,
1787 computing the offset is non-trivial, so expose it. */
1790 VARRAY_PUSH_TREE (stack, *p);
1793 /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1794 TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1795 to all the refs we've walked through.
1797 Gimplify the base, and then process each of the outer nodes from left
1799 ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1800 code != ARRAY_REF ? fb_either : fb_lvalue);
1802 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1804 tree t = VARRAY_TOP_TREE (stack);
1805 if (TREE_CODE (t) == ARRAY_REF)
1807 /* Gimplify the dimension. */
1808 enum gimplify_status tret;
1809 /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1810 Gimplify non-constant array indices into a temporary
1812 FIXME - The real fix is to gimplify post-modify
1813 expressions into a minimal gimple lvalue. However, that
1814 exposes bugs in alias analysis. The alias analyzer does
1815 not handle &PTR->FIELD very well. Will fix after the
1816 branch is merged into mainline (dnovillo 2004-05-03). */
1817 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1819 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1820 is_gimple_tmp_var, fb_rvalue);
1821 if (tret == GS_ERROR)
1825 recalculate_side_effects (t);
1829 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1830 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1832 canonicalize_component_ref (expr_p);
1833 ret = MIN (ret, GS_OK);
1839 /* Re-write the ARRAY_REF node pointed by EXPR_P.
1841 PRE_P points to the list where side effects that must happen before
1842 *EXPR_P should be stored.
1844 POST_P points to the list where side effects that must happen after
1845 *EXPR_P should be stored.
1847 FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1848 argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1849 ARRAY_REF should be extended. */
1851 static enum gimplify_status
1852 gimplify_array_ref (tree *expr_p, tree *pre_p,
1853 tree *post_p, int want_lvalue)
1855 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1856 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1857 /* If the size of the array elements is not constant,
1858 computing the offset is non-trivial, so expose it. */
1859 return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1861 /* Handle array and member refs together for now. When alias analysis
1862 improves, we may want to go back to handling them separately. */
1863 return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1866 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1868 PRE_P points to the list where side effects that must happen before
1869 *EXPR_P should be stored.
1871 POST_P points to the list where side effects that must happen after
1872 *EXPR_P should be stored.
1874 WANT_VALUE is nonzero iff we want to use the value of this expression
1875 in another expression. */
1877 static enum gimplify_status
1878 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1881 enum tree_code code;
1882 tree lhs, lvalue, rhs, t1;
1884 enum tree_code arith_code;
1885 enum gimplify_status ret;
1887 code = TREE_CODE (*expr_p);
1889 #if defined ENABLE_CHECKING
1890 if (code != POSTINCREMENT_EXPR
1891 && code != POSTDECREMENT_EXPR
1892 && code != PREINCREMENT_EXPR
1893 && code != PREDECREMENT_EXPR)
1897 /* Prefix or postfix? */
1898 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1899 /* Faster to treat as prefix if result is not used. */
1900 postfix = want_value;
1904 /* Add or subtract? */
1905 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1906 arith_code = PLUS_EXPR;
1908 arith_code = MINUS_EXPR;
1910 /* Gimplify the LHS into a GIMPLE lvalue. */
1911 lvalue = TREE_OPERAND (*expr_p, 0);
1912 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1913 if (ret == GS_ERROR)
1916 /* Extract the operands to the arithmetic operation. */
1918 rhs = TREE_OPERAND (*expr_p, 1);
1920 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1921 that as the result value and in the postqueue operation. */
1924 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1925 if (ret == GS_ERROR)
1929 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1930 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1934 gimplify_stmt (&t1);
1935 append_to_statement_list (t1, post_p);
1946 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
1949 : ID '(' arglist ')'
1955 PRE_P points to the list where side effects that must happen before
1956 *EXPR_P should be stored. */
1958 static enum gimplify_status
1959 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1963 enum gimplify_status ret;
1965 #if defined ENABLE_CHECKING
1966 if (TREE_CODE (*expr_p) != CALL_EXPR)
1970 /* For reliable diagnostics during inlining, it is necessary that
1971 every call_expr be annotated with file and line. */
1972 if (!EXPR_LOCUS (*expr_p))
1973 annotate_with_locus (*expr_p, input_location);
1975 /* This may be a call to a builtin function.
1977 Builtin function calls may be transformed into different
1978 (and more efficient) builtin function calls under certain
1979 circumstances. Unfortunately, gimplification can muck things
1980 up enough that the builtin expanders are not aware that certain
1981 transformations are still valid.
1983 So we attempt transformation/gimplification of the call before
1984 we gimplify the CALL_EXPR. At this time we do not manage to
1985 transform all calls in the same manner as the expanders do, but
1986 we do transform most of them. */
1987 decl = get_callee_fndecl (*expr_p);
1988 if (decl && DECL_BUILT_IN (decl))
1992 /* If it is allocation of stack, record the need to restore the memory
1993 when the enclosing bind_expr is exited. */
1994 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1995 gimplify_ctxp->save_stack = true;
1997 /* If it is restore of the stack, reset it, since it means we are
1998 regimplifying the bind_expr. Note that we use the fact that
1999 for try_finally_expr, try part is processed first. */
2000 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
2001 gimplify_ctxp->save_stack = false;
2003 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2005 if (new && new != *expr_p)
2007 /* There was a transformation of this call which computes the
2008 same value, but in a more efficient way. Return and try
2015 /* There is a sequence point before the call, so any side effects in
2016 the calling expression must occur before the actual call. Force
2017 gimplify_expr to use an internal post queue. */
2018 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2019 is_gimple_val, fb_rvalue);
2021 if (PUSH_ARGS_REVERSED)
2022 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2023 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2024 arglist = TREE_CHAIN (arglist))
2026 enum gimplify_status t;
2028 /* There is a sequence point before a function call. Side effects in
2029 the argument list must occur before the actual call. So, when
2030 gimplifying arguments, force gimplify_expr to use an internal
2031 post queue which is then appended to the end of PRE_P. */
2032 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2038 if (PUSH_ARGS_REVERSED)
2039 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2041 /* Try this again in case gimplification exposed something. */
2042 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2044 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2046 if (new && new != *expr_p)
2048 /* There was a transformation of this call which computes the
2049 same value, but in a more efficient way. Return and try
2056 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2057 decl. This allows us to eliminate redundant or useless
2058 calls to "const" functions. */
2059 if (TREE_CODE (*expr_p) == CALL_EXPR
2060 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2061 TREE_SIDE_EFFECTS (*expr_p) = 0;
2066 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2067 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2069 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2070 condition is true or false, respectively. If null, we should generate
2071 our own to skip over the evaluation of this specific expression.
2073 This function is the tree equivalent of do_jump.
2075 shortcut_cond_r should only be called by shortcut_cond_expr. */
2078 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2080 tree local_label = NULL_TREE;
2081 tree t, expr = NULL;
2083 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2084 retain the shortcut semantics. Just insert the gotos here;
2085 shortcut_cond_expr will append the real blocks later. */
2086 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2088 /* Turn if (a && b) into
2090 if (a); else goto no;
2091 if (b) goto yes; else goto no;
2094 if (false_label_p == NULL)
2095 false_label_p = &local_label;
2097 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2098 append_to_statement_list (t, &expr);
2100 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2102 append_to_statement_list (t, &expr);
2104 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2106 /* Turn if (a || b) into
2109 if (b) goto yes; else goto no;
2112 if (true_label_p == NULL)
2113 true_label_p = &local_label;
2115 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2116 append_to_statement_list (t, &expr);
2118 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2120 append_to_statement_list (t, &expr);
2122 else if (TREE_CODE (pred) == COND_EXPR)
2124 /* As long as we're messing with gotos, turn if (a ? b : c) into
2126 if (b) goto yes; else goto no;
2128 if (c) goto yes; else goto no; */
2129 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2130 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2132 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2137 expr = build (COND_EXPR, void_type_node, pred,
2138 build_and_jump (true_label_p),
2139 build_and_jump (false_label_p));
2144 t = build1 (LABEL_EXPR, void_type_node, local_label);
2145 append_to_statement_list (t, &expr);
2152 shortcut_cond_expr (tree expr)
2154 tree pred = TREE_OPERAND (expr, 0);
2155 tree then_ = TREE_OPERAND (expr, 1);
2156 tree else_ = TREE_OPERAND (expr, 2);
2157 tree true_label, false_label, end_label, t;
2159 tree *false_label_p;
2160 bool emit_end, emit_false;
2162 /* First do simple transformations. */
2163 if (!TREE_SIDE_EFFECTS (else_))
2165 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2166 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2168 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2169 then_ = shortcut_cond_expr (expr);
2170 pred = TREE_OPERAND (pred, 0);
2171 expr = build (COND_EXPR, void_type_node, pred, then_,
2172 build_empty_stmt ());
2175 if (!TREE_SIDE_EFFECTS (then_))
2177 /* If there is no 'then', turn
2180 if (a); else if (b); else d. */
2181 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2183 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2184 else_ = shortcut_cond_expr (expr);
2185 pred = TREE_OPERAND (pred, 0);
2186 expr = build (COND_EXPR, void_type_node, pred,
2187 build_empty_stmt (), else_);
2191 /* If we're done, great. */
2192 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2193 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2196 /* Otherwise we need to mess with gotos. Change
2199 if (a); else goto no;
2202 and recursively gimplify the condition. */
2204 true_label = false_label = end_label = NULL_TREE;
2206 /* If our arms just jump somewhere, hijack those labels so we don't
2207 generate jumps to jumps. */
2209 if (TREE_CODE (then_) == GOTO_EXPR
2210 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2212 true_label = GOTO_DESTINATION (then_);
2213 then_ = build_empty_stmt ();
2216 if (TREE_CODE (else_) == GOTO_EXPR
2217 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2219 false_label = GOTO_DESTINATION (else_);
2220 else_ = build_empty_stmt ();
2223 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2225 true_label_p = &true_label;
2227 true_label_p = NULL;
2229 /* The 'else' branch also needs a label if it contains interesting code. */
2230 if (false_label || TREE_SIDE_EFFECTS (else_))
2231 false_label_p = &false_label;
2233 false_label_p = NULL;
2235 /* If there was nothing else in our arms, just forward the label(s). */
2236 if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2237 return shortcut_cond_r (pred, true_label_p, false_label_p);
2239 /* If our last subexpression already has a terminal label, reuse it. */
2240 if (TREE_SIDE_EFFECTS (else_))
2241 expr = expr_last (else_);
2243 expr = expr_last (then_);
2244 if (TREE_CODE (expr) == LABEL_EXPR)
2245 end_label = LABEL_EXPR_LABEL (expr);
2247 /* If we don't care about jumping to the 'else' branch, jump to the end
2248 if the condition is false. */
2250 false_label_p = &end_label;
2252 /* We only want to emit these labels if we aren't hijacking them. */
2253 emit_end = (end_label == NULL_TREE);
2254 emit_false = (false_label == NULL_TREE);
2256 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2259 append_to_statement_list (pred, &expr);
2261 append_to_statement_list (then_, &expr);
2262 if (TREE_SIDE_EFFECTS (else_))
2264 t = build_and_jump (&end_label);
2265 append_to_statement_list (t, &expr);
2268 t = build1 (LABEL_EXPR, void_type_node, false_label);
2269 append_to_statement_list (t, &expr);
2271 append_to_statement_list (else_, &expr);
2273 if (emit_end && end_label)
2275 t = build1 (LABEL_EXPR, void_type_node, end_label);
2276 append_to_statement_list (t, &expr);
2282 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2285 gimple_boolify (tree expr)
2287 tree type = TREE_TYPE (expr);
2289 if (TREE_CODE (type) == BOOLEAN_TYPE)
2292 /* If this is the predicate of a COND_EXPR, it might not even be a
2294 expr = lang_hooks.truthvalue_conversion (expr);
2296 switch (TREE_CODE (expr))
2298 case TRUTH_AND_EXPR:
2300 case TRUTH_XOR_EXPR:
2301 case TRUTH_ANDIF_EXPR:
2302 case TRUTH_ORIF_EXPR:
2303 /* Also boolify the arguments of truth exprs. */
2304 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2307 case TRUTH_NOT_EXPR:
2308 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2311 case EQ_EXPR: case NE_EXPR:
2312 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2313 /* These expressions always produce boolean results. */
2314 TREE_TYPE (expr) = boolean_type_node;
2318 /* Other expressions that get here must have boolean values, but
2319 might need to be converted to the appropriate mode. */
2320 return convert (boolean_type_node, expr);
2324 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2333 The second form is used when *EXPR_P is of type void.
2335 PRE_P points to the list where side effects that must happen before
2336 *EXPR_P should be stored. */
2338 static enum gimplify_status
2339 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2341 tree expr = *expr_p;
2343 enum gimplify_status ret;
2345 /* If this COND_EXPR has a value, copy the values into a temporary within
2347 if (! VOID_TYPE_P (TREE_TYPE (expr)))
2356 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2360 /* Build the then clause, 't1 = a;'. But don't build an assignment
2361 if this branch is void; in C++ it can be, if it's a throw. */
2362 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2363 TREE_OPERAND (expr, 1)
2364 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2366 /* Build the else clause, 't1 = b;'. */
2367 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2368 TREE_OPERAND (expr, 2)
2369 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2371 TREE_TYPE (expr) = void_type_node;
2372 recalculate_side_effects (expr);
2374 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2375 gimplify_stmt (&expr);
2376 append_to_statement_list (expr, pre_p);
2382 /* Make sure the condition has BOOLEAN_TYPE. */
2383 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2385 /* Break apart && and || conditions. */
2386 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2387 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2389 expr = shortcut_cond_expr (expr);
2391 if (expr != *expr_p)
2395 /* We can't rely on gimplify_expr to re-gimplify the expanded
2396 form properly, as cleanups might cause the target labels to be
2397 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2398 set up a conditional context. */
2399 gimple_push_condition ();
2400 gimplify_stmt (expr_p);
2401 gimple_pop_condition (pre_p);
2407 /* Now do the normal gimplification. */
2408 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2409 is_gimple_condexpr, fb_rvalue);
2411 gimple_push_condition ();
2413 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2414 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2415 recalculate_side_effects (expr);
2417 gimple_pop_condition (pre_p);
2419 if (ret == GS_ERROR)
2421 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2423 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2424 /* Rewrite "if (a); else b" to "if (!a) b" */
2426 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2427 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2428 is_gimple_condexpr, fb_rvalue);
2430 tmp = TREE_OPERAND (expr, 1);
2431 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2432 TREE_OPERAND (expr, 2) = tmp;
2435 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2436 expr = TREE_OPERAND (expr, 0);
2442 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2448 PRE_P points to the list where side effects that must happen before
2449 *EXPR_P should be stored.
2451 POST_P points to the list where side effects that must happen after
2452 *EXPR_P should be stored.
2454 WANT_VALUE is nonzero iff we want to use the value of this expression
2455 in another expression. */
2457 static enum gimplify_status
2458 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2460 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2461 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2462 enum gimplify_status ret;
2464 #if defined ENABLE_CHECKING
2465 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2469 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2470 if (TREE_CODE (*expr_p) == INIT_EXPR)
2471 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2473 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2474 if (ret == GS_ERROR)
2477 /* If we are initializing something from a TARGET_EXPR, strip the
2478 TARGET_EXPR and initialize it directly. */
2479 /* What about code that pulls out the temp and uses it elsewhere? I
2480 think that such code never uses the TARGET_EXPR as an initializer. If
2481 I'm wrong, we'll abort because the temp won't have any RTL. In that
2482 case, I guess we'll need to replace references somehow. */
2483 if (TREE_CODE (*from_p) == TARGET_EXPR)
2484 *from_p = TARGET_EXPR_INITIAL (*from_p);
2486 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2487 the assignment down into the branches, since we can't generate a
2488 temporary of such a type. */
2489 if (TREE_CODE (*from_p) == COND_EXPR
2490 && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2493 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2496 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2497 if (ret == GS_ERROR)
2500 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2501 if (ret != GS_UNHANDLED)
2504 /* If the destination is already simple, nothing else needed. */
2505 if (is_gimple_tmp_var (*to_p))
2509 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2510 the LHS is a user variable, then we need to introduce a temporary.
2511 ie temp = RHS; LHS = temp.
2513 This way the optimizers can determine that the user variable is
2514 only modified if evaluation of the RHS does not throw.
2516 FIXME this should be handled by the is_gimple_rhs predicate. */
2518 if (TREE_CODE (*from_p) == CALL_EXPR
2519 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2520 /* If we're dealing with a renamable type, either source or dest
2521 must be a renamed variable. */
2522 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2523 && !is_gimple_reg (*to_p)))
2524 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2526 /* If the value being copied is of variable width, expose the length
2527 if the copy by converting the whole thing to a memcpy. */
2528 /* ??? Except that we can't manage this with VA_ARG_EXPR. Yes, this
2529 does leave us with an edge condition that doesn't work. The only
2530 way out is to rearrange how VA_ARG_EXPR works. */
2531 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2532 && TREE_CODE (*from_p) != VA_ARG_EXPR)
2536 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2537 t = unshare_expr (t);
2538 args = tree_cons (NULL, t, NULL);
2539 t = build_addr_expr (*from_p);
2540 args = tree_cons (NULL, t, args);
2541 dest = build_addr_expr (*to_p);
2542 args = tree_cons (NULL, dest, args);
2543 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2544 t = build_function_call_expr (t, args);
2547 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2548 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2555 ret = want_value ? GS_OK : GS_ALL_DONE;
2560 append_to_statement_list (*expr_p, pre_p);
2567 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2568 points to the expression to gimplify.
2570 Expressions of the form 'a && b' are gimplified to:
2572 a && b ? true : false
2574 gimplify_cond_expr will do the rest.
2576 PRE_P points to the list where side effects that must happen before
2577 *EXPR_P should be stored. */
2579 static enum gimplify_status
2580 gimplify_boolean_expr (tree *expr_p)
2582 /* Preserve the original type of the expression. */
2583 tree type = TREE_TYPE (*expr_p);
2585 *expr_p = build (COND_EXPR, type, *expr_p,
2586 convert (type, boolean_true_node),
2587 convert (type, boolean_false_node));
2592 /* Gimplifies an expression sequence. This function gimplifies each
2593 expression and re-writes the original expression with the last
2594 expression of the sequence in GIMPLE form.
2596 PRE_P points to the list where the side effects for all the
2597 expressions in the sequence will be emitted.
2599 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2600 /* ??? Should rearrange to share the pre-queue with all the indirect
2601 invocations of gimplify_expr. Would probably save on creations
2602 of statement_list nodes. */
2604 static enum gimplify_status
2605 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2611 tree *sub_p = &TREE_OPERAND (t, 0);
2613 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2614 gimplify_compound_expr (sub_p, pre_p, false);
2616 gimplify_stmt (sub_p);
2617 append_to_statement_list (*sub_p, pre_p);
2619 t = TREE_OPERAND (t, 1);
2621 while (TREE_CODE (t) == COMPOUND_EXPR);
2628 gimplify_stmt (expr_p);
2633 /* Gimplifies a statement list. These may be created either by an
2634 enlightened front-end, or by shortcut_cond_expr. */
2636 static enum gimplify_status
2637 gimplify_statement_list (tree *expr_p)
2639 tree_stmt_iterator i = tsi_start (*expr_p);
2641 while (!tsi_end_p (i))
2645 gimplify_stmt (tsi_stmt_ptr (i));
2648 if (TREE_CODE (t) == STATEMENT_LIST)
2650 tsi_link_before (&i, t, TSI_SAME_STMT);
2660 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2661 gimplify. After gimplification, EXPR_P will point to a new temporary
2662 that holds the original value of the SAVE_EXPR node.
2664 PRE_P points to the list where side effects that must happen before
2665 *EXPR_P should be stored. */
2667 static enum gimplify_status
2668 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2670 enum gimplify_status ret = GS_ALL_DONE;
2673 #if defined ENABLE_CHECKING
2674 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2678 val = TREE_OPERAND (*expr_p, 0);
2680 /* If the operand is already a GIMPLE temporary, just re-write the
2682 if (is_gimple_tmp_var (val))
2684 /* The operand may be a void-valued expression such as SAVE_EXPRs
2685 generated by the Java frontend for class initialization. It is
2686 being executed only for its side-effects. */
2687 else if (TREE_TYPE (val) == void_type_node)
2689 tree body = TREE_OPERAND (*expr_p, 0);
2690 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2691 append_to_statement_list (body, pre_p);
2692 *expr_p = build_empty_stmt ();
2695 *expr_p = TREE_OPERAND (*expr_p, 0)
2696 = get_initialized_tmp_var (val, pre_p, post_p);
2701 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2708 PRE_P points to the list where side effects that must happen before
2709 *EXPR_P should be stored.
2711 POST_P points to the list where side effects that must happen after
2712 *EXPR_P should be stored. */
2714 static enum gimplify_status
2715 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2717 tree expr = *expr_p;
2718 tree op0 = TREE_OPERAND (expr, 0);
2719 enum gimplify_status ret;
2721 switch (TREE_CODE (op0))
2724 /* Check if we are dealing with an expression of the form '&*ptr'.
2725 While the front end folds away '&*ptr' into 'ptr', these
2726 expressions may be generated internally by the compiler (e.g.,
2727 builtins like __builtin_va_end). */
2728 *expr_p = TREE_OPERAND (op0, 0);
2733 /* Fold &a[6] to (&a + 6). */
2734 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2737 /* This added an INDIRECT_REF. Fold it away. */
2738 op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2744 /* We use fb_either here because the C frontend sometimes takes
2745 the address of a call that returns a struct. */
2746 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2747 is_gimple_addr_expr_arg, fb_either);
2748 if (ret != GS_ERROR)
2750 /* At this point, the argument of the ADDR_EXPR should be
2751 sufficiently simple that there are never side effects. */
2752 /* ??? Could split out the decision code from build1 to verify. */
2753 TREE_SIDE_EFFECTS (expr) = 0;
2755 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2756 recompute_tree_invarant_for_addr_expr (expr);
2758 /* Mark the RHS addressable. */
2759 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2764 /* If the operand is gimplified into a _DECL, mark the address expression
2765 as TREE_INVARIANT. */
2766 if (DECL_P (TREE_OPERAND (expr, 0)))
2767 TREE_INVARIANT (expr) = 1;
2772 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2773 value; output operands should be a gimple lvalue. */
2775 static enum gimplify_status
2776 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2778 tree expr = *expr_p;
2779 int noutputs = list_length (ASM_OUTPUTS (expr));
2780 const char **oconstraints
2781 = (const char **) alloca ((noutputs) * sizeof (const char *));
2784 const char *constraint;
2785 bool allows_mem, allows_reg, is_inout;
2786 enum gimplify_status ret, tret;
2789 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2793 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2795 oconstraints[i] = constraint
2796 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2798 parse_output_constraint (&constraint, i, 0, 0,
2799 &allows_mem, &allows_reg, &is_inout);
2801 if (!allows_reg && allows_mem)
2802 lang_hooks.mark_addressable (TREE_VALUE (link));
2804 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2805 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2806 fb_lvalue | fb_mayfail);
2807 if (tret == GS_ERROR)
2809 error ("invalid lvalue in asm output %d", i);
2815 /* An input/output operand. To give the optimizers more
2816 flexibility, split it into separate input and output
2820 size_t constraint_len = strlen (constraint);
2822 /* Turn the in/out constraint into an output constraint. */
2823 char *p = xstrdup (constraint);
2825 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2828 /* And add a matching input constraint. */
2831 sprintf (buf, "%d", i);
2832 input = build_string (strlen (buf), buf);
2835 input = build_string (constraint_len - 1, constraint + 1);
2836 input = build_tree_list (build_tree_list (NULL_TREE, input),
2837 unshare_expr (TREE_VALUE (link)));
2838 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2842 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2845 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2846 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2847 oconstraints, &allows_mem, &allows_reg);
2849 /* If the operand is a memory input, it should be an lvalue. */
2850 if (!allows_reg && allows_mem)
2852 lang_hooks.mark_addressable (TREE_VALUE (link));
2853 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2854 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2855 if (tret == GS_ERROR)
2857 error ("memory input %d is not directly addressable", i);
2863 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2864 is_gimple_val, fb_rvalue);
2865 if (tret == GS_ERROR)
2873 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
2874 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2875 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2876 return to this function.
2878 FIXME should we complexify the prequeue handling instead? Or use flags
2879 for all the cleanups and let the optimizer tighten them up? The current
2880 code seems pretty fragile; it will break on a cleanup within any
2881 non-conditional nesting. But any such nesting would be broken, anyway;
2882 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2883 and continues out of it. We can do that at the RTL level, though, so
2884 having an optimizer to tighten up try/finally regions would be a Good
2887 static enum gimplify_status
2888 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2890 tree_stmt_iterator iter;
2893 tree temp = voidify_wrapper_expr (*expr_p);
2895 /* We only care about the number of conditions between the innermost
2896 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
2897 int old_conds = gimplify_ctxp->conditions;
2898 gimplify_ctxp->conditions = 0;
2900 body = TREE_OPERAND (*expr_p, 0);
2901 gimplify_to_stmt_list (&body);
2903 gimplify_ctxp->conditions = old_conds;
2905 for (iter = tsi_start (body); !tsi_end_p (iter); )
2907 tree *wce_p = tsi_stmt_ptr (iter);
2910 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2912 if (tsi_one_before_end_p (iter))
2914 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2922 sl = tsi_split_statement_list_after (&iter);
2923 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2924 append_to_statement_list (TREE_OPERAND (wce, 1),
2925 &TREE_OPERAND (tfe, 1));
2927 iter = tsi_start (sl);
2937 append_to_statement_list (body, pre_p);
2947 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
2948 is the cleanup action required. */
2951 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2955 /* Errors can result in improperly nested cleanups. Which results in
2956 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
2957 if (errorcount || sorrycount)
2960 if (gimple_conditional_context ())
2962 /* If we're in a conditional context, this is more complex. We only
2963 want to run the cleanup if we actually ran the initialization that
2964 necessitates it, but we want to run it after the end of the
2965 conditional context. So we wrap the try/finally around the
2966 condition and use a flag to determine whether or not to actually
2967 run the destructor. Thus
2971 becomes (approximately)
2975 if (test) { A::A(temp); flag = 1; val = f(temp); }
2978 if (flag) A::~A(temp);
2983 tree flag = create_tmp_var (boolean_type_node, "cleanup");
2984 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2985 boolean_false_node);
2986 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2988 cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2989 build_empty_stmt ());
2990 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2991 cleanup, NULL_TREE);
2992 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2993 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2994 append_to_statement_list (ftrue, pre_p);
2996 /* Because of this manipulation, and the EH edges that jump
2997 threading cannot redirect, the temporary (VAR) will appear
2998 to be used uninitialized. Don't warn. */
2999 TREE_NO_WARNING (var) = 1;
3003 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3004 cleanup, NULL_TREE);
3005 append_to_statement_list (wce, pre_p);
3008 gimplify_stmt (&TREE_OPERAND (wce, 1));
3011 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3013 static enum gimplify_status
3014 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3016 tree targ = *expr_p;
3017 tree temp = TARGET_EXPR_SLOT (targ);
3018 tree init = TARGET_EXPR_INITIAL (targ);
3019 enum gimplify_status ret;
3023 /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
3025 gimple_add_tmp_var (temp);
3027 /* Build up the initialization and add it to pre_p. */
3028 init = build (MODIFY_EXPR, void_type_node, temp, init);
3029 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3030 if (ret == GS_ERROR)
3033 append_to_statement_list (init, pre_p);
3035 /* If needed, push the cleanup for the temp. */
3036 if (TARGET_EXPR_CLEANUP (targ))
3038 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3039 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3042 /* Only expand this once. */
3043 TREE_OPERAND (targ, 3) = init;
3044 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3046 else if (!temp->decl.seen_in_bind_expr)
3047 /* We should have expanded this before. */
3054 /* Gimplification of expression trees. */
3056 /* Gimplify an expression which appears at statement context; usually, this
3057 means replacing it with a suitably gimple STATEMENT_LIST. */
3060 gimplify_stmt (tree *stmt_p)
3062 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3064 *stmt_p = alloc_stmt_list ();
3067 /* Similarly, but force the result to be a STATEMENT_LIST. */
3070 gimplify_to_stmt_list (tree *stmt_p)
3072 gimplify_stmt (stmt_p);
3073 if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3076 *stmt_p = alloc_stmt_list ();
3077 append_to_statement_list (t, stmt_p);
3082 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3083 gimplification failed.
3085 PRE_P points to the list where side effects that must happen before
3086 EXPR should be stored.
3088 POST_P points to the list where side effects that must happen after
3089 EXPR should be stored, or NULL if there is no suitable list. In
3090 that case, we copy the result to a temporary, emit the
3091 post-effects, and then return the temporary.
3093 GIMPLE_TEST_F points to a function that takes a tree T and
3094 returns nonzero if T is in the GIMPLE form requested by the
3095 caller. The GIMPLE predicates are in tree-gimple.c.
3097 This test is used twice. Before gimplification, the test is
3098 invoked to determine whether *EXPR_P is already gimple enough. If
3099 that fails, *EXPR_P is gimplified according to its code and
3100 GIMPLE_TEST_F is called again. If the test still fails, then a new
3101 temporary variable is created and assigned the value of the
3102 gimplified expression.
3104 FALLBACK tells the function what sort of a temporary we want. If the 1
3105 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3106 If both are set, either is OK, but an lvalue is preferable.
3108 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3109 iterates until solution. */
3111 enum gimplify_status
3112 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3113 bool (* gimple_test_f) (tree), fallback_t fallback)
3116 tree internal_pre = NULL_TREE;
3117 tree internal_post = NULL_TREE;
3119 int is_statement = (pre_p == NULL);
3121 location_t saved_location;
3122 enum gimplify_status ret;
3124 save_expr = *expr_p;
3125 if (save_expr == NULL_TREE)
3128 /* We used to check the predicate here and return immediately if it
3129 succeeds. This is wrong; the design is for gimplification to be
3130 idempotent, and for the predicates to only test for valid forms, not
3131 whether they are fully simplified. */
3133 /* Set up our internal queues if needed. */
3135 pre_p = &internal_pre;
3137 post_p = &internal_post;
3139 saved_location = input_location;
3140 if (save_expr == error_mark_node)
3143 locus = EXPR_LOCUS (save_expr);
3145 input_location = *locus;
3147 /* Loop over the specific gimplifiers until the toplevel node
3148 remains the same. */
3151 /* Strip any uselessness. */
3152 STRIP_MAIN_TYPE_NOPS (*expr_p);
3154 /* Remember the expr. */
3155 save_expr = *expr_p;
3157 /* Die, die, die, my darling. */
3158 if (save_expr == error_mark_node
3159 || TREE_TYPE (save_expr) == error_mark_node)
3165 /* Do any language-specific gimplification. */
3166 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3169 if (*expr_p == NULL_TREE)
3171 if (*expr_p != save_expr)
3174 else if (ret != GS_UNHANDLED)
3178 switch (TREE_CODE (*expr_p))
3180 /* First deal with the special cases. */
3182 case POSTINCREMENT_EXPR:
3183 case POSTDECREMENT_EXPR:
3184 case PREINCREMENT_EXPR:
3185 case PREDECREMENT_EXPR:
3186 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3187 fallback != fb_none);
3191 ret = gimplify_array_ref (expr_p, pre_p, post_p,
3192 fallback & fb_lvalue);
3196 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3197 fallback & fb_lvalue);
3201 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3205 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3212 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3217 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3218 fallback & fb_lvalue);
3223 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3224 fallback != fb_none);
3227 case TRUTH_ANDIF_EXPR:
3228 case TRUTH_ORIF_EXPR:
3229 ret = gimplify_boolean_expr (expr_p);
3232 case TRUTH_NOT_EXPR:
3233 TREE_OPERAND (*expr_p, 0)
3234 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3235 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3236 is_gimple_val, fb_rvalue);
3237 recalculate_side_effects (*expr_p);
3241 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3245 /* Mark any _DECL inside the operand as volatile to avoid the
3246 optimizers messing around with it. FIXME: Remove this once
3247 VA_ARG_EXPRs are properly lowered. */
3248 walk_tree (&TREE_OPERAND (*expr_p, 0), mark_decls_volatile_r,
3251 /* va_arg expressions are in GIMPLE form already. */
3257 if (IS_EMPTY_STMT (*expr_p))
3263 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3264 || fallback == fb_none)
3266 /* Just strip a conversion to void (or in void context) and
3268 *expr_p = TREE_OPERAND (*expr_p, 0);
3272 ret = gimplify_conversion (expr_p);
3273 if (ret == GS_ERROR)
3275 if (*expr_p != save_expr)
3279 case FIX_TRUNC_EXPR:
3281 case FIX_FLOOR_EXPR:
3282 case FIX_ROUND_EXPR:
3283 /* unary_expr: ... | '(' cast ')' val | ... */
3284 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3285 is_gimple_val, fb_rvalue);
3286 recalculate_side_effects (*expr_p);
3290 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3291 is_gimple_reg, fb_rvalue);
3292 recalculate_side_effects (*expr_p);
3295 /* Constants need not be gimplified. */
3305 *expr_p = DECL_INITIAL (*expr_p);
3309 /* FIXME make this a decl. */
3314 ret = gimplify_bind_expr (expr_p, pre_p);
3318 ret = gimplify_loop_expr (expr_p, pre_p);
3322 ret = gimplify_switch_expr (expr_p, pre_p);
3325 case LABELED_BLOCK_EXPR:
3326 ret = gimplify_labeled_block_expr (expr_p);
3329 case EXIT_BLOCK_EXPR:
3330 ret = gimplify_exit_block_expr (expr_p);
3334 ret = gimplify_exit_expr (expr_p);
3338 /* If the target is not LABEL, then it is a computed jump
3339 and the target needs to be gimplified. */
3340 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3341 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3342 NULL, is_gimple_val, fb_rvalue);
3347 #ifdef ENABLE_CHECKING
3348 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3353 case CASE_LABEL_EXPR:
3354 ret = gimplify_case_label_expr (expr_p);
3358 ret = gimplify_return_expr (*expr_p, pre_p);
3362 /* Don't reduce this in place; let gimplify_init_constructor work
3367 /* The following are special cases that are not handled by the
3368 original GIMPLE grammar. */
3370 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3373 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3378 enum gimplify_status r0, r1, r2;
3380 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3381 is_gimple_min_lval, fb_either);
3382 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3383 is_gimple_val, fb_rvalue);
3384 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3385 is_gimple_val, fb_rvalue);
3386 recalculate_side_effects (*expr_p);
3388 ret = MIN (r0, MIN (r1, r2));
3392 case NON_LVALUE_EXPR:
3393 /* This should have been stripped above. */
3398 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3401 case TRY_FINALLY_EXPR:
3402 case TRY_CATCH_EXPR:
3403 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3404 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3408 case CLEANUP_POINT_EXPR:
3409 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3413 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3417 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3421 case EH_FILTER_EXPR:
3422 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3427 /* This moves much of the actual computation out of the
3428 VTABLE_REF. Perhaps this should be revisited once we want to
3429 do clever things with VTABLE_REFs. */
3430 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3431 is_gimple_min_lval, fb_lvalue);
3436 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3440 /* We get here when taking the address of a label. We mark
3441 the label as "forced"; meaning it can never be removed and
3442 it is a potential target for any computed goto. */
3443 FORCED_LABEL (*expr_p) = 1;
3447 case STATEMENT_LIST:
3448 ret = gimplify_statement_list (expr_p);
3452 /* ??? If this is a local variable, and it has not been seen in any
3453 outer BIND_EXPR, then it's probably the result of a duplicate
3454 declaration, for which we've already issued an error. It would
3455 be really nice if the front end wouldn't leak these at all.
3456 Currently the only known culprit is C++ destructors, as seen
3457 in g++.old-deja/g++.jason/binding.C. */
3459 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3460 && decl_function_context (tmp) == current_function_decl
3461 && !tmp->decl.seen_in_bind_expr)
3463 #ifdef ENABLE_CHECKING
3464 if (!errorcount && !sorrycount)
3474 /* If *EXPR_P does not need to be special-cased, handle it
3475 according to its class. */
3476 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3477 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3478 post_p, is_gimple_val, fb_rvalue);
3479 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3480 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3481 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3482 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3483 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3485 enum gimplify_status r0, r1;
3487 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3488 post_p, is_gimple_val, fb_rvalue);
3489 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3490 post_p, is_gimple_val, fb_rvalue);
3494 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3495 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3501 /* Fail if we don't know how to handle this tree code. */
3504 recalculate_side_effects (*expr_p);
3508 /* If we replaced *expr_p, gimplify again. */
3509 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3512 while (ret == GS_OK);
3514 /* If we encountered an error_mark somewhere nested inside, either
3515 stub out the statement or propagate the error back out. */
3516 if (ret == GS_ERROR)
3519 *expr_p = build_empty_stmt ();
3523 #ifdef ENABLE_CHECKING
3524 /* This was only valid as a return value from the langhook, which
3525 we handled. Make sure it doesn't escape from any other context. */
3526 if (ret == GS_UNHANDLED)
3531 *expr_p = build_empty_stmt ();
3532 if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3534 /* We aren't looking for a value, and we don't have a valid
3535 statement. If it doesn't have side-effects, throw it away. */
3536 if (!TREE_SIDE_EFFECTS (*expr_p))
3537 *expr_p = build_empty_stmt ();
3538 else if (!TREE_THIS_VOLATILE (*expr_p))
3539 /* We only handle volatiles here; anything else with side-effects
3540 must be converted to a valid statement before we get here. */
3542 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3544 /* Historically, the compiler has treated a bare
3545 reference to a volatile lvalue as forcing a load. */
3546 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3547 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3550 /* We can't do anything useful with a volatile reference to
3551 incomplete type, so just throw it away. */
3552 *expr_p = build_empty_stmt ();
3555 /* If we are gimplifying at the statement level, we're done. Tack
3556 everything together and replace the original statement with the
3560 if (internal_pre || internal_post)
3562 append_to_statement_list (*expr_p, &internal_pre);
3563 append_to_statement_list (internal_post, &internal_pre);
3564 annotate_all_with_locus (&internal_pre, input_location);
3565 *expr_p = internal_pre;
3570 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3573 /* If it's sufficiently simple already, we're done. Unless we are
3574 handling some post-effects internally; if that's the case, we need to
3575 copy into a temp before adding the post-effects to the tree. */
3576 if (!internal_post && (*gimple_test_f) (*expr_p))
3579 /* Otherwise, we need to create a new temporary for the gimplified
3582 /* We can't return an lvalue if we have an internal postqueue. The
3583 object the lvalue refers to would (probably) be modified by the
3584 postqueue; we need to copy the value out first, which means an
3586 if ((fallback & fb_lvalue) && !internal_post
3587 && is_gimple_addr_expr_arg (*expr_p))
3589 /* An lvalue will do. Take the address of the expression, store it
3590 in a temporary, and replace the expression with an INDIRECT_REF of
3592 tmp = build_addr_expr (*expr_p);
3593 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3594 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3596 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3598 #if defined ENABLE_CHECKING
3599 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3603 /* An rvalue will do. Assign the gimplified expression into a new
3604 temporary TMP and replace the original expression with TMP. */
3606 if (internal_post || (fallback & fb_lvalue))
3607 /* The postqueue might change the value of the expression between
3608 the initialization and use of the temporary, so we can't use a
3609 formal temp. FIXME do we care? */
3610 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3612 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3614 else if (fallback & fb_mayfail)
3616 /* If this is an asm statement, and the user asked for the impossible,
3617 don't abort. Fail and let gimplify_asm_expr issue an error. */
3623 fprintf (stderr, "gimplification failed:\n");
3624 print_generic_expr (stderr, *expr_p, 0);
3625 debug_tree (*expr_p);
3629 #if defined ENABLE_CHECKING
3630 /* Make sure the temporary matches our predicate. */
3631 if (!(*gimple_test_f) (*expr_p))
3637 annotate_all_with_locus (&internal_post, input_location);
3638 append_to_statement_list (internal_post, pre_p);
3642 input_location = saved_location;
3646 #ifdef ENABLE_CHECKING
3647 /* Compare types A and B for a "close enough" match. */
3650 cpt_same_type (tree a, tree b)
3652 if (lang_hooks.types_compatible_p (a, b))
3655 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3656 link them together. This routine is intended to catch type errors
3657 that will affect the optimizers, and the optimizers don't add new
3658 dereferences of function pointers, so ignore it. */
3659 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3660 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3663 /* ??? The C FE pushes type qualifiers after the fact into the type of
3664 the element from the type of the array. See build_unary_op's handling
3665 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3666 should have done it when creating the variable in the first place.
3667 Alternately, why aren't the two array types made variants? */
3668 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3669 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3671 /* And because of those, we have to recurse down through pointers. */
3672 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3673 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3678 /* Check for some cases of the front end missing cast expressions.
3679 The type of a dereference should correspond to the pointer type;
3680 similarly the type of an address should match its object. */
3683 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3684 void *data ATTRIBUTE_UNUSED)
3687 tree ptype, otype, dtype;
3689 switch (TREE_CODE (t))
3693 otype = TREE_TYPE (t);
3694 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3695 dtype = TREE_TYPE (ptype);
3696 if (!cpt_same_type (otype, dtype))
3701 ptype = TREE_TYPE (t);
3702 otype = TREE_TYPE (TREE_OPERAND (t, 0));
3703 dtype = TREE_TYPE (ptype);
3704 if (!cpt_same_type (otype, dtype))
3706 /* &array is allowed to produce a pointer to the element,
3707 rather than a pointer to the array type. */
3708 if (TREE_CODE (otype) == ARRAY_TYPE
3709 && POINTER_TYPE_P (ptype)
3710 && cpt_same_type (TREE_TYPE (otype), dtype))
3725 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
3726 function decl containing BODY. */
3729 gimplify_body (tree *body_p, tree fndecl)
3731 location_t saved_location = input_location;
3734 timevar_push (TV_TREE_GIMPLIFY);
3735 push_gimplify_context ();
3737 /* Unshare most shared trees in the body. */
3738 unshare_all_trees (*body_p);
3740 /* Make sure input_location isn't set to something wierd. */
3741 input_location = DECL_SOURCE_LOCATION (fndecl);
3743 /* Gimplify the function's body. */
3744 gimplify_stmt (body_p);
3747 /* Unshare again, in case gimplification was sloppy. */
3748 unshare_all_trees (body);
3750 /* If there isn't an outer BIND_EXPR, add one. */
3751 if (TREE_CODE (body) == STATEMENT_LIST)
3753 tree t = expr_only (*body_p);
3757 if (TREE_CODE (body) != BIND_EXPR)
3759 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3760 NULL_TREE, NULL_TREE);
3761 TREE_SIDE_EFFECTS (b) = 1;
3762 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3767 pop_gimplify_context (body);
3769 #ifdef ENABLE_CHECKING
3770 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3773 timevar_pop (TV_TREE_GIMPLIFY);
3774 input_location = saved_location;
3777 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
3778 node for the function we want to gimplify. */
3781 gimplify_function_tree (tree fndecl)
3785 oldfn = current_function_decl;
3786 current_function_decl = fndecl;
3788 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3790 /* If we're instrumenting function entry/exit, then prepend the call to
3791 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3792 catch the exit hook. */
3793 /* ??? Add some way to ignore exceptions for this TFE. */
3794 if (flag_instrument_function_entry_exit
3795 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3799 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3800 TREE_SIDE_EFFECTS (tf) = 1;
3801 x = DECL_SAVED_TREE (fndecl);
3802 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3803 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3804 x = build_function_call_expr (x, NULL);
3805 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3807 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3808 TREE_SIDE_EFFECTS (bind) = 1;
3809 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3810 x = build_function_call_expr (x, NULL);
3811 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3812 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3814 DECL_SAVED_TREE (fndecl) = bind;
3817 current_function_decl = oldfn;
3820 #include "gt-gimplify.h"