1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
4 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "coretypes.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "tree-flow.h"
49 static struct gimplify_ctx
51 tree current_bind_expr;
54 tree conditional_cleanups;
58 varray_type case_labels;
59 /* The formal temporary table. Should this be persistent? */
64 /* Formal (expression) temporary table handling: Multiple occurrences of
65 the same scalar expression are evaluated into the same temporary. */
67 typedef struct gimple_temp_hash_elt
70 tree temp; /* Value */
73 /* Return a hash value for a formal temporary table entry. */
76 gimple_tree_hash (const void *p)
78 tree t = ((const elt_t *)p)->val;
79 return iterative_hash_expr (t, 0);
82 /* Compare two formal temporary table entries. */
85 gimple_tree_eq (const void *p1, const void *p2)
87 tree t1 = ((const elt_t *)p1)->val;
88 tree t2 = ((const elt_t *)p2)->val;
89 enum tree_code code = TREE_CODE (t1);
91 if (TREE_CODE (t2) != code
92 || TREE_TYPE (t1) != TREE_TYPE (t2))
95 if (!operand_equal_p (t1, t2, 0))
98 /* Only allow them to compare equal if they also hash equal; otherwise
99 results are nondeterminate, and we fail bootstrap comparison. */
100 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
106 /* Set up a context for the gimplifier. */
109 push_gimplify_context (void)
114 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
115 gimplify_ctxp->temp_htab
116 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
119 /* Tear down a context for the gimplifier. If BODY is non-null, then
120 put the temporaries into the outer BIND_EXPR. Otherwise, put them
121 in the unexpanded_var_list. */
124 pop_gimplify_context (tree body)
126 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
130 declare_tmp_vars (gimplify_ctxp->temps, body);
132 record_vars (gimplify_ctxp->temps);
136 fprintf (stderr, " collisions: %f ",
137 htab_collisions (gimplify_ctxp->temp_htab));
140 htab_delete (gimplify_ctxp->temp_htab);
141 free (gimplify_ctxp);
142 gimplify_ctxp = NULL;
146 gimple_push_bind_expr (tree bind)
148 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
149 gimplify_ctxp->current_bind_expr = bind;
153 gimple_pop_bind_expr (void)
155 gimplify_ctxp->current_bind_expr
156 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
160 gimple_current_bind_expr (void)
162 return gimplify_ctxp->current_bind_expr;
165 /* Returns true iff there is a COND_EXPR between us and the innermost
166 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
169 gimple_conditional_context (void)
171 return gimplify_ctxp->conditions > 0;
174 /* Note that we've entered a COND_EXPR. */
177 gimple_push_condition (void)
179 ++(gimplify_ctxp->conditions);
182 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
183 now, add any conditional cleanups we've seen to the prequeue. */
186 gimple_pop_condition (tree *pre_p)
188 int conds = --(gimplify_ctxp->conditions);
191 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
192 gimplify_ctxp->conditional_cleanups = NULL_TREE;
198 /* A subroutine of append_to_statement_list{,_force}. */
201 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
204 tree_stmt_iterator i;
211 if (t && TREE_CODE (t) == STATEMENT_LIST)
216 *list_p = list = alloc_stmt_list ();
220 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
223 /* Add T to the end of the list container pointed by LIST_P.
224 If T is an expression with no effects, it is ignored. */
227 append_to_statement_list (tree t, tree *list_p)
229 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
232 /* Similar, but the statement is always added, regardless of side effects. */
235 append_to_statement_list_force (tree t, tree *list_p)
237 append_to_statement_list_1 (t, list_p, t != NULL);
240 /* Add T to the end of a COMPOUND_EXPR pointed by LIST_P. The type
241 of the result is the type of T. */
244 append_to_compound_expr (tree t, tree *list_p)
251 *list_p = build (COMPOUND_EXPR, TREE_TYPE (t), *list_p, t);
254 /* Strip off a legitimate source ending from the input string NAME of
255 length LEN. Rather than having to know the names used by all of
256 our front ends, we strip off an ending of a period followed by
257 up to five characters. (Java uses ".class".) */
260 remove_suffix (char *name, int len)
264 for (i = 2; i < 8 && len > i; i++)
266 if (name[len - i] == '.')
268 name[len - i] = '\0';
274 /* Create a nameless artificial label and put it in the current function
275 context. Returns the newly created label. */
278 create_artificial_label (void)
280 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
281 DECL_ARTIFICIAL (lab) = 1;
282 DECL_CONTEXT (lab) = current_function_decl;
286 /* Create a new temporary name with PREFIX. Returns an identifier. */
288 static GTY(()) unsigned int tmp_var_id_num;
291 create_tmp_var_name (const char *prefix)
297 char *preftmp = ASTRDUP (prefix);
298 remove_suffix (preftmp, strlen (preftmp));
302 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
303 return get_identifier (tmp_name);
307 /* Create a new temporary variable declaration of type TYPE.
308 Does NOT push it into the current binding. */
311 create_tmp_var_raw (tree type, const char *prefix)
316 /* Make the type of the variable writable. */
317 new_type = build_type_variant (type, 0, 0);
318 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
320 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
322 /* The variable was declared by the compiler. */
323 DECL_ARTIFICIAL (tmp_var) = 1;
324 /* And we don't want debug info for it. */
325 DECL_IGNORED_P (tmp_var) = 1;
327 /* Make the variable writable. */
328 TREE_READONLY (tmp_var) = 0;
330 DECL_EXTERNAL (tmp_var) = 0;
331 TREE_STATIC (tmp_var) = 0;
332 TREE_USED (tmp_var) = 1;
337 /* Create a new temporary variable declaration of type TYPE. DOES push the
338 variable into the current binding. Further, assume that this is called
339 only from gimplification or optimization, at which point the creation of
340 certain types are bugs. */
343 create_tmp_var (tree type, const char *prefix)
347 #if defined ENABLE_CHECKING
348 /* If the type is an array or a type which must be created by the
349 frontend, something is wrong. */
350 if (TREE_CODE (type) == ARRAY_TYPE || TREE_ADDRESSABLE (type))
352 if (!COMPLETE_TYPE_P (type))
354 /* Variable sized types require lots of machinery to create; the
355 optimizers shouldn't be doing anything of the sort. */
356 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
360 tmp_var = create_tmp_var_raw (type, prefix);
361 gimple_add_tmp_var (tmp_var);
365 /* Given a tree, try to return a useful variable name that we can use
366 to prefix a temporary that is being assigned the value of the tree.
367 I.E. given <temp> = &A, return A. */
375 STRIP_NOPS (stripped_decl);
376 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
377 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
380 switch (TREE_CODE (stripped_decl))
383 return get_name (TREE_OPERAND (stripped_decl, 0));
391 /* Create a temporary with a name derived from VAL. Subroutine of
392 lookup_tmp_var; nobody else should call this function. */
395 create_tmp_from_val (tree val)
397 return create_tmp_var (TREE_TYPE (val), get_name (val));
400 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
401 an existing expression temporary. */
404 lookup_tmp_var (tree val, bool is_formal)
406 if (!is_formal || TREE_SIDE_EFFECTS (val))
407 return create_tmp_from_val (val);
414 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
417 elt_p = xmalloc (sizeof (*elt_p));
419 elt_p->temp = create_tmp_from_val (val);
420 *slot = (void *)elt_p;
423 elt_p = (elt_t *) *slot;
429 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
430 in gimplify_expr. Only use this function if:
432 1) The value of the unfactored expression represented by VAL will not
433 change between the initialization and use of the temporary, and
434 2) The temporary will not be otherwise modified.
436 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
437 and #2 means it is inappropriate for && temps.
439 For other cases, use get_initialized_tmp_var instead. */
442 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
447 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
449 t = lookup_tmp_var (val, is_formal);
451 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
453 class = TREE_CODE_CLASS (TREE_CODE (val));
454 if (EXPR_LOCUS (val))
455 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
457 annotate_with_locus (mod, input_location);
458 /* gimplify_modify_expr might want to reduce this further. */
459 gimplify_stmt (&mod);
460 append_to_statement_list (mod, pre_p);
466 get_formal_tmp_var (tree val, tree *pre_p)
468 return internal_get_tmp_var (val, pre_p, NULL, true);
471 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
472 are as in gimplify_expr. */
475 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
477 return internal_get_tmp_var (val, pre_p, post_p, false);
480 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
483 is_gimple_tmp_var (tree t)
485 /* FIXME this could trigger for other local artificials, too. */
486 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
487 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
490 /* Declares all the variables in VARS in SCOPE. Returns the last
491 DECL_STMT emitted. */
494 declare_tmp_vars (tree vars, tree scope)
501 /* C99 mode puts the default 'return 0;' for main() outside the outer
502 braces. So drill down until we find an actual scope. */
503 while (TREE_CODE (scope) == COMPOUND_EXPR)
504 scope = TREE_OPERAND (scope, 0);
506 if (TREE_CODE (scope) != BIND_EXPR)
509 temps = nreverse (last);
510 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
511 BIND_EXPR_VARS (scope) = temps;
513 /* We don't add the temps to the block for this BIND_EXPR, as we're
514 not interested in debugging info for them. */
519 gimple_add_tmp_var (tree tmp)
521 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
524 DECL_CONTEXT (tmp) = current_function_decl;
525 tmp->decl.seen_in_bind_expr = 1;
529 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
530 gimplify_ctxp->temps = tmp;
535 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
538 /* Determines whether to assign a locus to the statement STMT. */
541 should_carry_locus_p (tree stmt)
543 /* Don't emit a line note for a label. We particularly don't want to
544 emit one for the break label, since it doesn't actually correspond
545 to the beginning of the loop/switch. */
546 if (TREE_CODE (stmt) == LABEL_EXPR)
549 /* Do not annotate empty statements, since it confuses gcov. */
550 if (!TREE_SIDE_EFFECTS (stmt))
557 annotate_all_with_locus (tree *stmt_p, location_t locus)
559 tree_stmt_iterator i;
564 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
566 tree t = tsi_stmt (i);
568 #ifdef ENABLE_CHECKING
569 /* Assuming we've already been gimplified, we shouldn't
570 see nested chaining constructs anymore. */
571 if (TREE_CODE (t) == STATEMENT_LIST
572 || TREE_CODE (t) == COMPOUND_EXPR)
576 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
577 && ! EXPR_HAS_LOCATION (t)
578 && should_carry_locus_p (t))
579 annotate_with_locus (t, locus);
583 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
584 These nodes model computations that should only be done once. If we
585 were to unshare something like SAVE_EXPR(i++), the gimplification
586 process would create wrong code. */
589 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
591 enum tree_code code = TREE_CODE (*tp);
592 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
593 if (TREE_CODE_CLASS (code) == 't'
594 || TREE_CODE_CLASS (code) == 'd'
595 || TREE_CODE_CLASS (code) == 'c'
596 || code == SAVE_EXPR || code == TARGET_EXPR
597 /* We can't do anything sensible with a BLOCK used as an expression,
598 but we also can't abort when we see it because of non-expression
599 uses. So just avert our eyes and cross our fingers. Silly Java. */
602 else if (code == BIND_EXPR)
605 copy_tree_r (tp, walk_subtrees, data);
610 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
611 after VA_ARG_EXPRs are properly lowered. */
614 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
615 void *data ATTRIBUTE_UNUSED)
618 TREE_THIS_VOLATILE (*tp) = 1;
624 /* Callback for walk_tree to unshare most of the shared trees rooted at
625 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
626 then *TP is deep copied by calling copy_tree_r.
628 This unshares the same trees as copy_tree_r with the exception of
629 SAVE_EXPR nodes. These nodes model computations that should only be
630 done once. If we were to unshare something like SAVE_EXPR(i++), the
631 gimplification process would create wrong code. */
634 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
635 void *data ATTRIBUTE_UNUSED)
638 enum tree_code code = TREE_CODE (t);
640 /* Skip types, decls, and constants. */
641 if (TREE_CODE_CLASS (code) == 't'
642 || TREE_CODE_CLASS (code) == 'd'
643 || TREE_CODE_CLASS (code) == 'c')
646 /* Special-case BIND_EXPR. We should never be copying these, therefore
647 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
648 double processing of the DECL_INITIAL, which could be seen via both
649 the BIND_EXPR_VARS and a DECL_STMT. */
650 else if (code == BIND_EXPR)
652 if (TREE_VISITED (t))
654 TREE_VISITED (t) = 1;
656 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
659 /* If this node has been visited already, unshare it and don't look
661 else if (TREE_VISITED (t))
663 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
667 /* Otherwise, mark the tree as visited and keep looking. */
670 TREE_VISITED (t) = 1;
671 if (TREE_CODE (*tp) == VA_ARG_EXPR)
673 /* Mark any _DECL inside the operand as volatile to avoid
674 the optimizers messing around with it. We have to do this
675 early, otherwise we might mark a variable as volatile
676 after we gimplify other statements that use the variable
677 assuming it's not volatile. */
678 walk_tree (&TREE_OPERAND (*tp, 0), mark_decls_volatile_r,
687 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
688 void *data ATTRIBUTE_UNUSED)
690 if (TREE_VISITED (*tp))
691 TREE_VISITED (*tp) = 0;
698 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
701 unshare_all_trees (tree t)
703 walk_tree (&t, copy_if_shared_r, NULL, NULL);
704 walk_tree (&t, unmark_visited_r, NULL, NULL);
707 /* Unconditionally make an unshared copy of EXPR. This is used when using
708 stored expressions which span multiple functions, such as BINFO_VTABLE,
709 as the normal unsharing process can't tell that they're shared. */
712 unshare_expr (tree expr)
714 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
718 /* A terser interface for building a representation of a exception
722 gimple_build_eh_filter (tree body, tree allowed, tree failure)
726 /* FIXME should the allowed types go in TREE_TYPE? */
727 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
728 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
730 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
731 append_to_statement_list (body, &TREE_OPERAND (t, 0));
737 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
738 contain statements and have a value. Assign its value to a temporary
739 and give it void_type_node. Returns the temporary, or NULL_TREE if
740 WRAPPER was already void. */
743 voidify_wrapper_expr (tree wrapper)
745 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
750 /* Set p to point to the body of the wrapper. */
751 switch (TREE_CODE (wrapper))
754 /* For a BIND_EXPR, the body is operand 1. */
755 p = &BIND_EXPR_BODY (wrapper);
759 p = &TREE_OPERAND (wrapper, 0);
763 /* Advance to the last statement. Set all container types to void. */
764 if (TREE_CODE (*p) == STATEMENT_LIST)
766 tree_stmt_iterator i = tsi_last (*p);
767 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
771 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
773 TREE_SIDE_EFFECTS (*p) = 1;
774 TREE_TYPE (*p) = void_type_node;
778 if (p && TREE_CODE (*p) == INIT_EXPR)
780 /* The C++ frontend already did this for us. */;
781 temp = TREE_OPERAND (*p, 0);
783 else if (p && TREE_CODE (*p) == INDIRECT_REF)
785 /* If we're returning a dereference, move the dereference outside
787 tree ptr = TREE_OPERAND (*p, 0);
788 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
789 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
790 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
791 /* If this is a BIND_EXPR for a const inline function, it might not
792 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
793 TREE_SIDE_EFFECTS (wrapper) = 1;
797 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
798 if (p && !IS_EMPTY_STMT (*p))
800 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
801 TREE_SIDE_EFFECTS (wrapper) = 1;
805 TREE_TYPE (wrapper) = void_type_node;
812 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
813 a temporary through which they communicate. */
816 build_stack_save_restore (tree *save, tree *restore)
818 tree save_call, tmp_var;
821 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
823 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
825 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
827 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
828 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
831 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
833 static enum gimplify_status
834 gimplify_bind_expr (tree *expr_p, tree *pre_p)
836 tree bind_expr = *expr_p;
837 tree temp = voidify_wrapper_expr (bind_expr);
838 bool old_save_stack = gimplify_ctxp->save_stack;
841 /* Mark variables seen in this bind expr. */
842 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
843 t->decl.seen_in_bind_expr = 1;
845 gimple_push_bind_expr (bind_expr);
846 gimplify_ctxp->save_stack = false;
848 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
850 if (gimplify_ctxp->save_stack)
852 tree stack_save, stack_restore;
854 /* Save stack on entry and restore it on exit. Add a try_finally
855 block to achieve this. Note that mudflap depends on the
856 format of the emitted code: see mx_register_decls(). */
857 build_stack_save_restore (&stack_save, &stack_restore);
859 t = build (TRY_FINALLY_EXPR, void_type_node,
860 BIND_EXPR_BODY (bind_expr), NULL_TREE);
861 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
863 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
864 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
865 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
868 gimplify_ctxp->save_stack = old_save_stack;
869 gimple_pop_bind_expr ();
874 append_to_statement_list (bind_expr, pre_p);
881 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
882 GIMPLE value, it is assigned to a new temporary and the statement is
883 re-written to return the temporary.
885 PRE_P points to the list where side effects that must happen before
886 STMT should be stored. */
888 static enum gimplify_status
889 gimplify_return_expr (tree stmt, tree *pre_p)
891 tree ret_expr = TREE_OPERAND (stmt, 0);
892 tree result_decl, result;
894 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
897 if (ret_expr == error_mark_node)
900 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
901 result_decl = NULL_TREE;
904 result_decl = TREE_OPERAND (ret_expr, 0);
905 #ifdef ENABLE_CHECKING
906 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
907 && TREE_CODE (ret_expr) != INIT_EXPR)
908 || TREE_CODE (result_decl) != RESULT_DECL)
913 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
914 Recall that aggregate_value_p is FALSE for any aggregate type that is
915 returned in registers. If we're returning values in registers, then
916 we don't want to extend the lifetime of the RESULT_DECL, particularly
917 across another call. In addition, for those aggregates for which
918 hard_function_value generates a PARALLEL, we'll abort during normal
919 expansion of structure assignments; there's special code in expand_return
920 to handle this case that does not exist in expand_expr. */
922 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
923 result = result_decl;
924 else if (gimplify_ctxp->return_temp)
925 result = gimplify_ctxp->return_temp;
928 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
929 gimplify_ctxp->return_temp = result;
932 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
933 Then gimplify the whole thing. */
934 if (result != result_decl)
935 TREE_OPERAND (ret_expr, 0) = result;
936 gimplify_stmt (&TREE_OPERAND (stmt, 0));
937 append_to_statement_list (TREE_OPERAND (stmt, 0), pre_p);
939 /* If we didn't use a temporary, then the result is just the result_decl.
940 Otherwise we need a simple copy. This should already be gimple. */
941 if (result == result_decl)
944 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
945 TREE_OPERAND (stmt, 0) = ret_expr;
950 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
951 and replacing the LOOP_EXPR with goto, but if the loop contains an
952 EXIT_EXPR, we need to append a label for it to jump to. */
954 static enum gimplify_status
955 gimplify_loop_expr (tree *expr_p, tree *pre_p)
957 tree saved_label = gimplify_ctxp->exit_label;
958 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
959 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
961 append_to_statement_list (start_label, pre_p);
963 gimplify_ctxp->exit_label = NULL_TREE;
965 gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
966 append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
968 if (gimplify_ctxp->exit_label)
970 append_to_statement_list (jump_stmt, pre_p);
971 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
976 gimplify_ctxp->exit_label = saved_label;
981 /* Compare two case labels. Because the front end should already have
982 made sure that case ranges do not overlap, it is enough to only compare
983 the CASE_LOW values of each case label. */
986 compare_case_labels (const void *p1, const void *p2)
988 tree case1 = *(tree *)p1;
989 tree case2 = *(tree *)p2;
991 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
994 /* Sort the case labels in LABEL_VEC in ascending order. */
997 sort_case_labels (tree label_vec)
999 size_t len = TREE_VEC_LENGTH (label_vec);
1000 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1002 if (CASE_LOW (default_case))
1006 /* The last label in the vector should be the default case
1008 for (i = 0; i < len; ++i)
1010 tree t = TREE_VEC_ELT (label_vec, i);
1014 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1015 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1021 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1022 compare_case_labels);
1025 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1028 static enum gimplify_status
1029 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1031 tree switch_expr = *expr_p;
1032 enum gimplify_status ret;
1034 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1035 is_gimple_val, fb_rvalue);
1037 if (SWITCH_BODY (switch_expr))
1039 varray_type labels, saved_labels;
1040 tree label_vec, default_case = NULL_TREE;
1043 /* If someone can be bothered to fill in the labels, they can
1044 be bothered to null out the body too. */
1045 if (SWITCH_LABELS (switch_expr))
1048 saved_labels = gimplify_ctxp->case_labels;
1049 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1051 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1053 labels = gimplify_ctxp->case_labels;
1054 gimplify_ctxp->case_labels = saved_labels;
1056 len = VARRAY_ACTIVE_SIZE (labels);
1058 for (i = 0; i < len; ++i)
1060 tree t = VARRAY_TREE (labels, i);
1063 /* The default case must be the last label in the list. */
1065 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1071 label_vec = make_tree_vec (len + 1);
1072 SWITCH_LABELS (*expr_p) = label_vec;
1073 append_to_statement_list (switch_expr, pre_p);
1077 /* If the switch has no default label, add one, so that we jump
1078 around the switch body. */
1079 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1080 NULL_TREE, create_artificial_label ());
1081 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1082 *expr_p = build (LABEL_EXPR, void_type_node,
1083 CASE_LABEL (default_case));
1086 *expr_p = SWITCH_BODY (switch_expr);
1088 for (i = 0; i < len; ++i)
1089 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1090 TREE_VEC_ELT (label_vec, len) = default_case;
1092 sort_case_labels (label_vec);
1094 SWITCH_BODY (switch_expr) = NULL;
1096 else if (!SWITCH_LABELS (switch_expr))
1102 static enum gimplify_status
1103 gimplify_case_label_expr (tree *expr_p)
1105 tree expr = *expr_p;
1106 if (gimplify_ctxp->case_labels)
1107 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1110 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1114 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1115 a (possibly empty) body. */
1117 static enum gimplify_status
1118 gimplify_labeled_block_expr (tree *expr_p)
1120 tree body = LABELED_BLOCK_BODY (*expr_p);
1121 tree label = LABELED_BLOCK_LABEL (*expr_p);
1124 DECL_CONTEXT (label) = current_function_decl;
1125 t = build (LABEL_EXPR, void_type_node, label);
1126 if (body != NULL_TREE)
1127 t = build (COMPOUND_EXPR, void_type_node, body, t);
1133 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1135 static enum gimplify_status
1136 gimplify_exit_block_expr (tree *expr_p)
1138 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1141 /* First operand must be a LABELED_BLOCK_EXPR, which should
1142 already be lowered (or partially lowered) when we get here. */
1143 #if defined ENABLE_CHECKING
1144 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1148 label = LABELED_BLOCK_LABEL (labeled_block);
1149 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1154 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1158 build_and_jump (tree *label_p)
1160 if (label_p == NULL)
1161 /* If there's nowhere to jump, just fall through. */
1162 return build_empty_stmt ();
1164 if (*label_p == NULL_TREE)
1166 tree label = create_artificial_label ();
1170 return build1 (GOTO_EXPR, void_type_node, *label_p);
1173 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1174 This also involves building a label to jump to and communicating it to
1175 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1177 static enum gimplify_status
1178 gimplify_exit_expr (tree *expr_p)
1180 tree cond = TREE_OPERAND (*expr_p, 0);
1183 expr = build_and_jump (&gimplify_ctxp->exit_label);
1184 expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1190 /* A helper function to be called via walk_tree. Mark all labels under *TP
1191 as being forced. To be called for DECL_INITIAL of static variables. */
1194 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1198 if (TREE_CODE (*tp) == LABEL_DECL)
1199 FORCED_LABEL (*tp) = 1;
1204 /* Break out elements of a constructor used as an initializer into separate
1207 Note that we still need to clear any elements that don't have explicit
1208 initializers, so if not all elements are initialized we keep the
1209 original MODIFY_EXPR, we just remove all of the constructor elements. */
1211 static enum gimplify_status
1212 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1213 tree *post_p, int want_value)
1215 tree object = TREE_OPERAND (*expr_p, 0);
1216 tree ctor = TREE_OPERAND (*expr_p, 1);
1217 tree type = TREE_TYPE (ctor);
1218 enum gimplify_status ret;
1221 if (TREE_CODE (ctor) != CONSTRUCTOR)
1222 return GS_UNHANDLED;
1224 elt_list = CONSTRUCTOR_ELTS (ctor);
1227 switch (TREE_CODE (type))
1231 case QUAL_UNION_TYPE:
1234 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1235 HOST_WIDE_INT num_nonconstant_elements;
1238 /* Aggregate types must lower constructors to initialization of
1239 individual elements. The exception is that a CONSTRUCTOR node
1240 with no elements indicates zero-initialization of the whole. */
1241 if (elt_list == NULL)
1252 categorize_ctor_elements (ctor, &num_nonzero_elements,
1253 &num_nonconstant_elements);
1254 num_elements = count_type_elements (TREE_TYPE (ctor));
1256 /* If a const aggregate variable is being initialized, then it
1257 should never be a lose to promote the variable to be static. */
1258 if (num_nonconstant_elements == 0
1259 && TREE_READONLY (object)
1260 && TREE_CODE (object) == VAR_DECL)
1262 DECL_INITIAL (object) = ctor;
1263 TREE_STATIC (object) = 1;
1264 if (!DECL_NAME (object))
1265 DECL_NAME (object) = create_tmp_var_name ("C");
1266 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1268 /* ??? C++ doesn't automatically append a .<number> to the
1269 assembler name, and even when it does, it looks a FE private
1270 data structures to figure out what that number should be,
1271 which are not set for this variable. I suppose this is
1272 important for local statics for inline functions, which aren't
1273 "local" in the object file sense. So in order to get a unique
1274 TU-local symbol, we must invoke the lhd version now. */
1275 lhd_set_decl_assembler_name (object);
1277 *expr_p = build_empty_stmt ();
1281 /* If there are "lots" of initialized elements, and all of them
1282 are valid address constants, then the entire initializer can
1283 be dropped to memory, and then memcpy'd out. */
1284 if (num_nonconstant_elements == 0)
1286 HOST_WIDE_INT size = int_size_in_bytes (type);
1289 /* ??? We can still get unbounded array types, at least
1290 from the C++ front end. This seems wrong, but attempt
1291 to work around it for now. */
1294 size = int_size_in_bytes (TREE_TYPE (object));
1296 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1299 /* Find the maximum alignment we can assume for the object. */
1300 /* ??? Make use of DECL_OFFSET_ALIGN. */
1301 if (DECL_P (object))
1302 align = DECL_ALIGN (object);
1304 align = TYPE_ALIGN (type);
1306 if (size > 0 && !can_move_by_pieces (size, align))
1308 tree new = create_tmp_var_raw (type, "C");
1309 gimple_add_tmp_var (new);
1310 TREE_STATIC (new) = 1;
1311 TREE_READONLY (new) = 1;
1312 DECL_INITIAL (new) = ctor;
1313 if (align > DECL_ALIGN (new))
1315 DECL_ALIGN (new) = align;
1316 DECL_USER_ALIGN (new) = 1;
1318 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1320 TREE_OPERAND (*expr_p, 1) = new;
1325 /* If there are "lots" of initialized elements, even discounting
1326 those that are not address constants (and thus *must* be
1327 computed at runtime), then partition the constructor into
1328 constant and non-constant parts. Block copy the constant
1329 parts in, then generate code for the non-constant parts. */
1330 /* TODO. There's code in cp/typeck.c to do this. */
1332 /* If there are "lots" of zeros, then block clear the object first. */
1334 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1335 && num_nonzero_elements < num_elements/4)
1338 /* ??? This bit ought not be needed. For any element not present
1339 in the initializer, we should simply set them to zero. Except
1340 we'd need to *find* the elements that are not present, and that
1341 requires trickery to avoid quadratic compile-time behavior in
1342 large cases or excessive memory use in small cases. */
1345 HOST_WIDE_INT len = list_length (elt_list);
1346 if (TREE_CODE (type) == ARRAY_TYPE)
1348 tree nelts = array_type_nelts (type);
1349 if (!host_integerp (nelts, 1)
1350 || tree_low_cst (nelts, 1) != len)
1353 else if (len != fields_length (type))
1359 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1360 append_to_statement_list (*expr_p, pre_p);
1363 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1365 tree purpose, value, cref, init;
1367 purpose = TREE_PURPOSE (elt_list);
1368 value = TREE_VALUE (elt_list);
1370 if (cleared && initializer_zerop (value))
1373 if (TREE_CODE (type) == ARRAY_TYPE)
1375 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1377 /* ??? Here's to hoping the front end fills in all of the
1378 indicies, so we don't have to figure out what's missing
1382 /* ??? Need to handle this. */
1383 if (TREE_CODE (purpose) == RANGE_EXPR)
1386 cref = build (ARRAY_REF, t, object, purpose);
1390 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1394 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1395 /* Each member initialization is a full-expression. */
1396 gimplify_stmt (&init);
1397 append_to_statement_list (init, pre_p);
1400 *expr_p = build_empty_stmt ();
1408 /* Extract the real and imaginary parts out of the ctor. */
1412 r = TREE_VALUE (elt_list);
1413 elt_list = TREE_CHAIN (elt_list);
1416 i = TREE_VALUE (elt_list);
1417 if (TREE_CHAIN (elt_list))
1421 if (r == NULL || i == NULL)
1423 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1430 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1431 represent creation of a complex value. */
1432 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1434 ctor = build_complex (type, r, i);
1435 TREE_OPERAND (*expr_p, 1) = ctor;
1439 ctor = build (COMPLEX_EXPR, type, r, i);
1440 TREE_OPERAND (*expr_p, 1) = ctor;
1441 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1442 is_gimple_rhs, fb_rvalue);
1448 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1449 if (TREE_CONSTANT (ctor))
1450 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1453 /* Vector types use CONSTRUCTOR all the way through gimple
1454 compilation as a general initializer. */
1455 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1457 enum gimplify_status tret;
1458 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1459 is_gimple_constructor_elt, fb_rvalue);
1460 if (tret == GS_ERROR)
1467 /* So how did we get a CONSTRUCTOR for a scalar type? */
1471 if (ret == GS_ERROR)
1473 else if (want_value)
1475 append_to_statement_list (*expr_p, pre_p);
1483 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1484 different from its canonical type, wrap the whole thing inside a
1485 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1488 The canonical type of a COMPONENT_REF is the type of the field being
1489 referenced--unless the field is a bit-field which can be read directly
1490 in a smaller mode, in which case the canonical type is the
1491 sign-appropriate type corresponding to that mode. */
1494 canonicalize_component_ref (tree *expr_p)
1496 tree expr = *expr_p;
1499 if (TREE_CODE (expr) != COMPONENT_REF)
1502 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1503 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1505 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1507 if (TREE_TYPE (expr) != type)
1509 tree old_type = TREE_TYPE (expr);
1511 /* Set the type of the COMPONENT_REF to the underlying type. */
1512 TREE_TYPE (expr) = type;
1514 /* And wrap the whole thing inside a NOP_EXPR. */
1515 expr = build1 (NOP_EXPR, old_type, expr);
1521 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1522 to foo, embed that change in the ADDR_EXPR. Lest we perturb the type
1523 system too badly, we must take extra steps to ensure that the ADDR_EXPR
1524 and the addressed object continue to agree on types. */
1525 /* ??? We might could do better if we recognize
1533 canonicalize_addr_expr (tree* expr_p)
1535 tree expr = *expr_p;
1536 tree ctype = TREE_TYPE (expr);
1537 tree addr_expr = TREE_OPERAND (expr, 0);
1538 tree atype = TREE_TYPE (addr_expr);
1539 tree dctype, datype, ddatype, otype, obj_expr;
1541 /* Both cast and addr_expr types should be pointers. */
1542 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1545 /* The addr_expr type should be a pointer to an array. */
1546 datype = TREE_TYPE (atype);
1547 if (TREE_CODE (datype) != ARRAY_TYPE)
1550 /* Both cast and addr_expr types should address the same object type. */
1551 dctype = TREE_TYPE (ctype);
1552 ddatype = TREE_TYPE (datype);
1553 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1556 /* The addr_expr and the object type should match. */
1557 obj_expr = TREE_OPERAND (addr_expr, 0);
1558 otype = TREE_TYPE (obj_expr);
1559 if (!lang_hooks.types_compatible_p (otype, datype))
1562 /* All checks succeeded. Build a new node to merge the cast. */
1563 *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1566 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1567 underneath as appropriate. */
1569 static enum gimplify_status
1570 gimplify_conversion (tree *expr_p)
1572 /* Strip away as many useless type conversions as possible
1574 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1576 /* If we still have a conversion at the toplevel, then strip
1577 away all but the outermost conversion. */
1578 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1580 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1582 /* And remove the outermost conversion if it's useless. */
1583 if (tree_ssa_useless_type_conversion (*expr_p))
1584 *expr_p = TREE_OPERAND (*expr_p, 0);
1587 /* If we still have a conversion at the toplevel,
1588 then canonicalize some constructs. */
1589 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1591 tree sub = TREE_OPERAND (*expr_p, 0);
1593 /* If a NOP conversion is changing the type of a COMPONENT_REF
1594 expression, then canonicalize its type now in order to expose more
1595 redundant conversions. */
1596 if (TREE_CODE (sub) == COMPONENT_REF)
1597 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1599 /* If a NOP conversion is changing a pointer to array of foo
1600 to a pointer to foo, embed that change in the ADDR_EXPR. */
1601 else if (TREE_CODE (sub) == ADDR_EXPR)
1602 canonicalize_addr_expr (expr_p);
1608 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1610 static enum gimplify_status
1611 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1613 tree op1 = TREE_OPERAND (*expr_p, 0);
1614 tree op2 = TREE_OPERAND (*expr_p, 1);
1615 enum tree_code code;
1616 enum gimplify_status r0, r1;
1618 if (TREE_CODE (*expr_p) == MIN_EXPR)
1623 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1624 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1626 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1627 build (code, boolean_type_node, op1, op2),
1630 if (r0 == GS_ERROR || r1 == GS_ERROR)
1636 /* Build an expression for the address of T. Folds away INDIRECT_REF to
1637 avoid confusing the gimplify process. */
1640 build_addr_expr_with_type (tree t, tree ptrtype)
1642 if (TREE_CODE (t) == INDIRECT_REF)
1644 t = TREE_OPERAND (t, 0);
1645 if (TREE_TYPE (t) != ptrtype)
1646 t = build1 (NOP_EXPR, ptrtype, t);
1651 while (TREE_CODE (base) == COMPONENT_REF
1652 || TREE_CODE (base) == ARRAY_REF)
1653 base = TREE_OPERAND (base, 0);
1655 TREE_ADDRESSABLE (base) = 1;
1657 t = build1 (ADDR_EXPR, ptrtype, t);
1664 build_addr_expr (tree t)
1666 return build_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
1669 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1670 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1672 static enum gimplify_status
1673 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1675 tree array = TREE_OPERAND (*expr_p, 0);
1676 tree arrtype = TREE_TYPE (array);
1677 tree elttype = TREE_TYPE (arrtype);
1678 tree size = size_in_bytes (elttype);
1679 tree ptrtype = build_pointer_type (elttype);
1680 enum tree_code add_code = PLUS_EXPR;
1681 tree idx = TREE_OPERAND (*expr_p, 1);
1682 tree minidx, offset, addr, result;
1683 enum gimplify_status ret;
1685 /* If the array domain does not start at zero, apply the offset. */
1686 minidx = TYPE_DOMAIN (arrtype);
1689 minidx = TYPE_MIN_VALUE (minidx);
1690 if (minidx && !integer_zerop (minidx))
1692 idx = convert (TREE_TYPE (minidx), idx);
1693 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1697 /* If the index is negative -- a technically invalid situation now
1698 that we've biased the index back to zero -- then casting it to
1699 unsigned has ill effects. In particular, -1*4U/4U != -1.
1700 Represent this as a subtraction of a positive rather than addition
1701 of a negative. This will prevent any conversion back to ARRAY_REF
1702 from getting the wrong results from the division. */
1703 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1705 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1706 add_code = MINUS_EXPR;
1709 /* Pointer arithmetic must be done in sizetype. */
1710 idx = convert (sizetype, idx);
1712 /* Convert the index to a byte offset. */
1713 offset = size_binop (MULT_EXPR, size, idx);
1715 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1716 if (ret == GS_ERROR)
1719 addr = build_addr_expr_with_type (array, ptrtype);
1720 result = fold (build (add_code, ptrtype, addr, offset));
1721 *expr_p = build1 (INDIRECT_REF, elttype, result);
1726 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1727 node pointed by EXPR_P.
1730 : min_lval '[' val ']'
1732 | compound_lval '[' val ']'
1733 | compound_lval '.' ID
1735 This is not part of the original SIMPLE definition, which separates
1736 array and member references, but it seems reasonable to handle them
1737 together. Also, this way we don't run into problems with union
1738 aliasing; gcc requires that for accesses through a union to alias, the
1739 union reference must be explicit, which was not always the case when we
1740 were splitting up array and member refs.
1742 PRE_P points to the list where side effects that must happen before
1743 *EXPR_P should be stored.
1745 POST_P points to the list where side effects that must happen after
1746 *EXPR_P should be stored. */
1748 static enum gimplify_status
1749 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1750 tree *post_p, int want_lvalue)
1753 enum tree_code code;
1755 enum gimplify_status ret;
1757 #if defined ENABLE_CHECKING
1758 if (TREE_CODE (*expr_p) != ARRAY_REF
1759 && TREE_CODE (*expr_p) != COMPONENT_REF
1760 && TREE_CODE (*expr_p) != REALPART_EXPR
1761 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1765 code = ERROR_MARK; /* [GIMPLE] Avoid uninitialized use warning. */
1767 /* Create a stack of the subexpressions so later we can walk them in
1768 order from inner to outer. */
1769 VARRAY_TREE_INIT (stack, 10, "stack");
1772 TREE_CODE (*p) == ARRAY_REF
1773 || TREE_CODE (*p) == COMPONENT_REF
1774 || TREE_CODE (*p) == REALPART_EXPR
1775 || TREE_CODE (*p) == IMAGPART_EXPR;
1776 p = &TREE_OPERAND (*p, 0))
1778 code = TREE_CODE (*p);
1779 if (code == ARRAY_REF)
1781 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1782 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1783 /* If the size of the array elements is not constant,
1784 computing the offset is non-trivial, so expose it. */
1787 VARRAY_PUSH_TREE (stack, *p);
1790 /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1791 TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1792 to all the refs we've walked through.
1794 Gimplify the base, and then process each of the outer nodes from left
1796 ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1797 code != ARRAY_REF ? fb_either : fb_lvalue);
1799 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1801 tree t = VARRAY_TOP_TREE (stack);
1802 if (TREE_CODE (t) == ARRAY_REF)
1804 /* Gimplify the dimension. */
1805 enum gimplify_status tret;
1806 /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1807 Gimplify non-constant array indices into a temporary
1809 FIXME - The real fix is to gimplify post-modify
1810 expressions into a minimal gimple lvalue. However, that
1811 exposes bugs in alias analysis. The alias analyzer does
1812 not handle &PTR->FIELD very well. Will fix after the
1813 branch is merged into mainline (dnovillo 2004-05-03). */
1814 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1816 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1817 is_gimple_tmp_var, fb_rvalue);
1818 if (tret == GS_ERROR)
1822 recalculate_side_effects (t);
1826 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1827 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1829 canonicalize_component_ref (expr_p);
1830 ret = MIN (ret, GS_OK);
1836 /* Re-write the ARRAY_REF node pointed by EXPR_P.
1838 PRE_P points to the list where side effects that must happen before
1839 *EXPR_P should be stored.
1841 POST_P points to the list where side effects that must happen after
1842 *EXPR_P should be stored.
1844 FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1845 argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1846 ARRAY_REF should be extended. */
1848 static enum gimplify_status
1849 gimplify_array_ref (tree *expr_p, tree *pre_p,
1850 tree *post_p, int want_lvalue)
1852 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1853 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1854 /* If the size of the array elements is not constant,
1855 computing the offset is non-trivial, so expose it. */
1856 return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1858 /* Handle array and member refs together for now. When alias analysis
1859 improves, we may want to go back to handling them separately. */
1860 return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1863 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1865 PRE_P points to the list where side effects that must happen before
1866 *EXPR_P should be stored.
1868 POST_P points to the list where side effects that must happen after
1869 *EXPR_P should be stored.
1871 WANT_VALUE is nonzero iff we want to use the value of this expression
1872 in another expression. */
1874 static enum gimplify_status
1875 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1878 enum tree_code code;
1879 tree lhs, lvalue, rhs, t1;
1881 enum tree_code arith_code;
1882 enum gimplify_status ret;
1884 code = TREE_CODE (*expr_p);
1886 #if defined ENABLE_CHECKING
1887 if (code != POSTINCREMENT_EXPR
1888 && code != POSTDECREMENT_EXPR
1889 && code != PREINCREMENT_EXPR
1890 && code != PREDECREMENT_EXPR)
1894 /* Prefix or postfix? */
1895 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1896 /* Faster to treat as prefix if result is not used. */
1897 postfix = want_value;
1901 /* Add or subtract? */
1902 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1903 arith_code = PLUS_EXPR;
1905 arith_code = MINUS_EXPR;
1907 /* Gimplify the LHS into a GIMPLE lvalue. */
1908 lvalue = TREE_OPERAND (*expr_p, 0);
1909 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1910 if (ret == GS_ERROR)
1913 /* Extract the operands to the arithmetic operation. */
1915 rhs = TREE_OPERAND (*expr_p, 1);
1917 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1918 that as the result value and in the postqueue operation. */
1921 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1922 if (ret == GS_ERROR)
1926 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1927 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1931 gimplify_stmt (&t1);
1932 append_to_statement_list (t1, post_p);
1943 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
1946 : ID '(' arglist ')'
1952 PRE_P points to the list where side effects that must happen before
1953 *EXPR_P should be stored. */
1955 static enum gimplify_status
1956 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1960 enum gimplify_status ret;
1962 #if defined ENABLE_CHECKING
1963 if (TREE_CODE (*expr_p) != CALL_EXPR)
1967 /* For reliable diagnostics during inlining, it is necessary that
1968 every call_expr be annotated with file and line. */
1969 if (!EXPR_LOCUS (*expr_p))
1970 annotate_with_locus (*expr_p, input_location);
1972 /* This may be a call to a builtin function.
1974 Builtin function calls may be transformed into different
1975 (and more efficient) builtin function calls under certain
1976 circumstances. Unfortunately, gimplification can muck things
1977 up enough that the builtin expanders are not aware that certain
1978 transformations are still valid.
1980 So we attempt transformation/gimplification of the call before
1981 we gimplify the CALL_EXPR. At this time we do not manage to
1982 transform all calls in the same manner as the expanders do, but
1983 we do transform most of them. */
1984 decl = get_callee_fndecl (*expr_p);
1985 if (decl && DECL_BUILT_IN (decl))
1989 /* If it is allocation of stack, record the need to restore the memory
1990 when the enclosing bind_expr is exited. */
1991 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1992 gimplify_ctxp->save_stack = true;
1994 /* If it is restore of the stack, reset it, since it means we are
1995 regimplifying the bind_expr. Note that we use the fact that
1996 for try_finally_expr, try part is processed first. */
1997 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1998 gimplify_ctxp->save_stack = false;
2000 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2002 if (new && new != *expr_p)
2004 /* There was a transformation of this call which computes the
2005 same value, but in a more efficient way. Return and try
2012 /* There is a sequence point before the call, so any side effects in
2013 the calling expression must occur before the actual call. Force
2014 gimplify_expr to use an internal post queue. */
2015 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2016 is_gimple_val, fb_rvalue);
2018 if (PUSH_ARGS_REVERSED)
2019 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2020 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2021 arglist = TREE_CHAIN (arglist))
2023 enum gimplify_status t;
2025 /* There is a sequence point before a function call. Side effects in
2026 the argument list must occur before the actual call. So, when
2027 gimplifying arguments, force gimplify_expr to use an internal
2028 post queue which is then appended to the end of PRE_P. */
2029 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2035 if (PUSH_ARGS_REVERSED)
2036 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2038 /* Try this again in case gimplification exposed something. */
2039 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2041 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2043 if (new && new != *expr_p)
2045 /* There was a transformation of this call which computes the
2046 same value, but in a more efficient way. Return and try
2053 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2054 decl. This allows us to eliminate redundant or useless
2055 calls to "const" functions. */
2056 if (TREE_CODE (*expr_p) == CALL_EXPR
2057 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2058 TREE_SIDE_EFFECTS (*expr_p) = 0;
2063 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2064 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2066 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2067 condition is true or false, respectively. If null, we should generate
2068 our own to skip over the evaluation of this specific expression.
2070 This function is the tree equivalent of do_jump.
2072 shortcut_cond_r should only be called by shortcut_cond_expr. */
2075 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2077 tree local_label = NULL_TREE;
2078 tree t, expr = NULL;
2080 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2081 retain the shortcut semantics. Just insert the gotos here;
2082 shortcut_cond_expr will append the real blocks later. */
2083 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2085 /* Turn if (a && b) into
2087 if (a); else goto no;
2088 if (b) goto yes; else goto no;
2091 if (false_label_p == NULL)
2092 false_label_p = &local_label;
2094 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2095 append_to_statement_list (t, &expr);
2097 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2099 append_to_statement_list (t, &expr);
2101 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2103 /* Turn if (a || b) into
2106 if (b) goto yes; else goto no;
2109 if (true_label_p == NULL)
2110 true_label_p = &local_label;
2112 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2113 append_to_statement_list (t, &expr);
2115 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2117 append_to_statement_list (t, &expr);
2119 else if (TREE_CODE (pred) == COND_EXPR)
2121 /* As long as we're messing with gotos, turn if (a ? b : c) into
2123 if (b) goto yes; else goto no;
2125 if (c) goto yes; else goto no; */
2126 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2127 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2129 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2134 expr = build (COND_EXPR, void_type_node, pred,
2135 build_and_jump (true_label_p),
2136 build_and_jump (false_label_p));
2141 t = build1 (LABEL_EXPR, void_type_node, local_label);
2142 append_to_statement_list (t, &expr);
2149 shortcut_cond_expr (tree expr)
2151 tree pred = TREE_OPERAND (expr, 0);
2152 tree then_ = TREE_OPERAND (expr, 1);
2153 tree else_ = TREE_OPERAND (expr, 2);
2154 tree true_label, false_label, end_label, t;
2156 tree *false_label_p;
2157 bool emit_end, emit_false;
2159 /* First do simple transformations. */
2160 if (!TREE_SIDE_EFFECTS (else_))
2162 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2163 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2165 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2166 then_ = shortcut_cond_expr (expr);
2167 pred = TREE_OPERAND (pred, 0);
2168 expr = build (COND_EXPR, void_type_node, pred, then_,
2169 build_empty_stmt ());
2172 if (!TREE_SIDE_EFFECTS (then_))
2174 /* If there is no 'then', turn
2177 if (a); else if (b); else d. */
2178 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2180 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2181 else_ = shortcut_cond_expr (expr);
2182 pred = TREE_OPERAND (pred, 0);
2183 expr = build (COND_EXPR, void_type_node, pred,
2184 build_empty_stmt (), else_);
2188 /* If we're done, great. */
2189 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2190 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2193 /* Otherwise we need to mess with gotos. Change
2196 if (a); else goto no;
2199 and recursively gimplify the condition. */
2201 true_label = false_label = end_label = NULL_TREE;
2203 /* If our arms just jump somewhere, hijack those labels so we don't
2204 generate jumps to jumps. */
2206 if (TREE_CODE (then_) == GOTO_EXPR
2207 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2209 true_label = GOTO_DESTINATION (then_);
2210 then_ = build_empty_stmt ();
2213 if (TREE_CODE (else_) == GOTO_EXPR
2214 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2216 false_label = GOTO_DESTINATION (else_);
2217 else_ = build_empty_stmt ();
2220 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2222 true_label_p = &true_label;
2224 true_label_p = NULL;
2226 /* The 'else' branch also needs a label if it contains interesting code. */
2227 if (false_label || TREE_SIDE_EFFECTS (else_))
2228 false_label_p = &false_label;
2230 false_label_p = NULL;
2232 /* If there was nothing else in our arms, just forward the label(s). */
2233 if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2234 return shortcut_cond_r (pred, true_label_p, false_label_p);
2236 /* If our last subexpression already has a terminal label, reuse it. */
2237 if (TREE_SIDE_EFFECTS (else_))
2238 expr = expr_last (else_);
2240 expr = expr_last (then_);
2241 if (TREE_CODE (expr) == LABEL_EXPR)
2242 end_label = LABEL_EXPR_LABEL (expr);
2244 /* If we don't care about jumping to the 'else' branch, jump to the end
2245 if the condition is false. */
2247 false_label_p = &end_label;
2249 /* We only want to emit these labels if we aren't hijacking them. */
2250 emit_end = (end_label == NULL_TREE);
2251 emit_false = (false_label == NULL_TREE);
2253 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2256 append_to_statement_list (pred, &expr);
2258 append_to_statement_list (then_, &expr);
2259 if (TREE_SIDE_EFFECTS (else_))
2261 t = build_and_jump (&end_label);
2262 append_to_statement_list (t, &expr);
2265 t = build1 (LABEL_EXPR, void_type_node, false_label);
2266 append_to_statement_list (t, &expr);
2268 append_to_statement_list (else_, &expr);
2270 if (emit_end && end_label)
2272 t = build1 (LABEL_EXPR, void_type_node, end_label);
2273 append_to_statement_list (t, &expr);
2279 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2282 gimple_boolify (tree expr)
2284 tree type = TREE_TYPE (expr);
2286 if (TREE_CODE (type) == BOOLEAN_TYPE)
2289 /* If this is the predicate of a COND_EXPR, it might not even be a
2291 expr = lang_hooks.truthvalue_conversion (expr);
2293 switch (TREE_CODE (expr))
2295 case TRUTH_AND_EXPR:
2297 case TRUTH_XOR_EXPR:
2298 case TRUTH_ANDIF_EXPR:
2299 case TRUTH_ORIF_EXPR:
2300 /* Also boolify the arguments of truth exprs. */
2301 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2304 case TRUTH_NOT_EXPR:
2305 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2308 case EQ_EXPR: case NE_EXPR:
2309 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2310 /* These expressions always produce boolean results. */
2311 TREE_TYPE (expr) = boolean_type_node;
2315 /* Other expressions that get here must have boolean values, but
2316 might need to be converted to the appropriate mode. */
2317 return convert (boolean_type_node, expr);
2321 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2330 The second form is used when *EXPR_P is of type void.
2332 PRE_P points to the list where side effects that must happen before
2333 *EXPR_P should be stored. */
2335 static enum gimplify_status
2336 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2338 tree expr = *expr_p;
2340 enum gimplify_status ret;
2342 /* If this COND_EXPR has a value, copy the values into a temporary within
2344 if (! VOID_TYPE_P (TREE_TYPE (expr)))
2353 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2357 /* Build the then clause, 't1 = a;'. But don't build an assignment
2358 if this branch is void; in C++ it can be, if it's a throw. */
2359 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2360 TREE_OPERAND (expr, 1)
2361 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2363 /* Build the else clause, 't1 = b;'. */
2364 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2365 TREE_OPERAND (expr, 2)
2366 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2368 TREE_TYPE (expr) = void_type_node;
2369 recalculate_side_effects (expr);
2371 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2372 gimplify_stmt (&expr);
2373 append_to_statement_list (expr, pre_p);
2379 /* Make sure the condition has BOOLEAN_TYPE. */
2380 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2382 /* Break apart && and || conditions. */
2383 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2384 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2386 expr = shortcut_cond_expr (expr);
2388 if (expr != *expr_p)
2392 /* We can't rely on gimplify_expr to re-gimplify the expanded
2393 form properly, as cleanups might cause the target labels to be
2394 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2395 set up a conditional context. */
2396 gimple_push_condition ();
2397 gimplify_stmt (expr_p);
2398 gimple_pop_condition (pre_p);
2404 /* Now do the normal gimplification. */
2405 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2406 is_gimple_condexpr, fb_rvalue);
2408 gimple_push_condition ();
2410 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2411 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2412 recalculate_side_effects (expr);
2414 gimple_pop_condition (pre_p);
2416 if (ret == GS_ERROR)
2418 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2420 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2421 /* Rewrite "if (a); else b" to "if (!a) b" */
2423 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2424 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2425 is_gimple_condexpr, fb_rvalue);
2427 tmp = TREE_OPERAND (expr, 1);
2428 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2429 TREE_OPERAND (expr, 2) = tmp;
2432 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2433 expr = TREE_OPERAND (expr, 0);
2439 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2445 PRE_P points to the list where side effects that must happen before
2446 *EXPR_P should be stored.
2448 POST_P points to the list where side effects that must happen after
2449 *EXPR_P should be stored.
2451 WANT_VALUE is nonzero iff we want to use the value of this expression
2452 in another expression. */
2454 static enum gimplify_status
2455 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2457 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2458 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2459 enum gimplify_status ret;
2461 #if defined ENABLE_CHECKING
2462 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2466 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2467 if (TREE_CODE (*expr_p) == INIT_EXPR)
2468 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2470 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2471 if (ret == GS_ERROR)
2474 /* If we are initializing something from a TARGET_EXPR, strip the
2475 TARGET_EXPR and initialize it directly. */
2476 /* What about code that pulls out the temp and uses it elsewhere? I
2477 think that such code never uses the TARGET_EXPR as an initializer. If
2478 I'm wrong, we'll abort because the temp won't have any RTL. In that
2479 case, I guess we'll need to replace references somehow. */
2480 if (TREE_CODE (*from_p) == TARGET_EXPR)
2481 *from_p = TARGET_EXPR_INITIAL (*from_p);
2483 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2484 the assignment down into the branches, since we can't generate a
2485 temporary of such a type. */
2486 if (TREE_CODE (*from_p) == COND_EXPR
2487 && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2490 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2493 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2494 if (ret == GS_ERROR)
2497 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2498 if (ret != GS_UNHANDLED)
2501 /* If the destination is already simple, nothing else needed. */
2502 if (is_gimple_tmp_var (*to_p))
2506 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2507 the LHS is a user variable, then we need to introduce a temporary.
2508 ie temp = RHS; LHS = temp.
2510 This way the optimizers can determine that the user variable is
2511 only modified if evaluation of the RHS does not throw.
2513 FIXME this should be handled by the is_gimple_rhs predicate. */
2515 if (TREE_CODE (*from_p) == CALL_EXPR
2516 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2517 /* If we're dealing with a renamable type, either source or dest
2518 must be a renamed variable. */
2519 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2520 && !is_gimple_reg (*to_p)))
2521 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2523 /* If the value being copied is of variable width, expose the length
2524 if the copy by converting the whole thing to a memcpy. */
2525 /* ??? Except that we can't manage this with VA_ARG_EXPR. Yes, this
2526 does leave us with an edge condition that doesn't work. The only
2527 way out is to rearrange how VA_ARG_EXPR works. */
2528 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2529 && TREE_CODE (*from_p) != VA_ARG_EXPR)
2533 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2534 t = unshare_expr (t);
2535 args = tree_cons (NULL, t, NULL);
2536 t = build_addr_expr (*from_p);
2537 args = tree_cons (NULL, t, args);
2538 dest = build_addr_expr (*to_p);
2539 args = tree_cons (NULL, dest, args);
2540 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2541 t = build_function_call_expr (t, args);
2544 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2545 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2552 ret = want_value ? GS_OK : GS_ALL_DONE;
2557 append_to_statement_list (*expr_p, pre_p);
2564 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2565 points to the expression to gimplify.
2567 Expressions of the form 'a && b' are gimplified to:
2569 a && b ? true : false
2571 gimplify_cond_expr will do the rest.
2573 PRE_P points to the list where side effects that must happen before
2574 *EXPR_P should be stored. */
2576 static enum gimplify_status
2577 gimplify_boolean_expr (tree *expr_p)
2579 /* Preserve the original type of the expression. */
2580 tree type = TREE_TYPE (*expr_p);
2582 *expr_p = build (COND_EXPR, type, *expr_p,
2583 convert (type, boolean_true_node),
2584 convert (type, boolean_false_node));
2589 /* Gimplifies an expression sequence. This function gimplifies each
2590 expression and re-writes the original expression with the last
2591 expression of the sequence in GIMPLE form.
2593 PRE_P points to the list where the side effects for all the
2594 expressions in the sequence will be emitted.
2596 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2597 /* ??? Should rearrange to share the pre-queue with all the indirect
2598 invocations of gimplify_expr. Would probably save on creations
2599 of statement_list nodes. */
2601 static enum gimplify_status
2602 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2608 tree *sub_p = &TREE_OPERAND (t, 0);
2610 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2611 gimplify_compound_expr (sub_p, pre_p, false);
2613 gimplify_stmt (sub_p);
2614 append_to_statement_list (*sub_p, pre_p);
2616 t = TREE_OPERAND (t, 1);
2618 while (TREE_CODE (t) == COMPOUND_EXPR);
2625 gimplify_stmt (expr_p);
2630 /* Gimplifies a statement list. These may be created either by an
2631 enlightened front-end, or by shortcut_cond_expr. */
2633 static enum gimplify_status
2634 gimplify_statement_list (tree *expr_p)
2636 tree_stmt_iterator i = tsi_start (*expr_p);
2638 while (!tsi_end_p (i))
2642 gimplify_stmt (tsi_stmt_ptr (i));
2645 if (TREE_CODE (t) == STATEMENT_LIST)
2647 tsi_link_before (&i, t, TSI_SAME_STMT);
2657 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2658 gimplify. After gimplification, EXPR_P will point to a new temporary
2659 that holds the original value of the SAVE_EXPR node.
2661 PRE_P points to the list where side effects that must happen before
2662 *EXPR_P should be stored. */
2664 static enum gimplify_status
2665 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2667 enum gimplify_status ret = GS_ALL_DONE;
2670 #if defined ENABLE_CHECKING
2671 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2675 val = TREE_OPERAND (*expr_p, 0);
2677 /* If the operand is already a GIMPLE temporary, just re-write the
2679 if (is_gimple_tmp_var (val))
2681 /* The operand may be a void-valued expression such as SAVE_EXPRs
2682 generated by the Java frontend for class initialization. It is
2683 being executed only for its side-effects. */
2684 else if (TREE_TYPE (val) == void_type_node)
2686 tree body = TREE_OPERAND (*expr_p, 0);
2687 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2688 append_to_statement_list (body, pre_p);
2689 *expr_p = build_empty_stmt ();
2692 *expr_p = TREE_OPERAND (*expr_p, 0)
2693 = get_initialized_tmp_var (val, pre_p, post_p);
2698 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2705 PRE_P points to the list where side effects that must happen before
2706 *EXPR_P should be stored.
2708 POST_P points to the list where side effects that must happen after
2709 *EXPR_P should be stored. */
2711 static enum gimplify_status
2712 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2714 tree expr = *expr_p;
2715 tree op0 = TREE_OPERAND (expr, 0);
2716 enum gimplify_status ret;
2718 switch (TREE_CODE (op0))
2721 /* Check if we are dealing with an expression of the form '&*ptr'.
2722 While the front end folds away '&*ptr' into 'ptr', these
2723 expressions may be generated internally by the compiler (e.g.,
2724 builtins like __builtin_va_end). */
2725 *expr_p = TREE_OPERAND (op0, 0);
2730 /* Fold &a[6] to (&a + 6). */
2731 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2734 /* This added an INDIRECT_REF. Fold it away. */
2735 op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2741 /* We use fb_either here because the C frontend sometimes takes
2742 the address of a call that returns a struct. */
2743 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2744 is_gimple_addr_expr_arg, fb_either);
2745 if (ret != GS_ERROR)
2747 /* At this point, the argument of the ADDR_EXPR should be
2748 sufficiently simple that there are never side effects. */
2749 /* ??? Could split out the decision code from build1 to verify. */
2750 TREE_SIDE_EFFECTS (expr) = 0;
2752 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2753 recompute_tree_invarant_for_addr_expr (expr);
2755 /* Mark the RHS addressable. */
2756 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2761 /* If the operand is gimplified into a _DECL, mark the address expression
2762 as TREE_INVARIANT. */
2763 if (DECL_P (TREE_OPERAND (expr, 0)))
2764 TREE_INVARIANT (expr) = 1;
2769 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2770 value; output operands should be a gimple lvalue. */
2772 static enum gimplify_status
2773 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2775 tree expr = *expr_p;
2776 int noutputs = list_length (ASM_OUTPUTS (expr));
2777 const char **oconstraints
2778 = (const char **) alloca ((noutputs) * sizeof (const char *));
2781 const char *constraint;
2782 bool allows_mem, allows_reg, is_inout;
2783 enum gimplify_status ret, tret;
2786 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2790 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2792 oconstraints[i] = constraint
2793 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2795 parse_output_constraint (&constraint, i, 0, 0,
2796 &allows_mem, &allows_reg, &is_inout);
2798 if (!allows_reg && allows_mem)
2799 lang_hooks.mark_addressable (TREE_VALUE (link));
2801 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2802 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2803 fb_lvalue | fb_mayfail);
2804 if (tret == GS_ERROR)
2806 error ("invalid lvalue in asm output %d", i);
2812 /* An input/output operand. To give the optimizers more
2813 flexibility, split it into separate input and output
2817 size_t constraint_len = strlen (constraint);
2819 /* Turn the in/out constraint into an output constraint. */
2820 char *p = xstrdup (constraint);
2822 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2825 /* And add a matching input constraint. */
2828 sprintf (buf, "%d", i);
2829 input = build_string (strlen (buf), buf);
2832 input = build_string (constraint_len - 1, constraint + 1);
2833 input = build_tree_list (build_tree_list (NULL_TREE, input),
2834 unshare_expr (TREE_VALUE (link)));
2835 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2839 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2842 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2843 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2844 oconstraints, &allows_mem, &allows_reg);
2846 /* If the operand is a memory input, it should be an lvalue. */
2847 if (!allows_reg && allows_mem)
2849 lang_hooks.mark_addressable (TREE_VALUE (link));
2850 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2851 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2852 if (tret == GS_ERROR)
2854 error ("memory input %d is not directly addressable", i);
2860 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2861 is_gimple_val, fb_rvalue);
2862 if (tret == GS_ERROR)
2870 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
2871 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2872 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2873 return to this function.
2875 FIXME should we complexify the prequeue handling instead? Or use flags
2876 for all the cleanups and let the optimizer tighten them up? The current
2877 code seems pretty fragile; it will break on a cleanup within any
2878 non-conditional nesting. But any such nesting would be broken, anyway;
2879 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2880 and continues out of it. We can do that at the RTL level, though, so
2881 having an optimizer to tighten up try/finally regions would be a Good
2884 static enum gimplify_status
2885 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2887 tree_stmt_iterator iter;
2890 tree temp = voidify_wrapper_expr (*expr_p);
2892 /* We only care about the number of conditions between the innermost
2893 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
2894 int old_conds = gimplify_ctxp->conditions;
2895 gimplify_ctxp->conditions = 0;
2897 body = TREE_OPERAND (*expr_p, 0);
2898 gimplify_to_stmt_list (&body);
2900 gimplify_ctxp->conditions = old_conds;
2902 for (iter = tsi_start (body); !tsi_end_p (iter); )
2904 tree *wce_p = tsi_stmt_ptr (iter);
2907 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2909 if (tsi_one_before_end_p (iter))
2911 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2919 sl = tsi_split_statement_list_after (&iter);
2920 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2921 append_to_statement_list (TREE_OPERAND (wce, 1),
2922 &TREE_OPERAND (tfe, 1));
2924 iter = tsi_start (sl);
2934 append_to_statement_list (body, pre_p);
2944 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
2945 is the cleanup action required. */
2948 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2952 /* Errors can result in improperly nested cleanups. Which results in
2953 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
2954 if (errorcount || sorrycount)
2957 if (gimple_conditional_context ())
2959 /* If we're in a conditional context, this is more complex. We only
2960 want to run the cleanup if we actually ran the initialization that
2961 necessitates it, but we want to run it after the end of the
2962 conditional context. So we wrap the try/finally around the
2963 condition and use a flag to determine whether or not to actually
2964 run the destructor. Thus
2968 becomes (approximately)
2972 if (test) { A::A(temp); flag = 1; val = f(temp); }
2975 if (flag) A::~A(temp);
2980 tree flag = create_tmp_var (boolean_type_node, "cleanup");
2981 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2982 boolean_false_node);
2983 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2985 cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2986 build_empty_stmt ());
2987 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2988 cleanup, NULL_TREE);
2989 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2990 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2991 append_to_statement_list (ftrue, pre_p);
2993 /* Because of this manipulation, and the EH edges that jump
2994 threading cannot redirect, the temporary (VAR) will appear
2995 to be used uninitialized. Don't warn. */
2996 TREE_NO_WARNING (var) = 1;
3000 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3001 cleanup, NULL_TREE);
3002 append_to_statement_list (wce, pre_p);
3005 gimplify_stmt (&TREE_OPERAND (wce, 1));
3008 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3010 static enum gimplify_status
3011 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3013 tree targ = *expr_p;
3014 tree temp = TARGET_EXPR_SLOT (targ);
3015 tree init = TARGET_EXPR_INITIAL (targ);
3016 enum gimplify_status ret;
3020 /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
3022 gimple_add_tmp_var (temp);
3024 /* Build up the initialization and add it to pre_p. */
3025 init = build (MODIFY_EXPR, void_type_node, temp, init);
3026 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3027 if (ret == GS_ERROR)
3030 append_to_statement_list (init, pre_p);
3032 /* If needed, push the cleanup for the temp. */
3033 if (TARGET_EXPR_CLEANUP (targ))
3035 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3036 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3039 /* Only expand this once. */
3040 TREE_OPERAND (targ, 3) = init;
3041 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3043 else if (!temp->decl.seen_in_bind_expr)
3044 /* We should have expanded this before. */
3051 /* Gimplification of expression trees. */
3053 /* Gimplify an expression which appears at statement context; usually, this
3054 means replacing it with a suitably gimple STATEMENT_LIST. */
3057 gimplify_stmt (tree *stmt_p)
3059 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3061 *stmt_p = alloc_stmt_list ();
3064 /* Similarly, but force the result to be a STATEMENT_LIST. */
3067 gimplify_to_stmt_list (tree *stmt_p)
3069 gimplify_stmt (stmt_p);
3070 if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3073 *stmt_p = alloc_stmt_list ();
3074 append_to_statement_list (t, stmt_p);
3079 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3080 gimplification failed.
3082 PRE_P points to the list where side effects that must happen before
3083 EXPR should be stored.
3085 POST_P points to the list where side effects that must happen after
3086 EXPR should be stored, or NULL if there is no suitable list. In
3087 that case, we copy the result to a temporary, emit the
3088 post-effects, and then return the temporary.
3090 GIMPLE_TEST_F points to a function that takes a tree T and
3091 returns nonzero if T is in the GIMPLE form requested by the
3092 caller. The GIMPLE predicates are in tree-gimple.c.
3094 This test is used twice. Before gimplification, the test is
3095 invoked to determine whether *EXPR_P is already gimple enough. If
3096 that fails, *EXPR_P is gimplified according to its code and
3097 GIMPLE_TEST_F is called again. If the test still fails, then a new
3098 temporary variable is created and assigned the value of the
3099 gimplified expression.
3101 FALLBACK tells the function what sort of a temporary we want. If the 1
3102 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3103 If both are set, either is OK, but an lvalue is preferable.
3105 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3106 iterates until solution. */
3108 enum gimplify_status
3109 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3110 bool (* gimple_test_f) (tree), fallback_t fallback)
3113 tree internal_pre = NULL_TREE;
3114 tree internal_post = NULL_TREE;
3116 int is_statement = (pre_p == NULL);
3118 location_t saved_location;
3119 enum gimplify_status ret;
3121 save_expr = *expr_p;
3122 if (save_expr == NULL_TREE)
3125 /* We used to check the predicate here and return immediately if it
3126 succeeds. This is wrong; the design is for gimplification to be
3127 idempotent, and for the predicates to only test for valid forms, not
3128 whether they are fully simplified. */
3130 /* Set up our internal queues if needed. */
3132 pre_p = &internal_pre;
3134 post_p = &internal_post;
3136 saved_location = input_location;
3137 if (save_expr == error_mark_node)
3140 locus = EXPR_LOCUS (save_expr);
3142 input_location = *locus;
3144 /* Loop over the specific gimplifiers until the toplevel node
3145 remains the same. */
3148 /* Strip any uselessness. */
3149 STRIP_MAIN_TYPE_NOPS (*expr_p);
3151 /* Remember the expr. */
3152 save_expr = *expr_p;
3154 /* Die, die, die, my darling. */
3155 if (save_expr == error_mark_node
3156 || TREE_TYPE (save_expr) == error_mark_node)
3162 /* Do any language-specific gimplification. */
3163 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3166 if (*expr_p == NULL_TREE)
3168 if (*expr_p != save_expr)
3171 else if (ret != GS_UNHANDLED)
3175 switch (TREE_CODE (*expr_p))
3177 /* First deal with the special cases. */
3179 case POSTINCREMENT_EXPR:
3180 case POSTDECREMENT_EXPR:
3181 case PREINCREMENT_EXPR:
3182 case PREDECREMENT_EXPR:
3183 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3184 fallback != fb_none);
3188 ret = gimplify_array_ref (expr_p, pre_p, post_p,
3189 fallback & fb_lvalue);
3193 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3194 fallback & fb_lvalue);
3198 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3202 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3209 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3214 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3215 fallback & fb_lvalue);
3220 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3221 fallback != fb_none);
3224 case TRUTH_ANDIF_EXPR:
3225 case TRUTH_ORIF_EXPR:
3226 ret = gimplify_boolean_expr (expr_p);
3229 case TRUTH_NOT_EXPR:
3230 TREE_OPERAND (*expr_p, 0)
3231 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3232 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3233 is_gimple_val, fb_rvalue);
3234 recalculate_side_effects (*expr_p);
3238 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3242 /* Mark any _DECL inside the operand as volatile to avoid the
3243 optimizers messing around with it. FIXME: Remove this once
3244 VA_ARG_EXPRs are properly lowered. */
3245 walk_tree (&TREE_OPERAND (*expr_p, 0), mark_decls_volatile_r,
3248 /* va_arg expressions are in GIMPLE form already. */
3254 if (IS_EMPTY_STMT (*expr_p))
3260 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3261 || fallback == fb_none)
3263 /* Just strip a conversion to void (or in void context) and
3265 *expr_p = TREE_OPERAND (*expr_p, 0);
3269 ret = gimplify_conversion (expr_p);
3270 if (ret == GS_ERROR)
3272 if (*expr_p != save_expr)
3276 case FIX_TRUNC_EXPR:
3278 case FIX_FLOOR_EXPR:
3279 case FIX_ROUND_EXPR:
3280 /* unary_expr: ... | '(' cast ')' val | ... */
3281 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3282 is_gimple_val, fb_rvalue);
3283 recalculate_side_effects (*expr_p);
3287 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3288 is_gimple_reg, fb_rvalue);
3289 recalculate_side_effects (*expr_p);
3292 /* Constants need not be gimplified. */
3302 *expr_p = DECL_INITIAL (*expr_p);
3306 /* FIXME make this a decl. */
3311 ret = gimplify_bind_expr (expr_p, pre_p);
3315 ret = gimplify_loop_expr (expr_p, pre_p);
3319 ret = gimplify_switch_expr (expr_p, pre_p);
3322 case LABELED_BLOCK_EXPR:
3323 ret = gimplify_labeled_block_expr (expr_p);
3326 case EXIT_BLOCK_EXPR:
3327 ret = gimplify_exit_block_expr (expr_p);
3331 ret = gimplify_exit_expr (expr_p);
3335 /* If the target is not LABEL, then it is a computed jump
3336 and the target needs to be gimplified. */
3337 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3338 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3339 NULL, is_gimple_val, fb_rvalue);
3344 #ifdef ENABLE_CHECKING
3345 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3350 case CASE_LABEL_EXPR:
3351 ret = gimplify_case_label_expr (expr_p);
3355 ret = gimplify_return_expr (*expr_p, pre_p);
3359 /* Don't reduce this in place; let gimplify_init_constructor work
3364 /* The following are special cases that are not handled by the
3365 original GIMPLE grammar. */
3367 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3370 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3375 enum gimplify_status r0, r1, r2;
3377 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3378 is_gimple_min_lval, fb_either);
3379 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3380 is_gimple_val, fb_rvalue);
3381 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3382 is_gimple_val, fb_rvalue);
3383 recalculate_side_effects (*expr_p);
3385 ret = MIN (r0, MIN (r1, r2));
3389 case NON_LVALUE_EXPR:
3390 /* This should have been stripped above. */
3395 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3398 case TRY_FINALLY_EXPR:
3399 case TRY_CATCH_EXPR:
3400 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3401 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3405 case CLEANUP_POINT_EXPR:
3406 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3410 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3414 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3418 case EH_FILTER_EXPR:
3419 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3424 /* This moves much of the actual computation out of the
3425 VTABLE_REF. Perhaps this should be revisited once we want to
3426 do clever things with VTABLE_REFs. */
3427 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3428 is_gimple_min_lval, fb_lvalue);
3433 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3437 /* We get here when taking the address of a label. We mark
3438 the label as "forced"; meaning it can never be removed and
3439 it is a potential target for any computed goto. */
3440 FORCED_LABEL (*expr_p) = 1;
3444 case STATEMENT_LIST:
3445 ret = gimplify_statement_list (expr_p);
3449 /* ??? If this is a local variable, and it has not been seen in any
3450 outer BIND_EXPR, then it's probably the result of a duplicate
3451 declaration, for which we've already issued an error. It would
3452 be really nice if the front end wouldn't leak these at all.
3453 Currently the only known culprit is C++ destructors, as seen
3454 in g++.old-deja/g++.jason/binding.C. */
3456 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3457 && decl_function_context (tmp) == current_function_decl
3458 && !tmp->decl.seen_in_bind_expr)
3460 #ifdef ENABLE_CHECKING
3461 if (!errorcount && !sorrycount)
3471 /* If *EXPR_P does not need to be special-cased, handle it
3472 according to its class. */
3473 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3474 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3475 post_p, is_gimple_val, fb_rvalue);
3476 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3477 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3478 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3479 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3480 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3482 enum gimplify_status r0, r1;
3484 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3485 post_p, is_gimple_val, fb_rvalue);
3486 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3487 post_p, is_gimple_val, fb_rvalue);
3491 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3492 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3498 /* Fail if we don't know how to handle this tree code. */
3501 recalculate_side_effects (*expr_p);
3505 /* If we replaced *expr_p, gimplify again. */
3506 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3509 while (ret == GS_OK);
3511 /* If we encountered an error_mark somewhere nested inside, either
3512 stub out the statement or propagate the error back out. */
3513 if (ret == GS_ERROR)
3516 *expr_p = build_empty_stmt ();
3520 #ifdef ENABLE_CHECKING
3521 /* This was only valid as a return value from the langhook, which
3522 we handled. Make sure it doesn't escape from any other context. */
3523 if (ret == GS_UNHANDLED)
3528 *expr_p = build_empty_stmt ();
3529 if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3531 /* We aren't looking for a value, and we don't have a valid
3532 statement. If it doesn't have side-effects, throw it away. */
3533 if (!TREE_SIDE_EFFECTS (*expr_p))
3534 *expr_p = build_empty_stmt ();
3535 else if (!TREE_THIS_VOLATILE (*expr_p))
3536 /* We only handle volatiles here; anything else with side-effects
3537 must be converted to a valid statement before we get here. */
3539 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3541 /* Historically, the compiler has treated a bare
3542 reference to a volatile lvalue as forcing a load. */
3543 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3544 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3547 /* We can't do anything useful with a volatile reference to
3548 incomplete type, so just throw it away. */
3549 *expr_p = build_empty_stmt ();
3552 /* If we are gimplifying at the statement level, we're done. Tack
3553 everything together and replace the original statement with the
3557 if (internal_pre || internal_post)
3559 append_to_statement_list (*expr_p, &internal_pre);
3560 append_to_statement_list (internal_post, &internal_pre);
3561 annotate_all_with_locus (&internal_pre, input_location);
3562 *expr_p = internal_pre;
3567 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3570 /* If it's sufficiently simple already, we're done. Unless we are
3571 handling some post-effects internally; if that's the case, we need to
3572 copy into a temp before adding the post-effects to the tree. */
3573 if (!internal_post && (*gimple_test_f) (*expr_p))
3576 /* Otherwise, we need to create a new temporary for the gimplified
3579 /* We can't return an lvalue if we have an internal postqueue. The
3580 object the lvalue refers to would (probably) be modified by the
3581 postqueue; we need to copy the value out first, which means an
3583 if ((fallback & fb_lvalue) && !internal_post
3584 && is_gimple_addr_expr_arg (*expr_p))
3586 /* An lvalue will do. Take the address of the expression, store it
3587 in a temporary, and replace the expression with an INDIRECT_REF of
3589 tmp = build_addr_expr (*expr_p);
3590 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3591 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3593 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3595 #if defined ENABLE_CHECKING
3596 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3600 /* An rvalue will do. Assign the gimplified expression into a new
3601 temporary TMP and replace the original expression with TMP. */
3603 if (internal_post || (fallback & fb_lvalue))
3604 /* The postqueue might change the value of the expression between
3605 the initialization and use of the temporary, so we can't use a
3606 formal temp. FIXME do we care? */
3607 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3609 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3611 else if (fallback & fb_mayfail)
3613 /* If this is an asm statement, and the user asked for the impossible,
3614 don't abort. Fail and let gimplify_asm_expr issue an error. */
3620 fprintf (stderr, "gimplification failed:\n");
3621 print_generic_expr (stderr, *expr_p, 0);
3622 debug_tree (*expr_p);
3626 #if defined ENABLE_CHECKING
3627 /* Make sure the temporary matches our predicate. */
3628 if (!(*gimple_test_f) (*expr_p))
3634 annotate_all_with_locus (&internal_post, input_location);
3635 append_to_statement_list (internal_post, pre_p);
3639 input_location = saved_location;
3643 #ifdef ENABLE_CHECKING
3644 /* Compare types A and B for a "close enough" match. */
3647 cpt_same_type (tree a, tree b)
3649 if (lang_hooks.types_compatible_p (a, b))
3652 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3653 link them together. This routine is intended to catch type errors
3654 that will affect the optimizers, and the optimizers don't add new
3655 dereferences of function pointers, so ignore it. */
3656 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3657 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3660 /* ??? The C FE pushes type qualifiers after the fact into the type of
3661 the element from the type of the array. See build_unary_op's handling
3662 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3663 should have done it when creating the variable in the first place.
3664 Alternately, why aren't the two array types made variants? */
3665 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3666 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3668 /* And because of those, we have to recurse down through pointers. */
3669 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3670 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3675 /* Check for some cases of the front end missing cast expressions.
3676 The type of a dereference should correspond to the pointer type;
3677 similarly the type of an address should match its object. */
3680 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3681 void *data ATTRIBUTE_UNUSED)
3684 tree ptype, otype, dtype;
3686 switch (TREE_CODE (t))
3690 otype = TREE_TYPE (t);
3691 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3692 dtype = TREE_TYPE (ptype);
3693 if (!cpt_same_type (otype, dtype))
3698 ptype = TREE_TYPE (t);
3699 otype = TREE_TYPE (TREE_OPERAND (t, 0));
3700 dtype = TREE_TYPE (ptype);
3701 if (!cpt_same_type (otype, dtype))
3703 /* &array is allowed to produce a pointer to the element,
3704 rather than a pointer to the array type. */
3705 if (TREE_CODE (otype) == ARRAY_TYPE
3706 && POINTER_TYPE_P (ptype)
3707 && cpt_same_type (TREE_TYPE (otype), dtype))
3722 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
3723 function decl containing BODY. */
3726 gimplify_body (tree *body_p, tree fndecl)
3728 location_t saved_location = input_location;
3731 timevar_push (TV_TREE_GIMPLIFY);
3732 push_gimplify_context ();
3734 /* Unshare most shared trees in the body. */
3735 unshare_all_trees (*body_p);
3737 /* Make sure input_location isn't set to something wierd. */
3738 input_location = DECL_SOURCE_LOCATION (fndecl);
3740 /* Gimplify the function's body. */
3741 gimplify_stmt (body_p);
3744 /* Unshare again, in case gimplification was sloppy. */
3745 unshare_all_trees (body);
3747 /* If there isn't an outer BIND_EXPR, add one. */
3748 if (TREE_CODE (body) == STATEMENT_LIST)
3750 tree t = expr_only (*body_p);
3754 if (TREE_CODE (body) != BIND_EXPR)
3756 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3757 NULL_TREE, NULL_TREE);
3758 TREE_SIDE_EFFECTS (b) = 1;
3759 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3764 pop_gimplify_context (body);
3766 #ifdef ENABLE_CHECKING
3767 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3770 timevar_pop (TV_TREE_GIMPLIFY);
3771 input_location = saved_location;
3774 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
3775 node for the function we want to gimplify. */
3778 gimplify_function_tree (tree fndecl)
3782 oldfn = current_function_decl;
3783 current_function_decl = fndecl;
3785 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3787 /* If we're instrumenting function entry/exit, then prepend the call to
3788 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3789 catch the exit hook. */
3790 /* ??? Add some way to ignore exceptions for this TFE. */
3791 if (flag_instrument_function_entry_exit
3792 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3796 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3797 TREE_SIDE_EFFECTS (tf) = 1;
3798 x = DECL_SAVED_TREE (fndecl);
3799 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3800 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3801 x = build_function_call_expr (x, NULL);
3802 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3804 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3805 TREE_SIDE_EFFECTS (bind) = 1;
3806 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3807 x = build_function_call_expr (x, NULL);
3808 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3809 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3811 DECL_SAVED_TREE (fndecl) = bind;
3814 current_function_decl = oldfn;
3817 #include "gt-gimplify.h"