1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
32 #include "tree-gimple.h"
33 #include "tree-inline.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "tree-flow.h"
50 static struct gimplify_ctx
52 tree current_bind_expr;
55 tree conditional_cleanups;
59 varray_type case_labels;
60 /* The formal temporary table. Should this be persistent? */
65 /* Formal (expression) temporary table handling: Multiple occurrences of
66 the same scalar expression are evaluated into the same temporary. */
68 typedef struct gimple_temp_hash_elt
71 tree temp; /* Value */
74 /* Forward declarations. */
75 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
78 /* Return a hash value for a formal temporary table entry. */
81 gimple_tree_hash (const void *p)
83 tree t = ((const elt_t *) p)->val;
84 return iterative_hash_expr (t, 0);
87 /* Compare two formal temporary table entries. */
90 gimple_tree_eq (const void *p1, const void *p2)
92 tree t1 = ((const elt_t *) p1)->val;
93 tree t2 = ((const elt_t *) p2)->val;
94 enum tree_code code = TREE_CODE (t1);
96 if (TREE_CODE (t2) != code
97 || TREE_TYPE (t1) != TREE_TYPE (t2))
100 if (!operand_equal_p (t1, t2, 0))
103 /* Only allow them to compare equal if they also hash equal; otherwise
104 results are nondeterminate, and we fail bootstrap comparison. */
105 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
111 /* Set up a context for the gimplifier. */
114 push_gimplify_context (void)
119 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
120 gimplify_ctxp->temp_htab
121 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
124 /* Tear down a context for the gimplifier. If BODY is non-null, then
125 put the temporaries into the outer BIND_EXPR. Otherwise, put them
126 in the unexpanded_var_list. */
129 pop_gimplify_context (tree body)
131 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
135 declare_tmp_vars (gimplify_ctxp->temps, body);
137 record_vars (gimplify_ctxp->temps);
141 fprintf (stderr, " collisions: %f ",
142 htab_collisions (gimplify_ctxp->temp_htab));
145 htab_delete (gimplify_ctxp->temp_htab);
146 free (gimplify_ctxp);
147 gimplify_ctxp = NULL;
151 gimple_push_bind_expr (tree bind)
153 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
154 gimplify_ctxp->current_bind_expr = bind;
158 gimple_pop_bind_expr (void)
160 gimplify_ctxp->current_bind_expr
161 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
165 gimple_current_bind_expr (void)
167 return gimplify_ctxp->current_bind_expr;
170 /* Returns true iff there is a COND_EXPR between us and the innermost
171 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
174 gimple_conditional_context (void)
176 return gimplify_ctxp->conditions > 0;
179 /* Note that we've entered a COND_EXPR. */
182 gimple_push_condition (void)
184 ++(gimplify_ctxp->conditions);
187 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
188 now, add any conditional cleanups we've seen to the prequeue. */
191 gimple_pop_condition (tree *pre_p)
193 int conds = --(gimplify_ctxp->conditions);
197 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
198 gimplify_ctxp->conditional_cleanups = NULL_TREE;
204 /* A subroutine of append_to_statement_list{,_force}. */
207 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
210 tree_stmt_iterator i;
217 if (t && TREE_CODE (t) == STATEMENT_LIST)
222 *list_p = list = alloc_stmt_list ();
226 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
229 /* Add T to the end of the list container pointed by LIST_P.
230 If T is an expression with no effects, it is ignored. */
233 append_to_statement_list (tree t, tree *list_p)
235 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
238 /* Similar, but the statement is always added, regardless of side effects. */
241 append_to_statement_list_force (tree t, tree *list_p)
243 append_to_statement_list_1 (t, list_p, t != NULL);
246 /* Both gimplify the statement T and append it to LIST_P. */
249 gimplify_and_add (tree t, tree *list_p)
252 append_to_statement_list (t, list_p);
255 /* Strip off a legitimate source ending from the input string NAME of
256 length LEN. Rather than having to know the names used by all of
257 our front ends, we strip off an ending of a period followed by
258 up to five characters. (Java uses ".class".) */
261 remove_suffix (char *name, int len)
265 for (i = 2; i < 8 && len > i; i++)
267 if (name[len - i] == '.')
269 name[len - i] = '\0';
275 /* Create a nameless artificial label and put it in the current function
276 context. Returns the newly created label. */
279 create_artificial_label (void)
281 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
283 DECL_ARTIFICIAL (lab) = 1;
284 DECL_CONTEXT (lab) = current_function_decl;
288 /* Create a new temporary name with PREFIX. Returns an identifier. */
290 static GTY(()) unsigned int tmp_var_id_num;
293 create_tmp_var_name (const char *prefix)
299 char *preftmp = ASTRDUP (prefix);
301 remove_suffix (preftmp, strlen (preftmp));
305 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
306 return get_identifier (tmp_name);
310 /* Create a new temporary variable declaration of type TYPE.
311 Does NOT push it into the current binding. */
314 create_tmp_var_raw (tree type, const char *prefix)
319 /* Make the type of the variable writable. */
320 new_type = build_type_variant (type, 0, 0);
321 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
323 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
325 /* The variable was declared by the compiler. */
326 DECL_ARTIFICIAL (tmp_var) = 1;
327 /* And we don't want debug info for it. */
328 DECL_IGNORED_P (tmp_var) = 1;
330 /* Make the variable writable. */
331 TREE_READONLY (tmp_var) = 0;
333 DECL_EXTERNAL (tmp_var) = 0;
334 TREE_STATIC (tmp_var) = 0;
335 TREE_USED (tmp_var) = 1;
340 /* Create a new temporary variable declaration of type TYPE. DOES push the
341 variable into the current binding. Further, assume that this is called
342 only from gimplification or optimization, at which point the creation of
343 certain types are bugs. */
346 create_tmp_var (tree type, const char *prefix)
350 #if defined ENABLE_CHECKING
351 /* We don't allow types that are addressable (meaning we can't make copies),
352 incomplete, or of variable size. */
353 if (TREE_ADDRESSABLE (type)
354 || !COMPLETE_TYPE_P (type)
355 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
359 tmp_var = create_tmp_var_raw (type, prefix);
360 gimple_add_tmp_var (tmp_var);
364 /* Given a tree, try to return a useful variable name that we can use
365 to prefix a temporary that is being assigned the value of the tree.
366 I.E. given <temp> = &A, return A. */
374 STRIP_NOPS (stripped_decl);
375 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
376 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
379 switch (TREE_CODE (stripped_decl))
382 return get_name (TREE_OPERAND (stripped_decl, 0));
390 /* Create a temporary with a name derived from VAL. Subroutine of
391 lookup_tmp_var; nobody else should call this function. */
394 create_tmp_from_val (tree val)
396 return create_tmp_var (TREE_TYPE (val), get_name (val));
399 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
400 an existing expression temporary. */
403 lookup_tmp_var (tree val, bool is_formal)
405 if (!is_formal || TREE_SIDE_EFFECTS (val))
406 return create_tmp_from_val (val);
413 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
416 elt_p = xmalloc (sizeof (*elt_p));
418 elt_p->temp = create_tmp_from_val (val);
419 TREE_READONLY (elt_p->temp) = 1;
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_tmp_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_HAS_LOCATION (val))
455 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
457 SET_EXPR_LOCATION (mod, input_location);
459 /* gimplify_modify_expr might want to reduce this further. */
460 gimplify_and_add (mod, pre_p);
465 get_formal_tmp_var (tree val, tree *pre_p)
467 return internal_get_tmp_var (val, pre_p, NULL, true);
470 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
471 are as in gimplify_expr. */
474 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
476 return internal_get_tmp_var (val, pre_p, post_p, false);
479 /* Declares all the variables in VARS in SCOPE. */
482 declare_tmp_vars (tree vars, tree scope)
489 /* C99 mode puts the default 'return 0;' for main outside the outer
490 braces. So drill down until we find an actual scope. */
491 while (TREE_CODE (scope) == COMPOUND_EXPR)
492 scope = TREE_OPERAND (scope, 0);
494 if (TREE_CODE (scope) != BIND_EXPR)
497 temps = nreverse (last);
498 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
499 BIND_EXPR_VARS (scope) = temps;
504 gimple_add_tmp_var (tree tmp)
506 if (TREE_CHAIN (tmp) || DECL_SEEN_IN_BIND_EXPR_P (tmp))
509 DECL_CONTEXT (tmp) = current_function_decl;
510 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
514 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
515 gimplify_ctxp->temps = tmp;
520 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
523 /* Determines whether to assign a locus to the statement STMT. */
526 should_carry_locus_p (tree stmt)
528 /* Don't emit a line note for a label. We particularly don't want to
529 emit one for the break label, since it doesn't actually correspond
530 to the beginning of the loop/switch. */
531 if (TREE_CODE (stmt) == LABEL_EXPR)
534 /* Do not annotate empty statements, since it confuses gcov. */
535 if (!TREE_SIDE_EFFECTS (stmt))
542 annotate_one_with_locus (tree t, location_t locus)
544 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
545 && ! EXPR_HAS_LOCATION (t)
546 && should_carry_locus_p (t))
547 SET_EXPR_LOCATION (t, locus);
551 annotate_all_with_locus (tree *stmt_p, location_t locus)
553 tree_stmt_iterator i;
558 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
560 tree t = tsi_stmt (i);
562 #ifdef ENABLE_CHECKING
563 /* Assuming we've already been gimplified, we shouldn't
564 see nested chaining constructs anymore. */
565 if (TREE_CODE (t) == STATEMENT_LIST
566 || TREE_CODE (t) == COMPOUND_EXPR)
570 annotate_one_with_locus (t, locus);
574 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
575 These nodes model computations that should only be done once. If we
576 were to unshare something like SAVE_EXPR(i++), the gimplification
577 process would create wrong code. */
580 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
582 enum tree_code code = TREE_CODE (*tp);
583 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
584 if (TREE_CODE_CLASS (code) == 't'
585 || TREE_CODE_CLASS (code) == 'd'
586 || TREE_CODE_CLASS (code) == 'c'
587 || code == SAVE_EXPR || code == TARGET_EXPR
588 /* We can't do anything sensible with a BLOCK used as an expression,
589 but we also can't abort when we see it because of non-expression
590 uses. So just avert our eyes and cross our fingers. Silly Java. */
593 else if (code == BIND_EXPR)
596 copy_tree_r (tp, walk_subtrees, data);
601 /* Callback for walk_tree to unshare most of the shared trees rooted at
602 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
603 then *TP is deep copied by calling copy_tree_r.
605 This unshares the same trees as copy_tree_r with the exception of
606 SAVE_EXPR nodes. These nodes model computations that should only be
607 done once. If we were to unshare something like SAVE_EXPR(i++), the
608 gimplification process would create wrong code. */
611 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
612 void *data ATTRIBUTE_UNUSED)
615 enum tree_code code = TREE_CODE (t);
617 /* Skip types, decls, and constants. But we do want to look at their
618 types and the bounds of types. Mark them as visited so we properly
619 unmark their subtrees on the unmark pass. If we've already seen them,
620 don't look down further. */
621 if (TREE_CODE_CLASS (code) == 't'
622 || TREE_CODE_CLASS (code) == 'd'
623 || TREE_CODE_CLASS (code) == 'c')
625 if (TREE_VISITED (t))
628 TREE_VISITED (t) = 1;
631 /* If this node has been visited already, unshare it and don't look
633 else if (TREE_VISITED (t))
635 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
639 /* Otherwise, mark the tree as visited and keep looking. */
641 TREE_VISITED (t) = 1;
647 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
648 void *data ATTRIBUTE_UNUSED)
650 if (TREE_VISITED (*tp))
651 TREE_VISITED (*tp) = 0;
658 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
659 bodies of any nested functions if we are unsharing the entire body of
663 unshare_body (tree *body_p, tree fndecl)
665 struct cgraph_node *cgn = cgraph_node (fndecl);
667 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
668 if (body_p == &DECL_SAVED_TREE (fndecl))
669 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
670 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
673 /* Likewise, but mark all trees as not visited. */
676 unvisit_body (tree *body_p, tree fndecl)
678 struct cgraph_node *cgn = cgraph_node (fndecl);
680 walk_tree (body_p, unmark_visited_r, NULL, NULL);
681 if (body_p == &DECL_SAVED_TREE (fndecl))
682 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
683 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
686 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
689 unshare_all_trees (tree t)
691 walk_tree (&t, copy_if_shared_r, NULL, NULL);
692 walk_tree (&t, unmark_visited_r, NULL, NULL);
695 /* Unconditionally make an unshared copy of EXPR. This is used when using
696 stored expressions which span multiple functions, such as BINFO_VTABLE,
697 as the normal unsharing process can't tell that they're shared. */
700 unshare_expr (tree expr)
702 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
706 /* A terser interface for building a representation of a exception
710 gimple_build_eh_filter (tree body, tree allowed, tree failure)
714 /* FIXME should the allowed types go in TREE_TYPE? */
715 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
716 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
718 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
719 append_to_statement_list (body, &TREE_OPERAND (t, 0));
725 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
726 contain statements and have a value. Assign its value to a temporary
727 and give it void_type_node. Returns the temporary, or NULL_TREE if
728 WRAPPER was already void. */
731 voidify_wrapper_expr (tree wrapper, tree temp)
733 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
735 tree *p, sub = wrapper;
738 /* Set p to point to the body of the wrapper. */
739 switch (TREE_CODE (sub))
742 /* For a BIND_EXPR, the body is operand 1. */
743 p = &BIND_EXPR_BODY (sub);
747 p = &TREE_OPERAND (sub, 0);
751 /* Advance to the last statement. Set all container types to void. */
752 if (TREE_CODE (*p) == STATEMENT_LIST)
754 tree_stmt_iterator i = tsi_last (*p);
755 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
759 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
761 TREE_SIDE_EFFECTS (*p) = 1;
762 TREE_TYPE (*p) = void_type_node;
766 if (p == NULL || IS_EMPTY_STMT (*p))
768 /* Look through exception handling. */
769 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
770 || TREE_CODE (*p) == TRY_CATCH_EXPR)
775 /* The C++ frontend already did this for us. */
776 else if (TREE_CODE (*p) == INIT_EXPR
777 || TREE_CODE (*p) == TARGET_EXPR)
778 temp = TREE_OPERAND (*p, 0);
779 /* If we're returning a dereference, move the dereference
780 outside the wrapper. */
781 else if (TREE_CODE (*p) == INDIRECT_REF)
783 tree ptr = TREE_OPERAND (*p, 0);
784 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
785 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
786 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
787 /* If this is a BIND_EXPR for a const inline function, it might not
788 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
789 TREE_SIDE_EFFECTS (wrapper) = 1;
794 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
795 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
796 TREE_SIDE_EFFECTS (wrapper) = 1;
799 TREE_TYPE (wrapper) = void_type_node;
806 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
807 a temporary through which they communicate. */
810 build_stack_save_restore (tree *save, tree *restore)
812 tree save_call, tmp_var;
815 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
817 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
819 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
821 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
822 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
825 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
827 static enum gimplify_status
828 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
830 tree bind_expr = *expr_p;
831 bool old_save_stack = gimplify_ctxp->save_stack;
834 temp = voidify_wrapper_expr (bind_expr, temp);
836 /* Mark variables seen in this bind expr. */
837 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
838 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
840 gimple_push_bind_expr (bind_expr);
841 gimplify_ctxp->save_stack = false;
843 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
845 if (gimplify_ctxp->save_stack)
847 tree stack_save, stack_restore;
849 /* Save stack on entry and restore it on exit. Add a try_finally
850 block to achieve this. Note that mudflap depends on the
851 format of the emitted code: see mx_register_decls(). */
852 build_stack_save_restore (&stack_save, &stack_restore);
854 t = build (TRY_FINALLY_EXPR, void_type_node,
855 BIND_EXPR_BODY (bind_expr), NULL_TREE);
856 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
858 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
859 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
860 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
863 gimplify_ctxp->save_stack = old_save_stack;
864 gimple_pop_bind_expr ();
869 append_to_statement_list (bind_expr, pre_p);
876 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
877 GIMPLE value, it is assigned to a new temporary and the statement is
878 re-written to return the temporary.
880 PRE_P points to the list where side effects that must happen before
881 STMT should be stored. */
883 static enum gimplify_status
884 gimplify_return_expr (tree stmt, tree *pre_p)
886 tree ret_expr = TREE_OPERAND (stmt, 0);
887 tree result_decl, result;
889 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
890 || ret_expr == error_mark_node)
893 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
894 result_decl = NULL_TREE;
897 result_decl = TREE_OPERAND (ret_expr, 0);
898 #ifdef ENABLE_CHECKING
899 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
900 && TREE_CODE (ret_expr) != INIT_EXPR)
901 || TREE_CODE (result_decl) != RESULT_DECL)
906 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
907 Recall that aggregate_value_p is FALSE for any aggregate type that is
908 returned in registers. If we're returning values in registers, then
909 we don't want to extend the lifetime of the RESULT_DECL, particularly
910 across another call. In addition, for those aggregates for which
911 hard_function_value generates a PARALLEL, we'll abort during normal
912 expansion of structure assignments; there's special code in expand_return
913 to handle this case that does not exist in expand_expr. */
915 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
916 result = result_decl;
917 else if (gimplify_ctxp->return_temp)
918 result = gimplify_ctxp->return_temp;
921 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
923 /* ??? With complex control flow (usually involving abnormal edges),
924 we can wind up warning about an uninitialized value for this. Due
925 to how this variable is constructed and initialized, this is never
926 true. Give up and never warn. */
927 TREE_NO_WARNING (result) = 1;
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;
937 gimplify_and_add (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 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
951 and initialization explicit. */
953 static enum gimplify_status
954 gimplify_decl_expr (tree *stmt_p)
957 tree decl = DECL_EXPR_DECL (stmt);
961 if (TREE_TYPE (decl) == error_mark_node)
964 else if (TREE_CODE (decl) == TYPE_DECL)
965 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
967 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
969 tree init = DECL_INITIAL (decl);
971 if (!TREE_CONSTANT (DECL_SIZE (decl)))
973 /* This is a variable-sized decl. Simplify its size and mark it
974 for deferred expansion. Note that mudflap depends on the format
975 of the emitted code: see mx_register_decls(). */
978 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
979 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
980 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
982 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
983 t = build_fold_addr_expr (decl);
984 args = tree_cons (NULL, t, args);
985 t = implicit_built_in_decls[BUILT_IN_STACK_ALLOC];
986 t = build_function_call_expr (t, args);
988 gimplify_and_add (t, stmt_p);
989 DECL_DEFER_OUTPUT (decl) = 1;
992 if (init && init != error_mark_node)
994 if (!TREE_STATIC (decl))
996 DECL_INITIAL (decl) = NULL_TREE;
997 init = build (MODIFY_EXPR, void_type_node, decl, init);
998 gimplify_and_add (init, stmt_p);
1001 /* We must still examine initializers for static variables
1002 as they may contain a label address. */
1003 walk_tree (&init, force_labels_r, NULL, NULL);
1006 /* This decl isn't mentioned in the enclosing block, so add it to the
1007 list of temps. FIXME it seems a bit of a kludge to say that
1008 anonymous artificial vars aren't pushed, but everything else is. */
1009 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1010 gimple_add_tmp_var (decl);
1016 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1017 and replacing the LOOP_EXPR with goto, but if the loop contains an
1018 EXIT_EXPR, we need to append a label for it to jump to. */
1020 static enum gimplify_status
1021 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1023 tree saved_label = gimplify_ctxp->exit_label;
1024 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1025 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1027 append_to_statement_list (start_label, pre_p);
1029 gimplify_ctxp->exit_label = NULL_TREE;
1031 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1033 if (gimplify_ctxp->exit_label)
1035 append_to_statement_list (jump_stmt, pre_p);
1036 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1039 *expr_p = jump_stmt;
1041 gimplify_ctxp->exit_label = saved_label;
1046 /* Compare two case labels. Because the front end should already have
1047 made sure that case ranges do not overlap, it is enough to only compare
1048 the CASE_LOW values of each case label. */
1051 compare_case_labels (const void *p1, const void *p2)
1053 tree case1 = *(tree *)p1;
1054 tree case2 = *(tree *)p2;
1056 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1059 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1062 sort_case_labels (tree label_vec)
1064 size_t len = TREE_VEC_LENGTH (label_vec);
1065 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1067 if (CASE_LOW (default_case))
1071 /* The last label in the vector should be the default case
1073 for (i = 0; i < len; ++i)
1075 tree t = TREE_VEC_ELT (label_vec, i);
1079 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1080 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1086 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1087 compare_case_labels);
1090 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1093 static enum gimplify_status
1094 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1096 tree switch_expr = *expr_p;
1097 enum gimplify_status ret;
1099 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1100 is_gimple_val, fb_rvalue);
1102 if (SWITCH_BODY (switch_expr))
1104 varray_type labels, saved_labels;
1105 tree label_vec, default_case = NULL_TREE;
1108 /* If someone can be bothered to fill in the labels, they can
1109 be bothered to null out the body too. */
1110 if (SWITCH_LABELS (switch_expr))
1113 saved_labels = gimplify_ctxp->case_labels;
1114 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1116 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1118 labels = gimplify_ctxp->case_labels;
1119 gimplify_ctxp->case_labels = saved_labels;
1121 len = VARRAY_ACTIVE_SIZE (labels);
1123 for (i = 0; i < len; ++i)
1125 tree t = VARRAY_TREE (labels, i);
1128 /* The default case must be the last label in the list. */
1130 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1136 label_vec = make_tree_vec (len + 1);
1137 SWITCH_LABELS (*expr_p) = label_vec;
1138 append_to_statement_list (switch_expr, pre_p);
1142 /* If the switch has no default label, add one, so that we jump
1143 around the switch body. */
1144 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1145 NULL_TREE, create_artificial_label ());
1146 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1147 *expr_p = build (LABEL_EXPR, void_type_node,
1148 CASE_LABEL (default_case));
1151 *expr_p = SWITCH_BODY (switch_expr);
1153 for (i = 0; i < len; ++i)
1154 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1155 TREE_VEC_ELT (label_vec, len) = default_case;
1157 sort_case_labels (label_vec);
1159 SWITCH_BODY (switch_expr) = NULL;
1161 else if (!SWITCH_LABELS (switch_expr))
1167 static enum gimplify_status
1168 gimplify_case_label_expr (tree *expr_p)
1170 tree expr = *expr_p;
1171 if (gimplify_ctxp->case_labels)
1172 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1175 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1179 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1180 a (possibly empty) body. */
1182 static enum gimplify_status
1183 gimplify_labeled_block_expr (tree *expr_p)
1185 tree body = LABELED_BLOCK_BODY (*expr_p);
1186 tree label = LABELED_BLOCK_LABEL (*expr_p);
1189 DECL_CONTEXT (label) = current_function_decl;
1190 t = build (LABEL_EXPR, void_type_node, label);
1191 if (body != NULL_TREE)
1192 t = build (COMPOUND_EXPR, void_type_node, body, t);
1198 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1200 static enum gimplify_status
1201 gimplify_exit_block_expr (tree *expr_p)
1203 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1206 /* First operand must be a LABELED_BLOCK_EXPR, which should
1207 already be lowered (or partially lowered) when we get here. */
1208 #if defined ENABLE_CHECKING
1209 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1213 label = LABELED_BLOCK_LABEL (labeled_block);
1214 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1219 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1223 build_and_jump (tree *label_p)
1225 if (label_p == NULL)
1226 /* If there's nowhere to jump, just fall through. */
1229 if (*label_p == NULL_TREE)
1231 tree label = create_artificial_label ();
1235 return build1 (GOTO_EXPR, void_type_node, *label_p);
1238 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1239 This also involves building a label to jump to and communicating it to
1240 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1242 static enum gimplify_status
1243 gimplify_exit_expr (tree *expr_p)
1245 tree cond = TREE_OPERAND (*expr_p, 0);
1248 expr = build_and_jump (&gimplify_ctxp->exit_label);
1249 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1255 /* A helper function to be called via walk_tree. Mark all labels under *TP
1256 as being forced. To be called for DECL_INITIAL of static variables. */
1259 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1263 if (TREE_CODE (*tp) == LABEL_DECL)
1264 FORCED_LABEL (*tp) = 1;
1269 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1270 different from its canonical type, wrap the whole thing inside a
1271 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1274 The canonical type of a COMPONENT_REF is the type of the field being
1275 referenced--unless the field is a bit-field which can be read directly
1276 in a smaller mode, in which case the canonical type is the
1277 sign-appropriate type corresponding to that mode. */
1280 canonicalize_component_ref (tree *expr_p)
1282 tree expr = *expr_p;
1285 if (TREE_CODE (expr) != COMPONENT_REF)
1288 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1289 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1291 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1293 if (TREE_TYPE (expr) != type)
1295 tree old_type = TREE_TYPE (expr);
1297 /* Set the type of the COMPONENT_REF to the underlying type. */
1298 TREE_TYPE (expr) = type;
1300 /* And wrap the whole thing inside a NOP_EXPR. */
1301 expr = build1 (NOP_EXPR, old_type, expr);
1307 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1308 to foo, embed that change in the ADDR_EXPR by converting
1313 where L is the lower bound. For simplicity, only do this for constant
1317 canonicalize_addr_expr (tree *expr_p)
1319 tree expr = *expr_p;
1320 tree ctype = TREE_TYPE (expr);
1321 tree addr_expr = TREE_OPERAND (expr, 0);
1322 tree atype = TREE_TYPE (addr_expr);
1323 tree dctype, datype, ddatype, otype, obj_expr;
1325 /* Both cast and addr_expr types should be pointers. */
1326 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1329 /* The addr_expr type should be a pointer to an array. */
1330 datype = TREE_TYPE (atype);
1331 if (TREE_CODE (datype) != ARRAY_TYPE)
1334 /* Both cast and addr_expr types should address the same object type. */
1335 dctype = TREE_TYPE (ctype);
1336 ddatype = TREE_TYPE (datype);
1337 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1340 /* The addr_expr and the object type should match. */
1341 obj_expr = TREE_OPERAND (addr_expr, 0);
1342 otype = TREE_TYPE (obj_expr);
1343 if (!lang_hooks.types_compatible_p (otype, datype))
1346 /* The lower bound and element sizes must be constant. */
1347 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1348 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1349 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1352 /* All checks succeeded. Build a new node to merge the cast. */
1353 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1354 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1355 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1356 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1357 size_int (TYPE_ALIGN (dctype)
1359 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1362 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1363 underneath as appropriate. */
1365 static enum gimplify_status
1366 gimplify_conversion (tree *expr_p)
1368 /* If we still have a conversion at the toplevel, then strip
1369 away all but the outermost conversion. */
1370 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1372 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1374 /* And remove the outermost conversion if it's useless. */
1375 if (tree_ssa_useless_type_conversion (*expr_p))
1376 *expr_p = TREE_OPERAND (*expr_p, 0);
1379 /* If we still have a conversion at the toplevel,
1380 then canonicalize some constructs. */
1381 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1383 tree sub = TREE_OPERAND (*expr_p, 0);
1385 /* If a NOP conversion is changing the type of a COMPONENT_REF
1386 expression, then canonicalize its type now in order to expose more
1387 redundant conversions. */
1388 if (TREE_CODE (sub) == COMPONENT_REF)
1389 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1391 /* If a NOP conversion is changing a pointer to array of foo
1392 to a pointer to foo, embed that change in the ADDR_EXPR. */
1393 else if (TREE_CODE (sub) == ADDR_EXPR)
1394 canonicalize_addr_expr (expr_p);
1400 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1402 static enum gimplify_status
1403 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1405 tree op1 = TREE_OPERAND (*expr_p, 0);
1406 tree op2 = TREE_OPERAND (*expr_p, 1);
1407 enum tree_code code;
1408 enum gimplify_status r0, r1;
1410 if (TREE_CODE (*expr_p) == MIN_EXPR)
1415 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1416 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1418 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1419 build (code, boolean_type_node, op1, op2),
1422 if (r0 == GS_ERROR || r1 == GS_ERROR)
1428 /* Subroutine of gimplify_compound_lval.
1429 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1431 static enum gimplify_status
1432 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1434 tree array = TREE_OPERAND (*expr_p, 0);
1435 tree arrtype = TREE_TYPE (array);
1436 tree elttype = TREE_TYPE (arrtype);
1437 tree size = array_ref_element_size (*expr_p);
1438 tree ptrtype = build_pointer_type (elttype);
1439 enum tree_code add_code = PLUS_EXPR;
1440 tree idx = TREE_OPERAND (*expr_p, 1);
1441 tree minidx = unshare_expr (array_ref_low_bound (*expr_p));
1442 tree offset, addr, result;
1443 enum gimplify_status ret;
1445 /* If the array domain does not start at zero, apply the offset. */
1446 if (!integer_zerop (minidx))
1448 idx = convert (TREE_TYPE (minidx), idx);
1449 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1452 /* If the index is negative -- a technically invalid situation now
1453 that we've biased the index back to zero -- then casting it to
1454 unsigned has ill effects. In particular, -1*4U/4U != -1.
1455 Represent this as a subtraction of a positive rather than addition
1456 of a negative. This will prevent any conversion back to ARRAY_REF
1457 from getting the wrong results from the division. */
1458 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1460 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1461 add_code = MINUS_EXPR;
1464 /* Pointer arithmetic must be done in sizetype. */
1465 idx = fold_convert (sizetype, idx);
1467 /* Convert the index to a byte offset. */
1468 offset = size_binop (MULT_EXPR, size, idx);
1470 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1471 if (ret == GS_ERROR)
1474 addr = build_fold_addr_expr_with_type (array, ptrtype);
1475 result = fold (build (add_code, ptrtype, addr, offset));
1476 *expr_p = build1 (INDIRECT_REF, elttype, result);
1481 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1482 node pointed by EXPR_P.
1485 : min_lval '[' val ']'
1487 | compound_lval '[' val ']'
1488 | compound_lval '.' ID
1490 This is not part of the original SIMPLE definition, which separates
1491 array and member references, but it seems reasonable to handle them
1492 together. Also, this way we don't run into problems with union
1493 aliasing; gcc requires that for accesses through a union to alias, the
1494 union reference must be explicit, which was not always the case when we
1495 were splitting up array and member refs.
1497 PRE_P points to the list where side effects that must happen before
1498 *EXPR_P should be stored.
1500 POST_P points to the list where side effects that must happen after
1501 *EXPR_P should be stored. */
1503 static enum gimplify_status
1504 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1505 tree *post_p, fallback_t fallback)
1509 enum gimplify_status ret = GS_OK, tret;
1512 /* Create a stack of the subexpressions so later we can walk them in
1513 order from inner to outer. */
1514 VARRAY_TREE_INIT (stack, 10, "stack");
1516 /* We can either handle REALPART_EXPR, IMAGEPART_EXPR anything that
1517 handled_components can deal with. */
1519 (handled_component_p (*p)
1520 || TREE_CODE (*p) == REALPART_EXPR || TREE_CODE (*p) == IMAGPART_EXPR);
1521 p = &TREE_OPERAND (*p, 0))
1522 VARRAY_PUSH_TREE (stack, *p);
1524 #if defined ENABLE_CHECKING
1525 if (VARRAY_ACTIVE_SIZE (stack) == 0)
1529 /* Now STACK is a stack of pointers to all the refs we've walked through
1530 and P points to the innermost expression.
1532 Java requires that we elaborated nodes in source order. That
1533 means we must gimplify the inner expression followed by each of
1534 the indices, in order. But we can't gimplify the inner
1535 expression until we deal with any variable bounds, sizes, or
1536 positions in order to deal with PLACEHOLDER_EXPRs.
1538 So we do this in three steps. First we deal with the annotations
1539 for any variables in the components, then we gimplify the base,
1540 then we gimplify any indices, from left to right. */
1541 for (i = VARRAY_ACTIVE_SIZE (stack) - 1; i >= 0; i--)
1543 tree t = VARRAY_TREE (stack, i);
1545 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1547 /* Gimplify the low bound and element type size and put them into
1548 the ARRAY_REF. If these values are set, they have already been
1550 if (!TREE_OPERAND (t, 2))
1552 tree low = unshare_expr (array_ref_low_bound (t));
1553 if (!is_gimple_min_invariant (low))
1555 TREE_OPERAND (t, 2) = low;
1556 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1557 is_gimple_tmp_reg, fb_rvalue);
1558 ret = MIN (ret, tret);
1562 if (!TREE_OPERAND (t, 3))
1564 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1565 tree elmt_size = unshare_expr (array_ref_element_size (t));
1566 tree factor = size_int (TYPE_ALIGN (elmt_type) / BITS_PER_UNIT);
1568 /* Divide the element size by the alignment of the element
1570 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1572 if (!is_gimple_min_invariant (elmt_size))
1574 TREE_OPERAND (t, 3) = elmt_size;
1575 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1576 is_gimple_tmp_reg, fb_rvalue);
1577 ret = MIN (ret, tret);
1581 else if (TREE_CODE (t) == COMPONENT_REF)
1583 /* Set the field offset into T and gimplify it. */
1584 if (!TREE_OPERAND (t, 2))
1586 tree offset = unshare_expr (component_ref_field_offset (t));
1587 tree field = TREE_OPERAND (t, 1);
1589 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1591 /* Divide the offset by its alignment. */
1592 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1594 if (!is_gimple_min_invariant (offset))
1596 TREE_OPERAND (t, 2) = offset;
1597 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1598 is_gimple_tmp_reg, fb_rvalue);
1599 ret = MIN (ret, tret);
1605 /* Step 2 is to gimplify the base expression. */
1606 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1607 ret = MIN (ret, tret);
1609 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1610 loop we also remove any useless conversions. */
1611 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1613 tree t = VARRAY_TOP_TREE (stack);
1615 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1617 /* Gimplify the dimension.
1618 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1619 Gimplify non-constant array indices into a temporary
1621 FIXME - The real fix is to gimplify post-modify
1622 expressions into a minimal gimple lvalue. However, that
1623 exposes bugs in alias analysis. The alias analyzer does
1624 not handle &PTR->FIELD very well. Will fix after the
1625 branch is merged into mainline (dnovillo 2004-05-03). */
1626 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1628 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1629 is_gimple_tmp_reg, fb_rvalue);
1630 ret = MIN (ret, tret);
1633 else if (TREE_CODE (t) == BIT_FIELD_REF)
1635 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1636 is_gimple_val, fb_rvalue);
1637 ret = MIN (ret, tret);
1638 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1639 is_gimple_val, fb_rvalue);
1640 ret = MIN (ret, tret);
1643 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1645 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1646 set which would have caused all the outer expressions in EXPR_P
1647 leading to P to also have had TREE_SIDE_EFFECTS set. */
1648 recalculate_side_effects (t);
1652 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1653 ret = MIN (ret, tret);
1655 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1656 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1658 canonicalize_component_ref (expr_p);
1659 ret = MIN (ret, GS_OK);
1665 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1667 PRE_P points to the list where side effects that must happen before
1668 *EXPR_P should be stored.
1670 POST_P points to the list where side effects that must happen after
1671 *EXPR_P should be stored.
1673 WANT_VALUE is nonzero iff we want to use the value of this expression
1674 in another expression. */
1676 static enum gimplify_status
1677 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1680 enum tree_code code;
1681 tree lhs, lvalue, rhs, t1;
1683 enum tree_code arith_code;
1684 enum gimplify_status ret;
1686 code = TREE_CODE (*expr_p);
1688 #if defined ENABLE_CHECKING
1689 if (code != POSTINCREMENT_EXPR
1690 && code != POSTDECREMENT_EXPR
1691 && code != PREINCREMENT_EXPR
1692 && code != PREDECREMENT_EXPR)
1696 /* Prefix or postfix? */
1697 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1698 /* Faster to treat as prefix if result is not used. */
1699 postfix = want_value;
1703 /* Add or subtract? */
1704 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1705 arith_code = PLUS_EXPR;
1707 arith_code = MINUS_EXPR;
1709 /* Gimplify the LHS into a GIMPLE lvalue. */
1710 lvalue = TREE_OPERAND (*expr_p, 0);
1711 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1712 if (ret == GS_ERROR)
1715 /* Extract the operands to the arithmetic operation. */
1717 rhs = TREE_OPERAND (*expr_p, 1);
1719 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1720 that as the result value and in the postqueue operation. */
1723 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1724 if (ret == GS_ERROR)
1728 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1729 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1733 gimplify_and_add (t1, post_p);
1744 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1747 maybe_with_size_expr (tree *expr_p)
1749 tree expr = *expr_p;
1750 tree type = TREE_TYPE (expr);
1753 /* If we've already wrapped this or the type is error_mark_node, we can't do
1755 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1756 || type == error_mark_node)
1759 /* If the size isn't known or is a constant, we have nothing to do. */
1760 size = TYPE_SIZE_UNIT (type);
1761 if (!size || TREE_CODE (size) == INTEGER_CST)
1764 /* Otherwise, make a WITH_SIZE_EXPR. */
1765 size = unshare_expr (size);
1766 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1767 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1770 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1772 static enum gimplify_status
1773 gimplify_arg (tree *expr_p, tree *pre_p)
1775 bool (*test) (tree);
1778 /* In general, we allow lvalues for function arguments to avoid
1779 extra overhead of copying large aggregates out of even larger
1780 aggregates into temporaries only to copy the temporaries to
1781 the argument list. Make optimizers happy by pulling out to
1782 temporaries those types that fit in registers. */
1783 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
1784 test = is_gimple_val, fb = fb_rvalue;
1786 test = is_gimple_lvalue, fb = fb_either;
1788 /* If this is a variable sized type, we must remember the size. */
1789 maybe_with_size_expr (expr_p);
1791 /* There is a sequence point before a function call. Side effects in
1792 the argument list must occur before the actual call. So, when
1793 gimplifying arguments, force gimplify_expr to use an internal
1794 post queue which is then appended to the end of PRE_P. */
1795 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
1798 /* Gimplify the CALL_EXPR node pointed by EXPR_P. PRE_P points to the
1799 list where side effects that must happen before *EXPR_P should be stored.
1800 WANT_VALUE is true if the result of the call is desired. */
1802 static enum gimplify_status
1803 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1807 enum gimplify_status ret;
1810 #if defined ENABLE_CHECKING
1811 if (TREE_CODE (*expr_p) != CALL_EXPR)
1815 /* For reliable diagnostics during inlining, it is necessary that
1816 every call_expr be annotated with file and line. */
1817 if (! EXPR_HAS_LOCATION (*expr_p))
1818 SET_EXPR_LOCATION (*expr_p, input_location);
1820 /* This may be a call to a builtin function.
1822 Builtin function calls may be transformed into different
1823 (and more efficient) builtin function calls under certain
1824 circumstances. Unfortunately, gimplification can muck things
1825 up enough that the builtin expanders are not aware that certain
1826 transformations are still valid.
1828 So we attempt transformation/gimplification of the call before
1829 we gimplify the CALL_EXPR. At this time we do not manage to
1830 transform all calls in the same manner as the expanders do, but
1831 we do transform most of them. */
1832 decl = get_callee_fndecl (*expr_p);
1833 if (decl && DECL_BUILT_IN (decl))
1837 /* If it is allocation of stack, record the need to restore the memory
1838 when the enclosing bind_expr is exited. */
1839 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1840 gimplify_ctxp->save_stack = true;
1842 /* If it is restore of the stack, reset it, since it means we are
1843 regimplifying the bind_expr. Note that we use the fact that
1844 for try_finally_expr, try part is processed first. */
1845 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1846 gimplify_ctxp->save_stack = false;
1848 new = simplify_builtin (*expr_p, !want_value);
1850 if (new && new != *expr_p)
1852 /* There was a transformation of this call which computes the
1853 same value, but in a more efficient way. Return and try
1859 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
1860 /* Avoid gimplifying the second argument to va_start, which needs
1861 to be the plain PARM_DECL. */
1862 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
1865 /* There is a sequence point before the call, so any side effects in
1866 the calling expression must occur before the actual call. Force
1867 gimplify_expr to use an internal post queue. */
1868 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1869 is_gimple_call_addr, fb_rvalue);
1871 /* Make the return slot explicit if it isn't already. */
1872 if (aggregate_value_p (*expr_p, decl)
1873 && !CALL_EXPR_HAS_RETURN_SLOT_ADDR (*expr_p))
1875 slot = create_tmp_var (TREE_TYPE (*expr_p), NULL);
1876 arglist = build_fold_addr_expr (slot);
1877 arglist = tree_cons (NULL_TREE, arglist, TREE_OPERAND (*expr_p, 1));
1878 TREE_OPERAND (*expr_p, 1) = arglist;
1879 CALL_EXPR_HAS_RETURN_SLOT_ADDR (*expr_p) = 1;
1884 if (PUSH_ARGS_REVERSED)
1885 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1886 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1887 arglist = TREE_CHAIN (arglist))
1889 enum gimplify_status t;
1891 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
1896 if (PUSH_ARGS_REVERSED)
1897 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1899 /* Try this again in case gimplification exposed something. */
1900 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
1902 tree new = simplify_builtin (*expr_p, !want_value);
1904 if (new && new != *expr_p)
1906 /* There was a transformation of this call which computes the
1907 same value, but in a more efficient way. Return and try
1914 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1915 decl. This allows us to eliminate redundant or useless
1916 calls to "const" functions. */
1917 if (TREE_CODE (*expr_p) == CALL_EXPR
1918 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
1919 TREE_SIDE_EFFECTS (*expr_p) = 0;
1921 /* If we have a return slot, use it in the containing expression. */
1922 if (want_value && CALL_EXPR_HAS_RETURN_SLOT_ADDR (*expr_p))
1924 /* Don't warn about an unused return value. */
1925 TREE_USED (*expr_p) = 1;
1927 if (slot == NULL_TREE)
1929 slot = TREE_OPERAND (*expr_p, 1);
1930 slot = TREE_VALUE (slot);
1931 slot = build_fold_indirect_ref (slot);
1933 append_to_statement_list (*expr_p, pre_p);
1940 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
1941 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
1943 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
1944 condition is true or false, respectively. If null, we should generate
1945 our own to skip over the evaluation of this specific expression.
1947 This function is the tree equivalent of do_jump.
1949 shortcut_cond_r should only be called by shortcut_cond_expr. */
1952 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
1954 tree local_label = NULL_TREE;
1955 tree t, expr = NULL;
1957 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
1958 retain the shortcut semantics. Just insert the gotos here;
1959 shortcut_cond_expr will append the real blocks later. */
1960 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1962 /* Turn if (a && b) into
1964 if (a); else goto no;
1965 if (b) goto yes; else goto no;
1968 if (false_label_p == NULL)
1969 false_label_p = &local_label;
1971 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
1972 append_to_statement_list (t, &expr);
1974 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1976 append_to_statement_list (t, &expr);
1978 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1980 /* Turn if (a || b) into
1983 if (b) goto yes; else goto no;
1986 if (true_label_p == NULL)
1987 true_label_p = &local_label;
1989 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
1990 append_to_statement_list (t, &expr);
1992 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1994 append_to_statement_list (t, &expr);
1996 else if (TREE_CODE (pred) == COND_EXPR)
1998 /* As long as we're messing with gotos, turn if (a ? b : c) into
2000 if (b) goto yes; else goto no;
2002 if (c) goto yes; else goto no; */
2003 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2004 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2006 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2011 expr = build (COND_EXPR, void_type_node, pred,
2012 build_and_jump (true_label_p),
2013 build_and_jump (false_label_p));
2018 t = build1 (LABEL_EXPR, void_type_node, local_label);
2019 append_to_statement_list (t, &expr);
2026 shortcut_cond_expr (tree expr)
2028 tree pred = TREE_OPERAND (expr, 0);
2029 tree then_ = TREE_OPERAND (expr, 1);
2030 tree else_ = TREE_OPERAND (expr, 2);
2031 tree true_label, false_label, end_label, t;
2033 tree *false_label_p;
2034 bool emit_end, emit_false;
2035 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2036 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2038 /* First do simple transformations. */
2041 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2042 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2044 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2045 then_ = shortcut_cond_expr (expr);
2046 pred = TREE_OPERAND (pred, 0);
2047 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2052 /* If there is no 'then', turn
2055 if (a); else if (b); else d. */
2056 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2058 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2059 else_ = shortcut_cond_expr (expr);
2060 pred = TREE_OPERAND (pred, 0);
2061 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2065 /* If we're done, great. */
2066 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2067 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2070 /* Otherwise we need to mess with gotos. Change
2073 if (a); else goto no;
2076 and recursively gimplify the condition. */
2078 true_label = false_label = end_label = NULL_TREE;
2080 /* If our arms just jump somewhere, hijack those labels so we don't
2081 generate jumps to jumps. */
2084 && TREE_CODE (then_) == GOTO_EXPR
2085 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2087 true_label = GOTO_DESTINATION (then_);
2093 && TREE_CODE (else_) == GOTO_EXPR
2094 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2096 false_label = GOTO_DESTINATION (else_);
2101 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2103 true_label_p = &true_label;
2105 true_label_p = NULL;
2107 /* The 'else' branch also needs a label if it contains interesting code. */
2108 if (false_label || else_se)
2109 false_label_p = &false_label;
2111 false_label_p = NULL;
2113 /* If there was nothing else in our arms, just forward the label(s). */
2114 if (!then_se && !else_se)
2115 return shortcut_cond_r (pred, true_label_p, false_label_p);
2117 /* If our last subexpression already has a terminal label, reuse it. */
2119 expr = expr_last (else_);
2121 expr = expr_last (then_);
2124 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2125 end_label = LABEL_EXPR_LABEL (expr);
2127 /* If we don't care about jumping to the 'else' branch, jump to the end
2128 if the condition is false. */
2130 false_label_p = &end_label;
2132 /* We only want to emit these labels if we aren't hijacking them. */
2133 emit_end = (end_label == NULL_TREE);
2134 emit_false = (false_label == NULL_TREE);
2136 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2139 append_to_statement_list (pred, &expr);
2141 append_to_statement_list (then_, &expr);
2144 t = build_and_jump (&end_label);
2145 append_to_statement_list (t, &expr);
2148 t = build1 (LABEL_EXPR, void_type_node, false_label);
2149 append_to_statement_list (t, &expr);
2151 append_to_statement_list (else_, &expr);
2153 if (emit_end && end_label)
2155 t = build1 (LABEL_EXPR, void_type_node, end_label);
2156 append_to_statement_list (t, &expr);
2162 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2165 gimple_boolify (tree expr)
2167 tree type = TREE_TYPE (expr);
2169 if (TREE_CODE (type) == BOOLEAN_TYPE)
2172 /* If this is the predicate of a COND_EXPR, it might not even be a
2174 expr = lang_hooks.truthvalue_conversion (expr);
2176 switch (TREE_CODE (expr))
2178 case TRUTH_AND_EXPR:
2180 case TRUTH_XOR_EXPR:
2181 case TRUTH_ANDIF_EXPR:
2182 case TRUTH_ORIF_EXPR:
2183 /* Also boolify the arguments of truth exprs. */
2184 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2187 case TRUTH_NOT_EXPR:
2188 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2191 case EQ_EXPR: case NE_EXPR:
2192 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2193 /* These expressions always produce boolean results. */
2194 TREE_TYPE (expr) = boolean_type_node;
2198 /* Other expressions that get here must have boolean values, but
2199 might need to be converted to the appropriate mode. */
2200 return convert (boolean_type_node, expr);
2204 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2213 The second form is used when *EXPR_P is of type void.
2215 TARGET is the tree for T1 above.
2217 PRE_P points to the list where side effects that must happen before
2218 *EXPR_P should be stored. */
2220 static enum gimplify_status
2221 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2223 tree expr = *expr_p;
2224 tree tmp, tmp2, type;
2225 enum gimplify_status ret;
2227 type = TREE_TYPE (expr);
2229 TREE_TYPE (expr) = void_type_node;
2231 /* If this COND_EXPR has a value, copy the values into a temporary within
2233 else if (! VOID_TYPE_P (type))
2237 ret = gimplify_expr (&target, pre_p, NULL,
2238 is_gimple_min_lval, fb_lvalue);
2239 if (ret != GS_ERROR)
2242 tmp2 = unshare_expr (target);
2246 tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2250 /* Build the then clause, 't1 = a;'. But don't build an assignment
2251 if this branch is void; in C++ it can be, if it's a throw. */
2252 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2253 TREE_OPERAND (expr, 1)
2254 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2256 /* Build the else clause, 't1 = b;'. */
2257 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2258 TREE_OPERAND (expr, 2)
2259 = build (MODIFY_EXPR, void_type_node, tmp2, TREE_OPERAND (expr, 2));
2261 TREE_TYPE (expr) = void_type_node;
2262 recalculate_side_effects (expr);
2264 /* Move the COND_EXPR to the prequeue. */
2265 gimplify_and_add (expr, pre_p);
2271 /* Make sure the condition has BOOLEAN_TYPE. */
2272 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2274 /* Break apart && and || conditions. */
2275 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2276 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2278 expr = shortcut_cond_expr (expr);
2280 if (expr != *expr_p)
2284 /* We can't rely on gimplify_expr to re-gimplify the expanded
2285 form properly, as cleanups might cause the target labels to be
2286 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2287 set up a conditional context. */
2288 gimple_push_condition ();
2289 gimplify_stmt (expr_p);
2290 gimple_pop_condition (pre_p);
2296 /* Now do the normal gimplification. */
2297 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2298 is_gimple_condexpr, fb_rvalue);
2300 gimple_push_condition ();
2302 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2303 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2304 recalculate_side_effects (expr);
2306 gimple_pop_condition (pre_p);
2308 if (ret == GS_ERROR)
2310 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2312 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2313 /* Rewrite "if (a); else b" to "if (!a) b" */
2315 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2316 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2317 is_gimple_condexpr, fb_rvalue);
2319 tmp = TREE_OPERAND (expr, 1);
2320 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2321 TREE_OPERAND (expr, 2) = tmp;
2324 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2325 expr = TREE_OPERAND (expr, 0);
2331 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2332 a call to __builtin_memcpy. */
2334 static enum gimplify_status
2335 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2337 tree args, t, to, to_ptr, from;
2339 to = TREE_OPERAND (*expr_p, 0);
2340 from = TREE_OPERAND (*expr_p, 1);
2342 args = tree_cons (NULL, size, NULL);
2344 t = build_fold_addr_expr (from);
2345 args = tree_cons (NULL, t, args);
2347 to_ptr = build_fold_addr_expr (to);
2348 args = tree_cons (NULL, to_ptr, args);
2349 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2350 t = build_function_call_expr (t, args);
2354 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2355 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2362 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2363 a call to __builtin_memset. In this case we know that the RHS is
2364 a CONSTRUCTOR with an empty element list. */
2366 static enum gimplify_status
2367 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2369 tree args, t, to, to_ptr;
2371 to = TREE_OPERAND (*expr_p, 0);
2373 args = tree_cons (NULL, size, NULL);
2375 args = tree_cons (NULL, integer_zero_node, args);
2377 to_ptr = build_fold_addr_expr (to);
2378 args = tree_cons (NULL, to_ptr, args);
2379 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2380 t = build_function_call_expr (t, args);
2384 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2385 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2392 /* A subroutine of gimplify_modify_expr. Break out elements of a
2393 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2395 Note that we still need to clear any elements that don't have explicit
2396 initializers, so if not all elements are initialized we keep the
2397 original MODIFY_EXPR, we just remove all of the constructor elements. */
2399 static enum gimplify_status
2400 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2401 tree *post_p, bool want_value)
2403 tree object = TREE_OPERAND (*expr_p, 0);
2404 tree ctor = TREE_OPERAND (*expr_p, 1);
2405 tree type = TREE_TYPE (ctor);
2406 enum gimplify_status ret;
2409 if (TREE_CODE (ctor) != CONSTRUCTOR)
2410 return GS_UNHANDLED;
2412 elt_list = CONSTRUCTOR_ELTS (ctor);
2415 switch (TREE_CODE (type))
2419 case QUAL_UNION_TYPE:
2422 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
2423 HOST_WIDE_INT num_nonconstant_elements;
2426 /* Aggregate types must lower constructors to initialization of
2427 individual elements. The exception is that a CONSTRUCTOR node
2428 with no elements indicates zero-initialization of the whole. */
2429 if (elt_list == NULL)
2437 return GS_UNHANDLED;
2440 categorize_ctor_elements (ctor, &num_nonzero_elements,
2441 &num_nonconstant_elements);
2442 num_elements = count_type_elements (TREE_TYPE (ctor));
2444 /* If a const aggregate variable is being initialized, then it
2445 should never be a lose to promote the variable to be static. */
2446 if (num_nonconstant_elements == 0
2447 && TREE_READONLY (object)
2448 && TREE_CODE (object) == VAR_DECL)
2450 DECL_INITIAL (object) = ctor;
2451 TREE_STATIC (object) = 1;
2452 if (!DECL_NAME (object))
2453 DECL_NAME (object) = create_tmp_var_name ("C");
2454 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2456 /* ??? C++ doesn't automatically append a .<number> to the
2457 assembler name, and even when it does, it looks a FE private
2458 data structures to figure out what that number should be,
2459 which are not set for this variable. I suppose this is
2460 important for local statics for inline functions, which aren't
2461 "local" in the object file sense. So in order to get a unique
2462 TU-local symbol, we must invoke the lhd version now. */
2463 lhd_set_decl_assembler_name (object);
2465 *expr_p = NULL_TREE;
2469 /* If there are "lots" of initialized elements, and all of them
2470 are valid address constants, then the entire initializer can
2471 be dropped to memory, and then memcpy'd out. */
2472 if (num_nonconstant_elements == 0)
2474 HOST_WIDE_INT size = int_size_in_bytes (type);
2477 /* ??? We can still get unbounded array types, at least
2478 from the C++ front end. This seems wrong, but attempt
2479 to work around it for now. */
2482 size = int_size_in_bytes (TREE_TYPE (object));
2484 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2487 /* Find the maximum alignment we can assume for the object. */
2488 /* ??? Make use of DECL_OFFSET_ALIGN. */
2489 if (DECL_P (object))
2490 align = DECL_ALIGN (object);
2492 align = TYPE_ALIGN (type);
2494 if (size > 0 && !can_move_by_pieces (size, align))
2496 tree new = create_tmp_var_raw (type, "C");
2497 gimple_add_tmp_var (new);
2498 TREE_STATIC (new) = 1;
2499 TREE_READONLY (new) = 1;
2500 DECL_INITIAL (new) = ctor;
2501 if (align > DECL_ALIGN (new))
2503 DECL_ALIGN (new) = align;
2504 DECL_USER_ALIGN (new) = 1;
2506 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2508 TREE_OPERAND (*expr_p, 1) = new;
2513 /* If there are "lots" of initialized elements, even discounting
2514 those that are not address constants (and thus *must* be
2515 computed at runtime), then partition the constructor into
2516 constant and non-constant parts. Block copy the constant
2517 parts in, then generate code for the non-constant parts. */
2518 /* TODO. There's code in cp/typeck.c to do this. */
2520 /* If there are "lots" of zeros, then block clear the object first. */
2522 if (num_elements - num_nonzero_elements > CLEAR_RATIO
2523 && num_nonzero_elements < num_elements/4)
2526 /* ??? This bit ought not be needed. For any element not present
2527 in the initializer, we should simply set them to zero. Except
2528 we'd need to *find* the elements that are not present, and that
2529 requires trickery to avoid quadratic compile-time behavior in
2530 large cases or excessive memory use in small cases. */
2533 HOST_WIDE_INT len = list_length (elt_list);
2534 if (TREE_CODE (type) == ARRAY_TYPE)
2536 tree nelts = array_type_nelts (type);
2537 if (!host_integerp (nelts, 1)
2538 || tree_low_cst (nelts, 1) + 1 != len)
2541 else if (len != fields_length (type))
2547 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2548 Note that we still have to gimplify, in order to handle the
2549 case of variable sized types. Make an unshared copy of
2550 OBJECT before that so we can match a PLACEHOLDER_EXPR to it
2551 later, if needed. */
2552 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
2553 object = unshare_expr (TREE_OPERAND (*expr_p, 0));
2554 gimplify_stmt (expr_p);
2555 append_to_statement_list (*expr_p, pre_p);
2558 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
2560 tree purpose, value, cref, init;
2562 purpose = TREE_PURPOSE (elt_list);
2563 value = TREE_VALUE (elt_list);
2565 if (cleared && initializer_zerop (value))
2568 if (TREE_CODE (type) == ARRAY_TYPE)
2570 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2572 /* ??? Here's to hoping the front end fills in all of the
2573 indicies, so we don't have to figure out what's missing
2577 /* ??? Need to handle this. */
2578 if (TREE_CODE (purpose) == RANGE_EXPR)
2581 cref = build (ARRAY_REF, t, unshare_expr (object), purpose,
2582 NULL_TREE, NULL_TREE);
2585 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
2586 unshare_expr (object), purpose, NULL_TREE);
2588 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
2590 /* Each member initialization is a full-expression. */
2591 gimplify_and_add (init, pre_p);
2594 *expr_p = NULL_TREE;
2602 /* Extract the real and imaginary parts out of the ctor. */
2606 r = TREE_VALUE (elt_list);
2607 elt_list = TREE_CHAIN (elt_list);
2610 i = TREE_VALUE (elt_list);
2611 if (TREE_CHAIN (elt_list))
2615 if (r == NULL || i == NULL)
2617 tree zero = convert (TREE_TYPE (type), integer_zero_node);
2624 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
2625 represent creation of a complex value. */
2626 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
2628 ctor = build_complex (type, r, i);
2629 TREE_OPERAND (*expr_p, 1) = ctor;
2633 ctor = build (COMPLEX_EXPR, type, r, i);
2634 TREE_OPERAND (*expr_p, 1) = ctor;
2635 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
2636 is_gimple_tmp_rhs, fb_rvalue);
2642 /* Go ahead and simplify constant constructors to VECTOR_CST. */
2643 if (TREE_CONSTANT (ctor))
2644 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
2647 /* Vector types use CONSTRUCTOR all the way through gimple
2648 compilation as a general initializer. */
2649 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
2651 enum gimplify_status tret;
2652 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
2653 is_gimple_constructor_elt, fb_rvalue);
2654 if (tret == GS_ERROR)
2661 /* So how did we get a CONSTRUCTOR for a scalar type? */
2665 if (ret == GS_ERROR)
2667 else if (want_value)
2669 append_to_statement_list (*expr_p, pre_p);
2677 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
2678 based on the code of the RHS. We loop for as long as something changes. */
2680 static enum gimplify_status
2681 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2682 tree *post_p, bool want_value)
2684 enum gimplify_status ret = GS_OK;
2686 while (ret != GS_UNHANDLED)
2687 switch (TREE_CODE (*from_p))
2691 /* If we are initializing something from a TARGET_EXPR, strip the
2692 TARGET_EXPR and initialize it directly, if possible. This can't
2693 be done if the initializer is void, since that implies that the
2694 temporary is set in some non-trivial way.
2696 ??? What about code that pulls out the temp and uses it
2697 elsewhere? I think that such code never uses the TARGET_EXPR as
2698 an initializer. If I'm wrong, we'll abort because the temp won't
2699 have any RTL. In that case, I guess we'll need to replace
2700 references somehow. */
2701 tree init = TARGET_EXPR_INITIAL (*from_p);
2703 if (!VOID_TYPE_P (TREE_TYPE (init)))
2714 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2716 gimplify_compound_expr (from_p, pre_p, true);
2721 /* If we're initializing from a CONSTRUCTOR, break this into
2722 individual MODIFY_EXPRs. */
2723 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2726 /* If we're assigning to a non-register type, push the assignment
2727 down into the branches. This is mandatory for ADDRESSABLE types,
2728 since we cannot generate temporaries for such, but it saves a
2729 copy in other cases as well. */
2730 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
2733 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2740 /* Transform 'a = f();' to 'f(&a), a' if f returns in memory. */
2741 if (aggregate_value_p (*from_p, *from_p))
2744 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (*from_p))
2747 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue,
2749 if (ret == GS_ERROR)
2752 arg = build_fold_addr_expr (*to_p);
2753 arg = tree_cons (NULL_TREE, arg, TREE_OPERAND (*from_p, 1));
2754 TREE_OPERAND (*from_p, 1) = arg;
2755 CALL_EXPR_HAS_RETURN_SLOT_ADDR (*from_p) = 1;
2756 /* Don't warn about an unused return value. */
2757 TREE_USED (*from_p) = 1;
2761 gimplify_and_add (*from_p, pre_p);
2780 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2786 PRE_P points to the list where side effects that must happen before
2787 *EXPR_P should be stored.
2789 POST_P points to the list where side effects that must happen after
2790 *EXPR_P should be stored.
2792 WANT_VALUE is nonzero iff we want to use the value of this expression
2793 in another expression. */
2795 static enum gimplify_status
2796 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2798 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2799 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2800 enum gimplify_status ret = GS_UNHANDLED;
2802 #if defined ENABLE_CHECKING
2803 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2807 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2808 if (TREE_CODE (*expr_p) == INIT_EXPR)
2809 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2811 /* See if any simplifications can be done based on what the RHS is. */
2812 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2814 if (ret != GS_UNHANDLED)
2817 /* If the value being copied is of variable width, compute the length
2818 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
2819 before gimplifying any of the operands so that we can resolve any
2820 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
2821 the size of the expression to be copied, not of the destination, so
2822 that is what we must here. */
2823 maybe_with_size_expr (from_p);
2825 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2826 if (ret == GS_ERROR)
2829 ret = gimplify_expr (from_p, pre_p, post_p,
2830 rhs_predicate_for (*to_p), fb_rvalue);
2831 if (ret == GS_ERROR)
2834 /* Now see if the above changed *from_p to something we handle specially. */
2835 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2837 if (ret != GS_UNHANDLED)
2840 /* If we've got a variable sized assignment between two lvalues (i.e. does
2841 not involve a call), then we can make things a bit more straightforward
2842 by converting the assignment to memcpy or memset. */
2843 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
2845 tree from = TREE_OPERAND (*from_p, 0);
2846 tree size = TREE_OPERAND (*from_p, 1);
2848 if (TREE_CODE (from) == CONSTRUCTOR)
2849 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
2850 if (is_gimple_addressable (from))
2853 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
2857 /* If the destination is already simple, nothing else needed. */
2858 if (is_gimple_tmp_var (*to_p) || !want_value)
2865 append_to_statement_list (*expr_p, pre_p);
2872 /* Gimplify a comparison between two variable-sized objects. Do this
2873 with a call to BUILT_IN_MEMCMP. */
2875 static enum gimplify_status
2876 gimplify_variable_sized_compare (tree *expr_p)
2878 tree op0 = TREE_OPERAND (*expr_p, 0);
2879 tree op1 = TREE_OPERAND (*expr_p, 1);
2882 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2883 t = unshare_expr (t);
2884 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2885 args = tree_cons (NULL, t, NULL);
2886 t = build_fold_addr_expr (op1);
2887 args = tree_cons (NULL, t, args);
2888 dest = build_fold_addr_expr (op0);
2889 args = tree_cons (NULL, dest, args);
2890 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2891 t = build_function_call_expr (t, args);
2893 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2898 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2899 points to the expression to gimplify.
2901 Expressions of the form 'a && b' are gimplified to:
2903 a && b ? true : false
2905 gimplify_cond_expr will do the rest.
2907 PRE_P points to the list where side effects that must happen before
2908 *EXPR_P should be stored. */
2910 static enum gimplify_status
2911 gimplify_boolean_expr (tree *expr_p)
2913 /* Preserve the original type of the expression. */
2914 tree type = TREE_TYPE (*expr_p);
2916 *expr_p = build (COND_EXPR, type, *expr_p,
2917 convert (type, boolean_true_node),
2918 convert (type, boolean_false_node));
2923 /* Gimplifies an expression sequence. This function gimplifies each
2924 expression and re-writes the original expression with the last
2925 expression of the sequence in GIMPLE form.
2927 PRE_P points to the list where the side effects for all the
2928 expressions in the sequence will be emitted.
2930 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2931 /* ??? Should rearrange to share the pre-queue with all the indirect
2932 invocations of gimplify_expr. Would probably save on creations
2933 of statement_list nodes. */
2935 static enum gimplify_status
2936 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2942 tree *sub_p = &TREE_OPERAND (t, 0);
2944 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2945 gimplify_compound_expr (sub_p, pre_p, false);
2947 gimplify_stmt (sub_p);
2948 append_to_statement_list (*sub_p, pre_p);
2950 t = TREE_OPERAND (t, 1);
2952 while (TREE_CODE (t) == COMPOUND_EXPR);
2959 gimplify_stmt (expr_p);
2964 /* Gimplifies a statement list. These may be created either by an
2965 enlightened front-end, or by shortcut_cond_expr. */
2967 static enum gimplify_status
2968 gimplify_statement_list (tree *expr_p)
2970 tree_stmt_iterator i = tsi_start (*expr_p);
2972 while (!tsi_end_p (i))
2976 gimplify_stmt (tsi_stmt_ptr (i));
2981 else if (TREE_CODE (t) == STATEMENT_LIST)
2983 tsi_link_before (&i, t, TSI_SAME_STMT);
2993 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2994 gimplify. After gimplification, EXPR_P will point to a new temporary
2995 that holds the original value of the SAVE_EXPR node.
2997 PRE_P points to the list where side effects that must happen before
2998 *EXPR_P should be stored. */
3000 static enum gimplify_status
3001 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3003 enum gimplify_status ret = GS_ALL_DONE;
3006 #if defined ENABLE_CHECKING
3007 if (TREE_CODE (*expr_p) != SAVE_EXPR)
3011 val = TREE_OPERAND (*expr_p, 0);
3013 /* If the operand is already a GIMPLE temporary, just re-write the
3015 if (is_gimple_tmp_var (val))
3017 /* The operand may be a void-valued expression such as SAVE_EXPRs
3018 generated by the Java frontend for class initialization. It is
3019 being executed only for its side-effects. */
3020 else if (TREE_TYPE (val) == void_type_node)
3022 tree body = TREE_OPERAND (*expr_p, 0);
3023 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
3024 append_to_statement_list (body, pre_p);
3028 *expr_p = TREE_OPERAND (*expr_p, 0)
3029 = get_initialized_tmp_var (val, pre_p, post_p);
3034 /* Re-write the ADDR_EXPR node pointed by EXPR_P
3041 PRE_P points to the list where side effects that must happen before
3042 *EXPR_P should be stored.
3044 POST_P points to the list where side effects that must happen after
3045 *EXPR_P should be stored. */
3047 static enum gimplify_status
3048 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3050 tree expr = *expr_p;
3051 tree op0 = TREE_OPERAND (expr, 0);
3052 enum gimplify_status ret;
3054 switch (TREE_CODE (op0))
3057 /* Check if we are dealing with an expression of the form '&*ptr'.
3058 While the front end folds away '&*ptr' into 'ptr', these
3059 expressions may be generated internally by the compiler (e.g.,
3060 builtins like __builtin_va_end). */
3061 *expr_p = TREE_OPERAND (op0, 0);
3066 /* Fold &a[6] to (&a + 6). */
3067 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
3070 /* This added an INDIRECT_REF. Fold it away. */
3071 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3074 case VIEW_CONVERT_EXPR:
3075 /* Take the address of our operand and then convert it to the type of
3078 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3079 all clear. The impact of this transformation is even less clear. */
3080 *expr_p = fold_convert (TREE_TYPE (expr),
3081 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3086 /* We use fb_either here because the C frontend sometimes takes
3087 the address of a call that returns a struct; see
3088 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3089 the implied temporary explicit. */
3090 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3091 is_gimple_addressable, fb_either);
3092 if (ret != GS_ERROR)
3094 /* The above may have made an INDIRECT_REF (e.g, Ada's NULL_EXPR),
3095 so check for it here. It's not worth checking for the other
3097 if (TREE_CODE (TREE_OPERAND (expr, 0)) == INDIRECT_REF)
3099 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3103 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3105 recompute_tree_invarant_for_addr_expr (expr);
3107 /* Mark the RHS addressable. */
3108 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3116 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3117 value; output operands should be a gimple lvalue. */
3119 static enum gimplify_status
3120 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3122 tree expr = *expr_p;
3123 int noutputs = list_length (ASM_OUTPUTS (expr));
3124 const char **oconstraints
3125 = (const char **) alloca ((noutputs) * sizeof (const char *));
3128 const char *constraint;
3129 bool allows_mem, allows_reg, is_inout;
3130 enum gimplify_status ret, tret;
3133 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
3137 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3139 oconstraints[i] = constraint
3140 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3142 parse_output_constraint (&constraint, i, 0, 0,
3143 &allows_mem, &allows_reg, &is_inout);
3145 if (!allows_reg && allows_mem)
3146 lang_hooks.mark_addressable (TREE_VALUE (link));
3148 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3149 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3150 fb_lvalue | fb_mayfail);
3151 if (tret == GS_ERROR)
3153 error ("invalid lvalue in asm output %d", i);
3159 /* An input/output operand. To give the optimizers more
3160 flexibility, split it into separate input and output
3164 size_t constraint_len = strlen (constraint);
3166 /* Turn the in/out constraint into an output constraint. */
3167 char *p = xstrdup (constraint);
3169 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3172 /* And add a matching input constraint. */
3175 sprintf (buf, "%d", i);
3176 input = build_string (strlen (buf), buf);
3179 input = build_string (constraint_len - 1, constraint + 1);
3180 input = build_tree_list (build_tree_list (NULL_TREE, input),
3181 unshare_expr (TREE_VALUE (link)));
3182 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3186 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3189 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3190 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3191 oconstraints, &allows_mem, &allows_reg);
3193 /* If the operand is a memory input, it should be an lvalue. */
3194 if (!allows_reg && allows_mem)
3196 lang_hooks.mark_addressable (TREE_VALUE (link));
3197 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3198 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3199 if (tret == GS_ERROR)
3201 error ("memory input %d is not directly addressable", i);
3207 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3208 is_gimple_val, fb_rvalue);
3209 if (tret == GS_ERROR)
3217 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3218 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3219 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3220 return to this function.
3222 FIXME should we complexify the prequeue handling instead? Or use flags
3223 for all the cleanups and let the optimizer tighten them up? The current
3224 code seems pretty fragile; it will break on a cleanup within any
3225 non-conditional nesting. But any such nesting would be broken, anyway;
3226 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3227 and continues out of it. We can do that at the RTL level, though, so
3228 having an optimizer to tighten up try/finally regions would be a Good
3231 static enum gimplify_status
3232 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3234 tree_stmt_iterator iter;
3237 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3239 /* We only care about the number of conditions between the innermost
3240 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3241 int old_conds = gimplify_ctxp->conditions;
3242 gimplify_ctxp->conditions = 0;
3244 body = TREE_OPERAND (*expr_p, 0);
3245 gimplify_to_stmt_list (&body);
3247 gimplify_ctxp->conditions = old_conds;
3249 for (iter = tsi_start (body); !tsi_end_p (iter); )
3251 tree *wce_p = tsi_stmt_ptr (iter);
3254 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3256 if (tsi_one_before_end_p (iter))
3258 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
3266 sl = tsi_split_statement_list_after (&iter);
3267 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
3268 append_to_statement_list (TREE_OPERAND (wce, 0),
3269 &TREE_OPERAND (tfe, 1));
3271 iter = tsi_start (sl);
3281 append_to_statement_list (body, pre_p);
3291 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3292 is the cleanup action required. */
3295 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
3299 /* Errors can result in improperly nested cleanups. Which results in
3300 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3301 if (errorcount || sorrycount)
3304 if (gimple_conditional_context ())
3306 /* If we're in a conditional context, this is more complex. We only
3307 want to run the cleanup if we actually ran the initialization that
3308 necessitates it, but we want to run it after the end of the
3309 conditional context. So we wrap the try/finally around the
3310 condition and use a flag to determine whether or not to actually
3311 run the destructor. Thus
3315 becomes (approximately)
3319 if (test) { A::A(temp); flag = 1; val = f(temp); }
3322 if (flag) A::~A(temp);
3327 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3328 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3329 boolean_false_node);
3330 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3332 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3333 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3334 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3335 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3336 append_to_statement_list (ftrue, pre_p);
3338 /* Because of this manipulation, and the EH edges that jump
3339 threading cannot redirect, the temporary (VAR) will appear
3340 to be used uninitialized. Don't warn. */
3341 TREE_NO_WARNING (var) = 1;
3345 wce = build (WITH_CLEANUP_EXPR, void_type_node, cleanup);
3346 append_to_statement_list (wce, pre_p);
3349 gimplify_stmt (&TREE_OPERAND (wce, 0));
3352 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3354 static enum gimplify_status
3355 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3357 tree targ = *expr_p;
3358 tree temp = TARGET_EXPR_SLOT (targ);
3359 tree init = TARGET_EXPR_INITIAL (targ);
3360 enum gimplify_status ret;
3364 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3365 to the temps list. */
3366 gimple_add_tmp_var (temp);
3368 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3369 expression is supposed to initialize the slot. */
3370 if (VOID_TYPE_P (TREE_TYPE (init)))
3371 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3374 /* Special handling for BIND_EXPR can result in fewer temps. */
3376 if (TREE_CODE (init) == BIND_EXPR)
3377 gimplify_bind_expr (&init, temp, pre_p);
3380 init = build (MODIFY_EXPR, void_type_node, temp, init);
3381 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3385 if (ret == GS_ERROR)
3387 append_to_statement_list (init, pre_p);
3389 /* If needed, push the cleanup for the temp. */
3390 if (TARGET_EXPR_CLEANUP (targ))
3392 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3393 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3396 /* Only expand this once. */
3397 TREE_OPERAND (targ, 3) = init;
3398 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3400 else if (!DECL_SEEN_IN_BIND_EXPR_P (temp))
3401 /* We should have expanded this before. */
3408 /* Gimplification of expression trees. */
3410 /* Gimplify an expression which appears at statement context; usually, this
3411 means replacing it with a suitably gimple STATEMENT_LIST. */
3414 gimplify_stmt (tree *stmt_p)
3416 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3419 /* Similarly, but force the result to be a STATEMENT_LIST. */
3422 gimplify_to_stmt_list (tree *stmt_p)
3424 gimplify_stmt (stmt_p);
3426 *stmt_p = alloc_stmt_list ();
3427 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3430 *stmt_p = alloc_stmt_list ();
3431 append_to_statement_list (t, stmt_p);
3436 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3437 gimplification failed.
3439 PRE_P points to the list where side effects that must happen before
3440 EXPR should be stored.
3442 POST_P points to the list where side effects that must happen after
3443 EXPR should be stored, or NULL if there is no suitable list. In
3444 that case, we copy the result to a temporary, emit the
3445 post-effects, and then return the temporary.
3447 GIMPLE_TEST_F points to a function that takes a tree T and
3448 returns nonzero if T is in the GIMPLE form requested by the
3449 caller. The GIMPLE predicates are in tree-gimple.c.
3451 This test is used twice. Before gimplification, the test is
3452 invoked to determine whether *EXPR_P is already gimple enough. If
3453 that fails, *EXPR_P is gimplified according to its code and
3454 GIMPLE_TEST_F is called again. If the test still fails, then a new
3455 temporary variable is created and assigned the value of the
3456 gimplified expression.
3458 FALLBACK tells the function what sort of a temporary we want. If the 1
3459 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3460 If both are set, either is OK, but an lvalue is preferable.
3462 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3463 iterates until solution. */
3465 enum gimplify_status
3466 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3467 bool (* gimple_test_f) (tree), fallback_t fallback)
3470 tree internal_pre = NULL_TREE;
3471 tree internal_post = NULL_TREE;
3473 int is_statement = (pre_p == NULL);
3474 location_t saved_location;
3475 enum gimplify_status ret;
3477 save_expr = *expr_p;
3478 if (save_expr == NULL_TREE)
3481 /* We used to check the predicate here and return immediately if it
3482 succeeds. This is wrong; the design is for gimplification to be
3483 idempotent, and for the predicates to only test for valid forms, not
3484 whether they are fully simplified. */
3486 /* Set up our internal queues if needed. */
3488 pre_p = &internal_pre;
3490 post_p = &internal_post;
3492 saved_location = input_location;
3493 if (save_expr != error_mark_node
3494 && EXPR_HAS_LOCATION (*expr_p))
3495 input_location = EXPR_LOCATION (*expr_p);
3497 /* Loop over the specific gimplifiers until the toplevel node
3498 remains the same. */
3501 /* Strip away as many useless type conversions as possible
3503 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
3505 /* Remember the expr. */
3506 save_expr = *expr_p;
3508 /* Die, die, die, my darling. */
3509 if (save_expr == error_mark_node
3510 || (TREE_TYPE (save_expr)
3511 && TREE_TYPE (save_expr) == error_mark_node))
3517 /* Do any language-specific gimplification. */
3518 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3521 if (*expr_p == NULL_TREE)
3523 if (*expr_p != save_expr)
3526 else if (ret != GS_UNHANDLED)
3530 switch (TREE_CODE (*expr_p))
3532 /* First deal with the special cases. */
3534 case POSTINCREMENT_EXPR:
3535 case POSTDECREMENT_EXPR:
3536 case PREINCREMENT_EXPR:
3537 case PREDECREMENT_EXPR:
3538 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3539 fallback != fb_none);
3543 case ARRAY_RANGE_REF:
3547 case VIEW_CONVERT_EXPR:
3548 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3549 fallback ? fallback : fb_rvalue);
3553 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3557 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
3564 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3569 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3570 fallback != fb_none);
3573 case TRUTH_ANDIF_EXPR:
3574 case TRUTH_ORIF_EXPR:
3575 ret = gimplify_boolean_expr (expr_p);
3578 case TRUTH_NOT_EXPR:
3579 TREE_OPERAND (*expr_p, 0)
3580 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3581 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3582 is_gimple_val, fb_rvalue);
3583 recalculate_side_effects (*expr_p);
3587 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3591 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3596 if (IS_EMPTY_STMT (*expr_p))
3602 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3603 || fallback == fb_none)
3605 /* Just strip a conversion to void (or in void context) and
3607 *expr_p = TREE_OPERAND (*expr_p, 0);