Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_7 / gcc / gimplify.c
blob92fb51ab029c79f09c073ea000a2c7000db9a20c
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "tree-pretty-print.h"
33 #include "langhooks.h"
34 #include "tree-flow.h"
35 #include "cgraph.h"
36 #include "timevar.h"
37 #include "hashtab.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "ggc.h"
42 #include "diagnostic-core.h"
43 #include "target.h"
44 #include "pointer-set.h"
45 #include "splay-tree.h"
46 #include "vec.h"
47 #include "gimple.h"
48 #include "tree-pass.h"
50 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name. */
51 #include "expr.h" /* FIXME: for can_move_by_pieces
52 and STACK_CHECK_MAX_VAR_SIZE. */
54 enum gimplify_omp_var_data
56 GOVD_SEEN = 1,
57 GOVD_EXPLICIT = 2,
58 GOVD_SHARED = 4,
59 GOVD_PRIVATE = 8,
60 GOVD_FIRSTPRIVATE = 16,
61 GOVD_LASTPRIVATE = 32,
62 GOVD_REDUCTION = 64,
63 GOVD_LOCAL = 128,
64 GOVD_DEBUG_PRIVATE = 256,
65 GOVD_PRIVATE_OUTER_REF = 512,
66 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
67 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
71 enum omp_region_type
73 ORT_WORKSHARE = 0,
74 ORT_PARALLEL = 2,
75 ORT_COMBINED_PARALLEL = 3,
76 ORT_TASK = 4,
77 ORT_UNTIED_TASK = 5
80 struct gimplify_omp_ctx
82 struct gimplify_omp_ctx *outer_context;
83 splay_tree variables;
84 struct pointer_set_t *privatized_types;
85 location_t location;
86 enum omp_clause_default_kind default_kind;
87 enum omp_region_type region_type;
90 static struct gimplify_ctx *gimplify_ctxp;
91 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
94 /* Formal (expression) temporary table handling: multiple occurrences of
95 the same scalar expression are evaluated into the same temporary. */
97 typedef struct gimple_temp_hash_elt
99 tree val; /* Key */
100 tree temp; /* Value */
101 } elt_t;
103 /* Forward declaration. */
104 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
106 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
107 form and we don't do any syntax checking. */
109 void
110 mark_addressable (tree x)
112 while (handled_component_p (x))
113 x = TREE_OPERAND (x, 0);
114 if (TREE_CODE (x) == MEM_REF
115 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
116 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
117 if (TREE_CODE (x) != VAR_DECL
118 && TREE_CODE (x) != PARM_DECL
119 && TREE_CODE (x) != RESULT_DECL)
120 return;
121 TREE_ADDRESSABLE (x) = 1;
123 /* Also mark the artificial SSA_NAME that points to the partition of X. */
124 if (TREE_CODE (x) == VAR_DECL
125 && !DECL_EXTERNAL (x)
126 && !TREE_STATIC (x)
127 && cfun->gimple_df != NULL
128 && cfun->gimple_df->decls_to_pointers != NULL)
130 void *namep
131 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
132 if (namep)
133 TREE_ADDRESSABLE (*(tree *)namep) = 1;
137 /* Return a hash value for a formal temporary table entry. */
139 static hashval_t
140 gimple_tree_hash (const void *p)
142 tree t = ((const elt_t *) p)->val;
143 return iterative_hash_expr (t, 0);
146 /* Compare two formal temporary table entries. */
148 static int
149 gimple_tree_eq (const void *p1, const void *p2)
151 tree t1 = ((const elt_t *) p1)->val;
152 tree t2 = ((const elt_t *) p2)->val;
153 enum tree_code code = TREE_CODE (t1);
155 if (TREE_CODE (t2) != code
156 || TREE_TYPE (t1) != TREE_TYPE (t2))
157 return 0;
159 if (!operand_equal_p (t1, t2, 0))
160 return 0;
162 #ifdef ENABLE_CHECKING
163 /* Only allow them to compare equal if they also hash equal; otherwise
164 results are nondeterminate, and we fail bootstrap comparison. */
165 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
166 #endif
168 return 1;
171 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
172 *SEQ_P is NULL, a new sequence is allocated. This function is
173 similar to gimple_seq_add_stmt, but does not scan the operands.
174 During gimplification, we need to manipulate statement sequences
175 before the def/use vectors have been constructed. */
177 void
178 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
180 gimple_stmt_iterator si;
182 if (gs == NULL)
183 return;
185 if (*seq_p == NULL)
186 *seq_p = gimple_seq_alloc ();
188 si = gsi_last (*seq_p);
190 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
193 /* Shorter alias name for the above function for use in gimplify.c
194 only. */
196 static inline void
197 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
199 gimple_seq_add_stmt_without_update (seq_p, gs);
202 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
203 NULL, a new sequence is allocated. This function is
204 similar to gimple_seq_add_seq, but does not scan the operands.
205 During gimplification, we need to manipulate statement sequences
206 before the def/use vectors have been constructed. */
208 static void
209 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
211 gimple_stmt_iterator si;
213 if (src == NULL)
214 return;
216 if (*dst_p == NULL)
217 *dst_p = gimple_seq_alloc ();
219 si = gsi_last (*dst_p);
220 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
223 /* Set up a context for the gimplifier. */
225 void
226 push_gimplify_context (struct gimplify_ctx *c)
228 memset (c, '\0', sizeof (*c));
229 c->prev_context = gimplify_ctxp;
230 gimplify_ctxp = c;
233 /* Tear down a context for the gimplifier. If BODY is non-null, then
234 put the temporaries into the outer BIND_EXPR. Otherwise, put them
235 in the local_decls.
237 BODY is not a sequence, but the first tuple in a sequence. */
239 void
240 pop_gimplify_context (gimple body)
242 struct gimplify_ctx *c = gimplify_ctxp;
244 gcc_assert (c && (c->bind_expr_stack == NULL
245 || VEC_empty (gimple, c->bind_expr_stack)));
246 VEC_free (gimple, heap, c->bind_expr_stack);
247 gimplify_ctxp = c->prev_context;
249 if (body)
250 declare_vars (c->temps, body, false);
251 else
252 record_vars (c->temps);
254 if (c->temp_htab)
255 htab_delete (c->temp_htab);
258 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
260 static void
261 gimple_push_bind_expr (gimple gimple_bind)
263 if (gimplify_ctxp->bind_expr_stack == NULL)
264 gimplify_ctxp->bind_expr_stack = VEC_alloc (gimple, heap, 8);
265 VEC_safe_push (gimple, heap, gimplify_ctxp->bind_expr_stack, gimple_bind);
268 /* Pop the first element off the stack of bindings. */
270 static void
271 gimple_pop_bind_expr (void)
273 VEC_pop (gimple, gimplify_ctxp->bind_expr_stack);
276 /* Return the first element of the stack of bindings. */
278 gimple
279 gimple_current_bind_expr (void)
281 return VEC_last (gimple, gimplify_ctxp->bind_expr_stack);
284 /* Return the stack of bindings created during gimplification. */
286 VEC(gimple, heap) *
287 gimple_bind_expr_stack (void)
289 return gimplify_ctxp->bind_expr_stack;
292 /* Return true iff there is a COND_EXPR between us and the innermost
293 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
295 static bool
296 gimple_conditional_context (void)
298 return gimplify_ctxp->conditions > 0;
301 /* Note that we've entered a COND_EXPR. */
303 static void
304 gimple_push_condition (void)
306 #ifdef ENABLE_GIMPLE_CHECKING
307 if (gimplify_ctxp->conditions == 0)
308 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
309 #endif
310 ++(gimplify_ctxp->conditions);
313 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
314 now, add any conditional cleanups we've seen to the prequeue. */
316 static void
317 gimple_pop_condition (gimple_seq *pre_p)
319 int conds = --(gimplify_ctxp->conditions);
321 gcc_assert (conds >= 0);
322 if (conds == 0)
324 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
325 gimplify_ctxp->conditional_cleanups = NULL;
329 /* A stable comparison routine for use with splay trees and DECLs. */
331 static int
332 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
334 tree a = (tree) xa;
335 tree b = (tree) xb;
337 return DECL_UID (a) - DECL_UID (b);
340 /* Create a new omp construct that deals with variable remapping. */
342 static struct gimplify_omp_ctx *
343 new_omp_context (enum omp_region_type region_type)
345 struct gimplify_omp_ctx *c;
347 c = XCNEW (struct gimplify_omp_ctx);
348 c->outer_context = gimplify_omp_ctxp;
349 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
350 c->privatized_types = pointer_set_create ();
351 c->location = input_location;
352 c->region_type = region_type;
353 if ((region_type & ORT_TASK) == 0)
354 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
355 else
356 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
358 return c;
361 /* Destroy an omp construct that deals with variable remapping. */
363 static void
364 delete_omp_context (struct gimplify_omp_ctx *c)
366 splay_tree_delete (c->variables);
367 pointer_set_destroy (c->privatized_types);
368 XDELETE (c);
371 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
372 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
374 /* Both gimplify the statement T and append it to *SEQ_P. This function
375 behaves exactly as gimplify_stmt, but you don't have to pass T as a
376 reference. */
378 void
379 gimplify_and_add (tree t, gimple_seq *seq_p)
381 gimplify_stmt (&t, seq_p);
384 /* Gimplify statement T into sequence *SEQ_P, and return the first
385 tuple in the sequence of generated tuples for this statement.
386 Return NULL if gimplifying T produced no tuples. */
388 static gimple
389 gimplify_and_return_first (tree t, gimple_seq *seq_p)
391 gimple_stmt_iterator last = gsi_last (*seq_p);
393 gimplify_and_add (t, seq_p);
395 if (!gsi_end_p (last))
397 gsi_next (&last);
398 return gsi_stmt (last);
400 else
401 return gimple_seq_first_stmt (*seq_p);
404 /* Strip off a legitimate source ending from the input string NAME of
405 length LEN. Rather than having to know the names used by all of
406 our front ends, we strip off an ending of a period followed by
407 up to five characters. (Java uses ".class".) */
409 static inline void
410 remove_suffix (char *name, int len)
412 int i;
414 for (i = 2; i < 8 && len > i; i++)
416 if (name[len - i] == '.')
418 name[len - i] = '\0';
419 break;
424 /* Create a new temporary name with PREFIX. Return an identifier. */
426 static GTY(()) unsigned int tmp_var_id_num;
428 tree
429 create_tmp_var_name (const char *prefix)
431 char *tmp_name;
433 if (prefix)
435 char *preftmp = ASTRDUP (prefix);
437 remove_suffix (preftmp, strlen (preftmp));
438 clean_symbol_name (preftmp);
440 prefix = preftmp;
443 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
444 return get_identifier (tmp_name);
447 /* Create a new temporary variable declaration of type TYPE.
448 Do NOT push it into the current binding. */
450 tree
451 create_tmp_var_raw (tree type, const char *prefix)
453 tree tmp_var;
455 tmp_var = build_decl (input_location,
456 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
457 type);
459 /* The variable was declared by the compiler. */
460 DECL_ARTIFICIAL (tmp_var) = 1;
461 /* And we don't want debug info for it. */
462 DECL_IGNORED_P (tmp_var) = 1;
464 /* Make the variable writable. */
465 TREE_READONLY (tmp_var) = 0;
467 DECL_EXTERNAL (tmp_var) = 0;
468 TREE_STATIC (tmp_var) = 0;
469 TREE_USED (tmp_var) = 1;
471 return tmp_var;
474 /* Create a new temporary variable declaration of type TYPE. DO push the
475 variable into the current binding. Further, assume that this is called
476 only from gimplification or optimization, at which point the creation of
477 certain types are bugs. */
479 tree
480 create_tmp_var (tree type, const char *prefix)
482 tree tmp_var;
484 /* We don't allow types that are addressable (meaning we can't make copies),
485 or incomplete. We also used to reject every variable size objects here,
486 but now support those for which a constant upper bound can be obtained.
487 The processing for variable sizes is performed in gimple_add_tmp_var,
488 point at which it really matters and possibly reached via paths not going
489 through this function, e.g. after direct calls to create_tmp_var_raw. */
490 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
492 tmp_var = create_tmp_var_raw (type, prefix);
493 gimple_add_tmp_var (tmp_var);
494 return tmp_var;
497 /* Create a new temporary variable declaration of type TYPE by calling
498 create_tmp_var and if TYPE is a vector or a complex number, mark the new
499 temporary as gimple register. */
501 tree
502 create_tmp_reg (tree type, const char *prefix)
504 tree tmp;
506 tmp = create_tmp_var (type, prefix);
507 if (TREE_CODE (type) == COMPLEX_TYPE
508 || TREE_CODE (type) == VECTOR_TYPE)
509 DECL_GIMPLE_REG_P (tmp) = 1;
511 return tmp;
514 /* Create a temporary with a name derived from VAL. Subroutine of
515 lookup_tmp_var; nobody else should call this function. */
517 static inline tree
518 create_tmp_from_val (tree val)
520 /* Drop all qualifiers and address-space information from the value type. */
521 return create_tmp_var (TYPE_MAIN_VARIANT (TREE_TYPE (val)), get_name (val));
524 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
525 an existing expression temporary. */
527 static tree
528 lookup_tmp_var (tree val, bool is_formal)
530 tree ret;
531 bool tmp_reused = false;
533 /* If not optimizing, never really reuse a temporary. local-alloc
534 won't allocate any variable that is used in more than one basic
535 block, which means it will go into memory, causing much extra
536 work in reload and final and poorer code generation, outweighing
537 the extra memory allocation here. */
538 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
539 ret = create_tmp_from_val (val);
540 else
542 elt_t elt, *elt_p;
543 void **slot;
545 elt.val = val;
546 if (gimplify_ctxp->temp_htab == NULL)
547 gimplify_ctxp->temp_htab
548 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
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;
561 tmp_reused = true;
565 /* If the original val is a pointer (to a shared memory location) with
566 either the "point_to_guarded_by" or "point_to_guarded" attribute,
567 we need to copy the attribute to the tmp variable. Note that we don't
568 need to do this for non-pointer variables because, after transformation,
569 the original variable would still be accessed the same way it was
570 accessed in the original code as shown in the following example:
572 Original code: Transformed code:
574 gx = gy + 1; // read gy, write gx gy.0 = gy; // read gy
575 gx.1 = gy.0 + 1;
576 gx = gx.1; // write gx
578 On the other hand, if the original variable is a pointer, it
579 will not be accessed the same way as before:
581 Original code: Transformed code:
583 *gx = a + 5; // gx is read and gx.1 = gx; // gx is read but
584 // dereferenced // not deferenced
585 *gx.1 = a + 5;
587 if (!tmp_reused && DECL_P (val) && POINTER_TYPE_P (TREE_TYPE (val)))
589 tree attr = lookup_attribute ("point_to_guarded_by",
590 DECL_ATTRIBUTES (val));
591 if (!attr)
592 attr = lookup_attribute ("point_to_guarded", DECL_ATTRIBUTES (val));
594 if (attr)
596 DECL_ATTRIBUTES (ret) = build_tree_list (TREE_PURPOSE (attr),
597 TREE_VALUE (attr));
598 /* When detecting an issue later in the thread safety analysis
599 phase, we would like to print out the original variable name
600 (instead of the tmp variable name). So stick the original
601 variable's decl in the debug expr. */
602 DECL_DEBUG_EXPR_IS_FROM (ret) = 1;
603 SET_DECL_DEBUG_EXPR (ret, val);
607 return ret;
610 /* Return true if T is a CALL_EXPR or an expression that can be
611 assigned to a temporary. Note that this predicate should only be
612 used during gimplification. See the rationale for this in
613 gimplify_modify_expr. */
615 static bool
616 is_gimple_reg_rhs_or_call (tree t)
618 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
619 || TREE_CODE (t) == CALL_EXPR);
622 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
623 this predicate should only be used during gimplification. See the
624 rationale for this in gimplify_modify_expr. */
626 static bool
627 is_gimple_mem_rhs_or_call (tree t)
629 /* If we're dealing with a renamable type, either source or dest must be
630 a renamed variable. */
631 if (is_gimple_reg_type (TREE_TYPE (t)))
632 return is_gimple_val (t);
633 else
634 return (is_gimple_val (t) || is_gimple_lvalue (t)
635 || TREE_CODE (t) == CALL_EXPR);
638 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
640 static tree
641 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
642 bool is_formal)
644 tree t, mod;
646 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
647 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
648 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
649 fb_rvalue);
651 t = lookup_tmp_var (val, is_formal);
653 if (is_formal
654 && (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
655 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE))
656 DECL_GIMPLE_REG_P (t) = 1;
658 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
660 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
662 /* gimplify_modify_expr might want to reduce this further. */
663 gimplify_and_add (mod, pre_p);
664 ggc_free (mod);
666 /* If we're gimplifying into ssa, gimplify_modify_expr will have
667 given our temporary an SSA name. Find and return it. */
668 if (gimplify_ctxp->into_ssa)
670 gimple last = gimple_seq_last_stmt (*pre_p);
671 t = gimple_get_lhs (last);
674 return t;
677 /* Return a formal temporary variable initialized with VAL. PRE_P is as
678 in gimplify_expr. Only use this function if:
680 1) The value of the unfactored expression represented by VAL will not
681 change between the initialization and use of the temporary, and
682 2) The temporary will not be otherwise modified.
684 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
685 and #2 means it is inappropriate for && temps.
687 For other cases, use get_initialized_tmp_var instead. */
689 tree
690 get_formal_tmp_var (tree val, gimple_seq *pre_p)
692 return internal_get_tmp_var (val, pre_p, NULL, true);
695 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
696 are as in gimplify_expr. */
698 tree
699 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
701 return internal_get_tmp_var (val, pre_p, post_p, false);
704 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
705 generate debug info for them; otherwise don't. */
707 void
708 declare_vars (tree vars, gimple scope, bool debug_info)
710 tree last = vars;
711 if (last)
713 tree temps, block;
715 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
717 temps = nreverse (last);
719 block = gimple_bind_block (scope);
720 gcc_assert (!block || TREE_CODE (block) == BLOCK);
721 if (!block || !debug_info)
723 DECL_CHAIN (last) = gimple_bind_vars (scope);
724 gimple_bind_set_vars (scope, temps);
726 else
728 /* We need to attach the nodes both to the BIND_EXPR and to its
729 associated BLOCK for debugging purposes. The key point here
730 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
731 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
732 if (BLOCK_VARS (block))
733 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
734 else
736 gimple_bind_set_vars (scope,
737 chainon (gimple_bind_vars (scope), temps));
738 BLOCK_VARS (block) = temps;
744 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
745 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
746 no such upper bound can be obtained. */
748 static void
749 force_constant_size (tree var)
751 /* The only attempt we make is by querying the maximum size of objects
752 of the variable's type. */
754 HOST_WIDE_INT max_size;
756 gcc_assert (TREE_CODE (var) == VAR_DECL);
758 max_size = max_int_size_in_bytes (TREE_TYPE (var));
760 gcc_assert (max_size >= 0);
762 DECL_SIZE_UNIT (var)
763 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
764 DECL_SIZE (var)
765 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
768 /* Push the temporary variable TMP into the current binding. */
770 void
771 gimple_add_tmp_var (tree tmp)
773 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
775 /* Later processing assumes that the object size is constant, which might
776 not be true at this point. Force the use of a constant upper bound in
777 this case. */
778 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
779 force_constant_size (tmp);
781 DECL_CONTEXT (tmp) = current_function_decl;
782 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
784 if (gimplify_ctxp)
786 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
787 gimplify_ctxp->temps = tmp;
789 /* Mark temporaries local within the nearest enclosing parallel. */
790 if (gimplify_omp_ctxp)
792 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
793 while (ctx && ctx->region_type == ORT_WORKSHARE)
794 ctx = ctx->outer_context;
795 if (ctx)
796 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
799 else if (cfun)
800 record_vars (tmp);
801 else
803 gimple_seq body_seq;
805 /* This case is for nested functions. We need to expose the locals
806 they create. */
807 body_seq = gimple_body (current_function_decl);
808 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
812 /* Determine whether to assign a location to the statement GS. */
814 static bool
815 should_carry_location_p (gimple gs)
817 /* Don't emit a line note for a label. We particularly don't want to
818 emit one for the break label, since it doesn't actually correspond
819 to the beginning of the loop/switch. */
820 if (gimple_code (gs) == GIMPLE_LABEL)
821 return false;
823 return true;
826 /* Return true if a location should not be emitted for this statement
827 by annotate_one_with_location. */
829 static inline bool
830 gimple_do_not_emit_location_p (gimple g)
832 return gimple_plf (g, GF_PLF_1);
835 /* Mark statement G so a location will not be emitted by
836 annotate_one_with_location. */
838 static inline void
839 gimple_set_do_not_emit_location (gimple g)
841 /* The PLF flags are initialized to 0 when a new tuple is created,
842 so no need to initialize it anywhere. */
843 gimple_set_plf (g, GF_PLF_1, true);
846 /* Set the location for gimple statement GS to LOCATION. */
848 static void
849 annotate_one_with_location (gimple gs, location_t location)
851 if (!gimple_has_location (gs)
852 && !gimple_do_not_emit_location_p (gs)
853 && should_carry_location_p (gs))
854 gimple_set_location (gs, location);
857 /* Set LOCATION for all the statements after iterator GSI in sequence
858 SEQ. If GSI is pointing to the end of the sequence, start with the
859 first statement in SEQ. */
861 static void
862 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
863 location_t location)
865 if (gsi_end_p (gsi))
866 gsi = gsi_start (seq);
867 else
868 gsi_next (&gsi);
870 for (; !gsi_end_p (gsi); gsi_next (&gsi))
871 annotate_one_with_location (gsi_stmt (gsi), location);
874 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
876 void
877 annotate_all_with_location (gimple_seq stmt_p, location_t location)
879 gimple_stmt_iterator i;
881 if (gimple_seq_empty_p (stmt_p))
882 return;
884 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
886 gimple gs = gsi_stmt (i);
887 annotate_one_with_location (gs, location);
891 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
892 nodes that are referenced more than once in GENERIC functions. This is
893 necessary because gimplification (translation into GIMPLE) is performed
894 by modifying tree nodes in-place, so gimplication of a shared node in a
895 first context could generate an invalid GIMPLE form in a second context.
897 This is achieved with a simple mark/copy/unmark algorithm that walks the
898 GENERIC representation top-down, marks nodes with TREE_VISITED the first
899 time it encounters them, duplicates them if they already have TREE_VISITED
900 set, and finally removes the TREE_VISITED marks it has set.
902 The algorithm works only at the function level, i.e. it generates a GENERIC
903 representation of a function with no nodes shared within the function when
904 passed a GENERIC function (except for nodes that are allowed to be shared).
906 At the global level, it is also necessary to unshare tree nodes that are
907 referenced in more than one function, for the same aforementioned reason.
908 This requires some cooperation from the front-end. There are 2 strategies:
910 1. Manual unsharing. The front-end needs to call unshare_expr on every
911 expression that might end up being shared across functions.
913 2. Deep unsharing. This is an extension of regular unsharing. Instead
914 of calling unshare_expr on expressions that might be shared across
915 functions, the front-end pre-marks them with TREE_VISITED. This will
916 ensure that they are unshared on the first reference within functions
917 when the regular unsharing algorithm runs. The counterpart is that
918 this algorithm must look deeper than for manual unsharing, which is
919 specified by LANG_HOOKS_DEEP_UNSHARING.
921 If there are only few specific cases of node sharing across functions, it is
922 probably easier for a front-end to unshare the expressions manually. On the
923 contrary, if the expressions generated at the global level are as widespread
924 as expressions generated within functions, deep unsharing is very likely the
925 way to go. */
927 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
928 These nodes model computations that must be done once. If we were to
929 unshare something like SAVE_EXPR(i++), the gimplification process would
930 create wrong code. However, if DATA is non-null, it must hold a pointer
931 set that is used to unshare the subtrees of these nodes. */
933 static tree
934 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
936 tree t = *tp;
937 enum tree_code code = TREE_CODE (t);
939 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
940 copy their subtrees if we can make sure to do it only once. */
941 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
943 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
945 else
946 *walk_subtrees = 0;
949 /* Stop at types, decls, constants like copy_tree_r. */
950 else if (TREE_CODE_CLASS (code) == tcc_type
951 || TREE_CODE_CLASS (code) == tcc_declaration
952 || TREE_CODE_CLASS (code) == tcc_constant
953 /* We can't do anything sensible with a BLOCK used as an
954 expression, but we also can't just die when we see it
955 because of non-expression uses. So we avert our eyes
956 and cross our fingers. Silly Java. */
957 || code == BLOCK)
958 *walk_subtrees = 0;
960 /* Cope with the statement expression extension. */
961 else if (code == STATEMENT_LIST)
964 /* Leave the bulk of the work to copy_tree_r itself. */
965 else
966 copy_tree_r (tp, walk_subtrees, NULL);
968 return NULL_TREE;
971 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
972 If *TP has been visited already, then *TP is deeply copied by calling
973 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
975 static tree
976 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
978 tree t = *tp;
979 enum tree_code code = TREE_CODE (t);
981 /* Skip types, decls, and constants. But we do want to look at their
982 types and the bounds of types. Mark them as visited so we properly
983 unmark their subtrees on the unmark pass. If we've already seen them,
984 don't look down further. */
985 if (TREE_CODE_CLASS (code) == tcc_type
986 || TREE_CODE_CLASS (code) == tcc_declaration
987 || TREE_CODE_CLASS (code) == tcc_constant)
989 if (TREE_VISITED (t))
990 *walk_subtrees = 0;
991 else
992 TREE_VISITED (t) = 1;
995 /* If this node has been visited already, unshare it and don't look
996 any deeper. */
997 else if (TREE_VISITED (t))
999 walk_tree (tp, mostly_copy_tree_r, data, NULL);
1000 *walk_subtrees = 0;
1003 /* Otherwise, mark the node as visited and keep looking. */
1004 else
1005 TREE_VISITED (t) = 1;
1007 return NULL_TREE;
1010 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
1011 copy_if_shared_r callback unmodified. */
1013 static inline void
1014 copy_if_shared (tree *tp, void *data)
1016 walk_tree (tp, copy_if_shared_r, data, NULL);
1019 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
1020 any nested functions. */
1022 static void
1023 unshare_body (tree fndecl)
1025 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1026 /* If the language requires deep unsharing, we need a pointer set to make
1027 sure we don't repeatedly unshare subtrees of unshareable nodes. */
1028 struct pointer_set_t *visited
1029 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
1031 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
1032 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
1033 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1035 if (visited)
1036 pointer_set_destroy (visited);
1038 if (cgn)
1039 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1040 unshare_body (cgn->decl);
1043 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1044 Subtrees are walked until the first unvisited node is encountered. */
1046 static tree
1047 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1049 tree t = *tp;
1051 /* If this node has been visited, unmark it and keep looking. */
1052 if (TREE_VISITED (t))
1053 TREE_VISITED (t) = 0;
1055 /* Otherwise, don't look any deeper. */
1056 else
1057 *walk_subtrees = 0;
1059 return NULL_TREE;
1062 /* Unmark the visited trees rooted at *TP. */
1064 static inline void
1065 unmark_visited (tree *tp)
1067 walk_tree (tp, unmark_visited_r, NULL, NULL);
1070 /* Likewise, but mark all trees as not visited. */
1072 static void
1073 unvisit_body (tree fndecl)
1075 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1077 unmark_visited (&DECL_SAVED_TREE (fndecl));
1078 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1079 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1081 if (cgn)
1082 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1083 unvisit_body (cgn->decl);
1086 /* Unconditionally make an unshared copy of EXPR. This is used when using
1087 stored expressions which span multiple functions, such as BINFO_VTABLE,
1088 as the normal unsharing process can't tell that they're shared. */
1090 tree
1091 unshare_expr (tree expr)
1093 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1094 return expr;
1097 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1098 contain statements and have a value. Assign its value to a temporary
1099 and give it void_type_node. Return the temporary, or NULL_TREE if
1100 WRAPPER was already void. */
1102 tree
1103 voidify_wrapper_expr (tree wrapper, tree temp)
1105 tree type = TREE_TYPE (wrapper);
1106 if (type && !VOID_TYPE_P (type))
1108 tree *p;
1110 /* Set p to point to the body of the wrapper. Loop until we find
1111 something that isn't a wrapper. */
1112 for (p = &wrapper; p && *p; )
1114 switch (TREE_CODE (*p))
1116 case BIND_EXPR:
1117 TREE_SIDE_EFFECTS (*p) = 1;
1118 TREE_TYPE (*p) = void_type_node;
1119 /* For a BIND_EXPR, the body is operand 1. */
1120 p = &BIND_EXPR_BODY (*p);
1121 break;
1123 case CLEANUP_POINT_EXPR:
1124 case TRY_FINALLY_EXPR:
1125 case TRY_CATCH_EXPR:
1126 TREE_SIDE_EFFECTS (*p) = 1;
1127 TREE_TYPE (*p) = void_type_node;
1128 p = &TREE_OPERAND (*p, 0);
1129 break;
1131 case STATEMENT_LIST:
1133 tree_stmt_iterator i = tsi_last (*p);
1134 TREE_SIDE_EFFECTS (*p) = 1;
1135 TREE_TYPE (*p) = void_type_node;
1136 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1138 break;
1140 case COMPOUND_EXPR:
1141 /* Advance to the last statement. Set all container types to
1142 void. */
1143 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1145 TREE_SIDE_EFFECTS (*p) = 1;
1146 TREE_TYPE (*p) = void_type_node;
1148 break;
1150 case TRANSACTION_EXPR:
1151 TREE_SIDE_EFFECTS (*p) = 1;
1152 TREE_TYPE (*p) = void_type_node;
1153 p = &TRANSACTION_EXPR_BODY (*p);
1154 break;
1156 default:
1157 /* Assume that any tree upon which voidify_wrapper_expr is
1158 directly called is a wrapper, and that its body is op0. */
1159 if (p == &wrapper)
1161 TREE_SIDE_EFFECTS (*p) = 1;
1162 TREE_TYPE (*p) = void_type_node;
1163 p = &TREE_OPERAND (*p, 0);
1164 break;
1166 goto out;
1170 out:
1171 if (p == NULL || IS_EMPTY_STMT (*p))
1172 temp = NULL_TREE;
1173 else if (temp)
1175 /* The wrapper is on the RHS of an assignment that we're pushing
1176 down. */
1177 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1178 || TREE_CODE (temp) == MODIFY_EXPR);
1179 TREE_OPERAND (temp, 1) = *p;
1180 *p = temp;
1182 else
1184 temp = create_tmp_var (type, "retval");
1185 *p = build2 (INIT_EXPR, type, temp, *p);
1188 return temp;
1191 return NULL_TREE;
1194 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1195 a temporary through which they communicate. */
1197 static void
1198 build_stack_save_restore (gimple *save, gimple *restore)
1200 tree tmp_var;
1202 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1203 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1204 gimple_call_set_lhs (*save, tmp_var);
1206 *restore
1207 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1208 1, tmp_var);
1211 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1213 static enum gimplify_status
1214 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1216 tree bind_expr = *expr_p;
1217 bool old_save_stack = gimplify_ctxp->save_stack;
1218 tree t;
1219 gimple gimple_bind;
1220 gimple_seq body, cleanup;
1221 gimple stack_save;
1223 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1225 /* Mark variables seen in this bind expr. */
1226 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1228 if (TREE_CODE (t) == VAR_DECL)
1230 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1232 /* Mark variable as local. */
1233 if (ctx && !DECL_EXTERNAL (t)
1234 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1235 || splay_tree_lookup (ctx->variables,
1236 (splay_tree_key) t) == NULL))
1237 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1239 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1241 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1242 cfun->has_local_explicit_reg_vars = true;
1245 /* Preliminarily mark non-addressed complex variables as eligible
1246 for promotion to gimple registers. We'll transform their uses
1247 as we find them. */
1248 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1249 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1250 && !TREE_THIS_VOLATILE (t)
1251 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1252 && !needs_to_live_in_memory (t))
1253 DECL_GIMPLE_REG_P (t) = 1;
1256 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1257 BIND_EXPR_BLOCK (bind_expr));
1258 gimple_push_bind_expr (gimple_bind);
1260 gimplify_ctxp->save_stack = false;
1262 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1263 body = NULL;
1264 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1265 gimple_bind_set_body (gimple_bind, body);
1267 cleanup = NULL;
1268 stack_save = NULL;
1269 if (gimplify_ctxp->save_stack)
1271 gimple stack_restore;
1273 /* Save stack on entry and restore it on exit. Add a try_finally
1274 block to achieve this. Note that mudflap depends on the
1275 format of the emitted code: see mx_register_decls(). */
1276 build_stack_save_restore (&stack_save, &stack_restore);
1278 gimplify_seq_add_stmt (&cleanup, stack_restore);
1281 /* Add clobbers for all variables that go out of scope. */
1282 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1284 if (TREE_CODE (t) == VAR_DECL
1285 && !is_global_var (t)
1286 && DECL_CONTEXT (t) == current_function_decl
1287 && !DECL_HARD_REGISTER (t)
1288 && !TREE_THIS_VOLATILE (t)
1289 && !DECL_HAS_VALUE_EXPR_P (t)
1290 /* Only care for variables that have to be in memory. Others
1291 will be rewritten into SSA names, hence moved to the top-level. */
1292 && !is_gimple_reg (t)
1293 && flag_stack_reuse != SR_NONE)
1295 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1296 TREE_THIS_VOLATILE (clobber) = 1;
1297 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1301 if (cleanup)
1303 gimple gs;
1304 gimple_seq new_body;
1306 new_body = NULL;
1307 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1308 GIMPLE_TRY_FINALLY);
1310 if (stack_save)
1311 gimplify_seq_add_stmt (&new_body, stack_save);
1312 gimplify_seq_add_stmt (&new_body, gs);
1313 gimple_bind_set_body (gimple_bind, new_body);
1316 gimplify_ctxp->save_stack = old_save_stack;
1317 gimple_pop_bind_expr ();
1319 gimplify_seq_add_stmt (pre_p, gimple_bind);
1321 if (temp)
1323 *expr_p = temp;
1324 return GS_OK;
1327 *expr_p = NULL_TREE;
1328 return GS_ALL_DONE;
1331 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1332 GIMPLE value, it is assigned to a new temporary and the statement is
1333 re-written to return the temporary.
1335 PRE_P points to the sequence where side effects that must happen before
1336 STMT should be stored. */
1338 static enum gimplify_status
1339 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1341 gimple ret;
1342 tree ret_expr = TREE_OPERAND (stmt, 0);
1343 tree result_decl, result;
1345 if (ret_expr == error_mark_node)
1346 return GS_ERROR;
1348 if (!ret_expr
1349 || TREE_CODE (ret_expr) == RESULT_DECL
1350 || ret_expr == error_mark_node)
1352 gimple ret = gimple_build_return (ret_expr);
1353 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1354 gimplify_seq_add_stmt (pre_p, ret);
1355 return GS_ALL_DONE;
1358 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1359 result_decl = NULL_TREE;
1360 else
1362 result_decl = TREE_OPERAND (ret_expr, 0);
1364 /* See through a return by reference. */
1365 if (TREE_CODE (result_decl) == INDIRECT_REF)
1366 result_decl = TREE_OPERAND (result_decl, 0);
1368 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1369 || TREE_CODE (ret_expr) == INIT_EXPR)
1370 && TREE_CODE (result_decl) == RESULT_DECL);
1373 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1374 Recall that aggregate_value_p is FALSE for any aggregate type that is
1375 returned in registers. If we're returning values in registers, then
1376 we don't want to extend the lifetime of the RESULT_DECL, particularly
1377 across another call. In addition, for those aggregates for which
1378 hard_function_value generates a PARALLEL, we'll die during normal
1379 expansion of structure assignments; there's special code in expand_return
1380 to handle this case that does not exist in expand_expr. */
1381 if (!result_decl)
1382 result = NULL_TREE;
1383 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1385 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1387 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1388 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1389 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1390 should be effectively allocated by the caller, i.e. all calls to
1391 this function must be subject to the Return Slot Optimization. */
1392 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1393 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1395 result = result_decl;
1397 else if (gimplify_ctxp->return_temp)
1398 result = gimplify_ctxp->return_temp;
1399 else
1401 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1403 /* ??? With complex control flow (usually involving abnormal edges),
1404 we can wind up warning about an uninitialized value for this. Due
1405 to how this variable is constructed and initialized, this is never
1406 true. Give up and never warn. */
1407 TREE_NO_WARNING (result) = 1;
1409 gimplify_ctxp->return_temp = result;
1412 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1413 Then gimplify the whole thing. */
1414 if (result != result_decl)
1415 TREE_OPERAND (ret_expr, 0) = result;
1417 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1419 ret = gimple_build_return (result);
1420 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1421 gimplify_seq_add_stmt (pre_p, ret);
1423 return GS_ALL_DONE;
1426 /* Gimplify a variable-length array DECL. */
1428 static void
1429 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1431 /* This is a variable-sized decl. Simplify its size and mark it
1432 for deferred expansion. Note that mudflap depends on the format
1433 of the emitted code: see mx_register_decls(). */
1434 tree t, addr, ptr_type;
1436 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1437 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1439 /* All occurrences of this decl in final gimplified code will be
1440 replaced by indirection. Setting DECL_VALUE_EXPR does two
1441 things: First, it lets the rest of the gimplifier know what
1442 replacement to use. Second, it lets the debug info know
1443 where to find the value. */
1444 ptr_type = build_pointer_type (TREE_TYPE (decl));
1445 addr = create_tmp_var (ptr_type, get_name (decl));
1446 DECL_IGNORED_P (addr) = 0;
1447 t = build_fold_indirect_ref (addr);
1448 TREE_THIS_NOTRAP (t) = 1;
1449 SET_DECL_VALUE_EXPR (decl, t);
1450 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1452 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1453 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1454 size_int (DECL_ALIGN (decl)));
1455 /* The call has been built for a variable-sized object. */
1456 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1457 t = fold_convert (ptr_type, t);
1458 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1460 gimplify_and_add (t, seq_p);
1462 /* Indicate that we need to restore the stack level when the
1463 enclosing BIND_EXPR is exited. */
1464 gimplify_ctxp->save_stack = true;
1467 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1468 and initialization explicit. */
1470 static enum gimplify_status
1471 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1473 tree stmt = *stmt_p;
1474 tree decl = DECL_EXPR_DECL (stmt);
1476 *stmt_p = NULL_TREE;
1478 if (TREE_TYPE (decl) == error_mark_node)
1479 return GS_ERROR;
1481 if ((TREE_CODE (decl) == TYPE_DECL
1482 || TREE_CODE (decl) == VAR_DECL)
1483 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1484 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1486 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1487 in case its size expressions contain problematic nodes like CALL_EXPR. */
1488 if (TREE_CODE (decl) == TYPE_DECL
1489 && DECL_ORIGINAL_TYPE (decl)
1490 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1491 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1493 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1495 tree init = DECL_INITIAL (decl);
1497 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1498 || (!TREE_STATIC (decl)
1499 && flag_stack_check == GENERIC_STACK_CHECK
1500 && compare_tree_int (DECL_SIZE_UNIT (decl),
1501 STACK_CHECK_MAX_VAR_SIZE) > 0))
1502 gimplify_vla_decl (decl, seq_p);
1504 /* Some front ends do not explicitly declare all anonymous
1505 artificial variables. We compensate here by declaring the
1506 variables, though it would be better if the front ends would
1507 explicitly declare them. */
1508 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1509 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1510 gimple_add_tmp_var (decl);
1512 if (init && init != error_mark_node)
1514 if (!TREE_STATIC (decl))
1516 DECL_INITIAL (decl) = NULL_TREE;
1517 init = build2 (INIT_EXPR, void_type_node, decl, init);
1518 gimplify_and_add (init, seq_p);
1519 ggc_free (init);
1521 else
1522 /* We must still examine initializers for static variables
1523 as they may contain a label address. */
1524 walk_tree (&init, force_labels_r, NULL, NULL);
1528 return GS_ALL_DONE;
1531 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1532 and replacing the LOOP_EXPR with goto, but if the loop contains an
1533 EXIT_EXPR, we need to append a label for it to jump to. */
1535 static enum gimplify_status
1536 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1538 tree saved_label = gimplify_ctxp->exit_label;
1539 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1541 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1543 gimplify_ctxp->exit_label = NULL_TREE;
1545 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1547 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1549 if (gimplify_ctxp->exit_label)
1550 gimplify_seq_add_stmt (pre_p,
1551 gimple_build_label (gimplify_ctxp->exit_label));
1553 gimplify_ctxp->exit_label = saved_label;
1555 *expr_p = NULL;
1556 return GS_ALL_DONE;
1559 /* Gimplify a statement list onto a sequence. These may be created either
1560 by an enlightened front-end, or by shortcut_cond_expr. */
1562 static enum gimplify_status
1563 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1565 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1567 tree_stmt_iterator i = tsi_start (*expr_p);
1569 while (!tsi_end_p (i))
1571 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1572 tsi_delink (&i);
1575 if (temp)
1577 *expr_p = temp;
1578 return GS_OK;
1581 return GS_ALL_DONE;
1584 /* Compare two case labels. Because the front end should already have
1585 made sure that case ranges do not overlap, it is enough to only compare
1586 the CASE_LOW values of each case label. */
1588 static int
1589 compare_case_labels (const void *p1, const void *p2)
1591 const_tree const case1 = *(const_tree const*)p1;
1592 const_tree const case2 = *(const_tree const*)p2;
1594 /* The 'default' case label always goes first. */
1595 if (!CASE_LOW (case1))
1596 return -1;
1597 else if (!CASE_LOW (case2))
1598 return 1;
1599 else
1600 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1603 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1605 void
1606 sort_case_labels (VEC(tree,heap)* label_vec)
1608 VEC_qsort (tree, label_vec, compare_case_labels);
1611 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1612 branch to. */
1614 static enum gimplify_status
1615 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1617 tree switch_expr = *expr_p;
1618 gimple_seq switch_body_seq = NULL;
1619 enum gimplify_status ret;
1621 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1622 fb_rvalue);
1623 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1624 return ret;
1626 if (SWITCH_BODY (switch_expr))
1628 VEC (tree,heap) *labels;
1629 VEC (tree,heap) *saved_labels;
1630 tree default_case = NULL_TREE;
1631 size_t i, len;
1632 gimple gimple_switch;
1634 /* If someone can be bothered to fill in the labels, they can
1635 be bothered to null out the body too. */
1636 gcc_assert (!SWITCH_LABELS (switch_expr));
1638 /* save old labels, get new ones from body, then restore the old
1639 labels. Save all the things from the switch body to append after. */
1640 saved_labels = gimplify_ctxp->case_labels;
1641 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1643 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1644 labels = gimplify_ctxp->case_labels;
1645 gimplify_ctxp->case_labels = saved_labels;
1647 i = 0;
1648 while (i < VEC_length (tree, labels))
1650 tree elt = VEC_index (tree, labels, i);
1651 tree low = CASE_LOW (elt);
1652 bool remove_element = FALSE;
1654 if (low)
1656 /* Discard empty ranges. */
1657 tree high = CASE_HIGH (elt);
1658 if (high && tree_int_cst_lt (high, low))
1659 remove_element = TRUE;
1661 else
1663 /* The default case must be the last label in the list. */
1664 gcc_assert (!default_case);
1665 default_case = elt;
1666 remove_element = TRUE;
1669 if (remove_element)
1670 VEC_ordered_remove (tree, labels, i);
1671 else
1672 i++;
1674 len = i;
1676 if (!VEC_empty (tree, labels))
1677 sort_case_labels (labels);
1679 if (!default_case)
1681 tree type = TREE_TYPE (switch_expr);
1683 /* If the switch has no default label, add one, so that we jump
1684 around the switch body. If the labels already cover the whole
1685 range of type, add the default label pointing to one of the
1686 existing labels. */
1687 if (type == void_type_node)
1688 type = TREE_TYPE (SWITCH_COND (switch_expr));
1689 if (len
1690 && INTEGRAL_TYPE_P (type)
1691 && TYPE_MIN_VALUE (type)
1692 && TYPE_MAX_VALUE (type)
1693 && tree_int_cst_equal (CASE_LOW (VEC_index (tree, labels, 0)),
1694 TYPE_MIN_VALUE (type)))
1696 tree low, high = CASE_HIGH (VEC_index (tree, labels, len - 1));
1697 if (!high)
1698 high = CASE_LOW (VEC_index (tree, labels, len - 1));
1699 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (type)))
1701 for (i = 1; i < len; i++)
1703 high = CASE_LOW (VEC_index (tree, labels, i));
1704 low = CASE_HIGH (VEC_index (tree, labels, i - 1));
1705 if (!low)
1706 low = CASE_LOW (VEC_index (tree, labels, i - 1));
1707 if ((TREE_INT_CST_LOW (low) + 1
1708 != TREE_INT_CST_LOW (high))
1709 || (TREE_INT_CST_HIGH (low)
1710 + (TREE_INT_CST_LOW (high) == 0)
1711 != TREE_INT_CST_HIGH (high)))
1712 break;
1714 if (i == len)
1716 tree label = CASE_LABEL (VEC_index (tree, labels, 0));
1717 default_case = build_case_label (NULL_TREE, NULL_TREE,
1718 label);
1723 if (!default_case)
1725 gimple new_default;
1727 default_case
1728 = build_case_label (NULL_TREE, NULL_TREE,
1729 create_artificial_label (UNKNOWN_LOCATION));
1730 new_default = gimple_build_label (CASE_LABEL (default_case));
1731 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1735 gimple_switch = gimple_build_switch_vec (SWITCH_COND (switch_expr),
1736 default_case, labels);
1737 gimplify_seq_add_stmt (pre_p, gimple_switch);
1738 gimplify_seq_add_seq (pre_p, switch_body_seq);
1739 VEC_free(tree, heap, labels);
1741 else
1742 gcc_assert (SWITCH_LABELS (switch_expr));
1744 return GS_ALL_DONE;
1747 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1749 static enum gimplify_status
1750 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1752 struct gimplify_ctx *ctxp;
1753 gimple gimple_label;
1755 /* Invalid OpenMP programs can play Duff's Device type games with
1756 #pragma omp parallel. At least in the C front end, we don't
1757 detect such invalid branches until after gimplification. */
1758 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1759 if (ctxp->case_labels)
1760 break;
1762 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1763 VEC_safe_push (tree, heap, ctxp->case_labels, *expr_p);
1764 gimplify_seq_add_stmt (pre_p, gimple_label);
1766 return GS_ALL_DONE;
1769 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1770 if necessary. */
1772 tree
1773 build_and_jump (tree *label_p)
1775 if (label_p == NULL)
1776 /* If there's nowhere to jump, just fall through. */
1777 return NULL_TREE;
1779 if (*label_p == NULL_TREE)
1781 tree label = create_artificial_label (UNKNOWN_LOCATION);
1782 *label_p = label;
1785 return build1 (GOTO_EXPR, void_type_node, *label_p);
1788 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1789 This also involves building a label to jump to and communicating it to
1790 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1792 static enum gimplify_status
1793 gimplify_exit_expr (tree *expr_p)
1795 tree cond = TREE_OPERAND (*expr_p, 0);
1796 tree expr;
1798 expr = build_and_jump (&gimplify_ctxp->exit_label);
1799 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1800 *expr_p = expr;
1802 return GS_OK;
1805 /* A helper function to be called via walk_tree. Mark all labels under *TP
1806 as being forced. To be called for DECL_INITIAL of static variables. */
1808 tree
1809 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1811 if (TYPE_P (*tp))
1812 *walk_subtrees = 0;
1813 if (TREE_CODE (*tp) == LABEL_DECL)
1814 FORCED_LABEL (*tp) = 1;
1816 return NULL_TREE;
1819 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1820 different from its canonical type, wrap the whole thing inside a
1821 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1822 type.
1824 The canonical type of a COMPONENT_REF is the type of the field being
1825 referenced--unless the field is a bit-field which can be read directly
1826 in a smaller mode, in which case the canonical type is the
1827 sign-appropriate type corresponding to that mode. */
1829 static void
1830 canonicalize_component_ref (tree *expr_p)
1832 tree expr = *expr_p;
1833 tree type;
1835 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1837 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1838 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1839 else
1840 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1842 /* One could argue that all the stuff below is not necessary for
1843 the non-bitfield case and declare it a FE error if type
1844 adjustment would be needed. */
1845 if (TREE_TYPE (expr) != type)
1847 #ifdef ENABLE_TYPES_CHECKING
1848 tree old_type = TREE_TYPE (expr);
1849 #endif
1850 int type_quals;
1852 /* We need to preserve qualifiers and propagate them from
1853 operand 0. */
1854 type_quals = TYPE_QUALS (type)
1855 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1856 if (TYPE_QUALS (type) != type_quals)
1857 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1859 /* Set the type of the COMPONENT_REF to the underlying type. */
1860 TREE_TYPE (expr) = type;
1862 #ifdef ENABLE_TYPES_CHECKING
1863 /* It is now a FE error, if the conversion from the canonical
1864 type to the original expression type is not useless. */
1865 gcc_assert (useless_type_conversion_p (old_type, type));
1866 #endif
1870 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1871 to foo, embed that change in the ADDR_EXPR by converting
1872 T array[U];
1873 (T *)&array
1875 &array[L]
1876 where L is the lower bound. For simplicity, only do this for constant
1877 lower bound.
1878 The constraint is that the type of &array[L] is trivially convertible
1879 to T *. */
1881 static void
1882 canonicalize_addr_expr (tree *expr_p)
1884 tree expr = *expr_p;
1885 tree addr_expr = TREE_OPERAND (expr, 0);
1886 tree datype, ddatype, pddatype;
1888 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1889 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1890 || TREE_CODE (addr_expr) != ADDR_EXPR)
1891 return;
1893 /* The addr_expr type should be a pointer to an array. */
1894 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1895 if (TREE_CODE (datype) != ARRAY_TYPE)
1896 return;
1898 /* The pointer to element type shall be trivially convertible to
1899 the expression pointer type. */
1900 ddatype = TREE_TYPE (datype);
1901 pddatype = build_pointer_type (ddatype);
1902 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1903 pddatype))
1904 return;
1906 /* The lower bound and element sizes must be constant. */
1907 if (!TYPE_SIZE_UNIT (ddatype)
1908 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1909 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1910 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1911 return;
1913 /* All checks succeeded. Build a new node to merge the cast. */
1914 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1915 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1916 NULL_TREE, NULL_TREE);
1917 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1919 /* We can have stripped a required restrict qualifier above. */
1920 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1921 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1924 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1925 underneath as appropriate. */
1927 static enum gimplify_status
1928 gimplify_conversion (tree *expr_p)
1930 location_t loc = EXPR_LOCATION (*expr_p);
1931 gcc_assert (CONVERT_EXPR_P (*expr_p));
1933 /* Then strip away all but the outermost conversion. */
1934 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1936 /* And remove the outermost conversion if it's useless. */
1937 if (tree_ssa_useless_type_conversion (*expr_p))
1938 *expr_p = TREE_OPERAND (*expr_p, 0);
1940 /* If we still have a conversion at the toplevel,
1941 then canonicalize some constructs. */
1942 if (CONVERT_EXPR_P (*expr_p))
1944 tree sub = TREE_OPERAND (*expr_p, 0);
1946 /* If a NOP conversion is changing the type of a COMPONENT_REF
1947 expression, then canonicalize its type now in order to expose more
1948 redundant conversions. */
1949 if (TREE_CODE (sub) == COMPONENT_REF)
1950 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1952 /* If a NOP conversion is changing a pointer to array of foo
1953 to a pointer to foo, embed that change in the ADDR_EXPR. */
1954 else if (TREE_CODE (sub) == ADDR_EXPR)
1955 canonicalize_addr_expr (expr_p);
1958 /* If we have a conversion to a non-register type force the
1959 use of a VIEW_CONVERT_EXPR instead. */
1960 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1961 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1962 TREE_OPERAND (*expr_p, 0));
1964 return GS_OK;
1967 /* Nonlocal VLAs seen in the current function. */
1968 static struct pointer_set_t *nonlocal_vlas;
1970 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1971 DECL_VALUE_EXPR, and it's worth re-examining things. */
1973 static enum gimplify_status
1974 gimplify_var_or_parm_decl (tree *expr_p)
1976 tree decl = *expr_p;
1978 /* ??? If this is a local variable, and it has not been seen in any
1979 outer BIND_EXPR, then it's probably the result of a duplicate
1980 declaration, for which we've already issued an error. It would
1981 be really nice if the front end wouldn't leak these at all.
1982 Currently the only known culprit is C++ destructors, as seen
1983 in g++.old-deja/g++.jason/binding.C. */
1984 if (TREE_CODE (decl) == VAR_DECL
1985 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1986 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1987 && decl_function_context (decl) == current_function_decl)
1989 gcc_assert (seen_error ());
1990 return GS_ERROR;
1993 /* When within an OpenMP context, notice uses of variables. */
1994 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1995 return GS_ALL_DONE;
1997 /* If the decl is an alias for another expression, substitute it now. */
1998 if (DECL_HAS_VALUE_EXPR_P (decl))
2000 tree value_expr = DECL_VALUE_EXPR (decl);
2002 /* For referenced nonlocal VLAs add a decl for debugging purposes
2003 to the current function. */
2004 if (TREE_CODE (decl) == VAR_DECL
2005 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2006 && nonlocal_vlas != NULL
2007 && TREE_CODE (value_expr) == INDIRECT_REF
2008 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2009 && decl_function_context (decl) != current_function_decl)
2011 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2012 while (ctx && ctx->region_type == ORT_WORKSHARE)
2013 ctx = ctx->outer_context;
2014 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2016 tree copy = copy_node (decl), block;
2018 lang_hooks.dup_lang_specific_decl (copy);
2019 SET_DECL_RTL (copy, 0);
2020 TREE_USED (copy) = 1;
2021 block = DECL_INITIAL (current_function_decl);
2022 DECL_CHAIN (copy) = BLOCK_VARS (block);
2023 BLOCK_VARS (block) = copy;
2024 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2025 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2029 *expr_p = unshare_expr (value_expr);
2030 return GS_OK;
2033 return GS_ALL_DONE;
2036 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2037 node *EXPR_P.
2039 compound_lval
2040 : min_lval '[' val ']'
2041 | min_lval '.' ID
2042 | compound_lval '[' val ']'
2043 | compound_lval '.' ID
2045 This is not part of the original SIMPLE definition, which separates
2046 array and member references, but it seems reasonable to handle them
2047 together. Also, this way we don't run into problems with union
2048 aliasing; gcc requires that for accesses through a union to alias, the
2049 union reference must be explicit, which was not always the case when we
2050 were splitting up array and member refs.
2052 PRE_P points to the sequence where side effects that must happen before
2053 *EXPR_P should be stored.
2055 POST_P points to the sequence where side effects that must happen after
2056 *EXPR_P should be stored. */
2058 static enum gimplify_status
2059 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2060 fallback_t fallback)
2062 tree *p;
2063 VEC(tree,heap) *stack;
2064 enum gimplify_status ret = GS_ALL_DONE, tret;
2065 int i;
2066 location_t loc = EXPR_LOCATION (*expr_p);
2067 tree expr = *expr_p;
2069 /* Create a stack of the subexpressions so later we can walk them in
2070 order from inner to outer. */
2071 stack = VEC_alloc (tree, heap, 10);
2073 /* We can handle anything that get_inner_reference can deal with. */
2074 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2076 restart:
2077 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2078 if (TREE_CODE (*p) == INDIRECT_REF)
2079 *p = fold_indirect_ref_loc (loc, *p);
2081 if (handled_component_p (*p))
2083 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2084 additional COMPONENT_REFs. */
2085 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2086 && gimplify_var_or_parm_decl (p) == GS_OK)
2087 goto restart;
2088 else
2089 break;
2091 VEC_safe_push (tree, heap, stack, *p);
2094 gcc_assert (VEC_length (tree, stack));
2096 /* Now STACK is a stack of pointers to all the refs we've walked through
2097 and P points to the innermost expression.
2099 Java requires that we elaborated nodes in source order. That
2100 means we must gimplify the inner expression followed by each of
2101 the indices, in order. But we can't gimplify the inner
2102 expression until we deal with any variable bounds, sizes, or
2103 positions in order to deal with PLACEHOLDER_EXPRs.
2105 So we do this in three steps. First we deal with the annotations
2106 for any variables in the components, then we gimplify the base,
2107 then we gimplify any indices, from left to right. */
2108 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
2110 tree t = VEC_index (tree, stack, i);
2112 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2114 /* Gimplify the low bound and element type size and put them into
2115 the ARRAY_REF. If these values are set, they have already been
2116 gimplified. */
2117 if (TREE_OPERAND (t, 2) == NULL_TREE)
2119 tree low = unshare_expr (array_ref_low_bound (t));
2120 if (!is_gimple_min_invariant (low))
2122 TREE_OPERAND (t, 2) = low;
2123 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2124 post_p, is_gimple_reg,
2125 fb_rvalue);
2126 ret = MIN (ret, tret);
2129 else
2131 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2132 is_gimple_reg, fb_rvalue);
2133 ret = MIN (ret, tret);
2136 if (TREE_OPERAND (t, 3) == NULL_TREE)
2138 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2139 /* FIXME google - In some edge cases, ELMT_TYPE may
2140 not have been laid out. This causes an ICE later
2141 (PR 55245). We call layout_type if ELMT_TYPE is not
2142 yet complete, but this is not where this bug should
2143 be fixed. The FE should have laid out all the types
2144 before gimplification. When a proper fix for PR
2145 55245 is available, remove this. */
2146 if (!COMPLETE_TYPE_P (elmt_type))
2147 layout_type (elmt_type);
2148 tree elmt_size = unshare_expr (array_ref_element_size (t));
2149 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2151 /* Divide the element size by the alignment of the element
2152 type (above). */
2153 elmt_size
2154 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2156 if (!is_gimple_min_invariant (elmt_size))
2158 TREE_OPERAND (t, 3) = elmt_size;
2159 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2160 post_p, is_gimple_reg,
2161 fb_rvalue);
2162 ret = MIN (ret, tret);
2165 else
2167 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2168 is_gimple_reg, fb_rvalue);
2169 ret = MIN (ret, tret);
2172 else if (TREE_CODE (t) == COMPONENT_REF)
2174 /* Set the field offset into T and gimplify it. */
2175 if (TREE_OPERAND (t, 2) == NULL_TREE)
2177 tree offset = unshare_expr (component_ref_field_offset (t));
2178 tree field = TREE_OPERAND (t, 1);
2179 tree factor
2180 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2182 /* Divide the offset by its alignment. */
2183 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2185 if (!is_gimple_min_invariant (offset))
2187 TREE_OPERAND (t, 2) = offset;
2188 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2189 post_p, is_gimple_reg,
2190 fb_rvalue);
2191 ret = MIN (ret, tret);
2194 else
2196 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2197 is_gimple_reg, fb_rvalue);
2198 ret = MIN (ret, tret);
2203 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2204 so as to match the min_lval predicate. Failure to do so may result
2205 in the creation of large aggregate temporaries. */
2206 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2207 fallback | fb_lvalue);
2208 ret = MIN (ret, tret);
2210 /* And finally, the indices and operands to BIT_FIELD_REF. During this
2211 loop we also remove any useless conversions. */
2212 for (; VEC_length (tree, stack) > 0; )
2214 tree t = VEC_pop (tree, stack);
2216 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2218 /* Gimplify the dimension. */
2219 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2221 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2222 is_gimple_val, fb_rvalue);
2223 ret = MIN (ret, tret);
2226 else if (TREE_CODE (t) == BIT_FIELD_REF)
2228 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2229 is_gimple_val, fb_rvalue);
2230 ret = MIN (ret, tret);
2231 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2232 is_gimple_val, fb_rvalue);
2233 ret = MIN (ret, tret);
2236 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2238 /* The innermost expression P may have originally had
2239 TREE_SIDE_EFFECTS set which would have caused all the outer
2240 expressions in *EXPR_P leading to P to also have had
2241 TREE_SIDE_EFFECTS set. */
2242 recalculate_side_effects (t);
2245 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2246 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2248 canonicalize_component_ref (expr_p);
2251 VEC_free (tree, heap, stack);
2253 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2255 return ret;
2258 /* Gimplify the self modifying expression pointed to by EXPR_P
2259 (++, --, +=, -=).
2261 PRE_P points to the list where side effects that must happen before
2262 *EXPR_P should be stored.
2264 POST_P points to the list where side effects that must happen after
2265 *EXPR_P should be stored.
2267 WANT_VALUE is nonzero iff we want to use the value of this expression
2268 in another expression. */
2270 static enum gimplify_status
2271 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2272 bool want_value)
2274 enum tree_code code;
2275 tree lhs, lvalue, rhs, t1;
2276 gimple_seq post = NULL, *orig_post_p = post_p;
2277 bool postfix;
2278 enum tree_code arith_code;
2279 enum gimplify_status ret;
2280 location_t loc = EXPR_LOCATION (*expr_p);
2282 code = TREE_CODE (*expr_p);
2284 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2285 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2287 /* Prefix or postfix? */
2288 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2289 /* Faster to treat as prefix if result is not used. */
2290 postfix = want_value;
2291 else
2292 postfix = false;
2294 /* For postfix, make sure the inner expression's post side effects
2295 are executed after side effects from this expression. */
2296 if (postfix)
2297 post_p = &post;
2299 /* Add or subtract? */
2300 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2301 arith_code = PLUS_EXPR;
2302 else
2303 arith_code = MINUS_EXPR;
2305 /* Gimplify the LHS into a GIMPLE lvalue. */
2306 lvalue = TREE_OPERAND (*expr_p, 0);
2307 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2308 if (ret == GS_ERROR)
2309 return ret;
2311 /* Extract the operands to the arithmetic operation. */
2312 lhs = lvalue;
2313 rhs = TREE_OPERAND (*expr_p, 1);
2315 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2316 that as the result value and in the postqueue operation. We also
2317 make sure to make lvalue a minimal lval, see
2318 gcc.c-torture/execute/20040313-1.c for an example where this matters. */
2319 if (postfix)
2321 if (!is_gimple_min_lval (lvalue))
2323 mark_addressable (lvalue);
2324 lvalue = build_fold_addr_expr_loc (input_location, lvalue);
2325 gimplify_expr (&lvalue, pre_p, post_p, is_gimple_val, fb_rvalue);
2326 lvalue = build_fold_indirect_ref_loc (input_location, lvalue);
2328 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2329 if (ret == GS_ERROR)
2330 return ret;
2333 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2334 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2336 rhs = convert_to_ptrofftype_loc (loc, rhs);
2337 if (arith_code == MINUS_EXPR)
2338 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2339 arith_code = POINTER_PLUS_EXPR;
2342 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
2344 if (postfix)
2346 gimplify_assign (lvalue, t1, orig_post_p);
2347 gimplify_seq_add_seq (orig_post_p, post);
2348 *expr_p = lhs;
2349 return GS_ALL_DONE;
2351 else
2353 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2354 return GS_OK;
2358 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2360 static void
2361 maybe_with_size_expr (tree *expr_p)
2363 tree expr = *expr_p;
2364 tree type = TREE_TYPE (expr);
2365 tree size;
2367 /* If we've already wrapped this or the type is error_mark_node, we can't do
2368 anything. */
2369 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2370 || type == error_mark_node)
2371 return;
2373 /* If the size isn't known or is a constant, we have nothing to do. */
2374 size = TYPE_SIZE_UNIT (type);
2375 if (!size || TREE_CODE (size) == INTEGER_CST)
2376 return;
2378 /* Otherwise, make a WITH_SIZE_EXPR. */
2379 size = unshare_expr (size);
2380 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2381 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2384 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2385 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2386 the CALL_EXPR. */
2388 static enum gimplify_status
2389 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2391 bool (*test) (tree);
2392 fallback_t fb;
2394 /* In general, we allow lvalues for function arguments to avoid
2395 extra overhead of copying large aggregates out of even larger
2396 aggregates into temporaries only to copy the temporaries to
2397 the argument list. Make optimizers happy by pulling out to
2398 temporaries those types that fit in registers. */
2399 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2400 test = is_gimple_val, fb = fb_rvalue;
2401 else
2403 test = is_gimple_lvalue, fb = fb_either;
2404 /* Also strip a TARGET_EXPR that would force an extra copy. */
2405 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2407 tree init = TARGET_EXPR_INITIAL (*arg_p);
2408 if (init
2409 && !VOID_TYPE_P (TREE_TYPE (init)))
2410 *arg_p = init;
2414 /* If this is a variable sized type, we must remember the size. */
2415 maybe_with_size_expr (arg_p);
2417 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2418 /* Make sure arguments have the same location as the function call
2419 itself. */
2420 protected_set_expr_location (*arg_p, call_location);
2422 /* There is a sequence point before a function call. Side effects in
2423 the argument list must occur before the actual call. So, when
2424 gimplifying arguments, force gimplify_expr to use an internal
2425 post queue which is then appended to the end of PRE_P. */
2426 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2429 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2430 WANT_VALUE is true if the result of the call is desired. */
2432 static enum gimplify_status
2433 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2435 tree fndecl, parms, p, fnptrtype;
2436 enum gimplify_status ret;
2437 int i, nargs;
2438 gimple call;
2439 bool builtin_va_start_p = FALSE;
2440 location_t loc = EXPR_LOCATION (*expr_p);
2442 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2444 /* For reliable diagnostics during inlining, it is necessary that
2445 every call_expr be annotated with file and line. */
2446 if (! EXPR_HAS_LOCATION (*expr_p))
2447 SET_EXPR_LOCATION (*expr_p, input_location);
2449 /* This may be a call to a builtin function.
2451 Builtin function calls may be transformed into different
2452 (and more efficient) builtin function calls under certain
2453 circumstances. Unfortunately, gimplification can muck things
2454 up enough that the builtin expanders are not aware that certain
2455 transformations are still valid.
2457 So we attempt transformation/gimplification of the call before
2458 we gimplify the CALL_EXPR. At this time we do not manage to
2459 transform all calls in the same manner as the expanders do, but
2460 we do transform most of them. */
2461 fndecl = get_callee_fndecl (*expr_p);
2462 if (fndecl && DECL_BUILT_IN (fndecl))
2464 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2466 if (new_tree && new_tree != *expr_p)
2468 /* There was a transformation of this call which computes the
2469 same value, but in a more efficient way. Return and try
2470 again. */
2471 *expr_p = new_tree;
2472 return GS_OK;
2475 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2476 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_VA_START)
2478 builtin_va_start_p = TRUE;
2479 if (call_expr_nargs (*expr_p) < 2)
2481 error ("too few arguments to function %<va_start%>");
2482 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2483 return GS_OK;
2486 if (fold_builtin_next_arg (*expr_p, true))
2488 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2489 return GS_OK;
2494 /* Remember the original function pointer type. */
2495 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2497 /* There is a sequence point before the call, so any side effects in
2498 the calling expression must occur before the actual call. Force
2499 gimplify_expr to use an internal post queue. */
2500 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2501 is_gimple_call_addr, fb_rvalue);
2503 nargs = call_expr_nargs (*expr_p);
2505 /* Get argument types for verification. */
2506 fndecl = get_callee_fndecl (*expr_p);
2507 parms = NULL_TREE;
2508 if (fndecl)
2509 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2510 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2511 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2513 if (fndecl && DECL_ARGUMENTS (fndecl))
2514 p = DECL_ARGUMENTS (fndecl);
2515 else if (parms)
2516 p = parms;
2517 else
2518 p = NULL_TREE;
2519 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2522 /* If the last argument is __builtin_va_arg_pack () and it is not
2523 passed as a named argument, decrease the number of CALL_EXPR
2524 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2525 if (!p
2526 && i < nargs
2527 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2529 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2530 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2532 if (last_arg_fndecl
2533 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2534 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2535 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2537 tree call = *expr_p;
2539 --nargs;
2540 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2541 CALL_EXPR_FN (call),
2542 nargs, CALL_EXPR_ARGP (call));
2544 /* Copy all CALL_EXPR flags, location and block, except
2545 CALL_EXPR_VA_ARG_PACK flag. */
2546 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2547 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2548 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2549 = CALL_EXPR_RETURN_SLOT_OPT (call);
2550 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2551 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2553 /* Set CALL_EXPR_VA_ARG_PACK. */
2554 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2558 /* Finally, gimplify the function arguments. */
2559 if (nargs > 0)
2561 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2562 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2563 PUSH_ARGS_REVERSED ? i-- : i++)
2565 enum gimplify_status t;
2567 /* Avoid gimplifying the second argument to va_start, which needs to
2568 be the plain PARM_DECL. */
2569 if ((i != 1) || !builtin_va_start_p)
2571 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2572 EXPR_LOCATION (*expr_p));
2574 if (t == GS_ERROR)
2575 ret = GS_ERROR;
2580 /* Verify the function result. */
2581 if (want_value && fndecl
2582 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2584 error_at (loc, "using result of function returning %<void%>");
2585 ret = GS_ERROR;
2588 /* Try this again in case gimplification exposed something. */
2589 if (ret != GS_ERROR)
2591 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2593 if (new_tree && new_tree != *expr_p)
2595 /* There was a transformation of this call which computes the
2596 same value, but in a more efficient way. Return and try
2597 again. */
2598 *expr_p = new_tree;
2599 return GS_OK;
2602 else
2604 *expr_p = error_mark_node;
2605 return GS_ERROR;
2608 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2609 decl. This allows us to eliminate redundant or useless
2610 calls to "const" functions. */
2611 if (TREE_CODE (*expr_p) == CALL_EXPR)
2613 int flags = call_expr_flags (*expr_p);
2614 if (flags & (ECF_CONST | ECF_PURE)
2615 /* An infinite loop is considered a side effect. */
2616 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2617 TREE_SIDE_EFFECTS (*expr_p) = 0;
2620 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2621 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2622 form and delegate the creation of a GIMPLE_CALL to
2623 gimplify_modify_expr. This is always possible because when
2624 WANT_VALUE is true, the caller wants the result of this call into
2625 a temporary, which means that we will emit an INIT_EXPR in
2626 internal_get_tmp_var which will then be handled by
2627 gimplify_modify_expr. */
2628 if (!want_value)
2630 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2631 have to do is replicate it as a GIMPLE_CALL tuple. */
2632 gimple_stmt_iterator gsi;
2633 call = gimple_build_call_from_tree (*expr_p);
2634 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2635 gimplify_seq_add_stmt (pre_p, call);
2636 gsi = gsi_last (*pre_p);
2637 fold_stmt (&gsi);
2638 *expr_p = NULL_TREE;
2640 else
2641 /* Remember the original function type. */
2642 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2643 CALL_EXPR_FN (*expr_p));
2645 return ret;
2648 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2649 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2651 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2652 condition is true or false, respectively. If null, we should generate
2653 our own to skip over the evaluation of this specific expression.
2655 LOCUS is the source location of the COND_EXPR.
2657 This function is the tree equivalent of do_jump.
2659 shortcut_cond_r should only be called by shortcut_cond_expr. */
2661 static tree
2662 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2663 location_t locus)
2665 tree local_label = NULL_TREE;
2666 tree t, expr = NULL;
2668 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2669 retain the shortcut semantics. Just insert the gotos here;
2670 shortcut_cond_expr will append the real blocks later. */
2671 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2673 location_t new_locus;
2675 /* Turn if (a && b) into
2677 if (a); else goto no;
2678 if (b) goto yes; else goto no;
2679 (no:) */
2681 if (false_label_p == NULL)
2682 false_label_p = &local_label;
2684 /* Keep the original source location on the first 'if'. */
2685 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2686 append_to_statement_list (t, &expr);
2688 /* Set the source location of the && on the second 'if'. */
2689 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2690 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2691 new_locus);
2692 append_to_statement_list (t, &expr);
2694 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2696 location_t new_locus;
2698 /* Turn if (a || b) into
2700 if (a) goto yes;
2701 if (b) goto yes; else goto no;
2702 (yes:) */
2704 if (true_label_p == NULL)
2705 true_label_p = &local_label;
2707 /* Keep the original source location on the first 'if'. */
2708 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2709 append_to_statement_list (t, &expr);
2711 /* Set the source location of the || on the second 'if'. */
2712 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2713 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2714 new_locus);
2715 append_to_statement_list (t, &expr);
2717 else if (TREE_CODE (pred) == COND_EXPR
2718 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2719 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2721 location_t new_locus;
2723 /* As long as we're messing with gotos, turn if (a ? b : c) into
2724 if (a)
2725 if (b) goto yes; else goto no;
2726 else
2727 if (c) goto yes; else goto no;
2729 Don't do this if one of the arms has void type, which can happen
2730 in C++ when the arm is throw. */
2732 /* Keep the original source location on the first 'if'. Set the source
2733 location of the ? on the second 'if'. */
2734 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2735 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2736 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2737 false_label_p, locus),
2738 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2739 false_label_p, new_locus));
2741 else
2743 expr = build3 (COND_EXPR, void_type_node, pred,
2744 build_and_jump (true_label_p),
2745 build_and_jump (false_label_p));
2746 SET_EXPR_LOCATION (expr, locus);
2749 if (local_label)
2751 t = build1 (LABEL_EXPR, void_type_node, local_label);
2752 append_to_statement_list (t, &expr);
2755 return expr;
2758 /* Given a conditional expression EXPR with short-circuit boolean
2759 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2760 predicate appart into the equivalent sequence of conditionals. */
2762 static tree
2763 shortcut_cond_expr (tree expr)
2765 tree pred = TREE_OPERAND (expr, 0);
2766 tree then_ = TREE_OPERAND (expr, 1);
2767 tree else_ = TREE_OPERAND (expr, 2);
2768 tree true_label, false_label, end_label, t;
2769 tree *true_label_p;
2770 tree *false_label_p;
2771 bool emit_end, emit_false, jump_over_else;
2772 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2773 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2775 /* First do simple transformations. */
2776 if (!else_se)
2778 /* If there is no 'else', turn
2779 if (a && b) then c
2780 into
2781 if (a) if (b) then c. */
2782 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2784 /* Keep the original source location on the first 'if'. */
2785 location_t locus = EXPR_LOC_OR_HERE (expr);
2786 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2787 /* Set the source location of the && on the second 'if'. */
2788 if (EXPR_HAS_LOCATION (pred))
2789 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2790 then_ = shortcut_cond_expr (expr);
2791 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2792 pred = TREE_OPERAND (pred, 0);
2793 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2794 SET_EXPR_LOCATION (expr, locus);
2798 if (!then_se)
2800 /* If there is no 'then', turn
2801 if (a || b); else d
2802 into
2803 if (a); else if (b); else d. */
2804 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2806 /* Keep the original source location on the first 'if'. */
2807 location_t locus = EXPR_LOC_OR_HERE (expr);
2808 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2809 /* Set the source location of the || on the second 'if'. */
2810 if (EXPR_HAS_LOCATION (pred))
2811 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2812 else_ = shortcut_cond_expr (expr);
2813 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2814 pred = TREE_OPERAND (pred, 0);
2815 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2816 SET_EXPR_LOCATION (expr, locus);
2820 /* If we're done, great. */
2821 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2822 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2823 return expr;
2825 /* Otherwise we need to mess with gotos. Change
2826 if (a) c; else d;
2828 if (a); else goto no;
2829 c; goto end;
2830 no: d; end:
2831 and recursively gimplify the condition. */
2833 true_label = false_label = end_label = NULL_TREE;
2835 /* If our arms just jump somewhere, hijack those labels so we don't
2836 generate jumps to jumps. */
2838 if (then_
2839 && TREE_CODE (then_) == GOTO_EXPR
2840 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2842 true_label = GOTO_DESTINATION (then_);
2843 then_ = NULL;
2844 then_se = false;
2847 if (else_
2848 && TREE_CODE (else_) == GOTO_EXPR
2849 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2851 false_label = GOTO_DESTINATION (else_);
2852 else_ = NULL;
2853 else_se = false;
2856 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2857 if (true_label)
2858 true_label_p = &true_label;
2859 else
2860 true_label_p = NULL;
2862 /* The 'else' branch also needs a label if it contains interesting code. */
2863 if (false_label || else_se)
2864 false_label_p = &false_label;
2865 else
2866 false_label_p = NULL;
2868 /* If there was nothing else in our arms, just forward the label(s). */
2869 if (!then_se && !else_se)
2870 return shortcut_cond_r (pred, true_label_p, false_label_p,
2871 EXPR_LOC_OR_HERE (expr));
2873 /* If our last subexpression already has a terminal label, reuse it. */
2874 if (else_se)
2875 t = expr_last (else_);
2876 else if (then_se)
2877 t = expr_last (then_);
2878 else
2879 t = NULL;
2880 if (t && TREE_CODE (t) == LABEL_EXPR)
2881 end_label = LABEL_EXPR_LABEL (t);
2883 /* If we don't care about jumping to the 'else' branch, jump to the end
2884 if the condition is false. */
2885 if (!false_label_p)
2886 false_label_p = &end_label;
2888 /* We only want to emit these labels if we aren't hijacking them. */
2889 emit_end = (end_label == NULL_TREE);
2890 emit_false = (false_label == NULL_TREE);
2892 /* We only emit the jump over the else clause if we have to--if the
2893 then clause may fall through. Otherwise we can wind up with a
2894 useless jump and a useless label at the end of gimplified code,
2895 which will cause us to think that this conditional as a whole
2896 falls through even if it doesn't. If we then inline a function
2897 which ends with such a condition, that can cause us to issue an
2898 inappropriate warning about control reaching the end of a
2899 non-void function. */
2900 jump_over_else = block_may_fallthru (then_);
2902 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2903 EXPR_LOC_OR_HERE (expr));
2905 expr = NULL;
2906 append_to_statement_list (pred, &expr);
2908 append_to_statement_list (then_, &expr);
2909 if (else_se)
2911 if (jump_over_else)
2913 tree last = expr_last (expr);
2914 t = build_and_jump (&end_label);
2915 if (EXPR_HAS_LOCATION (last))
2916 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2917 append_to_statement_list (t, &expr);
2919 if (emit_false)
2921 t = build1 (LABEL_EXPR, void_type_node, false_label);
2922 append_to_statement_list (t, &expr);
2924 append_to_statement_list (else_, &expr);
2926 if (emit_end && end_label)
2928 t = build1 (LABEL_EXPR, void_type_node, end_label);
2929 append_to_statement_list (t, &expr);
2932 return expr;
2935 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2937 tree
2938 gimple_boolify (tree expr)
2940 tree type = TREE_TYPE (expr);
2941 location_t loc = EXPR_LOCATION (expr);
2943 if (TREE_CODE (expr) == NE_EXPR
2944 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2945 && integer_zerop (TREE_OPERAND (expr, 1)))
2947 tree call = TREE_OPERAND (expr, 0);
2948 tree fn = get_callee_fndecl (call);
2950 /* For __builtin_expect ((long) (x), y) recurse into x as well
2951 if x is truth_value_p. */
2952 if (fn
2953 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2954 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2955 && call_expr_nargs (call) == 2)
2957 tree arg = CALL_EXPR_ARG (call, 0);
2958 if (arg)
2960 if (TREE_CODE (arg) == NOP_EXPR
2961 && TREE_TYPE (arg) == TREE_TYPE (call))
2962 arg = TREE_OPERAND (arg, 0);
2963 if (truth_value_p (TREE_CODE (arg)))
2965 arg = gimple_boolify (arg);
2966 CALL_EXPR_ARG (call, 0)
2967 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2973 switch (TREE_CODE (expr))
2975 case TRUTH_AND_EXPR:
2976 case TRUTH_OR_EXPR:
2977 case TRUTH_XOR_EXPR:
2978 case TRUTH_ANDIF_EXPR:
2979 case TRUTH_ORIF_EXPR:
2980 /* Also boolify the arguments of truth exprs. */
2981 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2982 /* FALLTHRU */
2984 case TRUTH_NOT_EXPR:
2985 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2987 /* These expressions always produce boolean results. */
2988 if (TREE_CODE (type) != BOOLEAN_TYPE)
2989 TREE_TYPE (expr) = boolean_type_node;
2990 return expr;
2992 default:
2993 if (COMPARISON_CLASS_P (expr))
2995 /* There expressions always prduce boolean results. */
2996 if (TREE_CODE (type) != BOOLEAN_TYPE)
2997 TREE_TYPE (expr) = boolean_type_node;
2998 return expr;
3000 /* Other expressions that get here must have boolean values, but
3001 might need to be converted to the appropriate mode. */
3002 if (TREE_CODE (type) == BOOLEAN_TYPE)
3003 return expr;
3004 return fold_convert_loc (loc, boolean_type_node, expr);
3008 /* Given a conditional expression *EXPR_P without side effects, gimplify
3009 its operands. New statements are inserted to PRE_P. */
3011 static enum gimplify_status
3012 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3014 tree expr = *expr_p, cond;
3015 enum gimplify_status ret, tret;
3016 enum tree_code code;
3018 cond = gimple_boolify (COND_EXPR_COND (expr));
3020 /* We need to handle && and || specially, as their gimplification
3021 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3022 code = TREE_CODE (cond);
3023 if (code == TRUTH_ANDIF_EXPR)
3024 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3025 else if (code == TRUTH_ORIF_EXPR)
3026 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3027 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3028 COND_EXPR_COND (*expr_p) = cond;
3030 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3031 is_gimple_val, fb_rvalue);
3032 ret = MIN (ret, tret);
3033 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3034 is_gimple_val, fb_rvalue);
3036 return MIN (ret, tret);
3039 /* Return true if evaluating EXPR could trap.
3040 EXPR is GENERIC, while tree_could_trap_p can be called
3041 only on GIMPLE. */
3043 static bool
3044 generic_expr_could_trap_p (tree expr)
3046 unsigned i, n;
3048 if (!expr || is_gimple_val (expr))
3049 return false;
3051 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3052 return true;
3054 n = TREE_OPERAND_LENGTH (expr);
3055 for (i = 0; i < n; i++)
3056 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3057 return true;
3059 return false;
3062 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3063 into
3065 if (p) if (p)
3066 t1 = a; a;
3067 else or else
3068 t1 = b; b;
3071 The second form is used when *EXPR_P is of type void.
3073 PRE_P points to the list where side effects that must happen before
3074 *EXPR_P should be stored. */
3076 static enum gimplify_status
3077 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3079 tree expr = *expr_p;
3080 tree type = TREE_TYPE (expr);
3081 location_t loc = EXPR_LOCATION (expr);
3082 tree tmp, arm1, arm2;
3083 enum gimplify_status ret;
3084 tree label_true, label_false, label_cont;
3085 bool have_then_clause_p, have_else_clause_p;
3086 gimple gimple_cond;
3087 enum tree_code pred_code;
3088 gimple_seq seq = NULL;
3090 /* If this COND_EXPR has a value, copy the values into a temporary within
3091 the arms. */
3092 if (!VOID_TYPE_P (type))
3094 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3095 tree result;
3097 /* If either an rvalue is ok or we do not require an lvalue, create the
3098 temporary. But we cannot do that if the type is addressable. */
3099 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3100 && !TREE_ADDRESSABLE (type))
3102 if (gimplify_ctxp->allow_rhs_cond_expr
3103 /* If either branch has side effects or could trap, it can't be
3104 evaluated unconditionally. */
3105 && !TREE_SIDE_EFFECTS (then_)
3106 && !generic_expr_could_trap_p (then_)
3107 && !TREE_SIDE_EFFECTS (else_)
3108 && !generic_expr_could_trap_p (else_))
3109 return gimplify_pure_cond_expr (expr_p, pre_p);
3111 tmp = create_tmp_var (type, "iftmp");
3112 result = tmp;
3115 /* Otherwise, only create and copy references to the values. */
3116 else
3118 type = build_pointer_type (type);
3120 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3121 then_ = build_fold_addr_expr_loc (loc, then_);
3123 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3124 else_ = build_fold_addr_expr_loc (loc, else_);
3126 expr
3127 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3129 tmp = create_tmp_var (type, "iftmp");
3130 result = build_simple_mem_ref_loc (loc, tmp);
3133 /* Build the new then clause, `tmp = then_;'. But don't build the
3134 assignment if the value is void; in C++ it can be if it's a throw. */
3135 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3136 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3138 /* Similarly, build the new else clause, `tmp = else_;'. */
3139 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3140 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3142 TREE_TYPE (expr) = void_type_node;
3143 recalculate_side_effects (expr);
3145 /* Move the COND_EXPR to the prequeue. */
3146 gimplify_stmt (&expr, pre_p);
3148 *expr_p = result;
3149 return GS_ALL_DONE;
3152 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3153 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3154 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3155 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3157 /* Make sure the condition has BOOLEAN_TYPE. */
3158 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3160 /* Break apart && and || conditions. */
3161 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3162 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3164 expr = shortcut_cond_expr (expr);
3166 if (expr != *expr_p)
3168 *expr_p = expr;
3170 /* We can't rely on gimplify_expr to re-gimplify the expanded
3171 form properly, as cleanups might cause the target labels to be
3172 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3173 set up a conditional context. */
3174 gimple_push_condition ();
3175 gimplify_stmt (expr_p, &seq);
3176 gimple_pop_condition (pre_p);
3177 gimple_seq_add_seq (pre_p, seq);
3179 return GS_ALL_DONE;
3183 /* Now do the normal gimplification. */
3185 /* Gimplify condition. */
3186 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3187 fb_rvalue);
3188 if (ret == GS_ERROR)
3189 return GS_ERROR;
3190 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3192 gimple_push_condition ();
3194 have_then_clause_p = have_else_clause_p = false;
3195 if (TREE_OPERAND (expr, 1) != NULL
3196 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3197 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3198 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3199 == current_function_decl)
3200 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3201 have different locations, otherwise we end up with incorrect
3202 location information on the branches. */
3203 && (optimize
3204 || !EXPR_HAS_LOCATION (expr)
3205 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3206 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3208 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3209 have_then_clause_p = true;
3211 else
3212 label_true = create_artificial_label (UNKNOWN_LOCATION);
3213 if (TREE_OPERAND (expr, 2) != NULL
3214 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3215 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3216 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3217 == current_function_decl)
3218 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3219 have different locations, otherwise we end up with incorrect
3220 location information on the branches. */
3221 && (optimize
3222 || !EXPR_HAS_LOCATION (expr)
3223 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3224 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3226 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3227 have_else_clause_p = true;
3229 else
3230 label_false = create_artificial_label (UNKNOWN_LOCATION);
3232 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3233 &arm2);
3235 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3236 label_false);
3238 gimplify_seq_add_stmt (&seq, gimple_cond);
3239 label_cont = NULL_TREE;
3240 if (!have_then_clause_p)
3242 /* For if (...) {} else { code; } put label_true after
3243 the else block. */
3244 if (TREE_OPERAND (expr, 1) == NULL_TREE
3245 && !have_else_clause_p
3246 && TREE_OPERAND (expr, 2) != NULL_TREE)
3247 label_cont = label_true;
3248 else
3250 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3251 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3252 /* For if (...) { code; } else {} or
3253 if (...) { code; } else goto label; or
3254 if (...) { code; return; } else { ... }
3255 label_cont isn't needed. */
3256 if (!have_else_clause_p
3257 && TREE_OPERAND (expr, 2) != NULL_TREE
3258 && gimple_seq_may_fallthru (seq))
3260 gimple g;
3261 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3263 g = gimple_build_goto (label_cont);
3265 /* GIMPLE_COND's are very low level; they have embedded
3266 gotos. This particular embedded goto should not be marked
3267 with the location of the original COND_EXPR, as it would
3268 correspond to the COND_EXPR's condition, not the ELSE or the
3269 THEN arms. To avoid marking it with the wrong location, flag
3270 it as "no location". */
3271 gimple_set_do_not_emit_location (g);
3273 gimplify_seq_add_stmt (&seq, g);
3277 if (!have_else_clause_p)
3279 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3280 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3282 if (label_cont)
3283 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3285 gimple_pop_condition (pre_p);
3286 gimple_seq_add_seq (pre_p, seq);
3288 if (ret == GS_ERROR)
3289 ; /* Do nothing. */
3290 else if (have_then_clause_p || have_else_clause_p)
3291 ret = GS_ALL_DONE;
3292 else
3294 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3295 expr = TREE_OPERAND (expr, 0);
3296 gimplify_stmt (&expr, pre_p);
3299 *expr_p = NULL;
3300 return ret;
3303 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3304 to be marked addressable.
3306 We cannot rely on such an expression being directly markable if a temporary
3307 has been created by the gimplification. In this case, we create another
3308 temporary and initialize it with a copy, which will become a store after we
3309 mark it addressable. This can happen if the front-end passed us something
3310 that it could not mark addressable yet, like a Fortran pass-by-reference
3311 parameter (int) floatvar. */
3313 static void
3314 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3316 while (handled_component_p (*expr_p))
3317 expr_p = &TREE_OPERAND (*expr_p, 0);
3318 if (is_gimple_reg (*expr_p))
3319 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3322 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3323 a call to __builtin_memcpy. */
3325 static enum gimplify_status
3326 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3327 gimple_seq *seq_p)
3329 tree t, to, to_ptr, from, from_ptr;
3330 gimple gs;
3331 location_t loc = EXPR_LOCATION (*expr_p);
3333 to = TREE_OPERAND (*expr_p, 0);
3334 from = TREE_OPERAND (*expr_p, 1);
3336 /* Mark the RHS addressable. Beware that it may not be possible to do so
3337 directly if a temporary has been created by the gimplification. */
3338 prepare_gimple_addressable (&from, seq_p);
3340 mark_addressable (from);
3341 from_ptr = build_fold_addr_expr_loc (loc, from);
3342 gimplify_arg (&from_ptr, seq_p, loc);
3344 mark_addressable (to);
3345 to_ptr = build_fold_addr_expr_loc (loc, to);
3346 gimplify_arg (&to_ptr, seq_p, loc);
3348 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3350 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3352 if (want_value)
3354 /* tmp = memcpy() */
3355 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3356 gimple_call_set_lhs (gs, t);
3357 gimplify_seq_add_stmt (seq_p, gs);
3359 *expr_p = build_simple_mem_ref (t);
3360 return GS_ALL_DONE;
3363 gimplify_seq_add_stmt (seq_p, gs);
3364 *expr_p = NULL;
3365 return GS_ALL_DONE;
3368 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3369 a call to __builtin_memset. In this case we know that the RHS is
3370 a CONSTRUCTOR with an empty element list. */
3372 static enum gimplify_status
3373 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3374 gimple_seq *seq_p)
3376 tree t, from, to, to_ptr;
3377 gimple gs;
3378 location_t loc = EXPR_LOCATION (*expr_p);
3380 /* Assert our assumptions, to abort instead of producing wrong code
3381 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3382 not be immediately exposed. */
3383 from = TREE_OPERAND (*expr_p, 1);
3384 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3385 from = TREE_OPERAND (from, 0);
3387 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3388 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (from)));
3390 /* Now proceed. */
3391 to = TREE_OPERAND (*expr_p, 0);
3393 to_ptr = build_fold_addr_expr_loc (loc, to);
3394 gimplify_arg (&to_ptr, seq_p, loc);
3395 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3397 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3399 if (want_value)
3401 /* tmp = memset() */
3402 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3403 gimple_call_set_lhs (gs, t);
3404 gimplify_seq_add_stmt (seq_p, gs);
3406 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3407 return GS_ALL_DONE;
3410 gimplify_seq_add_stmt (seq_p, gs);
3411 *expr_p = NULL;
3412 return GS_ALL_DONE;
3415 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3416 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3417 assignment. Return non-null if we detect a potential overlap. */
3419 struct gimplify_init_ctor_preeval_data
3421 /* The base decl of the lhs object. May be NULL, in which case we
3422 have to assume the lhs is indirect. */
3423 tree lhs_base_decl;
3425 /* The alias set of the lhs object. */
3426 alias_set_type lhs_alias_set;
3429 static tree
3430 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3432 struct gimplify_init_ctor_preeval_data *data
3433 = (struct gimplify_init_ctor_preeval_data *) xdata;
3434 tree t = *tp;
3436 /* If we find the base object, obviously we have overlap. */
3437 if (data->lhs_base_decl == t)
3438 return t;
3440 /* If the constructor component is indirect, determine if we have a
3441 potential overlap with the lhs. The only bits of information we
3442 have to go on at this point are addressability and alias sets. */
3443 if ((INDIRECT_REF_P (t)
3444 || TREE_CODE (t) == MEM_REF)
3445 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3446 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3447 return t;
3449 /* If the constructor component is a call, determine if it can hide a
3450 potential overlap with the lhs through an INDIRECT_REF like above.
3451 ??? Ugh - this is completely broken. In fact this whole analysis
3452 doesn't look conservative. */
3453 if (TREE_CODE (t) == CALL_EXPR)
3455 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3457 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3458 if (POINTER_TYPE_P (TREE_VALUE (type))
3459 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3460 && alias_sets_conflict_p (data->lhs_alias_set,
3461 get_alias_set
3462 (TREE_TYPE (TREE_VALUE (type)))))
3463 return t;
3466 if (IS_TYPE_OR_DECL_P (t))
3467 *walk_subtrees = 0;
3468 return NULL;
3471 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3472 force values that overlap with the lhs (as described by *DATA)
3473 into temporaries. */
3475 static void
3476 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3477 struct gimplify_init_ctor_preeval_data *data)
3479 enum gimplify_status one;
3481 /* If the value is constant, then there's nothing to pre-evaluate. */
3482 if (TREE_CONSTANT (*expr_p))
3484 /* Ensure it does not have side effects, it might contain a reference to
3485 the object we're initializing. */
3486 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3487 return;
3490 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3491 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3492 return;
3494 /* Recurse for nested constructors. */
3495 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3497 unsigned HOST_WIDE_INT ix;
3498 constructor_elt *ce;
3499 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
3501 FOR_EACH_VEC_ELT (constructor_elt, v, ix, ce)
3502 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3504 return;
3507 /* If this is a variable sized type, we must remember the size. */
3508 maybe_with_size_expr (expr_p);
3510 /* Gimplify the constructor element to something appropriate for the rhs
3511 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3512 the gimplifier will consider this a store to memory. Doing this
3513 gimplification now means that we won't have to deal with complicated
3514 language-specific trees, nor trees like SAVE_EXPR that can induce
3515 exponential search behavior. */
3516 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3517 if (one == GS_ERROR)
3519 *expr_p = NULL;
3520 return;
3523 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3524 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3525 always be true for all scalars, since is_gimple_mem_rhs insists on a
3526 temporary variable for them. */
3527 if (DECL_P (*expr_p))
3528 return;
3530 /* If this is of variable size, we have no choice but to assume it doesn't
3531 overlap since we can't make a temporary for it. */
3532 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3533 return;
3535 /* Otherwise, we must search for overlap ... */
3536 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3537 return;
3539 /* ... and if found, force the value into a temporary. */
3540 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3543 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3544 a RANGE_EXPR in a CONSTRUCTOR for an array.
3546 var = lower;
3547 loop_entry:
3548 object[var] = value;
3549 if (var == upper)
3550 goto loop_exit;
3551 var = var + 1;
3552 goto loop_entry;
3553 loop_exit:
3555 We increment var _after_ the loop exit check because we might otherwise
3556 fail if upper == TYPE_MAX_VALUE (type for upper).
3558 Note that we never have to deal with SAVE_EXPRs here, because this has
3559 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3561 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
3562 gimple_seq *, bool);
3564 static void
3565 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3566 tree value, tree array_elt_type,
3567 gimple_seq *pre_p, bool cleared)
3569 tree loop_entry_label, loop_exit_label, fall_thru_label;
3570 tree var, var_type, cref, tmp;
3572 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3573 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3574 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3576 /* Create and initialize the index variable. */
3577 var_type = TREE_TYPE (upper);
3578 var = create_tmp_var (var_type, NULL);
3579 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3581 /* Add the loop entry label. */
3582 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3584 /* Build the reference. */
3585 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3586 var, NULL_TREE, NULL_TREE);
3588 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3589 the store. Otherwise just assign value to the reference. */
3591 if (TREE_CODE (value) == CONSTRUCTOR)
3592 /* NB we might have to call ourself recursively through
3593 gimplify_init_ctor_eval if the value is a constructor. */
3594 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3595 pre_p, cleared);
3596 else
3597 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3599 /* We exit the loop when the index var is equal to the upper bound. */
3600 gimplify_seq_add_stmt (pre_p,
3601 gimple_build_cond (EQ_EXPR, var, upper,
3602 loop_exit_label, fall_thru_label));
3604 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3606 /* Otherwise, increment the index var... */
3607 tmp = build2 (PLUS_EXPR, var_type, var,
3608 fold_convert (var_type, integer_one_node));
3609 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3611 /* ...and jump back to the loop entry. */
3612 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3614 /* Add the loop exit label. */
3615 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3618 /* Return true if FDECL is accessing a field that is zero sized. */
3620 static bool
3621 zero_sized_field_decl (const_tree fdecl)
3623 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3624 && integer_zerop (DECL_SIZE (fdecl)))
3625 return true;
3626 return false;
3629 /* Return true if TYPE is zero sized. */
3631 static bool
3632 zero_sized_type (const_tree type)
3634 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3635 && integer_zerop (TYPE_SIZE (type)))
3636 return true;
3637 return false;
3640 /* A subroutine of gimplify_init_constructor. Generate individual
3641 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3642 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3643 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3644 zeroed first. */
3646 static void
3647 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
3648 gimple_seq *pre_p, bool cleared)
3650 tree array_elt_type = NULL;
3651 unsigned HOST_WIDE_INT ix;
3652 tree purpose, value;
3654 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3655 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3657 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3659 tree cref;
3661 /* NULL values are created above for gimplification errors. */
3662 if (value == NULL)
3663 continue;
3665 if (cleared && initializer_zerop (value))
3666 continue;
3668 /* ??? Here's to hoping the front end fills in all of the indices,
3669 so we don't have to figure out what's missing ourselves. */
3670 gcc_assert (purpose);
3672 /* Skip zero-sized fields, unless value has side-effects. This can
3673 happen with calls to functions returning a zero-sized type, which
3674 we shouldn't discard. As a number of downstream passes don't
3675 expect sets of zero-sized fields, we rely on the gimplification of
3676 the MODIFY_EXPR we make below to drop the assignment statement. */
3677 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3678 continue;
3680 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3681 whole range. */
3682 if (TREE_CODE (purpose) == RANGE_EXPR)
3684 tree lower = TREE_OPERAND (purpose, 0);
3685 tree upper = TREE_OPERAND (purpose, 1);
3687 /* If the lower bound is equal to upper, just treat it as if
3688 upper was the index. */
3689 if (simple_cst_equal (lower, upper))
3690 purpose = upper;
3691 else
3693 gimplify_init_ctor_eval_range (object, lower, upper, value,
3694 array_elt_type, pre_p, cleared);
3695 continue;
3699 if (array_elt_type)
3701 /* Do not use bitsizetype for ARRAY_REF indices. */
3702 if (TYPE_DOMAIN (TREE_TYPE (object)))
3703 purpose
3704 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3705 purpose);
3706 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3707 purpose, NULL_TREE, NULL_TREE);
3709 else
3711 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3712 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3713 unshare_expr (object), purpose, NULL_TREE);
3716 if (TREE_CODE (value) == CONSTRUCTOR
3717 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3718 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3719 pre_p, cleared);
3720 else
3722 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3723 gimplify_and_add (init, pre_p);
3724 ggc_free (init);
3729 /* Return the appropriate RHS predicate for this LHS. */
3731 gimple_predicate
3732 rhs_predicate_for (tree lhs)
3734 if (is_gimple_reg (lhs))
3735 return is_gimple_reg_rhs_or_call;
3736 else
3737 return is_gimple_mem_rhs_or_call;
3740 /* Gimplify a C99 compound literal expression. This just means adding
3741 the DECL_EXPR before the current statement and using its anonymous
3742 decl instead. */
3744 static enum gimplify_status
3745 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p)
3747 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3748 tree decl = DECL_EXPR_DECL (decl_s);
3749 /* Mark the decl as addressable if the compound literal
3750 expression is addressable now, otherwise it is marked too late
3751 after we gimplify the initialization expression. */
3752 if (TREE_ADDRESSABLE (*expr_p))
3753 TREE_ADDRESSABLE (decl) = 1;
3755 /* Preliminarily mark non-addressed complex variables as eligible
3756 for promotion to gimple registers. We'll transform their uses
3757 as we find them. */
3758 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3759 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3760 && !TREE_THIS_VOLATILE (decl)
3761 && !needs_to_live_in_memory (decl))
3762 DECL_GIMPLE_REG_P (decl) = 1;
3764 /* This decl isn't mentioned in the enclosing block, so add it to the
3765 list of temps. FIXME it seems a bit of a kludge to say that
3766 anonymous artificial vars aren't pushed, but everything else is. */
3767 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3768 gimple_add_tmp_var (decl);
3770 gimplify_and_add (decl_s, pre_p);
3771 *expr_p = decl;
3772 return GS_OK;
3775 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3776 return a new CONSTRUCTOR if something changed. */
3778 static tree
3779 optimize_compound_literals_in_ctor (tree orig_ctor)
3781 tree ctor = orig_ctor;
3782 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
3783 unsigned int idx, num = VEC_length (constructor_elt, elts);
3785 for (idx = 0; idx < num; idx++)
3787 tree value = VEC_index (constructor_elt, elts, idx)->value;
3788 tree newval = value;
3789 if (TREE_CODE (value) == CONSTRUCTOR)
3790 newval = optimize_compound_literals_in_ctor (value);
3791 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3793 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3794 tree decl = DECL_EXPR_DECL (decl_s);
3795 tree init = DECL_INITIAL (decl);
3797 if (!TREE_ADDRESSABLE (value)
3798 && !TREE_ADDRESSABLE (decl)
3799 && init
3800 && TREE_CODE (init) == CONSTRUCTOR)
3801 newval = optimize_compound_literals_in_ctor (init);
3803 if (newval == value)
3804 continue;
3806 if (ctor == orig_ctor)
3808 ctor = copy_node (orig_ctor);
3809 CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
3810 elts = CONSTRUCTOR_ELTS (ctor);
3812 VEC_index (constructor_elt, elts, idx)->value = newval;
3814 return ctor;
3817 /* A subroutine of gimplify_modify_expr. Break out elements of a
3818 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3820 Note that we still need to clear any elements that don't have explicit
3821 initializers, so if not all elements are initialized we keep the
3822 original MODIFY_EXPR, we just remove all of the constructor elements.
3824 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3825 GS_ERROR if we would have to create a temporary when gimplifying
3826 this constructor. Otherwise, return GS_OK.
3828 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3830 static enum gimplify_status
3831 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3832 bool want_value, bool notify_temp_creation)
3834 tree object, ctor, type;
3835 enum gimplify_status ret;
3836 VEC(constructor_elt,gc) *elts;
3838 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3840 if (!notify_temp_creation)
3842 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3843 is_gimple_lvalue, fb_lvalue);
3844 if (ret == GS_ERROR)
3845 return ret;
3848 object = TREE_OPERAND (*expr_p, 0);
3849 ctor = TREE_OPERAND (*expr_p, 1) =
3850 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3851 type = TREE_TYPE (ctor);
3852 elts = CONSTRUCTOR_ELTS (ctor);
3853 ret = GS_ALL_DONE;
3855 switch (TREE_CODE (type))
3857 case RECORD_TYPE:
3858 case UNION_TYPE:
3859 case QUAL_UNION_TYPE:
3860 case ARRAY_TYPE:
3862 struct gimplify_init_ctor_preeval_data preeval_data;
3863 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3864 bool cleared, complete_p, valid_const_initializer;
3866 /* Aggregate types must lower constructors to initialization of
3867 individual elements. The exception is that a CONSTRUCTOR node
3868 with no elements indicates zero-initialization of the whole. */
3869 if (VEC_empty (constructor_elt, elts))
3871 if (notify_temp_creation)
3872 return GS_OK;
3873 break;
3876 /* Fetch information about the constructor to direct later processing.
3877 We might want to make static versions of it in various cases, and
3878 can only do so if it known to be a valid constant initializer. */
3879 valid_const_initializer
3880 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3881 &num_ctor_elements, &complete_p);
3883 /* If a const aggregate variable is being initialized, then it
3884 should never be a lose to promote the variable to be static. */
3885 if (valid_const_initializer
3886 && num_nonzero_elements > 1
3887 && TREE_READONLY (object)
3888 && TREE_CODE (object) == VAR_DECL
3889 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3891 if (notify_temp_creation)
3892 return GS_ERROR;
3893 DECL_INITIAL (object) = ctor;
3894 TREE_STATIC (object) = 1;
3895 if (!DECL_NAME (object))
3896 DECL_NAME (object) = create_tmp_var_name ("C");
3897 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3899 /* ??? C++ doesn't automatically append a .<number> to the
3900 assembler name, and even when it does, it looks a FE private
3901 data structures to figure out what that number should be,
3902 which are not set for this variable. I suppose this is
3903 important for local statics for inline functions, which aren't
3904 "local" in the object file sense. So in order to get a unique
3905 TU-local symbol, we must invoke the lhd version now. */
3906 lhd_set_decl_assembler_name (object);
3908 *expr_p = NULL_TREE;
3909 break;
3912 /* If there are "lots" of initialized elements, even discounting
3913 those that are not address constants (and thus *must* be
3914 computed at runtime), then partition the constructor into
3915 constant and non-constant parts. Block copy the constant
3916 parts in, then generate code for the non-constant parts. */
3917 /* TODO. There's code in cp/typeck.c to do this. */
3919 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3920 /* store_constructor will ignore the clearing of variable-sized
3921 objects. Initializers for such objects must explicitly set
3922 every field that needs to be set. */
3923 cleared = false;
3924 else if (!complete_p)
3925 /* If the constructor isn't complete, clear the whole object
3926 beforehand.
3928 ??? This ought not to be needed. For any element not present
3929 in the initializer, we should simply set them to zero. Except
3930 we'd need to *find* the elements that are not present, and that
3931 requires trickery to avoid quadratic compile-time behavior in
3932 large cases or excessive memory use in small cases. */
3933 cleared = true;
3934 else if (num_ctor_elements - num_nonzero_elements
3935 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3936 && num_nonzero_elements < num_ctor_elements / 4)
3937 /* If there are "lots" of zeros, it's more efficient to clear
3938 the memory and then set the nonzero elements. */
3939 cleared = true;
3940 else
3941 cleared = false;
3943 /* If there are "lots" of initialized elements, and all of them
3944 are valid address constants, then the entire initializer can
3945 be dropped to memory, and then memcpy'd out. Don't do this
3946 for sparse arrays, though, as it's more efficient to follow
3947 the standard CONSTRUCTOR behavior of memset followed by
3948 individual element initialization. Also don't do this for small
3949 all-zero initializers (which aren't big enough to merit
3950 clearing), and don't try to make bitwise copies of
3951 TREE_ADDRESSABLE types. */
3952 if (valid_const_initializer
3953 && !(cleared || num_nonzero_elements == 0)
3954 && !TREE_ADDRESSABLE (type))
3956 HOST_WIDE_INT size = int_size_in_bytes (type);
3957 unsigned int align;
3959 /* ??? We can still get unbounded array types, at least
3960 from the C++ front end. This seems wrong, but attempt
3961 to work around it for now. */
3962 if (size < 0)
3964 size = int_size_in_bytes (TREE_TYPE (object));
3965 if (size >= 0)
3966 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3969 /* Find the maximum alignment we can assume for the object. */
3970 /* ??? Make use of DECL_OFFSET_ALIGN. */
3971 if (DECL_P (object))
3972 align = DECL_ALIGN (object);
3973 else
3974 align = TYPE_ALIGN (type);
3976 if (size > 0
3977 && num_nonzero_elements > 1
3978 && !can_move_by_pieces (size, align))
3980 if (notify_temp_creation)
3981 return GS_ERROR;
3983 walk_tree (&ctor, force_labels_r, NULL, NULL);
3984 ctor = tree_output_constant_def (ctor);
3985 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3986 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3987 TREE_OPERAND (*expr_p, 1) = ctor;
3989 /* This is no longer an assignment of a CONSTRUCTOR, but
3990 we still may have processing to do on the LHS. So
3991 pretend we didn't do anything here to let that happen. */
3992 return GS_UNHANDLED;
3996 /* If the target is volatile, we have non-zero elements and more than
3997 one field to assign, initialize the target from a temporary. */
3998 if (TREE_THIS_VOLATILE (object)
3999 && !TREE_ADDRESSABLE (type)
4000 && num_nonzero_elements > 0
4001 && VEC_length (constructor_elt, elts) > 1)
4003 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4004 TREE_OPERAND (*expr_p, 0) = temp;
4005 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4006 *expr_p,
4007 build2 (MODIFY_EXPR, void_type_node,
4008 object, temp));
4009 return GS_OK;
4012 if (notify_temp_creation)
4013 return GS_OK;
4015 /* If there are nonzero elements and if needed, pre-evaluate to capture
4016 elements overlapping with the lhs into temporaries. We must do this
4017 before clearing to fetch the values before they are zeroed-out. */
4018 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4020 preeval_data.lhs_base_decl = get_base_address (object);
4021 if (!DECL_P (preeval_data.lhs_base_decl))
4022 preeval_data.lhs_base_decl = NULL;
4023 preeval_data.lhs_alias_set = get_alias_set (object);
4025 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4026 pre_p, post_p, &preeval_data);
4029 if (cleared)
4031 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4032 Note that we still have to gimplify, in order to handle the
4033 case of variable sized types. Avoid shared tree structures. */
4034 CONSTRUCTOR_ELTS (ctor) = NULL;
4035 TREE_SIDE_EFFECTS (ctor) = 0;
4036 object = unshare_expr (object);
4037 gimplify_stmt (expr_p, pre_p);
4040 /* If we have not block cleared the object, or if there are nonzero
4041 elements in the constructor, add assignments to the individual
4042 scalar fields of the object. */
4043 if (!cleared || num_nonzero_elements > 0)
4044 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4046 *expr_p = NULL_TREE;
4048 break;
4050 case COMPLEX_TYPE:
4052 tree r, i;
4054 if (notify_temp_creation)
4055 return GS_OK;
4057 /* Extract the real and imaginary parts out of the ctor. */
4058 gcc_assert (VEC_length (constructor_elt, elts) == 2);
4059 r = VEC_index (constructor_elt, elts, 0)->value;
4060 i = VEC_index (constructor_elt, elts, 1)->value;
4061 if (r == NULL || i == NULL)
4063 tree zero = build_zero_cst (TREE_TYPE (type));
4064 if (r == NULL)
4065 r = zero;
4066 if (i == NULL)
4067 i = zero;
4070 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4071 represent creation of a complex value. */
4072 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4074 ctor = build_complex (type, r, i);
4075 TREE_OPERAND (*expr_p, 1) = ctor;
4077 else
4079 ctor = build2 (COMPLEX_EXPR, type, r, i);
4080 TREE_OPERAND (*expr_p, 1) = ctor;
4081 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4082 pre_p,
4083 post_p,
4084 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4085 fb_rvalue);
4088 break;
4090 case VECTOR_TYPE:
4092 unsigned HOST_WIDE_INT ix;
4093 constructor_elt *ce;
4095 if (notify_temp_creation)
4096 return GS_OK;
4098 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4099 if (TREE_CONSTANT (ctor))
4101 bool constant_p = true;
4102 tree value;
4104 /* Even when ctor is constant, it might contain non-*_CST
4105 elements, such as addresses or trapping values like
4106 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4107 in VECTOR_CST nodes. */
4108 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4109 if (!CONSTANT_CLASS_P (value))
4111 constant_p = false;
4112 break;
4115 if (constant_p)
4117 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4118 break;
4121 /* Don't reduce an initializer constant even if we can't
4122 make a VECTOR_CST. It won't do anything for us, and it'll
4123 prevent us from representing it as a single constant. */
4124 if (initializer_constant_valid_p (ctor, type))
4125 break;
4127 TREE_CONSTANT (ctor) = 0;
4130 /* Vector types use CONSTRUCTOR all the way through gimple
4131 compilation as a general initializer. */
4132 FOR_EACH_VEC_ELT (constructor_elt, elts, ix, ce)
4134 enum gimplify_status tret;
4135 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4136 fb_rvalue);
4137 if (tret == GS_ERROR)
4138 ret = GS_ERROR;
4140 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4141 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4143 break;
4145 default:
4146 /* So how did we get a CONSTRUCTOR for a scalar type? */
4147 gcc_unreachable ();
4150 if (ret == GS_ERROR)
4151 return GS_ERROR;
4152 else if (want_value)
4154 *expr_p = object;
4155 return GS_OK;
4157 else
4159 /* If we have gimplified both sides of the initializer but have
4160 not emitted an assignment, do so now. */
4161 if (*expr_p)
4163 tree lhs = TREE_OPERAND (*expr_p, 0);
4164 tree rhs = TREE_OPERAND (*expr_p, 1);
4165 gimple init = gimple_build_assign (lhs, rhs);
4166 gimplify_seq_add_stmt (pre_p, init);
4167 *expr_p = NULL;
4170 return GS_ALL_DONE;
4174 /* Given a pointer value OP0, return a simplified version of an
4175 indirection through OP0, or NULL_TREE if no simplification is
4176 possible. Note that the resulting type may be different from
4177 the type pointed to in the sense that it is still compatible
4178 from the langhooks point of view. */
4180 tree
4181 gimple_fold_indirect_ref (tree t)
4183 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4184 tree sub = t;
4185 tree subtype;
4187 STRIP_NOPS (sub);
4188 subtype = TREE_TYPE (sub);
4189 if (!POINTER_TYPE_P (subtype))
4190 return NULL_TREE;
4192 if (TREE_CODE (sub) == ADDR_EXPR)
4194 tree op = TREE_OPERAND (sub, 0);
4195 tree optype = TREE_TYPE (op);
4196 /* *&p => p */
4197 if (useless_type_conversion_p (type, optype))
4198 return op;
4200 /* *(foo *)&fooarray => fooarray[0] */
4201 if (TREE_CODE (optype) == ARRAY_TYPE
4202 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4203 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4205 tree type_domain = TYPE_DOMAIN (optype);
4206 tree min_val = size_zero_node;
4207 if (type_domain && TYPE_MIN_VALUE (type_domain))
4208 min_val = TYPE_MIN_VALUE (type_domain);
4209 if (TREE_CODE (min_val) == INTEGER_CST)
4210 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4212 /* *(foo *)&complexfoo => __real__ complexfoo */
4213 else if (TREE_CODE (optype) == COMPLEX_TYPE
4214 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4215 return fold_build1 (REALPART_EXPR, type, op);
4216 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4217 else if (TREE_CODE (optype) == VECTOR_TYPE
4218 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4220 tree part_width = TYPE_SIZE (type);
4221 tree index = bitsize_int (0);
4222 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4226 /* *(p + CST) -> ... */
4227 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4228 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4230 tree addr = TREE_OPERAND (sub, 0);
4231 tree off = TREE_OPERAND (sub, 1);
4232 tree addrtype;
4234 STRIP_NOPS (addr);
4235 addrtype = TREE_TYPE (addr);
4237 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4238 if (TREE_CODE (addr) == ADDR_EXPR
4239 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4240 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4241 && host_integerp (off, 1))
4243 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4244 tree part_width = TYPE_SIZE (type);
4245 unsigned HOST_WIDE_INT part_widthi
4246 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4247 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4248 tree index = bitsize_int (indexi);
4249 if (offset / part_widthi
4250 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4251 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4252 part_width, index);
4255 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4256 if (TREE_CODE (addr) == ADDR_EXPR
4257 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4258 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4260 tree size = TYPE_SIZE_UNIT (type);
4261 if (tree_int_cst_equal (size, off))
4262 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4265 /* *(p + CST) -> MEM_REF <p, CST>. */
4266 if (TREE_CODE (addr) != ADDR_EXPR
4267 || DECL_P (TREE_OPERAND (addr, 0)))
4268 return fold_build2 (MEM_REF, type,
4269 addr,
4270 build_int_cst_wide (ptype,
4271 TREE_INT_CST_LOW (off),
4272 TREE_INT_CST_HIGH (off)));
4275 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4276 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4277 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4278 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4280 tree type_domain;
4281 tree min_val = size_zero_node;
4282 tree osub = sub;
4283 sub = gimple_fold_indirect_ref (sub);
4284 if (! sub)
4285 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4286 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4287 if (type_domain && TYPE_MIN_VALUE (type_domain))
4288 min_val = TYPE_MIN_VALUE (type_domain);
4289 if (TREE_CODE (min_val) == INTEGER_CST)
4290 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4293 return NULL_TREE;
4296 /* Given a pointer value OP0, return a simplified version of an
4297 indirection through OP0, or NULL_TREE if no simplification is
4298 possible. This may only be applied to a rhs of an expression.
4299 Note that the resulting type may be different from the type pointed
4300 to in the sense that it is still compatible from the langhooks
4301 point of view. */
4303 static tree
4304 gimple_fold_indirect_ref_rhs (tree t)
4306 return gimple_fold_indirect_ref (t);
4309 /* Subroutine of gimplify_modify_expr to do simplifications of
4310 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4311 something changes. */
4313 static enum gimplify_status
4314 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4315 gimple_seq *pre_p, gimple_seq *post_p,
4316 bool want_value)
4318 enum gimplify_status ret = GS_UNHANDLED;
4319 bool changed;
4323 changed = false;
4324 switch (TREE_CODE (*from_p))
4326 case VAR_DECL:
4327 /* If we're assigning from a read-only variable initialized with
4328 a constructor, do the direct assignment from the constructor,
4329 but only if neither source nor target are volatile since this
4330 latter assignment might end up being done on a per-field basis. */
4331 if (DECL_INITIAL (*from_p)
4332 && TREE_READONLY (*from_p)
4333 && !TREE_THIS_VOLATILE (*from_p)
4334 && !TREE_THIS_VOLATILE (*to_p)
4335 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4337 tree old_from = *from_p;
4338 enum gimplify_status subret;
4340 /* Move the constructor into the RHS. */
4341 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4343 /* Let's see if gimplify_init_constructor will need to put
4344 it in memory. */
4345 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4346 false, true);
4347 if (subret == GS_ERROR)
4349 /* If so, revert the change. */
4350 *from_p = old_from;
4352 else
4354 ret = GS_OK;
4355 changed = true;
4358 break;
4359 case INDIRECT_REF:
4361 /* If we have code like
4363 *(const A*)(A*)&x
4365 where the type of "x" is a (possibly cv-qualified variant
4366 of "A"), treat the entire expression as identical to "x".
4367 This kind of code arises in C++ when an object is bound
4368 to a const reference, and if "x" is a TARGET_EXPR we want
4369 to take advantage of the optimization below. */
4370 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4371 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4372 if (t)
4374 if (TREE_THIS_VOLATILE (t) != volatile_p)
4376 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4377 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4378 build_fold_addr_expr (t));
4379 if (REFERENCE_CLASS_P (t))
4380 TREE_THIS_VOLATILE (t) = volatile_p;
4382 *from_p = t;
4383 ret = GS_OK;
4384 changed = true;
4386 break;
4389 case TARGET_EXPR:
4391 /* If we are initializing something from a TARGET_EXPR, strip the
4392 TARGET_EXPR and initialize it directly, if possible. This can't
4393 be done if the initializer is void, since that implies that the
4394 temporary is set in some non-trivial way.
4396 ??? What about code that pulls out the temp and uses it
4397 elsewhere? I think that such code never uses the TARGET_EXPR as
4398 an initializer. If I'm wrong, we'll die because the temp won't
4399 have any RTL. In that case, I guess we'll need to replace
4400 references somehow. */
4401 tree init = TARGET_EXPR_INITIAL (*from_p);
4403 if (init
4404 && !VOID_TYPE_P (TREE_TYPE (init)))
4406 *from_p = init;
4407 ret = GS_OK;
4408 changed = true;
4411 break;
4413 case COMPOUND_EXPR:
4414 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4415 caught. */
4416 gimplify_compound_expr (from_p, pre_p, true);
4417 ret = GS_OK;
4418 changed = true;
4419 break;
4421 case CONSTRUCTOR:
4422 /* If we already made some changes, let the front end have a
4423 crack at this before we break it down. */
4424 if (ret != GS_UNHANDLED)
4425 break;
4426 /* If we're initializing from a CONSTRUCTOR, break this into
4427 individual MODIFY_EXPRs. */
4428 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4429 false);
4431 case COND_EXPR:
4432 /* If we're assigning to a non-register type, push the assignment
4433 down into the branches. This is mandatory for ADDRESSABLE types,
4434 since we cannot generate temporaries for such, but it saves a
4435 copy in other cases as well. */
4436 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4438 /* This code should mirror the code in gimplify_cond_expr. */
4439 enum tree_code code = TREE_CODE (*expr_p);
4440 tree cond = *from_p;
4441 tree result = *to_p;
4443 ret = gimplify_expr (&result, pre_p, post_p,
4444 is_gimple_lvalue, fb_lvalue);
4445 if (ret != GS_ERROR)
4446 ret = GS_OK;
4448 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4449 TREE_OPERAND (cond, 1)
4450 = build2 (code, void_type_node, result,
4451 TREE_OPERAND (cond, 1));
4452 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4453 TREE_OPERAND (cond, 2)
4454 = build2 (code, void_type_node, unshare_expr (result),
4455 TREE_OPERAND (cond, 2));
4457 TREE_TYPE (cond) = void_type_node;
4458 recalculate_side_effects (cond);
4460 if (want_value)
4462 gimplify_and_add (cond, pre_p);
4463 *expr_p = unshare_expr (result);
4465 else
4466 *expr_p = cond;
4467 return ret;
4469 break;
4471 case CALL_EXPR:
4472 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4473 return slot so that we don't generate a temporary. */
4474 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4475 && aggregate_value_p (*from_p, *from_p))
4477 bool use_target;
4479 if (!(rhs_predicate_for (*to_p))(*from_p))
4480 /* If we need a temporary, *to_p isn't accurate. */
4481 use_target = false;
4482 /* It's OK to use the return slot directly unless it's an NRV. */
4483 else if (TREE_CODE (*to_p) == RESULT_DECL
4484 && DECL_NAME (*to_p) == NULL_TREE
4485 && needs_to_live_in_memory (*to_p))
4486 use_target = true;
4487 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4488 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4489 /* Don't force regs into memory. */
4490 use_target = false;
4491 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4492 /* It's OK to use the target directly if it's being
4493 initialized. */
4494 use_target = true;
4495 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4496 /* Always use the target and thus RSO for variable-sized types.
4497 GIMPLE cannot deal with a variable-sized assignment
4498 embedded in a call statement. */
4499 use_target = true;
4500 else if (TREE_CODE (*to_p) != SSA_NAME
4501 && (!is_gimple_variable (*to_p)
4502 || needs_to_live_in_memory (*to_p)))
4503 /* Don't use the original target if it's already addressable;
4504 if its address escapes, and the called function uses the
4505 NRV optimization, a conforming program could see *to_p
4506 change before the called function returns; see c++/19317.
4507 When optimizing, the return_slot pass marks more functions
4508 as safe after we have escape info. */
4509 use_target = false;
4510 else
4511 use_target = true;
4513 if (use_target)
4515 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4516 mark_addressable (*to_p);
4519 break;
4521 case WITH_SIZE_EXPR:
4522 /* Likewise for calls that return an aggregate of non-constant size,
4523 since we would not be able to generate a temporary at all. */
4524 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4526 *from_p = TREE_OPERAND (*from_p, 0);
4527 /* We don't change ret in this case because the
4528 WITH_SIZE_EXPR might have been added in
4529 gimplify_modify_expr, so returning GS_OK would lead to an
4530 infinite loop. */
4531 changed = true;
4533 break;
4535 /* If we're initializing from a container, push the initialization
4536 inside it. */
4537 case CLEANUP_POINT_EXPR:
4538 case BIND_EXPR:
4539 case STATEMENT_LIST:
4541 tree wrap = *from_p;
4542 tree t;
4544 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4545 fb_lvalue);
4546 if (ret != GS_ERROR)
4547 ret = GS_OK;
4549 t = voidify_wrapper_expr (wrap, *expr_p);
4550 gcc_assert (t == *expr_p);
4552 if (want_value)
4554 gimplify_and_add (wrap, pre_p);
4555 *expr_p = unshare_expr (*to_p);
4557 else
4558 *expr_p = wrap;
4559 return GS_OK;
4562 case COMPOUND_LITERAL_EXPR:
4564 tree complit = TREE_OPERAND (*expr_p, 1);
4565 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4566 tree decl = DECL_EXPR_DECL (decl_s);
4567 tree init = DECL_INITIAL (decl);
4569 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4570 into struct T x = { 0, 1, 2 } if the address of the
4571 compound literal has never been taken. */
4572 if (!TREE_ADDRESSABLE (complit)
4573 && !TREE_ADDRESSABLE (decl)
4574 && init)
4576 *expr_p = copy_node (*expr_p);
4577 TREE_OPERAND (*expr_p, 1) = init;
4578 return GS_OK;
4582 default:
4583 break;
4586 while (changed);
4588 return ret;
4591 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4592 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4593 DECL_GIMPLE_REG_P set.
4595 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4596 other, unmodified part of the complex object just before the total store.
4597 As a consequence, if the object is still uninitialized, an undefined value
4598 will be loaded into a register, which may result in a spurious exception
4599 if the register is floating-point and the value happens to be a signaling
4600 NaN for example. Then the fully-fledged complex operations lowering pass
4601 followed by a DCE pass are necessary in order to fix things up. */
4603 static enum gimplify_status
4604 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4605 bool want_value)
4607 enum tree_code code, ocode;
4608 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4610 lhs = TREE_OPERAND (*expr_p, 0);
4611 rhs = TREE_OPERAND (*expr_p, 1);
4612 code = TREE_CODE (lhs);
4613 lhs = TREE_OPERAND (lhs, 0);
4615 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4616 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4617 TREE_NO_WARNING (other) = 1;
4618 other = get_formal_tmp_var (other, pre_p);
4620 realpart = code == REALPART_EXPR ? rhs : other;
4621 imagpart = code == REALPART_EXPR ? other : rhs;
4623 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4624 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4625 else
4626 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4628 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4629 *expr_p = (want_value) ? rhs : NULL_TREE;
4631 return GS_ALL_DONE;
4634 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4636 modify_expr
4637 : varname '=' rhs
4638 | '*' ID '=' rhs
4640 PRE_P points to the list where side effects that must happen before
4641 *EXPR_P should be stored.
4643 POST_P points to the list where side effects that must happen after
4644 *EXPR_P should be stored.
4646 WANT_VALUE is nonzero iff we want to use the value of this expression
4647 in another expression. */
4649 static enum gimplify_status
4650 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4651 bool want_value)
4653 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4654 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4655 enum gimplify_status ret = GS_UNHANDLED;
4656 gimple assign;
4657 location_t loc = EXPR_LOCATION (*expr_p);
4659 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4660 || TREE_CODE (*expr_p) == INIT_EXPR);
4662 /* Trying to simplify a clobber using normal logic doesn't work,
4663 so handle it here. */
4664 if (TREE_CLOBBER_P (*from_p))
4666 gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4667 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4668 *expr_p = NULL;
4669 return GS_ALL_DONE;
4672 /* Insert pointer conversions required by the middle-end that are not
4673 required by the frontend. This fixes middle-end type checking for
4674 for example gcc.dg/redecl-6.c. */
4675 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4677 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4678 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4679 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4682 /* See if any simplifications can be done based on what the RHS is. */
4683 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4684 want_value);
4685 if (ret != GS_UNHANDLED)
4686 return ret;
4688 /* For zero sized types only gimplify the left hand side and right hand
4689 side as statements and throw away the assignment. Do this after
4690 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4691 types properly. */
4692 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4694 gimplify_stmt (from_p, pre_p);
4695 gimplify_stmt (to_p, pre_p);
4696 *expr_p = NULL_TREE;
4697 return GS_ALL_DONE;
4700 /* If the value being copied is of variable width, compute the length
4701 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4702 before gimplifying any of the operands so that we can resolve any
4703 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4704 the size of the expression to be copied, not of the destination, so
4705 that is what we must do here. */
4706 maybe_with_size_expr (from_p);
4708 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4709 if (ret == GS_ERROR)
4710 return ret;
4712 /* As a special case, we have to temporarily allow for assignments
4713 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4714 a toplevel statement, when gimplifying the GENERIC expression
4715 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4716 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4718 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4719 prevent gimplify_expr from trying to create a new temporary for
4720 foo's LHS, we tell it that it should only gimplify until it
4721 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4722 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4723 and all we need to do here is set 'a' to be its LHS. */
4724 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4725 fb_rvalue);
4726 if (ret == GS_ERROR)
4727 return ret;
4729 /* Now see if the above changed *from_p to something we handle specially. */
4730 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4731 want_value);
4732 if (ret != GS_UNHANDLED)
4733 return ret;
4735 /* If we've got a variable sized assignment between two lvalues (i.e. does
4736 not involve a call), then we can make things a bit more straightforward
4737 by converting the assignment to memcpy or memset. */
4738 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4740 tree from = TREE_OPERAND (*from_p, 0);
4741 tree size = TREE_OPERAND (*from_p, 1);
4743 if (TREE_CODE (from) == CONSTRUCTOR)
4744 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4746 if (is_gimple_addressable (from))
4748 *from_p = from;
4749 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4750 pre_p);
4754 /* Transform partial stores to non-addressable complex variables into
4755 total stores. This allows us to use real instead of virtual operands
4756 for these variables, which improves optimization. */
4757 if ((TREE_CODE (*to_p) == REALPART_EXPR
4758 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4759 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4760 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4762 /* Try to alleviate the effects of the gimplification creating artificial
4763 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4764 if (!gimplify_ctxp->into_ssa
4765 && TREE_CODE (*from_p) == VAR_DECL
4766 && DECL_IGNORED_P (*from_p)
4767 && DECL_P (*to_p)
4768 && !DECL_IGNORED_P (*to_p))
4770 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4771 DECL_NAME (*from_p)
4772 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4773 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4774 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4777 if (want_value && TREE_THIS_VOLATILE (*to_p))
4778 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4780 if (TREE_CODE (*from_p) == CALL_EXPR)
4782 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4783 instead of a GIMPLE_ASSIGN. */
4784 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4785 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4786 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4787 assign = gimple_build_call_from_tree (*from_p);
4788 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4789 if (!gimple_call_noreturn_p (assign))
4790 gimple_call_set_lhs (assign, *to_p);
4792 else
4794 assign = gimple_build_assign (*to_p, *from_p);
4795 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4798 gimplify_seq_add_stmt (pre_p, assign);
4800 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4802 /* If we've somehow already got an SSA_NAME on the LHS, then
4803 we've probably modified it twice. Not good. */
4804 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
4805 *to_p = make_ssa_name (*to_p, assign);
4806 gimple_set_lhs (assign, *to_p);
4809 if (want_value)
4811 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4812 return GS_OK;
4814 else
4815 *expr_p = NULL;
4817 return GS_ALL_DONE;
4820 /* Gimplify a comparison between two variable-sized objects. Do this
4821 with a call to BUILT_IN_MEMCMP. */
4823 static enum gimplify_status
4824 gimplify_variable_sized_compare (tree *expr_p)
4826 location_t loc = EXPR_LOCATION (*expr_p);
4827 tree op0 = TREE_OPERAND (*expr_p, 0);
4828 tree op1 = TREE_OPERAND (*expr_p, 1);
4829 tree t, arg, dest, src, expr;
4831 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4832 arg = unshare_expr (arg);
4833 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4834 src = build_fold_addr_expr_loc (loc, op1);
4835 dest = build_fold_addr_expr_loc (loc, op0);
4836 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4837 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4839 expr
4840 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4841 SET_EXPR_LOCATION (expr, loc);
4842 *expr_p = expr;
4844 return GS_OK;
4847 /* Gimplify a comparison between two aggregate objects of integral scalar
4848 mode as a comparison between the bitwise equivalent scalar values. */
4850 static enum gimplify_status
4851 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4853 location_t loc = EXPR_LOCATION (*expr_p);
4854 tree op0 = TREE_OPERAND (*expr_p, 0);
4855 tree op1 = TREE_OPERAND (*expr_p, 1);
4857 tree type = TREE_TYPE (op0);
4858 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4860 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4861 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4863 *expr_p
4864 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4866 return GS_OK;
4869 /* Gimplify an expression sequence. This function gimplifies each
4870 expression and rewrites the original expression with the last
4871 expression of the sequence in GIMPLE form.
4873 PRE_P points to the list where the side effects for all the
4874 expressions in the sequence will be emitted.
4876 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4878 static enum gimplify_status
4879 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4881 tree t = *expr_p;
4885 tree *sub_p = &TREE_OPERAND (t, 0);
4887 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4888 gimplify_compound_expr (sub_p, pre_p, false);
4889 else
4890 gimplify_stmt (sub_p, pre_p);
4892 t = TREE_OPERAND (t, 1);
4894 while (TREE_CODE (t) == COMPOUND_EXPR);
4896 *expr_p = t;
4897 if (want_value)
4898 return GS_OK;
4899 else
4901 gimplify_stmt (expr_p, pre_p);
4902 return GS_ALL_DONE;
4906 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4907 gimplify. After gimplification, EXPR_P will point to a new temporary
4908 that holds the original value of the SAVE_EXPR node.
4910 PRE_P points to the list where side effects that must happen before
4911 *EXPR_P should be stored. */
4913 static enum gimplify_status
4914 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4916 enum gimplify_status ret = GS_ALL_DONE;
4917 tree val;
4919 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4920 val = TREE_OPERAND (*expr_p, 0);
4922 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4923 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4925 /* The operand may be a void-valued expression such as SAVE_EXPRs
4926 generated by the Java frontend for class initialization. It is
4927 being executed only for its side-effects. */
4928 if (TREE_TYPE (val) == void_type_node)
4930 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4931 is_gimple_stmt, fb_none);
4932 val = NULL;
4934 else
4935 val = get_initialized_tmp_var (val, pre_p, post_p);
4937 TREE_OPERAND (*expr_p, 0) = val;
4938 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4941 *expr_p = val;
4943 return ret;
4946 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4948 unary_expr
4949 : ...
4950 | '&' varname
4953 PRE_P points to the list where side effects that must happen before
4954 *EXPR_P should be stored.
4956 POST_P points to the list where side effects that must happen after
4957 *EXPR_P should be stored. */
4959 static enum gimplify_status
4960 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4962 tree expr = *expr_p;
4963 tree op0 = TREE_OPERAND (expr, 0);
4964 enum gimplify_status ret;
4965 location_t loc = EXPR_LOCATION (*expr_p);
4967 switch (TREE_CODE (op0))
4969 case INDIRECT_REF:
4970 do_indirect_ref:
4971 /* Check if we are dealing with an expression of the form '&*ptr'.
4972 While the front end folds away '&*ptr' into 'ptr', these
4973 expressions may be generated internally by the compiler (e.g.,
4974 builtins like __builtin_va_end). */
4975 /* Caution: the silent array decomposition semantics we allow for
4976 ADDR_EXPR means we can't always discard the pair. */
4977 /* Gimplification of the ADDR_EXPR operand may drop
4978 cv-qualification conversions, so make sure we add them if
4979 needed. */
4981 tree op00 = TREE_OPERAND (op0, 0);
4982 tree t_expr = TREE_TYPE (expr);
4983 tree t_op00 = TREE_TYPE (op00);
4985 if (!useless_type_conversion_p (t_expr, t_op00))
4986 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4987 *expr_p = op00;
4988 ret = GS_OK;
4990 break;
4992 case VIEW_CONVERT_EXPR:
4993 /* Take the address of our operand and then convert it to the type of
4994 this ADDR_EXPR.
4996 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4997 all clear. The impact of this transformation is even less clear. */
4999 /* If the operand is a useless conversion, look through it. Doing so
5000 guarantees that the ADDR_EXPR and its operand will remain of the
5001 same type. */
5002 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5003 op0 = TREE_OPERAND (op0, 0);
5005 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5006 build_fold_addr_expr_loc (loc,
5007 TREE_OPERAND (op0, 0)));
5008 ret = GS_OK;
5009 break;
5011 default:
5012 /* We use fb_either here because the C frontend sometimes takes
5013 the address of a call that returns a struct; see
5014 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5015 the implied temporary explicit. */
5017 /* Make the operand addressable. */
5018 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5019 is_gimple_addressable, fb_either);
5020 if (ret == GS_ERROR)
5021 break;
5023 /* Then mark it. Beware that it may not be possible to do so directly
5024 if a temporary has been created by the gimplification. */
5025 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5027 op0 = TREE_OPERAND (expr, 0);
5029 /* For various reasons, the gimplification of the expression
5030 may have made a new INDIRECT_REF. */
5031 if (TREE_CODE (op0) == INDIRECT_REF)
5032 goto do_indirect_ref;
5034 mark_addressable (TREE_OPERAND (expr, 0));
5036 /* Fix to PR/41163 (r151122) broke LIPO. Calls to builtin functions
5037 were 'canonicized' in profile-use pass, but not in profile-gen. */
5038 if (!flag_dyn_ipa)
5040 /* The FEs may end up building ADDR_EXPRs early on a decl with
5041 an incomplete type. Re-build ADDR_EXPRs in canonical form
5042 here. */
5043 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5044 *expr_p = build_fold_addr_expr (op0);
5047 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5048 recompute_tree_invariant_for_addr_expr (*expr_p);
5050 /* Fix to PR/41163 (r151122) broke LIPO. Calls to builtin functions
5051 were 'canonicized' in profile-use pass, but not in profile-gen. */
5052 if (!flag_dyn_ipa)
5054 /* If we re-built the ADDR_EXPR add a conversion to the original type
5055 if required. */
5056 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5057 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5060 break;
5063 return ret;
5066 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5067 value; output operands should be a gimple lvalue. */
5069 static enum gimplify_status
5070 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5072 tree expr;
5073 int noutputs;
5074 const char **oconstraints;
5075 int i;
5076 tree link;
5077 const char *constraint;
5078 bool allows_mem, allows_reg, is_inout;
5079 enum gimplify_status ret, tret;
5080 gimple stmt;
5081 VEC(tree, gc) *inputs;
5082 VEC(tree, gc) *outputs;
5083 VEC(tree, gc) *clobbers;
5084 VEC(tree, gc) *labels;
5085 tree link_next;
5087 expr = *expr_p;
5088 noutputs = list_length (ASM_OUTPUTS (expr));
5089 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5091 inputs = outputs = clobbers = labels = NULL;
5093 ret = GS_ALL_DONE;
5094 link_next = NULL_TREE;
5095 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5097 bool ok;
5098 size_t constraint_len;
5100 link_next = TREE_CHAIN (link);
5102 oconstraints[i]
5103 = constraint
5104 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5105 constraint_len = strlen (constraint);
5106 if (constraint_len == 0)
5107 continue;
5109 ok = parse_output_constraint (&constraint, i, 0, 0,
5110 &allows_mem, &allows_reg, &is_inout);
5111 if (!ok)
5113 ret = GS_ERROR;
5114 is_inout = false;
5117 if (!allows_reg && allows_mem)
5118 mark_addressable (TREE_VALUE (link));
5120 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5121 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5122 fb_lvalue | fb_mayfail);
5123 if (tret == GS_ERROR)
5125 error ("invalid lvalue in asm output %d", i);
5126 ret = tret;
5129 VEC_safe_push (tree, gc, outputs, link);
5130 TREE_CHAIN (link) = NULL_TREE;
5132 if (is_inout)
5134 /* An input/output operand. To give the optimizers more
5135 flexibility, split it into separate input and output
5136 operands. */
5137 tree input;
5138 char buf[10];
5140 /* Turn the in/out constraint into an output constraint. */
5141 char *p = xstrdup (constraint);
5142 p[0] = '=';
5143 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5145 /* And add a matching input constraint. */
5146 if (allows_reg)
5148 sprintf (buf, "%d", i);
5150 /* If there are multiple alternatives in the constraint,
5151 handle each of them individually. Those that allow register
5152 will be replaced with operand number, the others will stay
5153 unchanged. */
5154 if (strchr (p, ',') != NULL)
5156 size_t len = 0, buflen = strlen (buf);
5157 char *beg, *end, *str, *dst;
5159 for (beg = p + 1;;)
5161 end = strchr (beg, ',');
5162 if (end == NULL)
5163 end = strchr (beg, '\0');
5164 if ((size_t) (end - beg) < buflen)
5165 len += buflen + 1;
5166 else
5167 len += end - beg + 1;
5168 if (*end)
5169 beg = end + 1;
5170 else
5171 break;
5174 str = (char *) alloca (len);
5175 for (beg = p + 1, dst = str;;)
5177 const char *tem;
5178 bool mem_p, reg_p, inout_p;
5180 end = strchr (beg, ',');
5181 if (end)
5182 *end = '\0';
5183 beg[-1] = '=';
5184 tem = beg - 1;
5185 parse_output_constraint (&tem, i, 0, 0,
5186 &mem_p, &reg_p, &inout_p);
5187 if (dst != str)
5188 *dst++ = ',';
5189 if (reg_p)
5191 memcpy (dst, buf, buflen);
5192 dst += buflen;
5194 else
5196 if (end)
5197 len = end - beg;
5198 else
5199 len = strlen (beg);
5200 memcpy (dst, beg, len);
5201 dst += len;
5203 if (end)
5204 beg = end + 1;
5205 else
5206 break;
5208 *dst = '\0';
5209 input = build_string (dst - str, str);
5211 else
5212 input = build_string (strlen (buf), buf);
5214 else
5215 input = build_string (constraint_len - 1, constraint + 1);
5217 free (p);
5219 input = build_tree_list (build_tree_list (NULL_TREE, input),
5220 unshare_expr (TREE_VALUE (link)));
5221 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5225 link_next = NULL_TREE;
5226 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5228 link_next = TREE_CHAIN (link);
5229 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5230 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5231 oconstraints, &allows_mem, &allows_reg);
5233 /* If we can't make copies, we can only accept memory. */
5234 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5236 if (allows_mem)
5237 allows_reg = 0;
5238 else
5240 error ("impossible constraint in %<asm%>");
5241 error ("non-memory input %d must stay in memory", i);
5242 return GS_ERROR;
5246 /* If the operand is a memory input, it should be an lvalue. */
5247 if (!allows_reg && allows_mem)
5249 tree inputv = TREE_VALUE (link);
5250 STRIP_NOPS (inputv);
5251 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5252 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5253 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5254 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5255 TREE_VALUE (link) = error_mark_node;
5256 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5257 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5258 mark_addressable (TREE_VALUE (link));
5259 if (tret == GS_ERROR)
5261 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5262 input_location = EXPR_LOCATION (TREE_VALUE (link));
5263 error ("memory input %d is not directly addressable", i);
5264 ret = tret;
5267 else
5269 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5270 is_gimple_asm_val, fb_rvalue);
5271 if (tret == GS_ERROR)
5272 ret = tret;
5275 TREE_CHAIN (link) = NULL_TREE;
5276 VEC_safe_push (tree, gc, inputs, link);
5279 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5280 VEC_safe_push (tree, gc, clobbers, link);
5282 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5283 VEC_safe_push (tree, gc, labels, link);
5285 /* Do not add ASMs with errors to the gimple IL stream. */
5286 if (ret != GS_ERROR)
5288 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5289 inputs, outputs, clobbers, labels);
5291 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5292 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5294 gimplify_seq_add_stmt (pre_p, stmt);
5297 return ret;
5300 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5301 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5302 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5303 return to this function.
5305 FIXME should we complexify the prequeue handling instead? Or use flags
5306 for all the cleanups and let the optimizer tighten them up? The current
5307 code seems pretty fragile; it will break on a cleanup within any
5308 non-conditional nesting. But any such nesting would be broken, anyway;
5309 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5310 and continues out of it. We can do that at the RTL level, though, so
5311 having an optimizer to tighten up try/finally regions would be a Good
5312 Thing. */
5314 static enum gimplify_status
5315 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5317 gimple_stmt_iterator iter;
5318 gimple_seq body_sequence = NULL;
5320 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5322 /* We only care about the number of conditions between the innermost
5323 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5324 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5325 int old_conds = gimplify_ctxp->conditions;
5326 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5327 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5328 gimplify_ctxp->conditions = 0;
5329 gimplify_ctxp->conditional_cleanups = NULL;
5330 gimplify_ctxp->in_cleanup_point_expr = true;
5332 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5334 gimplify_ctxp->conditions = old_conds;
5335 gimplify_ctxp->conditional_cleanups = old_cleanups;
5336 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5338 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5340 gimple wce = gsi_stmt (iter);
5342 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5344 if (gsi_one_before_end_p (iter))
5346 /* Note that gsi_insert_seq_before and gsi_remove do not
5347 scan operands, unlike some other sequence mutators. */
5348 if (!gimple_wce_cleanup_eh_only (wce))
5349 gsi_insert_seq_before_without_update (&iter,
5350 gimple_wce_cleanup (wce),
5351 GSI_SAME_STMT);
5352 gsi_remove (&iter, true);
5353 break;
5355 else
5357 gimple gtry;
5358 gimple_seq seq;
5359 enum gimple_try_flags kind;
5361 if (gimple_wce_cleanup_eh_only (wce))
5362 kind = GIMPLE_TRY_CATCH;
5363 else
5364 kind = GIMPLE_TRY_FINALLY;
5365 seq = gsi_split_seq_after (iter);
5367 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5368 /* Do not use gsi_replace here, as it may scan operands.
5369 We want to do a simple structural modification only. */
5370 *gsi_stmt_ptr (&iter) = gtry;
5371 iter = gsi_start (seq);
5374 else
5375 gsi_next (&iter);
5378 gimplify_seq_add_seq (pre_p, body_sequence);
5379 if (temp)
5381 *expr_p = temp;
5382 return GS_OK;
5384 else
5386 *expr_p = NULL;
5387 return GS_ALL_DONE;
5391 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5392 is the cleanup action required. EH_ONLY is true if the cleanup should
5393 only be executed if an exception is thrown, not on normal exit. */
5395 static void
5396 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5398 gimple wce;
5399 gimple_seq cleanup_stmts = NULL;
5401 /* Errors can result in improperly nested cleanups. Which results in
5402 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5403 if (seen_error ())
5404 return;
5406 if (gimple_conditional_context ())
5408 /* If we're in a conditional context, this is more complex. We only
5409 want to run the cleanup if we actually ran the initialization that
5410 necessitates it, but we want to run it after the end of the
5411 conditional context. So we wrap the try/finally around the
5412 condition and use a flag to determine whether or not to actually
5413 run the destructor. Thus
5415 test ? f(A()) : 0
5417 becomes (approximately)
5419 flag = 0;
5420 try {
5421 if (test) { A::A(temp); flag = 1; val = f(temp); }
5422 else { val = 0; }
5423 } finally {
5424 if (flag) A::~A(temp);
5428 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5429 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5430 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5432 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5433 gimplify_stmt (&cleanup, &cleanup_stmts);
5434 wce = gimple_build_wce (cleanup_stmts);
5436 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5437 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5438 gimplify_seq_add_stmt (pre_p, ftrue);
5440 /* Because of this manipulation, and the EH edges that jump
5441 threading cannot redirect, the temporary (VAR) will appear
5442 to be used uninitialized. Don't warn. */
5443 TREE_NO_WARNING (var) = 1;
5445 else
5447 gimplify_stmt (&cleanup, &cleanup_stmts);
5448 wce = gimple_build_wce (cleanup_stmts);
5449 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5450 gimplify_seq_add_stmt (pre_p, wce);
5454 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5456 static enum gimplify_status
5457 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5459 tree targ = *expr_p;
5460 tree temp = TARGET_EXPR_SLOT (targ);
5461 tree init = TARGET_EXPR_INITIAL (targ);
5462 enum gimplify_status ret;
5464 if (init)
5466 tree cleanup = NULL_TREE;
5468 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5469 to the temps list. Handle also variable length TARGET_EXPRs. */
5470 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5472 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5473 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5474 gimplify_vla_decl (temp, pre_p);
5476 else
5477 gimple_add_tmp_var (temp);
5479 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5480 expression is supposed to initialize the slot. */
5481 if (VOID_TYPE_P (TREE_TYPE (init)))
5482 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5483 else
5485 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5486 init = init_expr;
5487 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5488 init = NULL;
5489 ggc_free (init_expr);
5491 if (ret == GS_ERROR)
5493 /* PR c++/28266 Make sure this is expanded only once. */
5494 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5495 return GS_ERROR;
5497 if (init)
5498 gimplify_and_add (init, pre_p);
5500 /* If needed, push the cleanup for the temp. */
5501 if (TARGET_EXPR_CLEANUP (targ))
5503 if (CLEANUP_EH_ONLY (targ))
5504 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5505 CLEANUP_EH_ONLY (targ), pre_p);
5506 else
5507 cleanup = TARGET_EXPR_CLEANUP (targ);
5510 /* Add a clobber for the temporary going out of scope, like
5511 gimplify_bind_expr. */
5512 if (gimplify_ctxp->in_cleanup_point_expr
5513 && needs_to_live_in_memory (temp)
5514 && flag_stack_reuse == SR_ALL)
5516 tree clobber = build_constructor (TREE_TYPE (temp), NULL);
5517 TREE_THIS_VOLATILE (clobber) = true;
5518 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5519 if (cleanup)
5520 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5521 clobber);
5522 else
5523 cleanup = clobber;
5526 if (cleanup)
5527 gimple_push_cleanup (temp, cleanup, false, pre_p);
5529 /* Only expand this once. */
5530 TREE_OPERAND (targ, 3) = init;
5531 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5533 else
5534 /* We should have expanded this before. */
5535 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5537 *expr_p = temp;
5538 return GS_OK;
5541 /* Gimplification of expression trees. */
5543 /* Gimplify an expression which appears at statement context. The
5544 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5545 NULL, a new sequence is allocated.
5547 Return true if we actually added a statement to the queue. */
5549 bool
5550 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5552 gimple_seq_node last;
5554 if (!*seq_p)
5555 *seq_p = gimple_seq_alloc ();
5557 last = gimple_seq_last (*seq_p);
5558 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5559 return last != gimple_seq_last (*seq_p);
5562 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5563 to CTX. If entries already exist, force them to be some flavor of private.
5564 If there is no enclosing parallel, do nothing. */
5566 void
5567 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5569 splay_tree_node n;
5571 if (decl == NULL || !DECL_P (decl))
5572 return;
5576 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5577 if (n != NULL)
5579 if (n->value & GOVD_SHARED)
5580 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5581 else
5582 return;
5584 else if (ctx->region_type != ORT_WORKSHARE)
5585 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5587 ctx = ctx->outer_context;
5589 while (ctx);
5592 /* Similarly for each of the type sizes of TYPE. */
5594 static void
5595 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5597 if (type == NULL || type == error_mark_node)
5598 return;
5599 type = TYPE_MAIN_VARIANT (type);
5601 if (pointer_set_insert (ctx->privatized_types, type))
5602 return;
5604 switch (TREE_CODE (type))
5606 case INTEGER_TYPE:
5607 case ENUMERAL_TYPE:
5608 case BOOLEAN_TYPE:
5609 case REAL_TYPE:
5610 case FIXED_POINT_TYPE:
5611 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5612 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5613 break;
5615 case ARRAY_TYPE:
5616 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5617 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5618 break;
5620 case RECORD_TYPE:
5621 case UNION_TYPE:
5622 case QUAL_UNION_TYPE:
5624 tree field;
5625 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5626 if (TREE_CODE (field) == FIELD_DECL)
5628 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5629 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5632 break;
5634 case POINTER_TYPE:
5635 case REFERENCE_TYPE:
5636 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5637 break;
5639 default:
5640 break;
5643 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5644 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5645 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5648 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5650 static void
5651 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5653 splay_tree_node n;
5654 unsigned int nflags;
5655 tree t;
5657 if (error_operand_p (decl))
5658 return;
5660 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5661 there are constructors involved somewhere. */
5662 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5663 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5664 flags |= GOVD_SEEN;
5666 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5667 if (n != NULL)
5669 /* We shouldn't be re-adding the decl with the same data
5670 sharing class. */
5671 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5672 /* The only combination of data sharing classes we should see is
5673 FIRSTPRIVATE and LASTPRIVATE. */
5674 nflags = n->value | flags;
5675 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5676 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5677 n->value = nflags;
5678 return;
5681 /* When adding a variable-sized variable, we have to handle all sorts
5682 of additional bits of data: the pointer replacement variable, and
5683 the parameters of the type. */
5684 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5686 /* Add the pointer replacement variable as PRIVATE if the variable
5687 replacement is private, else FIRSTPRIVATE since we'll need the
5688 address of the original variable either for SHARED, or for the
5689 copy into or out of the context. */
5690 if (!(flags & GOVD_LOCAL))
5692 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5693 nflags |= flags & GOVD_SEEN;
5694 t = DECL_VALUE_EXPR (decl);
5695 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5696 t = TREE_OPERAND (t, 0);
5697 gcc_assert (DECL_P (t));
5698 omp_add_variable (ctx, t, nflags);
5701 /* Add all of the variable and type parameters (which should have
5702 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5703 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5704 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5705 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5707 /* The variable-sized variable itself is never SHARED, only some form
5708 of PRIVATE. The sharing would take place via the pointer variable
5709 which we remapped above. */
5710 if (flags & GOVD_SHARED)
5711 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5712 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5714 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5715 alloca statement we generate for the variable, so make sure it
5716 is available. This isn't automatically needed for the SHARED
5717 case, since we won't be allocating local storage then.
5718 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5719 in this case omp_notice_variable will be called later
5720 on when it is gimplified. */
5721 else if (! (flags & GOVD_LOCAL)
5722 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5723 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5725 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5727 gcc_assert ((flags & GOVD_LOCAL) == 0);
5728 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5730 /* Similar to the direct variable sized case above, we'll need the
5731 size of references being privatized. */
5732 if ((flags & GOVD_SHARED) == 0)
5734 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5735 if (TREE_CODE (t) != INTEGER_CST)
5736 omp_notice_variable (ctx, t, true);
5740 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5743 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5744 This just prints out diagnostics about threadprivate variable uses
5745 in untied tasks. If DECL2 is non-NULL, prevent this warning
5746 on that variable. */
5748 static bool
5749 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5750 tree decl2)
5752 splay_tree_node n;
5754 if (ctx->region_type != ORT_UNTIED_TASK)
5755 return false;
5756 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5757 if (n == NULL)
5759 error ("threadprivate variable %qE used in untied task",
5760 DECL_NAME (decl));
5761 error_at (ctx->location, "enclosing task");
5762 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5764 if (decl2)
5765 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5766 return false;
5769 /* Record the fact that DECL was used within the OpenMP context CTX.
5770 IN_CODE is true when real code uses DECL, and false when we should
5771 merely emit default(none) errors. Return true if DECL is going to
5772 be remapped and thus DECL shouldn't be gimplified into its
5773 DECL_VALUE_EXPR (if any). */
5775 static bool
5776 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5778 splay_tree_node n;
5779 unsigned flags = in_code ? GOVD_SEEN : 0;
5780 bool ret = false, shared;
5782 if (error_operand_p (decl))
5783 return false;
5785 /* Threadprivate variables are predetermined. */
5786 if (is_global_var (decl))
5788 if (DECL_THREAD_LOCAL_P (decl))
5789 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5791 if (DECL_HAS_VALUE_EXPR_P (decl))
5793 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5795 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5796 return omp_notice_threadprivate_variable (ctx, decl, value);
5800 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5801 if (n == NULL)
5803 enum omp_clause_default_kind default_kind, kind;
5804 struct gimplify_omp_ctx *octx;
5806 if (ctx->region_type == ORT_WORKSHARE)
5807 goto do_outer;
5809 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5810 remapped firstprivate instead of shared. To some extent this is
5811 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5812 default_kind = ctx->default_kind;
5813 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5814 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5815 default_kind = kind;
5817 switch (default_kind)
5819 case OMP_CLAUSE_DEFAULT_NONE:
5820 error ("%qE not specified in enclosing parallel",
5821 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5822 if ((ctx->region_type & ORT_TASK) != 0)
5823 error_at (ctx->location, "enclosing task");
5824 else
5825 error_at (ctx->location, "enclosing parallel");
5826 /* FALLTHRU */
5827 case OMP_CLAUSE_DEFAULT_SHARED:
5828 flags |= GOVD_SHARED;
5829 break;
5830 case OMP_CLAUSE_DEFAULT_PRIVATE:
5831 flags |= GOVD_PRIVATE;
5832 break;
5833 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5834 flags |= GOVD_FIRSTPRIVATE;
5835 break;
5836 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5837 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5838 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5839 if (ctx->outer_context)
5840 omp_notice_variable (ctx->outer_context, decl, in_code);
5841 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5843 splay_tree_node n2;
5845 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5846 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5848 flags |= GOVD_FIRSTPRIVATE;
5849 break;
5851 if ((octx->region_type & ORT_PARALLEL) != 0)
5852 break;
5854 if (flags & GOVD_FIRSTPRIVATE)
5855 break;
5856 if (octx == NULL
5857 && (TREE_CODE (decl) == PARM_DECL
5858 || (!is_global_var (decl)
5859 && DECL_CONTEXT (decl) == current_function_decl)))
5861 flags |= GOVD_FIRSTPRIVATE;
5862 break;
5864 flags |= GOVD_SHARED;
5865 break;
5866 default:
5867 gcc_unreachable ();
5870 if ((flags & GOVD_PRIVATE)
5871 && lang_hooks.decls.omp_private_outer_ref (decl))
5872 flags |= GOVD_PRIVATE_OUTER_REF;
5874 omp_add_variable (ctx, decl, flags);
5876 shared = (flags & GOVD_SHARED) != 0;
5877 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5878 goto do_outer;
5881 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5882 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5883 && DECL_SIZE (decl)
5884 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5886 splay_tree_node n2;
5887 tree t = DECL_VALUE_EXPR (decl);
5888 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5889 t = TREE_OPERAND (t, 0);
5890 gcc_assert (DECL_P (t));
5891 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5892 n2->value |= GOVD_SEEN;
5895 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5896 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5898 /* If nothing changed, there's nothing left to do. */
5899 if ((n->value & flags) == flags)
5900 return ret;
5901 flags |= n->value;
5902 n->value = flags;
5904 do_outer:
5905 /* If the variable is private in the current context, then we don't
5906 need to propagate anything to an outer context. */
5907 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5908 return ret;
5909 if (ctx->outer_context
5910 && omp_notice_variable (ctx->outer_context, decl, in_code))
5911 return true;
5912 return ret;
5915 /* Verify that DECL is private within CTX. If there's specific information
5916 to the contrary in the innermost scope, generate an error. */
5918 static bool
5919 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
5921 splay_tree_node n;
5923 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5924 if (n != NULL)
5926 if (n->value & GOVD_SHARED)
5928 if (ctx == gimplify_omp_ctxp)
5930 error ("iteration variable %qE should be private",
5931 DECL_NAME (decl));
5932 n->value = GOVD_PRIVATE;
5933 return true;
5935 else
5936 return false;
5938 else if ((n->value & GOVD_EXPLICIT) != 0
5939 && (ctx == gimplify_omp_ctxp
5940 || (ctx->region_type == ORT_COMBINED_PARALLEL
5941 && gimplify_omp_ctxp->outer_context == ctx)))
5943 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5944 error ("iteration variable %qE should not be firstprivate",
5945 DECL_NAME (decl));
5946 else if ((n->value & GOVD_REDUCTION) != 0)
5947 error ("iteration variable %qE should not be reduction",
5948 DECL_NAME (decl));
5950 return (ctx == gimplify_omp_ctxp
5951 || (ctx->region_type == ORT_COMBINED_PARALLEL
5952 && gimplify_omp_ctxp->outer_context == ctx));
5955 if (ctx->region_type != ORT_WORKSHARE)
5956 return false;
5957 else if (ctx->outer_context)
5958 return omp_is_private (ctx->outer_context, decl);
5959 return false;
5962 /* Return true if DECL is private within a parallel region
5963 that binds to the current construct's context or in parallel
5964 region's REDUCTION clause. */
5966 static bool
5967 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
5969 splay_tree_node n;
5973 ctx = ctx->outer_context;
5974 if (ctx == NULL)
5975 return !(is_global_var (decl)
5976 /* References might be private, but might be shared too. */
5977 || lang_hooks.decls.omp_privatize_by_reference (decl));
5979 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5980 if (n != NULL)
5981 return (n->value & GOVD_SHARED) == 0;
5983 while (ctx->region_type == ORT_WORKSHARE);
5984 return false;
5987 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5988 and previous omp contexts. */
5990 static void
5991 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5992 enum omp_region_type region_type)
5994 struct gimplify_omp_ctx *ctx, *outer_ctx;
5995 struct gimplify_ctx gctx;
5996 tree c;
5998 ctx = new_omp_context (region_type);
5999 outer_ctx = ctx->outer_context;
6001 while ((c = *list_p) != NULL)
6003 bool remove = false;
6004 bool notice_outer = true;
6005 const char *check_non_private = NULL;
6006 unsigned int flags;
6007 tree decl;
6009 switch (OMP_CLAUSE_CODE (c))
6011 case OMP_CLAUSE_PRIVATE:
6012 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6013 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6015 flags |= GOVD_PRIVATE_OUTER_REF;
6016 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6018 else
6019 notice_outer = false;
6020 goto do_add;
6021 case OMP_CLAUSE_SHARED:
6022 flags = GOVD_SHARED | GOVD_EXPLICIT;
6023 goto do_add;
6024 case OMP_CLAUSE_FIRSTPRIVATE:
6025 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6026 check_non_private = "firstprivate";
6027 goto do_add;
6028 case OMP_CLAUSE_LASTPRIVATE:
6029 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6030 check_non_private = "lastprivate";
6031 goto do_add;
6032 case OMP_CLAUSE_REDUCTION:
6033 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6034 check_non_private = "reduction";
6035 goto do_add;
6037 do_add:
6038 decl = OMP_CLAUSE_DECL (c);
6039 if (error_operand_p (decl))
6041 remove = true;
6042 break;
6044 omp_add_variable (ctx, decl, flags);
6045 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6046 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6048 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6049 GOVD_LOCAL | GOVD_SEEN);
6050 gimplify_omp_ctxp = ctx;
6051 push_gimplify_context (&gctx);
6053 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = gimple_seq_alloc ();
6054 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = gimple_seq_alloc ();
6056 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6057 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6058 pop_gimplify_context
6059 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6060 push_gimplify_context (&gctx);
6061 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6062 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6063 pop_gimplify_context
6064 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6065 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6066 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6068 gimplify_omp_ctxp = outer_ctx;
6070 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6071 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6073 gimplify_omp_ctxp = ctx;
6074 push_gimplify_context (&gctx);
6075 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6077 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6078 NULL, NULL);
6079 TREE_SIDE_EFFECTS (bind) = 1;
6080 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6081 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6083 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6084 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6085 pop_gimplify_context
6086 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6087 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6089 gimplify_omp_ctxp = outer_ctx;
6091 if (notice_outer)
6092 goto do_notice;
6093 break;
6095 case OMP_CLAUSE_COPYIN:
6096 case OMP_CLAUSE_COPYPRIVATE:
6097 decl = OMP_CLAUSE_DECL (c);
6098 if (error_operand_p (decl))
6100 remove = true;
6101 break;
6103 do_notice:
6104 if (outer_ctx)
6105 omp_notice_variable (outer_ctx, decl, true);
6106 if (check_non_private
6107 && region_type == ORT_WORKSHARE
6108 && omp_check_private (ctx, decl))
6110 error ("%s variable %qE is private in outer context",
6111 check_non_private, DECL_NAME (decl));
6112 remove = true;
6114 break;
6116 case OMP_CLAUSE_FINAL:
6117 case OMP_CLAUSE_IF:
6118 OMP_CLAUSE_OPERAND (c, 0)
6119 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6120 /* Fall through. */
6122 case OMP_CLAUSE_SCHEDULE:
6123 case OMP_CLAUSE_NUM_THREADS:
6124 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6125 is_gimple_val, fb_rvalue) == GS_ERROR)
6126 remove = true;
6127 break;
6129 case OMP_CLAUSE_NOWAIT:
6130 case OMP_CLAUSE_ORDERED:
6131 case OMP_CLAUSE_UNTIED:
6132 case OMP_CLAUSE_COLLAPSE:
6133 case OMP_CLAUSE_MERGEABLE:
6134 break;
6136 case OMP_CLAUSE_DEFAULT:
6137 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6138 break;
6140 default:
6141 gcc_unreachable ();
6144 if (remove)
6145 *list_p = OMP_CLAUSE_CHAIN (c);
6146 else
6147 list_p = &OMP_CLAUSE_CHAIN (c);
6150 gimplify_omp_ctxp = ctx;
6153 /* For all variables that were not actually used within the context,
6154 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6156 static int
6157 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6159 tree *list_p = (tree *) data;
6160 tree decl = (tree) n->key;
6161 unsigned flags = n->value;
6162 enum omp_clause_code code;
6163 tree clause;
6164 bool private_debug;
6166 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6167 return 0;
6168 if ((flags & GOVD_SEEN) == 0)
6169 return 0;
6170 if (flags & GOVD_DEBUG_PRIVATE)
6172 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6173 private_debug = true;
6175 else
6176 private_debug
6177 = lang_hooks.decls.omp_private_debug_clause (decl,
6178 !!(flags & GOVD_SHARED));
6179 if (private_debug)
6180 code = OMP_CLAUSE_PRIVATE;
6181 else if (flags & GOVD_SHARED)
6183 if (is_global_var (decl))
6185 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6186 while (ctx != NULL)
6188 splay_tree_node on
6189 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6190 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6191 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6192 break;
6193 ctx = ctx->outer_context;
6195 if (ctx == NULL)
6196 return 0;
6198 code = OMP_CLAUSE_SHARED;
6200 else if (flags & GOVD_PRIVATE)
6201 code = OMP_CLAUSE_PRIVATE;
6202 else if (flags & GOVD_FIRSTPRIVATE)
6203 code = OMP_CLAUSE_FIRSTPRIVATE;
6204 else
6205 gcc_unreachable ();
6207 clause = build_omp_clause (input_location, code);
6208 OMP_CLAUSE_DECL (clause) = decl;
6209 OMP_CLAUSE_CHAIN (clause) = *list_p;
6210 if (private_debug)
6211 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6212 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6213 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6214 *list_p = clause;
6215 lang_hooks.decls.omp_finish_clause (clause);
6217 return 0;
6220 static void
6221 gimplify_adjust_omp_clauses (tree *list_p)
6223 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6224 tree c, decl;
6226 while ((c = *list_p) != NULL)
6228 splay_tree_node n;
6229 bool remove = false;
6231 switch (OMP_CLAUSE_CODE (c))
6233 case OMP_CLAUSE_PRIVATE:
6234 case OMP_CLAUSE_SHARED:
6235 case OMP_CLAUSE_FIRSTPRIVATE:
6236 decl = OMP_CLAUSE_DECL (c);
6237 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6238 remove = !(n->value & GOVD_SEEN);
6239 if (! remove)
6241 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6242 if ((n->value & GOVD_DEBUG_PRIVATE)
6243 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6245 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6246 || ((n->value & GOVD_DATA_SHARE_CLASS)
6247 == GOVD_PRIVATE));
6248 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6249 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6252 break;
6254 case OMP_CLAUSE_LASTPRIVATE:
6255 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6256 accurately reflect the presence of a FIRSTPRIVATE clause. */
6257 decl = OMP_CLAUSE_DECL (c);
6258 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6259 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6260 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6261 break;
6263 case OMP_CLAUSE_REDUCTION:
6264 case OMP_CLAUSE_COPYIN:
6265 case OMP_CLAUSE_COPYPRIVATE:
6266 case OMP_CLAUSE_IF:
6267 case OMP_CLAUSE_NUM_THREADS:
6268 case OMP_CLAUSE_SCHEDULE:
6269 case OMP_CLAUSE_NOWAIT:
6270 case OMP_CLAUSE_ORDERED:
6271 case OMP_CLAUSE_DEFAULT:
6272 case OMP_CLAUSE_UNTIED:
6273 case OMP_CLAUSE_COLLAPSE:
6274 case OMP_CLAUSE_FINAL:
6275 case OMP_CLAUSE_MERGEABLE:
6276 break;
6278 default:
6279 gcc_unreachable ();
6282 if (remove)
6283 *list_p = OMP_CLAUSE_CHAIN (c);
6284 else
6285 list_p = &OMP_CLAUSE_CHAIN (c);
6288 /* Add in any implicit data sharing. */
6289 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6291 gimplify_omp_ctxp = ctx->outer_context;
6292 delete_omp_context (ctx);
6295 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6296 gimplification of the body, as well as scanning the body for used
6297 variables. We need to do this scan now, because variable-sized
6298 decls will be decomposed during gimplification. */
6300 static void
6301 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6303 tree expr = *expr_p;
6304 gimple g;
6305 gimple_seq body = NULL;
6306 struct gimplify_ctx gctx;
6308 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6309 OMP_PARALLEL_COMBINED (expr)
6310 ? ORT_COMBINED_PARALLEL
6311 : ORT_PARALLEL);
6313 push_gimplify_context (&gctx);
6315 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6316 if (gimple_code (g) == GIMPLE_BIND)
6317 pop_gimplify_context (g);
6318 else
6319 pop_gimplify_context (NULL);
6321 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6323 g = gimple_build_omp_parallel (body,
6324 OMP_PARALLEL_CLAUSES (expr),
6325 NULL_TREE, NULL_TREE);
6326 if (OMP_PARALLEL_COMBINED (expr))
6327 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6328 gimplify_seq_add_stmt (pre_p, g);
6329 *expr_p = NULL_TREE;
6332 /* Gimplify the contents of an OMP_TASK statement. This involves
6333 gimplification of the body, as well as scanning the body for used
6334 variables. We need to do this scan now, because variable-sized
6335 decls will be decomposed during gimplification. */
6337 static void
6338 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6340 tree expr = *expr_p;
6341 gimple g;
6342 gimple_seq body = NULL;
6343 struct gimplify_ctx gctx;
6345 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6346 find_omp_clause (OMP_TASK_CLAUSES (expr),
6347 OMP_CLAUSE_UNTIED)
6348 ? ORT_UNTIED_TASK : ORT_TASK);
6350 push_gimplify_context (&gctx);
6352 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6353 if (gimple_code (g) == GIMPLE_BIND)
6354 pop_gimplify_context (g);
6355 else
6356 pop_gimplify_context (NULL);
6358 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6360 g = gimple_build_omp_task (body,
6361 OMP_TASK_CLAUSES (expr),
6362 NULL_TREE, NULL_TREE,
6363 NULL_TREE, NULL_TREE, NULL_TREE);
6364 gimplify_seq_add_stmt (pre_p, g);
6365 *expr_p = NULL_TREE;
6368 /* Gimplify the gross structure of an OMP_FOR statement. */
6370 static enum gimplify_status
6371 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6373 tree for_stmt, decl, var, t;
6374 enum gimplify_status ret = GS_ALL_DONE;
6375 enum gimplify_status tret;
6376 gimple gfor;
6377 gimple_seq for_body, for_pre_body;
6378 int i;
6380 for_stmt = *expr_p;
6382 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6383 ORT_WORKSHARE);
6385 /* Handle OMP_FOR_INIT. */
6386 for_pre_body = NULL;
6387 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6388 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6390 for_body = gimple_seq_alloc ();
6391 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6392 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6393 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6394 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6395 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6397 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6398 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6399 decl = TREE_OPERAND (t, 0);
6400 gcc_assert (DECL_P (decl));
6401 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6402 || POINTER_TYPE_P (TREE_TYPE (decl)));
6404 /* Make sure the iteration variable is private. */
6405 if (omp_is_private (gimplify_omp_ctxp, decl))
6406 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6407 else
6408 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6410 /* If DECL is not a gimple register, create a temporary variable to act
6411 as an iteration counter. This is valid, since DECL cannot be
6412 modified in the body of the loop. */
6413 if (!is_gimple_reg (decl))
6415 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6416 TREE_OPERAND (t, 0) = var;
6418 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6420 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6422 else
6423 var = decl;
6425 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6426 is_gimple_val, fb_rvalue);
6427 ret = MIN (ret, tret);
6428 if (ret == GS_ERROR)
6429 return ret;
6431 /* Handle OMP_FOR_COND. */
6432 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6433 gcc_assert (COMPARISON_CLASS_P (t));
6434 gcc_assert (TREE_OPERAND (t, 0) == decl);
6436 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6437 is_gimple_val, fb_rvalue);
6438 ret = MIN (ret, tret);
6440 /* Handle OMP_FOR_INCR. */
6441 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6442 switch (TREE_CODE (t))
6444 case PREINCREMENT_EXPR:
6445 case POSTINCREMENT_EXPR:
6446 t = build_int_cst (TREE_TYPE (decl), 1);
6447 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6448 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6449 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6450 break;
6452 case PREDECREMENT_EXPR:
6453 case POSTDECREMENT_EXPR:
6454 t = build_int_cst (TREE_TYPE (decl), -1);
6455 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6456 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6457 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6458 break;
6460 case MODIFY_EXPR:
6461 gcc_assert (TREE_OPERAND (t, 0) == decl);
6462 TREE_OPERAND (t, 0) = var;
6464 t = TREE_OPERAND (t, 1);
6465 switch (TREE_CODE (t))
6467 case PLUS_EXPR:
6468 if (TREE_OPERAND (t, 1) == decl)
6470 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6471 TREE_OPERAND (t, 0) = var;
6472 break;
6475 /* Fallthru. */
6476 case MINUS_EXPR:
6477 case POINTER_PLUS_EXPR:
6478 gcc_assert (TREE_OPERAND (t, 0) == decl);
6479 TREE_OPERAND (t, 0) = var;
6480 break;
6481 default:
6482 gcc_unreachable ();
6485 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6486 is_gimple_val, fb_rvalue);
6487 ret = MIN (ret, tret);
6488 break;
6490 default:
6491 gcc_unreachable ();
6494 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6496 tree c;
6497 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6498 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6499 && OMP_CLAUSE_DECL (c) == decl
6500 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6502 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6503 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6504 gcc_assert (TREE_OPERAND (t, 0) == var);
6505 t = TREE_OPERAND (t, 1);
6506 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6507 || TREE_CODE (t) == MINUS_EXPR
6508 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6509 gcc_assert (TREE_OPERAND (t, 0) == var);
6510 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6511 TREE_OPERAND (t, 1));
6512 gimplify_assign (decl, t,
6513 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6518 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6520 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6522 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6523 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6524 for_pre_body);
6526 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6528 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6529 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6530 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6531 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6532 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6533 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6534 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6535 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6538 gimplify_seq_add_stmt (pre_p, gfor);
6539 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6542 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6543 In particular, OMP_SECTIONS and OMP_SINGLE. */
6545 static void
6546 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6548 tree expr = *expr_p;
6549 gimple stmt;
6550 gimple_seq body = NULL;
6552 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6553 gimplify_and_add (OMP_BODY (expr), &body);
6554 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6556 if (TREE_CODE (expr) == OMP_SECTIONS)
6557 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6558 else if (TREE_CODE (expr) == OMP_SINGLE)
6559 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6560 else
6561 gcc_unreachable ();
6563 gimplify_seq_add_stmt (pre_p, stmt);
6566 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6567 stabilized the lhs of the atomic operation as *ADDR. Return true if
6568 EXPR is this stabilized form. */
6570 static bool
6571 goa_lhs_expr_p (tree expr, tree addr)
6573 /* Also include casts to other type variants. The C front end is fond
6574 of adding these for e.g. volatile variables. This is like
6575 STRIP_TYPE_NOPS but includes the main variant lookup. */
6576 STRIP_USELESS_TYPE_CONVERSION (expr);
6578 if (TREE_CODE (expr) == INDIRECT_REF)
6580 expr = TREE_OPERAND (expr, 0);
6581 while (expr != addr
6582 && (CONVERT_EXPR_P (expr)
6583 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6584 && TREE_CODE (expr) == TREE_CODE (addr)
6585 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6587 expr = TREE_OPERAND (expr, 0);
6588 addr = TREE_OPERAND (addr, 0);
6590 if (expr == addr)
6591 return true;
6592 return (TREE_CODE (addr) == ADDR_EXPR
6593 && TREE_CODE (expr) == ADDR_EXPR
6594 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6596 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6597 return true;
6598 return false;
6601 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6602 expression does not involve the lhs, evaluate it into a temporary.
6603 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6604 or -1 if an error was encountered. */
6606 static int
6607 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6608 tree lhs_var)
6610 tree expr = *expr_p;
6611 int saw_lhs;
6613 if (goa_lhs_expr_p (expr, lhs_addr))
6615 *expr_p = lhs_var;
6616 return 1;
6618 if (is_gimple_val (expr))
6619 return 0;
6621 saw_lhs = 0;
6622 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6624 case tcc_binary:
6625 case tcc_comparison:
6626 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6627 lhs_var);
6628 case tcc_unary:
6629 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6630 lhs_var);
6631 break;
6632 case tcc_expression:
6633 switch (TREE_CODE (expr))
6635 case TRUTH_ANDIF_EXPR:
6636 case TRUTH_ORIF_EXPR:
6637 case TRUTH_AND_EXPR:
6638 case TRUTH_OR_EXPR:
6639 case TRUTH_XOR_EXPR:
6640 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6641 lhs_addr, lhs_var);
6642 case TRUTH_NOT_EXPR:
6643 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6644 lhs_addr, lhs_var);
6645 break;
6646 case COMPOUND_EXPR:
6647 /* Break out any preevaluations from cp_build_modify_expr. */
6648 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6649 expr = TREE_OPERAND (expr, 1))
6650 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6651 *expr_p = expr;
6652 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6653 default:
6654 break;
6656 break;
6657 default:
6658 break;
6661 if (saw_lhs == 0)
6663 enum gimplify_status gs;
6664 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6665 if (gs != GS_ALL_DONE)
6666 saw_lhs = -1;
6669 return saw_lhs;
6672 /* Gimplify an OMP_ATOMIC statement. */
6674 static enum gimplify_status
6675 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6677 tree addr = TREE_OPERAND (*expr_p, 0);
6678 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6679 ? NULL : TREE_OPERAND (*expr_p, 1);
6680 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6681 tree tmp_load;
6682 gimple loadstmt, storestmt;
6684 tmp_load = create_tmp_reg (type, NULL);
6685 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6686 return GS_ERROR;
6688 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6689 != GS_ALL_DONE)
6690 return GS_ERROR;
6692 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6693 gimplify_seq_add_stmt (pre_p, loadstmt);
6694 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6695 != GS_ALL_DONE)
6696 return GS_ERROR;
6698 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6699 rhs = tmp_load;
6700 storestmt = gimple_build_omp_atomic_store (rhs);
6701 gimplify_seq_add_stmt (pre_p, storestmt);
6702 switch (TREE_CODE (*expr_p))
6704 case OMP_ATOMIC_READ:
6705 case OMP_ATOMIC_CAPTURE_OLD:
6706 *expr_p = tmp_load;
6707 gimple_omp_atomic_set_need_value (loadstmt);
6708 break;
6709 case OMP_ATOMIC_CAPTURE_NEW:
6710 *expr_p = rhs;
6711 gimple_omp_atomic_set_need_value (storestmt);
6712 break;
6713 default:
6714 *expr_p = NULL;
6715 break;
6718 return GS_ALL_DONE;
6721 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6722 body, and adding some EH bits. */
6724 static enum gimplify_status
6725 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6727 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6728 gimple g;
6729 gimple_seq body = NULL;
6730 struct gimplify_ctx gctx;
6731 int subcode = 0;
6733 /* Wrap the transaction body in a BIND_EXPR so we have a context
6734 where to put decls for OpenMP. */
6735 if (TREE_CODE (tbody) != BIND_EXPR)
6737 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6738 TREE_SIDE_EFFECTS (bind) = 1;
6739 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6740 TRANSACTION_EXPR_BODY (expr) = bind;
6743 push_gimplify_context (&gctx);
6744 temp = voidify_wrapper_expr (*expr_p, NULL);
6746 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6747 pop_gimplify_context (g);
6749 g = gimple_build_transaction (body, NULL);
6750 if (TRANSACTION_EXPR_OUTER (expr))
6751 subcode = GTMA_IS_OUTER;
6752 else if (TRANSACTION_EXPR_RELAXED (expr))
6753 subcode = GTMA_IS_RELAXED;
6754 gimple_transaction_set_subcode (g, subcode);
6756 gimplify_seq_add_stmt (pre_p, g);
6758 if (temp)
6760 *expr_p = temp;
6761 return GS_OK;
6764 *expr_p = NULL_TREE;
6765 return GS_ALL_DONE;
6768 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6769 expression produces a value to be used as an operand inside a GIMPLE
6770 statement, the value will be stored back in *EXPR_P. This value will
6771 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6772 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6773 emitted in PRE_P and POST_P.
6775 Additionally, this process may overwrite parts of the input
6776 expression during gimplification. Ideally, it should be
6777 possible to do non-destructive gimplification.
6779 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6780 the expression needs to evaluate to a value to be used as
6781 an operand in a GIMPLE statement, this value will be stored in
6782 *EXPR_P on exit. This happens when the caller specifies one
6783 of fb_lvalue or fb_rvalue fallback flags.
6785 PRE_P will contain the sequence of GIMPLE statements corresponding
6786 to the evaluation of EXPR and all the side-effects that must
6787 be executed before the main expression. On exit, the last
6788 statement of PRE_P is the core statement being gimplified. For
6789 instance, when gimplifying 'if (++a)' the last statement in
6790 PRE_P will be 'if (t.1)' where t.1 is the result of
6791 pre-incrementing 'a'.
6793 POST_P will contain the sequence of GIMPLE statements corresponding
6794 to the evaluation of all the side-effects that must be executed
6795 after the main expression. If this is NULL, the post
6796 side-effects are stored at the end of PRE_P.
6798 The reason why the output is split in two is to handle post
6799 side-effects explicitly. In some cases, an expression may have
6800 inner and outer post side-effects which need to be emitted in
6801 an order different from the one given by the recursive
6802 traversal. For instance, for the expression (*p--)++ the post
6803 side-effects of '--' must actually occur *after* the post
6804 side-effects of '++'. However, gimplification will first visit
6805 the inner expression, so if a separate POST sequence was not
6806 used, the resulting sequence would be:
6808 1 t.1 = *p
6809 2 p = p - 1
6810 3 t.2 = t.1 + 1
6811 4 *p = t.2
6813 However, the post-decrement operation in line #2 must not be
6814 evaluated until after the store to *p at line #4, so the
6815 correct sequence should be:
6817 1 t.1 = *p
6818 2 t.2 = t.1 + 1
6819 3 *p = t.2
6820 4 p = p - 1
6822 So, by specifying a separate post queue, it is possible
6823 to emit the post side-effects in the correct order.
6824 If POST_P is NULL, an internal queue will be used. Before
6825 returning to the caller, the sequence POST_P is appended to
6826 the main output sequence PRE_P.
6828 GIMPLE_TEST_F points to a function that takes a tree T and
6829 returns nonzero if T is in the GIMPLE form requested by the
6830 caller. The GIMPLE predicates are in gimple.c.
6832 FALLBACK tells the function what sort of a temporary we want if
6833 gimplification cannot produce an expression that complies with
6834 GIMPLE_TEST_F.
6836 fb_none means that no temporary should be generated
6837 fb_rvalue means that an rvalue is OK to generate
6838 fb_lvalue means that an lvalue is OK to generate
6839 fb_either means that either is OK, but an lvalue is preferable.
6840 fb_mayfail means that gimplification may fail (in which case
6841 GS_ERROR will be returned)
6843 The return value is either GS_ERROR or GS_ALL_DONE, since this
6844 function iterates until EXPR is completely gimplified or an error
6845 occurs. */
6847 enum gimplify_status
6848 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6849 bool (*gimple_test_f) (tree), fallback_t fallback)
6851 tree tmp;
6852 gimple_seq internal_pre = NULL;
6853 gimple_seq internal_post = NULL;
6854 tree save_expr;
6855 bool is_statement;
6856 location_t saved_location;
6857 enum gimplify_status ret;
6858 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6860 save_expr = *expr_p;
6861 if (save_expr == NULL_TREE)
6862 return GS_ALL_DONE;
6864 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6865 is_statement = gimple_test_f == is_gimple_stmt;
6866 if (is_statement)
6867 gcc_assert (pre_p);
6869 /* Consistency checks. */
6870 if (gimple_test_f == is_gimple_reg)
6871 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
6872 else if (gimple_test_f == is_gimple_val
6873 || gimple_test_f == is_gimple_call_addr
6874 || gimple_test_f == is_gimple_condexpr
6875 || gimple_test_f == is_gimple_mem_rhs
6876 || gimple_test_f == is_gimple_mem_rhs_or_call
6877 || gimple_test_f == is_gimple_reg_rhs
6878 || gimple_test_f == is_gimple_reg_rhs_or_call
6879 || gimple_test_f == is_gimple_asm_val
6880 || gimple_test_f == is_gimple_mem_ref_addr)
6881 gcc_assert (fallback & fb_rvalue);
6882 else if (gimple_test_f == is_gimple_min_lval
6883 || gimple_test_f == is_gimple_lvalue)
6884 gcc_assert (fallback & fb_lvalue);
6885 else if (gimple_test_f == is_gimple_addressable)
6886 gcc_assert (fallback & fb_either);
6887 else if (gimple_test_f == is_gimple_stmt)
6888 gcc_assert (fallback == fb_none);
6889 else
6891 /* We should have recognized the GIMPLE_TEST_F predicate to
6892 know what kind of fallback to use in case a temporary is
6893 needed to hold the value or address of *EXPR_P. */
6894 gcc_unreachable ();
6897 /* We used to check the predicate here and return immediately if it
6898 succeeds. This is wrong; the design is for gimplification to be
6899 idempotent, and for the predicates to only test for valid forms, not
6900 whether they are fully simplified. */
6901 if (pre_p == NULL)
6902 pre_p = &internal_pre;
6904 if (post_p == NULL)
6905 post_p = &internal_post;
6907 /* Remember the last statements added to PRE_P and POST_P. Every
6908 new statement added by the gimplification helpers needs to be
6909 annotated with location information. To centralize the
6910 responsibility, we remember the last statement that had been
6911 added to both queues before gimplifying *EXPR_P. If
6912 gimplification produces new statements in PRE_P and POST_P, those
6913 statements will be annotated with the same location information
6914 as *EXPR_P. */
6915 pre_last_gsi = gsi_last (*pre_p);
6916 post_last_gsi = gsi_last (*post_p);
6918 saved_location = input_location;
6919 if (save_expr != error_mark_node
6920 && EXPR_HAS_LOCATION (*expr_p))
6921 input_location = EXPR_LOCATION (*expr_p);
6923 /* Loop over the specific gimplifiers until the toplevel node
6924 remains the same. */
6927 /* Strip away as many useless type conversions as possible
6928 at the toplevel. */
6929 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
6931 /* Remember the expr. */
6932 save_expr = *expr_p;
6934 /* Die, die, die, my darling. */
6935 if (save_expr == error_mark_node
6936 || (TREE_TYPE (save_expr)
6937 && TREE_TYPE (save_expr) == error_mark_node))
6939 ret = GS_ERROR;
6940 break;
6943 /* Do any language-specific gimplification. */
6944 ret = ((enum gimplify_status)
6945 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
6946 if (ret == GS_OK)
6948 if (*expr_p == NULL_TREE)
6949 break;
6950 if (*expr_p != save_expr)
6951 continue;
6953 else if (ret != GS_UNHANDLED)
6954 break;
6956 /* Make sure that all the cases set 'ret' appropriately. */
6957 ret = GS_UNHANDLED;
6958 switch (TREE_CODE (*expr_p))
6960 /* First deal with the special cases. */
6962 case POSTINCREMENT_EXPR:
6963 case POSTDECREMENT_EXPR:
6964 case PREINCREMENT_EXPR:
6965 case PREDECREMENT_EXPR:
6966 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
6967 fallback != fb_none);
6968 break;
6970 case ARRAY_REF:
6971 case ARRAY_RANGE_REF:
6972 case REALPART_EXPR:
6973 case IMAGPART_EXPR:
6974 case COMPONENT_REF:
6975 case VIEW_CONVERT_EXPR:
6976 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
6977 fallback ? fallback : fb_rvalue);
6978 break;
6980 case COND_EXPR:
6981 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
6983 /* C99 code may assign to an array in a structure value of a
6984 conditional expression, and this has undefined behavior
6985 only on execution, so create a temporary if an lvalue is
6986 required. */
6987 if (fallback == fb_lvalue)
6989 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6990 mark_addressable (*expr_p);
6991 ret = GS_OK;
6993 break;
6995 case CALL_EXPR:
6996 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
6998 /* C99 code may assign to an array in a structure returned
6999 from a function, and this has undefined behavior only on
7000 execution, so create a temporary if an lvalue is
7001 required. */
7002 if (fallback == fb_lvalue)
7004 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7005 mark_addressable (*expr_p);
7006 ret = GS_OK;
7008 break;
7010 case TREE_LIST:
7011 gcc_unreachable ();
7013 case COMPOUND_EXPR:
7014 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7015 break;
7017 case COMPOUND_LITERAL_EXPR:
7018 ret = gimplify_compound_literal_expr (expr_p, pre_p);
7019 break;
7021 case MODIFY_EXPR:
7022 case INIT_EXPR:
7023 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7024 fallback != fb_none);
7025 break;
7027 case TRUTH_ANDIF_EXPR:
7028 case TRUTH_ORIF_EXPR:
7030 /* Preserve the original type of the expression and the
7031 source location of the outer expression. */
7032 tree org_type = TREE_TYPE (*expr_p);
7033 *expr_p = gimple_boolify (*expr_p);
7034 *expr_p = build3_loc (input_location, COND_EXPR,
7035 org_type, *expr_p,
7036 fold_convert_loc
7037 (input_location,
7038 org_type, boolean_true_node),
7039 fold_convert_loc
7040 (input_location,
7041 org_type, boolean_false_node));
7042 ret = GS_OK;
7043 break;
7046 case TRUTH_NOT_EXPR:
7048 tree type = TREE_TYPE (*expr_p);
7049 /* The parsers are careful to generate TRUTH_NOT_EXPR
7050 only with operands that are always zero or one.
7051 We do not fold here but handle the only interesting case
7052 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7053 *expr_p = gimple_boolify (*expr_p);
7054 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7055 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7056 TREE_TYPE (*expr_p),
7057 TREE_OPERAND (*expr_p, 0));
7058 else
7059 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7060 TREE_TYPE (*expr_p),
7061 TREE_OPERAND (*expr_p, 0),
7062 build_int_cst (TREE_TYPE (*expr_p), 1));
7063 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7064 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7065 ret = GS_OK;
7066 break;
7069 case ADDR_EXPR:
7070 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7071 break;
7073 case VA_ARG_EXPR:
7074 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7075 break;
7077 CASE_CONVERT:
7078 if (IS_EMPTY_STMT (*expr_p))
7080 ret = GS_ALL_DONE;
7081 break;
7084 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7085 || fallback == fb_none)
7087 /* Just strip a conversion to void (or in void context) and
7088 try again. */
7089 *expr_p = TREE_OPERAND (*expr_p, 0);
7090 ret = GS_OK;
7091 break;
7094 ret = gimplify_conversion (expr_p);
7095 if (ret == GS_ERROR)
7096 break;
7097 if (*expr_p != save_expr)
7098 break;
7099 /* FALLTHRU */
7101 case FIX_TRUNC_EXPR:
7102 /* unary_expr: ... | '(' cast ')' val | ... */
7103 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7104 is_gimple_val, fb_rvalue);
7105 recalculate_side_effects (*expr_p);
7106 break;
7108 case INDIRECT_REF:
7110 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7111 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7112 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7114 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7115 if (*expr_p != save_expr)
7117 ret = GS_OK;
7118 break;
7121 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7122 is_gimple_reg, fb_rvalue);
7123 if (ret == GS_ERROR)
7124 break;
7126 recalculate_side_effects (*expr_p);
7127 *expr_p = fold_build2_loc (input_location, MEM_REF,
7128 TREE_TYPE (*expr_p),
7129 TREE_OPERAND (*expr_p, 0),
7130 build_int_cst (saved_ptr_type, 0));
7131 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7132 TREE_THIS_NOTRAP (*expr_p) = notrap;
7133 ret = GS_OK;
7134 break;
7137 /* We arrive here through the various re-gimplifcation paths. */
7138 case MEM_REF:
7139 /* First try re-folding the whole thing. */
7140 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7141 TREE_OPERAND (*expr_p, 0),
7142 TREE_OPERAND (*expr_p, 1));
7143 if (tmp)
7145 *expr_p = tmp;
7146 recalculate_side_effects (*expr_p);
7147 ret = GS_OK;
7148 break;
7150 /* Avoid re-gimplifying the address operand if it is already
7151 in suitable form. Re-gimplifying would mark the address
7152 operand addressable. Always gimplify when not in SSA form
7153 as we still may have to gimplify decls with value-exprs. */
7154 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7155 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7157 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7158 is_gimple_mem_ref_addr, fb_rvalue);
7159 if (ret == GS_ERROR)
7160 break;
7162 recalculate_side_effects (*expr_p);
7163 ret = GS_ALL_DONE;
7164 break;
7166 /* Constants need not be gimplified. */
7167 case INTEGER_CST:
7168 case REAL_CST:
7169 case FIXED_CST:
7170 case STRING_CST:
7171 case COMPLEX_CST:
7172 case VECTOR_CST:
7173 ret = GS_ALL_DONE;
7174 break;
7176 case CONST_DECL:
7177 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7178 CONST_DECL node. Otherwise the decl is replaceable by its
7179 value. */
7180 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7181 if (fallback & fb_lvalue)
7182 ret = GS_ALL_DONE;
7183 else
7185 *expr_p = DECL_INITIAL (*expr_p);
7186 ret = GS_OK;
7188 break;
7190 case DECL_EXPR:
7191 ret = gimplify_decl_expr (expr_p, pre_p);
7192 break;
7194 case BIND_EXPR:
7195 ret = gimplify_bind_expr (expr_p, pre_p);
7196 break;
7198 case LOOP_EXPR:
7199 ret = gimplify_loop_expr (expr_p, pre_p);
7200 break;
7202 case SWITCH_EXPR:
7203 ret = gimplify_switch_expr (expr_p, pre_p);
7204 break;
7206 case EXIT_EXPR:
7207 ret = gimplify_exit_expr (expr_p);
7208 break;
7210 case GOTO_EXPR:
7211 /* If the target is not LABEL, then it is a computed jump
7212 and the target needs to be gimplified. */
7213 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7215 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7216 NULL, is_gimple_val, fb_rvalue);
7217 if (ret == GS_ERROR)
7218 break;
7220 gimplify_seq_add_stmt (pre_p,
7221 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7222 ret = GS_ALL_DONE;
7223 break;
7225 case PREDICT_EXPR:
7226 gimplify_seq_add_stmt (pre_p,
7227 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7228 PREDICT_EXPR_OUTCOME (*expr_p)));
7229 ret = GS_ALL_DONE;
7230 break;
7232 case LABEL_EXPR:
7233 ret = GS_ALL_DONE;
7234 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7235 == current_function_decl);
7236 gimplify_seq_add_stmt (pre_p,
7237 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7238 break;
7240 case CASE_LABEL_EXPR:
7241 ret = gimplify_case_label_expr (expr_p, pre_p);
7242 break;
7244 case RETURN_EXPR:
7245 ret = gimplify_return_expr (*expr_p, pre_p);
7246 break;
7248 case CONSTRUCTOR:
7249 /* Don't reduce this in place; let gimplify_init_constructor work its
7250 magic. Buf if we're just elaborating this for side effects, just
7251 gimplify any element that has side-effects. */
7252 if (fallback == fb_none)
7254 unsigned HOST_WIDE_INT ix;
7255 tree val;
7256 tree temp = NULL_TREE;
7257 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7258 if (TREE_SIDE_EFFECTS (val))
7259 append_to_statement_list (val, &temp);
7261 *expr_p = temp;
7262 ret = temp ? GS_OK : GS_ALL_DONE;
7264 /* C99 code may assign to an array in a constructed
7265 structure or union, and this has undefined behavior only
7266 on execution, so create a temporary if an lvalue is
7267 required. */
7268 else if (fallback == fb_lvalue)
7270 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7271 mark_addressable (*expr_p);
7272 ret = GS_OK;
7274 else
7275 ret = GS_ALL_DONE;
7276 break;
7278 /* The following are special cases that are not handled by the
7279 original GIMPLE grammar. */
7281 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7282 eliminated. */
7283 case SAVE_EXPR:
7284 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7285 break;
7287 case BIT_FIELD_REF:
7289 enum gimplify_status r0, r1, r2;
7291 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7292 post_p, is_gimple_lvalue, fb_either);
7293 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7294 post_p, is_gimple_val, fb_rvalue);
7295 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7296 post_p, is_gimple_val, fb_rvalue);
7297 recalculate_side_effects (*expr_p);
7299 ret = MIN (r0, MIN (r1, r2));
7301 break;
7303 case TARGET_MEM_REF:
7305 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7307 if (TMR_BASE (*expr_p))
7308 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7309 post_p, is_gimple_mem_ref_addr, fb_either);
7310 if (TMR_INDEX (*expr_p))
7311 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7312 post_p, is_gimple_val, fb_rvalue);
7313 if (TMR_INDEX2 (*expr_p))
7314 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7315 post_p, is_gimple_val, fb_rvalue);
7316 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7317 ret = MIN (r0, r1);
7319 break;
7321 case NON_LVALUE_EXPR:
7322 /* This should have been stripped above. */
7323 gcc_unreachable ();
7325 case ASM_EXPR:
7326 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7327 break;
7329 case TRY_FINALLY_EXPR:
7330 case TRY_CATCH_EXPR:
7332 gimple_seq eval, cleanup;
7333 gimple try_;
7335 /* Calls to destructors are generated automatically in FINALLY/CATCH
7336 block. They should have location as UNKNOWN_LOCATION. However,
7337 gimplify_call_expr will reset these call stmts to input_location
7338 if it finds stmt's location is unknown. To prevent resetting for
7339 destructors, we set the input_location to unknown.
7340 Note that this only affects the destructor calls in FINALLY/CATCH
7341 block, and will automatically reset to its original value by the
7342 end of gimplify_expr. */
7343 input_location = UNKNOWN_LOCATION;
7344 eval = cleanup = NULL;
7345 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7346 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7347 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7348 if (gimple_seq_empty_p (cleanup))
7350 gimple_seq_add_seq (pre_p, eval);
7351 ret = GS_ALL_DONE;
7352 break;
7354 try_ = gimple_build_try (eval, cleanup,
7355 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7356 ? GIMPLE_TRY_FINALLY
7357 : GIMPLE_TRY_CATCH);
7358 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7359 gimple_set_location (try_, saved_location);
7360 else
7361 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7362 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7363 gimple_try_set_catch_is_cleanup (try_,
7364 TRY_CATCH_IS_CLEANUP (*expr_p));
7365 gimplify_seq_add_stmt (pre_p, try_);
7366 ret = GS_ALL_DONE;
7367 break;
7370 case CLEANUP_POINT_EXPR:
7371 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7372 break;
7374 case TARGET_EXPR:
7375 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7376 break;
7378 case CATCH_EXPR:
7380 gimple c;
7381 gimple_seq handler = NULL;
7382 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7383 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7384 gimplify_seq_add_stmt (pre_p, c);
7385 ret = GS_ALL_DONE;
7386 break;
7389 case EH_FILTER_EXPR:
7391 gimple ehf;
7392 gimple_seq failure = NULL;
7394 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7395 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7396 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7397 gimplify_seq_add_stmt (pre_p, ehf);
7398 ret = GS_ALL_DONE;
7399 break;
7402 case OBJ_TYPE_REF:
7404 enum gimplify_status r0, r1;
7405 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7406 post_p, is_gimple_val, fb_rvalue);
7407 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7408 post_p, is_gimple_val, fb_rvalue);
7409 TREE_SIDE_EFFECTS (*expr_p) = 0;
7410 ret = MIN (r0, r1);
7412 break;
7414 case LABEL_DECL:
7415 /* We get here when taking the address of a label. We mark
7416 the label as "forced"; meaning it can never be removed and
7417 it is a potential target for any computed goto. */
7418 FORCED_LABEL (*expr_p) = 1;
7419 ret = GS_ALL_DONE;
7420 break;
7422 case STATEMENT_LIST:
7423 ret = gimplify_statement_list (expr_p, pre_p);
7424 break;
7426 case WITH_SIZE_EXPR:
7428 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7429 post_p == &internal_post ? NULL : post_p,
7430 gimple_test_f, fallback);
7431 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7432 is_gimple_val, fb_rvalue);
7433 ret = GS_ALL_DONE;
7435 break;
7437 case VAR_DECL:
7438 case PARM_DECL:
7439 ret = gimplify_var_or_parm_decl (expr_p);
7440 break;
7442 case RESULT_DECL:
7443 /* When within an OpenMP context, notice uses of variables. */
7444 if (gimplify_omp_ctxp)
7445 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7446 ret = GS_ALL_DONE;
7447 break;
7449 case SSA_NAME:
7450 /* Allow callbacks into the gimplifier during optimization. */
7451 ret = GS_ALL_DONE;
7452 break;
7454 case OMP_PARALLEL:
7455 gimplify_omp_parallel (expr_p, pre_p);
7456 ret = GS_ALL_DONE;
7457 break;
7459 case OMP_TASK:
7460 gimplify_omp_task (expr_p, pre_p);
7461 ret = GS_ALL_DONE;
7462 break;
7464 case OMP_FOR:
7465 ret = gimplify_omp_for (expr_p, pre_p);
7466 break;
7468 case OMP_SECTIONS:
7469 case OMP_SINGLE:
7470 gimplify_omp_workshare (expr_p, pre_p);
7471 ret = GS_ALL_DONE;
7472 break;
7474 case OMP_SECTION:
7475 case OMP_MASTER:
7476 case OMP_ORDERED:
7477 case OMP_CRITICAL:
7479 gimple_seq body = NULL;
7480 gimple g;
7482 gimplify_and_add (OMP_BODY (*expr_p), &body);
7483 switch (TREE_CODE (*expr_p))
7485 case OMP_SECTION:
7486 g = gimple_build_omp_section (body);
7487 break;
7488 case OMP_MASTER:
7489 g = gimple_build_omp_master (body);
7490 break;
7491 case OMP_ORDERED:
7492 g = gimple_build_omp_ordered (body);
7493 break;
7494 case OMP_CRITICAL:
7495 g = gimple_build_omp_critical (body,
7496 OMP_CRITICAL_NAME (*expr_p));
7497 break;
7498 default:
7499 gcc_unreachable ();
7501 gimplify_seq_add_stmt (pre_p, g);
7502 ret = GS_ALL_DONE;
7503 break;
7506 case OMP_ATOMIC:
7507 case OMP_ATOMIC_READ:
7508 case OMP_ATOMIC_CAPTURE_OLD:
7509 case OMP_ATOMIC_CAPTURE_NEW:
7510 ret = gimplify_omp_atomic (expr_p, pre_p);
7511 break;
7513 case TRANSACTION_EXPR:
7514 ret = gimplify_transaction (expr_p, pre_p);
7515 break;
7517 case TRUTH_AND_EXPR:
7518 case TRUTH_OR_EXPR:
7519 case TRUTH_XOR_EXPR:
7521 tree orig_type = TREE_TYPE (*expr_p);
7522 tree new_type, xop0, xop1;
7523 *expr_p = gimple_boolify (*expr_p);
7524 new_type = TREE_TYPE (*expr_p);
7525 if (!useless_type_conversion_p (orig_type, new_type))
7527 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7528 ret = GS_OK;
7529 break;
7532 /* Boolified binary truth expressions are semantically equivalent
7533 to bitwise binary expressions. Canonicalize them to the
7534 bitwise variant. */
7535 switch (TREE_CODE (*expr_p))
7537 case TRUTH_AND_EXPR:
7538 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7539 break;
7540 case TRUTH_OR_EXPR:
7541 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7542 break;
7543 case TRUTH_XOR_EXPR:
7544 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7545 break;
7546 default:
7547 break;
7549 /* Now make sure that operands have compatible type to
7550 expression's new_type. */
7551 xop0 = TREE_OPERAND (*expr_p, 0);
7552 xop1 = TREE_OPERAND (*expr_p, 1);
7553 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7554 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7555 new_type,
7556 xop0);
7557 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7558 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7559 new_type,
7560 xop1);
7561 /* Continue classified as tcc_binary. */
7562 goto expr_2;
7565 case FMA_EXPR:
7566 case VEC_PERM_EXPR:
7567 /* Classified as tcc_expression. */
7568 goto expr_3;
7570 case POINTER_PLUS_EXPR:
7572 enum gimplify_status r0, r1;
7573 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7574 post_p, is_gimple_val, fb_rvalue);
7575 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7576 post_p, is_gimple_val, fb_rvalue);
7577 recalculate_side_effects (*expr_p);
7578 ret = MIN (r0, r1);
7579 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7580 after gimplifying operands - this is similar to how
7581 it would be folding all gimplified stmts on creation
7582 to have them canonicalized, which is what we eventually
7583 should do anyway. */
7584 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7585 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7587 *expr_p = build_fold_addr_expr_with_type_loc
7588 (input_location,
7589 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7590 TREE_OPERAND (*expr_p, 0),
7591 fold_convert (ptr_type_node,
7592 TREE_OPERAND (*expr_p, 1))),
7593 TREE_TYPE (*expr_p));
7594 ret = MIN (ret, GS_OK);
7596 break;
7599 default:
7600 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7602 case tcc_comparison:
7603 /* Handle comparison of objects of non scalar mode aggregates
7604 with a call to memcmp. It would be nice to only have to do
7605 this for variable-sized objects, but then we'd have to allow
7606 the same nest of reference nodes we allow for MODIFY_EXPR and
7607 that's too complex.
7609 Compare scalar mode aggregates as scalar mode values. Using
7610 memcmp for them would be very inefficient at best, and is
7611 plain wrong if bitfields are involved. */
7613 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7615 /* Vector comparisons need no boolification. */
7616 if (TREE_CODE (type) == VECTOR_TYPE)
7617 goto expr_2;
7618 else if (!AGGREGATE_TYPE_P (type))
7620 tree org_type = TREE_TYPE (*expr_p);
7621 *expr_p = gimple_boolify (*expr_p);
7622 if (!useless_type_conversion_p (org_type,
7623 TREE_TYPE (*expr_p)))
7625 *expr_p = fold_convert_loc (input_location,
7626 org_type, *expr_p);
7627 ret = GS_OK;
7629 else
7630 goto expr_2;
7632 else if (TYPE_MODE (type) != BLKmode)
7633 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7634 else
7635 ret = gimplify_variable_sized_compare (expr_p);
7637 break;
7640 /* If *EXPR_P does not need to be special-cased, handle it
7641 according to its class. */
7642 case tcc_unary:
7643 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7644 post_p, is_gimple_val, fb_rvalue);
7645 break;
7647 case tcc_binary:
7648 expr_2:
7650 enum gimplify_status r0, r1;
7652 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7653 post_p, is_gimple_val, fb_rvalue);
7654 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7655 post_p, is_gimple_val, fb_rvalue);
7657 ret = MIN (r0, r1);
7658 break;
7661 expr_3:
7663 enum gimplify_status r0, r1, r2;
7665 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7666 post_p, is_gimple_val, fb_rvalue);
7667 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7668 post_p, is_gimple_val, fb_rvalue);
7669 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7670 post_p, is_gimple_val, fb_rvalue);
7672 ret = MIN (MIN (r0, r1), r2);
7673 break;
7676 case tcc_declaration:
7677 case tcc_constant:
7678 ret = GS_ALL_DONE;
7679 goto dont_recalculate;
7681 default:
7682 gcc_unreachable ();
7685 recalculate_side_effects (*expr_p);
7687 dont_recalculate:
7688 break;
7691 gcc_assert (*expr_p || ret != GS_OK);
7693 while (ret == GS_OK);
7695 /* If we encountered an error_mark somewhere nested inside, either
7696 stub out the statement or propagate the error back out. */
7697 if (ret == GS_ERROR)
7699 if (is_statement)
7700 *expr_p = NULL;
7701 goto out;
7704 /* This was only valid as a return value from the langhook, which
7705 we handled. Make sure it doesn't escape from any other context. */
7706 gcc_assert (ret != GS_UNHANDLED);
7708 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7710 /* We aren't looking for a value, and we don't have a valid
7711 statement. If it doesn't have side-effects, throw it away. */
7712 if (!TREE_SIDE_EFFECTS (*expr_p))
7713 *expr_p = NULL;
7714 else if (!TREE_THIS_VOLATILE (*expr_p))
7716 /* This is probably a _REF that contains something nested that
7717 has side effects. Recurse through the operands to find it. */
7718 enum tree_code code = TREE_CODE (*expr_p);
7720 switch (code)
7722 case COMPONENT_REF:
7723 case REALPART_EXPR:
7724 case IMAGPART_EXPR:
7725 case VIEW_CONVERT_EXPR:
7726 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7727 gimple_test_f, fallback);
7728 break;
7730 case ARRAY_REF:
7731 case ARRAY_RANGE_REF:
7732 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7733 gimple_test_f, fallback);
7734 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7735 gimple_test_f, fallback);
7736 break;
7738 default:
7739 /* Anything else with side-effects must be converted to
7740 a valid statement before we get here. */
7741 gcc_unreachable ();
7744 *expr_p = NULL;
7746 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7747 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7749 /* Historically, the compiler has treated a bare reference
7750 to a non-BLKmode volatile lvalue as forcing a load. */
7751 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7753 /* Normally, we do not want to create a temporary for a
7754 TREE_ADDRESSABLE type because such a type should not be
7755 copied by bitwise-assignment. However, we make an
7756 exception here, as all we are doing here is ensuring that
7757 we read the bytes that make up the type. We use
7758 create_tmp_var_raw because create_tmp_var will abort when
7759 given a TREE_ADDRESSABLE type. */
7760 tree tmp = create_tmp_var_raw (type, "vol");
7761 gimple_add_tmp_var (tmp);
7762 gimplify_assign (tmp, *expr_p, pre_p);
7763 *expr_p = NULL;
7765 else
7766 /* We can't do anything useful with a volatile reference to
7767 an incomplete type, so just throw it away. Likewise for
7768 a BLKmode type, since any implicit inner load should
7769 already have been turned into an explicit one by the
7770 gimplification process. */
7771 *expr_p = NULL;
7774 /* If we are gimplifying at the statement level, we're done. Tack
7775 everything together and return. */
7776 if (fallback == fb_none || is_statement)
7778 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7779 it out for GC to reclaim it. */
7780 *expr_p = NULL_TREE;
7782 if (!gimple_seq_empty_p (internal_pre)
7783 || !gimple_seq_empty_p (internal_post))
7785 gimplify_seq_add_seq (&internal_pre, internal_post);
7786 gimplify_seq_add_seq (pre_p, internal_pre);
7789 /* The result of gimplifying *EXPR_P is going to be the last few
7790 statements in *PRE_P and *POST_P. Add location information
7791 to all the statements that were added by the gimplification
7792 helpers. */
7793 if (!gimple_seq_empty_p (*pre_p))
7794 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7796 if (!gimple_seq_empty_p (*post_p))
7797 annotate_all_with_location_after (*post_p, post_last_gsi,
7798 input_location);
7800 goto out;
7803 #ifdef ENABLE_GIMPLE_CHECKING
7804 if (*expr_p)
7806 enum tree_code code = TREE_CODE (*expr_p);
7807 /* These expressions should already be in gimple IR form. */
7808 gcc_assert (code != MODIFY_EXPR
7809 && code != ASM_EXPR
7810 && code != BIND_EXPR
7811 && code != CATCH_EXPR
7812 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7813 && code != EH_FILTER_EXPR
7814 && code != GOTO_EXPR
7815 && code != LABEL_EXPR
7816 && code != LOOP_EXPR
7817 && code != SWITCH_EXPR
7818 && code != TRY_FINALLY_EXPR
7819 && code != OMP_CRITICAL
7820 && code != OMP_FOR
7821 && code != OMP_MASTER
7822 && code != OMP_ORDERED
7823 && code != OMP_PARALLEL
7824 && code != OMP_SECTIONS
7825 && code != OMP_SECTION
7826 && code != OMP_SINGLE);
7828 #endif
7830 /* Otherwise we're gimplifying a subexpression, so the resulting
7831 value is interesting. If it's a valid operand that matches
7832 GIMPLE_TEST_F, we're done. Unless we are handling some
7833 post-effects internally; if that's the case, we need to copy into
7834 a temporary before adding the post-effects to POST_P. */
7835 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7836 goto out;
7838 /* Otherwise, we need to create a new temporary for the gimplified
7839 expression. */
7841 /* We can't return an lvalue if we have an internal postqueue. The
7842 object the lvalue refers to would (probably) be modified by the
7843 postqueue; we need to copy the value out first, which means an
7844 rvalue. */
7845 if ((fallback & fb_lvalue)
7846 && gimple_seq_empty_p (internal_post)
7847 && is_gimple_addressable (*expr_p))
7849 /* An lvalue will do. Take the address of the expression, store it
7850 in a temporary, and replace the expression with an INDIRECT_REF of
7851 that temporary. */
7852 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
7853 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7854 *expr_p = build_simple_mem_ref (tmp);
7856 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7858 /* An rvalue will do. Assign the gimplified expression into a
7859 new temporary TMP and replace the original expression with
7860 TMP. First, make sure that the expression has a type so that
7861 it can be assigned into a temporary. */
7862 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7864 if (!gimple_seq_empty_p (internal_post) || (fallback & fb_lvalue))
7865 /* The postqueue might change the value of the expression between
7866 the initialization and use of the temporary, so we can't use a
7867 formal temp. FIXME do we care? */
7869 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7870 if (TREE_CODE (TREE_TYPE (*expr_p)) == COMPLEX_TYPE
7871 || TREE_CODE (TREE_TYPE (*expr_p)) == VECTOR_TYPE)
7872 DECL_GIMPLE_REG_P (*expr_p) = 1;
7874 else
7875 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7877 else
7879 #ifdef ENABLE_GIMPLE_CHECKING
7880 if (!(fallback & fb_mayfail))
7882 fprintf (stderr, "gimplification failed:\n");
7883 print_generic_expr (stderr, *expr_p, 0);
7884 debug_tree (*expr_p);
7885 internal_error ("gimplification failed");
7887 #endif
7888 gcc_assert (fallback & fb_mayfail);
7890 /* If this is an asm statement, and the user asked for the
7891 impossible, don't die. Fail and let gimplify_asm_expr
7892 issue an error. */
7893 ret = GS_ERROR;
7894 goto out;
7897 /* Make sure the temporary matches our predicate. */
7898 gcc_assert ((*gimple_test_f) (*expr_p));
7900 if (!gimple_seq_empty_p (internal_post))
7902 annotate_all_with_location (internal_post, input_location);
7903 gimplify_seq_add_seq (pre_p, internal_post);
7906 out:
7907 input_location = saved_location;
7908 return ret;
7911 /* Look through TYPE for variable-sized objects and gimplify each such
7912 size that we find. Add to LIST_P any statements generated. */
7914 void
7915 gimplify_type_sizes (tree type, gimple_seq *list_p)
7917 tree field, t;
7919 if (type == NULL || type == error_mark_node)
7920 return;
7922 /* We first do the main variant, then copy into any other variants. */
7923 type = TYPE_MAIN_VARIANT (type);
7925 /* Avoid infinite recursion. */
7926 if (TYPE_SIZES_GIMPLIFIED (type))
7927 return;
7929 TYPE_SIZES_GIMPLIFIED (type) = 1;
7931 switch (TREE_CODE (type))
7933 case INTEGER_TYPE:
7934 case ENUMERAL_TYPE:
7935 case BOOLEAN_TYPE:
7936 case REAL_TYPE:
7937 case FIXED_POINT_TYPE:
7938 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
7939 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
7941 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7943 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
7944 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
7946 break;
7948 case ARRAY_TYPE:
7949 /* These types may not have declarations, so handle them here. */
7950 gimplify_type_sizes (TREE_TYPE (type), list_p);
7951 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
7952 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
7953 with assigned stack slots, for -O1+ -g they should be tracked
7954 by VTA. */
7955 if (!(TYPE_NAME (type)
7956 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7957 && DECL_IGNORED_P (TYPE_NAME (type)))
7958 && TYPE_DOMAIN (type)
7959 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
7961 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7962 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7963 DECL_IGNORED_P (t) = 0;
7964 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7965 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7966 DECL_IGNORED_P (t) = 0;
7968 break;
7970 case RECORD_TYPE:
7971 case UNION_TYPE:
7972 case QUAL_UNION_TYPE:
7973 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7974 if (TREE_CODE (field) == FIELD_DECL)
7976 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
7977 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
7978 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
7979 gimplify_type_sizes (TREE_TYPE (field), list_p);
7981 break;
7983 case POINTER_TYPE:
7984 case REFERENCE_TYPE:
7985 /* We used to recurse on the pointed-to type here, which turned out to
7986 be incorrect because its definition might refer to variables not
7987 yet initialized at this point if a forward declaration is involved.
7989 It was actually useful for anonymous pointed-to types to ensure
7990 that the sizes evaluation dominates every possible later use of the
7991 values. Restricting to such types here would be safe since there
7992 is no possible forward declaration around, but would introduce an
7993 undesirable middle-end semantic to anonymity. We then defer to
7994 front-ends the responsibility of ensuring that the sizes are
7995 evaluated both early and late enough, e.g. by attaching artificial
7996 type declarations to the tree. */
7997 break;
7999 default:
8000 break;
8003 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8004 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8006 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8008 TYPE_SIZE (t) = TYPE_SIZE (type);
8009 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8010 TYPE_SIZES_GIMPLIFIED (t) = 1;
8014 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8015 a size or position, has had all of its SAVE_EXPRs evaluated.
8016 We add any required statements to *STMT_P. */
8018 void
8019 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8021 tree type, expr = *expr_p;
8023 /* We don't do anything if the value isn't there, is constant, or contains
8024 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8025 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8026 will want to replace it with a new variable, but that will cause problems
8027 if this type is from outside the function. It's OK to have that here. */
8028 if (is_gimple_sizepos (expr))
8029 return;
8031 type = TREE_TYPE (expr);
8032 *expr_p = unshare_expr (expr);
8034 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8035 expr = *expr_p;
8037 /* Verify that we've an exact type match with the original expression.
8038 In particular, we do not wish to drop a "sizetype" in favour of a
8039 type of similar dimensions. We don't want to pollute the generic
8040 type-stripping code with this knowledge because it doesn't matter
8041 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
8042 and friends retain their "sizetype-ness". */
8043 if (TREE_TYPE (expr) != type
8044 && TREE_CODE (type) == INTEGER_TYPE
8045 && TYPE_IS_SIZETYPE (type))
8047 tree tmp;
8048 gimple stmt;
8050 *expr_p = create_tmp_var (type, NULL);
8051 tmp = build1 (NOP_EXPR, type, expr);
8052 stmt = gimplify_assign (*expr_p, tmp, stmt_p);
8053 gimple_set_location (stmt, EXPR_LOC_OR_HERE (expr));
8057 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8058 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8059 is true, also gimplify the parameters. */
8061 gimple
8062 gimplify_body (tree fndecl, bool do_parms)
8064 location_t saved_location = input_location;
8065 gimple_seq parm_stmts, seq;
8066 gimple outer_bind;
8067 struct gimplify_ctx gctx;
8068 struct cgraph_node *cgn;
8070 timevar_push (TV_TREE_GIMPLIFY);
8072 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8073 gimplification. */
8074 default_rtl_profile ();
8076 gcc_assert (gimplify_ctxp == NULL);
8077 push_gimplify_context (&gctx);
8079 /* Unshare most shared trees in the body and in that of any nested functions.
8080 It would seem we don't have to do this for nested functions because
8081 they are supposed to be output and then the outer function gimplified
8082 first, but the g++ front end doesn't always do it that way. */
8083 unshare_body (fndecl);
8084 unvisit_body (fndecl);
8086 cgn = cgraph_get_node (fndecl);
8087 if (cgn && cgn->origin)
8088 nonlocal_vlas = pointer_set_create ();
8090 /* Make sure input_location isn't set to something weird. */
8091 input_location = DECL_SOURCE_LOCATION (fndecl);
8093 /* Resolve callee-copies. This has to be done before processing
8094 the body so that DECL_VALUE_EXPR gets processed correctly. */
8095 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8097 /* Gimplify the function's body. */
8098 seq = NULL;
8099 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8100 outer_bind = gimple_seq_first_stmt (seq);
8101 if (!outer_bind)
8103 outer_bind = gimple_build_nop ();
8104 gimplify_seq_add_stmt (&seq, outer_bind);
8107 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8108 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8109 if (gimple_code (outer_bind) == GIMPLE_BIND
8110 && gimple_seq_first (seq) == gimple_seq_last (seq))
8112 else
8113 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8115 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8117 /* If we had callee-copies statements, insert them at the beginning
8118 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8119 if (!gimple_seq_empty_p (parm_stmts))
8121 tree parm;
8123 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8124 gimple_bind_set_body (outer_bind, parm_stmts);
8126 for (parm = DECL_ARGUMENTS (current_function_decl);
8127 parm; parm = DECL_CHAIN (parm))
8128 if (DECL_HAS_VALUE_EXPR_P (parm))
8130 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8131 DECL_IGNORED_P (parm) = 0;
8135 if (nonlocal_vlas)
8137 pointer_set_destroy (nonlocal_vlas);
8138 nonlocal_vlas = NULL;
8141 pop_gimplify_context (outer_bind);
8142 gcc_assert (gimplify_ctxp == NULL);
8144 if (!seen_error ())
8145 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8147 timevar_pop (TV_TREE_GIMPLIFY);
8148 input_location = saved_location;
8150 return outer_bind;
8153 typedef char *char_p; /* For DEF_VEC_P. */
8154 DEF_VEC_P(char_p);
8155 DEF_VEC_ALLOC_P(char_p,heap);
8157 /* Return whether we should exclude FNDECL from instrumentation. */
8159 static bool
8160 flag_instrument_functions_exclude_p (tree fndecl)
8162 VEC(char_p,heap) *vec;
8164 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_functions;
8165 if (VEC_length (char_p, vec) > 0)
8167 const char *name;
8168 int i;
8169 char *s;
8171 name = lang_hooks.decl_printable_name (fndecl, 0);
8172 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8173 if (strstr (name, s) != NULL)
8174 return true;
8177 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_files;
8178 if (VEC_length (char_p, vec) > 0)
8180 const char *name;
8181 int i;
8182 char *s;
8184 name = DECL_SOURCE_FILE (fndecl);
8185 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8186 if (strstr (name, s) != NULL)
8187 return true;
8190 return false;
8193 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8194 node for the function we want to gimplify.
8196 Return the sequence of GIMPLE statements corresponding to the body
8197 of FNDECL. */
8199 void
8200 gimplify_function_tree (tree fndecl)
8202 tree oldfn, parm, ret;
8203 gimple_seq seq;
8204 gimple bind;
8206 gcc_assert (!gimple_body (fndecl));
8208 oldfn = current_function_decl;
8209 current_function_decl = fndecl;
8210 if (DECL_STRUCT_FUNCTION (fndecl))
8211 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8212 else
8213 push_struct_function (fndecl);
8215 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8217 /* Preliminarily mark non-addressed complex variables as eligible
8218 for promotion to gimple registers. We'll transform their uses
8219 as we find them. */
8220 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8221 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8222 && !TREE_THIS_VOLATILE (parm)
8223 && !needs_to_live_in_memory (parm))
8224 DECL_GIMPLE_REG_P (parm) = 1;
8227 ret = DECL_RESULT (fndecl);
8228 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8229 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8230 && !needs_to_live_in_memory (ret))
8231 DECL_GIMPLE_REG_P (ret) = 1;
8233 bind = gimplify_body (fndecl, true);
8235 /* The tree body of the function is no longer needed, replace it
8236 with the new GIMPLE body. */
8237 seq = gimple_seq_alloc ();
8238 gimple_seq_add_stmt (&seq, bind);
8239 gimple_set_body (fndecl, seq);
8241 /* If we're instrumenting function entry/exit, then prepend the call to
8242 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8243 catch the exit hook. */
8244 /* ??? Add some way to ignore exceptions for this TFE. */
8245 if (flag_instrument_function_entry_exit
8246 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8247 && !flag_instrument_functions_exclude_p (fndecl))
8249 tree x;
8250 gimple new_bind;
8251 gimple tf;
8252 gimple_seq cleanup = NULL, body = NULL;
8253 tree tmp_var;
8254 gimple call;
8256 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8257 call = gimple_build_call (x, 1, integer_zero_node);
8258 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8259 gimple_call_set_lhs (call, tmp_var);
8260 gimplify_seq_add_stmt (&cleanup, call);
8261 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8262 call = gimple_build_call (x, 2,
8263 build_fold_addr_expr (current_function_decl),
8264 tmp_var);
8265 gimplify_seq_add_stmt (&cleanup, call);
8266 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8268 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8269 call = gimple_build_call (x, 1, integer_zero_node);
8270 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8271 gimple_call_set_lhs (call, tmp_var);
8272 gimplify_seq_add_stmt (&body, call);
8273 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8274 call = gimple_build_call (x, 2,
8275 build_fold_addr_expr (current_function_decl),
8276 tmp_var);
8277 gimplify_seq_add_stmt (&body, call);
8278 gimplify_seq_add_stmt (&body, tf);
8279 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8280 /* Clear the block for BIND, since it is no longer directly inside
8281 the function, but within a try block. */
8282 gimple_bind_set_block (bind, NULL);
8284 /* Replace the current function body with the body
8285 wrapped in the try/finally TF. */
8286 seq = gimple_seq_alloc ();
8287 gimple_seq_add_stmt (&seq, new_bind);
8288 gimple_set_body (fndecl, seq);
8291 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8292 cfun->curr_properties = PROP_gimple_any;
8294 current_function_decl = oldfn;
8295 pop_cfun ();
8298 /* Some transformations like inlining may invalidate the GIMPLE form
8299 for operands. This function traverses all the operands in STMT and
8300 gimplifies anything that is not a valid gimple operand. Any new
8301 GIMPLE statements are inserted before *GSI_P. */
8303 void
8304 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8306 size_t i, num_ops;
8307 tree orig_lhs = NULL_TREE, lhs, t;
8308 gimple_seq pre = NULL;
8309 gimple post_stmt = NULL;
8310 struct gimplify_ctx gctx;
8312 push_gimplify_context (&gctx);
8313 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8315 switch (gimple_code (stmt))
8317 case GIMPLE_COND:
8318 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8319 is_gimple_val, fb_rvalue);
8320 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8321 is_gimple_val, fb_rvalue);
8322 break;
8323 case GIMPLE_SWITCH:
8324 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8325 is_gimple_val, fb_rvalue);
8326 break;
8327 case GIMPLE_OMP_ATOMIC_LOAD:
8328 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8329 is_gimple_val, fb_rvalue);
8330 break;
8331 case GIMPLE_ASM:
8333 size_t i, noutputs = gimple_asm_noutputs (stmt);
8334 const char *constraint, **oconstraints;
8335 bool allows_mem, allows_reg, is_inout;
8337 oconstraints
8338 = (const char **) alloca ((noutputs) * sizeof (const char *));
8339 for (i = 0; i < noutputs; i++)
8341 tree op = gimple_asm_output_op (stmt, i);
8342 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8343 oconstraints[i] = constraint;
8344 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8345 &allows_reg, &is_inout);
8346 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8347 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8348 fb_lvalue | fb_mayfail);
8350 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8352 tree op = gimple_asm_input_op (stmt, i);
8353 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8354 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8355 oconstraints, &allows_mem, &allows_reg);
8356 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8357 allows_reg = 0;
8358 if (!allows_reg && allows_mem)
8359 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8360 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8361 else
8362 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8363 is_gimple_asm_val, fb_rvalue);
8366 break;
8367 default:
8368 /* NOTE: We start gimplifying operands from last to first to
8369 make sure that side-effects on the RHS of calls, assignments
8370 and ASMs are executed before the LHS. The ordering is not
8371 important for other statements. */
8372 num_ops = gimple_num_ops (stmt);
8373 orig_lhs = gimple_get_lhs (stmt);
8374 for (i = num_ops; i > 0; i--)
8376 tree op = gimple_op (stmt, i - 1);
8377 if (op == NULL_TREE)
8378 continue;
8379 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8380 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8381 else if (i == 2
8382 && is_gimple_assign (stmt)
8383 && num_ops == 2
8384 && get_gimple_rhs_class (gimple_expr_code (stmt))
8385 == GIMPLE_SINGLE_RHS)
8386 gimplify_expr (&op, &pre, NULL,
8387 rhs_predicate_for (gimple_assign_lhs (stmt)),
8388 fb_rvalue);
8389 else if (i == 2 && is_gimple_call (stmt))
8391 if (TREE_CODE (op) == FUNCTION_DECL)
8392 continue;
8393 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8395 else
8396 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8397 gimple_set_op (stmt, i - 1, op);
8400 lhs = gimple_get_lhs (stmt);
8401 /* If the LHS changed it in a way that requires a simple RHS,
8402 create temporary. */
8403 if (lhs && !is_gimple_reg (lhs))
8405 bool need_temp = false;
8407 if (is_gimple_assign (stmt)
8408 && num_ops == 2
8409 && get_gimple_rhs_class (gimple_expr_code (stmt))
8410 == GIMPLE_SINGLE_RHS)
8411 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8412 rhs_predicate_for (gimple_assign_lhs (stmt)),
8413 fb_rvalue);
8414 else if (is_gimple_reg (lhs))
8416 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8418 if (is_gimple_call (stmt))
8420 i = gimple_call_flags (stmt);
8421 if ((i & ECF_LOOPING_CONST_OR_PURE)
8422 || !(i & (ECF_CONST | ECF_PURE)))
8423 need_temp = true;
8425 if (stmt_can_throw_internal (stmt))
8426 need_temp = true;
8429 else
8431 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8432 need_temp = true;
8433 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8435 if (is_gimple_call (stmt))
8437 tree fndecl = gimple_call_fndecl (stmt);
8439 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8440 && !(fndecl && DECL_RESULT (fndecl)
8441 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8442 need_temp = true;
8444 else
8445 need_temp = true;
8448 if (need_temp)
8450 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8452 if (TREE_CODE (orig_lhs) == SSA_NAME)
8453 orig_lhs = SSA_NAME_VAR (orig_lhs);
8455 if (gimple_in_ssa_p (cfun))
8456 temp = make_ssa_name (temp, NULL);
8457 gimple_set_lhs (stmt, temp);
8458 post_stmt = gimple_build_assign (lhs, temp);
8459 if (TREE_CODE (lhs) == SSA_NAME)
8460 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8463 break;
8466 if (gimple_referenced_vars (cfun))
8467 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
8468 add_referenced_var (t);
8470 if (!gimple_seq_empty_p (pre))
8472 if (gimple_in_ssa_p (cfun))
8474 gimple_stmt_iterator i;
8476 for (i = gsi_start (pre); !gsi_end_p (i); gsi_next (&i))
8477 mark_symbols_for_renaming (gsi_stmt (i));
8479 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8481 if (post_stmt)
8482 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8484 pop_gimplify_context (NULL);
8487 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8488 the predicate that will hold for the result. If VAR is not NULL, make the
8489 base variable of the final destination be VAR if suitable. */
8491 tree
8492 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8493 gimple_predicate gimple_test_f, tree var)
8495 tree t;
8496 enum gimplify_status ret;
8497 struct gimplify_ctx gctx;
8499 *stmts = NULL;
8501 /* gimple_test_f might be more strict than is_gimple_val, make
8502 sure we pass both. Just checking gimple_test_f doesn't work
8503 because most gimple predicates do not work recursively. */
8504 if (is_gimple_val (expr)
8505 && (*gimple_test_f) (expr))
8506 return expr;
8508 push_gimplify_context (&gctx);
8509 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8510 gimplify_ctxp->allow_rhs_cond_expr = true;
8512 if (var)
8513 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8515 if (TREE_CODE (expr) != MODIFY_EXPR
8516 && TREE_TYPE (expr) == void_type_node)
8518 gimplify_and_add (expr, stmts);
8519 expr = NULL_TREE;
8521 else
8523 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8524 gcc_assert (ret != GS_ERROR);
8527 if (gimple_referenced_vars (cfun))
8528 for (t = gimplify_ctxp->temps; t ; t = DECL_CHAIN (t))
8529 add_referenced_var (t);
8531 pop_gimplify_context (NULL);
8533 return expr;
8536 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8537 force the result to be either ssa_name or an invariant, otherwise
8538 just force it to be a rhs expression. If VAR is not NULL, make the
8539 base variable of the final destination be VAR if suitable. */
8541 tree
8542 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8544 return force_gimple_operand_1 (expr, stmts,
8545 simple ? is_gimple_val : is_gimple_reg_rhs,
8546 var);
8549 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8550 and VAR. If some statements are produced, emits them at GSI.
8551 If BEFORE is true. the statements are appended before GSI, otherwise
8552 they are appended after it. M specifies the way GSI moves after
8553 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8555 tree
8556 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8557 gimple_predicate gimple_test_f,
8558 tree var, bool before,
8559 enum gsi_iterator_update m)
8561 gimple_seq stmts;
8563 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8565 if (!gimple_seq_empty_p (stmts))
8567 if (gimple_in_ssa_p (cfun))
8569 gimple_stmt_iterator i;
8571 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
8572 mark_symbols_for_renaming (gsi_stmt (i));
8575 if (before)
8576 gsi_insert_seq_before (gsi, stmts, m);
8577 else
8578 gsi_insert_seq_after (gsi, stmts, m);
8581 return expr;
8584 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8585 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8586 otherwise just force it to be a rhs expression. If some statements are
8587 produced, emits them at GSI. If BEFORE is true, the statements are
8588 appended before GSI, otherwise they are appended after it. M specifies
8589 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8590 are the usual values). */
8592 tree
8593 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8594 bool simple_p, tree var, bool before,
8595 enum gsi_iterator_update m)
8597 return force_gimple_operand_gsi_1 (gsi, expr,
8598 simple_p
8599 ? is_gimple_val : is_gimple_reg_rhs,
8600 var, before, m);
8604 #include "gt-gimplify.h"