1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
32 #include "tree-gimple.h"
33 #include "tree-inline.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "tree-flow.h"
50 static struct gimplify_ctx
52 tree current_bind_expr;
55 tree conditional_cleanups;
59 varray_type case_labels;
60 /* The formal temporary table. Should this be persistent? */
65 /* Formal (expression) temporary table handling: Multiple occurrences of
66 the same scalar expression are evaluated into the same temporary. */
68 typedef struct gimple_temp_hash_elt
71 tree temp; /* Value */
74 /* Forward declarations. */
75 static enum gimplify_status gimplify_modify_expr_rhs (tree *, tree *, tree *,
76 tree *, tree *, bool);
77 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
80 /* Return a hash value for a formal temporary table entry. */
83 gimple_tree_hash (const void *p)
85 tree t = ((const elt_t *) p)->val;
86 return iterative_hash_expr (t, 0);
89 /* Compare two formal temporary table entries. */
92 gimple_tree_eq (const void *p1, const void *p2)
94 tree t1 = ((const elt_t *) p1)->val;
95 tree t2 = ((const elt_t *) p2)->val;
96 enum tree_code code = TREE_CODE (t1);
98 if (TREE_CODE (t2) != code
99 || TREE_TYPE (t1) != TREE_TYPE (t2))
102 if (!operand_equal_p (t1, t2, 0))
105 /* Only allow them to compare equal if they also hash equal; otherwise
106 results are nondeterminate, and we fail bootstrap comparison. */
107 if (gimple_tree_hash (p1) != gimple_tree_hash (p2))
113 /* Set up a context for the gimplifier. */
116 push_gimplify_context (void)
121 = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
122 gimplify_ctxp->temp_htab
123 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
126 /* Tear down a context for the gimplifier. If BODY is non-null, then
127 put the temporaries into the outer BIND_EXPR. Otherwise, put them
128 in the unexpanded_var_list. */
131 pop_gimplify_context (tree body)
133 if (!gimplify_ctxp || gimplify_ctxp->current_bind_expr)
137 declare_tmp_vars (gimplify_ctxp->temps, body);
139 record_vars (gimplify_ctxp->temps);
143 fprintf (stderr, " collisions: %f ",
144 htab_collisions (gimplify_ctxp->temp_htab));
147 htab_delete (gimplify_ctxp->temp_htab);
148 free (gimplify_ctxp);
149 gimplify_ctxp = NULL;
153 gimple_push_bind_expr (tree bind)
155 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
156 gimplify_ctxp->current_bind_expr = bind;
160 gimple_pop_bind_expr (void)
162 gimplify_ctxp->current_bind_expr
163 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
167 gimple_current_bind_expr (void)
169 return gimplify_ctxp->current_bind_expr;
172 /* Returns true iff there is a COND_EXPR between us and the innermost
173 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
176 gimple_conditional_context (void)
178 return gimplify_ctxp->conditions > 0;
181 /* Note that we've entered a COND_EXPR. */
184 gimple_push_condition (void)
186 ++(gimplify_ctxp->conditions);
189 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
190 now, add any conditional cleanups we've seen to the prequeue. */
193 gimple_pop_condition (tree *pre_p)
195 int conds = --(gimplify_ctxp->conditions);
199 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
200 gimplify_ctxp->conditional_cleanups = NULL_TREE;
206 /* A subroutine of append_to_statement_list{,_force}. */
209 append_to_statement_list_1 (tree t, tree *list_p, bool side_effects)
212 tree_stmt_iterator i;
219 if (t && TREE_CODE (t) == STATEMENT_LIST)
224 *list_p = list = alloc_stmt_list ();
228 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
231 /* Add T to the end of the list container pointed by LIST_P.
232 If T is an expression with no effects, it is ignored. */
235 append_to_statement_list (tree t, tree *list_p)
237 append_to_statement_list_1 (t, list_p, t ? TREE_SIDE_EFFECTS (t) : false);
240 /* Similar, but the statement is always added, regardless of side effects. */
243 append_to_statement_list_force (tree t, tree *list_p)
245 append_to_statement_list_1 (t, list_p, t != NULL);
248 /* Both gimplify the statement T and append it to LIST_P. */
251 gimplify_and_add (tree t, tree *list_p)
254 append_to_statement_list (t, list_p);
257 /* Strip off a legitimate source ending from the input string NAME of
258 length LEN. Rather than having to know the names used by all of
259 our front ends, we strip off an ending of a period followed by
260 up to five characters. (Java uses ".class".) */
263 remove_suffix (char *name, int len)
267 for (i = 2; i < 8 && len > i; i++)
269 if (name[len - i] == '.')
271 name[len - i] = '\0';
277 /* Create a nameless artificial label and put it in the current function
278 context. Returns the newly created label. */
281 create_artificial_label (void)
283 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
285 DECL_ARTIFICIAL (lab) = 1;
286 DECL_CONTEXT (lab) = current_function_decl;
290 /* Create a new temporary name with PREFIX. Returns an identifier. */
292 static GTY(()) unsigned int tmp_var_id_num;
295 create_tmp_var_name (const char *prefix)
301 char *preftmp = ASTRDUP (prefix);
303 remove_suffix (preftmp, strlen (preftmp));
307 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
308 return get_identifier (tmp_name);
312 /* Create a new temporary variable declaration of type TYPE.
313 Does NOT push it into the current binding. */
316 create_tmp_var_raw (tree type, const char *prefix)
321 /* Make the type of the variable writable. */
322 new_type = build_type_variant (type, 0, 0);
323 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
325 tmp_var = build_decl (VAR_DECL, create_tmp_var_name (prefix), type);
327 /* The variable was declared by the compiler. */
328 DECL_ARTIFICIAL (tmp_var) = 1;
329 /* And we don't want debug info for it. */
330 DECL_IGNORED_P (tmp_var) = 1;
332 /* Make the variable writable. */
333 TREE_READONLY (tmp_var) = 0;
335 DECL_EXTERNAL (tmp_var) = 0;
336 TREE_STATIC (tmp_var) = 0;
337 TREE_USED (tmp_var) = 1;
342 /* Create a new temporary variable declaration of type TYPE. DOES push the
343 variable into the current binding. Further, assume that this is called
344 only from gimplification or optimization, at which point the creation of
345 certain types are bugs. */
348 create_tmp_var (tree type, const char *prefix)
352 #if defined ENABLE_CHECKING
353 /* We don't allow types that are addressable (meaning we can't make copies),
354 incomplete, or of variable size. */
355 if (TREE_ADDRESSABLE (type)
356 || !COMPLETE_TYPE_P (type)
357 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
361 tmp_var = create_tmp_var_raw (type, prefix);
362 gimple_add_tmp_var (tmp_var);
366 /* Given a tree, try to return a useful variable name that we can use
367 to prefix a temporary that is being assigned the value of the tree.
368 I.E. given <temp> = &A, return A. */
376 STRIP_NOPS (stripped_decl);
377 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
378 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
381 switch (TREE_CODE (stripped_decl))
384 return get_name (TREE_OPERAND (stripped_decl, 0));
392 /* Create a temporary with a name derived from VAL. Subroutine of
393 lookup_tmp_var; nobody else should call this function. */
396 create_tmp_from_val (tree val)
398 return create_tmp_var (TREE_TYPE (val), get_name (val));
401 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
402 an existing expression temporary. */
405 lookup_tmp_var (tree val, bool is_formal)
407 if (!is_formal || TREE_SIDE_EFFECTS (val))
408 return create_tmp_from_val (val);
415 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
418 elt_p = xmalloc (sizeof (*elt_p));
420 elt_p->temp = create_tmp_from_val (val);
421 *slot = (void *)elt_p;
424 elt_p = (elt_t *) *slot;
430 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
431 in gimplify_expr. Only use this function if:
433 1) The value of the unfactored expression represented by VAL will not
434 change between the initialization and use of the temporary, and
435 2) The temporary will not be otherwise modified.
437 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
438 and #2 means it is inappropriate for && temps.
440 For other cases, use get_initialized_tmp_var instead. */
443 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
448 gimplify_expr (&val, pre_p, post_p, is_gimple_rhs, fb_rvalue);
450 t = lookup_tmp_var (val, is_formal);
452 mod = build (MODIFY_EXPR, TREE_TYPE (t), t, val);
454 class = TREE_CODE_CLASS (TREE_CODE (val));
455 if (EXPR_LOCUS (val))
456 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
458 annotate_with_locus (mod, input_location);
460 /* gimplify_modify_expr might want to reduce this further. */
461 gimplify_and_add (mod, pre_p);
466 get_formal_tmp_var (tree val, tree *pre_p)
468 return internal_get_tmp_var (val, pre_p, NULL, true);
471 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
472 are as in gimplify_expr. */
475 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
477 return internal_get_tmp_var (val, pre_p, post_p, false);
480 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
483 is_gimple_tmp_var (tree t)
485 /* FIXME this could trigger for other local artificials, too. */
486 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
487 && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
490 /* Declares all the variables in VARS in SCOPE. */
493 declare_tmp_vars (tree vars, tree scope)
500 /* C99 mode puts the default 'return 0;' for main outside the outer
501 braces. So drill down until we find an actual scope. */
502 while (TREE_CODE (scope) == COMPOUND_EXPR)
503 scope = TREE_OPERAND (scope, 0);
505 if (TREE_CODE (scope) != BIND_EXPR)
508 temps = nreverse (last);
509 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
510 BIND_EXPR_VARS (scope) = temps;
512 /* We don't add the temps to the block for this BIND_EXPR, as we're
513 not interested in debugging info for them. */
518 gimple_add_tmp_var (tree tmp)
520 if (TREE_CHAIN (tmp) || tmp->decl.seen_in_bind_expr)
523 DECL_CONTEXT (tmp) = current_function_decl;
524 tmp->decl.seen_in_bind_expr = 1;
528 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
529 gimplify_ctxp->temps = tmp;
534 declare_tmp_vars (tmp, DECL_SAVED_TREE (current_function_decl));
537 /* Determines whether to assign a locus to the statement STMT. */
540 should_carry_locus_p (tree stmt)
542 /* Don't emit a line note for a label. We particularly don't want to
543 emit one for the break label, since it doesn't actually correspond
544 to the beginning of the loop/switch. */
545 if (TREE_CODE (stmt) == LABEL_EXPR)
548 /* Do not annotate empty statements, since it confuses gcov. */
549 if (!TREE_SIDE_EFFECTS (stmt))
556 annotate_one_with_locus (tree t, location_t locus)
558 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t)))
559 && ! EXPR_HAS_LOCATION (t)
560 && should_carry_locus_p (t))
561 annotate_with_locus (t, locus);
565 annotate_all_with_locus (tree *stmt_p, location_t locus)
567 tree_stmt_iterator i;
572 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
574 tree t = tsi_stmt (i);
576 #ifdef ENABLE_CHECKING
577 /* Assuming we've already been gimplified, we shouldn't
578 see nested chaining constructs anymore. */
579 if (TREE_CODE (t) == STATEMENT_LIST
580 || TREE_CODE (t) == COMPOUND_EXPR)
584 annotate_one_with_locus (t, locus);
588 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
589 These nodes model computations that should only be done once. If we
590 were to unshare something like SAVE_EXPR(i++), the gimplification
591 process would create wrong code. */
594 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
596 enum tree_code code = TREE_CODE (*tp);
597 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
598 if (TREE_CODE_CLASS (code) == 't'
599 || TREE_CODE_CLASS (code) == 'd'
600 || TREE_CODE_CLASS (code) == 'c'
601 || code == SAVE_EXPR || code == TARGET_EXPR
602 /* We can't do anything sensible with a BLOCK used as an expression,
603 but we also can't abort when we see it because of non-expression
604 uses. So just avert our eyes and cross our fingers. Silly Java. */
607 else if (code == BIND_EXPR)
610 copy_tree_r (tp, walk_subtrees, data);
615 /* Mark all the _DECL nodes under *TP as volatile. FIXME: This must die
616 after VA_ARG_EXPRs are properly lowered. */
619 mark_decls_volatile_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
620 void *data ATTRIBUTE_UNUSED)
623 TREE_THIS_VOLATILE (*tp) = 1;
629 /* Callback for walk_tree to unshare most of the shared trees rooted at
630 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
631 then *TP is deep copied by calling copy_tree_r.
633 This unshares the same trees as copy_tree_r with the exception of
634 SAVE_EXPR nodes. These nodes model computations that should only be
635 done once. If we were to unshare something like SAVE_EXPR(i++), the
636 gimplification process would create wrong code. */
639 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
640 void *data ATTRIBUTE_UNUSED)
643 enum tree_code code = TREE_CODE (t);
645 /* Skip types, decls, and constants. But we do want to look at their
646 types and the bounds of types. Mark them as visited so we properly
647 unmark their subtrees on the unmark pass. If we've already seen them,
648 don't look down further. */
649 if (TREE_CODE_CLASS (code) == 't'
650 || TREE_CODE_CLASS (code) == 'd'
651 || TREE_CODE_CLASS (code) == 'c')
653 if (TREE_VISITED (t))
656 TREE_VISITED (t) = 1;
659 /* Special-case BIND_EXPR. We should never be copying these, therefore
660 we can omit examining BIND_EXPR_VARS. Which also avoids problems with
661 double processing of the DECL_INITIAL, which could be seen via both
662 the BIND_EXPR_VARS and a DECL_STMT. */
663 else if (code == BIND_EXPR)
665 if (TREE_VISITED (t))
667 TREE_VISITED (t) = 1;
669 walk_tree (&BIND_EXPR_BODY (t), copy_if_shared_r, NULL, NULL);
672 /* If this node has been visited already, unshare it and don't look
674 else if (TREE_VISITED (t))
676 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
680 /* Otherwise, mark the tree as visited and keep looking. */
683 TREE_VISITED (t) = 1;
684 if (TREE_CODE (*tp) == VA_ARG_EXPR
685 && targetm.calls.gimplify_va_arg_expr == NULL)
687 /* Mark any _DECL inside the operand as volatile to avoid
688 the optimizers messing around with it. We have to do this
689 early, otherwise we might mark a variable as volatile
690 after we gimplify other statements that use the variable
691 assuming it's not volatile. */
693 /* FIXME once most targets define the above hook, this should
694 go away (perhaps along with the #include "target.h"). */
695 walk_tree (&TREE_OPERAND (*tp, 0), mark_decls_volatile_r,
704 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
705 void *data ATTRIBUTE_UNUSED)
707 if (TREE_VISITED (*tp))
708 TREE_VISITED (*tp) = 0;
715 /* Unshare all the trees in BODY_P, a pointer to the body of FNDECL, and the
716 bodies of any nested functions. */
719 unshare_body (tree *body_p, tree fndecl)
721 struct cgraph_node *cgn = cgraph_node (fndecl);
723 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
724 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
725 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
728 /* Likewise, but mark all trees as not visited. */
731 unvisit_body (tree *body_p, tree fndecl)
733 struct cgraph_node *cgn = cgraph_node (fndecl);
735 walk_tree (body_p, unmark_visited_r, NULL, NULL);
736 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
737 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
740 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
743 unshare_all_trees (tree t)
745 walk_tree (&t, copy_if_shared_r, NULL, NULL);
746 walk_tree (&t, unmark_visited_r, NULL, NULL);
749 /* Unconditionally make an unshared copy of EXPR. This is used when using
750 stored expressions which span multiple functions, such as BINFO_VTABLE,
751 as the normal unsharing process can't tell that they're shared. */
754 unshare_expr (tree expr)
756 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
760 /* A terser interface for building a representation of a exception
764 gimple_build_eh_filter (tree body, tree allowed, tree failure)
768 /* FIXME should the allowed types go in TREE_TYPE? */
769 t = build (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
770 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
772 t = build (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
773 append_to_statement_list (body, &TREE_OPERAND (t, 0));
779 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
780 contain statements and have a value. Assign its value to a temporary
781 and give it void_type_node. Returns the temporary, or NULL_TREE if
782 WRAPPER was already void. */
785 voidify_wrapper_expr (tree wrapper, tree temp)
787 if (!VOID_TYPE_P (TREE_TYPE (wrapper)))
789 tree *p, sub = wrapper;
792 /* Set p to point to the body of the wrapper. */
793 switch (TREE_CODE (sub))
796 /* For a BIND_EXPR, the body is operand 1. */
797 p = &BIND_EXPR_BODY (sub);
801 p = &TREE_OPERAND (sub, 0);
805 /* Advance to the last statement. Set all container types to void. */
806 if (TREE_CODE (*p) == STATEMENT_LIST)
808 tree_stmt_iterator i = tsi_last (*p);
809 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
813 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
815 TREE_SIDE_EFFECTS (*p) = 1;
816 TREE_TYPE (*p) = void_type_node;
820 if (p == NULL || IS_EMPTY_STMT (*p))
822 /* Look through exception handling. */
823 else if (TREE_CODE (*p) == TRY_FINALLY_EXPR
824 || TREE_CODE (*p) == TRY_CATCH_EXPR)
829 /* The C++ frontend already did this for us. */
830 else if (TREE_CODE (*p) == INIT_EXPR
831 || TREE_CODE (*p) == TARGET_EXPR)
832 temp = TREE_OPERAND (*p, 0);
833 /* If we're returning a dereference, move the dereference
834 outside the wrapper. */
835 else if (TREE_CODE (*p) == INDIRECT_REF)
837 tree ptr = TREE_OPERAND (*p, 0);
838 temp = create_tmp_var (TREE_TYPE (ptr), "retval");
839 *p = build (MODIFY_EXPR, TREE_TYPE (ptr), temp, ptr);
840 temp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (temp)), temp);
841 /* If this is a BIND_EXPR for a const inline function, it might not
842 have TREE_SIDE_EFFECTS set. That is no longer accurate. */
843 TREE_SIDE_EFFECTS (wrapper) = 1;
848 temp = create_tmp_var (TREE_TYPE (wrapper), "retval");
849 *p = build (MODIFY_EXPR, TREE_TYPE (temp), temp, *p);
850 TREE_SIDE_EFFECTS (wrapper) = 1;
853 TREE_TYPE (wrapper) = void_type_node;
860 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
861 a temporary through which they communicate. */
864 build_stack_save_restore (tree *save, tree *restore)
866 tree save_call, tmp_var;
869 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
871 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
873 *save = build (MODIFY_EXPR, ptr_type_node, tmp_var, save_call);
875 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
876 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
879 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
881 static enum gimplify_status
882 gimplify_bind_expr (tree *expr_p, tree temp, tree *pre_p)
884 tree bind_expr = *expr_p;
885 bool old_save_stack = gimplify_ctxp->save_stack;
888 temp = voidify_wrapper_expr (bind_expr, temp);
890 /* Mark variables seen in this bind expr. */
891 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
892 t->decl.seen_in_bind_expr = 1;
894 gimple_push_bind_expr (bind_expr);
895 gimplify_ctxp->save_stack = false;
897 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
899 if (gimplify_ctxp->save_stack)
901 tree stack_save, stack_restore;
903 /* Save stack on entry and restore it on exit. Add a try_finally
904 block to achieve this. Note that mudflap depends on the
905 format of the emitted code: see mx_register_decls(). */
906 build_stack_save_restore (&stack_save, &stack_restore);
908 t = build (TRY_FINALLY_EXPR, void_type_node,
909 BIND_EXPR_BODY (bind_expr), NULL_TREE);
910 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
912 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
913 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
914 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
917 gimplify_ctxp->save_stack = old_save_stack;
918 gimple_pop_bind_expr ();
923 append_to_statement_list (bind_expr, pre_p);
930 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
931 GIMPLE value, it is assigned to a new temporary and the statement is
932 re-written to return the temporary.
934 PRE_P points to the list where side effects that must happen before
935 STMT should be stored. */
937 static enum gimplify_status
938 gimplify_return_expr (tree stmt, tree *pre_p)
940 tree ret_expr = TREE_OPERAND (stmt, 0);
941 tree result_decl, result;
943 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL)
946 if (ret_expr == error_mark_node)
949 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
950 result_decl = NULL_TREE;
953 result_decl = TREE_OPERAND (ret_expr, 0);
954 #ifdef ENABLE_CHECKING
955 if ((TREE_CODE (ret_expr) != MODIFY_EXPR
956 && TREE_CODE (ret_expr) != INIT_EXPR)
957 || TREE_CODE (result_decl) != RESULT_DECL)
962 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
963 Recall that aggregate_value_p is FALSE for any aggregate type that is
964 returned in registers. If we're returning values in registers, then
965 we don't want to extend the lifetime of the RESULT_DECL, particularly
966 across another call. In addition, for those aggregates for which
967 hard_function_value generates a PARALLEL, we'll abort during normal
968 expansion of structure assignments; there's special code in expand_return
969 to handle this case that does not exist in expand_expr. */
971 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
972 result = result_decl;
973 else if (gimplify_ctxp->return_temp)
974 result = gimplify_ctxp->return_temp;
977 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
979 /* ??? With complex control flow (usually involving abnormal edges),
980 we can wind up warning about an uninitialized value for this. Due
981 to how this variable is constructed and initialized, this is never
982 true. Give up and never warn. */
983 TREE_NO_WARNING (result) = 1;
985 gimplify_ctxp->return_temp = result;
988 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
989 Then gimplify the whole thing. */
990 if (result != result_decl)
991 TREE_OPERAND (ret_expr, 0) = result;
993 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
995 /* If we didn't use a temporary, then the result is just the result_decl.
996 Otherwise we need a simple copy. This should already be gimple. */
997 if (result == result_decl)
1000 ret_expr = build (MODIFY_EXPR, TREE_TYPE (result), result_decl, result);
1001 TREE_OPERAND (stmt, 0) = ret_expr;
1006 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1007 and replacing the LOOP_EXPR with goto, but if the loop contains an
1008 EXIT_EXPR, we need to append a label for it to jump to. */
1010 static enum gimplify_status
1011 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1013 tree saved_label = gimplify_ctxp->exit_label;
1014 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1015 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1017 append_to_statement_list (start_label, pre_p);
1019 gimplify_ctxp->exit_label = NULL_TREE;
1021 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1023 if (gimplify_ctxp->exit_label)
1025 append_to_statement_list (jump_stmt, pre_p);
1026 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1029 *expr_p = jump_stmt;
1031 gimplify_ctxp->exit_label = saved_label;
1036 /* Compare two case labels. Because the front end should already have
1037 made sure that case ranges do not overlap, it is enough to only compare
1038 the CASE_LOW values of each case label. */
1041 compare_case_labels (const void *p1, const void *p2)
1043 tree case1 = *(tree *)p1;
1044 tree case2 = *(tree *)p2;
1046 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1049 /* Sort the case labels in LABEL_VEC in ascending order. */
1052 sort_case_labels (tree label_vec)
1054 size_t len = TREE_VEC_LENGTH (label_vec);
1055 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1057 if (CASE_LOW (default_case))
1061 /* The last label in the vector should be the default case
1063 for (i = 0; i < len; ++i)
1065 tree t = TREE_VEC_ELT (label_vec, i);
1069 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1070 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1076 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1077 compare_case_labels);
1080 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1083 static enum gimplify_status
1084 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1086 tree switch_expr = *expr_p;
1087 enum gimplify_status ret;
1089 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1090 is_gimple_val, fb_rvalue);
1092 if (SWITCH_BODY (switch_expr))
1094 varray_type labels, saved_labels;
1095 tree label_vec, default_case = NULL_TREE;
1098 /* If someone can be bothered to fill in the labels, they can
1099 be bothered to null out the body too. */
1100 if (SWITCH_LABELS (switch_expr))
1103 saved_labels = gimplify_ctxp->case_labels;
1104 VARRAY_TREE_INIT (gimplify_ctxp->case_labels, 8, "case_labels");
1106 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1108 labels = gimplify_ctxp->case_labels;
1109 gimplify_ctxp->case_labels = saved_labels;
1111 len = VARRAY_ACTIVE_SIZE (labels);
1113 for (i = 0; i < len; ++i)
1115 tree t = VARRAY_TREE (labels, i);
1118 /* The default case must be the last label in the list. */
1120 VARRAY_TREE (labels, i) = VARRAY_TREE (labels, len - 1);
1126 label_vec = make_tree_vec (len + 1);
1127 SWITCH_LABELS (*expr_p) = label_vec;
1128 append_to_statement_list (switch_expr, pre_p);
1132 /* If the switch has no default label, add one, so that we jump
1133 around the switch body. */
1134 default_case = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1135 NULL_TREE, create_artificial_label ());
1136 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1137 *expr_p = build (LABEL_EXPR, void_type_node,
1138 CASE_LABEL (default_case));
1141 *expr_p = SWITCH_BODY (switch_expr);
1143 for (i = 0; i < len; ++i)
1144 TREE_VEC_ELT (label_vec, i) = VARRAY_TREE (labels, i);
1145 TREE_VEC_ELT (label_vec, len) = default_case;
1147 sort_case_labels (label_vec);
1149 SWITCH_BODY (switch_expr) = NULL;
1151 else if (!SWITCH_LABELS (switch_expr))
1157 static enum gimplify_status
1158 gimplify_case_label_expr (tree *expr_p)
1160 tree expr = *expr_p;
1161 if (gimplify_ctxp->case_labels)
1162 VARRAY_PUSH_TREE (gimplify_ctxp->case_labels, expr);
1165 *expr_p = build (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1169 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
1170 a (possibly empty) body. */
1172 static enum gimplify_status
1173 gimplify_labeled_block_expr (tree *expr_p)
1175 tree body = LABELED_BLOCK_BODY (*expr_p);
1176 tree label = LABELED_BLOCK_LABEL (*expr_p);
1179 DECL_CONTEXT (label) = current_function_decl;
1180 t = build (LABEL_EXPR, void_type_node, label);
1181 if (body != NULL_TREE)
1182 t = build (COMPOUND_EXPR, void_type_node, body, t);
1188 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
1190 static enum gimplify_status
1191 gimplify_exit_block_expr (tree *expr_p)
1193 tree labeled_block = TREE_OPERAND (*expr_p, 0);
1196 /* First operand must be a LABELED_BLOCK_EXPR, which should
1197 already be lowered (or partially lowered) when we get here. */
1198 #if defined ENABLE_CHECKING
1199 if (TREE_CODE (labeled_block) != LABELED_BLOCK_EXPR)
1203 label = LABELED_BLOCK_LABEL (labeled_block);
1204 *expr_p = build1 (GOTO_EXPR, void_type_node, label);
1209 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1213 build_and_jump (tree *label_p)
1215 if (label_p == NULL)
1216 /* If there's nowhere to jump, just fall through. */
1219 if (*label_p == NULL_TREE)
1221 tree label = create_artificial_label ();
1225 return build1 (GOTO_EXPR, void_type_node, *label_p);
1228 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1229 This also involves building a label to jump to and communicating it to
1230 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1232 static enum gimplify_status
1233 gimplify_exit_expr (tree *expr_p)
1235 tree cond = TREE_OPERAND (*expr_p, 0);
1238 expr = build_and_jump (&gimplify_ctxp->exit_label);
1239 expr = build (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1245 /* A helper function to be called via walk_tree. Mark all labels under *TP
1246 as being forced. To be called for DECL_INITIAL of static variables. */
1249 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1253 if (TREE_CODE (*tp) == LABEL_DECL)
1254 FORCED_LABEL (*tp) = 1;
1259 /* Break out elements of a constructor used as an initializer into separate
1262 Note that we still need to clear any elements that don't have explicit
1263 initializers, so if not all elements are initialized we keep the
1264 original MODIFY_EXPR, we just remove all of the constructor elements. */
1266 static enum gimplify_status
1267 gimplify_init_constructor (tree *expr_p, tree *pre_p,
1268 tree *post_p, bool want_value)
1270 tree object = TREE_OPERAND (*expr_p, 0);
1271 tree ctor = TREE_OPERAND (*expr_p, 1);
1272 tree type = TREE_TYPE (ctor);
1273 enum gimplify_status ret;
1276 if (TREE_CODE (ctor) != CONSTRUCTOR)
1277 return GS_UNHANDLED;
1279 elt_list = CONSTRUCTOR_ELTS (ctor);
1282 switch (TREE_CODE (type))
1286 case QUAL_UNION_TYPE:
1289 HOST_WIDE_INT i, num_elements, num_nonzero_elements;
1290 HOST_WIDE_INT num_nonconstant_elements;
1293 /* Aggregate types must lower constructors to initialization of
1294 individual elements. The exception is that a CONSTRUCTOR node
1295 with no elements indicates zero-initialization of the whole. */
1296 if (elt_list == NULL)
1304 return GS_UNHANDLED;
1307 categorize_ctor_elements (ctor, &num_nonzero_elements,
1308 &num_nonconstant_elements);
1309 num_elements = count_type_elements (TREE_TYPE (ctor));
1311 /* If a const aggregate variable is being initialized, then it
1312 should never be a lose to promote the variable to be static. */
1313 if (num_nonconstant_elements == 0
1314 && TREE_READONLY (object)
1315 && TREE_CODE (object) == VAR_DECL)
1317 DECL_INITIAL (object) = ctor;
1318 TREE_STATIC (object) = 1;
1319 if (!DECL_NAME (object))
1320 DECL_NAME (object) = create_tmp_var_name ("C");
1321 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
1323 /* ??? C++ doesn't automatically append a .<number> to the
1324 assembler name, and even when it does, it looks a FE private
1325 data structures to figure out what that number should be,
1326 which are not set for this variable. I suppose this is
1327 important for local statics for inline functions, which aren't
1328 "local" in the object file sense. So in order to get a unique
1329 TU-local symbol, we must invoke the lhd version now. */
1330 lhd_set_decl_assembler_name (object);
1332 *expr_p = NULL_TREE;
1336 /* If there are "lots" of initialized elements, and all of them
1337 are valid address constants, then the entire initializer can
1338 be dropped to memory, and then memcpy'd out. */
1339 if (num_nonconstant_elements == 0)
1341 HOST_WIDE_INT size = int_size_in_bytes (type);
1344 /* ??? We can still get unbounded array types, at least
1345 from the C++ front end. This seems wrong, but attempt
1346 to work around it for now. */
1349 size = int_size_in_bytes (TREE_TYPE (object));
1351 TREE_TYPE (ctor) = type = TREE_TYPE (object);
1354 /* Find the maximum alignment we can assume for the object. */
1355 /* ??? Make use of DECL_OFFSET_ALIGN. */
1356 if (DECL_P (object))
1357 align = DECL_ALIGN (object);
1359 align = TYPE_ALIGN (type);
1361 if (size > 0 && !can_move_by_pieces (size, align))
1363 tree new = create_tmp_var_raw (type, "C");
1364 gimple_add_tmp_var (new);
1365 TREE_STATIC (new) = 1;
1366 TREE_READONLY (new) = 1;
1367 DECL_INITIAL (new) = ctor;
1368 if (align > DECL_ALIGN (new))
1370 DECL_ALIGN (new) = align;
1371 DECL_USER_ALIGN (new) = 1;
1373 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
1375 TREE_OPERAND (*expr_p, 1) = new;
1380 /* If there are "lots" of initialized elements, even discounting
1381 those that are not address constants (and thus *must* be
1382 computed at runtime), then partition the constructor into
1383 constant and non-constant parts. Block copy the constant
1384 parts in, then generate code for the non-constant parts. */
1385 /* TODO. There's code in cp/typeck.c to do this. */
1387 /* If there are "lots" of zeros, then block clear the object first. */
1389 if (num_elements - num_nonzero_elements > CLEAR_RATIO
1390 && num_nonzero_elements < num_elements/4)
1393 /* ??? This bit ought not be needed. For any element not present
1394 in the initializer, we should simply set them to zero. Except
1395 we'd need to *find* the elements that are not present, and that
1396 requires trickery to avoid quadratic compile-time behavior in
1397 large cases or excessive memory use in small cases. */
1400 HOST_WIDE_INT len = list_length (elt_list);
1401 if (TREE_CODE (type) == ARRAY_TYPE)
1403 tree nelts = array_type_nelts (type);
1404 if (!host_integerp (nelts, 1)
1405 || tree_low_cst (nelts, 1) != len)
1408 else if (len != fields_length (type))
1414 CONSTRUCTOR_ELTS (ctor) = NULL_TREE;
1415 append_to_statement_list (*expr_p, pre_p);
1418 for (i = 0; elt_list; i++, elt_list = TREE_CHAIN (elt_list))
1420 tree purpose, value, cref, init;
1422 purpose = TREE_PURPOSE (elt_list);
1423 value = TREE_VALUE (elt_list);
1425 if (cleared && initializer_zerop (value))
1428 if (TREE_CODE (type) == ARRAY_TYPE)
1430 tree t = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
1432 /* ??? Here's to hoping the front end fills in all of the
1433 indicies, so we don't have to figure out what's missing
1437 /* ??? Need to handle this. */
1438 if (TREE_CODE (purpose) == RANGE_EXPR)
1441 cref = build (ARRAY_REF, t, object, purpose, NULL_TREE, NULL_TREE);
1444 cref = build (COMPONENT_REF, TREE_TYPE (purpose), object,
1445 purpose, NULL_TREE);
1447 init = build (MODIFY_EXPR, TREE_TYPE (purpose), cref, value);
1449 /* Each member initialization is a full-expression. */
1450 gimplify_and_add (init, pre_p);
1453 *expr_p = NULL_TREE;
1461 /* Extract the real and imaginary parts out of the ctor. */
1465 r = TREE_VALUE (elt_list);
1466 elt_list = TREE_CHAIN (elt_list);
1469 i = TREE_VALUE (elt_list);
1470 if (TREE_CHAIN (elt_list))
1474 if (r == NULL || i == NULL)
1476 tree zero = convert (TREE_TYPE (type), integer_zero_node);
1483 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
1484 represent creation of a complex value. */
1485 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
1487 ctor = build_complex (type, r, i);
1488 TREE_OPERAND (*expr_p, 1) = ctor;
1492 ctor = build (COMPLEX_EXPR, type, r, i);
1493 TREE_OPERAND (*expr_p, 1) = ctor;
1494 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
1495 is_gimple_rhs, fb_rvalue);
1501 /* Go ahead and simplify constant constructors to VECTOR_CST. */
1502 if (TREE_CONSTANT (ctor))
1503 TREE_OPERAND (*expr_p, 1) = build_vector (type, elt_list);
1506 /* Vector types use CONSTRUCTOR all the way through gimple
1507 compilation as a general initializer. */
1508 for (; elt_list; elt_list = TREE_CHAIN (elt_list))
1510 enum gimplify_status tret;
1511 tret = gimplify_expr (&TREE_VALUE (elt_list), pre_p, post_p,
1512 is_gimple_constructor_elt, fb_rvalue);
1513 if (tret == GS_ERROR)
1520 /* So how did we get a CONSTRUCTOR for a scalar type? */
1524 if (ret == GS_ERROR)
1526 else if (want_value)
1528 append_to_statement_list (*expr_p, pre_p);
1536 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1537 different from its canonical type, wrap the whole thing inside a
1538 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1541 The canonical type of a COMPONENT_REF is the type of the field being
1542 referenced--unless the field is a bit-field which can be read directly
1543 in a smaller mode, in which case the canonical type is the
1544 sign-appropriate type corresponding to that mode. */
1547 canonicalize_component_ref (tree *expr_p)
1549 tree expr = *expr_p;
1552 if (TREE_CODE (expr) != COMPONENT_REF)
1555 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1556 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1558 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1560 if (TREE_TYPE (expr) != type)
1562 tree old_type = TREE_TYPE (expr);
1564 /* Set the type of the COMPONENT_REF to the underlying type. */
1565 TREE_TYPE (expr) = type;
1567 /* And wrap the whole thing inside a NOP_EXPR. */
1568 expr = build1 (NOP_EXPR, old_type, expr);
1574 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1575 to foo, embed that change in the ADDR_EXPR by converting
1580 where L is the lower bound. For simplicity, only do this for constant
1584 canonicalize_addr_expr (tree *expr_p)
1586 tree expr = *expr_p;
1587 tree ctype = TREE_TYPE (expr);
1588 tree addr_expr = TREE_OPERAND (expr, 0);
1589 tree atype = TREE_TYPE (addr_expr);
1590 tree dctype, datype, ddatype, otype, obj_expr;
1592 /* Both cast and addr_expr types should be pointers. */
1593 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1596 /* The addr_expr type should be a pointer to an array. */
1597 datype = TREE_TYPE (atype);
1598 if (TREE_CODE (datype) != ARRAY_TYPE)
1601 /* Both cast and addr_expr types should address the same object type. */
1602 dctype = TREE_TYPE (ctype);
1603 ddatype = TREE_TYPE (datype);
1604 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1607 /* The addr_expr and the object type should match. */
1608 obj_expr = TREE_OPERAND (addr_expr, 0);
1609 otype = TREE_TYPE (obj_expr);
1610 if (!lang_hooks.types_compatible_p (otype, datype))
1613 /* The lower bound and element sizes must be constant. */
1614 if (TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1615 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1616 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1619 /* All checks succeeded. Build a new node to merge the cast. */
1620 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1621 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1622 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1623 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1624 size_int (TYPE_ALIGN (dctype)
1626 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1629 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1630 underneath as appropriate. */
1632 static enum gimplify_status
1633 gimplify_conversion (tree *expr_p)
1635 /* Strip away as many useless type conversions as possible
1637 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
1639 /* If we still have a conversion at the toplevel, then strip
1640 away all but the outermost conversion. */
1641 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1643 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1645 /* And remove the outermost conversion if it's useless. */
1646 if (tree_ssa_useless_type_conversion (*expr_p))
1647 *expr_p = TREE_OPERAND (*expr_p, 0);
1650 /* If we still have a conversion at the toplevel,
1651 then canonicalize some constructs. */
1652 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1654 tree sub = TREE_OPERAND (*expr_p, 0);
1656 /* If a NOP conversion is changing the type of a COMPONENT_REF
1657 expression, then canonicalize its type now in order to expose more
1658 redundant conversions. */
1659 if (TREE_CODE (sub) == COMPONENT_REF)
1660 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1662 /* If a NOP conversion is changing a pointer to array of foo
1663 to a pointer to foo, embed that change in the ADDR_EXPR. */
1664 else if (TREE_CODE (sub) == ADDR_EXPR)
1665 canonicalize_addr_expr (expr_p);
1671 /* Reduce MIN/MAX_EXPR to a COND_EXPR for further gimplification. */
1673 static enum gimplify_status
1674 gimplify_minimax_expr (tree *expr_p, tree *pre_p, tree *post_p)
1676 tree op1 = TREE_OPERAND (*expr_p, 0);
1677 tree op2 = TREE_OPERAND (*expr_p, 1);
1678 enum tree_code code;
1679 enum gimplify_status r0, r1;
1681 if (TREE_CODE (*expr_p) == MIN_EXPR)
1686 r0 = gimplify_expr (&op1, pre_p, post_p, is_gimple_val, fb_rvalue);
1687 r1 = gimplify_expr (&op2, pre_p, post_p, is_gimple_val, fb_rvalue);
1689 *expr_p = build (COND_EXPR, TREE_TYPE (*expr_p),
1690 build (code, boolean_type_node, op1, op2),
1693 if (r0 == GS_ERROR || r1 == GS_ERROR)
1699 /* Subroutine of gimplify_compound_lval.
1700 Converts an ARRAY_REF to the equivalent *(&array + offset) form. */
1702 static enum gimplify_status
1703 gimplify_array_ref_to_plus (tree *expr_p, tree *pre_p, tree *post_p)
1705 tree array = TREE_OPERAND (*expr_p, 0);
1706 tree arrtype = TREE_TYPE (array);
1707 tree elttype = TREE_TYPE (arrtype);
1708 tree size = array_ref_element_size (*expr_p);
1709 tree ptrtype = build_pointer_type (elttype);
1710 enum tree_code add_code = PLUS_EXPR;
1711 tree idx = TREE_OPERAND (*expr_p, 1);
1712 tree minidx = unshare_expr (array_ref_low_bound (*expr_p));
1713 tree offset, addr, result;
1714 enum gimplify_status ret;
1716 /* If the array domain does not start at zero, apply the offset. */
1717 if (!integer_zerop (minidx))
1719 idx = convert (TREE_TYPE (minidx), idx);
1720 idx = fold (build (MINUS_EXPR, TREE_TYPE (minidx), idx, minidx));
1723 /* If the index is negative -- a technically invalid situation now
1724 that we've biased the index back to zero -- then casting it to
1725 unsigned has ill effects. In particular, -1*4U/4U != -1.
1726 Represent this as a subtraction of a positive rather than addition
1727 of a negative. This will prevent any conversion back to ARRAY_REF
1728 from getting the wrong results from the division. */
1729 if (TREE_CODE (idx) == INTEGER_CST && tree_int_cst_sgn (idx) < 0)
1731 idx = fold (build1 (NEGATE_EXPR, TREE_TYPE (idx), idx));
1732 add_code = MINUS_EXPR;
1735 /* Pointer arithmetic must be done in sizetype. */
1736 idx = fold_convert (sizetype, idx);
1738 /* Convert the index to a byte offset. */
1739 offset = size_binop (MULT_EXPR, size, idx);
1741 ret = gimplify_expr (&array, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
1742 if (ret == GS_ERROR)
1745 addr = build_fold_addr_expr_with_type (array, ptrtype);
1746 result = fold (build (add_code, ptrtype, addr, offset));
1747 *expr_p = build1 (INDIRECT_REF, elttype, result);
1752 /* Build an expression for the address of T. Folds away INDIRECT_REF to
1753 avoid confusing the gimplify process. */
1756 build_addr_expr_with_type (tree t, tree ptrtype)
1758 if (TREE_CODE (t) == INDIRECT_REF)
1760 t = TREE_OPERAND (t, 0);
1761 if (TREE_TYPE (t) != ptrtype)
1762 t = build1 (NOP_EXPR, ptrtype, t);
1768 if (TREE_CODE (base) == REALPART_EXPR
1769 || TREE_CODE (base) == IMAGPART_EXPR)
1770 base = TREE_OPERAND (base, 0);
1772 while (handled_component_p (base))
1773 base = TREE_OPERAND (base, 0);
1776 TREE_ADDRESSABLE (base) = 1;
1778 t = build1 (ADDR_EXPR, ptrtype, t);
1785 build_addr_expr (tree t)
1787 return build_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
1790 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1791 node pointed by EXPR_P.
1794 : min_lval '[' val ']'
1796 | compound_lval '[' val ']'
1797 | compound_lval '.' ID
1799 This is not part of the original SIMPLE definition, which separates
1800 array and member references, but it seems reasonable to handle them
1801 together. Also, this way we don't run into problems with union
1802 aliasing; gcc requires that for accesses through a union to alias, the
1803 union reference must be explicit, which was not always the case when we
1804 were splitting up array and member refs.
1806 PRE_P points to the list where side effects that must happen before
1807 *EXPR_P should be stored.
1809 POST_P points to the list where side effects that must happen after
1810 *EXPR_P should be stored. */
1812 static enum gimplify_status
1813 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1814 tree *post_p, bool want_lvalue)
1818 enum gimplify_status ret = GS_OK, tret;
1820 #if defined ENABLE_CHECKING
1821 if (TREE_CODE (*expr_p) != ARRAY_REF
1822 && TREE_CODE (*expr_p) != ARRAY_RANGE_REF
1823 && TREE_CODE (*expr_p) != COMPONENT_REF
1824 && TREE_CODE (*expr_p) != BIT_FIELD_REF
1825 && TREE_CODE (*expr_p) != REALPART_EXPR
1826 && TREE_CODE (*expr_p) != IMAGPART_EXPR)
1830 /* Create a stack of the subexpressions so later we can walk them in
1831 order from inner to outer. */
1832 VARRAY_TREE_INIT (stack, 10, "stack");
1834 /* We can either handle one REALPART_EXPR or IMAGEPART_EXPR or
1835 nest of handled components. */
1836 if (TREE_CODE (*expr_p) == REALPART_EXPR
1837 || TREE_CODE (*expr_p) == IMAGPART_EXPR)
1838 p = &TREE_OPERAND (*expr_p, 0);
1840 for (p = expr_p; handled_component_p (*p); p = &TREE_OPERAND (*p, 0))
1841 VARRAY_PUSH_TREE (stack, *p);
1843 /* Now STACK is a stack of pointers to all the refs we've walked through
1844 and P points to the innermost expression.
1846 Process each of the outer nodes from left to right, then gimplify the
1847 base. We need to do it in this order so that PLACEHOLDER_EXPRs
1849 for (; VARRAY_ACTIVE_SIZE (stack) > 0; )
1851 tree t = VARRAY_TOP_TREE (stack);
1853 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1855 /* Gimplify the dimension.
1856 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1857 Gimplify non-constant array indices into a temporary
1859 FIXME - The real fix is to gimplify post-modify
1860 expressions into a minimal gimple lvalue. However, that
1861 exposes bugs in alias analysis. The alias analyzer does
1862 not handle &PTR->FIELD very well. Will fix after the
1863 branch is merged into mainline (dnovillo 2004-05-03). */
1864 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1866 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1867 is_gimple_tmp_var, fb_rvalue);
1868 ret = MIN (ret, tret);
1871 /* Gimplify the low bound and element type size and put them into
1872 the ARRAY_REF. If these values are set, they have already been
1874 if (!TREE_OPERAND (t, 2))
1876 tree low = unshare_expr (array_ref_low_bound (t));
1877 if (!is_gimple_min_invariant (low))
1879 TREE_OPERAND (t, 2) = low;
1880 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1881 is_gimple_tmp_var, fb_rvalue);
1882 ret = MIN (ret, tret);
1886 if (!TREE_OPERAND (t, 3))
1888 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1889 tree elmt_size = unshare_expr (array_ref_element_size (t));
1890 tree factor = size_int (TYPE_ALIGN (elmt_type) / BITS_PER_UNIT);
1892 /* Divide the element size by the alignment of the element
1894 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1896 if (!is_gimple_min_invariant (elmt_size))
1898 TREE_OPERAND (t, 3) = elmt_size;
1899 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1900 is_gimple_tmp_var, fb_rvalue);
1901 ret = MIN (ret, tret);
1905 else if (TREE_CODE (t) == COMPONENT_REF)
1907 /* Set the field offset into T and gimplify it. */
1908 if (!TREE_OPERAND (t, 2))
1910 tree offset = unshare_expr (component_ref_field_offset (t));
1911 tree field = TREE_OPERAND (t, 1);
1913 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1915 /* Divide the offset by its alignment. */
1916 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1918 if (!is_gimple_min_invariant (offset))
1920 TREE_OPERAND (t, 2) = offset;
1921 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1922 is_gimple_tmp_var, fb_rvalue);
1923 ret = MIN (ret, tret);
1927 else if (TREE_CODE (t) == BIT_FIELD_REF)
1929 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1930 is_gimple_val, fb_rvalue);
1931 ret = MIN (ret, tret);
1932 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1933 is_gimple_val, fb_rvalue);
1934 ret = MIN (ret, tret);
1937 recalculate_side_effects (t);
1941 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1942 want_lvalue ? fb_lvalue : fb_rvalue);
1943 ret = MIN (ret, tret);
1945 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1946 if (!want_lvalue && TREE_CODE (*expr_p) == COMPONENT_REF)
1948 canonicalize_component_ref (expr_p);
1949 ret = MIN (ret, GS_OK);
1955 /* Gimplify the self modifying expression pointed by EXPR_P (++, --, +=, -=).
1957 PRE_P points to the list where side effects that must happen before
1958 *EXPR_P should be stored.
1960 POST_P points to the list where side effects that must happen after
1961 *EXPR_P should be stored.
1963 WANT_VALUE is nonzero iff we want to use the value of this expression
1964 in another expression. */
1966 static enum gimplify_status
1967 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1970 enum tree_code code;
1971 tree lhs, lvalue, rhs, t1;
1973 enum tree_code arith_code;
1974 enum gimplify_status ret;
1976 code = TREE_CODE (*expr_p);
1978 #if defined ENABLE_CHECKING
1979 if (code != POSTINCREMENT_EXPR
1980 && code != POSTDECREMENT_EXPR
1981 && code != PREINCREMENT_EXPR
1982 && code != PREDECREMENT_EXPR)
1986 /* Prefix or postfix? */
1987 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1988 /* Faster to treat as prefix if result is not used. */
1989 postfix = want_value;
1993 /* Add or subtract? */
1994 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1995 arith_code = PLUS_EXPR;
1997 arith_code = MINUS_EXPR;
1999 /* Gimplify the LHS into a GIMPLE lvalue. */
2000 lvalue = TREE_OPERAND (*expr_p, 0);
2001 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2002 if (ret == GS_ERROR)
2005 /* Extract the operands to the arithmetic operation. */
2007 rhs = TREE_OPERAND (*expr_p, 1);
2009 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2010 that as the result value and in the postqueue operation. */
2013 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2014 if (ret == GS_ERROR)
2018 t1 = build (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
2019 t1 = build (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2023 gimplify_and_add (t1, post_p);
2034 /* Gimplify the CALL_EXPR node pointed by EXPR_P.
2037 : ID '(' arglist ')'
2043 PRE_P points to the list where side effects that must happen before
2044 *EXPR_P should be stored. */
2046 static enum gimplify_status
2047 gimplify_call_expr (tree *expr_p, tree *pre_p, bool (*gimple_test_f) (tree))
2051 enum gimplify_status ret;
2053 #if defined ENABLE_CHECKING
2054 if (TREE_CODE (*expr_p) != CALL_EXPR)
2058 /* For reliable diagnostics during inlining, it is necessary that
2059 every call_expr be annotated with file and line. */
2060 if (!EXPR_LOCUS (*expr_p))
2061 annotate_with_locus (*expr_p, input_location);
2063 /* This may be a call to a builtin function.
2065 Builtin function calls may be transformed into different
2066 (and more efficient) builtin function calls under certain
2067 circumstances. Unfortunately, gimplification can muck things
2068 up enough that the builtin expanders are not aware that certain
2069 transformations are still valid.
2071 So we attempt transformation/gimplification of the call before
2072 we gimplify the CALL_EXPR. At this time we do not manage to
2073 transform all calls in the same manner as the expanders do, but
2074 we do transform most of them. */
2075 decl = get_callee_fndecl (*expr_p);
2076 if (decl && DECL_BUILT_IN (decl))
2080 /* If it is allocation of stack, record the need to restore the memory
2081 when the enclosing bind_expr is exited. */
2082 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_ALLOC)
2083 gimplify_ctxp->save_stack = true;
2085 /* If it is restore of the stack, reset it, since it means we are
2086 regimplifying the bind_expr. Note that we use the fact that
2087 for try_finally_expr, try part is processed first. */
2088 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_STACK_RESTORE)
2089 gimplify_ctxp->save_stack = false;
2091 new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2093 if (new && new != *expr_p)
2095 /* There was a transformation of this call which computes the
2096 same value, but in a more efficient way. Return and try
2103 /* There is a sequence point before the call, so any side effects in
2104 the calling expression must occur before the actual call. Force
2105 gimplify_expr to use an internal post queue. */
2106 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2107 is_gimple_call_addr, fb_rvalue);
2109 if (PUSH_ARGS_REVERSED)
2110 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2111 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2112 arglist = TREE_CHAIN (arglist))
2114 enum gimplify_status t;
2116 /* There is a sequence point before a function call. Side effects in
2117 the argument list must occur before the actual call. So, when
2118 gimplifying arguments, force gimplify_expr to use an internal
2119 post queue which is then appended to the end of PRE_P. */
2120 t = gimplify_expr (&TREE_VALUE (arglist), pre_p, NULL, is_gimple_val,
2126 if (PUSH_ARGS_REVERSED)
2127 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2129 /* Try this again in case gimplification exposed something. */
2130 if (ret != GS_ERROR && decl && DECL_BUILT_IN (decl))
2132 tree new = simplify_builtin (*expr_p, gimple_test_f == is_gimple_stmt);
2134 if (new && new != *expr_p)
2136 /* There was a transformation of this call which computes the
2137 same value, but in a more efficient way. Return and try
2144 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2145 decl. This allows us to eliminate redundant or useless
2146 calls to "const" functions. */
2147 if (TREE_CODE (*expr_p) == CALL_EXPR
2148 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2149 TREE_SIDE_EFFECTS (*expr_p) = 0;
2154 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2155 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2157 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2158 condition is true or false, respectively. If null, we should generate
2159 our own to skip over the evaluation of this specific expression.
2161 This function is the tree equivalent of do_jump.
2163 shortcut_cond_r should only be called by shortcut_cond_expr. */
2166 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2168 tree local_label = NULL_TREE;
2169 tree t, expr = NULL;
2171 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2172 retain the shortcut semantics. Just insert the gotos here;
2173 shortcut_cond_expr will append the real blocks later. */
2174 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2176 /* Turn if (a && b) into
2178 if (a); else goto no;
2179 if (b) goto yes; else goto no;
2182 if (false_label_p == NULL)
2183 false_label_p = &local_label;
2185 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2186 append_to_statement_list (t, &expr);
2188 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2190 append_to_statement_list (t, &expr);
2192 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2194 /* Turn if (a || b) into
2197 if (b) goto yes; else goto no;
2200 if (true_label_p == NULL)
2201 true_label_p = &local_label;
2203 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2204 append_to_statement_list (t, &expr);
2206 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2208 append_to_statement_list (t, &expr);
2210 else if (TREE_CODE (pred) == COND_EXPR)
2212 /* As long as we're messing with gotos, turn if (a ? b : c) into
2214 if (b) goto yes; else goto no;
2216 if (c) goto yes; else goto no; */
2217 expr = build (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2218 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2220 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2225 expr = build (COND_EXPR, void_type_node, pred,
2226 build_and_jump (true_label_p),
2227 build_and_jump (false_label_p));
2232 t = build1 (LABEL_EXPR, void_type_node, local_label);
2233 append_to_statement_list (t, &expr);
2240 shortcut_cond_expr (tree expr)
2242 tree pred = TREE_OPERAND (expr, 0);
2243 tree then_ = TREE_OPERAND (expr, 1);
2244 tree else_ = TREE_OPERAND (expr, 2);
2245 tree true_label, false_label, end_label, t;
2247 tree *false_label_p;
2248 bool emit_end, emit_false;
2249 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2250 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2252 /* First do simple transformations. */
2255 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2256 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2258 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2259 then_ = shortcut_cond_expr (expr);
2260 pred = TREE_OPERAND (pred, 0);
2261 expr = build (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2266 /* If there is no 'then', turn
2269 if (a); else if (b); else d. */
2270 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2272 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2273 else_ = shortcut_cond_expr (expr);
2274 pred = TREE_OPERAND (pred, 0);
2275 expr = build (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2279 /* If we're done, great. */
2280 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2281 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2284 /* Otherwise we need to mess with gotos. Change
2287 if (a); else goto no;
2290 and recursively gimplify the condition. */
2292 true_label = false_label = end_label = NULL_TREE;
2294 /* If our arms just jump somewhere, hijack those labels so we don't
2295 generate jumps to jumps. */
2298 && TREE_CODE (then_) == GOTO_EXPR
2299 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2301 true_label = GOTO_DESTINATION (then_);
2307 && TREE_CODE (else_) == GOTO_EXPR
2308 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2310 false_label = GOTO_DESTINATION (else_);
2315 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2317 true_label_p = &true_label;
2319 true_label_p = NULL;
2321 /* The 'else' branch also needs a label if it contains interesting code. */
2322 if (false_label || else_se)
2323 false_label_p = &false_label;
2325 false_label_p = NULL;
2327 /* If there was nothing else in our arms, just forward the label(s). */
2328 if (!then_se && !else_se)
2329 return shortcut_cond_r (pred, true_label_p, false_label_p);
2331 /* If our last subexpression already has a terminal label, reuse it. */
2333 expr = expr_last (else_);
2335 expr = expr_last (then_);
2338 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2339 end_label = LABEL_EXPR_LABEL (expr);
2341 /* If we don't care about jumping to the 'else' branch, jump to the end
2342 if the condition is false. */
2344 false_label_p = &end_label;
2346 /* We only want to emit these labels if we aren't hijacking them. */
2347 emit_end = (end_label == NULL_TREE);
2348 emit_false = (false_label == NULL_TREE);
2350 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2353 append_to_statement_list (pred, &expr);
2355 append_to_statement_list (then_, &expr);
2358 t = build_and_jump (&end_label);
2359 append_to_statement_list (t, &expr);
2362 t = build1 (LABEL_EXPR, void_type_node, false_label);
2363 append_to_statement_list (t, &expr);
2365 append_to_statement_list (else_, &expr);
2367 if (emit_end && end_label)
2369 t = build1 (LABEL_EXPR, void_type_node, end_label);
2370 append_to_statement_list (t, &expr);
2376 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2379 gimple_boolify (tree expr)
2381 tree type = TREE_TYPE (expr);
2383 if (TREE_CODE (type) == BOOLEAN_TYPE)
2386 /* If this is the predicate of a COND_EXPR, it might not even be a
2388 expr = lang_hooks.truthvalue_conversion (expr);
2390 switch (TREE_CODE (expr))
2392 case TRUTH_AND_EXPR:
2394 case TRUTH_XOR_EXPR:
2395 case TRUTH_ANDIF_EXPR:
2396 case TRUTH_ORIF_EXPR:
2397 /* Also boolify the arguments of truth exprs. */
2398 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2401 case TRUTH_NOT_EXPR:
2402 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2405 case EQ_EXPR: case NE_EXPR:
2406 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2407 /* These expressions always produce boolean results. */
2408 TREE_TYPE (expr) = boolean_type_node;
2412 /* Other expressions that get here must have boolean values, but
2413 might need to be converted to the appropriate mode. */
2414 return convert (boolean_type_node, expr);
2418 /* Convert the conditional expression pointed by EXPR_P '(p) ? a : b;'
2427 The second form is used when *EXPR_P is of type void.
2429 TARGET is the tree for T1 above.
2431 PRE_P points to the list where side effects that must happen before
2432 *EXPR_P should be stored. */
2434 static enum gimplify_status
2435 gimplify_cond_expr (tree *expr_p, tree *pre_p, tree target)
2437 tree expr = *expr_p;
2439 enum gimplify_status ret;
2441 type = TREE_TYPE (expr);
2443 TREE_TYPE (expr) = void_type_node;
2445 /* If this COND_EXPR has a value, copy the values into a temporary within
2447 else if (! VOID_TYPE_P (type))
2456 tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2460 /* Build the then clause, 't1 = a;'. But don't build an assignment
2461 if this branch is void; in C++ it can be, if it's a throw. */
2462 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2463 TREE_OPERAND (expr, 1)
2464 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 1));
2466 /* Build the else clause, 't1 = b;'. */
2467 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2468 TREE_OPERAND (expr, 2)
2469 = build (MODIFY_EXPR, void_type_node, tmp, TREE_OPERAND (expr, 2));
2471 TREE_TYPE (expr) = void_type_node;
2472 recalculate_side_effects (expr);
2474 /* Move the COND_EXPR to the prequeue and use the temp in its place. */
2475 gimplify_and_add (expr, pre_p);
2481 /* Make sure the condition has BOOLEAN_TYPE. */
2482 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2484 /* Break apart && and || conditions. */
2485 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2486 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2488 expr = shortcut_cond_expr (expr);
2490 if (expr != *expr_p)
2494 /* We can't rely on gimplify_expr to re-gimplify the expanded
2495 form properly, as cleanups might cause the target labels to be
2496 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2497 set up a conditional context. */
2498 gimple_push_condition ();
2499 gimplify_stmt (expr_p);
2500 gimple_pop_condition (pre_p);
2506 /* Now do the normal gimplification. */
2507 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2508 is_gimple_condexpr, fb_rvalue);
2510 gimple_push_condition ();
2512 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2513 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2514 recalculate_side_effects (expr);
2516 gimple_pop_condition (pre_p);
2518 if (ret == GS_ERROR)
2520 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2522 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2523 /* Rewrite "if (a); else b" to "if (!a) b" */
2525 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2526 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2527 is_gimple_condexpr, fb_rvalue);
2529 tmp = TREE_OPERAND (expr, 1);
2530 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2531 TREE_OPERAND (expr, 2) = tmp;
2534 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2535 expr = TREE_OPERAND (expr, 0);
2541 /* Gimplify the MODIFY_EXPR node pointed by EXPR_P.
2547 PRE_P points to the list where side effects that must happen before
2548 *EXPR_P should be stored.
2550 POST_P points to the list where side effects that must happen after
2551 *EXPR_P should be stored.
2553 WANT_VALUE is nonzero iff we want to use the value of this expression
2554 in another expression. */
2556 static enum gimplify_status
2557 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
2559 tree *from_p = &TREE_OPERAND (*expr_p, 1);
2560 tree *to_p = &TREE_OPERAND (*expr_p, 0);
2561 enum gimplify_status ret = GS_UNHANDLED;
2563 #if defined ENABLE_CHECKING
2564 if (TREE_CODE (*expr_p) != MODIFY_EXPR && TREE_CODE (*expr_p) != INIT_EXPR)
2568 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer useful. */
2569 if (TREE_CODE (*expr_p) == INIT_EXPR)
2570 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
2572 /* See if any simplifications can be done based on what the RHS is. */
2573 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2575 if (ret != GS_UNHANDLED)
2578 /* If the value being copied is of variable width, expose the length
2579 if the copy by converting the whole thing to a memcpy. Note that
2580 we need to do this before gimplifying any of the operands
2581 so that we can resolve any PLACEHOLDER_EXPRs in the size. */
2582 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p))) != INTEGER_CST)
2586 t = TYPE_SIZE_UNIT (TREE_TYPE (*to_p));
2587 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, *to_p);
2588 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, *from_p);
2589 t = unshare_expr (t);
2590 args = tree_cons (NULL, t, NULL);
2591 t = build_fold_addr_expr (*from_p);
2592 args = tree_cons (NULL, t, args);
2593 dest = build_fold_addr_expr (*to_p);
2594 args = tree_cons (NULL, dest, args);
2595 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2596 t = build_function_call_expr (t, args);
2599 t = build1 (NOP_EXPR, TREE_TYPE (dest), t);
2600 t = build1 (INDIRECT_REF, TREE_TYPE (*to_p), t);
2606 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2607 if (ret == GS_ERROR)
2610 ret = gimplify_expr (from_p, pre_p, post_p, is_gimple_rhs, fb_rvalue);
2611 if (ret == GS_ERROR)
2614 /* Now see if the above changed *from_p to something we handle specially. */
2615 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
2617 if (ret != GS_UNHANDLED)
2620 /* If the destination is already simple, nothing else needed. */
2621 if (is_gimple_tmp_var (*to_p))
2625 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
2626 the LHS is a user variable, then we need to introduce a temporary.
2627 ie temp = RHS; LHS = temp.
2629 This way the optimizers can determine that the user variable is
2630 only modified if evaluation of the RHS does not throw.
2632 FIXME this should be handled by the is_gimple_rhs predicate. */
2634 if (aggregate_value_p (TREE_TYPE (*from_p), NULL_TREE))
2635 /* Don't force a temp of a large aggregate type; the copy could be
2636 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
2638 else if (TREE_CODE (*from_p) == CALL_EXPR
2639 || (flag_non_call_exceptions && tree_could_trap_p (*from_p))
2640 /* If we're dealing with a renamable type, either source or dest
2641 must be a renamed variable. */
2642 || (is_gimple_reg_type (TREE_TYPE (*from_p))
2643 && !is_gimple_reg (*to_p)))
2644 gimplify_expr (from_p, pre_p, post_p, is_gimple_val, fb_rvalue);
2646 ret = want_value ? GS_OK : GS_ALL_DONE;
2651 append_to_statement_list (*expr_p, pre_p);
2658 /* Subroutine of above to do simplications of MODIFY_EXPRs based on
2659 the code of the RHS. We loop for as long as we can do something. */
2661 static enum gimplify_status
2662 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
2663 tree *post_p, bool want_value)
2665 enum gimplify_status ret = GS_OK;
2667 while (ret != GS_UNHANDLED)
2668 switch (TREE_CODE (*from_p))
2672 /* If we are initializing something from a TARGET_EXPR, strip the
2673 TARGET_EXPR and initialize it directly, if possible. This can't
2674 be done if the initializer is void, since that implies that the
2675 temporary is set in some non-trivial way.
2677 ??? What about code that pulls out the temp and uses it
2678 elsewhere? I think that such code never uses the TARGET_EXPR as
2679 an initializer. If I'm wrong, we'll abort because the temp won't
2680 have any RTL. In that case, I guess we'll need to replace
2681 references somehow. */
2682 tree init = TARGET_EXPR_INITIAL (*from_p);
2684 if (!VOID_TYPE_P (TREE_TYPE (init)))
2695 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
2697 gimplify_compound_expr (from_p, pre_p, true);
2702 /* If we're initializing from a CONSTRUCTOR, break this into
2703 individual MODIFY_EXPRs. */
2704 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
2707 /* If we're assigning from a ?: expression with ADDRESSABLE type, push
2708 the assignment down into the branches, since we can't generate a
2709 temporary of such a type. */
2710 if (TREE_ADDRESSABLE (TREE_TYPE (*from_p)))
2713 return gimplify_cond_expr (expr_p, pre_p, *to_p);
2727 /* Gimplify a comparison between two variable-sized objects. Do this
2728 with a call to BUILT_IN_MEMCMP. */
2730 static enum gimplify_status
2731 gimplify_variable_sized_compare (tree *expr_p)
2733 tree op0 = TREE_OPERAND (*expr_p, 0);
2734 tree op1 = TREE_OPERAND (*expr_p, 1);
2737 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
2738 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
2739 t = unshare_expr (t);
2740 args = tree_cons (NULL, t, NULL);
2741 t = build_addr_expr (op1);
2742 args = tree_cons (NULL, t, args);
2743 dest = build_addr_expr (op0);
2744 args = tree_cons (NULL, dest, args);
2745 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
2746 t = build_function_call_expr (t, args);
2748 = build (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
2753 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
2754 points to the expression to gimplify.
2756 Expressions of the form 'a && b' are gimplified to:
2758 a && b ? true : false
2760 gimplify_cond_expr will do the rest.
2762 PRE_P points to the list where side effects that must happen before
2763 *EXPR_P should be stored. */
2765 static enum gimplify_status
2766 gimplify_boolean_expr (tree *expr_p)
2768 /* Preserve the original type of the expression. */
2769 tree type = TREE_TYPE (*expr_p);
2771 *expr_p = build (COND_EXPR, type, *expr_p,
2772 convert (type, boolean_true_node),
2773 convert (type, boolean_false_node));
2778 /* Gimplifies an expression sequence. This function gimplifies each
2779 expression and re-writes the original expression with the last
2780 expression of the sequence in GIMPLE form.
2782 PRE_P points to the list where the side effects for all the
2783 expressions in the sequence will be emitted.
2785 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
2786 /* ??? Should rearrange to share the pre-queue with all the indirect
2787 invocations of gimplify_expr. Would probably save on creations
2788 of statement_list nodes. */
2790 static enum gimplify_status
2791 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
2797 tree *sub_p = &TREE_OPERAND (t, 0);
2799 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
2800 gimplify_compound_expr (sub_p, pre_p, false);
2802 gimplify_stmt (sub_p);
2803 append_to_statement_list (*sub_p, pre_p);
2805 t = TREE_OPERAND (t, 1);
2807 while (TREE_CODE (t) == COMPOUND_EXPR);
2814 gimplify_stmt (expr_p);
2819 /* Gimplifies a statement list. These may be created either by an
2820 enlightened front-end, or by shortcut_cond_expr. */
2822 static enum gimplify_status
2823 gimplify_statement_list (tree *expr_p)
2825 tree_stmt_iterator i = tsi_start (*expr_p);
2827 while (!tsi_end_p (i))
2831 gimplify_stmt (tsi_stmt_ptr (i));
2836 else if (TREE_CODE (t) == STATEMENT_LIST)
2838 tsi_link_before (&i, t, TSI_SAME_STMT);
2848 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
2849 gimplify. After gimplification, EXPR_P will point to a new temporary
2850 that holds the original value of the SAVE_EXPR node.
2852 PRE_P points to the list where side effects that must happen before
2853 *EXPR_P should be stored. */
2855 static enum gimplify_status
2856 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
2858 enum gimplify_status ret = GS_ALL_DONE;
2861 #if defined ENABLE_CHECKING
2862 if (TREE_CODE (*expr_p) != SAVE_EXPR)
2866 val = TREE_OPERAND (*expr_p, 0);
2868 /* If the operand is already a GIMPLE temporary, just re-write the
2870 if (is_gimple_tmp_var (val))
2872 /* The operand may be a void-valued expression such as SAVE_EXPRs
2873 generated by the Java frontend for class initialization. It is
2874 being executed only for its side-effects. */
2875 else if (TREE_TYPE (val) == void_type_node)
2877 tree body = TREE_OPERAND (*expr_p, 0);
2878 ret = gimplify_expr (& body, pre_p, post_p, is_gimple_stmt, fb_none);
2879 append_to_statement_list (body, pre_p);
2883 *expr_p = TREE_OPERAND (*expr_p, 0)
2884 = get_initialized_tmp_var (val, pre_p, post_p);
2889 /* Re-write the ADDR_EXPR node pointed by EXPR_P
2896 PRE_P points to the list where side effects that must happen before
2897 *EXPR_P should be stored.
2899 POST_P points to the list where side effects that must happen after
2900 *EXPR_P should be stored. */
2902 static enum gimplify_status
2903 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
2905 tree expr = *expr_p;
2906 tree op0 = TREE_OPERAND (expr, 0);
2907 enum gimplify_status ret;
2909 switch (TREE_CODE (op0))
2912 /* Check if we are dealing with an expression of the form '&*ptr'.
2913 While the front end folds away '&*ptr' into 'ptr', these
2914 expressions may be generated internally by the compiler (e.g.,
2915 builtins like __builtin_va_end). */
2916 *expr_p = TREE_OPERAND (op0, 0);
2921 /* Fold &a[6] to (&a + 6). */
2922 ret = gimplify_array_ref_to_plus (&TREE_OPERAND (expr, 0),
2925 /* This added an INDIRECT_REF. Fold it away. */
2926 *expr_p = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2929 case VIEW_CONVERT_EXPR:
2930 /* Take the address of our operand and then convert it to the type of
2932 *expr_p = fold_convert (TREE_TYPE (expr),
2933 build_addr_expr (TREE_OPERAND (op0, 0)));
2938 /* We use fb_either here because the C frontend sometimes takes
2939 the address of a call that returns a struct. */
2940 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
2941 is_gimple_addr_expr_arg, fb_either);
2942 if (ret != GS_ERROR)
2944 /* At this point, the argument of the ADDR_EXPR should be
2945 sufficiently simple that there are never side effects. */
2946 /* ??? Could split out the decision code from build1 to verify. */
2947 TREE_SIDE_EFFECTS (expr) = 0;
2949 /* Make sure TREE_INVARIANT/TREE_CONSTANT is set properly. */
2950 recompute_tree_invarant_for_addr_expr (expr);
2952 /* Mark the RHS addressable. */
2953 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
2958 /* If the operand is gimplified into a _DECL, mark the address expression
2959 as TREE_INVARIANT. */
2960 if (DECL_P (TREE_OPERAND (expr, 0)))
2961 TREE_INVARIANT (expr) = 1;
2966 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
2967 value; output operands should be a gimple lvalue. */
2969 static enum gimplify_status
2970 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
2972 tree expr = *expr_p;
2973 int noutputs = list_length (ASM_OUTPUTS (expr));
2974 const char **oconstraints
2975 = (const char **) alloca ((noutputs) * sizeof (const char *));
2978 const char *constraint;
2979 bool allows_mem, allows_reg, is_inout;
2980 enum gimplify_status ret, tret;
2983 = resolve_asm_operand_names (ASM_STRING (expr), ASM_OUTPUTS (expr),
2987 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
2989 oconstraints[i] = constraint
2990 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
2992 parse_output_constraint (&constraint, i, 0, 0,
2993 &allows_mem, &allows_reg, &is_inout);
2995 if (!allows_reg && allows_mem)
2996 lang_hooks.mark_addressable (TREE_VALUE (link));
2998 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
2999 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3000 fb_lvalue | fb_mayfail);
3001 if (tret == GS_ERROR)
3003 error ("invalid lvalue in asm output %d", i);
3009 /* An input/output operand. To give the optimizers more
3010 flexibility, split it into separate input and output
3014 size_t constraint_len = strlen (constraint);
3016 /* Turn the in/out constraint into an output constraint. */
3017 char *p = xstrdup (constraint);
3019 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
3022 /* And add a matching input constraint. */
3025 sprintf (buf, "%d", i);
3026 input = build_string (strlen (buf), buf);
3029 input = build_string (constraint_len - 1, constraint + 1);
3030 input = build_tree_list (build_tree_list (NULL_TREE, input),
3031 unshare_expr (TREE_VALUE (link)));
3032 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
3036 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3039 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3040 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
3041 oconstraints, &allows_mem, &allows_reg);
3043 /* If the operand is a memory input, it should be an lvalue. */
3044 if (!allows_reg && allows_mem)
3046 lang_hooks.mark_addressable (TREE_VALUE (link));
3047 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3048 is_gimple_lvalue, fb_lvalue | fb_mayfail);
3049 if (tret == GS_ERROR)
3051 error ("memory input %d is not directly addressable", i);
3057 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3058 is_gimple_val, fb_rvalue);
3059 if (tret == GS_ERROR)
3067 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
3068 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
3069 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
3070 return to this function.
3072 FIXME should we complexify the prequeue handling instead? Or use flags
3073 for all the cleanups and let the optimizer tighten them up? The current
3074 code seems pretty fragile; it will break on a cleanup within any
3075 non-conditional nesting. But any such nesting would be broken, anyway;
3076 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
3077 and continues out of it. We can do that at the RTL level, though, so
3078 having an optimizer to tighten up try/finally regions would be a Good
3081 static enum gimplify_status
3082 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
3084 tree_stmt_iterator iter;
3087 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3089 /* We only care about the number of conditions between the innermost
3090 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count. */
3091 int old_conds = gimplify_ctxp->conditions;
3092 gimplify_ctxp->conditions = 0;
3094 body = TREE_OPERAND (*expr_p, 0);
3095 gimplify_to_stmt_list (&body);
3097 gimplify_ctxp->conditions = old_conds;
3099 for (iter = tsi_start (body); !tsi_end_p (iter); )
3101 tree *wce_p = tsi_stmt_ptr (iter);
3104 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
3106 if (tsi_one_before_end_p (iter))
3108 tsi_link_before (&iter, TREE_OPERAND (wce, 1), TSI_SAME_STMT);
3116 sl = tsi_split_statement_list_after (&iter);
3117 tfe = build (TRY_FINALLY_EXPR, void_type_node, sl, NULL_TREE);
3118 append_to_statement_list (TREE_OPERAND (wce, 1),
3119 &TREE_OPERAND (tfe, 1));
3121 iter = tsi_start (sl);
3131 append_to_statement_list (body, pre_p);
3141 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
3142 is the cleanup action required. */
3145 gimple_push_cleanup (tree var, tree cleanup, tree *pre_p)
3149 /* Errors can result in improperly nested cleanups. Which results in
3150 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
3151 if (errorcount || sorrycount)
3154 if (gimple_conditional_context ())
3156 /* If we're in a conditional context, this is more complex. We only
3157 want to run the cleanup if we actually ran the initialization that
3158 necessitates it, but we want to run it after the end of the
3159 conditional context. So we wrap the try/finally around the
3160 condition and use a flag to determine whether or not to actually
3161 run the destructor. Thus
3165 becomes (approximately)
3169 if (test) { A::A(temp); flag = 1; val = f(temp); }
3172 if (flag) A::~A(temp);
3177 tree flag = create_tmp_var (boolean_type_node, "cleanup");
3178 tree ffalse = build (MODIFY_EXPR, void_type_node, flag,
3179 boolean_false_node);
3180 tree ftrue = build (MODIFY_EXPR, void_type_node, flag,
3182 cleanup = build (COND_EXPR, void_type_node, flag, cleanup, NULL);
3183 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3184 cleanup, NULL_TREE);
3185 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
3186 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
3187 append_to_statement_list (ftrue, pre_p);
3189 /* Because of this manipulation, and the EH edges that jump
3190 threading cannot redirect, the temporary (VAR) will appear
3191 to be used uninitialized. Don't warn. */
3192 TREE_NO_WARNING (var) = 1;
3196 wce = build (WITH_CLEANUP_EXPR, void_type_node, NULL_TREE,
3197 cleanup, NULL_TREE);
3198 append_to_statement_list (wce, pre_p);
3201 gimplify_stmt (&TREE_OPERAND (wce, 1));
3204 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
3206 static enum gimplify_status
3207 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
3209 tree targ = *expr_p;
3210 tree temp = TARGET_EXPR_SLOT (targ);
3211 tree init = TARGET_EXPR_INITIAL (targ);
3212 enum gimplify_status ret;
3216 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
3217 to the temps list. */
3218 gimple_add_tmp_var (temp);
3220 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
3221 expression is supposed to initialize the slot. */
3222 if (VOID_TYPE_P (TREE_TYPE (init)))
3223 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
3226 /* Special handling for BIND_EXPR can result in fewer temps. */
3228 if (TREE_CODE (init) == BIND_EXPR)
3229 gimplify_bind_expr (&init, temp, pre_p);
3232 init = build (MODIFY_EXPR, void_type_node, temp, init);
3233 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
3237 if (ret == GS_ERROR)
3239 append_to_statement_list (init, pre_p);
3241 /* If needed, push the cleanup for the temp. */
3242 if (TARGET_EXPR_CLEANUP (targ))
3244 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
3245 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ), pre_p);
3248 /* Only expand this once. */
3249 TREE_OPERAND (targ, 3) = init;
3250 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
3252 else if (!temp->decl.seen_in_bind_expr)
3253 /* We should have expanded this before. */
3260 /* Gimplification of expression trees. */
3262 /* Gimplify an expression which appears at statement context; usually, this
3263 means replacing it with a suitably gimple STATEMENT_LIST. */
3266 gimplify_stmt (tree *stmt_p)
3268 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
3271 /* Similarly, but force the result to be a STATEMENT_LIST. */
3274 gimplify_to_stmt_list (tree *stmt_p)
3276 gimplify_stmt (stmt_p);
3278 *stmt_p = alloc_stmt_list ();
3279 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
3282 *stmt_p = alloc_stmt_list ();
3283 append_to_statement_list (t, stmt_p);
3288 /* Gimplifies the expression tree pointed by EXPR_P. Return 0 if
3289 gimplification failed.
3291 PRE_P points to the list where side effects that must happen before
3292 EXPR should be stored.
3294 POST_P points to the list where side effects that must happen after
3295 EXPR should be stored, or NULL if there is no suitable list. In
3296 that case, we copy the result to a temporary, emit the
3297 post-effects, and then return the temporary.
3299 GIMPLE_TEST_F points to a function that takes a tree T and
3300 returns nonzero if T is in the GIMPLE form requested by the
3301 caller. The GIMPLE predicates are in tree-gimple.c.
3303 This test is used twice. Before gimplification, the test is
3304 invoked to determine whether *EXPR_P is already gimple enough. If
3305 that fails, *EXPR_P is gimplified according to its code and
3306 GIMPLE_TEST_F is called again. If the test still fails, then a new
3307 temporary variable is created and assigned the value of the
3308 gimplified expression.
3310 FALLBACK tells the function what sort of a temporary we want. If the 1
3311 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
3312 If both are set, either is OK, but an lvalue is preferable.
3314 The return value is either GS_ERROR or GS_ALL_DONE, since this function
3315 iterates until solution. */
3317 enum gimplify_status
3318 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
3319 bool (* gimple_test_f) (tree), fallback_t fallback)
3322 tree internal_pre = NULL_TREE;
3323 tree internal_post = NULL_TREE;
3325 int is_statement = (pre_p == NULL);
3327 location_t saved_location;
3328 enum gimplify_status ret;
3330 save_expr = *expr_p;
3331 if (save_expr == NULL_TREE)
3334 /* We used to check the predicate here and return immediately if it
3335 succeeds. This is wrong; the design is for gimplification to be
3336 idempotent, and for the predicates to only test for valid forms, not
3337 whether they are fully simplified. */
3339 /* Set up our internal queues if needed. */
3341 pre_p = &internal_pre;
3343 post_p = &internal_post;
3345 saved_location = input_location;
3346 if (save_expr == error_mark_node)
3349 locus = EXPR_LOCUS (save_expr);
3351 input_location = *locus;
3353 /* Loop over the specific gimplifiers until the toplevel node
3354 remains the same. */
3357 /* Strip any uselessness. */
3358 STRIP_MAIN_TYPE_NOPS (*expr_p);
3360 /* Remember the expr. */
3361 save_expr = *expr_p;
3363 /* Die, die, die, my darling. */
3364 if (save_expr == error_mark_node
3365 || (TREE_TYPE (save_expr)
3366 && TREE_TYPE (save_expr) == error_mark_node))
3372 /* Do any language-specific gimplification. */
3373 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
3376 if (*expr_p == NULL_TREE)
3378 if (*expr_p != save_expr)
3381 else if (ret != GS_UNHANDLED)
3385 switch (TREE_CODE (*expr_p))
3387 /* First deal with the special cases. */
3389 case POSTINCREMENT_EXPR:
3390 case POSTDECREMENT_EXPR:
3391 case PREINCREMENT_EXPR:
3392 case PREDECREMENT_EXPR:
3393 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
3394 fallback != fb_none);
3398 case ARRAY_RANGE_REF:
3402 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
3403 fallback & fb_lvalue);
3407 ret = gimplify_cond_expr (expr_p, pre_p, NULL_TREE);
3411 ret = gimplify_call_expr (expr_p, pre_p, gimple_test_f);
3418 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
3423 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
3424 fallback != fb_none);
3427 case TRUTH_ANDIF_EXPR:
3428 case TRUTH_ORIF_EXPR:
3429 ret = gimplify_boolean_expr (expr_p);
3432 case TRUTH_NOT_EXPR:
3433 TREE_OPERAND (*expr_p, 0)
3434 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
3435 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3436 is_gimple_val, fb_rvalue);
3437 recalculate_side_effects (*expr_p);
3441 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
3445 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
3448 case VIEW_CONVERT_EXPR:
3449 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3450 || fallback == fb_none)
3452 /* Just strip a conversion to void (or in void context) and
3454 *expr_p = TREE_OPERAND (*expr_p, 0);
3458 /* If both types are BLKmode or if one type is of variable size,
3459 convert this into a pointer punning operation. This avoids
3460 copies of large data or making a variable-size temporary. */
3461 if ((TYPE_MODE (TREE_TYPE (*expr_p)) == BLKmode
3462 && TYPE_MODE (TREE_TYPE (TREE_OPERAND (*expr_p, 0))) == BLKmode)
3463 || !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (*expr_p)))
3464 || !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE
3465 (TREE_OPERAND (*expr_p,0)))))
3467 tree restype = TREE_TYPE (*expr_p);
3468 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (*expr_p),
3469 fold_convert (build_pointer_type (restype),
3471 (TREE_OPERAND (*expr_p, 0))));
3478 if (IS_EMPTY_STMT (*expr_p))
3484 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
3485 || fallback == fb_none)
3487 /* Just strip a conversion to void (or in void context) and
3489 *expr_p = TREE_OPERAND (*expr_p, 0);
3493 ret = gimplify_conversion (expr_p);
3494 if (ret == GS_ERROR)
3496 if (*expr_p != save_expr)
3500 case FIX_TRUNC_EXPR:
3502 case FIX_FLOOR_EXPR:
3503 case FIX_ROUND_EXPR:
3505 /* unary_expr: ... | '(' cast ')' val | ... */
3506 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3507 is_gimple_val, fb_rvalue);
3508 recalculate_side_effects (*expr_p);
3512 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3513 is_gimple_reg, fb_rvalue);
3514 recalculate_side_effects (*expr_p);
3517 /* Constants need not be gimplified. */
3527 *expr_p = DECL_INITIAL (*expr_p);
3531 /* FIXME make this a decl. */
3536 ret = gimplify_bind_expr (expr_p, NULL, pre_p);
3540 ret = gimplify_loop_expr (expr_p, pre_p);
3544 ret = gimplify_switch_expr (expr_p, pre_p);
3547 case LABELED_BLOCK_EXPR:
3548 ret = gimplify_labeled_block_expr (expr_p);
3551 case EXIT_BLOCK_EXPR:
3552 ret = gimplify_exit_block_expr (expr_p);
3556 ret = gimplify_exit_expr (expr_p);
3560 /* If the target is not LABEL, then it is a computed jump
3561 and the target needs to be gimplified. */
3562 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
3563 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
3564 NULL, is_gimple_val, fb_rvalue);
3569 #ifdef ENABLE_CHECKING
3570 if (decl_function_context (LABEL_EXPR_LABEL (*expr_p)) != current_function_decl)
3575 case CASE_LABEL_EXPR:
3576 ret = gimplify_case_label_expr (expr_p);
3580 ret = gimplify_return_expr (*expr_p, pre_p);
3584 /* Don't reduce this in place; let gimplify_init_constructor work
3589 /* The following are special cases that are not handled by the
3590 original GIMPLE grammar. */
3592 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
3595 ret = gimplify_save_expr (expr_p, pre_p, post_p);
3600 enum gimplify_status r0, r1, r2;
3602 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3603 is_gimple_min_lval, fb_either);
3604 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3605 is_gimple_val, fb_rvalue);
3606 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
3607 is_gimple_val, fb_rvalue);
3608 recalculate_side_effects (*expr_p);
3610 ret = MIN (r0, MIN (r1, r2));
3614 case NON_LVALUE_EXPR:
3615 /* This should have been stripped above. */
3620 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
3623 case TRY_FINALLY_EXPR:
3624 case TRY_CATCH_EXPR:
3625 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
3626 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
3630 case CLEANUP_POINT_EXPR:
3631 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
3635 ret = gimplify_target_expr (expr_p, pre_p, post_p);
3639 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
3643 case EH_FILTER_EXPR:
3644 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
3650 enum gimplify_status r0, r1;
3651 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
3652 is_gimple_val, fb_rvalue);
3653 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
3654 is_gimple_val, fb_rvalue);
3661 ret = gimplify_minimax_expr (expr_p, pre_p, post_p);
3665 /* We get here when taking the address of a label. We mark
3666 the label as "forced"; meaning it can never be removed and
3667 it is a potential target for any computed goto. */
3668 FORCED_LABEL (*expr_p) = 1;
3672 case STATEMENT_LIST:
3673 ret = gimplify_statement_list (expr_p);
3677 /* ??? If this is a local variable, and it has not been seen in any
3678 outer BIND_EXPR, then it's probably the result of a duplicate
3679 declaration, for which we've already issued an error. It would
3680 be really nice if the front end wouldn't leak these at all.
3681 Currently the only known culprit is C++ destructors, as seen
3682 in g++.old-deja/g++.jason/binding.C. */
3684 if (!TREE_STATIC (tmp) && !DECL_EXTERNAL (tmp)
3685 && decl_function_context (tmp) == current_function_decl
3686 && !tmp->decl.seen_in_bind_expr)
3688 #ifdef ENABLE_CHECKING
3689 if (!errorcount && !sorrycount)
3699 /* If this is a comparison of objects of aggregate type, handle
3700 it specially (by converting to a call to memcmp). It would be
3701 nice to only have to do this for variable-sized objects, but
3702 then we'd have to allow the same nest of reference nodes we
3703 allow for MODIFY_EXPR and that's too complex. */
3704 if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3705 && (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 1)))))
3706 ret = gimplify_variable_sized_compare (expr_p);
3708 /* If *EXPR_P does not need to be special-cased, handle it
3709 according to its class. */
3710 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '1')
3711 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3712 post_p, is_gimple_val, fb_rvalue);
3713 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '2'
3714 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == '<'
3715 || TREE_CODE (*expr_p) == TRUTH_AND_EXPR
3716 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
3717 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR)
3719 enum gimplify_status r0, r1;
3721 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
3722 post_p, is_gimple_val, fb_rvalue);
3723 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
3724 post_p, is_gimple_val, fb_rvalue);
3728 else if (TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'd'
3729 || TREE_CODE_CLASS (TREE_CODE (*expr_p)) == 'c')
3735 /* Fail if we don't know how to handle this tree code. */
3738 recalculate_side_effects (*expr_p);
3742 /* If we replaced *expr_p, gimplify again. */
3743 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
3746 while (ret == GS_OK);
3748 /* If we encountered an error_mark somewhere nested inside, either
3749 stub out the statement or propagate the error back out. */
3750 if (ret == GS_ERROR)
3757 #ifdef ENABLE_CHECKING
3758 /* This was only valid as a return value from the langhook, which
3759 we handled. Make sure it doesn't escape from any other context. */
3760 if (ret == GS_UNHANDLED)
3764 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
3766 /* We aren't looking for a value, and we don't have a valid
3767 statement. If it doesn't have side-effects, throw it away. */
3768 if (!TREE_SIDE_EFFECTS (*expr_p))
3770 else if (!TREE_THIS_VOLATILE (*expr_p))
3772 /* This is probably a _REF that contains something nested that
3773 has side effects. Recurse through the operands to find it. */
3774 enum tree_code code = TREE_CODE (*expr_p);
3776 if (code == COMPONENT_REF
3777 || code == REALPART_EXPR || code == IMAGPART_EXPR)
3778 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3779 gimple_test_f, fallback);
3780 else if (code == ARRAY_REF || code == ARRAY_RANGE_REF)
3782 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3783 gimple_test_f, fallback);
3784 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3785 gimple_test_f, fallback);
3788 /* Anything else with side-effects
3789 must be converted to a valid statement before we get here. */
3794 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p)))
3796 /* Historically, the compiler has treated a bare
3797 reference to a volatile lvalue as forcing a load. */
3798 tree tmp = create_tmp_var (TREE_TYPE (*expr_p), "vol");
3799 *expr_p = build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, *expr_p);
3802 /* We can't do anything useful with a volatile reference to
3803 incomplete type, so just throw it away. */
3807 /* If we are gimplifying at the statement level, we're done. Tack
3808 everything together and replace the original statement with the
3810 if (fallback == fb_none || is_statement)
3812 if (internal_pre || internal_post)
3814 append_to_statement_list (*expr_p, &internal_pre);
3815 append_to_statement_list (internal_post, &internal_pre);
3816 annotate_all_with_locus (&internal_pre, input_location);
3817 *expr_p = internal_pre;
3821 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
3822 annotate_all_with_locus (expr_p, input_location);
3824 annotate_one_with_locus (*expr_p, input_location);
3828 /* Otherwise we're gimplifying a subexpression, so the resulting value is
3831 /* If it's sufficiently simple already, we're done. Unless we are
3832 handling some post-effects internally; if that's the case, we need to
3833 copy into a temp before adding the post-effects to the tree. */
3834 if (!internal_post && (*gimple_test_f) (*expr_p))
3837 /* Otherwise, we need to create a new temporary for the gimplified
3840 /* We can't return an lvalue if we have an internal postqueue. The
3841 object the lvalue refers to would (probably) be modified by the
3842 postqueue; we need to copy the value out first, which means an
3844 if ((fallback & fb_lvalue) && !internal_post
3845 && is_gimple_addr_expr_arg (*expr_p))
3847 /* An lvalue will do. Take the address of the expression, store it
3848 in a temporary, and replace the expression with an INDIRECT_REF of
3850 tmp = build_fold_addr_expr (*expr_p);
3851 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
3852 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
3854 else if ((fallback & fb_rvalue) && is_gimple_rhs (*expr_p))
3856 #if defined ENABLE_CHECKING
3857 if (VOID_TYPE_P (TREE_TYPE (*expr_p)))
3861 /* An rvalue will do. Assign the gimplified expression into a new
3862 temporary TMP and replace the original expression with TMP. */
3864 if (internal_post || (fallback & fb_lvalue))
3865 /* The postqueue might change the value of the expression between
3866 the initialization and use of the temporary, so we can't use a
3867 formal temp. FIXME do we care? */
3868 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
3870 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3872 else if (fallback & fb_mayfail)
3874 /* If this is an asm statement, and the user asked for the impossible,
3875 don't abort. Fail and let gimplify_asm_expr issue an error. */
3881 fprintf (stderr, "gimplification failed:\n");
3882 print_generic_expr (stderr, *expr_p, 0);
3883 debug_tree (*expr_p);
3887 #if defined ENABLE_CHECKING
3888 /* Make sure the temporary matches our predicate. */
3889 if (!(*gimple_test_f) (*expr_p))
3895 annotate_all_with_locus (&internal_post, input_location);
3896 append_to_statement_list (internal_post, pre_p);
3900 input_location = saved_location;
3904 /* Look through TYPE for variable-sized objects and gimplify each such
3905 size that we find. Add to LIST_P any statements generated. */
3908 gimplify_type_sizes (tree type, tree *list_p)
3912 switch (TREE_CODE (type))
3922 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
3923 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
3927 /* These anonymous types don't have declarations, so handle them here. */
3928 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
3933 case QUAL_UNION_TYPE:
3934 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3935 if (TREE_CODE (field) == FIELD_DECL)
3936 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
3943 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
3944 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
3947 /* Subroutine of the above to gimplify one size or position, *EXPR_P.
3948 We add any required statements to STMT_P. */
3951 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
3953 /* We don't do anything if the value isn't there, is constant, or contains
3954 A PLACEHOLDER_EXPR. */
3955 if (*expr_p == NULL_TREE || TREE_CONSTANT (*expr_p)
3956 || CONTAINS_PLACEHOLDER_P (*expr_p))
3959 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
3962 #ifdef ENABLE_CHECKING
3963 /* Compare types A and B for a "close enough" match. */
3966 cpt_same_type (tree a, tree b)
3968 if (lang_hooks.types_compatible_p (a, b))
3971 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
3972 link them together. This routine is intended to catch type errors
3973 that will affect the optimizers, and the optimizers don't add new
3974 dereferences of function pointers, so ignore it. */
3975 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
3976 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
3979 /* ??? The C FE pushes type qualifiers after the fact into the type of
3980 the element from the type of the array. See build_unary_op's handling
3981 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
3982 should have done it when creating the variable in the first place.
3983 Alternately, why aren't the two array types made variants? */
3984 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
3985 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3987 /* And because of those, we have to recurse down through pointers. */
3988 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
3989 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
3994 /* Check for some cases of the front end missing cast expressions.
3995 The type of a dereference should correspond to the pointer type;
3996 similarly the type of an address should match its object. */
3999 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4000 void *data ATTRIBUTE_UNUSED)
4003 tree ptype, otype, dtype;
4005 switch (TREE_CODE (t))
4009 otype = TREE_TYPE (t);
4010 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
4011 dtype = TREE_TYPE (ptype);
4012 if (!cpt_same_type (otype, dtype))
4017 ptype = TREE_TYPE (t);
4018 otype = TREE_TYPE (TREE_OPERAND (t, 0));
4019 dtype = TREE_TYPE (ptype);
4020 if (!cpt_same_type (otype, dtype))
4022 /* &array is allowed to produce a pointer to the element, rather than
4023 a pointer to the array type. We must allow this in order to
4024 properly represent assigning the address of an array in C into
4025 pointer to the element type. */
4026 if (TREE_CODE (otype) == ARRAY_TYPE
4027 && POINTER_TYPE_P (ptype)
4028 && cpt_same_type (TREE_TYPE (otype), dtype))
4043 /* Gimplify the body of statements pointed by BODY_P. FNDECL is the
4044 function decl containing BODY. */
4047 gimplify_body (tree *body_p, tree fndecl)
4049 location_t saved_location = input_location;
4052 timevar_push (TV_TREE_GIMPLIFY);
4053 push_gimplify_context ();
4055 /* Unshare most shared trees in the body and in that of any nested functions.
4056 It would seem we don't have to do this for nested functions because
4057 they are supposed to be output and then the outer function gimplified
4058 first, but the g++ front end doesn't always do it that way. */
4059 unshare_body (body_p, fndecl);
4060 unvisit_body (body_p, fndecl);
4062 /* Make sure input_location isn't set to something wierd. */
4063 input_location = DECL_SOURCE_LOCATION (fndecl);
4065 /* Gimplify the function's body. */
4066 gimplify_stmt (body_p);
4069 /* Unshare again, in case gimplification was sloppy. */
4070 unshare_all_trees (body);
4073 body = alloc_stmt_list ();
4074 else if (TREE_CODE (body) == STATEMENT_LIST)
4076 tree t = expr_only (*body_p);
4081 /* If there isn't an outer BIND_EXPR, add one. */
4082 if (TREE_CODE (body) != BIND_EXPR)
4084 tree b = build (BIND_EXPR, void_type_node, NULL_TREE,
4085 NULL_TREE, NULL_TREE);
4086 TREE_SIDE_EFFECTS (b) = 1;
4087 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
4092 pop_gimplify_context (body);
4094 #ifdef ENABLE_CHECKING
4095 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
4098 timevar_pop (TV_TREE_GIMPLIFY);
4099 input_location = saved_location;
4102 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
4103 node for the function we want to gimplify. */
4106 gimplify_function_tree (tree fndecl)
4110 oldfn = current_function_decl;
4111 current_function_decl = fndecl;
4113 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl);
4115 /* If we're instrumenting function entry/exit, then prepend the call to
4116 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
4117 catch the exit hook. */
4118 /* ??? Add some way to ignore exceptions for this TFE. */
4119 if (flag_instrument_function_entry_exit
4120 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
4124 tf = build (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
4125 TREE_SIDE_EFFECTS (tf) = 1;
4126 x = DECL_SAVED_TREE (fndecl);
4127 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
4128 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
4129 x = build_function_call_expr (x, NULL);
4130 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
4132 bind = build (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4133 TREE_SIDE_EFFECTS (bind) = 1;
4134 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
4135 x = build_function_call_expr (x, NULL);
4136 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
4137 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
4139 DECL_SAVED_TREE (fndecl) = bind;
4142 current_function_decl = oldfn;
4145 #include "gt-gimplify.h"