* tree-ssa-operands.h (struct ssa_operand_memory_d):
[official-gcc.git] / gcc / gimplify.c
blob97745309e98f6d6db61ec9e2b4e7d7ec4e44fa44
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 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "rtl.h"
30 #include "varray.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"
37 #include "cgraph.h"
38 #include "timevar.h"
39 #include "except.h"
40 #include "hashtab.h"
41 #include "flags.h"
42 #include "real.h"
43 #include "function.h"
44 #include "output.h"
45 #include "expr.h"
46 #include "ggc.h"
47 #include "toplev.h"
48 #include "target.h"
49 #include "optabs.h"
50 #include "pointer-set.h"
53 enum gimplify_omp_var_data
55 GOVD_SEEN = 1,
56 GOVD_EXPLICIT = 2,
57 GOVD_SHARED = 4,
58 GOVD_PRIVATE = 8,
59 GOVD_FIRSTPRIVATE = 16,
60 GOVD_LASTPRIVATE = 32,
61 GOVD_REDUCTION = 64,
62 GOVD_LOCAL = 128,
63 GOVD_DEBUG_PRIVATE = 256,
64 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
65 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
68 struct gimplify_omp_ctx
70 struct gimplify_omp_ctx *outer_context;
71 splay_tree variables;
72 struct pointer_set_t *privatized_types;
73 location_t location;
74 enum omp_clause_default_kind default_kind;
75 bool is_parallel;
76 bool is_combined_parallel;
79 struct gimplify_ctx
81 struct gimplify_ctx *prev_context;
83 tree current_bind_expr;
84 tree temps;
85 tree conditional_cleanups;
86 tree exit_label;
87 tree return_temp;
89 VEC(tree,heap) *case_labels;
90 /* The formal temporary table. Should this be persistent? */
91 htab_t temp_htab;
93 int conditions;
94 bool save_stack;
95 bool into_ssa;
98 static struct gimplify_ctx *gimplify_ctxp;
99 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
103 /* Formal (expression) temporary table handling: Multiple occurrences of
104 the same scalar expression are evaluated into the same temporary. */
106 typedef struct gimple_temp_hash_elt
108 tree val; /* Key */
109 tree temp; /* Value */
110 } elt_t;
112 /* Forward declarations. */
113 static enum gimplify_status gimplify_compound_expr (tree *, tree *, bool);
114 #ifdef ENABLE_CHECKING
115 static bool cpt_same_type (tree a, tree b);
116 #endif
119 /* Return a hash value for a formal temporary table entry. */
121 static hashval_t
122 gimple_tree_hash (const void *p)
124 tree t = ((const elt_t *) p)->val;
125 return iterative_hash_expr (t, 0);
128 /* Compare two formal temporary table entries. */
130 static int
131 gimple_tree_eq (const void *p1, const void *p2)
133 tree t1 = ((const elt_t *) p1)->val;
134 tree t2 = ((const elt_t *) p2)->val;
135 enum tree_code code = TREE_CODE (t1);
137 if (TREE_CODE (t2) != code
138 || TREE_TYPE (t1) != TREE_TYPE (t2))
139 return 0;
141 if (!operand_equal_p (t1, t2, 0))
142 return 0;
144 /* Only allow them to compare equal if they also hash equal; otherwise
145 results are nondeterminate, and we fail bootstrap comparison. */
146 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
148 return 1;
151 /* Set up a context for the gimplifier. */
153 void
154 push_gimplify_context (void)
156 struct gimplify_ctx *c;
158 c = (struct gimplify_ctx *) xcalloc (1, sizeof (struct gimplify_ctx));
159 c->prev_context = gimplify_ctxp;
160 if (optimize)
161 c->temp_htab = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
163 gimplify_ctxp = c;
166 /* Tear down a context for the gimplifier. If BODY is non-null, then
167 put the temporaries into the outer BIND_EXPR. Otherwise, put them
168 in the unexpanded_var_list. */
170 void
171 pop_gimplify_context (tree body)
173 struct gimplify_ctx *c = gimplify_ctxp;
174 tree t;
176 gcc_assert (c && !c->current_bind_expr);
177 gimplify_ctxp = c->prev_context;
179 for (t = c->temps; t ; t = TREE_CHAIN (t))
180 DECL_GIMPLE_FORMAL_TEMP_P (t) = 0;
182 if (body)
183 declare_vars (c->temps, body, false);
184 else
185 record_vars (c->temps);
187 if (optimize)
188 htab_delete (c->temp_htab);
189 free (c);
192 static void
193 gimple_push_bind_expr (tree bind)
195 TREE_CHAIN (bind) = gimplify_ctxp->current_bind_expr;
196 gimplify_ctxp->current_bind_expr = bind;
199 static void
200 gimple_pop_bind_expr (void)
202 gimplify_ctxp->current_bind_expr
203 = TREE_CHAIN (gimplify_ctxp->current_bind_expr);
206 tree
207 gimple_current_bind_expr (void)
209 return gimplify_ctxp->current_bind_expr;
212 /* Returns true iff there is a COND_EXPR between us and the innermost
213 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
215 static bool
216 gimple_conditional_context (void)
218 return gimplify_ctxp->conditions > 0;
221 /* Note that we've entered a COND_EXPR. */
223 static void
224 gimple_push_condition (void)
226 #ifdef ENABLE_CHECKING
227 if (gimplify_ctxp->conditions == 0)
228 gcc_assert (!gimplify_ctxp->conditional_cleanups);
229 #endif
230 ++(gimplify_ctxp->conditions);
233 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
234 now, add any conditional cleanups we've seen to the prequeue. */
236 static void
237 gimple_pop_condition (tree *pre_p)
239 int conds = --(gimplify_ctxp->conditions);
241 gcc_assert (conds >= 0);
242 if (conds == 0)
244 append_to_statement_list (gimplify_ctxp->conditional_cleanups, pre_p);
245 gimplify_ctxp->conditional_cleanups = NULL_TREE;
249 /* A stable comparison routine for use with splay trees and DECLs. */
251 static int
252 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
254 tree a = (tree) xa;
255 tree b = (tree) xb;
257 return DECL_UID (a) - DECL_UID (b);
260 /* Create a new omp construct that deals with variable remapping. */
262 static struct gimplify_omp_ctx *
263 new_omp_context (bool is_parallel, bool is_combined_parallel)
265 struct gimplify_omp_ctx *c;
267 c = XCNEW (struct gimplify_omp_ctx);
268 c->outer_context = gimplify_omp_ctxp;
269 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
270 c->privatized_types = pointer_set_create ();
271 c->location = input_location;
272 c->is_parallel = is_parallel;
273 c->is_combined_parallel = is_combined_parallel;
274 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
276 return c;
279 /* Destroy an omp construct that deals with variable remapping. */
281 static void
282 delete_omp_context (struct gimplify_omp_ctx *c)
284 splay_tree_delete (c->variables);
285 pointer_set_destroy (c->privatized_types);
286 XDELETE (c);
289 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
290 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
292 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
294 static void
295 append_to_statement_list_1 (tree t, tree *list_p)
297 tree list = *list_p;
298 tree_stmt_iterator i;
300 if (!list)
302 if (t && TREE_CODE (t) == STATEMENT_LIST)
304 *list_p = t;
305 return;
307 *list_p = list = alloc_stmt_list ();
310 i = tsi_last (list);
311 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
314 /* Add T to the end of the list container pointed to by LIST_P.
315 If T is an expression with no effects, it is ignored. */
317 void
318 append_to_statement_list (tree t, tree *list_p)
320 if (t && TREE_SIDE_EFFECTS (t))
321 append_to_statement_list_1 (t, list_p);
324 /* Similar, but the statement is always added, regardless of side effects. */
326 void
327 append_to_statement_list_force (tree t, tree *list_p)
329 if (t != NULL_TREE)
330 append_to_statement_list_1 (t, list_p);
333 /* Both gimplify the statement T and append it to LIST_P. */
335 void
336 gimplify_and_add (tree t, tree *list_p)
338 gimplify_stmt (&t);
339 append_to_statement_list (t, list_p);
342 /* Strip off a legitimate source ending from the input string NAME of
343 length LEN. Rather than having to know the names used by all of
344 our front ends, we strip off an ending of a period followed by
345 up to five characters. (Java uses ".class".) */
347 static inline void
348 remove_suffix (char *name, int len)
350 int i;
352 for (i = 2; i < 8 && len > i; i++)
354 if (name[len - i] == '.')
356 name[len - i] = '\0';
357 break;
362 /* Create a nameless artificial label and put it in the current function
363 context. Returns the newly created label. */
365 tree
366 create_artificial_label (void)
368 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
370 DECL_ARTIFICIAL (lab) = 1;
371 DECL_IGNORED_P (lab) = 1;
372 DECL_CONTEXT (lab) = current_function_decl;
373 return lab;
376 /* Subroutine for find_single_pointer_decl. */
378 static tree
379 find_single_pointer_decl_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
380 void *data)
382 tree *pdecl = (tree *) data;
384 if (DECL_P (*tp) && POINTER_TYPE_P (TREE_TYPE (*tp)))
386 if (*pdecl)
388 /* We already found a pointer decl; return anything other
389 than NULL_TREE to unwind from walk_tree signalling that
390 we have a duplicate. */
391 return *tp;
393 *pdecl = *tp;
396 return NULL_TREE;
399 /* Find the single DECL of pointer type in the tree T and return it.
400 If there are zero or more than one such DECLs, return NULL. */
402 static tree
403 find_single_pointer_decl (tree t)
405 tree decl = NULL_TREE;
407 if (walk_tree (&t, find_single_pointer_decl_1, &decl, NULL))
409 /* find_single_pointer_decl_1 returns a nonzero value, causing
410 walk_tree to return a nonzero value, to indicate that it
411 found more than one pointer DECL. */
412 return NULL_TREE;
415 return decl;
418 /* Create a new temporary name with PREFIX. Returns an identifier. */
420 static GTY(()) unsigned int tmp_var_id_num;
422 tree
423 create_tmp_var_name (const char *prefix)
425 char *tmp_name;
427 if (prefix)
429 char *preftmp = ASTRDUP (prefix);
431 remove_suffix (preftmp, strlen (preftmp));
432 prefix = preftmp;
435 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
436 return get_identifier (tmp_name);
440 /* Create a new temporary variable declaration of type TYPE.
441 Does NOT push it into the current binding. */
443 tree
444 create_tmp_var_raw (tree type, const char *prefix)
446 tree tmp_var;
447 tree new_type;
449 /* Make the type of the variable writable. */
450 new_type = build_type_variant (type, 0, 0);
451 TYPE_ATTRIBUTES (new_type) = TYPE_ATTRIBUTES (type);
453 tmp_var = build_decl (VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
454 type);
456 /* The variable was declared by the compiler. */
457 DECL_ARTIFICIAL (tmp_var) = 1;
458 /* And we don't want debug info for it. */
459 DECL_IGNORED_P (tmp_var) = 1;
461 /* Make the variable writable. */
462 TREE_READONLY (tmp_var) = 0;
464 DECL_EXTERNAL (tmp_var) = 0;
465 TREE_STATIC (tmp_var) = 0;
466 TREE_USED (tmp_var) = 1;
468 return tmp_var;
471 /* Create a new temporary variable declaration of type TYPE. DOES push the
472 variable into the current binding. Further, assume that this is called
473 only from gimplification or optimization, at which point the creation of
474 certain types are bugs. */
476 tree
477 create_tmp_var (tree type, const char *prefix)
479 tree tmp_var;
481 /* We don't allow types that are addressable (meaning we can't make copies),
482 or incomplete. We also used to reject every variable size objects here,
483 but now support those for which a constant upper bound can be obtained.
484 The processing for variable sizes is performed in gimple_add_tmp_var,
485 point at which it really matters and possibly reached via paths not going
486 through this function, e.g. after direct calls to create_tmp_var_raw. */
487 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
489 tmp_var = create_tmp_var_raw (type, prefix);
490 gimple_add_tmp_var (tmp_var);
491 return tmp_var;
494 /* Given a tree, try to return a useful variable name that we can use
495 to prefix a temporary that is being assigned the value of the tree.
496 I.E. given <temp> = &A, return A. */
498 const char *
499 get_name (tree t)
501 tree stripped_decl;
503 stripped_decl = t;
504 STRIP_NOPS (stripped_decl);
505 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
506 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
507 else
509 switch (TREE_CODE (stripped_decl))
511 case ADDR_EXPR:
512 return get_name (TREE_OPERAND (stripped_decl, 0));
513 default:
514 return NULL;
519 /* Create a temporary with a name derived from VAL. Subroutine of
520 lookup_tmp_var; nobody else should call this function. */
522 static inline tree
523 create_tmp_from_val (tree val)
525 return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
528 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
529 an existing expression temporary. */
531 static tree
532 lookup_tmp_var (tree val, bool is_formal)
534 tree ret;
536 /* If not optimizing, never really reuse a temporary. local-alloc
537 won't allocate any variable that is used in more than one basic
538 block, which means it will go into memory, causing much extra
539 work in reload and final and poorer code generation, outweighing
540 the extra memory allocation here. */
541 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
542 ret = create_tmp_from_val (val);
543 else
545 elt_t elt, *elt_p;
546 void **slot;
548 elt.val = val;
549 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
550 if (*slot == NULL)
552 elt_p = XNEW (elt_t);
553 elt_p->val = val;
554 elt_p->temp = ret = create_tmp_from_val (val);
555 *slot = (void *) elt_p;
557 else
559 elt_p = (elt_t *) *slot;
560 ret = elt_p->temp;
564 if (is_formal)
565 DECL_GIMPLE_FORMAL_TEMP_P (ret) = 1;
567 return ret;
570 /* Returns a formal temporary variable initialized with VAL. PRE_P is as
571 in gimplify_expr. Only use this function if:
573 1) The value of the unfactored expression represented by VAL will not
574 change between the initialization and use of the temporary, and
575 2) The temporary will not be otherwise modified.
577 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
578 and #2 means it is inappropriate for && temps.
580 For other cases, use get_initialized_tmp_var instead. */
582 static tree
583 internal_get_tmp_var (tree val, tree *pre_p, tree *post_p, bool is_formal)
585 tree t, mod;
587 gimplify_expr (&val, pre_p, post_p, is_gimple_formal_tmp_rhs, fb_rvalue);
589 t = lookup_tmp_var (val, is_formal);
591 if (is_formal)
593 tree u = find_single_pointer_decl (val);
595 if (u && TREE_CODE (u) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (u))
596 u = DECL_GET_RESTRICT_BASE (u);
597 if (u && TYPE_RESTRICT (TREE_TYPE (u)))
599 if (DECL_BASED_ON_RESTRICT_P (t))
600 gcc_assert (u == DECL_GET_RESTRICT_BASE (t));
601 else
603 DECL_BASED_ON_RESTRICT_P (t) = 1;
604 SET_DECL_RESTRICT_BASE (t, u);
609 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
610 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
611 DECL_GIMPLE_REG_P (t) = 1;
613 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
615 if (EXPR_HAS_LOCATION (val))
616 SET_EXPR_LOCUS (mod, EXPR_LOCUS (val));
617 else
618 SET_EXPR_LOCATION (mod, input_location);
620 /* gimplify_modify_expr might want to reduce this further. */
621 gimplify_and_add (mod, pre_p);
623 /* If we're gimplifying into ssa, gimplify_modify_expr will have
624 given our temporary an ssa name. Find and return it. */
625 if (gimplify_ctxp->into_ssa)
626 t = TREE_OPERAND (mod, 0);
628 return t;
631 /* Returns a formal temporary variable initialized with VAL. PRE_P
632 points to a statement list where side-effects needed to compute VAL
633 should be stored. */
635 tree
636 get_formal_tmp_var (tree val, tree *pre_p)
638 return internal_get_tmp_var (val, pre_p, NULL, true);
641 /* Returns a temporary variable initialized with VAL. PRE_P and POST_P
642 are as in gimplify_expr. */
644 tree
645 get_initialized_tmp_var (tree val, tree *pre_p, tree *post_p)
647 return internal_get_tmp_var (val, pre_p, post_p, false);
650 /* Declares all the variables in VARS in SCOPE. If DEBUG_INFO is
651 true, generate debug info for them; otherwise don't. */
653 void
654 declare_vars (tree vars, tree scope, bool debug_info)
656 tree last = vars;
657 if (last)
659 tree temps, block;
661 /* C99 mode puts the default 'return 0;' for main outside the outer
662 braces. So drill down until we find an actual scope. */
663 while (TREE_CODE (scope) == COMPOUND_EXPR)
664 scope = TREE_OPERAND (scope, 0);
666 gcc_assert (TREE_CODE (scope) == BIND_EXPR);
668 temps = nreverse (last);
670 block = BIND_EXPR_BLOCK (scope);
671 if (!block || !debug_info)
673 TREE_CHAIN (last) = BIND_EXPR_VARS (scope);
674 BIND_EXPR_VARS (scope) = temps;
676 else
678 /* We need to attach the nodes both to the BIND_EXPR and to its
679 associated BLOCK for debugging purposes. The key point here
680 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
681 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
682 if (BLOCK_VARS (block))
683 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
684 else
686 BIND_EXPR_VARS (scope) = chainon (BIND_EXPR_VARS (scope), temps);
687 BLOCK_VARS (block) = temps;
693 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
694 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
695 no such upper bound can be obtained. */
697 static void
698 force_constant_size (tree var)
700 /* The only attempt we make is by querying the maximum size of objects
701 of the variable's type. */
703 HOST_WIDE_INT max_size;
705 gcc_assert (TREE_CODE (var) == VAR_DECL);
707 max_size = max_int_size_in_bytes (TREE_TYPE (var));
709 gcc_assert (max_size >= 0);
711 DECL_SIZE_UNIT (var)
712 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
713 DECL_SIZE (var)
714 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
717 void
718 gimple_add_tmp_var (tree tmp)
720 gcc_assert (!TREE_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
722 /* Later processing assumes that the object size is constant, which might
723 not be true at this point. Force the use of a constant upper bound in
724 this case. */
725 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
726 force_constant_size (tmp);
728 DECL_CONTEXT (tmp) = current_function_decl;
729 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
731 if (gimplify_ctxp)
733 TREE_CHAIN (tmp) = gimplify_ctxp->temps;
734 gimplify_ctxp->temps = tmp;
736 /* Mark temporaries local within the nearest enclosing parallel. */
737 if (gimplify_omp_ctxp)
739 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
740 while (ctx && !ctx->is_parallel)
741 ctx = ctx->outer_context;
742 if (ctx)
743 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
746 else if (cfun)
747 record_vars (tmp);
748 else
749 declare_vars (tmp, DECL_SAVED_TREE (current_function_decl), false);
752 /* Determines whether to assign a locus to the statement STMT. */
754 static bool
755 should_carry_locus_p (tree stmt)
757 /* Don't emit a line note for a label. We particularly don't want to
758 emit one for the break label, since it doesn't actually correspond
759 to the beginning of the loop/switch. */
760 if (TREE_CODE (stmt) == LABEL_EXPR)
761 return false;
763 /* Do not annotate empty statements, since it confuses gcov. */
764 if (!TREE_SIDE_EFFECTS (stmt))
765 return false;
767 return true;
770 static void
771 annotate_one_with_locus (tree t, location_t locus)
773 if (CAN_HAVE_LOCATION_P (t)
774 && ! EXPR_HAS_LOCATION (t) && should_carry_locus_p (t))
775 SET_EXPR_LOCATION (t, locus);
778 void
779 annotate_all_with_locus (tree *stmt_p, location_t locus)
781 tree_stmt_iterator i;
783 if (!*stmt_p)
784 return;
786 for (i = tsi_start (*stmt_p); !tsi_end_p (i); tsi_next (&i))
788 tree t = tsi_stmt (i);
790 /* Assuming we've already been gimplified, we shouldn't
791 see nested chaining constructs anymore. */
792 gcc_assert (TREE_CODE (t) != STATEMENT_LIST
793 && TREE_CODE (t) != COMPOUND_EXPR);
795 annotate_one_with_locus (t, locus);
799 /* Similar to copy_tree_r() but do not copy SAVE_EXPR or TARGET_EXPR nodes.
800 These nodes model computations that should only be done once. If we
801 were to unshare something like SAVE_EXPR(i++), the gimplification
802 process would create wrong code. */
804 static tree
805 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
807 enum tree_code code = TREE_CODE (*tp);
808 /* Don't unshare types, decls, constants and SAVE_EXPR nodes. */
809 if (TREE_CODE_CLASS (code) == tcc_type
810 || TREE_CODE_CLASS (code) == tcc_declaration
811 || TREE_CODE_CLASS (code) == tcc_constant
812 || code == SAVE_EXPR || code == TARGET_EXPR
813 /* We can't do anything sensible with a BLOCK used as an expression,
814 but we also can't just die when we see it because of non-expression
815 uses. So just avert our eyes and cross our fingers. Silly Java. */
816 || code == BLOCK)
817 *walk_subtrees = 0;
818 else
820 gcc_assert (code != BIND_EXPR);
821 copy_tree_r (tp, walk_subtrees, data);
824 return NULL_TREE;
827 /* Callback for walk_tree to unshare most of the shared trees rooted at
828 *TP. If *TP has been visited already (i.e., TREE_VISITED (*TP) == 1),
829 then *TP is deep copied by calling copy_tree_r.
831 This unshares the same trees as copy_tree_r with the exception of
832 SAVE_EXPR nodes. These nodes model computations that should only be
833 done once. If we were to unshare something like SAVE_EXPR(i++), the
834 gimplification process would create wrong code. */
836 static tree
837 copy_if_shared_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
838 void *data ATTRIBUTE_UNUSED)
840 tree t = *tp;
841 enum tree_code code = TREE_CODE (t);
843 /* Skip types, decls, and constants. But we do want to look at their
844 types and the bounds of types. Mark them as visited so we properly
845 unmark their subtrees on the unmark pass. If we've already seen them,
846 don't look down further. */
847 if (TREE_CODE_CLASS (code) == tcc_type
848 || TREE_CODE_CLASS (code) == tcc_declaration
849 || TREE_CODE_CLASS (code) == tcc_constant)
851 if (TREE_VISITED (t))
852 *walk_subtrees = 0;
853 else
854 TREE_VISITED (t) = 1;
857 /* If this node has been visited already, unshare it and don't look
858 any deeper. */
859 else if (TREE_VISITED (t))
861 walk_tree (tp, mostly_copy_tree_r, NULL, NULL);
862 *walk_subtrees = 0;
865 /* Otherwise, mark the tree as visited and keep looking. */
866 else
867 TREE_VISITED (t) = 1;
869 return NULL_TREE;
872 static tree
873 unmark_visited_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
874 void *data ATTRIBUTE_UNUSED)
876 if (TREE_VISITED (*tp))
877 TREE_VISITED (*tp) = 0;
878 else
879 *walk_subtrees = 0;
881 return NULL_TREE;
884 /* Unshare all the trees in BODY_P, a pointer into the body of FNDECL, and the
885 bodies of any nested functions if we are unsharing the entire body of
886 FNDECL. */
888 static void
889 unshare_body (tree *body_p, tree fndecl)
891 struct cgraph_node *cgn = cgraph_node (fndecl);
893 walk_tree (body_p, copy_if_shared_r, NULL, NULL);
894 if (body_p == &DECL_SAVED_TREE (fndecl))
895 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
896 unshare_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
899 /* Likewise, but mark all trees as not visited. */
901 static void
902 unvisit_body (tree *body_p, tree fndecl)
904 struct cgraph_node *cgn = cgraph_node (fndecl);
906 walk_tree (body_p, unmark_visited_r, NULL, NULL);
907 if (body_p == &DECL_SAVED_TREE (fndecl))
908 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
909 unvisit_body (&DECL_SAVED_TREE (cgn->decl), cgn->decl);
912 /* Unshare T and all the trees reached from T via TREE_CHAIN. */
914 static void
915 unshare_all_trees (tree t)
917 walk_tree (&t, copy_if_shared_r, NULL, NULL);
918 walk_tree (&t, unmark_visited_r, NULL, NULL);
921 /* Unconditionally make an unshared copy of EXPR. This is used when using
922 stored expressions which span multiple functions, such as BINFO_VTABLE,
923 as the normal unsharing process can't tell that they're shared. */
925 tree
926 unshare_expr (tree expr)
928 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
929 return expr;
932 /* A terser interface for building a representation of an exception
933 specification. */
935 tree
936 gimple_build_eh_filter (tree body, tree allowed, tree failure)
938 tree t;
940 /* FIXME should the allowed types go in TREE_TYPE? */
941 t = build2 (EH_FILTER_EXPR, void_type_node, allowed, NULL_TREE);
942 append_to_statement_list (failure, &EH_FILTER_FAILURE (t));
944 t = build2 (TRY_CATCH_EXPR, void_type_node, NULL_TREE, t);
945 append_to_statement_list (body, &TREE_OPERAND (t, 0));
947 return t;
951 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
952 contain statements and have a value. Assign its value to a temporary
953 and give it void_type_node. Returns the temporary, or NULL_TREE if
954 WRAPPER was already void. */
956 tree
957 voidify_wrapper_expr (tree wrapper, tree temp)
959 tree type = TREE_TYPE (wrapper);
960 if (type && !VOID_TYPE_P (type))
962 tree *p;
964 /* Set p to point to the body of the wrapper. Loop until we find
965 something that isn't a wrapper. */
966 for (p = &wrapper; p && *p; )
968 switch (TREE_CODE (*p))
970 case BIND_EXPR:
971 TREE_SIDE_EFFECTS (*p) = 1;
972 TREE_TYPE (*p) = void_type_node;
973 /* For a BIND_EXPR, the body is operand 1. */
974 p = &BIND_EXPR_BODY (*p);
975 break;
977 case CLEANUP_POINT_EXPR:
978 case TRY_FINALLY_EXPR:
979 case TRY_CATCH_EXPR:
980 TREE_SIDE_EFFECTS (*p) = 1;
981 TREE_TYPE (*p) = void_type_node;
982 p = &TREE_OPERAND (*p, 0);
983 break;
985 case STATEMENT_LIST:
987 tree_stmt_iterator i = tsi_last (*p);
988 TREE_SIDE_EFFECTS (*p) = 1;
989 TREE_TYPE (*p) = void_type_node;
990 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
992 break;
994 case COMPOUND_EXPR:
995 /* Advance to the last statement. Set all container types to void. */
996 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
998 TREE_SIDE_EFFECTS (*p) = 1;
999 TREE_TYPE (*p) = void_type_node;
1001 break;
1003 default:
1004 goto out;
1008 out:
1009 if (p == NULL || IS_EMPTY_STMT (*p))
1010 temp = NULL_TREE;
1011 else if (temp)
1013 /* The wrapper is on the RHS of an assignment that we're pushing
1014 down. */
1015 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1016 || TREE_CODE (temp) == MODIFY_EXPR);
1017 TREE_OPERAND (temp, 1) = *p;
1018 *p = temp;
1020 else
1022 temp = create_tmp_var (type, "retval");
1023 *p = build2 (INIT_EXPR, type, temp, *p);
1026 return temp;
1029 return NULL_TREE;
1032 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1033 a temporary through which they communicate. */
1035 static void
1036 build_stack_save_restore (tree *save, tree *restore)
1038 tree save_call, tmp_var;
1040 save_call =
1041 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_SAVE],
1042 NULL_TREE);
1043 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1045 *save = build2 (GIMPLE_MODIFY_STMT, ptr_type_node, tmp_var, save_call);
1046 *restore =
1047 build_function_call_expr (implicit_built_in_decls[BUILT_IN_STACK_RESTORE],
1048 tree_cons (NULL_TREE, tmp_var, NULL_TREE));
1051 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1053 static enum gimplify_status
1054 gimplify_bind_expr (tree *expr_p, tree *pre_p)
1056 tree bind_expr = *expr_p;
1057 bool old_save_stack = gimplify_ctxp->save_stack;
1058 tree t;
1060 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1062 /* Mark variables seen in this bind expr. */
1063 for (t = BIND_EXPR_VARS (bind_expr); t ; t = TREE_CHAIN (t))
1065 if (TREE_CODE (t) == VAR_DECL)
1067 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1069 /* Mark variable as local. */
1070 if (ctx && !is_global_var (t)
1071 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1072 || splay_tree_lookup (ctx->variables,
1073 (splay_tree_key) t) == NULL))
1074 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1076 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1079 /* Preliminarily mark non-addressed complex variables as eligible
1080 for promotion to gimple registers. We'll transform their uses
1081 as we find them. */
1082 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1083 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1084 && !TREE_THIS_VOLATILE (t)
1085 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1086 && !needs_to_live_in_memory (t))
1087 DECL_GIMPLE_REG_P (t) = 1;
1090 gimple_push_bind_expr (bind_expr);
1091 gimplify_ctxp->save_stack = false;
1093 gimplify_to_stmt_list (&BIND_EXPR_BODY (bind_expr));
1095 if (gimplify_ctxp->save_stack)
1097 tree stack_save, stack_restore;
1099 /* Save stack on entry and restore it on exit. Add a try_finally
1100 block to achieve this. Note that mudflap depends on the
1101 format of the emitted code: see mx_register_decls(). */
1102 build_stack_save_restore (&stack_save, &stack_restore);
1104 t = build2 (TRY_FINALLY_EXPR, void_type_node,
1105 BIND_EXPR_BODY (bind_expr), NULL_TREE);
1106 append_to_statement_list (stack_restore, &TREE_OPERAND (t, 1));
1108 BIND_EXPR_BODY (bind_expr) = NULL_TREE;
1109 append_to_statement_list (stack_save, &BIND_EXPR_BODY (bind_expr));
1110 append_to_statement_list (t, &BIND_EXPR_BODY (bind_expr));
1113 gimplify_ctxp->save_stack = old_save_stack;
1114 gimple_pop_bind_expr ();
1116 if (temp)
1118 *expr_p = temp;
1119 append_to_statement_list (bind_expr, pre_p);
1120 return GS_OK;
1122 else
1123 return GS_ALL_DONE;
1126 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1127 GIMPLE value, it is assigned to a new temporary and the statement is
1128 re-written to return the temporary.
1130 PRE_P points to the list where side effects that must happen before
1131 STMT should be stored. */
1133 static enum gimplify_status
1134 gimplify_return_expr (tree stmt, tree *pre_p)
1136 tree ret_expr = TREE_OPERAND (stmt, 0);
1137 tree result_decl, result;
1139 if (!ret_expr || TREE_CODE (ret_expr) == RESULT_DECL
1140 || ret_expr == error_mark_node)
1141 return GS_ALL_DONE;
1143 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1144 result_decl = NULL_TREE;
1145 else
1147 result_decl = GENERIC_TREE_OPERAND (ret_expr, 0);
1148 if (TREE_CODE (result_decl) == INDIRECT_REF)
1149 /* See through a return by reference. */
1150 result_decl = TREE_OPERAND (result_decl, 0);
1152 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1153 || TREE_CODE (ret_expr) == GIMPLE_MODIFY_STMT
1154 || TREE_CODE (ret_expr) == INIT_EXPR)
1155 && TREE_CODE (result_decl) == RESULT_DECL);
1158 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1159 Recall that aggregate_value_p is FALSE for any aggregate type that is
1160 returned in registers. If we're returning values in registers, then
1161 we don't want to extend the lifetime of the RESULT_DECL, particularly
1162 across another call. In addition, for those aggregates for which
1163 hard_function_value generates a PARALLEL, we'll die during normal
1164 expansion of structure assignments; there's special code in expand_return
1165 to handle this case that does not exist in expand_expr. */
1166 if (!result_decl
1167 || aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1168 result = result_decl;
1169 else if (gimplify_ctxp->return_temp)
1170 result = gimplify_ctxp->return_temp;
1171 else
1173 result = create_tmp_var (TREE_TYPE (result_decl), NULL);
1175 /* ??? With complex control flow (usually involving abnormal edges),
1176 we can wind up warning about an uninitialized value for this. Due
1177 to how this variable is constructed and initialized, this is never
1178 true. Give up and never warn. */
1179 TREE_NO_WARNING (result) = 1;
1181 gimplify_ctxp->return_temp = result;
1184 /* Smash the lhs of the GIMPLE_MODIFY_STMT to the temporary we plan to use.
1185 Then gimplify the whole thing. */
1186 if (result != result_decl)
1187 GENERIC_TREE_OPERAND (ret_expr, 0) = result;
1189 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1191 /* If we didn't use a temporary, then the result is just the result_decl.
1192 Otherwise we need a simple copy. This should already be gimple. */
1193 if (result == result_decl)
1194 ret_expr = result;
1195 else
1196 ret_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (result), result_decl,
1197 result);
1198 TREE_OPERAND (stmt, 0) = ret_expr;
1200 return GS_ALL_DONE;
1203 /* Gimplifies a DECL_EXPR node *STMT_P by making any necessary allocation
1204 and initialization explicit. */
1206 static enum gimplify_status
1207 gimplify_decl_expr (tree *stmt_p)
1209 tree stmt = *stmt_p;
1210 tree decl = DECL_EXPR_DECL (stmt);
1212 *stmt_p = NULL_TREE;
1214 if (TREE_TYPE (decl) == error_mark_node)
1215 return GS_ERROR;
1217 if ((TREE_CODE (decl) == TYPE_DECL
1218 || TREE_CODE (decl) == VAR_DECL)
1219 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1220 gimplify_type_sizes (TREE_TYPE (decl), stmt_p);
1222 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1224 tree init = DECL_INITIAL (decl);
1226 if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
1228 /* This is a variable-sized decl. Simplify its size and mark it
1229 for deferred expansion. Note that mudflap depends on the format
1230 of the emitted code: see mx_register_decls(). */
1231 tree t, args, addr, ptr_type;
1233 gimplify_one_sizepos (&DECL_SIZE (decl), stmt_p);
1234 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), stmt_p);
1236 /* All occurrences of this decl in final gimplified code will be
1237 replaced by indirection. Setting DECL_VALUE_EXPR does two
1238 things: First, it lets the rest of the gimplifier know what
1239 replacement to use. Second, it lets the debug info know
1240 where to find the value. */
1241 ptr_type = build_pointer_type (TREE_TYPE (decl));
1242 addr = create_tmp_var (ptr_type, get_name (decl));
1243 DECL_IGNORED_P (addr) = 0;
1244 t = build_fold_indirect_ref (addr);
1245 SET_DECL_VALUE_EXPR (decl, t);
1246 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1248 args = tree_cons (NULL, DECL_SIZE_UNIT (decl), NULL);
1249 t = built_in_decls[BUILT_IN_ALLOCA];
1250 t = build_function_call_expr (t, args);
1251 t = fold_convert (ptr_type, t);
1252 t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
1254 gimplify_and_add (t, stmt_p);
1256 /* Indicate that we need to restore the stack level when the
1257 enclosing BIND_EXPR is exited. */
1258 gimplify_ctxp->save_stack = true;
1261 if (init && init != error_mark_node)
1263 if (!TREE_STATIC (decl))
1265 DECL_INITIAL (decl) = NULL_TREE;
1266 init = build2 (INIT_EXPR, void_type_node, decl, init);
1267 gimplify_and_add (init, stmt_p);
1269 else
1270 /* We must still examine initializers for static variables
1271 as they may contain a label address. */
1272 walk_tree (&init, force_labels_r, NULL, NULL);
1275 /* Some front ends do not explicitly declare all anonymous
1276 artificial variables. We compensate here by declaring the
1277 variables, though it would be better if the front ends would
1278 explicitly declare them. */
1279 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1280 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1281 gimple_add_tmp_var (decl);
1284 return GS_ALL_DONE;
1287 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1288 and replacing the LOOP_EXPR with goto, but if the loop contains an
1289 EXIT_EXPR, we need to append a label for it to jump to. */
1291 static enum gimplify_status
1292 gimplify_loop_expr (tree *expr_p, tree *pre_p)
1294 tree saved_label = gimplify_ctxp->exit_label;
1295 tree start_label = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
1296 tree jump_stmt = build_and_jump (&LABEL_EXPR_LABEL (start_label));
1298 append_to_statement_list (start_label, pre_p);
1300 gimplify_ctxp->exit_label = NULL_TREE;
1302 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1304 if (gimplify_ctxp->exit_label)
1306 append_to_statement_list (jump_stmt, pre_p);
1307 *expr_p = build1 (LABEL_EXPR, void_type_node, gimplify_ctxp->exit_label);
1309 else
1310 *expr_p = jump_stmt;
1312 gimplify_ctxp->exit_label = saved_label;
1314 return GS_ALL_DONE;
1317 /* Compare two case labels. Because the front end should already have
1318 made sure that case ranges do not overlap, it is enough to only compare
1319 the CASE_LOW values of each case label. */
1321 static int
1322 compare_case_labels (const void *p1, const void *p2)
1324 tree case1 = *(tree *)p1;
1325 tree case2 = *(tree *)p2;
1327 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1330 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1332 void
1333 sort_case_labels (tree label_vec)
1335 size_t len = TREE_VEC_LENGTH (label_vec);
1336 tree default_case = TREE_VEC_ELT (label_vec, len - 1);
1338 if (CASE_LOW (default_case))
1340 size_t i;
1342 /* The last label in the vector should be the default case
1343 but it is not. */
1344 for (i = 0; i < len; ++i)
1346 tree t = TREE_VEC_ELT (label_vec, i);
1347 if (!CASE_LOW (t))
1349 default_case = t;
1350 TREE_VEC_ELT (label_vec, i) = TREE_VEC_ELT (label_vec, len - 1);
1351 TREE_VEC_ELT (label_vec, len - 1) = default_case;
1352 break;
1357 qsort (&TREE_VEC_ELT (label_vec, 0), len - 1, sizeof (tree),
1358 compare_case_labels);
1361 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1362 branch to. */
1364 static enum gimplify_status
1365 gimplify_switch_expr (tree *expr_p, tree *pre_p)
1367 tree switch_expr = *expr_p;
1368 enum gimplify_status ret;
1370 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL,
1371 is_gimple_val, fb_rvalue);
1373 if (SWITCH_BODY (switch_expr))
1375 VEC(tree,heap) *labels, *saved_labels;
1376 tree label_vec, default_case = NULL_TREE;
1377 size_t i, len;
1379 /* If someone can be bothered to fill in the labels, they can
1380 be bothered to null out the body too. */
1381 gcc_assert (!SWITCH_LABELS (switch_expr));
1383 saved_labels = gimplify_ctxp->case_labels;
1384 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1386 gimplify_to_stmt_list (&SWITCH_BODY (switch_expr));
1388 labels = gimplify_ctxp->case_labels;
1389 gimplify_ctxp->case_labels = saved_labels;
1391 i = 0;
1392 while (i < VEC_length (tree, labels))
1394 tree elt = VEC_index (tree, labels, i);
1395 tree low = CASE_LOW (elt);
1396 bool remove_element = FALSE;
1398 if (low)
1400 /* Discard empty ranges. */
1401 tree high = CASE_HIGH (elt);
1402 if (high && INT_CST_LT (high, low))
1403 remove_element = TRUE;
1405 else
1407 /* The default case must be the last label in the list. */
1408 gcc_assert (!default_case);
1409 default_case = elt;
1410 remove_element = TRUE;
1413 if (remove_element)
1414 VEC_ordered_remove (tree, labels, i);
1415 else
1416 i++;
1418 len = i;
1420 label_vec = make_tree_vec (len + 1);
1421 SWITCH_LABELS (*expr_p) = label_vec;
1422 append_to_statement_list (switch_expr, pre_p);
1424 if (! default_case)
1426 /* If the switch has no default label, add one, so that we jump
1427 around the switch body. */
1428 default_case = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE,
1429 NULL_TREE, create_artificial_label ());
1430 append_to_statement_list (SWITCH_BODY (switch_expr), pre_p);
1431 *expr_p = build1 (LABEL_EXPR, void_type_node,
1432 CASE_LABEL (default_case));
1434 else
1435 *expr_p = SWITCH_BODY (switch_expr);
1437 for (i = 0; i < len; ++i)
1438 TREE_VEC_ELT (label_vec, i) = VEC_index (tree, labels, i);
1439 TREE_VEC_ELT (label_vec, len) = default_case;
1441 VEC_free (tree, heap, labels);
1443 sort_case_labels (label_vec);
1445 SWITCH_BODY (switch_expr) = NULL;
1447 else
1448 gcc_assert (SWITCH_LABELS (switch_expr));
1450 return ret;
1453 static enum gimplify_status
1454 gimplify_case_label_expr (tree *expr_p)
1456 tree expr = *expr_p;
1457 struct gimplify_ctx *ctxp;
1459 /* Invalid OpenMP programs can play Duff's Device type games with
1460 #pragma omp parallel. At least in the C front end, we don't
1461 detect such invalid branches until after gimplification. */
1462 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1463 if (ctxp->case_labels)
1464 break;
1466 VEC_safe_push (tree, heap, ctxp->case_labels, expr);
1467 *expr_p = build1 (LABEL_EXPR, void_type_node, CASE_LABEL (expr));
1468 return GS_ALL_DONE;
1471 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1472 if necessary. */
1474 tree
1475 build_and_jump (tree *label_p)
1477 if (label_p == NULL)
1478 /* If there's nowhere to jump, just fall through. */
1479 return NULL_TREE;
1481 if (*label_p == NULL_TREE)
1483 tree label = create_artificial_label ();
1484 *label_p = label;
1487 return build1 (GOTO_EXPR, void_type_node, *label_p);
1490 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1491 This also involves building a label to jump to and communicating it to
1492 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1494 static enum gimplify_status
1495 gimplify_exit_expr (tree *expr_p)
1497 tree cond = TREE_OPERAND (*expr_p, 0);
1498 tree expr;
1500 expr = build_and_jump (&gimplify_ctxp->exit_label);
1501 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1502 *expr_p = expr;
1504 return GS_OK;
1507 /* A helper function to be called via walk_tree. Mark all labels under *TP
1508 as being forced. To be called for DECL_INITIAL of static variables. */
1510 tree
1511 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1513 if (TYPE_P (*tp))
1514 *walk_subtrees = 0;
1515 if (TREE_CODE (*tp) == LABEL_DECL)
1516 FORCED_LABEL (*tp) = 1;
1518 return NULL_TREE;
1521 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1522 different from its canonical type, wrap the whole thing inside a
1523 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1524 type.
1526 The canonical type of a COMPONENT_REF is the type of the field being
1527 referenced--unless the field is a bit-field which can be read directly
1528 in a smaller mode, in which case the canonical type is the
1529 sign-appropriate type corresponding to that mode. */
1531 static void
1532 canonicalize_component_ref (tree *expr_p)
1534 tree expr = *expr_p;
1535 tree type;
1537 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1539 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1540 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1541 else
1542 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1544 if (TREE_TYPE (expr) != type)
1546 tree old_type = TREE_TYPE (expr);
1548 /* Set the type of the COMPONENT_REF to the underlying type. */
1549 TREE_TYPE (expr) = type;
1551 /* And wrap the whole thing inside a NOP_EXPR. */
1552 expr = build1 (NOP_EXPR, old_type, expr);
1554 *expr_p = expr;
1558 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1559 to foo, embed that change in the ADDR_EXPR by converting
1560 T array[U];
1561 (T *)&array
1563 &array[L]
1564 where L is the lower bound. For simplicity, only do this for constant
1565 lower bound. */
1567 static void
1568 canonicalize_addr_expr (tree *expr_p)
1570 tree expr = *expr_p;
1571 tree ctype = TREE_TYPE (expr);
1572 tree addr_expr = TREE_OPERAND (expr, 0);
1573 tree atype = TREE_TYPE (addr_expr);
1574 tree dctype, datype, ddatype, otype, obj_expr;
1576 /* Both cast and addr_expr types should be pointers. */
1577 if (!POINTER_TYPE_P (ctype) || !POINTER_TYPE_P (atype))
1578 return;
1580 /* The addr_expr type should be a pointer to an array. */
1581 datype = TREE_TYPE (atype);
1582 if (TREE_CODE (datype) != ARRAY_TYPE)
1583 return;
1585 /* Both cast and addr_expr types should address the same object type. */
1586 dctype = TREE_TYPE (ctype);
1587 ddatype = TREE_TYPE (datype);
1588 if (!lang_hooks.types_compatible_p (ddatype, dctype))
1589 return;
1591 /* The addr_expr and the object type should match. */
1592 obj_expr = TREE_OPERAND (addr_expr, 0);
1593 otype = TREE_TYPE (obj_expr);
1594 if (!lang_hooks.types_compatible_p (otype, datype))
1595 return;
1597 /* The lower bound and element sizes must be constant. */
1598 if (!TYPE_SIZE_UNIT (dctype)
1599 || TREE_CODE (TYPE_SIZE_UNIT (dctype)) != INTEGER_CST
1600 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1601 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1602 return;
1604 /* All checks succeeded. Build a new node to merge the cast. */
1605 *expr_p = build4 (ARRAY_REF, dctype, obj_expr,
1606 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1607 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1608 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (dctype),
1609 size_int (TYPE_ALIGN_UNIT (dctype))));
1610 *expr_p = build1 (ADDR_EXPR, ctype, *expr_p);
1613 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1614 underneath as appropriate. */
1616 static enum gimplify_status
1617 gimplify_conversion (tree *expr_p)
1619 gcc_assert (TREE_CODE (*expr_p) == NOP_EXPR
1620 || TREE_CODE (*expr_p) == CONVERT_EXPR);
1622 /* Then strip away all but the outermost conversion. */
1623 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1625 /* And remove the outermost conversion if it's useless. */
1626 if (tree_ssa_useless_type_conversion (*expr_p))
1627 *expr_p = TREE_OPERAND (*expr_p, 0);
1629 /* If we still have a conversion at the toplevel,
1630 then canonicalize some constructs. */
1631 if (TREE_CODE (*expr_p) == NOP_EXPR || TREE_CODE (*expr_p) == CONVERT_EXPR)
1633 tree sub = TREE_OPERAND (*expr_p, 0);
1635 /* If a NOP conversion is changing the type of a COMPONENT_REF
1636 expression, then canonicalize its type now in order to expose more
1637 redundant conversions. */
1638 if (TREE_CODE (sub) == COMPONENT_REF)
1639 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1641 /* If a NOP conversion is changing a pointer to array of foo
1642 to a pointer to foo, embed that change in the ADDR_EXPR. */
1643 else if (TREE_CODE (sub) == ADDR_EXPR)
1644 canonicalize_addr_expr (expr_p);
1647 return GS_OK;
1650 /* Gimplify a VAR_DECL or PARM_DECL. Returns GS_OK if we expanded a
1651 DECL_VALUE_EXPR, and it's worth re-examining things. */
1653 static enum gimplify_status
1654 gimplify_var_or_parm_decl (tree *expr_p)
1656 tree decl = *expr_p;
1658 /* ??? If this is a local variable, and it has not been seen in any
1659 outer BIND_EXPR, then it's probably the result of a duplicate
1660 declaration, for which we've already issued an error. It would
1661 be really nice if the front end wouldn't leak these at all.
1662 Currently the only known culprit is C++ destructors, as seen
1663 in g++.old-deja/g++.jason/binding.C. */
1664 if (TREE_CODE (decl) == VAR_DECL
1665 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1666 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1667 && decl_function_context (decl) == current_function_decl)
1669 gcc_assert (errorcount || sorrycount);
1670 return GS_ERROR;
1673 /* When within an OpenMP context, notice uses of variables. */
1674 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1675 return GS_ALL_DONE;
1677 /* If the decl is an alias for another expression, substitute it now. */
1678 if (DECL_HAS_VALUE_EXPR_P (decl))
1680 *expr_p = unshare_expr (DECL_VALUE_EXPR (decl));
1681 return GS_OK;
1684 return GS_ALL_DONE;
1688 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1689 node pointed to by EXPR_P.
1691 compound_lval
1692 : min_lval '[' val ']'
1693 | min_lval '.' ID
1694 | compound_lval '[' val ']'
1695 | compound_lval '.' ID
1697 This is not part of the original SIMPLE definition, which separates
1698 array and member references, but it seems reasonable to handle them
1699 together. Also, this way we don't run into problems with union
1700 aliasing; gcc requires that for accesses through a union to alias, the
1701 union reference must be explicit, which was not always the case when we
1702 were splitting up array and member refs.
1704 PRE_P points to the list where side effects that must happen before
1705 *EXPR_P should be stored.
1707 POST_P points to the list where side effects that must happen after
1708 *EXPR_P should be stored. */
1710 static enum gimplify_status
1711 gimplify_compound_lval (tree *expr_p, tree *pre_p,
1712 tree *post_p, fallback_t fallback)
1714 tree *p;
1715 VEC(tree,heap) *stack;
1716 enum gimplify_status ret = GS_OK, tret;
1717 int i;
1719 /* Create a stack of the subexpressions so later we can walk them in
1720 order from inner to outer. */
1721 stack = VEC_alloc (tree, heap, 10);
1723 /* We can handle anything that get_inner_reference can deal with. */
1724 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
1726 restart:
1727 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
1728 if (TREE_CODE (*p) == INDIRECT_REF)
1729 *p = fold_indirect_ref (*p);
1731 if (handled_component_p (*p))
1733 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
1734 additional COMPONENT_REFs. */
1735 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
1736 && gimplify_var_or_parm_decl (p) == GS_OK)
1737 goto restart;
1738 else
1739 break;
1741 VEC_safe_push (tree, heap, stack, *p);
1744 gcc_assert (VEC_length (tree, stack));
1746 /* Now STACK is a stack of pointers to all the refs we've walked through
1747 and P points to the innermost expression.
1749 Java requires that we elaborated nodes in source order. That
1750 means we must gimplify the inner expression followed by each of
1751 the indices, in order. But we can't gimplify the inner
1752 expression until we deal with any variable bounds, sizes, or
1753 positions in order to deal with PLACEHOLDER_EXPRs.
1755 So we do this in three steps. First we deal with the annotations
1756 for any variables in the components, then we gimplify the base,
1757 then we gimplify any indices, from left to right. */
1758 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
1760 tree t = VEC_index (tree, stack, i);
1762 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1764 /* Gimplify the low bound and element type size and put them into
1765 the ARRAY_REF. If these values are set, they have already been
1766 gimplified. */
1767 if (!TREE_OPERAND (t, 2))
1769 tree low = unshare_expr (array_ref_low_bound (t));
1770 if (!is_gimple_min_invariant (low))
1772 TREE_OPERAND (t, 2) = low;
1773 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1774 is_gimple_formal_tmp_reg, fb_rvalue);
1775 ret = MIN (ret, tret);
1779 if (!TREE_OPERAND (t, 3))
1781 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
1782 tree elmt_size = unshare_expr (array_ref_element_size (t));
1783 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
1785 /* Divide the element size by the alignment of the element
1786 type (above). */
1787 elmt_size = size_binop (EXACT_DIV_EXPR, elmt_size, factor);
1789 if (!is_gimple_min_invariant (elmt_size))
1791 TREE_OPERAND (t, 3) = elmt_size;
1792 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
1793 is_gimple_formal_tmp_reg, fb_rvalue);
1794 ret = MIN (ret, tret);
1798 else if (TREE_CODE (t) == COMPONENT_REF)
1800 /* Set the field offset into T and gimplify it. */
1801 if (!TREE_OPERAND (t, 2))
1803 tree offset = unshare_expr (component_ref_field_offset (t));
1804 tree field = TREE_OPERAND (t, 1);
1805 tree factor
1806 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
1808 /* Divide the offset by its alignment. */
1809 offset = size_binop (EXACT_DIV_EXPR, offset, factor);
1811 if (!is_gimple_min_invariant (offset))
1813 TREE_OPERAND (t, 2) = offset;
1814 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1815 is_gimple_formal_tmp_reg, fb_rvalue);
1816 ret = MIN (ret, tret);
1822 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
1823 so as to match the min_lval predicate. Failure to do so may result
1824 in the creation of large aggregate temporaries. */
1825 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
1826 fallback | fb_lvalue);
1827 ret = MIN (ret, tret);
1829 /* And finally, the indices and operands to BIT_FIELD_REF. During this
1830 loop we also remove any useless conversions. */
1831 for (; VEC_length (tree, stack) > 0; )
1833 tree t = VEC_pop (tree, stack);
1835 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1837 /* Gimplify the dimension.
1838 Temporary fix for gcc.c-torture/execute/20040313-1.c.
1839 Gimplify non-constant array indices into a temporary
1840 variable.
1841 FIXME - The real fix is to gimplify post-modify
1842 expressions into a minimal gimple lvalue. However, that
1843 exposes bugs in alias analysis. The alias analyzer does
1844 not handle &PTR->FIELD very well. Will fix after the
1845 branch is merged into mainline (dnovillo 2004-05-03). */
1846 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
1848 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1849 is_gimple_formal_tmp_reg, fb_rvalue);
1850 ret = MIN (ret, tret);
1853 else if (TREE_CODE (t) == BIT_FIELD_REF)
1855 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
1856 is_gimple_val, fb_rvalue);
1857 ret = MIN (ret, tret);
1858 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
1859 is_gimple_val, fb_rvalue);
1860 ret = MIN (ret, tret);
1863 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
1865 /* The innermost expression P may have originally had TREE_SIDE_EFFECTS
1866 set which would have caused all the outer expressions in EXPR_P
1867 leading to P to also have had TREE_SIDE_EFFECTS set. */
1868 recalculate_side_effects (t);
1871 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval, fallback);
1872 ret = MIN (ret, tret);
1874 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
1875 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
1877 canonicalize_component_ref (expr_p);
1878 ret = MIN (ret, GS_OK);
1881 VEC_free (tree, heap, stack);
1883 return ret;
1886 /* Gimplify the self modifying expression pointed to by EXPR_P
1887 (++, --, +=, -=).
1889 PRE_P points to the list where side effects that must happen before
1890 *EXPR_P should be stored.
1892 POST_P points to the list where side effects that must happen after
1893 *EXPR_P should be stored.
1895 WANT_VALUE is nonzero iff we want to use the value of this expression
1896 in another expression. */
1898 static enum gimplify_status
1899 gimplify_self_mod_expr (tree *expr_p, tree *pre_p, tree *post_p,
1900 bool want_value)
1902 enum tree_code code;
1903 tree lhs, lvalue, rhs, t1, post = NULL, *orig_post_p = post_p;
1904 bool postfix;
1905 enum tree_code arith_code;
1906 enum gimplify_status ret;
1908 code = TREE_CODE (*expr_p);
1910 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
1911 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
1913 /* Prefix or postfix? */
1914 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1915 /* Faster to treat as prefix if result is not used. */
1916 postfix = want_value;
1917 else
1918 postfix = false;
1920 /* For postfix, make sure the inner expression's post side effects
1921 are executed after side effects from this expression. */
1922 if (postfix)
1923 post_p = &post;
1925 /* Add or subtract? */
1926 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
1927 arith_code = PLUS_EXPR;
1928 else
1929 arith_code = MINUS_EXPR;
1931 /* Gimplify the LHS into a GIMPLE lvalue. */
1932 lvalue = TREE_OPERAND (*expr_p, 0);
1933 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
1934 if (ret == GS_ERROR)
1935 return ret;
1937 /* Extract the operands to the arithmetic operation. */
1938 lhs = lvalue;
1939 rhs = TREE_OPERAND (*expr_p, 1);
1941 /* For postfix operator, we evaluate the LHS to an rvalue and then use
1942 that as the result value and in the postqueue operation. */
1943 if (postfix)
1945 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
1946 if (ret == GS_ERROR)
1947 return ret;
1950 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
1951 t1 = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (lvalue), lvalue, t1);
1953 if (postfix)
1955 gimplify_and_add (t1, orig_post_p);
1956 append_to_statement_list (post, orig_post_p);
1957 *expr_p = lhs;
1958 return GS_ALL_DONE;
1960 else
1962 *expr_p = t1;
1963 return GS_OK;
1967 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
1969 static void
1970 maybe_with_size_expr (tree *expr_p)
1972 tree expr = *expr_p;
1973 tree type = TREE_TYPE (expr);
1974 tree size;
1976 /* If we've already wrapped this or the type is error_mark_node, we can't do
1977 anything. */
1978 if (TREE_CODE (expr) == WITH_SIZE_EXPR
1979 || type == error_mark_node)
1980 return;
1982 /* If the size isn't known or is a constant, we have nothing to do. */
1983 size = TYPE_SIZE_UNIT (type);
1984 if (!size || TREE_CODE (size) == INTEGER_CST)
1985 return;
1987 /* Otherwise, make a WITH_SIZE_EXPR. */
1988 size = unshare_expr (size);
1989 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
1990 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
1993 /* Subroutine of gimplify_call_expr: Gimplify a single argument. */
1995 static enum gimplify_status
1996 gimplify_arg (tree *expr_p, tree *pre_p)
1998 bool (*test) (tree);
1999 fallback_t fb;
2001 /* In general, we allow lvalues for function arguments to avoid
2002 extra overhead of copying large aggregates out of even larger
2003 aggregates into temporaries only to copy the temporaries to
2004 the argument list. Make optimizers happy by pulling out to
2005 temporaries those types that fit in registers. */
2006 if (is_gimple_reg_type (TREE_TYPE (*expr_p)))
2007 test = is_gimple_val, fb = fb_rvalue;
2008 else
2009 test = is_gimple_lvalue, fb = fb_either;
2011 /* If this is a variable sized type, we must remember the size. */
2012 maybe_with_size_expr (expr_p);
2014 /* There is a sequence point before a function call. Side effects in
2015 the argument list must occur before the actual call. So, when
2016 gimplifying arguments, force gimplify_expr to use an internal
2017 post queue which is then appended to the end of PRE_P. */
2018 return gimplify_expr (expr_p, pre_p, NULL, test, fb);
2021 /* Gimplify the CALL_EXPR node pointed to by EXPR_P. PRE_P points to the
2022 list where side effects that must happen before *EXPR_P should be stored.
2023 WANT_VALUE is true if the result of the call is desired. */
2025 static enum gimplify_status
2026 gimplify_call_expr (tree *expr_p, tree *pre_p, bool want_value)
2028 tree decl;
2029 tree arglist;
2030 enum gimplify_status ret;
2032 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2034 /* For reliable diagnostics during inlining, it is necessary that
2035 every call_expr be annotated with file and line. */
2036 if (! EXPR_HAS_LOCATION (*expr_p))
2037 SET_EXPR_LOCATION (*expr_p, input_location);
2039 /* This may be a call to a builtin function.
2041 Builtin function calls may be transformed into different
2042 (and more efficient) builtin function calls under certain
2043 circumstances. Unfortunately, gimplification can muck things
2044 up enough that the builtin expanders are not aware that certain
2045 transformations are still valid.
2047 So we attempt transformation/gimplification of the call before
2048 we gimplify the CALL_EXPR. At this time we do not manage to
2049 transform all calls in the same manner as the expanders do, but
2050 we do transform most of them. */
2051 decl = get_callee_fndecl (*expr_p);
2052 if (decl && DECL_BUILT_IN (decl))
2054 tree arglist = TREE_OPERAND (*expr_p, 1);
2055 tree new = fold_builtin (decl, arglist, !want_value);
2057 if (new && new != *expr_p)
2059 /* There was a transformation of this call which computes the
2060 same value, but in a more efficient way. Return and try
2061 again. */
2062 *expr_p = new;
2063 return GS_OK;
2066 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2067 && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_START)
2069 if (!arglist || !TREE_CHAIN (arglist))
2071 error ("too few arguments to function %<va_start%>");
2072 *expr_p = build_empty_stmt ();
2073 return GS_OK;
2076 if (fold_builtin_next_arg (TREE_CHAIN (arglist)))
2078 *expr_p = build_empty_stmt ();
2079 return GS_OK;
2081 /* Avoid gimplifying the second argument to va_start, which needs
2082 to be the plain PARM_DECL. */
2083 return gimplify_arg (&TREE_VALUE (TREE_OPERAND (*expr_p, 1)), pre_p);
2087 /* There is a sequence point before the call, so any side effects in
2088 the calling expression must occur before the actual call. Force
2089 gimplify_expr to use an internal post queue. */
2090 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, NULL,
2091 is_gimple_call_addr, fb_rvalue);
2093 if (PUSH_ARGS_REVERSED)
2094 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2095 for (arglist = TREE_OPERAND (*expr_p, 1); arglist;
2096 arglist = TREE_CHAIN (arglist))
2098 enum gimplify_status t;
2100 t = gimplify_arg (&TREE_VALUE (arglist), pre_p);
2102 if (t == GS_ERROR)
2103 ret = GS_ERROR;
2105 if (PUSH_ARGS_REVERSED)
2106 TREE_OPERAND (*expr_p, 1) = nreverse (TREE_OPERAND (*expr_p, 1));
2108 /* Try this again in case gimplification exposed something. */
2109 if (ret != GS_ERROR)
2111 decl = get_callee_fndecl (*expr_p);
2112 if (decl && DECL_BUILT_IN (decl))
2114 tree arglist = TREE_OPERAND (*expr_p, 1);
2115 tree new = fold_builtin (decl, arglist, !want_value);
2117 if (new && new != *expr_p)
2119 /* There was a transformation of this call which computes the
2120 same value, but in a more efficient way. Return and try
2121 again. */
2122 *expr_p = new;
2123 return GS_OK;
2128 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2129 decl. This allows us to eliminate redundant or useless
2130 calls to "const" functions. */
2131 if (TREE_CODE (*expr_p) == CALL_EXPR
2132 && (call_expr_flags (*expr_p) & (ECF_CONST | ECF_PURE)))
2133 TREE_SIDE_EFFECTS (*expr_p) = 0;
2135 return ret;
2138 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2139 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2141 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2142 condition is true or false, respectively. If null, we should generate
2143 our own to skip over the evaluation of this specific expression.
2145 This function is the tree equivalent of do_jump.
2147 shortcut_cond_r should only be called by shortcut_cond_expr. */
2149 static tree
2150 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p)
2152 tree local_label = NULL_TREE;
2153 tree t, expr = NULL;
2155 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2156 retain the shortcut semantics. Just insert the gotos here;
2157 shortcut_cond_expr will append the real blocks later. */
2158 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2160 /* Turn if (a && b) into
2162 if (a); else goto no;
2163 if (b) goto yes; else goto no;
2164 (no:) */
2166 if (false_label_p == NULL)
2167 false_label_p = &local_label;
2169 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p);
2170 append_to_statement_list (t, &expr);
2172 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2173 false_label_p);
2174 append_to_statement_list (t, &expr);
2176 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2178 /* Turn if (a || b) into
2180 if (a) goto yes;
2181 if (b) goto yes; else goto no;
2182 (yes:) */
2184 if (true_label_p == NULL)
2185 true_label_p = &local_label;
2187 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL);
2188 append_to_statement_list (t, &expr);
2190 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2191 false_label_p);
2192 append_to_statement_list (t, &expr);
2194 else if (TREE_CODE (pred) == COND_EXPR)
2196 /* As long as we're messing with gotos, turn if (a ? b : c) into
2197 if (a)
2198 if (b) goto yes; else goto no;
2199 else
2200 if (c) goto yes; else goto no; */
2201 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2202 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2203 false_label_p),
2204 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2205 false_label_p));
2207 else
2209 expr = build3 (COND_EXPR, void_type_node, pred,
2210 build_and_jump (true_label_p),
2211 build_and_jump (false_label_p));
2214 if (local_label)
2216 t = build1 (LABEL_EXPR, void_type_node, local_label);
2217 append_to_statement_list (t, &expr);
2220 return expr;
2223 static tree
2224 shortcut_cond_expr (tree expr)
2226 tree pred = TREE_OPERAND (expr, 0);
2227 tree then_ = TREE_OPERAND (expr, 1);
2228 tree else_ = TREE_OPERAND (expr, 2);
2229 tree true_label, false_label, end_label, t;
2230 tree *true_label_p;
2231 tree *false_label_p;
2232 bool emit_end, emit_false, jump_over_else;
2233 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2234 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2236 /* First do simple transformations. */
2237 if (!else_se)
2239 /* If there is no 'else', turn (a && b) into if (a) if (b). */
2240 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2242 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2243 then_ = shortcut_cond_expr (expr);
2244 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2245 pred = TREE_OPERAND (pred, 0);
2246 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2249 if (!then_se)
2251 /* If there is no 'then', turn
2252 if (a || b); else d
2253 into
2254 if (a); else if (b); else d. */
2255 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2257 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2258 else_ = shortcut_cond_expr (expr);
2259 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2260 pred = TREE_OPERAND (pred, 0);
2261 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2265 /* If we're done, great. */
2266 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2267 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2268 return expr;
2270 /* Otherwise we need to mess with gotos. Change
2271 if (a) c; else d;
2273 if (a); else goto no;
2274 c; goto end;
2275 no: d; end:
2276 and recursively gimplify the condition. */
2278 true_label = false_label = end_label = NULL_TREE;
2280 /* If our arms just jump somewhere, hijack those labels so we don't
2281 generate jumps to jumps. */
2283 if (then_
2284 && TREE_CODE (then_) == GOTO_EXPR
2285 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2287 true_label = GOTO_DESTINATION (then_);
2288 then_ = NULL;
2289 then_se = false;
2292 if (else_
2293 && TREE_CODE (else_) == GOTO_EXPR
2294 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2296 false_label = GOTO_DESTINATION (else_);
2297 else_ = NULL;
2298 else_se = false;
2301 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2302 if (true_label)
2303 true_label_p = &true_label;
2304 else
2305 true_label_p = NULL;
2307 /* The 'else' branch also needs a label if it contains interesting code. */
2308 if (false_label || else_se)
2309 false_label_p = &false_label;
2310 else
2311 false_label_p = NULL;
2313 /* If there was nothing else in our arms, just forward the label(s). */
2314 if (!then_se && !else_se)
2315 return shortcut_cond_r (pred, true_label_p, false_label_p);
2317 /* If our last subexpression already has a terminal label, reuse it. */
2318 if (else_se)
2319 expr = expr_last (else_);
2320 else if (then_se)
2321 expr = expr_last (then_);
2322 else
2323 expr = NULL;
2324 if (expr && TREE_CODE (expr) == LABEL_EXPR)
2325 end_label = LABEL_EXPR_LABEL (expr);
2327 /* If we don't care about jumping to the 'else' branch, jump to the end
2328 if the condition is false. */
2329 if (!false_label_p)
2330 false_label_p = &end_label;
2332 /* We only want to emit these labels if we aren't hijacking them. */
2333 emit_end = (end_label == NULL_TREE);
2334 emit_false = (false_label == NULL_TREE);
2336 /* We only emit the jump over the else clause if we have to--if the
2337 then clause may fall through. Otherwise we can wind up with a
2338 useless jump and a useless label at the end of gimplified code,
2339 which will cause us to think that this conditional as a whole
2340 falls through even if it doesn't. If we then inline a function
2341 which ends with such a condition, that can cause us to issue an
2342 inappropriate warning about control reaching the end of a
2343 non-void function. */
2344 jump_over_else = block_may_fallthru (then_);
2346 pred = shortcut_cond_r (pred, true_label_p, false_label_p);
2348 expr = NULL;
2349 append_to_statement_list (pred, &expr);
2351 append_to_statement_list (then_, &expr);
2352 if (else_se)
2354 if (jump_over_else)
2356 t = build_and_jump (&end_label);
2357 append_to_statement_list (t, &expr);
2359 if (emit_false)
2361 t = build1 (LABEL_EXPR, void_type_node, false_label);
2362 append_to_statement_list (t, &expr);
2364 append_to_statement_list (else_, &expr);
2366 if (emit_end && end_label)
2368 t = build1 (LABEL_EXPR, void_type_node, end_label);
2369 append_to_statement_list (t, &expr);
2372 return expr;
2375 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2377 tree
2378 gimple_boolify (tree expr)
2380 tree type = TREE_TYPE (expr);
2382 if (TREE_CODE (type) == BOOLEAN_TYPE)
2383 return expr;
2385 switch (TREE_CODE (expr))
2387 case TRUTH_AND_EXPR:
2388 case TRUTH_OR_EXPR:
2389 case TRUTH_XOR_EXPR:
2390 case TRUTH_ANDIF_EXPR:
2391 case TRUTH_ORIF_EXPR:
2392 /* Also boolify the arguments of truth exprs. */
2393 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2394 /* FALLTHRU */
2396 case TRUTH_NOT_EXPR:
2397 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2398 /* FALLTHRU */
2400 case EQ_EXPR: case NE_EXPR:
2401 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2402 /* These expressions always produce boolean results. */
2403 TREE_TYPE (expr) = boolean_type_node;
2404 return expr;
2406 default:
2407 /* Other expressions that get here must have boolean values, but
2408 might need to be converted to the appropriate mode. */
2409 return fold_convert (boolean_type_node, expr);
2413 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2414 into
2416 if (p) if (p)
2417 t1 = a; a;
2418 else or else
2419 t1 = b; b;
2422 The second form is used when *EXPR_P is of type void.
2424 TARGET is the tree for T1 above.
2426 PRE_P points to the list where side effects that must happen before
2427 *EXPR_P should be stored. */
2429 static enum gimplify_status
2430 gimplify_cond_expr (tree *expr_p, tree *pre_p, fallback_t fallback)
2432 tree expr = *expr_p;
2433 tree tmp, tmp2, type;
2434 enum gimplify_status ret;
2436 type = TREE_TYPE (expr);
2438 /* If this COND_EXPR has a value, copy the values into a temporary within
2439 the arms. */
2440 if (! VOID_TYPE_P (type))
2442 tree result;
2444 if ((fallback & fb_lvalue) == 0)
2446 result = tmp2 = tmp = create_tmp_var (TREE_TYPE (expr), "iftmp");
2447 ret = GS_ALL_DONE;
2449 else
2451 tree type = build_pointer_type (TREE_TYPE (expr));
2453 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2454 TREE_OPERAND (expr, 1) =
2455 build_fold_addr_expr (TREE_OPERAND (expr, 1));
2457 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2458 TREE_OPERAND (expr, 2) =
2459 build_fold_addr_expr (TREE_OPERAND (expr, 2));
2461 tmp2 = tmp = create_tmp_var (type, "iftmp");
2463 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (expr, 0),
2464 TREE_OPERAND (expr, 1), TREE_OPERAND (expr, 2));
2466 result = build_fold_indirect_ref (tmp);
2467 ret = GS_ALL_DONE;
2470 /* Build the then clause, 't1 = a;'. But don't build an assignment
2471 if this branch is void; in C++ it can be, if it's a throw. */
2472 if (TREE_TYPE (TREE_OPERAND (expr, 1)) != void_type_node)
2473 TREE_OPERAND (expr, 1)
2474 = build2 (GIMPLE_MODIFY_STMT, void_type_node, tmp,
2475 TREE_OPERAND (expr, 1));
2477 /* Build the else clause, 't1 = b;'. */
2478 if (TREE_TYPE (TREE_OPERAND (expr, 2)) != void_type_node)
2479 TREE_OPERAND (expr, 2)
2480 = build2 (GIMPLE_MODIFY_STMT, void_type_node, tmp2,
2481 TREE_OPERAND (expr, 2));
2483 TREE_TYPE (expr) = void_type_node;
2484 recalculate_side_effects (expr);
2486 /* Move the COND_EXPR to the prequeue. */
2487 gimplify_and_add (expr, pre_p);
2489 *expr_p = result;
2490 return ret;
2493 /* Make sure the condition has BOOLEAN_TYPE. */
2494 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2496 /* Break apart && and || conditions. */
2497 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
2498 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
2500 expr = shortcut_cond_expr (expr);
2502 if (expr != *expr_p)
2504 *expr_p = expr;
2506 /* We can't rely on gimplify_expr to re-gimplify the expanded
2507 form properly, as cleanups might cause the target labels to be
2508 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
2509 set up a conditional context. */
2510 gimple_push_condition ();
2511 gimplify_stmt (expr_p);
2512 gimple_pop_condition (pre_p);
2514 return GS_ALL_DONE;
2518 /* Now do the normal gimplification. */
2519 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2520 is_gimple_condexpr, fb_rvalue);
2522 gimple_push_condition ();
2524 gimplify_to_stmt_list (&TREE_OPERAND (expr, 1));
2525 gimplify_to_stmt_list (&TREE_OPERAND (expr, 2));
2526 recalculate_side_effects (expr);
2528 gimple_pop_condition (pre_p);
2530 if (ret == GS_ERROR)
2532 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2533 ret = GS_ALL_DONE;
2534 else if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 2)))
2535 /* Rewrite "if (a); else b" to "if (!a) b" */
2537 TREE_OPERAND (expr, 0) = invert_truthvalue (TREE_OPERAND (expr, 0));
2538 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL,
2539 is_gimple_condexpr, fb_rvalue);
2541 tmp = TREE_OPERAND (expr, 1);
2542 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 2);
2543 TREE_OPERAND (expr, 2) = tmp;
2545 else
2546 /* Both arms are empty; replace the COND_EXPR with its predicate. */
2547 expr = TREE_OPERAND (expr, 0);
2549 *expr_p = expr;
2550 return ret;
2553 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2554 a call to __builtin_memcpy. */
2556 static enum gimplify_status
2557 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value)
2559 tree args, t, to, to_ptr, from;
2561 to = GENERIC_TREE_OPERAND (*expr_p, 0);
2562 from = GENERIC_TREE_OPERAND (*expr_p, 1);
2564 args = tree_cons (NULL, size, NULL);
2566 t = build_fold_addr_expr (from);
2567 args = tree_cons (NULL, t, args);
2569 to_ptr = build_fold_addr_expr (to);
2570 args = tree_cons (NULL, to_ptr, args);
2571 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
2572 t = build_function_call_expr (t, args);
2574 if (want_value)
2576 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2577 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2580 *expr_p = t;
2581 return GS_OK;
2584 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
2585 a call to __builtin_memset. In this case we know that the RHS is
2586 a CONSTRUCTOR with an empty element list. */
2588 static enum gimplify_status
2589 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value)
2591 tree args, t, to, to_ptr;
2593 to = GENERIC_TREE_OPERAND (*expr_p, 0);
2595 args = tree_cons (NULL, size, NULL);
2597 args = tree_cons (NULL, integer_zero_node, args);
2599 to_ptr = build_fold_addr_expr (to);
2600 args = tree_cons (NULL, to_ptr, args);
2601 t = implicit_built_in_decls[BUILT_IN_MEMSET];
2602 t = build_function_call_expr (t, args);
2604 if (want_value)
2606 t = build1 (NOP_EXPR, TREE_TYPE (to_ptr), t);
2607 t = build1 (INDIRECT_REF, TREE_TYPE (to), t);
2610 *expr_p = t;
2611 return GS_OK;
2614 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
2615 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
2616 assignment. Returns non-null if we detect a potential overlap. */
2618 struct gimplify_init_ctor_preeval_data
2620 /* The base decl of the lhs object. May be NULL, in which case we
2621 have to assume the lhs is indirect. */
2622 tree lhs_base_decl;
2624 /* The alias set of the lhs object. */
2625 int lhs_alias_set;
2628 static tree
2629 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
2631 struct gimplify_init_ctor_preeval_data *data
2632 = (struct gimplify_init_ctor_preeval_data *) xdata;
2633 tree t = *tp;
2635 /* If we find the base object, obviously we have overlap. */
2636 if (data->lhs_base_decl == t)
2637 return t;
2639 /* If the constructor component is indirect, determine if we have a
2640 potential overlap with the lhs. The only bits of information we
2641 have to go on at this point are addressability and alias sets. */
2642 if (TREE_CODE (t) == INDIRECT_REF
2643 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
2644 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
2645 return t;
2647 if (IS_TYPE_OR_DECL_P (t))
2648 *walk_subtrees = 0;
2649 return NULL;
2652 /* A subroutine of gimplify_init_constructor. Pre-evaluate *EXPR_P,
2653 force values that overlap with the lhs (as described by *DATA)
2654 into temporaries. */
2656 static void
2657 gimplify_init_ctor_preeval (tree *expr_p, tree *pre_p, tree *post_p,
2658 struct gimplify_init_ctor_preeval_data *data)
2660 enum gimplify_status one;
2662 /* If the value is invariant, then there's nothing to pre-evaluate.
2663 But ensure it doesn't have any side-effects since a SAVE_EXPR is
2664 invariant but has side effects and might contain a reference to
2665 the object we're initializing. */
2666 if (TREE_INVARIANT (*expr_p) && !TREE_SIDE_EFFECTS (*expr_p))
2667 return;
2669 /* If the type has non-trivial constructors, we can't pre-evaluate. */
2670 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
2671 return;
2673 /* Recurse for nested constructors. */
2674 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
2676 unsigned HOST_WIDE_INT ix;
2677 constructor_elt *ce;
2678 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
2680 for (ix = 0; VEC_iterate (constructor_elt, v, ix, ce); ix++)
2681 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
2682 return;
2685 /* If this is a variable sized type, we must remember the size. */
2686 maybe_with_size_expr (expr_p);
2688 /* Gimplify the constructor element to something appropriate for the rhs
2689 of a MODIFY_EXPR. Given that we know the lhs is an aggregate, we know
2690 the gimplifier will consider this a store to memory. Doing this
2691 gimplification now means that we won't have to deal with complicated
2692 language-specific trees, nor trees like SAVE_EXPR that can induce
2693 exponential search behavior. */
2694 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
2695 if (one == GS_ERROR)
2697 *expr_p = NULL;
2698 return;
2701 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
2702 with the lhs, since "a = { .x=a }" doesn't make sense. This will
2703 always be true for all scalars, since is_gimple_mem_rhs insists on a
2704 temporary variable for them. */
2705 if (DECL_P (*expr_p))
2706 return;
2708 /* If this is of variable size, we have no choice but to assume it doesn't
2709 overlap since we can't make a temporary for it. */
2710 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
2711 return;
2713 /* Otherwise, we must search for overlap ... */
2714 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
2715 return;
2717 /* ... and if found, force the value into a temporary. */
2718 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
2721 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
2722 a RANGE_EXPR in a CONSTRUCTOR for an array.
2724 var = lower;
2725 loop_entry:
2726 object[var] = value;
2727 if (var == upper)
2728 goto loop_exit;
2729 var = var + 1;
2730 goto loop_entry;
2731 loop_exit:
2733 We increment var _after_ the loop exit check because we might otherwise
2734 fail if upper == TYPE_MAX_VALUE (type for upper).
2736 Note that we never have to deal with SAVE_EXPRs here, because this has
2737 already been taken care of for us, in gimplify_init_ctor_preeval(). */
2739 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
2740 tree *, bool);
2742 static void
2743 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
2744 tree value, tree array_elt_type,
2745 tree *pre_p, bool cleared)
2747 tree loop_entry_label, loop_exit_label;
2748 tree var, var_type, cref;
2750 loop_entry_label = create_artificial_label ();
2751 loop_exit_label = create_artificial_label ();
2753 /* Create and initialize the index variable. */
2754 var_type = TREE_TYPE (upper);
2755 var = create_tmp_var (var_type, NULL);
2756 append_to_statement_list (build2 (GIMPLE_MODIFY_STMT, var_type, var, lower),
2757 pre_p);
2759 /* Add the loop entry label. */
2760 append_to_statement_list (build1 (LABEL_EXPR,
2761 void_type_node,
2762 loop_entry_label),
2763 pre_p);
2765 /* Build the reference. */
2766 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2767 var, NULL_TREE, NULL_TREE);
2769 /* If we are a constructor, just call gimplify_init_ctor_eval to do
2770 the store. Otherwise just assign value to the reference. */
2772 if (TREE_CODE (value) == CONSTRUCTOR)
2773 /* NB we might have to call ourself recursively through
2774 gimplify_init_ctor_eval if the value is a constructor. */
2775 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2776 pre_p, cleared);
2777 else
2778 append_to_statement_list (build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (cref),
2779 cref, value),
2780 pre_p);
2782 /* We exit the loop when the index var is equal to the upper bound. */
2783 gimplify_and_add (build3 (COND_EXPR, void_type_node,
2784 build2 (EQ_EXPR, boolean_type_node,
2785 var, upper),
2786 build1 (GOTO_EXPR,
2787 void_type_node,
2788 loop_exit_label),
2789 NULL_TREE),
2790 pre_p);
2792 /* Otherwise, increment the index var... */
2793 append_to_statement_list (build2 (GIMPLE_MODIFY_STMT, var_type, var,
2794 build2 (PLUS_EXPR, var_type, var,
2795 fold_convert (var_type,
2796 integer_one_node))),
2797 pre_p);
2799 /* ...and jump back to the loop entry. */
2800 append_to_statement_list (build1 (GOTO_EXPR,
2801 void_type_node,
2802 loop_entry_label),
2803 pre_p);
2805 /* Add the loop exit label. */
2806 append_to_statement_list (build1 (LABEL_EXPR,
2807 void_type_node,
2808 loop_exit_label),
2809 pre_p);
2812 /* Return true if FDECL is accessing a field that is zero sized. */
2814 static bool
2815 zero_sized_field_decl (tree fdecl)
2817 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
2818 && integer_zerop (DECL_SIZE (fdecl)))
2819 return true;
2820 return false;
2823 /* Return true if TYPE is zero sized. */
2825 static bool
2826 zero_sized_type (tree type)
2828 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
2829 && integer_zerop (TYPE_SIZE (type)))
2830 return true;
2831 return false;
2834 /* A subroutine of gimplify_init_constructor. Generate individual
2835 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
2836 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
2837 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
2838 zeroed first. */
2840 static void
2841 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
2842 tree *pre_p, bool cleared)
2844 tree array_elt_type = NULL;
2845 unsigned HOST_WIDE_INT ix;
2846 tree purpose, value;
2848 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
2849 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
2851 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
2853 tree cref, init;
2855 /* NULL values are created above for gimplification errors. */
2856 if (value == NULL)
2857 continue;
2859 if (cleared && initializer_zerop (value))
2860 continue;
2862 /* ??? Here's to hoping the front end fills in all of the indices,
2863 so we don't have to figure out what's missing ourselves. */
2864 gcc_assert (purpose);
2866 /* Skip zero-sized fields, unless value has side-effects. This can
2867 happen with calls to functions returning a zero-sized type, which
2868 we shouldn't discard. As a number of downstream passes don't
2869 expect sets of zero-sized fields, we rely on the gimplification of
2870 the MODIFY_EXPR we make below to drop the assignment statement. */
2871 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
2872 continue;
2874 /* If we have a RANGE_EXPR, we have to build a loop to assign the
2875 whole range. */
2876 if (TREE_CODE (purpose) == RANGE_EXPR)
2878 tree lower = TREE_OPERAND (purpose, 0);
2879 tree upper = TREE_OPERAND (purpose, 1);
2881 /* If the lower bound is equal to upper, just treat it as if
2882 upper was the index. */
2883 if (simple_cst_equal (lower, upper))
2884 purpose = upper;
2885 else
2887 gimplify_init_ctor_eval_range (object, lower, upper, value,
2888 array_elt_type, pre_p, cleared);
2889 continue;
2893 if (array_elt_type)
2895 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
2896 purpose, NULL_TREE, NULL_TREE);
2898 else
2900 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
2901 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
2902 unshare_expr (object), purpose, NULL_TREE);
2905 if (TREE_CODE (value) == CONSTRUCTOR
2906 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
2907 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
2908 pre_p, cleared);
2909 else
2911 init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
2912 gimplify_and_add (init, pre_p);
2917 /* A subroutine of gimplify_modify_expr. Break out elements of a
2918 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
2920 Note that we still need to clear any elements that don't have explicit
2921 initializers, so if not all elements are initialized we keep the
2922 original MODIFY_EXPR, we just remove all of the constructor elements. */
2924 static enum gimplify_status
2925 gimplify_init_constructor (tree *expr_p, tree *pre_p,
2926 tree *post_p, bool want_value)
2928 tree object;
2929 tree ctor = GENERIC_TREE_OPERAND (*expr_p, 1);
2930 tree type = TREE_TYPE (ctor);
2931 enum gimplify_status ret;
2932 VEC(constructor_elt,gc) *elts;
2934 if (TREE_CODE (ctor) != CONSTRUCTOR)
2935 return GS_UNHANDLED;
2937 ret = gimplify_expr (&GENERIC_TREE_OPERAND (*expr_p, 0), pre_p, post_p,
2938 is_gimple_lvalue, fb_lvalue);
2939 if (ret == GS_ERROR)
2940 return ret;
2941 object = GENERIC_TREE_OPERAND (*expr_p, 0);
2943 elts = CONSTRUCTOR_ELTS (ctor);
2945 ret = GS_ALL_DONE;
2946 switch (TREE_CODE (type))
2948 case RECORD_TYPE:
2949 case UNION_TYPE:
2950 case QUAL_UNION_TYPE:
2951 case ARRAY_TYPE:
2953 struct gimplify_init_ctor_preeval_data preeval_data;
2954 HOST_WIDE_INT num_type_elements, num_ctor_elements;
2955 HOST_WIDE_INT num_nonzero_elements;
2956 bool cleared, valid_const_initializer;
2958 /* Aggregate types must lower constructors to initialization of
2959 individual elements. The exception is that a CONSTRUCTOR node
2960 with no elements indicates zero-initialization of the whole. */
2961 if (VEC_empty (constructor_elt, elts))
2962 break;
2964 /* Fetch information about the constructor to direct later processing.
2965 We might want to make static versions of it in various cases, and
2966 can only do so if it known to be a valid constant initializer. */
2967 valid_const_initializer
2968 = categorize_ctor_elements (ctor, &num_nonzero_elements,
2969 &num_ctor_elements, &cleared);
2971 /* If a const aggregate variable is being initialized, then it
2972 should never be a lose to promote the variable to be static. */
2973 if (valid_const_initializer
2974 && num_nonzero_elements > 1
2975 && TREE_READONLY (object)
2976 && TREE_CODE (object) == VAR_DECL)
2978 DECL_INITIAL (object) = ctor;
2979 TREE_STATIC (object) = 1;
2980 if (!DECL_NAME (object))
2981 DECL_NAME (object) = create_tmp_var_name ("C");
2982 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
2984 /* ??? C++ doesn't automatically append a .<number> to the
2985 assembler name, and even when it does, it looks a FE private
2986 data structures to figure out what that number should be,
2987 which are not set for this variable. I suppose this is
2988 important for local statics for inline functions, which aren't
2989 "local" in the object file sense. So in order to get a unique
2990 TU-local symbol, we must invoke the lhd version now. */
2991 lhd_set_decl_assembler_name (object);
2993 *expr_p = NULL_TREE;
2994 break;
2997 /* If there are "lots" of initialized elements, even discounting
2998 those that are not address constants (and thus *must* be
2999 computed at runtime), then partition the constructor into
3000 constant and non-constant parts. Block copy the constant
3001 parts in, then generate code for the non-constant parts. */
3002 /* TODO. There's code in cp/typeck.c to do this. */
3004 num_type_elements = count_type_elements (type, true);
3006 /* If count_type_elements could not determine number of type elements
3007 for a constant-sized object, assume clearing is needed.
3008 Don't do this for variable-sized objects, as store_constructor
3009 will ignore the clearing of variable-sized objects. */
3010 if (num_type_elements < 0 && int_size_in_bytes (type) >= 0)
3011 cleared = true;
3012 /* If there are "lots" of zeros, then block clear the object first. */
3013 else if (num_type_elements - num_nonzero_elements > CLEAR_RATIO
3014 && num_nonzero_elements < num_type_elements/4)
3015 cleared = true;
3016 /* ??? This bit ought not be needed. For any element not present
3017 in the initializer, we should simply set them to zero. Except
3018 we'd need to *find* the elements that are not present, and that
3019 requires trickery to avoid quadratic compile-time behavior in
3020 large cases or excessive memory use in small cases. */
3021 else if (num_ctor_elements < num_type_elements)
3022 cleared = true;
3024 /* If there are "lots" of initialized elements, and all of them
3025 are valid address constants, then the entire initializer can
3026 be dropped to memory, and then memcpy'd out. Don't do this
3027 for sparse arrays, though, as it's more efficient to follow
3028 the standard CONSTRUCTOR behavior of memset followed by
3029 individual element initialization. */
3030 if (valid_const_initializer && !cleared)
3032 HOST_WIDE_INT size = int_size_in_bytes (type);
3033 unsigned int align;
3035 /* ??? We can still get unbounded array types, at least
3036 from the C++ front end. This seems wrong, but attempt
3037 to work around it for now. */
3038 if (size < 0)
3040 size = int_size_in_bytes (TREE_TYPE (object));
3041 if (size >= 0)
3042 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3045 /* Find the maximum alignment we can assume for the object. */
3046 /* ??? Make use of DECL_OFFSET_ALIGN. */
3047 if (DECL_P (object))
3048 align = DECL_ALIGN (object);
3049 else
3050 align = TYPE_ALIGN (type);
3052 if (size > 0 && !can_move_by_pieces (size, align))
3054 tree new = create_tmp_var_raw (type, "C");
3056 gimple_add_tmp_var (new);
3057 TREE_STATIC (new) = 1;
3058 TREE_READONLY (new) = 1;
3059 DECL_INITIAL (new) = ctor;
3060 if (align > DECL_ALIGN (new))
3062 DECL_ALIGN (new) = align;
3063 DECL_USER_ALIGN (new) = 1;
3065 walk_tree (&DECL_INITIAL (new), force_labels_r, NULL, NULL);
3067 GENERIC_TREE_OPERAND (*expr_p, 1) = new;
3069 /* This is no longer an assignment of a CONSTRUCTOR, but
3070 we still may have processing to do on the LHS. So
3071 pretend we didn't do anything here to let that happen. */
3072 return GS_UNHANDLED;
3076 /* If there are nonzero elements, pre-evaluate to capture elements
3077 overlapping with the lhs into temporaries. We must do this before
3078 clearing to fetch the values before they are zeroed-out. */
3079 if (num_nonzero_elements > 0)
3081 preeval_data.lhs_base_decl = get_base_address (object);
3082 if (!DECL_P (preeval_data.lhs_base_decl))
3083 preeval_data.lhs_base_decl = NULL;
3084 preeval_data.lhs_alias_set = get_alias_set (object);
3086 gimplify_init_ctor_preeval (&GENERIC_TREE_OPERAND (*expr_p, 1),
3087 pre_p, post_p, &preeval_data);
3090 if (cleared)
3092 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3093 Note that we still have to gimplify, in order to handle the
3094 case of variable sized types. Avoid shared tree structures. */
3095 CONSTRUCTOR_ELTS (ctor) = NULL;
3096 object = unshare_expr (object);
3097 gimplify_stmt (expr_p);
3098 append_to_statement_list (*expr_p, pre_p);
3101 /* If we have not block cleared the object, or if there are nonzero
3102 elements in the constructor, add assignments to the individual
3103 scalar fields of the object. */
3104 if (!cleared || num_nonzero_elements > 0)
3105 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3107 *expr_p = NULL_TREE;
3109 break;
3111 case COMPLEX_TYPE:
3113 tree r, i;
3115 /* Extract the real and imaginary parts out of the ctor. */
3116 gcc_assert (VEC_length (constructor_elt, elts) == 2);
3117 r = VEC_index (constructor_elt, elts, 0)->value;
3118 i = VEC_index (constructor_elt, elts, 1)->value;
3119 if (r == NULL || i == NULL)
3121 tree zero = fold_convert (TREE_TYPE (type), integer_zero_node);
3122 if (r == NULL)
3123 r = zero;
3124 if (i == NULL)
3125 i = zero;
3128 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3129 represent creation of a complex value. */
3130 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3132 ctor = build_complex (type, r, i);
3133 TREE_OPERAND (*expr_p, 1) = ctor;
3135 else
3137 ctor = build2 (COMPLEX_EXPR, type, r, i);
3138 TREE_OPERAND (*expr_p, 1) = ctor;
3139 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
3140 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
3141 fb_rvalue);
3144 break;
3146 case VECTOR_TYPE:
3148 unsigned HOST_WIDE_INT ix;
3149 constructor_elt *ce;
3151 /* Go ahead and simplify constant constructors to VECTOR_CST. */
3152 if (TREE_CONSTANT (ctor))
3154 bool constant_p = true;
3155 tree value;
3157 /* Even when ctor is constant, it might contain non-*_CST
3158 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
3159 belong into VECTOR_CST nodes. */
3160 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
3161 if (!CONSTANT_CLASS_P (value))
3163 constant_p = false;
3164 break;
3167 if (constant_p)
3169 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
3170 break;
3173 /* Don't reduce a TREE_CONSTANT vector ctor even if we can't
3174 make a VECTOR_CST. It won't do anything for us, and it'll
3175 prevent us from representing it as a single constant. */
3176 break;
3179 /* Vector types use CONSTRUCTOR all the way through gimple
3180 compilation as a general initializer. */
3181 for (ix = 0; VEC_iterate (constructor_elt, elts, ix, ce); ix++)
3183 enum gimplify_status tret;
3184 tret = gimplify_expr (&ce->value, pre_p, post_p,
3185 is_gimple_val, fb_rvalue);
3186 if (tret == GS_ERROR)
3187 ret = GS_ERROR;
3190 break;
3192 default:
3193 /* So how did we get a CONSTRUCTOR for a scalar type? */
3194 gcc_unreachable ();
3197 if (ret == GS_ERROR)
3198 return GS_ERROR;
3199 else if (want_value)
3201 append_to_statement_list (*expr_p, pre_p);
3202 *expr_p = object;
3203 return GS_OK;
3205 else
3206 return GS_ALL_DONE;
3209 /* Given a pointer value OP0, return a simplified version of an
3210 indirection through OP0, or NULL_TREE if no simplification is
3211 possible. This may only be applied to a rhs of an expression.
3212 Note that the resulting type may be different from the type pointed
3213 to in the sense that it is still compatible from the langhooks
3214 point of view. */
3216 static tree
3217 fold_indirect_ref_rhs (tree t)
3219 tree type = TREE_TYPE (TREE_TYPE (t));
3220 tree sub = t;
3221 tree subtype;
3223 STRIP_USELESS_TYPE_CONVERSION (sub);
3224 subtype = TREE_TYPE (sub);
3225 if (!POINTER_TYPE_P (subtype))
3226 return NULL_TREE;
3228 if (TREE_CODE (sub) == ADDR_EXPR)
3230 tree op = TREE_OPERAND (sub, 0);
3231 tree optype = TREE_TYPE (op);
3232 /* *&p => p */
3233 if (lang_hooks.types_compatible_p (type, optype))
3234 return op;
3235 /* *(foo *)&fooarray => fooarray[0] */
3236 else if (TREE_CODE (optype) == ARRAY_TYPE
3237 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
3239 tree type_domain = TYPE_DOMAIN (optype);
3240 tree min_val = size_zero_node;
3241 if (type_domain && TYPE_MIN_VALUE (type_domain))
3242 min_val = TYPE_MIN_VALUE (type_domain);
3243 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
3247 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3248 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3249 && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
3251 tree type_domain;
3252 tree min_val = size_zero_node;
3253 tree osub = sub;
3254 sub = fold_indirect_ref_rhs (sub);
3255 if (! sub)
3256 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
3257 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3258 if (type_domain && TYPE_MIN_VALUE (type_domain))
3259 min_val = TYPE_MIN_VALUE (type_domain);
3260 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
3263 return NULL_TREE;
3266 /* Subroutine of gimplify_modify_expr to do simplifications of MODIFY_EXPRs
3267 based on the code of the RHS. We loop for as long as something changes. */
3269 static enum gimplify_status
3270 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p, tree *pre_p,
3271 tree *post_p, bool want_value)
3273 enum gimplify_status ret = GS_OK;
3275 while (ret != GS_UNHANDLED)
3276 switch (TREE_CODE (*from_p))
3278 case INDIRECT_REF:
3280 /* If we have code like
3282 *(const A*)(A*)&x
3284 where the type of "x" is a (possibly cv-qualified variant
3285 of "A"), treat the entire expression as identical to "x".
3286 This kind of code arises in C++ when an object is bound
3287 to a const reference, and if "x" is a TARGET_EXPR we want
3288 to take advantage of the optimization below. */
3289 tree t = fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
3290 if (t)
3292 *from_p = t;
3293 ret = GS_OK;
3295 else
3296 ret = GS_UNHANDLED;
3297 break;
3300 case TARGET_EXPR:
3302 /* If we are initializing something from a TARGET_EXPR, strip the
3303 TARGET_EXPR and initialize it directly, if possible. This can't
3304 be done if the initializer is void, since that implies that the
3305 temporary is set in some non-trivial way.
3307 ??? What about code that pulls out the temp and uses it
3308 elsewhere? I think that such code never uses the TARGET_EXPR as
3309 an initializer. If I'm wrong, we'll die because the temp won't
3310 have any RTL. In that case, I guess we'll need to replace
3311 references somehow. */
3312 tree init = TARGET_EXPR_INITIAL (*from_p);
3314 if (!VOID_TYPE_P (TREE_TYPE (init)))
3316 *from_p = init;
3317 ret = GS_OK;
3319 else
3320 ret = GS_UNHANDLED;
3322 break;
3324 case COMPOUND_EXPR:
3325 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
3326 caught. */
3327 gimplify_compound_expr (from_p, pre_p, true);
3328 ret = GS_OK;
3329 break;
3331 case CONSTRUCTOR:
3332 /* If we're initializing from a CONSTRUCTOR, break this into
3333 individual MODIFY_EXPRs. */
3334 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value);
3336 case COND_EXPR:
3337 /* If we're assigning to a non-register type, push the assignment
3338 down into the branches. This is mandatory for ADDRESSABLE types,
3339 since we cannot generate temporaries for such, but it saves a
3340 copy in other cases as well. */
3341 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
3343 /* This code should mirror the code in gimplify_cond_expr. */
3344 enum tree_code code = TREE_CODE (*expr_p);
3345 tree cond = *from_p;
3346 tree result = *to_p;
3348 ret = gimplify_expr (&result, pre_p, post_p,
3349 is_gimple_min_lval, fb_lvalue);
3350 if (ret != GS_ERROR)
3351 ret = GS_OK;
3353 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
3354 TREE_OPERAND (cond, 1)
3355 = build2 (code, void_type_node, result,
3356 TREE_OPERAND (cond, 1));
3357 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
3358 TREE_OPERAND (cond, 2)
3359 = build2 (code, void_type_node, unshare_expr (result),
3360 TREE_OPERAND (cond, 2));
3362 TREE_TYPE (cond) = void_type_node;
3363 recalculate_side_effects (cond);
3365 if (want_value)
3367 gimplify_and_add (cond, pre_p);
3368 *expr_p = unshare_expr (result);
3370 else
3371 *expr_p = cond;
3372 return ret;
3374 else
3375 ret = GS_UNHANDLED;
3376 break;
3378 case CALL_EXPR:
3379 /* For calls that return in memory, give *to_p as the CALL_EXPR's
3380 return slot so that we don't generate a temporary. */
3381 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
3382 && aggregate_value_p (*from_p, *from_p))
3384 bool use_target;
3386 if (!(rhs_predicate_for (*to_p))(*from_p))
3387 /* If we need a temporary, *to_p isn't accurate. */
3388 use_target = false;
3389 else if (TREE_CODE (*to_p) == RESULT_DECL
3390 && DECL_NAME (*to_p) == NULL_TREE
3391 && needs_to_live_in_memory (*to_p))
3392 /* It's OK to use the return slot directly unless it's an NRV. */
3393 use_target = true;
3394 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
3395 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
3396 /* Don't force regs into memory. */
3397 use_target = false;
3398 else if (TREE_CODE (*to_p) == VAR_DECL
3399 && DECL_GIMPLE_FORMAL_TEMP_P (*to_p))
3400 /* Don't use the original target if it's a formal temp; we
3401 don't want to take their addresses. */
3402 use_target = false;
3403 else if (TREE_CODE (*expr_p) == INIT_EXPR)
3404 /* It's OK to use the target directly if it's being
3405 initialized. */
3406 use_target = true;
3407 else if (!is_gimple_non_addressable (*to_p))
3408 /* Don't use the original target if it's already addressable;
3409 if its address escapes, and the called function uses the
3410 NRV optimization, a conforming program could see *to_p
3411 change before the called function returns; see c++/19317.
3412 When optimizing, the return_slot pass marks more functions
3413 as safe after we have escape info. */
3414 use_target = false;
3415 else
3416 use_target = true;
3418 if (use_target)
3420 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
3421 lang_hooks.mark_addressable (*to_p);
3425 ret = GS_UNHANDLED;
3426 break;
3428 /* If we're initializing from a container, push the initialization
3429 inside it. */
3430 case CLEANUP_POINT_EXPR:
3431 case BIND_EXPR:
3432 case STATEMENT_LIST:
3434 tree wrap = *from_p;
3435 tree t;
3437 ret = gimplify_expr (to_p, pre_p, post_p,
3438 is_gimple_min_lval, fb_lvalue);
3439 if (ret != GS_ERROR)
3440 ret = GS_OK;
3442 t = voidify_wrapper_expr (wrap, *expr_p);
3443 gcc_assert (t == *expr_p);
3445 if (want_value)
3447 gimplify_and_add (wrap, pre_p);
3448 *expr_p = unshare_expr (*to_p);
3450 else
3451 *expr_p = wrap;
3452 return GS_OK;
3455 default:
3456 ret = GS_UNHANDLED;
3457 break;
3460 return ret;
3463 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
3464 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
3465 DECL_GIMPLE_REG_P set. */
3467 static enum gimplify_status
3468 gimplify_modify_expr_complex_part (tree *expr_p, tree *pre_p, bool want_value)
3470 enum tree_code code, ocode;
3471 tree lhs, rhs, new_rhs, other, realpart, imagpart;
3473 lhs = GENERIC_TREE_OPERAND (*expr_p, 0);
3474 rhs = GENERIC_TREE_OPERAND (*expr_p, 1);
3475 code = TREE_CODE (lhs);
3476 lhs = TREE_OPERAND (lhs, 0);
3478 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
3479 other = build1 (ocode, TREE_TYPE (rhs), lhs);
3480 other = get_formal_tmp_var (other, pre_p);
3482 realpart = code == REALPART_EXPR ? rhs : other;
3483 imagpart = code == REALPART_EXPR ? other : rhs;
3485 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
3486 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
3487 else
3488 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
3490 GENERIC_TREE_OPERAND (*expr_p, 0) = lhs;
3491 GENERIC_TREE_OPERAND (*expr_p, 1) = new_rhs;
3493 if (want_value)
3495 append_to_statement_list (*expr_p, pre_p);
3496 *expr_p = rhs;
3499 return GS_ALL_DONE;
3503 /* Destructively convert the TREE pointer in TP into a gimple tuple if
3504 appropriate. */
3506 static void
3507 tree_to_gimple_tuple (tree *tp)
3510 switch (TREE_CODE (*tp))
3512 case GIMPLE_MODIFY_STMT:
3513 return;
3514 case MODIFY_EXPR:
3516 struct gimple_stmt *gs;
3517 tree lhs = TREE_OPERAND (*tp, 0);
3518 bool def_stmt_self_p = false;
3520 if (TREE_CODE (lhs) == SSA_NAME)
3522 if (SSA_NAME_DEF_STMT (lhs) == *tp)
3523 def_stmt_self_p = true;
3526 gs = &make_node (GIMPLE_MODIFY_STMT)->gstmt;
3527 gs->base = (*tp)->base;
3528 /* The set to base above overwrites the CODE. */
3529 TREE_SET_CODE ((tree) gs, GIMPLE_MODIFY_STMT);
3531 gs->locus = EXPR_LOCUS (*tp);
3532 gs->operands[0] = TREE_OPERAND (*tp, 0);
3533 gs->operands[1] = TREE_OPERAND (*tp, 1);
3534 gs->block = TREE_BLOCK (*tp);
3535 *tp = (tree)gs;
3537 /* If we re-gimplify a set to an SSA_NAME, we must change the
3538 SSA name's DEF_STMT link. */
3539 if (def_stmt_self_p)
3540 SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (*tp, 0)) = *tp;
3542 return;
3544 default:
3545 break;
3549 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
3551 modify_expr
3552 : varname '=' rhs
3553 | '*' ID '=' rhs
3555 PRE_P points to the list where side effects that must happen before
3556 *EXPR_P should be stored.
3558 POST_P points to the list where side effects that must happen after
3559 *EXPR_P should be stored.
3561 WANT_VALUE is nonzero iff we want to use the value of this expression
3562 in another expression. */
3564 static enum gimplify_status
3565 gimplify_modify_expr (tree *expr_p, tree *pre_p, tree *post_p, bool want_value)
3567 tree *from_p = &GENERIC_TREE_OPERAND (*expr_p, 1);
3568 tree *to_p = &GENERIC_TREE_OPERAND (*expr_p, 0);
3569 enum gimplify_status ret = GS_UNHANDLED;
3571 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
3572 || TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT
3573 || TREE_CODE (*expr_p) == INIT_EXPR);
3575 /* For zero sized types only gimplify the left hand side and right hand side
3576 as statements and throw away the assignment. */
3577 if (zero_sized_type (TREE_TYPE (*from_p)))
3579 gimplify_stmt (from_p);
3580 gimplify_stmt (to_p);
3581 append_to_statement_list (*from_p, pre_p);
3582 append_to_statement_list (*to_p, pre_p);
3583 *expr_p = NULL_TREE;
3584 return GS_ALL_DONE;
3587 /* See if any simplifications can be done based on what the RHS is. */
3588 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3589 want_value);
3590 if (ret != GS_UNHANDLED)
3591 return ret;
3593 /* If the value being copied is of variable width, compute the length
3594 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
3595 before gimplifying any of the operands so that we can resolve any
3596 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
3597 the size of the expression to be copied, not of the destination, so
3598 that is what we must here. */
3599 maybe_with_size_expr (from_p);
3601 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
3602 if (ret == GS_ERROR)
3603 return ret;
3605 ret = gimplify_expr (from_p, pre_p, post_p,
3606 rhs_predicate_for (*to_p), fb_rvalue);
3607 if (ret == GS_ERROR)
3608 return ret;
3610 /* Now see if the above changed *from_p to something we handle specially. */
3611 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
3612 want_value);
3613 if (ret != GS_UNHANDLED)
3614 return ret;
3616 /* If we've got a variable sized assignment between two lvalues (i.e. does
3617 not involve a call), then we can make things a bit more straightforward
3618 by converting the assignment to memcpy or memset. */
3619 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
3621 tree from = TREE_OPERAND (*from_p, 0);
3622 tree size = TREE_OPERAND (*from_p, 1);
3624 if (TREE_CODE (from) == CONSTRUCTOR)
3625 return gimplify_modify_expr_to_memset (expr_p, size, want_value);
3626 if (is_gimple_addressable (from))
3628 *from_p = from;
3629 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value);
3633 /* Transform partial stores to non-addressable complex variables into
3634 total stores. This allows us to use real instead of virtual operands
3635 for these variables, which improves optimization. */
3636 if ((TREE_CODE (*to_p) == REALPART_EXPR
3637 || TREE_CODE (*to_p) == IMAGPART_EXPR)
3638 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
3639 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
3641 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
3643 /* If we've somehow already got an SSA_NAME on the LHS, then
3644 we're probably modified it twice. Not good. */
3645 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
3646 *to_p = make_ssa_name (*to_p, *expr_p);
3649 if (want_value)
3651 tree_to_gimple_tuple (expr_p);
3653 append_to_statement_list (*expr_p, pre_p);
3654 *expr_p = *to_p;
3655 return GS_OK;
3658 return GS_ALL_DONE;
3661 /* Gimplify a comparison between two variable-sized objects. Do this
3662 with a call to BUILT_IN_MEMCMP. */
3664 static enum gimplify_status
3665 gimplify_variable_sized_compare (tree *expr_p)
3667 tree op0 = TREE_OPERAND (*expr_p, 0);
3668 tree op1 = TREE_OPERAND (*expr_p, 1);
3669 tree args, t, dest;
3671 t = TYPE_SIZE_UNIT (TREE_TYPE (op0));
3672 t = unshare_expr (t);
3673 t = SUBSTITUTE_PLACEHOLDER_IN_EXPR (t, op0);
3674 args = tree_cons (NULL, t, NULL);
3675 t = build_fold_addr_expr (op1);
3676 args = tree_cons (NULL, t, args);
3677 dest = build_fold_addr_expr (op0);
3678 args = tree_cons (NULL, dest, args);
3679 t = implicit_built_in_decls[BUILT_IN_MEMCMP];
3680 t = build_function_call_expr (t, args);
3681 *expr_p
3682 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
3684 return GS_OK;
3687 /* Gimplify a comparison between two aggregate objects of integral scalar
3688 mode as a comparison between the bitwise equivalent scalar values. */
3690 static enum gimplify_status
3691 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
3693 tree op0 = TREE_OPERAND (*expr_p, 0);
3694 tree op1 = TREE_OPERAND (*expr_p, 1);
3696 tree type = TREE_TYPE (op0);
3697 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
3699 op0 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op0);
3700 op1 = fold_build1 (VIEW_CONVERT_EXPR, scalar_type, op1);
3702 *expr_p
3703 = fold_build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
3705 return GS_OK;
3708 /* Gimplify TRUTH_ANDIF_EXPR and TRUTH_ORIF_EXPR expressions. EXPR_P
3709 points to the expression to gimplify.
3711 Expressions of the form 'a && b' are gimplified to:
3713 a && b ? true : false
3715 gimplify_cond_expr will do the rest.
3717 PRE_P points to the list where side effects that must happen before
3718 *EXPR_P should be stored. */
3720 static enum gimplify_status
3721 gimplify_boolean_expr (tree *expr_p)
3723 /* Preserve the original type of the expression. */
3724 tree type = TREE_TYPE (*expr_p);
3726 *expr_p = build3 (COND_EXPR, type, *expr_p,
3727 fold_convert (type, boolean_true_node),
3728 fold_convert (type, boolean_false_node));
3730 return GS_OK;
3733 /* Gimplifies an expression sequence. This function gimplifies each
3734 expression and re-writes the original expression with the last
3735 expression of the sequence in GIMPLE form.
3737 PRE_P points to the list where the side effects for all the
3738 expressions in the sequence will be emitted.
3740 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
3741 /* ??? Should rearrange to share the pre-queue with all the indirect
3742 invocations of gimplify_expr. Would probably save on creations
3743 of statement_list nodes. */
3745 static enum gimplify_status
3746 gimplify_compound_expr (tree *expr_p, tree *pre_p, bool want_value)
3748 tree t = *expr_p;
3752 tree *sub_p = &TREE_OPERAND (t, 0);
3754 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
3755 gimplify_compound_expr (sub_p, pre_p, false);
3756 else
3757 gimplify_stmt (sub_p);
3758 append_to_statement_list (*sub_p, pre_p);
3760 t = TREE_OPERAND (t, 1);
3762 while (TREE_CODE (t) == COMPOUND_EXPR);
3764 *expr_p = t;
3765 if (want_value)
3766 return GS_OK;
3767 else
3769 gimplify_stmt (expr_p);
3770 return GS_ALL_DONE;
3774 /* Gimplifies a statement list. These may be created either by an
3775 enlightened front-end, or by shortcut_cond_expr. */
3777 static enum gimplify_status
3778 gimplify_statement_list (tree *expr_p, tree *pre_p)
3780 tree temp = voidify_wrapper_expr (*expr_p, NULL);
3782 tree_stmt_iterator i = tsi_start (*expr_p);
3784 while (!tsi_end_p (i))
3786 tree t;
3788 gimplify_stmt (tsi_stmt_ptr (i));
3790 t = tsi_stmt (i);
3791 if (t == NULL)
3792 tsi_delink (&i);
3793 else if (TREE_CODE (t) == STATEMENT_LIST)
3795 tsi_link_before (&i, t, TSI_SAME_STMT);
3796 tsi_delink (&i);
3798 else
3799 tsi_next (&i);
3802 if (temp)
3804 append_to_statement_list (*expr_p, pre_p);
3805 *expr_p = temp;
3806 return GS_OK;
3809 return GS_ALL_DONE;
3812 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
3813 gimplify. After gimplification, EXPR_P will point to a new temporary
3814 that holds the original value of the SAVE_EXPR node.
3816 PRE_P points to the list where side effects that must happen before
3817 *EXPR_P should be stored. */
3819 static enum gimplify_status
3820 gimplify_save_expr (tree *expr_p, tree *pre_p, tree *post_p)
3822 enum gimplify_status ret = GS_ALL_DONE;
3823 tree val;
3825 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
3826 val = TREE_OPERAND (*expr_p, 0);
3828 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
3829 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
3831 /* The operand may be a void-valued expression such as SAVE_EXPRs
3832 generated by the Java frontend for class initialization. It is
3833 being executed only for its side-effects. */
3834 if (TREE_TYPE (val) == void_type_node)
3836 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3837 is_gimple_stmt, fb_none);
3838 append_to_statement_list (TREE_OPERAND (*expr_p, 0), pre_p);
3839 val = NULL;
3841 else
3842 val = get_initialized_tmp_var (val, pre_p, post_p);
3844 TREE_OPERAND (*expr_p, 0) = val;
3845 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
3848 *expr_p = val;
3850 return ret;
3853 /* Re-write the ADDR_EXPR node pointed to by EXPR_P
3855 unary_expr
3856 : ...
3857 | '&' varname
3860 PRE_P points to the list where side effects that must happen before
3861 *EXPR_P should be stored.
3863 POST_P points to the list where side effects that must happen after
3864 *EXPR_P should be stored. */
3866 static enum gimplify_status
3867 gimplify_addr_expr (tree *expr_p, tree *pre_p, tree *post_p)
3869 tree expr = *expr_p;
3870 tree op0 = TREE_OPERAND (expr, 0);
3871 enum gimplify_status ret;
3873 switch (TREE_CODE (op0))
3875 case INDIRECT_REF:
3876 case MISALIGNED_INDIRECT_REF:
3877 do_indirect_ref:
3878 /* Check if we are dealing with an expression of the form '&*ptr'.
3879 While the front end folds away '&*ptr' into 'ptr', these
3880 expressions may be generated internally by the compiler (e.g.,
3881 builtins like __builtin_va_end). */
3882 /* Caution: the silent array decomposition semantics we allow for
3883 ADDR_EXPR means we can't always discard the pair. */
3884 /* Gimplification of the ADDR_EXPR operand may drop
3885 cv-qualification conversions, so make sure we add them if
3886 needed. */
3888 tree op00 = TREE_OPERAND (op0, 0);
3889 tree t_expr = TREE_TYPE (expr);
3890 tree t_op00 = TREE_TYPE (op00);
3892 if (!lang_hooks.types_compatible_p (t_expr, t_op00))
3894 #ifdef ENABLE_CHECKING
3895 tree t_op0 = TREE_TYPE (op0);
3896 gcc_assert (POINTER_TYPE_P (t_expr)
3897 && cpt_same_type (TREE_CODE (t_op0) == ARRAY_TYPE
3898 ? TREE_TYPE (t_op0) : t_op0,
3899 TREE_TYPE (t_expr))
3900 && POINTER_TYPE_P (t_op00)
3901 && cpt_same_type (t_op0, TREE_TYPE (t_op00)));
3902 #endif
3903 op00 = fold_convert (TREE_TYPE (expr), op00);
3905 *expr_p = op00;
3906 ret = GS_OK;
3908 break;
3910 case VIEW_CONVERT_EXPR:
3911 /* Take the address of our operand and then convert it to the type of
3912 this ADDR_EXPR.
3914 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
3915 all clear. The impact of this transformation is even less clear. */
3917 /* If the operand is a useless conversion, look through it. Doing so
3918 guarantees that the ADDR_EXPR and its operand will remain of the
3919 same type. */
3920 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
3921 op0 = TREE_OPERAND (op0, 0);
3923 *expr_p = fold_convert (TREE_TYPE (expr),
3924 build_fold_addr_expr (TREE_OPERAND (op0, 0)));
3925 ret = GS_OK;
3926 break;
3928 default:
3929 /* We use fb_either here because the C frontend sometimes takes
3930 the address of a call that returns a struct; see
3931 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
3932 the implied temporary explicit. */
3933 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
3934 is_gimple_addressable, fb_either);
3935 if (ret != GS_ERROR)
3937 op0 = TREE_OPERAND (expr, 0);
3939 /* For various reasons, the gimplification of the expression
3940 may have made a new INDIRECT_REF. */
3941 if (TREE_CODE (op0) == INDIRECT_REF)
3942 goto do_indirect_ref;
3944 /* Make sure TREE_INVARIANT, TREE_CONSTANT, and TREE_SIDE_EFFECTS
3945 is set properly. */
3946 recompute_tree_invariant_for_addr_expr (expr);
3948 /* Mark the RHS addressable. */
3949 lang_hooks.mark_addressable (TREE_OPERAND (expr, 0));
3951 break;
3954 return ret;
3957 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
3958 value; output operands should be a gimple lvalue. */
3960 static enum gimplify_status
3961 gimplify_asm_expr (tree *expr_p, tree *pre_p, tree *post_p)
3963 tree expr = *expr_p;
3964 int noutputs = list_length (ASM_OUTPUTS (expr));
3965 const char **oconstraints
3966 = (const char **) alloca ((noutputs) * sizeof (const char *));
3967 int i;
3968 tree link;
3969 const char *constraint;
3970 bool allows_mem, allows_reg, is_inout;
3971 enum gimplify_status ret, tret;
3973 ret = GS_ALL_DONE;
3974 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = TREE_CHAIN (link))
3976 size_t constraint_len;
3977 oconstraints[i] = constraint
3978 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3979 constraint_len = strlen (constraint);
3980 if (constraint_len == 0)
3981 continue;
3983 parse_output_constraint (&constraint, i, 0, 0,
3984 &allows_mem, &allows_reg, &is_inout);
3986 if (!allows_reg && allows_mem)
3987 lang_hooks.mark_addressable (TREE_VALUE (link));
3989 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
3990 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
3991 fb_lvalue | fb_mayfail);
3992 if (tret == GS_ERROR)
3994 error ("invalid lvalue in asm output %d", i);
3995 ret = tret;
3998 if (is_inout)
4000 /* An input/output operand. To give the optimizers more
4001 flexibility, split it into separate input and output
4002 operands. */
4003 tree input;
4004 char buf[10];
4006 /* Turn the in/out constraint into an output constraint. */
4007 char *p = xstrdup (constraint);
4008 p[0] = '=';
4009 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
4011 /* And add a matching input constraint. */
4012 if (allows_reg)
4014 sprintf (buf, "%d", i);
4016 /* If there are multiple alternatives in the constraint,
4017 handle each of them individually. Those that allow register
4018 will be replaced with operand number, the others will stay
4019 unchanged. */
4020 if (strchr (p, ',') != NULL)
4022 size_t len = 0, buflen = strlen (buf);
4023 char *beg, *end, *str, *dst;
4025 for (beg = p + 1;;)
4027 end = strchr (beg, ',');
4028 if (end == NULL)
4029 end = strchr (beg, '\0');
4030 if ((size_t) (end - beg) < buflen)
4031 len += buflen + 1;
4032 else
4033 len += end - beg + 1;
4034 if (*end)
4035 beg = end + 1;
4036 else
4037 break;
4040 str = (char *) alloca (len);
4041 for (beg = p + 1, dst = str;;)
4043 const char *tem;
4044 bool mem_p, reg_p, inout_p;
4046 end = strchr (beg, ',');
4047 if (end)
4048 *end = '\0';
4049 beg[-1] = '=';
4050 tem = beg - 1;
4051 parse_output_constraint (&tem, i, 0, 0,
4052 &mem_p, &reg_p, &inout_p);
4053 if (dst != str)
4054 *dst++ = ',';
4055 if (reg_p)
4057 memcpy (dst, buf, buflen);
4058 dst += buflen;
4060 else
4062 if (end)
4063 len = end - beg;
4064 else
4065 len = strlen (beg);
4066 memcpy (dst, beg, len);
4067 dst += len;
4069 if (end)
4070 beg = end + 1;
4071 else
4072 break;
4074 *dst = '\0';
4075 input = build_string (dst - str, str);
4077 else
4078 input = build_string (strlen (buf), buf);
4080 else
4081 input = build_string (constraint_len - 1, constraint + 1);
4083 free (p);
4085 input = build_tree_list (build_tree_list (NULL_TREE, input),
4086 unshare_expr (TREE_VALUE (link)));
4087 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
4091 for (link = ASM_INPUTS (expr); link; ++i, link = TREE_CHAIN (link))
4093 constraint
4094 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4095 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4096 oconstraints, &allows_mem, &allows_reg);
4098 /* If the operand is a memory input, it should be an lvalue. */
4099 if (!allows_reg && allows_mem)
4101 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4102 is_gimple_lvalue, fb_lvalue | fb_mayfail);
4103 lang_hooks.mark_addressable (TREE_VALUE (link));
4104 if (tret == GS_ERROR)
4106 error ("memory input %d is not directly addressable", i);
4107 ret = tret;
4110 else
4112 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
4113 is_gimple_asm_val, fb_rvalue);
4114 if (tret == GS_ERROR)
4115 ret = tret;
4119 return ret;
4122 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
4123 WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
4124 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
4125 return to this function.
4127 FIXME should we complexify the prequeue handling instead? Or use flags
4128 for all the cleanups and let the optimizer tighten them up? The current
4129 code seems pretty fragile; it will break on a cleanup within any
4130 non-conditional nesting. But any such nesting would be broken, anyway;
4131 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
4132 and continues out of it. We can do that at the RTL level, though, so
4133 having an optimizer to tighten up try/finally regions would be a Good
4134 Thing. */
4136 static enum gimplify_status
4137 gimplify_cleanup_point_expr (tree *expr_p, tree *pre_p)
4139 tree_stmt_iterator iter;
4140 tree body;
4142 tree temp = voidify_wrapper_expr (*expr_p, NULL);
4144 /* We only care about the number of conditions between the innermost
4145 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
4146 any cleanups collected outside the CLEANUP_POINT_EXPR. */
4147 int old_conds = gimplify_ctxp->conditions;
4148 tree old_cleanups = gimplify_ctxp->conditional_cleanups;
4149 gimplify_ctxp->conditions = 0;
4150 gimplify_ctxp->conditional_cleanups = NULL_TREE;
4152 body = TREE_OPERAND (*expr_p, 0);
4153 gimplify_to_stmt_list (&body);
4155 gimplify_ctxp->conditions = old_conds;
4156 gimplify_ctxp->conditional_cleanups = old_cleanups;
4158 for (iter = tsi_start (body); !tsi_end_p (iter); )
4160 tree *wce_p = tsi_stmt_ptr (iter);
4161 tree wce = *wce_p;
4163 if (TREE_CODE (wce) == WITH_CLEANUP_EXPR)
4165 if (tsi_one_before_end_p (iter))
4167 tsi_link_before (&iter, TREE_OPERAND (wce, 0), TSI_SAME_STMT);
4168 tsi_delink (&iter);
4169 break;
4171 else
4173 tree sl, tfe;
4174 enum tree_code code;
4176 if (CLEANUP_EH_ONLY (wce))
4177 code = TRY_CATCH_EXPR;
4178 else
4179 code = TRY_FINALLY_EXPR;
4181 sl = tsi_split_statement_list_after (&iter);
4182 tfe = build2 (code, void_type_node, sl, NULL_TREE);
4183 append_to_statement_list (TREE_OPERAND (wce, 0),
4184 &TREE_OPERAND (tfe, 1));
4185 *wce_p = tfe;
4186 iter = tsi_start (sl);
4189 else
4190 tsi_next (&iter);
4193 if (temp)
4195 *expr_p = temp;
4196 append_to_statement_list (body, pre_p);
4197 return GS_OK;
4199 else
4201 *expr_p = body;
4202 return GS_ALL_DONE;
4206 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
4207 is the cleanup action required. */
4209 static void
4210 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, tree *pre_p)
4212 tree wce;
4214 /* Errors can result in improperly nested cleanups. Which results in
4215 confusion when trying to resolve the WITH_CLEANUP_EXPR. */
4216 if (errorcount || sorrycount)
4217 return;
4219 if (gimple_conditional_context ())
4221 /* If we're in a conditional context, this is more complex. We only
4222 want to run the cleanup if we actually ran the initialization that
4223 necessitates it, but we want to run it after the end of the
4224 conditional context. So we wrap the try/finally around the
4225 condition and use a flag to determine whether or not to actually
4226 run the destructor. Thus
4228 test ? f(A()) : 0
4230 becomes (approximately)
4232 flag = 0;
4233 try {
4234 if (test) { A::A(temp); flag = 1; val = f(temp); }
4235 else { val = 0; }
4236 } finally {
4237 if (flag) A::~A(temp);
4242 tree flag = create_tmp_var (boolean_type_node, "cleanup");
4243 tree ffalse = build2 (GIMPLE_MODIFY_STMT, void_type_node, flag,
4244 boolean_false_node);
4245 tree ftrue = build2 (GIMPLE_MODIFY_STMT, void_type_node, flag,
4246 boolean_true_node);
4247 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
4248 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4249 append_to_statement_list (ffalse, &gimplify_ctxp->conditional_cleanups);
4250 append_to_statement_list (wce, &gimplify_ctxp->conditional_cleanups);
4251 append_to_statement_list (ftrue, pre_p);
4253 /* Because of this manipulation, and the EH edges that jump
4254 threading cannot redirect, the temporary (VAR) will appear
4255 to be used uninitialized. Don't warn. */
4256 TREE_NO_WARNING (var) = 1;
4258 else
4260 wce = build1 (WITH_CLEANUP_EXPR, void_type_node, cleanup);
4261 CLEANUP_EH_ONLY (wce) = eh_only;
4262 append_to_statement_list (wce, pre_p);
4265 gimplify_stmt (&TREE_OPERAND (wce, 0));
4268 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
4270 static enum gimplify_status
4271 gimplify_target_expr (tree *expr_p, tree *pre_p, tree *post_p)
4273 tree targ = *expr_p;
4274 tree temp = TARGET_EXPR_SLOT (targ);
4275 tree init = TARGET_EXPR_INITIAL (targ);
4276 enum gimplify_status ret;
4278 if (init)
4280 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
4281 to the temps list. */
4282 gimple_add_tmp_var (temp);
4284 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
4285 expression is supposed to initialize the slot. */
4286 if (VOID_TYPE_P (TREE_TYPE (init)))
4287 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
4288 else
4290 init = build2 (INIT_EXPR, void_type_node, temp, init);
4291 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt,
4292 fb_none);
4294 if (ret == GS_ERROR)
4295 return GS_ERROR;
4296 append_to_statement_list (init, pre_p);
4298 /* If needed, push the cleanup for the temp. */
4299 if (TARGET_EXPR_CLEANUP (targ))
4301 gimplify_stmt (&TARGET_EXPR_CLEANUP (targ));
4302 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
4303 CLEANUP_EH_ONLY (targ), pre_p);
4306 /* Only expand this once. */
4307 TREE_OPERAND (targ, 3) = init;
4308 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
4310 else
4311 /* We should have expanded this before. */
4312 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
4314 *expr_p = temp;
4315 return GS_OK;
4318 /* Gimplification of expression trees. */
4320 /* Gimplify an expression which appears at statement context; usually, this
4321 means replacing it with a suitably gimple STATEMENT_LIST. */
4323 void
4324 gimplify_stmt (tree *stmt_p)
4326 gimplify_expr (stmt_p, NULL, NULL, is_gimple_stmt, fb_none);
4329 /* Similarly, but force the result to be a STATEMENT_LIST. */
4331 void
4332 gimplify_to_stmt_list (tree *stmt_p)
4334 gimplify_stmt (stmt_p);
4335 if (!*stmt_p)
4336 *stmt_p = alloc_stmt_list ();
4337 else if (TREE_CODE (*stmt_p) != STATEMENT_LIST)
4339 tree t = *stmt_p;
4340 *stmt_p = alloc_stmt_list ();
4341 append_to_statement_list (t, stmt_p);
4346 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
4347 to CTX. If entries already exist, force them to be some flavor of private.
4348 If there is no enclosing parallel, do nothing. */
4350 void
4351 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
4353 splay_tree_node n;
4355 if (decl == NULL || !DECL_P (decl))
4356 return;
4360 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4361 if (n != NULL)
4363 if (n->value & GOVD_SHARED)
4364 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
4365 else
4366 return;
4368 else if (ctx->is_parallel)
4369 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
4371 ctx = ctx->outer_context;
4373 while (ctx);
4376 /* Similarly for each of the type sizes of TYPE. */
4378 static void
4379 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
4381 if (type == NULL || type == error_mark_node)
4382 return;
4383 type = TYPE_MAIN_VARIANT (type);
4385 if (pointer_set_insert (ctx->privatized_types, type))
4386 return;
4388 switch (TREE_CODE (type))
4390 case INTEGER_TYPE:
4391 case ENUMERAL_TYPE:
4392 case BOOLEAN_TYPE:
4393 case REAL_TYPE:
4394 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
4395 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
4396 break;
4398 case ARRAY_TYPE:
4399 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4400 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
4401 break;
4403 case RECORD_TYPE:
4404 case UNION_TYPE:
4405 case QUAL_UNION_TYPE:
4407 tree field;
4408 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4409 if (TREE_CODE (field) == FIELD_DECL)
4411 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
4412 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
4415 break;
4417 case POINTER_TYPE:
4418 case REFERENCE_TYPE:
4419 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
4420 break;
4422 default:
4423 break;
4426 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
4427 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
4428 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
4431 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
4433 static void
4434 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
4436 splay_tree_node n;
4437 unsigned int nflags;
4438 tree t;
4440 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4441 return;
4443 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
4444 there are constructors involved somewhere. */
4445 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
4446 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
4447 flags |= GOVD_SEEN;
4449 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4450 if (n != NULL)
4452 /* We shouldn't be re-adding the decl with the same data
4453 sharing class. */
4454 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
4455 /* The only combination of data sharing classes we should see is
4456 FIRSTPRIVATE and LASTPRIVATE. */
4457 nflags = n->value | flags;
4458 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
4459 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
4460 n->value = nflags;
4461 return;
4464 /* When adding a variable-sized variable, we have to handle all sorts
4465 of additional bits of data: the pointer replacement variable, and
4466 the parameters of the type. */
4467 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
4469 /* Add the pointer replacement variable as PRIVATE if the variable
4470 replacement is private, else FIRSTPRIVATE since we'll need the
4471 address of the original variable either for SHARED, or for the
4472 copy into or out of the context. */
4473 if (!(flags & GOVD_LOCAL))
4475 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
4476 nflags |= flags & GOVD_SEEN;
4477 t = DECL_VALUE_EXPR (decl);
4478 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
4479 t = TREE_OPERAND (t, 0);
4480 gcc_assert (DECL_P (t));
4481 omp_add_variable (ctx, t, nflags);
4484 /* Add all of the variable and type parameters (which should have
4485 been gimplified to a formal temporary) as FIRSTPRIVATE. */
4486 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
4487 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
4488 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4490 /* The variable-sized variable itself is never SHARED, only some form
4491 of PRIVATE. The sharing would take place via the pointer variable
4492 which we remapped above. */
4493 if (flags & GOVD_SHARED)
4494 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
4495 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
4497 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
4498 alloca statement we generate for the variable, so make sure it
4499 is available. This isn't automatically needed for the SHARED
4500 case, since we won't be allocating local storage then. */
4501 else
4502 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
4504 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
4506 gcc_assert ((flags & GOVD_LOCAL) == 0);
4507 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
4509 /* Similar to the direct variable sized case above, we'll need the
4510 size of references being privatized. */
4511 if ((flags & GOVD_SHARED) == 0)
4513 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
4514 if (TREE_CODE (t) != INTEGER_CST)
4515 omp_notice_variable (ctx, t, true);
4519 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
4522 /* Record the fact that DECL was used within the OpenMP context CTX.
4523 IN_CODE is true when real code uses DECL, and false when we should
4524 merely emit default(none) errors. Return true if DECL is going to
4525 be remapped and thus DECL shouldn't be gimplified into its
4526 DECL_VALUE_EXPR (if any). */
4528 static bool
4529 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
4531 splay_tree_node n;
4532 unsigned flags = in_code ? GOVD_SEEN : 0;
4533 bool ret = false, shared;
4535 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4536 return false;
4538 /* Threadprivate variables are predetermined. */
4539 if (is_global_var (decl))
4541 if (DECL_THREAD_LOCAL_P (decl))
4542 return false;
4544 if (DECL_HAS_VALUE_EXPR_P (decl))
4546 tree value = get_base_address (DECL_VALUE_EXPR (decl));
4548 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
4549 return false;
4553 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4554 if (n == NULL)
4556 enum omp_clause_default_kind default_kind, kind;
4558 if (!ctx->is_parallel)
4559 goto do_outer;
4561 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
4562 remapped firstprivate instead of shared. To some extent this is
4563 addressed in omp_firstprivatize_type_sizes, but not effectively. */
4564 default_kind = ctx->default_kind;
4565 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
4566 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
4567 default_kind = kind;
4569 switch (default_kind)
4571 case OMP_CLAUSE_DEFAULT_NONE:
4572 error ("%qs not specified in enclosing parallel",
4573 IDENTIFIER_POINTER (DECL_NAME (decl)));
4574 error ("%Henclosing parallel", &ctx->location);
4575 /* FALLTHRU */
4576 case OMP_CLAUSE_DEFAULT_SHARED:
4577 flags |= GOVD_SHARED;
4578 break;
4579 case OMP_CLAUSE_DEFAULT_PRIVATE:
4580 flags |= GOVD_PRIVATE;
4581 break;
4582 default:
4583 gcc_unreachable ();
4586 omp_add_variable (ctx, decl, flags);
4588 shared = (flags & GOVD_SHARED) != 0;
4589 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4590 goto do_outer;
4593 shared = ((flags | n->value) & GOVD_SHARED) != 0;
4594 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
4596 /* If nothing changed, there's nothing left to do. */
4597 if ((n->value & flags) == flags)
4598 return ret;
4599 flags |= n->value;
4600 n->value = flags;
4602 do_outer:
4603 /* If the variable is private in the current context, then we don't
4604 need to propagate anything to an outer context. */
4605 if (flags & GOVD_PRIVATE)
4606 return ret;
4607 if (ctx->outer_context
4608 && omp_notice_variable (ctx->outer_context, decl, in_code))
4609 return true;
4610 return ret;
4613 /* Verify that DECL is private within CTX. If there's specific information
4614 to the contrary in the innermost scope, generate an error. */
4616 static bool
4617 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
4619 splay_tree_node n;
4621 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
4622 if (n != NULL)
4624 if (n->value & GOVD_SHARED)
4626 if (ctx == gimplify_omp_ctxp)
4628 error ("iteration variable %qs should be private",
4629 IDENTIFIER_POINTER (DECL_NAME (decl)));
4630 n->value = GOVD_PRIVATE;
4631 return true;
4633 else
4634 return false;
4636 else if ((n->value & GOVD_EXPLICIT) != 0
4637 && (ctx == gimplify_omp_ctxp
4638 || (ctx->is_combined_parallel
4639 && gimplify_omp_ctxp->outer_context == ctx)))
4641 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
4642 error ("iteration variable %qs should not be firstprivate",
4643 IDENTIFIER_POINTER (DECL_NAME (decl)));
4644 else if ((n->value & GOVD_REDUCTION) != 0)
4645 error ("iteration variable %qs should not be reduction",
4646 IDENTIFIER_POINTER (DECL_NAME (decl)));
4648 return true;
4651 if (ctx->is_parallel)
4652 return false;
4653 else if (ctx->outer_context)
4654 return omp_is_private (ctx->outer_context, decl);
4655 else
4656 return !is_global_var (decl);
4659 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
4660 and previous omp contexts. */
4662 static void
4663 gimplify_scan_omp_clauses (tree *list_p, tree *pre_p, bool in_parallel,
4664 bool in_combined_parallel)
4666 struct gimplify_omp_ctx *ctx, *outer_ctx;
4667 tree c;
4669 ctx = new_omp_context (in_parallel, in_combined_parallel);
4670 outer_ctx = ctx->outer_context;
4672 while ((c = *list_p) != NULL)
4674 enum gimplify_status gs;
4675 bool remove = false;
4676 bool notice_outer = true;
4677 unsigned int flags;
4678 tree decl;
4680 switch (OMP_CLAUSE_CODE (c))
4682 case OMP_CLAUSE_PRIVATE:
4683 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
4684 notice_outer = false;
4685 goto do_add;
4686 case OMP_CLAUSE_SHARED:
4687 flags = GOVD_SHARED | GOVD_EXPLICIT;
4688 goto do_add;
4689 case OMP_CLAUSE_FIRSTPRIVATE:
4690 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
4691 goto do_add;
4692 case OMP_CLAUSE_LASTPRIVATE:
4693 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
4694 goto do_add;
4695 case OMP_CLAUSE_REDUCTION:
4696 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
4697 goto do_add;
4699 do_add:
4700 decl = OMP_CLAUSE_DECL (c);
4701 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4703 remove = true;
4704 break;
4706 /* Handle NRV results passed by reference. */
4707 if (TREE_CODE (decl) == INDIRECT_REF
4708 && TREE_CODE (TREE_OPERAND (decl, 0)) == RESULT_DECL
4709 && DECL_BY_REFERENCE (TREE_OPERAND (decl, 0)))
4710 OMP_CLAUSE_DECL (c) = decl = TREE_OPERAND (decl, 0);
4711 omp_add_variable (ctx, decl, flags);
4712 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4713 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
4715 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
4716 GOVD_LOCAL | GOVD_SEEN);
4717 gimplify_omp_ctxp = ctx;
4718 push_gimplify_context ();
4719 gimplify_stmt (&OMP_CLAUSE_REDUCTION_INIT (c));
4720 pop_gimplify_context (OMP_CLAUSE_REDUCTION_INIT (c));
4721 push_gimplify_context ();
4722 gimplify_stmt (&OMP_CLAUSE_REDUCTION_MERGE (c));
4723 pop_gimplify_context (OMP_CLAUSE_REDUCTION_MERGE (c));
4724 gimplify_omp_ctxp = outer_ctx;
4726 if (notice_outer)
4727 goto do_notice;
4728 break;
4730 case OMP_CLAUSE_COPYIN:
4731 case OMP_CLAUSE_COPYPRIVATE:
4732 decl = OMP_CLAUSE_DECL (c);
4733 if (decl == error_mark_node || TREE_TYPE (decl) == error_mark_node)
4735 remove = true;
4736 break;
4738 /* Handle NRV results passed by reference. */
4739 if (TREE_CODE (decl) == INDIRECT_REF
4740 && TREE_CODE (TREE_OPERAND (decl, 0)) == RESULT_DECL
4741 && DECL_BY_REFERENCE (TREE_OPERAND (decl, 0)))
4742 OMP_CLAUSE_DECL (c) = decl = TREE_OPERAND (decl, 0);
4743 do_notice:
4744 if (outer_ctx)
4745 omp_notice_variable (outer_ctx, decl, true);
4746 break;
4748 case OMP_CLAUSE_IF:
4749 OMP_CLAUSE_OPERAND (c, 0)
4750 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
4751 /* Fall through. */
4753 case OMP_CLAUSE_SCHEDULE:
4754 case OMP_CLAUSE_NUM_THREADS:
4755 gs = gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
4756 is_gimple_val, fb_rvalue);
4757 if (gs == GS_ERROR)
4758 remove = true;
4759 break;
4761 case OMP_CLAUSE_NOWAIT:
4762 case OMP_CLAUSE_ORDERED:
4763 break;
4765 case OMP_CLAUSE_DEFAULT:
4766 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
4767 break;
4769 default:
4770 gcc_unreachable ();
4773 if (remove)
4774 *list_p = OMP_CLAUSE_CHAIN (c);
4775 else
4776 list_p = &OMP_CLAUSE_CHAIN (c);
4779 gimplify_omp_ctxp = ctx;
4782 /* For all variables that were not actually used within the context,
4783 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
4785 static int
4786 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
4788 tree *list_p = (tree *) data;
4789 tree decl = (tree) n->key;
4790 unsigned flags = n->value;
4791 enum omp_clause_code code;
4792 tree clause;
4793 bool private_debug;
4795 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
4796 return 0;
4797 if ((flags & GOVD_SEEN) == 0)
4798 return 0;
4799 if (flags & GOVD_DEBUG_PRIVATE)
4801 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
4802 private_debug = true;
4804 else
4805 private_debug
4806 = lang_hooks.decls.omp_private_debug_clause (decl,
4807 !!(flags & GOVD_SHARED));
4808 if (private_debug)
4809 code = OMP_CLAUSE_PRIVATE;
4810 else if (flags & GOVD_SHARED)
4812 if (is_global_var (decl))
4813 return 0;
4814 code = OMP_CLAUSE_SHARED;
4816 else if (flags & GOVD_PRIVATE)
4817 code = OMP_CLAUSE_PRIVATE;
4818 else if (flags & GOVD_FIRSTPRIVATE)
4819 code = OMP_CLAUSE_FIRSTPRIVATE;
4820 else
4821 gcc_unreachable ();
4823 clause = build_omp_clause (code);
4824 OMP_CLAUSE_DECL (clause) = decl;
4825 OMP_CLAUSE_CHAIN (clause) = *list_p;
4826 if (private_debug)
4827 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
4828 *list_p = clause;
4830 return 0;
4833 static void
4834 gimplify_adjust_omp_clauses (tree *list_p)
4836 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
4837 tree c, decl;
4839 while ((c = *list_p) != NULL)
4841 splay_tree_node n;
4842 bool remove = false;
4844 switch (OMP_CLAUSE_CODE (c))
4846 case OMP_CLAUSE_PRIVATE:
4847 case OMP_CLAUSE_SHARED:
4848 case OMP_CLAUSE_FIRSTPRIVATE:
4849 decl = OMP_CLAUSE_DECL (c);
4850 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4851 remove = !(n->value & GOVD_SEEN);
4852 if (! remove)
4854 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
4855 if ((n->value & GOVD_DEBUG_PRIVATE)
4856 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
4858 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
4859 || ((n->value & GOVD_DATA_SHARE_CLASS)
4860 == GOVD_PRIVATE));
4861 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
4862 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
4865 break;
4867 case OMP_CLAUSE_LASTPRIVATE:
4868 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
4869 accurately reflect the presence of a FIRSTPRIVATE clause. */
4870 decl = OMP_CLAUSE_DECL (c);
4871 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
4872 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
4873 = (n->value & GOVD_FIRSTPRIVATE) != 0;
4874 break;
4876 case OMP_CLAUSE_REDUCTION:
4877 case OMP_CLAUSE_COPYIN:
4878 case OMP_CLAUSE_COPYPRIVATE:
4879 case OMP_CLAUSE_IF:
4880 case OMP_CLAUSE_NUM_THREADS:
4881 case OMP_CLAUSE_SCHEDULE:
4882 case OMP_CLAUSE_NOWAIT:
4883 case OMP_CLAUSE_ORDERED:
4884 case OMP_CLAUSE_DEFAULT:
4885 break;
4887 default:
4888 gcc_unreachable ();
4891 if (remove)
4892 *list_p = OMP_CLAUSE_CHAIN (c);
4893 else
4894 list_p = &OMP_CLAUSE_CHAIN (c);
4897 /* Add in any implicit data sharing. */
4898 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
4900 gimplify_omp_ctxp = ctx->outer_context;
4901 delete_omp_context (ctx);
4904 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
4905 gimplification of the body, as well as scanning the body for used
4906 variables. We need to do this scan now, because variable-sized
4907 decls will be decomposed during gimplification. */
4909 static enum gimplify_status
4910 gimplify_omp_parallel (tree *expr_p, tree *pre_p)
4912 tree expr = *expr_p;
4914 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p, true,
4915 OMP_PARALLEL_COMBINED (expr));
4917 push_gimplify_context ();
4919 gimplify_stmt (&OMP_PARALLEL_BODY (expr));
4921 if (TREE_CODE (OMP_PARALLEL_BODY (expr)) == BIND_EXPR)
4922 pop_gimplify_context (OMP_PARALLEL_BODY (expr));
4923 else
4924 pop_gimplify_context (NULL_TREE);
4926 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
4928 return GS_ALL_DONE;
4931 /* Gimplify the gross structure of an OMP_FOR statement. */
4933 static enum gimplify_status
4934 gimplify_omp_for (tree *expr_p, tree *pre_p)
4936 tree for_stmt, decl, t;
4937 enum gimplify_status ret = GS_OK;
4939 for_stmt = *expr_p;
4941 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p, false, false);
4943 t = OMP_FOR_INIT (for_stmt);
4944 gcc_assert (TREE_CODE (t) == MODIFY_EXPR
4945 || TREE_CODE (t) == GIMPLE_MODIFY_STMT);
4946 decl = GENERIC_TREE_OPERAND (t, 0);
4947 gcc_assert (DECL_P (decl));
4948 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl)));
4950 /* Make sure the iteration variable is private. */
4951 if (omp_is_private (gimplify_omp_ctxp, decl))
4952 omp_notice_variable (gimplify_omp_ctxp, decl, true);
4953 else
4954 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
4956 ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
4957 &OMP_FOR_PRE_BODY (for_stmt),
4958 NULL, is_gimple_val, fb_rvalue);
4960 tree_to_gimple_tuple (&OMP_FOR_INIT (for_stmt));
4962 t = OMP_FOR_COND (for_stmt);
4963 gcc_assert (COMPARISON_CLASS_P (t));
4964 gcc_assert (GENERIC_TREE_OPERAND (t, 0) == decl);
4966 ret |= gimplify_expr (&GENERIC_TREE_OPERAND (t, 1),
4967 &OMP_FOR_PRE_BODY (for_stmt),
4968 NULL, is_gimple_val, fb_rvalue);
4970 tree_to_gimple_tuple (&OMP_FOR_INCR (for_stmt));
4971 t = OMP_FOR_INCR (for_stmt);
4972 switch (TREE_CODE (t))
4974 case PREINCREMENT_EXPR:
4975 case POSTINCREMENT_EXPR:
4976 t = build_int_cst (TREE_TYPE (decl), 1);
4977 goto build_modify;
4978 case PREDECREMENT_EXPR:
4979 case POSTDECREMENT_EXPR:
4980 t = build_int_cst (TREE_TYPE (decl), -1);
4981 goto build_modify;
4982 build_modify:
4983 t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
4984 t = build2 (GIMPLE_MODIFY_STMT, void_type_node, decl, t);
4985 OMP_FOR_INCR (for_stmt) = t;
4986 break;
4988 case GIMPLE_MODIFY_STMT:
4989 gcc_assert (GIMPLE_STMT_OPERAND (t, 0) == decl);
4990 t = GIMPLE_STMT_OPERAND (t, 1);
4991 switch (TREE_CODE (t))
4993 case PLUS_EXPR:
4994 if (TREE_OPERAND (t, 1) == decl)
4996 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
4997 TREE_OPERAND (t, 0) = decl;
4998 break;
5000 case MINUS_EXPR:
5001 gcc_assert (TREE_OPERAND (t, 0) == decl);
5002 break;
5003 default:
5004 gcc_unreachable ();
5007 ret |= gimplify_expr (&TREE_OPERAND (t, 1), &OMP_FOR_PRE_BODY (for_stmt),
5008 NULL, is_gimple_val, fb_rvalue);
5009 break;
5011 default:
5012 gcc_unreachable ();
5015 gimplify_to_stmt_list (&OMP_FOR_BODY (for_stmt));
5016 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
5018 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
5021 /* Gimplify the gross structure of other OpenMP worksharing constructs.
5022 In particular, OMP_SECTIONS and OMP_SINGLE. */
5024 static enum gimplify_status
5025 gimplify_omp_workshare (tree *expr_p, tree *pre_p)
5027 tree stmt = *expr_p;
5029 gimplify_scan_omp_clauses (&OMP_CLAUSES (stmt), pre_p, false, false);
5030 gimplify_to_stmt_list (&OMP_BODY (stmt));
5031 gimplify_adjust_omp_clauses (&OMP_CLAUSES (stmt));
5033 return GS_ALL_DONE;
5036 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
5037 stabilized the lhs of the atomic operation as *ADDR. Return true if
5038 EXPR is this stabilized form. */
5040 static bool
5041 goa_lhs_expr_p (tree expr, tree addr)
5043 /* Also include casts to other type variants. The C front end is fond
5044 of adding these for e.g. volatile variables. This is like
5045 STRIP_TYPE_NOPS but includes the main variant lookup. */
5046 while ((TREE_CODE (expr) == NOP_EXPR
5047 || TREE_CODE (expr) == CONVERT_EXPR
5048 || TREE_CODE (expr) == NON_LVALUE_EXPR)
5049 && TREE_OPERAND (expr, 0) != error_mark_node
5050 && (TYPE_MAIN_VARIANT (TREE_TYPE (expr))
5051 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (expr, 0)))))
5052 expr = TREE_OPERAND (expr, 0);
5054 if (TREE_CODE (expr) == INDIRECT_REF && TREE_OPERAND (expr, 0) == addr)
5055 return true;
5056 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
5057 return true;
5058 return false;
5061 /* A subroutine of gimplify_omp_atomic. Attempt to implement the atomic
5062 operation as a __sync_fetch_and_op builtin. INDEX is log2 of the
5063 size of the data type, and thus usable to find the index of the builtin
5064 decl. Returns GS_UNHANDLED if the expression is not of the proper form. */
5066 static enum gimplify_status
5067 gimplify_omp_atomic_fetch_op (tree *expr_p, tree addr, tree rhs, int index)
5069 enum built_in_function base;
5070 tree decl, args, itype;
5071 enum insn_code *optab;
5073 /* Check for one of the supported fetch-op operations. */
5074 switch (TREE_CODE (rhs))
5076 case PLUS_EXPR:
5077 base = BUILT_IN_FETCH_AND_ADD_N;
5078 optab = sync_add_optab;
5079 break;
5080 case MINUS_EXPR:
5081 base = BUILT_IN_FETCH_AND_SUB_N;
5082 optab = sync_add_optab;
5083 break;
5084 case BIT_AND_EXPR:
5085 base = BUILT_IN_FETCH_AND_AND_N;
5086 optab = sync_and_optab;
5087 break;
5088 case BIT_IOR_EXPR:
5089 base = BUILT_IN_FETCH_AND_OR_N;
5090 optab = sync_ior_optab;
5091 break;
5092 case BIT_XOR_EXPR:
5093 base = BUILT_IN_FETCH_AND_XOR_N;
5094 optab = sync_xor_optab;
5095 break;
5096 default:
5097 return GS_UNHANDLED;
5100 /* Make sure the expression is of the proper form. */
5101 if (goa_lhs_expr_p (TREE_OPERAND (rhs, 0), addr))
5102 rhs = TREE_OPERAND (rhs, 1);
5103 else if (commutative_tree_code (TREE_CODE (rhs))
5104 && goa_lhs_expr_p (TREE_OPERAND (rhs, 1), addr))
5105 rhs = TREE_OPERAND (rhs, 0);
5106 else
5107 return GS_UNHANDLED;
5109 decl = built_in_decls[base + index + 1];
5110 itype = TREE_TYPE (TREE_TYPE (decl));
5112 if (optab[TYPE_MODE (itype)] == CODE_FOR_nothing)
5113 return GS_UNHANDLED;
5115 args = tree_cons (NULL, fold_convert (itype, rhs), NULL);
5116 args = tree_cons (NULL, addr, args);
5117 *expr_p = build_function_call_expr (decl, args);
5118 return GS_OK;
5121 /* A subroutine of gimplify_omp_atomic_pipeline. Walk *EXPR_P and replace
5122 appearances of *LHS_ADDR with LHS_VAR. If an expression does not involve
5123 the lhs, evaluate it into a temporary. Return 1 if the lhs appeared as
5124 a subexpression, 0 if it did not, or -1 if an error was encountered. */
5126 static int
5127 goa_stabilize_expr (tree *expr_p, tree *pre_p, tree lhs_addr, tree lhs_var)
5129 tree expr = *expr_p;
5130 int saw_lhs;
5132 if (goa_lhs_expr_p (expr, lhs_addr))
5134 *expr_p = lhs_var;
5135 return 1;
5137 if (is_gimple_val (expr))
5138 return 0;
5140 saw_lhs = 0;
5141 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
5143 case tcc_binary:
5144 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
5145 lhs_addr, lhs_var);
5146 case tcc_unary:
5147 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
5148 lhs_addr, lhs_var);
5149 break;
5150 default:
5151 break;
5154 if (saw_lhs == 0)
5156 enum gimplify_status gs;
5157 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
5158 if (gs != GS_ALL_DONE)
5159 saw_lhs = -1;
5162 return saw_lhs;
5165 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
5167 oldval = *addr;
5168 repeat:
5169 newval = rhs; // with oldval replacing *addr in rhs
5170 oldval = __sync_val_compare_and_swap (addr, oldval, newval);
5171 if (oldval != newval)
5172 goto repeat;
5174 INDEX is log2 of the size of the data type, and thus usable to find the
5175 index of the builtin decl. */
5177 static enum gimplify_status
5178 gimplify_omp_atomic_pipeline (tree *expr_p, tree *pre_p, tree addr,
5179 tree rhs, int index)
5181 tree oldval, oldival, oldival2, newval, newival, label;
5182 tree type, itype, cmpxchg, args, x, iaddr;
5184 cmpxchg = built_in_decls[BUILT_IN_VAL_COMPARE_AND_SWAP_N + index + 1];
5185 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5186 itype = TREE_TYPE (TREE_TYPE (cmpxchg));
5188 if (sync_compare_and_swap[TYPE_MODE (itype)] == CODE_FOR_nothing)
5189 return GS_UNHANDLED;
5191 oldval = create_tmp_var (type, NULL);
5192 newval = create_tmp_var (type, NULL);
5194 /* Precompute as much of RHS as possible. In the same walk, replace
5195 occurrences of the lhs value with our temporary. */
5196 if (goa_stabilize_expr (&rhs, pre_p, addr, oldval) < 0)
5197 return GS_ERROR;
5199 x = build_fold_indirect_ref (addr);
5200 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, oldval, x);
5201 gimplify_and_add (x, pre_p);
5203 /* For floating-point values, we'll need to view-convert them to integers
5204 so that we can perform the atomic compare and swap. Simplify the
5205 following code by always setting up the "i"ntegral variables. */
5206 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5208 oldival = oldval;
5209 newival = newval;
5210 iaddr = addr;
5212 else
5214 oldival = create_tmp_var (itype, NULL);
5215 newival = create_tmp_var (itype, NULL);
5217 x = build1 (VIEW_CONVERT_EXPR, itype, oldval);
5218 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, oldival, x);
5219 gimplify_and_add (x, pre_p);
5220 iaddr = fold_convert (build_pointer_type (itype), addr);
5223 oldival2 = create_tmp_var (itype, NULL);
5225 label = create_artificial_label ();
5226 x = build1 (LABEL_EXPR, void_type_node, label);
5227 gimplify_and_add (x, pre_p);
5229 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, newval, rhs);
5230 gimplify_and_add (x, pre_p);
5232 if (newval != newival)
5234 x = build1 (VIEW_CONVERT_EXPR, itype, newval);
5235 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, newival, x);
5236 gimplify_and_add (x, pre_p);
5239 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, oldival2,
5240 fold_convert (itype, oldival));
5241 gimplify_and_add (x, pre_p);
5243 args = tree_cons (NULL, fold_convert (itype, newival), NULL);
5244 args = tree_cons (NULL, fold_convert (itype, oldival), args);
5245 args = tree_cons (NULL, iaddr, args);
5246 x = build_function_call_expr (cmpxchg, args);
5247 if (oldval == oldival)
5248 x = fold_convert (type, x);
5249 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, oldival, x);
5250 gimplify_and_add (x, pre_p);
5252 /* For floating point, be prepared for the loop backedge. */
5253 if (oldval != oldival)
5255 x = build1 (VIEW_CONVERT_EXPR, type, oldival);
5256 x = build2 (GIMPLE_MODIFY_STMT, void_type_node, oldval, x);
5257 gimplify_and_add (x, pre_p);
5260 /* Note that we always perform the comparison as an integer, even for
5261 floating point. This allows the atomic operation to properly
5262 succeed even with NaNs and -0.0. */
5263 x = build3 (COND_EXPR, void_type_node,
5264 build2 (NE_EXPR, boolean_type_node, oldival, oldival2),
5265 build1 (GOTO_EXPR, void_type_node, label), NULL);
5266 gimplify_and_add (x, pre_p);
5268 *expr_p = NULL;
5269 return GS_ALL_DONE;
5272 /* A subroutine of gimplify_omp_atomic. Implement the atomic operation as:
5274 GOMP_atomic_start ();
5275 *addr = rhs;
5276 GOMP_atomic_end ();
5278 The result is not globally atomic, but works so long as all parallel
5279 references are within #pragma omp atomic directives. According to
5280 responses received from omp@openmp.org, appears to be within spec.
5281 Which makes sense, since that's how several other compilers handle
5282 this situation as well. */
5284 static enum gimplify_status
5285 gimplify_omp_atomic_mutex (tree *expr_p, tree *pre_p, tree addr, tree rhs)
5287 tree t;
5289 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_START];
5290 t = build_function_call_expr (t, NULL);
5291 gimplify_and_add (t, pre_p);
5293 t = build_fold_indirect_ref (addr);
5294 t = build2 (GIMPLE_MODIFY_STMT, void_type_node, t, rhs);
5295 gimplify_and_add (t, pre_p);
5297 t = built_in_decls[BUILT_IN_GOMP_ATOMIC_END];
5298 t = build_function_call_expr (t, NULL);
5299 gimplify_and_add (t, pre_p);
5301 *expr_p = NULL;
5302 return GS_ALL_DONE;
5305 /* Gimplify an OMP_ATOMIC statement. */
5307 static enum gimplify_status
5308 gimplify_omp_atomic (tree *expr_p, tree *pre_p)
5310 tree addr = TREE_OPERAND (*expr_p, 0);
5311 tree rhs = TREE_OPERAND (*expr_p, 1);
5312 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
5313 HOST_WIDE_INT index;
5315 /* Make sure the type is one of the supported sizes. */
5316 index = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5317 index = exact_log2 (index);
5318 if (index >= 0 && index <= 4)
5320 enum gimplify_status gs;
5321 unsigned int align;
5323 if (DECL_P (TREE_OPERAND (addr, 0)))
5324 align = DECL_ALIGN_UNIT (TREE_OPERAND (addr, 0));
5325 else if (TREE_CODE (TREE_OPERAND (addr, 0)) == COMPONENT_REF
5326 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (addr, 0), 1))
5327 == FIELD_DECL)
5328 align = DECL_ALIGN_UNIT (TREE_OPERAND (TREE_OPERAND (addr, 0), 1));
5329 else
5330 align = TYPE_ALIGN_UNIT (type);
5332 /* __sync builtins require strict data alignment. */
5333 if (exact_log2 (align) >= index)
5335 /* When possible, use specialized atomic update functions. */
5336 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5338 gs = gimplify_omp_atomic_fetch_op (expr_p, addr, rhs, index);
5339 if (gs != GS_UNHANDLED)
5340 return gs;
5343 /* If we don't have specialized __sync builtins, try and implement
5344 as a compare and swap loop. */
5345 gs = gimplify_omp_atomic_pipeline (expr_p, pre_p, addr, rhs, index);
5346 if (gs != GS_UNHANDLED)
5347 return gs;
5351 /* The ultimate fallback is wrapping the operation in a mutex. */
5352 return gimplify_omp_atomic_mutex (expr_p, pre_p, addr, rhs);
5355 /* Gimplifies the expression tree pointed to by EXPR_P. Return 0 if
5356 gimplification failed.
5358 PRE_P points to the list where side effects that must happen before
5359 EXPR should be stored.
5361 POST_P points to the list where side effects that must happen after
5362 EXPR should be stored, or NULL if there is no suitable list. In
5363 that case, we copy the result to a temporary, emit the
5364 post-effects, and then return the temporary.
5366 GIMPLE_TEST_F points to a function that takes a tree T and
5367 returns nonzero if T is in the GIMPLE form requested by the
5368 caller. The GIMPLE predicates are in tree-gimple.c.
5370 This test is used twice. Before gimplification, the test is
5371 invoked to determine whether *EXPR_P is already gimple enough. If
5372 that fails, *EXPR_P is gimplified according to its code and
5373 GIMPLE_TEST_F is called again. If the test still fails, then a new
5374 temporary variable is created and assigned the value of the
5375 gimplified expression.
5377 FALLBACK tells the function what sort of a temporary we want. If the 1
5378 bit is set, an rvalue is OK. If the 2 bit is set, an lvalue is OK.
5379 If both are set, either is OK, but an lvalue is preferable.
5381 The return value is either GS_ERROR or GS_ALL_DONE, since this function
5382 iterates until solution. */
5384 enum gimplify_status
5385 gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p,
5386 bool (* gimple_test_f) (tree), fallback_t fallback)
5388 tree tmp;
5389 tree internal_pre = NULL_TREE;
5390 tree internal_post = NULL_TREE;
5391 tree save_expr;
5392 int is_statement = (pre_p == NULL);
5393 location_t saved_location;
5394 enum gimplify_status ret;
5396 save_expr = *expr_p;
5397 if (save_expr == NULL_TREE)
5398 return GS_ALL_DONE;
5400 /* We used to check the predicate here and return immediately if it
5401 succeeds. This is wrong; the design is for gimplification to be
5402 idempotent, and for the predicates to only test for valid forms, not
5403 whether they are fully simplified. */
5405 /* Set up our internal queues if needed. */
5406 if (pre_p == NULL)
5407 pre_p = &internal_pre;
5408 if (post_p == NULL)
5409 post_p = &internal_post;
5411 saved_location = input_location;
5412 if (save_expr != error_mark_node
5413 && EXPR_HAS_LOCATION (*expr_p))
5414 input_location = EXPR_LOCATION (*expr_p);
5416 /* Loop over the specific gimplifiers until the toplevel node
5417 remains the same. */
5420 /* Strip away as many useless type conversions as possible
5421 at the toplevel. */
5422 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
5424 /* Remember the expr. */
5425 save_expr = *expr_p;
5427 /* Die, die, die, my darling. */
5428 if (save_expr == error_mark_node
5429 || (!GIMPLE_STMT_P (save_expr)
5430 && TREE_TYPE (save_expr)
5431 && TREE_TYPE (save_expr) == error_mark_node))
5433 ret = GS_ERROR;
5434 break;
5437 /* Do any language-specific gimplification. */
5438 ret = lang_hooks.gimplify_expr (expr_p, pre_p, post_p);
5439 if (ret == GS_OK)
5441 if (*expr_p == NULL_TREE)
5442 break;
5443 if (*expr_p != save_expr)
5444 continue;
5446 else if (ret != GS_UNHANDLED)
5447 break;
5449 ret = GS_OK;
5450 switch (TREE_CODE (*expr_p))
5452 /* First deal with the special cases. */
5454 case POSTINCREMENT_EXPR:
5455 case POSTDECREMENT_EXPR:
5456 case PREINCREMENT_EXPR:
5457 case PREDECREMENT_EXPR:
5458 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
5459 fallback != fb_none);
5460 break;
5462 case ARRAY_REF:
5463 case ARRAY_RANGE_REF:
5464 case REALPART_EXPR:
5465 case IMAGPART_EXPR:
5466 case COMPONENT_REF:
5467 case VIEW_CONVERT_EXPR:
5468 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
5469 fallback ? fallback : fb_rvalue);
5470 break;
5472 case COND_EXPR:
5473 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
5474 /* C99 code may assign to an array in a structure value of a
5475 conditional expression, and this has undefined behavior
5476 only on execution, so create a temporary if an lvalue is
5477 required. */
5478 if (fallback == fb_lvalue)
5480 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5481 lang_hooks.mark_addressable (*expr_p);
5483 break;
5485 case CALL_EXPR:
5486 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
5487 /* C99 code may assign to an array in a structure returned
5488 from a function, and this has undefined behavior only on
5489 execution, so create a temporary if an lvalue is
5490 required. */
5491 if (fallback == fb_lvalue)
5493 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5494 lang_hooks.mark_addressable (*expr_p);
5496 break;
5498 case TREE_LIST:
5499 gcc_unreachable ();
5501 case COMPOUND_EXPR:
5502 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
5503 break;
5505 case MODIFY_EXPR:
5506 case GIMPLE_MODIFY_STMT:
5507 case INIT_EXPR:
5508 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
5509 fallback != fb_none);
5511 if (*expr_p)
5513 /* The distinction between MODIFY_EXPR and INIT_EXPR is no longer
5514 useful. */
5515 if (TREE_CODE (*expr_p) == INIT_EXPR)
5516 TREE_SET_CODE (*expr_p, MODIFY_EXPR);
5518 /* Convert MODIFY_EXPR to GIMPLE_MODIFY_STMT. */
5519 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
5520 tree_to_gimple_tuple (expr_p);
5523 break;
5525 case TRUTH_ANDIF_EXPR:
5526 case TRUTH_ORIF_EXPR:
5527 ret = gimplify_boolean_expr (expr_p);
5528 break;
5530 case TRUTH_NOT_EXPR:
5531 TREE_OPERAND (*expr_p, 0)
5532 = gimple_boolify (TREE_OPERAND (*expr_p, 0));
5533 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5534 is_gimple_val, fb_rvalue);
5535 recalculate_side_effects (*expr_p);
5536 break;
5538 case ADDR_EXPR:
5539 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
5540 break;
5542 case VA_ARG_EXPR:
5543 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
5544 break;
5546 case CONVERT_EXPR:
5547 case NOP_EXPR:
5548 if (IS_EMPTY_STMT (*expr_p))
5550 ret = GS_ALL_DONE;
5551 break;
5554 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
5555 || fallback == fb_none)
5557 /* Just strip a conversion to void (or in void context) and
5558 try again. */
5559 *expr_p = TREE_OPERAND (*expr_p, 0);
5560 break;
5563 ret = gimplify_conversion (expr_p);
5564 if (ret == GS_ERROR)
5565 break;
5566 if (*expr_p != save_expr)
5567 break;
5568 /* FALLTHRU */
5570 case FIX_TRUNC_EXPR:
5571 /* unary_expr: ... | '(' cast ')' val | ... */
5572 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5573 is_gimple_val, fb_rvalue);
5574 recalculate_side_effects (*expr_p);
5575 break;
5577 case INDIRECT_REF:
5578 *expr_p = fold_indirect_ref (*expr_p);
5579 if (*expr_p != save_expr)
5580 break;
5581 /* else fall through. */
5582 case ALIGN_INDIRECT_REF:
5583 case MISALIGNED_INDIRECT_REF:
5584 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5585 is_gimple_reg, fb_rvalue);
5586 recalculate_side_effects (*expr_p);
5587 break;
5589 /* Constants need not be gimplified. */
5590 case INTEGER_CST:
5591 case REAL_CST:
5592 case STRING_CST:
5593 case COMPLEX_CST:
5594 case VECTOR_CST:
5595 ret = GS_ALL_DONE;
5596 break;
5598 case CONST_DECL:
5599 /* If we require an lvalue, such as for ADDR_EXPR, retain the
5600 CONST_DECL node. Otherwise the decl is replaceable by its
5601 value. */
5602 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
5603 if (fallback & fb_lvalue)
5604 ret = GS_ALL_DONE;
5605 else
5606 *expr_p = DECL_INITIAL (*expr_p);
5607 break;
5609 case DECL_EXPR:
5610 ret = gimplify_decl_expr (expr_p);
5611 break;
5613 case EXC_PTR_EXPR:
5614 /* FIXME make this a decl. */
5615 ret = GS_ALL_DONE;
5616 break;
5618 case BIND_EXPR:
5619 ret = gimplify_bind_expr (expr_p, pre_p);
5620 break;
5622 case LOOP_EXPR:
5623 ret = gimplify_loop_expr (expr_p, pre_p);
5624 break;
5626 case SWITCH_EXPR:
5627 ret = gimplify_switch_expr (expr_p, pre_p);
5628 break;
5630 case EXIT_EXPR:
5631 ret = gimplify_exit_expr (expr_p);
5632 break;
5634 case GOTO_EXPR:
5635 /* If the target is not LABEL, then it is a computed jump
5636 and the target needs to be gimplified. */
5637 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
5638 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
5639 NULL, is_gimple_val, fb_rvalue);
5640 break;
5642 case LABEL_EXPR:
5643 ret = GS_ALL_DONE;
5644 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
5645 == current_function_decl);
5646 break;
5648 case CASE_LABEL_EXPR:
5649 ret = gimplify_case_label_expr (expr_p);
5650 break;
5652 case RETURN_EXPR:
5653 ret = gimplify_return_expr (*expr_p, pre_p);
5654 break;
5656 case CONSTRUCTOR:
5657 /* Don't reduce this in place; let gimplify_init_constructor work its
5658 magic. Buf if we're just elaborating this for side effects, just
5659 gimplify any element that has side-effects. */
5660 if (fallback == fb_none)
5662 unsigned HOST_WIDE_INT ix;
5663 constructor_elt *ce;
5664 tree temp = NULL_TREE;
5665 for (ix = 0;
5666 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (*expr_p),
5667 ix, ce);
5668 ix++)
5669 if (TREE_SIDE_EFFECTS (ce->value))
5670 append_to_statement_list (ce->value, &temp);
5672 *expr_p = temp;
5673 ret = GS_OK;
5675 /* C99 code may assign to an array in a constructed
5676 structure or union, and this has undefined behavior only
5677 on execution, so create a temporary if an lvalue is
5678 required. */
5679 else if (fallback == fb_lvalue)
5681 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
5682 lang_hooks.mark_addressable (*expr_p);
5684 else
5685 ret = GS_ALL_DONE;
5686 break;
5688 /* The following are special cases that are not handled by the
5689 original GIMPLE grammar. */
5691 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
5692 eliminated. */
5693 case SAVE_EXPR:
5694 ret = gimplify_save_expr (expr_p, pre_p, post_p);
5695 break;
5697 case BIT_FIELD_REF:
5699 enum gimplify_status r0, r1, r2;
5701 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5702 is_gimple_lvalue, fb_either);
5703 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5704 is_gimple_val, fb_rvalue);
5705 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p, post_p,
5706 is_gimple_val, fb_rvalue);
5707 recalculate_side_effects (*expr_p);
5709 ret = MIN (r0, MIN (r1, r2));
5711 break;
5713 case NON_LVALUE_EXPR:
5714 /* This should have been stripped above. */
5715 gcc_unreachable ();
5717 case ASM_EXPR:
5718 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
5719 break;
5721 case TRY_FINALLY_EXPR:
5722 case TRY_CATCH_EXPR:
5723 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 0));
5724 gimplify_to_stmt_list (&TREE_OPERAND (*expr_p, 1));
5725 ret = GS_ALL_DONE;
5726 break;
5728 case CLEANUP_POINT_EXPR:
5729 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
5730 break;
5732 case TARGET_EXPR:
5733 ret = gimplify_target_expr (expr_p, pre_p, post_p);
5734 break;
5736 case CATCH_EXPR:
5737 gimplify_to_stmt_list (&CATCH_BODY (*expr_p));
5738 ret = GS_ALL_DONE;
5739 break;
5741 case EH_FILTER_EXPR:
5742 gimplify_to_stmt_list (&EH_FILTER_FAILURE (*expr_p));
5743 ret = GS_ALL_DONE;
5744 break;
5746 case OBJ_TYPE_REF:
5748 enum gimplify_status r0, r1;
5749 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p, post_p,
5750 is_gimple_val, fb_rvalue);
5751 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p, post_p,
5752 is_gimple_val, fb_rvalue);
5753 ret = MIN (r0, r1);
5755 break;
5757 case LABEL_DECL:
5758 /* We get here when taking the address of a label. We mark
5759 the label as "forced"; meaning it can never be removed and
5760 it is a potential target for any computed goto. */
5761 FORCED_LABEL (*expr_p) = 1;
5762 ret = GS_ALL_DONE;
5763 break;
5765 case STATEMENT_LIST:
5766 ret = gimplify_statement_list (expr_p, pre_p);
5767 break;
5769 case WITH_SIZE_EXPR:
5771 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5772 post_p == &internal_post ? NULL : post_p,
5773 gimple_test_f, fallback);
5774 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5775 is_gimple_val, fb_rvalue);
5777 break;
5779 case VAR_DECL:
5780 case PARM_DECL:
5781 ret = gimplify_var_or_parm_decl (expr_p);
5782 break;
5784 case RESULT_DECL:
5785 /* When within an OpenMP context, notice uses of variables. */
5786 if (gimplify_omp_ctxp)
5787 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
5788 ret = GS_ALL_DONE;
5789 break;
5791 case SSA_NAME:
5792 /* Allow callbacks into the gimplifier during optimization. */
5793 ret = GS_ALL_DONE;
5794 break;
5796 case OMP_PARALLEL:
5797 ret = gimplify_omp_parallel (expr_p, pre_p);
5798 break;
5800 case OMP_FOR:
5801 ret = gimplify_omp_for (expr_p, pre_p);
5802 break;
5804 case OMP_SECTIONS:
5805 case OMP_SINGLE:
5806 ret = gimplify_omp_workshare (expr_p, pre_p);
5807 break;
5809 case OMP_SECTION:
5810 case OMP_MASTER:
5811 case OMP_ORDERED:
5812 case OMP_CRITICAL:
5813 gimplify_to_stmt_list (&OMP_BODY (*expr_p));
5814 break;
5816 case OMP_ATOMIC:
5817 ret = gimplify_omp_atomic (expr_p, pre_p);
5818 break;
5820 case OMP_RETURN:
5821 case OMP_CONTINUE:
5822 ret = GS_ALL_DONE;
5823 break;
5825 default:
5826 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
5828 case tcc_comparison:
5829 /* Handle comparison of objects of non scalar mode aggregates
5830 with a call to memcmp. It would be nice to only have to do
5831 this for variable-sized objects, but then we'd have to allow
5832 the same nest of reference nodes we allow for MODIFY_EXPR and
5833 that's too complex.
5835 Compare scalar mode aggregates as scalar mode values. Using
5836 memcmp for them would be very inefficient at best, and is
5837 plain wrong if bitfields are involved. */
5840 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
5842 if (!AGGREGATE_TYPE_P (type))
5843 goto expr_2;
5844 else if (TYPE_MODE (type) != BLKmode)
5845 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
5846 else
5847 ret = gimplify_variable_sized_compare (expr_p);
5849 break;
5852 /* If *EXPR_P does not need to be special-cased, handle it
5853 according to its class. */
5854 case tcc_unary:
5855 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5856 post_p, is_gimple_val, fb_rvalue);
5857 break;
5859 case tcc_binary:
5860 expr_2:
5862 enum gimplify_status r0, r1;
5864 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
5865 post_p, is_gimple_val, fb_rvalue);
5866 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
5867 post_p, is_gimple_val, fb_rvalue);
5869 ret = MIN (r0, r1);
5870 break;
5873 case tcc_declaration:
5874 case tcc_constant:
5875 ret = GS_ALL_DONE;
5876 goto dont_recalculate;
5878 default:
5879 gcc_assert (TREE_CODE (*expr_p) == TRUTH_AND_EXPR
5880 || TREE_CODE (*expr_p) == TRUTH_OR_EXPR
5881 || TREE_CODE (*expr_p) == TRUTH_XOR_EXPR);
5882 goto expr_2;
5885 recalculate_side_effects (*expr_p);
5886 dont_recalculate:
5887 break;
5890 /* If we replaced *expr_p, gimplify again. */
5891 if (ret == GS_OK && (*expr_p == NULL || *expr_p == save_expr))
5892 ret = GS_ALL_DONE;
5894 while (ret == GS_OK);
5896 /* If we encountered an error_mark somewhere nested inside, either
5897 stub out the statement or propagate the error back out. */
5898 if (ret == GS_ERROR)
5900 if (is_statement)
5901 *expr_p = NULL;
5902 goto out;
5905 /* This was only valid as a return value from the langhook, which
5906 we handled. Make sure it doesn't escape from any other context. */
5907 gcc_assert (ret != GS_UNHANDLED);
5909 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
5911 /* We aren't looking for a value, and we don't have a valid
5912 statement. If it doesn't have side-effects, throw it away. */
5913 if (!TREE_SIDE_EFFECTS (*expr_p))
5914 *expr_p = NULL;
5915 else if (!TREE_THIS_VOLATILE (*expr_p))
5917 /* This is probably a _REF that contains something nested that
5918 has side effects. Recurse through the operands to find it. */
5919 enum tree_code code = TREE_CODE (*expr_p);
5921 switch (code)
5923 case COMPONENT_REF:
5924 case REALPART_EXPR:
5925 case IMAGPART_EXPR:
5926 case VIEW_CONVERT_EXPR:
5927 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5928 gimple_test_f, fallback);
5929 break;
5931 case ARRAY_REF:
5932 case ARRAY_RANGE_REF:
5933 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5934 gimple_test_f, fallback);
5935 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
5936 gimple_test_f, fallback);
5937 break;
5939 default:
5940 /* Anything else with side-effects must be converted to
5941 a valid statement before we get here. */
5942 gcc_unreachable ();
5945 *expr_p = NULL;
5947 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
5948 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
5950 /* Historically, the compiler has treated a bare reference
5951 to a non-BLKmode volatile lvalue as forcing a load. */
5952 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
5953 /* Normally, we do not want to create a temporary for a
5954 TREE_ADDRESSABLE type because such a type should not be
5955 copied by bitwise-assignment. However, we make an
5956 exception here, as all we are doing here is ensuring that
5957 we read the bytes that make up the type. We use
5958 create_tmp_var_raw because create_tmp_var will abort when
5959 given a TREE_ADDRESSABLE type. */
5960 tree tmp = create_tmp_var_raw (type, "vol");
5961 gimple_add_tmp_var (tmp);
5962 *expr_p = build2 (GIMPLE_MODIFY_STMT, type, tmp, *expr_p);
5964 else
5965 /* We can't do anything useful with a volatile reference to
5966 an incomplete type, so just throw it away. Likewise for
5967 a BLKmode type, since any implicit inner load should
5968 already have been turned into an explicit one by the
5969 gimplification process. */
5970 *expr_p = NULL;
5973 /* If we are gimplifying at the statement level, we're done. Tack
5974 everything together and replace the original statement with the
5975 gimplified form. */
5976 if (fallback == fb_none || is_statement)
5978 if (internal_pre || internal_post)
5980 append_to_statement_list (*expr_p, &internal_pre);
5981 append_to_statement_list (internal_post, &internal_pre);
5982 annotate_all_with_locus (&internal_pre, input_location);
5983 *expr_p = internal_pre;
5985 else if (!*expr_p)
5987 else if (TREE_CODE (*expr_p) == STATEMENT_LIST)
5988 annotate_all_with_locus (expr_p, input_location);
5989 else
5990 annotate_one_with_locus (*expr_p, input_location);
5991 goto out;
5994 /* Otherwise we're gimplifying a subexpression, so the resulting value is
5995 interesting. */
5997 /* If it's sufficiently simple already, we're done. Unless we are
5998 handling some post-effects internally; if that's the case, we need to
5999 copy into a temp before adding the post-effects to the tree. */
6000 if (!internal_post && (*gimple_test_f) (*expr_p))
6001 goto out;
6003 /* Otherwise, we need to create a new temporary for the gimplified
6004 expression. */
6006 /* We can't return an lvalue if we have an internal postqueue. The
6007 object the lvalue refers to would (probably) be modified by the
6008 postqueue; we need to copy the value out first, which means an
6009 rvalue. */
6010 if ((fallback & fb_lvalue) && !internal_post
6011 && is_gimple_addressable (*expr_p))
6013 /* An lvalue will do. Take the address of the expression, store it
6014 in a temporary, and replace the expression with an INDIRECT_REF of
6015 that temporary. */
6016 tmp = build_fold_addr_expr (*expr_p);
6017 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
6018 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (tmp)), tmp);
6020 else if ((fallback & fb_rvalue) && is_gimple_formal_tmp_rhs (*expr_p))
6022 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
6024 /* An rvalue will do. Assign the gimplified expression into a new
6025 temporary TMP and replace the original expression with TMP. */
6027 if (internal_post || (fallback & fb_lvalue))
6028 /* The postqueue might change the value of the expression between
6029 the initialization and use of the temporary, so we can't use a
6030 formal temp. FIXME do we care? */
6031 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6032 else
6033 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
6035 if (TREE_CODE (*expr_p) != SSA_NAME)
6036 DECL_GIMPLE_FORMAL_TEMP_P (*expr_p) = 1;
6038 else
6040 #ifdef ENABLE_CHECKING
6041 if (!(fallback & fb_mayfail))
6043 fprintf (stderr, "gimplification failed:\n");
6044 print_generic_expr (stderr, *expr_p, 0);
6045 debug_tree (*expr_p);
6046 internal_error ("gimplification failed");
6048 #endif
6049 gcc_assert (fallback & fb_mayfail);
6050 /* If this is an asm statement, and the user asked for the
6051 impossible, don't die. Fail and let gimplify_asm_expr
6052 issue an error. */
6053 ret = GS_ERROR;
6054 goto out;
6057 /* Make sure the temporary matches our predicate. */
6058 gcc_assert ((*gimple_test_f) (*expr_p));
6060 if (internal_post)
6062 annotate_all_with_locus (&internal_post, input_location);
6063 append_to_statement_list (internal_post, pre_p);
6066 out:
6067 input_location = saved_location;
6068 return ret;
6071 /* Look through TYPE for variable-sized objects and gimplify each such
6072 size that we find. Add to LIST_P any statements generated. */
6074 void
6075 gimplify_type_sizes (tree type, tree *list_p)
6077 tree field, t;
6079 if (type == NULL || type == error_mark_node)
6080 return;
6082 /* We first do the main variant, then copy into any other variants. */
6083 type = TYPE_MAIN_VARIANT (type);
6085 /* Avoid infinite recursion. */
6086 if (TYPE_SIZES_GIMPLIFIED (type))
6087 return;
6089 TYPE_SIZES_GIMPLIFIED (type) = 1;
6091 switch (TREE_CODE (type))
6093 case INTEGER_TYPE:
6094 case ENUMERAL_TYPE:
6095 case BOOLEAN_TYPE:
6096 case REAL_TYPE:
6097 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
6098 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
6100 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6102 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
6103 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
6105 break;
6107 case ARRAY_TYPE:
6108 /* These types may not have declarations, so handle them here. */
6109 gimplify_type_sizes (TREE_TYPE (type), list_p);
6110 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
6111 break;
6113 case RECORD_TYPE:
6114 case UNION_TYPE:
6115 case QUAL_UNION_TYPE:
6116 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6117 if (TREE_CODE (field) == FIELD_DECL)
6119 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
6120 gimplify_type_sizes (TREE_TYPE (field), list_p);
6122 break;
6124 case POINTER_TYPE:
6125 case REFERENCE_TYPE:
6126 /* We used to recurse on the pointed-to type here, which turned out to
6127 be incorrect because its definition might refer to variables not
6128 yet initialized at this point if a forward declaration is involved.
6130 It was actually useful for anonymous pointed-to types to ensure
6131 that the sizes evaluation dominates every possible later use of the
6132 values. Restricting to such types here would be safe since there
6133 is no possible forward declaration around, but would introduce an
6134 undesirable middle-end semantic to anonymity. We then defer to
6135 front-ends the responsibility of ensuring that the sizes are
6136 evaluated both early and late enough, e.g. by attaching artificial
6137 type declarations to the tree. */
6138 break;
6140 default:
6141 break;
6144 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
6145 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
6147 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6149 TYPE_SIZE (t) = TYPE_SIZE (type);
6150 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
6151 TYPE_SIZES_GIMPLIFIED (t) = 1;
6155 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
6156 a size or position, has had all of its SAVE_EXPRs evaluated.
6157 We add any required statements to STMT_P. */
6159 void
6160 gimplify_one_sizepos (tree *expr_p, tree *stmt_p)
6162 tree type, expr = *expr_p;
6164 /* We don't do anything if the value isn't there, is constant, or contains
6165 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
6166 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
6167 will want to replace it with a new variable, but that will cause problems
6168 if this type is from outside the function. It's OK to have that here. */
6169 if (expr == NULL_TREE || TREE_CONSTANT (expr)
6170 || TREE_CODE (expr) == VAR_DECL
6171 || CONTAINS_PLACEHOLDER_P (expr))
6172 return;
6174 type = TREE_TYPE (expr);
6175 *expr_p = unshare_expr (expr);
6177 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
6178 expr = *expr_p;
6180 /* Verify that we've an exact type match with the original expression.
6181 In particular, we do not wish to drop a "sizetype" in favour of a
6182 type of similar dimensions. We don't want to pollute the generic
6183 type-stripping code with this knowledge because it doesn't matter
6184 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
6185 and friends retain their "sizetype-ness". */
6186 if (TREE_TYPE (expr) != type
6187 && TREE_CODE (type) == INTEGER_TYPE
6188 && TYPE_IS_SIZETYPE (type))
6190 tree tmp;
6192 *expr_p = create_tmp_var (type, NULL);
6193 tmp = build1 (NOP_EXPR, type, expr);
6194 tmp = build2 (GIMPLE_MODIFY_STMT, type, *expr_p, tmp);
6195 if (EXPR_HAS_LOCATION (expr))
6196 SET_EXPR_LOCUS (tmp, EXPR_LOCUS (expr));
6197 else
6198 SET_EXPR_LOCATION (tmp, input_location);
6200 gimplify_and_add (tmp, stmt_p);
6204 #ifdef ENABLE_CHECKING
6205 /* Compare types A and B for a "close enough" match. */
6207 static bool
6208 cpt_same_type (tree a, tree b)
6210 if (lang_hooks.types_compatible_p (a, b))
6211 return true;
6213 /* ??? The C++ FE decomposes METHOD_TYPES to FUNCTION_TYPES and doesn't
6214 link them together. This routine is intended to catch type errors
6215 that will affect the optimizers, and the optimizers don't add new
6216 dereferences of function pointers, so ignore it. */
6217 if ((TREE_CODE (a) == FUNCTION_TYPE || TREE_CODE (a) == METHOD_TYPE)
6218 && (TREE_CODE (b) == FUNCTION_TYPE || TREE_CODE (b) == METHOD_TYPE))
6219 return true;
6221 /* ??? The C FE pushes type qualifiers after the fact into the type of
6222 the element from the type of the array. See build_unary_op's handling
6223 of ADDR_EXPR. This seems wrong -- if we were going to do this, we
6224 should have done it when creating the variable in the first place.
6225 Alternately, why aren't the two array types made variants? */
6226 if (TREE_CODE (a) == ARRAY_TYPE && TREE_CODE (b) == ARRAY_TYPE)
6227 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6229 /* And because of those, we have to recurse down through pointers. */
6230 if (POINTER_TYPE_P (a) && POINTER_TYPE_P (b))
6231 return cpt_same_type (TREE_TYPE (a), TREE_TYPE (b));
6233 return false;
6236 /* Check for some cases of the front end missing cast expressions.
6237 The type of a dereference should correspond to the pointer type;
6238 similarly the type of an address should match its object. */
6240 static tree
6241 check_pointer_types_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
6242 void *data ATTRIBUTE_UNUSED)
6244 tree t = *tp;
6245 tree ptype, otype, dtype;
6247 switch (TREE_CODE (t))
6249 case INDIRECT_REF:
6250 case ARRAY_REF:
6251 otype = TREE_TYPE (t);
6252 ptype = TREE_TYPE (TREE_OPERAND (t, 0));
6253 dtype = TREE_TYPE (ptype);
6254 gcc_assert (cpt_same_type (otype, dtype));
6255 break;
6257 case ADDR_EXPR:
6258 ptype = TREE_TYPE (t);
6259 otype = TREE_TYPE (TREE_OPERAND (t, 0));
6260 dtype = TREE_TYPE (ptype);
6261 if (!cpt_same_type (otype, dtype))
6263 /* &array is allowed to produce a pointer to the element, rather than
6264 a pointer to the array type. We must allow this in order to
6265 properly represent assigning the address of an array in C into
6266 pointer to the element type. */
6267 gcc_assert (TREE_CODE (otype) == ARRAY_TYPE
6268 && POINTER_TYPE_P (ptype)
6269 && cpt_same_type (TREE_TYPE (otype), dtype));
6270 break;
6272 break;
6274 default:
6275 return NULL_TREE;
6279 return NULL_TREE;
6281 #endif
6283 /* Gimplify the body of statements pointed to by BODY_P. FNDECL is the
6284 function decl containing BODY. */
6286 void
6287 gimplify_body (tree *body_p, tree fndecl, bool do_parms)
6289 location_t saved_location = input_location;
6290 tree body, parm_stmts;
6292 timevar_push (TV_TREE_GIMPLIFY);
6294 gcc_assert (gimplify_ctxp == NULL);
6295 push_gimplify_context ();
6297 /* Unshare most shared trees in the body and in that of any nested functions.
6298 It would seem we don't have to do this for nested functions because
6299 they are supposed to be output and then the outer function gimplified
6300 first, but the g++ front end doesn't always do it that way. */
6301 unshare_body (body_p, fndecl);
6302 unvisit_body (body_p, fndecl);
6304 /* Make sure input_location isn't set to something wierd. */
6305 input_location = DECL_SOURCE_LOCATION (fndecl);
6307 /* Resolve callee-copies. This has to be done before processing
6308 the body so that DECL_VALUE_EXPR gets processed correctly. */
6309 parm_stmts = do_parms ? gimplify_parameters () : NULL;
6311 /* Gimplify the function's body. */
6312 gimplify_stmt (body_p);
6313 body = *body_p;
6315 if (!body)
6316 body = alloc_stmt_list ();
6317 else if (TREE_CODE (body) == STATEMENT_LIST)
6319 tree t = expr_only (*body_p);
6320 if (t)
6321 body = t;
6324 /* If there isn't an outer BIND_EXPR, add one. */
6325 if (TREE_CODE (body) != BIND_EXPR)
6327 tree b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6328 NULL_TREE, NULL_TREE);
6329 TREE_SIDE_EFFECTS (b) = 1;
6330 append_to_statement_list_force (body, &BIND_EXPR_BODY (b));
6331 body = b;
6334 /* If we had callee-copies statements, insert them at the beginning
6335 of the function. */
6336 if (parm_stmts)
6338 append_to_statement_list_force (BIND_EXPR_BODY (body), &parm_stmts);
6339 BIND_EXPR_BODY (body) = parm_stmts;
6342 /* Unshare again, in case gimplification was sloppy. */
6343 unshare_all_trees (body);
6345 *body_p = body;
6347 pop_gimplify_context (body);
6348 gcc_assert (gimplify_ctxp == NULL);
6350 #ifdef ENABLE_CHECKING
6351 walk_tree (body_p, check_pointer_types_r, NULL, NULL);
6352 #endif
6354 timevar_pop (TV_TREE_GIMPLIFY);
6355 input_location = saved_location;
6358 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
6359 node for the function we want to gimplify. */
6361 void
6362 gimplify_function_tree (tree fndecl)
6364 tree oldfn, parm, ret;
6366 oldfn = current_function_decl;
6367 current_function_decl = fndecl;
6368 cfun = DECL_STRUCT_FUNCTION (fndecl);
6369 if (cfun == NULL)
6370 allocate_struct_function (fndecl);
6372 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = TREE_CHAIN (parm))
6374 /* Preliminarily mark non-addressed complex variables as eligible
6375 for promotion to gimple registers. We'll transform their uses
6376 as we find them. */
6377 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
6378 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
6379 && !TREE_THIS_VOLATILE (parm)
6380 && !needs_to_live_in_memory (parm))
6381 DECL_GIMPLE_REG_P (parm) = 1;
6384 ret = DECL_RESULT (fndecl);
6385 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
6386 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
6387 && !needs_to_live_in_memory (ret))
6388 DECL_GIMPLE_REG_P (ret) = 1;
6390 gimplify_body (&DECL_SAVED_TREE (fndecl), fndecl, true);
6392 /* If we're instrumenting function entry/exit, then prepend the call to
6393 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
6394 catch the exit hook. */
6395 /* ??? Add some way to ignore exceptions for this TFE. */
6396 if (flag_instrument_function_entry_exit
6397 && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl))
6399 tree tf, x, bind;
6401 tf = build2 (TRY_FINALLY_EXPR, void_type_node, NULL, NULL);
6402 TREE_SIDE_EFFECTS (tf) = 1;
6403 x = DECL_SAVED_TREE (fndecl);
6404 append_to_statement_list (x, &TREE_OPERAND (tf, 0));
6405 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_EXIT];
6406 x = build_function_call_expr (x, NULL);
6407 append_to_statement_list (x, &TREE_OPERAND (tf, 1));
6409 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6410 TREE_SIDE_EFFECTS (bind) = 1;
6411 x = implicit_built_in_decls[BUILT_IN_PROFILE_FUNC_ENTER];
6412 x = build_function_call_expr (x, NULL);
6413 append_to_statement_list (x, &BIND_EXPR_BODY (bind));
6414 append_to_statement_list (tf, &BIND_EXPR_BODY (bind));
6416 DECL_SAVED_TREE (fndecl) = bind;
6419 cfun->gimplified = true;
6420 current_function_decl = oldfn;
6421 cfun = oldfn ? DECL_STRUCT_FUNCTION (oldfn) : NULL;
6424 /* Expands EXPR to list of gimple statements STMTS. If SIMPLE is true,
6425 force the result to be either ssa_name or an invariant, otherwise
6426 just force it to be a rhs expression. If VAR is not NULL, make the
6427 base variable of the final destination be VAR if suitable. */
6429 tree
6430 force_gimple_operand (tree expr, tree *stmts, bool simple, tree var)
6432 tree t;
6433 enum gimplify_status ret;
6434 gimple_predicate gimple_test_f;
6436 *stmts = NULL_TREE;
6438 if (is_gimple_val (expr))
6439 return expr;
6441 gimple_test_f = simple ? is_gimple_val : is_gimple_reg_rhs;
6443 push_gimplify_context ();
6444 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
6446 if (var)
6447 expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, expr);
6449 ret = gimplify_expr (&expr, stmts, NULL,
6450 gimple_test_f, fb_rvalue);
6451 gcc_assert (ret != GS_ERROR);
6453 if (gimple_referenced_vars (cfun))
6455 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
6456 add_referenced_var (t);
6459 pop_gimplify_context (NULL);
6461 return expr;
6464 /* Invokes force_gimple_operand for EXPR with parameters SIMPLE_P and VAR. If
6465 some statements are produced, emits them before BSI. */
6467 tree
6468 force_gimple_operand_bsi (block_stmt_iterator *bsi, tree expr,
6469 bool simple_p, tree var)
6471 tree stmts;
6473 expr = force_gimple_operand (expr, &stmts, simple_p, var);
6474 if (stmts)
6475 bsi_insert_before (bsi, stmts, BSI_SAME_STMT);
6477 return expr;
6480 #include "gt-gimplify.h"