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"
50 static struct gimplify_ctx
52 tree current_bind_expr;
55 tree conditional_cleanups;
59 varray_type case_labels;
60 /* The formal temporary table. Should this be persistent? */
65 /* Formal (expression) temporary table handling: Multiple occurrences of
66 the same scalar expression are evaluated into the same temporary. */
68 typedef struct gimple_temp_hash_elt
71 tree temp; /* Value */
74 /* Return a hash value for a formal temporary table entry. */
77 gimple_tree_hash (const void *p)
79 tree t = ((const elt_t *)p)->val;
80 return iterative_hash_expr (t, 0);
83 /* Compare two formal temporary table entries. */
86 gimple_tree_eq (const void *p1, const void *p2)
88 tree t1 = ((const elt_t *)p1)->val;
89 tree t2 = ((const elt_t *)p2)->val;
90 enum tree_code code = TREE_CODE (t1);
92 if (TREE_CODE (t2) != code
93 || TREE_TYPE (t1) != TREE_TYPE (t2))
96 if (!operand_equal_p (t1, t2, 0))
99 /* Only allow them to compare equal if they also hash equal; otherwise
100 results are nondeterminate, and we fail bootstrap comparison. */
101 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
107 /* Set up a context for the gimplifier. */
110 push_gimplify_context (void)
115 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
116 gimplify_ctxp->temp_htab
117 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
120 /* Tear down a context for the gimplifier. If BODY is non-null, then
121 put the temporaries into the outer BIND_EXPR. Otherwise, put them
122 in the unexpanded_var_list. */
125 pop_gimplify_context (tree body)
127 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
131 declare_tmp_vars (gimplify_ctxp->temps, body);
133 record_vars (gimplify_ctxp->temps);
137 fprintf (stderr, " collisions: %f ",
138 htab_collisions (gimplify_ctxp->temp_htab));
141 htab_delete (gimplify_ctxp->temp_htab);
142 free (gimplify_ctxp);
143 gimplify_ctxp = NULL;
147 gimple_push_bind_expr (tree bind)
149 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
150 gimplify_ctxp->current_bind_expr = bind;
154 gimple_pop_bind_expr (void)
156 gimplify_ctxp->current_bind_expr
157 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
161 gimple_current_bind_expr (void)
163 return gimplify_ctxp->current_bind_expr;
166 /* Returns true iff there is a COND_EXPR between us and the innermost
167 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
170 gimple_conditional_context (void)
172 return gimplify_ctxp->conditions > 0;
175 /* Note that we've entered a COND_EXPR. */
178 gimple_push_condition (void)
180 ++(gimplify_ctxp->conditions);
183 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
184 now, add any conditional cleanups we've seen to the prequeue. */
187 gimple_pop_condition (tree *pre_p)
189 int conds = --(gimplify_ctxp->conditions);
192 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
193 gimplify_ctxp->conditional_cleanups = NULL_TREE;
199 /* A subroutine of append_to_statement_list{,_force}. */
202 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
205 tree_stmt_iterator i;
212 if (t && TREE_CODE (t) == STATEMENT_LIST)
217 *list_p = list = alloc_stmt_list ();
221 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
224 /* Add T to the end of the list container pointed by LIST_P.
225 If T is an expression with no effects, it is ignored. */
228 append_to_statement_list (tree t, tree *list_p)
230 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
233 /* Similar, but the statement is always added, regardless of side effects. */
236 append_to_statement_list_force (tree t, tree *list_p)
238 append_to_statement_list_1 (t, list_p, t != NULL);
241 /* Both gimplify the statement T and append it to LIST_P. */
244 gimplify_and_add (tree t, tree *list_p)
247 append_to_statement_list (t, list_p);
250 /* Add T to the end of a COMPOUND_EXPR pointed by LIST_P. The type
251 of the result is the type of T. */
254 append_to_compound_expr (tree t, tree *list_p)
261 *list_p = build (COMPOUND_EXPR, TREE_TYPE (t), *list_p, t);
264 /* Strip off a legitimate source ending from the input string NAME of
265 length LEN. Rather than having to know the names used by all of
266 our front ends, we strip off an ending of a period followed by
267 up to five characters. (Java uses ".class".) */
270 remove_suffix (char *name, int len)
274 for (i = 2; i < 8 && len > i; i++)
276 if (name[len - i] == '.')
278 name[len - i] = '\0';
284 /* Create a nameless artificial label and put it in the current function
285 context. Returns the newly created label. */
288 create_artificial_label (void)
290 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
291 DECL_ARTIFICIAL (lab) = 1;
292 DECL_CONTEXT (lab) = current_function_decl;
296 /* Create a new temporary name with PREFIX. Returns an identifier. */
298 static GTY(()) unsigned int tmp_var_id_num;
301 create_tmp_var_name (const char *prefix)
307 char *preftmp = ASTRDUP (prefix);
308 remove_suffix (preftmp, strlen (preftmp));
312 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
313 return get_identifier (tmp_name);
317 /* Create a new temporary variable declaration of type TYPE.
318 Does NOT push it into the current binding. */
321 create_tmp_var_raw (tree type, const char *prefix)
326 /* Make the type of the variable writable. */
327 new_type = build_type_variant (type, 0, 0);
328 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
330 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
332 /* The variable was declared by the compiler. */
333 DECL_ARTIFICIAL (tmp_var) = 1;
334 /* And we don't want debug info for it. */
335 DECL_IGNORED_P (tmp_var) = 1;
337 /* Make the variable writable. */
338 TREE_READONLY (tmp_var) = 0;
340 DECL_EXTERNAL (tmp_var) = 0;
341 TREE_STATIC (tmp_var) = 0;
342 TREE_USED (tmp_var) = 1;
347 /* Create a new temporary variable declaration of type TYPE. DOES push the
348 variable into the current binding. Further, assume that this is called
349 only from gimplification or optimization, at which point the creation of
350 certain types are bugs. */
353 create_tmp_var (tree type, const char *prefix)
357 #if defined ENABLE_CHECKING
358 /* If the type is an array or a type which must be created by the
359 frontend, something is wrong. */
360 if (TREE_CODE (type) == ARRAY_TYPE || TREE_ADDRESSABLE (type))
362 if (!COMPLETE_TYPE_P (type))
364 /* Variable sized types require lots of machinery to create; the
365 optimizers shouldn't be doing anything of the sort. */
366 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
370 tmp_var = create_tmp_var_raw (type, prefix);
371 gimple_add_tmp_var (tmp_var);
375 /* Given a tree, try to return a useful variable name that we can use
376 to prefix a temporary that is being assigned the value of the tree.
377 I.E. given <temp> = &A, return A. */
385 STRIP_NOPS (stripped_decl);
386 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
387 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
390 switch (TREE_CODE (stripped_decl))
393 return get_name (TREE_OPERAND (stripped_decl, 0));
401 /* Create a temporary with a name derived from VAL. Subroutine of
402 lookup_tmp_var; nobody else should call this function. */
405 create_tmp_from_val (tree val)
407 return create_tmp_var (TREE_TYPE (val), get_name (val));
410 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
411 an existing expression temporary. */
414 lookup_tmp_var (tree val, bool is_formal)
416 if (!is_formal || TREE_SIDE_EFFECTS (val))
417 return create_tmp_from_val (val);
424 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
427 elt_p = xmalloc (sizeof (*elt_p));
429 elt_p->temp = create_tmp_from_val (val);
430 *slot = (void *)elt_p;
433 elt_p = (elt_t *) *slot;
439 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
440 in gimplify_expr. Only use this function if:
442 1) The value of the unfactored expression represented by VAL will not
443 change between the initialization and use of the temporary, and
444 2) The temporary will not be otherwise modified.
446 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
447 and #2 means it is inappropriate for && temps.
449 For other cases, use get_initialized_tmp_var instead. */
452 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
457 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
459 t = lookup_tmp_var (val, is_formal);
461 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
463 class = TREE_CODE_CLASS (TREE_CODE (val));
464 if (EXPR_LOCUS (val))
465 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
467 annotate_with_locus (mod, input_location);
468 /* gimplify_modify_expr might want to reduce this further. */
469 gimplify_stmt (&mod);
470 append_to_statement_list (mod, pre_p);
476 get_formal_tmp_var (tree val, tree *pre_p)
478 return internal_get_tmp_var (val, pre_p, NULL, true);
481 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
482 are as in gimplify_expr. */
485 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
487 return internal_get_tmp_var (val, pre_p, post_p, false);
490 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
493 is_gimple_tmp_var (tree t)
495 /* FIXME this could trigger for other local artificials, too. */
496 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
497 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
500 /* Declares all the variables in VARS in SCOPE. Returns the last
501 DECL_STMT emitted. */
504 declare_tmp_vars (tree vars, tree scope)
511 /* C99 mode puts the default 'return 0;' for main() outside the outer
512 braces. So drill down until we find an actual scope. */
513 while (TREE_CODE (scope) == COMPOUND_EXPR)
514 scope = TREE_OPERAND (scope, 0);
516 if (TREE_CODE (scope) != BIND_EXPR)
519 temps = nreverse (last);
520 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
521 BIND_EXPR_VARS (scope) = temps;
523 /* We don't add the temps to the block for this BIND_EXPR, as we're
524 not interested in debugging info for them. */
529 gimple_add_tmp_var (tree tmp)
531 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
534 DECL_CONTEXT (tmp) = current_function_decl;
535 tmp->decl.seen_in_bind_expr = 1;
539 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
540 gimplify_ctxp->temps = tmp;
545 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
548 /* Determines whether to assign a locus to the statement STMT. */
551 should_carry_locus_p (tree stmt)
553 /* Don't emit a line note for a label. We particularly don't want to
554 emit one for the break label, since it doesn't actually correspond
555 to the beginning of the loop/switch. */
556 if (TREE_CODE (stmt) == LABEL_EXPR)
559 /* Do not annotate empty statements, since it confuses gcov. */
560 if (!TREE_SIDE_EFFECTS (stmt))
567 annotate_all_with_locus (tree *stmt_p, location_t locus)
569 tree_stmt_iterator i;
574 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
576 tree t = tsi_stmt (i);
578 #ifdef ENABLE_CHECKING
579 /* Assuming we've already been gimplified, we shouldn't
580 see nested chaining constructs anymore. */
581 if (TREE_CODE (t) == STATEMENT_LIST
582 || TREE_CODE (t) == COMPOUND_EXPR)
586 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
587 && ! EXPR_HAS_LOCATION (t)
588 && should_carry_locus_p (t))
589 annotate_with_locus (t, locus);
593 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
594 These nodes model computations that should only be done once. If we
595 were to unshare something like SAVE_EXPR(i++), the gimplification
596 process would create wrong code. */
599 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
601 enum tree_code code = TREE_CODE (*tp);
602 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
603 if (TREE_CODE_CLASS (code) == 't'
604 || TREE_CODE_CLASS (code) == 'd'
605 || TREE_CODE_CLASS (code) == 'c'
606 || code == SAVE_EXPR || code == TARGET_EXPR
607 /* We can't do anything sensible with a BLOCK used as an expression,
608 but we also can't abort when we see it because of non-expression
609 uses. So just avert our eyes and cross our fingers. Silly Java. */
612 else if (code == BIND_EXPR)
615 copy_tree_r (tp, walk_subtrees, data);
620 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
621 after VA_ARG_EXPRs are properly lowered. */
624 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
625 void *data ATTRIBUTE_UNUSED)
628 TREE_THIS_VOLATILE (*tp) = 1;
634 /* Callback for walk_tree to unshare most of the shared trees rooted at
635 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
636 then *TP is deep copied by calling copy_tree_r.
638 This unshares the same trees as copy_tree_r with the exception of
639 SAVE_EXPR nodes. These nodes model computations that should only be
640 done once. If we were to unshare something like SAVE_EXPR(i++), the
641 gimplification process would create wrong code. */
644 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
645 void *data ATTRIBUTE_UNUSED)
648 enum tree_code code = TREE_CODE (t);
650 /* Skip types, decls, and constants. */
651 if (TREE_CODE_CLASS (code) == 't'
652 || TREE_CODE_CLASS (code) == 'd'
653 || TREE_CODE_CLASS (code) == 'c')
656 /* Special-case BIND_EXPR. We should never be copying these, therefore
657 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
658 double processing of the DECL_INITIAL, which could be seen via both
659 the BIND_EXPR_VARS and a DECL_STMT. */
660 else if (code == BIND_EXPR)
662 if (TREE_VISITED (t))
664 TREE_VISITED (t) = 1;
666 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
669 /* If this node has been visited already, unshare it and don't look
671 else if (TREE_VISITED (t))
673 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
677 /* Otherwise, mark the tree as visited and keep looking. */
680 TREE_VISITED (t) = 1;
681 if (TREE_CODE (*tp) == VA_ARG_EXPR
682 && targetm.calls.gimplify_va_arg_expr == NULL)
684 /* Mark any _DECL inside the operand as volatile to avoid
685 the optimizers messing around with it. We have to do this
686 early, otherwise we might mark a variable as volatile
687 after we gimplify other statements that use the variable
688 assuming it's not volatile. */
690 /* FIXME once most targets define the above hook, this should
691 go away (perhaps along with the #include "target.h"). */
692 walk_tree (&TREE_OPERAND (*tp, 0), mark_decls_volatile_r,
701 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
702 void *data ATTRIBUTE_UNUSED)
704 if (TREE_VISITED (*tp))
705 TREE_VISITED (*tp) = 0;
712 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
715 unshare_all_trees (tree t)
717 walk_tree (&t, copy_if_shared_r, NULL, NULL);
718 walk_tree (&t, unmark_visited_r, NULL, NULL);
721 /* Unconditionally make an unshared copy of EXPR. This is used when using
722 stored expressions which span multiple functions, such as BINFO_VTABLE,
723 as the normal unsharing process can't tell that they're shared. */
726 unshare_expr (tree expr)
728 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
732 /* A terser interface for building a representation of a exception
736 gimple_build_eh_filter (tree body, tree allowed, tree failure)
740 /* FIXME should the allowed types go in TREE_TYPE? */
741 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
742 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
744 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
745 append_to_statement_list (body, &TREE_OPERAND (t, 0));
751 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
752 contain statements and have a value. Assign its value to a temporary
753 and give it void_type_node. Returns the temporary, or NULL_TREE if
754 WRAPPER was already void. */
757 voidify_wrapper_expr (tree wrapper)
759 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
764 /* Set p to point to the body of the wrapper. */
765 switch (TREE_CODE (wrapper))
768 /* For a BIND_EXPR, the body is operand 1. */
769 p = &BIND_EXPR_BODY (wrapper);
773 p = &TREE_OPERAND (wrapper, 0);
777 /* Advance to the last statement. Set all container types to void. */
778 if (TREE_CODE (*p) == STATEMENT_LIST)
780 tree_stmt_iterator i = tsi_last (*p);
781 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
785 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
787 TREE_SIDE_EFFECTS (*p) = 1;
788 TREE_TYPE (*p) = void_type_node;
792 if (p && TREE_CODE (*p) == INIT_EXPR)
794 /* The C++ frontend already did this for us. */;
795 temp = TREE_OPERAND (*p, 0);
797 else if (p && TREE_CODE (*p) == INDIRECT_REF)
799 /* If we're returning a dereference, move the dereference outside
801 tree ptr = TREE_OPERAND (*p, 0);
802 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
803 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
804 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
805 /* If this is a BIND_EXPR for a const inline function, it might not
806 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
807 TREE_SIDE_EFFECTS (wrapper) = 1;
811 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
812 if (p && !IS_EMPTY_STMT (*p))
814 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
815 TREE_SIDE_EFFECTS (wrapper) = 1;
819 TREE_TYPE (wrapper) = void_type_node;
826 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
827 a temporary through which they communicate. */
830 build_stack_save_restore (tree *save, tree *restore)
832 tree save_call, tmp_var;
835 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
837 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
839 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
841 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
842 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
845 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
847 static enum gimplify_status
848 gimplify_bind_expr (tree *expr_p, tree *pre_p)
850 tree bind_expr = *expr_p;
851 tree temp = voidify_wrapper_expr (bind_expr);
852 bool old_save_stack = gimplify_ctxp->save_stack;
855 /* Mark variables seen in this bind expr. */
856 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
857 t->decl.seen_in_bind_expr = 1;
859 gimple_push_bind_expr (bind_expr);
860 gimplify_ctxp->save_stack = false;
862 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
864 if (gimplify_ctxp->save_stack)
866 tree stack_save, stack_restore;
868 /* Save stack on entry and restore it on exit. Add a try_finally
869 block to achieve this. Note that mudflap depends on the
870 format of the emitted code: see mx_register_decls(). */
871 build_stack_save_restore (&stack_save, &stack_restore);
873 t = build (TRY_FINALLY_EXPR, void_type_node,
874 BIND_EXPR_BODY (bind_expr), NULL_TREE);
875 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
877 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
878 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
879 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
882 gimplify_ctxp->save_stack = old_save_stack;
883 gimple_pop_bind_expr ();
888 append_to_statement_list (bind_expr, pre_p);
895 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
896 GIMPLE value, it is assigned to a new temporary and the statement is
897 re-written to return the temporary.
899 PRE_P points to the list where side effects that must happen before
900 STMT should be stored. */
902 static enum gimplify_status
903 gimplify_return_expr (tree stmt, tree *pre_p)
905 tree ret_expr = TREE_OPERAND (stmt, 0);
906 tree result_decl, result;
908 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
911 if (ret_expr == error_mark_node)
914 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
915 result_decl = NULL_TREE;
918 result_decl = TREE_OPERAND (ret_expr, 0);
919 #ifdef ENABLE_CHECKING
920 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
921 && TREE_CODE (ret_expr) != INIT_EXPR)
922 || TREE_CODE (result_decl) != RESULT_DECL)
927 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
928 Recall that aggregate_value_p is FALSE for any aggregate type that is
929 returned in registers. If we're returning values in registers, then
930 we don't want to extend the lifetime of the RESULT_DECL, particularly
931 across another call. In addition, for those aggregates for which
932 hard_function_value generates a PARALLEL, we'll abort during normal
933 expansion of structure assignments; there's special code in expand_return
934 to handle this case that does not exist in expand_expr. */
936 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
937 result = result_decl;
938 else if (gimplify_ctxp->return_temp)
939 result = gimplify_ctxp->return_temp;
942 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
943 gimplify_ctxp->return_temp = result;
946 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
947 Then gimplify the whole thing. */
948 if (result != result_decl)
949 TREE_OPERAND (ret_expr, 0) = result;
950 gimplify_stmt (&TREE_OPERAND (stmt, 0));
951 append_to_statement_list (TREE_OPERAND (stmt, 0), pre_p);
953 /* If we didn't use a temporary, then the result is just the result_decl.
954 Otherwise we need a simple copy. This should already be gimple. */
955 if (result == result_decl)
958 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
959 TREE_OPERAND (stmt, 0) = ret_expr;
964 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
965 and replacing the LOOP_EXPR with goto, but if the loop contains an
966 EXIT_EXPR, we need to append a label for it to jump to. */
968 static enum gimplify_status
969 gimplify_loop_expr (tree *expr_p, tree *pre_p)
971 tree saved_label = gimplify_ctxp->exit_label;
972 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
973 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
975 append_to_statement_list (start_label, pre_p);
977 gimplify_ctxp->exit_label = NULL_TREE;
979 gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
980 append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
982 if (gimplify_ctxp->exit_label)
984 append_to_statement_list (jump_stmt, pre_p);
985 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
990 gimplify_ctxp->exit_label = saved_label;
995 /* Compare two case labels. Because the front end should already have
996 made sure that case ranges do not overlap, it is enough to only compare
997 the CASE_LOW values of each case label. */
1000 compare_case_labels (const void *p1, const void *p2)
1002 tree case1 = *(tree *)p1;
1003 tree case2 = *(tree *)p2;
1005 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1008 /* Sort the case labels in LABEL_VEC in ascending order. */
1011 sort_case_labels (tree label_vec)
1013 size_t len = TREE_VEC_LENGTH (label_vec);
1014 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1016 if (CASE_LOW (default_case))
1020 /* The last label in the vector should be the default case
1022 for (i = 0; i < len; ++i)
1024 tree t = TREE_VEC_ELT (label_vec, i);
1028 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1029 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1035 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1036 compare_case_labels);
1039 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1042 static enum gimplify_status
1043 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1045 tree switch_expr = *expr_p;
1046 enum gimplify_status ret;
1048 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1049 is_gimple_val, fb_rvalue);
1051 if (SWITCH_BODY (switch_expr))
1053 varray_type labels, saved_labels;
1054 tree label_vec, default_case = NULL_TREE;
1057 /* If someone can be bothered to fill in the labels, they can
1058 be bothered to null out the body too. */
1059 if (SWITCH_LABELS (switch_expr))
1062 saved_labels = gimplify_ctxp->case_labels;
1063 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1065 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1067 labels = gimplify_ctxp->case_labels;
1068 gimplify_ctxp->case_labels = saved_labels;
1070 len = VARRAY_ACTIVE_SIZE (labels);
1072 for (i = 0; i < len; ++i)
1074 tree t = VARRAY_TREE (labels, i);
1077 /* The default case must be the last label in the list. */
1079 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1085 label_vec = make_tree_vec (len + 1);
1086 SWITCH_LABELS (*expr_p) = label_vec;
1087 append_to_statement_list (switch_expr, pre_p);
1091 /* If the switch has no default label, add one, so that we jump
1092 around the switch body. */
1093 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1094 NULL_TREE, create_artificial_label ());
1095 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1096 *expr_p = build (LABEL_EXPR, void_type_node,
1097 CASE_LABEL (default_case));
1100 *expr_p = SWITCH_BODY (switch_expr);
1102 for (i = 0; i < len; ++i)
1103 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1104 TREE_VEC_ELT (label_vec, len) = default_case;
1106 sort_case_labels (label_vec);
1108 SWITCH_BODY (switch_expr) = NULL;
1110 else if (!SWITCH_LABELS (switch_expr))
1116 static enum gimplify_status
1117 gimplify_case_label_expr (tree *expr_p)
1119 tree expr = *expr_p;
1120 if (gimplify_ctxp->case_labels)
1121 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1124 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1128 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1129 a (possibly empty) body. */
1131 static enum gimplify_status
1132 gimplify_labeled_block_expr (tree *expr_p)
1134 tree body = LABELED_BLOCK_BODY (*expr_p);
1135 tree label = LABELED_BLOCK_LABEL (*expr_p);
1138 DECL_CONTEXT (label) = current_function_decl;
1139 t = build (LABEL_EXPR, void_type_node, label);
1140 if (body != NULL_TREE)
1141 t = build (COMPOUND_EXPR, void_type_node, body, t);
1147 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1149 static enum gimplify_status
1150 gimplify_exit_block_expr (tree *expr_p)
1152 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1155 /* First operand must be a LABELED_BLOCK_EXPR, which should
1156 already be lowered (or partially lowered) when we get here. */
1157 #if defined ENABLE_CHECKING
1158 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1162 label = LABELED_BLOCK_LABEL (labeled_block);
1163 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1168 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1172 build_and_jump (tree *label_p)
1174 if (label_p == NULL)
1175 /* If there's nowhere to jump, just fall through. */
1176 return build_empty_stmt ();
1178 if (*label_p == NULL_TREE)
1180 tree label = create_artificial_label ();
1184 return build1 (GOTO_EXPR, void_type_node, *label_p);
1187 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1188 This also involves building a label to jump to and communicating it to
1189 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1191 static enum gimplify_status
1192 gimplify_exit_expr (tree *expr_p)
1194 tree cond = TREE_OPERAND (*expr_p, 0);
1197 expr = build_and_jump (&gimplify_ctxp->exit_label);
1198 expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1204 /* A helper function to be called via walk_tree. Mark all labels under *TP
1205 as being forced. To be called for DECL_INITIAL of static variables. */
1208 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1212 if (TREE_CODE (*tp) == LABEL_DECL)
1213 FORCED_LABEL (*tp) = 1;
1218 /* Break out elements of a constructor used as an initializer into separate
1221 Note that we still need to clear any elements that don't have explicit
1222 initializers, so if not all elements are initialized we keep the
1223 original MODIFY_EXPR, we just remove all of the constructor elements. */
1225 static enum gimplify_status
1226 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1227 tree *post_p, int want_value)
1229 tree object = TREE_OPERAND (*expr_p, 0);
1230 tree ctor = TREE_OPERAND (*expr_p, 1);
1231 tree type = TREE_TYPE (ctor);
1232 enum gimplify_status ret;
1235 if (TREE_CODE (ctor) != CONSTRUCTOR)
1236 return GS_UNHANDLED;
1238 elt_list = CONSTRUCTOR_ELTS (ctor);
1241 switch (TREE_CODE (type))
1245 case QUAL_UNION_TYPE:
1248 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1249 HOST_WIDE_INT num_nonconstant_elements;
1252 /* Aggregate types must lower constructors to initialization of
1253 individual elements. The exception is that a CONSTRUCTOR node
1254 with no elements indicates zero-initialization of the whole. */
1255 if (elt_list == NULL)
1266 categorize_ctor_elements (ctor, &num_nonzero_elements,
1267 &num_nonconstant_elements);
1268 num_elements = count_type_elements (TREE_TYPE (ctor));
1270 /* If a const aggregate variable is being initialized, then it
1271 should never be a lose to promote the variable to be static. */
1272 if (num_nonconstant_elements == 0
1273 && TREE_READONLY (object)
1274 && TREE_CODE (object) == VAR_DECL)
1276 DECL_INITIAL (object) = ctor;
1277 TREE_STATIC (object) = 1;
1278 if (!DECL_NAME (object))
1279 DECL_NAME (object) = create_tmp_var_name ("C");
1280 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1282 /* ??? C++ doesn't automatically append a .<number> to the
1283 assembler name, and even when it does, it looks a FE private
1284 data structures to figure out what that number should be,
1285 which are not set for this variable. I suppose this is
1286 important for local statics for inline functions, which aren't
1287 "local" in the object file sense. So in order to get a unique
1288 TU-local symbol, we must invoke the lhd version now. */
1289 lhd_set_decl_assembler_name (object);
1291 *expr_p = build_empty_stmt ();
1295 /* If there are "lots" of initialized elements, and all of them
1296 are valid address constants, then the entire initializer can
1297 be dropped to memory, and then memcpy'd out. */
1298 if (num_nonconstant_elements == 0)
1300 HOST_WIDE_INT size = int_size_in_bytes (type);
1303 /* ??? We can still get unbounded array types, at least
1304 from the C++ front end. This seems wrong, but attempt
1305 to work around it for now. */
1308 size = int_size_in_bytes (TREE_TYPE (object));
1310 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1313 /* Find the maximum alignment we can assume for the object. */
1314 /* ??? Make use of DECL_OFFSET_ALIGN. */
1315 if (DECL_P (object))
1316 align = DECL_ALIGN (object);
1318 align = TYPE_ALIGN (type);
1320 if (size > 0 && !can_move_by_pieces (size, align))
1322 tree new = create_tmp_var_raw (type, "C");
1323 gimple_add_tmp_var (new);
1324 TREE_STATIC (new) = 1;
1325 TREE_READONLY (new) = 1;
1326 DECL_INITIAL (new) = ctor;
1327 if (align > DECL_ALIGN (new))
1329 DECL_ALIGN (new) = align;
1330 DECL_USER_ALIGN (new) = 1;
1332 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1334 TREE_OPERAND (*expr_p, 1) = new;
1339 /* If there are "lots" of initialized elements, even discounting
1340 those that are not address constants (and thus *must* be
1341 computed at runtime), then partition the constructor into
1342 constant and non-constant parts. Block copy the constant
1343 parts in, then generate code for the non-constant parts. */
1344 /* TODO. There's code in cp/typeck.c to do this. */
1346 /* If there are "lots" of zeros, then block clear the object first. */
1348 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1349 && num_nonzero_elements < num_elements/4)
1352 /* ??? This bit ought not be needed. For any element not present
1353 in the initializer, we should simply set them to zero. Except
1354 we'd need to *find* the elements that are not present, and that
1355 requires trickery to avoid quadratic compile-time behavior in
1356 large cases or excessive memory use in small cases. */
1359 HOST_WIDE_INT len = list_length (elt_list);
1360 if (TREE_CODE (type) == ARRAY_TYPE)
1362 tree nelts = array_type_nelts (type);
1363 if (!host_integerp (nelts, 1)
1364 || tree_low_cst (nelts, 1) != len)
1367 else if (len != fields_length (type))
1373 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1374 append_to_statement_list (*expr_p, pre_p);
1377 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1379 tree purpose, value, cref, init;
1381 purpose = TREE_PURPOSE (elt_list);
1382 value = TREE_VALUE (elt_list);
1384 if (cleared && initializer_zerop (value))
1387 if (TREE_CODE (type) == ARRAY_TYPE)
1389 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1391 /* ??? Here's to hoping the front end fills in all of the
1392 indicies, so we don't have to figure out what's missing
1396 /* ??? Need to handle this. */
1397 if (TREE_CODE (purpose) == RANGE_EXPR)
1400 cref = build (ARRAY_REF, t, object, purpose);
1404 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1408 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1409 /* Each member initialization is a full-expression. */
1410 gimplify_stmt (&init);
1411 append_to_statement_list (init, pre_p);
1414 *expr_p = build_empty_stmt ();
1422 /* Extract the real and imaginary parts out of the ctor. */
1426 r = TREE_VALUE (elt_list);
1427 elt_list = TREE_CHAIN (elt_list);
1430 i = TREE_VALUE (elt_list);
1431 if (TREE_CHAIN (elt_list))
1435 if (r == NULL || i == NULL)
1437 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1444 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1445 represent creation of a complex value. */
1446 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1448 ctor = build_complex (type, r, i);
1449 TREE_OPERAND (*expr_p, 1) = ctor;
1453 ctor = build (COMPLEX_EXPR, type, r, i);
1454 TREE_OPERAND (*expr_p, 1) = ctor;
1455 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1456 is_gimple_rhs, fb_rvalue);
1462 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1463 if (TREE_CONSTANT (ctor))
1464 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1467 /* Vector types use CONSTRUCTOR all the way through gimple
1468 compilation as a general initializer. */
1469 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1471 enum gimplify_status tret;
1472 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1473 is_gimple_constructor_elt, fb_rvalue);
1474 if (tret == GS_ERROR)
1481 /* So how did we get a CONSTRUCTOR for a scalar type? */
1485 if (ret == GS_ERROR)
1487 else if (want_value)
1489 append_to_statement_list (*expr_p, pre_p);
1497 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1498 different from its canonical type, wrap the whole thing inside a
1499 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1502 The canonical type of a COMPONENT_REF is the type of the field being
1503 referenced--unless the field is a bit-field which can be read directly
1504 in a smaller mode, in which case the canonical type is the
1505 sign-appropriate type corresponding to that mode. */
1508 canonicalize_component_ref (tree *expr_p)
1510 tree expr = *expr_p;
1513 if (TREE_CODE (expr) != COMPONENT_REF)
1516 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1517 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1519 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1521 if (TREE_TYPE (expr) != type)
1523 tree old_type = TREE_TYPE (expr);
1525 /* Set the type of the COMPONENT_REF to the underlying type. */
1526 TREE_TYPE (expr) = type;
1528 /* And wrap the whole thing inside a NOP_EXPR. */
1529 expr = build1 (NOP_EXPR, old_type, expr);
1535 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1536 to foo, embed that change in the ADDR_EXPR. Lest we perturb the type
1537 system too badly, we must take extra steps to ensure that the ADDR_EXPR
1538 and the addressed object continue to agree on types. */
1539 /* ??? We might could do better if we recognize
1547 canonicalize_addr_expr (tree* expr_p)
1549 tree expr = *expr_p;
1550 tree ctype = TREE_TYPE (expr);
1551 tree addr_expr = TREE_OPERAND (expr, 0);
1552 tree atype = TREE_TYPE (addr_expr);
1553 tree dctype, datype, ddatype, otype, obj_expr;
1555 /* Both cast and addr_expr types should be pointers. */
1556 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1559 /* The addr_expr type should be a pointer to an array. */
1560 datype = TREE_TYPE (atype);
1561 if (TREE_CODE (datype) != ARRAY_TYPE)
1564 /* Both cast and addr_expr types should address the same object type. */
1565 dctype = TREE_TYPE (ctype);
1566 ddatype = TREE_TYPE (datype);
1567 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1570 /* The addr_expr and the object type should match. */
1571 obj_expr = TREE_OPERAND (addr_expr, 0);
1572 otype = TREE_TYPE (obj_expr);
1573 if (!lang_hooks.types_compatible_p (otype, datype))
1576 /* All checks succeeded. Build a new node to merge the cast. */
1577 *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1580 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1581 underneath as appropriate. */
1583 static enum gimplify_status
1584 gimplify_conversion (tree *expr_p)
1586 /* Strip away as many useless type conversions as possible
1588 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1590 /* If we still have a conversion at the toplevel, then strip
1591 away all but the outermost conversion. */
1592 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1594 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1596 /* And remove the outermost conversion if it's useless. */
1597 if (tree_ssa_useless_type_conversion (*expr_p))
1598 *expr_p = TREE_OPERAND (*expr_p, 0);
1601 /* If we still have a conversion at the toplevel,
1602 then canonicalize some constructs. */
1603 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1605 tree sub = TREE_OPERAND (*expr_p, 0);
1607 /* If a NOP conversion is changing the type of a COMPONENT_REF
1608 expression, then canonicalize its type now in order to expose more
1609 redundant conversions. */
1610 if (TREE_CODE (sub) == COMPONENT_REF)
1611 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1613 /* If a NOP conversion is changing a pointer to array of foo
1614 to a pointer to foo, embed that change in the ADDR_EXPR. */
1615 else if (TREE_CODE (sub) == ADDR_EXPR)
1616 canonicalize_addr_expr (expr_p);
1622 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1624 static enum gimplify_status
1625 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1627 tree op1 = TREE_OPERAND (*expr_p, 0);
1628 tree op2 = TREE_OPERAND (*expr_p, 1);
1629 enum tree_code code;
1630 enum gimplify_status r0, r1;
1632 if (TREE_CODE (*expr_p) == MIN_EXPR)
1637 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1638 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1640 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1641 build (code, boolean_type_node, op1, op2),
1644 if (r0 == GS_ERROR || r1 == GS_ERROR)
1650 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1651 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1653 static enum gimplify_status
1654 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1656 tree array = TREE_OPERAND (*expr_p, 0);
1657 tree arrtype = TREE_TYPE (array);
1658 tree elttype = TREE_TYPE (arrtype);
1659 tree size = size_in_bytes (elttype);
1660 tree ptrtype = build_pointer_type (elttype);
1661 enum tree_code add_code = PLUS_EXPR;
1662 tree idx = TREE_OPERAND (*expr_p, 1);
1663 tree minidx, offset, addr, result;
1664 enum gimplify_status ret;
1666 /* If the array domain does not start at zero, apply the offset. */
1667 minidx = TYPE_DOMAIN (arrtype);
1670 minidx = TYPE_MIN_VALUE (minidx);
1671 if (minidx && !integer_zerop (minidx))
1673 idx = convert (TREE_TYPE (minidx), idx);
1674 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1678 /* If the index is negative -- a technically invalid situation now
1679 that we've biased the index back to zero -- then casting it to
1680 unsigned has ill effects. In particular, -1*4U/4U != -1.
1681 Represent this as a subtraction of a positive rather than addition
1682 of a negative. This will prevent any conversion back to ARRAY_REF
1683 from getting the wrong results from the division. */
1684 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1686 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1687 add_code = MINUS_EXPR;
1690 /* Pointer arithmetic must be done in sizetype. */
1691 idx = convert (sizetype, idx);
1693 /* Convert the index to a byte offset. */
1694 offset = size_binop (MULT_EXPR, size, idx);
1696 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1697 if (ret == GS_ERROR)
1700 addr = build_fold_addr_expr_with_type (array, ptrtype);
1701 result = fold (build (add_code, ptrtype, addr, offset));
1702 *expr_p = build1 (INDIRECT_REF, elttype, result);
1707 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1708 node pointed by EXPR_P.
1711 : min_lval '[' val ']'
1713 | compound_lval '[' val ']'
1714 | compound_lval '.' ID
1716 This is not part of the original SIMPLE definition, which separates
1717 array and member references, but it seems reasonable to handle them
1718 together. Also, this way we don't run into problems with union
1719 aliasing; gcc requires that for accesses through a union to alias, the
1720 union reference must be explicit, which was not always the case when we
1721 were splitting up array and member refs.
1723 PRE_P points to the list where side effects that must happen before
1724 *EXPR_P should be stored.
1726 POST_P points to the list where side effects that must happen after
1727 *EXPR_P should be stored. */
1729 static enum gimplify_status
1730 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1731 tree *post_p, int want_lvalue)
1734 enum tree_code code;
1736 enum gimplify_status ret;
1738 #if defined ENABLE_CHECKING
1739 if (TREE_CODE (*expr_p) != ARRAY_REF
1740 && TREE_CODE (*expr_p) != COMPONENT_REF
1741 && TREE_CODE (*expr_p) != REALPART_EXPR
1742 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1746 code = ERROR_MARK; /* [GIMPLE] Avoid uninitialized use warning. */
1748 /* Create a stack of the subexpressions so later we can walk them in
1749 order from inner to outer. */
1750 VARRAY_TREE_INIT (stack, 10, "stack");
1753 TREE_CODE (*p) == ARRAY_REF
1754 || TREE_CODE (*p) == COMPONENT_REF
1755 || TREE_CODE (*p) == REALPART_EXPR
1756 || TREE_CODE (*p) == IMAGPART_EXPR;
1757 p = &TREE_OPERAND (*p, 0))
1759 code = TREE_CODE (*p);
1760 if (code == ARRAY_REF)
1762 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1763 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1764 /* If the size of the array elements is not constant,
1765 computing the offset is non-trivial, so expose it. */
1768 VARRAY_PUSH_TREE (stack, *p);
1771 /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1772 TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1773 to all the refs we've walked through.
1775 Gimplify the base, and then process each of the outer nodes from left
1777 ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1778 code != ARRAY_REF ? fb_either : fb_lvalue);
1780 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1782 tree t = VARRAY_TOP_TREE (stack);
1783 if (TREE_CODE (t) == ARRAY_REF)
1785 /* Gimplify the dimension. */
1786 enum gimplify_status tret;
1787 /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1788 Gimplify non-constant array indices into a temporary
1790 FIXME - The real fix is to gimplify post-modify
1791 expressions into a minimal gimple lvalue. However, that
1792 exposes bugs in alias analysis. The alias analyzer does
1793 not handle &PTR->FIELD very well. Will fix after the
1794 branch is merged into mainline (dnovillo 2004-05-03). */
1795 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1797 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1798 is_gimple_tmp_var, fb_rvalue);
1799 if (tret == GS_ERROR)
1803 recalculate_side_effects (t);
1807 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1808 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1810 canonicalize_component_ref (expr_p);
1811 ret = MIN (ret, GS_OK);
1817 /* Re-write the ARRAY_REF node pointed by EXPR_P.
1819 PRE_P points to the list where side effects that must happen before
1820 *EXPR_P should be stored.
1822 POST_P points to the list where side effects that must happen after
1823 *EXPR_P should be stored.
1825 FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1826 argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1827 ARRAY_REF should be extended. */
1829 static enum gimplify_status
1830 gimplify_array_ref (tree *expr_p, tree *pre_p,
1831 tree *post_p, int want_lvalue)
1833 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1834 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1835 /* If the size of the array elements is not constant,
1836 computing the offset is non-trivial, so expose it. */
1837 return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1839 /* Handle array and member refs together for now. When alias analysis
1840 improves, we may want to go back to handling them separately. */
1841 return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1844 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1846 PRE_P points to the list where side effects that must happen before
1847 *EXPR_P should be stored.
1849 POST_P points to the list where side effects that must happen after
1850 *EXPR_P should be stored.
1852 WANT_VALUE is nonzero iff we want to use the value of this expression
1853 in another expression. */
1855 static enum gimplify_status
1856 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1859 enum tree_code code;
1860 tree lhs, lvalue, rhs, t1;
1862 enum tree_code arith_code;
1863 enum gimplify_status ret;
1865 code = TREE_CODE (*expr_p);
1867 #if defined ENABLE_CHECKING
1868 if (code != POSTINCREMENT_EXPR
1869 && code != POSTDECREMENT_EXPR
1870 && code != PREINCREMENT_EXPR
1871 && code != PREDECREMENT_EXPR)
1875 /* Prefix or postfix? */
1876 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1877 /* Faster to treat as prefix if result is not used. */
1878 postfix = want_value;
1882 /* Add or subtract? */
1883 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1884 arith_code = PLUS_EXPR;
1886 arith_code = MINUS_EXPR;
1888 /* Gimplify the LHS into a GIMPLE lvalue. */
1889 lvalue = TREE_OPERAND (*expr_p, 0);
1890 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1891 if (ret == GS_ERROR)
1894 /* Extract the operands to the arithmetic operation. */
1896 rhs = TREE_OPERAND (*expr_p, 1);
1898 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1899 that as the result value and in the postqueue operation. */
1902 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1903 if (ret == GS_ERROR)
1907 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1908 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1912 gimplify_stmt (&t1);
1913 append_to_statement_list (t1, post_p);
1924 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
1927 : ID '(' arglist ')'
1933 PRE_P points to the list where side effects that must happen before
1934 *EXPR_P should be stored. */
1936 static enum gimplify_status
1937 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1941 enum gimplify_status ret;
1943 #if defined ENABLE_CHECKING
1944 if (TREE_CODE (*expr_p) != CALL_EXPR)
1948 /* For reliable diagnostics during inlining, it is necessary that
1949 every call_expr be annotated with file and line. */
1950 if (!EXPR_LOCUS (*expr_p))
1951 annotate_with_locus (*expr_p, input_location);
1953 /* This may be a call to a builtin function.
1955 Builtin function calls may be transformed into different
1956 (and more efficient) builtin function calls under certain
1957 circumstances. Unfortunately, gimplification can muck things
1958 up enough that the builtin expanders are not aware that certain
1959 transformations are still valid.
1961 So we attempt transformation/gimplification of the call before
1962 we gimplify the CALL_EXPR. At this time we do not manage to
1963 transform all calls in the same manner as the expanders do, but
1964 we do transform most of them. */
1965 decl = get_callee_fndecl (*expr_p);
1966 if (decl && DECL_BUILT_IN (decl))
1970 /* If it is allocation of stack, record the need to restore the memory
1971 when the enclosing bind_expr is exited. */
1972 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1973 gimplify_ctxp->save_stack = true;
1975 /* If it is restore of the stack, reset it, since it means we are
1976 regimplifying the bind_expr. Note that we use the fact that
1977 for try_finally_expr, try part is processed first. */
1978 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1979 gimplify_ctxp->save_stack = false;
1981 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
1983 if (new && new != *expr_p)
1985 /* There was a transformation of this call which computes the
1986 same value, but in a more efficient way. Return and try
1993 /* There is a sequence point before the call, so any side effects in
1994 the calling expression must occur before the actual call. Force
1995 gimplify_expr to use an internal post queue. */
1996 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1997 is_gimple_val, fb_rvalue);
1999 if (PUSH_ARGS_REVERSED)
2000 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2001 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2002 arglist = TREE_CHAIN (arglist))
2004 enum gimplify_status t;
2006 /* There is a sequence point before a function call. Side effects in
2007 the argument list must occur before the actual call. So, when
2008 gimplifying arguments, force gimplify_expr to use an internal
2009 post queue which is then appended to the end of PRE_P. */
2010 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2016 if (PUSH_ARGS_REVERSED)
2017 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2019 /* Try this again in case gimplification exposed something. */
2020 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2022 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2024 if (new && new != *expr_p)
2026 /* There was a transformation of this call which computes the
2027 same value, but in a more efficient way. Return and try
2034 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2035 decl. This allows us to eliminate redundant or useless
2036 calls to "const" functions. */
2037 if (TREE_CODE (*expr_p) == CALL_EXPR
2038 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2039 TREE_SIDE_EFFECTS (*expr_p) = 0;
2044 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2045 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2047 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2048 condition is true or false, respectively. If null, we should generate
2049 our own to skip over the evaluation of this specific expression.
2051 This function is the tree equivalent of do_jump.
2053 shortcut_cond_r should only be called by shortcut_cond_expr. */
2056 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2058 tree local_label = NULL_TREE;
2059 tree t, expr = NULL;
2061 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2062 retain the shortcut semantics. Just insert the gotos here;
2063 shortcut_cond_expr will append the real blocks later. */
2064 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2066 /* Turn if (a && b) into
2068 if (a); else goto no;
2069 if (b) goto yes; else goto no;
2072 if (false_label_p == NULL)
2073 false_label_p = &local_label;
2075 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2076 append_to_statement_list (t, &expr);
2078 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2080 append_to_statement_list (t, &expr);
2082 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2084 /* Turn if (a || b) into
2087 if (b) goto yes; else goto no;
2090 if (true_label_p == NULL)
2091 true_label_p = &local_label;
2093 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2094 append_to_statement_list (t, &expr);
2096 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2098 append_to_statement_list (t, &expr);
2100 else if (TREE_CODE (pred) == COND_EXPR)
2102 /* As long as we're messing with gotos, turn if (a ? b : c) into
2104 if (b) goto yes; else goto no;
2106 if (c) goto yes; else goto no; */
2107 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2108 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2110 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2115 expr = build (COND_EXPR, void_type_node, pred,
2116 build_and_jump (true_label_p),
2117 build_and_jump (false_label_p));
2122 t = build1 (LABEL_EXPR, void_type_node, local_label);
2123 append_to_statement_list (t, &expr);
2130 shortcut_cond_expr (tree expr)
2132 tree pred = TREE_OPERAND (expr, 0);
2133 tree then_ = TREE_OPERAND (expr, 1);
2134 tree else_ = TREE_OPERAND (expr, 2);
2135 tree true_label, false_label, end_label, t;
2137 tree *false_label_p;
2138 bool emit_end, emit_false;
2140 /* First do simple transformations. */
2141 if (!TREE_SIDE_EFFECTS (else_))
2143 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2144 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2146 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2147 then_ = shortcut_cond_expr (expr);
2148 pred = TREE_OPERAND (pred, 0);
2149 expr = build (COND_EXPR, void_type_node, pred, then_,
2150 build_empty_stmt ());
2153 if (!TREE_SIDE_EFFECTS (then_))
2155 /* If there is no 'then', turn
2158 if (a); else if (b); else d. */
2159 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2161 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2162 else_ = shortcut_cond_expr (expr);
2163 pred = TREE_OPERAND (pred, 0);
2164 expr = build (COND_EXPR, void_type_node, pred,
2165 build_empty_stmt (), else_);
2169 /* If we're done, great. */
2170 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2171 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2174 /* Otherwise we need to mess with gotos. Change
2177 if (a); else goto no;
2180 and recursively gimplify the condition. */
2182 true_label = false_label = end_label = NULL_TREE;
2184 /* If our arms just jump somewhere, hijack those labels so we don't
2185 generate jumps to jumps. */
2187 if (TREE_CODE (then_) == GOTO_EXPR
2188 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2190 true_label = GOTO_DESTINATION (then_);
2191 then_ = build_empty_stmt ();
2194 if (TREE_CODE (else_) == GOTO_EXPR
2195 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2197 false_label = GOTO_DESTINATION (else_);
2198 else_ = build_empty_stmt ();
2201 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2203 true_label_p = &true_label;
2205 true_label_p = NULL;
2207 /* The 'else' branch also needs a label if it contains interesting code. */
2208 if (false_label || TREE_SIDE_EFFECTS (else_))
2209 false_label_p = &false_label;
2211 false_label_p = NULL;
2213 /* If there was nothing else in our arms, just forward the label(s). */
2214 if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2215 return shortcut_cond_r (pred, true_label_p, false_label_p);
2217 /* If our last subexpression already has a terminal label, reuse it. */
2218 if (TREE_SIDE_EFFECTS (else_))
2219 expr = expr_last (else_);
2221 expr = expr_last (then_);
2222 if (TREE_CODE (expr) == LABEL_EXPR)
2223 end_label = LABEL_EXPR_LABEL (expr);
2225 /* If we don't care about jumping to the 'else' branch, jump to the end
2226 if the condition is false. */
2228 false_label_p = &end_label;
2230 /* We only want to emit these labels if we aren't hijacking them. */
2231 emit_end = (end_label == NULL_TREE);
2232 emit_false = (false_label == NULL_TREE);
2234 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2237 append_to_statement_list (pred, &expr);
2239 append_to_statement_list (then_, &expr);
2240 if (TREE_SIDE_EFFECTS (else_))
2242 t = build_and_jump (&end_label);
2243 append_to_statement_list (t, &expr);
2246 t = build1 (LABEL_EXPR, void_type_node, false_label);
2247 append_to_statement_list (t, &expr);
2249 append_to_statement_list (else_, &expr);
2251 if (emit_end && end_label)
2253 t = build1 (LABEL_EXPR, void_type_node, end_label);
2254 append_to_statement_list (t, &expr);
2260 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2263 gimple_boolify (tree expr)
2265 tree type = TREE_TYPE (expr);
2267 if (TREE_CODE (type) == BOOLEAN_TYPE)
2270 /* If this is the predicate of a COND_EXPR, it might not even be a
2272 expr = lang_hooks.truthvalue_conversion (expr);
2274 switch (TREE_CODE (expr))
2276 case TRUTH_AND_EXPR:
2278 case TRUTH_XOR_EXPR:
2279 case TRUTH_ANDIF_EXPR:
2280 case TRUTH_ORIF_EXPR:
2281 /* Also boolify the arguments of truth exprs. */
2282 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2285 case TRUTH_NOT_EXPR:
2286 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2289 case EQ_EXPR: case NE_EXPR:
2290 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2291 /* These expressions always produce boolean results. */
2292 TREE_TYPE (expr) = boolean_type_node;
2296 /* Other expressions that get here must have boolean values, but
2297 might need to be converted to the appropriate mode. */
2298 return convert (boolean_type_node, expr);
2302 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2311 The second form is used when *EXPR_P is of type void.
2313 PRE_P points to the list where side effects that must happen before
2314 *EXPR_P should be stored. */
2316 static enum gimplify_status
2317 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2319 tree expr = *expr_p;
2321 enum gimplify_status ret;
2323 /* If this COND_EXPR has a value, copy the values into a temporary within
2325 if (! VOID_TYPE_P (TREE_TYPE (expr)))
2334 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2338 /* Build the then clause, 't1 = a;'. But don't build an assignment
2339 if this branch is void; in C++ it can be, if it's a throw. */
2340 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2341 TREE_OPERAND (expr, 1)
2342 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2344 /* Build the else clause, 't1 = b;'. */
2345 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2346 TREE_OPERAND (expr, 2)
2347 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2349 TREE_TYPE (expr) = void_type_node;
2350 recalculate_side_effects (expr);
2352 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2353 gimplify_stmt (&expr);
2354 append_to_statement_list (expr, pre_p);
2360 /* Make sure the condition has BOOLEAN_TYPE. */
2361 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2363 /* Break apart && and || conditions. */
2364 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2365 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2367 expr = shortcut_cond_expr (expr);
2369 if (expr != *expr_p)
2373 /* We can't rely on gimplify_expr to re-gimplify the expanded
2374 form properly, as cleanups might cause the target labels to be
2375 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2376 set up a conditional context. */
2377 gimple_push_condition ();
2378 gimplify_stmt (expr_p);
2379 gimple_pop_condition (pre_p);
2385 /* Now do the normal gimplification. */
2386 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2387 is_gimple_condexpr, fb_rvalue);
2389 gimple_push_condition ();
2391 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2392 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2393 recalculate_side_effects (expr);
2395 gimple_pop_condition (pre_p);
2397 if (ret == GS_ERROR)
2399 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2401 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2402 /* Rewrite "if (a); else b" to "if (!a) b" */
2404 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2405 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2406 is_gimple_condexpr, fb_rvalue);
2408 tmp = TREE_OPERAND (expr, 1);
2409 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2410 TREE_OPERAND (expr, 2) = tmp;
2413 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2414 expr = TREE_OPERAND (expr, 0);
2420 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2426 PRE_P points to the list where side effects that must happen before
2427 *EXPR_P should be stored.
2429 POST_P points to the list where side effects that must happen after
2430 *EXPR_P should be stored.
2432 WANT_VALUE is nonzero iff we want to use the value of this expression
2433 in another expression. */
2435 static enum gimplify_status
2436 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2438 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2439 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2440 enum gimplify_status ret;
2442 #if defined ENABLE_CHECKING
2443 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2447 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2448 if (TREE_CODE (*expr_p) == INIT_EXPR)
2449 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2451 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2452 if (ret == GS_ERROR)
2455 /* If we are initializing something from a TARGET_EXPR, strip the
2456 TARGET_EXPR and initialize it directly. */
2457 /* What about code that pulls out the temp and uses it elsewhere? I
2458 think that such code never uses the TARGET_EXPR as an initializer. If
2459 I'm wrong, we'll abort because the temp won't have any RTL. In that
2460 case, I guess we'll need to replace references somehow. */
2461 if (TREE_CODE (*from_p) == TARGET_EXPR)
2462 *from_p = TARGET_EXPR_INITIAL (*from_p);
2464 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2465 the assignment down into the branches, since we can't generate a
2466 temporary of such a type. */
2467 if (TREE_CODE (*from_p) == COND_EXPR
2468 && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2471 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2474 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2475 if (ret == GS_ERROR)
2478 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2479 if (ret != GS_UNHANDLED)
2482 /* If the destination is already simple, nothing else needed. */
2483 if (is_gimple_tmp_var (*to_p))
2487 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2488 the LHS is a user variable, then we need to introduce a temporary.
2489 ie temp = RHS; LHS = temp.
2491 This way the optimizers can determine that the user variable is
2492 only modified if evaluation of the RHS does not throw.
2494 FIXME this should be handled by the is_gimple_rhs predicate. */
2496 if (aggregate_value_p (TREE_TYPE (*from_p), NULL_TREE))
2497 /* Don't force a temp of a large aggregate type; the copy could be
2498 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
2500 else if (TREE_CODE (*from_p) == CALL_EXPR
2501 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2502 /* If we're dealing with a renamable type, either source or dest
2503 must be a renamed variable. */
2504 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2505 && !is_gimple_reg (*to_p)))
2506 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2508 /* If the value being copied is of variable width, expose the length
2509 if the copy by converting the whole thing to a memcpy. */
2510 /* ??? Except that we can't manage this with VA_ARG_EXPR. Yes, this
2511 does leave us with an edge condition that doesn't work. The only
2512 way out is to rearrange how VA_ARG_EXPR works. */
2513 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2514 && TREE_CODE (*from_p) != VA_ARG_EXPR)
2518 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2519 t = unshare_expr (t);
2520 args = tree_cons (NULL, t, NULL);
2521 t = build_fold_addr_expr (*from_p);
2522 args = tree_cons (NULL, t, args);
2523 dest = build_fold_addr_expr (*to_p);
2524 args = tree_cons (NULL, dest, args);
2525 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2526 t = build_function_call_expr (t, args);
2529 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2530 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2537 ret = want_value ? GS_OK : GS_ALL_DONE;
2542 append_to_statement_list (*expr_p, pre_p);
2549 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2550 points to the expression to gimplify.
2552 Expressions of the form 'a && b' are gimplified to:
2554 a && b ? true : false
2556 gimplify_cond_expr will do the rest.
2558 PRE_P points to the list where side effects that must happen before
2559 *EXPR_P should be stored. */
2561 static enum gimplify_status
2562 gimplify_boolean_expr (tree *expr_p)
2564 /* Preserve the original type of the expression. */
2565 tree type = TREE_TYPE (*expr_p);
2567 *expr_p = build (COND_EXPR, type, *expr_p,
2568 convert (type, boolean_true_node),
2569 convert (type, boolean_false_node));
2574 /* Gimplifies an expression sequence. This function gimplifies each
2575 expression and re-writes the original expression with the last
2576 expression of the sequence in GIMPLE form.
2578 PRE_P points to the list where the side effects for all the
2579 expressions in the sequence will be emitted.
2581 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2582 /* ??? Should rearrange to share the pre-queue with all the indirect
2583 invocations of gimplify_expr. Would probably save on creations
2584 of statement_list nodes. */
2586 static enum gimplify_status
2587 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2593 tree *sub_p = &TREE_OPERAND (t, 0);
2595 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2596 gimplify_compound_expr (sub_p, pre_p, false);
2598 gimplify_stmt (sub_p);
2599 append_to_statement_list (*sub_p, pre_p);
2601 t = TREE_OPERAND (t, 1);
2603 while (TREE_CODE (t) == COMPOUND_EXPR);
2610 gimplify_stmt (expr_p);
2615 /* Gimplifies a statement list. These may be created either by an
2616 enlightened front-end, or by shortcut_cond_expr. */
2618 static enum gimplify_status
2619 gimplify_statement_list (tree *expr_p)
2621 tree_stmt_iterator i = tsi_start (*expr_p);
2623 while (!tsi_end_p (i))
2627 gimplify_stmt (tsi_stmt_ptr (i));
2630 if (TREE_CODE (t) == STATEMENT_LIST)
2632 tsi_link_before (&i, t, TSI_SAME_STMT);
2642 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2643 gimplify. After gimplification, EXPR_P will point to a new temporary
2644 that holds the original value of the SAVE_EXPR node.
2646 PRE_P points to the list where side effects that must happen before
2647 *EXPR_P should be stored. */
2649 static enum gimplify_status
2650 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2652 enum gimplify_status ret = GS_ALL_DONE;
2655 #if defined ENABLE_CHECKING
2656 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2660 val = TREE_OPERAND (*expr_p, 0);
2662 /* If the operand is already a GIMPLE temporary, just re-write the
2664 if (is_gimple_tmp_var (val))
2666 /* The operand may be a void-valued expression such as SAVE_EXPRs
2667 generated by the Java frontend for class initialization. It is
2668 being executed only for its side-effects. */
2669 else if (TREE_TYPE (val) == void_type_node)
2671 tree body = TREE_OPERAND (*expr_p, 0);
2672 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2673 append_to_statement_list (body, pre_p);
2674 *expr_p = build_empty_stmt ();
2677 *expr_p = TREE_OPERAND (*expr_p, 0)
2678 = get_initialized_tmp_var (val, pre_p, post_p);
2683 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2690 PRE_P points to the list where side effects that must happen before
2691 *EXPR_P should be stored.
2693 POST_P points to the list where side effects that must happen after
2694 *EXPR_P should be stored. */
2696 static enum gimplify_status
2697 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2699 tree expr = *expr_p;
2700 tree op0 = TREE_OPERAND (expr, 0);
2701 enum gimplify_status ret;
2703 switch (TREE_CODE (op0))
2706 /* Check if we are dealing with an expression of the form '&*ptr'.
2707 While the front end folds away '&*ptr' into 'ptr', these
2708 expressions may be generated internally by the compiler (e.g.,
2709 builtins like __builtin_va_end). */
2710 *expr_p = TREE_OPERAND (op0, 0);
2715 /* Fold &a[6] to (&a + 6). */
2716 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2719 /* This added an INDIRECT_REF. Fold it away. */
2720 op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2726 /* We use fb_either here because the C frontend sometimes takes
2727 the address of a call that returns a struct. */
2728 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2729 is_gimple_addr_expr_arg, fb_either);
2730 if (ret != GS_ERROR)
2732 /* At this point, the argument of the ADDR_EXPR should be
2733 sufficiently simple that there are never side effects. */
2734 /* ??? Could split out the decision code from build1 to verify. */
2735 TREE_SIDE_EFFECTS (expr) = 0;
2737 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2738 recompute_tree_invarant_for_addr_expr (expr);
2740 /* Mark the RHS addressable. */
2741 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2746 /* If the operand is gimplified into a _DECL, mark the address expression
2747 as TREE_INVARIANT. */
2748 if (DECL_P (TREE_OPERAND (expr, 0)))
2749 TREE_INVARIANT (expr) = 1;
2754 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2755 value; output operands should be a gimple lvalue. */
2757 static enum gimplify_status
2758 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2760 tree expr = *expr_p;
2761 int noutputs = list_length (ASM_OUTPUTS (expr));
2762 const char **oconstraints
2763 = (const char **) alloca ((noutputs) * sizeof (const char *));
2766 const char *constraint;
2767 bool allows_mem, allows_reg, is_inout;
2768 enum gimplify_status ret, tret;
2771 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2775 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2777 oconstraints[i] = constraint
2778 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2780 parse_output_constraint (&constraint, i, 0, 0,
2781 &allows_mem, &allows_reg, &is_inout);
2783 if (!allows_reg && allows_mem)
2784 lang_hooks.mark_addressable (TREE_VALUE (link));
2786 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2787 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2788 fb_lvalue | fb_mayfail);
2789 if (tret == GS_ERROR)
2791 error ("invalid lvalue in asm output %d", i);
2797 /* An input/output operand. To give the optimizers more
2798 flexibility, split it into separate input and output
2802 size_t constraint_len = strlen (constraint);
2804 /* Turn the in/out constraint into an output constraint. */
2805 char *p = xstrdup (constraint);
2807 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2810 /* And add a matching input constraint. */
2813 sprintf (buf, "%d", i);
2814 input = build_string (strlen (buf), buf);
2817 input = build_string (constraint_len - 1, constraint + 1);
2818 input = build_tree_list (build_tree_list (NULL_TREE, input),
2819 unshare_expr (TREE_VALUE (link)));
2820 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2824 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2827 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2828 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2829 oconstraints, &allows_mem, &allows_reg);
2831 /* If the operand is a memory input, it should be an lvalue. */
2832 if (!allows_reg && allows_mem)
2834 lang_hooks.mark_addressable (TREE_VALUE (link));
2835 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2836 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2837 if (tret == GS_ERROR)
2839 error ("memory input %d is not directly addressable", i);
2845 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2846 is_gimple_val, fb_rvalue);
2847 if (tret == GS_ERROR)
2855 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
2856 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2857 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2858 return to this function.
2860 FIXME should we complexify the prequeue handling instead? Or use flags
2861 for all the cleanups and let the optimizer tighten them up? The current
2862 code seems pretty fragile; it will break on a cleanup within any
2863 non-conditional nesting. But any such nesting would be broken, anyway;
2864 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2865 and continues out of it. We can do that at the RTL level, though, so
2866 having an optimizer to tighten up try/finally regions would be a Good
2869 static enum gimplify_status
2870 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2872 tree_stmt_iterator iter;
2875 tree temp = voidify_wrapper_expr (*expr_p);
2877 /* We only care about the number of conditions between the innermost
2878 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
2879 int old_conds = gimplify_ctxp->conditions;
2880 gimplify_ctxp->conditions = 0;
2882 body = TREE_OPERAND (*expr_p, 0);
2883 gimplify_to_stmt_list (&body);
2885 gimplify_ctxp->conditions = old_conds;
2887 for (iter = tsi_start (body); !tsi_end_p (iter); )
2889 tree *wce_p = tsi_stmt_ptr (iter);
2892 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2894 if (tsi_one_before_end_p (iter))
2896 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2904 sl = tsi_split_statement_list_after (&iter);
2905 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2906 append_to_statement_list (TREE_OPERAND (wce, 1),
2907 &TREE_OPERAND (tfe, 1));
2909 iter = tsi_start (sl);
2919 append_to_statement_list (body, pre_p);
2929 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
2930 is the cleanup action required. */
2933 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2937 /* Errors can result in improperly nested cleanups. Which results in
2938 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
2939 if (errorcount || sorrycount)
2942 if (gimple_conditional_context ())
2944 /* If we're in a conditional context, this is more complex. We only
2945 want to run the cleanup if we actually ran the initialization that
2946 necessitates it, but we want to run it after the end of the
2947 conditional context. So we wrap the try/finally around the
2948 condition and use a flag to determine whether or not to actually
2949 run the destructor. Thus
2953 becomes (approximately)
2957 if (test) { A::A(temp); flag = 1; val = f(temp); }
2960 if (flag) A::~A(temp);
2965 tree flag = create_tmp_var (boolean_type_node, "cleanup");
2966 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2967 boolean_false_node);
2968 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2970 cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2971 build_empty_stmt ());
2972 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2973 cleanup, NULL_TREE);
2974 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2975 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2976 append_to_statement_list (ftrue, pre_p);
2978 /* Because of this manipulation, and the EH edges that jump
2979 threading cannot redirect, the temporary (VAR) will appear
2980 to be used uninitialized. Don't warn. */
2981 TREE_NO_WARNING (var) = 1;
2985 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2986 cleanup, NULL_TREE);
2987 append_to_statement_list (wce, pre_p);
2990 gimplify_stmt (&TREE_OPERAND (wce, 1));
2993 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
2995 static enum gimplify_status
2996 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
2998 tree targ = *expr_p;
2999 tree temp = TARGET_EXPR_SLOT (targ);
3000 tree init = TARGET_EXPR_INITIAL (targ);
3001 enum gimplify_status ret;
3005 /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
3007 gimple_add_tmp_var (temp);
3009 /* Build up the initialization and add it to pre_p. */
3010 init = build (MODIFY_EXPR, void_type_node, temp, init);
3011 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3012 if (ret == GS_ERROR)
3015 append_to_statement_list (init, pre_p);
3017 /* If needed, push the cleanup for the temp. */
3018 if (TARGET_EXPR_CLEANUP (targ))
3020 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3021 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3024 /* Only expand this once. */
3025 TREE_OPERAND (targ, 3) = init;
3026 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3028 else if (!temp->decl.seen_in_bind_expr)
3029 /* We should have expanded this before. */
3036 /* Gimplification of expression trees. */
3038 /* Gimplify an expression which appears at statement context; usually, this
3039 means replacing it with a suitably gimple STATEMENT_LIST. */
3042 gimplify_stmt (tree *stmt_p)
3044 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3046 *stmt_p = alloc_stmt_list ();
3049 /* Similarly, but force the result to be a STATEMENT_LIST. */
3052 gimplify_to_stmt_list (tree *stmt_p)
3054 gimplify_stmt (stmt_p);
3055 if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3058 *stmt_p = alloc_stmt_list ();
3059 append_to_statement_list (t, stmt_p);
3064 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3065 gimplification failed.
3067 PRE_P points to the list where side effects that must happen before
3068 EXPR should be stored.
3070 POST_P points to the list where side effects that must happen after
3071 EXPR should be stored, or NULL if there is no suitable list. In
3072 that case, we copy the result to a temporary, emit the
3073 post-effects, and then return the temporary.
3075 GIMPLE_TEST_F points to a function that takes a tree T and
3076 returns nonzero if T is in the GIMPLE form requested by the
3077 caller. The GIMPLE predicates are in tree-gimple.c.
3079 This test is used twice. Before gimplification, the test is
3080 invoked to determine whether *EXPR_P is already gimple enough. If
3081 that fails, *EXPR_P is gimplified according to its code and
3082 GIMPLE_TEST_F is called again. If the test still fails, then a new
3083 temporary variable is created and assigned the value of the
3084 gimplified expression.
3086 FALLBACK tells the function what sort of a temporary we want. If the 1
3087 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3088 If both are set, either is OK, but an lvalue is preferable.
3090 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3091 iterates until solution. */
3093 enum gimplify_status
3094 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3095 bool (* gimple_test_f) (tree), fallback_t fallback)
3098 tree internal_pre = NULL_TREE;
3099 tree internal_post = NULL_TREE;
3101 int is_statement = (pre_p == NULL);
3103 location_t saved_location;
3104 enum gimplify_status ret;
3106 save_expr = *expr_p;
3107 if (save_expr == NULL_TREE)
3110 /* We used to check the predicate here and return immediately if it
3111 succeeds. This is wrong; the design is for gimplification to be
3112 idempotent, and for the predicates to only test for valid forms, not
3113 whether they are fully simplified. */
3115 /* Set up our internal queues if needed. */
3117 pre_p = &internal_pre;
3119 post_p = &internal_post;
3121 saved_location = input_location;
3122 if (save_expr == error_mark_node)
3125 locus = EXPR_LOCUS (save_expr);
3127 input_location = *locus;
3129 /* Loop over the specific gimplifiers until the toplevel node
3130 remains the same. */
3133 /* Strip any uselessness. */
3134 STRIP_MAIN_TYPE_NOPS (*expr_p);
3136 /* Remember the expr. */
3137 save_expr = *expr_p;
3139 /* Die, die, die, my darling. */
3140 if (save_expr == error_mark_node
3141 || TREE_TYPE (save_expr) == error_mark_node)
3147 /* Do any language-specific gimplification. */
3148 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3151 if (*expr_p == NULL_TREE)
3153 if (*expr_p != save_expr)
3156 else if (ret != GS_UNHANDLED)
3160 switch (TREE_CODE (*expr_p))
3162 /* First deal with the special cases. */
3164 case POSTINCREMENT_EXPR:
3165 case POSTDECREMENT_EXPR:
3166 case PREINCREMENT_EXPR:
3167 case PREDECREMENT_EXPR:
3168 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3169 fallback != fb_none);
3173 ret = gimplify_array_ref (expr_p, pre_p, post_p,
3174 fallback & fb_lvalue);
3178 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3179 fallback & fb_lvalue);
3183 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3187 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3194 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3199 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3200 fallback & fb_lvalue);
3205 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3206 fallback != fb_none);
3209 case TRUTH_ANDIF_EXPR:
3210 case TRUTH_ORIF_EXPR:
3211 ret = gimplify_boolean_expr (expr_p);
3214 case TRUTH_NOT_EXPR:
3215 TREE_OPERAND (*expr_p, 0)
3216 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3217 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3218 is_gimple_val, fb_rvalue);
3219 recalculate_side_effects (*expr_p);
3223 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3227 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3232 if (IS_EMPTY_STMT (*expr_p))
3238 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3239 || fallback == fb_none)
3241 /* Just strip a conversion to void (or in void context) and
3243 *expr_p = TREE_OPERAND (*expr_p, 0);
3247 ret = gimplify_conversion (expr_p);
3248 if (ret == GS_ERROR)
3250 if (*expr_p != save_expr)
3254 case FIX_TRUNC_EXPR:
3256 case FIX_FLOOR_EXPR:
3257 case FIX_ROUND_EXPR:
3258 /* unary_expr: ... | '(' cast ')' val | ... */
3259 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3260 is_gimple_val, fb_rvalue);
3261 recalculate_side_effects (*expr_p);
3265 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3266 is_gimple_reg, fb_rvalue);
3267 recalculate_side_effects (*expr_p);
3270 /* Constants need not be gimplified. */
3280 *expr_p = DECL_INITIAL (*expr_p);
3284 /* FIXME make this a decl. */
3289 ret = gimplify_bind_expr (expr_p, pre_p);
3293 ret = gimplify_loop_expr (expr_p, pre_p);
3297 ret = gimplify_switch_expr (expr_p, pre_p);
3300 case LABELED_BLOCK_EXPR:
3301 ret = gimplify_labeled_block_expr (expr_p);
3304 case EXIT_BLOCK_EXPR:
3305 ret = gimplify_exit_block_expr (expr_p);
3309 ret = gimplify_exit_expr (expr_p);
3313 /* If the target is not LABEL, then it is a computed jump
3314 and the target needs to be gimplified. */
3315 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3316 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3317 NULL, is_gimple_val, fb_rvalue);
3322 #ifdef ENABLE_CHECKING
3323 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3328 case CASE_LABEL_EXPR:
3329 ret = gimplify_case_label_expr (expr_p);
3333 ret = gimplify_return_expr (*expr_p, pre_p);
3337 /* Don't reduce this in place; let gimplify_init_constructor work
3342 /* The following are special cases that are not handled by the
3343 original GIMPLE grammar. */
3345 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3348 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3353 enum gimplify_status r0, r1, r2;
3355 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3356 is_gimple_min_lval, fb_either);
3357 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3358 is_gimple_val, fb_rvalue);
3359 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3360 is_gimple_val, fb_rvalue);
3361 recalculate_side_effects (*expr_p);
3363 ret = MIN (r0, MIN (r1, r2));
3367 case NON_LVALUE_EXPR:
3368 /* This should have been stripped above. */
3373 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3376 case TRY_FINALLY_EXPR:
3377 case TRY_CATCH_EXPR:
3378 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3379 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3383 case CLEANUP_POINT_EXPR:
3384 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3388 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3392 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3396 case EH_FILTER_EXPR:
3397 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3402 /* This moves much of the actual computation out of the
3403 VTABLE_REF. Perhaps this should be revisited once we want to
3404 do clever things with VTABLE_REFs. */
3405 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3406 is_gimple_min_lval, fb_lvalue);
3411 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3415 /* We get here when taking the address of a label. We mark
3416 the label as "forced"; meaning it can never be removed and
3417 it is a potential target for any computed goto. */
3418 FORCED_LABEL (*expr_p) = 1;
3422 case STATEMENT_LIST:
3423 ret = gimplify_statement_list (expr_p);
3427 /* ??? If this is a local variable, and it has not been seen in any
3428 outer BIND_EXPR, then it's probably the result of a duplicate
3429 declaration, for which we've already issued an error. It would
3430 be really nice if the front end wouldn't leak these at all.
3431 Currently the only known culprit is C++ destructors, as seen
3432 in g++.old-deja/g++.jason/binding.C. */
3434 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3435 && decl_function_context (tmp) == current_function_decl
3436 && !tmp->decl.seen_in_bind_expr)
3438 #ifdef ENABLE_CHECKING
3439 if (!errorcount && !sorrycount)
3449 /* If *EXPR_P does not need to be special-cased, handle it
3450 according to its class. */
3451 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3452 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3453 post_p, is_gimple_val, fb_rvalue);
3454 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3455 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3456 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3457 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3458 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3460 enum gimplify_status r0, r1;
3462 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3463 post_p, is_gimple_val, fb_rvalue);
3464 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3465 post_p, is_gimple_val, fb_rvalue);
3469 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3470 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3476 /* Fail if we don't know how to handle this tree code. */
3479 recalculate_side_effects (*expr_p);
3483 /* If we replaced *expr_p, gimplify again. */
3484 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3487 while (ret == GS_OK);
3489 /* If we encountered an error_mark somewhere nested inside, either
3490 stub out the statement or propagate the error back out. */
3491 if (ret == GS_ERROR)
3494 *expr_p = build_empty_stmt ();
3498 #ifdef ENABLE_CHECKING
3499 /* This was only valid as a return value from the langhook, which
3500 we handled. Make sure it doesn't escape from any other context. */
3501 if (ret == GS_UNHANDLED)
3506 *expr_p = build_empty_stmt ();
3507 if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3509 /* We aren't looking for a value, and we don't have a valid
3510 statement. If it doesn't have side-effects, throw it away. */
3511 if (!TREE_SIDE_EFFECTS (*expr_p))
3512 *expr_p = build_empty_stmt ();
3513 else if (!TREE_THIS_VOLATILE (*expr_p))
3514 /* We only handle volatiles here; anything else with side-effects
3515 must be converted to a valid statement before we get here. */
3517 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3519 /* Historically, the compiler has treated a bare
3520 reference to a volatile lvalue as forcing a load. */
3521 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3522 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3525 /* We can't do anything useful with a volatile reference to
3526 incomplete type, so just throw it away. */
3527 *expr_p = build_empty_stmt ();
3530 /* If we are gimplifying at the statement level, we're done. Tack
3531 everything together and replace the original statement with the
3535 if (internal_pre || internal_post)
3537 append_to_statement_list (*expr_p, &internal_pre);
3538 append_to_statement_list (internal_post, &internal_pre);
3539 annotate_all_with_locus (&internal_pre, input_location);
3540 *expr_p = internal_pre;
3545 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3548 /* If it's sufficiently simple already, we're done. Unless we are
3549 handling some post-effects internally; if that's the case, we need to
3550 copy into a temp before adding the post-effects to the tree. */
3551 if (!internal_post && (*gimple_test_f) (*expr_p))
3554 /* Otherwise, we need to create a new temporary for the gimplified
3557 /* We can't return an lvalue if we have an internal postqueue. The
3558 object the lvalue refers to would (probably) be modified by the
3559 postqueue; we need to copy the value out first, which means an
3561 if ((fallback & fb_lvalue) && !internal_post
3562 && is_gimple_addr_expr_arg (*expr_p))
3564 /* An lvalue will do. Take the address of the expression, store it
3565 in a temporary, and replace the expression with an INDIRECT_REF of
3567 tmp = build_fold_addr_expr (*expr_p);
3568 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3569 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3571 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3573 #if defined ENABLE_CHECKING
3574 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3578 /* An rvalue will do. Assign the gimplified expression into a new
3579 temporary TMP and replace the original expression with TMP. */
3581 if (internal_post || (fallback & fb_lvalue))
3582 /* The postqueue might change the value of the expression between
3583 the initialization and use of the temporary, so we can't use a
3584 formal temp. FIXME do we care? */
3585 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3587 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3589 else if (fallback & fb_mayfail)
3591 /* If this is an asm statement, and the user asked for the impossible,
3592 don't abort. Fail and let gimplify_asm_expr issue an error. */
3598 fprintf (stderr, "gimplification failed:\n");
3599 print_generic_expr (stderr, *expr_p, 0);
3600 debug_tree (*expr_p);
3604 #if defined ENABLE_CHECKING
3605 /* Make sure the temporary matches our predicate. */
3606 if (!(*gimple_test_f) (*expr_p))
3612 annotate_all_with_locus (&internal_post, input_location);
3613 append_to_statement_list (internal_post, pre_p);
3617 input_location = saved_location;
3621 #ifdef ENABLE_CHECKING
3622 /* Compare types A and B for a "close enough" match. */
3625 cpt_same_type (tree a, tree b)
3627 if (lang_hooks.types_compatible_p (a, b))
3630 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3631 link them together. This routine is intended to catch type errors
3632 that will affect the optimizers, and the optimizers don't add new
3633 dereferences of function pointers, so ignore it. */
3634 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3635 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3638 /* ??? The C FE pushes type qualifiers after the fact into the type of
3639 the element from the type of the array. See build_unary_op's handling
3640 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3641 should have done it when creating the variable in the first place.
3642 Alternately, why aren't the two array types made variants? */
3643 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3644 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3646 /* And because of those, we have to recurse down through pointers. */
3647 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3648 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3653 /* Check for some cases of the front end missing cast expressions.
3654 The type of a dereference should correspond to the pointer type;
3655 similarly the type of an address should match its object. */
3658 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3659 void *data ATTRIBUTE_UNUSED)
3662 tree ptype, otype, dtype;
3664 switch (TREE_CODE (t))
3668 otype = TREE_TYPE (t);
3669 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3670 dtype = TREE_TYPE (ptype);
3671 if (!cpt_same_type (otype, dtype))
3676 ptype = TREE_TYPE (t);
3677 otype = TREE_TYPE (TREE_OPERAND (t, 0));
3678 dtype = TREE_TYPE (ptype);
3679 if (!cpt_same_type (otype, dtype))
3681 /* &array is allowed to produce a pointer to the element,
3682 rather than a pointer to the array type. */
3683 if (TREE_CODE (otype) == ARRAY_TYPE
3684 && POINTER_TYPE_P (ptype)
3685 && cpt_same_type (TREE_TYPE (otype), dtype))
3700 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
3701 function decl containing BODY. */
3704 gimplify_body (tree *body_p, tree fndecl)
3706 location_t saved_location = input_location;
3709 timevar_push (TV_TREE_GIMPLIFY);
3710 push_gimplify_context ();
3712 /* Unshare most shared trees in the body. */
3713 unshare_all_trees (*body_p);
3715 /* Make sure input_location isn't set to something wierd. */
3716 input_location = DECL_SOURCE_LOCATION (fndecl);
3718 /* Gimplify the function's body. */
3719 gimplify_stmt (body_p);
3722 /* Unshare again, in case gimplification was sloppy. */
3723 unshare_all_trees (body);
3725 /* If there isn't an outer BIND_EXPR, add one. */
3726 if (TREE_CODE (body) == STATEMENT_LIST)
3728 tree t = expr_only (*body_p);
3732 if (TREE_CODE (body) != BIND_EXPR)
3734 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3735 NULL_TREE, NULL_TREE);
3736 TREE_SIDE_EFFECTS (b) = 1;
3737 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3742 pop_gimplify_context (body);
3744 #ifdef ENABLE_CHECKING
3745 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3748 timevar_pop (TV_TREE_GIMPLIFY);
3749 input_location = saved_location;
3752 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
3753 node for the function we want to gimplify. */
3756 gimplify_function_tree (tree fndecl)
3760 oldfn = current_function_decl;
3761 current_function_decl = fndecl;
3763 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3765 /* If we're instrumenting function entry/exit, then prepend the call to
3766 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3767 catch the exit hook. */
3768 /* ??? Add some way to ignore exceptions for this TFE. */
3769 if (flag_instrument_function_entry_exit
3770 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3774 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3775 TREE_SIDE_EFFECTS (tf) = 1;
3776 x = DECL_SAVED_TREE (fndecl);
3777 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3778 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3779 x = build_function_call_expr (x, NULL);
3780 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3782 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3783 TREE_SIDE_EFFECTS (bind) = 1;
3784 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3785 x = build_function_call_expr (x, NULL);
3786 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3787 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3789 DECL_SAVED_TREE (fndecl) = bind;
3792 current_function_decl = oldfn;
3795 #include "gt-gimplify.h"