1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
4 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "coretypes.h"
33 #include "tree-gimple.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "langhooks.h"
37 #include "langhooks-def.h"
38 #include "tree-flow.h"
50 static struct gimplify_ctx
52 tree current_bind_expr;
55 tree conditional_cleanups;
59 varray_type case_labels;
60 /* The formal temporary table. Should this be persistent? */
65 /* Formal (expression) temporary table handling: Multiple occurrences of
66 the same scalar expression are evaluated into the same temporary. */
68 typedef struct gimple_temp_hash_elt
71 tree temp; /* Value */
74 /* Return a hash value for a formal temporary table entry. */
77 gimple_tree_hash (const void *p)
79 tree t = ((const elt_t *)p)->val;
80 return iterative_hash_expr (t, 0);
83 /* Compare two formal temporary table entries. */
86 gimple_tree_eq (const void *p1, const void *p2)
88 tree t1 = ((const elt_t *)p1)->val;
89 tree t2 = ((const elt_t *)p2)->val;
90 enum tree_code code = TREE_CODE (t1);
92 if (TREE_CODE (t2) != code
93 || TREE_TYPE (t1) != TREE_TYPE (t2))
96 if (!operand_equal_p (t1, t2, 0))
99 /* Only allow them to compare equal if they also hash equal; otherwise
100 results are nondeterminate, and we fail bootstrap comparison. */
101 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
107 /* Set up a context for the gimplifier. */
110 push_gimplify_context (void)
115 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
116 gimplify_ctxp->temp_htab
117 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
120 /* Tear down a context for the gimplifier. If BODY is non-null, then
121 put the temporaries into the outer BIND_EXPR. Otherwise, put them
122 in the unexpanded_var_list. */
125 pop_gimplify_context (tree body)
127 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
131 declare_tmp_vars (gimplify_ctxp->temps, body);
133 record_vars (gimplify_ctxp->temps);
137 fprintf (stderr, " collisions: %f ",
138 htab_collisions (gimplify_ctxp->temp_htab));
141 htab_delete (gimplify_ctxp->temp_htab);
142 free (gimplify_ctxp);
143 gimplify_ctxp = NULL;
147 gimple_push_bind_expr (tree bind)
149 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
150 gimplify_ctxp->current_bind_expr = bind;
154 gimple_pop_bind_expr (void)
156 gimplify_ctxp->current_bind_expr
157 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
161 gimple_current_bind_expr (void)
163 return gimplify_ctxp->current_bind_expr;
166 /* Returns true iff there is a COND_EXPR between us and the innermost
167 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
170 gimple_conditional_context (void)
172 return gimplify_ctxp->conditions > 0;
175 /* Note that we've entered a COND_EXPR. */
178 gimple_push_condition (void)
180 ++(gimplify_ctxp->conditions);
183 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
184 now, add any conditional cleanups we've seen to the prequeue. */
187 gimple_pop_condition (tree *pre_p)
189 int conds = --(gimplify_ctxp->conditions);
192 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
193 gimplify_ctxp->conditional_cleanups = NULL_TREE;
199 /* A subroutine of append_to_statement_list{,_force}. */
202 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
205 tree_stmt_iterator i;
212 if (t && TREE_CODE (t) == STATEMENT_LIST)
217 *list_p = list = alloc_stmt_list ();
221 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
224 /* Add T to the end of the list container pointed by LIST_P.
225 If T is an expression with no effects, it is ignored. */
228 append_to_statement_list (tree t, tree *list_p)
230 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
233 /* Similar, but the statement is always added, regardless of side effects. */
236 append_to_statement_list_force (tree t, tree *list_p)
238 append_to_statement_list_1 (t, list_p, t != NULL);
241 /* Both gimplify the statement T and append it to LIST_P. */
244 gimplify_and_add (tree t, tree *list_p)
247 append_to_statement_list (t, list_p);
250 /* Add T to the end of a COMPOUND_EXPR pointed by LIST_P. The type
251 of the result is the type of T. */
254 append_to_compound_expr (tree t, tree *list_p)
261 *list_p = build (COMPOUND_EXPR, TREE_TYPE (t), *list_p, t);
264 /* Strip off a legitimate source ending from the input string NAME of
265 length LEN. Rather than having to know the names used by all of
266 our front ends, we strip off an ending of a period followed by
267 up to five characters. (Java uses ".class".) */
270 remove_suffix (char *name, int len)
274 for (i = 2; i < 8 && len > i; i++)
276 if (name[len - i] == '.')
278 name[len - i] = '\0';
284 /* Create a nameless artificial label and put it in the current function
285 context. Returns the newly created label. */
288 create_artificial_label (void)
290 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
291 DECL_ARTIFICIAL (lab) = 1;
292 DECL_CONTEXT (lab) = current_function_decl;
296 /* Create a new temporary name with PREFIX. Returns an identifier. */
298 static GTY(()) unsigned int tmp_var_id_num;
301 create_tmp_var_name (const char *prefix)
307 char *preftmp = ASTRDUP (prefix);
308 remove_suffix (preftmp, strlen (preftmp));
312 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
313 return get_identifier (tmp_name);
317 /* Create a new temporary variable declaration of type TYPE.
318 Does NOT push it into the current binding. */
321 create_tmp_var_raw (tree type, const char *prefix)
326 /* Make the type of the variable writable. */
327 new_type = build_type_variant (type, 0, 0);
328 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
330 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
332 /* The variable was declared by the compiler. */
333 DECL_ARTIFICIAL (tmp_var) = 1;
334 /* And we don't want debug info for it. */
335 DECL_IGNORED_P (tmp_var) = 1;
337 /* Make the variable writable. */
338 TREE_READONLY (tmp_var) = 0;
340 DECL_EXTERNAL (tmp_var) = 0;
341 TREE_STATIC (tmp_var) = 0;
342 TREE_USED (tmp_var) = 1;
347 /* Create a new temporary variable declaration of type TYPE. DOES push the
348 variable into the current binding. Further, assume that this is called
349 only from gimplification or optimization, at which point the creation of
350 certain types are bugs. */
353 create_tmp_var (tree type, const char *prefix)
357 #if defined ENABLE_CHECKING
358 /* If the type is an array or a type which must be created by the
359 frontend, something is wrong. */
360 if (TREE_CODE (type) == ARRAY_TYPE || TREE_ADDRESSABLE (type))
362 if (!COMPLETE_TYPE_P (type))
364 /* Variable sized types require lots of machinery to create; the
365 optimizers shouldn't be doing anything of the sort. */
366 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
370 tmp_var = create_tmp_var_raw (type, prefix);
371 gimple_add_tmp_var (tmp_var);
375 /* Given a tree, try to return a useful variable name that we can use
376 to prefix a temporary that is being assigned the value of the tree.
377 I.E. given <temp> = &A, return A. */
385 STRIP_NOPS (stripped_decl);
386 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
387 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
390 switch (TREE_CODE (stripped_decl))
393 return get_name (TREE_OPERAND (stripped_decl, 0));
401 /* Create a temporary with a name derived from VAL. Subroutine of
402 lookup_tmp_var; nobody else should call this function. */
405 create_tmp_from_val (tree val)
407 return create_tmp_var (TREE_TYPE (val), get_name (val));
410 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
411 an existing expression temporary. */
414 lookup_tmp_var (tree val, bool is_formal)
416 if (!is_formal || TREE_SIDE_EFFECTS (val))
417 return create_tmp_from_val (val);
424 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
427 elt_p = xmalloc (sizeof (*elt_p));
429 elt_p->temp = create_tmp_from_val (val);
430 *slot = (void *)elt_p;
433 elt_p = (elt_t *) *slot;
439 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
440 in gimplify_expr. Only use this function if:
442 1) The value of the unfactored expression represented by VAL will not
443 change between the initialization and use of the temporary, and
444 2) The temporary will not be otherwise modified.
446 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
447 and #2 means it is inappropriate for && temps.
449 For other cases, use get_initialized_tmp_var instead. */
452 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
457 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
459 t = lookup_tmp_var (val, is_formal);
461 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
463 class = TREE_CODE_CLASS (TREE_CODE (val));
464 if (EXPR_LOCUS (val))
465 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
467 annotate_with_locus (mod, input_location);
468 /* gimplify_modify_expr might want to reduce this further. */
469 gimplify_stmt (&mod);
470 append_to_statement_list (mod, pre_p);
476 get_formal_tmp_var (tree val, tree *pre_p)
478 return internal_get_tmp_var (val, pre_p, NULL, true);
481 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
482 are as in gimplify_expr. */
485 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
487 return internal_get_tmp_var (val, pre_p, post_p, false);
490 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
493 is_gimple_tmp_var (tree t)
495 /* FIXME this could trigger for other local artificials, too. */
496 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
497 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
500 /* Declares all the variables in VARS in SCOPE. Returns the last
501 DECL_STMT emitted. */
504 declare_tmp_vars (tree vars, tree scope)
511 /* C99 mode puts the default 'return 0;' for main() outside the outer
512 braces. So drill down until we find an actual scope. */
513 while (TREE_CODE (scope) == COMPOUND_EXPR)
514 scope = TREE_OPERAND (scope, 0);
516 if (TREE_CODE (scope) != BIND_EXPR)
519 temps = nreverse (last);
520 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
521 BIND_EXPR_VARS (scope) = temps;
523 /* We don't add the temps to the block for this BIND_EXPR, as we're
524 not interested in debugging info for them. */
529 gimple_add_tmp_var (tree tmp)
531 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
534 DECL_CONTEXT (tmp) = current_function_decl;
535 tmp->decl.seen_in_bind_expr = 1;
539 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
540 gimplify_ctxp->temps = tmp;
545 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
548 /* Determines whether to assign a locus to the statement STMT. */
551 should_carry_locus_p (tree stmt)
553 /* Don't emit a line note for a label. We particularly don't want to
554 emit one for the break label, since it doesn't actually correspond
555 to the beginning of the loop/switch. */
556 if (TREE_CODE (stmt) == LABEL_EXPR)
559 /* Do not annotate empty statements, since it confuses gcov. */
560 if (!TREE_SIDE_EFFECTS (stmt))
567 annotate_all_with_locus (tree *stmt_p, location_t locus)
569 tree_stmt_iterator i;
574 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
576 tree t = tsi_stmt (i);
578 #ifdef ENABLE_CHECKING
579 /* Assuming we've already been gimplified, we shouldn't
580 see nested chaining constructs anymore. */
581 if (TREE_CODE (t) == STATEMENT_LIST
582 || TREE_CODE (t) == COMPOUND_EXPR)
586 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
587 && ! EXPR_HAS_LOCATION (t)
588 && should_carry_locus_p (t))
589 annotate_with_locus (t, locus);
593 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
594 These nodes model computations that should only be done once. If we
595 were to unshare something like SAVE_EXPR(i++), the gimplification
596 process would create wrong code. */
599 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
601 enum tree_code code = TREE_CODE (*tp);
602 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
603 if (TREE_CODE_CLASS (code) == 't'
604 || TREE_CODE_CLASS (code) == 'd'
605 || TREE_CODE_CLASS (code) == 'c'
606 || code == SAVE_EXPR || code == TARGET_EXPR
607 /* We can't do anything sensible with a BLOCK used as an expression,
608 but we also can't abort when we see it because of non-expression
609 uses. So just avert our eyes and cross our fingers. Silly Java. */
612 else if (code == BIND_EXPR)
615 copy_tree_r (tp, walk_subtrees, data);
620 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
621 after VA_ARG_EXPRs are properly lowered. */
624 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
625 void *data ATTRIBUTE_UNUSED)
628 TREE_THIS_VOLATILE (*tp) = 1;
634 /* Callback for walk_tree to unshare most of the shared trees rooted at
635 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
636 then *TP is deep copied by calling copy_tree_r.
638 This unshares the same trees as copy_tree_r with the exception of
639 SAVE_EXPR nodes. These nodes model computations that should only be
640 done once. If we were to unshare something like SAVE_EXPR(i++), the
641 gimplification process would create wrong code. */
644 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
645 void *data ATTRIBUTE_UNUSED)
648 enum tree_code code = TREE_CODE (t);
650 /* Skip types, decls, and constants. */
651 if (TREE_CODE_CLASS (code) == 't'
652 || TREE_CODE_CLASS (code) == 'd'
653 || TREE_CODE_CLASS (code) == 'c')
656 /* Special-case BIND_EXPR. We should never be copying these, therefore
657 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
658 double processing of the DECL_INITIAL, which could be seen via both
659 the BIND_EXPR_VARS and a DECL_STMT. */
660 else if (code == BIND_EXPR)
662 if (TREE_VISITED (t))
664 TREE_VISITED (t) = 1;
666 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
669 /* If this node has been visited already, unshare it and don't look
671 else if (TREE_VISITED (t))
673 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
677 /* Otherwise, mark the tree as visited and keep looking. */
680 TREE_VISITED (t) = 1;
681 if (TREE_CODE (*tp) == VA_ARG_EXPR
682 && targetm.calls.gimplify_va_arg_expr == NULL)
684 /* Mark any _DECL inside the operand as volatile to avoid
685 the optimizers messing around with it. We have to do this
686 early, otherwise we might mark a variable as volatile
687 after we gimplify other statements that use the variable
688 assuming it's not volatile. */
690 /* FIXME once most targets define the above hook, this should
691 go away (perhaps along with the #include "target.h"). */
692 walk_tree (&TREE_OPERAND (*tp, 0), mark_decls_volatile_r,
701 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
702 void *data ATTRIBUTE_UNUSED)
704 if (TREE_VISITED (*tp))
705 TREE_VISITED (*tp) = 0;
712 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
715 unshare_all_trees (tree t)
717 walk_tree (&t, copy_if_shared_r, NULL, NULL);
718 walk_tree (&t, unmark_visited_r, NULL, NULL);
721 /* Unconditionally make an unshared copy of EXPR. This is used when using
722 stored expressions which span multiple functions, such as BINFO_VTABLE,
723 as the normal unsharing process can't tell that they're shared. */
726 unshare_expr (tree expr)
728 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
732 /* A terser interface for building a representation of a exception
736 gimple_build_eh_filter (tree body, tree allowed, tree failure)
740 /* FIXME should the allowed types go in TREE_TYPE? */
741 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
742 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
744 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
745 append_to_statement_list (body, &TREE_OPERAND (t, 0));
751 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
752 contain statements and have a value. Assign its value to a temporary
753 and give it void_type_node. Returns the temporary, or NULL_TREE if
754 WRAPPER was already void. */
757 voidify_wrapper_expr (tree wrapper, tree temp)
759 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
761 tree *p, sub = wrapper;
764 /* Set p to point to the body of the wrapper. */
765 switch (TREE_CODE (sub))
768 /* For a BIND_EXPR, the body is operand 1. */
769 p = &BIND_EXPR_BODY (sub);
773 p = &TREE_OPERAND (sub, 0);
777 /* Advance to the last statement. Set all container types to void. */
778 if (TREE_CODE (*p) == STATEMENT_LIST)
780 tree_stmt_iterator i = tsi_last (*p);
781 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
785 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
787 TREE_SIDE_EFFECTS (*p) = 1;
788 TREE_TYPE (*p) = void_type_node;
792 if (p == NULL || IS_EMPTY_STMT (*p))
794 /* Look through exception handling. */
795 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
796 || TREE_CODE (*p) == TRY_CATCH_EXPR)
801 /* The C++ frontend already did this for us. */
802 else if (TREE_CODE (*p) == INIT_EXPR)
803 temp = TREE_OPERAND (*p, 0);
804 /* If we're returning a dereference, move the dereference
805 outside the wrapper. */
806 else if (TREE_CODE (*p) == INDIRECT_REF)
808 tree ptr = TREE_OPERAND (*p, 0);
809 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
810 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
811 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
812 /* If this is a BIND_EXPR for a const inline function, it might not
813 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
814 TREE_SIDE_EFFECTS (wrapper) = 1;
819 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
820 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
821 TREE_SIDE_EFFECTS (wrapper) = 1;
824 TREE_TYPE (wrapper) = void_type_node;
831 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
832 a temporary through which they communicate. */
835 build_stack_save_restore (tree *save, tree *restore)
837 tree save_call, tmp_var;
840 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
842 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
844 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
846 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
847 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
850 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
852 static enum gimplify_status
853 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
855 tree bind_expr = *expr_p;
856 bool old_save_stack = gimplify_ctxp->save_stack;
859 temp = voidify_wrapper_expr (bind_expr, temp);
861 /* Mark variables seen in this bind expr. */
862 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
863 t->decl.seen_in_bind_expr = 1;
865 gimple_push_bind_expr (bind_expr);
866 gimplify_ctxp->save_stack = false;
868 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
870 if (gimplify_ctxp->save_stack)
872 tree stack_save, stack_restore;
874 /* Save stack on entry and restore it on exit. Add a try_finally
875 block to achieve this. Note that mudflap depends on the
876 format of the emitted code: see mx_register_decls(). */
877 build_stack_save_restore (&stack_save, &stack_restore);
879 t = build (TRY_FINALLY_EXPR, void_type_node,
880 BIND_EXPR_BODY (bind_expr), NULL_TREE);
881 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
883 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
884 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
885 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
888 gimplify_ctxp->save_stack = old_save_stack;
889 gimple_pop_bind_expr ();
894 append_to_statement_list (bind_expr, pre_p);
901 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
902 GIMPLE value, it is assigned to a new temporary and the statement is
903 re-written to return the temporary.
905 PRE_P points to the list where side effects that must happen before
906 STMT should be stored. */
908 static enum gimplify_status
909 gimplify_return_expr (tree stmt, tree *pre_p)
911 tree ret_expr = TREE_OPERAND (stmt, 0);
912 tree result_decl, result;
914 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
917 if (ret_expr == error_mark_node)
920 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
921 result_decl = NULL_TREE;
924 result_decl = TREE_OPERAND (ret_expr, 0);
925 #ifdef ENABLE_CHECKING
926 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
927 && TREE_CODE (ret_expr) != INIT_EXPR)
928 || TREE_CODE (result_decl) != RESULT_DECL)
933 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
934 Recall that aggregate_value_p is FALSE for any aggregate type that is
935 returned in registers. If we're returning values in registers, then
936 we don't want to extend the lifetime of the RESULT_DECL, particularly
937 across another call. In addition, for those aggregates for which
938 hard_function_value generates a PARALLEL, we'll abort during normal
939 expansion of structure assignments; there's special code in expand_return
940 to handle this case that does not exist in expand_expr. */
942 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
943 result = result_decl;
944 else if (gimplify_ctxp->return_temp)
945 result = gimplify_ctxp->return_temp;
948 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
950 /* ??? With complex control flow (usually involving abnormal edges),
951 we can wind up warning about an uninitialized value for this. Due
952 to how this variable is constructed and initialized, this is never
953 true. Give up and never warn. */
954 TREE_NO_WARNING (result) = 1;
956 gimplify_ctxp->return_temp = result;
959 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
960 Then gimplify the whole thing. */
961 if (result != result_decl)
962 TREE_OPERAND (ret_expr, 0) = result;
963 gimplify_stmt (&TREE_OPERAND (stmt, 0));
964 append_to_statement_list (TREE_OPERAND (stmt, 0), pre_p);
966 /* If we didn't use a temporary, then the result is just the result_decl.
967 Otherwise we need a simple copy. This should already be gimple. */
968 if (result == result_decl)
971 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
972 TREE_OPERAND (stmt, 0) = ret_expr;
977 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
978 and replacing the LOOP_EXPR with goto, but if the loop contains an
979 EXIT_EXPR, we need to append a label for it to jump to. */
981 static enum gimplify_status
982 gimplify_loop_expr (tree *expr_p, tree *pre_p)
984 tree saved_label = gimplify_ctxp->exit_label;
985 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
986 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
988 append_to_statement_list (start_label, pre_p);
990 gimplify_ctxp->exit_label = NULL_TREE;
992 gimplify_stmt (&LOOP_EXPR_BODY (*expr_p));
993 append_to_statement_list (LOOP_EXPR_BODY (*expr_p), pre_p);
995 if (gimplify_ctxp->exit_label)
997 append_to_statement_list (jump_stmt, pre_p);
998 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1001 *expr_p = jump_stmt;
1003 gimplify_ctxp->exit_label = saved_label;
1008 /* Compare two case labels. Because the front end should already have
1009 made sure that case ranges do not overlap, it is enough to only compare
1010 the CASE_LOW values of each case label. */
1013 compare_case_labels (const void *p1, const void *p2)
1015 tree case1 = *(tree *)p1;
1016 tree case2 = *(tree *)p2;
1018 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1021 /* Sort the case labels in LABEL_VEC in ascending order. */
1024 sort_case_labels (tree label_vec)
1026 size_t len = TREE_VEC_LENGTH (label_vec);
1027 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1029 if (CASE_LOW (default_case))
1033 /* The last label in the vector should be the default case
1035 for (i = 0; i < len; ++i)
1037 tree t = TREE_VEC_ELT (label_vec, i);
1041 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1042 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1048 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1049 compare_case_labels);
1052 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1055 static enum gimplify_status
1056 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1058 tree switch_expr = *expr_p;
1059 enum gimplify_status ret;
1061 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1062 is_gimple_val, fb_rvalue);
1064 if (SWITCH_BODY (switch_expr))
1066 varray_type labels, saved_labels;
1067 tree label_vec, default_case = NULL_TREE;
1070 /* If someone can be bothered to fill in the labels, they can
1071 be bothered to null out the body too. */
1072 if (SWITCH_LABELS (switch_expr))
1075 saved_labels = gimplify_ctxp->case_labels;
1076 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1078 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1080 labels = gimplify_ctxp->case_labels;
1081 gimplify_ctxp->case_labels = saved_labels;
1083 len = VARRAY_ACTIVE_SIZE (labels);
1085 for (i = 0; i < len; ++i)
1087 tree t = VARRAY_TREE (labels, i);
1090 /* The default case must be the last label in the list. */
1092 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1098 label_vec = make_tree_vec (len + 1);
1099 SWITCH_LABELS (*expr_p) = label_vec;
1100 append_to_statement_list (switch_expr, pre_p);
1104 /* If the switch has no default label, add one, so that we jump
1105 around the switch body. */
1106 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1107 NULL_TREE, create_artificial_label ());
1108 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1109 *expr_p = build (LABEL_EXPR, void_type_node,
1110 CASE_LABEL (default_case));
1113 *expr_p = SWITCH_BODY (switch_expr);
1115 for (i = 0; i < len; ++i)
1116 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1117 TREE_VEC_ELT (label_vec, len) = default_case;
1119 sort_case_labels (label_vec);
1121 SWITCH_BODY (switch_expr) = NULL;
1123 else if (!SWITCH_LABELS (switch_expr))
1129 static enum gimplify_status
1130 gimplify_case_label_expr (tree *expr_p)
1132 tree expr = *expr_p;
1133 if (gimplify_ctxp->case_labels)
1134 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1137 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1141 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1142 a (possibly empty) body. */
1144 static enum gimplify_status
1145 gimplify_labeled_block_expr (tree *expr_p)
1147 tree body = LABELED_BLOCK_BODY (*expr_p);
1148 tree label = LABELED_BLOCK_LABEL (*expr_p);
1151 DECL_CONTEXT (label) = current_function_decl;
1152 t = build (LABEL_EXPR, void_type_node, label);
1153 if (body != NULL_TREE)
1154 t = build (COMPOUND_EXPR, void_type_node, body, t);
1160 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1162 static enum gimplify_status
1163 gimplify_exit_block_expr (tree *expr_p)
1165 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1168 /* First operand must be a LABELED_BLOCK_EXPR, which should
1169 already be lowered (or partially lowered) when we get here. */
1170 #if defined ENABLE_CHECKING
1171 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1175 label = LABELED_BLOCK_LABEL (labeled_block);
1176 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1181 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1185 build_and_jump (tree *label_p)
1187 if (label_p == NULL)
1188 /* If there's nowhere to jump, just fall through. */
1189 return build_empty_stmt ();
1191 if (*label_p == NULL_TREE)
1193 tree label = create_artificial_label ();
1197 return build1 (GOTO_EXPR, void_type_node, *label_p);
1200 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1201 This also involves building a label to jump to and communicating it to
1202 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1204 static enum gimplify_status
1205 gimplify_exit_expr (tree *expr_p)
1207 tree cond = TREE_OPERAND (*expr_p, 0);
1210 expr = build_and_jump (&gimplify_ctxp->exit_label);
1211 expr = build (COND_EXPR, void_type_node, cond, expr, build_empty_stmt ());
1217 /* A helper function to be called via walk_tree. Mark all labels under *TP
1218 as being forced. To be called for DECL_INITIAL of static variables. */
1221 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1225 if (TREE_CODE (*tp) == LABEL_DECL)
1226 FORCED_LABEL (*tp) = 1;
1231 /* Break out elements of a constructor used as an initializer into separate
1234 Note that we still need to clear any elements that don't have explicit
1235 initializers, so if not all elements are initialized we keep the
1236 original MODIFY_EXPR, we just remove all of the constructor elements. */
1238 static enum gimplify_status
1239 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1240 tree *post_p, int want_value)
1242 tree object = TREE_OPERAND (*expr_p, 0);
1243 tree ctor = TREE_OPERAND (*expr_p, 1);
1244 tree type = TREE_TYPE (ctor);
1245 enum gimplify_status ret;
1248 if (TREE_CODE (ctor) != CONSTRUCTOR)
1249 return GS_UNHANDLED;
1251 elt_list = CONSTRUCTOR_ELTS (ctor);
1254 switch (TREE_CODE (type))
1258 case QUAL_UNION_TYPE:
1261 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1262 HOST_WIDE_INT num_nonconstant_elements;
1265 /* Aggregate types must lower constructors to initialization of
1266 individual elements. The exception is that a CONSTRUCTOR node
1267 with no elements indicates zero-initialization of the whole. */
1268 if (elt_list == NULL)
1279 categorize_ctor_elements (ctor, &num_nonzero_elements,
1280 &num_nonconstant_elements);
1281 num_elements = count_type_elements (TREE_TYPE (ctor));
1283 /* If a const aggregate variable is being initialized, then it
1284 should never be a lose to promote the variable to be static. */
1285 if (num_nonconstant_elements == 0
1286 && TREE_READONLY (object)
1287 && TREE_CODE (object) == VAR_DECL)
1289 DECL_INITIAL (object) = ctor;
1290 TREE_STATIC (object) = 1;
1291 if (!DECL_NAME (object))
1292 DECL_NAME (object) = create_tmp_var_name ("C");
1293 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1295 /* ??? C++ doesn't automatically append a .<number> to the
1296 assembler name, and even when it does, it looks a FE private
1297 data structures to figure out what that number should be,
1298 which are not set for this variable. I suppose this is
1299 important for local statics for inline functions, which aren't
1300 "local" in the object file sense. So in order to get a unique
1301 TU-local symbol, we must invoke the lhd version now. */
1302 lhd_set_decl_assembler_name (object);
1304 *expr_p = build_empty_stmt ();
1308 /* If there are "lots" of initialized elements, and all of them
1309 are valid address constants, then the entire initializer can
1310 be dropped to memory, and then memcpy'd out. */
1311 if (num_nonconstant_elements == 0)
1313 HOST_WIDE_INT size = int_size_in_bytes (type);
1316 /* ??? We can still get unbounded array types, at least
1317 from the C++ front end. This seems wrong, but attempt
1318 to work around it for now. */
1321 size = int_size_in_bytes (TREE_TYPE (object));
1323 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1326 /* Find the maximum alignment we can assume for the object. */
1327 /* ??? Make use of DECL_OFFSET_ALIGN. */
1328 if (DECL_P (object))
1329 align = DECL_ALIGN (object);
1331 align = TYPE_ALIGN (type);
1333 if (size > 0 && !can_move_by_pieces (size, align))
1335 tree new = create_tmp_var_raw (type, "C");
1336 gimple_add_tmp_var (new);
1337 TREE_STATIC (new) = 1;
1338 TREE_READONLY (new) = 1;
1339 DECL_INITIAL (new) = ctor;
1340 if (align > DECL_ALIGN (new))
1342 DECL_ALIGN (new) = align;
1343 DECL_USER_ALIGN (new) = 1;
1345 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1347 TREE_OPERAND (*expr_p, 1) = new;
1352 /* If there are "lots" of initialized elements, even discounting
1353 those that are not address constants (and thus *must* be
1354 computed at runtime), then partition the constructor into
1355 constant and non-constant parts. Block copy the constant
1356 parts in, then generate code for the non-constant parts. */
1357 /* TODO. There's code in cp/typeck.c to do this. */
1359 /* If there are "lots" of zeros, then block clear the object first. */
1361 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1362 && num_nonzero_elements < num_elements/4)
1365 /* ??? This bit ought not be needed. For any element not present
1366 in the initializer, we should simply set them to zero. Except
1367 we'd need to *find* the elements that are not present, and that
1368 requires trickery to avoid quadratic compile-time behavior in
1369 large cases or excessive memory use in small cases. */
1372 HOST_WIDE_INT len = list_length (elt_list);
1373 if (TREE_CODE (type) == ARRAY_TYPE)
1375 tree nelts = array_type_nelts (type);
1376 if (!host_integerp (nelts, 1)
1377 || tree_low_cst (nelts, 1) != len)
1380 else if (len != fields_length (type))
1386 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1387 append_to_statement_list (*expr_p, pre_p);
1390 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1392 tree purpose, value, cref, init;
1394 purpose = TREE_PURPOSE (elt_list);
1395 value = TREE_VALUE (elt_list);
1397 if (cleared && initializer_zerop (value))
1400 if (TREE_CODE (type) == ARRAY_TYPE)
1402 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1404 /* ??? Here's to hoping the front end fills in all of the
1405 indicies, so we don't have to figure out what's missing
1409 /* ??? Need to handle this. */
1410 if (TREE_CODE (purpose) == RANGE_EXPR)
1413 cref = build (ARRAY_REF, t, object, purpose);
1417 cref = build (COMPONENT_REF, TREE_TYPE (purpose),
1421 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1422 /* Each member initialization is a full-expression. */
1423 gimplify_stmt (&init);
1424 append_to_statement_list (init, pre_p);
1427 *expr_p = build_empty_stmt ();
1435 /* Extract the real and imaginary parts out of the ctor. */
1439 r = TREE_VALUE (elt_list);
1440 elt_list = TREE_CHAIN (elt_list);
1443 i = TREE_VALUE (elt_list);
1444 if (TREE_CHAIN (elt_list))
1448 if (r == NULL || i == NULL)
1450 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1457 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1458 represent creation of a complex value. */
1459 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1461 ctor = build_complex (type, r, i);
1462 TREE_OPERAND (*expr_p, 1) = ctor;
1466 ctor = build (COMPLEX_EXPR, type, r, i);
1467 TREE_OPERAND (*expr_p, 1) = ctor;
1468 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1469 is_gimple_rhs, fb_rvalue);
1475 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1476 if (TREE_CONSTANT (ctor))
1477 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1480 /* Vector types use CONSTRUCTOR all the way through gimple
1481 compilation as a general initializer. */
1482 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1484 enum gimplify_status tret;
1485 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1486 is_gimple_constructor_elt, fb_rvalue);
1487 if (tret == GS_ERROR)
1494 /* So how did we get a CONSTRUCTOR for a scalar type? */
1498 if (ret == GS_ERROR)
1500 else if (want_value)
1502 append_to_statement_list (*expr_p, pre_p);
1510 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1511 different from its canonical type, wrap the whole thing inside a
1512 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1515 The canonical type of a COMPONENT_REF is the type of the field being
1516 referenced--unless the field is a bit-field which can be read directly
1517 in a smaller mode, in which case the canonical type is the
1518 sign-appropriate type corresponding to that mode. */
1521 canonicalize_component_ref (tree *expr_p)
1523 tree expr = *expr_p;
1526 if (TREE_CODE (expr) != COMPONENT_REF)
1529 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1530 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1532 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1534 if (TREE_TYPE (expr) != type)
1536 tree old_type = TREE_TYPE (expr);
1538 /* Set the type of the COMPONENT_REF to the underlying type. */
1539 TREE_TYPE (expr) = type;
1541 /* And wrap the whole thing inside a NOP_EXPR. */
1542 expr = build1 (NOP_EXPR, old_type, expr);
1548 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1549 to foo, embed that change in the ADDR_EXPR. Lest we perturb the type
1550 system too badly, we must take extra steps to ensure that the ADDR_EXPR
1551 and the addressed object continue to agree on types. */
1552 /* ??? We might could do better if we recognize
1560 canonicalize_addr_expr (tree* expr_p)
1562 tree expr = *expr_p;
1563 tree ctype = TREE_TYPE (expr);
1564 tree addr_expr = TREE_OPERAND (expr, 0);
1565 tree atype = TREE_TYPE (addr_expr);
1566 tree dctype, datype, ddatype, otype, obj_expr;
1568 /* Both cast and addr_expr types should be pointers. */
1569 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1572 /* The addr_expr type should be a pointer to an array. */
1573 datype = TREE_TYPE (atype);
1574 if (TREE_CODE (datype) != ARRAY_TYPE)
1577 /* Both cast and addr_expr types should address the same object type. */
1578 dctype = TREE_TYPE (ctype);
1579 ddatype = TREE_TYPE (datype);
1580 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1583 /* The addr_expr and the object type should match. */
1584 obj_expr = TREE_OPERAND (addr_expr, 0);
1585 otype = TREE_TYPE (obj_expr);
1586 if (!lang_hooks.types_compatible_p (otype, datype))
1589 /* All checks succeeded. Build a new node to merge the cast. */
1590 *expr_p = build1 (ADDR_EXPR, ctype, obj_expr);
1593 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1594 underneath as appropriate. */
1596 static enum gimplify_status
1597 gimplify_conversion (tree *expr_p)
1599 /* Strip away as many useless type conversions as possible
1601 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1603 /* If we still have a conversion at the toplevel, then strip
1604 away all but the outermost conversion. */
1605 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1607 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1609 /* And remove the outermost conversion if it's useless. */
1610 if (tree_ssa_useless_type_conversion (*expr_p))
1611 *expr_p = TREE_OPERAND (*expr_p, 0);
1614 /* If we still have a conversion at the toplevel,
1615 then canonicalize some constructs. */
1616 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1618 tree sub = TREE_OPERAND (*expr_p, 0);
1620 /* If a NOP conversion is changing the type of a COMPONENT_REF
1621 expression, then canonicalize its type now in order to expose more
1622 redundant conversions. */
1623 if (TREE_CODE (sub) == COMPONENT_REF)
1624 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1626 /* If a NOP conversion is changing a pointer to array of foo
1627 to a pointer to foo, embed that change in the ADDR_EXPR. */
1628 else if (TREE_CODE (sub) == ADDR_EXPR)
1629 canonicalize_addr_expr (expr_p);
1635 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1637 static enum gimplify_status
1638 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1640 tree op1 = TREE_OPERAND (*expr_p, 0);
1641 tree op2 = TREE_OPERAND (*expr_p, 1);
1642 enum tree_code code;
1643 enum gimplify_status r0, r1;
1645 if (TREE_CODE (*expr_p) == MIN_EXPR)
1650 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1651 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1653 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1654 build (code, boolean_type_node, op1, op2),
1657 if (r0 == GS_ERROR || r1 == GS_ERROR)
1663 /* Subroutine of gimplify_compound_lval and gimplify_array_ref.
1664 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1666 static enum gimplify_status
1667 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1669 tree array = TREE_OPERAND (*expr_p, 0);
1670 tree arrtype = TREE_TYPE (array);
1671 tree elttype = TREE_TYPE (arrtype);
1672 tree size = size_in_bytes (elttype);
1673 tree ptrtype = build_pointer_type (elttype);
1674 enum tree_code add_code = PLUS_EXPR;
1675 tree idx = TREE_OPERAND (*expr_p, 1);
1676 tree minidx, offset, addr, result;
1677 enum gimplify_status ret;
1679 /* If the array domain does not start at zero, apply the offset. */
1680 minidx = TYPE_DOMAIN (arrtype);
1683 minidx = TYPE_MIN_VALUE (minidx);
1684 if (minidx && !integer_zerop (minidx))
1686 idx = convert (TREE_TYPE (minidx), idx);
1687 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1691 /* If the index is negative -- a technically invalid situation now
1692 that we've biased the index back to zero -- then casting it to
1693 unsigned has ill effects. In particular, -1*4U/4U != -1.
1694 Represent this as a subtraction of a positive rather than addition
1695 of a negative. This will prevent any conversion back to ARRAY_REF
1696 from getting the wrong results from the division. */
1697 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1699 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1700 add_code = MINUS_EXPR;
1703 /* Pointer arithmetic must be done in sizetype. */
1704 idx = convert (sizetype, idx);
1706 /* Convert the index to a byte offset. */
1707 offset = size_binop (MULT_EXPR, size, idx);
1709 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1710 if (ret == GS_ERROR)
1713 addr = build_fold_addr_expr_with_type (array, ptrtype);
1714 result = fold (build (add_code, ptrtype, addr, offset));
1715 *expr_p = build1 (INDIRECT_REF, elttype, result);
1720 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1721 node pointed by EXPR_P.
1724 : min_lval '[' val ']'
1726 | compound_lval '[' val ']'
1727 | compound_lval '.' ID
1729 This is not part of the original SIMPLE definition, which separates
1730 array and member references, but it seems reasonable to handle them
1731 together. Also, this way we don't run into problems with union
1732 aliasing; gcc requires that for accesses through a union to alias, the
1733 union reference must be explicit, which was not always the case when we
1734 were splitting up array and member refs.
1736 PRE_P points to the list where side effects that must happen before
1737 *EXPR_P should be stored.
1739 POST_P points to the list where side effects that must happen after
1740 *EXPR_P should be stored. */
1742 static enum gimplify_status
1743 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1744 tree *post_p, int want_lvalue)
1747 enum tree_code code;
1749 enum gimplify_status ret;
1751 #if defined ENABLE_CHECKING
1752 if (TREE_CODE (*expr_p) != ARRAY_REF
1753 && TREE_CODE (*expr_p) != COMPONENT_REF
1754 && TREE_CODE (*expr_p) != REALPART_EXPR
1755 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1759 code = ERROR_MARK; /* [GIMPLE] Avoid uninitialized use warning. */
1761 /* Create a stack of the subexpressions so later we can walk them in
1762 order from inner to outer. */
1763 VARRAY_TREE_INIT (stack, 10, "stack");
1766 TREE_CODE (*p) == ARRAY_REF
1767 || TREE_CODE (*p) == COMPONENT_REF
1768 || TREE_CODE (*p) == REALPART_EXPR
1769 || TREE_CODE (*p) == IMAGPART_EXPR;
1770 p = &TREE_OPERAND (*p, 0))
1772 code = TREE_CODE (*p);
1773 if (code == ARRAY_REF)
1775 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*p, 0)));
1776 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1777 /* If the size of the array elements is not constant,
1778 computing the offset is non-trivial, so expose it. */
1781 VARRAY_PUSH_TREE (stack, *p);
1784 /* Now 'p' points to the first bit that isn't a ref, 'code' is the
1785 TREE_CODE of the last bit that was, and 'stack' is a stack of pointers
1786 to all the refs we've walked through.
1788 Gimplify the base, and then process each of the outer nodes from left
1790 ret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1791 code != ARRAY_REF ? fb_either : fb_lvalue);
1793 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1795 tree t = VARRAY_TOP_TREE (stack);
1796 if (TREE_CODE (t) == ARRAY_REF)
1798 /* Gimplify the dimension. */
1799 enum gimplify_status tret;
1800 /* Temporary fix for gcc.c-torture/execute/20040313-1.c.
1801 Gimplify non-constant array indices into a temporary
1803 FIXME - The real fix is to gimplify post-modify
1804 expressions into a minimal gimple lvalue. However, that
1805 exposes bugs in alias analysis. The alias analyzer does
1806 not handle &PTR->FIELD very well. Will fix after the
1807 branch is merged into mainline (dnovillo 2004-05-03). */
1808 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1810 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1811 is_gimple_tmp_var, fb_rvalue);
1812 if (tret == GS_ERROR)
1816 recalculate_side_effects (t);
1820 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1821 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1823 canonicalize_component_ref (expr_p);
1824 ret = MIN (ret, GS_OK);
1830 /* Re-write the ARRAY_REF node pointed by EXPR_P.
1832 PRE_P points to the list where side effects that must happen before
1833 *EXPR_P should be stored.
1835 POST_P points to the list where side effects that must happen after
1836 *EXPR_P should be stored.
1838 FIXME: ARRAY_REF currently doesn't accept a pointer as the array
1839 argument, so this gimplification uses an INDIRECT_REF of ARRAY_TYPE.
1840 ARRAY_REF should be extended. */
1842 static enum gimplify_status
1843 gimplify_array_ref (tree *expr_p, tree *pre_p,
1844 tree *post_p, int want_lvalue)
1846 tree elttype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (*expr_p, 0)));
1847 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (elttype)))
1848 /* If the size of the array elements is not constant,
1849 computing the offset is non-trivial, so expose it. */
1850 return gimplify_array_ref_to_plus (expr_p, pre_p, post_p);
1852 /* Handle array and member refs together for now. When alias analysis
1853 improves, we may want to go back to handling them separately. */
1854 return gimplify_compound_lval (expr_p, pre_p, post_p, want_lvalue);
1857 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1859 PRE_P points to the list where side effects that must happen before
1860 *EXPR_P should be stored.
1862 POST_P points to the list where side effects that must happen after
1863 *EXPR_P should be stored.
1865 WANT_VALUE is nonzero iff we want to use the value of this expression
1866 in another expression. */
1868 static enum gimplify_status
1869 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1872 enum tree_code code;
1873 tree lhs, lvalue, rhs, t1;
1875 enum tree_code arith_code;
1876 enum gimplify_status ret;
1878 code = TREE_CODE (*expr_p);
1880 #if defined ENABLE_CHECKING
1881 if (code != POSTINCREMENT_EXPR
1882 && code != POSTDECREMENT_EXPR
1883 && code != PREINCREMENT_EXPR
1884 && code != PREDECREMENT_EXPR)
1888 /* Prefix or postfix? */
1889 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1890 /* Faster to treat as prefix if result is not used. */
1891 postfix = want_value;
1895 /* Add or subtract? */
1896 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1897 arith_code = PLUS_EXPR;
1899 arith_code = MINUS_EXPR;
1901 /* Gimplify the LHS into a GIMPLE lvalue. */
1902 lvalue = TREE_OPERAND (*expr_p, 0);
1903 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1904 if (ret == GS_ERROR)
1907 /* Extract the operands to the arithmetic operation. */
1909 rhs = TREE_OPERAND (*expr_p, 1);
1911 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1912 that as the result value and in the postqueue operation. */
1915 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1916 if (ret == GS_ERROR)
1920 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1921 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
1925 gimplify_stmt (&t1);
1926 append_to_statement_list (t1, post_p);
1937 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
1940 : ID '(' arglist ')'
1946 PRE_P points to the list where side effects that must happen before
1947 *EXPR_P should be stored. */
1949 static enum gimplify_status
1950 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
1954 enum gimplify_status ret;
1956 #if defined ENABLE_CHECKING
1957 if (TREE_CODE (*expr_p) != CALL_EXPR)
1961 /* For reliable diagnostics during inlining, it is necessary that
1962 every call_expr be annotated with file and line. */
1963 if (!EXPR_LOCUS (*expr_p))
1964 annotate_with_locus (*expr_p, input_location);
1966 /* This may be a call to a builtin function.
1968 Builtin function calls may be transformed into different
1969 (and more efficient) builtin function calls under certain
1970 circumstances. Unfortunately, gimplification can muck things
1971 up enough that the builtin expanders are not aware that certain
1972 transformations are still valid.
1974 So we attempt transformation/gimplification of the call before
1975 we gimplify the CALL_EXPR. At this time we do not manage to
1976 transform all calls in the same manner as the expanders do, but
1977 we do transform most of them. */
1978 decl = get_callee_fndecl (*expr_p);
1979 if (decl && DECL_BUILT_IN (decl))
1983 /* If it is allocation of stack, record the need to restore the memory
1984 when the enclosing bind_expr is exited. */
1985 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
1986 gimplify_ctxp->save_stack = true;
1988 /* If it is restore of the stack, reset it, since it means we are
1989 regimplifying the bind_expr. Note that we use the fact that
1990 for try_finally_expr, try part is processed first. */
1991 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
1992 gimplify_ctxp->save_stack = false;
1994 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
1996 if (new && new != *expr_p)
1998 /* There was a transformation of this call which computes the
1999 same value, but in a more efficient way. Return and try
2006 /* There is a sequence point before the call, so any side effects in
2007 the calling expression must occur before the actual call. Force
2008 gimplify_expr to use an internal post queue. */
2009 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2010 is_gimple_val, fb_rvalue);
2012 if (PUSH_ARGS_REVERSED)
2013 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2014 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2015 arglist = TREE_CHAIN (arglist))
2017 enum gimplify_status t;
2019 /* There is a sequence point before a function call. Side effects in
2020 the argument list must occur before the actual call. So, when
2021 gimplifying arguments, force gimplify_expr to use an internal
2022 post queue which is then appended to the end of PRE_P. */
2023 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2029 if (PUSH_ARGS_REVERSED)
2030 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2032 /* Try this again in case gimplification exposed something. */
2033 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2035 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2037 if (new && new != *expr_p)
2039 /* There was a transformation of this call which computes the
2040 same value, but in a more efficient way. Return and try
2047 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2048 decl. This allows us to eliminate redundant or useless
2049 calls to "const" functions. */
2050 if (TREE_CODE (*expr_p) == CALL_EXPR
2051 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2052 TREE_SIDE_EFFECTS (*expr_p) = 0;
2057 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2058 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2060 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2061 condition is true or false, respectively. If null, we should generate
2062 our own to skip over the evaluation of this specific expression.
2064 This function is the tree equivalent of do_jump.
2066 shortcut_cond_r should only be called by shortcut_cond_expr. */
2069 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2071 tree local_label = NULL_TREE;
2072 tree t, expr = NULL;
2074 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2075 retain the shortcut semantics. Just insert the gotos here;
2076 shortcut_cond_expr will append the real blocks later. */
2077 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2079 /* Turn if (a && b) into
2081 if (a); else goto no;
2082 if (b) goto yes; else goto no;
2085 if (false_label_p == NULL)
2086 false_label_p = &local_label;
2088 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2089 append_to_statement_list (t, &expr);
2091 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2093 append_to_statement_list (t, &expr);
2095 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2097 /* Turn if (a || b) into
2100 if (b) goto yes; else goto no;
2103 if (true_label_p == NULL)
2104 true_label_p = &local_label;
2106 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2107 append_to_statement_list (t, &expr);
2109 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2111 append_to_statement_list (t, &expr);
2113 else if (TREE_CODE (pred) == COND_EXPR)
2115 /* As long as we're messing with gotos, turn if (a ? b : c) into
2117 if (b) goto yes; else goto no;
2119 if (c) goto yes; else goto no; */
2120 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2121 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2123 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2128 expr = build (COND_EXPR, void_type_node, pred,
2129 build_and_jump (true_label_p),
2130 build_and_jump (false_label_p));
2135 t = build1 (LABEL_EXPR, void_type_node, local_label);
2136 append_to_statement_list (t, &expr);
2143 shortcut_cond_expr (tree expr)
2145 tree pred = TREE_OPERAND (expr, 0);
2146 tree then_ = TREE_OPERAND (expr, 1);
2147 tree else_ = TREE_OPERAND (expr, 2);
2148 tree true_label, false_label, end_label, t;
2150 tree *false_label_p;
2151 bool emit_end, emit_false;
2153 /* First do simple transformations. */
2154 if (!TREE_SIDE_EFFECTS (else_))
2156 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2157 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2159 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2160 then_ = shortcut_cond_expr (expr);
2161 pred = TREE_OPERAND (pred, 0);
2162 expr = build (COND_EXPR, void_type_node, pred, then_,
2163 build_empty_stmt ());
2166 if (!TREE_SIDE_EFFECTS (then_))
2168 /* If there is no 'then', turn
2171 if (a); else if (b); else d. */
2172 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2174 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2175 else_ = shortcut_cond_expr (expr);
2176 pred = TREE_OPERAND (pred, 0);
2177 expr = build (COND_EXPR, void_type_node, pred,
2178 build_empty_stmt (), else_);
2182 /* If we're done, great. */
2183 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2184 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2187 /* Otherwise we need to mess with gotos. Change
2190 if (a); else goto no;
2193 and recursively gimplify the condition. */
2195 true_label = false_label = end_label = NULL_TREE;
2197 /* If our arms just jump somewhere, hijack those labels so we don't
2198 generate jumps to jumps. */
2200 if (TREE_CODE (then_) == GOTO_EXPR
2201 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2203 true_label = GOTO_DESTINATION (then_);
2204 then_ = build_empty_stmt ();
2207 if (TREE_CODE (else_) == GOTO_EXPR
2208 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2210 false_label = GOTO_DESTINATION (else_);
2211 else_ = build_empty_stmt ();
2214 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2216 true_label_p = &true_label;
2218 true_label_p = NULL;
2220 /* The 'else' branch also needs a label if it contains interesting code. */
2221 if (false_label || TREE_SIDE_EFFECTS (else_))
2222 false_label_p = &false_label;
2224 false_label_p = NULL;
2226 /* If there was nothing else in our arms, just forward the label(s). */
2227 if (!TREE_SIDE_EFFECTS (then_) && !TREE_SIDE_EFFECTS (else_))
2228 return shortcut_cond_r (pred, true_label_p, false_label_p);
2230 /* If our last subexpression already has a terminal label, reuse it. */
2231 if (TREE_SIDE_EFFECTS (else_))
2232 expr = expr_last (else_);
2234 expr = expr_last (then_);
2235 if (TREE_CODE (expr) == LABEL_EXPR)
2236 end_label = LABEL_EXPR_LABEL (expr);
2238 /* If we don't care about jumping to the 'else' branch, jump to the end
2239 if the condition is false. */
2241 false_label_p = &end_label;
2243 /* We only want to emit these labels if we aren't hijacking them. */
2244 emit_end = (end_label == NULL_TREE);
2245 emit_false = (false_label == NULL_TREE);
2247 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2250 append_to_statement_list (pred, &expr);
2252 append_to_statement_list (then_, &expr);
2253 if (TREE_SIDE_EFFECTS (else_))
2255 t = build_and_jump (&end_label);
2256 append_to_statement_list (t, &expr);
2259 t = build1 (LABEL_EXPR, void_type_node, false_label);
2260 append_to_statement_list (t, &expr);
2262 append_to_statement_list (else_, &expr);
2264 if (emit_end && end_label)
2266 t = build1 (LABEL_EXPR, void_type_node, end_label);
2267 append_to_statement_list (t, &expr);
2273 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2276 gimple_boolify (tree expr)
2278 tree type = TREE_TYPE (expr);
2280 if (TREE_CODE (type) == BOOLEAN_TYPE)
2283 /* If this is the predicate of a COND_EXPR, it might not even be a
2285 expr = lang_hooks.truthvalue_conversion (expr);
2287 switch (TREE_CODE (expr))
2289 case TRUTH_AND_EXPR:
2291 case TRUTH_XOR_EXPR:
2292 case TRUTH_ANDIF_EXPR:
2293 case TRUTH_ORIF_EXPR:
2294 /* Also boolify the arguments of truth exprs. */
2295 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2298 case TRUTH_NOT_EXPR:
2299 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2302 case EQ_EXPR: case NE_EXPR:
2303 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2304 /* These expressions always produce boolean results. */
2305 TREE_TYPE (expr) = boolean_type_node;
2309 /* Other expressions that get here must have boolean values, but
2310 might need to be converted to the appropriate mode. */
2311 return convert (boolean_type_node, expr);
2315 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2324 The second form is used when *EXPR_P is of type void.
2326 PRE_P points to the list where side effects that must happen before
2327 *EXPR_P should be stored. */
2329 static enum gimplify_status
2330 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2332 tree expr = *expr_p;
2334 enum gimplify_status ret;
2336 /* If this COND_EXPR has a value, copy the values into a temporary within
2338 if (! VOID_TYPE_P (TREE_TYPE (expr)))
2347 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2351 /* Build the then clause, 't1 = a;'. But don't build an assignment
2352 if this branch is void; in C++ it can be, if it's a throw. */
2353 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2354 TREE_OPERAND (expr, 1)
2355 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2357 /* Build the else clause, 't1 = b;'. */
2358 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2359 TREE_OPERAND (expr, 2)
2360 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2362 TREE_TYPE (expr) = void_type_node;
2363 recalculate_side_effects (expr);
2365 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2366 gimplify_stmt (&expr);
2367 append_to_statement_list (expr, pre_p);
2373 /* Make sure the condition has BOOLEAN_TYPE. */
2374 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2376 /* Break apart && and || conditions. */
2377 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2378 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2380 expr = shortcut_cond_expr (expr);
2382 if (expr != *expr_p)
2386 /* We can't rely on gimplify_expr to re-gimplify the expanded
2387 form properly, as cleanups might cause the target labels to be
2388 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2389 set up a conditional context. */
2390 gimple_push_condition ();
2391 gimplify_stmt (expr_p);
2392 gimple_pop_condition (pre_p);
2398 /* Now do the normal gimplification. */
2399 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2400 is_gimple_condexpr, fb_rvalue);
2402 gimple_push_condition ();
2404 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2405 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2406 recalculate_side_effects (expr);
2408 gimple_pop_condition (pre_p);
2410 if (ret == GS_ERROR)
2412 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2414 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2415 /* Rewrite "if (a); else b" to "if (!a) b" */
2417 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2418 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2419 is_gimple_condexpr, fb_rvalue);
2421 tmp = TREE_OPERAND (expr, 1);
2422 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2423 TREE_OPERAND (expr, 2) = tmp;
2426 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2427 expr = TREE_OPERAND (expr, 0);
2433 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2439 PRE_P points to the list where side effects that must happen before
2440 *EXPR_P should be stored.
2442 POST_P points to the list where side effects that must happen after
2443 *EXPR_P should be stored.
2445 WANT_VALUE is nonzero iff we want to use the value of this expression
2446 in another expression. */
2448 static enum gimplify_status
2449 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2451 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2452 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2453 enum gimplify_status ret;
2455 #if defined ENABLE_CHECKING
2456 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2460 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2461 if (TREE_CODE (*expr_p) == INIT_EXPR)
2462 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2464 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2465 if (ret == GS_ERROR)
2468 /* If we are initializing something from a TARGET_EXPR, strip the
2469 TARGET_EXPR and initialize it directly. */
2470 /* What about code that pulls out the temp and uses it elsewhere? I
2471 think that such code never uses the TARGET_EXPR as an initializer. If
2472 I'm wrong, we'll abort because the temp won't have any RTL. In that
2473 case, I guess we'll need to replace references somehow. */
2474 if (TREE_CODE (*from_p) == TARGET_EXPR)
2475 *from_p = TARGET_EXPR_INITIAL (*from_p);
2477 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2478 the assignment down into the branches, since we can't generate a
2479 temporary of such a type. */
2480 if (TREE_CODE (*from_p) == COND_EXPR
2481 && TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2484 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2487 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2488 if (ret == GS_ERROR)
2491 ret = gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2492 if (ret != GS_UNHANDLED)
2495 /* If the destination is already simple, nothing else needed. */
2496 if (is_gimple_tmp_var (*to_p))
2500 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2501 the LHS is a user variable, then we need to introduce a temporary.
2502 ie temp = RHS; LHS = temp.
2504 This way the optimizers can determine that the user variable is
2505 only modified if evaluation of the RHS does not throw.
2507 FIXME this should be handled by the is_gimple_rhs predicate. */
2509 if (aggregate_value_p (TREE_TYPE (*from_p), NULL_TREE))
2510 /* Don't force a temp of a large aggregate type; the copy could be
2511 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
2513 else if (TREE_CODE (*from_p) == CALL_EXPR
2514 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2515 /* If we're dealing with a renamable type, either source or dest
2516 must be a renamed variable. */
2517 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2518 && !is_gimple_reg (*to_p)))
2519 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2521 /* If the value being copied is of variable width, expose the length
2522 if the copy by converting the whole thing to a memcpy. */
2523 /* ??? Except that we can't manage this with VA_ARG_EXPR. Yes, this
2524 does leave us with an edge condition that doesn't work. The only
2525 way out is to rearrange how VA_ARG_EXPR works. */
2526 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST
2527 && TREE_CODE (*from_p) != VA_ARG_EXPR)
2531 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2532 t = unshare_expr (t);
2533 args = tree_cons (NULL, t, NULL);
2534 t = build_fold_addr_expr (*from_p);
2535 args = tree_cons (NULL, t, args);
2536 dest = build_fold_addr_expr (*to_p);
2537 args = tree_cons (NULL, dest, args);
2538 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2539 t = build_function_call_expr (t, args);
2542 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2543 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2550 ret = want_value ? GS_OK : GS_ALL_DONE;
2555 append_to_statement_list (*expr_p, pre_p);
2562 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2563 points to the expression to gimplify.
2565 Expressions of the form 'a && b' are gimplified to:
2567 a && b ? true : false
2569 gimplify_cond_expr will do the rest.
2571 PRE_P points to the list where side effects that must happen before
2572 *EXPR_P should be stored. */
2574 static enum gimplify_status
2575 gimplify_boolean_expr (tree *expr_p)
2577 /* Preserve the original type of the expression. */
2578 tree type = TREE_TYPE (*expr_p);
2580 *expr_p = build (COND_EXPR, type, *expr_p,
2581 convert (type, boolean_true_node),
2582 convert (type, boolean_false_node));
2587 /* Gimplifies an expression sequence. This function gimplifies each
2588 expression and re-writes the original expression with the last
2589 expression of the sequence in GIMPLE form.
2591 PRE_P points to the list where the side effects for all the
2592 expressions in the sequence will be emitted.
2594 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2595 /* ??? Should rearrange to share the pre-queue with all the indirect
2596 invocations of gimplify_expr. Would probably save on creations
2597 of statement_list nodes. */
2599 static enum gimplify_status
2600 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2606 tree *sub_p = &TREE_OPERAND (t, 0);
2608 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2609 gimplify_compound_expr (sub_p, pre_p, false);
2611 gimplify_stmt (sub_p);
2612 append_to_statement_list (*sub_p, pre_p);
2614 t = TREE_OPERAND (t, 1);
2616 while (TREE_CODE (t) == COMPOUND_EXPR);
2623 gimplify_stmt (expr_p);
2628 /* Gimplifies a statement list. These may be created either by an
2629 enlightened front-end, or by shortcut_cond_expr. */
2631 static enum gimplify_status
2632 gimplify_statement_list (tree *expr_p)
2634 tree_stmt_iterator i = tsi_start (*expr_p);
2636 while (!tsi_end_p (i))
2640 gimplify_stmt (tsi_stmt_ptr (i));
2643 if (TREE_CODE (t) == STATEMENT_LIST)
2645 tsi_link_before (&i, t, TSI_SAME_STMT);
2655 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2656 gimplify. After gimplification, EXPR_P will point to a new temporary
2657 that holds the original value of the SAVE_EXPR node.
2659 PRE_P points to the list where side effects that must happen before
2660 *EXPR_P should be stored. */
2662 static enum gimplify_status
2663 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2665 enum gimplify_status ret = GS_ALL_DONE;
2668 #if defined ENABLE_CHECKING
2669 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2673 val = TREE_OPERAND (*expr_p, 0);
2675 /* If the operand is already a GIMPLE temporary, just re-write the
2677 if (is_gimple_tmp_var (val))
2679 /* The operand may be a void-valued expression such as SAVE_EXPRs
2680 generated by the Java frontend for class initialization. It is
2681 being executed only for its side-effects. */
2682 else if (TREE_TYPE (val) == void_type_node)
2684 tree body = TREE_OPERAND (*expr_p, 0);
2685 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2686 append_to_statement_list (body, pre_p);
2687 *expr_p = build_empty_stmt ();
2690 *expr_p = TREE_OPERAND (*expr_p, 0)
2691 = get_initialized_tmp_var (val, pre_p, post_p);
2696 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2703 PRE_P points to the list where side effects that must happen before
2704 *EXPR_P should be stored.
2706 POST_P points to the list where side effects that must happen after
2707 *EXPR_P should be stored. */
2709 static enum gimplify_status
2710 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2712 tree expr = *expr_p;
2713 tree op0 = TREE_OPERAND (expr, 0);
2714 enum gimplify_status ret;
2716 switch (TREE_CODE (op0))
2719 /* Check if we are dealing with an expression of the form '&*ptr'.
2720 While the front end folds away '&*ptr' into 'ptr', these
2721 expressions may be generated internally by the compiler (e.g.,
2722 builtins like __builtin_va_end). */
2723 *expr_p = TREE_OPERAND (op0, 0);
2728 /* Fold &a[6] to (&a + 6). */
2729 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2732 /* This added an INDIRECT_REF. Fold it away. */
2733 op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2739 /* We use fb_either here because the C frontend sometimes takes
2740 the address of a call that returns a struct. */
2741 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2742 is_gimple_addr_expr_arg, fb_either);
2743 if (ret != GS_ERROR)
2745 /* At this point, the argument of the ADDR_EXPR should be
2746 sufficiently simple that there are never side effects. */
2747 /* ??? Could split out the decision code from build1 to verify. */
2748 TREE_SIDE_EFFECTS (expr) = 0;
2750 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2751 recompute_tree_invarant_for_addr_expr (expr);
2753 /* Mark the RHS addressable. */
2754 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2759 /* If the operand is gimplified into a _DECL, mark the address expression
2760 as TREE_INVARIANT. */
2761 if (DECL_P (TREE_OPERAND (expr, 0)))
2762 TREE_INVARIANT (expr) = 1;
2767 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2768 value; output operands should be a gimple lvalue. */
2770 static enum gimplify_status
2771 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2773 tree expr = *expr_p;
2774 int noutputs = list_length (ASM_OUTPUTS (expr));
2775 const char **oconstraints
2776 = (const char **) alloca ((noutputs) * sizeof (const char *));
2779 const char *constraint;
2780 bool allows_mem, allows_reg, is_inout;
2781 enum gimplify_status ret, tret;
2784 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2788 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2790 oconstraints[i] = constraint
2791 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2793 parse_output_constraint (&constraint, i, 0, 0,
2794 &allows_mem, &allows_reg, &is_inout);
2796 if (!allows_reg && allows_mem)
2797 lang_hooks.mark_addressable (TREE_VALUE (link));
2799 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2800 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
2801 fb_lvalue | fb_mayfail);
2802 if (tret == GS_ERROR)
2804 error ("invalid lvalue in asm output %d", i);
2810 /* An input/output operand. To give the optimizers more
2811 flexibility, split it into separate input and output
2815 size_t constraint_len = strlen (constraint);
2817 /* Turn the in/out constraint into an output constraint. */
2818 char *p = xstrdup (constraint);
2820 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
2823 /* And add a matching input constraint. */
2826 sprintf (buf, "%d", i);
2827 input = build_string (strlen (buf), buf);
2830 input = build_string (constraint_len - 1, constraint + 1);
2831 input = build_tree_list (build_tree_list (NULL_TREE, input),
2832 unshare_expr (TREE_VALUE (link)));
2833 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
2837 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2840 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2841 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
2842 oconstraints, &allows_mem, &allows_reg);
2844 /* If the operand is a memory input, it should be an lvalue. */
2845 if (!allows_reg && allows_mem)
2847 lang_hooks.mark_addressable (TREE_VALUE (link));
2848 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2849 is_gimple_lvalue, fb_lvalue | fb_mayfail);
2850 if (tret == GS_ERROR)
2852 error ("memory input %d is not directly addressable", i);
2858 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2859 is_gimple_val, fb_rvalue);
2860 if (tret == GS_ERROR)
2868 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
2869 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
2870 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
2871 return to this function.
2873 FIXME should we complexify the prequeue handling instead? Or use flags
2874 for all the cleanups and let the optimizer tighten them up? The current
2875 code seems pretty fragile; it will break on a cleanup within any
2876 non-conditional nesting. But any such nesting would be broken, anyway;
2877 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
2878 and continues out of it. We can do that at the RTL level, though, so
2879 having an optimizer to tighten up try/finally regions would be a Good
2882 static enum gimplify_status
2883 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
2885 tree_stmt_iterator iter;
2888 tree temp = voidify_wrapper_expr (*expr_p, NULL);
2890 /* We only care about the number of conditions between the innermost
2891 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
2892 int old_conds = gimplify_ctxp->conditions;
2893 gimplify_ctxp->conditions = 0;
2895 body = TREE_OPERAND (*expr_p, 0);
2896 gimplify_to_stmt_list (&body);
2898 gimplify_ctxp->conditions = old_conds;
2900 for (iter = tsi_start (body); !tsi_end_p (iter); )
2902 tree *wce_p = tsi_stmt_ptr (iter);
2905 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
2907 if (tsi_one_before_end_p (iter))
2909 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
2917 sl = tsi_split_statement_list_after (&iter);
2918 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
2919 append_to_statement_list (TREE_OPERAND (wce, 1),
2920 &TREE_OPERAND (tfe, 1));
2922 iter = tsi_start (sl);
2932 append_to_statement_list (body, pre_p);
2942 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
2943 is the cleanup action required. */
2946 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
2950 /* Errors can result in improperly nested cleanups. Which results in
2951 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
2952 if (errorcount || sorrycount)
2955 if (gimple_conditional_context ())
2957 /* If we're in a conditional context, this is more complex. We only
2958 want to run the cleanup if we actually ran the initialization that
2959 necessitates it, but we want to run it after the end of the
2960 conditional context. So we wrap the try/finally around the
2961 condition and use a flag to determine whether or not to actually
2962 run the destructor. Thus
2966 becomes (approximately)
2970 if (test) { A::A(temp); flag = 1; val = f(temp); }
2973 if (flag) A::~A(temp);
2978 tree flag = create_tmp_var (boolean_type_node, "cleanup");
2979 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
2980 boolean_false_node);
2981 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
2983 cleanup = build (COND_EXPR, void_type_node, flag, cleanup,
2984 build_empty_stmt ());
2985 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2986 cleanup, NULL_TREE);
2987 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
2988 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
2989 append_to_statement_list (ftrue, pre_p);
2991 /* Because of this manipulation, and the EH edges that jump
2992 threading cannot redirect, the temporary (VAR) will appear
2993 to be used uninitialized. Don't warn. */
2994 TREE_NO_WARNING (var) = 1;
2998 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
2999 cleanup, NULL_TREE);
3000 append_to_statement_list (wce, pre_p);
3003 gimplify_stmt (&TREE_OPERAND (wce, 1));
3006 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3008 static enum gimplify_status
3009 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3011 tree targ = *expr_p;
3012 tree temp = TARGET_EXPR_SLOT (targ);
3013 tree init = TARGET_EXPR_INITIAL (targ);
3014 enum gimplify_status ret;
3018 /* TARGET_EXPR temps aren't part of the enclosing block, so add it to the
3020 gimple_add_tmp_var (temp);
3022 /* Build up the initialization and add it to pre_p. Special handling
3023 for BIND_EXPR can result in fewer temporaries created. */
3024 if (TREE_CODE (init) == BIND_EXPR)
3025 gimplify_bind_expr (&init, temp, pre_p);
3028 if (! VOID_TYPE_P (TREE_TYPE (init)))
3029 init = build (MODIFY_EXPR, void_type_node, temp, init);
3030 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3031 if (ret == GS_ERROR)
3034 append_to_statement_list (init, pre_p);
3036 /* If needed, push the cleanup for the temp. */
3037 if (TARGET_EXPR_CLEANUP (targ))
3039 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3040 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3043 /* Only expand this once. */
3044 TREE_OPERAND (targ, 3) = init;
3045 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3047 else if (!temp->decl.seen_in_bind_expr)
3048 /* We should have expanded this before. */
3055 /* Gimplification of expression trees. */
3057 /* Gimplify an expression which appears at statement context; usually, this
3058 means replacing it with a suitably gimple STATEMENT_LIST. */
3061 gimplify_stmt (tree *stmt_p)
3063 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3065 *stmt_p = alloc_stmt_list ();
3068 /* Similarly, but force the result to be a STATEMENT_LIST. */
3071 gimplify_to_stmt_list (tree *stmt_p)
3073 gimplify_stmt (stmt_p);
3074 if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3077 *stmt_p = alloc_stmt_list ();
3078 append_to_statement_list (t, stmt_p);
3083 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3084 gimplification failed.
3086 PRE_P points to the list where side effects that must happen before
3087 EXPR should be stored.
3089 POST_P points to the list where side effects that must happen after
3090 EXPR should be stored, or NULL if there is no suitable list. In
3091 that case, we copy the result to a temporary, emit the
3092 post-effects, and then return the temporary.
3094 GIMPLE_TEST_F points to a function that takes a tree T and
3095 returns nonzero if T is in the GIMPLE form requested by the
3096 caller. The GIMPLE predicates are in tree-gimple.c.
3098 This test is used twice. Before gimplification, the test is
3099 invoked to determine whether *EXPR_P is already gimple enough. If
3100 that fails, *EXPR_P is gimplified according to its code and
3101 GIMPLE_TEST_F is called again. If the test still fails, then a new
3102 temporary variable is created and assigned the value of the
3103 gimplified expression.
3105 FALLBACK tells the function what sort of a temporary we want. If the 1
3106 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3107 If both are set, either is OK, but an lvalue is preferable.
3109 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3110 iterates until solution. */
3112 enum gimplify_status
3113 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3114 bool (* gimple_test_f) (tree), fallback_t fallback)
3117 tree internal_pre = NULL_TREE;
3118 tree internal_post = NULL_TREE;
3120 int is_statement = (pre_p == NULL);
3122 location_t saved_location;
3123 enum gimplify_status ret;
3125 save_expr = *expr_p;
3126 if (save_expr == NULL_TREE)
3129 /* We used to check the predicate here and return immediately if it
3130 succeeds. This is wrong; the design is for gimplification to be
3131 idempotent, and for the predicates to only test for valid forms, not
3132 whether they are fully simplified. */
3134 /* Set up our internal queues if needed. */
3136 pre_p = &internal_pre;
3138 post_p = &internal_post;
3140 saved_location = input_location;
3141 if (save_expr == error_mark_node)
3144 locus = EXPR_LOCUS (save_expr);
3146 input_location = *locus;
3148 /* Loop over the specific gimplifiers until the toplevel node
3149 remains the same. */
3152 /* Strip any uselessness. */
3153 STRIP_MAIN_TYPE_NOPS (*expr_p);
3155 /* Remember the expr. */
3156 save_expr = *expr_p;
3158 /* Die, die, die, my darling. */
3159 if (save_expr == error_mark_node
3160 || TREE_TYPE (save_expr) == error_mark_node)
3166 /* Do any language-specific gimplification. */
3167 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3170 if (*expr_p == NULL_TREE)
3172 if (*expr_p != save_expr)
3175 else if (ret != GS_UNHANDLED)
3179 switch (TREE_CODE (*expr_p))
3181 /* First deal with the special cases. */
3183 case POSTINCREMENT_EXPR:
3184 case POSTDECREMENT_EXPR:
3185 case PREINCREMENT_EXPR:
3186 case PREDECREMENT_EXPR:
3187 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3188 fallback != fb_none);
3192 ret = gimplify_array_ref (expr_p, pre_p, post_p,
3193 fallback & fb_lvalue);
3197 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3198 fallback & fb_lvalue);
3202 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3206 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3213 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3218 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3219 fallback & fb_lvalue);
3224 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3225 fallback != fb_none);
3228 case TRUTH_ANDIF_EXPR:
3229 case TRUTH_ORIF_EXPR:
3230 ret = gimplify_boolean_expr (expr_p);
3233 case TRUTH_NOT_EXPR:
3234 TREE_OPERAND (*expr_p, 0)
3235 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3236 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3237 is_gimple_val, fb_rvalue);
3238 recalculate_side_effects (*expr_p);
3242 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3246 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3251 if (IS_EMPTY_STMT (*expr_p))
3257 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3258 || fallback == fb_none)
3260 /* Just strip a conversion to void (or in void context) and
3262 *expr_p = TREE_OPERAND (*expr_p, 0);
3266 ret = gimplify_conversion (expr_p);
3267 if (ret == GS_ERROR)
3269 if (*expr_p != save_expr)
3273 case FIX_TRUNC_EXPR:
3275 case FIX_FLOOR_EXPR:
3276 case FIX_ROUND_EXPR:
3277 /* unary_expr: ... | '(' cast ')' val | ... */
3278 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3279 is_gimple_val, fb_rvalue);
3280 recalculate_side_effects (*expr_p);
3284 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3285 is_gimple_reg, fb_rvalue);
3286 recalculate_side_effects (*expr_p);
3289 /* Constants need not be gimplified. */
3299 *expr_p = DECL_INITIAL (*expr_p);
3303 /* FIXME make this a decl. */
3308 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3312 ret = gimplify_loop_expr (expr_p, pre_p);
3316 ret = gimplify_switch_expr (expr_p, pre_p);
3319 case LABELED_BLOCK_EXPR:
3320 ret = gimplify_labeled_block_expr (expr_p);
3323 case EXIT_BLOCK_EXPR:
3324 ret = gimplify_exit_block_expr (expr_p);
3328 ret = gimplify_exit_expr (expr_p);
3332 /* If the target is not LABEL, then it is a computed jump
3333 and the target needs to be gimplified. */
3334 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3335 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3336 NULL, is_gimple_val, fb_rvalue);
3341 #ifdef ENABLE_CHECKING
3342 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3347 case CASE_LABEL_EXPR:
3348 ret = gimplify_case_label_expr (expr_p);
3352 ret = gimplify_return_expr (*expr_p, pre_p);
3356 /* Don't reduce this in place; let gimplify_init_constructor work
3361 /* The following are special cases that are not handled by the
3362 original GIMPLE grammar. */
3364 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3367 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3372 enum gimplify_status r0, r1, r2;
3374 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3375 is_gimple_min_lval, fb_either);
3376 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3377 is_gimple_val, fb_rvalue);
3378 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3379 is_gimple_val, fb_rvalue);
3380 recalculate_side_effects (*expr_p);
3382 ret = MIN (r0, MIN (r1, r2));
3386 case NON_LVALUE_EXPR:
3387 /* This should have been stripped above. */
3392 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3395 case TRY_FINALLY_EXPR:
3396 case TRY_CATCH_EXPR:
3397 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3398 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3402 case CLEANUP_POINT_EXPR:
3403 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3407 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3411 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3415 case EH_FILTER_EXPR:
3416 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3421 /* This moves much of the actual computation out of the
3422 VTABLE_REF. Perhaps this should be revisited once we want to
3423 do clever things with VTABLE_REFs. */
3424 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3425 is_gimple_min_lval, fb_lvalue);
3430 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3434 /* We get here when taking the address of a label. We mark
3435 the label as "forced"; meaning it can never be removed and
3436 it is a potential target for any computed goto. */
3437 FORCED_LABEL (*expr_p) = 1;
3441 case STATEMENT_LIST:
3442 ret = gimplify_statement_list (expr_p);
3446 /* ??? If this is a local variable, and it has not been seen in any
3447 outer BIND_EXPR, then it's probably the result of a duplicate
3448 declaration, for which we've already issued an error. It would
3449 be really nice if the front end wouldn't leak these at all.
3450 Currently the only known culprit is C++ destructors, as seen
3451 in g++.old-deja/g++.jason/binding.C. */
3453 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3454 && decl_function_context (tmp) == current_function_decl
3455 && !tmp->decl.seen_in_bind_expr)
3457 #ifdef ENABLE_CHECKING
3458 if (!errorcount && !sorrycount)
3468 /* If *EXPR_P does not need to be special-cased, handle it
3469 according to its class. */
3470 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3471 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3472 post_p, is_gimple_val, fb_rvalue);
3473 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3474 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3475 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3476 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3477 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3479 enum gimplify_status r0, r1;
3481 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3482 post_p, is_gimple_val, fb_rvalue);
3483 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3484 post_p, is_gimple_val, fb_rvalue);
3488 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3489 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3495 /* Fail if we don't know how to handle this tree code. */
3498 recalculate_side_effects (*expr_p);
3502 /* If we replaced *expr_p, gimplify again. */
3503 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3506 while (ret == GS_OK);
3508 /* If we encountered an error_mark somewhere nested inside, either
3509 stub out the statement or propagate the error back out. */
3510 if (ret == GS_ERROR)
3513 *expr_p = build_empty_stmt ();
3517 #ifdef ENABLE_CHECKING
3518 /* This was only valid as a return value from the langhook, which
3519 we handled. Make sure it doesn't escape from any other context. */
3520 if (ret == GS_UNHANDLED)
3525 *expr_p = build_empty_stmt ();
3526 if (fallback == fb_none && !is_gimple_stmt (*expr_p))
3528 /* We aren't looking for a value, and we don't have a valid
3529 statement. If it doesn't have side-effects, throw it away. */
3530 if (!TREE_SIDE_EFFECTS (*expr_p))
3531 *expr_p = build_empty_stmt ();
3532 else if (!TREE_THIS_VOLATILE (*expr_p))
3533 /* We only handle volatiles here; anything else with side-effects
3534 must be converted to a valid statement before we get here. */
3536 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3538 /* Historically, the compiler has treated a bare
3539 reference to a volatile lvalue as forcing a load. */
3540 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3541 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3544 /* We can't do anything useful with a volatile reference to
3545 incomplete type, so just throw it away. */
3546 *expr_p = build_empty_stmt ();
3549 /* If we are gimplifying at the statement level, we're done. Tack
3550 everything together and replace the original statement with the
3552 if (fallback == fb_none || is_statement)
3554 if (internal_pre || internal_post)
3556 append_to_statement_list (*expr_p, &internal_pre);
3557 append_to_statement_list (internal_post, &internal_pre);
3558 annotate_all_with_locus (&internal_pre, input_location);
3559 *expr_p = internal_pre;
3564 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3567 /* If it's sufficiently simple already, we're done. Unless we are
3568 handling some post-effects internally; if that's the case, we need to
3569 copy into a temp before adding the post-effects to the tree. */
3570 if (!internal_post && (*gimple_test_f) (*expr_p))
3573 /* Otherwise, we need to create a new temporary for the gimplified
3576 /* We can't return an lvalue if we have an internal postqueue. The
3577 object the lvalue refers to would (probably) be modified by the
3578 postqueue; we need to copy the value out first, which means an
3580 if ((fallback & fb_lvalue) && !internal_post
3581 && is_gimple_addr_expr_arg (*expr_p))
3583 /* An lvalue will do. Take the address of the expression, store it
3584 in a temporary, and replace the expression with an INDIRECT_REF of
3586 tmp = build_fold_addr_expr (*expr_p);
3587 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3588 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3590 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3592 #if defined ENABLE_CHECKING
3593 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3597 /* An rvalue will do. Assign the gimplified expression into a new
3598 temporary TMP and replace the original expression with TMP. */
3600 if (internal_post || (fallback & fb_lvalue))
3601 /* The postqueue might change the value of the expression between
3602 the initialization and use of the temporary, so we can't use a
3603 formal temp. FIXME do we care? */
3604 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3606 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3608 else if (fallback & fb_mayfail)
3610 /* If this is an asm statement, and the user asked for the impossible,
3611 don't abort. Fail and let gimplify_asm_expr issue an error. */
3617 fprintf (stderr, "gimplification failed:\n");
3618 print_generic_expr (stderr, *expr_p, 0);
3619 debug_tree (*expr_p);
3623 #if defined ENABLE_CHECKING
3624 /* Make sure the temporary matches our predicate. */
3625 if (!(*gimple_test_f) (*expr_p))
3631 annotate_all_with_locus (&internal_post, input_location);
3632 append_to_statement_list (internal_post, pre_p);
3636 input_location = saved_location;
3640 #ifdef ENABLE_CHECKING
3641 /* Compare types A and B for a "close enough" match. */
3644 cpt_same_type (tree a, tree b)
3646 if (lang_hooks.types_compatible_p (a, b))
3649 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3650 link them together. This routine is intended to catch type errors
3651 that will affect the optimizers, and the optimizers don't add new
3652 dereferences of function pointers, so ignore it. */
3653 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3654 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3657 /* ??? The C FE pushes type qualifiers after the fact into the type of
3658 the element from the type of the array. See build_unary_op's handling
3659 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3660 should have done it when creating the variable in the first place.
3661 Alternately, why aren't the two array types made variants? */
3662 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3663 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3665 /* And because of those, we have to recurse down through pointers. */
3666 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3667 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3672 /* Check for some cases of the front end missing cast expressions.
3673 The type of a dereference should correspond to the pointer type;
3674 similarly the type of an address should match its object. */
3677 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3678 void *data ATTRIBUTE_UNUSED)
3681 tree ptype, otype, dtype;
3683 switch (TREE_CODE (t))
3687 otype = TREE_TYPE (t);
3688 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
3689 dtype = TREE_TYPE (ptype);
3690 if (!cpt_same_type (otype, dtype))
3695 ptype = TREE_TYPE (t);
3696 otype = TREE_TYPE (TREE_OPERAND (t, 0));
3697 dtype = TREE_TYPE (ptype);
3698 if (!cpt_same_type (otype, dtype))
3700 /* &array is allowed to produce a pointer to the element,
3701 rather than a pointer to the array type. */
3702 if (TREE_CODE (otype) == ARRAY_TYPE
3703 && POINTER_TYPE_P (ptype)
3704 && cpt_same_type (TREE_TYPE (otype), dtype))
3719 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
3720 function decl containing BODY. */
3723 gimplify_body (tree *body_p, tree fndecl)
3725 location_t saved_location = input_location;
3728 timevar_push (TV_TREE_GIMPLIFY);
3729 push_gimplify_context ();
3731 /* Unshare most shared trees in the body. */
3732 unshare_all_trees (*body_p);
3734 /* Make sure input_location isn't set to something wierd. */
3735 input_location = DECL_SOURCE_LOCATION (fndecl);
3737 /* Gimplify the function's body. */
3738 gimplify_stmt (body_p);
3741 /* Unshare again, in case gimplification was sloppy. */
3742 unshare_all_trees (body);
3744 /* If there isn't an outer BIND_EXPR, add one. */
3745 if (TREE_CODE (body) == STATEMENT_LIST)
3747 tree t = expr_only (*body_p);
3751 if (TREE_CODE (body) != BIND_EXPR)
3753 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
3754 NULL_TREE, NULL_TREE);
3755 TREE_SIDE_EFFECTS (b) = 1;
3756 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
3761 pop_gimplify_context (body);
3763 #ifdef ENABLE_CHECKING
3764 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
3767 timevar_pop (TV_TREE_GIMPLIFY);
3768 input_location = saved_location;
3771 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
3772 node for the function we want to gimplify. */
3775 gimplify_function_tree (tree fndecl)
3779 oldfn = current_function_decl;
3780 current_function_decl = fndecl;
3782 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
3784 /* If we're instrumenting function entry/exit, then prepend the call to
3785 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
3786 catch the exit hook. */
3787 /* ??? Add some way to ignore exceptions for this TFE. */
3788 if (flag_instrument_function_entry_exit
3789 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
3793 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
3794 TREE_SIDE_EFFECTS (tf) = 1;
3795 x = DECL_SAVED_TREE (fndecl);
3796 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
3797 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
3798 x = build_function_call_expr (x, NULL);
3799 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
3801 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3802 TREE_SIDE_EFFECTS (bind) = 1;
3803 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
3804 x = build_function_call_expr (x, NULL);
3805 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
3806 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
3808 DECL_SAVED_TREE (fndecl) = bind;
3811 current_function_decl = oldfn;
3814 #include "gt-gimplify.h"