Mark ChangeLog
[official-gcc.git] / gcc / gimplify.c
blob52fbfc5732c3f68ed90351038a4e56a3d62e0d94
1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2013 Free Software Foundation, Inc.
4 Major work done by Sebastian Pop <s.pop@laposte.net>,
5 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "tree-iterator.h"
30 #include "tree-inline.h"
31 #include "tree-pretty-print.h"
32 #include "langhooks.h"
33 #include "tree-flow.h"
34 #include "cgraph.h"
35 #include "timevar.h"
36 #include "hashtab.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "diagnostic-core.h"
41 #include "target.h"
42 #include "pointer-set.h"
43 #include "splay-tree.h"
44 #include "vec.h"
45 #include "gimple.h"
47 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
48 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
50 enum gimplify_omp_var_data
52 GOVD_SEEN = 1,
53 GOVD_EXPLICIT = 2,
54 GOVD_SHARED = 4,
55 GOVD_PRIVATE = 8,
56 GOVD_FIRSTPRIVATE = 16,
57 GOVD_LASTPRIVATE = 32,
58 GOVD_REDUCTION = 64,
59 GOVD_LOCAL = 128,
60 GOVD_DEBUG_PRIVATE = 256,
61 GOVD_PRIVATE_OUTER_REF = 512,
62 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
63 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
67 enum omp_region_type
69 ORT_WORKSHARE = 0,
70 ORT_PARALLEL = 2,
71 ORT_COMBINED_PARALLEL = 3,
72 ORT_TASK = 4,
73 ORT_UNTIED_TASK = 5
76 struct gimplify_omp_ctx
78 struct gimplify_omp_ctx *outer_context;
79 splay_tree variables;
80 struct pointer_set_t *privatized_types;
81 location_t location;
82 enum omp_clause_default_kind default_kind;
83 enum omp_region_type region_type;
86 static struct gimplify_ctx *gimplify_ctxp;
87 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
90 /* Formal (expression) temporary table handling: multiple occurrences of
91 the same scalar expression are evaluated into the same temporary. */
93 typedef struct gimple_temp_hash_elt
95 tree val; /* Key */
96 tree temp; /* Value */
97 } elt_t;
99 /* Forward declaration. */
100 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
102 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
103 form and we don't do any syntax checking. */
105 void
106 mark_addressable (tree x)
108 while (handled_component_p (x))
109 x = TREE_OPERAND (x, 0);
110 if (TREE_CODE (x) == MEM_REF
111 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
112 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
113 if (TREE_CODE (x) != VAR_DECL
114 && TREE_CODE (x) != PARM_DECL
115 && TREE_CODE (x) != RESULT_DECL)
116 return;
117 TREE_ADDRESSABLE (x) = 1;
119 /* Also mark the artificial SSA_NAME that points to the partition of X. */
120 if (TREE_CODE (x) == VAR_DECL
121 && !DECL_EXTERNAL (x)
122 && !TREE_STATIC (x)
123 && cfun->gimple_df != NULL
124 && cfun->gimple_df->decls_to_pointers != NULL)
126 void *namep
127 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
128 if (namep)
129 TREE_ADDRESSABLE (*(tree *)namep) = 1;
133 /* Return a hash value for a formal temporary table entry. */
135 static hashval_t
136 gimple_tree_hash (const void *p)
138 tree t = ((const elt_t *) p)->val;
139 return iterative_hash_expr (t, 0);
142 /* Compare two formal temporary table entries. */
144 static int
145 gimple_tree_eq (const void *p1, const void *p2)
147 tree t1 = ((const elt_t *) p1)->val;
148 tree t2 = ((const elt_t *) p2)->val;
149 enum tree_code code = TREE_CODE (t1);
151 if (TREE_CODE (t2) != code
152 || TREE_TYPE (t1) != TREE_TYPE (t2))
153 return 0;
155 if (!operand_equal_p (t1, t2, 0))
156 return 0;
158 #ifdef ENABLE_CHECKING
159 /* Only allow them to compare equal if they also hash equal; otherwise
160 results are nondeterminate, and we fail bootstrap comparison. */
161 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
162 #endif
164 return 1;
167 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
168 *SEQ_P is NULL, a new sequence is allocated. This function is
169 similar to gimple_seq_add_stmt, but does not scan the operands.
170 During gimplification, we need to manipulate statement sequences
171 before the def/use vectors have been constructed. */
173 void
174 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
176 gimple_stmt_iterator si;
178 if (gs == NULL)
179 return;
181 si = gsi_last (*seq_p);
182 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
185 /* Shorter alias name for the above function for use in gimplify.c
186 only. */
188 static inline void
189 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
191 gimple_seq_add_stmt_without_update (seq_p, gs);
194 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
195 NULL, a new sequence is allocated. This function is
196 similar to gimple_seq_add_seq, but does not scan the operands.
197 During gimplification, we need to manipulate statement sequences
198 before the def/use vectors have been constructed. */
200 static void
201 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
203 gimple_stmt_iterator si;
205 if (src == NULL)
206 return;
208 si = gsi_last (*dst_p);
209 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
212 /* Set up a context for the gimplifier. */
214 void
215 push_gimplify_context (struct gimplify_ctx *c)
217 memset (c, '\0', sizeof (*c));
218 c->prev_context = gimplify_ctxp;
219 gimplify_ctxp = c;
222 /* Tear down a context for the gimplifier. If BODY is non-null, then
223 put the temporaries into the outer BIND_EXPR. Otherwise, put them
224 in the local_decls.
226 BODY is not a sequence, but the first tuple in a sequence. */
228 void
229 pop_gimplify_context (gimple body)
231 struct gimplify_ctx *c = gimplify_ctxp;
233 gcc_assert (c
234 && (!c->bind_expr_stack.exists ()
235 || c->bind_expr_stack.is_empty ()));
236 c->bind_expr_stack.release ();
237 gimplify_ctxp = c->prev_context;
239 if (body)
240 declare_vars (c->temps, body, false);
241 else
242 record_vars (c->temps);
244 if (c->temp_htab)
245 htab_delete (c->temp_htab);
248 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
250 static void
251 gimple_push_bind_expr (gimple gimple_bind)
253 gimplify_ctxp->bind_expr_stack.reserve (8);
254 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
257 /* Pop the first element off the stack of bindings. */
259 static void
260 gimple_pop_bind_expr (void)
262 gimplify_ctxp->bind_expr_stack.pop ();
265 /* Return the first element of the stack of bindings. */
267 gimple
268 gimple_current_bind_expr (void)
270 return gimplify_ctxp->bind_expr_stack.last ();
273 /* Return the stack of bindings created during gimplification. */
275 vec<gimple>
276 gimple_bind_expr_stack (void)
278 return gimplify_ctxp->bind_expr_stack;
281 /* Return true iff there is a COND_EXPR between us and the innermost
282 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
284 static bool
285 gimple_conditional_context (void)
287 return gimplify_ctxp->conditions > 0;
290 /* Note that we've entered a COND_EXPR. */
292 static void
293 gimple_push_condition (void)
295 #ifdef ENABLE_GIMPLE_CHECKING
296 if (gimplify_ctxp->conditions == 0)
297 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
298 #endif
299 ++(gimplify_ctxp->conditions);
302 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
303 now, add any conditional cleanups we've seen to the prequeue. */
305 static void
306 gimple_pop_condition (gimple_seq *pre_p)
308 int conds = --(gimplify_ctxp->conditions);
310 gcc_assert (conds >= 0);
311 if (conds == 0)
313 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
314 gimplify_ctxp->conditional_cleanups = NULL;
318 /* A stable comparison routine for use with splay trees and DECLs. */
320 static int
321 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
323 tree a = (tree) xa;
324 tree b = (tree) xb;
326 return DECL_UID (a) - DECL_UID (b);
329 /* Create a new omp construct that deals with variable remapping. */
331 static struct gimplify_omp_ctx *
332 new_omp_context (enum omp_region_type region_type)
334 struct gimplify_omp_ctx *c;
336 c = XCNEW (struct gimplify_omp_ctx);
337 c->outer_context = gimplify_omp_ctxp;
338 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
339 c->privatized_types = pointer_set_create ();
340 c->location = input_location;
341 c->region_type = region_type;
342 if ((region_type & ORT_TASK) == 0)
343 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
344 else
345 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
347 return c;
350 /* Destroy an omp construct that deals with variable remapping. */
352 static void
353 delete_omp_context (struct gimplify_omp_ctx *c)
355 splay_tree_delete (c->variables);
356 pointer_set_destroy (c->privatized_types);
357 XDELETE (c);
360 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
361 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
363 /* Both gimplify the statement T and append it to *SEQ_P. This function
364 behaves exactly as gimplify_stmt, but you don't have to pass T as a
365 reference. */
367 void
368 gimplify_and_add (tree t, gimple_seq *seq_p)
370 gimplify_stmt (&t, seq_p);
373 /* Gimplify statement T into sequence *SEQ_P, and return the first
374 tuple in the sequence of generated tuples for this statement.
375 Return NULL if gimplifying T produced no tuples. */
377 static gimple
378 gimplify_and_return_first (tree t, gimple_seq *seq_p)
380 gimple_stmt_iterator last = gsi_last (*seq_p);
382 gimplify_and_add (t, seq_p);
384 if (!gsi_end_p (last))
386 gsi_next (&last);
387 return gsi_stmt (last);
389 else
390 return gimple_seq_first_stmt (*seq_p);
393 /* Strip off a legitimate source ending from the input string NAME of
394 length LEN. Rather than having to know the names used by all of
395 our front ends, we strip off an ending of a period followed by
396 up to five characters. (Java uses ".class".) */
398 static inline void
399 remove_suffix (char *name, int len)
401 int i;
403 for (i = 2; i < 8 && len > i; i++)
405 if (name[len - i] == '.')
407 name[len - i] = '\0';
408 break;
413 /* Create a new temporary name with PREFIX. Return an identifier. */
415 static GTY(()) unsigned int tmp_var_id_num;
417 tree
418 create_tmp_var_name (const char *prefix)
420 char *tmp_name;
422 if (prefix)
424 char *preftmp = ASTRDUP (prefix);
426 remove_suffix (preftmp, strlen (preftmp));
427 clean_symbol_name (preftmp);
429 prefix = preftmp;
432 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
433 return get_identifier (tmp_name);
436 /* Create a new temporary variable declaration of type TYPE.
437 Do NOT push it into the current binding. */
439 tree
440 create_tmp_var_raw (tree type, const char *prefix)
442 tree tmp_var;
444 tmp_var = build_decl (input_location,
445 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
446 type);
448 /* The variable was declared by the compiler. */
449 DECL_ARTIFICIAL (tmp_var) = 1;
450 /* And we don't want debug info for it. */
451 DECL_IGNORED_P (tmp_var) = 1;
453 /* Make the variable writable. */
454 TREE_READONLY (tmp_var) = 0;
456 DECL_EXTERNAL (tmp_var) = 0;
457 TREE_STATIC (tmp_var) = 0;
458 TREE_USED (tmp_var) = 1;
460 return tmp_var;
463 /* Create a new temporary variable declaration of type TYPE. DO push the
464 variable into the current binding. Further, assume that this is called
465 only from gimplification or optimization, at which point the creation of
466 certain types are bugs. */
468 tree
469 create_tmp_var (tree type, const char *prefix)
471 tree tmp_var;
473 /* We don't allow types that are addressable (meaning we can't make copies),
474 or incomplete. We also used to reject every variable size objects here,
475 but now support those for which a constant upper bound can be obtained.
476 The processing for variable sizes is performed in gimple_add_tmp_var,
477 point at which it really matters and possibly reached via paths not going
478 through this function, e.g. after direct calls to create_tmp_var_raw. */
479 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
481 tmp_var = create_tmp_var_raw (type, prefix);
482 gimple_add_tmp_var (tmp_var);
483 return tmp_var;
486 /* Create a new temporary variable declaration of type TYPE by calling
487 create_tmp_var and if TYPE is a vector or a complex number, mark the new
488 temporary as gimple register. */
490 tree
491 create_tmp_reg (tree type, const char *prefix)
493 tree tmp;
495 tmp = create_tmp_var (type, prefix);
496 if (TREE_CODE (type) == COMPLEX_TYPE
497 || TREE_CODE (type) == VECTOR_TYPE)
498 DECL_GIMPLE_REG_P (tmp) = 1;
500 return tmp;
503 /* Returns true iff T is a valid RHS for an assignment to a renamed
504 user -- or front-end generated artificial -- variable. */
506 static bool
507 is_gimple_reg_rhs (tree t)
509 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
512 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
513 LHS, or for a call argument. */
515 static bool
516 is_gimple_mem_rhs (tree t)
518 /* If we're dealing with a renamable type, either source or dest must be
519 a renamed variable. */
520 if (is_gimple_reg_type (TREE_TYPE (t)))
521 return is_gimple_val (t);
522 else
523 return is_gimple_val (t) || is_gimple_lvalue (t);
526 /* Return true if T is a CALL_EXPR or an expression that can be
527 assigned to a temporary. Note that this predicate should only be
528 used during gimplification. See the rationale for this in
529 gimplify_modify_expr. */
531 static bool
532 is_gimple_reg_rhs_or_call (tree t)
534 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
535 || TREE_CODE (t) == CALL_EXPR);
538 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
539 this predicate should only be used during gimplification. See the
540 rationale for this in gimplify_modify_expr. */
542 static bool
543 is_gimple_mem_rhs_or_call (tree t)
545 /* If we're dealing with a renamable type, either source or dest must be
546 a renamed variable. */
547 if (is_gimple_reg_type (TREE_TYPE (t)))
548 return is_gimple_val (t);
549 else
550 return (is_gimple_val (t) || is_gimple_lvalue (t)
551 || TREE_CODE (t) == CALL_EXPR);
554 /* Create a temporary with a name derived from VAL. Subroutine of
555 lookup_tmp_var; nobody else should call this function. */
557 static inline tree
558 create_tmp_from_val (tree val, bool is_formal)
560 /* Drop all qualifiers and address-space information from the value type. */
561 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
562 tree var = create_tmp_var (type, get_name (val));
563 if (is_formal
564 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
565 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
566 DECL_GIMPLE_REG_P (var) = 1;
567 return var;
570 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
571 an existing expression temporary. */
573 static tree
574 lookup_tmp_var (tree val, bool is_formal)
576 tree ret;
578 /* If not optimizing, never really reuse a temporary. local-alloc
579 won't allocate any variable that is used in more than one basic
580 block, which means it will go into memory, causing much extra
581 work in reload and final and poorer code generation, outweighing
582 the extra memory allocation here. */
583 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
584 ret = create_tmp_from_val (val, is_formal);
585 else
587 elt_t elt, *elt_p;
588 void **slot;
590 elt.val = val;
591 if (gimplify_ctxp->temp_htab == NULL)
592 gimplify_ctxp->temp_htab
593 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
594 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
595 if (*slot == NULL)
597 elt_p = XNEW (elt_t);
598 elt_p->val = val;
599 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
600 *slot = (void *) elt_p;
602 else
604 elt_p = (elt_t *) *slot;
605 ret = elt_p->temp;
609 return ret;
612 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
614 static tree
615 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
616 bool is_formal)
618 tree t, mod;
620 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
621 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
622 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
623 fb_rvalue);
625 if (gimplify_ctxp->into_ssa
626 && is_gimple_reg_type (TREE_TYPE (val)))
627 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
628 else
629 t = lookup_tmp_var (val, is_formal);
631 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
633 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
635 /* gimplify_modify_expr might want to reduce this further. */
636 gimplify_and_add (mod, pre_p);
637 ggc_free (mod);
639 return t;
642 /* Return a formal temporary variable initialized with VAL. PRE_P is as
643 in gimplify_expr. Only use this function if:
645 1) The value of the unfactored expression represented by VAL will not
646 change between the initialization and use of the temporary, and
647 2) The temporary will not be otherwise modified.
649 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
650 and #2 means it is inappropriate for && temps.
652 For other cases, use get_initialized_tmp_var instead. */
654 tree
655 get_formal_tmp_var (tree val, gimple_seq *pre_p)
657 return internal_get_tmp_var (val, pre_p, NULL, true);
660 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
661 are as in gimplify_expr. */
663 tree
664 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
666 return internal_get_tmp_var (val, pre_p, post_p, false);
669 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
670 generate debug info for them; otherwise don't. */
672 void
673 declare_vars (tree vars, gimple scope, bool debug_info)
675 tree last = vars;
676 if (last)
678 tree temps, block;
680 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
682 temps = nreverse (last);
684 block = gimple_bind_block (scope);
685 gcc_assert (!block || TREE_CODE (block) == BLOCK);
686 if (!block || !debug_info)
688 DECL_CHAIN (last) = gimple_bind_vars (scope);
689 gimple_bind_set_vars (scope, temps);
691 else
693 /* We need to attach the nodes both to the BIND_EXPR and to its
694 associated BLOCK for debugging purposes. The key point here
695 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
696 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
697 if (BLOCK_VARS (block))
698 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
699 else
701 gimple_bind_set_vars (scope,
702 chainon (gimple_bind_vars (scope), temps));
703 BLOCK_VARS (block) = temps;
709 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
710 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
711 no such upper bound can be obtained. */
713 static void
714 force_constant_size (tree var)
716 /* The only attempt we make is by querying the maximum size of objects
717 of the variable's type. */
719 HOST_WIDE_INT max_size;
721 gcc_assert (TREE_CODE (var) == VAR_DECL);
723 max_size = max_int_size_in_bytes (TREE_TYPE (var));
725 gcc_assert (max_size >= 0);
727 DECL_SIZE_UNIT (var)
728 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
729 DECL_SIZE (var)
730 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
733 /* Push the temporary variable TMP into the current binding. */
735 void
736 gimple_add_tmp_var (tree tmp)
738 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
740 /* Later processing assumes that the object size is constant, which might
741 not be true at this point. Force the use of a constant upper bound in
742 this case. */
743 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
744 force_constant_size (tmp);
746 DECL_CONTEXT (tmp) = current_function_decl;
747 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
749 if (gimplify_ctxp)
751 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
752 gimplify_ctxp->temps = tmp;
754 /* Mark temporaries local within the nearest enclosing parallel. */
755 if (gimplify_omp_ctxp)
757 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
758 while (ctx && ctx->region_type == ORT_WORKSHARE)
759 ctx = ctx->outer_context;
760 if (ctx)
761 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
764 else if (cfun)
765 record_vars (tmp);
766 else
768 gimple_seq body_seq;
770 /* This case is for nested functions. We need to expose the locals
771 they create. */
772 body_seq = gimple_body (current_function_decl);
773 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
777 /* Determine whether to assign a location to the statement GS. */
779 static bool
780 should_carry_location_p (gimple gs)
782 /* Don't emit a line note for a label. We particularly don't want to
783 emit one for the break label, since it doesn't actually correspond
784 to the beginning of the loop/switch. */
785 if (gimple_code (gs) == GIMPLE_LABEL)
786 return false;
788 return true;
791 /* Return true if a location should not be emitted for this statement
792 by annotate_one_with_location. */
794 static inline bool
795 gimple_do_not_emit_location_p (gimple g)
797 return gimple_plf (g, GF_PLF_1);
800 /* Mark statement G so a location will not be emitted by
801 annotate_one_with_location. */
803 static inline void
804 gimple_set_do_not_emit_location (gimple g)
806 /* The PLF flags are initialized to 0 when a new tuple is created,
807 so no need to initialize it anywhere. */
808 gimple_set_plf (g, GF_PLF_1, true);
811 /* Set the location for gimple statement GS to LOCATION. */
813 static void
814 annotate_one_with_location (gimple gs, location_t location)
816 if (!gimple_has_location (gs)
817 && !gimple_do_not_emit_location_p (gs)
818 && should_carry_location_p (gs))
819 gimple_set_location (gs, location);
822 /* Set LOCATION for all the statements after iterator GSI in sequence
823 SEQ. If GSI is pointing to the end of the sequence, start with the
824 first statement in SEQ. */
826 static void
827 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
828 location_t location)
830 if (gsi_end_p (gsi))
831 gsi = gsi_start (seq);
832 else
833 gsi_next (&gsi);
835 for (; !gsi_end_p (gsi); gsi_next (&gsi))
836 annotate_one_with_location (gsi_stmt (gsi), location);
839 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
841 void
842 annotate_all_with_location (gimple_seq stmt_p, location_t location)
844 gimple_stmt_iterator i;
846 if (gimple_seq_empty_p (stmt_p))
847 return;
849 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
851 gimple gs = gsi_stmt (i);
852 annotate_one_with_location (gs, location);
856 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
857 nodes that are referenced more than once in GENERIC functions. This is
858 necessary because gimplification (translation into GIMPLE) is performed
859 by modifying tree nodes in-place, so gimplication of a shared node in a
860 first context could generate an invalid GIMPLE form in a second context.
862 This is achieved with a simple mark/copy/unmark algorithm that walks the
863 GENERIC representation top-down, marks nodes with TREE_VISITED the first
864 time it encounters them, duplicates them if they already have TREE_VISITED
865 set, and finally removes the TREE_VISITED marks it has set.
867 The algorithm works only at the function level, i.e. it generates a GENERIC
868 representation of a function with no nodes shared within the function when
869 passed a GENERIC function (except for nodes that are allowed to be shared).
871 At the global level, it is also necessary to unshare tree nodes that are
872 referenced in more than one function, for the same aforementioned reason.
873 This requires some cooperation from the front-end. There are 2 strategies:
875 1. Manual unsharing. The front-end needs to call unshare_expr on every
876 expression that might end up being shared across functions.
878 2. Deep unsharing. This is an extension of regular unsharing. Instead
879 of calling unshare_expr on expressions that might be shared across
880 functions, the front-end pre-marks them with TREE_VISITED. This will
881 ensure that they are unshared on the first reference within functions
882 when the regular unsharing algorithm runs. The counterpart is that
883 this algorithm must look deeper than for manual unsharing, which is
884 specified by LANG_HOOKS_DEEP_UNSHARING.
886 If there are only few specific cases of node sharing across functions, it is
887 probably easier for a front-end to unshare the expressions manually. On the
888 contrary, if the expressions generated at the global level are as widespread
889 as expressions generated within functions, deep unsharing is very likely the
890 way to go. */
892 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
893 These nodes model computations that must be done once. If we were to
894 unshare something like SAVE_EXPR(i++), the gimplification process would
895 create wrong code. However, if DATA is non-null, it must hold a pointer
896 set that is used to unshare the subtrees of these nodes. */
898 static tree
899 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
901 tree t = *tp;
902 enum tree_code code = TREE_CODE (t);
904 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
905 copy their subtrees if we can make sure to do it only once. */
906 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
908 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
910 else
911 *walk_subtrees = 0;
914 /* Stop at types, decls, constants like copy_tree_r. */
915 else if (TREE_CODE_CLASS (code) == tcc_type
916 || TREE_CODE_CLASS (code) == tcc_declaration
917 || TREE_CODE_CLASS (code) == tcc_constant
918 /* We can't do anything sensible with a BLOCK used as an
919 expression, but we also can't just die when we see it
920 because of non-expression uses. So we avert our eyes
921 and cross our fingers. Silly Java. */
922 || code == BLOCK)
923 *walk_subtrees = 0;
925 /* Cope with the statement expression extension. */
926 else if (code == STATEMENT_LIST)
929 /* Leave the bulk of the work to copy_tree_r itself. */
930 else
931 copy_tree_r (tp, walk_subtrees, NULL);
933 return NULL_TREE;
936 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
937 If *TP has been visited already, then *TP is deeply copied by calling
938 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
940 static tree
941 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
943 tree t = *tp;
944 enum tree_code code = TREE_CODE (t);
946 /* Skip types, decls, and constants. But we do want to look at their
947 types and the bounds of types. Mark them as visited so we properly
948 unmark their subtrees on the unmark pass. If we've already seen them,
949 don't look down further. */
950 if (TREE_CODE_CLASS (code) == tcc_type
951 || TREE_CODE_CLASS (code) == tcc_declaration
952 || TREE_CODE_CLASS (code) == tcc_constant)
954 if (TREE_VISITED (t))
955 *walk_subtrees = 0;
956 else
957 TREE_VISITED (t) = 1;
960 /* If this node has been visited already, unshare it and don't look
961 any deeper. */
962 else if (TREE_VISITED (t))
964 walk_tree (tp, mostly_copy_tree_r, data, NULL);
965 *walk_subtrees = 0;
968 /* Otherwise, mark the node as visited and keep looking. */
969 else
970 TREE_VISITED (t) = 1;
972 return NULL_TREE;
975 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
976 copy_if_shared_r callback unmodified. */
978 static inline void
979 copy_if_shared (tree *tp, void *data)
981 walk_tree (tp, copy_if_shared_r, data, NULL);
984 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
985 any nested functions. */
987 static void
988 unshare_body (tree fndecl)
990 struct cgraph_node *cgn = cgraph_get_node (fndecl);
991 /* If the language requires deep unsharing, we need a pointer set to make
992 sure we don't repeatedly unshare subtrees of unshareable nodes. */
993 struct pointer_set_t *visited
994 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
996 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
997 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
998 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
1000 if (visited)
1001 pointer_set_destroy (visited);
1003 if (cgn)
1004 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1005 unshare_body (cgn->symbol.decl);
1008 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
1009 Subtrees are walked until the first unvisited node is encountered. */
1011 static tree
1012 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1014 tree t = *tp;
1016 /* If this node has been visited, unmark it and keep looking. */
1017 if (TREE_VISITED (t))
1018 TREE_VISITED (t) = 0;
1020 /* Otherwise, don't look any deeper. */
1021 else
1022 *walk_subtrees = 0;
1024 return NULL_TREE;
1027 /* Unmark the visited trees rooted at *TP. */
1029 static inline void
1030 unmark_visited (tree *tp)
1032 walk_tree (tp, unmark_visited_r, NULL, NULL);
1035 /* Likewise, but mark all trees as not visited. */
1037 static void
1038 unvisit_body (tree fndecl)
1040 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1042 unmark_visited (&DECL_SAVED_TREE (fndecl));
1043 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1044 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1046 if (cgn)
1047 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1048 unvisit_body (cgn->symbol.decl);
1051 /* Unconditionally make an unshared copy of EXPR. This is used when using
1052 stored expressions which span multiple functions, such as BINFO_VTABLE,
1053 as the normal unsharing process can't tell that they're shared. */
1055 tree
1056 unshare_expr (tree expr)
1058 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1059 return expr;
1062 /* Worker for unshare_expr_without_location. */
1064 static tree
1065 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1067 if (EXPR_P (*tp))
1068 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1069 else
1070 *walk_subtrees = 0;
1071 return NULL_TREE;
1074 /* Similar to unshare_expr but also prune all expression locations
1075 from EXPR. */
1077 tree
1078 unshare_expr_without_location (tree expr)
1080 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1081 if (EXPR_P (expr))
1082 walk_tree (&expr, prune_expr_location, NULL, NULL);
1083 return expr;
1086 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1087 contain statements and have a value. Assign its value to a temporary
1088 and give it void_type_node. Return the temporary, or NULL_TREE if
1089 WRAPPER was already void. */
1091 tree
1092 voidify_wrapper_expr (tree wrapper, tree temp)
1094 tree type = TREE_TYPE (wrapper);
1095 if (type && !VOID_TYPE_P (type))
1097 tree *p;
1099 /* Set p to point to the body of the wrapper. Loop until we find
1100 something that isn't a wrapper. */
1101 for (p = &wrapper; p && *p; )
1103 switch (TREE_CODE (*p))
1105 case BIND_EXPR:
1106 TREE_SIDE_EFFECTS (*p) = 1;
1107 TREE_TYPE (*p) = void_type_node;
1108 /* For a BIND_EXPR, the body is operand 1. */
1109 p = &BIND_EXPR_BODY (*p);
1110 break;
1112 case CLEANUP_POINT_EXPR:
1113 case TRY_FINALLY_EXPR:
1114 case TRY_CATCH_EXPR:
1115 TREE_SIDE_EFFECTS (*p) = 1;
1116 TREE_TYPE (*p) = void_type_node;
1117 p = &TREE_OPERAND (*p, 0);
1118 break;
1120 case STATEMENT_LIST:
1122 tree_stmt_iterator i = tsi_last (*p);
1123 TREE_SIDE_EFFECTS (*p) = 1;
1124 TREE_TYPE (*p) = void_type_node;
1125 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1127 break;
1129 case COMPOUND_EXPR:
1130 /* Advance to the last statement. Set all container types to
1131 void. */
1132 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1134 TREE_SIDE_EFFECTS (*p) = 1;
1135 TREE_TYPE (*p) = void_type_node;
1137 break;
1139 case TRANSACTION_EXPR:
1140 TREE_SIDE_EFFECTS (*p) = 1;
1141 TREE_TYPE (*p) = void_type_node;
1142 p = &TRANSACTION_EXPR_BODY (*p);
1143 break;
1145 default:
1146 /* Assume that any tree upon which voidify_wrapper_expr is
1147 directly called is a wrapper, and that its body is op0. */
1148 if (p == &wrapper)
1150 TREE_SIDE_EFFECTS (*p) = 1;
1151 TREE_TYPE (*p) = void_type_node;
1152 p = &TREE_OPERAND (*p, 0);
1153 break;
1155 goto out;
1159 out:
1160 if (p == NULL || IS_EMPTY_STMT (*p))
1161 temp = NULL_TREE;
1162 else if (temp)
1164 /* The wrapper is on the RHS of an assignment that we're pushing
1165 down. */
1166 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1167 || TREE_CODE (temp) == MODIFY_EXPR);
1168 TREE_OPERAND (temp, 1) = *p;
1169 *p = temp;
1171 else
1173 temp = create_tmp_var (type, "retval");
1174 *p = build2 (INIT_EXPR, type, temp, *p);
1177 return temp;
1180 return NULL_TREE;
1183 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1184 a temporary through which they communicate. */
1186 static void
1187 build_stack_save_restore (gimple *save, gimple *restore)
1189 tree tmp_var;
1191 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1192 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1193 gimple_call_set_lhs (*save, tmp_var);
1195 *restore
1196 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1197 1, tmp_var);
1200 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1202 static enum gimplify_status
1203 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1205 tree bind_expr = *expr_p;
1206 bool old_save_stack = gimplify_ctxp->save_stack;
1207 tree t;
1208 gimple gimple_bind;
1209 gimple_seq body, cleanup;
1210 gimple stack_save;
1212 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1214 /* Mark variables seen in this bind expr. */
1215 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1217 if (TREE_CODE (t) == VAR_DECL)
1219 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1221 /* Mark variable as local. */
1222 if (ctx && !DECL_EXTERNAL (t)
1223 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1224 || splay_tree_lookup (ctx->variables,
1225 (splay_tree_key) t) == NULL))
1226 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1228 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1230 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1231 cfun->has_local_explicit_reg_vars = true;
1234 /* Preliminarily mark non-addressed complex variables as eligible
1235 for promotion to gimple registers. We'll transform their uses
1236 as we find them. */
1237 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1238 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1239 && !TREE_THIS_VOLATILE (t)
1240 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1241 && !needs_to_live_in_memory (t))
1242 DECL_GIMPLE_REG_P (t) = 1;
1245 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1246 BIND_EXPR_BLOCK (bind_expr));
1247 gimple_push_bind_expr (gimple_bind);
1249 gimplify_ctxp->save_stack = false;
1251 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1252 body = NULL;
1253 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1254 gimple_bind_set_body (gimple_bind, body);
1256 cleanup = NULL;
1257 stack_save = NULL;
1258 if (gimplify_ctxp->save_stack)
1260 gimple stack_restore;
1262 /* Save stack on entry and restore it on exit. Add a try_finally
1263 block to achieve this. Note that mudflap depends on the
1264 format of the emitted code: see mx_register_decls(). */
1265 build_stack_save_restore (&stack_save, &stack_restore);
1267 gimplify_seq_add_stmt (&cleanup, stack_restore);
1270 /* Add clobbers for all variables that go out of scope. */
1271 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1273 if (TREE_CODE (t) == VAR_DECL
1274 && !is_global_var (t)
1275 && DECL_CONTEXT (t) == current_function_decl
1276 && !DECL_HARD_REGISTER (t)
1277 && !TREE_THIS_VOLATILE (t)
1278 && !DECL_HAS_VALUE_EXPR_P (t)
1279 /* Only care for variables that have to be in memory. Others
1280 will be rewritten into SSA names, hence moved to the top-level. */
1281 && !is_gimple_reg (t)
1282 && flag_stack_reuse != SR_NONE)
1284 tree clobber = build_constructor (TREE_TYPE (t),
1285 NULL);
1286 TREE_THIS_VOLATILE (clobber) = 1;
1287 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1291 if (cleanup)
1293 gimple gs;
1294 gimple_seq new_body;
1296 new_body = NULL;
1297 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1298 GIMPLE_TRY_FINALLY);
1300 if (stack_save)
1301 gimplify_seq_add_stmt (&new_body, stack_save);
1302 gimplify_seq_add_stmt (&new_body, gs);
1303 gimple_bind_set_body (gimple_bind, new_body);
1306 gimplify_ctxp->save_stack = old_save_stack;
1307 gimple_pop_bind_expr ();
1309 gimplify_seq_add_stmt (pre_p, gimple_bind);
1311 if (temp)
1313 *expr_p = temp;
1314 return GS_OK;
1317 *expr_p = NULL_TREE;
1318 return GS_ALL_DONE;
1321 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1322 GIMPLE value, it is assigned to a new temporary and the statement is
1323 re-written to return the temporary.
1325 PRE_P points to the sequence where side effects that must happen before
1326 STMT should be stored. */
1328 static enum gimplify_status
1329 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1331 gimple ret;
1332 tree ret_expr = TREE_OPERAND (stmt, 0);
1333 tree result_decl, result;
1335 if (ret_expr == error_mark_node)
1336 return GS_ERROR;
1338 if (!ret_expr
1339 || TREE_CODE (ret_expr) == RESULT_DECL
1340 || ret_expr == error_mark_node)
1342 gimple ret = gimple_build_return (ret_expr);
1343 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1344 gimplify_seq_add_stmt (pre_p, ret);
1345 return GS_ALL_DONE;
1348 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1349 result_decl = NULL_TREE;
1350 else
1352 result_decl = TREE_OPERAND (ret_expr, 0);
1354 /* See through a return by reference. */
1355 if (TREE_CODE (result_decl) == INDIRECT_REF)
1356 result_decl = TREE_OPERAND (result_decl, 0);
1358 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1359 || TREE_CODE (ret_expr) == INIT_EXPR)
1360 && TREE_CODE (result_decl) == RESULT_DECL);
1363 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1364 Recall that aggregate_value_p is FALSE for any aggregate type that is
1365 returned in registers. If we're returning values in registers, then
1366 we don't want to extend the lifetime of the RESULT_DECL, particularly
1367 across another call. In addition, for those aggregates for which
1368 hard_function_value generates a PARALLEL, we'll die during normal
1369 expansion of structure assignments; there's special code in expand_return
1370 to handle this case that does not exist in expand_expr. */
1371 if (!result_decl)
1372 result = NULL_TREE;
1373 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1375 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1377 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1378 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1379 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1380 should be effectively allocated by the caller, i.e. all calls to
1381 this function must be subject to the Return Slot Optimization. */
1382 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1383 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1385 result = result_decl;
1387 else if (gimplify_ctxp->return_temp)
1388 result = gimplify_ctxp->return_temp;
1389 else
1391 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1393 /* ??? With complex control flow (usually involving abnormal edges),
1394 we can wind up warning about an uninitialized value for this. Due
1395 to how this variable is constructed and initialized, this is never
1396 true. Give up and never warn. */
1397 TREE_NO_WARNING (result) = 1;
1399 gimplify_ctxp->return_temp = result;
1402 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1403 Then gimplify the whole thing. */
1404 if (result != result_decl)
1405 TREE_OPERAND (ret_expr, 0) = result;
1407 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1409 ret = gimple_build_return (result);
1410 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1411 gimplify_seq_add_stmt (pre_p, ret);
1413 return GS_ALL_DONE;
1416 /* Gimplify a variable-length array DECL. */
1418 static void
1419 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1421 /* This is a variable-sized decl. Simplify its size and mark it
1422 for deferred expansion. Note that mudflap depends on the format
1423 of the emitted code: see mx_register_decls(). */
1424 tree t, addr, ptr_type;
1426 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1427 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1429 /* All occurrences of this decl in final gimplified code will be
1430 replaced by indirection. Setting DECL_VALUE_EXPR does two
1431 things: First, it lets the rest of the gimplifier know what
1432 replacement to use. Second, it lets the debug info know
1433 where to find the value. */
1434 ptr_type = build_pointer_type (TREE_TYPE (decl));
1435 addr = create_tmp_var (ptr_type, get_name (decl));
1436 DECL_IGNORED_P (addr) = 0;
1437 t = build_fold_indirect_ref (addr);
1438 TREE_THIS_NOTRAP (t) = 1;
1439 SET_DECL_VALUE_EXPR (decl, t);
1440 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1442 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1443 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1444 size_int (DECL_ALIGN (decl)));
1445 /* The call has been built for a variable-sized object. */
1446 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1447 t = fold_convert (ptr_type, t);
1448 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1450 gimplify_and_add (t, seq_p);
1452 /* Indicate that we need to restore the stack level when the
1453 enclosing BIND_EXPR is exited. */
1454 gimplify_ctxp->save_stack = true;
1457 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1458 and initialization explicit. */
1460 static enum gimplify_status
1461 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1463 tree stmt = *stmt_p;
1464 tree decl = DECL_EXPR_DECL (stmt);
1466 *stmt_p = NULL_TREE;
1468 if (TREE_TYPE (decl) == error_mark_node)
1469 return GS_ERROR;
1471 if ((TREE_CODE (decl) == TYPE_DECL
1472 || TREE_CODE (decl) == VAR_DECL)
1473 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1474 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1476 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1477 in case its size expressions contain problematic nodes like CALL_EXPR. */
1478 if (TREE_CODE (decl) == TYPE_DECL
1479 && DECL_ORIGINAL_TYPE (decl)
1480 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1481 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1483 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1485 tree init = DECL_INITIAL (decl);
1487 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1488 || (!TREE_STATIC (decl)
1489 && flag_stack_check == GENERIC_STACK_CHECK
1490 && compare_tree_int (DECL_SIZE_UNIT (decl),
1491 STACK_CHECK_MAX_VAR_SIZE) > 0))
1492 gimplify_vla_decl (decl, seq_p);
1494 /* Some front ends do not explicitly declare all anonymous
1495 artificial variables. We compensate here by declaring the
1496 variables, though it would be better if the front ends would
1497 explicitly declare them. */
1498 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1499 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1500 gimple_add_tmp_var (decl);
1502 if (init && init != error_mark_node)
1504 if (!TREE_STATIC (decl))
1506 DECL_INITIAL (decl) = NULL_TREE;
1507 init = build2 (INIT_EXPR, void_type_node, decl, init);
1508 gimplify_and_add (init, seq_p);
1509 ggc_free (init);
1511 else
1512 /* We must still examine initializers for static variables
1513 as they may contain a label address. */
1514 walk_tree (&init, force_labels_r, NULL, NULL);
1518 return GS_ALL_DONE;
1521 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1522 and replacing the LOOP_EXPR with goto, but if the loop contains an
1523 EXIT_EXPR, we need to append a label for it to jump to. */
1525 static enum gimplify_status
1526 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1528 tree saved_label = gimplify_ctxp->exit_label;
1529 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1531 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1533 gimplify_ctxp->exit_label = NULL_TREE;
1535 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1537 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1539 if (gimplify_ctxp->exit_label)
1540 gimplify_seq_add_stmt (pre_p,
1541 gimple_build_label (gimplify_ctxp->exit_label));
1543 gimplify_ctxp->exit_label = saved_label;
1545 *expr_p = NULL;
1546 return GS_ALL_DONE;
1549 /* Gimplify a statement list onto a sequence. These may be created either
1550 by an enlightened front-end, or by shortcut_cond_expr. */
1552 static enum gimplify_status
1553 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1555 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1557 tree_stmt_iterator i = tsi_start (*expr_p);
1559 while (!tsi_end_p (i))
1561 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1562 tsi_delink (&i);
1565 if (temp)
1567 *expr_p = temp;
1568 return GS_OK;
1571 return GS_ALL_DONE;
1574 /* Compare two case labels. Because the front end should already have
1575 made sure that case ranges do not overlap, it is enough to only compare
1576 the CASE_LOW values of each case label. */
1578 static int
1579 compare_case_labels (const void *p1, const void *p2)
1581 const_tree const case1 = *(const_tree const*)p1;
1582 const_tree const case2 = *(const_tree const*)p2;
1584 /* The 'default' case label always goes first. */
1585 if (!CASE_LOW (case1))
1586 return -1;
1587 else if (!CASE_LOW (case2))
1588 return 1;
1589 else
1590 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1593 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1595 void
1596 sort_case_labels (vec<tree> label_vec)
1598 label_vec.qsort (compare_case_labels);
1601 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1603 LABELS is a vector that contains all case labels to look at.
1605 INDEX_TYPE is the type of the switch index expression. Case labels
1606 in LABELS are discarded if their values are not in the value range
1607 covered by INDEX_TYPE. The remaining case label values are folded
1608 to INDEX_TYPE.
1610 If a default case exists in LABELS, it is removed from LABELS and
1611 returned in DEFAULT_CASEP. If no default case exists, but the
1612 case labels already cover the whole range of INDEX_TYPE, a default
1613 case is returned pointing to one of the existing case labels.
1614 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1616 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1617 apply and no action is taken regardless of whether a default case is
1618 found or not. */
1620 void
1621 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1622 tree index_type,
1623 tree *default_casep)
1625 tree min_value, max_value;
1626 tree default_case = NULL_TREE;
1627 size_t i, len;
1629 i = 0;
1630 min_value = TYPE_MIN_VALUE (index_type);
1631 max_value = TYPE_MAX_VALUE (index_type);
1632 while (i < labels.length ())
1634 tree elt = labels[i];
1635 tree low = CASE_LOW (elt);
1636 tree high = CASE_HIGH (elt);
1637 bool remove_element = FALSE;
1639 if (low)
1641 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1642 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1644 /* This is a non-default case label, i.e. it has a value.
1646 See if the case label is reachable within the range of
1647 the index type. Remove out-of-range case values. Turn
1648 case ranges into a canonical form (high > low strictly)
1649 and convert the case label values to the index type.
1651 NB: The type of gimple_switch_index() may be the promoted
1652 type, but the case labels retain the original type. */
1654 if (high)
1656 /* This is a case range. Discard empty ranges.
1657 If the bounds or the range are equal, turn this
1658 into a simple (one-value) case. */
1659 int cmp = tree_int_cst_compare (high, low);
1660 if (cmp < 0)
1661 remove_element = TRUE;
1662 else if (cmp == 0)
1663 high = NULL_TREE;
1666 if (! high)
1668 /* If the simple case value is unreachable, ignore it. */
1669 if ((TREE_CODE (min_value) == INTEGER_CST
1670 && tree_int_cst_compare (low, min_value) < 0)
1671 || (TREE_CODE (max_value) == INTEGER_CST
1672 && tree_int_cst_compare (low, max_value) > 0))
1673 remove_element = TRUE;
1674 else
1675 low = fold_convert (index_type, low);
1677 else
1679 /* If the entire case range is unreachable, ignore it. */
1680 if ((TREE_CODE (min_value) == INTEGER_CST
1681 && tree_int_cst_compare (high, min_value) < 0)
1682 || (TREE_CODE (max_value) == INTEGER_CST
1683 && tree_int_cst_compare (low, max_value) > 0))
1684 remove_element = TRUE;
1685 else
1687 /* If the lower bound is less than the index type's
1688 minimum value, truncate the range bounds. */
1689 if (TREE_CODE (min_value) == INTEGER_CST
1690 && tree_int_cst_compare (low, min_value) < 0)
1691 low = min_value;
1692 low = fold_convert (index_type, low);
1694 /* If the upper bound is greater than the index type's
1695 maximum value, truncate the range bounds. */
1696 if (TREE_CODE (max_value) == INTEGER_CST
1697 && tree_int_cst_compare (high, max_value) > 0)
1698 high = max_value;
1699 high = fold_convert (index_type, high);
1701 /* We may have folded a case range to a one-value case. */
1702 if (tree_int_cst_equal (low, high))
1703 high = NULL_TREE;
1707 CASE_LOW (elt) = low;
1708 CASE_HIGH (elt) = high;
1710 else
1712 gcc_assert (!default_case);
1713 default_case = elt;
1714 /* The default case must be passed separately to the
1715 gimple_build_switch routine. But if DEFAULT_CASEP
1716 is NULL, we do not remove the default case (it would
1717 be completely lost). */
1718 if (default_casep)
1719 remove_element = TRUE;
1722 if (remove_element)
1723 labels.ordered_remove (i);
1724 else
1725 i++;
1727 len = i;
1729 if (!labels.is_empty ())
1730 sort_case_labels (labels);
1732 if (default_casep && !default_case)
1734 /* If the switch has no default label, add one, so that we jump
1735 around the switch body. If the labels already cover the whole
1736 range of the switch index_type, add the default label pointing
1737 to one of the existing labels. */
1738 if (len
1739 && TYPE_MIN_VALUE (index_type)
1740 && TYPE_MAX_VALUE (index_type)
1741 && tree_int_cst_equal (CASE_LOW (labels[0]),
1742 TYPE_MIN_VALUE (index_type)))
1744 tree low, high = CASE_HIGH (labels[len - 1]);
1745 if (!high)
1746 high = CASE_LOW (labels[len - 1]);
1747 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1749 for (i = 1; i < len; i++)
1751 high = CASE_LOW (labels[i]);
1752 low = CASE_HIGH (labels[i - 1]);
1753 if (!low)
1754 low = CASE_LOW (labels[i - 1]);
1755 if ((TREE_INT_CST_LOW (low) + 1
1756 != TREE_INT_CST_LOW (high))
1757 || (TREE_INT_CST_HIGH (low)
1758 + (TREE_INT_CST_LOW (high) == 0)
1759 != TREE_INT_CST_HIGH (high)))
1760 break;
1762 if (i == len)
1764 tree label = CASE_LABEL (labels[0]);
1765 default_case = build_case_label (NULL_TREE, NULL_TREE,
1766 label);
1772 if (default_casep)
1773 *default_casep = default_case;
1776 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1777 branch to. */
1779 static enum gimplify_status
1780 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1782 tree switch_expr = *expr_p;
1783 gimple_seq switch_body_seq = NULL;
1784 enum gimplify_status ret;
1785 tree index_type = TREE_TYPE (switch_expr);
1786 if (index_type == NULL_TREE)
1787 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1789 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1790 fb_rvalue);
1791 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1792 return ret;
1794 if (SWITCH_BODY (switch_expr))
1796 vec<tree> labels;
1797 vec<tree> saved_labels;
1798 tree default_case = NULL_TREE;
1799 gimple gimple_switch;
1801 /* If someone can be bothered to fill in the labels, they can
1802 be bothered to null out the body too. */
1803 gcc_assert (!SWITCH_LABELS (switch_expr));
1805 /* Save old labels, get new ones from body, then restore the old
1806 labels. Save all the things from the switch body to append after. */
1807 saved_labels = gimplify_ctxp->case_labels;
1808 gimplify_ctxp->case_labels.create (8);
1810 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1811 labels = gimplify_ctxp->case_labels;
1812 gimplify_ctxp->case_labels = saved_labels;
1814 preprocess_case_label_vec_for_gimple (labels, index_type,
1815 &default_case);
1817 if (!default_case)
1819 gimple new_default;
1821 default_case
1822 = build_case_label (NULL_TREE, NULL_TREE,
1823 create_artificial_label (UNKNOWN_LOCATION));
1824 new_default = gimple_build_label (CASE_LABEL (default_case));
1825 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1828 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1829 default_case, labels);
1830 gimplify_seq_add_stmt (pre_p, gimple_switch);
1831 gimplify_seq_add_seq (pre_p, switch_body_seq);
1832 labels.release ();
1834 else
1835 gcc_assert (SWITCH_LABELS (switch_expr));
1837 return GS_ALL_DONE;
1840 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1842 static enum gimplify_status
1843 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1845 struct gimplify_ctx *ctxp;
1846 gimple gimple_label;
1848 /* Invalid OpenMP programs can play Duff's Device type games with
1849 #pragma omp parallel. At least in the C front end, we don't
1850 detect such invalid branches until after gimplification. */
1851 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1852 if (ctxp->case_labels.exists ())
1853 break;
1855 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1856 ctxp->case_labels.safe_push (*expr_p);
1857 gimplify_seq_add_stmt (pre_p, gimple_label);
1859 return GS_ALL_DONE;
1862 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1863 if necessary. */
1865 tree
1866 build_and_jump (tree *label_p)
1868 if (label_p == NULL)
1869 /* If there's nowhere to jump, just fall through. */
1870 return NULL_TREE;
1872 if (*label_p == NULL_TREE)
1874 tree label = create_artificial_label (UNKNOWN_LOCATION);
1875 *label_p = label;
1878 return build1 (GOTO_EXPR, void_type_node, *label_p);
1881 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1882 This also involves building a label to jump to and communicating it to
1883 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1885 static enum gimplify_status
1886 gimplify_exit_expr (tree *expr_p)
1888 tree cond = TREE_OPERAND (*expr_p, 0);
1889 tree expr;
1891 expr = build_and_jump (&gimplify_ctxp->exit_label);
1892 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1893 *expr_p = expr;
1895 return GS_OK;
1898 /* A helper function to be called via walk_tree. Mark all labels under *TP
1899 as being forced. To be called for DECL_INITIAL of static variables. */
1901 tree
1902 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1904 if (TYPE_P (*tp))
1905 *walk_subtrees = 0;
1906 if (TREE_CODE (*tp) == LABEL_DECL)
1907 FORCED_LABEL (*tp) = 1;
1909 return NULL_TREE;
1912 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1913 different from its canonical type, wrap the whole thing inside a
1914 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1915 type.
1917 The canonical type of a COMPONENT_REF is the type of the field being
1918 referenced--unless the field is a bit-field which can be read directly
1919 in a smaller mode, in which case the canonical type is the
1920 sign-appropriate type corresponding to that mode. */
1922 static void
1923 canonicalize_component_ref (tree *expr_p)
1925 tree expr = *expr_p;
1926 tree type;
1928 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1930 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1931 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1932 else
1933 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1935 /* One could argue that all the stuff below is not necessary for
1936 the non-bitfield case and declare it a FE error if type
1937 adjustment would be needed. */
1938 if (TREE_TYPE (expr) != type)
1940 #ifdef ENABLE_TYPES_CHECKING
1941 tree old_type = TREE_TYPE (expr);
1942 #endif
1943 int type_quals;
1945 /* We need to preserve qualifiers and propagate them from
1946 operand 0. */
1947 type_quals = TYPE_QUALS (type)
1948 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1949 if (TYPE_QUALS (type) != type_quals)
1950 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1952 /* Set the type of the COMPONENT_REF to the underlying type. */
1953 TREE_TYPE (expr) = type;
1955 #ifdef ENABLE_TYPES_CHECKING
1956 /* It is now a FE error, if the conversion from the canonical
1957 type to the original expression type is not useless. */
1958 gcc_assert (useless_type_conversion_p (old_type, type));
1959 #endif
1963 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1964 to foo, embed that change in the ADDR_EXPR by converting
1965 T array[U];
1966 (T *)&array
1968 &array[L]
1969 where L is the lower bound. For simplicity, only do this for constant
1970 lower bound.
1971 The constraint is that the type of &array[L] is trivially convertible
1972 to T *. */
1974 static void
1975 canonicalize_addr_expr (tree *expr_p)
1977 tree expr = *expr_p;
1978 tree addr_expr = TREE_OPERAND (expr, 0);
1979 tree datype, ddatype, pddatype;
1981 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1982 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1983 || TREE_CODE (addr_expr) != ADDR_EXPR)
1984 return;
1986 /* The addr_expr type should be a pointer to an array. */
1987 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1988 if (TREE_CODE (datype) != ARRAY_TYPE)
1989 return;
1991 /* The pointer to element type shall be trivially convertible to
1992 the expression pointer type. */
1993 ddatype = TREE_TYPE (datype);
1994 pddatype = build_pointer_type (ddatype);
1995 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1996 pddatype))
1997 return;
1999 /* The lower bound and element sizes must be constant. */
2000 if (!TYPE_SIZE_UNIT (ddatype)
2001 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
2002 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
2003 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
2004 return;
2006 /* All checks succeeded. Build a new node to merge the cast. */
2007 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
2008 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
2009 NULL_TREE, NULL_TREE);
2010 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
2012 /* We can have stripped a required restrict qualifier above. */
2013 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2014 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2017 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2018 underneath as appropriate. */
2020 static enum gimplify_status
2021 gimplify_conversion (tree *expr_p)
2023 location_t loc = EXPR_LOCATION (*expr_p);
2024 gcc_assert (CONVERT_EXPR_P (*expr_p));
2026 /* Then strip away all but the outermost conversion. */
2027 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2029 /* And remove the outermost conversion if it's useless. */
2030 if (tree_ssa_useless_type_conversion (*expr_p))
2031 *expr_p = TREE_OPERAND (*expr_p, 0);
2033 /* If we still have a conversion at the toplevel,
2034 then canonicalize some constructs. */
2035 if (CONVERT_EXPR_P (*expr_p))
2037 tree sub = TREE_OPERAND (*expr_p, 0);
2039 /* If a NOP conversion is changing the type of a COMPONENT_REF
2040 expression, then canonicalize its type now in order to expose more
2041 redundant conversions. */
2042 if (TREE_CODE (sub) == COMPONENT_REF)
2043 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2045 /* If a NOP conversion is changing a pointer to array of foo
2046 to a pointer to foo, embed that change in the ADDR_EXPR. */
2047 else if (TREE_CODE (sub) == ADDR_EXPR)
2048 canonicalize_addr_expr (expr_p);
2051 /* If we have a conversion to a non-register type force the
2052 use of a VIEW_CONVERT_EXPR instead. */
2053 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2054 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2055 TREE_OPERAND (*expr_p, 0));
2057 return GS_OK;
2060 /* Nonlocal VLAs seen in the current function. */
2061 static struct pointer_set_t *nonlocal_vlas;
2063 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
2064 static tree nonlocal_vla_vars;
2066 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2067 DECL_VALUE_EXPR, and it's worth re-examining things. */
2069 static enum gimplify_status
2070 gimplify_var_or_parm_decl (tree *expr_p)
2072 tree decl = *expr_p;
2074 /* ??? If this is a local variable, and it has not been seen in any
2075 outer BIND_EXPR, then it's probably the result of a duplicate
2076 declaration, for which we've already issued an error. It would
2077 be really nice if the front end wouldn't leak these at all.
2078 Currently the only known culprit is C++ destructors, as seen
2079 in g++.old-deja/g++.jason/binding.C. */
2080 if (TREE_CODE (decl) == VAR_DECL
2081 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2082 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2083 && decl_function_context (decl) == current_function_decl)
2085 gcc_assert (seen_error ());
2086 return GS_ERROR;
2089 /* When within an OpenMP context, notice uses of variables. */
2090 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2091 return GS_ALL_DONE;
2093 /* If the decl is an alias for another expression, substitute it now. */
2094 if (DECL_HAS_VALUE_EXPR_P (decl))
2096 tree value_expr = DECL_VALUE_EXPR (decl);
2098 /* For referenced nonlocal VLAs add a decl for debugging purposes
2099 to the current function. */
2100 if (TREE_CODE (decl) == VAR_DECL
2101 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2102 && nonlocal_vlas != NULL
2103 && TREE_CODE (value_expr) == INDIRECT_REF
2104 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2105 && decl_function_context (decl) != current_function_decl)
2107 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2108 while (ctx && ctx->region_type == ORT_WORKSHARE)
2109 ctx = ctx->outer_context;
2110 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2112 tree copy = copy_node (decl);
2114 lang_hooks.dup_lang_specific_decl (copy);
2115 SET_DECL_RTL (copy, 0);
2116 TREE_USED (copy) = 1;
2117 DECL_CHAIN (copy) = nonlocal_vla_vars;
2118 nonlocal_vla_vars = copy;
2119 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2120 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2124 *expr_p = unshare_expr (value_expr);
2125 return GS_OK;
2128 return GS_ALL_DONE;
2131 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2132 node *EXPR_P.
2134 compound_lval
2135 : min_lval '[' val ']'
2136 | min_lval '.' ID
2137 | compound_lval '[' val ']'
2138 | compound_lval '.' ID
2140 This is not part of the original SIMPLE definition, which separates
2141 array and member references, but it seems reasonable to handle them
2142 together. Also, this way we don't run into problems with union
2143 aliasing; gcc requires that for accesses through a union to alias, the
2144 union reference must be explicit, which was not always the case when we
2145 were splitting up array and member refs.
2147 PRE_P points to the sequence where side effects that must happen before
2148 *EXPR_P should be stored.
2150 POST_P points to the sequence where side effects that must happen after
2151 *EXPR_P should be stored. */
2153 static enum gimplify_status
2154 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2155 fallback_t fallback)
2157 tree *p;
2158 vec<tree> expr_stack;
2159 enum gimplify_status ret = GS_ALL_DONE, tret;
2160 int i;
2161 location_t loc = EXPR_LOCATION (*expr_p);
2162 tree expr = *expr_p;
2164 /* Create a stack of the subexpressions so later we can walk them in
2165 order from inner to outer. */
2166 expr_stack.create (10);
2168 /* We can handle anything that get_inner_reference can deal with. */
2169 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2171 restart:
2172 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2173 if (TREE_CODE (*p) == INDIRECT_REF)
2174 *p = fold_indirect_ref_loc (loc, *p);
2176 if (handled_component_p (*p))
2178 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2179 additional COMPONENT_REFs. */
2180 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2181 && gimplify_var_or_parm_decl (p) == GS_OK)
2182 goto restart;
2183 else
2184 break;
2186 expr_stack.safe_push (*p);
2189 gcc_assert (expr_stack.length ());
2191 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2192 walked through and P points to the innermost expression.
2194 Java requires that we elaborated nodes in source order. That
2195 means we must gimplify the inner expression followed by each of
2196 the indices, in order. But we can't gimplify the inner
2197 expression until we deal with any variable bounds, sizes, or
2198 positions in order to deal with PLACEHOLDER_EXPRs.
2200 So we do this in three steps. First we deal with the annotations
2201 for any variables in the components, then we gimplify the base,
2202 then we gimplify any indices, from left to right. */
2203 for (i = expr_stack.length () - 1; i >= 0; i--)
2205 tree t = expr_stack[i];
2207 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2209 /* Gimplify the low bound and element type size and put them into
2210 the ARRAY_REF. If these values are set, they have already been
2211 gimplified. */
2212 if (TREE_OPERAND (t, 2) == NULL_TREE)
2214 tree low = unshare_expr (array_ref_low_bound (t));
2215 if (!is_gimple_min_invariant (low))
2217 TREE_OPERAND (t, 2) = low;
2218 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2219 post_p, is_gimple_reg,
2220 fb_rvalue);
2221 ret = MIN (ret, tret);
2224 else
2226 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2227 is_gimple_reg, fb_rvalue);
2228 ret = MIN (ret, tret);
2231 if (TREE_OPERAND (t, 3) == NULL_TREE)
2233 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2234 tree elmt_size = unshare_expr (array_ref_element_size (t));
2235 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2237 /* Divide the element size by the alignment of the element
2238 type (above). */
2239 elmt_size
2240 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2242 if (!is_gimple_min_invariant (elmt_size))
2244 TREE_OPERAND (t, 3) = elmt_size;
2245 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2246 post_p, is_gimple_reg,
2247 fb_rvalue);
2248 ret = MIN (ret, tret);
2251 else
2253 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2254 is_gimple_reg, fb_rvalue);
2255 ret = MIN (ret, tret);
2258 else if (TREE_CODE (t) == COMPONENT_REF)
2260 /* Set the field offset into T and gimplify it. */
2261 if (TREE_OPERAND (t, 2) == NULL_TREE)
2263 tree offset = unshare_expr (component_ref_field_offset (t));
2264 tree field = TREE_OPERAND (t, 1);
2265 tree factor
2266 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2268 /* Divide the offset by its alignment. */
2269 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2271 if (!is_gimple_min_invariant (offset))
2273 TREE_OPERAND (t, 2) = offset;
2274 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2275 post_p, is_gimple_reg,
2276 fb_rvalue);
2277 ret = MIN (ret, tret);
2280 else
2282 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2283 is_gimple_reg, fb_rvalue);
2284 ret = MIN (ret, tret);
2289 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2290 so as to match the min_lval predicate. Failure to do so may result
2291 in the creation of large aggregate temporaries. */
2292 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2293 fallback | fb_lvalue);
2294 ret = MIN (ret, tret);
2296 /* And finally, the indices and operands of ARRAY_REF. During this
2297 loop we also remove any useless conversions. */
2298 for (; expr_stack.length () > 0; )
2300 tree t = expr_stack.pop ();
2302 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2304 /* Gimplify the dimension. */
2305 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2307 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2308 is_gimple_val, fb_rvalue);
2309 ret = MIN (ret, tret);
2313 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2315 /* The innermost expression P may have originally had
2316 TREE_SIDE_EFFECTS set which would have caused all the outer
2317 expressions in *EXPR_P leading to P to also have had
2318 TREE_SIDE_EFFECTS set. */
2319 recalculate_side_effects (t);
2322 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2323 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2325 canonicalize_component_ref (expr_p);
2328 expr_stack.release ();
2330 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2332 return ret;
2335 /* Gimplify the self modifying expression pointed to by EXPR_P
2336 (++, --, +=, -=).
2338 PRE_P points to the list where side effects that must happen before
2339 *EXPR_P should be stored.
2341 POST_P points to the list where side effects that must happen after
2342 *EXPR_P should be stored.
2344 WANT_VALUE is nonzero iff we want to use the value of this expression
2345 in another expression.
2347 ARITH_TYPE is the type the computation should be performed in. */
2349 enum gimplify_status
2350 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2351 bool want_value, tree arith_type)
2353 enum tree_code code;
2354 tree lhs, lvalue, rhs, t1;
2355 gimple_seq post = NULL, *orig_post_p = post_p;
2356 bool postfix;
2357 enum tree_code arith_code;
2358 enum gimplify_status ret;
2359 location_t loc = EXPR_LOCATION (*expr_p);
2361 code = TREE_CODE (*expr_p);
2363 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2364 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2366 /* Prefix or postfix? */
2367 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2368 /* Faster to treat as prefix if result is not used. */
2369 postfix = want_value;
2370 else
2371 postfix = false;
2373 /* For postfix, make sure the inner expression's post side effects
2374 are executed after side effects from this expression. */
2375 if (postfix)
2376 post_p = &post;
2378 /* Add or subtract? */
2379 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2380 arith_code = PLUS_EXPR;
2381 else
2382 arith_code = MINUS_EXPR;
2384 /* Gimplify the LHS into a GIMPLE lvalue. */
2385 lvalue = TREE_OPERAND (*expr_p, 0);
2386 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2387 if (ret == GS_ERROR)
2388 return ret;
2390 /* Extract the operands to the arithmetic operation. */
2391 lhs = lvalue;
2392 rhs = TREE_OPERAND (*expr_p, 1);
2394 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2395 that as the result value and in the postqueue operation. */
2396 if (postfix)
2398 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2399 if (ret == GS_ERROR)
2400 return ret;
2402 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2405 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2406 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2408 rhs = convert_to_ptrofftype_loc (loc, rhs);
2409 if (arith_code == MINUS_EXPR)
2410 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2411 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2413 else
2414 t1 = fold_convert (TREE_TYPE (*expr_p),
2415 fold_build2 (arith_code, arith_type,
2416 fold_convert (arith_type, lhs),
2417 fold_convert (arith_type, rhs)));
2419 if (postfix)
2421 gimplify_assign (lvalue, t1, pre_p);
2422 gimplify_seq_add_seq (orig_post_p, post);
2423 *expr_p = lhs;
2424 return GS_ALL_DONE;
2426 else
2428 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2429 return GS_OK;
2433 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2435 static void
2436 maybe_with_size_expr (tree *expr_p)
2438 tree expr = *expr_p;
2439 tree type = TREE_TYPE (expr);
2440 tree size;
2442 /* If we've already wrapped this or the type is error_mark_node, we can't do
2443 anything. */
2444 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2445 || type == error_mark_node)
2446 return;
2448 /* If the size isn't known or is a constant, we have nothing to do. */
2449 size = TYPE_SIZE_UNIT (type);
2450 if (!size || TREE_CODE (size) == INTEGER_CST)
2451 return;
2453 /* Otherwise, make a WITH_SIZE_EXPR. */
2454 size = unshare_expr (size);
2455 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2456 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2459 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2460 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2461 the CALL_EXPR. */
2463 static enum gimplify_status
2464 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2466 bool (*test) (tree);
2467 fallback_t fb;
2469 /* In general, we allow lvalues for function arguments to avoid
2470 extra overhead of copying large aggregates out of even larger
2471 aggregates into temporaries only to copy the temporaries to
2472 the argument list. Make optimizers happy by pulling out to
2473 temporaries those types that fit in registers. */
2474 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2475 test = is_gimple_val, fb = fb_rvalue;
2476 else
2478 test = is_gimple_lvalue, fb = fb_either;
2479 /* Also strip a TARGET_EXPR that would force an extra copy. */
2480 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2482 tree init = TARGET_EXPR_INITIAL (*arg_p);
2483 if (init
2484 && !VOID_TYPE_P (TREE_TYPE (init)))
2485 *arg_p = init;
2489 /* If this is a variable sized type, we must remember the size. */
2490 maybe_with_size_expr (arg_p);
2492 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2493 /* Make sure arguments have the same location as the function call
2494 itself. */
2495 protected_set_expr_location (*arg_p, call_location);
2497 /* There is a sequence point before a function call. Side effects in
2498 the argument list must occur before the actual call. So, when
2499 gimplifying arguments, force gimplify_expr to use an internal
2500 post queue which is then appended to the end of PRE_P. */
2501 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2504 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2505 WANT_VALUE is true if the result of the call is desired. */
2507 static enum gimplify_status
2508 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2510 tree fndecl, parms, p, fnptrtype;
2511 enum gimplify_status ret;
2512 int i, nargs;
2513 gimple call;
2514 bool builtin_va_start_p = FALSE;
2515 location_t loc = EXPR_LOCATION (*expr_p);
2517 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2519 /* For reliable diagnostics during inlining, it is necessary that
2520 every call_expr be annotated with file and line. */
2521 if (! EXPR_HAS_LOCATION (*expr_p))
2522 SET_EXPR_LOCATION (*expr_p, input_location);
2524 /* This may be a call to a builtin function.
2526 Builtin function calls may be transformed into different
2527 (and more efficient) builtin function calls under certain
2528 circumstances. Unfortunately, gimplification can muck things
2529 up enough that the builtin expanders are not aware that certain
2530 transformations are still valid.
2532 So we attempt transformation/gimplification of the call before
2533 we gimplify the CALL_EXPR. At this time we do not manage to
2534 transform all calls in the same manner as the expanders do, but
2535 we do transform most of them. */
2536 fndecl = get_callee_fndecl (*expr_p);
2537 if (fndecl
2538 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2539 switch (DECL_FUNCTION_CODE (fndecl))
2541 case BUILT_IN_VA_START:
2543 builtin_va_start_p = TRUE;
2544 if (call_expr_nargs (*expr_p) < 2)
2546 error ("too few arguments to function %<va_start%>");
2547 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2548 return GS_OK;
2551 if (fold_builtin_next_arg (*expr_p, true))
2553 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2554 return GS_OK;
2556 break;
2558 case BUILT_IN_LINE:
2560 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2561 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2562 return GS_OK;
2564 case BUILT_IN_FILE:
2566 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2567 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2568 return GS_OK;
2570 case BUILT_IN_FUNCTION:
2572 const char *function;
2573 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2574 *expr_p = build_string_literal (strlen (function) + 1, function);
2575 return GS_OK;
2577 default:
2580 if (fndecl && DECL_BUILT_IN (fndecl))
2582 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2583 if (new_tree && new_tree != *expr_p)
2585 /* There was a transformation of this call which computes the
2586 same value, but in a more efficient way. Return and try
2587 again. */
2588 *expr_p = new_tree;
2589 return GS_OK;
2593 /* Remember the original function pointer type. */
2594 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2596 /* There is a sequence point before the call, so any side effects in
2597 the calling expression must occur before the actual call. Force
2598 gimplify_expr to use an internal post queue. */
2599 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2600 is_gimple_call_addr, fb_rvalue);
2602 nargs = call_expr_nargs (*expr_p);
2604 /* Get argument types for verification. */
2605 fndecl = get_callee_fndecl (*expr_p);
2606 parms = NULL_TREE;
2607 if (fndecl)
2608 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2609 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2610 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2612 if (fndecl && DECL_ARGUMENTS (fndecl))
2613 p = DECL_ARGUMENTS (fndecl);
2614 else if (parms)
2615 p = parms;
2616 else
2617 p = NULL_TREE;
2618 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2621 /* If the last argument is __builtin_va_arg_pack () and it is not
2622 passed as a named argument, decrease the number of CALL_EXPR
2623 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2624 if (!p
2625 && i < nargs
2626 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2628 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2629 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2631 if (last_arg_fndecl
2632 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2633 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2634 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2636 tree call = *expr_p;
2638 --nargs;
2639 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2640 CALL_EXPR_FN (call),
2641 nargs, CALL_EXPR_ARGP (call));
2643 /* Copy all CALL_EXPR flags, location and block, except
2644 CALL_EXPR_VA_ARG_PACK flag. */
2645 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2646 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2647 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2648 = CALL_EXPR_RETURN_SLOT_OPT (call);
2649 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2650 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2652 /* Set CALL_EXPR_VA_ARG_PACK. */
2653 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2657 /* Finally, gimplify the function arguments. */
2658 if (nargs > 0)
2660 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2661 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2662 PUSH_ARGS_REVERSED ? i-- : i++)
2664 enum gimplify_status t;
2666 /* Avoid gimplifying the second argument to va_start, which needs to
2667 be the plain PARM_DECL. */
2668 if ((i != 1) || !builtin_va_start_p)
2670 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2671 EXPR_LOCATION (*expr_p));
2673 if (t == GS_ERROR)
2674 ret = GS_ERROR;
2679 /* Verify the function result. */
2680 if (want_value && fndecl
2681 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2683 error_at (loc, "using result of function returning %<void%>");
2684 ret = GS_ERROR;
2687 /* Try this again in case gimplification exposed something. */
2688 if (ret != GS_ERROR)
2690 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2692 if (new_tree && new_tree != *expr_p)
2694 /* There was a transformation of this call which computes the
2695 same value, but in a more efficient way. Return and try
2696 again. */
2697 *expr_p = new_tree;
2698 return GS_OK;
2701 else
2703 *expr_p = error_mark_node;
2704 return GS_ERROR;
2707 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2708 decl. This allows us to eliminate redundant or useless
2709 calls to "const" functions. */
2710 if (TREE_CODE (*expr_p) == CALL_EXPR)
2712 int flags = call_expr_flags (*expr_p);
2713 if (flags & (ECF_CONST | ECF_PURE)
2714 /* An infinite loop is considered a side effect. */
2715 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2716 TREE_SIDE_EFFECTS (*expr_p) = 0;
2719 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2720 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2721 form and delegate the creation of a GIMPLE_CALL to
2722 gimplify_modify_expr. This is always possible because when
2723 WANT_VALUE is true, the caller wants the result of this call into
2724 a temporary, which means that we will emit an INIT_EXPR in
2725 internal_get_tmp_var which will then be handled by
2726 gimplify_modify_expr. */
2727 if (!want_value)
2729 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2730 have to do is replicate it as a GIMPLE_CALL tuple. */
2731 gimple_stmt_iterator gsi;
2732 call = gimple_build_call_from_tree (*expr_p);
2733 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2734 gimplify_seq_add_stmt (pre_p, call);
2735 gsi = gsi_last (*pre_p);
2736 fold_stmt (&gsi);
2737 *expr_p = NULL_TREE;
2739 else
2740 /* Remember the original function type. */
2741 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2742 CALL_EXPR_FN (*expr_p));
2744 return ret;
2747 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2748 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2750 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2751 condition is true or false, respectively. If null, we should generate
2752 our own to skip over the evaluation of this specific expression.
2754 LOCUS is the source location of the COND_EXPR.
2756 This function is the tree equivalent of do_jump.
2758 shortcut_cond_r should only be called by shortcut_cond_expr. */
2760 static tree
2761 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2762 location_t locus)
2764 tree local_label = NULL_TREE;
2765 tree t, expr = NULL;
2767 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2768 retain the shortcut semantics. Just insert the gotos here;
2769 shortcut_cond_expr will append the real blocks later. */
2770 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2772 location_t new_locus;
2774 /* Turn if (a && b) into
2776 if (a); else goto no;
2777 if (b) goto yes; else goto no;
2778 (no:) */
2780 if (false_label_p == NULL)
2781 false_label_p = &local_label;
2783 /* Keep the original source location on the first 'if'. */
2784 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2785 append_to_statement_list (t, &expr);
2787 /* Set the source location of the && on the second 'if'. */
2788 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2789 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2790 new_locus);
2791 append_to_statement_list (t, &expr);
2793 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2795 location_t new_locus;
2797 /* Turn if (a || b) into
2799 if (a) goto yes;
2800 if (b) goto yes; else goto no;
2801 (yes:) */
2803 if (true_label_p == NULL)
2804 true_label_p = &local_label;
2806 /* Keep the original source location on the first 'if'. */
2807 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2808 append_to_statement_list (t, &expr);
2810 /* Set the source location of the || on the second 'if'. */
2811 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2812 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2813 new_locus);
2814 append_to_statement_list (t, &expr);
2816 else if (TREE_CODE (pred) == COND_EXPR
2817 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2818 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2820 location_t new_locus;
2822 /* As long as we're messing with gotos, turn if (a ? b : c) into
2823 if (a)
2824 if (b) goto yes; else goto no;
2825 else
2826 if (c) goto yes; else goto no;
2828 Don't do this if one of the arms has void type, which can happen
2829 in C++ when the arm is throw. */
2831 /* Keep the original source location on the first 'if'. Set the source
2832 location of the ? on the second 'if'. */
2833 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2834 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2835 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2836 false_label_p, locus),
2837 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2838 false_label_p, new_locus));
2840 else
2842 expr = build3 (COND_EXPR, void_type_node, pred,
2843 build_and_jump (true_label_p),
2844 build_and_jump (false_label_p));
2845 SET_EXPR_LOCATION (expr, locus);
2848 if (local_label)
2850 t = build1 (LABEL_EXPR, void_type_node, local_label);
2851 append_to_statement_list (t, &expr);
2854 return expr;
2857 /* Given a conditional expression EXPR with short-circuit boolean
2858 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2859 predicate apart into the equivalent sequence of conditionals. */
2861 static tree
2862 shortcut_cond_expr (tree expr)
2864 tree pred = TREE_OPERAND (expr, 0);
2865 tree then_ = TREE_OPERAND (expr, 1);
2866 tree else_ = TREE_OPERAND (expr, 2);
2867 tree true_label, false_label, end_label, t;
2868 tree *true_label_p;
2869 tree *false_label_p;
2870 bool emit_end, emit_false, jump_over_else;
2871 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2872 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2874 /* First do simple transformations. */
2875 if (!else_se)
2877 /* If there is no 'else', turn
2878 if (a && b) then c
2879 into
2880 if (a) if (b) then c. */
2881 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2883 /* Keep the original source location on the first 'if'. */
2884 location_t locus = EXPR_LOC_OR_HERE (expr);
2885 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2886 /* Set the source location of the && on the second 'if'. */
2887 if (EXPR_HAS_LOCATION (pred))
2888 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2889 then_ = shortcut_cond_expr (expr);
2890 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2891 pred = TREE_OPERAND (pred, 0);
2892 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2893 SET_EXPR_LOCATION (expr, locus);
2897 if (!then_se)
2899 /* If there is no 'then', turn
2900 if (a || b); else d
2901 into
2902 if (a); else if (b); else d. */
2903 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2905 /* Keep the original source location on the first 'if'. */
2906 location_t locus = EXPR_LOC_OR_HERE (expr);
2907 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2908 /* Set the source location of the || on the second 'if'. */
2909 if (EXPR_HAS_LOCATION (pred))
2910 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2911 else_ = shortcut_cond_expr (expr);
2912 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2913 pred = TREE_OPERAND (pred, 0);
2914 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2915 SET_EXPR_LOCATION (expr, locus);
2919 /* If we're done, great. */
2920 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2921 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2922 return expr;
2924 /* Otherwise we need to mess with gotos. Change
2925 if (a) c; else d;
2927 if (a); else goto no;
2928 c; goto end;
2929 no: d; end:
2930 and recursively gimplify the condition. */
2932 true_label = false_label = end_label = NULL_TREE;
2934 /* If our arms just jump somewhere, hijack those labels so we don't
2935 generate jumps to jumps. */
2937 if (then_
2938 && TREE_CODE (then_) == GOTO_EXPR
2939 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2941 true_label = GOTO_DESTINATION (then_);
2942 then_ = NULL;
2943 then_se = false;
2946 if (else_
2947 && TREE_CODE (else_) == GOTO_EXPR
2948 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2950 false_label = GOTO_DESTINATION (else_);
2951 else_ = NULL;
2952 else_se = false;
2955 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2956 if (true_label)
2957 true_label_p = &true_label;
2958 else
2959 true_label_p = NULL;
2961 /* The 'else' branch also needs a label if it contains interesting code. */
2962 if (false_label || else_se)
2963 false_label_p = &false_label;
2964 else
2965 false_label_p = NULL;
2967 /* If there was nothing else in our arms, just forward the label(s). */
2968 if (!then_se && !else_se)
2969 return shortcut_cond_r (pred, true_label_p, false_label_p,
2970 EXPR_LOC_OR_HERE (expr));
2972 /* If our last subexpression already has a terminal label, reuse it. */
2973 if (else_se)
2974 t = expr_last (else_);
2975 else if (then_se)
2976 t = expr_last (then_);
2977 else
2978 t = NULL;
2979 if (t && TREE_CODE (t) == LABEL_EXPR)
2980 end_label = LABEL_EXPR_LABEL (t);
2982 /* If we don't care about jumping to the 'else' branch, jump to the end
2983 if the condition is false. */
2984 if (!false_label_p)
2985 false_label_p = &end_label;
2987 /* We only want to emit these labels if we aren't hijacking them. */
2988 emit_end = (end_label == NULL_TREE);
2989 emit_false = (false_label == NULL_TREE);
2991 /* We only emit the jump over the else clause if we have to--if the
2992 then clause may fall through. Otherwise we can wind up with a
2993 useless jump and a useless label at the end of gimplified code,
2994 which will cause us to think that this conditional as a whole
2995 falls through even if it doesn't. If we then inline a function
2996 which ends with such a condition, that can cause us to issue an
2997 inappropriate warning about control reaching the end of a
2998 non-void function. */
2999 jump_over_else = block_may_fallthru (then_);
3001 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3002 EXPR_LOC_OR_HERE (expr));
3004 expr = NULL;
3005 append_to_statement_list (pred, &expr);
3007 append_to_statement_list (then_, &expr);
3008 if (else_se)
3010 if (jump_over_else)
3012 tree last = expr_last (expr);
3013 t = build_and_jump (&end_label);
3014 if (EXPR_HAS_LOCATION (last))
3015 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3016 append_to_statement_list (t, &expr);
3018 if (emit_false)
3020 t = build1 (LABEL_EXPR, void_type_node, false_label);
3021 append_to_statement_list (t, &expr);
3023 append_to_statement_list (else_, &expr);
3025 if (emit_end && end_label)
3027 t = build1 (LABEL_EXPR, void_type_node, end_label);
3028 append_to_statement_list (t, &expr);
3031 return expr;
3034 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3036 tree
3037 gimple_boolify (tree expr)
3039 tree type = TREE_TYPE (expr);
3040 location_t loc = EXPR_LOCATION (expr);
3042 if (TREE_CODE (expr) == NE_EXPR
3043 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3044 && integer_zerop (TREE_OPERAND (expr, 1)))
3046 tree call = TREE_OPERAND (expr, 0);
3047 tree fn = get_callee_fndecl (call);
3049 /* For __builtin_expect ((long) (x), y) recurse into x as well
3050 if x is truth_value_p. */
3051 if (fn
3052 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3053 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3054 && call_expr_nargs (call) == 2)
3056 tree arg = CALL_EXPR_ARG (call, 0);
3057 if (arg)
3059 if (TREE_CODE (arg) == NOP_EXPR
3060 && TREE_TYPE (arg) == TREE_TYPE (call))
3061 arg = TREE_OPERAND (arg, 0);
3062 if (truth_value_p (TREE_CODE (arg)))
3064 arg = gimple_boolify (arg);
3065 CALL_EXPR_ARG (call, 0)
3066 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3072 switch (TREE_CODE (expr))
3074 case TRUTH_AND_EXPR:
3075 case TRUTH_OR_EXPR:
3076 case TRUTH_XOR_EXPR:
3077 case TRUTH_ANDIF_EXPR:
3078 case TRUTH_ORIF_EXPR:
3079 /* Also boolify the arguments of truth exprs. */
3080 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3081 /* FALLTHRU */
3083 case TRUTH_NOT_EXPR:
3084 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3086 /* These expressions always produce boolean results. */
3087 if (TREE_CODE (type) != BOOLEAN_TYPE)
3088 TREE_TYPE (expr) = boolean_type_node;
3089 return expr;
3091 default:
3092 if (COMPARISON_CLASS_P (expr))
3094 /* There expressions always prduce boolean results. */
3095 if (TREE_CODE (type) != BOOLEAN_TYPE)
3096 TREE_TYPE (expr) = boolean_type_node;
3097 return expr;
3099 /* Other expressions that get here must have boolean values, but
3100 might need to be converted to the appropriate mode. */
3101 if (TREE_CODE (type) == BOOLEAN_TYPE)
3102 return expr;
3103 return fold_convert_loc (loc, boolean_type_node, expr);
3107 /* Given a conditional expression *EXPR_P without side effects, gimplify
3108 its operands. New statements are inserted to PRE_P. */
3110 static enum gimplify_status
3111 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3113 tree expr = *expr_p, cond;
3114 enum gimplify_status ret, tret;
3115 enum tree_code code;
3117 cond = gimple_boolify (COND_EXPR_COND (expr));
3119 /* We need to handle && and || specially, as their gimplification
3120 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3121 code = TREE_CODE (cond);
3122 if (code == TRUTH_ANDIF_EXPR)
3123 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3124 else if (code == TRUTH_ORIF_EXPR)
3125 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3126 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3127 COND_EXPR_COND (*expr_p) = cond;
3129 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3130 is_gimple_val, fb_rvalue);
3131 ret = MIN (ret, tret);
3132 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3133 is_gimple_val, fb_rvalue);
3135 return MIN (ret, tret);
3138 /* Return true if evaluating EXPR could trap.
3139 EXPR is GENERIC, while tree_could_trap_p can be called
3140 only on GIMPLE. */
3142 static bool
3143 generic_expr_could_trap_p (tree expr)
3145 unsigned i, n;
3147 if (!expr || is_gimple_val (expr))
3148 return false;
3150 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3151 return true;
3153 n = TREE_OPERAND_LENGTH (expr);
3154 for (i = 0; i < n; i++)
3155 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3156 return true;
3158 return false;
3161 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3162 into
3164 if (p) if (p)
3165 t1 = a; a;
3166 else or else
3167 t1 = b; b;
3170 The second form is used when *EXPR_P is of type void.
3172 PRE_P points to the list where side effects that must happen before
3173 *EXPR_P should be stored. */
3175 static enum gimplify_status
3176 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3178 tree expr = *expr_p;
3179 tree type = TREE_TYPE (expr);
3180 location_t loc = EXPR_LOCATION (expr);
3181 tree tmp, arm1, arm2;
3182 enum gimplify_status ret;
3183 tree label_true, label_false, label_cont;
3184 bool have_then_clause_p, have_else_clause_p;
3185 gimple gimple_cond;
3186 enum tree_code pred_code;
3187 gimple_seq seq = NULL;
3189 /* If this COND_EXPR has a value, copy the values into a temporary within
3190 the arms. */
3191 if (!VOID_TYPE_P (type))
3193 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3194 tree result;
3196 /* If either an rvalue is ok or we do not require an lvalue, create the
3197 temporary. But we cannot do that if the type is addressable. */
3198 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3199 && !TREE_ADDRESSABLE (type))
3201 if (gimplify_ctxp->allow_rhs_cond_expr
3202 /* If either branch has side effects or could trap, it can't be
3203 evaluated unconditionally. */
3204 && !TREE_SIDE_EFFECTS (then_)
3205 && !generic_expr_could_trap_p (then_)
3206 && !TREE_SIDE_EFFECTS (else_)
3207 && !generic_expr_could_trap_p (else_))
3208 return gimplify_pure_cond_expr (expr_p, pre_p);
3210 tmp = create_tmp_var (type, "iftmp");
3211 result = tmp;
3214 /* Otherwise, only create and copy references to the values. */
3215 else
3217 type = build_pointer_type (type);
3219 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3220 then_ = build_fold_addr_expr_loc (loc, then_);
3222 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3223 else_ = build_fold_addr_expr_loc (loc, else_);
3225 expr
3226 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3228 tmp = create_tmp_var (type, "iftmp");
3229 result = build_simple_mem_ref_loc (loc, tmp);
3232 /* Build the new then clause, `tmp = then_;'. But don't build the
3233 assignment if the value is void; in C++ it can be if it's a throw. */
3234 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3235 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3237 /* Similarly, build the new else clause, `tmp = else_;'. */
3238 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3239 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3241 TREE_TYPE (expr) = void_type_node;
3242 recalculate_side_effects (expr);
3244 /* Move the COND_EXPR to the prequeue. */
3245 gimplify_stmt (&expr, pre_p);
3247 *expr_p = result;
3248 return GS_ALL_DONE;
3251 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3252 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3253 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3254 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3256 /* Make sure the condition has BOOLEAN_TYPE. */
3257 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3259 /* Break apart && and || conditions. */
3260 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3261 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3263 expr = shortcut_cond_expr (expr);
3265 if (expr != *expr_p)
3267 *expr_p = expr;
3269 /* We can't rely on gimplify_expr to re-gimplify the expanded
3270 form properly, as cleanups might cause the target labels to be
3271 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3272 set up a conditional context. */
3273 gimple_push_condition ();
3274 gimplify_stmt (expr_p, &seq);
3275 gimple_pop_condition (pre_p);
3276 gimple_seq_add_seq (pre_p, seq);
3278 return GS_ALL_DONE;
3282 /* Now do the normal gimplification. */
3284 /* Gimplify condition. */
3285 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3286 fb_rvalue);
3287 if (ret == GS_ERROR)
3288 return GS_ERROR;
3289 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3291 gimple_push_condition ();
3293 have_then_clause_p = have_else_clause_p = false;
3294 if (TREE_OPERAND (expr, 1) != NULL
3295 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3296 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3297 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3298 == current_function_decl)
3299 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3300 have different locations, otherwise we end up with incorrect
3301 location information on the branches. */
3302 && (optimize
3303 || !EXPR_HAS_LOCATION (expr)
3304 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3305 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3307 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3308 have_then_clause_p = true;
3310 else
3311 label_true = create_artificial_label (UNKNOWN_LOCATION);
3312 if (TREE_OPERAND (expr, 2) != NULL
3313 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3314 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3315 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3316 == current_function_decl)
3317 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3318 have different locations, otherwise we end up with incorrect
3319 location information on the branches. */
3320 && (optimize
3321 || !EXPR_HAS_LOCATION (expr)
3322 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3323 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3325 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3326 have_else_clause_p = true;
3328 else
3329 label_false = create_artificial_label (UNKNOWN_LOCATION);
3331 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3332 &arm2);
3334 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3335 label_false);
3337 gimplify_seq_add_stmt (&seq, gimple_cond);
3338 label_cont = NULL_TREE;
3339 if (!have_then_clause_p)
3341 /* For if (...) {} else { code; } put label_true after
3342 the else block. */
3343 if (TREE_OPERAND (expr, 1) == NULL_TREE
3344 && !have_else_clause_p
3345 && TREE_OPERAND (expr, 2) != NULL_TREE)
3346 label_cont = label_true;
3347 else
3349 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3350 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3351 /* For if (...) { code; } else {} or
3352 if (...) { code; } else goto label; or
3353 if (...) { code; return; } else { ... }
3354 label_cont isn't needed. */
3355 if (!have_else_clause_p
3356 && TREE_OPERAND (expr, 2) != NULL_TREE
3357 && gimple_seq_may_fallthru (seq))
3359 gimple g;
3360 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3362 g = gimple_build_goto (label_cont);
3364 /* GIMPLE_COND's are very low level; they have embedded
3365 gotos. This particular embedded goto should not be marked
3366 with the location of the original COND_EXPR, as it would
3367 correspond to the COND_EXPR's condition, not the ELSE or the
3368 THEN arms. To avoid marking it with the wrong location, flag
3369 it as "no location". */
3370 gimple_set_do_not_emit_location (g);
3372 gimplify_seq_add_stmt (&seq, g);
3376 if (!have_else_clause_p)
3378 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3379 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3381 if (label_cont)
3382 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3384 gimple_pop_condition (pre_p);
3385 gimple_seq_add_seq (pre_p, seq);
3387 if (ret == GS_ERROR)
3388 ; /* Do nothing. */
3389 else if (have_then_clause_p || have_else_clause_p)
3390 ret = GS_ALL_DONE;
3391 else
3393 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3394 expr = TREE_OPERAND (expr, 0);
3395 gimplify_stmt (&expr, pre_p);
3398 *expr_p = NULL;
3399 return ret;
3402 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3403 to be marked addressable.
3405 We cannot rely on such an expression being directly markable if a temporary
3406 has been created by the gimplification. In this case, we create another
3407 temporary and initialize it with a copy, which will become a store after we
3408 mark it addressable. This can happen if the front-end passed us something
3409 that it could not mark addressable yet, like a Fortran pass-by-reference
3410 parameter (int) floatvar. */
3412 static void
3413 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3415 while (handled_component_p (*expr_p))
3416 expr_p = &TREE_OPERAND (*expr_p, 0);
3417 if (is_gimple_reg (*expr_p))
3418 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3421 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3422 a call to __builtin_memcpy. */
3424 static enum gimplify_status
3425 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3426 gimple_seq *seq_p)
3428 tree t, to, to_ptr, from, from_ptr;
3429 gimple gs;
3430 location_t loc = EXPR_LOCATION (*expr_p);
3432 to = TREE_OPERAND (*expr_p, 0);
3433 from = TREE_OPERAND (*expr_p, 1);
3435 /* Mark the RHS addressable. Beware that it may not be possible to do so
3436 directly if a temporary has been created by the gimplification. */
3437 prepare_gimple_addressable (&from, seq_p);
3439 mark_addressable (from);
3440 from_ptr = build_fold_addr_expr_loc (loc, from);
3441 gimplify_arg (&from_ptr, seq_p, loc);
3443 mark_addressable (to);
3444 to_ptr = build_fold_addr_expr_loc (loc, to);
3445 gimplify_arg (&to_ptr, seq_p, loc);
3447 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3449 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3451 if (want_value)
3453 /* tmp = memcpy() */
3454 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3455 gimple_call_set_lhs (gs, t);
3456 gimplify_seq_add_stmt (seq_p, gs);
3458 *expr_p = build_simple_mem_ref (t);
3459 return GS_ALL_DONE;
3462 gimplify_seq_add_stmt (seq_p, gs);
3463 *expr_p = NULL;
3464 return GS_ALL_DONE;
3467 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3468 a call to __builtin_memset. In this case we know that the RHS is
3469 a CONSTRUCTOR with an empty element list. */
3471 static enum gimplify_status
3472 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3473 gimple_seq *seq_p)
3475 tree t, from, to, to_ptr;
3476 gimple gs;
3477 location_t loc = EXPR_LOCATION (*expr_p);
3479 /* Assert our assumptions, to abort instead of producing wrong code
3480 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3481 not be immediately exposed. */
3482 from = TREE_OPERAND (*expr_p, 1);
3483 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3484 from = TREE_OPERAND (from, 0);
3486 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3487 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3489 /* Now proceed. */
3490 to = TREE_OPERAND (*expr_p, 0);
3492 to_ptr = build_fold_addr_expr_loc (loc, to);
3493 gimplify_arg (&to_ptr, seq_p, loc);
3494 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3496 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3498 if (want_value)
3500 /* tmp = memset() */
3501 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3502 gimple_call_set_lhs (gs, t);
3503 gimplify_seq_add_stmt (seq_p, gs);
3505 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3506 return GS_ALL_DONE;
3509 gimplify_seq_add_stmt (seq_p, gs);
3510 *expr_p = NULL;
3511 return GS_ALL_DONE;
3514 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3515 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3516 assignment. Return non-null if we detect a potential overlap. */
3518 struct gimplify_init_ctor_preeval_data
3520 /* The base decl of the lhs object. May be NULL, in which case we
3521 have to assume the lhs is indirect. */
3522 tree lhs_base_decl;
3524 /* The alias set of the lhs object. */
3525 alias_set_type lhs_alias_set;
3528 static tree
3529 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3531 struct gimplify_init_ctor_preeval_data *data
3532 = (struct gimplify_init_ctor_preeval_data *) xdata;
3533 tree t = *tp;
3535 /* If we find the base object, obviously we have overlap. */
3536 if (data->lhs_base_decl == t)
3537 return t;
3539 /* If the constructor component is indirect, determine if we have a
3540 potential overlap with the lhs. The only bits of information we
3541 have to go on at this point are addressability and alias sets. */
3542 if ((INDIRECT_REF_P (t)
3543 || TREE_CODE (t) == MEM_REF)
3544 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3545 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3546 return t;
3548 /* If the constructor component is a call, determine if it can hide a
3549 potential overlap with the lhs through an INDIRECT_REF like above.
3550 ??? Ugh - this is completely broken. In fact this whole analysis
3551 doesn't look conservative. */
3552 if (TREE_CODE (t) == CALL_EXPR)
3554 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3556 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3557 if (POINTER_TYPE_P (TREE_VALUE (type))
3558 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3559 && alias_sets_conflict_p (data->lhs_alias_set,
3560 get_alias_set
3561 (TREE_TYPE (TREE_VALUE (type)))))
3562 return t;
3565 if (IS_TYPE_OR_DECL_P (t))
3566 *walk_subtrees = 0;
3567 return NULL;
3570 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3571 force values that overlap with the lhs (as described by *DATA)
3572 into temporaries. */
3574 static void
3575 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3576 struct gimplify_init_ctor_preeval_data *data)
3578 enum gimplify_status one;
3580 /* If the value is constant, then there's nothing to pre-evaluate. */
3581 if (TREE_CONSTANT (*expr_p))
3583 /* Ensure it does not have side effects, it might contain a reference to
3584 the object we're initializing. */
3585 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3586 return;
3589 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3590 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3591 return;
3593 /* Recurse for nested constructors. */
3594 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3596 unsigned HOST_WIDE_INT ix;
3597 constructor_elt *ce;
3598 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3600 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3601 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3603 return;
3606 /* If this is a variable sized type, we must remember the size. */
3607 maybe_with_size_expr (expr_p);
3609 /* Gimplify the constructor element to something appropriate for the rhs
3610 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3611 the gimplifier will consider this a store to memory. Doing this
3612 gimplification now means that we won't have to deal with complicated
3613 language-specific trees, nor trees like SAVE_EXPR that can induce
3614 exponential search behavior. */
3615 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3616 if (one == GS_ERROR)
3618 *expr_p = NULL;
3619 return;
3622 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3623 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3624 always be true for all scalars, since is_gimple_mem_rhs insists on a
3625 temporary variable for them. */
3626 if (DECL_P (*expr_p))
3627 return;
3629 /* If this is of variable size, we have no choice but to assume it doesn't
3630 overlap since we can't make a temporary for it. */
3631 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3632 return;
3634 /* Otherwise, we must search for overlap ... */
3635 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3636 return;
3638 /* ... and if found, force the value into a temporary. */
3639 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3642 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3643 a RANGE_EXPR in a CONSTRUCTOR for an array.
3645 var = lower;
3646 loop_entry:
3647 object[var] = value;
3648 if (var == upper)
3649 goto loop_exit;
3650 var = var + 1;
3651 goto loop_entry;
3652 loop_exit:
3654 We increment var _after_ the loop exit check because we might otherwise
3655 fail if upper == TYPE_MAX_VALUE (type for upper).
3657 Note that we never have to deal with SAVE_EXPRs here, because this has
3658 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3660 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3661 gimple_seq *, bool);
3663 static void
3664 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3665 tree value, tree array_elt_type,
3666 gimple_seq *pre_p, bool cleared)
3668 tree loop_entry_label, loop_exit_label, fall_thru_label;
3669 tree var, var_type, cref, tmp;
3671 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3672 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3673 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3675 /* Create and initialize the index variable. */
3676 var_type = TREE_TYPE (upper);
3677 var = create_tmp_var (var_type, NULL);
3678 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3680 /* Add the loop entry label. */
3681 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3683 /* Build the reference. */
3684 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3685 var, NULL_TREE, NULL_TREE);
3687 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3688 the store. Otherwise just assign value to the reference. */
3690 if (TREE_CODE (value) == CONSTRUCTOR)
3691 /* NB we might have to call ourself recursively through
3692 gimplify_init_ctor_eval if the value is a constructor. */
3693 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3694 pre_p, cleared);
3695 else
3696 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3698 /* We exit the loop when the index var is equal to the upper bound. */
3699 gimplify_seq_add_stmt (pre_p,
3700 gimple_build_cond (EQ_EXPR, var, upper,
3701 loop_exit_label, fall_thru_label));
3703 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3705 /* Otherwise, increment the index var... */
3706 tmp = build2 (PLUS_EXPR, var_type, var,
3707 fold_convert (var_type, integer_one_node));
3708 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3710 /* ...and jump back to the loop entry. */
3711 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3713 /* Add the loop exit label. */
3714 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3717 /* Return true if FDECL is accessing a field that is zero sized. */
3719 static bool
3720 zero_sized_field_decl (const_tree fdecl)
3722 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3723 && integer_zerop (DECL_SIZE (fdecl)))
3724 return true;
3725 return false;
3728 /* Return true if TYPE is zero sized. */
3730 static bool
3731 zero_sized_type (const_tree type)
3733 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3734 && integer_zerop (TYPE_SIZE (type)))
3735 return true;
3736 return false;
3739 /* A subroutine of gimplify_init_constructor. Generate individual
3740 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3741 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3742 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3743 zeroed first. */
3745 static void
3746 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3747 gimple_seq *pre_p, bool cleared)
3749 tree array_elt_type = NULL;
3750 unsigned HOST_WIDE_INT ix;
3751 tree purpose, value;
3753 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3754 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3756 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3758 tree cref;
3760 /* NULL values are created above for gimplification errors. */
3761 if (value == NULL)
3762 continue;
3764 if (cleared && initializer_zerop (value))
3765 continue;
3767 /* ??? Here's to hoping the front end fills in all of the indices,
3768 so we don't have to figure out what's missing ourselves. */
3769 gcc_assert (purpose);
3771 /* Skip zero-sized fields, unless value has side-effects. This can
3772 happen with calls to functions returning a zero-sized type, which
3773 we shouldn't discard. As a number of downstream passes don't
3774 expect sets of zero-sized fields, we rely on the gimplification of
3775 the MODIFY_EXPR we make below to drop the assignment statement. */
3776 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3777 continue;
3779 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3780 whole range. */
3781 if (TREE_CODE (purpose) == RANGE_EXPR)
3783 tree lower = TREE_OPERAND (purpose, 0);
3784 tree upper = TREE_OPERAND (purpose, 1);
3786 /* If the lower bound is equal to upper, just treat it as if
3787 upper was the index. */
3788 if (simple_cst_equal (lower, upper))
3789 purpose = upper;
3790 else
3792 gimplify_init_ctor_eval_range (object, lower, upper, value,
3793 array_elt_type, pre_p, cleared);
3794 continue;
3798 if (array_elt_type)
3800 /* Do not use bitsizetype for ARRAY_REF indices. */
3801 if (TYPE_DOMAIN (TREE_TYPE (object)))
3802 purpose
3803 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3804 purpose);
3805 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3806 purpose, NULL_TREE, NULL_TREE);
3808 else
3810 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3811 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3812 unshare_expr (object), purpose, NULL_TREE);
3815 if (TREE_CODE (value) == CONSTRUCTOR
3816 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3817 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3818 pre_p, cleared);
3819 else
3821 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3822 gimplify_and_add (init, pre_p);
3823 ggc_free (init);
3828 /* Return the appropriate RHS predicate for this LHS. */
3830 gimple_predicate
3831 rhs_predicate_for (tree lhs)
3833 if (is_gimple_reg (lhs))
3834 return is_gimple_reg_rhs_or_call;
3835 else
3836 return is_gimple_mem_rhs_or_call;
3839 /* Gimplify a C99 compound literal expression. This just means adding
3840 the DECL_EXPR before the current statement and using its anonymous
3841 decl instead. */
3843 static enum gimplify_status
3844 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3845 bool (*gimple_test_f) (tree),
3846 fallback_t fallback)
3848 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3849 tree decl = DECL_EXPR_DECL (decl_s);
3850 tree init = DECL_INITIAL (decl);
3851 /* Mark the decl as addressable if the compound literal
3852 expression is addressable now, otherwise it is marked too late
3853 after we gimplify the initialization expression. */
3854 if (TREE_ADDRESSABLE (*expr_p))
3855 TREE_ADDRESSABLE (decl) = 1;
3856 /* Otherwise, if we don't need an lvalue and have a literal directly
3857 substitute it. Check if it matches the gimple predicate, as
3858 otherwise we'd generate a new temporary, and we can as well just
3859 use the decl we already have. */
3860 else if (!TREE_ADDRESSABLE (decl)
3861 && init
3862 && (fallback & fb_lvalue) == 0
3863 && gimple_test_f (init))
3865 *expr_p = init;
3866 return GS_OK;
3869 /* Preliminarily mark non-addressed complex variables as eligible
3870 for promotion to gimple registers. We'll transform their uses
3871 as we find them. */
3872 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3873 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3874 && !TREE_THIS_VOLATILE (decl)
3875 && !needs_to_live_in_memory (decl))
3876 DECL_GIMPLE_REG_P (decl) = 1;
3878 /* If the decl is not addressable, then it is being used in some
3879 expression or on the right hand side of a statement, and it can
3880 be put into a readonly data section. */
3881 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3882 TREE_READONLY (decl) = 1;
3884 /* This decl isn't mentioned in the enclosing block, so add it to the
3885 list of temps. FIXME it seems a bit of a kludge to say that
3886 anonymous artificial vars aren't pushed, but everything else is. */
3887 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3888 gimple_add_tmp_var (decl);
3890 gimplify_and_add (decl_s, pre_p);
3891 *expr_p = decl;
3892 return GS_OK;
3895 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3896 return a new CONSTRUCTOR if something changed. */
3898 static tree
3899 optimize_compound_literals_in_ctor (tree orig_ctor)
3901 tree ctor = orig_ctor;
3902 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3903 unsigned int idx, num = vec_safe_length (elts);
3905 for (idx = 0; idx < num; idx++)
3907 tree value = (*elts)[idx].value;
3908 tree newval = value;
3909 if (TREE_CODE (value) == CONSTRUCTOR)
3910 newval = optimize_compound_literals_in_ctor (value);
3911 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3913 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3914 tree decl = DECL_EXPR_DECL (decl_s);
3915 tree init = DECL_INITIAL (decl);
3917 if (!TREE_ADDRESSABLE (value)
3918 && !TREE_ADDRESSABLE (decl)
3919 && init
3920 && TREE_CODE (init) == CONSTRUCTOR)
3921 newval = optimize_compound_literals_in_ctor (init);
3923 if (newval == value)
3924 continue;
3926 if (ctor == orig_ctor)
3928 ctor = copy_node (orig_ctor);
3929 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3930 elts = CONSTRUCTOR_ELTS (ctor);
3932 (*elts)[idx].value = newval;
3934 return ctor;
3937 /* A subroutine of gimplify_modify_expr. Break out elements of a
3938 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3940 Note that we still need to clear any elements that don't have explicit
3941 initializers, so if not all elements are initialized we keep the
3942 original MODIFY_EXPR, we just remove all of the constructor elements.
3944 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3945 GS_ERROR if we would have to create a temporary when gimplifying
3946 this constructor. Otherwise, return GS_OK.
3948 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3950 static enum gimplify_status
3951 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3952 bool want_value, bool notify_temp_creation)
3954 tree object, ctor, type;
3955 enum gimplify_status ret;
3956 vec<constructor_elt, va_gc> *elts;
3958 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3960 if (!notify_temp_creation)
3962 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3963 is_gimple_lvalue, fb_lvalue);
3964 if (ret == GS_ERROR)
3965 return ret;
3968 object = TREE_OPERAND (*expr_p, 0);
3969 ctor = TREE_OPERAND (*expr_p, 1) =
3970 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3971 type = TREE_TYPE (ctor);
3972 elts = CONSTRUCTOR_ELTS (ctor);
3973 ret = GS_ALL_DONE;
3975 switch (TREE_CODE (type))
3977 case RECORD_TYPE:
3978 case UNION_TYPE:
3979 case QUAL_UNION_TYPE:
3980 case ARRAY_TYPE:
3982 struct gimplify_init_ctor_preeval_data preeval_data;
3983 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3984 bool cleared, complete_p, valid_const_initializer;
3986 /* Aggregate types must lower constructors to initialization of
3987 individual elements. The exception is that a CONSTRUCTOR node
3988 with no elements indicates zero-initialization of the whole. */
3989 if (vec_safe_is_empty (elts))
3991 if (notify_temp_creation)
3992 return GS_OK;
3993 break;
3996 /* Fetch information about the constructor to direct later processing.
3997 We might want to make static versions of it in various cases, and
3998 can only do so if it known to be a valid constant initializer. */
3999 valid_const_initializer
4000 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4001 &num_ctor_elements, &complete_p);
4003 /* If a const aggregate variable is being initialized, then it
4004 should never be a lose to promote the variable to be static. */
4005 if (valid_const_initializer
4006 && num_nonzero_elements > 1
4007 && TREE_READONLY (object)
4008 && TREE_CODE (object) == VAR_DECL
4009 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4011 if (notify_temp_creation)
4012 return GS_ERROR;
4013 DECL_INITIAL (object) = ctor;
4014 TREE_STATIC (object) = 1;
4015 if (!DECL_NAME (object))
4016 DECL_NAME (object) = create_tmp_var_name ("C");
4017 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4019 /* ??? C++ doesn't automatically append a .<number> to the
4020 assembler name, and even when it does, it looks at FE private
4021 data structures to figure out what that number should be,
4022 which are not set for this variable. I suppose this is
4023 important for local statics for inline functions, which aren't
4024 "local" in the object file sense. So in order to get a unique
4025 TU-local symbol, we must invoke the lhd version now. */
4026 lhd_set_decl_assembler_name (object);
4028 *expr_p = NULL_TREE;
4029 break;
4032 /* If there are "lots" of initialized elements, even discounting
4033 those that are not address constants (and thus *must* be
4034 computed at runtime), then partition the constructor into
4035 constant and non-constant parts. Block copy the constant
4036 parts in, then generate code for the non-constant parts. */
4037 /* TODO. There's code in cp/typeck.c to do this. */
4039 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4040 /* store_constructor will ignore the clearing of variable-sized
4041 objects. Initializers for such objects must explicitly set
4042 every field that needs to be set. */
4043 cleared = false;
4044 else if (!complete_p)
4045 /* If the constructor isn't complete, clear the whole object
4046 beforehand.
4048 ??? This ought not to be needed. For any element not present
4049 in the initializer, we should simply set them to zero. Except
4050 we'd need to *find* the elements that are not present, and that
4051 requires trickery to avoid quadratic compile-time behavior in
4052 large cases or excessive memory use in small cases. */
4053 cleared = true;
4054 else if (num_ctor_elements - num_nonzero_elements
4055 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4056 && num_nonzero_elements < num_ctor_elements / 4)
4057 /* If there are "lots" of zeros, it's more efficient to clear
4058 the memory and then set the nonzero elements. */
4059 cleared = true;
4060 else
4061 cleared = false;
4063 /* If there are "lots" of initialized elements, and all of them
4064 are valid address constants, then the entire initializer can
4065 be dropped to memory, and then memcpy'd out. Don't do this
4066 for sparse arrays, though, as it's more efficient to follow
4067 the standard CONSTRUCTOR behavior of memset followed by
4068 individual element initialization. Also don't do this for small
4069 all-zero initializers (which aren't big enough to merit
4070 clearing), and don't try to make bitwise copies of
4071 TREE_ADDRESSABLE types. */
4072 if (valid_const_initializer
4073 && !(cleared || num_nonzero_elements == 0)
4074 && !TREE_ADDRESSABLE (type))
4076 HOST_WIDE_INT size = int_size_in_bytes (type);
4077 unsigned int align;
4079 /* ??? We can still get unbounded array types, at least
4080 from the C++ front end. This seems wrong, but attempt
4081 to work around it for now. */
4082 if (size < 0)
4084 size = int_size_in_bytes (TREE_TYPE (object));
4085 if (size >= 0)
4086 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4089 /* Find the maximum alignment we can assume for the object. */
4090 /* ??? Make use of DECL_OFFSET_ALIGN. */
4091 if (DECL_P (object))
4092 align = DECL_ALIGN (object);
4093 else
4094 align = TYPE_ALIGN (type);
4096 /* Do a block move either if the size is so small as to make
4097 each individual move a sub-unit move on average, or if it
4098 is so large as to make individual moves inefficient. */
4099 if (size > 0
4100 && num_nonzero_elements > 1
4101 && (size < num_nonzero_elements
4102 || !can_move_by_pieces (size, align)))
4104 if (notify_temp_creation)
4105 return GS_ERROR;
4107 walk_tree (&ctor, force_labels_r, NULL, NULL);
4108 ctor = tree_output_constant_def (ctor);
4109 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4110 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4111 TREE_OPERAND (*expr_p, 1) = ctor;
4113 /* This is no longer an assignment of a CONSTRUCTOR, but
4114 we still may have processing to do on the LHS. So
4115 pretend we didn't do anything here to let that happen. */
4116 return GS_UNHANDLED;
4120 /* If the target is volatile, we have non-zero elements and more than
4121 one field to assign, initialize the target from a temporary. */
4122 if (TREE_THIS_VOLATILE (object)
4123 && !TREE_ADDRESSABLE (type)
4124 && num_nonzero_elements > 0
4125 && vec_safe_length (elts) > 1)
4127 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4128 TREE_OPERAND (*expr_p, 0) = temp;
4129 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4130 *expr_p,
4131 build2 (MODIFY_EXPR, void_type_node,
4132 object, temp));
4133 return GS_OK;
4136 if (notify_temp_creation)
4137 return GS_OK;
4139 /* If there are nonzero elements and if needed, pre-evaluate to capture
4140 elements overlapping with the lhs into temporaries. We must do this
4141 before clearing to fetch the values before they are zeroed-out. */
4142 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4144 preeval_data.lhs_base_decl = get_base_address (object);
4145 if (!DECL_P (preeval_data.lhs_base_decl))
4146 preeval_data.lhs_base_decl = NULL;
4147 preeval_data.lhs_alias_set = get_alias_set (object);
4149 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4150 pre_p, post_p, &preeval_data);
4153 if (cleared)
4155 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4156 Note that we still have to gimplify, in order to handle the
4157 case of variable sized types. Avoid shared tree structures. */
4158 CONSTRUCTOR_ELTS (ctor) = NULL;
4159 TREE_SIDE_EFFECTS (ctor) = 0;
4160 object = unshare_expr (object);
4161 gimplify_stmt (expr_p, pre_p);
4164 /* If we have not block cleared the object, or if there are nonzero
4165 elements in the constructor, add assignments to the individual
4166 scalar fields of the object. */
4167 if (!cleared || num_nonzero_elements > 0)
4168 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4170 *expr_p = NULL_TREE;
4172 break;
4174 case COMPLEX_TYPE:
4176 tree r, i;
4178 if (notify_temp_creation)
4179 return GS_OK;
4181 /* Extract the real and imaginary parts out of the ctor. */
4182 gcc_assert (elts->length () == 2);
4183 r = (*elts)[0].value;
4184 i = (*elts)[1].value;
4185 if (r == NULL || i == NULL)
4187 tree zero = build_zero_cst (TREE_TYPE (type));
4188 if (r == NULL)
4189 r = zero;
4190 if (i == NULL)
4191 i = zero;
4194 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4195 represent creation of a complex value. */
4196 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4198 ctor = build_complex (type, r, i);
4199 TREE_OPERAND (*expr_p, 1) = ctor;
4201 else
4203 ctor = build2 (COMPLEX_EXPR, type, r, i);
4204 TREE_OPERAND (*expr_p, 1) = ctor;
4205 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4206 pre_p,
4207 post_p,
4208 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4209 fb_rvalue);
4212 break;
4214 case VECTOR_TYPE:
4216 unsigned HOST_WIDE_INT ix;
4217 constructor_elt *ce;
4219 if (notify_temp_creation)
4220 return GS_OK;
4222 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4223 if (TREE_CONSTANT (ctor))
4225 bool constant_p = true;
4226 tree value;
4228 /* Even when ctor is constant, it might contain non-*_CST
4229 elements, such as addresses or trapping values like
4230 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4231 in VECTOR_CST nodes. */
4232 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4233 if (!CONSTANT_CLASS_P (value))
4235 constant_p = false;
4236 break;
4239 if (constant_p)
4241 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4242 break;
4245 /* Don't reduce an initializer constant even if we can't
4246 make a VECTOR_CST. It won't do anything for us, and it'll
4247 prevent us from representing it as a single constant. */
4248 if (initializer_constant_valid_p (ctor, type))
4249 break;
4251 TREE_CONSTANT (ctor) = 0;
4254 /* Vector types use CONSTRUCTOR all the way through gimple
4255 compilation as a general initializer. */
4256 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4258 enum gimplify_status tret;
4259 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4260 fb_rvalue);
4261 if (tret == GS_ERROR)
4262 ret = GS_ERROR;
4264 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4265 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4267 break;
4269 default:
4270 /* So how did we get a CONSTRUCTOR for a scalar type? */
4271 gcc_unreachable ();
4274 if (ret == GS_ERROR)
4275 return GS_ERROR;
4276 else if (want_value)
4278 *expr_p = object;
4279 return GS_OK;
4281 else
4283 /* If we have gimplified both sides of the initializer but have
4284 not emitted an assignment, do so now. */
4285 if (*expr_p)
4287 tree lhs = TREE_OPERAND (*expr_p, 0);
4288 tree rhs = TREE_OPERAND (*expr_p, 1);
4289 gimple init = gimple_build_assign (lhs, rhs);
4290 gimplify_seq_add_stmt (pre_p, init);
4291 *expr_p = NULL;
4294 return GS_ALL_DONE;
4298 /* Given a pointer value OP0, return a simplified version of an
4299 indirection through OP0, or NULL_TREE if no simplification is
4300 possible. Note that the resulting type may be different from
4301 the type pointed to in the sense that it is still compatible
4302 from the langhooks point of view. */
4304 tree
4305 gimple_fold_indirect_ref (tree t)
4307 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4308 tree sub = t;
4309 tree subtype;
4311 STRIP_NOPS (sub);
4312 subtype = TREE_TYPE (sub);
4313 if (!POINTER_TYPE_P (subtype))
4314 return NULL_TREE;
4316 if (TREE_CODE (sub) == ADDR_EXPR)
4318 tree op = TREE_OPERAND (sub, 0);
4319 tree optype = TREE_TYPE (op);
4320 /* *&p => p */
4321 if (useless_type_conversion_p (type, optype))
4322 return op;
4324 /* *(foo *)&fooarray => fooarray[0] */
4325 if (TREE_CODE (optype) == ARRAY_TYPE
4326 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4327 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4329 tree type_domain = TYPE_DOMAIN (optype);
4330 tree min_val = size_zero_node;
4331 if (type_domain && TYPE_MIN_VALUE (type_domain))
4332 min_val = TYPE_MIN_VALUE (type_domain);
4333 if (TREE_CODE (min_val) == INTEGER_CST)
4334 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4336 /* *(foo *)&complexfoo => __real__ complexfoo */
4337 else if (TREE_CODE (optype) == COMPLEX_TYPE
4338 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4339 return fold_build1 (REALPART_EXPR, type, op);
4340 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4341 else if (TREE_CODE (optype) == VECTOR_TYPE
4342 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4344 tree part_width = TYPE_SIZE (type);
4345 tree index = bitsize_int (0);
4346 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4350 /* *(p + CST) -> ... */
4351 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4352 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4354 tree addr = TREE_OPERAND (sub, 0);
4355 tree off = TREE_OPERAND (sub, 1);
4356 tree addrtype;
4358 STRIP_NOPS (addr);
4359 addrtype = TREE_TYPE (addr);
4361 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4362 if (TREE_CODE (addr) == ADDR_EXPR
4363 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4364 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4365 && host_integerp (off, 1))
4367 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4368 tree part_width = TYPE_SIZE (type);
4369 unsigned HOST_WIDE_INT part_widthi
4370 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4371 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4372 tree index = bitsize_int (indexi);
4373 if (offset / part_widthi
4374 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4375 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4376 part_width, index);
4379 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4380 if (TREE_CODE (addr) == ADDR_EXPR
4381 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4382 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4384 tree size = TYPE_SIZE_UNIT (type);
4385 if (tree_int_cst_equal (size, off))
4386 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4389 /* *(p + CST) -> MEM_REF <p, CST>. */
4390 if (TREE_CODE (addr) != ADDR_EXPR
4391 || DECL_P (TREE_OPERAND (addr, 0)))
4392 return fold_build2 (MEM_REF, type,
4393 addr,
4394 build_int_cst_wide (ptype,
4395 TREE_INT_CST_LOW (off),
4396 TREE_INT_CST_HIGH (off)));
4399 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4400 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4401 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4402 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4404 tree type_domain;
4405 tree min_val = size_zero_node;
4406 tree osub = sub;
4407 sub = gimple_fold_indirect_ref (sub);
4408 if (! sub)
4409 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4410 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4411 if (type_domain && TYPE_MIN_VALUE (type_domain))
4412 min_val = TYPE_MIN_VALUE (type_domain);
4413 if (TREE_CODE (min_val) == INTEGER_CST)
4414 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4417 return NULL_TREE;
4420 /* Given a pointer value OP0, return a simplified version of an
4421 indirection through OP0, or NULL_TREE if no simplification is
4422 possible. This may only be applied to a rhs of an expression.
4423 Note that the resulting type may be different from the type pointed
4424 to in the sense that it is still compatible from the langhooks
4425 point of view. */
4427 static tree
4428 gimple_fold_indirect_ref_rhs (tree t)
4430 return gimple_fold_indirect_ref (t);
4433 /* Subroutine of gimplify_modify_expr to do simplifications of
4434 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4435 something changes. */
4437 static enum gimplify_status
4438 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4439 gimple_seq *pre_p, gimple_seq *post_p,
4440 bool want_value)
4442 enum gimplify_status ret = GS_UNHANDLED;
4443 bool changed;
4447 changed = false;
4448 switch (TREE_CODE (*from_p))
4450 case VAR_DECL:
4451 /* If we're assigning from a read-only variable initialized with
4452 a constructor, do the direct assignment from the constructor,
4453 but only if neither source nor target are volatile since this
4454 latter assignment might end up being done on a per-field basis. */
4455 if (DECL_INITIAL (*from_p)
4456 && TREE_READONLY (*from_p)
4457 && !TREE_THIS_VOLATILE (*from_p)
4458 && !TREE_THIS_VOLATILE (*to_p)
4459 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4461 tree old_from = *from_p;
4462 enum gimplify_status subret;
4464 /* Move the constructor into the RHS. */
4465 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4467 /* Let's see if gimplify_init_constructor will need to put
4468 it in memory. */
4469 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4470 false, true);
4471 if (subret == GS_ERROR)
4473 /* If so, revert the change. */
4474 *from_p = old_from;
4476 else
4478 ret = GS_OK;
4479 changed = true;
4482 break;
4483 case INDIRECT_REF:
4485 /* If we have code like
4487 *(const A*)(A*)&x
4489 where the type of "x" is a (possibly cv-qualified variant
4490 of "A"), treat the entire expression as identical to "x".
4491 This kind of code arises in C++ when an object is bound
4492 to a const reference, and if "x" is a TARGET_EXPR we want
4493 to take advantage of the optimization below. */
4494 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4495 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4496 if (t)
4498 if (TREE_THIS_VOLATILE (t) != volatile_p)
4500 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4501 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4502 build_fold_addr_expr (t));
4503 if (REFERENCE_CLASS_P (t))
4504 TREE_THIS_VOLATILE (t) = volatile_p;
4506 *from_p = t;
4507 ret = GS_OK;
4508 changed = true;
4510 break;
4513 case TARGET_EXPR:
4515 /* If we are initializing something from a TARGET_EXPR, strip the
4516 TARGET_EXPR and initialize it directly, if possible. This can't
4517 be done if the initializer is void, since that implies that the
4518 temporary is set in some non-trivial way.
4520 ??? What about code that pulls out the temp and uses it
4521 elsewhere? I think that such code never uses the TARGET_EXPR as
4522 an initializer. If I'm wrong, we'll die because the temp won't
4523 have any RTL. In that case, I guess we'll need to replace
4524 references somehow. */
4525 tree init = TARGET_EXPR_INITIAL (*from_p);
4527 if (init
4528 && !VOID_TYPE_P (TREE_TYPE (init)))
4530 *from_p = init;
4531 ret = GS_OK;
4532 changed = true;
4535 break;
4537 case COMPOUND_EXPR:
4538 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4539 caught. */
4540 gimplify_compound_expr (from_p, pre_p, true);
4541 ret = GS_OK;
4542 changed = true;
4543 break;
4545 case CONSTRUCTOR:
4546 /* If we already made some changes, let the front end have a
4547 crack at this before we break it down. */
4548 if (ret != GS_UNHANDLED)
4549 break;
4550 /* If we're initializing from a CONSTRUCTOR, break this into
4551 individual MODIFY_EXPRs. */
4552 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4553 false);
4555 case COND_EXPR:
4556 /* If we're assigning to a non-register type, push the assignment
4557 down into the branches. This is mandatory for ADDRESSABLE types,
4558 since we cannot generate temporaries for such, but it saves a
4559 copy in other cases as well. */
4560 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4562 /* This code should mirror the code in gimplify_cond_expr. */
4563 enum tree_code code = TREE_CODE (*expr_p);
4564 tree cond = *from_p;
4565 tree result = *to_p;
4567 ret = gimplify_expr (&result, pre_p, post_p,
4568 is_gimple_lvalue, fb_lvalue);
4569 if (ret != GS_ERROR)
4570 ret = GS_OK;
4572 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4573 TREE_OPERAND (cond, 1)
4574 = build2 (code, void_type_node, result,
4575 TREE_OPERAND (cond, 1));
4576 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4577 TREE_OPERAND (cond, 2)
4578 = build2 (code, void_type_node, unshare_expr (result),
4579 TREE_OPERAND (cond, 2));
4581 TREE_TYPE (cond) = void_type_node;
4582 recalculate_side_effects (cond);
4584 if (want_value)
4586 gimplify_and_add (cond, pre_p);
4587 *expr_p = unshare_expr (result);
4589 else
4590 *expr_p = cond;
4591 return ret;
4593 break;
4595 case CALL_EXPR:
4596 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4597 return slot so that we don't generate a temporary. */
4598 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4599 && aggregate_value_p (*from_p, *from_p))
4601 bool use_target;
4603 if (!(rhs_predicate_for (*to_p))(*from_p))
4604 /* If we need a temporary, *to_p isn't accurate. */
4605 use_target = false;
4606 /* It's OK to use the return slot directly unless it's an NRV. */
4607 else if (TREE_CODE (*to_p) == RESULT_DECL
4608 && DECL_NAME (*to_p) == NULL_TREE
4609 && needs_to_live_in_memory (*to_p))
4610 use_target = true;
4611 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4612 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4613 /* Don't force regs into memory. */
4614 use_target = false;
4615 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4616 /* It's OK to use the target directly if it's being
4617 initialized. */
4618 use_target = true;
4619 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4620 /* Always use the target and thus RSO for variable-sized types.
4621 GIMPLE cannot deal with a variable-sized assignment
4622 embedded in a call statement. */
4623 use_target = true;
4624 else if (TREE_CODE (*to_p) != SSA_NAME
4625 && (!is_gimple_variable (*to_p)
4626 || needs_to_live_in_memory (*to_p)))
4627 /* Don't use the original target if it's already addressable;
4628 if its address escapes, and the called function uses the
4629 NRV optimization, a conforming program could see *to_p
4630 change before the called function returns; see c++/19317.
4631 When optimizing, the return_slot pass marks more functions
4632 as safe after we have escape info. */
4633 use_target = false;
4634 else
4635 use_target = true;
4637 if (use_target)
4639 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4640 mark_addressable (*to_p);
4643 break;
4645 case WITH_SIZE_EXPR:
4646 /* Likewise for calls that return an aggregate of non-constant size,
4647 since we would not be able to generate a temporary at all. */
4648 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4650 *from_p = TREE_OPERAND (*from_p, 0);
4651 /* We don't change ret in this case because the
4652 WITH_SIZE_EXPR might have been added in
4653 gimplify_modify_expr, so returning GS_OK would lead to an
4654 infinite loop. */
4655 changed = true;
4657 break;
4659 /* If we're initializing from a container, push the initialization
4660 inside it. */
4661 case CLEANUP_POINT_EXPR:
4662 case BIND_EXPR:
4663 case STATEMENT_LIST:
4665 tree wrap = *from_p;
4666 tree t;
4668 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4669 fb_lvalue);
4670 if (ret != GS_ERROR)
4671 ret = GS_OK;
4673 t = voidify_wrapper_expr (wrap, *expr_p);
4674 gcc_assert (t == *expr_p);
4676 if (want_value)
4678 gimplify_and_add (wrap, pre_p);
4679 *expr_p = unshare_expr (*to_p);
4681 else
4682 *expr_p = wrap;
4683 return GS_OK;
4686 case COMPOUND_LITERAL_EXPR:
4688 tree complit = TREE_OPERAND (*expr_p, 1);
4689 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4690 tree decl = DECL_EXPR_DECL (decl_s);
4691 tree init = DECL_INITIAL (decl);
4693 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4694 into struct T x = { 0, 1, 2 } if the address of the
4695 compound literal has never been taken. */
4696 if (!TREE_ADDRESSABLE (complit)
4697 && !TREE_ADDRESSABLE (decl)
4698 && init)
4700 *expr_p = copy_node (*expr_p);
4701 TREE_OPERAND (*expr_p, 1) = init;
4702 return GS_OK;
4706 default:
4707 break;
4710 while (changed);
4712 return ret;
4716 /* Return true if T looks like a valid GIMPLE statement. */
4718 static bool
4719 is_gimple_stmt (tree t)
4721 const enum tree_code code = TREE_CODE (t);
4723 switch (code)
4725 case NOP_EXPR:
4726 /* The only valid NOP_EXPR is the empty statement. */
4727 return IS_EMPTY_STMT (t);
4729 case BIND_EXPR:
4730 case COND_EXPR:
4731 /* These are only valid if they're void. */
4732 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4734 case SWITCH_EXPR:
4735 case GOTO_EXPR:
4736 case RETURN_EXPR:
4737 case LABEL_EXPR:
4738 case CASE_LABEL_EXPR:
4739 case TRY_CATCH_EXPR:
4740 case TRY_FINALLY_EXPR:
4741 case EH_FILTER_EXPR:
4742 case CATCH_EXPR:
4743 case ASM_EXPR:
4744 case STATEMENT_LIST:
4745 case OMP_PARALLEL:
4746 case OMP_FOR:
4747 case OMP_SECTIONS:
4748 case OMP_SECTION:
4749 case OMP_SINGLE:
4750 case OMP_MASTER:
4751 case OMP_ORDERED:
4752 case OMP_CRITICAL:
4753 case OMP_TASK:
4754 /* These are always void. */
4755 return true;
4757 case CALL_EXPR:
4758 case MODIFY_EXPR:
4759 case PREDICT_EXPR:
4760 /* These are valid regardless of their type. */
4761 return true;
4763 default:
4764 return false;
4769 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4770 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4771 DECL_GIMPLE_REG_P set.
4773 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4774 other, unmodified part of the complex object just before the total store.
4775 As a consequence, if the object is still uninitialized, an undefined value
4776 will be loaded into a register, which may result in a spurious exception
4777 if the register is floating-point and the value happens to be a signaling
4778 NaN for example. Then the fully-fledged complex operations lowering pass
4779 followed by a DCE pass are necessary in order to fix things up. */
4781 static enum gimplify_status
4782 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4783 bool want_value)
4785 enum tree_code code, ocode;
4786 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4788 lhs = TREE_OPERAND (*expr_p, 0);
4789 rhs = TREE_OPERAND (*expr_p, 1);
4790 code = TREE_CODE (lhs);
4791 lhs = TREE_OPERAND (lhs, 0);
4793 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4794 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4795 TREE_NO_WARNING (other) = 1;
4796 other = get_formal_tmp_var (other, pre_p);
4798 realpart = code == REALPART_EXPR ? rhs : other;
4799 imagpart = code == REALPART_EXPR ? other : rhs;
4801 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4802 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4803 else
4804 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4806 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4807 *expr_p = (want_value) ? rhs : NULL_TREE;
4809 return GS_ALL_DONE;
4812 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4814 modify_expr
4815 : varname '=' rhs
4816 | '*' ID '=' rhs
4818 PRE_P points to the list where side effects that must happen before
4819 *EXPR_P should be stored.
4821 POST_P points to the list where side effects that must happen after
4822 *EXPR_P should be stored.
4824 WANT_VALUE is nonzero iff we want to use the value of this expression
4825 in another expression. */
4827 static enum gimplify_status
4828 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4829 bool want_value)
4831 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4832 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4833 enum gimplify_status ret = GS_UNHANDLED;
4834 gimple assign;
4835 location_t loc = EXPR_LOCATION (*expr_p);
4836 gimple_stmt_iterator gsi;
4838 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4839 || TREE_CODE (*expr_p) == INIT_EXPR);
4841 /* Trying to simplify a clobber using normal logic doesn't work,
4842 so handle it here. */
4843 if (TREE_CLOBBER_P (*from_p))
4845 gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4846 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4847 *expr_p = NULL;
4848 return GS_ALL_DONE;
4851 /* Insert pointer conversions required by the middle-end that are not
4852 required by the frontend. This fixes middle-end type checking for
4853 for example gcc.dg/redecl-6.c. */
4854 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4856 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4857 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4858 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4861 /* See if any simplifications can be done based on what the RHS is. */
4862 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4863 want_value);
4864 if (ret != GS_UNHANDLED)
4865 return ret;
4867 /* For zero sized types only gimplify the left hand side and right hand
4868 side as statements and throw away the assignment. Do this after
4869 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4870 types properly. */
4871 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4873 gimplify_stmt (from_p, pre_p);
4874 gimplify_stmt (to_p, pre_p);
4875 *expr_p = NULL_TREE;
4876 return GS_ALL_DONE;
4879 /* If the value being copied is of variable width, compute the length
4880 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4881 before gimplifying any of the operands so that we can resolve any
4882 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4883 the size of the expression to be copied, not of the destination, so
4884 that is what we must do here. */
4885 maybe_with_size_expr (from_p);
4887 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4888 if (ret == GS_ERROR)
4889 return ret;
4891 /* As a special case, we have to temporarily allow for assignments
4892 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4893 a toplevel statement, when gimplifying the GENERIC expression
4894 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4895 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4897 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4898 prevent gimplify_expr from trying to create a new temporary for
4899 foo's LHS, we tell it that it should only gimplify until it
4900 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4901 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4902 and all we need to do here is set 'a' to be its LHS. */
4903 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4904 fb_rvalue);
4905 if (ret == GS_ERROR)
4906 return ret;
4908 /* Now see if the above changed *from_p to something we handle specially. */
4909 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4910 want_value);
4911 if (ret != GS_UNHANDLED)
4912 return ret;
4914 /* If we've got a variable sized assignment between two lvalues (i.e. does
4915 not involve a call), then we can make things a bit more straightforward
4916 by converting the assignment to memcpy or memset. */
4917 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4919 tree from = TREE_OPERAND (*from_p, 0);
4920 tree size = TREE_OPERAND (*from_p, 1);
4922 if (TREE_CODE (from) == CONSTRUCTOR)
4923 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4925 if (is_gimple_addressable (from))
4927 *from_p = from;
4928 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4929 pre_p);
4933 /* Transform partial stores to non-addressable complex variables into
4934 total stores. This allows us to use real instead of virtual operands
4935 for these variables, which improves optimization. */
4936 if ((TREE_CODE (*to_p) == REALPART_EXPR
4937 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4938 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4939 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4941 /* Try to alleviate the effects of the gimplification creating artificial
4942 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4943 if (!gimplify_ctxp->into_ssa
4944 && TREE_CODE (*from_p) == VAR_DECL
4945 && DECL_IGNORED_P (*from_p)
4946 && DECL_P (*to_p)
4947 && !DECL_IGNORED_P (*to_p))
4949 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4950 DECL_NAME (*from_p)
4951 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4952 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4953 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4956 if (want_value && TREE_THIS_VOLATILE (*to_p))
4957 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4959 if (TREE_CODE (*from_p) == CALL_EXPR)
4961 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4962 instead of a GIMPLE_ASSIGN. */
4963 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4964 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4965 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4966 assign = gimple_build_call_from_tree (*from_p);
4967 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4968 if (!gimple_call_noreturn_p (assign))
4969 gimple_call_set_lhs (assign, *to_p);
4971 else
4973 assign = gimple_build_assign (*to_p, *from_p);
4974 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4977 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4979 /* We should have got an SSA name from the start. */
4980 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4983 gimplify_seq_add_stmt (pre_p, assign);
4984 gsi = gsi_last (*pre_p);
4985 fold_stmt (&gsi);
4987 if (want_value)
4989 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4990 return GS_OK;
4992 else
4993 *expr_p = NULL;
4995 return GS_ALL_DONE;
4998 /* Gimplify a comparison between two variable-sized objects. Do this
4999 with a call to BUILT_IN_MEMCMP. */
5001 static enum gimplify_status
5002 gimplify_variable_sized_compare (tree *expr_p)
5004 location_t loc = EXPR_LOCATION (*expr_p);
5005 tree op0 = TREE_OPERAND (*expr_p, 0);
5006 tree op1 = TREE_OPERAND (*expr_p, 1);
5007 tree t, arg, dest, src, expr;
5009 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
5010 arg = unshare_expr (arg);
5011 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
5012 src = build_fold_addr_expr_loc (loc, op1);
5013 dest = build_fold_addr_expr_loc (loc, op0);
5014 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
5015 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
5017 expr
5018 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
5019 SET_EXPR_LOCATION (expr, loc);
5020 *expr_p = expr;
5022 return GS_OK;
5025 /* Gimplify a comparison between two aggregate objects of integral scalar
5026 mode as a comparison between the bitwise equivalent scalar values. */
5028 static enum gimplify_status
5029 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
5031 location_t loc = EXPR_LOCATION (*expr_p);
5032 tree op0 = TREE_OPERAND (*expr_p, 0);
5033 tree op1 = TREE_OPERAND (*expr_p, 1);
5035 tree type = TREE_TYPE (op0);
5036 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
5038 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
5039 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
5041 *expr_p
5042 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
5044 return GS_OK;
5047 /* Gimplify an expression sequence. This function gimplifies each
5048 expression and rewrites the original expression with the last
5049 expression of the sequence in GIMPLE form.
5051 PRE_P points to the list where the side effects for all the
5052 expressions in the sequence will be emitted.
5054 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
5056 static enum gimplify_status
5057 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
5059 tree t = *expr_p;
5063 tree *sub_p = &TREE_OPERAND (t, 0);
5065 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
5066 gimplify_compound_expr (sub_p, pre_p, false);
5067 else
5068 gimplify_stmt (sub_p, pre_p);
5070 t = TREE_OPERAND (t, 1);
5072 while (TREE_CODE (t) == COMPOUND_EXPR);
5074 *expr_p = t;
5075 if (want_value)
5076 return GS_OK;
5077 else
5079 gimplify_stmt (expr_p, pre_p);
5080 return GS_ALL_DONE;
5084 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5085 gimplify. After gimplification, EXPR_P will point to a new temporary
5086 that holds the original value of the SAVE_EXPR node.
5088 PRE_P points to the list where side effects that must happen before
5089 *EXPR_P should be stored. */
5091 static enum gimplify_status
5092 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5094 enum gimplify_status ret = GS_ALL_DONE;
5095 tree val;
5097 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5098 val = TREE_OPERAND (*expr_p, 0);
5100 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5101 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5103 /* The operand may be a void-valued expression such as SAVE_EXPRs
5104 generated by the Java frontend for class initialization. It is
5105 being executed only for its side-effects. */
5106 if (TREE_TYPE (val) == void_type_node)
5108 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5109 is_gimple_stmt, fb_none);
5110 val = NULL;
5112 else
5113 val = get_initialized_tmp_var (val, pre_p, post_p);
5115 TREE_OPERAND (*expr_p, 0) = val;
5116 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5119 *expr_p = val;
5121 return ret;
5124 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5126 unary_expr
5127 : ...
5128 | '&' varname
5131 PRE_P points to the list where side effects that must happen before
5132 *EXPR_P should be stored.
5134 POST_P points to the list where side effects that must happen after
5135 *EXPR_P should be stored. */
5137 static enum gimplify_status
5138 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5140 tree expr = *expr_p;
5141 tree op0 = TREE_OPERAND (expr, 0);
5142 enum gimplify_status ret;
5143 location_t loc = EXPR_LOCATION (*expr_p);
5145 switch (TREE_CODE (op0))
5147 case INDIRECT_REF:
5148 do_indirect_ref:
5149 /* Check if we are dealing with an expression of the form '&*ptr'.
5150 While the front end folds away '&*ptr' into 'ptr', these
5151 expressions may be generated internally by the compiler (e.g.,
5152 builtins like __builtin_va_end). */
5153 /* Caution: the silent array decomposition semantics we allow for
5154 ADDR_EXPR means we can't always discard the pair. */
5155 /* Gimplification of the ADDR_EXPR operand may drop
5156 cv-qualification conversions, so make sure we add them if
5157 needed. */
5159 tree op00 = TREE_OPERAND (op0, 0);
5160 tree t_expr = TREE_TYPE (expr);
5161 tree t_op00 = TREE_TYPE (op00);
5163 if (!useless_type_conversion_p (t_expr, t_op00))
5164 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5165 *expr_p = op00;
5166 ret = GS_OK;
5168 break;
5170 case VIEW_CONVERT_EXPR:
5171 /* Take the address of our operand and then convert it to the type of
5172 this ADDR_EXPR.
5174 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5175 all clear. The impact of this transformation is even less clear. */
5177 /* If the operand is a useless conversion, look through it. Doing so
5178 guarantees that the ADDR_EXPR and its operand will remain of the
5179 same type. */
5180 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5181 op0 = TREE_OPERAND (op0, 0);
5183 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5184 build_fold_addr_expr_loc (loc,
5185 TREE_OPERAND (op0, 0)));
5186 ret = GS_OK;
5187 break;
5189 default:
5190 /* We use fb_either here because the C frontend sometimes takes
5191 the address of a call that returns a struct; see
5192 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5193 the implied temporary explicit. */
5195 /* Make the operand addressable. */
5196 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5197 is_gimple_addressable, fb_either);
5198 if (ret == GS_ERROR)
5199 break;
5201 /* Then mark it. Beware that it may not be possible to do so directly
5202 if a temporary has been created by the gimplification. */
5203 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5205 op0 = TREE_OPERAND (expr, 0);
5207 /* For various reasons, the gimplification of the expression
5208 may have made a new INDIRECT_REF. */
5209 if (TREE_CODE (op0) == INDIRECT_REF)
5210 goto do_indirect_ref;
5212 mark_addressable (TREE_OPERAND (expr, 0));
5214 /* The FEs may end up building ADDR_EXPRs early on a decl with
5215 an incomplete type. Re-build ADDR_EXPRs in canonical form
5216 here. */
5217 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5218 *expr_p = build_fold_addr_expr (op0);
5220 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5221 recompute_tree_invariant_for_addr_expr (*expr_p);
5223 /* If we re-built the ADDR_EXPR add a conversion to the original type
5224 if required. */
5225 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5226 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5228 break;
5231 return ret;
5234 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5235 value; output operands should be a gimple lvalue. */
5237 static enum gimplify_status
5238 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5240 tree expr;
5241 int noutputs;
5242 const char **oconstraints;
5243 int i;
5244 tree link;
5245 const char *constraint;
5246 bool allows_mem, allows_reg, is_inout;
5247 enum gimplify_status ret, tret;
5248 gimple stmt;
5249 vec<tree, va_gc> *inputs;
5250 vec<tree, va_gc> *outputs;
5251 vec<tree, va_gc> *clobbers;
5252 vec<tree, va_gc> *labels;
5253 tree link_next;
5255 expr = *expr_p;
5256 noutputs = list_length (ASM_OUTPUTS (expr));
5257 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5259 inputs = NULL;
5260 outputs = NULL;
5261 clobbers = NULL;
5262 labels = NULL;
5264 ret = GS_ALL_DONE;
5265 link_next = NULL_TREE;
5266 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5268 bool ok;
5269 size_t constraint_len;
5271 link_next = TREE_CHAIN (link);
5273 oconstraints[i]
5274 = constraint
5275 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5276 constraint_len = strlen (constraint);
5277 if (constraint_len == 0)
5278 continue;
5280 ok = parse_output_constraint (&constraint, i, 0, 0,
5281 &allows_mem, &allows_reg, &is_inout);
5282 if (!ok)
5284 ret = GS_ERROR;
5285 is_inout = false;
5288 if (!allows_reg && allows_mem)
5289 mark_addressable (TREE_VALUE (link));
5291 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5292 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5293 fb_lvalue | fb_mayfail);
5294 if (tret == GS_ERROR)
5296 error ("invalid lvalue in asm output %d", i);
5297 ret = tret;
5300 vec_safe_push (outputs, link);
5301 TREE_CHAIN (link) = NULL_TREE;
5303 if (is_inout)
5305 /* An input/output operand. To give the optimizers more
5306 flexibility, split it into separate input and output
5307 operands. */
5308 tree input;
5309 char buf[10];
5311 /* Turn the in/out constraint into an output constraint. */
5312 char *p = xstrdup (constraint);
5313 p[0] = '=';
5314 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5316 /* And add a matching input constraint. */
5317 if (allows_reg)
5319 sprintf (buf, "%d", i);
5321 /* If there are multiple alternatives in the constraint,
5322 handle each of them individually. Those that allow register
5323 will be replaced with operand number, the others will stay
5324 unchanged. */
5325 if (strchr (p, ',') != NULL)
5327 size_t len = 0, buflen = strlen (buf);
5328 char *beg, *end, *str, *dst;
5330 for (beg = p + 1;;)
5332 end = strchr (beg, ',');
5333 if (end == NULL)
5334 end = strchr (beg, '\0');
5335 if ((size_t) (end - beg) < buflen)
5336 len += buflen + 1;
5337 else
5338 len += end - beg + 1;
5339 if (*end)
5340 beg = end + 1;
5341 else
5342 break;
5345 str = (char *) alloca (len);
5346 for (beg = p + 1, dst = str;;)
5348 const char *tem;
5349 bool mem_p, reg_p, inout_p;
5351 end = strchr (beg, ',');
5352 if (end)
5353 *end = '\0';
5354 beg[-1] = '=';
5355 tem = beg - 1;
5356 parse_output_constraint (&tem, i, 0, 0,
5357 &mem_p, &reg_p, &inout_p);
5358 if (dst != str)
5359 *dst++ = ',';
5360 if (reg_p)
5362 memcpy (dst, buf, buflen);
5363 dst += buflen;
5365 else
5367 if (end)
5368 len = end - beg;
5369 else
5370 len = strlen (beg);
5371 memcpy (dst, beg, len);
5372 dst += len;
5374 if (end)
5375 beg = end + 1;
5376 else
5377 break;
5379 *dst = '\0';
5380 input = build_string (dst - str, str);
5382 else
5383 input = build_string (strlen (buf), buf);
5385 else
5386 input = build_string (constraint_len - 1, constraint + 1);
5388 free (p);
5390 input = build_tree_list (build_tree_list (NULL_TREE, input),
5391 unshare_expr (TREE_VALUE (link)));
5392 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5396 link_next = NULL_TREE;
5397 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5399 link_next = TREE_CHAIN (link);
5400 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5401 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5402 oconstraints, &allows_mem, &allows_reg);
5404 /* If we can't make copies, we can only accept memory. */
5405 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5407 if (allows_mem)
5408 allows_reg = 0;
5409 else
5411 error ("impossible constraint in %<asm%>");
5412 error ("non-memory input %d must stay in memory", i);
5413 return GS_ERROR;
5417 /* If the operand is a memory input, it should be an lvalue. */
5418 if (!allows_reg && allows_mem)
5420 tree inputv = TREE_VALUE (link);
5421 STRIP_NOPS (inputv);
5422 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5423 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5424 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5425 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5426 TREE_VALUE (link) = error_mark_node;
5427 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5428 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5429 mark_addressable (TREE_VALUE (link));
5430 if (tret == GS_ERROR)
5432 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5433 input_location = EXPR_LOCATION (TREE_VALUE (link));
5434 error ("memory input %d is not directly addressable", i);
5435 ret = tret;
5438 else
5440 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5441 is_gimple_asm_val, fb_rvalue);
5442 if (tret == GS_ERROR)
5443 ret = tret;
5446 TREE_CHAIN (link) = NULL_TREE;
5447 vec_safe_push (inputs, link);
5450 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5451 vec_safe_push (clobbers, link);
5453 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5454 vec_safe_push (labels, link);
5456 /* Do not add ASMs with errors to the gimple IL stream. */
5457 if (ret != GS_ERROR)
5459 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5460 inputs, outputs, clobbers, labels);
5462 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5463 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5465 gimplify_seq_add_stmt (pre_p, stmt);
5468 return ret;
5471 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5472 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5473 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5474 return to this function.
5476 FIXME should we complexify the prequeue handling instead? Or use flags
5477 for all the cleanups and let the optimizer tighten them up? The current
5478 code seems pretty fragile; it will break on a cleanup within any
5479 non-conditional nesting. But any such nesting would be broken, anyway;
5480 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5481 and continues out of it. We can do that at the RTL level, though, so
5482 having an optimizer to tighten up try/finally regions would be a Good
5483 Thing. */
5485 static enum gimplify_status
5486 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5488 gimple_stmt_iterator iter;
5489 gimple_seq body_sequence = NULL;
5491 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5493 /* We only care about the number of conditions between the innermost
5494 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5495 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5496 int old_conds = gimplify_ctxp->conditions;
5497 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5498 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5499 gimplify_ctxp->conditions = 0;
5500 gimplify_ctxp->conditional_cleanups = NULL;
5501 gimplify_ctxp->in_cleanup_point_expr = true;
5503 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5505 gimplify_ctxp->conditions = old_conds;
5506 gimplify_ctxp->conditional_cleanups = old_cleanups;
5507 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5509 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5511 gimple wce = gsi_stmt (iter);
5513 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5515 if (gsi_one_before_end_p (iter))
5517 /* Note that gsi_insert_seq_before and gsi_remove do not
5518 scan operands, unlike some other sequence mutators. */
5519 if (!gimple_wce_cleanup_eh_only (wce))
5520 gsi_insert_seq_before_without_update (&iter,
5521 gimple_wce_cleanup (wce),
5522 GSI_SAME_STMT);
5523 gsi_remove (&iter, true);
5524 break;
5526 else
5528 gimple gtry;
5529 gimple_seq seq;
5530 enum gimple_try_flags kind;
5532 if (gimple_wce_cleanup_eh_only (wce))
5533 kind = GIMPLE_TRY_CATCH;
5534 else
5535 kind = GIMPLE_TRY_FINALLY;
5536 seq = gsi_split_seq_after (iter);
5538 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5539 /* Do not use gsi_replace here, as it may scan operands.
5540 We want to do a simple structural modification only. */
5541 gsi_set_stmt (&iter, gtry);
5542 iter = gsi_start (gtry->gimple_try.eval);
5545 else
5546 gsi_next (&iter);
5549 gimplify_seq_add_seq (pre_p, body_sequence);
5550 if (temp)
5552 *expr_p = temp;
5553 return GS_OK;
5555 else
5557 *expr_p = NULL;
5558 return GS_ALL_DONE;
5562 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5563 is the cleanup action required. EH_ONLY is true if the cleanup should
5564 only be executed if an exception is thrown, not on normal exit. */
5566 static void
5567 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5569 gimple wce;
5570 gimple_seq cleanup_stmts = NULL;
5572 /* Errors can result in improperly nested cleanups. Which results in
5573 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5574 if (seen_error ())
5575 return;
5577 if (gimple_conditional_context ())
5579 /* If we're in a conditional context, this is more complex. We only
5580 want to run the cleanup if we actually ran the initialization that
5581 necessitates it, but we want to run it after the end of the
5582 conditional context. So we wrap the try/finally around the
5583 condition and use a flag to determine whether or not to actually
5584 run the destructor. Thus
5586 test ? f(A()) : 0
5588 becomes (approximately)
5590 flag = 0;
5591 try {
5592 if (test) { A::A(temp); flag = 1; val = f(temp); }
5593 else { val = 0; }
5594 } finally {
5595 if (flag) A::~A(temp);
5599 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5600 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5601 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5603 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5604 gimplify_stmt (&cleanup, &cleanup_stmts);
5605 wce = gimple_build_wce (cleanup_stmts);
5607 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5608 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5609 gimplify_seq_add_stmt (pre_p, ftrue);
5611 /* Because of this manipulation, and the EH edges that jump
5612 threading cannot redirect, the temporary (VAR) will appear
5613 to be used uninitialized. Don't warn. */
5614 TREE_NO_WARNING (var) = 1;
5616 else
5618 gimplify_stmt (&cleanup, &cleanup_stmts);
5619 wce = gimple_build_wce (cleanup_stmts);
5620 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5621 gimplify_seq_add_stmt (pre_p, wce);
5625 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5627 static enum gimplify_status
5628 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5630 tree targ = *expr_p;
5631 tree temp = TARGET_EXPR_SLOT (targ);
5632 tree init = TARGET_EXPR_INITIAL (targ);
5633 enum gimplify_status ret;
5635 if (init)
5637 tree cleanup = NULL_TREE;
5639 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5640 to the temps list. Handle also variable length TARGET_EXPRs. */
5641 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5643 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5644 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5645 gimplify_vla_decl (temp, pre_p);
5647 else
5648 gimple_add_tmp_var (temp);
5650 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5651 expression is supposed to initialize the slot. */
5652 if (VOID_TYPE_P (TREE_TYPE (init)))
5653 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5654 else
5656 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5657 init = init_expr;
5658 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5659 init = NULL;
5660 ggc_free (init_expr);
5662 if (ret == GS_ERROR)
5664 /* PR c++/28266 Make sure this is expanded only once. */
5665 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5666 return GS_ERROR;
5668 if (init)
5669 gimplify_and_add (init, pre_p);
5671 /* If needed, push the cleanup for the temp. */
5672 if (TARGET_EXPR_CLEANUP (targ))
5674 if (CLEANUP_EH_ONLY (targ))
5675 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5676 CLEANUP_EH_ONLY (targ), pre_p);
5677 else
5678 cleanup = TARGET_EXPR_CLEANUP (targ);
5681 /* Add a clobber for the temporary going out of scope, like
5682 gimplify_bind_expr. */
5683 if (gimplify_ctxp->in_cleanup_point_expr
5684 && needs_to_live_in_memory (temp)
5685 && flag_stack_reuse == SR_ALL)
5687 tree clobber = build_constructor (TREE_TYPE (temp),
5688 NULL);
5689 TREE_THIS_VOLATILE (clobber) = true;
5690 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5691 if (cleanup)
5692 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5693 clobber);
5694 else
5695 cleanup = clobber;
5698 if (cleanup)
5699 gimple_push_cleanup (temp, cleanup, false, pre_p);
5701 /* Only expand this once. */
5702 TREE_OPERAND (targ, 3) = init;
5703 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5705 else
5706 /* We should have expanded this before. */
5707 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5709 *expr_p = temp;
5710 return GS_OK;
5713 /* Gimplification of expression trees. */
5715 /* Gimplify an expression which appears at statement context. The
5716 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5717 NULL, a new sequence is allocated.
5719 Return true if we actually added a statement to the queue. */
5721 bool
5722 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5724 gimple_seq_node last;
5726 last = gimple_seq_last (*seq_p);
5727 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5728 return last != gimple_seq_last (*seq_p);
5731 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5732 to CTX. If entries already exist, force them to be some flavor of private.
5733 If there is no enclosing parallel, do nothing. */
5735 void
5736 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5738 splay_tree_node n;
5740 if (decl == NULL || !DECL_P (decl))
5741 return;
5745 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5746 if (n != NULL)
5748 if (n->value & GOVD_SHARED)
5749 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5750 else
5751 return;
5753 else if (ctx->region_type != ORT_WORKSHARE)
5754 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5756 ctx = ctx->outer_context;
5758 while (ctx);
5761 /* Similarly for each of the type sizes of TYPE. */
5763 static void
5764 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5766 if (type == NULL || type == error_mark_node)
5767 return;
5768 type = TYPE_MAIN_VARIANT (type);
5770 if (pointer_set_insert (ctx->privatized_types, type))
5771 return;
5773 switch (TREE_CODE (type))
5775 case INTEGER_TYPE:
5776 case ENUMERAL_TYPE:
5777 case BOOLEAN_TYPE:
5778 case REAL_TYPE:
5779 case FIXED_POINT_TYPE:
5780 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5781 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5782 break;
5784 case ARRAY_TYPE:
5785 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5786 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5787 break;
5789 case RECORD_TYPE:
5790 case UNION_TYPE:
5791 case QUAL_UNION_TYPE:
5793 tree field;
5794 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5795 if (TREE_CODE (field) == FIELD_DECL)
5797 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5798 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5801 break;
5803 case POINTER_TYPE:
5804 case REFERENCE_TYPE:
5805 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5806 break;
5808 default:
5809 break;
5812 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5813 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5814 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5817 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5819 static void
5820 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5822 splay_tree_node n;
5823 unsigned int nflags;
5824 tree t;
5826 if (error_operand_p (decl))
5827 return;
5829 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5830 there are constructors involved somewhere. */
5831 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5832 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5833 flags |= GOVD_SEEN;
5835 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5836 if (n != NULL)
5838 /* We shouldn't be re-adding the decl with the same data
5839 sharing class. */
5840 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5841 /* The only combination of data sharing classes we should see is
5842 FIRSTPRIVATE and LASTPRIVATE. */
5843 nflags = n->value | flags;
5844 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5845 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5846 n->value = nflags;
5847 return;
5850 /* When adding a variable-sized variable, we have to handle all sorts
5851 of additional bits of data: the pointer replacement variable, and
5852 the parameters of the type. */
5853 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5855 /* Add the pointer replacement variable as PRIVATE if the variable
5856 replacement is private, else FIRSTPRIVATE since we'll need the
5857 address of the original variable either for SHARED, or for the
5858 copy into or out of the context. */
5859 if (!(flags & GOVD_LOCAL))
5861 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5862 nflags |= flags & GOVD_SEEN;
5863 t = DECL_VALUE_EXPR (decl);
5864 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5865 t = TREE_OPERAND (t, 0);
5866 gcc_assert (DECL_P (t));
5867 omp_add_variable (ctx, t, nflags);
5870 /* Add all of the variable and type parameters (which should have
5871 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5872 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5873 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5874 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5876 /* The variable-sized variable itself is never SHARED, only some form
5877 of PRIVATE. The sharing would take place via the pointer variable
5878 which we remapped above. */
5879 if (flags & GOVD_SHARED)
5880 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5881 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5883 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5884 alloca statement we generate for the variable, so make sure it
5885 is available. This isn't automatically needed for the SHARED
5886 case, since we won't be allocating local storage then.
5887 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5888 in this case omp_notice_variable will be called later
5889 on when it is gimplified. */
5890 else if (! (flags & GOVD_LOCAL)
5891 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5892 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5894 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5896 gcc_assert ((flags & GOVD_LOCAL) == 0);
5897 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5899 /* Similar to the direct variable sized case above, we'll need the
5900 size of references being privatized. */
5901 if ((flags & GOVD_SHARED) == 0)
5903 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5904 if (TREE_CODE (t) != INTEGER_CST)
5905 omp_notice_variable (ctx, t, true);
5909 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5912 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5913 This just prints out diagnostics about threadprivate variable uses
5914 in untied tasks. If DECL2 is non-NULL, prevent this warning
5915 on that variable. */
5917 static bool
5918 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5919 tree decl2)
5921 splay_tree_node n;
5923 if (ctx->region_type != ORT_UNTIED_TASK)
5924 return false;
5925 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5926 if (n == NULL)
5928 error ("threadprivate variable %qE used in untied task",
5929 DECL_NAME (decl));
5930 error_at (ctx->location, "enclosing task");
5931 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5933 if (decl2)
5934 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5935 return false;
5938 /* Record the fact that DECL was used within the OpenMP context CTX.
5939 IN_CODE is true when real code uses DECL, and false when we should
5940 merely emit default(none) errors. Return true if DECL is going to
5941 be remapped and thus DECL shouldn't be gimplified into its
5942 DECL_VALUE_EXPR (if any). */
5944 static bool
5945 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5947 splay_tree_node n;
5948 unsigned flags = in_code ? GOVD_SEEN : 0;
5949 bool ret = false, shared;
5951 if (error_operand_p (decl))
5952 return false;
5954 /* Threadprivate variables are predetermined. */
5955 if (is_global_var (decl))
5957 if (DECL_THREAD_LOCAL_P (decl))
5958 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5960 if (DECL_HAS_VALUE_EXPR_P (decl))
5962 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5964 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5965 return omp_notice_threadprivate_variable (ctx, decl, value);
5969 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5970 if (n == NULL)
5972 enum omp_clause_default_kind default_kind, kind;
5973 struct gimplify_omp_ctx *octx;
5975 if (ctx->region_type == ORT_WORKSHARE)
5976 goto do_outer;
5978 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5979 remapped firstprivate instead of shared. To some extent this is
5980 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5981 default_kind = ctx->default_kind;
5982 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5983 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5984 default_kind = kind;
5986 switch (default_kind)
5988 case OMP_CLAUSE_DEFAULT_NONE:
5989 error ("%qE not specified in enclosing parallel",
5990 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5991 if ((ctx->region_type & ORT_TASK) != 0)
5992 error_at (ctx->location, "enclosing task");
5993 else
5994 error_at (ctx->location, "enclosing parallel");
5995 /* FALLTHRU */
5996 case OMP_CLAUSE_DEFAULT_SHARED:
5997 flags |= GOVD_SHARED;
5998 break;
5999 case OMP_CLAUSE_DEFAULT_PRIVATE:
6000 flags |= GOVD_PRIVATE;
6001 break;
6002 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
6003 flags |= GOVD_FIRSTPRIVATE;
6004 break;
6005 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6006 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6007 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6008 if (ctx->outer_context)
6009 omp_notice_variable (ctx->outer_context, decl, in_code);
6010 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
6012 splay_tree_node n2;
6014 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6015 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6017 flags |= GOVD_FIRSTPRIVATE;
6018 break;
6020 if ((octx->region_type & ORT_PARALLEL) != 0)
6021 break;
6023 if (flags & GOVD_FIRSTPRIVATE)
6024 break;
6025 if (octx == NULL
6026 && (TREE_CODE (decl) == PARM_DECL
6027 || (!is_global_var (decl)
6028 && DECL_CONTEXT (decl) == current_function_decl)))
6030 flags |= GOVD_FIRSTPRIVATE;
6031 break;
6033 flags |= GOVD_SHARED;
6034 break;
6035 default:
6036 gcc_unreachable ();
6039 if ((flags & GOVD_PRIVATE)
6040 && lang_hooks.decls.omp_private_outer_ref (decl))
6041 flags |= GOVD_PRIVATE_OUTER_REF;
6043 omp_add_variable (ctx, decl, flags);
6045 shared = (flags & GOVD_SHARED) != 0;
6046 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6047 goto do_outer;
6050 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6051 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6052 && DECL_SIZE (decl)
6053 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6055 splay_tree_node n2;
6056 tree t = DECL_VALUE_EXPR (decl);
6057 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6058 t = TREE_OPERAND (t, 0);
6059 gcc_assert (DECL_P (t));
6060 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6061 n2->value |= GOVD_SEEN;
6064 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6065 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6067 /* If nothing changed, there's nothing left to do. */
6068 if ((n->value & flags) == flags)
6069 return ret;
6070 flags |= n->value;
6071 n->value = flags;
6073 do_outer:
6074 /* If the variable is private in the current context, then we don't
6075 need to propagate anything to an outer context. */
6076 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6077 return ret;
6078 if (ctx->outer_context
6079 && omp_notice_variable (ctx->outer_context, decl, in_code))
6080 return true;
6081 return ret;
6084 /* Verify that DECL is private within CTX. If there's specific information
6085 to the contrary in the innermost scope, generate an error. */
6087 static bool
6088 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
6090 splay_tree_node n;
6092 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6093 if (n != NULL)
6095 if (n->value & GOVD_SHARED)
6097 if (ctx == gimplify_omp_ctxp)
6099 error ("iteration variable %qE should be private",
6100 DECL_NAME (decl));
6101 n->value = GOVD_PRIVATE;
6102 return true;
6104 else
6105 return false;
6107 else if ((n->value & GOVD_EXPLICIT) != 0
6108 && (ctx == gimplify_omp_ctxp
6109 || (ctx->region_type == ORT_COMBINED_PARALLEL
6110 && gimplify_omp_ctxp->outer_context == ctx)))
6112 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6113 error ("iteration variable %qE should not be firstprivate",
6114 DECL_NAME (decl));
6115 else if ((n->value & GOVD_REDUCTION) != 0)
6116 error ("iteration variable %qE should not be reduction",
6117 DECL_NAME (decl));
6119 return (ctx == gimplify_omp_ctxp
6120 || (ctx->region_type == ORT_COMBINED_PARALLEL
6121 && gimplify_omp_ctxp->outer_context == ctx));
6124 if (ctx->region_type != ORT_WORKSHARE)
6125 return false;
6126 else if (ctx->outer_context)
6127 return omp_is_private (ctx->outer_context, decl);
6128 return false;
6131 /* Return true if DECL is private within a parallel region
6132 that binds to the current construct's context or in parallel
6133 region's REDUCTION clause. */
6135 static bool
6136 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl, bool copyprivate)
6138 splay_tree_node n;
6142 ctx = ctx->outer_context;
6143 if (ctx == NULL)
6144 return !(is_global_var (decl)
6145 /* References might be private, but might be shared too,
6146 when checking for copyprivate, assume they might be
6147 private, otherwise assume they might be shared. */
6148 || (!copyprivate
6149 && lang_hooks.decls.omp_privatize_by_reference (decl)));
6151 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6152 if (n != NULL)
6153 return (n->value & GOVD_SHARED) == 0;
6155 while (ctx->region_type == ORT_WORKSHARE);
6156 return false;
6159 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6160 and previous omp contexts. */
6162 static void
6163 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6164 enum omp_region_type region_type)
6166 struct gimplify_omp_ctx *ctx, *outer_ctx;
6167 struct gimplify_ctx gctx;
6168 tree c;
6170 ctx = new_omp_context (region_type);
6171 outer_ctx = ctx->outer_context;
6173 while ((c = *list_p) != NULL)
6175 bool remove = false;
6176 bool notice_outer = true;
6177 const char *check_non_private = NULL;
6178 unsigned int flags;
6179 tree decl;
6181 switch (OMP_CLAUSE_CODE (c))
6183 case OMP_CLAUSE_PRIVATE:
6184 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6185 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6187 flags |= GOVD_PRIVATE_OUTER_REF;
6188 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6190 else
6191 notice_outer = false;
6192 goto do_add;
6193 case OMP_CLAUSE_SHARED:
6194 flags = GOVD_SHARED | GOVD_EXPLICIT;
6195 goto do_add;
6196 case OMP_CLAUSE_FIRSTPRIVATE:
6197 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6198 check_non_private = "firstprivate";
6199 goto do_add;
6200 case OMP_CLAUSE_LASTPRIVATE:
6201 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6202 check_non_private = "lastprivate";
6203 goto do_add;
6204 case OMP_CLAUSE_REDUCTION:
6205 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6206 check_non_private = "reduction";
6207 goto do_add;
6209 do_add:
6210 decl = OMP_CLAUSE_DECL (c);
6211 if (error_operand_p (decl))
6213 remove = true;
6214 break;
6216 omp_add_variable (ctx, decl, flags);
6217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6218 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6220 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6221 GOVD_LOCAL | GOVD_SEEN);
6222 gimplify_omp_ctxp = ctx;
6223 push_gimplify_context (&gctx);
6225 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6226 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6228 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6229 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6230 pop_gimplify_context
6231 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6232 push_gimplify_context (&gctx);
6233 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6234 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6235 pop_gimplify_context
6236 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6237 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6238 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6240 gimplify_omp_ctxp = outer_ctx;
6242 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6243 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6245 gimplify_omp_ctxp = ctx;
6246 push_gimplify_context (&gctx);
6247 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6249 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6250 NULL, NULL);
6251 TREE_SIDE_EFFECTS (bind) = 1;
6252 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6253 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6255 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6256 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6257 pop_gimplify_context
6258 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6259 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6261 gimplify_omp_ctxp = outer_ctx;
6263 if (notice_outer)
6264 goto do_notice;
6265 break;
6267 case OMP_CLAUSE_COPYIN:
6268 case OMP_CLAUSE_COPYPRIVATE:
6269 decl = OMP_CLAUSE_DECL (c);
6270 if (error_operand_p (decl))
6272 remove = true;
6273 break;
6275 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_COPYPRIVATE
6276 && !remove
6277 && !omp_check_private (ctx, decl, true))
6279 remove = true;
6280 if (is_global_var (decl))
6282 if (DECL_THREAD_LOCAL_P (decl))
6283 remove = false;
6284 else if (DECL_HAS_VALUE_EXPR_P (decl))
6286 tree value = get_base_address (DECL_VALUE_EXPR (decl));
6288 if (value
6289 && DECL_P (value)
6290 && DECL_THREAD_LOCAL_P (value))
6291 remove = false;
6294 if (remove)
6295 error_at (OMP_CLAUSE_LOCATION (c),
6296 "copyprivate variable %qE is not threadprivate"
6297 " or private in outer context", DECL_NAME (decl));
6299 do_notice:
6300 if (outer_ctx)
6301 omp_notice_variable (outer_ctx, decl, true);
6302 if (check_non_private
6303 && region_type == ORT_WORKSHARE
6304 && omp_check_private (ctx, decl, false))
6306 error ("%s variable %qE is private in outer context",
6307 check_non_private, DECL_NAME (decl));
6308 remove = true;
6310 break;
6312 case OMP_CLAUSE_FINAL:
6313 case OMP_CLAUSE_IF:
6314 OMP_CLAUSE_OPERAND (c, 0)
6315 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6316 /* Fall through. */
6318 case OMP_CLAUSE_SCHEDULE:
6319 case OMP_CLAUSE_NUM_THREADS:
6320 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6321 is_gimple_val, fb_rvalue) == GS_ERROR)
6322 remove = true;
6323 break;
6325 case OMP_CLAUSE_NOWAIT:
6326 case OMP_CLAUSE_ORDERED:
6327 case OMP_CLAUSE_UNTIED:
6328 case OMP_CLAUSE_COLLAPSE:
6329 case OMP_CLAUSE_MERGEABLE:
6330 break;
6332 case OMP_CLAUSE_DEFAULT:
6333 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6334 break;
6336 default:
6337 gcc_unreachable ();
6340 if (remove)
6341 *list_p = OMP_CLAUSE_CHAIN (c);
6342 else
6343 list_p = &OMP_CLAUSE_CHAIN (c);
6346 gimplify_omp_ctxp = ctx;
6349 /* For all variables that were not actually used within the context,
6350 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6352 static int
6353 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6355 tree *list_p = (tree *) data;
6356 tree decl = (tree) n->key;
6357 unsigned flags = n->value;
6358 enum omp_clause_code code;
6359 tree clause;
6360 bool private_debug;
6362 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6363 return 0;
6364 if ((flags & GOVD_SEEN) == 0)
6365 return 0;
6366 if (flags & GOVD_DEBUG_PRIVATE)
6368 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6369 private_debug = true;
6371 else
6372 private_debug
6373 = lang_hooks.decls.omp_private_debug_clause (decl,
6374 !!(flags & GOVD_SHARED));
6375 if (private_debug)
6376 code = OMP_CLAUSE_PRIVATE;
6377 else if (flags & GOVD_SHARED)
6379 if (is_global_var (decl))
6381 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6382 while (ctx != NULL)
6384 splay_tree_node on
6385 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6386 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6387 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6388 break;
6389 ctx = ctx->outer_context;
6391 if (ctx == NULL)
6392 return 0;
6394 code = OMP_CLAUSE_SHARED;
6396 else if (flags & GOVD_PRIVATE)
6397 code = OMP_CLAUSE_PRIVATE;
6398 else if (flags & GOVD_FIRSTPRIVATE)
6399 code = OMP_CLAUSE_FIRSTPRIVATE;
6400 else
6401 gcc_unreachable ();
6403 clause = build_omp_clause (input_location, code);
6404 OMP_CLAUSE_DECL (clause) = decl;
6405 OMP_CLAUSE_CHAIN (clause) = *list_p;
6406 if (private_debug)
6407 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6408 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6409 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6410 *list_p = clause;
6411 lang_hooks.decls.omp_finish_clause (clause);
6413 return 0;
6416 static void
6417 gimplify_adjust_omp_clauses (tree *list_p)
6419 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6420 tree c, decl;
6422 while ((c = *list_p) != NULL)
6424 splay_tree_node n;
6425 bool remove = false;
6427 switch (OMP_CLAUSE_CODE (c))
6429 case OMP_CLAUSE_PRIVATE:
6430 case OMP_CLAUSE_SHARED:
6431 case OMP_CLAUSE_FIRSTPRIVATE:
6432 decl = OMP_CLAUSE_DECL (c);
6433 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6434 remove = !(n->value & GOVD_SEEN);
6435 if (! remove)
6437 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6438 if ((n->value & GOVD_DEBUG_PRIVATE)
6439 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6441 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6442 || ((n->value & GOVD_DATA_SHARE_CLASS)
6443 == GOVD_PRIVATE));
6444 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6445 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6448 break;
6450 case OMP_CLAUSE_LASTPRIVATE:
6451 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6452 accurately reflect the presence of a FIRSTPRIVATE clause. */
6453 decl = OMP_CLAUSE_DECL (c);
6454 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6455 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6456 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6457 break;
6459 case OMP_CLAUSE_REDUCTION:
6460 case OMP_CLAUSE_COPYIN:
6461 case OMP_CLAUSE_COPYPRIVATE:
6462 case OMP_CLAUSE_IF:
6463 case OMP_CLAUSE_NUM_THREADS:
6464 case OMP_CLAUSE_SCHEDULE:
6465 case OMP_CLAUSE_NOWAIT:
6466 case OMP_CLAUSE_ORDERED:
6467 case OMP_CLAUSE_DEFAULT:
6468 case OMP_CLAUSE_UNTIED:
6469 case OMP_CLAUSE_COLLAPSE:
6470 case OMP_CLAUSE_FINAL:
6471 case OMP_CLAUSE_MERGEABLE:
6472 break;
6474 default:
6475 gcc_unreachable ();
6478 if (remove)
6479 *list_p = OMP_CLAUSE_CHAIN (c);
6480 else
6481 list_p = &OMP_CLAUSE_CHAIN (c);
6484 /* Add in any implicit data sharing. */
6485 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6487 gimplify_omp_ctxp = ctx->outer_context;
6488 delete_omp_context (ctx);
6491 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6492 gimplification of the body, as well as scanning the body for used
6493 variables. We need to do this scan now, because variable-sized
6494 decls will be decomposed during gimplification. */
6496 static void
6497 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6499 tree expr = *expr_p;
6500 gimple g;
6501 gimple_seq body = NULL;
6502 struct gimplify_ctx gctx;
6504 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6505 OMP_PARALLEL_COMBINED (expr)
6506 ? ORT_COMBINED_PARALLEL
6507 : ORT_PARALLEL);
6509 push_gimplify_context (&gctx);
6511 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6512 if (gimple_code (g) == GIMPLE_BIND)
6513 pop_gimplify_context (g);
6514 else
6515 pop_gimplify_context (NULL);
6517 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6519 g = gimple_build_omp_parallel (body,
6520 OMP_PARALLEL_CLAUSES (expr),
6521 NULL_TREE, NULL_TREE);
6522 if (OMP_PARALLEL_COMBINED (expr))
6523 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6524 gimplify_seq_add_stmt (pre_p, g);
6525 *expr_p = NULL_TREE;
6528 /* Gimplify the contents of an OMP_TASK statement. This involves
6529 gimplification of the body, as well as scanning the body for used
6530 variables. We need to do this scan now, because variable-sized
6531 decls will be decomposed during gimplification. */
6533 static void
6534 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6536 tree expr = *expr_p;
6537 gimple g;
6538 gimple_seq body = NULL;
6539 struct gimplify_ctx gctx;
6541 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6542 find_omp_clause (OMP_TASK_CLAUSES (expr),
6543 OMP_CLAUSE_UNTIED)
6544 ? ORT_UNTIED_TASK : ORT_TASK);
6546 push_gimplify_context (&gctx);
6548 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6549 if (gimple_code (g) == GIMPLE_BIND)
6550 pop_gimplify_context (g);
6551 else
6552 pop_gimplify_context (NULL);
6554 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6556 g = gimple_build_omp_task (body,
6557 OMP_TASK_CLAUSES (expr),
6558 NULL_TREE, NULL_TREE,
6559 NULL_TREE, NULL_TREE, NULL_TREE);
6560 gimplify_seq_add_stmt (pre_p, g);
6561 *expr_p = NULL_TREE;
6564 /* Gimplify the gross structure of an OMP_FOR statement. */
6566 static enum gimplify_status
6567 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6569 tree for_stmt, decl, var, t;
6570 enum gimplify_status ret = GS_ALL_DONE;
6571 enum gimplify_status tret;
6572 gimple gfor;
6573 gimple_seq for_body, for_pre_body;
6574 int i;
6576 for_stmt = *expr_p;
6578 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6579 ORT_WORKSHARE);
6581 /* Handle OMP_FOR_INIT. */
6582 for_pre_body = NULL;
6583 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6584 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6586 for_body = NULL;
6587 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6588 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6589 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6590 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6591 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6593 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6594 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6595 decl = TREE_OPERAND (t, 0);
6596 gcc_assert (DECL_P (decl));
6597 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6598 || POINTER_TYPE_P (TREE_TYPE (decl)));
6600 /* Make sure the iteration variable is private. */
6601 if (omp_is_private (gimplify_omp_ctxp, decl))
6602 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6603 else
6604 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6606 /* If DECL is not a gimple register, create a temporary variable to act
6607 as an iteration counter. This is valid, since DECL cannot be
6608 modified in the body of the loop. */
6609 if (!is_gimple_reg (decl))
6611 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6612 TREE_OPERAND (t, 0) = var;
6614 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6616 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6618 else
6619 var = decl;
6621 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6622 is_gimple_val, fb_rvalue);
6623 ret = MIN (ret, tret);
6624 if (ret == GS_ERROR)
6625 return ret;
6627 /* Handle OMP_FOR_COND. */
6628 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6629 gcc_assert (COMPARISON_CLASS_P (t));
6630 gcc_assert (TREE_OPERAND (t, 0) == decl);
6632 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6633 is_gimple_val, fb_rvalue);
6634 ret = MIN (ret, tret);
6636 /* Handle OMP_FOR_INCR. */
6637 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6638 switch (TREE_CODE (t))
6640 case PREINCREMENT_EXPR:
6641 case POSTINCREMENT_EXPR:
6642 t = build_int_cst (TREE_TYPE (decl), 1);
6643 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6644 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6645 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6646 break;
6648 case PREDECREMENT_EXPR:
6649 case POSTDECREMENT_EXPR:
6650 t = build_int_cst (TREE_TYPE (decl), -1);
6651 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6652 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6653 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6654 break;
6656 case MODIFY_EXPR:
6657 gcc_assert (TREE_OPERAND (t, 0) == decl);
6658 TREE_OPERAND (t, 0) = var;
6660 t = TREE_OPERAND (t, 1);
6661 switch (TREE_CODE (t))
6663 case PLUS_EXPR:
6664 if (TREE_OPERAND (t, 1) == decl)
6666 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6667 TREE_OPERAND (t, 0) = var;
6668 break;
6671 /* Fallthru. */
6672 case MINUS_EXPR:
6673 case POINTER_PLUS_EXPR:
6674 gcc_assert (TREE_OPERAND (t, 0) == decl);
6675 TREE_OPERAND (t, 0) = var;
6676 break;
6677 default:
6678 gcc_unreachable ();
6681 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6682 is_gimple_val, fb_rvalue);
6683 ret = MIN (ret, tret);
6684 break;
6686 default:
6687 gcc_unreachable ();
6690 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6692 tree c;
6693 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6694 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6695 && OMP_CLAUSE_DECL (c) == decl
6696 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6698 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6699 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6700 gcc_assert (TREE_OPERAND (t, 0) == var);
6701 t = TREE_OPERAND (t, 1);
6702 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6703 || TREE_CODE (t) == MINUS_EXPR
6704 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6705 gcc_assert (TREE_OPERAND (t, 0) == var);
6706 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6707 TREE_OPERAND (t, 1));
6708 gimplify_assign (decl, t,
6709 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6714 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6716 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6718 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6719 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6720 for_pre_body);
6722 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6724 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6725 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6726 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6727 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6728 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6729 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6730 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6731 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6734 gimplify_seq_add_stmt (pre_p, gfor);
6735 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6738 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6739 In particular, OMP_SECTIONS and OMP_SINGLE. */
6741 static void
6742 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6744 tree expr = *expr_p;
6745 gimple stmt;
6746 gimple_seq body = NULL;
6748 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6749 gimplify_and_add (OMP_BODY (expr), &body);
6750 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6752 if (TREE_CODE (expr) == OMP_SECTIONS)
6753 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6754 else if (TREE_CODE (expr) == OMP_SINGLE)
6755 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6756 else
6757 gcc_unreachable ();
6759 gimplify_seq_add_stmt (pre_p, stmt);
6762 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6763 stabilized the lhs of the atomic operation as *ADDR. Return true if
6764 EXPR is this stabilized form. */
6766 static bool
6767 goa_lhs_expr_p (tree expr, tree addr)
6769 /* Also include casts to other type variants. The C front end is fond
6770 of adding these for e.g. volatile variables. This is like
6771 STRIP_TYPE_NOPS but includes the main variant lookup. */
6772 STRIP_USELESS_TYPE_CONVERSION (expr);
6774 if (TREE_CODE (expr) == INDIRECT_REF)
6776 expr = TREE_OPERAND (expr, 0);
6777 while (expr != addr
6778 && (CONVERT_EXPR_P (expr)
6779 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6780 && TREE_CODE (expr) == TREE_CODE (addr)
6781 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6783 expr = TREE_OPERAND (expr, 0);
6784 addr = TREE_OPERAND (addr, 0);
6786 if (expr == addr)
6787 return true;
6788 return (TREE_CODE (addr) == ADDR_EXPR
6789 && TREE_CODE (expr) == ADDR_EXPR
6790 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6792 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6793 return true;
6794 return false;
6797 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6798 expression does not involve the lhs, evaluate it into a temporary.
6799 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6800 or -1 if an error was encountered. */
6802 static int
6803 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6804 tree lhs_var)
6806 tree expr = *expr_p;
6807 int saw_lhs;
6809 if (goa_lhs_expr_p (expr, lhs_addr))
6811 *expr_p = lhs_var;
6812 return 1;
6814 if (is_gimple_val (expr))
6815 return 0;
6817 saw_lhs = 0;
6818 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6820 case tcc_binary:
6821 case tcc_comparison:
6822 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6823 lhs_var);
6824 case tcc_unary:
6825 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6826 lhs_var);
6827 break;
6828 case tcc_expression:
6829 switch (TREE_CODE (expr))
6831 case TRUTH_ANDIF_EXPR:
6832 case TRUTH_ORIF_EXPR:
6833 case TRUTH_AND_EXPR:
6834 case TRUTH_OR_EXPR:
6835 case TRUTH_XOR_EXPR:
6836 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6837 lhs_addr, lhs_var);
6838 case TRUTH_NOT_EXPR:
6839 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6840 lhs_addr, lhs_var);
6841 break;
6842 case COMPOUND_EXPR:
6843 /* Break out any preevaluations from cp_build_modify_expr. */
6844 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6845 expr = TREE_OPERAND (expr, 1))
6846 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6847 *expr_p = expr;
6848 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6849 default:
6850 break;
6852 break;
6853 default:
6854 break;
6857 if (saw_lhs == 0)
6859 enum gimplify_status gs;
6860 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6861 if (gs != GS_ALL_DONE)
6862 saw_lhs = -1;
6865 return saw_lhs;
6868 /* Gimplify an OMP_ATOMIC statement. */
6870 static enum gimplify_status
6871 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6873 tree addr = TREE_OPERAND (*expr_p, 0);
6874 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6875 ? NULL : TREE_OPERAND (*expr_p, 1);
6876 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6877 tree tmp_load;
6878 gimple loadstmt, storestmt;
6880 tmp_load = create_tmp_reg (type, NULL);
6881 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6882 return GS_ERROR;
6884 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6885 != GS_ALL_DONE)
6886 return GS_ERROR;
6888 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6889 gimplify_seq_add_stmt (pre_p, loadstmt);
6890 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6891 != GS_ALL_DONE)
6892 return GS_ERROR;
6894 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6895 rhs = tmp_load;
6896 storestmt = gimple_build_omp_atomic_store (rhs);
6897 gimplify_seq_add_stmt (pre_p, storestmt);
6898 switch (TREE_CODE (*expr_p))
6900 case OMP_ATOMIC_READ:
6901 case OMP_ATOMIC_CAPTURE_OLD:
6902 *expr_p = tmp_load;
6903 gimple_omp_atomic_set_need_value (loadstmt);
6904 break;
6905 case OMP_ATOMIC_CAPTURE_NEW:
6906 *expr_p = rhs;
6907 gimple_omp_atomic_set_need_value (storestmt);
6908 break;
6909 default:
6910 *expr_p = NULL;
6911 break;
6914 return GS_ALL_DONE;
6917 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6918 body, and adding some EH bits. */
6920 static enum gimplify_status
6921 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6923 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6924 gimple g;
6925 gimple_seq body = NULL;
6926 struct gimplify_ctx gctx;
6927 int subcode = 0;
6929 /* Wrap the transaction body in a BIND_EXPR so we have a context
6930 where to put decls for OpenMP. */
6931 if (TREE_CODE (tbody) != BIND_EXPR)
6933 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6934 TREE_SIDE_EFFECTS (bind) = 1;
6935 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6936 TRANSACTION_EXPR_BODY (expr) = bind;
6939 push_gimplify_context (&gctx);
6940 temp = voidify_wrapper_expr (*expr_p, NULL);
6942 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6943 pop_gimplify_context (g);
6945 g = gimple_build_transaction (body, NULL);
6946 if (TRANSACTION_EXPR_OUTER (expr))
6947 subcode = GTMA_IS_OUTER;
6948 else if (TRANSACTION_EXPR_RELAXED (expr))
6949 subcode = GTMA_IS_RELAXED;
6950 gimple_transaction_set_subcode (g, subcode);
6952 gimplify_seq_add_stmt (pre_p, g);
6954 if (temp)
6956 *expr_p = temp;
6957 return GS_OK;
6960 *expr_p = NULL_TREE;
6961 return GS_ALL_DONE;
6964 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6965 expression produces a value to be used as an operand inside a GIMPLE
6966 statement, the value will be stored back in *EXPR_P. This value will
6967 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6968 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6969 emitted in PRE_P and POST_P.
6971 Additionally, this process may overwrite parts of the input
6972 expression during gimplification. Ideally, it should be
6973 possible to do non-destructive gimplification.
6975 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6976 the expression needs to evaluate to a value to be used as
6977 an operand in a GIMPLE statement, this value will be stored in
6978 *EXPR_P on exit. This happens when the caller specifies one
6979 of fb_lvalue or fb_rvalue fallback flags.
6981 PRE_P will contain the sequence of GIMPLE statements corresponding
6982 to the evaluation of EXPR and all the side-effects that must
6983 be executed before the main expression. On exit, the last
6984 statement of PRE_P is the core statement being gimplified. For
6985 instance, when gimplifying 'if (++a)' the last statement in
6986 PRE_P will be 'if (t.1)' where t.1 is the result of
6987 pre-incrementing 'a'.
6989 POST_P will contain the sequence of GIMPLE statements corresponding
6990 to the evaluation of all the side-effects that must be executed
6991 after the main expression. If this is NULL, the post
6992 side-effects are stored at the end of PRE_P.
6994 The reason why the output is split in two is to handle post
6995 side-effects explicitly. In some cases, an expression may have
6996 inner and outer post side-effects which need to be emitted in
6997 an order different from the one given by the recursive
6998 traversal. For instance, for the expression (*p--)++ the post
6999 side-effects of '--' must actually occur *after* the post
7000 side-effects of '++'. However, gimplification will first visit
7001 the inner expression, so if a separate POST sequence was not
7002 used, the resulting sequence would be:
7004 1 t.1 = *p
7005 2 p = p - 1
7006 3 t.2 = t.1 + 1
7007 4 *p = t.2
7009 However, the post-decrement operation in line #2 must not be
7010 evaluated until after the store to *p at line #4, so the
7011 correct sequence should be:
7013 1 t.1 = *p
7014 2 t.2 = t.1 + 1
7015 3 *p = t.2
7016 4 p = p - 1
7018 So, by specifying a separate post queue, it is possible
7019 to emit the post side-effects in the correct order.
7020 If POST_P is NULL, an internal queue will be used. Before
7021 returning to the caller, the sequence POST_P is appended to
7022 the main output sequence PRE_P.
7024 GIMPLE_TEST_F points to a function that takes a tree T and
7025 returns nonzero if T is in the GIMPLE form requested by the
7026 caller. The GIMPLE predicates are in gimple.c.
7028 FALLBACK tells the function what sort of a temporary we want if
7029 gimplification cannot produce an expression that complies with
7030 GIMPLE_TEST_F.
7032 fb_none means that no temporary should be generated
7033 fb_rvalue means that an rvalue is OK to generate
7034 fb_lvalue means that an lvalue is OK to generate
7035 fb_either means that either is OK, but an lvalue is preferable.
7036 fb_mayfail means that gimplification may fail (in which case
7037 GS_ERROR will be returned)
7039 The return value is either GS_ERROR or GS_ALL_DONE, since this
7040 function iterates until EXPR is completely gimplified or an error
7041 occurs. */
7043 enum gimplify_status
7044 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7045 bool (*gimple_test_f) (tree), fallback_t fallback)
7047 tree tmp;
7048 gimple_seq internal_pre = NULL;
7049 gimple_seq internal_post = NULL;
7050 tree save_expr;
7051 bool is_statement;
7052 location_t saved_location;
7053 enum gimplify_status ret;
7054 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7056 save_expr = *expr_p;
7057 if (save_expr == NULL_TREE)
7058 return GS_ALL_DONE;
7060 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7061 is_statement = gimple_test_f == is_gimple_stmt;
7062 if (is_statement)
7063 gcc_assert (pre_p);
7065 /* Consistency checks. */
7066 if (gimple_test_f == is_gimple_reg)
7067 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7068 else if (gimple_test_f == is_gimple_val
7069 || gimple_test_f == is_gimple_call_addr
7070 || gimple_test_f == is_gimple_condexpr
7071 || gimple_test_f == is_gimple_mem_rhs
7072 || gimple_test_f == is_gimple_mem_rhs_or_call
7073 || gimple_test_f == is_gimple_reg_rhs
7074 || gimple_test_f == is_gimple_reg_rhs_or_call
7075 || gimple_test_f == is_gimple_asm_val
7076 || gimple_test_f == is_gimple_mem_ref_addr)
7077 gcc_assert (fallback & fb_rvalue);
7078 else if (gimple_test_f == is_gimple_min_lval
7079 || gimple_test_f == is_gimple_lvalue)
7080 gcc_assert (fallback & fb_lvalue);
7081 else if (gimple_test_f == is_gimple_addressable)
7082 gcc_assert (fallback & fb_either);
7083 else if (gimple_test_f == is_gimple_stmt)
7084 gcc_assert (fallback == fb_none);
7085 else
7087 /* We should have recognized the GIMPLE_TEST_F predicate to
7088 know what kind of fallback to use in case a temporary is
7089 needed to hold the value or address of *EXPR_P. */
7090 gcc_unreachable ();
7093 /* We used to check the predicate here and return immediately if it
7094 succeeds. This is wrong; the design is for gimplification to be
7095 idempotent, and for the predicates to only test for valid forms, not
7096 whether they are fully simplified. */
7097 if (pre_p == NULL)
7098 pre_p = &internal_pre;
7100 if (post_p == NULL)
7101 post_p = &internal_post;
7103 /* Remember the last statements added to PRE_P and POST_P. Every
7104 new statement added by the gimplification helpers needs to be
7105 annotated with location information. To centralize the
7106 responsibility, we remember the last statement that had been
7107 added to both queues before gimplifying *EXPR_P. If
7108 gimplification produces new statements in PRE_P and POST_P, those
7109 statements will be annotated with the same location information
7110 as *EXPR_P. */
7111 pre_last_gsi = gsi_last (*pre_p);
7112 post_last_gsi = gsi_last (*post_p);
7114 saved_location = input_location;
7115 if (save_expr != error_mark_node
7116 && EXPR_HAS_LOCATION (*expr_p))
7117 input_location = EXPR_LOCATION (*expr_p);
7119 /* Loop over the specific gimplifiers until the toplevel node
7120 remains the same. */
7123 /* Strip away as many useless type conversions as possible
7124 at the toplevel. */
7125 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7127 /* Remember the expr. */
7128 save_expr = *expr_p;
7130 /* Die, die, die, my darling. */
7131 if (save_expr == error_mark_node
7132 || (TREE_TYPE (save_expr)
7133 && TREE_TYPE (save_expr) == error_mark_node))
7135 ret = GS_ERROR;
7136 break;
7139 /* Do any language-specific gimplification. */
7140 ret = ((enum gimplify_status)
7141 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7142 if (ret == GS_OK)
7144 if (*expr_p == NULL_TREE)
7145 break;
7146 if (*expr_p != save_expr)
7147 continue;
7149 else if (ret != GS_UNHANDLED)
7150 break;
7152 /* Make sure that all the cases set 'ret' appropriately. */
7153 ret = GS_UNHANDLED;
7154 switch (TREE_CODE (*expr_p))
7156 /* First deal with the special cases. */
7158 case POSTINCREMENT_EXPR:
7159 case POSTDECREMENT_EXPR:
7160 case PREINCREMENT_EXPR:
7161 case PREDECREMENT_EXPR:
7162 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7163 fallback != fb_none,
7164 TREE_TYPE (*expr_p));
7165 break;
7167 case ARRAY_REF:
7168 case ARRAY_RANGE_REF:
7169 case REALPART_EXPR:
7170 case IMAGPART_EXPR:
7171 case COMPONENT_REF:
7172 case VIEW_CONVERT_EXPR:
7173 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7174 fallback ? fallback : fb_rvalue);
7175 break;
7177 case COND_EXPR:
7178 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7180 /* C99 code may assign to an array in a structure value of a
7181 conditional expression, and this has undefined behavior
7182 only on execution, so create a temporary if an lvalue is
7183 required. */
7184 if (fallback == fb_lvalue)
7186 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7187 mark_addressable (*expr_p);
7188 ret = GS_OK;
7190 break;
7192 case CALL_EXPR:
7193 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7195 /* C99 code may assign to an array in a structure returned
7196 from a function, and this has undefined behavior only on
7197 execution, so create a temporary if an lvalue is
7198 required. */
7199 if (fallback == fb_lvalue)
7201 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7202 mark_addressable (*expr_p);
7203 ret = GS_OK;
7205 break;
7207 case TREE_LIST:
7208 gcc_unreachable ();
7210 case COMPOUND_EXPR:
7211 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7212 break;
7214 case COMPOUND_LITERAL_EXPR:
7215 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7216 gimple_test_f, fallback);
7217 break;
7219 case MODIFY_EXPR:
7220 case INIT_EXPR:
7221 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7222 fallback != fb_none);
7223 break;
7225 case TRUTH_ANDIF_EXPR:
7226 case TRUTH_ORIF_EXPR:
7228 /* Preserve the original type of the expression and the
7229 source location of the outer expression. */
7230 tree org_type = TREE_TYPE (*expr_p);
7231 *expr_p = gimple_boolify (*expr_p);
7232 *expr_p = build3_loc (input_location, COND_EXPR,
7233 org_type, *expr_p,
7234 fold_convert_loc
7235 (input_location,
7236 org_type, boolean_true_node),
7237 fold_convert_loc
7238 (input_location,
7239 org_type, boolean_false_node));
7240 ret = GS_OK;
7241 break;
7244 case TRUTH_NOT_EXPR:
7246 tree type = TREE_TYPE (*expr_p);
7247 /* The parsers are careful to generate TRUTH_NOT_EXPR
7248 only with operands that are always zero or one.
7249 We do not fold here but handle the only interesting case
7250 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7251 *expr_p = gimple_boolify (*expr_p);
7252 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7253 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7254 TREE_TYPE (*expr_p),
7255 TREE_OPERAND (*expr_p, 0));
7256 else
7257 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7258 TREE_TYPE (*expr_p),
7259 TREE_OPERAND (*expr_p, 0),
7260 build_int_cst (TREE_TYPE (*expr_p), 1));
7261 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7262 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7263 ret = GS_OK;
7264 break;
7267 case ADDR_EXPR:
7268 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7269 break;
7271 case VA_ARG_EXPR:
7272 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7273 break;
7275 CASE_CONVERT:
7276 if (IS_EMPTY_STMT (*expr_p))
7278 ret = GS_ALL_DONE;
7279 break;
7282 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7283 || fallback == fb_none)
7285 /* Just strip a conversion to void (or in void context) and
7286 try again. */
7287 *expr_p = TREE_OPERAND (*expr_p, 0);
7288 ret = GS_OK;
7289 break;
7292 ret = gimplify_conversion (expr_p);
7293 if (ret == GS_ERROR)
7294 break;
7295 if (*expr_p != save_expr)
7296 break;
7297 /* FALLTHRU */
7299 case FIX_TRUNC_EXPR:
7300 /* unary_expr: ... | '(' cast ')' val | ... */
7301 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7302 is_gimple_val, fb_rvalue);
7303 recalculate_side_effects (*expr_p);
7304 break;
7306 case INDIRECT_REF:
7308 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7309 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7310 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7312 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7313 if (*expr_p != save_expr)
7315 ret = GS_OK;
7316 break;
7319 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7320 is_gimple_reg, fb_rvalue);
7321 if (ret == GS_ERROR)
7322 break;
7324 recalculate_side_effects (*expr_p);
7325 *expr_p = fold_build2_loc (input_location, MEM_REF,
7326 TREE_TYPE (*expr_p),
7327 TREE_OPERAND (*expr_p, 0),
7328 build_int_cst (saved_ptr_type, 0));
7329 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7330 TREE_THIS_NOTRAP (*expr_p) = notrap;
7331 ret = GS_OK;
7332 break;
7335 /* We arrive here through the various re-gimplifcation paths. */
7336 case MEM_REF:
7337 /* First try re-folding the whole thing. */
7338 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7339 TREE_OPERAND (*expr_p, 0),
7340 TREE_OPERAND (*expr_p, 1));
7341 if (tmp)
7343 *expr_p = tmp;
7344 recalculate_side_effects (*expr_p);
7345 ret = GS_OK;
7346 break;
7348 /* Avoid re-gimplifying the address operand if it is already
7349 in suitable form. Re-gimplifying would mark the address
7350 operand addressable. Always gimplify when not in SSA form
7351 as we still may have to gimplify decls with value-exprs. */
7352 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7353 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7355 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7356 is_gimple_mem_ref_addr, fb_rvalue);
7357 if (ret == GS_ERROR)
7358 break;
7360 recalculate_side_effects (*expr_p);
7361 ret = GS_ALL_DONE;
7362 break;
7364 /* Constants need not be gimplified. */
7365 case INTEGER_CST:
7366 case REAL_CST:
7367 case FIXED_CST:
7368 case STRING_CST:
7369 case COMPLEX_CST:
7370 case VECTOR_CST:
7371 ret = GS_ALL_DONE;
7372 break;
7374 case CONST_DECL:
7375 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7376 CONST_DECL node. Otherwise the decl is replaceable by its
7377 value. */
7378 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7379 if (fallback & fb_lvalue)
7380 ret = GS_ALL_DONE;
7381 else
7383 *expr_p = DECL_INITIAL (*expr_p);
7384 ret = GS_OK;
7386 break;
7388 case DECL_EXPR:
7389 ret = gimplify_decl_expr (expr_p, pre_p);
7390 break;
7392 case BIND_EXPR:
7393 ret = gimplify_bind_expr (expr_p, pre_p);
7394 break;
7396 case LOOP_EXPR:
7397 ret = gimplify_loop_expr (expr_p, pre_p);
7398 break;
7400 case SWITCH_EXPR:
7401 ret = gimplify_switch_expr (expr_p, pre_p);
7402 break;
7404 case EXIT_EXPR:
7405 ret = gimplify_exit_expr (expr_p);
7406 break;
7408 case GOTO_EXPR:
7409 /* If the target is not LABEL, then it is a computed jump
7410 and the target needs to be gimplified. */
7411 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7413 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7414 NULL, is_gimple_val, fb_rvalue);
7415 if (ret == GS_ERROR)
7416 break;
7418 gimplify_seq_add_stmt (pre_p,
7419 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7420 ret = GS_ALL_DONE;
7421 break;
7423 case PREDICT_EXPR:
7424 gimplify_seq_add_stmt (pre_p,
7425 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7426 PREDICT_EXPR_OUTCOME (*expr_p)));
7427 ret = GS_ALL_DONE;
7428 break;
7430 case LABEL_EXPR:
7431 ret = GS_ALL_DONE;
7432 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7433 == current_function_decl);
7434 gimplify_seq_add_stmt (pre_p,
7435 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7436 break;
7438 case CASE_LABEL_EXPR:
7439 ret = gimplify_case_label_expr (expr_p, pre_p);
7440 break;
7442 case RETURN_EXPR:
7443 ret = gimplify_return_expr (*expr_p, pre_p);
7444 break;
7446 case CONSTRUCTOR:
7447 /* Don't reduce this in place; let gimplify_init_constructor work its
7448 magic. Buf if we're just elaborating this for side effects, just
7449 gimplify any element that has side-effects. */
7450 if (fallback == fb_none)
7452 unsigned HOST_WIDE_INT ix;
7453 tree val;
7454 tree temp = NULL_TREE;
7455 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7456 if (TREE_SIDE_EFFECTS (val))
7457 append_to_statement_list (val, &temp);
7459 *expr_p = temp;
7460 ret = temp ? GS_OK : GS_ALL_DONE;
7462 /* C99 code may assign to an array in a constructed
7463 structure or union, and this has undefined behavior only
7464 on execution, so create a temporary if an lvalue is
7465 required. */
7466 else if (fallback == fb_lvalue)
7468 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7469 mark_addressable (*expr_p);
7470 ret = GS_OK;
7472 else
7473 ret = GS_ALL_DONE;
7474 break;
7476 /* The following are special cases that are not handled by the
7477 original GIMPLE grammar. */
7479 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7480 eliminated. */
7481 case SAVE_EXPR:
7482 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7483 break;
7485 case BIT_FIELD_REF:
7486 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7487 post_p, is_gimple_lvalue, fb_either);
7488 recalculate_side_effects (*expr_p);
7489 break;
7491 case TARGET_MEM_REF:
7493 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7495 if (TMR_BASE (*expr_p))
7496 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7497 post_p, is_gimple_mem_ref_addr, fb_either);
7498 if (TMR_INDEX (*expr_p))
7499 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7500 post_p, is_gimple_val, fb_rvalue);
7501 if (TMR_INDEX2 (*expr_p))
7502 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7503 post_p, is_gimple_val, fb_rvalue);
7504 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7505 ret = MIN (r0, r1);
7507 break;
7509 case NON_LVALUE_EXPR:
7510 /* This should have been stripped above. */
7511 gcc_unreachable ();
7513 case ASM_EXPR:
7514 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7515 break;
7517 case TRY_FINALLY_EXPR:
7518 case TRY_CATCH_EXPR:
7520 gimple_seq eval, cleanup;
7521 gimple try_;
7523 /* Calls to destructors are generated automatically in FINALLY/CATCH
7524 block. They should have location as UNKNOWN_LOCATION. However,
7525 gimplify_call_expr will reset these call stmts to input_location
7526 if it finds stmt's location is unknown. To prevent resetting for
7527 destructors, we set the input_location to unknown.
7528 Note that this only affects the destructor calls in FINALLY/CATCH
7529 block, and will automatically reset to its original value by the
7530 end of gimplify_expr. */
7531 input_location = UNKNOWN_LOCATION;
7532 eval = cleanup = NULL;
7533 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7534 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7535 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7536 if (gimple_seq_empty_p (cleanup))
7538 gimple_seq_add_seq (pre_p, eval);
7539 ret = GS_ALL_DONE;
7540 break;
7542 try_ = gimple_build_try (eval, cleanup,
7543 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7544 ? GIMPLE_TRY_FINALLY
7545 : GIMPLE_TRY_CATCH);
7546 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
7547 gimple_set_location (try_, saved_location);
7548 else
7549 gimple_set_location (try_, EXPR_LOCATION (save_expr));
7550 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7551 gimple_try_set_catch_is_cleanup (try_,
7552 TRY_CATCH_IS_CLEANUP (*expr_p));
7553 gimplify_seq_add_stmt (pre_p, try_);
7554 ret = GS_ALL_DONE;
7555 break;
7558 case CLEANUP_POINT_EXPR:
7559 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7560 break;
7562 case TARGET_EXPR:
7563 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7564 break;
7566 case CATCH_EXPR:
7568 gimple c;
7569 gimple_seq handler = NULL;
7570 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7571 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7572 gimplify_seq_add_stmt (pre_p, c);
7573 ret = GS_ALL_DONE;
7574 break;
7577 case EH_FILTER_EXPR:
7579 gimple ehf;
7580 gimple_seq failure = NULL;
7582 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7583 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7584 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7585 gimplify_seq_add_stmt (pre_p, ehf);
7586 ret = GS_ALL_DONE;
7587 break;
7590 case OBJ_TYPE_REF:
7592 enum gimplify_status r0, r1;
7593 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7594 post_p, is_gimple_val, fb_rvalue);
7595 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7596 post_p, is_gimple_val, fb_rvalue);
7597 TREE_SIDE_EFFECTS (*expr_p) = 0;
7598 ret = MIN (r0, r1);
7600 break;
7602 case LABEL_DECL:
7603 /* We get here when taking the address of a label. We mark
7604 the label as "forced"; meaning it can never be removed and
7605 it is a potential target for any computed goto. */
7606 FORCED_LABEL (*expr_p) = 1;
7607 ret = GS_ALL_DONE;
7608 break;
7610 case STATEMENT_LIST:
7611 ret = gimplify_statement_list (expr_p, pre_p);
7612 break;
7614 case WITH_SIZE_EXPR:
7616 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7617 post_p == &internal_post ? NULL : post_p,
7618 gimple_test_f, fallback);
7619 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7620 is_gimple_val, fb_rvalue);
7621 ret = GS_ALL_DONE;
7623 break;
7625 case VAR_DECL:
7626 case PARM_DECL:
7627 ret = gimplify_var_or_parm_decl (expr_p);
7628 break;
7630 case RESULT_DECL:
7631 /* When within an OpenMP context, notice uses of variables. */
7632 if (gimplify_omp_ctxp)
7633 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7634 ret = GS_ALL_DONE;
7635 break;
7637 case SSA_NAME:
7638 /* Allow callbacks into the gimplifier during optimization. */
7639 ret = GS_ALL_DONE;
7640 break;
7642 case OMP_PARALLEL:
7643 gimplify_omp_parallel (expr_p, pre_p);
7644 ret = GS_ALL_DONE;
7645 break;
7647 case OMP_TASK:
7648 gimplify_omp_task (expr_p, pre_p);
7649 ret = GS_ALL_DONE;
7650 break;
7652 case OMP_FOR:
7653 ret = gimplify_omp_for (expr_p, pre_p);
7654 break;
7656 case OMP_SECTIONS:
7657 case OMP_SINGLE:
7658 gimplify_omp_workshare (expr_p, pre_p);
7659 ret = GS_ALL_DONE;
7660 break;
7662 case OMP_SECTION:
7663 case OMP_MASTER:
7664 case OMP_ORDERED:
7665 case OMP_CRITICAL:
7667 gimple_seq body = NULL;
7668 gimple g;
7670 gimplify_and_add (OMP_BODY (*expr_p), &body);
7671 switch (TREE_CODE (*expr_p))
7673 case OMP_SECTION:
7674 g = gimple_build_omp_section (body);
7675 break;
7676 case OMP_MASTER:
7677 g = gimple_build_omp_master (body);
7678 break;
7679 case OMP_ORDERED:
7680 g = gimple_build_omp_ordered (body);
7681 break;
7682 case OMP_CRITICAL:
7683 g = gimple_build_omp_critical (body,
7684 OMP_CRITICAL_NAME (*expr_p));
7685 break;
7686 default:
7687 gcc_unreachable ();
7689 gimplify_seq_add_stmt (pre_p, g);
7690 ret = GS_ALL_DONE;
7691 break;
7694 case OMP_ATOMIC:
7695 case OMP_ATOMIC_READ:
7696 case OMP_ATOMIC_CAPTURE_OLD:
7697 case OMP_ATOMIC_CAPTURE_NEW:
7698 ret = gimplify_omp_atomic (expr_p, pre_p);
7699 break;
7701 case TRANSACTION_EXPR:
7702 ret = gimplify_transaction (expr_p, pre_p);
7703 break;
7705 case TRUTH_AND_EXPR:
7706 case TRUTH_OR_EXPR:
7707 case TRUTH_XOR_EXPR:
7709 tree orig_type = TREE_TYPE (*expr_p);
7710 tree new_type, xop0, xop1;
7711 *expr_p = gimple_boolify (*expr_p);
7712 new_type = TREE_TYPE (*expr_p);
7713 if (!useless_type_conversion_p (orig_type, new_type))
7715 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7716 ret = GS_OK;
7717 break;
7720 /* Boolified binary truth expressions are semantically equivalent
7721 to bitwise binary expressions. Canonicalize them to the
7722 bitwise variant. */
7723 switch (TREE_CODE (*expr_p))
7725 case TRUTH_AND_EXPR:
7726 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7727 break;
7728 case TRUTH_OR_EXPR:
7729 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7730 break;
7731 case TRUTH_XOR_EXPR:
7732 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7733 break;
7734 default:
7735 break;
7737 /* Now make sure that operands have compatible type to
7738 expression's new_type. */
7739 xop0 = TREE_OPERAND (*expr_p, 0);
7740 xop1 = TREE_OPERAND (*expr_p, 1);
7741 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7742 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7743 new_type,
7744 xop0);
7745 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7746 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7747 new_type,
7748 xop1);
7749 /* Continue classified as tcc_binary. */
7750 goto expr_2;
7753 case FMA_EXPR:
7754 case VEC_COND_EXPR:
7755 case VEC_PERM_EXPR:
7756 /* Classified as tcc_expression. */
7757 goto expr_3;
7759 case POINTER_PLUS_EXPR:
7761 enum gimplify_status r0, r1;
7762 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7763 post_p, is_gimple_val, fb_rvalue);
7764 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7765 post_p, is_gimple_val, fb_rvalue);
7766 recalculate_side_effects (*expr_p);
7767 ret = MIN (r0, r1);
7768 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7769 after gimplifying operands - this is similar to how
7770 it would be folding all gimplified stmts on creation
7771 to have them canonicalized, which is what we eventually
7772 should do anyway. */
7773 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7774 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7776 *expr_p = build_fold_addr_expr_with_type_loc
7777 (input_location,
7778 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7779 TREE_OPERAND (*expr_p, 0),
7780 fold_convert (ptr_type_node,
7781 TREE_OPERAND (*expr_p, 1))),
7782 TREE_TYPE (*expr_p));
7783 ret = MIN (ret, GS_OK);
7785 break;
7788 default:
7789 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7791 case tcc_comparison:
7792 /* Handle comparison of objects of non scalar mode aggregates
7793 with a call to memcmp. It would be nice to only have to do
7794 this for variable-sized objects, but then we'd have to allow
7795 the same nest of reference nodes we allow for MODIFY_EXPR and
7796 that's too complex.
7798 Compare scalar mode aggregates as scalar mode values. Using
7799 memcmp for them would be very inefficient at best, and is
7800 plain wrong if bitfields are involved. */
7802 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7804 /* Vector comparisons need no boolification. */
7805 if (TREE_CODE (type) == VECTOR_TYPE)
7806 goto expr_2;
7807 else if (!AGGREGATE_TYPE_P (type))
7809 tree org_type = TREE_TYPE (*expr_p);
7810 *expr_p = gimple_boolify (*expr_p);
7811 if (!useless_type_conversion_p (org_type,
7812 TREE_TYPE (*expr_p)))
7814 *expr_p = fold_convert_loc (input_location,
7815 org_type, *expr_p);
7816 ret = GS_OK;
7818 else
7819 goto expr_2;
7821 else if (TYPE_MODE (type) != BLKmode)
7822 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7823 else
7824 ret = gimplify_variable_sized_compare (expr_p);
7826 break;
7829 /* If *EXPR_P does not need to be special-cased, handle it
7830 according to its class. */
7831 case tcc_unary:
7832 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7833 post_p, is_gimple_val, fb_rvalue);
7834 break;
7836 case tcc_binary:
7837 expr_2:
7839 enum gimplify_status r0, r1;
7841 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7842 post_p, is_gimple_val, fb_rvalue);
7843 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7844 post_p, is_gimple_val, fb_rvalue);
7846 ret = MIN (r0, r1);
7847 break;
7850 expr_3:
7852 enum gimplify_status r0, r1, r2;
7854 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7855 post_p, is_gimple_val, fb_rvalue);
7856 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7857 post_p, is_gimple_val, fb_rvalue);
7858 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7859 post_p, is_gimple_val, fb_rvalue);
7861 ret = MIN (MIN (r0, r1), r2);
7862 break;
7865 case tcc_declaration:
7866 case tcc_constant:
7867 ret = GS_ALL_DONE;
7868 goto dont_recalculate;
7870 default:
7871 gcc_unreachable ();
7874 recalculate_side_effects (*expr_p);
7876 dont_recalculate:
7877 break;
7880 gcc_assert (*expr_p || ret != GS_OK);
7882 while (ret == GS_OK);
7884 /* If we encountered an error_mark somewhere nested inside, either
7885 stub out the statement or propagate the error back out. */
7886 if (ret == GS_ERROR)
7888 if (is_statement)
7889 *expr_p = NULL;
7890 goto out;
7893 /* This was only valid as a return value from the langhook, which
7894 we handled. Make sure it doesn't escape from any other context. */
7895 gcc_assert (ret != GS_UNHANDLED);
7897 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7899 /* We aren't looking for a value, and we don't have a valid
7900 statement. If it doesn't have side-effects, throw it away. */
7901 if (!TREE_SIDE_EFFECTS (*expr_p))
7902 *expr_p = NULL;
7903 else if (!TREE_THIS_VOLATILE (*expr_p))
7905 /* This is probably a _REF that contains something nested that
7906 has side effects. Recurse through the operands to find it. */
7907 enum tree_code code = TREE_CODE (*expr_p);
7909 switch (code)
7911 case COMPONENT_REF:
7912 case REALPART_EXPR:
7913 case IMAGPART_EXPR:
7914 case VIEW_CONVERT_EXPR:
7915 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7916 gimple_test_f, fallback);
7917 break;
7919 case ARRAY_REF:
7920 case ARRAY_RANGE_REF:
7921 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7922 gimple_test_f, fallback);
7923 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7924 gimple_test_f, fallback);
7925 break;
7927 default:
7928 /* Anything else with side-effects must be converted to
7929 a valid statement before we get here. */
7930 gcc_unreachable ();
7933 *expr_p = NULL;
7935 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7936 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7938 /* Historically, the compiler has treated a bare reference
7939 to a non-BLKmode volatile lvalue as forcing a load. */
7940 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7942 /* Normally, we do not want to create a temporary for a
7943 TREE_ADDRESSABLE type because such a type should not be
7944 copied by bitwise-assignment. However, we make an
7945 exception here, as all we are doing here is ensuring that
7946 we read the bytes that make up the type. We use
7947 create_tmp_var_raw because create_tmp_var will abort when
7948 given a TREE_ADDRESSABLE type. */
7949 tree tmp = create_tmp_var_raw (type, "vol");
7950 gimple_add_tmp_var (tmp);
7951 gimplify_assign (tmp, *expr_p, pre_p);
7952 *expr_p = NULL;
7954 else
7955 /* We can't do anything useful with a volatile reference to
7956 an incomplete type, so just throw it away. Likewise for
7957 a BLKmode type, since any implicit inner load should
7958 already have been turned into an explicit one by the
7959 gimplification process. */
7960 *expr_p = NULL;
7963 /* If we are gimplifying at the statement level, we're done. Tack
7964 everything together and return. */
7965 if (fallback == fb_none || is_statement)
7967 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7968 it out for GC to reclaim it. */
7969 *expr_p = NULL_TREE;
7971 if (!gimple_seq_empty_p (internal_pre)
7972 || !gimple_seq_empty_p (internal_post))
7974 gimplify_seq_add_seq (&internal_pre, internal_post);
7975 gimplify_seq_add_seq (pre_p, internal_pre);
7978 /* The result of gimplifying *EXPR_P is going to be the last few
7979 statements in *PRE_P and *POST_P. Add location information
7980 to all the statements that were added by the gimplification
7981 helpers. */
7982 if (!gimple_seq_empty_p (*pre_p))
7983 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7985 if (!gimple_seq_empty_p (*post_p))
7986 annotate_all_with_location_after (*post_p, post_last_gsi,
7987 input_location);
7989 goto out;
7992 #ifdef ENABLE_GIMPLE_CHECKING
7993 if (*expr_p)
7995 enum tree_code code = TREE_CODE (*expr_p);
7996 /* These expressions should already be in gimple IR form. */
7997 gcc_assert (code != MODIFY_EXPR
7998 && code != ASM_EXPR
7999 && code != BIND_EXPR
8000 && code != CATCH_EXPR
8001 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8002 && code != EH_FILTER_EXPR
8003 && code != GOTO_EXPR
8004 && code != LABEL_EXPR
8005 && code != LOOP_EXPR
8006 && code != SWITCH_EXPR
8007 && code != TRY_FINALLY_EXPR
8008 && code != OMP_CRITICAL
8009 && code != OMP_FOR
8010 && code != OMP_MASTER
8011 && code != OMP_ORDERED
8012 && code != OMP_PARALLEL
8013 && code != OMP_SECTIONS
8014 && code != OMP_SECTION
8015 && code != OMP_SINGLE);
8017 #endif
8019 /* Otherwise we're gimplifying a subexpression, so the resulting
8020 value is interesting. If it's a valid operand that matches
8021 GIMPLE_TEST_F, we're done. Unless we are handling some
8022 post-effects internally; if that's the case, we need to copy into
8023 a temporary before adding the post-effects to POST_P. */
8024 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8025 goto out;
8027 /* Otherwise, we need to create a new temporary for the gimplified
8028 expression. */
8030 /* We can't return an lvalue if we have an internal postqueue. The
8031 object the lvalue refers to would (probably) be modified by the
8032 postqueue; we need to copy the value out first, which means an
8033 rvalue. */
8034 if ((fallback & fb_lvalue)
8035 && gimple_seq_empty_p (internal_post)
8036 && is_gimple_addressable (*expr_p))
8038 /* An lvalue will do. Take the address of the expression, store it
8039 in a temporary, and replace the expression with an INDIRECT_REF of
8040 that temporary. */
8041 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8042 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8043 *expr_p = build_simple_mem_ref (tmp);
8045 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8047 /* An rvalue will do. Assign the gimplified expression into a
8048 new temporary TMP and replace the original expression with
8049 TMP. First, make sure that the expression has a type so that
8050 it can be assigned into a temporary. */
8051 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8052 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8054 else
8056 #ifdef ENABLE_GIMPLE_CHECKING
8057 if (!(fallback & fb_mayfail))
8059 fprintf (stderr, "gimplification failed:\n");
8060 print_generic_expr (stderr, *expr_p, 0);
8061 debug_tree (*expr_p);
8062 internal_error ("gimplification failed");
8064 #endif
8065 gcc_assert (fallback & fb_mayfail);
8067 /* If this is an asm statement, and the user asked for the
8068 impossible, don't die. Fail and let gimplify_asm_expr
8069 issue an error. */
8070 ret = GS_ERROR;
8071 goto out;
8074 /* Make sure the temporary matches our predicate. */
8075 gcc_assert ((*gimple_test_f) (*expr_p));
8077 if (!gimple_seq_empty_p (internal_post))
8079 annotate_all_with_location (internal_post, input_location);
8080 gimplify_seq_add_seq (pre_p, internal_post);
8083 out:
8084 input_location = saved_location;
8085 return ret;
8088 /* Look through TYPE for variable-sized objects and gimplify each such
8089 size that we find. Add to LIST_P any statements generated. */
8091 void
8092 gimplify_type_sizes (tree type, gimple_seq *list_p)
8094 tree field, t;
8096 if (type == NULL || type == error_mark_node)
8097 return;
8099 /* We first do the main variant, then copy into any other variants. */
8100 type = TYPE_MAIN_VARIANT (type);
8102 /* Avoid infinite recursion. */
8103 if (TYPE_SIZES_GIMPLIFIED (type))
8104 return;
8106 TYPE_SIZES_GIMPLIFIED (type) = 1;
8108 switch (TREE_CODE (type))
8110 case INTEGER_TYPE:
8111 case ENUMERAL_TYPE:
8112 case BOOLEAN_TYPE:
8113 case REAL_TYPE:
8114 case FIXED_POINT_TYPE:
8115 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8116 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8118 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8120 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8121 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8123 break;
8125 case ARRAY_TYPE:
8126 /* These types may not have declarations, so handle them here. */
8127 gimplify_type_sizes (TREE_TYPE (type), list_p);
8128 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8129 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8130 with assigned stack slots, for -O1+ -g they should be tracked
8131 by VTA. */
8132 if (!(TYPE_NAME (type)
8133 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8134 && DECL_IGNORED_P (TYPE_NAME (type)))
8135 && TYPE_DOMAIN (type)
8136 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8138 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8139 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8140 DECL_IGNORED_P (t) = 0;
8141 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8142 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8143 DECL_IGNORED_P (t) = 0;
8145 break;
8147 case RECORD_TYPE:
8148 case UNION_TYPE:
8149 case QUAL_UNION_TYPE:
8150 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8151 if (TREE_CODE (field) == FIELD_DECL)
8153 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8154 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8155 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8156 gimplify_type_sizes (TREE_TYPE (field), list_p);
8158 break;
8160 case POINTER_TYPE:
8161 case REFERENCE_TYPE:
8162 /* We used to recurse on the pointed-to type here, which turned out to
8163 be incorrect because its definition might refer to variables not
8164 yet initialized at this point if a forward declaration is involved.
8166 It was actually useful for anonymous pointed-to types to ensure
8167 that the sizes evaluation dominates every possible later use of the
8168 values. Restricting to such types here would be safe since there
8169 is no possible forward declaration around, but would introduce an
8170 undesirable middle-end semantic to anonymity. We then defer to
8171 front-ends the responsibility of ensuring that the sizes are
8172 evaluated both early and late enough, e.g. by attaching artificial
8173 type declarations to the tree. */
8174 break;
8176 default:
8177 break;
8180 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8181 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8183 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8185 TYPE_SIZE (t) = TYPE_SIZE (type);
8186 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8187 TYPE_SIZES_GIMPLIFIED (t) = 1;
8191 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8192 a size or position, has had all of its SAVE_EXPRs evaluated.
8193 We add any required statements to *STMT_P. */
8195 void
8196 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8198 tree expr = *expr_p;
8200 /* We don't do anything if the value isn't there, is constant, or contains
8201 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8202 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8203 will want to replace it with a new variable, but that will cause problems
8204 if this type is from outside the function. It's OK to have that here. */
8205 if (is_gimple_sizepos (expr))
8206 return;
8208 *expr_p = unshare_expr (expr);
8210 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8213 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8214 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8215 is true, also gimplify the parameters. */
8217 gimple
8218 gimplify_body (tree fndecl, bool do_parms)
8220 location_t saved_location = input_location;
8221 gimple_seq parm_stmts, seq;
8222 gimple outer_bind;
8223 struct gimplify_ctx gctx;
8224 struct cgraph_node *cgn;
8226 timevar_push (TV_TREE_GIMPLIFY);
8228 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8229 gimplification. */
8230 default_rtl_profile ();
8232 gcc_assert (gimplify_ctxp == NULL);
8233 push_gimplify_context (&gctx);
8235 /* Unshare most shared trees in the body and in that of any nested functions.
8236 It would seem we don't have to do this for nested functions because
8237 they are supposed to be output and then the outer function gimplified
8238 first, but the g++ front end doesn't always do it that way. */
8239 unshare_body (fndecl);
8240 unvisit_body (fndecl);
8242 cgn = cgraph_get_node (fndecl);
8243 if (cgn && cgn->origin)
8244 nonlocal_vlas = pointer_set_create ();
8246 /* Make sure input_location isn't set to something weird. */
8247 input_location = DECL_SOURCE_LOCATION (fndecl);
8249 /* Resolve callee-copies. This has to be done before processing
8250 the body so that DECL_VALUE_EXPR gets processed correctly. */
8251 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8253 /* Gimplify the function's body. */
8254 seq = NULL;
8255 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8256 outer_bind = gimple_seq_first_stmt (seq);
8257 if (!outer_bind)
8259 outer_bind = gimple_build_nop ();
8260 gimplify_seq_add_stmt (&seq, outer_bind);
8263 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8264 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8265 if (gimple_code (outer_bind) == GIMPLE_BIND
8266 && gimple_seq_first (seq) == gimple_seq_last (seq))
8268 else
8269 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8271 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8273 /* If we had callee-copies statements, insert them at the beginning
8274 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8275 if (!gimple_seq_empty_p (parm_stmts))
8277 tree parm;
8279 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8280 gimple_bind_set_body (outer_bind, parm_stmts);
8282 for (parm = DECL_ARGUMENTS (current_function_decl);
8283 parm; parm = DECL_CHAIN (parm))
8284 if (DECL_HAS_VALUE_EXPR_P (parm))
8286 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8287 DECL_IGNORED_P (parm) = 0;
8291 if (nonlocal_vlas)
8293 if (nonlocal_vla_vars)
8295 /* tree-nested.c may later on call declare_vars (..., true);
8296 which relies on BLOCK_VARS chain to be the tail of the
8297 gimple_bind_vars chain. Ensure we don't violate that
8298 assumption. */
8299 if (gimple_bind_block (outer_bind)
8300 == DECL_INITIAL (current_function_decl))
8301 declare_vars (nonlocal_vla_vars, outer_bind, true);
8302 else
8303 BLOCK_VARS (DECL_INITIAL (current_function_decl))
8304 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl)),
8305 nonlocal_vla_vars);
8306 nonlocal_vla_vars = NULL_TREE;
8308 pointer_set_destroy (nonlocal_vlas);
8309 nonlocal_vlas = NULL;
8312 pop_gimplify_context (outer_bind);
8313 gcc_assert (gimplify_ctxp == NULL);
8315 #ifdef ENABLE_CHECKING
8316 if (!seen_error ())
8317 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8318 #endif
8320 timevar_pop (TV_TREE_GIMPLIFY);
8321 input_location = saved_location;
8323 return outer_bind;
8326 typedef char *char_p; /* For DEF_VEC_P. */
8328 /* Return whether we should exclude FNDECL from instrumentation. */
8330 static bool
8331 flag_instrument_functions_exclude_p (tree fndecl)
8333 vec<char_p> *v;
8335 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8336 if (v && v->length () > 0)
8338 const char *name;
8339 int i;
8340 char *s;
8342 name = lang_hooks.decl_printable_name (fndecl, 0);
8343 FOR_EACH_VEC_ELT (*v, i, s)
8344 if (strstr (name, s) != NULL)
8345 return true;
8348 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8349 if (v && v->length () > 0)
8351 const char *name;
8352 int i;
8353 char *s;
8355 name = DECL_SOURCE_FILE (fndecl);
8356 FOR_EACH_VEC_ELT (*v, i, s)
8357 if (strstr (name, s) != NULL)
8358 return true;
8361 return false;
8364 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8365 node for the function we want to gimplify.
8367 Return the sequence of GIMPLE statements corresponding to the body
8368 of FNDECL. */
8370 void
8371 gimplify_function_tree (tree fndecl)
8373 tree parm, ret;
8374 gimple_seq seq;
8375 gimple bind;
8377 gcc_assert (!gimple_body (fndecl));
8379 if (DECL_STRUCT_FUNCTION (fndecl))
8380 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8381 else
8382 push_struct_function (fndecl);
8384 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8386 /* Preliminarily mark non-addressed complex variables as eligible
8387 for promotion to gimple registers. We'll transform their uses
8388 as we find them. */
8389 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8390 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8391 && !TREE_THIS_VOLATILE (parm)
8392 && !needs_to_live_in_memory (parm))
8393 DECL_GIMPLE_REG_P (parm) = 1;
8396 ret = DECL_RESULT (fndecl);
8397 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8398 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8399 && !needs_to_live_in_memory (ret))
8400 DECL_GIMPLE_REG_P (ret) = 1;
8402 bind = gimplify_body (fndecl, true);
8404 /* The tree body of the function is no longer needed, replace it
8405 with the new GIMPLE body. */
8406 seq = NULL;
8407 gimple_seq_add_stmt (&seq, bind);
8408 gimple_set_body (fndecl, seq);
8410 /* If we're instrumenting function entry/exit, then prepend the call to
8411 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8412 catch the exit hook. */
8413 /* ??? Add some way to ignore exceptions for this TFE. */
8414 if (flag_instrument_function_entry_exit
8415 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8416 && !flag_instrument_functions_exclude_p (fndecl))
8418 tree x;
8419 gimple new_bind;
8420 gimple tf;
8421 gimple_seq cleanup = NULL, body = NULL;
8422 tree tmp_var;
8423 gimple call;
8425 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8426 call = gimple_build_call (x, 1, integer_zero_node);
8427 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8428 gimple_call_set_lhs (call, tmp_var);
8429 gimplify_seq_add_stmt (&cleanup, call);
8430 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8431 call = gimple_build_call (x, 2,
8432 build_fold_addr_expr (current_function_decl),
8433 tmp_var);
8434 gimplify_seq_add_stmt (&cleanup, call);
8435 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8437 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8438 call = gimple_build_call (x, 1, integer_zero_node);
8439 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8440 gimple_call_set_lhs (call, tmp_var);
8441 gimplify_seq_add_stmt (&body, call);
8442 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8443 call = gimple_build_call (x, 2,
8444 build_fold_addr_expr (current_function_decl),
8445 tmp_var);
8446 gimplify_seq_add_stmt (&body, call);
8447 gimplify_seq_add_stmt (&body, tf);
8448 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8449 /* Clear the block for BIND, since it is no longer directly inside
8450 the function, but within a try block. */
8451 gimple_bind_set_block (bind, NULL);
8453 /* Replace the current function body with the body
8454 wrapped in the try/finally TF. */
8455 seq = NULL;
8456 gimple_seq_add_stmt (&seq, new_bind);
8457 gimple_set_body (fndecl, seq);
8460 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8461 cfun->curr_properties = PROP_gimple_any;
8463 pop_cfun ();
8466 /* Some transformations like inlining may invalidate the GIMPLE form
8467 for operands. This function traverses all the operands in STMT and
8468 gimplifies anything that is not a valid gimple operand. Any new
8469 GIMPLE statements are inserted before *GSI_P. */
8471 void
8472 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8474 size_t i, num_ops;
8475 tree lhs;
8476 gimple_seq pre = NULL;
8477 gimple post_stmt = NULL;
8478 struct gimplify_ctx gctx;
8480 push_gimplify_context (&gctx);
8481 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8483 switch (gimple_code (stmt))
8485 case GIMPLE_COND:
8486 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8487 is_gimple_val, fb_rvalue);
8488 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8489 is_gimple_val, fb_rvalue);
8490 break;
8491 case GIMPLE_SWITCH:
8492 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8493 is_gimple_val, fb_rvalue);
8494 break;
8495 case GIMPLE_OMP_ATOMIC_LOAD:
8496 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8497 is_gimple_val, fb_rvalue);
8498 break;
8499 case GIMPLE_ASM:
8501 size_t i, noutputs = gimple_asm_noutputs (stmt);
8502 const char *constraint, **oconstraints;
8503 bool allows_mem, allows_reg, is_inout;
8505 oconstraints
8506 = (const char **) alloca ((noutputs) * sizeof (const char *));
8507 for (i = 0; i < noutputs; i++)
8509 tree op = gimple_asm_output_op (stmt, i);
8510 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8511 oconstraints[i] = constraint;
8512 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8513 &allows_reg, &is_inout);
8514 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8515 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8516 fb_lvalue | fb_mayfail);
8518 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8520 tree op = gimple_asm_input_op (stmt, i);
8521 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8522 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8523 oconstraints, &allows_mem, &allows_reg);
8524 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8525 allows_reg = 0;
8526 if (!allows_reg && allows_mem)
8527 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8528 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8529 else
8530 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8531 is_gimple_asm_val, fb_rvalue);
8534 break;
8535 default:
8536 /* NOTE: We start gimplifying operands from last to first to
8537 make sure that side-effects on the RHS of calls, assignments
8538 and ASMs are executed before the LHS. The ordering is not
8539 important for other statements. */
8540 num_ops = gimple_num_ops (stmt);
8541 for (i = num_ops; i > 0; i--)
8543 tree op = gimple_op (stmt, i - 1);
8544 if (op == NULL_TREE)
8545 continue;
8546 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8547 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8548 else if (i == 2
8549 && is_gimple_assign (stmt)
8550 && num_ops == 2
8551 && get_gimple_rhs_class (gimple_expr_code (stmt))
8552 == GIMPLE_SINGLE_RHS)
8553 gimplify_expr (&op, &pre, NULL,
8554 rhs_predicate_for (gimple_assign_lhs (stmt)),
8555 fb_rvalue);
8556 else if (i == 2 && is_gimple_call (stmt))
8558 if (TREE_CODE (op) == FUNCTION_DECL)
8559 continue;
8560 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8562 else
8563 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8564 gimple_set_op (stmt, i - 1, op);
8567 lhs = gimple_get_lhs (stmt);
8568 /* If the LHS changed it in a way that requires a simple RHS,
8569 create temporary. */
8570 if (lhs && !is_gimple_reg (lhs))
8572 bool need_temp = false;
8574 if (is_gimple_assign (stmt)
8575 && num_ops == 2
8576 && get_gimple_rhs_class (gimple_expr_code (stmt))
8577 == GIMPLE_SINGLE_RHS)
8578 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8579 rhs_predicate_for (gimple_assign_lhs (stmt)),
8580 fb_rvalue);
8581 else if (is_gimple_reg (lhs))
8583 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8585 if (is_gimple_call (stmt))
8587 i = gimple_call_flags (stmt);
8588 if ((i & ECF_LOOPING_CONST_OR_PURE)
8589 || !(i & (ECF_CONST | ECF_PURE)))
8590 need_temp = true;
8592 if (stmt_can_throw_internal (stmt))
8593 need_temp = true;
8596 else
8598 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8599 need_temp = true;
8600 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8602 if (is_gimple_call (stmt))
8604 tree fndecl = gimple_call_fndecl (stmt);
8606 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8607 && !(fndecl && DECL_RESULT (fndecl)
8608 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8609 need_temp = true;
8611 else
8612 need_temp = true;
8615 if (need_temp)
8617 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8618 if (gimple_in_ssa_p (cfun))
8619 temp = make_ssa_name (temp, NULL);
8620 gimple_set_lhs (stmt, temp);
8621 post_stmt = gimple_build_assign (lhs, temp);
8622 if (TREE_CODE (lhs) == SSA_NAME)
8623 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8626 break;
8629 if (!gimple_seq_empty_p (pre))
8630 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8631 if (post_stmt)
8632 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8634 pop_gimplify_context (NULL);
8636 update_stmt (stmt);
8639 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8640 the predicate that will hold for the result. If VAR is not NULL, make the
8641 base variable of the final destination be VAR if suitable. */
8643 tree
8644 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8645 gimple_predicate gimple_test_f, tree var)
8647 enum gimplify_status ret;
8648 struct gimplify_ctx gctx;
8649 location_t saved_location;
8651 *stmts = NULL;
8653 /* gimple_test_f might be more strict than is_gimple_val, make
8654 sure we pass both. Just checking gimple_test_f doesn't work
8655 because most gimple predicates do not work recursively. */
8656 if (is_gimple_val (expr)
8657 && (*gimple_test_f) (expr))
8658 return expr;
8660 push_gimplify_context (&gctx);
8661 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8662 gimplify_ctxp->allow_rhs_cond_expr = true;
8663 saved_location = input_location;
8664 input_location = UNKNOWN_LOCATION;
8666 if (var)
8668 if (gimplify_ctxp->into_ssa
8669 && is_gimple_reg (var))
8670 var = make_ssa_name (var, NULL);
8671 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8674 if (TREE_CODE (expr) != MODIFY_EXPR
8675 && TREE_TYPE (expr) == void_type_node)
8677 gimplify_and_add (expr, stmts);
8678 expr = NULL_TREE;
8680 else
8682 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8683 gcc_assert (ret != GS_ERROR);
8686 input_location = saved_location;
8687 pop_gimplify_context (NULL);
8689 return expr;
8692 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8693 force the result to be either ssa_name or an invariant, otherwise
8694 just force it to be a rhs expression. If VAR is not NULL, make the
8695 base variable of the final destination be VAR if suitable. */
8697 tree
8698 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8700 return force_gimple_operand_1 (expr, stmts,
8701 simple ? is_gimple_val : is_gimple_reg_rhs,
8702 var);
8705 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8706 and VAR. If some statements are produced, emits them at GSI.
8707 If BEFORE is true. the statements are appended before GSI, otherwise
8708 they are appended after it. M specifies the way GSI moves after
8709 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8711 tree
8712 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8713 gimple_predicate gimple_test_f,
8714 tree var, bool before,
8715 enum gsi_iterator_update m)
8717 gimple_seq stmts;
8719 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8721 if (!gimple_seq_empty_p (stmts))
8723 if (before)
8724 gsi_insert_seq_before (gsi, stmts, m);
8725 else
8726 gsi_insert_seq_after (gsi, stmts, m);
8729 return expr;
8732 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8733 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8734 otherwise just force it to be a rhs expression. If some statements are
8735 produced, emits them at GSI. If BEFORE is true, the statements are
8736 appended before GSI, otherwise they are appended after it. M specifies
8737 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8738 are the usual values). */
8740 tree
8741 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8742 bool simple_p, tree var, bool before,
8743 enum gsi_iterator_update m)
8745 return force_gimple_operand_gsi_1 (gsi, expr,
8746 simple_p
8747 ? is_gimple_val : is_gimple_reg_rhs,
8748 var, before, m);
8752 #include "gt-gimplify.h"