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, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
31 #include "tree-gimple.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "tree-flow.h"
50 #include "pointer-set.h"
51 #include "splay-tree.h"
54 enum gimplify_omp_var_data
60 GOVD_FIRSTPRIVATE = 16,
61 GOVD_LASTPRIVATE = 32,
64 GOVD_DEBUG_PRIVATE = 256,
65 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
66 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
69 struct gimplify_omp_ctx
71 struct gimplify_omp_ctx *outer_context;
73 struct pointer_set_t *privatized_types;
75 enum omp_clause_default_kind default_kind;
77 bool is_combined_parallel;
82 struct gimplify_ctx *prev_context;
84 tree current_bind_expr;
86 tree conditional_cleanups;
90 VEC(tree,heap) *case_labels;
91 /* The formal temporary table. Should this be persistent? */
99 static struct gimplify_ctx *gimplify_ctxp;
100 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
104 /* Formal (expression) temporary table handling: Multiple occurrences of
105 the same scalar expression are evaluated into the same temporary. */
107 typedef struct gimple_temp_hash_elt
110 tree temp; /* Value */
113 /* Forward declarations. */
114 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
115 #ifdef ENABLE_CHECKING
116 static bool cpt_same_type (tree a, tree b);
119 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
120 form and we don't do any syntax checking. */
122 mark_addressable (tree x)
124 while (handled_component_p (x))
125 x = TREE_OPERAND (x, 0);
126 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
128 TREE_ADDRESSABLE (x) = 1;
131 /* Return a hash value for a formal temporary table entry. */
134 gimple_tree_hash (const void *p)
136 tree t = ((const elt_t *) p)->val;
137 return iterative_hash_expr (t, 0);
140 /* Compare two formal temporary table entries. */
143 gimple_tree_eq (const void *p1, const void *p2)
145 tree t1 = ((const elt_t *) p1)->val;
146 tree t2 = ((const elt_t *) p2)->val;
147 enum tree_code code = TREE_CODE (t1);
149 if (TREE_CODE (t2) != code
150 || TREE_TYPE (t1) != TREE_TYPE (t2))
153 if (!operand_equal_p (t1, t2, 0))
156 /* Only allow them to compare equal if they also hash equal; otherwise
157 results are nondeterminate, and we fail bootstrap comparison. */
158 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
163 /* Set up a context for the gimplifier. */
166 push_gimplify_context (void)
168 struct gimplify_ctx *c;
170 c = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
171 c->prev_context = gimplify_ctxp;
173 c->temp_htab = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
178 /* Tear down a context for the gimplifier. If BODY is non-null, then
179 put the temporaries into the outer BIND_EXPR. Otherwise, put them
180 in the unexpanded_var_list. */
183 pop_gimplify_context (tree body)
185 struct gimplify_ctx *c = gimplify_ctxp;
188 gcc_assert (c && !c->current_bind_expr);
189 gimplify_ctxp = c->prev_context;
191 for (t = c->temps; t ; t = TREE_CHAIN (t))
192 DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
195 declare_vars (c->temps, body, false);
197 record_vars (c->temps);
200 htab_delete (c->temp_htab);
205 gimple_push_bind_expr (tree bind)
207 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
208 gimplify_ctxp->current_bind_expr = bind;
212 gimple_pop_bind_expr (void)
214 gimplify_ctxp->current_bind_expr
215 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
219 gimple_current_bind_expr (void)
221 return gimplify_ctxp->current_bind_expr;
224 /* Returns true iff there is a COND_EXPR between us and the innermost
225 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
228 gimple_conditional_context (void)
230 return gimplify_ctxp->conditions > 0;
233 /* Note that we've entered a COND_EXPR. */
236 gimple_push_condition (void)
238 #ifdef ENABLE_CHECKING
239 if (gimplify_ctxp->conditions == 0)
240 gcc_assert (!gimplify_ctxp->conditional_cleanups);
242 ++(gimplify_ctxp->conditions);
245 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
246 now, add any conditional cleanups we've seen to the prequeue. */
249 gimple_pop_condition (tree *pre_p)
251 int conds = --(gimplify_ctxp->conditions);
253 gcc_assert (conds >= 0);
256 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
257 gimplify_ctxp->conditional_cleanups = NULL_TREE;
261 /* A stable comparison routine for use with splay trees and DECLs. */
264 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
269 return DECL_UID (a) - DECL_UID (b);
272 /* Create a new omp construct that deals with variable remapping. */
274 static struct gimplify_omp_ctx *
275 new_omp_context (bool is_parallel, bool is_combined_parallel)
277 struct gimplify_omp_ctx *c;
279 c = XCNEW (struct gimplify_omp_ctx);
280 c->outer_context = gimplify_omp_ctxp;
281 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
282 c->privatized_types = pointer_set_create ();
283 c->location = input_location;
284 c->is_parallel = is_parallel;
285 c->is_combined_parallel = is_combined_parallel;
286 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
291 /* Destroy an omp construct that deals with variable remapping. */
294 delete_omp_context (struct gimplify_omp_ctx *c)
296 splay_tree_delete (c->variables);
297 pointer_set_destroy (c->privatized_types);
301 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
302 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
304 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
307 append_to_statement_list_1 (tree t, tree *list_p)
310 tree_stmt_iterator i;
314 if (t && TREE_CODE (t) == STATEMENT_LIST)
319 *list_p = list = alloc_stmt_list ();
323 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
326 /* Add T to the end of the list container pointed to by LIST_P.
327 If T is an expression with no effects, it is ignored. */
330 append_to_statement_list (tree t, tree *list_p)
332 if (t && TREE_SIDE_EFFECTS (t))
333 append_to_statement_list_1 (t, list_p);
336 /* Similar, but the statement is always added, regardless of side effects. */
339 append_to_statement_list_force (tree t, tree *list_p)
342 append_to_statement_list_1 (t, list_p);
345 /* Both gimplify the statement T and append it to LIST_P. */
348 gimplify_and_add (tree t, tree *list_p)
351 append_to_statement_list (t, list_p);
354 /* Strip off a legitimate source ending from the input string NAME of
355 length LEN. Rather than having to know the names used by all of
356 our front ends, we strip off an ending of a period followed by
357 up to five characters. (Java uses ".class".) */
360 remove_suffix (char *name, int len)
364 for (i = 2; i < 8 && len > i; i++)
366 if (name[len - i] == '.')
368 name[len - i] = '\0';
374 /* Create a nameless artificial label and put it in the current function
375 context. Returns the newly created label. */
378 create_artificial_label (void)
380 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
382 DECL_ARTIFICIAL (lab) = 1;
383 DECL_IGNORED_P (lab) = 1;
384 DECL_CONTEXT (lab) = current_function_decl;
388 /* Subroutine for find_single_pointer_decl. */
391 find_single_pointer_decl_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
394 tree *pdecl = (tree *) data;
396 if (DECL_P (*tp) && POINTER_TYPE_P (TREE_TYPE (*tp)))
400 /* We already found a pointer decl; return anything other
401 than NULL_TREE to unwind from walk_tree signalling that
402 we have a duplicate. */
411 /* Find the single DECL of pointer type in the tree T and return it.
412 If there are zero or more than one such DECLs, return NULL. */
415 find_single_pointer_decl (tree t)
417 tree decl = NULL_TREE;
419 if (walk_tree (&t, find_single_pointer_decl_1, &decl, NULL))
421 /* find_single_pointer_decl_1 returns a nonzero value, causing
422 walk_tree to return a nonzero value, to indicate that it
423 found more than one pointer DECL. */
430 /* Create a new temporary name with PREFIX. Returns an identifier. */
432 static GTY(()) unsigned int tmp_var_id_num;
435 create_tmp_var_name (const char *prefix)
441 char *preftmp = ASTRDUP (prefix);
443 remove_suffix (preftmp, strlen (preftmp));
447 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
448 return get_identifier (tmp_name);
452 /* Create a new temporary variable declaration of type TYPE.
453 Does NOT push it into the current binding. */
456 create_tmp_var_raw (tree type, const char *prefix)
461 /* Make the type of the variable writable. */
462 new_type = build_type_variant (type, 0, 0);
463 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
465 tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
468 /* The variable was declared by the compiler. */
469 DECL_ARTIFICIAL (tmp_var) = 1;
470 /* And we don't want debug info for it. */
471 DECL_IGNORED_P (tmp_var) = 1;
473 /* Make the variable writable. */
474 TREE_READONLY (tmp_var) = 0;
476 DECL_EXTERNAL (tmp_var) = 0;
477 TREE_STATIC (tmp_var) = 0;
478 TREE_USED (tmp_var) = 1;
483 /* Create a new temporary variable declaration of type TYPE. DOES push the
484 variable into the current binding. Further, assume that this is called
485 only from gimplification or optimization, at which point the creation of
486 certain types are bugs. */
489 create_tmp_var (tree type, const char *prefix)
493 /* We don't allow types that are addressable (meaning we can't make copies),
494 or incomplete. We also used to reject every variable size objects here,
495 but now support those for which a constant upper bound can be obtained.
496 The processing for variable sizes is performed in gimple_add_tmp_var,
497 point at which it really matters and possibly reached via paths not going
498 through this function, e.g. after direct calls to create_tmp_var_raw. */
499 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
501 tmp_var = create_tmp_var_raw (type, prefix);
502 gimple_add_tmp_var (tmp_var);
506 /* Given a tree, try to return a useful variable name that we can use
507 to prefix a temporary that is being assigned the value of the tree.
508 I.E. given <temp> = &A, return A. */
516 STRIP_NOPS (stripped_decl);
517 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
518 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
521 switch (TREE_CODE (stripped_decl))
524 return get_name (TREE_OPERAND (stripped_decl, 0));
531 /* Create a temporary with a name derived from VAL. Subroutine of
532 lookup_tmp_var; nobody else should call this function. */
535 create_tmp_from_val (tree val)
537 return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
540 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
541 an existing expression temporary. */
544 lookup_tmp_var (tree val, bool is_formal)
548 /* If not optimizing, never really reuse a temporary. local-alloc
549 won't allocate any variable that is used in more than one basic
550 block, which means it will go into memory, causing much extra
551 work in reload and final and poorer code generation, outweighing
552 the extra memory allocation here. */
553 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
554 ret = create_tmp_from_val (val);
561 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
564 elt_p = XNEW (elt_t);
566 elt_p->temp = ret = create_tmp_from_val (val);
567 *slot = (void *) elt_p;
571 elt_p = (elt_t *) *slot;
577 DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
582 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
583 in gimplify_expr. Only use this function if:
585 1) The value of the unfactored expression represented by VAL will not
586 change between the initialization and use of the temporary, and
587 2) The temporary will not be otherwise modified.
589 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
590 and #2 means it is inappropriate for && temps.
592 For other cases, use get_initialized_tmp_var instead. */
595 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
599 gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
601 t = lookup_tmp_var (val, is_formal);
605 tree u = find_single_pointer_decl (val);
607 if (u && TREE_CODE (u) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (u))
608 u = DECL_GET_RESTRICT_BASE (u);
609 if (u && TYPE_RESTRICT (TREE_TYPE (u)))
611 if (DECL_BASED_ON_RESTRICT_P (t))
612 gcc_assert (u == DECL_GET_RESTRICT_BASE (t));
615 DECL_BASED_ON_RESTRICT_P (t) = 1;
616 SET_DECL_RESTRICT_BASE (t, u);
621 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
622 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
623 DECL_GIMPLE_REG_P (t) = 1;
625 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
627 if (EXPR_HAS_LOCATION (val))
628 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
630 SET_EXPR_LOCATION (mod, input_location);
632 /* gimplify_modify_expr might want to reduce this further. */
633 gimplify_and_add (mod, pre_p);
635 /* If we're gimplifying into ssa, gimplify_modify_expr will have
636 given our temporary an ssa name. Find and return it. */
637 if (gimplify_ctxp->into_ssa)
638 t = TREE_OPERAND (mod, 0);
643 /* Returns a formal temporary variable initialized with VAL. PRE_P
644 points to a statement list where side-effects needed to compute VAL
648 get_formal_tmp_var (tree val, tree *pre_p)
650 return internal_get_tmp_var (val, pre_p, NULL, true);
653 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
654 are as in gimplify_expr. */
657 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
659 return internal_get_tmp_var (val, pre_p, post_p, false);
662 /* Declares all the variables in VARS in SCOPE. If DEBUG_INFO is
663 true, generate debug info for them; otherwise don't. */
666 declare_vars (tree vars, tree scope, bool debug_info)
673 /* C99 mode puts the default 'return 0;' for main outside the outer
674 braces. So drill down until we find an actual scope. */
675 while (TREE_CODE (scope) == COMPOUND_EXPR)
676 scope = TREE_OPERAND (scope, 0);
678 gcc_assert (TREE_CODE (scope) == BIND_EXPR);
680 temps = nreverse (last);
682 block = BIND_EXPR_BLOCK (scope);
683 if (!block || !debug_info)
685 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
686 BIND_EXPR_VARS (scope) = temps;
690 /* We need to attach the nodes both to the BIND_EXPR and to its
691 associated BLOCK for debugging purposes. The key point here
692 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
693 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
694 if (BLOCK_VARS (block))
695 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
698 BIND_EXPR_VARS (scope) = chainon (BIND_EXPR_VARS (scope), temps);
699 BLOCK_VARS (block) = temps;
705 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
706 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
707 no such upper bound can be obtained. */
710 force_constant_size (tree var)
712 /* The only attempt we make is by querying the maximum size of objects
713 of the variable's type. */
715 HOST_WIDE_INT max_size;
717 gcc_assert (TREE_CODE (var) == VAR_DECL);
719 max_size = max_int_size_in_bytes (TREE_TYPE (var));
721 gcc_assert (max_size >= 0);
724 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
726 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
730 gimple_add_tmp_var (tree tmp)
732 gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
734 /* Later processing assumes that the object size is constant, which might
735 not be true at this point. Force the use of a constant upper bound in
737 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
738 force_constant_size (tmp);
740 DECL_CONTEXT (tmp) = current_function_decl;
741 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
745 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
746 gimplify_ctxp->temps = tmp;
748 /* Mark temporaries local within the nearest enclosing parallel. */
749 if (gimplify_omp_ctxp)
751 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
752 while (ctx && !ctx->is_parallel)
753 ctx = ctx->outer_context;
755 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
761 declare_vars (tmp, DECL_SAVED_TREE (current_function_decl), false);
764 /* Determines whether to assign a locus to the statement STMT. */
767 should_carry_locus_p (tree stmt)
769 /* Don't emit a line note for a label. We particularly don't want to
770 emit one for the break label, since it doesn't actually correspond
771 to the beginning of the loop/switch. */
772 if (TREE_CODE (stmt) == LABEL_EXPR)
775 /* Do not annotate empty statements, since it confuses gcov. */
776 if (!TREE_SIDE_EFFECTS (stmt))
783 annotate_one_with_locus (tree t, location_t locus)
785 if (CAN_HAVE_LOCATION_P (t)
786 && ! EXPR_HAS_LOCATION (t) && should_carry_locus_p (t))
787 SET_EXPR_LOCATION (t, locus);
791 annotate_all_with_locus (tree *stmt_p, location_t locus)
793 tree_stmt_iterator i;
798 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
800 tree t = tsi_stmt (i);
802 /* Assuming we've already been gimplified, we shouldn't
803 see nested chaining constructs anymore. */
804 gcc_assert (TREE_CODE (t) != STATEMENT_LIST
805 && TREE_CODE (t) != COMPOUND_EXPR);
807 annotate_one_with_locus (t, locus);
811 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
812 These nodes model computations that should only be done once. If we
813 were to unshare something like SAVE_EXPR(i++), the gimplification
814 process would create wrong code. */
817 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
819 enum tree_code code = TREE_CODE (*tp);
820 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
821 if (TREE_CODE_CLASS (code) == tcc_type
822 || TREE_CODE_CLASS (code) == tcc_declaration
823 || TREE_CODE_CLASS (code) == tcc_constant
824 || code == SAVE_EXPR || code == TARGET_EXPR
825 /* We can't do anything sensible with a BLOCK used as an expression,
826 but we also can't just die when we see it because of non-expression
827 uses. So just avert our eyes and cross our fingers. Silly Java. */
832 gcc_assert (code != BIND_EXPR);
833 copy_tree_r (tp, walk_subtrees, data);
839 /* Callback for walk_tree to unshare most of the shared trees rooted at
840 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
841 then *TP is deep copied by calling copy_tree_r.
843 This unshares the same trees as copy_tree_r with the exception of
844 SAVE_EXPR nodes. These nodes model computations that should only be
845 done once. If we were to unshare something like SAVE_EXPR(i++), the
846 gimplification process would create wrong code. */
849 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
850 void *data ATTRIBUTE_UNUSED)
853 enum tree_code code = TREE_CODE (t);
855 /* Skip types, decls, and constants. But we do want to look at their
856 types and the bounds of types. Mark them as visited so we properly
857 unmark their subtrees on the unmark pass. If we've already seen them,
858 don't look down further. */
859 if (TREE_CODE_CLASS (code) == tcc_type
860 || TREE_CODE_CLASS (code) == tcc_declaration
861 || TREE_CODE_CLASS (code) == tcc_constant)
863 if (TREE_VISITED (t))
866 TREE_VISITED (t) = 1;
869 /* If this node has been visited already, unshare it and don't look
871 else if (TREE_VISITED (t))
873 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
877 /* Otherwise, mark the tree as visited and keep looking. */
879 TREE_VISITED (t) = 1;
885 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
886 void *data ATTRIBUTE_UNUSED)
888 if (TREE_VISITED (*tp))
889 TREE_VISITED (*tp) = 0;
896 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
897 bodies of any nested functions if we are unsharing the entire body of
901 unshare_body (tree *body_p, tree fndecl)
903 struct cgraph_node *cgn = cgraph_node (fndecl);
905 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
906 if (body_p == &DECL_SAVED_TREE (fndecl))
907 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
908 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
911 /* Likewise, but mark all trees as not visited. */
914 unvisit_body (tree *body_p, tree fndecl)
916 struct cgraph_node *cgn = cgraph_node (fndecl);
918 walk_tree (body_p, unmark_visited_r, NULL, NULL);
919 if (body_p == &DECL_SAVED_TREE (fndecl))
920 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
921 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
924 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
927 unshare_all_trees (tree t)
929 walk_tree (&t, copy_if_shared_r, NULL, NULL);
930 walk_tree (&t, unmark_visited_r, NULL, NULL);
933 /* Unconditionally make an unshared copy of EXPR. This is used when using
934 stored expressions which span multiple functions, such as BINFO_VTABLE,
935 as the normal unsharing process can't tell that they're shared. */
938 unshare_expr (tree expr)
940 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
944 /* A terser interface for building a representation of an exception
948 gimple_build_eh_filter (tree body, tree allowed, tree failure)
952 /* FIXME should the allowed types go in TREE_TYPE? */
953 t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
954 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
956 t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
957 append_to_statement_list (body, &TREE_OPERAND (t, 0));
963 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
964 contain statements and have a value. Assign its value to a temporary
965 and give it void_type_node. Returns the temporary, or NULL_TREE if
966 WRAPPER was already void. */
969 voidify_wrapper_expr (tree wrapper, tree temp)
971 tree type = TREE_TYPE (wrapper);
972 if (type && !VOID_TYPE_P (type))
976 /* Set p to point to the body of the wrapper. Loop until we find
977 something that isn't a wrapper. */
978 for (p = &wrapper; p && *p; )
980 switch (TREE_CODE (*p))
983 TREE_SIDE_EFFECTS (*p) = 1;
984 TREE_TYPE (*p) = void_type_node;
985 /* For a BIND_EXPR, the body is operand 1. */
986 p = &BIND_EXPR_BODY (*p);
989 case CLEANUP_POINT_EXPR:
990 case TRY_FINALLY_EXPR:
992 TREE_SIDE_EFFECTS (*p) = 1;
993 TREE_TYPE (*p) = void_type_node;
994 p = &TREE_OPERAND (*p, 0);
999 tree_stmt_iterator i = tsi_last (*p);
1000 TREE_SIDE_EFFECTS (*p) = 1;
1001 TREE_TYPE (*p) = void_type_node;
1002 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1007 /* Advance to the last statement. Set all container types to void. */
1008 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1010 TREE_SIDE_EFFECTS (*p) = 1;
1011 TREE_TYPE (*p) = void_type_node;
1021 if (p == NULL || IS_EMPTY_STMT (*p))
1025 /* The wrapper is on the RHS of an assignment that we're pushing
1027 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1028 || TREE_CODE (temp) == GIMPLE_MODIFY_STMT
1029 || TREE_CODE (temp) == MODIFY_EXPR);
1030 GENERIC_TREE_OPERAND (temp, 1) = *p;
1035 temp = create_tmp_var (type, "retval");
1036 *p = build2 (INIT_EXPR, type, temp, *p);
1045 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1046 a temporary through which they communicate. */
1049 build_stack_save_restore (tree *save, tree *restore)
1051 tree save_call, tmp_var;
1054 build_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE], 0);
1055 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1057 *save = build_gimple_modify_stmt (tmp_var, save_call);
1059 build_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
1063 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1065 static enum gimplify_status
1066 gimplify_bind_expr (tree *expr_p, tree *pre_p)
1068 tree bind_expr = *expr_p;
1069 bool old_save_stack = gimplify_ctxp->save_stack;
1072 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1074 /* Mark variables seen in this bind expr. */
1075 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1077 if (TREE_CODE (t) == VAR_DECL)
1079 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1081 /* Mark variable as local. */
1082 if (ctx && !is_global_var (t)
1083 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1084 || splay_tree_lookup (ctx->variables,
1085 (splay_tree_key) t) == NULL))
1086 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1088 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1091 /* Preliminarily mark non-addressed complex variables as eligible
1092 for promotion to gimple registers. We'll transform their uses
1094 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1095 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1096 && !TREE_THIS_VOLATILE (t)
1097 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1098 && !needs_to_live_in_memory (t))
1099 DECL_GIMPLE_REG_P (t) = 1;
1102 gimple_push_bind_expr (bind_expr);
1103 gimplify_ctxp->save_stack = false;
1105 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
1107 if (gimplify_ctxp->save_stack)
1109 tree stack_save, stack_restore;
1111 /* Save stack on entry and restore it on exit. Add a try_finally
1112 block to achieve this. Note that mudflap depends on the
1113 format of the emitted code: see mx_register_decls(). */
1114 build_stack_save_restore (&stack_save, &stack_restore);
1116 t = build2 (TRY_FINALLY_EXPR, void_type_node,
1117 BIND_EXPR_BODY (bind_expr), NULL_TREE);
1118 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
1120 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
1121 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
1122 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
1125 gimplify_ctxp->save_stack = old_save_stack;
1126 gimple_pop_bind_expr ();
1131 append_to_statement_list (bind_expr, pre_p);
1138 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1139 GIMPLE value, it is assigned to a new temporary and the statement is
1140 re-written to return the temporary.
1142 PRE_P points to the list where side effects that must happen before
1143 STMT should be stored. */
1145 static enum gimplify_status
1146 gimplify_return_expr (tree stmt, tree *pre_p)
1148 tree ret_expr = TREE_OPERAND (stmt, 0);
1149 tree result_decl, result;
1151 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
1152 || ret_expr == error_mark_node)
1155 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1156 result_decl = NULL_TREE;
1159 result_decl = GENERIC_TREE_OPERAND (ret_expr, 0);
1160 if (TREE_CODE (result_decl) == INDIRECT_REF)
1161 /* See through a return by reference. */
1162 result_decl = TREE_OPERAND (result_decl, 0);
1164 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1165 || TREE_CODE (ret_expr) == GIMPLE_MODIFY_STMT
1166 || TREE_CODE (ret_expr) == INIT_EXPR)
1167 && TREE_CODE (result_decl) == RESULT_DECL);
1170 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1171 Recall that aggregate_value_p is FALSE for any aggregate type that is
1172 returned in registers. If we're returning values in registers, then
1173 we don't want to extend the lifetime of the RESULT_DECL, particularly
1174 across another call. In addition, for those aggregates for which
1175 hard_function_value generates a PARALLEL, we'll die during normal
1176 expansion of structure assignments; there's special code in expand_return
1177 to handle this case that does not exist in expand_expr. */
1179 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1180 result = result_decl;
1181 else if (gimplify_ctxp->return_temp)
1182 result = gimplify_ctxp->return_temp;
1185 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
1186 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1187 || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1188 DECL_GIMPLE_REG_P (result) = 1;
1190 /* ??? With complex control flow (usually involving abnormal edges),
1191 we can wind up warning about an uninitialized value for this. Due
1192 to how this variable is constructed and initialized, this is never
1193 true. Give up and never warn. */
1194 TREE_NO_WARNING (result) = 1;
1196 gimplify_ctxp->return_temp = result;
1199 /* Smash the lhs of the GIMPLE_MODIFY_STMT to the temporary we plan to use.
1200 Then gimplify the whole thing. */
1201 if (result != result_decl)
1202 GENERIC_TREE_OPERAND (ret_expr, 0) = result;
1204 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1206 /* If we didn't use a temporary, then the result is just the result_decl.
1207 Otherwise we need a simple copy. This should already be gimple. */
1208 if (result == result_decl)
1211 ret_expr = build_gimple_modify_stmt (result_decl, result);
1212 TREE_OPERAND (stmt, 0) = ret_expr;
1217 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1218 and initialization explicit. */
1220 static enum gimplify_status
1221 gimplify_decl_expr (tree *stmt_p)
1223 tree stmt = *stmt_p;
1224 tree decl = DECL_EXPR_DECL (stmt);
1226 *stmt_p = NULL_TREE;
1228 if (TREE_TYPE (decl) == error_mark_node)
1231 if ((TREE_CODE (decl) == TYPE_DECL
1232 || TREE_CODE (decl) == VAR_DECL)
1233 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1234 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1236 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1238 tree init = DECL_INITIAL (decl);
1240 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1242 /* This is a variable-sized decl. Simplify its size and mark it
1243 for deferred expansion. Note that mudflap depends on the format
1244 of the emitted code: see mx_register_decls(). */
1245 tree t, addr, ptr_type;
1247 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
1248 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
1250 /* All occurrences of this decl in final gimplified code will be
1251 replaced by indirection. Setting DECL_VALUE_EXPR does two
1252 things: First, it lets the rest of the gimplifier know what
1253 replacement to use. Second, it lets the debug info know
1254 where to find the value. */
1255 ptr_type = build_pointer_type (TREE_TYPE (decl));
1256 addr = create_tmp_var (ptr_type, get_name (decl));
1257 DECL_IGNORED_P (addr) = 0;
1258 t = build_fold_indirect_ref (addr);
1259 SET_DECL_VALUE_EXPR (decl, t);
1260 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1262 t = built_in_decls[BUILT_IN_ALLOCA];
1263 t = build_call_expr (t, 1, DECL_SIZE_UNIT (decl));
1264 t = fold_convert (ptr_type, t);
1265 t = build_gimple_modify_stmt (addr, t);
1267 gimplify_and_add (t, stmt_p);
1269 /* Indicate that we need to restore the stack level when the
1270 enclosing BIND_EXPR is exited. */
1271 gimplify_ctxp->save_stack = true;
1274 if (init && init != error_mark_node)
1276 if (!TREE_STATIC (decl))
1278 DECL_INITIAL (decl) = NULL_TREE;
1279 init = build2 (INIT_EXPR, void_type_node, decl, init);
1280 gimplify_and_add (init, stmt_p);
1283 /* We must still examine initializers for static variables
1284 as they may contain a label address. */
1285 walk_tree (&init, force_labels_r, NULL, NULL);
1288 /* Some front ends do not explicitly declare all anonymous
1289 artificial variables. We compensate here by declaring the
1290 variables, though it would be better if the front ends would
1291 explicitly declare them. */
1292 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1293 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1294 gimple_add_tmp_var (decl);
1300 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1301 and replacing the LOOP_EXPR with goto, but if the loop contains an
1302 EXIT_EXPR, we need to append a label for it to jump to. */
1304 static enum gimplify_status
1305 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1307 tree saved_label = gimplify_ctxp->exit_label;
1308 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1309 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1311 append_to_statement_list (start_label, pre_p);
1313 gimplify_ctxp->exit_label = NULL_TREE;
1315 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1317 if (gimplify_ctxp->exit_label)
1319 append_to_statement_list (jump_stmt, pre_p);
1320 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1323 *expr_p = jump_stmt;
1325 gimplify_ctxp->exit_label = saved_label;
1330 /* Compare two case labels. Because the front end should already have
1331 made sure that case ranges do not overlap, it is enough to only compare
1332 the CASE_LOW values of each case label. */
1335 compare_case_labels (const void *p1, const void *p2)
1337 const_tree const case1 = *(const_tree const*)p1;
1338 const_tree const case2 = *(const_tree const*)p2;
1340 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1343 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1346 sort_case_labels (tree label_vec)
1348 size_t len = TREE_VEC_LENGTH (label_vec);
1349 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1351 if (CASE_LOW (default_case))
1355 /* The last label in the vector should be the default case
1357 for (i = 0; i < len; ++i)
1359 tree t = TREE_VEC_ELT (label_vec, i);
1363 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1364 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1370 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1371 compare_case_labels);
1374 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1377 static enum gimplify_status
1378 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1380 tree switch_expr = *expr_p;
1381 enum gimplify_status ret;
1383 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1384 is_gimple_val, fb_rvalue);
1386 if (SWITCH_BODY (switch_expr))
1388 VEC(tree,heap) *labels, *saved_labels;
1389 tree label_vec, default_case = NULL_TREE;
1392 /* If someone can be bothered to fill in the labels, they can
1393 be bothered to null out the body too. */
1394 gcc_assert (!SWITCH_LABELS (switch_expr));
1396 saved_labels = gimplify_ctxp->case_labels;
1397 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1399 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1401 labels = gimplify_ctxp->case_labels;
1402 gimplify_ctxp->case_labels = saved_labels;
1405 while (i < VEC_length (tree, labels))
1407 tree elt = VEC_index (tree, labels, i);
1408 tree low = CASE_LOW (elt);
1409 bool remove_element = FALSE;
1413 /* Discard empty ranges. */
1414 tree high = CASE_HIGH (elt);
1415 if (high && INT_CST_LT (high, low))
1416 remove_element = TRUE;
1420 /* The default case must be the last label in the list. */
1421 gcc_assert (!default_case);
1423 remove_element = TRUE;
1427 VEC_ordered_remove (tree, labels, i);
1433 label_vec = make_tree_vec (len + 1);
1434 SWITCH_LABELS (*expr_p) = label_vec;
1435 append_to_statement_list (switch_expr, pre_p);
1439 /* If the switch has no default label, add one, so that we jump
1440 around the switch body. */
1441 default_case = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1442 NULL_TREE, create_artificial_label ());
1443 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1444 *expr_p = build1 (LABEL_EXPR, void_type_node,
1445 CASE_LABEL (default_case));
1448 *expr_p = SWITCH_BODY (switch_expr);
1450 for (i = 0; i < len; ++i)
1451 TREE_VEC_ELT (label_vec, i) = VEC_index (tree, labels, i);
1452 TREE_VEC_ELT (label_vec, len) = default_case;
1454 VEC_free (tree, heap, labels);
1456 sort_case_labels (label_vec);
1458 SWITCH_BODY (switch_expr) = NULL;
1461 gcc_assert (SWITCH_LABELS (switch_expr));
1466 static enum gimplify_status
1467 gimplify_case_label_expr (tree *expr_p)
1469 tree expr = *expr_p;
1470 struct gimplify_ctx *ctxp;
1472 /* Invalid OpenMP programs can play Duff's Device type games with
1473 #pragma omp parallel. At least in the C front end, we don't
1474 detect such invalid branches until after gimplification. */
1475 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1476 if (ctxp->case_labels)
1479 VEC_safe_push (tree, heap, ctxp->case_labels, expr);
1480 *expr_p = build1 (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1484 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1488 build_and_jump (tree *label_p)
1490 if (label_p == NULL)
1491 /* If there's nowhere to jump, just fall through. */
1494 if (*label_p == NULL_TREE)
1496 tree label = create_artificial_label ();
1500 return build1 (GOTO_EXPR, void_type_node, *label_p);
1503 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1504 This also involves building a label to jump to and communicating it to
1505 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1507 static enum gimplify_status
1508 gimplify_exit_expr (tree *expr_p)
1510 tree cond = TREE_OPERAND (*expr_p, 0);
1513 expr = build_and_jump (&gimplify_ctxp->exit_label);
1514 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1520 /* A helper function to be called via walk_tree. Mark all labels under *TP
1521 as being forced. To be called for DECL_INITIAL of static variables. */
1524 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1528 if (TREE_CODE (*tp) == LABEL_DECL)
1529 FORCED_LABEL (*tp) = 1;
1534 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1535 different from its canonical type, wrap the whole thing inside a
1536 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1539 The canonical type of a COMPONENT_REF is the type of the field being
1540 referenced--unless the field is a bit-field which can be read directly
1541 in a smaller mode, in which case the canonical type is the
1542 sign-appropriate type corresponding to that mode. */
1545 canonicalize_component_ref (tree *expr_p)
1547 tree expr = *expr_p;
1550 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1552 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1553 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1555 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1557 if (TREE_TYPE (expr) != type)
1559 tree old_type = TREE_TYPE (expr);
1561 /* Set the type of the COMPONENT_REF to the underlying type. */
1562 TREE_TYPE (expr) = type;
1564 /* And wrap the whole thing inside a NOP_EXPR. */
1565 expr = build1 (NOP_EXPR, old_type, expr);
1571 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1572 to foo, embed that change in the ADDR_EXPR by converting
1577 where L is the lower bound. For simplicity, only do this for constant
1581 canonicalize_addr_expr (tree *expr_p)
1583 tree expr = *expr_p;
1584 tree ctype = TREE_TYPE (expr);
1585 tree addr_expr = TREE_OPERAND (expr, 0);
1586 tree atype = TREE_TYPE (addr_expr);
1587 tree dctype, datype, ddatype, otype, obj_expr;
1589 /* Both cast and addr_expr types should be pointers. */
1590 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1593 /* The addr_expr type should be a pointer to an array. */
1594 datype = TREE_TYPE (atype);
1595 if (TREE_CODE (datype) != ARRAY_TYPE)
1598 /* Both cast and addr_expr types should address the same object type. */
1599 dctype = TREE_TYPE (ctype);
1600 ddatype = TREE_TYPE (datype);
1601 if (!useless_type_conversion_p (dctype, ddatype))
1604 /* The addr_expr and the object type should match. */
1605 obj_expr = TREE_OPERAND (addr_expr, 0);
1606 otype = TREE_TYPE (obj_expr);
1607 if (!useless_type_conversion_p (datype, otype))
1610 /* The lower bound and element sizes must be constant. */
1611 if (!TYPE_SIZE_UNIT (dctype)
1612 || TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1613 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1614 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1617 /* All checks succeeded. Build a new node to merge the cast. */
1618 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1619 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1620 NULL_TREE, NULL_TREE);
1621 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1624 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1625 underneath as appropriate. */
1627 static enum gimplify_status
1628 gimplify_conversion (tree *expr_p)
1631 gcc_assert (TREE_CODE (*expr_p) == NOP_EXPR
1632 || TREE_CODE (*expr_p) == CONVERT_EXPR);
1634 /* Then strip away all but the outermost conversion. */
1635 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1637 /* And remove the outermost conversion if it's useless. */
1638 if (tree_ssa_useless_type_conversion (*expr_p))
1639 *expr_p = TREE_OPERAND (*expr_p, 0);
1641 /* Attempt to avoid NOP_EXPR by producing reference to a subtype.
1642 For example this fold (subclass *)&A into &A->subclass avoiding
1643 a need for statement. */
1644 if (TREE_CODE (*expr_p) == NOP_EXPR
1645 && POINTER_TYPE_P (TREE_TYPE (*expr_p))
1646 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (*expr_p, 0)))
1647 && (tem = maybe_fold_offset_to_reference
1648 (TREE_OPERAND (*expr_p, 0),
1649 integer_zero_node, TREE_TYPE (TREE_TYPE (*expr_p)))))
1651 tree ptr_type = build_pointer_type (TREE_TYPE (tem));
1652 if (useless_type_conversion_p (TREE_TYPE (*expr_p), ptr_type))
1653 *expr_p = build_fold_addr_expr_with_type (tem, ptr_type);
1656 /* If we still have a conversion at the toplevel,
1657 then canonicalize some constructs. */
1658 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1660 tree sub = TREE_OPERAND (*expr_p, 0);
1662 /* If a NOP conversion is changing the type of a COMPONENT_REF
1663 expression, then canonicalize its type now in order to expose more
1664 redundant conversions. */
1665 if (TREE_CODE (sub) == COMPONENT_REF)
1666 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1668 /* If a NOP conversion is changing a pointer to array of foo
1669 to a pointer to foo, embed that change in the ADDR_EXPR. */
1670 else if (TREE_CODE (sub) == ADDR_EXPR)
1671 canonicalize_addr_expr (expr_p);
1677 /* Gimplify a VAR_DECL or PARM_DECL. Returns GS_OK if we expanded a
1678 DECL_VALUE_EXPR, and it's worth re-examining things. */
1680 static enum gimplify_status
1681 gimplify_var_or_parm_decl (tree *expr_p)
1683 tree decl = *expr_p;
1685 /* ??? If this is a local variable, and it has not been seen in any
1686 outer BIND_EXPR, then it's probably the result of a duplicate
1687 declaration, for which we've already issued an error. It would
1688 be really nice if the front end wouldn't leak these at all.
1689 Currently the only known culprit is C++ destructors, as seen
1690 in g++.old-deja/g++.jason/binding.C. */
1691 if (TREE_CODE (decl) == VAR_DECL
1692 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1693 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1694 && decl_function_context (decl) == current_function_decl)
1696 gcc_assert (errorcount || sorrycount);
1700 /* When within an OpenMP context, notice uses of variables. */
1701 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1704 /* If the decl is an alias for another expression, substitute it now. */
1705 if (DECL_HAS_VALUE_EXPR_P (decl))
1707 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
1715 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1716 node pointed to by EXPR_P.
1719 : min_lval '[' val ']'
1721 | compound_lval '[' val ']'
1722 | compound_lval '.' ID
1724 This is not part of the original SIMPLE definition, which separates
1725 array and member references, but it seems reasonable to handle them
1726 together. Also, this way we don't run into problems with union
1727 aliasing; gcc requires that for accesses through a union to alias, the
1728 union reference must be explicit, which was not always the case when we
1729 were splitting up array and member refs.
1731 PRE_P points to the list where side effects that must happen before
1732 *EXPR_P should be stored.
1734 POST_P points to the list where side effects that must happen after
1735 *EXPR_P should be stored. */
1737 static enum gimplify_status
1738 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1739 tree *post_p, fallback_t fallback)
1742 VEC(tree,heap) *stack;
1743 enum gimplify_status ret = GS_OK, tret;
1746 /* Create a stack of the subexpressions so later we can walk them in
1747 order from inner to outer. */
1748 stack = VEC_alloc (tree, heap, 10);
1750 /* We can handle anything that get_inner_reference can deal with. */
1751 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1754 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1755 if (TREE_CODE (*p) == INDIRECT_REF)
1756 *p = fold_indirect_ref (*p);
1758 if (handled_component_p (*p))
1760 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1761 additional COMPONENT_REFs. */
1762 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1763 && gimplify_var_or_parm_decl (p) == GS_OK)
1768 VEC_safe_push (tree, heap, stack, *p);
1771 gcc_assert (VEC_length (tree, stack));
1773 /* Now STACK is a stack of pointers to all the refs we've walked through
1774 and P points to the innermost expression.
1776 Java requires that we elaborated nodes in source order. That
1777 means we must gimplify the inner expression followed by each of
1778 the indices, in order. But we can't gimplify the inner
1779 expression until we deal with any variable bounds, sizes, or
1780 positions in order to deal with PLACEHOLDER_EXPRs.
1782 So we do this in three steps. First we deal with the annotations
1783 for any variables in the components, then we gimplify the base,
1784 then we gimplify any indices, from left to right. */
1785 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
1787 tree t = VEC_index (tree, stack, i);
1789 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1791 /* Gimplify the low bound and element type size and put them into
1792 the ARRAY_REF. If these values are set, they have already been
1794 if (!TREE_OPERAND (t, 2))
1796 tree low = unshare_expr (array_ref_low_bound (t));
1797 if (!is_gimple_min_invariant (low))
1799 TREE_OPERAND (t, 2) = low;
1800 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1801 is_gimple_formal_tmp_reg, fb_rvalue);
1802 ret = MIN (ret, tret);
1806 if (!TREE_OPERAND (t, 3))
1808 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1809 tree elmt_size = unshare_expr (array_ref_element_size (t));
1810 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1812 /* Divide the element size by the alignment of the element
1814 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1816 if (!is_gimple_min_invariant (elmt_size))
1818 TREE_OPERAND (t, 3) = elmt_size;
1819 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1820 is_gimple_formal_tmp_reg, fb_rvalue);
1821 ret = MIN (ret, tret);
1825 else if (TREE_CODE (t) == COMPONENT_REF)
1827 /* Set the field offset into T and gimplify it. */
1828 if (!TREE_OPERAND (t, 2))
1830 tree offset = unshare_expr (component_ref_field_offset (t));
1831 tree field = TREE_OPERAND (t, 1);
1833 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1835 /* Divide the offset by its alignment. */
1836 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1838 if (!is_gimple_min_invariant (offset))
1840 TREE_OPERAND (t, 2) = offset;
1841 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1842 is_gimple_formal_tmp_reg, fb_rvalue);
1843 ret = MIN (ret, tret);
1849 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1850 so as to match the min_lval predicate. Failure to do so may result
1851 in the creation of large aggregate temporaries. */
1852 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1853 fallback | fb_lvalue);
1854 ret = MIN (ret, tret);
1856 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1857 loop we also remove any useless conversions. */
1858 for (; VEC_length (tree, stack) > 0; )
1860 tree t = VEC_pop (tree, stack);
1862 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1864 /* Gimplify the dimension.
1865 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1866 Gimplify non-constant array indices into a temporary
1868 FIXME - The real fix is to gimplify post-modify
1869 expressions into a minimal gimple lvalue. However, that
1870 exposes bugs in alias analysis. The alias analyzer does
1871 not handle &PTR->FIELD very well. Will fix after the
1872 branch is merged into mainline (dnovillo 2004-05-03). */
1873 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1875 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1876 is_gimple_formal_tmp_reg, fb_rvalue);
1877 ret = MIN (ret, tret);
1880 else if (TREE_CODE (t) == BIT_FIELD_REF)
1882 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1883 is_gimple_val, fb_rvalue);
1884 ret = MIN (ret, tret);
1885 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1886 is_gimple_val, fb_rvalue);
1887 ret = MIN (ret, tret);
1890 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1892 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1893 set which would have caused all the outer expressions in EXPR_P
1894 leading to P to also have had TREE_SIDE_EFFECTS set. */
1895 recalculate_side_effects (t);
1898 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1899 ret = MIN (ret, tret);
1901 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1902 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1904 canonicalize_component_ref (expr_p);
1905 ret = MIN (ret, GS_OK);
1908 VEC_free (tree, heap, stack);
1913 /* Gimplify the self modifying expression pointed to by EXPR_P
1916 PRE_P points to the list where side effects that must happen before
1917 *EXPR_P should be stored.
1919 POST_P points to the list where side effects that must happen after
1920 *EXPR_P should be stored.
1922 WANT_VALUE is nonzero iff we want to use the value of this expression
1923 in another expression. */
1925 static enum gimplify_status
1926 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1929 enum tree_code code;
1930 tree lhs, lvalue, rhs, t1, post = NULL, *orig_post_p = post_p;
1932 enum tree_code arith_code;
1933 enum gimplify_status ret;
1935 code = TREE_CODE (*expr_p);
1937 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1938 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1940 /* Prefix or postfix? */
1941 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1942 /* Faster to treat as prefix if result is not used. */
1943 postfix = want_value;
1947 /* For postfix, make sure the inner expression's post side effects
1948 are executed after side effects from this expression. */
1952 /* Add or subtract? */
1953 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1954 arith_code = PLUS_EXPR;
1956 arith_code = MINUS_EXPR;
1958 /* Gimplify the LHS into a GIMPLE lvalue. */
1959 lvalue = TREE_OPERAND (*expr_p, 0);
1960 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1961 if (ret == GS_ERROR)
1964 /* Extract the operands to the arithmetic operation. */
1966 rhs = TREE_OPERAND (*expr_p, 1);
1968 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1969 that as the result value and in the postqueue operation. */
1972 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1973 if (ret == GS_ERROR)
1977 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
1978 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
1980 rhs = fold_convert (sizetype, rhs);
1981 if (arith_code == MINUS_EXPR)
1982 rhs = fold_build1 (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
1983 arith_code = POINTER_PLUS_EXPR;
1986 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1987 t1 = build_gimple_modify_stmt (lvalue, t1);
1991 gimplify_and_add (t1, orig_post_p);
1992 append_to_statement_list (post, orig_post_p);
2003 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2006 maybe_with_size_expr (tree *expr_p)
2008 tree expr = *expr_p;
2009 tree type = TREE_TYPE (expr);
2012 /* If we've already wrapped this or the type is error_mark_node, we can't do
2014 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2015 || type == error_mark_node)
2018 /* If the size isn't known or is a constant, we have nothing to do. */
2019 size = TYPE_SIZE_UNIT (type);
2020 if (!size || TREE_CODE (size) == INTEGER_CST)
2023 /* Otherwise, make a WITH_SIZE_EXPR. */
2024 size = unshare_expr (size);
2025 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2026 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2029 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
2031 static enum gimplify_status
2032 gimplify_arg (tree *expr_p, tree *pre_p)
2034 bool (*test) (tree);
2037 /* In general, we allow lvalues for function arguments to avoid
2038 extra overhead of copying large aggregates out of even larger
2039 aggregates into temporaries only to copy the temporaries to
2040 the argument list. Make optimizers happy by pulling out to
2041 temporaries those types that fit in registers. */
2042 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
2043 test = is_gimple_val, fb = fb_rvalue;
2045 test = is_gimple_lvalue, fb = fb_either;
2047 /* If this is a variable sized type, we must remember the size. */
2048 maybe_with_size_expr (expr_p);
2050 /* There is a sequence point before a function call. Side effects in
2051 the argument list must occur before the actual call. So, when
2052 gimplifying arguments, force gimplify_expr to use an internal
2053 post queue which is then appended to the end of PRE_P. */
2054 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
2057 /* Gimplify the CALL_EXPR node pointed to by EXPR_P. PRE_P points to the
2058 list where side effects that must happen before *EXPR_P should be stored.
2059 WANT_VALUE is true if the result of the call is desired. */
2061 static enum gimplify_status
2062 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
2064 tree decl, parms, p;
2065 enum gimplify_status ret;
2068 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2070 /* For reliable diagnostics during inlining, it is necessary that
2071 every call_expr be annotated with file and line. */
2072 if (! EXPR_HAS_LOCATION (*expr_p))
2073 SET_EXPR_LOCATION (*expr_p, input_location);
2075 /* This may be a call to a builtin function.
2077 Builtin function calls may be transformed into different
2078 (and more efficient) builtin function calls under certain
2079 circumstances. Unfortunately, gimplification can muck things
2080 up enough that the builtin expanders are not aware that certain
2081 transformations are still valid.
2083 So we attempt transformation/gimplification of the call before
2084 we gimplify the CALL_EXPR. At this time we do not manage to
2085 transform all calls in the same manner as the expanders do, but
2086 we do transform most of them. */
2087 decl = get_callee_fndecl (*expr_p);
2088 if (decl && DECL_BUILT_IN (decl))
2090 tree new = fold_call_expr (*expr_p, !want_value);
2092 if (new && new != *expr_p)
2094 /* There was a transformation of this call which computes the
2095 same value, but in a more efficient way. Return and try
2101 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2102 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
2104 if (call_expr_nargs (*expr_p) < 2)
2106 error ("too few arguments to function %<va_start%>");
2107 *expr_p = build_empty_stmt ();
2111 if (fold_builtin_next_arg (*expr_p, true))
2113 *expr_p = build_empty_stmt ();
2116 /* Avoid gimplifying the second argument to va_start, which needs
2117 to be the plain PARM_DECL. */
2118 return gimplify_arg (&CALL_EXPR_ARG (*expr_p, 0), pre_p);
2122 /* There is a sequence point before the call, so any side effects in
2123 the calling expression must occur before the actual call. Force
2124 gimplify_expr to use an internal post queue. */
2125 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2126 is_gimple_call_addr, fb_rvalue);
2128 nargs = call_expr_nargs (*expr_p);
2130 /* Get argument types for verification. */
2131 decl = get_callee_fndecl (*expr_p);
2134 parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
2135 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2136 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2138 /* Verify if the type of the argument matches that of the function
2139 declaration. If we cannot verify this or there is a mismatch,
2140 mark the call expression so it doesn't get inlined later. */
2141 if (decl && DECL_ARGUMENTS (decl))
2143 for (i = 0, p = DECL_ARGUMENTS (decl); i < nargs;
2144 i++, p = TREE_CHAIN (p))
2146 /* We cannot distinguish a varargs function from the case
2147 of excess parameters, still deferring the inlining decision
2148 to the callee is possible. */
2151 if (p == error_mark_node
2152 || CALL_EXPR_ARG (*expr_p, i) == error_mark_node
2153 || !fold_convertible_p (DECL_ARG_TYPE (p),
2154 CALL_EXPR_ARG (*expr_p, i)))
2156 CALL_CANNOT_INLINE_P (*expr_p) = 1;
2163 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
2165 /* If this is a varargs function defer inlining decision
2169 if (TREE_VALUE (p) == error_mark_node
2170 || CALL_EXPR_ARG (*expr_p, i) == error_mark_node
2171 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
2172 || !fold_convertible_p (TREE_VALUE (p),
2173 CALL_EXPR_ARG (*expr_p, i)))
2175 CALL_CANNOT_INLINE_P (*expr_p) = 1;
2180 else if (nargs != 0)
2181 CALL_CANNOT_INLINE_P (*expr_p) = 1;
2183 /* Finally, gimplify the function arguments. */
2184 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2185 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2186 PUSH_ARGS_REVERSED ? i-- : i++)
2188 enum gimplify_status t;
2190 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p);
2196 /* Try this again in case gimplification exposed something. */
2197 if (ret != GS_ERROR)
2199 tree new = fold_call_expr (*expr_p, !want_value);
2201 if (new && new != *expr_p)
2203 /* There was a transformation of this call which computes the
2204 same value, but in a more efficient way. Return and try
2211 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2212 decl. This allows us to eliminate redundant or useless
2213 calls to "const" functions. */
2214 if (TREE_CODE (*expr_p) == CALL_EXPR
2215 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2216 TREE_SIDE_EFFECTS (*expr_p) = 0;
2221 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2222 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2224 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2225 condition is true or false, respectively. If null, we should generate
2226 our own to skip over the evaluation of this specific expression.
2228 This function is the tree equivalent of do_jump.
2230 shortcut_cond_r should only be called by shortcut_cond_expr. */
2233 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2235 tree local_label = NULL_TREE;
2236 tree t, expr = NULL;
2238 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2239 retain the shortcut semantics. Just insert the gotos here;
2240 shortcut_cond_expr will append the real blocks later. */
2241 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2243 /* Turn if (a && b) into
2245 if (a); else goto no;
2246 if (b) goto yes; else goto no;
2249 if (false_label_p == NULL)
2250 false_label_p = &local_label;
2252 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2253 append_to_statement_list (t, &expr);
2255 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2257 append_to_statement_list (t, &expr);
2259 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2261 /* Turn if (a || b) into
2264 if (b) goto yes; else goto no;
2267 if (true_label_p == NULL)
2268 true_label_p = &local_label;
2270 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2271 append_to_statement_list (t, &expr);
2273 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2275 append_to_statement_list (t, &expr);
2277 else if (TREE_CODE (pred) == COND_EXPR)
2279 /* As long as we're messing with gotos, turn if (a ? b : c) into
2281 if (b) goto yes; else goto no;
2283 if (c) goto yes; else goto no; */
2284 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2285 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2287 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2292 expr = build3 (COND_EXPR, void_type_node, pred,
2293 build_and_jump (true_label_p),
2294 build_and_jump (false_label_p));
2299 t = build1 (LABEL_EXPR, void_type_node, local_label);
2300 append_to_statement_list (t, &expr);
2307 shortcut_cond_expr (tree expr)
2309 tree pred = TREE_OPERAND (expr, 0);
2310 tree then_ = TREE_OPERAND (expr, 1);
2311 tree else_ = TREE_OPERAND (expr, 2);
2312 tree true_label, false_label, end_label, t;
2314 tree *false_label_p;
2315 bool emit_end, emit_false, jump_over_else;
2316 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2317 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2319 /* First do simple transformations. */
2322 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2323 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2325 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2326 then_ = shortcut_cond_expr (expr);
2327 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2328 pred = TREE_OPERAND (pred, 0);
2329 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2334 /* If there is no 'then', turn
2337 if (a); else if (b); else d. */
2338 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2340 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2341 else_ = shortcut_cond_expr (expr);
2342 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2343 pred = TREE_OPERAND (pred, 0);
2344 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2348 /* If we're done, great. */
2349 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2350 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2353 /* Otherwise we need to mess with gotos. Change
2356 if (a); else goto no;
2359 and recursively gimplify the condition. */
2361 true_label = false_label = end_label = NULL_TREE;
2363 /* If our arms just jump somewhere, hijack those labels so we don't
2364 generate jumps to jumps. */
2367 && TREE_CODE (then_) == GOTO_EXPR
2368 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2370 true_label = GOTO_DESTINATION (then_);
2376 && TREE_CODE (else_) == GOTO_EXPR
2377 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2379 false_label = GOTO_DESTINATION (else_);
2384 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2386 true_label_p = &true_label;
2388 true_label_p = NULL;
2390 /* The 'else' branch also needs a label if it contains interesting code. */
2391 if (false_label || else_se)
2392 false_label_p = &false_label;
2394 false_label_p = NULL;
2396 /* If there was nothing else in our arms, just forward the label(s). */
2397 if (!then_se && !else_se)
2398 return shortcut_cond_r (pred, true_label_p, false_label_p);
2400 /* If our last subexpression already has a terminal label, reuse it. */
2402 expr = expr_last (else_);
2404 expr = expr_last (then_);
2407 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2408 end_label = LABEL_EXPR_LABEL (expr);
2410 /* If we don't care about jumping to the 'else' branch, jump to the end
2411 if the condition is false. */
2413 false_label_p = &end_label;
2415 /* We only want to emit these labels if we aren't hijacking them. */
2416 emit_end = (end_label == NULL_TREE);
2417 emit_false = (false_label == NULL_TREE);
2419 /* We only emit the jump over the else clause if we have to--if the
2420 then clause may fall through. Otherwise we can wind up with a
2421 useless jump and a useless label at the end of gimplified code,
2422 which will cause us to think that this conditional as a whole
2423 falls through even if it doesn't. If we then inline a function
2424 which ends with such a condition, that can cause us to issue an
2425 inappropriate warning about control reaching the end of a
2426 non-void function. */
2427 jump_over_else = block_may_fallthru (then_);
2429 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2432 append_to_statement_list (pred, &expr);
2434 append_to_statement_list (then_, &expr);
2439 t = build_and_jump (&end_label);
2440 append_to_statement_list (t, &expr);
2444 t = build1 (LABEL_EXPR, void_type_node, false_label);
2445 append_to_statement_list (t, &expr);
2447 append_to_statement_list (else_, &expr);
2449 if (emit_end && end_label)
2451 t = build1 (LABEL_EXPR, void_type_node, end_label);
2452 append_to_statement_list (t, &expr);
2458 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2461 gimple_boolify (tree expr)
2463 tree type = TREE_TYPE (expr);
2465 if (TREE_CODE (type) == BOOLEAN_TYPE)
2468 switch (TREE_CODE (expr))
2470 case TRUTH_AND_EXPR:
2472 case TRUTH_XOR_EXPR:
2473 case TRUTH_ANDIF_EXPR:
2474 case TRUTH_ORIF_EXPR:
2475 /* Also boolify the arguments of truth exprs. */
2476 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2479 case TRUTH_NOT_EXPR:
2480 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2483 case EQ_EXPR: case NE_EXPR:
2484 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2485 /* These expressions always produce boolean results. */
2486 TREE_TYPE (expr) = boolean_type_node;
2490 /* Other expressions that get here must have boolean values, but
2491 might need to be converted to the appropriate mode. */
2492 return fold_convert (boolean_type_node, expr);
2496 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2505 The second form is used when *EXPR_P is of type void.
2507 TARGET is the tree for T1 above.
2509 PRE_P points to the list where side effects that must happen before
2510 *EXPR_P should be stored. */
2512 static enum gimplify_status
2513 gimplify_cond_expr (tree *expr_p, tree *pre_p, fallback_t fallback)
2515 tree expr = *expr_p;
2516 tree tmp, tmp2, type;
2517 enum gimplify_status ret;
2519 type = TREE_TYPE (expr);
2521 /* If this COND_EXPR has a value, copy the values into a temporary within
2523 if (! VOID_TYPE_P (type))
2527 if ((fallback & fb_lvalue) == 0)
2529 result = tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2534 tree type = build_pointer_type (TREE_TYPE (expr));
2536 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2537 TREE_OPERAND (expr, 1) =
2538 build_fold_addr_expr (TREE_OPERAND (expr, 1));
2540 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2541 TREE_OPERAND (expr, 2) =
2542 build_fold_addr_expr (TREE_OPERAND (expr, 2));
2544 tmp2 = tmp = create_tmp_var (type, "iftmp");
2546 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (expr, 0),
2547 TREE_OPERAND (expr, 1), TREE_OPERAND (expr, 2));
2549 result = build_fold_indirect_ref (tmp);
2553 /* Build the then clause, 't1 = a;'. But don't build an assignment
2554 if this branch is void; in C++ it can be, if it's a throw. */
2555 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2556 TREE_OPERAND (expr, 1)
2557 = build_gimple_modify_stmt (tmp, TREE_OPERAND (expr, 1));
2559 /* Build the else clause, 't1 = b;'. */
2560 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2561 TREE_OPERAND (expr, 2)
2562 = build_gimple_modify_stmt (tmp2, TREE_OPERAND (expr, 2));
2564 TREE_TYPE (expr) = void_type_node;
2565 recalculate_side_effects (expr);
2567 /* Move the COND_EXPR to the prequeue. */
2568 gimplify_and_add (expr, pre_p);
2574 /* Make sure the condition has BOOLEAN_TYPE. */
2575 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2577 /* Break apart && and || conditions. */
2578 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2579 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2581 expr = shortcut_cond_expr (expr);
2583 if (expr != *expr_p)
2587 /* We can't rely on gimplify_expr to re-gimplify the expanded
2588 form properly, as cleanups might cause the target labels to be
2589 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2590 set up a conditional context. */
2591 gimple_push_condition ();
2592 gimplify_stmt (expr_p);
2593 gimple_pop_condition (pre_p);
2599 /* Now do the normal gimplification. */
2600 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2601 is_gimple_condexpr, fb_rvalue);
2603 gimple_push_condition ();
2605 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2606 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2607 recalculate_side_effects (expr);
2609 gimple_pop_condition (pre_p);
2611 if (ret == GS_ERROR)
2613 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2615 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2616 /* Rewrite "if (a); else b" to "if (!a) b" */
2618 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2619 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2620 is_gimple_condexpr, fb_rvalue);
2622 tmp = TREE_OPERAND (expr, 1);
2623 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2624 TREE_OPERAND (expr, 2) = tmp;
2627 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2628 expr = TREE_OPERAND (expr, 0);
2634 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2635 a call to __builtin_memcpy. */
2637 static enum gimplify_status
2638 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2640 tree t, to, to_ptr, from, from_ptr;
2642 to = GENERIC_TREE_OPERAND (*expr_p, 0);
2643 from = GENERIC_TREE_OPERAND (*expr_p, 1);
2645 from_ptr = build_fold_addr_expr (from);
2647 to_ptr = build_fold_addr_expr (to);
2648 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2649 t = build_call_expr (t, 3, to_ptr, from_ptr, size);
2653 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2654 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2661 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2662 a call to __builtin_memset. In this case we know that the RHS is
2663 a CONSTRUCTOR with an empty element list. */
2665 static enum gimplify_status
2666 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2670 to = GENERIC_TREE_OPERAND (*expr_p, 0);
2672 to_ptr = build_fold_addr_expr (to);
2673 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2674 t = build_call_expr (t, 3, to_ptr, integer_zero_node, size);
2678 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2679 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2686 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2687 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2688 assignment. Returns non-null if we detect a potential overlap. */
2690 struct gimplify_init_ctor_preeval_data
2692 /* The base decl of the lhs object. May be NULL, in which case we
2693 have to assume the lhs is indirect. */
2696 /* The alias set of the lhs object. */
2701 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2703 struct gimplify_init_ctor_preeval_data *data
2704 = (struct gimplify_init_ctor_preeval_data *) xdata;
2707 /* If we find the base object, obviously we have overlap. */
2708 if (data->lhs_base_decl == t)
2711 /* If the constructor component is indirect, determine if we have a
2712 potential overlap with the lhs. The only bits of information we
2713 have to go on at this point are addressability and alias sets. */
2714 if (TREE_CODE (t) == INDIRECT_REF
2715 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2716 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2719 /* If the constructor component is a call, determine if it can hide a
2720 potential overlap with the lhs through an INDIRECT_REF like above. */
2721 if (TREE_CODE (t) == CALL_EXPR)
2723 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
2725 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
2726 if (POINTER_TYPE_P (TREE_VALUE (type))
2727 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2728 && alias_sets_conflict_p (data->lhs_alias_set,
2730 (TREE_TYPE (TREE_VALUE (type)))))
2734 if (IS_TYPE_OR_DECL_P (t))
2739 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2740 force values that overlap with the lhs (as described by *DATA)
2741 into temporaries. */
2744 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2745 struct gimplify_init_ctor_preeval_data *data)
2747 enum gimplify_status one;
2749 /* If the value is invariant, then there's nothing to pre-evaluate.
2750 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2751 invariant but has side effects and might contain a reference to
2752 the object we're initializing. */
2753 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2756 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2757 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2760 /* Recurse for nested constructors. */
2761 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2763 unsigned HOST_WIDE_INT ix;
2764 constructor_elt *ce;
2765 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
2767 for (ix = 0; VEC_iterate (constructor_elt, v, ix, ce); ix++)
2768 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
2772 /* If this is a variable sized type, we must remember the size. */
2773 maybe_with_size_expr (expr_p);
2775 /* Gimplify the constructor element to something appropriate for the rhs
2776 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2777 the gimplifier will consider this a store to memory. Doing this
2778 gimplification now means that we won't have to deal with complicated
2779 language-specific trees, nor trees like SAVE_EXPR that can induce
2780 exponential search behavior. */
2781 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2782 if (one == GS_ERROR)
2788 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2789 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2790 always be true for all scalars, since is_gimple_mem_rhs insists on a
2791 temporary variable for them. */
2792 if (DECL_P (*expr_p))
2795 /* If this is of variable size, we have no choice but to assume it doesn't
2796 overlap since we can't make a temporary for it. */
2797 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
2800 /* Otherwise, we must search for overlap ... */
2801 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2804 /* ... and if found, force the value into a temporary. */
2805 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2808 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
2809 a RANGE_EXPR in a CONSTRUCTOR for an array.
2813 object[var] = value;
2820 We increment var _after_ the loop exit check because we might otherwise
2821 fail if upper == TYPE_MAX_VALUE (type for upper).
2823 Note that we never have to deal with SAVE_EXPRs here, because this has
2824 already been taken care of for us, in gimplify_init_ctor_preeval(). */
2826 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
2830 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
2831 tree value, tree array_elt_type,
2832 tree *pre_p, bool cleared)
2834 tree loop_entry_label, loop_exit_label;
2835 tree var, var_type, cref, tmp;
2837 loop_entry_label = create_artificial_label ();
2838 loop_exit_label = create_artificial_label ();
2840 /* Create and initialize the index variable. */
2841 var_type = TREE_TYPE (upper);
2842 var = create_tmp_var (var_type, NULL);
2843 append_to_statement_list (build_gimple_modify_stmt (var, lower), pre_p);
2845 /* Add the loop entry label. */
2846 append_to_statement_list (build1 (LABEL_EXPR,
2851 /* Build the reference. */
2852 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2853 var, NULL_TREE, NULL_TREE);
2855 /* If we are a constructor, just call gimplify_init_ctor_eval to do
2856 the store. Otherwise just assign value to the reference. */
2858 if (TREE_CODE (value) == CONSTRUCTOR)
2859 /* NB we might have to call ourself recursively through
2860 gimplify_init_ctor_eval if the value is a constructor. */
2861 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2864 append_to_statement_list (build_gimple_modify_stmt (cref, value), pre_p);
2866 /* We exit the loop when the index var is equal to the upper bound. */
2867 gimplify_and_add (build3 (COND_EXPR, void_type_node,
2868 build2 (EQ_EXPR, boolean_type_node,
2876 /* Otherwise, increment the index var... */
2877 tmp = build2 (PLUS_EXPR, var_type, var,
2878 fold_convert (var_type, integer_one_node));
2879 append_to_statement_list (build_gimple_modify_stmt (var, tmp), pre_p);
2881 /* ...and jump back to the loop entry. */
2882 append_to_statement_list (build1 (GOTO_EXPR,
2887 /* Add the loop exit label. */
2888 append_to_statement_list (build1 (LABEL_EXPR,
2894 /* Return true if FDECL is accessing a field that is zero sized. */
2897 zero_sized_field_decl (tree fdecl)
2899 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
2900 && integer_zerop (DECL_SIZE (fdecl)))
2905 /* Return true if TYPE is zero sized. */
2908 zero_sized_type (tree type)
2910 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
2911 && integer_zerop (TYPE_SIZE (type)))
2916 /* A subroutine of gimplify_init_constructor. Generate individual
2917 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2918 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
2919 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2923 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
2924 tree *pre_p, bool cleared)
2926 tree array_elt_type = NULL;
2927 unsigned HOST_WIDE_INT ix;
2928 tree purpose, value;
2930 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2931 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2933 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
2937 /* NULL values are created above for gimplification errors. */
2941 if (cleared && initializer_zerop (value))
2944 /* ??? Here's to hoping the front end fills in all of the indices,
2945 so we don't have to figure out what's missing ourselves. */
2946 gcc_assert (purpose);
2948 /* Skip zero-sized fields, unless value has side-effects. This can
2949 happen with calls to functions returning a zero-sized type, which
2950 we shouldn't discard. As a number of downstream passes don't
2951 expect sets of zero-sized fields, we rely on the gimplification of
2952 the MODIFY_EXPR we make below to drop the assignment statement. */
2953 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
2956 /* If we have a RANGE_EXPR, we have to build a loop to assign the
2958 if (TREE_CODE (purpose) == RANGE_EXPR)
2960 tree lower = TREE_OPERAND (purpose, 0);
2961 tree upper = TREE_OPERAND (purpose, 1);
2963 /* If the lower bound is equal to upper, just treat it as if
2964 upper was the index. */
2965 if (simple_cst_equal (lower, upper))
2969 gimplify_init_ctor_eval_range (object, lower, upper, value,
2970 array_elt_type, pre_p, cleared);
2977 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2978 purpose, NULL_TREE, NULL_TREE);
2982 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
2983 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
2984 unshare_expr (object), purpose, NULL_TREE);
2987 if (TREE_CODE (value) == CONSTRUCTOR
2988 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
2989 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2993 init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
2994 gimplify_and_add (init, pre_p);
2999 /* A subroutine of gimplify_modify_expr. Break out elements of a
3000 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3002 Note that we still need to clear any elements that don't have explicit
3003 initializers, so if not all elements are initialized we keep the
3004 original MODIFY_EXPR, we just remove all of the constructor elements. */
3006 static enum gimplify_status
3007 gimplify_init_constructor (tree *expr_p, tree *pre_p,
3008 tree *post_p, bool want_value)
3011 tree ctor = GENERIC_TREE_OPERAND (*expr_p, 1);
3012 tree type = TREE_TYPE (ctor);
3013 enum gimplify_status ret;
3014 VEC(constructor_elt,gc) *elts;
3016 if (TREE_CODE (ctor) != CONSTRUCTOR)
3017 return GS_UNHANDLED;
3019 ret = gimplify_expr (&GENERIC_TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3020 is_gimple_lvalue, fb_lvalue);
3021 if (ret == GS_ERROR)
3023 object = GENERIC_TREE_OPERAND (*expr_p, 0);
3025 elts = CONSTRUCTOR_ELTS (ctor);
3028 switch (TREE_CODE (type))
3032 case QUAL_UNION_TYPE:
3035 struct gimplify_init_ctor_preeval_data preeval_data;
3036 HOST_WIDE_INT num_type_elements, num_ctor_elements;
3037 HOST_WIDE_INT num_nonzero_elements;
3038 bool cleared, valid_const_initializer;
3040 /* Aggregate types must lower constructors to initialization of
3041 individual elements. The exception is that a CONSTRUCTOR node
3042 with no elements indicates zero-initialization of the whole. */
3043 if (VEC_empty (constructor_elt, elts))
3046 /* Fetch information about the constructor to direct later processing.
3047 We might want to make static versions of it in various cases, and
3048 can only do so if it known to be a valid constant initializer. */
3049 valid_const_initializer
3050 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3051 &num_ctor_elements, &cleared);
3053 /* If a const aggregate variable is being initialized, then it
3054 should never be a lose to promote the variable to be static. */
3055 if (valid_const_initializer
3056 && num_nonzero_elements > 1
3057 && TREE_READONLY (object)
3058 && TREE_CODE (object) == VAR_DECL)
3060 DECL_INITIAL (object) = ctor;
3061 TREE_STATIC (object) = 1;
3062 if (!DECL_NAME (object))
3063 DECL_NAME (object) = create_tmp_var_name ("C");
3064 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3066 /* ??? C++ doesn't automatically append a .<number> to the
3067 assembler name, and even when it does, it looks a FE private
3068 data structures to figure out what that number should be,
3069 which are not set for this variable. I suppose this is
3070 important for local statics for inline functions, which aren't
3071 "local" in the object file sense. So in order to get a unique
3072 TU-local symbol, we must invoke the lhd version now. */
3073 lhd_set_decl_assembler_name (object);
3075 *expr_p = NULL_TREE;
3079 /* If there are "lots" of initialized elements, even discounting
3080 those that are not address constants (and thus *must* be
3081 computed at runtime), then partition the constructor into
3082 constant and non-constant parts. Block copy the constant
3083 parts in, then generate code for the non-constant parts. */
3084 /* TODO. There's code in cp/typeck.c to do this. */
3086 num_type_elements = count_type_elements (type, true);
3088 /* If count_type_elements could not determine number of type elements
3089 for a constant-sized object, assume clearing is needed.
3090 Don't do this for variable-sized objects, as store_constructor
3091 will ignore the clearing of variable-sized objects. */
3092 if (num_type_elements < 0 && int_size_in_bytes (type) >= 0)
3094 /* If there are "lots" of zeros, then block clear the object first. */
3095 else if (num_type_elements - num_nonzero_elements > CLEAR_RATIO
3096 && num_nonzero_elements < num_type_elements/4)
3098 /* ??? This bit ought not be needed. For any element not present
3099 in the initializer, we should simply set them to zero. Except
3100 we'd need to *find* the elements that are not present, and that
3101 requires trickery to avoid quadratic compile-time behavior in
3102 large cases or excessive memory use in small cases. */
3103 else if (num_ctor_elements < num_type_elements)
3106 /* If there are "lots" of initialized elements, and all of them
3107 are valid address constants, then the entire initializer can
3108 be dropped to memory, and then memcpy'd out. Don't do this
3109 for sparse arrays, though, as it's more efficient to follow
3110 the standard CONSTRUCTOR behavior of memset followed by
3111 individual element initialization. */
3112 if (valid_const_initializer && !cleared)
3114 HOST_WIDE_INT size = int_size_in_bytes (type);
3117 /* ??? We can still get unbounded array types, at least
3118 from the C++ front end. This seems wrong, but attempt
3119 to work around it for now. */
3122 size = int_size_in_bytes (TREE_TYPE (object));
3124 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3127 /* Find the maximum alignment we can assume for the object. */
3128 /* ??? Make use of DECL_OFFSET_ALIGN. */
3129 if (DECL_P (object))
3130 align = DECL_ALIGN (object);
3132 align = TYPE_ALIGN (type);
3134 if (size > 0 && !can_move_by_pieces (size, align))
3136 tree new = create_tmp_var_raw (type, "C");
3138 gimple_add_tmp_var (new);
3139 TREE_STATIC (new) = 1;
3140 TREE_READONLY (new) = 1;
3141 DECL_INITIAL (new) = ctor;
3142 if (align > DECL_ALIGN (new))
3144 DECL_ALIGN (new) = align;
3145 DECL_USER_ALIGN (new) = 1;
3147 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
3149 GENERIC_TREE_OPERAND (*expr_p, 1) = new;
3151 /* This is no longer an assignment of a CONSTRUCTOR, but
3152 we still may have processing to do on the LHS. So
3153 pretend we didn't do anything here to let that happen. */
3154 return GS_UNHANDLED;
3158 /* If there are nonzero elements, pre-evaluate to capture elements
3159 overlapping with the lhs into temporaries. We must do this before
3160 clearing to fetch the values before they are zeroed-out. */
3161 if (num_nonzero_elements > 0)
3163 preeval_data.lhs_base_decl = get_base_address (object);
3164 if (!DECL_P (preeval_data.lhs_base_decl))
3165 preeval_data.lhs_base_decl = NULL;
3166 preeval_data.lhs_alias_set = get_alias_set (object);
3168 gimplify_init_ctor_preeval (&GENERIC_TREE_OPERAND (*expr_p, 1),
3169 pre_p, post_p, &preeval_data);
3174 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3175 Note that we still have to gimplify, in order to handle the
3176 case of variable sized types. Avoid shared tree structures. */
3177 CONSTRUCTOR_ELTS (ctor) = NULL;
3178 object = unshare_expr (object);
3179 gimplify_stmt (expr_p);
3180 append_to_statement_list (*expr_p, pre_p);
3183 /* If we have not block cleared the object, or if there are nonzero
3184 elements in the constructor, add assignments to the individual
3185 scalar fields of the object. */
3186 if (!cleared || num_nonzero_elements > 0)
3187 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3189 *expr_p = NULL_TREE;
3197 /* Extract the real and imaginary parts out of the ctor. */
3198 gcc_assert (VEC_length (constructor_elt, elts) == 2);
3199 r = VEC_index (constructor_elt, elts, 0)->value;
3200 i = VEC_index (constructor_elt, elts, 1)->value;
3201 if (r == NULL || i == NULL)
3203 tree zero = fold_convert (TREE_TYPE (type), integer_zero_node);
3210 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3211 represent creation of a complex value. */
3212 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3214 ctor = build_complex (type, r, i);
3215 TREE_OPERAND (*expr_p, 1) = ctor;
3219 ctor = build2 (COMPLEX_EXPR, type, r, i);
3220 TREE_OPERAND (*expr_p, 1) = ctor;
3221 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3222 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3230 unsigned HOST_WIDE_INT ix;
3231 constructor_elt *ce;
3233 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3234 if (TREE_CONSTANT (ctor))
3236 bool constant_p = true;
3239 /* Even when ctor is constant, it might contain non-*_CST
3240 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
3241 belong into VECTOR_CST nodes. */
3242 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3243 if (!CONSTANT_CLASS_P (value))
3251 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3255 /* Don't reduce a TREE_CONSTANT vector ctor even if we can't
3256 make a VECTOR_CST. It won't do anything for us, and it'll
3257 prevent us from representing it as a single constant. */
3261 /* Vector types use CONSTRUCTOR all the way through gimple
3262 compilation as a general initializer. */
3263 for (ix = 0; VEC_iterate (constructor_elt, elts, ix, ce); ix++)
3265 enum gimplify_status tret;
3266 tret = gimplify_expr (&ce->value, pre_p, post_p,
3267 is_gimple_val, fb_rvalue);
3268 if (tret == GS_ERROR)
3271 if (!is_gimple_reg (GENERIC_TREE_OPERAND (*expr_p, 0)))
3272 GENERIC_TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
3277 /* So how did we get a CONSTRUCTOR for a scalar type? */
3281 if (ret == GS_ERROR)
3283 else if (want_value)
3285 append_to_statement_list (*expr_p, pre_p);
3293 /* Given a pointer value OP0, return a simplified version of an
3294 indirection through OP0, or NULL_TREE if no simplification is
3295 possible. This may only be applied to a rhs of an expression.
3296 Note that the resulting type may be different from the type pointed
3297 to in the sense that it is still compatible from the langhooks
3301 fold_indirect_ref_rhs (tree t)
3303 tree type = TREE_TYPE (TREE_TYPE (t));
3307 STRIP_USELESS_TYPE_CONVERSION (sub);
3308 subtype = TREE_TYPE (sub);
3309 if (!POINTER_TYPE_P (subtype))
3312 if (TREE_CODE (sub) == ADDR_EXPR)
3314 tree op = TREE_OPERAND (sub, 0);
3315 tree optype = TREE_TYPE (op);
3317 if (useless_type_conversion_p (type, optype))
3319 /* *(foo *)&fooarray => fooarray[0] */
3320 else if (TREE_CODE (optype) == ARRAY_TYPE
3321 && useless_type_conversion_p (type, TREE_TYPE (optype)))
3323 tree type_domain = TYPE_DOMAIN (optype);
3324 tree min_val = size_zero_node;
3325 if (type_domain && TYPE_MIN_VALUE (type_domain))
3326 min_val = TYPE_MIN_VALUE (type_domain);
3327 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3331 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3332 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3333 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3336 tree min_val = size_zero_node;
3338 sub = fold_indirect_ref_rhs (sub);
3340 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3341 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3342 if (type_domain && TYPE_MIN_VALUE (type_domain))
3343 min_val = TYPE_MIN_VALUE (type_domain);
3344 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3350 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
3351 based on the code of the RHS. We loop for as long as something changes. */
3353 static enum gimplify_status
3354 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
3355 tree *post_p, bool want_value)
3357 enum gimplify_status ret = GS_OK;
3359 while (ret != GS_UNHANDLED)
3360 switch (TREE_CODE (*from_p))
3364 /* If we have code like
3368 where the type of "x" is a (possibly cv-qualified variant
3369 of "A"), treat the entire expression as identical to "x".
3370 This kind of code arises in C++ when an object is bound
3371 to a const reference, and if "x" is a TARGET_EXPR we want
3372 to take advantage of the optimization below. */
3373 tree t = fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
3386 /* If we are initializing something from a TARGET_EXPR, strip the
3387 TARGET_EXPR and initialize it directly, if possible. This can't
3388 be done if the initializer is void, since that implies that the
3389 temporary is set in some non-trivial way.
3391 ??? What about code that pulls out the temp and uses it
3392 elsewhere? I think that such code never uses the TARGET_EXPR as
3393 an initializer. If I'm wrong, we'll die because the temp won't
3394 have any RTL. In that case, I guess we'll need to replace
3395 references somehow. */
3396 tree init = TARGET_EXPR_INITIAL (*from_p);
3398 if (!VOID_TYPE_P (TREE_TYPE (init)))
3409 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
3411 gimplify_compound_expr (from_p, pre_p, true);
3416 /* If we're initializing from a CONSTRUCTOR, break this into
3417 individual MODIFY_EXPRs. */
3418 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
3421 /* If we're assigning to a non-register type, push the assignment
3422 down into the branches. This is mandatory for ADDRESSABLE types,
3423 since we cannot generate temporaries for such, but it saves a
3424 copy in other cases as well. */
3425 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
3427 /* This code should mirror the code in gimplify_cond_expr. */
3428 enum tree_code code = TREE_CODE (*expr_p);
3429 tree cond = *from_p;
3430 tree result = *to_p;
3432 ret = gimplify_expr (&result, pre_p, post_p,
3433 is_gimple_min_lval, fb_lvalue);
3434 if (ret != GS_ERROR)
3437 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
3438 TREE_OPERAND (cond, 1)
3439 = build2 (code, void_type_node, result,
3440 TREE_OPERAND (cond, 1));
3441 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
3442 TREE_OPERAND (cond, 2)
3443 = build2 (code, void_type_node, unshare_expr (result),
3444 TREE_OPERAND (cond, 2));
3446 TREE_TYPE (cond) = void_type_node;
3447 recalculate_side_effects (cond);
3451 gimplify_and_add (cond, pre_p);
3452 *expr_p = unshare_expr (result);
3463 /* For calls that return in memory, give *to_p as the CALL_EXPR's
3464 return slot so that we don't generate a temporary. */
3465 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
3466 && aggregate_value_p (*from_p, *from_p))
3470 if (!(rhs_predicate_for (*to_p))(*from_p))
3471 /* If we need a temporary, *to_p isn't accurate. */
3473 else if (TREE_CODE (*to_p) == RESULT_DECL
3474 && DECL_NAME (*to_p) == NULL_TREE
3475 && needs_to_live_in_memory (*to_p))
3476 /* It's OK to use the return slot directly unless it's an NRV. */
3478 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
3479 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
3480 /* Don't force regs into memory. */
3482 else if (TREE_CODE (*to_p) == VAR_DECL
3483 && DECL_GIMPLE_FORMAL_TEMP_P (*to_p))
3484 /* Don't use the original target if it's a formal temp; we
3485 don't want to take their addresses. */
3487 else if (TREE_CODE (*expr_p) == INIT_EXPR)
3488 /* It's OK to use the target directly if it's being
3491 else if (!is_gimple_non_addressable (*to_p))
3492 /* Don't use the original target if it's already addressable;
3493 if its address escapes, and the called function uses the
3494 NRV optimization, a conforming program could see *to_p
3495 change before the called function returns; see c++/19317.
3496 When optimizing, the return_slot pass marks more functions
3497 as safe after we have escape info. */
3504 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
3505 mark_addressable (*to_p);
3512 /* If we're initializing from a container, push the initialization
3514 case CLEANUP_POINT_EXPR:
3516 case STATEMENT_LIST:
3518 tree wrap = *from_p;
3521 ret = gimplify_expr (to_p, pre_p, post_p,
3522 is_gimple_min_lval, fb_lvalue);
3523 if (ret != GS_ERROR)
3526 t = voidify_wrapper_expr (wrap, *expr_p);
3527 gcc_assert (t == *expr_p);
3531 gimplify_and_add (wrap, pre_p);
3532 *expr_p = unshare_expr (*to_p);
3547 /* Destructively convert the TREE pointer in TP into a gimple tuple if
3551 tree_to_gimple_tuple (tree *tp)
3554 switch (TREE_CODE (*tp))
3556 case GIMPLE_MODIFY_STMT:
3560 struct gimple_stmt *gs;
3561 tree lhs = TREE_OPERAND (*tp, 0);
3562 bool def_stmt_self_p = false;
3564 if (TREE_CODE (lhs) == SSA_NAME)
3566 if (SSA_NAME_DEF_STMT (lhs) == *tp)
3567 def_stmt_self_p = true;
3570 gs = &make_node (GIMPLE_MODIFY_STMT)->gstmt;
3571 gs->base = (*tp)->base;
3572 /* The set to base above overwrites the CODE. */
3573 TREE_SET_CODE ((tree) gs, GIMPLE_MODIFY_STMT);
3575 gs->locus = EXPR_LOCUS (*tp);
3576 gs->operands[0] = TREE_OPERAND (*tp, 0);
3577 gs->operands[1] = TREE_OPERAND (*tp, 1);
3578 gs->block = TREE_BLOCK (*tp);
3581 /* If we re-gimplify a set to an SSA_NAME, we must change the
3582 SSA name's DEF_STMT link. */
3583 if (def_stmt_self_p)
3584 SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (*tp, 0)) = *tp;
3593 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
3594 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
3595 DECL_GIMPLE_REG_P set. */
3597 static enum gimplify_status
3598 gimplify_modify_expr_complex_part (tree *expr_p, tree *pre_p, bool want_value)
3600 enum tree_code code, ocode;
3601 tree lhs, rhs, new_rhs, other, realpart, imagpart;
3603 lhs = GENERIC_TREE_OPERAND (*expr_p, 0);
3604 rhs = GENERIC_TREE_OPERAND (*expr_p, 1);
3605 code = TREE_CODE (lhs);
3606 lhs = TREE_OPERAND (lhs, 0);
3608 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
3609 other = build1 (ocode, TREE_TYPE (rhs), lhs);
3610 other = get_formal_tmp_var (other, pre_p);
3612 realpart = code == REALPART_EXPR ? rhs : other;
3613 imagpart = code == REALPART_EXPR ? other : rhs;
3615 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
3616 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
3618 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
3620 GENERIC_TREE_OPERAND (*expr_p, 0) = lhs;
3621 GENERIC_TREE_OPERAND (*expr_p, 1) = new_rhs;
3625 tree_to_gimple_tuple (expr_p);
3627 append_to_statement_list (*expr_p, pre_p);
3634 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
3640 PRE_P points to the list where side effects that must happen before
3641 *EXPR_P should be stored.
3643 POST_P points to the list where side effects that must happen after
3644 *EXPR_P should be stored.
3646 WANT_VALUE is nonzero iff we want to use the value of this expression
3647 in another expression. */
3649 static enum gimplify_status
3650 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
3652 tree *from_p = &GENERIC_TREE_OPERAND (*expr_p, 1);
3653 tree *to_p = &GENERIC_TREE_OPERAND (*expr_p, 0);
3654 enum gimplify_status ret = GS_UNHANDLED;
3656 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
3657 || TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT
3658 || TREE_CODE (*expr_p) == INIT_EXPR);
3660 /* For zero sized types only gimplify the left hand side and right hand side
3661 as statements and throw away the assignment. */
3662 if (zero_sized_type (TREE_TYPE (*from_p)))
3664 gimplify_stmt (from_p);
3665 gimplify_stmt (to_p);
3666 append_to_statement_list (*from_p, pre_p);
3667 append_to_statement_list (*to_p, pre_p);
3668 *expr_p = NULL_TREE;
3672 /* See if any simplifications can be done based on what the RHS is. */
3673 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3675 if (ret != GS_UNHANDLED)
3678 /* If the value being copied is of variable width, compute the length
3679 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
3680 before gimplifying any of the operands so that we can resolve any
3681 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
3682 the size of the expression to be copied, not of the destination, so
3683 that is what we must here. */
3684 maybe_with_size_expr (from_p);
3686 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3687 if (ret == GS_ERROR)
3690 ret = gimplify_expr (from_p, pre_p, post_p,
3691 rhs_predicate_for (*to_p), fb_rvalue);
3692 if (ret == GS_ERROR)
3695 /* Now see if the above changed *from_p to something we handle specially. */
3696 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3698 if (ret != GS_UNHANDLED)
3701 /* If we've got a variable sized assignment between two lvalues (i.e. does
3702 not involve a call), then we can make things a bit more straightforward
3703 by converting the assignment to memcpy or memset. */
3704 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
3706 tree from = TREE_OPERAND (*from_p, 0);
3707 tree size = TREE_OPERAND (*from_p, 1);
3709 if (TREE_CODE (from) == CONSTRUCTOR)
3710 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
3711 if (is_gimple_addressable (from))
3714 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
3718 /* Transform partial stores to non-addressable complex variables into
3719 total stores. This allows us to use real instead of virtual operands
3720 for these variables, which improves optimization. */
3721 if ((TREE_CODE (*to_p) == REALPART_EXPR
3722 || TREE_CODE (*to_p) == IMAGPART_EXPR)
3723 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
3724 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
3726 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
3728 /* If we've somehow already got an SSA_NAME on the LHS, then
3729 we're probably modified it twice. Not good. */
3730 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
3731 *to_p = make_ssa_name (*to_p, *expr_p);
3734 /* Try to alleviate the effects of the gimplification creating artificial
3735 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
3736 if (!gimplify_ctxp->into_ssa
3737 && DECL_P (*from_p) && DECL_IGNORED_P (*from_p)
3738 && DECL_P (*to_p) && !DECL_IGNORED_P (*to_p))
3740 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
3742 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
3743 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
3744 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
3749 tree_to_gimple_tuple (expr_p);
3751 append_to_statement_list (*expr_p, pre_p);
3759 /* Gimplify a comparison between two variable-sized objects. Do this
3760 with a call to BUILT_IN_MEMCMP. */
3762 static enum gimplify_status
3763 gimplify_variable_sized_compare (tree *expr_p)
3765 tree op0 = TREE_OPERAND (*expr_p, 0);
3766 tree op1 = TREE_OPERAND (*expr_p, 1);
3767 tree t, arg, dest, src;
3769 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
3770 arg = unshare_expr (arg);
3771 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
3772 src = build_fold_addr_expr (op1);
3773 dest = build_fold_addr_expr (op0);
3774 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
3775 t = build_call_expr (t, 3, dest, src, arg);
3777 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
3782 /* Gimplify a comparison between two aggregate objects of integral scalar
3783 mode as a comparison between the bitwise equivalent scalar values. */
3785 static enum gimplify_status
3786 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
3788 tree op0 = TREE_OPERAND (*expr_p, 0);
3789 tree op1 = TREE_OPERAND (*expr_p, 1);
3791 tree type = TREE_TYPE (op0);
3792 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
3794 op0 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op0);
3795 op1 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op1);
3798 = fold_build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
3803 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
3804 points to the expression to gimplify.
3806 Expressions of the form 'a && b' are gimplified to:
3808 a && b ? true : false
3810 gimplify_cond_expr will do the rest.
3812 PRE_P points to the list where side effects that must happen before
3813 *EXPR_P should be stored. */
3815 static enum gimplify_status
3816 gimplify_boolean_expr (tree *expr_p)
3818 /* Preserve the original type of the expression. */
3819 tree type = TREE_TYPE (*expr_p);
3821 *expr_p = build3 (COND_EXPR, type, *expr_p,
3822 fold_convert (type, boolean_true_node),
3823 fold_convert (type, boolean_false_node));
3828 /* Gimplifies an expression sequence. This function gimplifies each
3829 expression and re-writes the original expression with the last
3830 expression of the sequence in GIMPLE form.
3832 PRE_P points to the list where the side effects for all the
3833 expressions in the sequence will be emitted.
3835 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
3836 /* ??? Should rearrange to share the pre-queue with all the indirect
3837 invocations of gimplify_expr. Would probably save on creations
3838 of statement_list nodes. */
3840 static enum gimplify_status
3841 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
3847 tree *sub_p = &TREE_OPERAND (t, 0);
3849 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3850 gimplify_compound_expr (sub_p, pre_p, false);
3852 gimplify_stmt (sub_p);
3853 append_to_statement_list (*sub_p, pre_p);
3855 t = TREE_OPERAND (t, 1);
3857 while (TREE_CODE (t) == COMPOUND_EXPR);
3864 gimplify_stmt (expr_p);
3869 /* Gimplifies a statement list. These may be created either by an
3870 enlightened front-end, or by shortcut_cond_expr. */
3872 static enum gimplify_status
3873 gimplify_statement_list (tree *expr_p, tree *pre_p)
3875 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3877 tree_stmt_iterator i = tsi_start (*expr_p);
3879 while (!tsi_end_p (i))
3883 gimplify_stmt (tsi_stmt_ptr (i));
3888 else if (TREE_CODE (t) == STATEMENT_LIST)
3890 tsi_link_before (&i, t, TSI_SAME_STMT);
3899 append_to_statement_list (*expr_p, pre_p);
3907 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
3908 gimplify. After gimplification, EXPR_P will point to a new temporary
3909 that holds the original value of the SAVE_EXPR node.
3911 PRE_P points to the list where side effects that must happen before
3912 *EXPR_P should be stored. */
3914 static enum gimplify_status
3915 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3917 enum gimplify_status ret = GS_ALL_DONE;
3920 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
3921 val = TREE_OPERAND (*expr_p, 0);
3923 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
3924 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
3926 /* The operand may be a void-valued expression such as SAVE_EXPRs
3927 generated by the Java frontend for class initialization. It is
3928 being executed only for its side-effects. */
3929 if (TREE_TYPE (val) == void_type_node)
3931 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3932 is_gimple_stmt, fb_none);
3933 append_to_statement_list (TREE_OPERAND (*expr_p, 0), pre_p);
3937 val = get_initialized_tmp_var (val, pre_p, post_p);
3939 TREE_OPERAND (*expr_p, 0) = val;
3940 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
3948 /* Re-write the ADDR_EXPR node pointed to by EXPR_P
3955 PRE_P points to the list where side effects that must happen before
3956 *EXPR_P should be stored.
3958 POST_P points to the list where side effects that must happen after
3959 *EXPR_P should be stored. */
3961 static enum gimplify_status
3962 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3964 tree expr = *expr_p;
3965 tree op0 = TREE_OPERAND (expr, 0);
3966 enum gimplify_status ret;
3968 switch (TREE_CODE (op0))
3971 case MISALIGNED_INDIRECT_REF:
3973 /* Check if we are dealing with an expression of the form '&*ptr'.
3974 While the front end folds away '&*ptr' into 'ptr', these
3975 expressions may be generated internally by the compiler (e.g.,
3976 builtins like __builtin_va_end). */
3977 /* Caution: the silent array decomposition semantics we allow for
3978 ADDR_EXPR means we can't always discard the pair. */
3979 /* Gimplification of the ADDR_EXPR operand may drop
3980 cv-qualification conversions, so make sure we add them if
3983 tree op00 = TREE_OPERAND (op0, 0);
3984 tree t_expr = TREE_TYPE (expr);
3985 tree t_op00 = TREE_TYPE (op00);
3987 if (!useless_type_conversion_p (t_expr, t_op00))
3989 #ifdef ENABLE_CHECKING
3990 tree t_op0 = TREE_TYPE (op0);
3991 gcc_assert (POINTER_TYPE_P (t_expr)
3992 && (cpt_same_type (TREE_TYPE (t_expr), t_op0)
3993 || (TREE_CODE (t_op0) == ARRAY_TYPE
3994 && cpt_same_type (TREE_TYPE (t_expr),
3995 TREE_TYPE (t_op0))))
3996 && POINTER_TYPE_P (t_op00)
3997 && cpt_same_type (t_op0, TREE_TYPE (t_op00)));
3999 op00 = fold_convert (TREE_TYPE (expr), op00);
4006 case VIEW_CONVERT_EXPR:
4007 /* Take the address of our operand and then convert it to the type of
4010 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4011 all clear. The impact of this transformation is even less clear. */
4013 /* If the operand is a useless conversion, look through it. Doing so
4014 guarantees that the ADDR_EXPR and its operand will remain of the
4016 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4017 op0 = TREE_OPERAND (op0, 0);
4019 *expr_p = fold_convert (TREE_TYPE (expr),
4020 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
4025 /* We use fb_either here because the C frontend sometimes takes
4026 the address of a call that returns a struct; see
4027 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4028 the implied temporary explicit. */
4030 /* Mark the RHS addressable. */
4031 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4032 is_gimple_addressable, fb_either);
4033 if (ret != GS_ERROR)
4035 op0 = TREE_OPERAND (expr, 0);
4037 /* For various reasons, the gimplification of the expression
4038 may have made a new INDIRECT_REF. */
4039 if (TREE_CODE (op0) == INDIRECT_REF)
4040 goto do_indirect_ref;
4042 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
4044 recompute_tree_invariant_for_addr_expr (expr);
4046 mark_addressable (TREE_OPERAND (expr, 0));
4054 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4055 value; output operands should be a gimple lvalue. */
4057 static enum gimplify_status
4058 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
4060 tree expr = *expr_p;
4061 int noutputs = list_length (ASM_OUTPUTS (expr));
4062 const char **oconstraints
4063 = (const char **) alloca ((noutputs) * sizeof (const char *));
4066 const char *constraint;
4067 bool allows_mem, allows_reg, is_inout;
4068 enum gimplify_status ret, tret;
4071 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
4073 size_t constraint_len;
4074 oconstraints[i] = constraint
4075 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4076 constraint_len = strlen (constraint);
4077 if (constraint_len == 0)
4080 parse_output_constraint (&constraint, i, 0, 0,
4081 &allows_mem, &allows_reg, &is_inout);
4083 if (!allows_reg && allows_mem)
4084 mark_addressable (TREE_VALUE (link));
4086 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4087 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
4088 fb_lvalue | fb_mayfail);
4089 if (tret == GS_ERROR)
4091 error ("invalid lvalue in asm output %d", i);
4097 /* An input/output operand. To give the optimizers more
4098 flexibility, split it into separate input and output
4103 /* Turn the in/out constraint into an output constraint. */
4104 char *p = xstrdup (constraint);
4106 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4108 /* And add a matching input constraint. */
4111 sprintf (buf, "%d", i);
4113 /* If there are multiple alternatives in the constraint,
4114 handle each of them individually. Those that allow register
4115 will be replaced with operand number, the others will stay
4117 if (strchr (p, ',') != NULL)
4119 size_t len = 0, buflen = strlen (buf);
4120 char *beg, *end, *str, *dst;
4124 end = strchr (beg, ',');
4126 end = strchr (beg, '\0');
4127 if ((size_t) (end - beg) < buflen)
4130 len += end - beg + 1;
4137 str = (char *) alloca (len);
4138 for (beg = p + 1, dst = str;;)
4141 bool mem_p, reg_p, inout_p;
4143 end = strchr (beg, ',');
4148 parse_output_constraint (&tem, i, 0, 0,
4149 &mem_p, ®_p, &inout_p);
4154 memcpy (dst, buf, buflen);
4163 memcpy (dst, beg, len);
4172 input = build_string (dst - str, str);
4175 input = build_string (strlen (buf), buf);
4178 input = build_string (constraint_len - 1, constraint + 1);
4182 input = build_tree_list (build_tree_list (NULL_TREE, input),
4183 unshare_expr (TREE_VALUE (link)));
4184 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4188 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
4191 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4192 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4193 oconstraints, &allows_mem, &allows_reg);
4195 /* If we can't make copies, we can only accept memory. */
4196 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
4202 error ("impossible constraint in %<asm%>");
4203 error ("non-memory input %d must stay in memory", i);
4208 /* If the operand is a memory input, it should be an lvalue. */
4209 if (!allows_reg && allows_mem)
4211 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4212 is_gimple_lvalue, fb_lvalue | fb_mayfail);
4213 mark_addressable (TREE_VALUE (link));
4214 if (tret == GS_ERROR)
4216 error ("memory input %d is not directly addressable", i);
4222 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4223 is_gimple_asm_val, fb_rvalue);
4224 if (tret == GS_ERROR)
4232 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
4233 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
4234 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
4235 return to this function.
4237 FIXME should we complexify the prequeue handling instead? Or use flags
4238 for all the cleanups and let the optimizer tighten them up? The current
4239 code seems pretty fragile; it will break on a cleanup within any
4240 non-conditional nesting. But any such nesting would be broken, anyway;
4241 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
4242 and continues out of it. We can do that at the RTL level, though, so
4243 having an optimizer to tighten up try/finally regions would be a Good
4246 static enum gimplify_status
4247 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
4249 tree_stmt_iterator iter;
4252 tree temp = voidify_wrapper_expr (*expr_p, NULL);
4254 /* We only care about the number of conditions between the innermost
4255 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
4256 any cleanups collected outside the CLEANUP_POINT_EXPR. */
4257 int old_conds = gimplify_ctxp->conditions;
4258 tree old_cleanups = gimplify_ctxp->conditional_cleanups;
4259 gimplify_ctxp->conditions = 0;
4260 gimplify_ctxp->conditional_cleanups = NULL_TREE;
4262 body = TREE_OPERAND (*expr_p, 0);
4263 gimplify_to_stmt_list (&body);
4265 gimplify_ctxp->conditions = old_conds;
4266 gimplify_ctxp->conditional_cleanups = old_cleanups;
4268 for (iter = tsi_start (body); !tsi_end_p (iter); )
4270 tree *wce_p = tsi_stmt_ptr (iter);
4273 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
4275 if (tsi_one_before_end_p (iter))
4277 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
4284 enum tree_code code;
4286 if (CLEANUP_EH_ONLY (wce))
4287 code = TRY_CATCH_EXPR;
4289 code = TRY_FINALLY_EXPR;
4291 sl = tsi_split_statement_list_after (&iter);
4292 tfe = build2 (code, void_type_node, sl, NULL_TREE);
4293 append_to_statement_list (TREE_OPERAND (wce, 0),
4294 &TREE_OPERAND (tfe, 1));
4296 iter = tsi_start (sl);
4306 append_to_statement_list (body, pre_p);
4316 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
4317 is the cleanup action required. */
4320 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
4324 /* Errors can result in improperly nested cleanups. Which results in
4325 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
4326 if (errorcount || sorrycount)
4329 if (gimple_conditional_context ())
4331 /* If we're in a conditional context, this is more complex. We only
4332 want to run the cleanup if we actually ran the initialization that
4333 necessitates it, but we want to run it after the end of the
4334 conditional context. So we wrap the try/finally around the
4335 condition and use a flag to determine whether or not to actually
4336 run the destructor. Thus
4340 becomes (approximately)
4344 if (test) { A::A(temp); flag = 1; val = f(temp); }
4347 if (flag) A::~A(temp);
4352 tree flag = create_tmp_var (boolean_type_node, "cleanup");
4353 tree ffalse = build_gimple_modify_stmt (flag, boolean_false_node);
4354 tree ftrue = build_gimple_modify_stmt (flag, boolean_true_node);
4355 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
4356 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4357 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
4358 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
4359 append_to_statement_list (ftrue, pre_p);
4361 /* Because of this manipulation, and the EH edges that jump
4362 threading cannot redirect, the temporary (VAR) will appear
4363 to be used uninitialized. Don't warn. */
4364 TREE_NO_WARNING (var) = 1;
4368 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4369 CLEANUP_EH_ONLY (wce) = eh_only;
4370 append_to_statement_list (wce, pre_p);
4373 gimplify_stmt (&TREE_OPERAND (wce, 0));
4376 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
4378 static enum gimplify_status
4379 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
4381 tree targ = *expr_p;
4382 tree temp = TARGET_EXPR_SLOT (targ);
4383 tree init = TARGET_EXPR_INITIAL (targ);
4384 enum gimplify_status ret;
4388 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
4389 to the temps list. */
4390 gimple_add_tmp_var (temp);
4392 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
4393 expression is supposed to initialize the slot. */
4394 if (VOID_TYPE_P (TREE_TYPE (init)))
4395 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
4398 init = build2 (INIT_EXPR, void_type_node, temp, init);
4399 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
4402 if (ret == GS_ERROR)
4404 /* PR c++/28266 Make sure this is expanded only once. */
4405 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4408 append_to_statement_list (init, pre_p);
4410 /* If needed, push the cleanup for the temp. */
4411 if (TARGET_EXPR_CLEANUP (targ))
4413 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
4414 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
4415 CLEANUP_EH_ONLY (targ), pre_p);
4418 /* Only expand this once. */
4419 TREE_OPERAND (targ, 3) = init;
4420 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4423 /* We should have expanded this before. */
4424 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
4430 /* Gimplification of expression trees. */
4432 /* Gimplify an expression which appears at statement context; usually, this
4433 means replacing it with a suitably gimple STATEMENT_LIST. */
4436 gimplify_stmt (tree *stmt_p)
4438 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
4441 /* Similarly, but force the result to be a STATEMENT_LIST. */
4444 gimplify_to_stmt_list (tree *stmt_p)
4446 gimplify_stmt (stmt_p);
4448 *stmt_p = alloc_stmt_list ();
4449 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
4452 *stmt_p = alloc_stmt_list ();
4453 append_to_statement_list (t, stmt_p);
4458 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
4459 to CTX. If entries already exist, force them to be some flavor of private.
4460 If there is no enclosing parallel, do nothing. */
4463 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
4467 if (decl == NULL || !DECL_P (decl))
4472 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4475 if (n->value & GOVD_SHARED)
4476 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
4480 else if (ctx->is_parallel)
4481 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
4483 ctx = ctx->outer_context;
4488 /* Similarly for each of the type sizes of TYPE. */
4491 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
4493 if (type == NULL || type == error_mark_node)
4495 type = TYPE_MAIN_VARIANT (type);
4497 if (pointer_set_insert (ctx->privatized_types, type))
4500 switch (TREE_CODE (type))
4506 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
4507 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
4511 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4512 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
4517 case QUAL_UNION_TYPE:
4520 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4521 if (TREE_CODE (field) == FIELD_DECL)
4523 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
4524 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
4530 case REFERENCE_TYPE:
4531 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4538 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
4539 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
4540 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
4543 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
4546 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
4549 unsigned int nflags;
4552 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4555 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
4556 there are constructors involved somewhere. */
4557 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
4558 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
4561 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4564 /* We shouldn't be re-adding the decl with the same data
4566 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
4567 /* The only combination of data sharing classes we should see is
4568 FIRSTPRIVATE and LASTPRIVATE. */
4569 nflags = n->value | flags;
4570 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
4571 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
4576 /* When adding a variable-sized variable, we have to handle all sorts
4577 of additional bits of data: the pointer replacement variable, and
4578 the parameters of the type. */
4579 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
4581 /* Add the pointer replacement variable as PRIVATE if the variable
4582 replacement is private, else FIRSTPRIVATE since we'll need the
4583 address of the original variable either for SHARED, or for the
4584 copy into or out of the context. */
4585 if (!(flags & GOVD_LOCAL))
4587 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
4588 nflags |= flags & GOVD_SEEN;
4589 t = DECL_VALUE_EXPR (decl);
4590 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
4591 t = TREE_OPERAND (t, 0);
4592 gcc_assert (DECL_P (t));
4593 omp_add_variable (ctx, t, nflags);
4596 /* Add all of the variable and type parameters (which should have
4597 been gimplified to a formal temporary) as FIRSTPRIVATE. */
4598 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
4599 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
4600 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4602 /* The variable-sized variable itself is never SHARED, only some form
4603 of PRIVATE. The sharing would take place via the pointer variable
4604 which we remapped above. */
4605 if (flags & GOVD_SHARED)
4606 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
4607 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
4609 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
4610 alloca statement we generate for the variable, so make sure it
4611 is available. This isn't automatically needed for the SHARED
4612 case, since we won't be allocating local storage then.
4613 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
4614 in this case omp_notice_variable will be called later
4615 on when it is gimplified. */
4616 else if (! (flags & GOVD_LOCAL))
4617 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
4619 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
4621 gcc_assert ((flags & GOVD_LOCAL) == 0);
4622 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4624 /* Similar to the direct variable sized case above, we'll need the
4625 size of references being privatized. */
4626 if ((flags & GOVD_SHARED) == 0)
4628 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
4629 if (TREE_CODE (t) != INTEGER_CST)
4630 omp_notice_variable (ctx, t, true);
4634 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
4637 /* Record the fact that DECL was used within the OpenMP context CTX.
4638 IN_CODE is true when real code uses DECL, and false when we should
4639 merely emit default(none) errors. Return true if DECL is going to
4640 be remapped and thus DECL shouldn't be gimplified into its
4641 DECL_VALUE_EXPR (if any). */
4644 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
4647 unsigned flags = in_code ? GOVD_SEEN : 0;
4648 bool ret = false, shared;
4650 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4653 /* Threadprivate variables are predetermined. */
4654 if (is_global_var (decl))
4656 if (DECL_THREAD_LOCAL_P (decl))
4659 if (DECL_HAS_VALUE_EXPR_P (decl))
4661 tree value = get_base_address (DECL_VALUE_EXPR (decl));
4663 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
4668 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4671 enum omp_clause_default_kind default_kind, kind;
4673 if (!ctx->is_parallel)
4676 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
4677 remapped firstprivate instead of shared. To some extent this is
4678 addressed in omp_firstprivatize_type_sizes, but not effectively. */
4679 default_kind = ctx->default_kind;
4680 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
4681 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
4682 default_kind = kind;
4684 switch (default_kind)
4686 case OMP_CLAUSE_DEFAULT_NONE:
4687 error ("%qs not specified in enclosing parallel",
4688 IDENTIFIER_POINTER (DECL_NAME (decl)));
4689 error ("%Henclosing parallel", &ctx->location);
4691 case OMP_CLAUSE_DEFAULT_SHARED:
4692 flags |= GOVD_SHARED;
4694 case OMP_CLAUSE_DEFAULT_PRIVATE:
4695 flags |= GOVD_PRIVATE;
4701 omp_add_variable (ctx, decl, flags);
4703 shared = (flags & GOVD_SHARED) != 0;
4704 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4708 shared = ((flags | n->value) & GOVD_SHARED) != 0;
4709 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4711 /* If nothing changed, there's nothing left to do. */
4712 if ((n->value & flags) == flags)
4718 /* If the variable is private in the current context, then we don't
4719 need to propagate anything to an outer context. */
4720 if (flags & GOVD_PRIVATE)
4722 if (ctx->outer_context
4723 && omp_notice_variable (ctx->outer_context, decl, in_code))
4728 /* Verify that DECL is private within CTX. If there's specific information
4729 to the contrary in the innermost scope, generate an error. */
4732 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
4736 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4739 if (n->value & GOVD_SHARED)
4741 if (ctx == gimplify_omp_ctxp)
4743 error ("iteration variable %qs should be private",
4744 IDENTIFIER_POINTER (DECL_NAME (decl)));
4745 n->value = GOVD_PRIVATE;
4751 else if ((n->value & GOVD_EXPLICIT) != 0
4752 && (ctx == gimplify_omp_ctxp
4753 || (ctx->is_combined_parallel
4754 && gimplify_omp_ctxp->outer_context == ctx)))
4756 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
4757 error ("iteration variable %qs should not be firstprivate",
4758 IDENTIFIER_POINTER (DECL_NAME (decl)));
4759 else if ((n->value & GOVD_REDUCTION) != 0)
4760 error ("iteration variable %qs should not be reduction",
4761 IDENTIFIER_POINTER (DECL_NAME (decl)));
4766 if (ctx->is_parallel)
4768 else if (ctx->outer_context)
4769 return omp_is_private (ctx->outer_context, decl);
4771 return !is_global_var (decl);
4774 /* Return true if DECL is private within a parallel region
4775 that binds to the current construct's context or in parallel
4776 region's REDUCTION clause. */
4779 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
4785 ctx = ctx->outer_context;
4787 return !(is_global_var (decl)
4788 /* References might be private, but might be shared too. */
4789 || lang_hooks.decls.omp_privatize_by_reference (decl));
4791 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4793 return (n->value & GOVD_SHARED) == 0;
4795 while (!ctx->is_parallel);
4799 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
4800 and previous omp contexts. */
4803 gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel,
4804 bool in_combined_parallel)
4806 struct gimplify_omp_ctx *ctx, *outer_ctx;
4809 ctx = new_omp_context (in_parallel, in_combined_parallel);
4810 outer_ctx = ctx->outer_context;
4812 while ((c = *list_p) != NULL)
4814 enum gimplify_status gs;
4815 bool remove = false;
4816 bool notice_outer = true;
4817 const char *check_non_private = NULL;
4821 switch (OMP_CLAUSE_CODE (c))
4823 case OMP_CLAUSE_PRIVATE:
4824 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
4825 notice_outer = false;
4827 case OMP_CLAUSE_SHARED:
4828 flags = GOVD_SHARED | GOVD_EXPLICIT;
4830 case OMP_CLAUSE_FIRSTPRIVATE:
4831 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
4832 check_non_private = "firstprivate";
4834 case OMP_CLAUSE_LASTPRIVATE:
4835 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
4836 check_non_private = "lastprivate";
4838 case OMP_CLAUSE_REDUCTION:
4839 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
4840 check_non_private = "reduction";
4844 decl = OMP_CLAUSE_DECL (c);
4845 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4850 omp_add_variable (ctx, decl, flags);
4851 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4852 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4854 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
4855 GOVD_LOCAL | GOVD_SEEN);
4856 gimplify_omp_ctxp = ctx;
4857 push_gimplify_context ();
4858 gimplify_stmt (&OMP_CLAUSE_REDUCTION_INIT (c));
4859 pop_gimplify_context (OMP_CLAUSE_REDUCTION_INIT (c));
4860 push_gimplify_context ();
4861 gimplify_stmt (&OMP_CLAUSE_REDUCTION_MERGE (c));
4862 pop_gimplify_context (OMP_CLAUSE_REDUCTION_MERGE (c));
4863 gimplify_omp_ctxp = outer_ctx;
4869 case OMP_CLAUSE_COPYIN:
4870 case OMP_CLAUSE_COPYPRIVATE:
4871 decl = OMP_CLAUSE_DECL (c);
4872 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4879 omp_notice_variable (outer_ctx, decl, true);
4880 if (check_non_private
4882 && omp_check_private (ctx, decl))
4884 error ("%s variable %qs is private in outer context",
4885 check_non_private, IDENTIFIER_POINTER (DECL_NAME (decl)));
4891 OMP_CLAUSE_OPERAND (c, 0)
4892 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
4895 case OMP_CLAUSE_SCHEDULE:
4896 case OMP_CLAUSE_NUM_THREADS:
4897 gs = gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
4898 is_gimple_val, fb_rvalue);
4903 case OMP_CLAUSE_NOWAIT:
4904 case OMP_CLAUSE_ORDERED:
4907 case OMP_CLAUSE_DEFAULT:
4908 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
4916 *list_p = OMP_CLAUSE_CHAIN (c);
4918 list_p = &OMP_CLAUSE_CHAIN (c);
4921 gimplify_omp_ctxp = ctx;
4924 /* For all variables that were not actually used within the context,
4925 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
4928 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
4930 tree *list_p = (tree *) data;
4931 tree decl = (tree) n->key;
4932 unsigned flags = n->value;
4933 enum omp_clause_code code;
4937 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
4939 if ((flags & GOVD_SEEN) == 0)
4941 if (flags & GOVD_DEBUG_PRIVATE)
4943 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
4944 private_debug = true;
4948 = lang_hooks.decls.omp_private_debug_clause (decl,
4949 !!(flags & GOVD_SHARED));
4951 code = OMP_CLAUSE_PRIVATE;
4952 else if (flags & GOVD_SHARED)
4954 if (is_global_var (decl))
4956 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
4960 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4961 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
4962 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
4964 ctx = ctx->outer_context;
4969 code = OMP_CLAUSE_SHARED;
4971 else if (flags & GOVD_PRIVATE)
4972 code = OMP_CLAUSE_PRIVATE;
4973 else if (flags & GOVD_FIRSTPRIVATE)
4974 code = OMP_CLAUSE_FIRSTPRIVATE;
4978 clause = build_omp_clause (code);
4979 OMP_CLAUSE_DECL (clause) = decl;
4980 OMP_CLAUSE_CHAIN (clause) = *list_p;
4982 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
4989 gimplify_adjust_omp_clauses (tree *list_p)
4991 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
4994 while ((c = *list_p) != NULL)
4997 bool remove = false;
4999 switch (OMP_CLAUSE_CODE (c))
5001 case OMP_CLAUSE_PRIVATE:
5002 case OMP_CLAUSE_SHARED:
5003 case OMP_CLAUSE_FIRSTPRIVATE:
5004 decl = OMP_CLAUSE_DECL (c);
5005 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5006 remove = !(n->value & GOVD_SEEN);
5009 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
5010 if ((n->value & GOVD_DEBUG_PRIVATE)
5011 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
5013 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
5014 || ((n->value & GOVD_DATA_SHARE_CLASS)
5016 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
5017 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
5022 case OMP_CLAUSE_LASTPRIVATE:
5023 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
5024 accurately reflect the presence of a FIRSTPRIVATE clause. */
5025 decl = OMP_CLAUSE_DECL (c);
5026 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5027 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
5028 = (n->value & GOVD_FIRSTPRIVATE) != 0;
5031 case OMP_CLAUSE_REDUCTION:
5032 case OMP_CLAUSE_COPYIN:
5033 case OMP_CLAUSE_COPYPRIVATE:
5035 case OMP_CLAUSE_NUM_THREADS:
5036 case OMP_CLAUSE_SCHEDULE:
5037 case OMP_CLAUSE_NOWAIT:
5038 case OMP_CLAUSE_ORDERED:
5039 case OMP_CLAUSE_DEFAULT:
5047 *list_p = OMP_CLAUSE_CHAIN (c);
5049 list_p = &OMP_CLAUSE_CHAIN (c);
5052 /* Add in any implicit data sharing. */
5053 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
5055 gimplify_omp_ctxp = ctx->outer_context;
5056 delete_omp_context (ctx);
5059 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
5060 gimplification of the body, as well as scanning the body for used
5061 variables. We need to do this scan now, because variable-sized
5062 decls will be decomposed during gimplification. */
5064 static enum gimplify_status
5065 gimplify_omp_parallel (tree *expr_p, tree *pre_p)
5067 tree expr = *expr_p;
5069 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p, true,
5070 OMP_PARALLEL_COMBINED (expr));
5072 push_gimplify_context ();
5074 gimplify_stmt (&OMP_PARALLEL_BODY (expr));
5076 if (TREE_CODE (OMP_PARALLEL_BODY (expr)) == BIND_EXPR)
5077 pop_gimplify_context (OMP_PARALLEL_BODY (expr));
5079 pop_gimplify_context (NULL_TREE);
5081 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
5086 /* Gimplify the gross structure of an OMP_FOR statement. */
5088 static enum gimplify_status
5089 gimplify_omp_for (tree *expr_p, tree *pre_p)
5091 tree for_stmt, decl, t;
5092 enum gimplify_status ret = GS_OK;
5096 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, false, false);
5098 t = OMP_FOR_INIT (for_stmt);
5099 gcc_assert (TREE_CODE (t) == MODIFY_EXPR
5100 || TREE_CODE (t) == GIMPLE_MODIFY_STMT);
5101 decl = GENERIC_TREE_OPERAND (t, 0);
5102 gcc_assert (DECL_P (decl));
5103 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl)));
5105 /* Make sure the iteration variable is private. */
5106 if (omp_is_private (gimplify_omp_ctxp, decl))
5107 omp_notice_variable (gimplify_omp_ctxp, decl, true);
5109 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
5111 ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
5112 &OMP_FOR_PRE_BODY (for_stmt),
5113 NULL, is_gimple_val, fb_rvalue);
5115 tree_to_gimple_tuple (&OMP_FOR_INIT (for_stmt));
5117 t = OMP_FOR_COND (for_stmt);
5118 gcc_assert (COMPARISON_CLASS_P (t));
5119 gcc_assert (GENERIC_TREE_OPERAND (t, 0) == decl);
5121 ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
5122 &OMP_FOR_PRE_BODY (for_stmt),
5123 NULL, is_gimple_val, fb_rvalue);
5125 tree_to_gimple_tuple (&OMP_FOR_INCR (for_stmt));
5126 t = OMP_FOR_INCR (for_stmt);
5127 switch (TREE_CODE (t))
5129 case PREINCREMENT_EXPR:
5130 case POSTINCREMENT_EXPR:
5131 t = build_int_cst (TREE_TYPE (decl), 1);
5132 t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
5133 t = build_gimple_modify_stmt (decl, t);
5134 OMP_FOR_INCR (for_stmt) = t;
5137 case PREDECREMENT_EXPR:
5138 case POSTDECREMENT_EXPR:
5139 t = build_int_cst (TREE_TYPE (decl), -1);
5140 t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
5141 t = build_gimple_modify_stmt (decl, t);
5142 OMP_FOR_INCR (for_stmt) = t;
5145 case GIMPLE_MODIFY_STMT:
5146 gcc_assert (GIMPLE_STMT_OPERAND (t, 0) == decl);
5147 t = GIMPLE_STMT_OPERAND (t, 1);
5148 switch (TREE_CODE (t))
5151 if (TREE_OPERAND (t, 1) == decl)
5153 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
5154 TREE_OPERAND (t, 0) = decl;
5158 gcc_assert (TREE_OPERAND (t, 0) == decl);
5164 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
5165 NULL, is_gimple_val, fb_rvalue);
5172 gimplify_to_stmt_list (&OMP_FOR_BODY (for_stmt));
5173 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
5175 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
5178 /* Gimplify the gross structure of other OpenMP worksharing constructs.
5179 In particular, OMP_SECTIONS and OMP_SINGLE. */
5181 static enum gimplify_status
5182 gimplify_omp_workshare (tree *expr_p, tree *pre_p)
5184 tree stmt = *expr_p;
5186 gimplify_scan_omp_clauses (&OMP_CLAUSES (stmt), pre_p, false, false);
5187 gimplify_to_stmt_list (&OMP_BODY (stmt));
5188 gimplify_adjust_omp_clauses (&OMP_CLAUSES (stmt));
5193 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
5194 stabilized the lhs of the atomic operation as *ADDR. Return true if
5195 EXPR is this stabilized form. */
5198 goa_lhs_expr_p (tree expr, tree addr)
5200 /* Also include casts to other type variants. The C front end is fond
5201 of adding these for e.g. volatile variables. This is like
5202 STRIP_TYPE_NOPS but includes the main variant lookup. */
5203 while ((TREE_CODE (expr) == NOP_EXPR
5204 || TREE_CODE (expr) == CONVERT_EXPR
5205 || TREE_CODE (expr) == NON_LVALUE_EXPR)
5206 && TREE_OPERAND (expr, 0) != error_mark_node
5207 && (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
5208 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))))
5209 expr = TREE_OPERAND (expr, 0);
5211 if (TREE_CODE (expr) == INDIRECT_REF && TREE_OPERAND (expr, 0) == addr)
5213 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
5218 /* A subroutine of gimplify_omp_atomic. Attempt to implement the atomic
5219 operation as a __sync_fetch_and_op builtin. INDEX is log2 of the
5220 size of the data type, and thus usable to find the index of the builtin
5221 decl. Returns GS_UNHANDLED if the expression is not of the proper form. */
5223 static enum gimplify_status
5224 gimplify_omp_atomic_fetch_op (tree *expr_p, tree addr, tree rhs, int index)
5226 enum built_in_function base;
5228 enum insn_code *optab;
5230 /* Check for one of the supported fetch-op operations. */
5231 switch (TREE_CODE (rhs))
5233 case POINTER_PLUS_EXPR:
5235 base = BUILT_IN_FETCH_AND_ADD_N;
5236 optab = sync_add_optab;
5239 base = BUILT_IN_FETCH_AND_SUB_N;
5240 optab = sync_add_optab;
5243 base = BUILT_IN_FETCH_AND_AND_N;
5244 optab = sync_and_optab;
5247 base = BUILT_IN_FETCH_AND_OR_N;
5248 optab = sync_ior_optab;
5251 base = BUILT_IN_FETCH_AND_XOR_N;
5252 optab = sync_xor_optab;
5255 return GS_UNHANDLED;
5258 /* Make sure the expression is of the proper form. */
5259 if (goa_lhs_expr_p (TREE_OPERAND (rhs, 0), addr))
5260 rhs = TREE_OPERAND (rhs, 1);
5261 else if (commutative_tree_code (TREE_CODE (rhs))
5262 && goa_lhs_expr_p (TREE_OPERAND (rhs, 1), addr))
5263 rhs = TREE_OPERAND (rhs, 0);
5265 return GS_UNHANDLED;
5267 decl = built_in_decls[base + index + 1];
5268 itype = TREE_TYPE (TREE_TYPE (decl));
5270 if (optab[TYPE_MODE (itype)] == CODE_FOR_nothing)
5271 return GS_UNHANDLED;
5273 *expr_p = build_call_expr (decl, 2, addr, fold_convert (itype, rhs));
5277 /* A subroutine of gimplify_omp_atomic_pipeline. Walk *EXPR_P and replace
5278 appearances of *LHS_ADDR with LHS_VAR. If an expression does not involve
5279 the lhs, evaluate it into a temporary. Return 1 if the lhs appeared as
5280 a subexpression, 0 if it did not, or -1 if an error was encountered. */
5283 goa_stabilize_expr (tree *expr_p, tree *pre_p, tree lhs_addr, tree lhs_var)
5285 tree expr = *expr_p;
5288 if (goa_lhs_expr_p (expr, lhs_addr))
5293 if (is_gimple_val (expr))
5297 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
5300 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
5303 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
5312 enum gimplify_status gs;
5313 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
5314 if (gs != GS_ALL_DONE)
5321 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
5325 newval = rhs; // with oldval replacing *addr in rhs
5326 oldval = __sync_val_compare_and_swap (addr, oldval, newval);
5327 if (oldval != newval)
5330 INDEX is log2 of the size of the data type, and thus usable to find the
5331 index of the builtin decl. */
5333 static enum gimplify_status
5334 gimplify_omp_atomic_pipeline (tree *expr_p, tree *pre_p, tree addr,
5335 tree rhs, int index)
5337 tree oldval, oldival, oldival2, newval, newival, label;
5338 tree type, itype, cmpxchg, x, iaddr;
5340 cmpxchg = built_in_decls[BUILT_IN_VAL_COMPARE_AND_SWAP_N + index + 1];
5341 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5342 itype = TREE_TYPE (TREE_TYPE (cmpxchg));
5344 if (sync_compare_and_swap[TYPE_MODE (itype)] == CODE_FOR_nothing)
5345 return GS_UNHANDLED;
5347 oldval = create_tmp_var (type, NULL);
5348 newval = create_tmp_var (type, NULL);
5350 /* Precompute as much of RHS as possible. In the same walk, replace
5351 occurrences of the lhs value with our temporary. */
5352 if (goa_stabilize_expr (&rhs, pre_p, addr, oldval) < 0)
5355 x = build_fold_indirect_ref (addr);
5356 x = build_gimple_modify_stmt (oldval, x);
5357 gimplify_and_add (x, pre_p);
5359 /* For floating-point values, we'll need to view-convert them to integers
5360 so that we can perform the atomic compare and swap. Simplify the
5361 following code by always setting up the "i"ntegral variables. */
5362 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5370 oldival = create_tmp_var (itype, NULL);
5371 newival = create_tmp_var (itype, NULL);
5373 x = build1 (VIEW_CONVERT_EXPR, itype, oldval);
5374 x = build_gimple_modify_stmt (oldival, x);
5375 gimplify_and_add (x, pre_p);
5376 iaddr = fold_convert (build_pointer_type (itype), addr);
5379 oldival2 = create_tmp_var (itype, NULL);
5381 label = create_artificial_label ();
5382 x = build1 (LABEL_EXPR, void_type_node, label);
5383 gimplify_and_add (x, pre_p);
5385 x = build_gimple_modify_stmt (newval, rhs);
5386 gimplify_and_add (x, pre_p);
5388 if (newval != newival)
5390 x = build1 (VIEW_CONVERT_EXPR, itype, newval);
5391 x = build_gimple_modify_stmt (newival, x);
5392 gimplify_and_add (x, pre_p);
5395 x = build_gimple_modify_stmt (oldival2, fold_convert (itype, oldival));
5396 gimplify_and_add (x, pre_p);
5398 x = build_call_expr (cmpxchg, 3, iaddr, fold_convert (itype, oldival),
5399 fold_convert (itype, newival));
5400 if (oldval == oldival)
5401 x = fold_convert (type, x);
5402 x = build_gimple_modify_stmt (oldival, x);
5403 gimplify_and_add (x, pre_p);
5405 /* For floating point, be prepared for the loop backedge. */
5406 if (oldval != oldival)
5408 x = build1 (VIEW_CONVERT_EXPR, type, oldival);
5409 x = build_gimple_modify_stmt (oldval, x);
5410 gimplify_and_add (x, pre_p);
5413 /* Note that we always perform the comparison as an integer, even for
5414 floating point. This allows the atomic operation to properly
5415 succeed even with NaNs and -0.0. */
5416 x = build3 (COND_EXPR, void_type_node,
5417 build2 (NE_EXPR, boolean_type_node,
5418 fold_convert (itype, oldival), oldival2),
5419 build1 (GOTO_EXPR, void_type_node, label), NULL);
5420 gimplify_and_add (x, pre_p);
5426 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
5428 GOMP_atomic_start ();
5432 The result is not globally atomic, but works so long as all parallel
5433 references are within #pragma omp atomic directives. According to
5434 responses received from omp@openmp.org, appears to be within spec.
5435 Which makes sense, since that's how several other compilers handle
5436 this situation as well. */
5438 static enum gimplify_status
5439 gimplify_omp_atomic_mutex (tree *expr_p, tree *pre_p, tree addr, tree rhs)
5443 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_START];
5444 t = build_call_expr (t, 0);
5445 gimplify_and_add (t, pre_p);
5447 t = build_fold_indirect_ref (addr);
5448 t = build_gimple_modify_stmt (t, rhs);
5449 gimplify_and_add (t, pre_p);
5451 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_END];
5452 t = build_call_expr (t, 0);
5453 gimplify_and_add (t, pre_p);
5459 /* Gimplify an OMP_ATOMIC statement. */
5461 static enum gimplify_status
5462 gimplify_omp_atomic (tree *expr_p, tree *pre_p)
5464 tree addr = TREE_OPERAND (*expr_p, 0);
5465 tree rhs = TREE_OPERAND (*expr_p, 1);
5466 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5467 HOST_WIDE_INT index;
5469 /* Make sure the type is one of the supported sizes. */
5470 index = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5471 index = exact_log2 (index);
5472 if (index >= 0 && index <= 4)
5474 enum gimplify_status gs;
5477 if (DECL_P (TREE_OPERAND (addr, 0)))
5478 align = DECL_ALIGN_UNIT (TREE_OPERAND (addr, 0));
5479 else if (TREE_CODE (TREE_OPERAND (addr, 0)) == COMPONENT_REF
5480 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (addr, 0), 1))
5482 align = DECL_ALIGN_UNIT (TREE_OPERAND (TREE_OPERAND (addr, 0), 1));
5484 align = TYPE_ALIGN_UNIT (type);
5486 /* __sync builtins require strict data alignment. */
5487 if (exact_log2 (align) >= index)
5489 /* When possible, use specialized atomic update functions. */
5490 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5492 gs = gimplify_omp_atomic_fetch_op (expr_p, addr, rhs, index);
5493 if (gs != GS_UNHANDLED)
5497 /* If we don't have specialized __sync builtins, try and implement
5498 as a compare and swap loop. */
5499 gs = gimplify_omp_atomic_pipeline (expr_p, pre_p, addr, rhs, index);
5500 if (gs != GS_UNHANDLED)
5505 /* The ultimate fallback is wrapping the operation in a mutex. */
5506 return gimplify_omp_atomic_mutex (expr_p, pre_p, addr, rhs);
5509 /* Gimplifies the expression tree pointed to by EXPR_P. Return 0 if
5510 gimplification failed.
5512 PRE_P points to the list where side effects that must happen before
5513 EXPR should be stored.
5515 POST_P points to the list where side effects that must happen after
5516 EXPR should be stored, or NULL if there is no suitable list. In
5517 that case, we copy the result to a temporary, emit the
5518 post-effects, and then return the temporary.
5520 GIMPLE_TEST_F points to a function that takes a tree T and
5521 returns nonzero if T is in the GIMPLE form requested by the
5522 caller. The GIMPLE predicates are in tree-gimple.c.
5524 This test is used twice. Before gimplification, the test is
5525 invoked to determine whether *EXPR_P is already gimple enough. If
5526 that fails, *EXPR_P is gimplified according to its code and
5527 GIMPLE_TEST_F is called again. If the test still fails, then a new
5528 temporary variable is created and assigned the value of the
5529 gimplified expression.
5531 FALLBACK tells the function what sort of a temporary we want. If the 1
5532 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
5533 If both are set, either is OK, but an lvalue is preferable.
5535 The return value is either GS_ERROR or GS_ALL_DONE, since this function
5536 iterates until solution. */
5538 enum gimplify_status
5539 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
5540 bool (* gimple_test_f) (tree), fallback_t fallback)
5543 tree internal_pre = NULL_TREE;
5544 tree internal_post = NULL_TREE;
5546 int is_statement = (pre_p == NULL);
5547 location_t saved_location;
5548 enum gimplify_status ret;
5550 save_expr = *expr_p;
5551 if (save_expr == NULL_TREE)
5554 /* We used to check the predicate here and return immediately if it
5555 succeeds. This is wrong; the design is for gimplification to be
5556 idempotent, and for the predicates to only test for valid forms, not
5557 whether they are fully simplified. */
5559 /* Set up our internal queues if needed. */
5561 pre_p = &internal_pre;
5563 post_p = &internal_post;
5565 saved_location = input_location;
5566 if (save_expr != error_mark_node
5567 && EXPR_HAS_LOCATION (*expr_p))
5568 input_location = EXPR_LOCATION (*expr_p);
5570 /* Loop over the specific gimplifiers until the toplevel node
5571 remains the same. */
5574 /* Strip away as many useless type conversions as possible
5576 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
5578 /* Remember the expr. */
5579 save_expr = *expr_p;
5581 /* Die, die, die, my darling. */
5582 if (save_expr == error_mark_node
5583 || (!GIMPLE_STMT_P (save_expr)
5584 && TREE_TYPE (save_expr)
5585 && TREE_TYPE (save_expr) == error_mark_node))
5591 /* Do any language-specific gimplification. */
5592 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
5595 if (*expr_p == NULL_TREE)
5597 if (*expr_p != save_expr)
5600 else if (ret != GS_UNHANDLED)
5604 switch (TREE_CODE (*expr_p))
5606 /* First deal with the special cases. */
5608 case POSTINCREMENT_EXPR:
5609 case POSTDECREMENT_EXPR:
5610 case PREINCREMENT_EXPR:
5611 case PREDECREMENT_EXPR:
5612 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
5613 fallback != fb_none);
5617 case ARRAY_RANGE_REF:
5621 case VIEW_CONVERT_EXPR:
5622 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
5623 fallback ? fallback : fb_rvalue);
5627 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
5628 /* C99 code may assign to an array in a structure value of a
5629 conditional expression, and this has undefined behavior
5630 only on execution, so create a temporary if an lvalue is
5632 if (fallback == fb_lvalue)
5634 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5635 mark_addressable (*expr_p);
5640 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
5641 /* C99 code may assign to an array in a structure returned
5642 from a function, and this has undefined behavior only on
5643 execution, so create a temporary if an lvalue is
5645 if (fallback == fb_lvalue)
5647 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5648 mark_addressable (*expr_p);
5656 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
5660 case GIMPLE_MODIFY_STMT:
5662 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
5663 fallback != fb_none);
5667 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer
5669 if (TREE_CODE (*expr_p) == INIT_EXPR)
5670 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
5672 /* Convert MODIFY_EXPR to GIMPLE_MODIFY_STMT. */
5673 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
5674 tree_to_gimple_tuple (expr_p);
5679 case TRUTH_ANDIF_EXPR:
5680 case TRUTH_ORIF_EXPR:
5681 ret = gimplify_boolean_expr (expr_p);
5684 case TRUTH_NOT_EXPR:
5685 TREE_OPERAND (*expr_p, 0)
5686 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
5687 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5688 is_gimple_val, fb_rvalue);
5689 recalculate_side_effects (*expr_p);
5693 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
5697 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
5702 if (IS_EMPTY_STMT (*expr_p))
5708 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
5709 || fallback == fb_none)
5711 /* Just strip a conversion to void (or in void context) and
5713 *expr_p = TREE_OPERAND (*expr_p, 0);
5717 ret = gimplify_conversion (expr_p);
5718 if (ret == GS_ERROR)
5720 if (*expr_p != save_expr)
5724 case FIX_TRUNC_EXPR:
5725 /* unary_expr: ... | '(' cast ')' val | ... */
5726 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5727 is_gimple_val, fb_rvalue);
5728 recalculate_side_effects (*expr_p);
5732 *expr_p = fold_indirect_ref (*expr_p);
5733 if (*expr_p != save_expr)
5735 /* else fall through. */
5736 case ALIGN_INDIRECT_REF:
5737 case MISALIGNED_INDIRECT_REF:
5738 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5739 is_gimple_reg, fb_rvalue);
5740 recalculate_side_effects (*expr_p);
5743 /* Constants need not be gimplified. */
5753 /* If we require an lvalue, such as for ADDR_EXPR, retain the
5754 CONST_DECL node. Otherwise the decl is replaceable by its
5756 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
5757 if (fallback & fb_lvalue)
5760 *expr_p = DECL_INITIAL (*expr_p);
5764 ret = gimplify_decl_expr (expr_p);
5768 /* FIXME make this a decl. */
5773 ret = gimplify_bind_expr (expr_p, pre_p);
5777 ret = gimplify_loop_expr (expr_p, pre_p);
5781 ret = gimplify_switch_expr (expr_p, pre_p);
5785 ret = gimplify_exit_expr (expr_p);
5789 /* If the target is not LABEL, then it is a computed jump
5790 and the target needs to be gimplified. */
5791 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
5792 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
5793 NULL, is_gimple_val, fb_rvalue);
5798 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
5799 == current_function_decl);
5802 case CASE_LABEL_EXPR:
5803 ret = gimplify_case_label_expr (expr_p);
5807 ret = gimplify_return_expr (*expr_p, pre_p);
5811 /* Don't reduce this in place; let gimplify_init_constructor work its
5812 magic. Buf if we're just elaborating this for side effects, just
5813 gimplify any element that has side-effects. */
5814 if (fallback == fb_none)
5816 unsigned HOST_WIDE_INT ix;
5817 constructor_elt *ce;
5818 tree temp = NULL_TREE;
5820 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (*expr_p),
5823 if (TREE_SIDE_EFFECTS (ce->value))
5824 append_to_statement_list (ce->value, &temp);
5829 /* C99 code may assign to an array in a constructed
5830 structure or union, and this has undefined behavior only
5831 on execution, so create a temporary if an lvalue is
5833 else if (fallback == fb_lvalue)
5835 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5836 mark_addressable (*expr_p);
5842 /* The following are special cases that are not handled by the
5843 original GIMPLE grammar. */
5845 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
5848 ret = gimplify_save_expr (expr_p, pre_p, post_p);
5853 enum gimplify_status r0, r1, r2;
5855 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5856 is_gimple_lvalue, fb_either);
5857 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5858 is_gimple_val, fb_rvalue);
5859 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
5860 is_gimple_val, fb_rvalue);
5861 recalculate_side_effects (*expr_p);
5863 ret = MIN (r0, MIN (r1, r2));
5867 case NON_LVALUE_EXPR:
5868 /* This should have been stripped above. */
5872 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
5875 case TRY_FINALLY_EXPR:
5876 case TRY_CATCH_EXPR:
5877 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
5878 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
5882 case CLEANUP_POINT_EXPR:
5883 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
5887 ret = gimplify_target_expr (expr_p, pre_p, post_p);
5891 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
5895 case EH_FILTER_EXPR:
5896 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
5900 case CHANGE_DYNAMIC_TYPE_EXPR:
5901 ret = gimplify_expr (&CHANGE_DYNAMIC_TYPE_LOCATION (*expr_p),
5902 pre_p, post_p, is_gimple_reg, fb_lvalue);
5907 enum gimplify_status r0, r1;
5908 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
5909 is_gimple_val, fb_rvalue);
5910 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
5911 is_gimple_val, fb_rvalue);
5917 /* We get here when taking the address of a label. We mark
5918 the label as "forced"; meaning it can never be removed and
5919 it is a potential target for any computed goto. */
5920 FORCED_LABEL (*expr_p) = 1;
5924 case STATEMENT_LIST:
5925 ret = gimplify_statement_list (expr_p, pre_p);
5928 case WITH_SIZE_EXPR:
5930 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5931 post_p == &internal_post ? NULL : post_p,
5932 gimple_test_f, fallback);
5933 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5934 is_gimple_val, fb_rvalue);
5940 ret = gimplify_var_or_parm_decl (expr_p);
5944 /* When within an OpenMP context, notice uses of variables. */
5945 if (gimplify_omp_ctxp)
5946 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
5951 /* Allow callbacks into the gimplifier during optimization. */
5956 ret = gimplify_omp_parallel (expr_p, pre_p);
5960 ret = gimplify_omp_for (expr_p, pre_p);
5965 ret = gimplify_omp_workshare (expr_p, pre_p);
5972 gimplify_to_stmt_list (&OMP_BODY (*expr_p));
5976 ret = gimplify_omp_atomic (expr_p, pre_p);
5984 case POINTER_PLUS_EXPR:
5985 /* Convert ((type *)A)+offset into &A->field_of_type_and_offset.
5986 The second is gimple immediate saving a need for extra statement.
5988 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
5989 && (tmp = maybe_fold_offset_to_reference
5990 (TREE_OPERAND (*expr_p, 0), TREE_OPERAND (*expr_p, 1),
5991 TREE_TYPE (TREE_TYPE (*expr_p)))))
5993 tree ptr_type = build_pointer_type (TREE_TYPE (tmp));
5994 if (useless_type_conversion_p (TREE_TYPE (*expr_p), ptr_type))
5996 *expr_p = build_fold_addr_expr_with_type (tmp, ptr_type);
6000 /* Convert (void *)&a + 4 into (void *)&a[1]. */
6001 if (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == NOP_EXPR
6002 && TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
6003 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*expr_p,
6005 && (tmp = maybe_fold_offset_to_reference
6006 (TREE_OPERAND (TREE_OPERAND (*expr_p, 0), 0),
6007 TREE_OPERAND (*expr_p, 1),
6008 TREE_TYPE (TREE_TYPE
6009 (TREE_OPERAND (TREE_OPERAND (*expr_p, 0),
6012 tmp = build_fold_addr_expr (tmp);
6013 *expr_p = fold_convert (TREE_TYPE (*expr_p), tmp);
6018 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
6020 case tcc_comparison:
6021 /* Handle comparison of objects of non scalar mode aggregates
6022 with a call to memcmp. It would be nice to only have to do
6023 this for variable-sized objects, but then we'd have to allow
6024 the same nest of reference nodes we allow for MODIFY_EXPR and
6027 Compare scalar mode aggregates as scalar mode values. Using
6028 memcmp for them would be very inefficient at best, and is
6029 plain wrong if bitfields are involved. */
6032 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
6034 if (!AGGREGATE_TYPE_P (type))
6036 else if (TYPE_MODE (type) != BLKmode)
6037 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
6039 ret = gimplify_variable_sized_compare (expr_p);
6044 /* If *EXPR_P does not need to be special-cased, handle it
6045 according to its class. */
6047 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6048 post_p, is_gimple_val, fb_rvalue);
6054 enum gimplify_status r0, r1;
6056 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
6057 post_p, is_gimple_val, fb_rvalue);
6058 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
6059 post_p, is_gimple_val, fb_rvalue);
6065 case tcc_declaration:
6068 goto dont_recalculate;
6071 gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
6072 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
6073 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
6077 recalculate_side_effects (*expr_p);
6082 /* If we replaced *expr_p, gimplify again. */
6083 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
6086 while (ret == GS_OK);
6088 /* If we encountered an error_mark somewhere nested inside, either
6089 stub out the statement or propagate the error back out. */
6090 if (ret == GS_ERROR)
6097 /* This was only valid as a return value from the langhook, which
6098 we handled. Make sure it doesn't escape from any other context. */
6099 gcc_assert (ret != GS_UNHANDLED);
6101 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
6103 /* We aren't looking for a value, and we don't have a valid
6104 statement. If it doesn't have side-effects, throw it away. */
6105 if (!TREE_SIDE_EFFECTS (*expr_p))
6107 else if (!TREE_THIS_VOLATILE (*expr_p))
6109 /* This is probably a _REF that contains something nested that
6110 has side effects. Recurse through the operands to find it. */
6111 enum tree_code code = TREE_CODE (*expr_p);
6118 case VIEW_CONVERT_EXPR:
6119 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6120 gimple_test_f, fallback);
6124 case ARRAY_RANGE_REF:
6125 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
6126 gimple_test_f, fallback);
6127 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
6128 gimple_test_f, fallback);
6132 /* Anything else with side-effects must be converted to
6133 a valid statement before we get here. */
6139 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
6140 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
6142 /* Historically, the compiler has treated a bare reference
6143 to a non-BLKmode volatile lvalue as forcing a load. */
6144 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
6145 /* Normally, we do not want to create a temporary for a
6146 TREE_ADDRESSABLE type because such a type should not be
6147 copied by bitwise-assignment. However, we make an
6148 exception here, as all we are doing here is ensuring that
6149 we read the bytes that make up the type. We use
6150 create_tmp_var_raw because create_tmp_var will abort when
6151 given a TREE_ADDRESSABLE type. */
6152 tree tmp = create_tmp_var_raw (type, "vol");
6153 gimple_add_tmp_var (tmp);
6154 *expr_p = build_gimple_modify_stmt (tmp, *expr_p);
6157 /* We can't do anything useful with a volatile reference to
6158 an incomplete type, so just throw it away. Likewise for
6159 a BLKmode type, since any implicit inner load should
6160 already have been turned into an explicit one by the
6161 gimplification process. */
6165 /* If we are gimplifying at the statement level, we're done. Tack
6166 everything together and replace the original statement with the
6168 if (fallback == fb_none || is_statement)
6170 if (internal_pre || internal_post)
6172 append_to_statement_list (*expr_p, &internal_pre);
6173 append_to_statement_list (internal_post, &internal_pre);
6174 annotate_all_with_locus (&internal_pre, input_location);
6175 *expr_p = internal_pre;
6179 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
6180 annotate_all_with_locus (expr_p, input_location);
6182 annotate_one_with_locus (*expr_p, input_location);
6186 /* Otherwise we're gimplifying a subexpression, so the resulting value is
6189 /* If it's sufficiently simple already, we're done. Unless we are
6190 handling some post-effects internally; if that's the case, we need to
6191 copy into a temp before adding the post-effects to the tree. */
6192 if (!internal_post && (*gimple_test_f) (*expr_p))
6195 /* Otherwise, we need to create a new temporary for the gimplified
6198 /* We can't return an lvalue if we have an internal postqueue. The
6199 object the lvalue refers to would (probably) be modified by the
6200 postqueue; we need to copy the value out first, which means an
6202 if ((fallback & fb_lvalue) && !internal_post
6203 && is_gimple_addressable (*expr_p))
6205 /* An lvalue will do. Take the address of the expression, store it
6206 in a temporary, and replace the expression with an INDIRECT_REF of
6208 tmp = build_fold_addr_expr (*expr_p);
6209 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
6210 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
6212 else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
6214 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
6216 /* An rvalue will do. Assign the gimplified expression into a new
6217 temporary TMP and replace the original expression with TMP. */
6219 if (internal_post || (fallback & fb_lvalue))
6220 /* The postqueue might change the value of the expression between
6221 the initialization and use of the temporary, so we can't use a
6222 formal temp. FIXME do we care? */
6223 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6225 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
6227 if (TREE_CODE (*expr_p) != SSA_NAME)
6228 DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
6232 #ifdef ENABLE_CHECKING
6233 if (!(fallback & fb_mayfail))
6235 fprintf (stderr, "gimplification failed:\n");
6236 print_generic_expr (stderr, *expr_p, 0);
6237 debug_tree (*expr_p);
6238 internal_error ("gimplification failed");
6241 gcc_assert (fallback & fb_mayfail);
6242 /* If this is an asm statement, and the user asked for the
6243 impossible, don't die. Fail and let gimplify_asm_expr
6249 /* Make sure the temporary matches our predicate. */
6250 gcc_assert ((*gimple_test_f) (*expr_p));
6254 annotate_all_with_locus (&internal_post, input_location);
6255 append_to_statement_list (internal_post, pre_p);
6259 input_location = saved_location;
6263 /* Look through TYPE for variable-sized objects and gimplify each such
6264 size that we find. Add to LIST_P any statements generated. */
6267 gimplify_type_sizes (tree type, tree *list_p)
6271 if (type == NULL || type == error_mark_node)
6274 /* We first do the main variant, then copy into any other variants. */
6275 type = TYPE_MAIN_VARIANT (type);
6277 /* Avoid infinite recursion. */
6278 if (TYPE_SIZES_GIMPLIFIED (type))
6281 TYPE_SIZES_GIMPLIFIED (type) = 1;
6283 switch (TREE_CODE (type))
6289 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
6290 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
6292 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6294 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
6295 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
6300 /* These types may not have declarations, so handle them here. */
6301 gimplify_type_sizes (TREE_TYPE (type), list_p);
6302 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
6307 case QUAL_UNION_TYPE:
6308 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6309 if (TREE_CODE (field) == FIELD_DECL)
6311 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
6312 gimplify_type_sizes (TREE_TYPE (field), list_p);
6317 case REFERENCE_TYPE:
6318 /* We used to recurse on the pointed-to type here, which turned out to
6319 be incorrect because its definition might refer to variables not
6320 yet initialized at this point if a forward declaration is involved.
6322 It was actually useful for anonymous pointed-to types to ensure
6323 that the sizes evaluation dominates every possible later use of the
6324 values. Restricting to such types here would be safe since there
6325 is no possible forward declaration around, but would introduce an
6326 undesirable middle-end semantic to anonymity. We then defer to
6327 front-ends the responsibility of ensuring that the sizes are
6328 evaluated both early and late enough, e.g. by attaching artificial
6329 type declarations to the tree. */
6336 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
6337 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
6339 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6341 TYPE_SIZE (t) = TYPE_SIZE (type);
6342 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
6343 TYPE_SIZES_GIMPLIFIED (t) = 1;
6347 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
6348 a size or position, has had all of its SAVE_EXPRs evaluated.
6349 We add any required statements to STMT_P. */
6352 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
6354 tree type, expr = *expr_p;
6356 /* We don't do anything if the value isn't there, is constant, or contains
6357 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
6358 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
6359 will want to replace it with a new variable, but that will cause problems
6360 if this type is from outside the function. It's OK to have that here. */
6361 if (expr == NULL_TREE || TREE_CONSTANT (expr)
6362 || TREE_CODE (expr) == VAR_DECL
6363 || CONTAINS_PLACEHOLDER_P (expr))
6366 type = TREE_TYPE (expr);
6367 *expr_p = unshare_expr (expr);
6369 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
6372 /* Verify that we've an exact type match with the original expression.
6373 In particular, we do not wish to drop a "sizetype" in favour of a
6374 type of similar dimensions. We don't want to pollute the generic
6375 type-stripping code with this knowledge because it doesn't matter
6376 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
6377 and friends retain their "sizetype-ness". */
6378 if (TREE_TYPE (expr) != type
6379 && TREE_CODE (type) == INTEGER_TYPE
6380 && TYPE_IS_SIZETYPE (type))
6384 *expr_p = create_tmp_var (type, NULL);
6385 tmp = build1 (NOP_EXPR, type, expr);
6386 tmp = build_gimple_modify_stmt (*expr_p, tmp);
6387 if (EXPR_HAS_LOCATION (expr))
6388 SET_EXPR_LOCUS (tmp, EXPR_LOCUS (expr));
6390 SET_EXPR_LOCATION (tmp, input_location);
6392 gimplify_and_add (tmp, stmt_p);
6396 #ifdef ENABLE_CHECKING
6397 /* Compare types A and B for a "close enough" match. */
6400 cpt_same_type (tree a, tree b)
6402 if (useless_type_conversion_p (a, b))
6405 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
6406 link them together. This routine is intended to catch type errors
6407 that will affect the optimizers, and the optimizers don't add new
6408 dereferences of function pointers, so ignore it. */
6409 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
6410 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
6413 /* ??? The C FE pushes type qualifiers after the fact into the type of
6414 the element from the type of the array. See build_unary_op's handling
6415 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
6416 should have done it when creating the variable in the first place.
6417 Alternately, why aren't the two array types made variants? */
6418 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
6419 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6421 /* And because of those, we have to recurse down through pointers. */
6422 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
6423 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6428 /* Check for some cases of the front end missing cast expressions.
6429 The type of a dereference should correspond to the pointer type;
6430 similarly the type of an address should match its object. */
6433 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
6434 void *data ATTRIBUTE_UNUSED)
6437 tree ptype, otype, dtype;
6439 switch (TREE_CODE (t))
6443 otype = TREE_TYPE (t);
6444 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
6445 dtype = TREE_TYPE (ptype);
6446 gcc_assert (cpt_same_type (otype, dtype));
6450 ptype = TREE_TYPE (t);
6451 otype = TREE_TYPE (TREE_OPERAND (t, 0));
6452 dtype = TREE_TYPE (ptype);
6453 if (!cpt_same_type (dtype, otype))
6455 /* &array is allowed to produce a pointer to the element, rather than
6456 a pointer to the array type. We must allow this in order to
6457 properly represent assigning the address of an array in C into
6458 pointer to the element type. */
6459 gcc_assert (TREE_CODE (otype) == ARRAY_TYPE
6460 && POINTER_TYPE_P (ptype)
6461 && cpt_same_type (dtype, TREE_TYPE (otype)));
6475 /* Gimplify the body of statements pointed to by BODY_P. FNDECL is the
6476 function decl containing BODY. */
6479 gimplify_body (tree *body_p, tree fndecl, bool do_parms)
6481 location_t saved_location = input_location;
6482 tree body, parm_stmts;
6484 timevar_push (TV_TREE_GIMPLIFY);
6486 gcc_assert (gimplify_ctxp == NULL);
6487 push_gimplify_context ();
6489 /* Unshare most shared trees in the body and in that of any nested functions.
6490 It would seem we don't have to do this for nested functions because
6491 they are supposed to be output and then the outer function gimplified
6492 first, but the g++ front end doesn't always do it that way. */
6493 unshare_body (body_p, fndecl);
6494 unvisit_body (body_p, fndecl);
6496 /* Make sure input_location isn't set to something wierd. */
6497 input_location = DECL_SOURCE_LOCATION (fndecl);
6499 /* Resolve callee-copies. This has to be done before processing
6500 the body so that DECL_VALUE_EXPR gets processed correctly. */
6501 parm_stmts = do_parms ? gimplify_parameters () : NULL;
6503 /* Gimplify the function's body. */
6504 gimplify_stmt (body_p);
6508 body = alloc_stmt_list ();
6509 else if (TREE_CODE (body) == STATEMENT_LIST)
6511 tree t = expr_only (*body_p);
6516 /* If there isn't an outer BIND_EXPR, add one. */
6517 if (TREE_CODE (body) != BIND_EXPR)
6519 tree b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6520 NULL_TREE, NULL_TREE);
6521 TREE_SIDE_EFFECTS (b) = 1;
6522 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
6526 /* If we had callee-copies statements, insert them at the beginning
6530 append_to_statement_list_force (BIND_EXPR_BODY (body), &parm_stmts);
6531 BIND_EXPR_BODY (body) = parm_stmts;
6534 /* Unshare again, in case gimplification was sloppy. */
6535 unshare_all_trees (body);
6539 pop_gimplify_context (body);
6540 gcc_assert (gimplify_ctxp == NULL);
6542 #ifdef ENABLE_CHECKING
6543 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
6546 timevar_pop (TV_TREE_GIMPLIFY);
6547 input_location = saved_location;
6550 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
6551 node for the function we want to gimplify. */
6554 gimplify_function_tree (tree fndecl)
6556 tree oldfn, parm, ret;
6558 oldfn = current_function_decl;
6559 current_function_decl = fndecl;
6560 cfun = DECL_STRUCT_FUNCTION (fndecl);
6562 allocate_struct_function (fndecl);
6564 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = TREE_CHAIN (parm))
6566 /* Preliminarily mark non-addressed complex variables as eligible
6567 for promotion to gimple registers. We'll transform their uses
6569 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
6570 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
6571 && !TREE_THIS_VOLATILE (parm)
6572 && !needs_to_live_in_memory (parm))
6573 DECL_GIMPLE_REG_P (parm) = 1;
6576 ret = DECL_RESULT (fndecl);
6577 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
6578 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
6579 && !needs_to_live_in_memory (ret))
6580 DECL_GIMPLE_REG_P (ret) = 1;
6582 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl, true);
6584 /* If we're instrumenting function entry/exit, then prepend the call to
6585 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
6586 catch the exit hook. */
6587 /* ??? Add some way to ignore exceptions for this TFE. */
6588 if (flag_instrument_function_entry_exit
6589 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
6593 tf = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
6594 TREE_SIDE_EFFECTS (tf) = 1;
6595 x = DECL_SAVED_TREE (fndecl);
6596 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
6597 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
6598 x = build_call_expr (x, 0);
6599 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
6601 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6602 TREE_SIDE_EFFECTS (bind) = 1;
6603 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
6604 x = build_call_expr (x, 0);
6605 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
6606 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
6608 DECL_SAVED_TREE (fndecl) = bind;
6611 cfun->gimplified = true;
6612 current_function_decl = oldfn;
6613 cfun = oldfn ? DECL_STRUCT_FUNCTION (oldfn) : NULL;
6616 /* Expands EXPR to list of gimple statements STMTS. If SIMPLE is true,
6617 force the result to be either ssa_name or an invariant, otherwise
6618 just force it to be a rhs expression. If VAR is not NULL, make the
6619 base variable of the final destination be VAR if suitable. */
6622 force_gimple_operand (tree expr, tree *stmts, bool simple, tree var)
6625 enum gimplify_status ret;
6626 gimple_predicate gimple_test_f;
6630 if (is_gimple_val (expr))
6633 gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
6635 push_gimplify_context ();
6636 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
6639 expr = build_gimple_modify_stmt (var, expr);
6641 ret = gimplify_expr (&expr, stmts, NULL,
6642 gimple_test_f, fb_rvalue);
6643 gcc_assert (ret != GS_ERROR);
6645 if (gimple_referenced_vars (cfun))
6647 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
6648 add_referenced_var (t);
6651 pop_gimplify_context (NULL);
6656 /* Invokes force_gimple_operand for EXPR with parameters SIMPLE_P and VAR. If
6657 some statements are produced, emits them at BSI. If BEFORE is true.
6658 the statements are appended before BSI, otherwise they are appended after
6659 it. M specifies the way BSI moves after insertion (BSI_SAME_STMT or
6660 BSI_CONTINUE_LINKING are the usual values). */
6663 force_gimple_operand_bsi (block_stmt_iterator *bsi, tree expr,
6664 bool simple_p, tree var, bool before,
6665 enum bsi_iterator_update m)
6669 expr = force_gimple_operand (expr, &stmts, simple_p, var);
6672 if (gimple_in_ssa_p (cfun))
6674 tree_stmt_iterator tsi;
6676 for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
6677 mark_symbols_for_renaming (tsi_stmt (tsi));
6681 bsi_insert_before (bsi, stmts, m);
6683 bsi_insert_after (bsi, stmts, m);
6689 #include "gt-gimplify.h"