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_modify_expr_rhs (tree *, tree *, tree *,
76 tree *, tree *, bool);
77 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
80 /* Return a hash value for a formal temporary table entry. */
83 gimple_tree_hash (const void *p)
85 tree t = ((const elt_t *) p)->val;
86 return iterative_hash_expr (t, 0);
89 /* Compare two formal temporary table entries. */
92 gimple_tree_eq (const void *p1, const void *p2)
94 tree t1 = ((const elt_t *) p1)->val;
95 tree t2 = ((const elt_t *) p2)->val;
96 enum tree_code code = TREE_CODE (t1);
98 if (TREE_CODE (t2) != code
99 || TREE_TYPE (t1) != TREE_TYPE (t2))
102 if (!operand_equal_p (t1, t2, 0))
105 /* Only allow them to compare equal if they also hash equal; otherwise
106 results are nondeterminate, and we fail bootstrap comparison. */
107 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
113 /* Set up a context for the gimplifier. */
116 push_gimplify_context (void)
121 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
122 gimplify_ctxp->temp_htab
123 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
126 /* Tear down a context for the gimplifier. If BODY is non-null, then
127 put the temporaries into the outer BIND_EXPR. Otherwise, put them
128 in the unexpanded_var_list. */
131 pop_gimplify_context (tree body)
133 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
137 declare_tmp_vars (gimplify_ctxp->temps, body);
139 record_vars (gimplify_ctxp->temps);
143 fprintf (stderr, " collisions: %f ",
144 htab_collisions (gimplify_ctxp->temp_htab));
147 htab_delete (gimplify_ctxp->temp_htab);
148 free (gimplify_ctxp);
149 gimplify_ctxp = NULL;
153 gimple_push_bind_expr (tree bind)
155 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
156 gimplify_ctxp->current_bind_expr = bind;
160 gimple_pop_bind_expr (void)
162 gimplify_ctxp->current_bind_expr
163 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
167 gimple_current_bind_expr (void)
169 return gimplify_ctxp->current_bind_expr;
172 /* Returns true iff there is a COND_EXPR between us and the innermost
173 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
176 gimple_conditional_context (void)
178 return gimplify_ctxp->conditions > 0;
181 /* Note that we've entered a COND_EXPR. */
184 gimple_push_condition (void)
186 ++(gimplify_ctxp->conditions);
189 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
190 now, add any conditional cleanups we've seen to the prequeue. */
193 gimple_pop_condition (tree *pre_p)
195 int conds = --(gimplify_ctxp->conditions);
199 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
200 gimplify_ctxp->conditional_cleanups = NULL_TREE;
206 /* A subroutine of append_to_statement_list{,_force}. */
209 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
212 tree_stmt_iterator i;
219 if (t && TREE_CODE (t) == STATEMENT_LIST)
224 *list_p = list = alloc_stmt_list ();
228 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
231 /* Add T to the end of the list container pointed by LIST_P.
232 If T is an expression with no effects, it is ignored. */
235 append_to_statement_list (tree t, tree *list_p)
237 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
240 /* Similar, but the statement is always added, regardless of side effects. */
243 append_to_statement_list_force (tree t, tree *list_p)
245 append_to_statement_list_1 (t, list_p, t != NULL);
248 /* Both gimplify the statement T and append it to LIST_P. */
251 gimplify_and_add (tree t, tree *list_p)
254 append_to_statement_list (t, list_p);
257 /* Strip off a legitimate source ending from the input string NAME of
258 length LEN. Rather than having to know the names used by all of
259 our front ends, we strip off an ending of a period followed by
260 up to five characters. (Java uses ".class".) */
263 remove_suffix (char *name, int len)
267 for (i = 2; i < 8 && len > i; i++)
269 if (name[len - i] == '.')
271 name[len - i] = '\0';
277 /* Create a nameless artificial label and put it in the current function
278 context. Returns the newly created label. */
281 create_artificial_label (void)
283 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
285 DECL_ARTIFICIAL (lab) = 1;
286 DECL_CONTEXT (lab) = current_function_decl;
290 /* Create a new temporary name with PREFIX. Returns an identifier. */
292 static GTY(()) unsigned int tmp_var_id_num;
295 create_tmp_var_name (const char *prefix)
301 char *preftmp = ASTRDUP (prefix);
303 remove_suffix (preftmp, strlen (preftmp));
307 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
308 return get_identifier (tmp_name);
312 /* Create a new temporary variable declaration of type TYPE.
313 Does NOT push it into the current binding. */
316 create_tmp_var_raw (tree type, const char *prefix)
321 /* Make the type of the variable writable. */
322 new_type = build_type_variant (type, 0, 0);
323 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
325 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
327 /* The variable was declared by the compiler. */
328 DECL_ARTIFICIAL (tmp_var) = 1;
329 /* And we don't want debug info for it. */
330 DECL_IGNORED_P (tmp_var) = 1;
332 /* Make the variable writable. */
333 TREE_READONLY (tmp_var) = 0;
335 DECL_EXTERNAL (tmp_var) = 0;
336 TREE_STATIC (tmp_var) = 0;
337 TREE_USED (tmp_var) = 1;
342 /* Create a new temporary variable declaration of type TYPE. DOES push the
343 variable into the current binding. Further, assume that this is called
344 only from gimplification or optimization, at which point the creation of
345 certain types are bugs. */
348 create_tmp_var (tree type, const char *prefix)
352 #if defined ENABLE_CHECKING
353 /* We don't allow types that are addressable (meaning we can't make copies),
354 incomplete, or of variable size. */
355 if (TREE_ADDRESSABLE (type)
356 || !COMPLETE_TYPE_P (type)
357 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
361 tmp_var = create_tmp_var_raw (type, prefix);
362 gimple_add_tmp_var (tmp_var);
366 /* Given a tree, try to return a useful variable name that we can use
367 to prefix a temporary that is being assigned the value of the tree.
368 I.E. given <temp> = &A, return A. */
376 STRIP_NOPS (stripped_decl);
377 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
378 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
381 switch (TREE_CODE (stripped_decl))
384 return get_name (TREE_OPERAND (stripped_decl, 0));
392 /* Create a temporary with a name derived from VAL. Subroutine of
393 lookup_tmp_var; nobody else should call this function. */
396 create_tmp_from_val (tree val)
398 return create_tmp_var (TREE_TYPE (val), get_name (val));
401 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
402 an existing expression temporary. */
405 lookup_tmp_var (tree val, bool is_formal)
407 if (!is_formal || TREE_SIDE_EFFECTS (val))
408 return create_tmp_from_val (val);
415 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
418 elt_p = xmalloc (sizeof (*elt_p));
420 elt_p->temp = create_tmp_from_val (val);
421 TREE_READONLY (elt_p->temp) = 1;
422 *slot = (void *) elt_p;
425 elt_p = (elt_t *) *slot;
431 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
432 in gimplify_expr. Only use this function if:
434 1) The value of the unfactored expression represented by VAL will not
435 change between the initialization and use of the temporary, and
436 2) The temporary will not be otherwise modified.
438 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
439 and #2 means it is inappropriate for && temps.
441 For other cases, use get_initialized_tmp_var instead. */
444 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
449 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
451 t = lookup_tmp_var (val, is_formal);
453 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
455 class = TREE_CODE_CLASS (TREE_CODE (val));
456 if (EXPR_HAS_LOCATION (val))
457 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
459 SET_EXPR_LOCATION (mod, input_location);
461 /* gimplify_modify_expr might want to reduce this further. */
462 gimplify_and_add (mod, pre_p);
467 get_formal_tmp_var (tree val, tree *pre_p)
469 return internal_get_tmp_var (val, pre_p, NULL, true);
472 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
473 are as in gimplify_expr. */
476 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
478 return internal_get_tmp_var (val, pre_p, post_p, false);
481 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
484 is_gimple_tmp_var (tree t)
486 /* FIXME this could trigger for other local artificials, too. */
487 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
488 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
491 /* Declares all the variables in VARS in SCOPE. */
494 declare_tmp_vars (tree vars, tree scope)
501 /* C99 mode puts the default 'return 0;' for main outside the outer
502 braces. So drill down until we find an actual scope. */
503 while (TREE_CODE (scope) == COMPOUND_EXPR)
504 scope = TREE_OPERAND (scope, 0);
506 if (TREE_CODE (scope) != BIND_EXPR)
509 temps = nreverse (last);
510 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
511 BIND_EXPR_VARS (scope) = temps;
513 /* We don't add the temps to the block for this BIND_EXPR, as we're
514 not interested in debugging info for them. */
519 gimple_add_tmp_var (tree tmp)
521 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
524 DECL_CONTEXT (tmp) = current_function_decl;
525 tmp->decl.seen_in_bind_expr = 1;
529 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
530 gimplify_ctxp->temps = tmp;
535 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
538 /* Determines whether to assign a locus to the statement STMT. */
541 should_carry_locus_p (tree stmt)
543 /* Don't emit a line note for a label. We particularly don't want to
544 emit one for the break label, since it doesn't actually correspond
545 to the beginning of the loop/switch. */
546 if (TREE_CODE (stmt) == LABEL_EXPR)
549 /* Do not annotate empty statements, since it confuses gcov. */
550 if (!TREE_SIDE_EFFECTS (stmt))
557 annotate_one_with_locus (tree t, location_t locus)
559 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
560 && ! EXPR_HAS_LOCATION (t)
561 && should_carry_locus_p (t))
562 SET_EXPR_LOCATION (t, locus);
566 annotate_all_with_locus (tree *stmt_p, location_t locus)
568 tree_stmt_iterator i;
573 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
575 tree t = tsi_stmt (i);
577 #ifdef ENABLE_CHECKING
578 /* Assuming we've already been gimplified, we shouldn't
579 see nested chaining constructs anymore. */
580 if (TREE_CODE (t) == STATEMENT_LIST
581 || TREE_CODE (t) == COMPOUND_EXPR)
585 annotate_one_with_locus (t, locus);
589 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
590 These nodes model computations that should only be done once. If we
591 were to unshare something like SAVE_EXPR(i++), the gimplification
592 process would create wrong code. */
595 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
597 enum tree_code code = TREE_CODE (*tp);
598 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
599 if (TREE_CODE_CLASS (code) == 't'
600 || TREE_CODE_CLASS (code) == 'd'
601 || TREE_CODE_CLASS (code) == 'c'
602 || code == SAVE_EXPR || code == TARGET_EXPR
603 /* We can't do anything sensible with a BLOCK used as an expression,
604 but we also can't abort when we see it because of non-expression
605 uses. So just avert our eyes and cross our fingers. Silly Java. */
608 else if (code == BIND_EXPR)
611 copy_tree_r (tp, walk_subtrees, data);
616 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
617 after VA_ARG_EXPRs are properly lowered. */
620 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
621 void *data ATTRIBUTE_UNUSED)
624 TREE_THIS_VOLATILE (*tp) = 1;
630 /* Callback for walk_tree to unshare most of the shared trees rooted at
631 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
632 then *TP is deep copied by calling copy_tree_r.
634 This unshares the same trees as copy_tree_r with the exception of
635 SAVE_EXPR nodes. These nodes model computations that should only be
636 done once. If we were to unshare something like SAVE_EXPR(i++), the
637 gimplification process would create wrong code. */
640 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
641 void *data ATTRIBUTE_UNUSED)
644 enum tree_code code = TREE_CODE (t);
646 /* Skip types, decls, and constants. But we do want to look at their
647 types and the bounds of types. Mark them as visited so we properly
648 unmark their subtrees on the unmark pass. If we've already seen them,
649 don't look down further. */
650 if (TREE_CODE_CLASS (code) == 't'
651 || TREE_CODE_CLASS (code) == 'd'
652 || TREE_CODE_CLASS (code) == 'c')
654 if (TREE_VISITED (t))
657 TREE_VISITED (t) = 1;
660 /* Special-case BIND_EXPR. We should never be copying these, therefore
661 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
662 double processing of the DECL_INITIAL, which could be seen via both
663 the BIND_EXPR_VARS and a DECL_EXPR. */
664 else if (code == BIND_EXPR)
666 if (TREE_VISITED (t))
668 TREE_VISITED (t) = 1;
670 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
673 /* If this node has been visited already, unshare it and don't look
675 else if (TREE_VISITED (t))
677 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
681 /* Otherwise, mark the tree as visited and keep looking. */
684 TREE_VISITED (t) = 1;
685 if (TREE_CODE (*tp) == VA_ARG_EXPR
686 && targetm.calls.gimplify_va_arg_expr == NULL)
688 /* Mark any _DECL inside the operand as volatile to avoid
689 the optimizers messing around with it. We have to do this
690 early, otherwise we might mark a variable as volatile
691 after we gimplify other statements that use the variable
692 assuming it's not volatile. */
694 /* FIXME once most targets define the above hook, this should
695 go away (perhaps along with the #include "target.h"). */
696 walk_tree (&TREE_OPERAND (*tp, 0), mark_decls_volatile_r,
705 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
706 void *data ATTRIBUTE_UNUSED)
708 if (TREE_VISITED (*tp))
709 TREE_VISITED (*tp) = 0;
716 /* Unshare all the trees in BODY_P, a pointer to the body of FNDECL, and the
717 bodies of any nested functions. */
720 unshare_body (tree *body_p, tree fndecl)
722 struct cgraph_node *cgn = cgraph_node (fndecl);
724 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
725 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
726 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
729 /* Likewise, but mark all trees as not visited. */
732 unvisit_body (tree *body_p, tree fndecl)
734 struct cgraph_node *cgn = cgraph_node (fndecl);
736 walk_tree (body_p, unmark_visited_r, NULL, NULL);
737 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
738 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
741 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
744 unshare_all_trees (tree t)
746 walk_tree (&t, copy_if_shared_r, NULL, NULL);
747 walk_tree (&t, unmark_visited_r, NULL, NULL);
750 /* Unconditionally make an unshared copy of EXPR. This is used when using
751 stored expressions which span multiple functions, such as BINFO_VTABLE,
752 as the normal unsharing process can't tell that they're shared. */
755 unshare_expr (tree expr)
757 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
761 /* A terser interface for building a representation of a exception
765 gimple_build_eh_filter (tree body, tree allowed, tree failure)
769 /* FIXME should the allowed types go in TREE_TYPE? */
770 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
771 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
773 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
774 append_to_statement_list (body, &TREE_OPERAND (t, 0));
780 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
781 contain statements and have a value. Assign its value to a temporary
782 and give it void_type_node. Returns the temporary, or NULL_TREE if
783 WRAPPER was already void. */
786 voidify_wrapper_expr (tree wrapper, tree temp)
788 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
790 tree *p, sub = wrapper;
793 /* Set p to point to the body of the wrapper. */
794 switch (TREE_CODE (sub))
797 /* For a BIND_EXPR, the body is operand 1. */
798 p = &BIND_EXPR_BODY (sub);
802 p = &TREE_OPERAND (sub, 0);
806 /* Advance to the last statement. Set all container types to void. */
807 if (TREE_CODE (*p) == STATEMENT_LIST)
809 tree_stmt_iterator i = tsi_last (*p);
810 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
814 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
816 TREE_SIDE_EFFECTS (*p) = 1;
817 TREE_TYPE (*p) = void_type_node;
821 if (p == NULL || IS_EMPTY_STMT (*p))
823 /* Look through exception handling. */
824 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
825 || TREE_CODE (*p) == TRY_CATCH_EXPR)
830 /* The C++ frontend already did this for us. */
831 else if (TREE_CODE (*p) == INIT_EXPR
832 || TREE_CODE (*p) == TARGET_EXPR)
833 temp = TREE_OPERAND (*p, 0);
834 /* If we're returning a dereference, move the dereference
835 outside the wrapper. */
836 else if (TREE_CODE (*p) == INDIRECT_REF)
838 tree ptr = TREE_OPERAND (*p, 0);
839 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
840 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
841 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
842 /* If this is a BIND_EXPR for a const inline function, it might not
843 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
844 TREE_SIDE_EFFECTS (wrapper) = 1;
849 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
850 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
851 TREE_SIDE_EFFECTS (wrapper) = 1;
854 TREE_TYPE (wrapper) = void_type_node;
861 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
862 a temporary through which they communicate. */
865 build_stack_save_restore (tree *save, tree *restore)
867 tree save_call, tmp_var;
870 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
872 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
874 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
876 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
877 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
880 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
882 static enum gimplify_status
883 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
885 tree bind_expr = *expr_p;
886 bool old_save_stack = gimplify_ctxp->save_stack;
889 temp = voidify_wrapper_expr (bind_expr, temp);
891 /* Mark variables seen in this bind expr. */
892 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
893 t->decl.seen_in_bind_expr = 1;
895 gimple_push_bind_expr (bind_expr);
896 gimplify_ctxp->save_stack = false;
898 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
900 if (gimplify_ctxp->save_stack)
902 tree stack_save, stack_restore;
904 /* Save stack on entry and restore it on exit. Add a try_finally
905 block to achieve this. Note that mudflap depends on the
906 format of the emitted code: see mx_register_decls(). */
907 build_stack_save_restore (&stack_save, &stack_restore);
909 t = build (TRY_FINALLY_EXPR, void_type_node,
910 BIND_EXPR_BODY (bind_expr), NULL_TREE);
911 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
913 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
914 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
915 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
918 gimplify_ctxp->save_stack = old_save_stack;
919 gimple_pop_bind_expr ();
924 append_to_statement_list (bind_expr, pre_p);
931 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
932 GIMPLE value, it is assigned to a new temporary and the statement is
933 re-written to return the temporary.
935 PRE_P points to the list where side effects that must happen before
936 STMT should be stored. */
938 static enum gimplify_status
939 gimplify_return_expr (tree stmt, tree *pre_p)
941 tree ret_expr = TREE_OPERAND (stmt, 0);
942 tree result_decl, result;
944 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
947 if (ret_expr == error_mark_node)
950 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
951 result_decl = NULL_TREE;
954 result_decl = TREE_OPERAND (ret_expr, 0);
955 #ifdef ENABLE_CHECKING
956 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
957 && TREE_CODE (ret_expr) != INIT_EXPR)
958 || TREE_CODE (result_decl) != RESULT_DECL)
963 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
964 Recall that aggregate_value_p is FALSE for any aggregate type that is
965 returned in registers. If we're returning values in registers, then
966 we don't want to extend the lifetime of the RESULT_DECL, particularly
967 across another call. In addition, for those aggregates for which
968 hard_function_value generates a PARALLEL, we'll abort during normal
969 expansion of structure assignments; there's special code in expand_return
970 to handle this case that does not exist in expand_expr. */
972 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
973 result = result_decl;
974 else if (gimplify_ctxp->return_temp)
975 result = gimplify_ctxp->return_temp;
978 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
980 /* ??? With complex control flow (usually involving abnormal edges),
981 we can wind up warning about an uninitialized value for this. Due
982 to how this variable is constructed and initialized, this is never
983 true. Give up and never warn. */
984 TREE_NO_WARNING (result) = 1;
986 gimplify_ctxp->return_temp = result;
989 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
990 Then gimplify the whole thing. */
991 if (result != result_decl)
992 TREE_OPERAND (ret_expr, 0) = result;
994 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
996 /* If we didn't use a temporary, then the result is just the result_decl.
997 Otherwise we need a simple copy. This should already be gimple. */
998 if (result == result_decl)
1001 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
1002 TREE_OPERAND (stmt, 0) = ret_expr;
1007 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1008 and initialization explicit. */
1010 static enum gimplify_status
1011 gimplify_decl_expr (tree *stmt_p)
1013 tree stmt = *stmt_p;
1014 tree decl = DECL_EXPR_DECL (stmt);
1016 *stmt_p = NULL_TREE;
1018 if (TREE_TYPE (decl) == error_mark_node)
1021 else if (TREE_CODE (decl) == TYPE_DECL)
1022 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1024 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1026 tree init = DECL_INITIAL (decl);
1028 if (!TREE_CONSTANT (DECL_SIZE (decl)))
1030 /* This is a variable-sized decl. Simplify its size and mark it
1031 for deferred expansion. Note that mudflap depends on the format
1032 of the emitted code: see mx_register_decls(). */
1035 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1036 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
1037 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
1039 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
1040 t = build_fold_addr_expr (decl);
1041 args = tree_cons (NULL, t, args);
1042 t = implicit_built_in_decls[BUILT_IN_STACK_ALLOC];
1043 t = build_function_call_expr (t, args);
1045 gimplify_and_add (t, stmt_p);
1046 DECL_DEFER_OUTPUT (decl) = 1;
1049 if (init && init != error_mark_node)
1051 if (!TREE_STATIC (decl))
1053 DECL_INITIAL (decl) = NULL_TREE;
1054 init = build (MODIFY_EXPR, void_type_node, decl, init);
1055 gimplify_and_add (init, stmt_p);
1058 /* We must still examine initializers for static variables
1059 as they may contain a label address. */
1060 walk_tree (&init, force_labels_r, NULL, NULL);
1063 /* This decl isn't mentioned in the enclosing block, so add it to the
1064 list of temps. FIXME it seems a bit of a kludge to say that
1065 anonymous artificial vars aren't pushed, but everything else is. */
1066 if (DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1067 gimple_add_tmp_var (decl);
1073 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1074 and replacing the LOOP_EXPR with goto, but if the loop contains an
1075 EXIT_EXPR, we need to append a label for it to jump to. */
1077 static enum gimplify_status
1078 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1080 tree saved_label = gimplify_ctxp->exit_label;
1081 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1082 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1084 append_to_statement_list (start_label, pre_p);
1086 gimplify_ctxp->exit_label = NULL_TREE;
1088 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1090 if (gimplify_ctxp->exit_label)
1092 append_to_statement_list (jump_stmt, pre_p);
1093 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1096 *expr_p = jump_stmt;
1098 gimplify_ctxp->exit_label = saved_label;
1103 /* Compare two case labels. Because the front end should already have
1104 made sure that case ranges do not overlap, it is enough to only compare
1105 the CASE_LOW values of each case label. */
1108 compare_case_labels (const void *p1, const void *p2)
1110 tree case1 = *(tree *)p1;
1111 tree case2 = *(tree *)p2;
1113 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1116 /* Sort the case labels in LABEL_VEC in ascending order. */
1119 sort_case_labels (tree label_vec)
1121 size_t len = TREE_VEC_LENGTH (label_vec);
1122 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1124 if (CASE_LOW (default_case))
1128 /* The last label in the vector should be the default case
1130 for (i = 0; i < len; ++i)
1132 tree t = TREE_VEC_ELT (label_vec, i);
1136 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1137 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1143 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1144 compare_case_labels);
1147 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1150 static enum gimplify_status
1151 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1153 tree switch_expr = *expr_p;
1154 enum gimplify_status ret;
1156 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1157 is_gimple_val, fb_rvalue);
1159 if (SWITCH_BODY (switch_expr))
1161 varray_type labels, saved_labels;
1162 tree label_vec, default_case = NULL_TREE;
1165 /* If someone can be bothered to fill in the labels, they can
1166 be bothered to null out the body too. */
1167 if (SWITCH_LABELS (switch_expr))
1170 saved_labels = gimplify_ctxp->case_labels;
1171 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1173 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1175 labels = gimplify_ctxp->case_labels;
1176 gimplify_ctxp->case_labels = saved_labels;
1178 len = VARRAY_ACTIVE_SIZE (labels);
1180 for (i = 0; i < len; ++i)
1182 tree t = VARRAY_TREE (labels, i);
1185 /* The default case must be the last label in the list. */
1187 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1193 label_vec = make_tree_vec (len + 1);
1194 SWITCH_LABELS (*expr_p) = label_vec;
1195 append_to_statement_list (switch_expr, pre_p);
1199 /* If the switch has no default label, add one, so that we jump
1200 around the switch body. */
1201 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1202 NULL_TREE, create_artificial_label ());
1203 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1204 *expr_p = build (LABEL_EXPR, void_type_node,
1205 CASE_LABEL (default_case));
1208 *expr_p = SWITCH_BODY (switch_expr);
1210 for (i = 0; i < len; ++i)
1211 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1212 TREE_VEC_ELT (label_vec, len) = default_case;
1214 sort_case_labels (label_vec);
1216 SWITCH_BODY (switch_expr) = NULL;
1218 else if (!SWITCH_LABELS (switch_expr))
1224 static enum gimplify_status
1225 gimplify_case_label_expr (tree *expr_p)
1227 tree expr = *expr_p;
1228 if (gimplify_ctxp->case_labels)
1229 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1232 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1236 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1237 a (possibly empty) body. */
1239 static enum gimplify_status
1240 gimplify_labeled_block_expr (tree *expr_p)
1242 tree body = LABELED_BLOCK_BODY (*expr_p);
1243 tree label = LABELED_BLOCK_LABEL (*expr_p);
1246 DECL_CONTEXT (label) = current_function_decl;
1247 t = build (LABEL_EXPR, void_type_node, label);
1248 if (body != NULL_TREE)
1249 t = build (COMPOUND_EXPR, void_type_node, body, t);
1255 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1257 static enum gimplify_status
1258 gimplify_exit_block_expr (tree *expr_p)
1260 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1263 /* First operand must be a LABELED_BLOCK_EXPR, which should
1264 already be lowered (or partially lowered) when we get here. */
1265 #if defined ENABLE_CHECKING
1266 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1270 label = LABELED_BLOCK_LABEL (labeled_block);
1271 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1276 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1280 build_and_jump (tree *label_p)
1282 if (label_p == NULL)
1283 /* If there's nowhere to jump, just fall through. */
1286 if (*label_p == NULL_TREE)
1288 tree label = create_artificial_label ();
1292 return build1 (GOTO_EXPR, void_type_node, *label_p);
1295 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1296 This also involves building a label to jump to and communicating it to
1297 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1299 static enum gimplify_status
1300 gimplify_exit_expr (tree *expr_p)
1302 tree cond = TREE_OPERAND (*expr_p, 0);
1305 expr = build_and_jump (&gimplify_ctxp->exit_label);
1306 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1312 /* A helper function to be called via walk_tree. Mark all labels under *TP
1313 as being forced. To be called for DECL_INITIAL of static variables. */
1316 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1320 if (TREE_CODE (*tp) == LABEL_DECL)
1321 FORCED_LABEL (*tp) = 1;
1326 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1327 different from its canonical type, wrap the whole thing inside a
1328 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1331 The canonical type of a COMPONENT_REF is the type of the field being
1332 referenced--unless the field is a bit-field which can be read directly
1333 in a smaller mode, in which case the canonical type is the
1334 sign-appropriate type corresponding to that mode. */
1337 canonicalize_component_ref (tree *expr_p)
1339 tree expr = *expr_p;
1342 if (TREE_CODE (expr) != COMPONENT_REF)
1345 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1346 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1348 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1350 if (TREE_TYPE (expr) != type)
1352 tree old_type = TREE_TYPE (expr);
1354 /* Set the type of the COMPONENT_REF to the underlying type. */
1355 TREE_TYPE (expr) = type;
1357 /* And wrap the whole thing inside a NOP_EXPR. */
1358 expr = build1 (NOP_EXPR, old_type, expr);
1364 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1365 to foo, embed that change in the ADDR_EXPR by converting
1370 where L is the lower bound. For simplicity, only do this for constant
1374 canonicalize_addr_expr (tree *expr_p)
1376 tree expr = *expr_p;
1377 tree ctype = TREE_TYPE (expr);
1378 tree addr_expr = TREE_OPERAND (expr, 0);
1379 tree atype = TREE_TYPE (addr_expr);
1380 tree dctype, datype, ddatype, otype, obj_expr;
1382 /* Both cast and addr_expr types should be pointers. */
1383 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1386 /* The addr_expr type should be a pointer to an array. */
1387 datype = TREE_TYPE (atype);
1388 if (TREE_CODE (datype) != ARRAY_TYPE)
1391 /* Both cast and addr_expr types should address the same object type. */
1392 dctype = TREE_TYPE (ctype);
1393 ddatype = TREE_TYPE (datype);
1394 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1397 /* The addr_expr and the object type should match. */
1398 obj_expr = TREE_OPERAND (addr_expr, 0);
1399 otype = TREE_TYPE (obj_expr);
1400 if (!lang_hooks.types_compatible_p (otype, datype))
1403 /* The lower bound and element sizes must be constant. */
1404 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1405 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1406 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1409 /* All checks succeeded. Build a new node to merge the cast. */
1410 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1411 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1412 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1413 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1414 size_int (TYPE_ALIGN (dctype)
1416 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1419 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1420 underneath as appropriate. */
1422 static enum gimplify_status
1423 gimplify_conversion (tree *expr_p)
1425 /* Strip away as many useless type conversions as possible
1427 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1429 /* If we still have a conversion at the toplevel, then strip
1430 away all but the outermost conversion. */
1431 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1433 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1435 /* And remove the outermost conversion if it's useless. */
1436 if (tree_ssa_useless_type_conversion (*expr_p))
1437 *expr_p = TREE_OPERAND (*expr_p, 0);
1440 /* If we still have a conversion at the toplevel,
1441 then canonicalize some constructs. */
1442 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1444 tree sub = TREE_OPERAND (*expr_p, 0);
1446 /* If a NOP conversion is changing the type of a COMPONENT_REF
1447 expression, then canonicalize its type now in order to expose more
1448 redundant conversions. */
1449 if (TREE_CODE (sub) == COMPONENT_REF)
1450 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1452 /* If a NOP conversion is changing a pointer to array of foo
1453 to a pointer to foo, embed that change in the ADDR_EXPR. */
1454 else if (TREE_CODE (sub) == ADDR_EXPR)
1455 canonicalize_addr_expr (expr_p);
1461 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1463 static enum gimplify_status
1464 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1466 tree op1 = TREE_OPERAND (*expr_p, 0);
1467 tree op2 = TREE_OPERAND (*expr_p, 1);
1468 enum tree_code code;
1469 enum gimplify_status r0, r1;
1471 if (TREE_CODE (*expr_p) == MIN_EXPR)
1476 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1477 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1479 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1480 build (code, boolean_type_node, op1, op2),
1483 if (r0 == GS_ERROR || r1 == GS_ERROR)
1489 /* Subroutine of gimplify_compound_lval.
1490 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1492 static enum gimplify_status
1493 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1495 tree array = TREE_OPERAND (*expr_p, 0);
1496 tree arrtype = TREE_TYPE (array);
1497 tree elttype = TREE_TYPE (arrtype);
1498 tree size = array_ref_element_size (*expr_p);
1499 tree ptrtype = build_pointer_type (elttype);
1500 enum tree_code add_code = PLUS_EXPR;
1501 tree idx = TREE_OPERAND (*expr_p, 1);
1502 tree minidx = unshare_expr (array_ref_low_bound (*expr_p));
1503 tree offset, addr, result;
1504 enum gimplify_status ret;
1506 /* If the array domain does not start at zero, apply the offset. */
1507 if (!integer_zerop (minidx))
1509 idx = convert (TREE_TYPE (minidx), idx);
1510 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1513 /* If the index is negative -- a technically invalid situation now
1514 that we've biased the index back to zero -- then casting it to
1515 unsigned has ill effects. In particular, -1*4U/4U != -1.
1516 Represent this as a subtraction of a positive rather than addition
1517 of a negative. This will prevent any conversion back to ARRAY_REF
1518 from getting the wrong results from the division. */
1519 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1521 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1522 add_code = MINUS_EXPR;
1525 /* Pointer arithmetic must be done in sizetype. */
1526 idx = fold_convert (sizetype, idx);
1528 /* Convert the index to a byte offset. */
1529 offset = size_binop (MULT_EXPR, size, idx);
1531 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1532 if (ret == GS_ERROR)
1535 addr = build_fold_addr_expr_with_type (array, ptrtype);
1536 result = fold (build (add_code, ptrtype, addr, offset));
1537 *expr_p = build1 (INDIRECT_REF, elttype, result);
1542 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1543 node pointed by EXPR_P.
1546 : min_lval '[' val ']'
1548 | compound_lval '[' val ']'
1549 | compound_lval '.' ID
1551 This is not part of the original SIMPLE definition, which separates
1552 array and member references, but it seems reasonable to handle them
1553 together. Also, this way we don't run into problems with union
1554 aliasing; gcc requires that for accesses through a union to alias, the
1555 union reference must be explicit, which was not always the case when we
1556 were splitting up array and member refs.
1558 PRE_P points to the list where side effects that must happen before
1559 *EXPR_P should be stored.
1561 POST_P points to the list where side effects that must happen after
1562 *EXPR_P should be stored. */
1564 static enum gimplify_status
1565 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1566 tree *post_p, fallback_t fallback)
1570 enum gimplify_status ret = GS_OK, tret;
1573 #if defined ENABLE_CHECKING
1574 if (TREE_CODE (*expr_p) != ARRAY_REF
1575 && TREE_CODE (*expr_p) != ARRAY_RANGE_REF
1576 && TREE_CODE (*expr_p) != COMPONENT_REF
1577 && TREE_CODE (*expr_p) != BIT_FIELD_REF
1578 && TREE_CODE (*expr_p) != REALPART_EXPR
1579 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1583 /* Create a stack of the subexpressions so later we can walk them in
1584 order from inner to outer. */
1585 VARRAY_TREE_INIT (stack, 10, "stack");
1587 /* We can either handle REALPART_EXPR, IMAGEPART_EXPR anything that
1588 handled_components can deal with. */
1590 (handled_component_p (*p)
1591 || TREE_CODE (*p) == REALPART_EXPR || TREE_CODE (*p) == IMAGPART_EXPR);
1592 p = &TREE_OPERAND (*p, 0))
1593 VARRAY_PUSH_TREE (stack, *p);
1595 /* Now STACK is a stack of pointers to all the refs we've walked through
1596 and P points to the innermost expression.
1598 Java requires that we elaborated nodes in source order. That
1599 means we must gimplify the inner expression followed by each of
1600 the indices, in order. But we can't gimplify the inner
1601 expression until we deal with any variable bounds, sizes, or
1602 positions in order to deal with PLACEHOLDER_EXPRs.
1604 So we do this in three steps. First we deal with the annotations
1605 for any variables in the components, then we gimplify the base,
1606 then we gimplify any indices, from left to right. */
1607 for (i = VARRAY_ACTIVE_SIZE (stack) - 1; i >= 0; i--)
1609 tree t = VARRAY_TREE (stack, i);
1611 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1613 /* Gimplify the low bound and element type size and put them into
1614 the ARRAY_REF. If these values are set, they have already been
1616 if (!TREE_OPERAND (t, 2))
1618 tree low = unshare_expr (array_ref_low_bound (t));
1619 if (!is_gimple_min_invariant (low))
1621 TREE_OPERAND (t, 2) = low;
1622 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1623 is_gimple_tmp_var, fb_rvalue);
1624 ret = MIN (ret, tret);
1628 if (!TREE_OPERAND (t, 3))
1630 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1631 tree elmt_size = unshare_expr (array_ref_element_size (t));
1632 tree factor = size_int (TYPE_ALIGN (elmt_type) / BITS_PER_UNIT);
1634 /* Divide the element size by the alignment of the element
1636 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1638 if (!is_gimple_min_invariant (elmt_size))
1640 TREE_OPERAND (t, 3) = elmt_size;
1641 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1642 is_gimple_tmp_var, fb_rvalue);
1643 ret = MIN (ret, tret);
1647 else if (TREE_CODE (t) == COMPONENT_REF)
1649 /* Set the field offset into T and gimplify it. */
1650 if (!TREE_OPERAND (t, 2))
1652 tree offset = unshare_expr (component_ref_field_offset (t));
1653 tree field = TREE_OPERAND (t, 1);
1655 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1657 /* Divide the offset by its alignment. */
1658 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1660 if (!is_gimple_min_invariant (offset))
1662 TREE_OPERAND (t, 2) = offset;
1663 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1664 is_gimple_tmp_var, fb_rvalue);
1665 ret = MIN (ret, tret);
1671 /* Step 2 is to gimplify the base expression. */
1672 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1673 ret = MIN (ret, tret);
1675 /* And finally, the indices and operands to BIT_FIELD_REF. */
1676 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1678 tree t = VARRAY_TOP_TREE (stack);
1680 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1682 /* Gimplify the dimension.
1683 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1684 Gimplify non-constant array indices into a temporary
1686 FIXME - The real fix is to gimplify post-modify
1687 expressions into a minimal gimple lvalue. However, that
1688 exposes bugs in alias analysis. The alias analyzer does
1689 not handle &PTR->FIELD very well. Will fix after the
1690 branch is merged into mainline (dnovillo 2004-05-03). */
1691 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1693 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1694 is_gimple_tmp_var, fb_rvalue);
1695 ret = MIN (ret, tret);
1698 else if (TREE_CODE (t) == BIT_FIELD_REF)
1700 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1701 is_gimple_val, fb_rvalue);
1702 ret = MIN (ret, tret);
1703 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1704 is_gimple_val, fb_rvalue);
1705 ret = MIN (ret, tret);
1708 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1709 set which would have caused all the outer expressions in EXPR_P
1710 leading to P to also have had TREE_SIDE_EFFECTS set. */
1711 recalculate_side_effects (t);
1715 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1716 ret = MIN (ret, tret);
1718 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1719 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1721 canonicalize_component_ref (expr_p);
1722 ret = MIN (ret, GS_OK);
1728 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1730 PRE_P points to the list where side effects that must happen before
1731 *EXPR_P should be stored.
1733 POST_P points to the list where side effects that must happen after
1734 *EXPR_P should be stored.
1736 WANT_VALUE is nonzero iff we want to use the value of this expression
1737 in another expression. */
1739 static enum gimplify_status
1740 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1743 enum tree_code code;
1744 tree lhs, lvalue, rhs, t1;
1746 enum tree_code arith_code;
1747 enum gimplify_status ret;
1749 code = TREE_CODE (*expr_p);
1751 #if defined ENABLE_CHECKING
1752 if (code != POSTINCREMENT_EXPR
1753 && code != POSTDECREMENT_EXPR
1754 && code != PREINCREMENT_EXPR
1755 && code != PREDECREMENT_EXPR)
1759 /* Prefix or postfix? */
1760 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1761 /* Faster to treat as prefix if result is not used. */
1762 postfix = want_value;
1766 /* Add or subtract? */
1767 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1768 arith_code = PLUS_EXPR;
1770 arith_code = MINUS_EXPR;
1772 /* Gimplify the LHS into a GIMPLE lvalue. */
1773 lvalue = TREE_OPERAND (*expr_p, 0);
1774 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1775 if (ret == GS_ERROR)
1778 /* Extract the operands to the arithmetic operation. */
1780 rhs = TREE_OPERAND (*expr_p, 1);
1782 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1783 that as the result value and in the postqueue operation. */
1786 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1787 if (ret == GS_ERROR)
1791 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1792 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1796 gimplify_and_add (t1, post_p);
1807 /* Gimplify the CALL_EXPR node pointed by EXPR_P. PRE_P points to the
1808 list where side effects that must happen before *EXPR_P should be stored.
1809 WANT_VALUE is true if the result of the call is desired. */
1811 static enum gimplify_status
1812 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
1816 enum gimplify_status ret;
1818 #if defined ENABLE_CHECKING
1819 if (TREE_CODE (*expr_p) != CALL_EXPR)
1823 /* For reliable diagnostics during inlining, it is necessary that
1824 every call_expr be annotated with file and line. */
1825 if (! EXPR_HAS_LOCATION (*expr_p))
1826 SET_EXPR_LOCATION (*expr_p, input_location);
1828 /* This may be a call to a builtin function.
1830 Builtin function calls may be transformed into different
1831 (and more efficient) builtin function calls under certain
1832 circumstances. Unfortunately, gimplification can muck things
1833 up enough that the builtin expanders are not aware that certain
1834 transformations are still valid.
1836 So we attempt transformation/gimplification of the call before
1837 we gimplify the CALL_EXPR. At this time we do not manage to
1838 transform all calls in the same manner as the expanders do, but
1839 we do transform most of them. */
1840 decl = get_callee_fndecl (*expr_p);
1841 if (decl && DECL_BUILT_IN (decl))
1845 /* If it is allocation of stack, record the need to restore the memory
1846 when the enclosing bind_expr is exited. */
1847 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1848 gimplify_ctxp->save_stack = true;
1850 /* If it is restore of the stack, reset it, since it means we are
1851 regimplifying the bind_expr. Note that we use the fact that
1852 for try_finally_expr, try part is processed first. */
1853 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1854 gimplify_ctxp->save_stack = false;
1856 new = simplify_builtin (*expr_p, !want_value);
1858 if (new && new != *expr_p)
1860 /* There was a transformation of this call which computes the
1861 same value, but in a more efficient way. Return and try
1868 /* There is a sequence point before the call, so any side effects in
1869 the calling expression must occur before the actual call. Force
1870 gimplify_expr to use an internal post queue. */
1871 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
1872 is_gimple_call_addr, fb_rvalue);
1874 if (PUSH_ARGS_REVERSED)
1875 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1876 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
1877 arglist = TREE_CHAIN (arglist))
1879 enum gimplify_status t;
1880 bool (*test) (tree);
1883 /* In general, we allow lvalues for function arguments to avoid
1884 extra overhead of copying large aggregates out of even larger
1885 aggregates into temporaries only to copy the temporaries to
1886 the argument list. Make optimizers happy by pulling out to
1887 temporaries those types that fit in registers. */
1888 if (is_gimple_reg_type (TREE_TYPE (TREE_VALUE (arglist))))
1889 test = is_gimple_val, fb = fb_rvalue;
1891 test = is_gimple_lvalue, fb = fb_either;
1893 /* There is a sequence point before a function call. Side effects in
1894 the argument list must occur before the actual call. So, when
1895 gimplifying arguments, force gimplify_expr to use an internal
1896 post queue which is then appended to the end of PRE_P. */
1897 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, test, fb);
1902 if (PUSH_ARGS_REVERSED)
1903 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
1905 /* Try this again in case gimplification exposed something. */
1906 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
1908 tree new = simplify_builtin (*expr_p, !want_value);
1910 if (new && new != *expr_p)
1912 /* There was a transformation of this call which computes the
1913 same value, but in a more efficient way. Return and try
1920 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1921 decl. This allows us to eliminate redundant or useless
1922 calls to "const" functions. */
1923 if (TREE_CODE (*expr_p) == CALL_EXPR
1924 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
1925 TREE_SIDE_EFFECTS (*expr_p) = 0;
1930 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
1931 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
1933 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
1934 condition is true or false, respectively. If null, we should generate
1935 our own to skip over the evaluation of this specific expression.
1937 This function is the tree equivalent of do_jump.
1939 shortcut_cond_r should only be called by shortcut_cond_expr. */
1942 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
1944 tree local_label = NULL_TREE;
1945 tree t, expr = NULL;
1947 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
1948 retain the shortcut semantics. Just insert the gotos here;
1949 shortcut_cond_expr will append the real blocks later. */
1950 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
1952 /* Turn if (a && b) into
1954 if (a); else goto no;
1955 if (b) goto yes; else goto no;
1958 if (false_label_p == NULL)
1959 false_label_p = &local_label;
1961 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
1962 append_to_statement_list (t, &expr);
1964 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1966 append_to_statement_list (t, &expr);
1968 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
1970 /* Turn if (a || b) into
1973 if (b) goto yes; else goto no;
1976 if (true_label_p == NULL)
1977 true_label_p = &local_label;
1979 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
1980 append_to_statement_list (t, &expr);
1982 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1984 append_to_statement_list (t, &expr);
1986 else if (TREE_CODE (pred) == COND_EXPR)
1988 /* As long as we're messing with gotos, turn if (a ? b : c) into
1990 if (b) goto yes; else goto no;
1992 if (c) goto yes; else goto no; */
1993 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
1994 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
1996 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2001 expr = build (COND_EXPR, void_type_node, pred,
2002 build_and_jump (true_label_p),
2003 build_and_jump (false_label_p));
2008 t = build1 (LABEL_EXPR, void_type_node, local_label);
2009 append_to_statement_list (t, &expr);
2016 shortcut_cond_expr (tree expr)
2018 tree pred = TREE_OPERAND (expr, 0);
2019 tree then_ = TREE_OPERAND (expr, 1);
2020 tree else_ = TREE_OPERAND (expr, 2);
2021 tree true_label, false_label, end_label, t;
2023 tree *false_label_p;
2024 bool emit_end, emit_false;
2025 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2026 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2028 /* First do simple transformations. */
2031 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2032 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2034 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2035 then_ = shortcut_cond_expr (expr);
2036 pred = TREE_OPERAND (pred, 0);
2037 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2042 /* If there is no 'then', turn
2045 if (a); else if (b); else d. */
2046 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2048 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2049 else_ = shortcut_cond_expr (expr);
2050 pred = TREE_OPERAND (pred, 0);
2051 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2055 /* If we're done, great. */
2056 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2057 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2060 /* Otherwise we need to mess with gotos. Change
2063 if (a); else goto no;
2066 and recursively gimplify the condition. */
2068 true_label = false_label = end_label = NULL_TREE;
2070 /* If our arms just jump somewhere, hijack those labels so we don't
2071 generate jumps to jumps. */
2074 && TREE_CODE (then_) == GOTO_EXPR
2075 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2077 true_label = GOTO_DESTINATION (then_);
2083 && TREE_CODE (else_) == GOTO_EXPR
2084 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2086 false_label = GOTO_DESTINATION (else_);
2091 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2093 true_label_p = &true_label;
2095 true_label_p = NULL;
2097 /* The 'else' branch also needs a label if it contains interesting code. */
2098 if (false_label || else_se)
2099 false_label_p = &false_label;
2101 false_label_p = NULL;
2103 /* If there was nothing else in our arms, just forward the label(s). */
2104 if (!then_se && !else_se)
2105 return shortcut_cond_r (pred, true_label_p, false_label_p);
2107 /* If our last subexpression already has a terminal label, reuse it. */
2109 expr = expr_last (else_);
2111 expr = expr_last (then_);
2114 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2115 end_label = LABEL_EXPR_LABEL (expr);
2117 /* If we don't care about jumping to the 'else' branch, jump to the end
2118 if the condition is false. */
2120 false_label_p = &end_label;
2122 /* We only want to emit these labels if we aren't hijacking them. */
2123 emit_end = (end_label == NULL_TREE);
2124 emit_false = (false_label == NULL_TREE);
2126 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2129 append_to_statement_list (pred, &expr);
2131 append_to_statement_list (then_, &expr);
2134 t = build_and_jump (&end_label);
2135 append_to_statement_list (t, &expr);
2138 t = build1 (LABEL_EXPR, void_type_node, false_label);
2139 append_to_statement_list (t, &expr);
2141 append_to_statement_list (else_, &expr);
2143 if (emit_end && end_label)
2145 t = build1 (LABEL_EXPR, void_type_node, end_label);
2146 append_to_statement_list (t, &expr);
2152 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2155 gimple_boolify (tree expr)
2157 tree type = TREE_TYPE (expr);
2159 if (TREE_CODE (type) == BOOLEAN_TYPE)
2162 /* If this is the predicate of a COND_EXPR, it might not even be a
2164 expr = lang_hooks.truthvalue_conversion (expr);
2166 switch (TREE_CODE (expr))
2168 case TRUTH_AND_EXPR:
2170 case TRUTH_XOR_EXPR:
2171 case TRUTH_ANDIF_EXPR:
2172 case TRUTH_ORIF_EXPR:
2173 /* Also boolify the arguments of truth exprs. */
2174 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2177 case TRUTH_NOT_EXPR:
2178 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2181 case EQ_EXPR: case NE_EXPR:
2182 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2183 /* These expressions always produce boolean results. */
2184 TREE_TYPE (expr) = boolean_type_node;
2188 /* Other expressions that get here must have boolean values, but
2189 might need to be converted to the appropriate mode. */
2190 return convert (boolean_type_node, expr);
2194 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2203 The second form is used when *EXPR_P is of type void.
2205 TARGET is the tree for T1 above.
2207 PRE_P points to the list where side effects that must happen before
2208 *EXPR_P should be stored. */
2210 static enum gimplify_status
2211 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2213 tree expr = *expr_p;
2215 enum gimplify_status ret;
2217 type = TREE_TYPE (expr);
2219 TREE_TYPE (expr) = void_type_node;
2221 /* If this COND_EXPR has a value, copy the values into a temporary within
2223 else if (! VOID_TYPE_P (type))
2232 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2236 /* Build the then clause, 't1 = a;'. But don't build an assignment
2237 if this branch is void; in C++ it can be, if it's a throw. */
2238 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2239 TREE_OPERAND (expr, 1)
2240 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2242 /* Build the else clause, 't1 = b;'. */
2243 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2244 TREE_OPERAND (expr, 2)
2245 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2247 TREE_TYPE (expr) = void_type_node;
2248 recalculate_side_effects (expr);
2250 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2251 gimplify_and_add (expr, pre_p);
2257 /* Make sure the condition has BOOLEAN_TYPE. */
2258 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2260 /* Break apart && and || conditions. */
2261 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2262 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2264 expr = shortcut_cond_expr (expr);
2266 if (expr != *expr_p)
2270 /* We can't rely on gimplify_expr to re-gimplify the expanded
2271 form properly, as cleanups might cause the target labels to be
2272 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2273 set up a conditional context. */
2274 gimple_push_condition ();
2275 gimplify_stmt (expr_p);
2276 gimple_pop_condition (pre_p);
2282 /* Now do the normal gimplification. */
2283 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2284 is_gimple_condexpr, fb_rvalue);
2286 gimple_push_condition ();
2288 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2289 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2290 recalculate_side_effects (expr);
2292 gimple_pop_condition (pre_p);
2294 if (ret == GS_ERROR)
2296 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2298 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2299 /* Rewrite "if (a); else b" to "if (!a) b" */
2301 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2302 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2303 is_gimple_condexpr, fb_rvalue);
2305 tmp = TREE_OPERAND (expr, 1);
2306 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2307 TREE_OPERAND (expr, 2) = tmp;
2310 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2311 expr = TREE_OPERAND (expr, 0);
2317 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2318 a call to __builtin_memcpy. */
2320 static enum gimplify_status
2321 gimplify_modify_expr_to_memcpy (tree *expr_p, bool want_value)
2323 tree args, t, to, to_ptr, from;
2325 to = TREE_OPERAND (*expr_p, 0);
2326 from = TREE_OPERAND (*expr_p, 1);
2328 t = TYPE_SIZE_UNIT (TREE_TYPE (to));
2329 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, to);
2330 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, from);
2331 t = unshare_expr (t);
2332 args = tree_cons (NULL, t, NULL);
2334 t = build_fold_addr_expr (from);
2335 args = tree_cons (NULL, t, args);
2337 to_ptr = build_fold_addr_expr (to);
2338 args = tree_cons (NULL, to_ptr, args);
2339 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2340 t = build_function_call_expr (t, args);
2344 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2345 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2352 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2353 a call to __builtin_memset. In this case we know that the RHS is
2354 a CONSTRUCTOR with an empty element list. */
2356 static enum gimplify_status
2357 gimplify_modify_expr_to_memset (tree *expr_p, bool want_value)
2359 tree args, t, to, to_ptr;
2361 to = TREE_OPERAND (*expr_p, 0);
2363 t = TYPE_SIZE_UNIT (TREE_TYPE (to));
2364 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, to);
2365 t = unshare_expr (t);
2366 args = tree_cons (NULL, t, NULL);
2368 args = tree_cons (NULL, integer_zero_node, args);
2370 to_ptr = build_fold_addr_expr (to);
2371 args = tree_cons (NULL, to, args);
2372 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2373 t = build_function_call_expr (t, args);
2377 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2378 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2385 /* A subroutine of gimplify_modify_expr. Break out elements of a
2386 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2388 Note that we still need to clear any elements that don't have explicit
2389 initializers, so if not all elements are initialized we keep the
2390 original MODIFY_EXPR, we just remove all of the constructor elements. */
2392 static enum gimplify_status
2393 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2394 tree *post_p, bool want_value)
2396 tree object = TREE_OPERAND (*expr_p, 0);
2397 tree ctor = TREE_OPERAND (*expr_p, 1);
2398 tree type = TREE_TYPE (ctor);
2399 enum gimplify_status ret;
2402 if (TREE_CODE (ctor) != CONSTRUCTOR)
2403 return GS_UNHANDLED;
2405 elt_list = CONSTRUCTOR_ELTS (ctor);
2408 switch (TREE_CODE (type))
2412 case QUAL_UNION_TYPE:
2415 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
2416 HOST_WIDE_INT num_nonconstant_elements;
2419 /* Aggregate types must lower constructors to initialization of
2420 individual elements. The exception is that a CONSTRUCTOR node
2421 with no elements indicates zero-initialization of the whole. */
2422 if (elt_list == NULL)
2430 return GS_UNHANDLED;
2433 categorize_ctor_elements (ctor, &num_nonzero_elements,
2434 &num_nonconstant_elements);
2435 num_elements = count_type_elements (TREE_TYPE (ctor));
2437 /* If a const aggregate variable is being initialized, then it
2438 should never be a lose to promote the variable to be static. */
2439 if (num_nonconstant_elements == 0
2440 && TREE_READONLY (object)
2441 && TREE_CODE (object) == VAR_DECL)
2443 DECL_INITIAL (object) = ctor;
2444 TREE_STATIC (object) = 1;
2445 if (!DECL_NAME (object))
2446 DECL_NAME (object) = create_tmp_var_name ("C");
2447 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2449 /* ??? C++ doesn't automatically append a .<number> to the
2450 assembler name, and even when it does, it looks a FE private
2451 data structures to figure out what that number should be,
2452 which are not set for this variable. I suppose this is
2453 important for local statics for inline functions, which aren't
2454 "local" in the object file sense. So in order to get a unique
2455 TU-local symbol, we must invoke the lhd version now. */
2456 lhd_set_decl_assembler_name (object);
2458 *expr_p = NULL_TREE;
2462 /* If there are "lots" of initialized elements, and all of them
2463 are valid address constants, then the entire initializer can
2464 be dropped to memory, and then memcpy'd out. */
2465 if (num_nonconstant_elements == 0)
2467 HOST_WIDE_INT size = int_size_in_bytes (type);
2470 /* ??? We can still get unbounded array types, at least
2471 from the C++ front end. This seems wrong, but attempt
2472 to work around it for now. */
2475 size = int_size_in_bytes (TREE_TYPE (object));
2477 TREE_TYPE (ctor) = type = TREE_TYPE (object);
2480 /* Find the maximum alignment we can assume for the object. */
2481 /* ??? Make use of DECL_OFFSET_ALIGN. */
2482 if (DECL_P (object))
2483 align = DECL_ALIGN (object);
2485 align = TYPE_ALIGN (type);
2487 if (size > 0 && !can_move_by_pieces (size, align))
2489 tree new = create_tmp_var_raw (type, "C");
2490 gimple_add_tmp_var (new);
2491 TREE_STATIC (new) = 1;
2492 TREE_READONLY (new) = 1;
2493 DECL_INITIAL (new) = ctor;
2494 if (align > DECL_ALIGN (new))
2496 DECL_ALIGN (new) = align;
2497 DECL_USER_ALIGN (new) = 1;
2499 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
2501 TREE_OPERAND (*expr_p, 1) = new;
2506 /* If there are "lots" of initialized elements, even discounting
2507 those that are not address constants (and thus *must* be
2508 computed at runtime), then partition the constructor into
2509 constant and non-constant parts. Block copy the constant
2510 parts in, then generate code for the non-constant parts. */
2511 /* TODO. There's code in cp/typeck.c to do this. */
2513 /* If there are "lots" of zeros, then block clear the object first. */
2515 if (num_elements - num_nonzero_elements > CLEAR_RATIO
2516 && num_nonzero_elements < num_elements/4)
2519 /* ??? This bit ought not be needed. For any element not present
2520 in the initializer, we should simply set them to zero. Except
2521 we'd need to *find* the elements that are not present, and that
2522 requires trickery to avoid quadratic compile-time behavior in
2523 large cases or excessive memory use in small cases. */
2526 HOST_WIDE_INT len = list_length (elt_list);
2527 if (TREE_CODE (type) == ARRAY_TYPE)
2529 tree nelts = array_type_nelts (type);
2530 if (!host_integerp (nelts, 1)
2531 || tree_low_cst (nelts, 1) != len)
2534 else if (len != fields_length (type))
2540 /* Zap the CONSTRUCTOR element list, which simplifies this case.
2541 Note that we still have to gimplify, in order to handle the
2542 case of variable sized types. */
2543 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
2544 gimplify_stmt (expr_p);
2545 append_to_statement_list (*expr_p, pre_p);
2548 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
2550 tree purpose, value, cref, init;
2552 purpose = TREE_PURPOSE (elt_list);
2553 value = TREE_VALUE (elt_list);
2555 if (cleared && initializer_zerop (value))
2558 if (TREE_CODE (type) == ARRAY_TYPE)
2560 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2562 /* ??? Here's to hoping the front end fills in all of the
2563 indicies, so we don't have to figure out what's missing
2567 /* ??? Need to handle this. */
2568 if (TREE_CODE (purpose) == RANGE_EXPR)
2571 cref = build (ARRAY_REF, t, object, purpose,
2572 NULL_TREE, NULL_TREE);
2575 cref = build (COMPONENT_REF, TREE_TYPE (purpose), object,
2576 purpose, NULL_TREE);
2578 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
2580 /* Each member initialization is a full-expression. */
2581 gimplify_and_add (init, pre_p);
2584 *expr_p = NULL_TREE;
2592 /* Extract the real and imaginary parts out of the ctor. */
2596 r = TREE_VALUE (elt_list);
2597 elt_list = TREE_CHAIN (elt_list);
2600 i = TREE_VALUE (elt_list);
2601 if (TREE_CHAIN (elt_list))
2605 if (r == NULL || i == NULL)
2607 tree zero = convert (TREE_TYPE (type), integer_zero_node);
2614 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
2615 represent creation of a complex value. */
2616 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
2618 ctor = build_complex (type, r, i);
2619 TREE_OPERAND (*expr_p, 1) = ctor;
2623 ctor = build (COMPLEX_EXPR, type, r, i);
2624 TREE_OPERAND (*expr_p, 1) = ctor;
2625 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
2626 is_gimple_rhs, fb_rvalue);
2632 /* Go ahead and simplify constant constructors to VECTOR_CST. */
2633 if (TREE_CONSTANT (ctor))
2634 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
2637 /* Vector types use CONSTRUCTOR all the way through gimple
2638 compilation as a general initializer. */
2639 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
2641 enum gimplify_status tret;
2642 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
2643 is_gimple_constructor_elt, fb_rvalue);
2644 if (tret == GS_ERROR)
2651 /* So how did we get a CONSTRUCTOR for a scalar type? */
2655 if (ret == GS_ERROR)
2657 else if (want_value)
2659 append_to_statement_list (*expr_p, pre_p);
2667 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
2668 based on the code of the RHS. We loop for as long as something changes. */
2670 static enum gimplify_status
2671 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2672 tree *post_p, bool want_value)
2674 enum gimplify_status ret = GS_OK;
2676 while (ret != GS_UNHANDLED)
2677 switch (TREE_CODE (*from_p))
2681 /* If we are initializing something from a TARGET_EXPR, strip the
2682 TARGET_EXPR and initialize it directly, if possible. This can't
2683 be done if the initializer is void, since that implies that the
2684 temporary is set in some non-trivial way.
2686 ??? What about code that pulls out the temp and uses it
2687 elsewhere? I think that such code never uses the TARGET_EXPR as
2688 an initializer. If I'm wrong, we'll abort because the temp won't
2689 have any RTL. In that case, I guess we'll need to replace
2690 references somehow. */
2691 tree init = TARGET_EXPR_INITIAL (*from_p);
2693 if (!VOID_TYPE_P (TREE_TYPE (init)))
2704 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2706 gimplify_compound_expr (from_p, pre_p, true);
2711 /* If we're initializing from a CONSTRUCTOR, break this into
2712 individual MODIFY_EXPRs. */
2713 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2716 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2717 the assignment down into the branches, since we can't generate a
2718 temporary of such a type. */
2719 if (TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2722 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2736 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2742 PRE_P points to the list where side effects that must happen before
2743 *EXPR_P should be stored.
2745 POST_P points to the list where side effects that must happen after
2746 *EXPR_P should be stored.
2748 WANT_VALUE is nonzero iff we want to use the value of this expression
2749 in another expression. */
2751 static enum gimplify_status
2752 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2754 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2755 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2756 enum gimplify_status ret = GS_UNHANDLED;
2758 #if defined ENABLE_CHECKING
2759 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2763 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2764 if (TREE_CODE (*expr_p) == INIT_EXPR)
2765 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2767 /* See if any simplifications can be done based on what the RHS is. */
2768 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2770 if (ret != GS_UNHANDLED)
2773 /* If the value being copied is of variable width, expose the length
2774 if the copy by converting the whole thing to a memcpy/memset.
2775 Note that we need to do this before gimplifying any of the operands
2776 so that we can resolve any PLACEHOLDER_EXPRs in the size. */
2777 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST)
2779 if (TREE_CODE (*from_p) == CONSTRUCTOR)
2780 return gimplify_modify_expr_to_memset (expr_p, want_value);
2782 return gimplify_modify_expr_to_memcpy (expr_p, want_value);
2785 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2786 if (ret == GS_ERROR)
2789 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2790 if (ret == GS_ERROR)
2793 /* Now see if the above changed *from_p to something we handle specially. */
2794 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2796 if (ret != GS_UNHANDLED)
2799 /* If the destination is already simple, nothing else needed. */
2800 if (is_gimple_tmp_var (*to_p))
2804 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2805 the LHS is a user variable, then we need to introduce a temporary.
2806 ie temp = RHS; LHS = temp.
2808 This way the optimizers can determine that the user variable is
2809 only modified if evaluation of the RHS does not throw.
2811 FIXME this should be handled by the is_gimple_rhs predicate. */
2813 if (aggregate_value_p (TREE_TYPE (*from_p), NULL_TREE))
2814 /* Don't force a temp of a large aggregate type; the copy could be
2815 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
2817 else if (TREE_CODE (*from_p) == CALL_EXPR
2818 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2819 /* If we're dealing with a renamable type, either source or dest
2820 must be a renamed variable. */
2821 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2822 && !is_gimple_reg (*to_p)))
2823 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2825 ret = want_value ? GS_OK : GS_ALL_DONE;
2830 append_to_statement_list (*expr_p, pre_p);
2837 /* Gimplify a comparison between two variable-sized objects. Do this
2838 with a call to BUILT_IN_MEMCMP. */
2840 static enum gimplify_status
2841 gimplify_variable_sized_compare (tree *expr_p)
2843 tree op0 = TREE_OPERAND (*expr_p, 0);
2844 tree op1 = TREE_OPERAND (*expr_p, 1);
2847 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2848 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2849 t = unshare_expr (t);
2850 args = tree_cons (NULL, t, NULL);
2851 t = build_fold_addr_expr (op1);
2852 args = tree_cons (NULL, t, args);
2853 dest = build_fold_addr_expr (op0);
2854 args = tree_cons (NULL, dest, args);
2855 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2856 t = build_function_call_expr (t, args);
2858 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2863 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2864 points to the expression to gimplify.
2866 Expressions of the form 'a && b' are gimplified to:
2868 a && b ? true : false
2870 gimplify_cond_expr will do the rest.
2872 PRE_P points to the list where side effects that must happen before
2873 *EXPR_P should be stored. */
2875 static enum gimplify_status
2876 gimplify_boolean_expr (tree *expr_p)
2878 /* Preserve the original type of the expression. */
2879 tree type = TREE_TYPE (*expr_p);
2881 *expr_p = build (COND_EXPR, type, *expr_p,
2882 convert (type, boolean_true_node),
2883 convert (type, boolean_false_node));
2888 /* Gimplifies an expression sequence. This function gimplifies each
2889 expression and re-writes the original expression with the last
2890 expression of the sequence in GIMPLE form.
2892 PRE_P points to the list where the side effects for all the
2893 expressions in the sequence will be emitted.
2895 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2896 /* ??? Should rearrange to share the pre-queue with all the indirect
2897 invocations of gimplify_expr. Would probably save on creations
2898 of statement_list nodes. */
2900 static enum gimplify_status
2901 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2907 tree *sub_p = &TREE_OPERAND (t, 0);
2909 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2910 gimplify_compound_expr (sub_p, pre_p, false);
2912 gimplify_stmt (sub_p);
2913 append_to_statement_list (*sub_p, pre_p);
2915 t = TREE_OPERAND (t, 1);
2917 while (TREE_CODE (t) == COMPOUND_EXPR);
2924 gimplify_stmt (expr_p);
2929 /* Gimplifies a statement list. These may be created either by an
2930 enlightened front-end, or by shortcut_cond_expr. */
2932 static enum gimplify_status
2933 gimplify_statement_list (tree *expr_p)
2935 tree_stmt_iterator i = tsi_start (*expr_p);
2937 while (!tsi_end_p (i))
2941 gimplify_stmt (tsi_stmt_ptr (i));
2946 else if (TREE_CODE (t) == STATEMENT_LIST)
2948 tsi_link_before (&i, t, TSI_SAME_STMT);
2958 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2959 gimplify. After gimplification, EXPR_P will point to a new temporary
2960 that holds the original value of the SAVE_EXPR node.
2962 PRE_P points to the list where side effects that must happen before
2963 *EXPR_P should be stored. */
2965 static enum gimplify_status
2966 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2968 enum gimplify_status ret = GS_ALL_DONE;
2971 #if defined ENABLE_CHECKING
2972 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2976 val = TREE_OPERAND (*expr_p, 0);
2978 /* If the operand is already a GIMPLE temporary, just re-write the
2980 if (is_gimple_tmp_var (val))
2982 /* The operand may be a void-valued expression such as SAVE_EXPRs
2983 generated by the Java frontend for class initialization. It is
2984 being executed only for its side-effects. */
2985 else if (TREE_TYPE (val) == void_type_node)
2987 tree body = TREE_OPERAND (*expr_p, 0);
2988 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2989 append_to_statement_list (body, pre_p);
2993 *expr_p = TREE_OPERAND (*expr_p, 0)
2994 = get_initialized_tmp_var (val, pre_p, post_p);
2999 /* Re-write the ADDR_EXPR node pointed by EXPR_P
3006 PRE_P points to the list where side effects that must happen before
3007 *EXPR_P should be stored.
3009 POST_P points to the list where side effects that must happen after
3010 *EXPR_P should be stored. */
3012 static enum gimplify_status
3013 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3015 tree expr = *expr_p;
3016 tree op0 = TREE_OPERAND (expr, 0);
3017 enum gimplify_status ret;
3019 switch (TREE_CODE (op0))
3022 /* Check if we are dealing with an expression of the form '&*ptr'.
3023 While the front end folds away '&*ptr' into 'ptr', these
3024 expressions may be generated internally by the compiler (e.g.,
3025 builtins like __builtin_va_end). */
3026 *expr_p = TREE_OPERAND (op0, 0);
3031 /* Fold &a[6] to (&a + 6). */
3032 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
3035 /* This added an INDIRECT_REF. Fold it away. */
3036 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
3039 case VIEW_CONVERT_EXPR:
3040 /* Take the address of our operand and then convert it to the type of
3043 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3044 all clear. The impact of this transformation is even less clear. */
3045 *expr_p = fold_convert (TREE_TYPE (expr),
3046 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3051 /* We use fb_either here because the C frontend sometimes takes
3052 the address of a call that returns a struct. */
3053 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3054 is_gimple_addr_expr_arg, fb_either);
3055 if (ret != GS_ERROR)
3057 /* At this point, the argument of the ADDR_EXPR should be
3058 sufficiently simple that there are never side effects. */
3059 /* ??? Could split out the decision code from build1 to verify. */
3060 TREE_SIDE_EFFECTS (expr) = 0;
3062 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
3063 recompute_tree_invarant_for_addr_expr (expr);
3065 /* Mark the RHS addressable. */
3066 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3071 /* If the operand is gimplified into a _DECL, mark the address expression
3072 as TREE_INVARIANT. */
3073 if (DECL_P (TREE_OPERAND (expr, 0)))
3074 TREE_INVARIANT (expr) = 1;
3079 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3080 value; output operands should be a gimple lvalue. */
3082 static enum gimplify_status
3083 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3085 tree expr = *expr_p;
3086 int noutputs = list_length (ASM_OUTPUTS (expr));
3087 const char **oconstraints
3088 = (const char **) alloca ((noutputs) * sizeof (const char *));
3091 const char *constraint;
3092 bool allows_mem, allows_reg, is_inout;
3093 enum gimplify_status ret, tret;
3096 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
3100 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3102 oconstraints[i] = constraint
3103 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3105 parse_output_constraint (&constraint, i, 0, 0,
3106 &allows_mem, &allows_reg, &is_inout);
3108 if (!allows_reg && allows_mem)
3109 lang_hooks.mark_addressable (TREE_VALUE (link));
3111 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3112 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3113 fb_lvalue | fb_mayfail);
3114 if (tret == GS_ERROR)
3116 error ("invalid lvalue in asm output %d", i);
3122 /* An input/output operand. To give the optimizers more
3123 flexibility, split it into separate input and output
3127 size_t constraint_len = strlen (constraint);
3129 /* Turn the in/out constraint into an output constraint. */
3130 char *p = xstrdup (constraint);
3132 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3135 /* And add a matching input constraint. */
3138 sprintf (buf, "%d", i);
3139 input = build_string (strlen (buf), buf);
3142 input = build_string (constraint_len - 1, constraint + 1);
3143 input = build_tree_list (build_tree_list (NULL_TREE, input),
3144 unshare_expr (TREE_VALUE (link)));
3145 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3149 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3152 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3153 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3154 oconstraints, &allows_mem, &allows_reg);
3156 /* If the operand is a memory input, it should be an lvalue. */
3157 if (!allows_reg && allows_mem)
3159 lang_hooks.mark_addressable (TREE_VALUE (link));
3160 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3161 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3162 if (tret == GS_ERROR)
3164 error ("memory input %d is not directly addressable", i);
3170 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3171 is_gimple_val, fb_rvalue);
3172 if (tret == GS_ERROR)
3180 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3181 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3182 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3183 return to this function.
3185 FIXME should we complexify the prequeue handling instead? Or use flags
3186 for all the cleanups and let the optimizer tighten them up? The current
3187 code seems pretty fragile; it will break on a cleanup within any
3188 non-conditional nesting. But any such nesting would be broken, anyway;
3189 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3190 and continues out of it. We can do that at the RTL level, though, so
3191 having an optimizer to tighten up try/finally regions would be a Good
3194 static enum gimplify_status
3195 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3197 tree_stmt_iterator iter;
3200 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3202 /* We only care about the number of conditions between the innermost
3203 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3204 int old_conds = gimplify_ctxp->conditions;
3205 gimplify_ctxp->conditions = 0;
3207 body = TREE_OPERAND (*expr_p, 0);
3208 gimplify_to_stmt_list (&body);
3210 gimplify_ctxp->conditions = old_conds;
3212 for (iter = tsi_start (body); !tsi_end_p (iter); )
3214 tree *wce_p = tsi_stmt_ptr (iter);
3217 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3219 if (tsi_one_before_end_p (iter))
3221 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
3229 sl = tsi_split_statement_list_after (&iter);
3230 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
3231 append_to_statement_list (TREE_OPERAND (wce, 1),
3232 &TREE_OPERAND (tfe, 1));
3234 iter = tsi_start (sl);
3244 append_to_statement_list (body, pre_p);
3254 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3255 is the cleanup action required. */
3258 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
3262 /* Errors can result in improperly nested cleanups. Which results in
3263 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3264 if (errorcount || sorrycount)
3267 if (gimple_conditional_context ())
3269 /* If we're in a conditional context, this is more complex. We only
3270 want to run the cleanup if we actually ran the initialization that
3271 necessitates it, but we want to run it after the end of the
3272 conditional context. So we wrap the try/finally around the
3273 condition and use a flag to determine whether or not to actually
3274 run the destructor. Thus
3278 becomes (approximately)
3282 if (test) { A::A(temp); flag = 1; val = f(temp); }
3285 if (flag) A::~A(temp);
3290 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3291 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3292 boolean_false_node);
3293 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3295 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3296 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3297 cleanup, NULL_TREE);
3298 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3299 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3300 append_to_statement_list (ftrue, pre_p);
3302 /* Because of this manipulation, and the EH edges that jump
3303 threading cannot redirect, the temporary (VAR) will appear
3304 to be used uninitialized. Don't warn. */
3305 TREE_NO_WARNING (var) = 1;
3309 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3310 cleanup, NULL_TREE);
3311 append_to_statement_list (wce, pre_p);
3314 gimplify_stmt (&TREE_OPERAND (wce, 1));
3317 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3319 static enum gimplify_status
3320 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3322 tree targ = *expr_p;
3323 tree temp = TARGET_EXPR_SLOT (targ);
3324 tree init = TARGET_EXPR_INITIAL (targ);
3325 enum gimplify_status ret;
3329 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3330 to the temps list. */
3331 gimple_add_tmp_var (temp);
3333 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3334 expression is supposed to initialize the slot. */
3335 if (VOID_TYPE_P (TREE_TYPE (init)))
3336 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3339 /* Special handling for BIND_EXPR can result in fewer temps. */
3341 if (TREE_CODE (init) == BIND_EXPR)
3342 gimplify_bind_expr (&init, temp, pre_p);
3345 init = build (MODIFY_EXPR, void_type_node, temp, init);
3346 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3350 if (ret == GS_ERROR)
3352 append_to_statement_list (init, pre_p);
3354 /* If needed, push the cleanup for the temp. */
3355 if (TARGET_EXPR_CLEANUP (targ))
3357 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3358 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3361 /* Only expand this once. */
3362 TREE_OPERAND (targ, 3) = init;
3363 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3365 else if (!temp->decl.seen_in_bind_expr)
3366 /* We should have expanded this before. */
3373 /* Gimplification of expression trees. */
3375 /* Gimplify an expression which appears at statement context; usually, this
3376 means replacing it with a suitably gimple STATEMENT_LIST. */
3379 gimplify_stmt (tree *stmt_p)
3381 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3384 /* Similarly, but force the result to be a STATEMENT_LIST. */
3387 gimplify_to_stmt_list (tree *stmt_p)
3389 gimplify_stmt (stmt_p);
3391 *stmt_p = alloc_stmt_list ();
3392 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3395 *stmt_p = alloc_stmt_list ();
3396 append_to_statement_list (t, stmt_p);
3401 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3402 gimplification failed.
3404 PRE_P points to the list where side effects that must happen before
3405 EXPR should be stored.
3407 POST_P points to the list where side effects that must happen after
3408 EXPR should be stored, or NULL if there is no suitable list. In
3409 that case, we copy the result to a temporary, emit the
3410 post-effects, and then return the temporary.
3412 GIMPLE_TEST_F points to a function that takes a tree T and
3413 returns nonzero if T is in the GIMPLE form requested by the
3414 caller. The GIMPLE predicates are in tree-gimple.c.
3416 This test is used twice. Before gimplification, the test is
3417 invoked to determine whether *EXPR_P is already gimple enough. If
3418 that fails, *EXPR_P is gimplified according to its code and
3419 GIMPLE_TEST_F is called again. If the test still fails, then a new
3420 temporary variable is created and assigned the value of the
3421 gimplified expression.
3423 FALLBACK tells the function what sort of a temporary we want. If the 1
3424 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3425 If both are set, either is OK, but an lvalue is preferable.
3427 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3428 iterates until solution. */
3430 enum gimplify_status
3431 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3432 bool (* gimple_test_f) (tree), fallback_t fallback)
3435 tree internal_pre = NULL_TREE;
3436 tree internal_post = NULL_TREE;
3438 int is_statement = (pre_p == NULL);
3439 location_t saved_location;
3440 enum gimplify_status ret;
3442 save_expr = *expr_p;
3443 if (save_expr == NULL_TREE)
3446 /* We used to check the predicate here and return immediately if it
3447 succeeds. This is wrong; the design is for gimplification to be
3448 idempotent, and for the predicates to only test for valid forms, not
3449 whether they are fully simplified. */
3451 /* Set up our internal queues if needed. */
3453 pre_p = &internal_pre;
3455 post_p = &internal_post;
3457 saved_location = input_location;
3458 if (save_expr != error_mark_node
3459 && EXPR_HAS_LOCATION (*expr_p))
3460 input_location = EXPR_LOCATION (*expr_p);
3462 /* Loop over the specific gimplifiers until the toplevel node
3463 remains the same. */
3466 /* Strip any uselessness. */
3467 STRIP_MAIN_TYPE_NOPS (*expr_p);
3469 /* Remember the expr. */
3470 save_expr = *expr_p;
3472 /* Die, die, die, my darling. */
3473 if (save_expr == error_mark_node
3474 || (TREE_TYPE (save_expr)
3475 && TREE_TYPE (save_expr) == error_mark_node))
3481 /* Do any language-specific gimplification. */
3482 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3485 if (*expr_p == NULL_TREE)
3487 if (*expr_p != save_expr)
3490 else if (ret != GS_UNHANDLED)
3494 switch (TREE_CODE (*expr_p))
3496 /* First deal with the special cases. */
3498 case POSTINCREMENT_EXPR:
3499 case POSTDECREMENT_EXPR:
3500 case PREINCREMENT_EXPR:
3501 case PREDECREMENT_EXPR:
3502 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3503 fallback != fb_none);
3507 case ARRAY_RANGE_REF:
3511 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3512 fallback ? fallback : fb_rvalue);
3516 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3520 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
3527 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3532 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3533 fallback != fb_none);
3536 case TRUTH_ANDIF_EXPR:
3537 case TRUTH_ORIF_EXPR:
3538 ret = gimplify_boolean_expr (expr_p);
3541 case TRUTH_NOT_EXPR:
3542 TREE_OPERAND (*expr_p, 0)
3543 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3544 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3545 is_gimple_val, fb_rvalue);
3546 recalculate_side_effects (*expr_p);
3550 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3554 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3557 case VIEW_CONVERT_EXPR:
3558 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3559 || fallback == fb_none)
3561 /* Just strip a conversion to void (or in void context) and
3563 *expr_p = TREE_OPERAND (*expr_p, 0);
3567 /* If both types are BLKmode or if one type is of variable size,
3568 convert this into a pointer punning operation. This avoids
3569 copies of large data or making a variable-size temporary.
3571 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3572 all clear. The impact of this transformation is even less
3575 if ((TYPE_MODE (TREE_TYPE (*expr_p)) == BLKmode
3576 && TYPE_MODE (TREE_TYPE (TREE_OPERAND (*expr_p, 0))) == BLKmode)
3577 || !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p)))
3578 || !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE
3579 (TREE_OPERAND (*expr_p,0)))))
3581 tree restype = TREE_TYPE (*expr_p);
3582 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (*expr_p),
3583 fold_convert (build_pointer_type (restype),
3584 build_fold_addr_expr
3585 (TREE_OPERAND (*expr_p, 0))));
3592 if (IS_EMPTY_STMT (*expr_p))
3598 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3599 || fallback == fb_none)
3601 /* Just strip a conversion to void (or in void context) and
3603 *expr_p = TREE_OPERAND (*expr_p, 0);
3607 ret = gimplify_conversion (expr_p);
3608 if (ret == GS_ERROR)
3610 if (*expr_p != save_expr)
3614 case FIX_TRUNC_EXPR:
3616 case FIX_FLOOR_EXPR:
3617 case FIX_ROUND_EXPR:
3619 /* unary_expr: ... | '(' cast ')' val | ... */
3620 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3621 is_gimple_val, fb_rvalue);
3622 recalculate_side_effects (*expr_p);
3626 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3627 is_gimple_reg, fb_rvalue);
3628 recalculate_side_effects (*expr_p);
3631 /* Constants need not be gimplified. */
3641 *expr_p = DECL_INITIAL (*expr_p);
3645 ret = gimplify_decl_expr (expr_p);
3649 /* FIXME make this a decl. */
3654 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3658 ret = gimplify_loop_expr (expr_p, pre_p);
3662 ret = gimplify_switch_expr (expr_p, pre_p);
3665 case LABELED_BLOCK_EXPR:
3666 ret = gimplify_labeled_block_expr (expr_p);
3669 case EXIT_BLOCK_EXPR:
3670 ret = gimplify_exit_block_expr (expr_p);
3674 ret = gimplify_exit_expr (expr_p);
3678 /* If the target is not LABEL, then it is a computed jump
3679 and the target needs to be gimplified. */
3680 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3681 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3682 NULL, is_gimple_val, fb_rvalue);
3687 #ifdef ENABLE_CHECKING
3688 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3693 case CASE_LABEL_EXPR:
3694 ret = gimplify_case_label_expr (expr_p);
3698 ret = gimplify_return_expr (*expr_p, pre_p);
3702 /* Don't reduce this in place; let gimplify_init_constructor work
3707 /* The following are special cases that are not handled by the
3708 original GIMPLE grammar. */
3710 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3713 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3718 enum gimplify_status r0, r1, r2;
3720 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3721 is_gimple_lvalue, fb_either);
3722 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3723 is_gimple_val, fb_rvalue);
3724 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3725 is_gimple_val, fb_rvalue);
3726 recalculate_side_effects (*expr_p);
3728 ret = MIN (r0, MIN (r1, r2));
3732 case NON_LVALUE_EXPR:
3733 /* This should have been stripped above. */
3738 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3741 case TRY_FINALLY_EXPR:
3742 case TRY_CATCH_EXPR:
3743 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3744 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3748 case CLEANUP_POINT_EXPR:
3749 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3753 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3757 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3761 case EH_FILTER_EXPR:
3762 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3768 enum gimplify_status r0, r1;
3769 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
3770 is_gimple_val, fb_rvalue);
3771 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
3772 is_gimple_val, fb_rvalue);
3779 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3783 /* We get here when taking the address of a label. We mark
3784 the label as "forced"; meaning it can never be removed and
3785 it is a potential target for any computed goto. */
3786 FORCED_LABEL (*expr_p) = 1;
3790 case STATEMENT_LIST:
3791 ret = gimplify_statement_list (expr_p);
3795 /* ??? If this is a local variable, and it has not been seen in any
3796 outer BIND_EXPR, then it's probably the result of a duplicate
3797 declaration, for which we've already issued an error. It would
3798 be really nice if the front end wouldn't leak these at all.
3799 Currently the only known culprit is C++ destructors, as seen
3800 in g++.old-deja/g++.jason/binding.C. */
3802 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3803 && decl_function_context (tmp) == current_function_decl
3804 && !tmp->decl.seen_in_bind_expr)
3806 #ifdef ENABLE_CHECKING
3807 if (!errorcount && !sorrycount)
3817 /* If this is a comparison of objects of aggregate type, handle
3818 it specially (by converting to a call to memcmp). It would be
3819 nice to only have to do this for variable-sized objects, but
3820 then we'd have to allow the same nest of reference nodes we
3821 allow for MODIFY_EXPR and that's too complex. */
3822 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3823 && (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1)))))
3824 ret = gimplify_variable_sized_compare (expr_p);
3826 /* If *EXPR_P does not need to be special-cased, handle it
3827 according to its class. */
3828 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3829 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3830 post_p, is_gimple_val, fb_rvalue);
3831 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3832 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3833 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3834 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3835 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3837 enum gimplify_status r0, r1;
3839 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3840 post_p, is_gimple_val, fb_rvalue);
3841 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3842 post_p, is_gimple_val, fb_rvalue);
3846 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3847 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3853 /* Fail if we don't know how to handle this tree code. */
3856 recalculate_side_effects (*expr_p);
3860 /* If we replaced *expr_p, gimplify again. */
3861 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3864 while (ret == GS_OK);
3866 /* If we encountered an error_mark somewhere nested inside, either
3867 stub out the statement or propagate the error back out. */
3868 if (ret == GS_ERROR)
3875 #ifdef ENABLE_CHECKING
3876 /* This was only valid as a return value from the langhook, which
3877 we handled. Make sure it doesn't escape from any other context. */
3878 if (ret == GS_UNHANDLED)
3882 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
3884 /* We aren't looking for a value, and we don't have a valid
3885 statement. If it doesn't have side-effects, throw it away. */
3886 if (!TREE_SIDE_EFFECTS (*expr_p))
3888 else if (!TREE_THIS_VOLATILE (*expr_p))
3890 /* This is probably a _REF that contains something nested that
3891 has side effects. Recurse through the operands to find it. */
3892 enum tree_code code = TREE_CODE (*expr_p);
3894 if (code == COMPONENT_REF
3895 || code == REALPART_EXPR || code == IMAGPART_EXPR)
3896 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3897 gimple_test_f, fallback);
3898 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
3900 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3901 gimple_test_f, fallback);
3902 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3903 gimple_test_f, fallback);
3906 /* Anything else with side-effects
3907 must be converted to a valid statement before we get here. */
3912 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3914 /* Historically, the compiler has treated a bare
3915 reference to a volatile lvalue as forcing a load. */
3916 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3917 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3920 /* We can't do anything useful with a volatile reference to
3921 incomplete type, so just throw it away. */
3925 /* If we are gimplifying at the statement level, we're done. Tack
3926 everything together and replace the original statement with the
3928 if (fallback == fb_none || is_statement)
3930 if (internal_pre || internal_post)
3932 append_to_statement_list (*expr_p, &internal_pre);
3933 append_to_statement_list (internal_post, &internal_pre);
3934 annotate_all_with_locus (&internal_pre, input_location);
3935 *expr_p = internal_pre;
3939 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
3940 annotate_all_with_locus (expr_p, input_location);
3942 annotate_one_with_locus (*expr_p, input_location);
3946 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3949 /* If it's sufficiently simple already, we're done. Unless we are
3950 handling some post-effects internally; if that's the case, we need to
3951 copy into a temp before adding the post-effects to the tree. */
3952 if (!internal_post && (*gimple_test_f) (*expr_p))
3955 /* Otherwise, we need to create a new temporary for the gimplified
3958 /* We can't return an lvalue if we have an internal postqueue. The
3959 object the lvalue refers to would (probably) be modified by the
3960 postqueue; we need to copy the value out first, which means an
3962 if ((fallback & fb_lvalue) && !internal_post
3963 && is_gimple_addr_expr_arg (*expr_p))
3965 /* An lvalue will do. Take the address of the expression, store it
3966 in a temporary, and replace the expression with an INDIRECT_REF of
3968 tmp = build_fold_addr_expr (*expr_p);
3969 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3970 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3972 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3974 #if defined ENABLE_CHECKING
3975 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3979 /* An rvalue will do. Assign the gimplified expression into a new
3980 temporary TMP and replace the original expression with TMP. */
3982 if (internal_post || (fallback & fb_lvalue))
3983 /* The postqueue might change the value of the expression between
3984 the initialization and use of the temporary, so we can't use a
3985 formal temp. FIXME do we care? */
3986 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3988 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3990 else if (fallback & fb_mayfail)
3992 /* If this is an asm statement, and the user asked for the impossible,
3993 don't abort. Fail and let gimplify_asm_expr issue an error. */
3999 fprintf (stderr, "gimplification failed:\n");
4000 print_generic_expr (stderr, *expr_p, 0);
4001 debug_tree (*expr_p);
4005 #if defined ENABLE_CHECKING
4006 /* Make sure the temporary matches our predicate. */
4007 if (!(*gimple_test_f) (*expr_p))
4013 annotate_all_with_locus (&internal_post, input_location);
4014 append_to_statement_list (internal_post, pre_p);
4018 input_location = saved_location;
4022 /* Look through TYPE for variable-sized objects and gimplify each such
4023 size that we find. Add to LIST_P any statements generated. */
4026 gimplify_type_sizes (tree type, tree *list_p)
4030 switch (TREE_CODE (type))
4040 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
4041 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
4045 /* These anonymous types don't have declarations, so handle them here. */
4046 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
4051 case QUAL_UNION_TYPE:
4052 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4053 if (TREE_CODE (field) == FIELD_DECL)
4054 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
4061 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
4062 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
4065 /* Subroutine of the above to gimplify one size or position, *EXPR_P.
4066 We add any required statements to STMT_P. */
4069 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
4071 /* We don't do anything if the value isn't there, is constant, or contains
4072 A PLACEHOLDER_EXPR. */
4073 if (*expr_p == NULL_TREE || TREE_CONSTANT (*expr_p)
4074 || CONTAINS_PLACEHOLDER_P (*expr_p))
4077 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
4080 #ifdef ENABLE_CHECKING
4081 /* Compare types A and B for a "close enough" match. */
4084 cpt_same_type (tree a, tree b)
4086 if (lang_hooks.types_compatible_p (a, b))
4089 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
4090 link them together. This routine is intended to catch type errors
4091 that will affect the optimizers, and the optimizers don't add new
4092 dereferences of function pointers, so ignore it. */
4093 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
4094 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
4097 /* ??? The C FE pushes type qualifiers after the fact into the type of
4098 the element from the type of the array. See build_unary_op's handling
4099 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
4100 should have done it when creating the variable in the first place.
4101 Alternately, why aren't the two array types made variants? */
4102 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
4103 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4105 /* And because of those, we have to recurse down through pointers. */
4106 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
4107 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
4112 /* Check for some cases of the front end missing cast expressions.
4113 The type of a dereference should correspond to the pointer type;
4114 similarly the type of an address should match its object. */
4117 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4118 void *data ATTRIBUTE_UNUSED)
4121 tree ptype, otype, dtype;
4123 switch (TREE_CODE (t))
4127 otype = TREE_TYPE (t);
4128 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
4129 dtype = TREE_TYPE (ptype);
4130 if (!cpt_same_type (otype, dtype))
4135 ptype = TREE_TYPE (t);
4136 otype = TREE_TYPE (TREE_OPERAND (t, 0));
4137 dtype = TREE_TYPE (ptype);
4138 if (!cpt_same_type (otype, dtype))
4140 /* &array is allowed to produce a pointer to the element, rather than
4141 a pointer to the array type. We must allow this in order to
4142 properly represent assigning the address of an array in C into
4143 pointer to the element type. */
4144 if (TREE_CODE (otype) == ARRAY_TYPE
4145 && POINTER_TYPE_P (ptype)
4146 && cpt_same_type (TREE_TYPE (otype), dtype))
4161 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
4162 function decl containing BODY. */
4165 gimplify_body (tree *body_p, tree fndecl)
4167 location_t saved_location = input_location;
4170 timevar_push (TV_TREE_GIMPLIFY);
4171 push_gimplify_context ();
4173 /* Unshare most shared trees in the body and in that of any nested functions.
4174 It would seem we don't have to do this for nested functions because
4175 they are supposed to be output and then the outer function gimplified
4176 first, but the g++ front end doesn't always do it that way. */
4177 unshare_body (body_p, fndecl);
4178 unvisit_body (body_p, fndecl);
4180 /* Make sure input_location isn't set to something wierd. */
4181 input_location = DECL_SOURCE_LOCATION (fndecl);
4183 /* Gimplify the function's body. */
4184 gimplify_stmt (body_p);
4187 /* Unshare again, in case gimplification was sloppy. */
4188 unshare_all_trees (body);
4191 body = alloc_stmt_list ();
4192 else if (TREE_CODE (body) == STATEMENT_LIST)
4194 tree t = expr_only (*body_p);
4199 /* If there isn't an outer BIND_EXPR, add one. */
4200 if (TREE_CODE (body) != BIND_EXPR)
4202 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
4203 NULL_TREE, NULL_TREE);
4204 TREE_SIDE_EFFECTS (b) = 1;
4205 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
4210 pop_gimplify_context (body);
4212 #ifdef ENABLE_CHECKING
4213 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
4216 timevar_pop (TV_TREE_GIMPLIFY);
4217 input_location = saved_location;
4220 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
4221 node for the function we want to gimplify. */
4224 gimplify_function_tree (tree fndecl)
4228 oldfn = current_function_decl;
4229 current_function_decl = fndecl;
4231 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
4233 /* If we're instrumenting function entry/exit, then prepend the call to
4234 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
4235 catch the exit hook. */
4236 /* ??? Add some way to ignore exceptions for this TFE. */
4237 if (flag_instrument_function_entry_exit
4238 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
4242 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4243 TREE_SIDE_EFFECTS (tf) = 1;
4244 x = DECL_SAVED_TREE (fndecl);
4245 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
4246 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
4247 x = build_function_call_expr (x, NULL);
4248 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
4250 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4251 TREE_SIDE_EFFECTS (bind) = 1;
4252 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
4253 x = build_function_call_expr (x, NULL);
4254 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
4255 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
4257 DECL_SAVED_TREE (fndecl) = bind;
4260 current_function_decl = oldfn;
4263 #include "gt-gimplify.h"