Merge trunk version 203514 into gupc branch.
[official-gcc.git] / gcc / gimplify.c
blob74acf0642cfc0feaa4c27528b246fe684dcb43f8
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-ssa.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"
46 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
47 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
48 #include "tree-mudflap.h"
49 #include "expr.h"
50 #include "tm_p.h"
52 enum gimplify_omp_var_data
54 GOVD_SEEN = 1,
55 GOVD_EXPLICIT = 2,
56 GOVD_SHARED = 4,
57 GOVD_PRIVATE = 8,
58 GOVD_FIRSTPRIVATE = 16,
59 GOVD_LASTPRIVATE = 32,
60 GOVD_REDUCTION = 64,
61 GOVD_LOCAL = 128,
62 GOVD_MAP = 256,
63 GOVD_DEBUG_PRIVATE = 512,
64 GOVD_PRIVATE_OUTER_REF = 1024,
65 GOVD_LINEAR = 2048,
66 GOVD_ALIGNED = 4096,
67 GOVD_MAP_TO_ONLY = 8192,
68 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
69 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
70 | GOVD_LOCAL)
74 enum omp_region_type
76 ORT_WORKSHARE = 0,
77 ORT_SIMD = 1,
78 ORT_PARALLEL = 2,
79 ORT_COMBINED_PARALLEL = 3,
80 ORT_TASK = 4,
81 ORT_UNTIED_TASK = 5,
82 ORT_TEAMS = 8,
83 ORT_TARGET_DATA = 16,
84 ORT_TARGET = 32
87 struct gimplify_omp_ctx
89 struct gimplify_omp_ctx *outer_context;
90 splay_tree variables;
91 struct pointer_set_t *privatized_types;
92 location_t location;
93 enum omp_clause_default_kind default_kind;
94 enum omp_region_type region_type;
95 bool combined_loop;
98 static struct gimplify_ctx *gimplify_ctxp;
99 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
102 /* Forward declaration. */
103 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
105 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
106 form and we don't do any syntax checking. */
108 void
109 mark_addressable (tree x)
111 while (handled_component_p (x))
112 x = TREE_OPERAND (x, 0);
113 if (TREE_CODE (x) == MEM_REF
114 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
115 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
116 if (TREE_CODE (x) != VAR_DECL
117 && TREE_CODE (x) != PARM_DECL
118 && TREE_CODE (x) != RESULT_DECL)
119 return;
120 TREE_ADDRESSABLE (x) = 1;
122 /* Also mark the artificial SSA_NAME that points to the partition of X. */
123 if (TREE_CODE (x) == VAR_DECL
124 && !DECL_EXTERNAL (x)
125 && !TREE_STATIC (x)
126 && cfun->gimple_df != NULL
127 && cfun->gimple_df->decls_to_pointers != NULL)
129 void *namep
130 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
131 if (namep)
132 TREE_ADDRESSABLE (*(tree *)namep) = 1;
136 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
137 *SEQ_P is NULL, a new sequence is allocated. This function is
138 similar to gimple_seq_add_stmt, but does not scan the operands.
139 During gimplification, we need to manipulate statement sequences
140 before the def/use vectors have been constructed. */
142 void
143 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
145 gimple_stmt_iterator si;
147 if (gs == NULL)
148 return;
150 si = gsi_last (*seq_p);
151 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
154 /* Shorter alias name for the above function for use in gimplify.c
155 only. */
157 static inline void
158 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
160 gimple_seq_add_stmt_without_update (seq_p, gs);
163 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
164 NULL, a new sequence is allocated. This function is
165 similar to gimple_seq_add_seq, but does not scan the operands.
166 During gimplification, we need to manipulate statement sequences
167 before the def/use vectors have been constructed. */
169 static void
170 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
172 gimple_stmt_iterator si;
174 if (src == NULL)
175 return;
177 si = gsi_last (*dst_p);
178 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
181 /* Set up a context for the gimplifier. */
183 void
184 push_gimplify_context (struct gimplify_ctx *c)
186 memset (c, '\0', sizeof (*c));
187 c->prev_context = gimplify_ctxp;
188 gimplify_ctxp = c;
191 /* Tear down a context for the gimplifier. If BODY is non-null, then
192 put the temporaries into the outer BIND_EXPR. Otherwise, put them
193 in the local_decls.
195 BODY is not a sequence, but the first tuple in a sequence. */
197 void
198 pop_gimplify_context (gimple body)
200 struct gimplify_ctx *c = gimplify_ctxp;
202 gcc_assert (c
203 && (!c->bind_expr_stack.exists ()
204 || c->bind_expr_stack.is_empty ()));
205 c->bind_expr_stack.release ();
206 gimplify_ctxp = c->prev_context;
208 if (body)
209 declare_vars (c->temps, body, false);
210 else
211 record_vars (c->temps);
213 if (c->temp_htab.is_created ())
214 c->temp_htab.dispose ();
217 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
219 static void
220 gimple_push_bind_expr (gimple gimple_bind)
222 gimplify_ctxp->bind_expr_stack.reserve (8);
223 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
226 /* Pop the first element off the stack of bindings. */
228 static void
229 gimple_pop_bind_expr (void)
231 gimplify_ctxp->bind_expr_stack.pop ();
234 /* Return the first element of the stack of bindings. */
236 gimple
237 gimple_current_bind_expr (void)
239 return gimplify_ctxp->bind_expr_stack.last ();
242 /* Return the stack of bindings created during gimplification. */
244 vec<gimple>
245 gimple_bind_expr_stack (void)
247 return gimplify_ctxp->bind_expr_stack;
250 /* Return true iff there is a COND_EXPR between us and the innermost
251 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
253 static bool
254 gimple_conditional_context (void)
256 return gimplify_ctxp->conditions > 0;
259 /* Note that we've entered a COND_EXPR. */
261 static void
262 gimple_push_condition (void)
264 #ifdef ENABLE_GIMPLE_CHECKING
265 if (gimplify_ctxp->conditions == 0)
266 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
267 #endif
268 ++(gimplify_ctxp->conditions);
271 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
272 now, add any conditional cleanups we've seen to the prequeue. */
274 static void
275 gimple_pop_condition (gimple_seq *pre_p)
277 int conds = --(gimplify_ctxp->conditions);
279 gcc_assert (conds >= 0);
280 if (conds == 0)
282 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
283 gimplify_ctxp->conditional_cleanups = NULL;
287 /* A stable comparison routine for use with splay trees and DECLs. */
289 static int
290 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
292 tree a = (tree) xa;
293 tree b = (tree) xb;
295 return DECL_UID (a) - DECL_UID (b);
298 /* Create a new omp construct that deals with variable remapping. */
300 static struct gimplify_omp_ctx *
301 new_omp_context (enum omp_region_type region_type)
303 struct gimplify_omp_ctx *c;
305 c = XCNEW (struct gimplify_omp_ctx);
306 c->outer_context = gimplify_omp_ctxp;
307 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
308 c->privatized_types = pointer_set_create ();
309 c->location = input_location;
310 c->region_type = region_type;
311 if ((region_type & ORT_TASK) == 0)
312 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
313 else
314 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
316 return c;
319 /* Destroy an omp construct that deals with variable remapping. */
321 static void
322 delete_omp_context (struct gimplify_omp_ctx *c)
324 splay_tree_delete (c->variables);
325 pointer_set_destroy (c->privatized_types);
326 XDELETE (c);
329 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
330 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
332 /* Both gimplify the statement T and append it to *SEQ_P. This function
333 behaves exactly as gimplify_stmt, but you don't have to pass T as a
334 reference. */
336 void
337 gimplify_and_add (tree t, gimple_seq *seq_p)
339 gimplify_stmt (&t, seq_p);
342 /* Gimplify statement T into sequence *SEQ_P, and return the first
343 tuple in the sequence of generated tuples for this statement.
344 Return NULL if gimplifying T produced no tuples. */
346 static gimple
347 gimplify_and_return_first (tree t, gimple_seq *seq_p)
349 gimple_stmt_iterator last = gsi_last (*seq_p);
351 gimplify_and_add (t, seq_p);
353 if (!gsi_end_p (last))
355 gsi_next (&last);
356 return gsi_stmt (last);
358 else
359 return gimple_seq_first_stmt (*seq_p);
362 /* Strip off a legitimate source ending from the input string NAME of
363 length LEN. Rather than having to know the names used by all of
364 our front ends, we strip off an ending of a period followed by
365 up to five characters. (Java uses ".class".) */
367 static inline void
368 remove_suffix (char *name, int len)
370 int i;
372 for (i = 2; i < 8 && len > i; i++)
374 if (name[len - i] == '.')
376 name[len - i] = '\0';
377 break;
382 /* Create a new temporary name with PREFIX. Return an identifier. */
384 static GTY(()) unsigned int tmp_var_id_num;
386 tree
387 create_tmp_var_name (const char *prefix)
389 char *tmp_name;
391 if (prefix)
393 char *preftmp = ASTRDUP (prefix);
395 remove_suffix (preftmp, strlen (preftmp));
396 clean_symbol_name (preftmp);
398 prefix = preftmp;
401 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
402 return get_identifier (tmp_name);
405 /* Create a new temporary variable declaration of type TYPE.
406 Do NOT push it into the current binding. */
408 tree
409 create_tmp_var_raw (tree type, const char *prefix)
411 tree tmp_var;
413 /* Temps. cannot be UPC shared qualified. */
414 gcc_assert (!upc_shared_type_p (type));
416 tmp_var = build_decl (input_location,
417 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
418 type);
420 /* The variable was declared by the compiler. */
421 DECL_ARTIFICIAL (tmp_var) = 1;
422 /* And we don't want debug info for it. */
423 DECL_IGNORED_P (tmp_var) = 1;
425 /* Make the variable writable. */
426 TREE_READONLY (tmp_var) = 0;
428 DECL_EXTERNAL (tmp_var) = 0;
429 TREE_STATIC (tmp_var) = 0;
430 TREE_USED (tmp_var) = 1;
432 return tmp_var;
435 /* Create a new temporary variable declaration of type TYPE. DO push the
436 variable into the current binding. Further, assume that this is called
437 only from gimplification or optimization, at which point the creation of
438 certain types are bugs. */
440 tree
441 create_tmp_var (tree type, const char *prefix)
443 tree tmp_var;
445 /* We don't allow types that are addressable (meaning we can't make copies),
446 or incomplete. We also used to reject every variable size objects here,
447 but now support those for which a constant upper bound can be obtained.
448 The processing for variable sizes is performed in gimple_add_tmp_var,
449 point at which it really matters and possibly reached via paths not going
450 through this function, e.g. after direct calls to create_tmp_var_raw. */
451 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
453 tmp_var = create_tmp_var_raw (type, prefix);
454 gimple_add_tmp_var (tmp_var);
455 return tmp_var;
458 /* Create a new temporary variable declaration of type TYPE by calling
459 create_tmp_var and if TYPE is a vector or a complex number, mark the new
460 temporary as gimple register. */
462 tree
463 create_tmp_reg (tree type, const char *prefix)
465 tree tmp;
467 tmp = create_tmp_var (type, prefix);
468 if (TREE_CODE (type) == COMPLEX_TYPE
469 || TREE_CODE (type) == VECTOR_TYPE)
470 DECL_GIMPLE_REG_P (tmp) = 1;
472 return tmp;
475 /* Returns true iff T is a valid RHS for an assignment to a renamed
476 user -- or front-end generated artificial -- variable. */
478 static bool
479 is_gimple_reg_rhs (tree t)
481 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
484 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
485 LHS, or for a call argument. */
487 static bool
488 is_gimple_mem_rhs (tree t)
490 /* If we're dealing with a renamable type, either source or dest must be
491 a renamed variable. */
492 if (is_gimple_reg_type (TREE_TYPE (t)))
493 return is_gimple_val (t);
494 else
495 return is_gimple_val (t) || is_gimple_lvalue (t);
498 /* Return true if T is a CALL_EXPR or an expression that can be
499 assigned to a temporary. Note that this predicate should only be
500 used during gimplification. See the rationale for this in
501 gimplify_modify_expr. */
503 static bool
504 is_gimple_reg_rhs_or_call (tree t)
506 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
507 || TREE_CODE (t) == CALL_EXPR);
510 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
511 this predicate should only be used during gimplification. See the
512 rationale for this in gimplify_modify_expr. */
514 static bool
515 is_gimple_mem_rhs_or_call (tree t)
517 /* If we're dealing with a renamable type, either source or dest must be
518 a renamed variable. */
519 if (is_gimple_reg_type (TREE_TYPE (t)))
520 return is_gimple_val (t);
521 else
522 return (is_gimple_val (t) || is_gimple_lvalue (t)
523 || TREE_CODE (t) == CALL_EXPR);
526 /* Create a temporary with a name derived from VAL. Subroutine of
527 lookup_tmp_var; nobody else should call this function. */
529 static inline tree
530 create_tmp_from_val (tree val, bool is_formal)
532 /* Drop all qualifiers and address-space information from the value type. */
533 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
534 tree var = create_tmp_var (type, get_name (val));
535 if (is_formal
536 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
537 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
538 DECL_GIMPLE_REG_P (var) = 1;
539 return var;
542 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
543 an existing expression temporary. */
545 static tree
546 lookup_tmp_var (tree val, bool is_formal)
548 tree ret;
550 /* If not optimizing, never really reuse a temporary. local-alloc
551 won't allocate any variable that is used in more than one basic
552 block, which means it will go into memory, causing much extra
553 work in reload and final and poorer code generation, outweighing
554 the extra memory allocation here. */
555 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
556 ret = create_tmp_from_val (val, is_formal);
557 else
559 elt_t elt, *elt_p;
560 elt_t **slot;
562 elt.val = val;
563 if (!gimplify_ctxp->temp_htab.is_created ())
564 gimplify_ctxp->temp_htab.create (1000);
565 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
566 if (*slot == NULL)
568 elt_p = XNEW (elt_t);
569 elt_p->val = val;
570 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
571 *slot = elt_p;
573 else
575 elt_p = *slot;
576 ret = elt_p->temp;
580 return ret;
583 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
585 static tree
586 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
587 bool is_formal)
589 tree t, mod;
591 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
592 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
593 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
594 fb_rvalue);
596 if (gimplify_ctxp->into_ssa
597 && is_gimple_reg_type (TREE_TYPE (val)))
598 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
599 else
600 t = lookup_tmp_var (val, is_formal);
602 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
604 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
606 /* gimplify_modify_expr might want to reduce this further. */
607 gimplify_and_add (mod, pre_p);
608 ggc_free (mod);
610 return t;
613 /* Return a formal temporary variable initialized with VAL. PRE_P is as
614 in gimplify_expr. Only use this function if:
616 1) The value of the unfactored expression represented by VAL will not
617 change between the initialization and use of the temporary, and
618 2) The temporary will not be otherwise modified.
620 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
621 and #2 means it is inappropriate for && temps.
623 For other cases, use get_initialized_tmp_var instead. */
625 tree
626 get_formal_tmp_var (tree val, gimple_seq *pre_p)
628 return internal_get_tmp_var (val, pre_p, NULL, true);
631 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
632 are as in gimplify_expr. */
634 tree
635 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
637 return internal_get_tmp_var (val, pre_p, post_p, false);
640 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
641 generate debug info for them; otherwise don't. */
643 void
644 declare_vars (tree vars, gimple scope, bool debug_info)
646 tree last = vars;
647 if (last)
649 tree temps, block;
651 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
653 temps = nreverse (last);
655 block = gimple_bind_block (scope);
656 gcc_assert (!block || TREE_CODE (block) == BLOCK);
657 if (!block || !debug_info)
659 DECL_CHAIN (last) = gimple_bind_vars (scope);
660 gimple_bind_set_vars (scope, temps);
662 else
664 /* We need to attach the nodes both to the BIND_EXPR and to its
665 associated BLOCK for debugging purposes. The key point here
666 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
667 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
668 if (BLOCK_VARS (block))
669 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
670 else
672 gimple_bind_set_vars (scope,
673 chainon (gimple_bind_vars (scope), temps));
674 BLOCK_VARS (block) = temps;
680 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
681 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
682 no such upper bound can be obtained. */
684 static void
685 force_constant_size (tree var)
687 /* The only attempt we make is by querying the maximum size of objects
688 of the variable's type. */
690 HOST_WIDE_INT max_size;
692 gcc_assert (TREE_CODE (var) == VAR_DECL);
694 max_size = max_int_size_in_bytes (TREE_TYPE (var));
696 gcc_assert (max_size >= 0);
698 DECL_SIZE_UNIT (var)
699 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
700 DECL_SIZE (var)
701 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
704 /* Push the temporary variable TMP into the current binding. */
706 void
707 gimple_add_tmp_var (tree tmp)
709 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
711 /* Later processing assumes that the object size is constant, which might
712 not be true at this point. Force the use of a constant upper bound in
713 this case. */
714 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
715 force_constant_size (tmp);
717 DECL_CONTEXT (tmp) = current_function_decl;
718 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
720 if (gimplify_ctxp)
722 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
723 gimplify_ctxp->temps = tmp;
725 /* Mark temporaries local within the nearest enclosing parallel. */
726 if (gimplify_omp_ctxp)
728 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
729 while (ctx
730 && (ctx->region_type == ORT_WORKSHARE
731 || ctx->region_type == ORT_SIMD))
732 ctx = ctx->outer_context;
733 if (ctx)
734 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
737 else if (cfun)
738 record_vars (tmp);
739 else
741 gimple_seq body_seq;
743 /* This case is for nested functions. We need to expose the locals
744 they create. */
745 body_seq = gimple_body (current_function_decl);
746 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
750 /* Determine whether to assign a location to the statement GS. */
752 static bool
753 should_carry_location_p (gimple gs)
755 /* Don't emit a line note for a label. We particularly don't want to
756 emit one for the break label, since it doesn't actually correspond
757 to the beginning of the loop/switch. */
758 if (gimple_code (gs) == GIMPLE_LABEL)
759 return false;
761 return true;
764 /* Return true if a location should not be emitted for this statement
765 by annotate_one_with_location. */
767 static inline bool
768 gimple_do_not_emit_location_p (gimple g)
770 return gimple_plf (g, GF_PLF_1);
773 /* Mark statement G so a location will not be emitted by
774 annotate_one_with_location. */
776 static inline void
777 gimple_set_do_not_emit_location (gimple g)
779 /* The PLF flags are initialized to 0 when a new tuple is created,
780 so no need to initialize it anywhere. */
781 gimple_set_plf (g, GF_PLF_1, true);
784 /* Set the location for gimple statement GS to LOCATION. */
786 static void
787 annotate_one_with_location (gimple gs, location_t location)
789 if (!gimple_has_location (gs)
790 && !gimple_do_not_emit_location_p (gs)
791 && should_carry_location_p (gs))
792 gimple_set_location (gs, location);
795 /* Set LOCATION for all the statements after iterator GSI in sequence
796 SEQ. If GSI is pointing to the end of the sequence, start with the
797 first statement in SEQ. */
799 static void
800 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
801 location_t location)
803 if (gsi_end_p (gsi))
804 gsi = gsi_start (seq);
805 else
806 gsi_next (&gsi);
808 for (; !gsi_end_p (gsi); gsi_next (&gsi))
809 annotate_one_with_location (gsi_stmt (gsi), location);
812 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
814 void
815 annotate_all_with_location (gimple_seq stmt_p, location_t location)
817 gimple_stmt_iterator i;
819 if (gimple_seq_empty_p (stmt_p))
820 return;
822 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
824 gimple gs = gsi_stmt (i);
825 annotate_one_with_location (gs, location);
829 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
830 nodes that are referenced more than once in GENERIC functions. This is
831 necessary because gimplification (translation into GIMPLE) is performed
832 by modifying tree nodes in-place, so gimplication of a shared node in a
833 first context could generate an invalid GIMPLE form in a second context.
835 This is achieved with a simple mark/copy/unmark algorithm that walks the
836 GENERIC representation top-down, marks nodes with TREE_VISITED the first
837 time it encounters them, duplicates them if they already have TREE_VISITED
838 set, and finally removes the TREE_VISITED marks it has set.
840 The algorithm works only at the function level, i.e. it generates a GENERIC
841 representation of a function with no nodes shared within the function when
842 passed a GENERIC function (except for nodes that are allowed to be shared).
844 At the global level, it is also necessary to unshare tree nodes that are
845 referenced in more than one function, for the same aforementioned reason.
846 This requires some cooperation from the front-end. There are 2 strategies:
848 1. Manual unsharing. The front-end needs to call unshare_expr on every
849 expression that might end up being shared across functions.
851 2. Deep unsharing. This is an extension of regular unsharing. Instead
852 of calling unshare_expr on expressions that might be shared across
853 functions, the front-end pre-marks them with TREE_VISITED. This will
854 ensure that they are unshared on the first reference within functions
855 when the regular unsharing algorithm runs. The counterpart is that
856 this algorithm must look deeper than for manual unsharing, which is
857 specified by LANG_HOOKS_DEEP_UNSHARING.
859 If there are only few specific cases of node sharing across functions, it is
860 probably easier for a front-end to unshare the expressions manually. On the
861 contrary, if the expressions generated at the global level are as widespread
862 as expressions generated within functions, deep unsharing is very likely the
863 way to go. */
865 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
866 These nodes model computations that must be done once. If we were to
867 unshare something like SAVE_EXPR(i++), the gimplification process would
868 create wrong code. However, if DATA is non-null, it must hold a pointer
869 set that is used to unshare the subtrees of these nodes. */
871 static tree
872 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
874 tree t = *tp;
875 enum tree_code code = TREE_CODE (t);
877 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
878 copy their subtrees if we can make sure to do it only once. */
879 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
881 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
883 else
884 *walk_subtrees = 0;
887 /* Stop at types, decls, constants like copy_tree_r. */
888 else if (TREE_CODE_CLASS (code) == tcc_type
889 || TREE_CODE_CLASS (code) == tcc_declaration
890 || TREE_CODE_CLASS (code) == tcc_constant
891 /* We can't do anything sensible with a BLOCK used as an
892 expression, but we also can't just die when we see it
893 because of non-expression uses. So we avert our eyes
894 and cross our fingers. Silly Java. */
895 || code == BLOCK)
896 *walk_subtrees = 0;
898 /* Cope with the statement expression extension. */
899 else if (code == STATEMENT_LIST)
902 /* Leave the bulk of the work to copy_tree_r itself. */
903 else
904 copy_tree_r (tp, walk_subtrees, NULL);
906 return NULL_TREE;
909 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
910 If *TP has been visited already, then *TP is deeply copied by calling
911 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
913 static tree
914 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
916 tree t = *tp;
917 enum tree_code code = TREE_CODE (t);
919 /* Skip types, decls, and constants. But we do want to look at their
920 types and the bounds of types. Mark them as visited so we properly
921 unmark their subtrees on the unmark pass. If we've already seen them,
922 don't look down further. */
923 if (TREE_CODE_CLASS (code) == tcc_type
924 || TREE_CODE_CLASS (code) == tcc_declaration
925 || TREE_CODE_CLASS (code) == tcc_constant)
927 if (TREE_VISITED (t))
928 *walk_subtrees = 0;
929 else
930 TREE_VISITED (t) = 1;
933 /* If this node has been visited already, unshare it and don't look
934 any deeper. */
935 else if (TREE_VISITED (t))
937 walk_tree (tp, mostly_copy_tree_r, data, NULL);
938 *walk_subtrees = 0;
941 /* Otherwise, mark the node as visited and keep looking. */
942 else
943 TREE_VISITED (t) = 1;
945 return NULL_TREE;
948 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
949 copy_if_shared_r callback unmodified. */
951 static inline void
952 copy_if_shared (tree *tp, void *data)
954 walk_tree (tp, copy_if_shared_r, data, NULL);
957 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
958 any nested functions. */
960 static void
961 unshare_body (tree fndecl)
963 struct cgraph_node *cgn = cgraph_get_node (fndecl);
964 /* If the language requires deep unsharing, we need a pointer set to make
965 sure we don't repeatedly unshare subtrees of unshareable nodes. */
966 struct pointer_set_t *visited
967 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
969 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
970 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
971 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
973 if (visited)
974 pointer_set_destroy (visited);
976 if (cgn)
977 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
978 unshare_body (cgn->symbol.decl);
981 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
982 Subtrees are walked until the first unvisited node is encountered. */
984 static tree
985 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
987 tree t = *tp;
989 /* If this node has been visited, unmark it and keep looking. */
990 if (TREE_VISITED (t))
991 TREE_VISITED (t) = 0;
993 /* Otherwise, don't look any deeper. */
994 else
995 *walk_subtrees = 0;
997 return NULL_TREE;
1000 /* Unmark the visited trees rooted at *TP. */
1002 static inline void
1003 unmark_visited (tree *tp)
1005 walk_tree (tp, unmark_visited_r, NULL, NULL);
1008 /* Likewise, but mark all trees as not visited. */
1010 static void
1011 unvisit_body (tree fndecl)
1013 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1015 unmark_visited (&DECL_SAVED_TREE (fndecl));
1016 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1017 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1019 if (cgn)
1020 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1021 unvisit_body (cgn->symbol.decl);
1024 /* Unconditionally make an unshared copy of EXPR. This is used when using
1025 stored expressions which span multiple functions, such as BINFO_VTABLE,
1026 as the normal unsharing process can't tell that they're shared. */
1028 tree
1029 unshare_expr (tree expr)
1031 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1032 return expr;
1035 /* Worker for unshare_expr_without_location. */
1037 static tree
1038 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1040 if (EXPR_P (*tp))
1041 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1042 else
1043 *walk_subtrees = 0;
1044 return NULL_TREE;
1047 /* Similar to unshare_expr but also prune all expression locations
1048 from EXPR. */
1050 tree
1051 unshare_expr_without_location (tree expr)
1053 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1054 if (EXPR_P (expr))
1055 walk_tree (&expr, prune_expr_location, NULL, NULL);
1056 return expr;
1059 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1060 contain statements and have a value. Assign its value to a temporary
1061 and give it void_type_node. Return the temporary, or NULL_TREE if
1062 WRAPPER was already void. */
1064 tree
1065 voidify_wrapper_expr (tree wrapper, tree temp)
1067 tree type = TREE_TYPE (wrapper);
1068 if (type && !VOID_TYPE_P (type))
1070 tree *p;
1072 /* Set p to point to the body of the wrapper. Loop until we find
1073 something that isn't a wrapper. */
1074 for (p = &wrapper; p && *p; )
1076 switch (TREE_CODE (*p))
1078 case BIND_EXPR:
1079 TREE_SIDE_EFFECTS (*p) = 1;
1080 TREE_TYPE (*p) = void_type_node;
1081 /* For a BIND_EXPR, the body is operand 1. */
1082 p = &BIND_EXPR_BODY (*p);
1083 break;
1085 case CLEANUP_POINT_EXPR:
1086 case TRY_FINALLY_EXPR:
1087 case TRY_CATCH_EXPR:
1088 TREE_SIDE_EFFECTS (*p) = 1;
1089 TREE_TYPE (*p) = void_type_node;
1090 p = &TREE_OPERAND (*p, 0);
1091 break;
1093 case STATEMENT_LIST:
1095 tree_stmt_iterator i = tsi_last (*p);
1096 TREE_SIDE_EFFECTS (*p) = 1;
1097 TREE_TYPE (*p) = void_type_node;
1098 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1100 break;
1102 case COMPOUND_EXPR:
1103 /* Advance to the last statement. Set all container types to
1104 void. */
1105 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1107 TREE_SIDE_EFFECTS (*p) = 1;
1108 TREE_TYPE (*p) = void_type_node;
1110 break;
1112 case TRANSACTION_EXPR:
1113 TREE_SIDE_EFFECTS (*p) = 1;
1114 TREE_TYPE (*p) = void_type_node;
1115 p = &TRANSACTION_EXPR_BODY (*p);
1116 break;
1118 default:
1119 /* Assume that any tree upon which voidify_wrapper_expr is
1120 directly called is a wrapper, and that its body is op0. */
1121 if (p == &wrapper)
1123 TREE_SIDE_EFFECTS (*p) = 1;
1124 TREE_TYPE (*p) = void_type_node;
1125 p = &TREE_OPERAND (*p, 0);
1126 break;
1128 goto out;
1132 out:
1133 if (p == NULL || IS_EMPTY_STMT (*p))
1134 temp = NULL_TREE;
1135 else if (temp)
1137 /* The wrapper is on the RHS of an assignment that we're pushing
1138 down. */
1139 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1140 || TREE_CODE (temp) == MODIFY_EXPR);
1141 TREE_OPERAND (temp, 1) = *p;
1142 *p = temp;
1144 else
1146 temp = create_tmp_var (type, "retval");
1147 *p = build2 (INIT_EXPR, type, temp, *p);
1150 return temp;
1153 return NULL_TREE;
1156 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1157 a temporary through which they communicate. */
1159 static void
1160 build_stack_save_restore (gimple *save, gimple *restore)
1162 tree tmp_var;
1164 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1165 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1166 gimple_call_set_lhs (*save, tmp_var);
1168 *restore
1169 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1170 1, tmp_var);
1173 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1175 static enum gimplify_status
1176 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1178 tree bind_expr = *expr_p;
1179 bool old_save_stack = gimplify_ctxp->save_stack;
1180 tree t;
1181 gimple gimple_bind;
1182 gimple_seq body, cleanup;
1183 gimple stack_save;
1185 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1187 /* Mark variables seen in this bind expr. */
1188 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1190 if (TREE_CODE (t) == VAR_DECL)
1192 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1194 /* Mark variable as local. */
1195 if (ctx && !DECL_EXTERNAL (t)
1196 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1197 || splay_tree_lookup (ctx->variables,
1198 (splay_tree_key) t) == NULL))
1199 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1201 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1203 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1204 cfun->has_local_explicit_reg_vars = true;
1207 /* Preliminarily mark non-addressed complex variables as eligible
1208 for promotion to gimple registers. We'll transform their uses
1209 as we find them. */
1210 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1211 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1212 && !TREE_THIS_VOLATILE (t)
1213 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1214 && !needs_to_live_in_memory (t))
1215 DECL_GIMPLE_REG_P (t) = 1;
1218 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1219 BIND_EXPR_BLOCK (bind_expr));
1220 gimple_push_bind_expr (gimple_bind);
1222 gimplify_ctxp->save_stack = false;
1224 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1225 body = NULL;
1226 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1227 gimple_bind_set_body (gimple_bind, body);
1229 cleanup = NULL;
1230 stack_save = NULL;
1231 if (gimplify_ctxp->save_stack)
1233 gimple stack_restore;
1235 /* Save stack on entry and restore it on exit. Add a try_finally
1236 block to achieve this. Note that mudflap depends on the
1237 format of the emitted code: see mx_register_decls(). */
1238 build_stack_save_restore (&stack_save, &stack_restore);
1240 gimplify_seq_add_stmt (&cleanup, stack_restore);
1243 /* Add clobbers for all variables that go out of scope. */
1244 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1246 if (TREE_CODE (t) == VAR_DECL
1247 && !is_global_var (t)
1248 && DECL_CONTEXT (t) == current_function_decl
1249 && !DECL_HARD_REGISTER (t)
1250 && !TREE_THIS_VOLATILE (t)
1251 && !DECL_HAS_VALUE_EXPR_P (t)
1252 /* Only care for variables that have to be in memory. Others
1253 will be rewritten into SSA names, hence moved to the top-level. */
1254 && !is_gimple_reg (t)
1255 && flag_stack_reuse != SR_NONE)
1257 tree clobber = build_constructor (TREE_TYPE (t),
1258 NULL);
1259 TREE_THIS_VOLATILE (clobber) = 1;
1260 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1264 if (cleanup)
1266 gimple gs;
1267 gimple_seq new_body;
1269 new_body = NULL;
1270 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1271 GIMPLE_TRY_FINALLY);
1273 if (stack_save)
1274 gimplify_seq_add_stmt (&new_body, stack_save);
1275 gimplify_seq_add_stmt (&new_body, gs);
1276 gimple_bind_set_body (gimple_bind, new_body);
1279 gimplify_ctxp->save_stack = old_save_stack;
1280 gimple_pop_bind_expr ();
1282 gimplify_seq_add_stmt (pre_p, gimple_bind);
1284 if (temp)
1286 *expr_p = temp;
1287 return GS_OK;
1290 *expr_p = NULL_TREE;
1291 return GS_ALL_DONE;
1294 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1295 GIMPLE value, it is assigned to a new temporary and the statement is
1296 re-written to return the temporary.
1298 PRE_P points to the sequence where side effects that must happen before
1299 STMT should be stored. */
1301 static enum gimplify_status
1302 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1304 gimple ret;
1305 tree ret_expr = TREE_OPERAND (stmt, 0);
1306 tree result_decl, result;
1308 if (ret_expr == error_mark_node)
1309 return GS_ERROR;
1311 if (!ret_expr
1312 || TREE_CODE (ret_expr) == RESULT_DECL
1313 || ret_expr == error_mark_node)
1315 gimple ret = gimple_build_return (ret_expr);
1316 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1317 gimplify_seq_add_stmt (pre_p, ret);
1318 return GS_ALL_DONE;
1321 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1322 result_decl = NULL_TREE;
1323 else
1325 result_decl = TREE_OPERAND (ret_expr, 0);
1327 /* See through a return by reference. */
1328 if (TREE_CODE (result_decl) == INDIRECT_REF)
1329 result_decl = TREE_OPERAND (result_decl, 0);
1331 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1332 || TREE_CODE (ret_expr) == INIT_EXPR)
1333 && TREE_CODE (result_decl) == RESULT_DECL);
1336 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1337 Recall that aggregate_value_p is FALSE for any aggregate type that is
1338 returned in registers. If we're returning values in registers, then
1339 we don't want to extend the lifetime of the RESULT_DECL, particularly
1340 across another call. In addition, for those aggregates for which
1341 hard_function_value generates a PARALLEL, we'll die during normal
1342 expansion of structure assignments; there's special code in expand_return
1343 to handle this case that does not exist in expand_expr. */
1344 if (!result_decl)
1345 result = NULL_TREE;
1346 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1348 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1350 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1351 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1352 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1353 should be effectively allocated by the caller, i.e. all calls to
1354 this function must be subject to the Return Slot Optimization. */
1355 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1356 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1358 result = result_decl;
1360 else if (gimplify_ctxp->return_temp)
1361 result = gimplify_ctxp->return_temp;
1362 else
1364 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1366 /* ??? With complex control flow (usually involving abnormal edges),
1367 we can wind up warning about an uninitialized value for this. Due
1368 to how this variable is constructed and initialized, this is never
1369 true. Give up and never warn. */
1370 TREE_NO_WARNING (result) = 1;
1372 gimplify_ctxp->return_temp = result;
1375 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1376 Then gimplify the whole thing. */
1377 if (result != result_decl)
1378 TREE_OPERAND (ret_expr, 0) = result;
1380 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1382 ret = gimple_build_return (result);
1383 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1384 gimplify_seq_add_stmt (pre_p, ret);
1386 return GS_ALL_DONE;
1389 /* Gimplify a variable-length array DECL. */
1391 static void
1392 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1394 /* This is a variable-sized decl. Simplify its size and mark it
1395 for deferred expansion. Note that mudflap depends on the format
1396 of the emitted code: see mx_register_decls(). */
1397 tree t, addr, ptr_type;
1399 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1400 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1402 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1403 if (DECL_HAS_VALUE_EXPR_P (decl))
1404 return;
1406 /* All occurrences of this decl in final gimplified code will be
1407 replaced by indirection. Setting DECL_VALUE_EXPR does two
1408 things: First, it lets the rest of the gimplifier know what
1409 replacement to use. Second, it lets the debug info know
1410 where to find the value. */
1411 ptr_type = build_pointer_type (TREE_TYPE (decl));
1412 addr = create_tmp_var (ptr_type, get_name (decl));
1413 DECL_IGNORED_P (addr) = 0;
1414 t = build_fold_indirect_ref (addr);
1415 TREE_THIS_NOTRAP (t) = 1;
1416 SET_DECL_VALUE_EXPR (decl, t);
1417 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1419 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1420 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1421 size_int (DECL_ALIGN (decl)));
1422 /* The call has been built for a variable-sized object. */
1423 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1424 t = fold_convert (ptr_type, t);
1425 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1427 gimplify_and_add (t, seq_p);
1429 /* Indicate that we need to restore the stack level when the
1430 enclosing BIND_EXPR is exited. */
1431 gimplify_ctxp->save_stack = true;
1434 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1435 and initialization explicit. */
1437 static enum gimplify_status
1438 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1440 tree stmt = *stmt_p;
1441 tree decl = DECL_EXPR_DECL (stmt);
1443 *stmt_p = NULL_TREE;
1445 if (TREE_TYPE (decl) == error_mark_node)
1446 return GS_ERROR;
1448 if ((TREE_CODE (decl) == TYPE_DECL
1449 || TREE_CODE (decl) == VAR_DECL)
1450 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1451 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1453 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1454 in case its size expressions contain problematic nodes like CALL_EXPR. */
1455 if (TREE_CODE (decl) == TYPE_DECL
1456 && DECL_ORIGINAL_TYPE (decl)
1457 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1458 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1460 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1462 tree init = DECL_INITIAL (decl);
1464 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1465 || (!TREE_STATIC (decl)
1466 && flag_stack_check == GENERIC_STACK_CHECK
1467 && compare_tree_int (DECL_SIZE_UNIT (decl),
1468 STACK_CHECK_MAX_VAR_SIZE) > 0))
1469 gimplify_vla_decl (decl, seq_p);
1471 /* Some front ends do not explicitly declare all anonymous
1472 artificial variables. We compensate here by declaring the
1473 variables, though it would be better if the front ends would
1474 explicitly declare them. */
1475 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1476 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1477 gimple_add_tmp_var (decl);
1479 if (init && init != error_mark_node)
1481 if (!TREE_STATIC (decl))
1483 DECL_INITIAL (decl) = NULL_TREE;
1484 init = build2 (INIT_EXPR, void_type_node, decl, init);
1485 gimplify_and_add (init, seq_p);
1486 ggc_free (init);
1488 else
1489 /* We must still examine initializers for static variables
1490 as they may contain a label address. */
1491 walk_tree (&init, force_labels_r, NULL, NULL);
1495 return GS_ALL_DONE;
1498 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1499 and replacing the LOOP_EXPR with goto, but if the loop contains an
1500 EXIT_EXPR, we need to append a label for it to jump to. */
1502 static enum gimplify_status
1503 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1505 tree saved_label = gimplify_ctxp->exit_label;
1506 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1508 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1510 gimplify_ctxp->exit_label = NULL_TREE;
1512 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1514 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1516 if (gimplify_ctxp->exit_label)
1517 gimplify_seq_add_stmt (pre_p,
1518 gimple_build_label (gimplify_ctxp->exit_label));
1520 gimplify_ctxp->exit_label = saved_label;
1522 *expr_p = NULL;
1523 return GS_ALL_DONE;
1526 /* Gimplify a statement list onto a sequence. These may be created either
1527 by an enlightened front-end, or by shortcut_cond_expr. */
1529 static enum gimplify_status
1530 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1532 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1534 tree_stmt_iterator i = tsi_start (*expr_p);
1536 while (!tsi_end_p (i))
1538 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1539 tsi_delink (&i);
1542 if (temp)
1544 *expr_p = temp;
1545 return GS_OK;
1548 return GS_ALL_DONE;
1551 /* Compare two case labels. Because the front end should already have
1552 made sure that case ranges do not overlap, it is enough to only compare
1553 the CASE_LOW values of each case label. */
1555 static int
1556 compare_case_labels (const void *p1, const void *p2)
1558 const_tree const case1 = *(const_tree const*)p1;
1559 const_tree const case2 = *(const_tree const*)p2;
1561 /* The 'default' case label always goes first. */
1562 if (!CASE_LOW (case1))
1563 return -1;
1564 else if (!CASE_LOW (case2))
1565 return 1;
1566 else
1567 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1570 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1572 void
1573 sort_case_labels (vec<tree> label_vec)
1575 label_vec.qsort (compare_case_labels);
1578 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1580 LABELS is a vector that contains all case labels to look at.
1582 INDEX_TYPE is the type of the switch index expression. Case labels
1583 in LABELS are discarded if their values are not in the value range
1584 covered by INDEX_TYPE. The remaining case label values are folded
1585 to INDEX_TYPE.
1587 If a default case exists in LABELS, it is removed from LABELS and
1588 returned in DEFAULT_CASEP. If no default case exists, but the
1589 case labels already cover the whole range of INDEX_TYPE, a default
1590 case is returned pointing to one of the existing case labels.
1591 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1593 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1594 apply and no action is taken regardless of whether a default case is
1595 found or not. */
1597 void
1598 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1599 tree index_type,
1600 tree *default_casep)
1602 tree min_value, max_value;
1603 tree default_case = NULL_TREE;
1604 size_t i, len;
1606 i = 0;
1607 min_value = TYPE_MIN_VALUE (index_type);
1608 max_value = TYPE_MAX_VALUE (index_type);
1609 while (i < labels.length ())
1611 tree elt = labels[i];
1612 tree low = CASE_LOW (elt);
1613 tree high = CASE_HIGH (elt);
1614 bool remove_element = FALSE;
1616 if (low)
1618 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1619 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1621 /* This is a non-default case label, i.e. it has a value.
1623 See if the case label is reachable within the range of
1624 the index type. Remove out-of-range case values. Turn
1625 case ranges into a canonical form (high > low strictly)
1626 and convert the case label values to the index type.
1628 NB: The type of gimple_switch_index() may be the promoted
1629 type, but the case labels retain the original type. */
1631 if (high)
1633 /* This is a case range. Discard empty ranges.
1634 If the bounds or the range are equal, turn this
1635 into a simple (one-value) case. */
1636 int cmp = tree_int_cst_compare (high, low);
1637 if (cmp < 0)
1638 remove_element = TRUE;
1639 else if (cmp == 0)
1640 high = NULL_TREE;
1643 if (! high)
1645 /* If the simple case value is unreachable, ignore it. */
1646 if ((TREE_CODE (min_value) == INTEGER_CST
1647 && tree_int_cst_compare (low, min_value) < 0)
1648 || (TREE_CODE (max_value) == INTEGER_CST
1649 && tree_int_cst_compare (low, max_value) > 0))
1650 remove_element = TRUE;
1651 else
1652 low = fold_convert (index_type, low);
1654 else
1656 /* If the entire case range is unreachable, ignore it. */
1657 if ((TREE_CODE (min_value) == INTEGER_CST
1658 && tree_int_cst_compare (high, min_value) < 0)
1659 || (TREE_CODE (max_value) == INTEGER_CST
1660 && tree_int_cst_compare (low, max_value) > 0))
1661 remove_element = TRUE;
1662 else
1664 /* If the lower bound is less than the index type's
1665 minimum value, truncate the range bounds. */
1666 if (TREE_CODE (min_value) == INTEGER_CST
1667 && tree_int_cst_compare (low, min_value) < 0)
1668 low = min_value;
1669 low = fold_convert (index_type, low);
1671 /* If the upper bound is greater than the index type's
1672 maximum value, truncate the range bounds. */
1673 if (TREE_CODE (max_value) == INTEGER_CST
1674 && tree_int_cst_compare (high, max_value) > 0)
1675 high = max_value;
1676 high = fold_convert (index_type, high);
1678 /* We may have folded a case range to a one-value case. */
1679 if (tree_int_cst_equal (low, high))
1680 high = NULL_TREE;
1684 CASE_LOW (elt) = low;
1685 CASE_HIGH (elt) = high;
1687 else
1689 gcc_assert (!default_case);
1690 default_case = elt;
1691 /* The default case must be passed separately to the
1692 gimple_build_switch routine. But if DEFAULT_CASEP
1693 is NULL, we do not remove the default case (it would
1694 be completely lost). */
1695 if (default_casep)
1696 remove_element = TRUE;
1699 if (remove_element)
1700 labels.ordered_remove (i);
1701 else
1702 i++;
1704 len = i;
1706 if (!labels.is_empty ())
1707 sort_case_labels (labels);
1709 if (default_casep && !default_case)
1711 /* If the switch has no default label, add one, so that we jump
1712 around the switch body. If the labels already cover the whole
1713 range of the switch index_type, add the default label pointing
1714 to one of the existing labels. */
1715 if (len
1716 && TYPE_MIN_VALUE (index_type)
1717 && TYPE_MAX_VALUE (index_type)
1718 && tree_int_cst_equal (CASE_LOW (labels[0]),
1719 TYPE_MIN_VALUE (index_type)))
1721 tree low, high = CASE_HIGH (labels[len - 1]);
1722 if (!high)
1723 high = CASE_LOW (labels[len - 1]);
1724 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1726 for (i = 1; i < len; i++)
1728 high = CASE_LOW (labels[i]);
1729 low = CASE_HIGH (labels[i - 1]);
1730 if (!low)
1731 low = CASE_LOW (labels[i - 1]);
1732 if ((TREE_INT_CST_LOW (low) + 1
1733 != TREE_INT_CST_LOW (high))
1734 || (TREE_INT_CST_HIGH (low)
1735 + (TREE_INT_CST_LOW (high) == 0)
1736 != TREE_INT_CST_HIGH (high)))
1737 break;
1739 if (i == len)
1741 tree label = CASE_LABEL (labels[0]);
1742 default_case = build_case_label (NULL_TREE, NULL_TREE,
1743 label);
1749 if (default_casep)
1750 *default_casep = default_case;
1753 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1754 branch to. */
1756 static enum gimplify_status
1757 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1759 tree switch_expr = *expr_p;
1760 gimple_seq switch_body_seq = NULL;
1761 enum gimplify_status ret;
1762 tree index_type = TREE_TYPE (switch_expr);
1763 if (index_type == NULL_TREE)
1764 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1766 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1767 fb_rvalue);
1768 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1769 return ret;
1771 if (SWITCH_BODY (switch_expr))
1773 vec<tree> labels;
1774 vec<tree> saved_labels;
1775 tree default_case = NULL_TREE;
1776 gimple gimple_switch;
1778 /* If someone can be bothered to fill in the labels, they can
1779 be bothered to null out the body too. */
1780 gcc_assert (!SWITCH_LABELS (switch_expr));
1782 /* Save old labels, get new ones from body, then restore the old
1783 labels. Save all the things from the switch body to append after. */
1784 saved_labels = gimplify_ctxp->case_labels;
1785 gimplify_ctxp->case_labels.create (8);
1787 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1788 labels = gimplify_ctxp->case_labels;
1789 gimplify_ctxp->case_labels = saved_labels;
1791 preprocess_case_label_vec_for_gimple (labels, index_type,
1792 &default_case);
1794 if (!default_case)
1796 gimple new_default;
1798 default_case
1799 = build_case_label (NULL_TREE, NULL_TREE,
1800 create_artificial_label (UNKNOWN_LOCATION));
1801 new_default = gimple_build_label (CASE_LABEL (default_case));
1802 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1805 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1806 default_case, labels);
1807 gimplify_seq_add_stmt (pre_p, gimple_switch);
1808 gimplify_seq_add_seq (pre_p, switch_body_seq);
1809 labels.release ();
1811 else
1812 gcc_assert (SWITCH_LABELS (switch_expr));
1814 return GS_ALL_DONE;
1817 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1819 static enum gimplify_status
1820 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1822 struct gimplify_ctx *ctxp;
1823 gimple gimple_label;
1825 /* Invalid OpenMP programs can play Duff's Device type games with
1826 #pragma omp parallel. At least in the C front end, we don't
1827 detect such invalid branches until after gimplification. */
1828 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1829 if (ctxp->case_labels.exists ())
1830 break;
1832 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1833 ctxp->case_labels.safe_push (*expr_p);
1834 gimplify_seq_add_stmt (pre_p, gimple_label);
1836 return GS_ALL_DONE;
1839 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1840 if necessary. */
1842 tree
1843 build_and_jump (tree *label_p)
1845 if (label_p == NULL)
1846 /* If there's nowhere to jump, just fall through. */
1847 return NULL_TREE;
1849 if (*label_p == NULL_TREE)
1851 tree label = create_artificial_label (UNKNOWN_LOCATION);
1852 *label_p = label;
1855 return build1 (GOTO_EXPR, void_type_node, *label_p);
1858 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1859 This also involves building a label to jump to and communicating it to
1860 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1862 static enum gimplify_status
1863 gimplify_exit_expr (tree *expr_p)
1865 tree cond = TREE_OPERAND (*expr_p, 0);
1866 tree expr;
1868 expr = build_and_jump (&gimplify_ctxp->exit_label);
1869 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1870 *expr_p = expr;
1872 return GS_OK;
1875 /* A helper function to be called via walk_tree. Mark all labels under *TP
1876 as being forced. To be called for DECL_INITIAL of static variables. */
1878 tree
1879 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1881 if (TYPE_P (*tp))
1882 *walk_subtrees = 0;
1883 if (TREE_CODE (*tp) == LABEL_DECL)
1884 FORCED_LABEL (*tp) = 1;
1886 return NULL_TREE;
1889 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1890 different from its canonical type, wrap the whole thing inside a
1891 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1892 type.
1894 The canonical type of a COMPONENT_REF is the type of the field being
1895 referenced--unless the field is a bit-field which can be read directly
1896 in a smaller mode, in which case the canonical type is the
1897 sign-appropriate type corresponding to that mode. */
1899 static void
1900 canonicalize_component_ref (tree *expr_p)
1902 tree expr = *expr_p;
1903 tree type;
1905 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1907 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1908 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1909 else
1910 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1912 /* One could argue that all the stuff below is not necessary for
1913 the non-bitfield case and declare it a FE error if type
1914 adjustment would be needed. */
1915 if (TREE_TYPE (expr) != type)
1917 #ifdef ENABLE_TYPES_CHECKING
1918 tree old_type = TREE_TYPE (expr);
1919 #endif
1920 int type_quals;
1922 /* We need to preserve qualifiers and propagate them from
1923 operand 0. */
1924 type_quals = TYPE_QUALS (type)
1925 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1926 if (TYPE_QUALS (type) != type_quals)
1927 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1929 /* Set the type of the COMPONENT_REF to the underlying type. */
1930 TREE_TYPE (expr) = type;
1932 #ifdef ENABLE_TYPES_CHECKING
1933 /* It is now a FE error, if the conversion from the canonical
1934 type to the original expression type is not useless. */
1935 gcc_assert (useless_type_conversion_p (old_type, type));
1936 #endif
1940 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1941 to foo, embed that change in the ADDR_EXPR by converting
1942 T array[U];
1943 (T *)&array
1945 &array[L]
1946 where L is the lower bound. For simplicity, only do this for constant
1947 lower bound.
1948 The constraint is that the type of &array[L] is trivially convertible
1949 to T *. */
1951 static void
1952 canonicalize_addr_expr (tree *expr_p)
1954 tree expr = *expr_p;
1955 tree addr_expr = TREE_OPERAND (expr, 0);
1956 tree datype, ddatype, pddatype;
1958 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1959 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1960 || TREE_CODE (addr_expr) != ADDR_EXPR)
1961 return;
1963 /* The addr_expr type should be a pointer to an array. */
1964 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1965 if (TREE_CODE (datype) != ARRAY_TYPE)
1966 return;
1968 /* The pointer to element type shall be trivially convertible to
1969 the expression pointer type. */
1970 ddatype = TREE_TYPE (datype);
1971 pddatype = build_pointer_type (ddatype);
1972 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1973 pddatype))
1974 return;
1976 /* The lower bound and element sizes must be constant. */
1977 if (!TYPE_SIZE_UNIT (ddatype)
1978 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1979 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1980 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1981 return;
1983 /* All checks succeeded. Build a new node to merge the cast. */
1984 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1985 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1986 NULL_TREE, NULL_TREE);
1987 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1989 /* We can have stripped a required restrict qualifier above. */
1990 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1991 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1994 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1995 underneath as appropriate. */
1997 static enum gimplify_status
1998 gimplify_conversion (tree *expr_p)
2000 location_t loc = EXPR_LOCATION (*expr_p);
2001 gcc_assert (CONVERT_EXPR_P (*expr_p));
2003 /* Then strip away all but the outermost conversion. */
2004 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2006 /* And remove the outermost conversion if it's useless. */
2007 if (tree_ssa_useless_type_conversion (*expr_p))
2008 *expr_p = TREE_OPERAND (*expr_p, 0);
2010 /* If we still have a conversion at the toplevel,
2011 then canonicalize some constructs. */
2012 if (CONVERT_EXPR_P (*expr_p))
2014 tree sub = TREE_OPERAND (*expr_p, 0);
2016 /* If a NOP conversion is changing the type of a COMPONENT_REF
2017 expression, then canonicalize its type now in order to expose more
2018 redundant conversions. */
2019 if (TREE_CODE (sub) == COMPONENT_REF)
2020 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2022 /* If a NOP conversion is changing a pointer to array of foo
2023 to a pointer to foo, embed that change in the ADDR_EXPR. */
2024 else if (TREE_CODE (sub) == ADDR_EXPR)
2025 canonicalize_addr_expr (expr_p);
2028 /* If we have a conversion to a non-register type force the
2029 use of a VIEW_CONVERT_EXPR instead. */
2030 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2031 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2032 TREE_OPERAND (*expr_p, 0));
2034 return GS_OK;
2037 /* Nonlocal VLAs seen in the current function. */
2038 static struct pointer_set_t *nonlocal_vlas;
2040 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2041 DECL_VALUE_EXPR, and it's worth re-examining things. */
2043 static enum gimplify_status
2044 gimplify_var_or_parm_decl (tree *expr_p)
2046 tree decl = *expr_p;
2048 /* ??? If this is a local variable, and it has not been seen in any
2049 outer BIND_EXPR, then it's probably the result of a duplicate
2050 declaration, for which we've already issued an error. It would
2051 be really nice if the front end wouldn't leak these at all.
2052 Currently the only known culprit is C++ destructors, as seen
2053 in g++.old-deja/g++.jason/binding.C. */
2054 if (TREE_CODE (decl) == VAR_DECL
2055 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2056 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2057 && decl_function_context (decl) == current_function_decl)
2059 gcc_assert (seen_error ());
2060 return GS_ERROR;
2063 /* When within an OpenMP context, notice uses of variables. */
2064 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2065 return GS_ALL_DONE;
2067 /* If the decl is an alias for another expression, substitute it now. */
2068 if (DECL_HAS_VALUE_EXPR_P (decl))
2070 tree value_expr = DECL_VALUE_EXPR (decl);
2072 /* For referenced nonlocal VLAs add a decl for debugging purposes
2073 to the current function. */
2074 if (TREE_CODE (decl) == VAR_DECL
2075 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2076 && nonlocal_vlas != NULL
2077 && TREE_CODE (value_expr) == INDIRECT_REF
2078 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2079 && decl_function_context (decl) != current_function_decl)
2081 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2082 while (ctx
2083 && (ctx->region_type == ORT_WORKSHARE
2084 || ctx->region_type == ORT_SIMD))
2085 ctx = ctx->outer_context;
2086 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2088 tree copy = copy_node (decl), block;
2090 lang_hooks.dup_lang_specific_decl (copy);
2091 SET_DECL_RTL (copy, 0);
2092 TREE_USED (copy) = 1;
2093 block = DECL_INITIAL (current_function_decl);
2094 DECL_CHAIN (copy) = BLOCK_VARS (block);
2095 BLOCK_VARS (block) = copy;
2096 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2097 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2101 *expr_p = unshare_expr (value_expr);
2102 return GS_OK;
2105 return GS_ALL_DONE;
2108 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2109 node *EXPR_P.
2111 compound_lval
2112 : min_lval '[' val ']'
2113 | min_lval '.' ID
2114 | compound_lval '[' val ']'
2115 | compound_lval '.' ID
2117 This is not part of the original SIMPLE definition, which separates
2118 array and member references, but it seems reasonable to handle them
2119 together. Also, this way we don't run into problems with union
2120 aliasing; gcc requires that for accesses through a union to alias, the
2121 union reference must be explicit, which was not always the case when we
2122 were splitting up array and member refs.
2124 PRE_P points to the sequence where side effects that must happen before
2125 *EXPR_P should be stored.
2127 POST_P points to the sequence where side effects that must happen after
2128 *EXPR_P should be stored. */
2130 static enum gimplify_status
2131 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2132 fallback_t fallback)
2134 tree *p;
2135 vec<tree> expr_stack;
2136 enum gimplify_status ret = GS_ALL_DONE, tret;
2137 int i;
2138 location_t loc = EXPR_LOCATION (*expr_p);
2139 tree expr = *expr_p;
2141 /* Create a stack of the subexpressions so later we can walk them in
2142 order from inner to outer. */
2143 expr_stack.create (10);
2145 /* We can handle anything that get_inner_reference can deal with. */
2146 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2148 restart:
2149 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2150 if (TREE_CODE (*p) == INDIRECT_REF)
2151 *p = fold_indirect_ref_loc (loc, *p);
2153 if (handled_component_p (*p))
2155 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2156 additional COMPONENT_REFs. */
2157 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2158 && gimplify_var_or_parm_decl (p) == GS_OK)
2159 goto restart;
2160 else
2161 break;
2163 expr_stack.safe_push (*p);
2166 gcc_assert (expr_stack.length ());
2168 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2169 walked through and P points to the innermost expression.
2171 Java requires that we elaborated nodes in source order. That
2172 means we must gimplify the inner expression followed by each of
2173 the indices, in order. But we can't gimplify the inner
2174 expression until we deal with any variable bounds, sizes, or
2175 positions in order to deal with PLACEHOLDER_EXPRs.
2177 So we do this in three steps. First we deal with the annotations
2178 for any variables in the components, then we gimplify the base,
2179 then we gimplify any indices, from left to right. */
2180 for (i = expr_stack.length () - 1; i >= 0; i--)
2182 tree t = expr_stack[i];
2184 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2186 /* Gimplify the low bound and element type size and put them into
2187 the ARRAY_REF. If these values are set, they have already been
2188 gimplified. */
2189 if (TREE_OPERAND (t, 2) == NULL_TREE)
2191 tree low = unshare_expr (array_ref_low_bound (t));
2192 if (!is_gimple_min_invariant (low))
2194 TREE_OPERAND (t, 2) = low;
2195 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2196 post_p, is_gimple_reg,
2197 fb_rvalue);
2198 ret = MIN (ret, tret);
2201 else
2203 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2204 is_gimple_reg, fb_rvalue);
2205 ret = MIN (ret, tret);
2208 if (TREE_OPERAND (t, 3) == NULL_TREE)
2210 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2211 tree elmt_size = unshare_expr (array_ref_element_size (t));
2212 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2214 /* Divide the element size by the alignment of the element
2215 type (above). */
2216 elmt_size
2217 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2219 if (!is_gimple_min_invariant (elmt_size))
2221 TREE_OPERAND (t, 3) = elmt_size;
2222 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2223 post_p, is_gimple_reg,
2224 fb_rvalue);
2225 ret = MIN (ret, tret);
2228 else
2230 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2231 is_gimple_reg, fb_rvalue);
2232 ret = MIN (ret, tret);
2235 else if (TREE_CODE (t) == COMPONENT_REF)
2237 /* Set the field offset into T and gimplify it. */
2238 if (TREE_OPERAND (t, 2) == NULL_TREE)
2240 tree offset = unshare_expr (component_ref_field_offset (t));
2241 tree field = TREE_OPERAND (t, 1);
2242 tree factor
2243 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2245 /* Divide the offset by its alignment. */
2246 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2248 if (!is_gimple_min_invariant (offset))
2250 TREE_OPERAND (t, 2) = offset;
2251 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2252 post_p, is_gimple_reg,
2253 fb_rvalue);
2254 ret = MIN (ret, tret);
2257 else
2259 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2260 is_gimple_reg, fb_rvalue);
2261 ret = MIN (ret, tret);
2266 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2267 so as to match the min_lval predicate. Failure to do so may result
2268 in the creation of large aggregate temporaries. */
2269 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2270 fallback | fb_lvalue);
2271 ret = MIN (ret, tret);
2273 /* And finally, the indices and operands of ARRAY_REF. During this
2274 loop we also remove any useless conversions. */
2275 for (; expr_stack.length () > 0; )
2277 tree t = expr_stack.pop ();
2279 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2281 /* Gimplify the dimension. */
2282 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2284 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2285 is_gimple_val, fb_rvalue);
2286 ret = MIN (ret, tret);
2290 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2292 /* The innermost expression P may have originally had
2293 TREE_SIDE_EFFECTS set which would have caused all the outer
2294 expressions in *EXPR_P leading to P to also have had
2295 TREE_SIDE_EFFECTS set. */
2296 recalculate_side_effects (t);
2299 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2300 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2302 canonicalize_component_ref (expr_p);
2305 expr_stack.release ();
2307 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2309 return ret;
2312 /* Gimplify the self modifying expression pointed to by EXPR_P
2313 (++, --, +=, -=).
2315 PRE_P points to the list where side effects that must happen before
2316 *EXPR_P should be stored.
2318 POST_P points to the list where side effects that must happen after
2319 *EXPR_P should be stored.
2321 WANT_VALUE is nonzero iff we want to use the value of this expression
2322 in another expression.
2324 ARITH_TYPE is the type the computation should be performed in. */
2326 enum gimplify_status
2327 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2328 bool want_value, tree arith_type)
2330 enum tree_code code;
2331 tree lhs, lvalue, rhs, t1;
2332 gimple_seq post = NULL, *orig_post_p = post_p;
2333 bool postfix;
2334 enum tree_code arith_code;
2335 enum gimplify_status ret;
2336 location_t loc = EXPR_LOCATION (*expr_p);
2338 code = TREE_CODE (*expr_p);
2340 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2341 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2343 /* Prefix or postfix? */
2344 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2345 /* Faster to treat as prefix if result is not used. */
2346 postfix = want_value;
2347 else
2348 postfix = false;
2350 /* For postfix, make sure the inner expression's post side effects
2351 are executed after side effects from this expression. */
2352 if (postfix)
2353 post_p = &post;
2355 /* Add or subtract? */
2356 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2357 arith_code = PLUS_EXPR;
2358 else
2359 arith_code = MINUS_EXPR;
2361 /* Gimplify the LHS into a GIMPLE lvalue. */
2362 lvalue = TREE_OPERAND (*expr_p, 0);
2363 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2364 if (ret == GS_ERROR)
2365 return ret;
2367 /* Extract the operands to the arithmetic operation. */
2368 lhs = lvalue;
2369 rhs = TREE_OPERAND (*expr_p, 1);
2371 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2372 that as the result value and in the postqueue operation. */
2373 if (postfix)
2375 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2376 if (ret == GS_ERROR)
2377 return ret;
2379 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2382 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2383 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2385 rhs = convert_to_ptrofftype_loc (loc, rhs);
2386 if (arith_code == MINUS_EXPR)
2387 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2388 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2390 else
2391 t1 = fold_convert (TREE_TYPE (*expr_p),
2392 fold_build2 (arith_code, arith_type,
2393 fold_convert (arith_type, lhs),
2394 fold_convert (arith_type, rhs)));
2396 if (postfix)
2398 gimplify_assign (lvalue, t1, pre_p);
2399 gimplify_seq_add_seq (orig_post_p, post);
2400 *expr_p = lhs;
2401 return GS_ALL_DONE;
2403 else
2405 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2406 return GS_OK;
2410 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2412 static void
2413 maybe_with_size_expr (tree *expr_p)
2415 tree expr = *expr_p;
2416 tree type = TREE_TYPE (expr);
2417 tree size;
2419 /* If we've already wrapped this or the type is error_mark_node, we can't do
2420 anything. */
2421 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2422 || type == error_mark_node)
2423 return;
2425 /* If the size isn't known or is a constant, we have nothing to do. */
2426 size = TYPE_SIZE_UNIT (type);
2427 if (!size || TREE_CODE (size) == INTEGER_CST)
2428 return;
2430 /* Otherwise, make a WITH_SIZE_EXPR. */
2431 size = unshare_expr (size);
2432 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2433 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2436 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2437 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2438 the CALL_EXPR. */
2440 static enum gimplify_status
2441 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2443 bool (*test) (tree);
2444 fallback_t fb;
2446 /* In general, we allow lvalues for function arguments to avoid
2447 extra overhead of copying large aggregates out of even larger
2448 aggregates into temporaries only to copy the temporaries to
2449 the argument list. Make optimizers happy by pulling out to
2450 temporaries those types that fit in registers. */
2451 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2452 test = is_gimple_val, fb = fb_rvalue;
2453 else
2455 test = is_gimple_lvalue, fb = fb_either;
2456 /* Also strip a TARGET_EXPR that would force an extra copy. */
2457 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2459 tree init = TARGET_EXPR_INITIAL (*arg_p);
2460 if (init
2461 && !VOID_TYPE_P (TREE_TYPE (init)))
2462 *arg_p = init;
2466 /* If this is a variable sized type, we must remember the size. */
2467 maybe_with_size_expr (arg_p);
2469 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2470 /* Make sure arguments have the same location as the function call
2471 itself. */
2472 protected_set_expr_location (*arg_p, call_location);
2474 /* There is a sequence point before a function call. Side effects in
2475 the argument list must occur before the actual call. So, when
2476 gimplifying arguments, force gimplify_expr to use an internal
2477 post queue which is then appended to the end of PRE_P. */
2478 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2481 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2482 WANT_VALUE is true if the result of the call is desired. */
2484 static enum gimplify_status
2485 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2487 tree fndecl, parms, p, fnptrtype;
2488 enum gimplify_status ret;
2489 int i, nargs;
2490 gimple call;
2491 bool builtin_va_start_p = FALSE;
2492 location_t loc = EXPR_LOCATION (*expr_p);
2494 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2496 /* For reliable diagnostics during inlining, it is necessary that
2497 every call_expr be annotated with file and line. */
2498 if (! EXPR_HAS_LOCATION (*expr_p))
2499 SET_EXPR_LOCATION (*expr_p, input_location);
2501 /* This may be a call to a builtin function.
2503 Builtin function calls may be transformed into different
2504 (and more efficient) builtin function calls under certain
2505 circumstances. Unfortunately, gimplification can muck things
2506 up enough that the builtin expanders are not aware that certain
2507 transformations are still valid.
2509 So we attempt transformation/gimplification of the call before
2510 we gimplify the CALL_EXPR. At this time we do not manage to
2511 transform all calls in the same manner as the expanders do, but
2512 we do transform most of them. */
2513 fndecl = get_callee_fndecl (*expr_p);
2514 if (fndecl
2515 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2516 switch (DECL_FUNCTION_CODE (fndecl))
2518 case BUILT_IN_VA_START:
2520 builtin_va_start_p = TRUE;
2521 if (call_expr_nargs (*expr_p) < 2)
2523 error ("too few arguments to function %<va_start%>");
2524 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2525 return GS_OK;
2528 if (fold_builtin_next_arg (*expr_p, true))
2530 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2531 return GS_OK;
2533 break;
2535 case BUILT_IN_LINE:
2537 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2538 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2539 return GS_OK;
2541 case BUILT_IN_FILE:
2543 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2544 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2545 return GS_OK;
2547 case BUILT_IN_FUNCTION:
2549 const char *function;
2550 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2551 *expr_p = build_string_literal (strlen (function) + 1, function);
2552 return GS_OK;
2554 default:
2557 if (fndecl && DECL_BUILT_IN (fndecl))
2559 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2560 if (new_tree && new_tree != *expr_p)
2562 /* There was a transformation of this call which computes the
2563 same value, but in a more efficient way. Return and try
2564 again. */
2565 *expr_p = new_tree;
2566 return GS_OK;
2570 /* Remember the original function pointer type. */
2571 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2573 /* There is a sequence point before the call, so any side effects in
2574 the calling expression must occur before the actual call. Force
2575 gimplify_expr to use an internal post queue. */
2576 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2577 is_gimple_call_addr, fb_rvalue);
2579 nargs = call_expr_nargs (*expr_p);
2581 /* Get argument types for verification. */
2582 fndecl = get_callee_fndecl (*expr_p);
2583 parms = NULL_TREE;
2584 if (fndecl)
2585 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2586 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2587 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2589 if (fndecl && DECL_ARGUMENTS (fndecl))
2590 p = DECL_ARGUMENTS (fndecl);
2591 else if (parms)
2592 p = parms;
2593 else
2594 p = NULL_TREE;
2595 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2598 /* If the last argument is __builtin_va_arg_pack () and it is not
2599 passed as a named argument, decrease the number of CALL_EXPR
2600 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2601 if (!p
2602 && i < nargs
2603 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2605 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2606 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2608 if (last_arg_fndecl
2609 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2610 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2611 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2613 tree call = *expr_p;
2615 --nargs;
2616 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2617 CALL_EXPR_FN (call),
2618 nargs, CALL_EXPR_ARGP (call));
2620 /* Copy all CALL_EXPR flags, location and block, except
2621 CALL_EXPR_VA_ARG_PACK flag. */
2622 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2623 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2624 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2625 = CALL_EXPR_RETURN_SLOT_OPT (call);
2626 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2627 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2629 /* Set CALL_EXPR_VA_ARG_PACK. */
2630 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2634 /* Finally, gimplify the function arguments. */
2635 if (nargs > 0)
2637 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2638 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2639 PUSH_ARGS_REVERSED ? i-- : i++)
2641 enum gimplify_status t;
2643 /* Avoid gimplifying the second argument to va_start, which needs to
2644 be the plain PARM_DECL. */
2645 if ((i != 1) || !builtin_va_start_p)
2647 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2648 EXPR_LOCATION (*expr_p));
2650 if (t == GS_ERROR)
2651 ret = GS_ERROR;
2656 /* Verify the function result. */
2657 if (want_value && fndecl
2658 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2660 error_at (loc, "using result of function returning %<void%>");
2661 ret = GS_ERROR;
2664 /* Try this again in case gimplification exposed something. */
2665 if (ret != GS_ERROR)
2667 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2669 if (new_tree && new_tree != *expr_p)
2671 /* There was a transformation of this call which computes the
2672 same value, but in a more efficient way. Return and try
2673 again. */
2674 *expr_p = new_tree;
2675 return GS_OK;
2678 else
2680 *expr_p = error_mark_node;
2681 return GS_ERROR;
2684 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2685 decl. This allows us to eliminate redundant or useless
2686 calls to "const" functions. */
2687 if (TREE_CODE (*expr_p) == CALL_EXPR)
2689 int flags = call_expr_flags (*expr_p);
2690 if (flags & (ECF_CONST | ECF_PURE)
2691 /* An infinite loop is considered a side effect. */
2692 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2693 TREE_SIDE_EFFECTS (*expr_p) = 0;
2696 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2697 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2698 form and delegate the creation of a GIMPLE_CALL to
2699 gimplify_modify_expr. This is always possible because when
2700 WANT_VALUE is true, the caller wants the result of this call into
2701 a temporary, which means that we will emit an INIT_EXPR in
2702 internal_get_tmp_var which will then be handled by
2703 gimplify_modify_expr. */
2704 if (!want_value)
2706 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2707 have to do is replicate it as a GIMPLE_CALL tuple. */
2708 gimple_stmt_iterator gsi;
2709 call = gimple_build_call_from_tree (*expr_p);
2710 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2711 notice_special_calls (call);
2712 gimplify_seq_add_stmt (pre_p, call);
2713 gsi = gsi_last (*pre_p);
2714 /* Don't fold stmts inside of target construct. We'll do it
2715 during omplower pass instead. */
2716 struct gimplify_omp_ctx *ctx;
2717 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2718 if (ctx->region_type == ORT_TARGET)
2719 break;
2720 if (ctx == NULL)
2721 fold_stmt (&gsi);
2722 *expr_p = NULL_TREE;
2724 else
2725 /* Remember the original function type. */
2726 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2727 CALL_EXPR_FN (*expr_p));
2729 return ret;
2732 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2733 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2735 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2736 condition is true or false, respectively. If null, we should generate
2737 our own to skip over the evaluation of this specific expression.
2739 LOCUS is the source location of the COND_EXPR.
2741 This function is the tree equivalent of do_jump.
2743 shortcut_cond_r should only be called by shortcut_cond_expr. */
2745 static tree
2746 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2747 location_t locus)
2749 tree local_label = NULL_TREE;
2750 tree t, expr = NULL;
2752 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2753 retain the shortcut semantics. Just insert the gotos here;
2754 shortcut_cond_expr will append the real blocks later. */
2755 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2757 location_t new_locus;
2759 /* Turn if (a && b) into
2761 if (a); else goto no;
2762 if (b) goto yes; else goto no;
2763 (no:) */
2765 if (false_label_p == NULL)
2766 false_label_p = &local_label;
2768 /* Keep the original source location on the first 'if'. */
2769 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2770 append_to_statement_list (t, &expr);
2772 /* Set the source location of the && on the second 'if'. */
2773 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2774 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2775 new_locus);
2776 append_to_statement_list (t, &expr);
2778 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2780 location_t new_locus;
2782 /* Turn if (a || b) into
2784 if (a) goto yes;
2785 if (b) goto yes; else goto no;
2786 (yes:) */
2788 if (true_label_p == NULL)
2789 true_label_p = &local_label;
2791 /* Keep the original source location on the first 'if'. */
2792 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2793 append_to_statement_list (t, &expr);
2795 /* Set the source location of the || on the second 'if'. */
2796 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2797 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2798 new_locus);
2799 append_to_statement_list (t, &expr);
2801 else if (TREE_CODE (pred) == COND_EXPR
2802 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2803 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2805 location_t new_locus;
2807 /* As long as we're messing with gotos, turn if (a ? b : c) into
2808 if (a)
2809 if (b) goto yes; else goto no;
2810 else
2811 if (c) goto yes; else goto no;
2813 Don't do this if one of the arms has void type, which can happen
2814 in C++ when the arm is throw. */
2816 /* Keep the original source location on the first 'if'. Set the source
2817 location of the ? on the second 'if'. */
2818 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2819 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2820 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2821 false_label_p, locus),
2822 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2823 false_label_p, new_locus));
2825 else
2827 expr = build3 (COND_EXPR, void_type_node, pred,
2828 build_and_jump (true_label_p),
2829 build_and_jump (false_label_p));
2830 SET_EXPR_LOCATION (expr, locus);
2833 if (local_label)
2835 t = build1 (LABEL_EXPR, void_type_node, local_label);
2836 append_to_statement_list (t, &expr);
2839 return expr;
2842 /* Given a conditional expression EXPR with short-circuit boolean
2843 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2844 predicate apart into the equivalent sequence of conditionals. */
2846 static tree
2847 shortcut_cond_expr (tree expr)
2849 tree pred = TREE_OPERAND (expr, 0);
2850 tree then_ = TREE_OPERAND (expr, 1);
2851 tree else_ = TREE_OPERAND (expr, 2);
2852 tree true_label, false_label, end_label, t;
2853 tree *true_label_p;
2854 tree *false_label_p;
2855 bool emit_end, emit_false, jump_over_else;
2856 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2857 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2859 /* First do simple transformations. */
2860 if (!else_se)
2862 /* If there is no 'else', turn
2863 if (a && b) then c
2864 into
2865 if (a) if (b) then c. */
2866 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2868 /* Keep the original source location on the first 'if'. */
2869 location_t locus = EXPR_LOC_OR_HERE (expr);
2870 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2871 /* Set the source location of the && on the second 'if'. */
2872 if (EXPR_HAS_LOCATION (pred))
2873 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2874 then_ = shortcut_cond_expr (expr);
2875 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2876 pred = TREE_OPERAND (pred, 0);
2877 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2878 SET_EXPR_LOCATION (expr, locus);
2882 if (!then_se)
2884 /* If there is no 'then', turn
2885 if (a || b); else d
2886 into
2887 if (a); else if (b); else d. */
2888 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2890 /* Keep the original source location on the first 'if'. */
2891 location_t locus = EXPR_LOC_OR_HERE (expr);
2892 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2893 /* Set the source location of the || on the second 'if'. */
2894 if (EXPR_HAS_LOCATION (pred))
2895 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2896 else_ = shortcut_cond_expr (expr);
2897 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2898 pred = TREE_OPERAND (pred, 0);
2899 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2900 SET_EXPR_LOCATION (expr, locus);
2904 /* If we're done, great. */
2905 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2906 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2907 return expr;
2909 /* Otherwise we need to mess with gotos. Change
2910 if (a) c; else d;
2912 if (a); else goto no;
2913 c; goto end;
2914 no: d; end:
2915 and recursively gimplify the condition. */
2917 true_label = false_label = end_label = NULL_TREE;
2919 /* If our arms just jump somewhere, hijack those labels so we don't
2920 generate jumps to jumps. */
2922 if (then_
2923 && TREE_CODE (then_) == GOTO_EXPR
2924 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2926 true_label = GOTO_DESTINATION (then_);
2927 then_ = NULL;
2928 then_se = false;
2931 if (else_
2932 && TREE_CODE (else_) == GOTO_EXPR
2933 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2935 false_label = GOTO_DESTINATION (else_);
2936 else_ = NULL;
2937 else_se = false;
2940 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2941 if (true_label)
2942 true_label_p = &true_label;
2943 else
2944 true_label_p = NULL;
2946 /* The 'else' branch also needs a label if it contains interesting code. */
2947 if (false_label || else_se)
2948 false_label_p = &false_label;
2949 else
2950 false_label_p = NULL;
2952 /* If there was nothing else in our arms, just forward the label(s). */
2953 if (!then_se && !else_se)
2954 return shortcut_cond_r (pred, true_label_p, false_label_p,
2955 EXPR_LOC_OR_HERE (expr));
2957 /* If our last subexpression already has a terminal label, reuse it. */
2958 if (else_se)
2959 t = expr_last (else_);
2960 else if (then_se)
2961 t = expr_last (then_);
2962 else
2963 t = NULL;
2964 if (t && TREE_CODE (t) == LABEL_EXPR)
2965 end_label = LABEL_EXPR_LABEL (t);
2967 /* If we don't care about jumping to the 'else' branch, jump to the end
2968 if the condition is false. */
2969 if (!false_label_p)
2970 false_label_p = &end_label;
2972 /* We only want to emit these labels if we aren't hijacking them. */
2973 emit_end = (end_label == NULL_TREE);
2974 emit_false = (false_label == NULL_TREE);
2976 /* We only emit the jump over the else clause if we have to--if the
2977 then clause may fall through. Otherwise we can wind up with a
2978 useless jump and a useless label at the end of gimplified code,
2979 which will cause us to think that this conditional as a whole
2980 falls through even if it doesn't. If we then inline a function
2981 which ends with such a condition, that can cause us to issue an
2982 inappropriate warning about control reaching the end of a
2983 non-void function. */
2984 jump_over_else = block_may_fallthru (then_);
2986 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2987 EXPR_LOC_OR_HERE (expr));
2989 expr = NULL;
2990 append_to_statement_list (pred, &expr);
2992 append_to_statement_list (then_, &expr);
2993 if (else_se)
2995 if (jump_over_else)
2997 tree last = expr_last (expr);
2998 t = build_and_jump (&end_label);
2999 if (EXPR_HAS_LOCATION (last))
3000 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3001 append_to_statement_list (t, &expr);
3003 if (emit_false)
3005 t = build1 (LABEL_EXPR, void_type_node, false_label);
3006 append_to_statement_list (t, &expr);
3008 append_to_statement_list (else_, &expr);
3010 if (emit_end && end_label)
3012 t = build1 (LABEL_EXPR, void_type_node, end_label);
3013 append_to_statement_list (t, &expr);
3016 return expr;
3019 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3021 tree
3022 gimple_boolify (tree expr)
3024 tree type = TREE_TYPE (expr);
3025 location_t loc = EXPR_LOCATION (expr);
3027 if (TREE_CODE (expr) == NE_EXPR
3028 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3029 && integer_zerop (TREE_OPERAND (expr, 1)))
3031 tree call = TREE_OPERAND (expr, 0);
3032 tree fn = get_callee_fndecl (call);
3034 /* For __builtin_expect ((long) (x), y) recurse into x as well
3035 if x is truth_value_p. */
3036 if (fn
3037 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3038 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3039 && call_expr_nargs (call) == 2)
3041 tree arg = CALL_EXPR_ARG (call, 0);
3042 if (arg)
3044 if (TREE_CODE (arg) == NOP_EXPR
3045 && TREE_TYPE (arg) == TREE_TYPE (call))
3046 arg = TREE_OPERAND (arg, 0);
3047 if (truth_value_p (TREE_CODE (arg)))
3049 arg = gimple_boolify (arg);
3050 CALL_EXPR_ARG (call, 0)
3051 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3057 switch (TREE_CODE (expr))
3059 case TRUTH_AND_EXPR:
3060 case TRUTH_OR_EXPR:
3061 case TRUTH_XOR_EXPR:
3062 case TRUTH_ANDIF_EXPR:
3063 case TRUTH_ORIF_EXPR:
3064 /* Also boolify the arguments of truth exprs. */
3065 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3066 /* FALLTHRU */
3068 case TRUTH_NOT_EXPR:
3069 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3071 /* These expressions always produce boolean results. */
3072 if (TREE_CODE (type) != BOOLEAN_TYPE)
3073 TREE_TYPE (expr) = boolean_type_node;
3074 return expr;
3076 default:
3077 if (COMPARISON_CLASS_P (expr))
3079 /* There expressions always prduce boolean results. */
3080 if (TREE_CODE (type) != BOOLEAN_TYPE)
3081 TREE_TYPE (expr) = boolean_type_node;
3082 return expr;
3084 /* Other expressions that get here must have boolean values, but
3085 might need to be converted to the appropriate mode. */
3086 if (TREE_CODE (type) == BOOLEAN_TYPE)
3087 return expr;
3088 return fold_convert_loc (loc, boolean_type_node, expr);
3092 /* Given a conditional expression *EXPR_P without side effects, gimplify
3093 its operands. New statements are inserted to PRE_P. */
3095 static enum gimplify_status
3096 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3098 tree expr = *expr_p, cond;
3099 enum gimplify_status ret, tret;
3100 enum tree_code code;
3102 cond = gimple_boolify (COND_EXPR_COND (expr));
3104 /* We need to handle && and || specially, as their gimplification
3105 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3106 code = TREE_CODE (cond);
3107 if (code == TRUTH_ANDIF_EXPR)
3108 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3109 else if (code == TRUTH_ORIF_EXPR)
3110 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3111 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3112 COND_EXPR_COND (*expr_p) = cond;
3114 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3115 is_gimple_val, fb_rvalue);
3116 ret = MIN (ret, tret);
3117 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3118 is_gimple_val, fb_rvalue);
3120 return MIN (ret, tret);
3123 /* Return true if evaluating EXPR could trap.
3124 EXPR is GENERIC, while tree_could_trap_p can be called
3125 only on GIMPLE. */
3127 static bool
3128 generic_expr_could_trap_p (tree expr)
3130 unsigned i, n;
3132 if (!expr || is_gimple_val (expr))
3133 return false;
3135 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3136 return true;
3138 n = TREE_OPERAND_LENGTH (expr);
3139 for (i = 0; i < n; i++)
3140 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3141 return true;
3143 return false;
3146 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3147 into
3149 if (p) if (p)
3150 t1 = a; a;
3151 else or else
3152 t1 = b; b;
3155 The second form is used when *EXPR_P is of type void.
3157 PRE_P points to the list where side effects that must happen before
3158 *EXPR_P should be stored. */
3160 static enum gimplify_status
3161 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3163 tree expr = *expr_p;
3164 tree type = TREE_TYPE (expr);
3165 location_t loc = EXPR_LOCATION (expr);
3166 tree tmp, arm1, arm2;
3167 enum gimplify_status ret;
3168 tree label_true, label_false, label_cont;
3169 bool have_then_clause_p, have_else_clause_p;
3170 gimple gimple_cond;
3171 enum tree_code pred_code;
3172 gimple_seq seq = NULL;
3174 /* If this COND_EXPR has a value, copy the values into a temporary within
3175 the arms. */
3176 if (!VOID_TYPE_P (type))
3178 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3179 tree result;
3181 /* If either an rvalue is ok or we do not require an lvalue, create the
3182 temporary. But we cannot do that if the type is addressable. */
3183 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3184 && !TREE_ADDRESSABLE (type))
3186 if (gimplify_ctxp->allow_rhs_cond_expr
3187 /* If either branch has side effects or could trap, it can't be
3188 evaluated unconditionally. */
3189 && !TREE_SIDE_EFFECTS (then_)
3190 && !generic_expr_could_trap_p (then_)
3191 && !TREE_SIDE_EFFECTS (else_)
3192 && !generic_expr_could_trap_p (else_))
3193 return gimplify_pure_cond_expr (expr_p, pre_p);
3195 tmp = create_tmp_var (type, "iftmp");
3196 result = tmp;
3199 /* Otherwise, only create and copy references to the values. */
3200 else
3202 type = build_pointer_type (type);
3204 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3205 then_ = build_fold_addr_expr_loc (loc, then_);
3207 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3208 else_ = build_fold_addr_expr_loc (loc, else_);
3210 expr
3211 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3213 tmp = create_tmp_var (type, "iftmp");
3214 result = build_simple_mem_ref_loc (loc, tmp);
3217 /* Build the new then clause, `tmp = then_;'. But don't build the
3218 assignment if the value is void; in C++ it can be if it's a throw. */
3219 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3220 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3222 /* Similarly, build the new else clause, `tmp = else_;'. */
3223 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3224 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3226 TREE_TYPE (expr) = void_type_node;
3227 recalculate_side_effects (expr);
3229 /* Move the COND_EXPR to the prequeue. */
3230 gimplify_stmt (&expr, pre_p);
3232 *expr_p = result;
3233 return GS_ALL_DONE;
3236 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3237 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3238 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3239 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3241 /* Make sure the condition has BOOLEAN_TYPE. */
3242 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3244 /* Break apart && and || conditions. */
3245 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3246 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3248 expr = shortcut_cond_expr (expr);
3250 if (expr != *expr_p)
3252 *expr_p = expr;
3254 /* We can't rely on gimplify_expr to re-gimplify the expanded
3255 form properly, as cleanups might cause the target labels to be
3256 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3257 set up a conditional context. */
3258 gimple_push_condition ();
3259 gimplify_stmt (expr_p, &seq);
3260 gimple_pop_condition (pre_p);
3261 gimple_seq_add_seq (pre_p, seq);
3263 return GS_ALL_DONE;
3267 /* Now do the normal gimplification. */
3269 /* Gimplify condition. */
3270 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3271 fb_rvalue);
3272 if (ret == GS_ERROR)
3273 return GS_ERROR;
3274 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3276 gimple_push_condition ();
3278 have_then_clause_p = have_else_clause_p = false;
3279 if (TREE_OPERAND (expr, 1) != NULL
3280 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3281 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3282 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3283 == current_function_decl)
3284 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3285 have different locations, otherwise we end up with incorrect
3286 location information on the branches. */
3287 && (optimize
3288 || !EXPR_HAS_LOCATION (expr)
3289 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3290 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3292 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3293 have_then_clause_p = true;
3295 else
3296 label_true = create_artificial_label (UNKNOWN_LOCATION);
3297 if (TREE_OPERAND (expr, 2) != NULL
3298 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3299 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3300 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3301 == current_function_decl)
3302 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3303 have different locations, otherwise we end up with incorrect
3304 location information on the branches. */
3305 && (optimize
3306 || !EXPR_HAS_LOCATION (expr)
3307 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3308 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3310 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3311 have_else_clause_p = true;
3313 else
3314 label_false = create_artificial_label (UNKNOWN_LOCATION);
3316 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3317 &arm2);
3319 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3320 label_false);
3322 gimplify_seq_add_stmt (&seq, gimple_cond);
3323 label_cont = NULL_TREE;
3324 if (!have_then_clause_p)
3326 /* For if (...) {} else { code; } put label_true after
3327 the else block. */
3328 if (TREE_OPERAND (expr, 1) == NULL_TREE
3329 && !have_else_clause_p
3330 && TREE_OPERAND (expr, 2) != NULL_TREE)
3331 label_cont = label_true;
3332 else
3334 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3335 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3336 /* For if (...) { code; } else {} or
3337 if (...) { code; } else goto label; or
3338 if (...) { code; return; } else { ... }
3339 label_cont isn't needed. */
3340 if (!have_else_clause_p
3341 && TREE_OPERAND (expr, 2) != NULL_TREE
3342 && gimple_seq_may_fallthru (seq))
3344 gimple g;
3345 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3347 g = gimple_build_goto (label_cont);
3349 /* GIMPLE_COND's are very low level; they have embedded
3350 gotos. This particular embedded goto should not be marked
3351 with the location of the original COND_EXPR, as it would
3352 correspond to the COND_EXPR's condition, not the ELSE or the
3353 THEN arms. To avoid marking it with the wrong location, flag
3354 it as "no location". */
3355 gimple_set_do_not_emit_location (g);
3357 gimplify_seq_add_stmt (&seq, g);
3361 if (!have_else_clause_p)
3363 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3364 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3366 if (label_cont)
3367 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3369 gimple_pop_condition (pre_p);
3370 gimple_seq_add_seq (pre_p, seq);
3372 if (ret == GS_ERROR)
3373 ; /* Do nothing. */
3374 else if (have_then_clause_p || have_else_clause_p)
3375 ret = GS_ALL_DONE;
3376 else
3378 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3379 expr = TREE_OPERAND (expr, 0);
3380 gimplify_stmt (&expr, pre_p);
3383 *expr_p = NULL;
3384 return ret;
3387 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3388 to be marked addressable.
3390 We cannot rely on such an expression being directly markable if a temporary
3391 has been created by the gimplification. In this case, we create another
3392 temporary and initialize it with a copy, which will become a store after we
3393 mark it addressable. This can happen if the front-end passed us something
3394 that it could not mark addressable yet, like a Fortran pass-by-reference
3395 parameter (int) floatvar. */
3397 static void
3398 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3400 while (handled_component_p (*expr_p))
3401 expr_p = &TREE_OPERAND (*expr_p, 0);
3402 if (is_gimple_reg (*expr_p))
3403 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3406 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3407 a call to __builtin_memcpy. */
3409 static enum gimplify_status
3410 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3411 gimple_seq *seq_p)
3413 tree t, to, to_ptr, from, from_ptr;
3414 gimple gs;
3415 location_t loc = EXPR_LOCATION (*expr_p);
3417 to = TREE_OPERAND (*expr_p, 0);
3418 from = TREE_OPERAND (*expr_p, 1);
3420 /* Mark the RHS addressable. Beware that it may not be possible to do so
3421 directly if a temporary has been created by the gimplification. */
3422 prepare_gimple_addressable (&from, seq_p);
3424 mark_addressable (from);
3425 from_ptr = build_fold_addr_expr_loc (loc, from);
3426 gimplify_arg (&from_ptr, seq_p, loc);
3428 mark_addressable (to);
3429 to_ptr = build_fold_addr_expr_loc (loc, to);
3430 gimplify_arg (&to_ptr, seq_p, loc);
3432 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3434 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3436 if (want_value)
3438 /* tmp = memcpy() */
3439 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3440 gimple_call_set_lhs (gs, t);
3441 gimplify_seq_add_stmt (seq_p, gs);
3443 *expr_p = build_simple_mem_ref (t);
3444 return GS_ALL_DONE;
3447 gimplify_seq_add_stmt (seq_p, gs);
3448 *expr_p = NULL;
3449 return GS_ALL_DONE;
3452 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3453 a call to __builtin_memset. In this case we know that the RHS is
3454 a CONSTRUCTOR with an empty element list. */
3456 static enum gimplify_status
3457 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3458 gimple_seq *seq_p)
3460 tree t, from, to, to_ptr;
3461 gimple gs;
3462 location_t loc = EXPR_LOCATION (*expr_p);
3464 /* Assert our assumptions, to abort instead of producing wrong code
3465 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3466 not be immediately exposed. */
3467 from = TREE_OPERAND (*expr_p, 1);
3468 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3469 from = TREE_OPERAND (from, 0);
3471 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3472 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3474 /* Now proceed. */
3475 to = TREE_OPERAND (*expr_p, 0);
3477 to_ptr = build_fold_addr_expr_loc (loc, to);
3478 gimplify_arg (&to_ptr, seq_p, loc);
3479 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3481 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3483 if (want_value)
3485 /* tmp = memset() */
3486 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3487 gimple_call_set_lhs (gs, t);
3488 gimplify_seq_add_stmt (seq_p, gs);
3490 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3491 return GS_ALL_DONE;
3494 gimplify_seq_add_stmt (seq_p, gs);
3495 *expr_p = NULL;
3496 return GS_ALL_DONE;
3499 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3500 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3501 assignment. Return non-null if we detect a potential overlap. */
3503 struct gimplify_init_ctor_preeval_data
3505 /* The base decl of the lhs object. May be NULL, in which case we
3506 have to assume the lhs is indirect. */
3507 tree lhs_base_decl;
3509 /* The alias set of the lhs object. */
3510 alias_set_type lhs_alias_set;
3513 static tree
3514 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3516 struct gimplify_init_ctor_preeval_data *data
3517 = (struct gimplify_init_ctor_preeval_data *) xdata;
3518 tree t = *tp;
3520 /* If we find the base object, obviously we have overlap. */
3521 if (data->lhs_base_decl == t)
3522 return t;
3524 /* If the constructor component is indirect, determine if we have a
3525 potential overlap with the lhs. The only bits of information we
3526 have to go on at this point are addressability and alias sets. */
3527 if ((INDIRECT_REF_P (t)
3528 || TREE_CODE (t) == MEM_REF)
3529 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3530 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3531 return t;
3533 /* If the constructor component is a call, determine if it can hide a
3534 potential overlap with the lhs through an INDIRECT_REF like above.
3535 ??? Ugh - this is completely broken. In fact this whole analysis
3536 doesn't look conservative. */
3537 if (TREE_CODE (t) == CALL_EXPR)
3539 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3541 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3542 if (POINTER_TYPE_P (TREE_VALUE (type))
3543 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3544 && alias_sets_conflict_p (data->lhs_alias_set,
3545 get_alias_set
3546 (TREE_TYPE (TREE_VALUE (type)))))
3547 return t;
3550 if (IS_TYPE_OR_DECL_P (t))
3551 *walk_subtrees = 0;
3552 return NULL;
3555 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3556 force values that overlap with the lhs (as described by *DATA)
3557 into temporaries. */
3559 static void
3560 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3561 struct gimplify_init_ctor_preeval_data *data)
3563 enum gimplify_status one;
3565 /* If the value is constant, then there's nothing to pre-evaluate. */
3566 if (TREE_CONSTANT (*expr_p))
3568 /* Ensure it does not have side effects, it might contain a reference to
3569 the object we're initializing. */
3570 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3571 return;
3574 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3575 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3576 return;
3578 /* Recurse for nested constructors. */
3579 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3581 unsigned HOST_WIDE_INT ix;
3582 constructor_elt *ce;
3583 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3585 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3586 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3588 return;
3591 /* If this is a variable sized type, we must remember the size. */
3592 maybe_with_size_expr (expr_p);
3594 /* Gimplify the constructor element to something appropriate for the rhs
3595 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3596 the gimplifier will consider this a store to memory. Doing this
3597 gimplification now means that we won't have to deal with complicated
3598 language-specific trees, nor trees like SAVE_EXPR that can induce
3599 exponential search behavior. */
3600 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3601 if (one == GS_ERROR)
3603 *expr_p = NULL;
3604 return;
3607 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3608 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3609 always be true for all scalars, since is_gimple_mem_rhs insists on a
3610 temporary variable for them. */
3611 if (DECL_P (*expr_p))
3612 return;
3614 /* If this is of variable size, we have no choice but to assume it doesn't
3615 overlap since we can't make a temporary for it. */
3616 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3617 return;
3619 /* Otherwise, we must search for overlap ... */
3620 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3621 return;
3623 /* ... and if found, force the value into a temporary. */
3624 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3627 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3628 a RANGE_EXPR in a CONSTRUCTOR for an array.
3630 var = lower;
3631 loop_entry:
3632 object[var] = value;
3633 if (var == upper)
3634 goto loop_exit;
3635 var = var + 1;
3636 goto loop_entry;
3637 loop_exit:
3639 We increment var _after_ the loop exit check because we might otherwise
3640 fail if upper == TYPE_MAX_VALUE (type for upper).
3642 Note that we never have to deal with SAVE_EXPRs here, because this has
3643 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3645 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3646 gimple_seq *, bool);
3648 static void
3649 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3650 tree value, tree array_elt_type,
3651 gimple_seq *pre_p, bool cleared)
3653 tree loop_entry_label, loop_exit_label, fall_thru_label;
3654 tree var, var_type, cref, tmp;
3656 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3657 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3658 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3660 /* Create and initialize the index variable. */
3661 var_type = TREE_TYPE (upper);
3662 var = create_tmp_var (var_type, NULL);
3663 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3665 /* Add the loop entry label. */
3666 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3668 /* Build the reference. */
3669 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3670 var, NULL_TREE, NULL_TREE);
3672 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3673 the store. Otherwise just assign value to the reference. */
3675 if (TREE_CODE (value) == CONSTRUCTOR)
3676 /* NB we might have to call ourself recursively through
3677 gimplify_init_ctor_eval if the value is a constructor. */
3678 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3679 pre_p, cleared);
3680 else
3681 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3683 /* We exit the loop when the index var is equal to the upper bound. */
3684 gimplify_seq_add_stmt (pre_p,
3685 gimple_build_cond (EQ_EXPR, var, upper,
3686 loop_exit_label, fall_thru_label));
3688 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3690 /* Otherwise, increment the index var... */
3691 tmp = build2 (PLUS_EXPR, var_type, var,
3692 fold_convert (var_type, integer_one_node));
3693 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3695 /* ...and jump back to the loop entry. */
3696 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3698 /* Add the loop exit label. */
3699 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3702 /* Return true if FDECL is accessing a field that is zero sized. */
3704 static bool
3705 zero_sized_field_decl (const_tree fdecl)
3707 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3708 && integer_zerop (DECL_SIZE (fdecl)))
3709 return true;
3710 return false;
3713 /* Return true if TYPE is zero sized. */
3715 static bool
3716 zero_sized_type (const_tree type)
3718 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3719 && integer_zerop (TYPE_SIZE (type)))
3720 return true;
3721 return false;
3724 /* A subroutine of gimplify_init_constructor. Generate individual
3725 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3726 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3727 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3728 zeroed first. */
3730 static void
3731 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3732 gimple_seq *pre_p, bool cleared)
3734 tree array_elt_type = NULL;
3735 unsigned HOST_WIDE_INT ix;
3736 tree purpose, value;
3738 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3739 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3741 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3743 tree cref;
3745 /* NULL values are created above for gimplification errors. */
3746 if (value == NULL)
3747 continue;
3749 if (cleared && initializer_zerop (value))
3750 continue;
3752 /* ??? Here's to hoping the front end fills in all of the indices,
3753 so we don't have to figure out what's missing ourselves. */
3754 gcc_assert (purpose);
3756 /* Skip zero-sized fields, unless value has side-effects. This can
3757 happen with calls to functions returning a zero-sized type, which
3758 we shouldn't discard. As a number of downstream passes don't
3759 expect sets of zero-sized fields, we rely on the gimplification of
3760 the MODIFY_EXPR we make below to drop the assignment statement. */
3761 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3762 continue;
3764 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3765 whole range. */
3766 if (TREE_CODE (purpose) == RANGE_EXPR)
3768 tree lower = TREE_OPERAND (purpose, 0);
3769 tree upper = TREE_OPERAND (purpose, 1);
3771 /* If the lower bound is equal to upper, just treat it as if
3772 upper was the index. */
3773 if (simple_cst_equal (lower, upper))
3774 purpose = upper;
3775 else
3777 gimplify_init_ctor_eval_range (object, lower, upper, value,
3778 array_elt_type, pre_p, cleared);
3779 continue;
3783 if (array_elt_type)
3785 /* Do not use bitsizetype for ARRAY_REF indices. */
3786 if (TYPE_DOMAIN (TREE_TYPE (object)))
3787 purpose
3788 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3789 purpose);
3790 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3791 purpose, NULL_TREE, NULL_TREE);
3793 else
3795 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3796 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3797 unshare_expr (object), purpose, NULL_TREE);
3800 if (TREE_CODE (value) == CONSTRUCTOR
3801 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3802 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3803 pre_p, cleared);
3804 else
3806 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3807 gimplify_and_add (init, pre_p);
3808 ggc_free (init);
3813 /* Return the appropriate RHS predicate for this LHS. */
3815 gimple_predicate
3816 rhs_predicate_for (tree lhs)
3818 if (is_gimple_reg (lhs))
3819 return is_gimple_reg_rhs_or_call;
3820 else
3821 return is_gimple_mem_rhs_or_call;
3824 /* Gimplify a C99 compound literal expression. This just means adding
3825 the DECL_EXPR before the current statement and using its anonymous
3826 decl instead. */
3828 static enum gimplify_status
3829 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3830 bool (*gimple_test_f) (tree),
3831 fallback_t fallback)
3833 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3834 tree decl = DECL_EXPR_DECL (decl_s);
3835 tree init = DECL_INITIAL (decl);
3836 /* Mark the decl as addressable if the compound literal
3837 expression is addressable now, otherwise it is marked too late
3838 after we gimplify the initialization expression. */
3839 if (TREE_ADDRESSABLE (*expr_p))
3840 TREE_ADDRESSABLE (decl) = 1;
3841 /* Otherwise, if we don't need an lvalue and have a literal directly
3842 substitute it. Check if it matches the gimple predicate, as
3843 otherwise we'd generate a new temporary, and we can as well just
3844 use the decl we already have. */
3845 else if (!TREE_ADDRESSABLE (decl)
3846 && init
3847 && (fallback & fb_lvalue) == 0
3848 && gimple_test_f (init))
3850 *expr_p = init;
3851 return GS_OK;
3854 /* Preliminarily mark non-addressed complex variables as eligible
3855 for promotion to gimple registers. We'll transform their uses
3856 as we find them. */
3857 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3858 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3859 && !TREE_THIS_VOLATILE (decl)
3860 && !needs_to_live_in_memory (decl))
3861 DECL_GIMPLE_REG_P (decl) = 1;
3863 /* If the decl is not addressable, then it is being used in some
3864 expression or on the right hand side of a statement, and it can
3865 be put into a readonly data section. */
3866 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3867 TREE_READONLY (decl) = 1;
3869 /* This decl isn't mentioned in the enclosing block, so add it to the
3870 list of temps. FIXME it seems a bit of a kludge to say that
3871 anonymous artificial vars aren't pushed, but everything else is. */
3872 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3873 gimple_add_tmp_var (decl);
3875 gimplify_and_add (decl_s, pre_p);
3876 *expr_p = decl;
3877 return GS_OK;
3880 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3881 return a new CONSTRUCTOR if something changed. */
3883 static tree
3884 optimize_compound_literals_in_ctor (tree orig_ctor)
3886 tree ctor = orig_ctor;
3887 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3888 unsigned int idx, num = vec_safe_length (elts);
3890 for (idx = 0; idx < num; idx++)
3892 tree value = (*elts)[idx].value;
3893 tree newval = value;
3894 if (TREE_CODE (value) == CONSTRUCTOR)
3895 newval = optimize_compound_literals_in_ctor (value);
3896 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3898 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3899 tree decl = DECL_EXPR_DECL (decl_s);
3900 tree init = DECL_INITIAL (decl);
3902 if (!TREE_ADDRESSABLE (value)
3903 && !TREE_ADDRESSABLE (decl)
3904 && init
3905 && TREE_CODE (init) == CONSTRUCTOR)
3906 newval = optimize_compound_literals_in_ctor (init);
3908 if (newval == value)
3909 continue;
3911 if (ctor == orig_ctor)
3913 ctor = copy_node (orig_ctor);
3914 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3915 elts = CONSTRUCTOR_ELTS (ctor);
3917 (*elts)[idx].value = newval;
3919 return ctor;
3922 /* A subroutine of gimplify_modify_expr. Break out elements of a
3923 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3925 Note that we still need to clear any elements that don't have explicit
3926 initializers, so if not all elements are initialized we keep the
3927 original MODIFY_EXPR, we just remove all of the constructor elements.
3929 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3930 GS_ERROR if we would have to create a temporary when gimplifying
3931 this constructor. Otherwise, return GS_OK.
3933 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3935 static enum gimplify_status
3936 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3937 bool want_value, bool notify_temp_creation)
3939 tree object, ctor, type;
3940 enum gimplify_status ret;
3941 vec<constructor_elt, va_gc> *elts;
3943 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3945 if (!notify_temp_creation)
3947 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3948 is_gimple_lvalue, fb_lvalue);
3949 if (ret == GS_ERROR)
3950 return ret;
3953 object = TREE_OPERAND (*expr_p, 0);
3954 ctor = TREE_OPERAND (*expr_p, 1) =
3955 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3956 type = TREE_TYPE (ctor);
3957 elts = CONSTRUCTOR_ELTS (ctor);
3958 ret = GS_ALL_DONE;
3960 switch (TREE_CODE (type))
3962 case RECORD_TYPE:
3963 case UNION_TYPE:
3964 case QUAL_UNION_TYPE:
3965 case ARRAY_TYPE:
3967 struct gimplify_init_ctor_preeval_data preeval_data;
3968 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3969 bool cleared, complete_p, valid_const_initializer;
3971 /* Aggregate types must lower constructors to initialization of
3972 individual elements. The exception is that a CONSTRUCTOR node
3973 with no elements indicates zero-initialization of the whole. */
3974 if (vec_safe_is_empty (elts))
3976 if (notify_temp_creation)
3977 return GS_OK;
3978 break;
3981 /* Fetch information about the constructor to direct later processing.
3982 We might want to make static versions of it in various cases, and
3983 can only do so if it known to be a valid constant initializer. */
3984 valid_const_initializer
3985 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3986 &num_ctor_elements, &complete_p);
3988 /* If a const aggregate variable is being initialized, then it
3989 should never be a lose to promote the variable to be static. */
3990 if (valid_const_initializer
3991 && num_nonzero_elements > 1
3992 && TREE_READONLY (object)
3993 && TREE_CODE (object) == VAR_DECL
3994 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3996 if (notify_temp_creation)
3997 return GS_ERROR;
3998 DECL_INITIAL (object) = ctor;
3999 TREE_STATIC (object) = 1;
4000 if (!DECL_NAME (object))
4001 DECL_NAME (object) = create_tmp_var_name ("C");
4002 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4004 /* ??? C++ doesn't automatically append a .<number> to the
4005 assembler name, and even when it does, it looks at FE private
4006 data structures to figure out what that number should be,
4007 which are not set for this variable. I suppose this is
4008 important for local statics for inline functions, which aren't
4009 "local" in the object file sense. So in order to get a unique
4010 TU-local symbol, we must invoke the lhd version now. */
4011 lhd_set_decl_assembler_name (object);
4013 *expr_p = NULL_TREE;
4014 break;
4017 /* If there are "lots" of initialized elements, even discounting
4018 those that are not address constants (and thus *must* be
4019 computed at runtime), then partition the constructor into
4020 constant and non-constant parts. Block copy the constant
4021 parts in, then generate code for the non-constant parts. */
4022 /* TODO. There's code in cp/typeck.c to do this. */
4024 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4025 /* store_constructor will ignore the clearing of variable-sized
4026 objects. Initializers for such objects must explicitly set
4027 every field that needs to be set. */
4028 cleared = false;
4029 else if (!complete_p)
4030 /* If the constructor isn't complete, clear the whole object
4031 beforehand.
4033 ??? This ought not to be needed. For any element not present
4034 in the initializer, we should simply set them to zero. Except
4035 we'd need to *find* the elements that are not present, and that
4036 requires trickery to avoid quadratic compile-time behavior in
4037 large cases or excessive memory use in small cases. */
4038 cleared = true;
4039 else if (num_ctor_elements - num_nonzero_elements
4040 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4041 && num_nonzero_elements < num_ctor_elements / 4)
4042 /* If there are "lots" of zeros, it's more efficient to clear
4043 the memory and then set the nonzero elements. */
4044 cleared = true;
4045 else
4046 cleared = false;
4048 /* If there are "lots" of initialized elements, and all of them
4049 are valid address constants, then the entire initializer can
4050 be dropped to memory, and then memcpy'd out. Don't do this
4051 for sparse arrays, though, as it's more efficient to follow
4052 the standard CONSTRUCTOR behavior of memset followed by
4053 individual element initialization. Also don't do this for small
4054 all-zero initializers (which aren't big enough to merit
4055 clearing), and don't try to make bitwise copies of
4056 TREE_ADDRESSABLE types. */
4057 if (valid_const_initializer
4058 && !(cleared || num_nonzero_elements == 0)
4059 && !TREE_ADDRESSABLE (type))
4061 HOST_WIDE_INT size = int_size_in_bytes (type);
4062 unsigned int align;
4064 /* ??? We can still get unbounded array types, at least
4065 from the C++ front end. This seems wrong, but attempt
4066 to work around it for now. */
4067 if (size < 0)
4069 size = int_size_in_bytes (TREE_TYPE (object));
4070 if (size >= 0)
4071 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4074 /* Find the maximum alignment we can assume for the object. */
4075 /* ??? Make use of DECL_OFFSET_ALIGN. */
4076 if (DECL_P (object))
4077 align = DECL_ALIGN (object);
4078 else
4079 align = TYPE_ALIGN (type);
4081 /* Do a block move either if the size is so small as to make
4082 each individual move a sub-unit move on average, or if it
4083 is so large as to make individual moves inefficient. */
4084 if (size > 0
4085 && num_nonzero_elements > 1
4086 && (size < num_nonzero_elements
4087 || !can_move_by_pieces (size, align)))
4089 if (notify_temp_creation)
4090 return GS_ERROR;
4092 walk_tree (&ctor, force_labels_r, NULL, NULL);
4093 ctor = tree_output_constant_def (ctor);
4094 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4095 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4096 TREE_OPERAND (*expr_p, 1) = ctor;
4098 /* This is no longer an assignment of a CONSTRUCTOR, but
4099 we still may have processing to do on the LHS. So
4100 pretend we didn't do anything here to let that happen. */
4101 return GS_UNHANDLED;
4105 /* If the target is volatile, we have non-zero elements and more than
4106 one field to assign, initialize the target from a temporary. */
4107 if (TREE_THIS_VOLATILE (object)
4108 && !TREE_ADDRESSABLE (type)
4109 && num_nonzero_elements > 0
4110 && vec_safe_length (elts) > 1)
4112 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4113 TREE_OPERAND (*expr_p, 0) = temp;
4114 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4115 *expr_p,
4116 build2 (MODIFY_EXPR, void_type_node,
4117 object, temp));
4118 return GS_OK;
4121 if (notify_temp_creation)
4122 return GS_OK;
4124 /* If there are nonzero elements and if needed, pre-evaluate to capture
4125 elements overlapping with the lhs into temporaries. We must do this
4126 before clearing to fetch the values before they are zeroed-out. */
4127 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4129 preeval_data.lhs_base_decl = get_base_address (object);
4130 if (!DECL_P (preeval_data.lhs_base_decl))
4131 preeval_data.lhs_base_decl = NULL;
4132 preeval_data.lhs_alias_set = get_alias_set (object);
4134 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4135 pre_p, post_p, &preeval_data);
4138 if (cleared)
4140 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4141 Note that we still have to gimplify, in order to handle the
4142 case of variable sized types. Avoid shared tree structures. */
4143 CONSTRUCTOR_ELTS (ctor) = NULL;
4144 TREE_SIDE_EFFECTS (ctor) = 0;
4145 object = unshare_expr (object);
4146 gimplify_stmt (expr_p, pre_p);
4149 /* If we have not block cleared the object, or if there are nonzero
4150 elements in the constructor, add assignments to the individual
4151 scalar fields of the object. */
4152 if (!cleared || num_nonzero_elements > 0)
4153 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4155 *expr_p = NULL_TREE;
4157 break;
4159 case COMPLEX_TYPE:
4161 tree r, i;
4163 if (notify_temp_creation)
4164 return GS_OK;
4166 /* Extract the real and imaginary parts out of the ctor. */
4167 gcc_assert (elts->length () == 2);
4168 r = (*elts)[0].value;
4169 i = (*elts)[1].value;
4170 if (r == NULL || i == NULL)
4172 tree zero = build_zero_cst (TREE_TYPE (type));
4173 if (r == NULL)
4174 r = zero;
4175 if (i == NULL)
4176 i = zero;
4179 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4180 represent creation of a complex value. */
4181 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4183 ctor = build_complex (type, r, i);
4184 TREE_OPERAND (*expr_p, 1) = ctor;
4186 else
4188 ctor = build2 (COMPLEX_EXPR, type, r, i);
4189 TREE_OPERAND (*expr_p, 1) = ctor;
4190 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4191 pre_p,
4192 post_p,
4193 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4194 fb_rvalue);
4197 break;
4199 case VECTOR_TYPE:
4201 unsigned HOST_WIDE_INT ix;
4202 constructor_elt *ce;
4204 if (notify_temp_creation)
4205 return GS_OK;
4207 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4208 if (TREE_CONSTANT (ctor))
4210 bool constant_p = true;
4211 tree value;
4213 /* Even when ctor is constant, it might contain non-*_CST
4214 elements, such as addresses or trapping values like
4215 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4216 in VECTOR_CST nodes. */
4217 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4218 if (!CONSTANT_CLASS_P (value))
4220 constant_p = false;
4221 break;
4224 if (constant_p)
4226 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4227 break;
4230 /* Don't reduce an initializer constant even if we can't
4231 make a VECTOR_CST. It won't do anything for us, and it'll
4232 prevent us from representing it as a single constant. */
4233 if (initializer_constant_valid_p (ctor, type))
4234 break;
4236 TREE_CONSTANT (ctor) = 0;
4239 /* Vector types use CONSTRUCTOR all the way through gimple
4240 compilation as a general initializer. */
4241 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4243 enum gimplify_status tret;
4244 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4245 fb_rvalue);
4246 if (tret == GS_ERROR)
4247 ret = GS_ERROR;
4249 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4250 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4252 break;
4254 default:
4255 /* So how did we get a CONSTRUCTOR for a scalar type? */
4256 gcc_unreachable ();
4259 if (ret == GS_ERROR)
4260 return GS_ERROR;
4261 else if (want_value)
4263 *expr_p = object;
4264 return GS_OK;
4266 else
4268 /* If we have gimplified both sides of the initializer but have
4269 not emitted an assignment, do so now. */
4270 if (*expr_p)
4272 tree lhs = TREE_OPERAND (*expr_p, 0);
4273 tree rhs = TREE_OPERAND (*expr_p, 1);
4274 gimple init = gimple_build_assign (lhs, rhs);
4275 gimplify_seq_add_stmt (pre_p, init);
4276 *expr_p = NULL;
4279 return GS_ALL_DONE;
4283 /* Given a pointer value OP0, return a simplified version of an
4284 indirection through OP0, or NULL_TREE if no simplification is
4285 possible. This may only be applied to a rhs of an expression.
4286 Note that the resulting type may be different from the type pointed
4287 to in the sense that it is still compatible from the langhooks
4288 point of view. */
4290 static tree
4291 gimple_fold_indirect_ref_rhs (tree t)
4293 return gimple_fold_indirect_ref (t);
4296 /* Subroutine of gimplify_modify_expr to do simplifications of
4297 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4298 something changes. */
4300 static enum gimplify_status
4301 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4302 gimple_seq *pre_p, gimple_seq *post_p,
4303 bool want_value)
4305 enum gimplify_status ret = GS_UNHANDLED;
4306 bool changed;
4310 changed = false;
4311 switch (TREE_CODE (*from_p))
4313 case VAR_DECL:
4314 /* If we're assigning from a read-only variable initialized with
4315 a constructor, do the direct assignment from the constructor,
4316 but only if neither source nor target are volatile since this
4317 latter assignment might end up being done on a per-field basis. */
4318 if (DECL_INITIAL (*from_p)
4319 && TREE_READONLY (*from_p)
4320 && !TREE_THIS_VOLATILE (*from_p)
4321 && !TREE_THIS_VOLATILE (*to_p)
4322 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4324 tree old_from = *from_p;
4325 enum gimplify_status subret;
4327 /* Move the constructor into the RHS. */
4328 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4330 /* Let's see if gimplify_init_constructor will need to put
4331 it in memory. */
4332 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4333 false, true);
4334 if (subret == GS_ERROR)
4336 /* If so, revert the change. */
4337 *from_p = old_from;
4339 else
4341 ret = GS_OK;
4342 changed = true;
4345 break;
4346 case INDIRECT_REF:
4348 /* If we have code like
4350 *(const A*)(A*)&x
4352 where the type of "x" is a (possibly cv-qualified variant
4353 of "A"), treat the entire expression as identical to "x".
4354 This kind of code arises in C++ when an object is bound
4355 to a const reference, and if "x" is a TARGET_EXPR we want
4356 to take advantage of the optimization below. */
4357 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4358 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4359 if (t)
4361 if (TREE_THIS_VOLATILE (t) != volatile_p)
4363 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4364 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4365 build_fold_addr_expr (t));
4366 if (REFERENCE_CLASS_P (t))
4367 TREE_THIS_VOLATILE (t) = volatile_p;
4369 *from_p = t;
4370 ret = GS_OK;
4371 changed = true;
4373 break;
4376 case TARGET_EXPR:
4378 /* If we are initializing something from a TARGET_EXPR, strip the
4379 TARGET_EXPR and initialize it directly, if possible. This can't
4380 be done if the initializer is void, since that implies that the
4381 temporary is set in some non-trivial way.
4383 ??? What about code that pulls out the temp and uses it
4384 elsewhere? I think that such code never uses the TARGET_EXPR as
4385 an initializer. If I'm wrong, we'll die because the temp won't
4386 have any RTL. In that case, I guess we'll need to replace
4387 references somehow. */
4388 tree init = TARGET_EXPR_INITIAL (*from_p);
4390 if (init
4391 && !VOID_TYPE_P (TREE_TYPE (init)))
4393 *from_p = init;
4394 ret = GS_OK;
4395 changed = true;
4398 break;
4400 case COMPOUND_EXPR:
4401 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4402 caught. */
4403 gimplify_compound_expr (from_p, pre_p, true);
4404 ret = GS_OK;
4405 changed = true;
4406 break;
4408 case CONSTRUCTOR:
4409 /* If we already made some changes, let the front end have a
4410 crack at this before we break it down. */
4411 if (ret != GS_UNHANDLED)
4412 break;
4413 /* If we're initializing from a CONSTRUCTOR, break this into
4414 individual MODIFY_EXPRs. */
4415 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4416 false);
4418 case COND_EXPR:
4419 /* If we're assigning to a non-register type, push the assignment
4420 down into the branches. This is mandatory for ADDRESSABLE types,
4421 since we cannot generate temporaries for such, but it saves a
4422 copy in other cases as well. */
4423 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4425 /* This code should mirror the code in gimplify_cond_expr. */
4426 enum tree_code code = TREE_CODE (*expr_p);
4427 tree cond = *from_p;
4428 tree result = *to_p;
4430 ret = gimplify_expr (&result, pre_p, post_p,
4431 is_gimple_lvalue, fb_lvalue);
4432 if (ret != GS_ERROR)
4433 ret = GS_OK;
4435 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4436 TREE_OPERAND (cond, 1)
4437 = build2 (code, void_type_node, result,
4438 TREE_OPERAND (cond, 1));
4439 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4440 TREE_OPERAND (cond, 2)
4441 = build2 (code, void_type_node, unshare_expr (result),
4442 TREE_OPERAND (cond, 2));
4444 TREE_TYPE (cond) = void_type_node;
4445 recalculate_side_effects (cond);
4447 if (want_value)
4449 gimplify_and_add (cond, pre_p);
4450 *expr_p = unshare_expr (result);
4452 else
4453 *expr_p = cond;
4454 return ret;
4456 break;
4458 case CALL_EXPR:
4459 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4460 return slot so that we don't generate a temporary. */
4461 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4462 && aggregate_value_p (*from_p, *from_p))
4464 bool use_target;
4466 if (!(rhs_predicate_for (*to_p))(*from_p))
4467 /* If we need a temporary, *to_p isn't accurate. */
4468 use_target = false;
4469 /* It's OK to use the return slot directly unless it's an NRV. */
4470 else if (TREE_CODE (*to_p) == RESULT_DECL
4471 && DECL_NAME (*to_p) == NULL_TREE
4472 && needs_to_live_in_memory (*to_p))
4473 use_target = true;
4474 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4475 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4476 /* Don't force regs into memory. */
4477 use_target = false;
4478 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4479 /* It's OK to use the target directly if it's being
4480 initialized. */
4481 use_target = true;
4482 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4483 /* Always use the target and thus RSO for variable-sized types.
4484 GIMPLE cannot deal with a variable-sized assignment
4485 embedded in a call statement. */
4486 use_target = true;
4487 else if (TREE_CODE (*to_p) != SSA_NAME
4488 && (!is_gimple_variable (*to_p)
4489 || needs_to_live_in_memory (*to_p)))
4490 /* Don't use the original target if it's already addressable;
4491 if its address escapes, and the called function uses the
4492 NRV optimization, a conforming program could see *to_p
4493 change before the called function returns; see c++/19317.
4494 When optimizing, the return_slot pass marks more functions
4495 as safe after we have escape info. */
4496 use_target = false;
4497 else
4498 use_target = true;
4500 if (use_target)
4502 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4503 mark_addressable (*to_p);
4506 break;
4508 case WITH_SIZE_EXPR:
4509 /* Likewise for calls that return an aggregate of non-constant size,
4510 since we would not be able to generate a temporary at all. */
4511 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4513 *from_p = TREE_OPERAND (*from_p, 0);
4514 /* We don't change ret in this case because the
4515 WITH_SIZE_EXPR might have been added in
4516 gimplify_modify_expr, so returning GS_OK would lead to an
4517 infinite loop. */
4518 changed = true;
4520 break;
4522 /* If we're initializing from a container, push the initialization
4523 inside it. */
4524 case CLEANUP_POINT_EXPR:
4525 case BIND_EXPR:
4526 case STATEMENT_LIST:
4528 tree wrap = *from_p;
4529 tree t;
4531 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4532 fb_lvalue);
4533 if (ret != GS_ERROR)
4534 ret = GS_OK;
4536 t = voidify_wrapper_expr (wrap, *expr_p);
4537 gcc_assert (t == *expr_p);
4539 if (want_value)
4541 gimplify_and_add (wrap, pre_p);
4542 *expr_p = unshare_expr (*to_p);
4544 else
4545 *expr_p = wrap;
4546 return GS_OK;
4549 case COMPOUND_LITERAL_EXPR:
4551 tree complit = TREE_OPERAND (*expr_p, 1);
4552 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4553 tree decl = DECL_EXPR_DECL (decl_s);
4554 tree init = DECL_INITIAL (decl);
4556 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4557 into struct T x = { 0, 1, 2 } if the address of the
4558 compound literal has never been taken. */
4559 if (!TREE_ADDRESSABLE (complit)
4560 && !TREE_ADDRESSABLE (decl)
4561 && init)
4563 *expr_p = copy_node (*expr_p);
4564 TREE_OPERAND (*expr_p, 1) = init;
4565 return GS_OK;
4569 default:
4570 break;
4573 while (changed);
4575 return ret;
4579 /* Return true if T looks like a valid GIMPLE statement. */
4581 static bool
4582 is_gimple_stmt (tree t)
4584 const enum tree_code code = TREE_CODE (t);
4586 switch (code)
4588 case NOP_EXPR:
4589 /* The only valid NOP_EXPR is the empty statement. */
4590 return IS_EMPTY_STMT (t);
4592 case BIND_EXPR:
4593 case COND_EXPR:
4594 /* These are only valid if they're void. */
4595 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4597 case SWITCH_EXPR:
4598 case GOTO_EXPR:
4599 case RETURN_EXPR:
4600 case LABEL_EXPR:
4601 case CASE_LABEL_EXPR:
4602 case TRY_CATCH_EXPR:
4603 case TRY_FINALLY_EXPR:
4604 case EH_FILTER_EXPR:
4605 case CATCH_EXPR:
4606 case ASM_EXPR:
4607 case STATEMENT_LIST:
4608 case OMP_PARALLEL:
4609 case OMP_FOR:
4610 case OMP_SIMD:
4611 case OMP_DISTRIBUTE:
4612 case OMP_SECTIONS:
4613 case OMP_SECTION:
4614 case OMP_SINGLE:
4615 case OMP_MASTER:
4616 case OMP_TASKGROUP:
4617 case OMP_ORDERED:
4618 case OMP_CRITICAL:
4619 case OMP_TASK:
4620 /* These are always void. */
4621 return true;
4623 case CALL_EXPR:
4624 case MODIFY_EXPR:
4625 case PREDICT_EXPR:
4626 /* These are valid regardless of their type. */
4627 return true;
4629 default:
4630 return false;
4635 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4636 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4637 DECL_GIMPLE_REG_P set.
4639 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4640 other, unmodified part of the complex object just before the total store.
4641 As a consequence, if the object is still uninitialized, an undefined value
4642 will be loaded into a register, which may result in a spurious exception
4643 if the register is floating-point and the value happens to be a signaling
4644 NaN for example. Then the fully-fledged complex operations lowering pass
4645 followed by a DCE pass are necessary in order to fix things up. */
4647 static enum gimplify_status
4648 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4649 bool want_value)
4651 enum tree_code code, ocode;
4652 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4654 lhs = TREE_OPERAND (*expr_p, 0);
4655 rhs = TREE_OPERAND (*expr_p, 1);
4656 code = TREE_CODE (lhs);
4657 lhs = TREE_OPERAND (lhs, 0);
4659 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4660 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4661 TREE_NO_WARNING (other) = 1;
4662 other = get_formal_tmp_var (other, pre_p);
4664 realpart = code == REALPART_EXPR ? rhs : other;
4665 imagpart = code == REALPART_EXPR ? other : rhs;
4667 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4668 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4669 else
4670 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4672 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4673 *expr_p = (want_value) ? rhs : NULL_TREE;
4675 return GS_ALL_DONE;
4678 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4680 modify_expr
4681 : varname '=' rhs
4682 | '*' ID '=' rhs
4684 PRE_P points to the list where side effects that must happen before
4685 *EXPR_P should be stored.
4687 POST_P points to the list where side effects that must happen after
4688 *EXPR_P should be stored.
4690 WANT_VALUE is nonzero iff we want to use the value of this expression
4691 in another expression. */
4693 static enum gimplify_status
4694 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4695 bool want_value)
4697 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4698 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4699 enum gimplify_status ret = GS_UNHANDLED;
4700 gimple assign;
4701 location_t loc = EXPR_LOCATION (*expr_p);
4702 gimple_stmt_iterator gsi;
4704 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4705 || TREE_CODE (*expr_p) == INIT_EXPR);
4707 /* Trying to simplify a clobber using normal logic doesn't work,
4708 so handle it here. */
4709 if (TREE_CLOBBER_P (*from_p))
4711 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4712 if (ret == GS_ERROR)
4713 return ret;
4714 gcc_assert (!want_value
4715 && (TREE_CODE (*to_p) == VAR_DECL
4716 || TREE_CODE (*to_p) == MEM_REF));
4717 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4718 *expr_p = NULL;
4719 return GS_ALL_DONE;
4722 /* Insert pointer conversions required by the middle-end that are not
4723 required by the frontend. This fixes middle-end type checking for
4724 for example gcc.dg/redecl-6.c. */
4725 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4727 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4728 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4729 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4732 /* See if any simplifications can be done based on what the RHS is. */
4733 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4734 want_value);
4735 if (ret != GS_UNHANDLED)
4736 return ret;
4738 /* For zero sized types only gimplify the left hand side and right hand
4739 side as statements and throw away the assignment. Do this after
4740 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4741 types properly. */
4742 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4744 gimplify_stmt (from_p, pre_p);
4745 gimplify_stmt (to_p, pre_p);
4746 *expr_p = NULL_TREE;
4747 return GS_ALL_DONE;
4750 /* If the value being copied is of variable width, compute the length
4751 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4752 before gimplifying any of the operands so that we can resolve any
4753 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4754 the size of the expression to be copied, not of the destination, so
4755 that is what we must do here. */
4756 maybe_with_size_expr (from_p);
4758 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4759 if (ret == GS_ERROR)
4760 return ret;
4762 /* As a special case, we have to temporarily allow for assignments
4763 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4764 a toplevel statement, when gimplifying the GENERIC expression
4765 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4766 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4768 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4769 prevent gimplify_expr from trying to create a new temporary for
4770 foo's LHS, we tell it that it should only gimplify until it
4771 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4772 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4773 and all we need to do here is set 'a' to be its LHS. */
4774 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4775 fb_rvalue);
4776 if (ret == GS_ERROR)
4777 return ret;
4779 /* Now see if the above changed *from_p to something we handle specially. */
4780 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4781 want_value);
4782 if (ret != GS_UNHANDLED)
4783 return ret;
4785 /* If we've got a variable sized assignment between two lvalues (i.e. does
4786 not involve a call), then we can make things a bit more straightforward
4787 by converting the assignment to memcpy or memset. */
4788 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4790 tree from = TREE_OPERAND (*from_p, 0);
4791 tree size = TREE_OPERAND (*from_p, 1);
4793 if (TREE_CODE (from) == CONSTRUCTOR)
4794 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4796 if (is_gimple_addressable (from))
4798 *from_p = from;
4799 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4800 pre_p);
4804 /* Transform partial stores to non-addressable complex variables into
4805 total stores. This allows us to use real instead of virtual operands
4806 for these variables, which improves optimization. */
4807 if ((TREE_CODE (*to_p) == REALPART_EXPR
4808 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4809 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4810 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4812 /* Try to alleviate the effects of the gimplification creating artificial
4813 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4814 if (!gimplify_ctxp->into_ssa
4815 && TREE_CODE (*from_p) == VAR_DECL
4816 && DECL_IGNORED_P (*from_p)
4817 && DECL_P (*to_p)
4818 && !DECL_IGNORED_P (*to_p))
4820 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4821 DECL_NAME (*from_p)
4822 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4823 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4824 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4827 if (want_value && TREE_THIS_VOLATILE (*to_p))
4828 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4830 if (TREE_CODE (*from_p) == CALL_EXPR)
4832 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4833 instead of a GIMPLE_ASSIGN. */
4834 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4835 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4836 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4837 assign = gimple_build_call_from_tree (*from_p);
4838 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4839 notice_special_calls (assign);
4840 if (!gimple_call_noreturn_p (assign))
4841 gimple_call_set_lhs (assign, *to_p);
4843 else
4845 assign = gimple_build_assign (*to_p, *from_p);
4846 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4849 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4851 /* We should have got an SSA name from the start. */
4852 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4855 gimplify_seq_add_stmt (pre_p, assign);
4856 gsi = gsi_last (*pre_p);
4857 /* Don't fold stmts inside of target construct. We'll do it
4858 during omplower pass instead. */
4859 struct gimplify_omp_ctx *ctx;
4860 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
4861 if (ctx->region_type == ORT_TARGET)
4862 break;
4863 if (ctx == NULL)
4864 fold_stmt (&gsi);
4866 if (want_value)
4868 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4869 return GS_OK;
4871 else
4872 *expr_p = NULL;
4874 return GS_ALL_DONE;
4877 /* Gimplify a comparison between two variable-sized objects. Do this
4878 with a call to BUILT_IN_MEMCMP. */
4880 static enum gimplify_status
4881 gimplify_variable_sized_compare (tree *expr_p)
4883 location_t loc = EXPR_LOCATION (*expr_p);
4884 tree op0 = TREE_OPERAND (*expr_p, 0);
4885 tree op1 = TREE_OPERAND (*expr_p, 1);
4886 tree t, arg, dest, src, expr;
4888 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4889 arg = unshare_expr (arg);
4890 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4891 src = build_fold_addr_expr_loc (loc, op1);
4892 dest = build_fold_addr_expr_loc (loc, op0);
4893 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4894 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4896 expr
4897 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4898 SET_EXPR_LOCATION (expr, loc);
4899 *expr_p = expr;
4901 return GS_OK;
4904 /* Gimplify a comparison between two aggregate objects of integral scalar
4905 mode as a comparison between the bitwise equivalent scalar values. */
4907 static enum gimplify_status
4908 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4910 location_t loc = EXPR_LOCATION (*expr_p);
4911 tree op0 = TREE_OPERAND (*expr_p, 0);
4912 tree op1 = TREE_OPERAND (*expr_p, 1);
4914 tree type = TREE_TYPE (op0);
4915 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4917 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4918 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4920 *expr_p
4921 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4923 return GS_OK;
4926 /* Gimplify an expression sequence. This function gimplifies each
4927 expression and rewrites the original expression with the last
4928 expression of the sequence in GIMPLE form.
4930 PRE_P points to the list where the side effects for all the
4931 expressions in the sequence will be emitted.
4933 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4935 static enum gimplify_status
4936 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4938 tree t = *expr_p;
4942 tree *sub_p = &TREE_OPERAND (t, 0);
4944 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4945 gimplify_compound_expr (sub_p, pre_p, false);
4946 else
4947 gimplify_stmt (sub_p, pre_p);
4949 t = TREE_OPERAND (t, 1);
4951 while (TREE_CODE (t) == COMPOUND_EXPR);
4953 *expr_p = t;
4954 if (want_value)
4955 return GS_OK;
4956 else
4958 gimplify_stmt (expr_p, pre_p);
4959 return GS_ALL_DONE;
4963 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4964 gimplify. After gimplification, EXPR_P will point to a new temporary
4965 that holds the original value of the SAVE_EXPR node.
4967 PRE_P points to the list where side effects that must happen before
4968 *EXPR_P should be stored. */
4970 static enum gimplify_status
4971 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4973 enum gimplify_status ret = GS_ALL_DONE;
4974 tree val;
4976 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4977 val = TREE_OPERAND (*expr_p, 0);
4979 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4980 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4982 /* The operand may be a void-valued expression such as SAVE_EXPRs
4983 generated by the Java frontend for class initialization. It is
4984 being executed only for its side-effects. */
4985 if (TREE_TYPE (val) == void_type_node)
4987 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4988 is_gimple_stmt, fb_none);
4989 val = NULL;
4991 else
4992 val = get_initialized_tmp_var (val, pre_p, post_p);
4994 TREE_OPERAND (*expr_p, 0) = val;
4995 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4998 *expr_p = val;
5000 return ret;
5003 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5005 unary_expr
5006 : ...
5007 | '&' varname
5010 PRE_P points to the list where side effects that must happen before
5011 *EXPR_P should be stored.
5013 POST_P points to the list where side effects that must happen after
5014 *EXPR_P should be stored. */
5016 static enum gimplify_status
5017 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5019 tree expr = *expr_p;
5020 tree op0 = TREE_OPERAND (expr, 0);
5021 enum gimplify_status ret;
5022 location_t loc = EXPR_LOCATION (*expr_p);
5024 switch (TREE_CODE (op0))
5026 case INDIRECT_REF:
5027 do_indirect_ref:
5028 /* Check if we are dealing with an expression of the form '&*ptr'.
5029 While the front end folds away '&*ptr' into 'ptr', these
5030 expressions may be generated internally by the compiler (e.g.,
5031 builtins like __builtin_va_end). */
5032 /* Caution: the silent array decomposition semantics we allow for
5033 ADDR_EXPR means we can't always discard the pair. */
5034 /* Gimplification of the ADDR_EXPR operand may drop
5035 cv-qualification conversions, so make sure we add them if
5036 needed. */
5038 tree op00 = TREE_OPERAND (op0, 0);
5039 tree t_expr = TREE_TYPE (expr);
5040 tree t_op00 = TREE_TYPE (op00);
5042 if (!useless_type_conversion_p (t_expr, t_op00))
5043 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5044 *expr_p = op00;
5045 ret = GS_OK;
5047 break;
5049 case VIEW_CONVERT_EXPR:
5050 /* Take the address of our operand and then convert it to the type of
5051 this ADDR_EXPR.
5053 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5054 all clear. The impact of this transformation is even less clear. */
5056 /* If the operand is a useless conversion, look through it. Doing so
5057 guarantees that the ADDR_EXPR and its operand will remain of the
5058 same type. */
5059 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5060 op0 = TREE_OPERAND (op0, 0);
5062 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5063 build_fold_addr_expr_loc (loc,
5064 TREE_OPERAND (op0, 0)));
5065 ret = GS_OK;
5066 break;
5068 default:
5069 /* We use fb_either here because the C frontend sometimes takes
5070 the address of a call that returns a struct; see
5071 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5072 the implied temporary explicit. */
5074 /* Make the operand addressable. */
5075 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5076 is_gimple_addressable, fb_either);
5077 if (ret == GS_ERROR)
5078 break;
5080 /* Then mark it. Beware that it may not be possible to do so directly
5081 if a temporary has been created by the gimplification. */
5082 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5084 op0 = TREE_OPERAND (expr, 0);
5086 /* For various reasons, the gimplification of the expression
5087 may have made a new INDIRECT_REF. */
5088 if (TREE_CODE (op0) == INDIRECT_REF)
5089 goto do_indirect_ref;
5091 mark_addressable (TREE_OPERAND (expr, 0));
5093 /* The FEs may end up building ADDR_EXPRs early on a decl with
5094 an incomplete type. Re-build ADDR_EXPRs in canonical form
5095 here. */
5096 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5097 *expr_p = build_fold_addr_expr (op0);
5099 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5100 recompute_tree_invariant_for_addr_expr (*expr_p);
5102 /* If we re-built the ADDR_EXPR add a conversion to the original type
5103 if required. */
5104 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5105 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5107 break;
5110 return ret;
5113 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5114 value; output operands should be a gimple lvalue. */
5116 static enum gimplify_status
5117 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5119 tree expr;
5120 int noutputs;
5121 const char **oconstraints;
5122 int i;
5123 tree link;
5124 const char *constraint;
5125 bool allows_mem, allows_reg, is_inout;
5126 enum gimplify_status ret, tret;
5127 gimple stmt;
5128 vec<tree, va_gc> *inputs;
5129 vec<tree, va_gc> *outputs;
5130 vec<tree, va_gc> *clobbers;
5131 vec<tree, va_gc> *labels;
5132 tree link_next;
5134 expr = *expr_p;
5135 noutputs = list_length (ASM_OUTPUTS (expr));
5136 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5138 inputs = NULL;
5139 outputs = NULL;
5140 clobbers = NULL;
5141 labels = NULL;
5143 ret = GS_ALL_DONE;
5144 link_next = NULL_TREE;
5145 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5147 bool ok;
5148 size_t constraint_len;
5150 link_next = TREE_CHAIN (link);
5152 oconstraints[i]
5153 = constraint
5154 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5155 constraint_len = strlen (constraint);
5156 if (constraint_len == 0)
5157 continue;
5159 ok = parse_output_constraint (&constraint, i, 0, 0,
5160 &allows_mem, &allows_reg, &is_inout);
5161 if (!ok)
5163 ret = GS_ERROR;
5164 is_inout = false;
5167 if (!allows_reg && allows_mem)
5168 mark_addressable (TREE_VALUE (link));
5170 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5171 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5172 fb_lvalue | fb_mayfail);
5173 if (tret == GS_ERROR)
5175 error ("invalid lvalue in asm output %d", i);
5176 ret = tret;
5179 vec_safe_push (outputs, link);
5180 TREE_CHAIN (link) = NULL_TREE;
5182 if (is_inout)
5184 /* An input/output operand. To give the optimizers more
5185 flexibility, split it into separate input and output
5186 operands. */
5187 tree input;
5188 char buf[10];
5190 /* Turn the in/out constraint into an output constraint. */
5191 char *p = xstrdup (constraint);
5192 p[0] = '=';
5193 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5195 /* And add a matching input constraint. */
5196 if (allows_reg)
5198 sprintf (buf, "%d", i);
5200 /* If there are multiple alternatives in the constraint,
5201 handle each of them individually. Those that allow register
5202 will be replaced with operand number, the others will stay
5203 unchanged. */
5204 if (strchr (p, ',') != NULL)
5206 size_t len = 0, buflen = strlen (buf);
5207 char *beg, *end, *str, *dst;
5209 for (beg = p + 1;;)
5211 end = strchr (beg, ',');
5212 if (end == NULL)
5213 end = strchr (beg, '\0');
5214 if ((size_t) (end - beg) < buflen)
5215 len += buflen + 1;
5216 else
5217 len += end - beg + 1;
5218 if (*end)
5219 beg = end + 1;
5220 else
5221 break;
5224 str = (char *) alloca (len);
5225 for (beg = p + 1, dst = str;;)
5227 const char *tem;
5228 bool mem_p, reg_p, inout_p;
5230 end = strchr (beg, ',');
5231 if (end)
5232 *end = '\0';
5233 beg[-1] = '=';
5234 tem = beg - 1;
5235 parse_output_constraint (&tem, i, 0, 0,
5236 &mem_p, &reg_p, &inout_p);
5237 if (dst != str)
5238 *dst++ = ',';
5239 if (reg_p)
5241 memcpy (dst, buf, buflen);
5242 dst += buflen;
5244 else
5246 if (end)
5247 len = end - beg;
5248 else
5249 len = strlen (beg);
5250 memcpy (dst, beg, len);
5251 dst += len;
5253 if (end)
5254 beg = end + 1;
5255 else
5256 break;
5258 *dst = '\0';
5259 input = build_string (dst - str, str);
5261 else
5262 input = build_string (strlen (buf), buf);
5264 else
5265 input = build_string (constraint_len - 1, constraint + 1);
5267 free (p);
5269 input = build_tree_list (build_tree_list (NULL_TREE, input),
5270 unshare_expr (TREE_VALUE (link)));
5271 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5275 link_next = NULL_TREE;
5276 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5278 link_next = TREE_CHAIN (link);
5279 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5280 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5281 oconstraints, &allows_mem, &allows_reg);
5283 /* If we can't make copies, we can only accept memory. */
5284 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5286 if (allows_mem)
5287 allows_reg = 0;
5288 else
5290 error ("impossible constraint in %<asm%>");
5291 error ("non-memory input %d must stay in memory", i);
5292 return GS_ERROR;
5296 /* If the operand is a memory input, it should be an lvalue. */
5297 if (!allows_reg && allows_mem)
5299 tree inputv = TREE_VALUE (link);
5300 STRIP_NOPS (inputv);
5301 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5302 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5303 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5304 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5305 TREE_VALUE (link) = error_mark_node;
5306 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5307 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5308 mark_addressable (TREE_VALUE (link));
5309 if (tret == GS_ERROR)
5311 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5312 input_location = EXPR_LOCATION (TREE_VALUE (link));
5313 error ("memory input %d is not directly addressable", i);
5314 ret = tret;
5317 else
5319 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5320 is_gimple_asm_val, fb_rvalue);
5321 if (tret == GS_ERROR)
5322 ret = tret;
5325 TREE_CHAIN (link) = NULL_TREE;
5326 vec_safe_push (inputs, link);
5329 link_next = NULL_TREE;
5330 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5332 link_next = TREE_CHAIN (link);
5333 TREE_CHAIN (link) = NULL_TREE;
5334 vec_safe_push (clobbers, link);
5337 link_next = NULL_TREE;
5338 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5340 link_next = TREE_CHAIN (link);
5341 TREE_CHAIN (link) = NULL_TREE;
5342 vec_safe_push (labels, link);
5345 /* Do not add ASMs with errors to the gimple IL stream. */
5346 if (ret != GS_ERROR)
5348 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5349 inputs, outputs, clobbers, labels);
5351 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5352 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5354 gimplify_seq_add_stmt (pre_p, stmt);
5357 return ret;
5360 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5361 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5362 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5363 return to this function.
5365 FIXME should we complexify the prequeue handling instead? Or use flags
5366 for all the cleanups and let the optimizer tighten them up? The current
5367 code seems pretty fragile; it will break on a cleanup within any
5368 non-conditional nesting. But any such nesting would be broken, anyway;
5369 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5370 and continues out of it. We can do that at the RTL level, though, so
5371 having an optimizer to tighten up try/finally regions would be a Good
5372 Thing. */
5374 static enum gimplify_status
5375 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5377 gimple_stmt_iterator iter;
5378 gimple_seq body_sequence = NULL;
5380 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5382 /* We only care about the number of conditions between the innermost
5383 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5384 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5385 int old_conds = gimplify_ctxp->conditions;
5386 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5387 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5388 gimplify_ctxp->conditions = 0;
5389 gimplify_ctxp->conditional_cleanups = NULL;
5390 gimplify_ctxp->in_cleanup_point_expr = true;
5392 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5394 gimplify_ctxp->conditions = old_conds;
5395 gimplify_ctxp->conditional_cleanups = old_cleanups;
5396 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5398 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5400 gimple wce = gsi_stmt (iter);
5402 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5404 if (gsi_one_before_end_p (iter))
5406 /* Note that gsi_insert_seq_before and gsi_remove do not
5407 scan operands, unlike some other sequence mutators. */
5408 if (!gimple_wce_cleanup_eh_only (wce))
5409 gsi_insert_seq_before_without_update (&iter,
5410 gimple_wce_cleanup (wce),
5411 GSI_SAME_STMT);
5412 gsi_remove (&iter, true);
5413 break;
5415 else
5417 gimple gtry;
5418 gimple_seq seq;
5419 enum gimple_try_flags kind;
5421 if (gimple_wce_cleanup_eh_only (wce))
5422 kind = GIMPLE_TRY_CATCH;
5423 else
5424 kind = GIMPLE_TRY_FINALLY;
5425 seq = gsi_split_seq_after (iter);
5427 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5428 /* Do not use gsi_replace here, as it may scan operands.
5429 We want to do a simple structural modification only. */
5430 gsi_set_stmt (&iter, gtry);
5431 iter = gsi_start (gtry->gimple_try.eval);
5434 else
5435 gsi_next (&iter);
5438 gimplify_seq_add_seq (pre_p, body_sequence);
5439 if (temp)
5441 *expr_p = temp;
5442 return GS_OK;
5444 else
5446 *expr_p = NULL;
5447 return GS_ALL_DONE;
5451 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5452 is the cleanup action required. EH_ONLY is true if the cleanup should
5453 only be executed if an exception is thrown, not on normal exit. */
5455 static void
5456 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5458 gimple wce;
5459 gimple_seq cleanup_stmts = NULL;
5461 /* Errors can result in improperly nested cleanups. Which results in
5462 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5463 if (seen_error ())
5464 return;
5466 if (gimple_conditional_context ())
5468 /* If we're in a conditional context, this is more complex. We only
5469 want to run the cleanup if we actually ran the initialization that
5470 necessitates it, but we want to run it after the end of the
5471 conditional context. So we wrap the try/finally around the
5472 condition and use a flag to determine whether or not to actually
5473 run the destructor. Thus
5475 test ? f(A()) : 0
5477 becomes (approximately)
5479 flag = 0;
5480 try {
5481 if (test) { A::A(temp); flag = 1; val = f(temp); }
5482 else { val = 0; }
5483 } finally {
5484 if (flag) A::~A(temp);
5488 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5489 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5490 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5492 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5493 gimplify_stmt (&cleanup, &cleanup_stmts);
5494 wce = gimple_build_wce (cleanup_stmts);
5496 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5497 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5498 gimplify_seq_add_stmt (pre_p, ftrue);
5500 /* Because of this manipulation, and the EH edges that jump
5501 threading cannot redirect, the temporary (VAR) will appear
5502 to be used uninitialized. Don't warn. */
5503 TREE_NO_WARNING (var) = 1;
5505 else
5507 gimplify_stmt (&cleanup, &cleanup_stmts);
5508 wce = gimple_build_wce (cleanup_stmts);
5509 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5510 gimplify_seq_add_stmt (pre_p, wce);
5514 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5516 static enum gimplify_status
5517 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5519 tree targ = *expr_p;
5520 tree temp = TARGET_EXPR_SLOT (targ);
5521 tree init = TARGET_EXPR_INITIAL (targ);
5522 enum gimplify_status ret;
5524 if (init)
5526 tree cleanup = NULL_TREE;
5528 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5529 to the temps list. Handle also variable length TARGET_EXPRs. */
5530 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5532 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5533 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5534 gimplify_vla_decl (temp, pre_p);
5536 else
5537 gimple_add_tmp_var (temp);
5539 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5540 expression is supposed to initialize the slot. */
5541 if (VOID_TYPE_P (TREE_TYPE (init)))
5542 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5543 else
5545 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5546 init = init_expr;
5547 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5548 init = NULL;
5549 ggc_free (init_expr);
5551 if (ret == GS_ERROR)
5553 /* PR c++/28266 Make sure this is expanded only once. */
5554 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5555 return GS_ERROR;
5557 if (init)
5558 gimplify_and_add (init, pre_p);
5560 /* If needed, push the cleanup for the temp. */
5561 if (TARGET_EXPR_CLEANUP (targ))
5563 if (CLEANUP_EH_ONLY (targ))
5564 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5565 CLEANUP_EH_ONLY (targ), pre_p);
5566 else
5567 cleanup = TARGET_EXPR_CLEANUP (targ);
5570 /* Add a clobber for the temporary going out of scope, like
5571 gimplify_bind_expr. */
5572 if (gimplify_ctxp->in_cleanup_point_expr
5573 && needs_to_live_in_memory (temp)
5574 && flag_stack_reuse == SR_ALL)
5576 tree clobber = build_constructor (TREE_TYPE (temp),
5577 NULL);
5578 TREE_THIS_VOLATILE (clobber) = true;
5579 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5580 if (cleanup)
5581 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5582 clobber);
5583 else
5584 cleanup = clobber;
5587 if (cleanup)
5588 gimple_push_cleanup (temp, cleanup, false, pre_p);
5590 /* Only expand this once. */
5591 TREE_OPERAND (targ, 3) = init;
5592 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5594 else
5595 /* We should have expanded this before. */
5596 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5598 *expr_p = temp;
5599 return GS_OK;
5602 /* Gimplification of expression trees. */
5604 /* Gimplify an expression which appears at statement context. The
5605 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5606 NULL, a new sequence is allocated.
5608 Return true if we actually added a statement to the queue. */
5610 bool
5611 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5613 gimple_seq_node last;
5615 last = gimple_seq_last (*seq_p);
5616 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5617 return last != gimple_seq_last (*seq_p);
5620 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5621 to CTX. If entries already exist, force them to be some flavor of private.
5622 If there is no enclosing parallel, do nothing. */
5624 void
5625 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5627 splay_tree_node n;
5629 if (decl == NULL || !DECL_P (decl))
5630 return;
5634 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5635 if (n != NULL)
5637 if (n->value & GOVD_SHARED)
5638 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5639 else if (n->value & GOVD_MAP)
5640 n->value |= GOVD_MAP_TO_ONLY;
5641 else
5642 return;
5644 else if (ctx->region_type == ORT_TARGET)
5645 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5646 else if (ctx->region_type != ORT_WORKSHARE
5647 && ctx->region_type != ORT_SIMD
5648 && ctx->region_type != ORT_TARGET_DATA)
5649 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5651 ctx = ctx->outer_context;
5653 while (ctx);
5656 /* Similarly for each of the type sizes of TYPE. */
5658 static void
5659 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5661 if (type == NULL || type == error_mark_node)
5662 return;
5663 type = TYPE_MAIN_VARIANT (type);
5665 if (pointer_set_insert (ctx->privatized_types, type))
5666 return;
5668 switch (TREE_CODE (type))
5670 case INTEGER_TYPE:
5671 case ENUMERAL_TYPE:
5672 case BOOLEAN_TYPE:
5673 case REAL_TYPE:
5674 case FIXED_POINT_TYPE:
5675 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5676 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5677 break;
5679 case ARRAY_TYPE:
5680 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5681 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5682 break;
5684 case RECORD_TYPE:
5685 case UNION_TYPE:
5686 case QUAL_UNION_TYPE:
5688 tree field;
5689 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5690 if (TREE_CODE (field) == FIELD_DECL)
5692 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5693 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5696 break;
5698 case POINTER_TYPE:
5699 case REFERENCE_TYPE:
5700 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5701 break;
5703 default:
5704 break;
5707 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5708 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5709 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5712 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5714 static void
5715 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5717 splay_tree_node n;
5718 unsigned int nflags;
5719 tree t;
5721 if (error_operand_p (decl))
5722 return;
5724 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5725 there are constructors involved somewhere. */
5726 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5727 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5728 flags |= GOVD_SEEN;
5730 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5731 if (n != NULL && n->value != GOVD_ALIGNED)
5733 /* We shouldn't be re-adding the decl with the same data
5734 sharing class. */
5735 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5736 /* The only combination of data sharing classes we should see is
5737 FIRSTPRIVATE and LASTPRIVATE. */
5738 nflags = n->value | flags;
5739 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5740 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5741 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5742 n->value = nflags;
5743 return;
5746 /* When adding a variable-sized variable, we have to handle all sorts
5747 of additional bits of data: the pointer replacement variable, and
5748 the parameters of the type. */
5749 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5751 /* Add the pointer replacement variable as PRIVATE if the variable
5752 replacement is private, else FIRSTPRIVATE since we'll need the
5753 address of the original variable either for SHARED, or for the
5754 copy into or out of the context. */
5755 if (!(flags & GOVD_LOCAL))
5757 nflags = flags & GOVD_MAP
5758 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5759 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5760 nflags |= flags & GOVD_SEEN;
5761 t = DECL_VALUE_EXPR (decl);
5762 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5763 t = TREE_OPERAND (t, 0);
5764 gcc_assert (DECL_P (t));
5765 omp_add_variable (ctx, t, nflags);
5768 /* Add all of the variable and type parameters (which should have
5769 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5770 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5771 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5772 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5774 /* The variable-sized variable itself is never SHARED, only some form
5775 of PRIVATE. The sharing would take place via the pointer variable
5776 which we remapped above. */
5777 if (flags & GOVD_SHARED)
5778 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5779 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5781 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5782 alloca statement we generate for the variable, so make sure it
5783 is available. This isn't automatically needed for the SHARED
5784 case, since we won't be allocating local storage then.
5785 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5786 in this case omp_notice_variable will be called later
5787 on when it is gimplified. */
5788 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5789 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5790 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5792 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5793 && lang_hooks.decls.omp_privatize_by_reference (decl))
5795 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5797 /* Similar to the direct variable sized case above, we'll need the
5798 size of references being privatized. */
5799 if ((flags & GOVD_SHARED) == 0)
5801 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5802 if (TREE_CODE (t) != INTEGER_CST)
5803 omp_notice_variable (ctx, t, true);
5807 if (n != NULL)
5808 n->value |= flags;
5809 else
5810 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5813 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5814 This just prints out diagnostics about threadprivate variable uses
5815 in untied tasks. If DECL2 is non-NULL, prevent this warning
5816 on that variable. */
5818 static bool
5819 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5820 tree decl2)
5822 splay_tree_node n;
5823 struct gimplify_omp_ctx *octx;
5825 for (octx = ctx; octx; octx = octx->outer_context)
5826 if (octx->region_type == ORT_TARGET)
5828 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5829 if (n == NULL)
5831 error ("threadprivate variable %qE used in target region",
5832 DECL_NAME (decl));
5833 error_at (octx->location, "enclosing target region");
5834 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5836 if (decl2)
5837 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5840 if (ctx->region_type != ORT_UNTIED_TASK)
5841 return false;
5842 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5843 if (n == NULL)
5845 error ("threadprivate variable %qE used in untied task",
5846 DECL_NAME (decl));
5847 error_at (ctx->location, "enclosing task");
5848 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5850 if (decl2)
5851 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5852 return false;
5855 /* Record the fact that DECL was used within the OpenMP context CTX.
5856 IN_CODE is true when real code uses DECL, and false when we should
5857 merely emit default(none) errors. Return true if DECL is going to
5858 be remapped and thus DECL shouldn't be gimplified into its
5859 DECL_VALUE_EXPR (if any). */
5861 static bool
5862 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5864 splay_tree_node n;
5865 unsigned flags = in_code ? GOVD_SEEN : 0;
5866 bool ret = false, shared;
5868 if (error_operand_p (decl))
5869 return false;
5871 /* Threadprivate variables are predetermined. */
5872 if (is_global_var (decl))
5874 if (DECL_THREAD_LOCAL_P (decl))
5875 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5877 if (DECL_HAS_VALUE_EXPR_P (decl))
5879 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5881 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5882 return omp_notice_threadprivate_variable (ctx, decl, value);
5886 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5887 if (ctx->region_type == ORT_TARGET)
5889 if (n == NULL)
5891 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5893 error ("%qD referenced in target region does not have "
5894 "a mappable type", decl);
5895 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5897 else
5898 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5900 else
5901 n->value |= flags;
5902 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5903 goto do_outer;
5906 if (n == NULL)
5908 enum omp_clause_default_kind default_kind, kind;
5909 struct gimplify_omp_ctx *octx;
5911 if (ctx->region_type == ORT_WORKSHARE
5912 || ctx->region_type == ORT_SIMD
5913 || ctx->region_type == ORT_TARGET_DATA)
5914 goto do_outer;
5916 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5917 remapped firstprivate instead of shared. To some extent this is
5918 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5919 default_kind = ctx->default_kind;
5920 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5921 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5922 default_kind = kind;
5924 switch (default_kind)
5926 case OMP_CLAUSE_DEFAULT_NONE:
5927 if ((ctx->region_type & ORT_TASK) != 0)
5929 error ("%qE not specified in enclosing task",
5930 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5931 error_at (ctx->location, "enclosing task");
5933 else if (ctx->region_type == ORT_TEAMS)
5935 error ("%qE not specified in enclosing teams construct",
5936 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5937 error_at (ctx->location, "enclosing teams construct");
5939 else
5941 error ("%qE not specified in enclosing parallel",
5942 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5943 error_at (ctx->location, "enclosing parallel");
5945 /* FALLTHRU */
5946 case OMP_CLAUSE_DEFAULT_SHARED:
5947 flags |= GOVD_SHARED;
5948 break;
5949 case OMP_CLAUSE_DEFAULT_PRIVATE:
5950 flags |= GOVD_PRIVATE;
5951 break;
5952 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5953 flags |= GOVD_FIRSTPRIVATE;
5954 break;
5955 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5956 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5957 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5958 if (ctx->outer_context)
5959 omp_notice_variable (ctx->outer_context, decl, in_code);
5960 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5962 splay_tree_node n2;
5964 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
5965 continue;
5966 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5967 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5969 flags |= GOVD_FIRSTPRIVATE;
5970 break;
5972 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
5973 break;
5975 if (flags & GOVD_FIRSTPRIVATE)
5976 break;
5977 if (octx == NULL
5978 && (TREE_CODE (decl) == PARM_DECL
5979 || (!is_global_var (decl)
5980 && DECL_CONTEXT (decl) == current_function_decl)))
5982 flags |= GOVD_FIRSTPRIVATE;
5983 break;
5985 flags |= GOVD_SHARED;
5986 break;
5987 default:
5988 gcc_unreachable ();
5991 if ((flags & GOVD_PRIVATE)
5992 && lang_hooks.decls.omp_private_outer_ref (decl))
5993 flags |= GOVD_PRIVATE_OUTER_REF;
5995 omp_add_variable (ctx, decl, flags);
5997 shared = (flags & GOVD_SHARED) != 0;
5998 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5999 goto do_outer;
6002 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6003 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6004 && DECL_SIZE (decl)
6005 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6007 splay_tree_node n2;
6008 tree t = DECL_VALUE_EXPR (decl);
6009 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6010 t = TREE_OPERAND (t, 0);
6011 gcc_assert (DECL_P (t));
6012 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6013 n2->value |= GOVD_SEEN;
6016 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6017 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6019 /* If nothing changed, there's nothing left to do. */
6020 if ((n->value & flags) == flags)
6021 return ret;
6022 flags |= n->value;
6023 n->value = flags;
6025 do_outer:
6026 /* If the variable is private in the current context, then we don't
6027 need to propagate anything to an outer context. */
6028 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6029 return ret;
6030 if (ctx->outer_context
6031 && omp_notice_variable (ctx->outer_context, decl, in_code))
6032 return true;
6033 return ret;
6036 /* Verify that DECL is private within CTX. If there's specific information
6037 to the contrary in the innermost scope, generate an error. */
6039 static bool
6040 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
6042 splay_tree_node n;
6044 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6045 if (n != NULL)
6047 if (n->value & GOVD_SHARED)
6049 if (ctx == gimplify_omp_ctxp)
6051 if (simd)
6052 error ("iteration variable %qE is predetermined linear",
6053 DECL_NAME (decl));
6054 else
6055 error ("iteration variable %qE should be private",
6056 DECL_NAME (decl));
6057 n->value = GOVD_PRIVATE;
6058 return true;
6060 else
6061 return false;
6063 else if ((n->value & GOVD_EXPLICIT) != 0
6064 && (ctx == gimplify_omp_ctxp
6065 || (ctx->region_type == ORT_COMBINED_PARALLEL
6066 && gimplify_omp_ctxp->outer_context == ctx)))
6068 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6069 error ("iteration variable %qE should not be firstprivate",
6070 DECL_NAME (decl));
6071 else if ((n->value & GOVD_REDUCTION) != 0)
6072 error ("iteration variable %qE should not be reduction",
6073 DECL_NAME (decl));
6074 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
6075 error ("iteration variable %qE should not be lastprivate",
6076 DECL_NAME (decl));
6077 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6078 error ("iteration variable %qE should not be private",
6079 DECL_NAME (decl));
6080 else if (simd && (n->value & GOVD_LINEAR) != 0)
6081 error ("iteration variable %qE is predetermined linear",
6082 DECL_NAME (decl));
6084 return (ctx == gimplify_omp_ctxp
6085 || (ctx->region_type == ORT_COMBINED_PARALLEL
6086 && gimplify_omp_ctxp->outer_context == ctx));
6089 if (ctx->region_type != ORT_WORKSHARE
6090 && ctx->region_type != ORT_SIMD)
6091 return false;
6092 else if (ctx->outer_context)
6093 return omp_is_private (ctx->outer_context, decl, simd);
6094 return false;
6097 /* Return true if DECL is private within a parallel region
6098 that binds to the current construct's context or in parallel
6099 region's REDUCTION clause. */
6101 static bool
6102 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6104 splay_tree_node n;
6108 ctx = ctx->outer_context;
6109 if (ctx == NULL)
6110 return !(is_global_var (decl)
6111 /* References might be private, but might be shared too. */
6112 || lang_hooks.decls.omp_privatize_by_reference (decl));
6114 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6115 continue;
6117 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6118 if (n != NULL)
6119 return (n->value & GOVD_SHARED) == 0;
6121 while (ctx->region_type == ORT_WORKSHARE
6122 || ctx->region_type == ORT_SIMD);
6123 return false;
6126 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6127 and previous omp contexts. */
6129 static void
6130 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6131 enum omp_region_type region_type)
6133 struct gimplify_omp_ctx *ctx, *outer_ctx;
6134 struct gimplify_ctx gctx;
6135 tree c;
6137 ctx = new_omp_context (region_type);
6138 outer_ctx = ctx->outer_context;
6140 while ((c = *list_p) != NULL)
6142 bool remove = false;
6143 bool notice_outer = true;
6144 const char *check_non_private = NULL;
6145 unsigned int flags;
6146 tree decl;
6148 switch (OMP_CLAUSE_CODE (c))
6150 case OMP_CLAUSE_PRIVATE:
6151 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6152 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6154 flags |= GOVD_PRIVATE_OUTER_REF;
6155 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6157 else
6158 notice_outer = false;
6159 goto do_add;
6160 case OMP_CLAUSE_SHARED:
6161 flags = GOVD_SHARED | GOVD_EXPLICIT;
6162 goto do_add;
6163 case OMP_CLAUSE_FIRSTPRIVATE:
6164 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6165 check_non_private = "firstprivate";
6166 goto do_add;
6167 case OMP_CLAUSE_LASTPRIVATE:
6168 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6169 check_non_private = "lastprivate";
6170 goto do_add;
6171 case OMP_CLAUSE_REDUCTION:
6172 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6173 check_non_private = "reduction";
6174 goto do_add;
6175 case OMP_CLAUSE_LINEAR:
6176 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6177 is_gimple_val, fb_rvalue) == GS_ERROR)
6179 remove = true;
6180 break;
6182 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6183 goto do_add;
6185 case OMP_CLAUSE_MAP:
6186 if (OMP_CLAUSE_SIZE (c)
6187 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6188 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6190 remove = true;
6191 break;
6193 decl = OMP_CLAUSE_DECL (c);
6194 if (!DECL_P (decl))
6196 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6197 NULL, is_gimple_lvalue, fb_lvalue)
6198 == GS_ERROR)
6200 remove = true;
6201 break;
6203 break;
6205 flags = GOVD_MAP | GOVD_EXPLICIT;
6206 goto do_add;
6208 case OMP_CLAUSE_DEPEND:
6209 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6211 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6212 NULL, is_gimple_val, fb_rvalue);
6213 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6215 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6217 remove = true;
6218 break;
6220 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6221 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6222 is_gimple_val, fb_rvalue) == GS_ERROR)
6224 remove = true;
6225 break;
6227 break;
6229 case OMP_CLAUSE_TO:
6230 case OMP_CLAUSE_FROM:
6231 if (OMP_CLAUSE_SIZE (c)
6232 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6233 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6235 remove = true;
6236 break;
6238 decl = OMP_CLAUSE_DECL (c);
6239 if (error_operand_p (decl))
6241 remove = true;
6242 break;
6244 if (!DECL_P (decl))
6246 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6247 NULL, is_gimple_lvalue, fb_lvalue)
6248 == GS_ERROR)
6250 remove = true;
6251 break;
6253 break;
6255 goto do_notice;
6257 do_add:
6258 decl = OMP_CLAUSE_DECL (c);
6259 if (error_operand_p (decl))
6261 remove = true;
6262 break;
6264 omp_add_variable (ctx, decl, flags);
6265 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6266 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6268 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6269 GOVD_LOCAL | GOVD_SEEN);
6270 gimplify_omp_ctxp = ctx;
6271 push_gimplify_context (&gctx);
6273 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6274 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6276 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6277 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6278 pop_gimplify_context
6279 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6280 push_gimplify_context (&gctx);
6281 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6282 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6283 pop_gimplify_context
6284 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6285 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6286 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6288 gimplify_omp_ctxp = outer_ctx;
6290 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6291 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6293 gimplify_omp_ctxp = ctx;
6294 push_gimplify_context (&gctx);
6295 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6297 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6298 NULL, NULL);
6299 TREE_SIDE_EFFECTS (bind) = 1;
6300 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6301 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6303 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6304 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6305 pop_gimplify_context
6306 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6307 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6309 gimplify_omp_ctxp = outer_ctx;
6311 if (notice_outer)
6312 goto do_notice;
6313 break;
6315 case OMP_CLAUSE_COPYIN:
6316 case OMP_CLAUSE_COPYPRIVATE:
6317 decl = OMP_CLAUSE_DECL (c);
6318 if (error_operand_p (decl))
6320 remove = true;
6321 break;
6323 do_notice:
6324 if (outer_ctx)
6325 omp_notice_variable (outer_ctx, decl, true);
6326 if (check_non_private
6327 && region_type == ORT_WORKSHARE
6328 && omp_check_private (ctx, decl))
6330 error ("%s variable %qE is private in outer context",
6331 check_non_private, DECL_NAME (decl));
6332 remove = true;
6334 break;
6336 case OMP_CLAUSE_FINAL:
6337 case OMP_CLAUSE_IF:
6338 OMP_CLAUSE_OPERAND (c, 0)
6339 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6340 /* Fall through. */
6342 case OMP_CLAUSE_SCHEDULE:
6343 case OMP_CLAUSE_NUM_THREADS:
6344 case OMP_CLAUSE_NUM_TEAMS:
6345 case OMP_CLAUSE_THREAD_LIMIT:
6346 case OMP_CLAUSE_DIST_SCHEDULE:
6347 case OMP_CLAUSE_DEVICE:
6348 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6349 is_gimple_val, fb_rvalue) == GS_ERROR)
6350 remove = true;
6351 break;
6353 case OMP_CLAUSE_NOWAIT:
6354 case OMP_CLAUSE_ORDERED:
6355 case OMP_CLAUSE_UNTIED:
6356 case OMP_CLAUSE_COLLAPSE:
6357 case OMP_CLAUSE_MERGEABLE:
6358 case OMP_CLAUSE_PROC_BIND:
6359 case OMP_CLAUSE_SAFELEN:
6360 break;
6362 case OMP_CLAUSE_ALIGNED:
6363 decl = OMP_CLAUSE_DECL (c);
6364 if (error_operand_p (decl))
6366 remove = true;
6367 break;
6369 if (!is_global_var (decl)
6370 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6371 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6372 break;
6374 case OMP_CLAUSE_DEFAULT:
6375 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6376 break;
6378 default:
6379 gcc_unreachable ();
6382 if (remove)
6383 *list_p = OMP_CLAUSE_CHAIN (c);
6384 else
6385 list_p = &OMP_CLAUSE_CHAIN (c);
6388 gimplify_omp_ctxp = ctx;
6391 /* For all variables that were not actually used within the context,
6392 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6394 static int
6395 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6397 tree *list_p = (tree *) data;
6398 tree decl = (tree) n->key;
6399 unsigned flags = n->value;
6400 enum omp_clause_code code;
6401 tree clause;
6402 bool private_debug;
6404 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6405 return 0;
6406 if ((flags & GOVD_SEEN) == 0)
6407 return 0;
6408 if (flags & GOVD_DEBUG_PRIVATE)
6410 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6411 private_debug = true;
6413 else if (flags & GOVD_MAP)
6414 private_debug = false;
6415 else
6416 private_debug
6417 = lang_hooks.decls.omp_private_debug_clause (decl,
6418 !!(flags & GOVD_SHARED));
6419 if (private_debug)
6420 code = OMP_CLAUSE_PRIVATE;
6421 else if (flags & GOVD_MAP)
6422 code = OMP_CLAUSE_MAP;
6423 else if (flags & GOVD_SHARED)
6425 if (is_global_var (decl))
6427 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6428 while (ctx != NULL)
6430 splay_tree_node on
6431 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6432 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6433 | GOVD_PRIVATE | GOVD_REDUCTION
6434 | GOVD_LINEAR)) != 0)
6435 break;
6436 ctx = ctx->outer_context;
6438 if (ctx == NULL)
6439 return 0;
6441 code = OMP_CLAUSE_SHARED;
6443 else if (flags & GOVD_PRIVATE)
6444 code = OMP_CLAUSE_PRIVATE;
6445 else if (flags & GOVD_FIRSTPRIVATE)
6446 code = OMP_CLAUSE_FIRSTPRIVATE;
6447 else if (flags & GOVD_LASTPRIVATE)
6448 code = OMP_CLAUSE_LASTPRIVATE;
6449 else if (flags & GOVD_ALIGNED)
6450 return 0;
6451 else
6452 gcc_unreachable ();
6454 clause = build_omp_clause (input_location, code);
6455 OMP_CLAUSE_DECL (clause) = decl;
6456 OMP_CLAUSE_CHAIN (clause) = *list_p;
6457 if (private_debug)
6458 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6459 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6460 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6461 else if (code == OMP_CLAUSE_MAP)
6463 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6464 ? OMP_CLAUSE_MAP_TO
6465 : OMP_CLAUSE_MAP_TOFROM;
6466 if (DECL_SIZE (decl)
6467 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6469 tree decl2 = DECL_VALUE_EXPR (decl);
6470 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6471 decl2 = TREE_OPERAND (decl2, 0);
6472 gcc_assert (DECL_P (decl2));
6473 tree mem = build_simple_mem_ref (decl2);
6474 OMP_CLAUSE_DECL (clause) = mem;
6475 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6476 if (gimplify_omp_ctxp->outer_context)
6478 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6479 omp_notice_variable (ctx, decl2, true);
6480 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6482 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6483 OMP_CLAUSE_MAP);
6484 OMP_CLAUSE_DECL (nc) = decl;
6485 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6486 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6487 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6488 OMP_CLAUSE_CHAIN (clause) = nc;
6491 *list_p = clause;
6492 lang_hooks.decls.omp_finish_clause (clause);
6494 return 0;
6497 static void
6498 gimplify_adjust_omp_clauses (tree *list_p)
6500 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6501 tree c, decl;
6503 while ((c = *list_p) != NULL)
6505 splay_tree_node n;
6506 bool remove = false;
6508 switch (OMP_CLAUSE_CODE (c))
6510 case OMP_CLAUSE_PRIVATE:
6511 case OMP_CLAUSE_SHARED:
6512 case OMP_CLAUSE_FIRSTPRIVATE:
6513 case OMP_CLAUSE_LINEAR:
6514 decl = OMP_CLAUSE_DECL (c);
6515 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6516 remove = !(n->value & GOVD_SEEN);
6517 if (! remove)
6519 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6520 if ((n->value & GOVD_DEBUG_PRIVATE)
6521 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6523 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6524 || ((n->value & GOVD_DATA_SHARE_CLASS)
6525 == GOVD_PRIVATE));
6526 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6527 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6529 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6530 && ctx->outer_context
6531 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6532 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6533 && !is_global_var (decl))
6535 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6537 n = splay_tree_lookup (ctx->outer_context->variables,
6538 (splay_tree_key) decl);
6539 if (n == NULL
6540 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6542 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6543 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6544 if (n == NULL)
6545 omp_add_variable (ctx->outer_context, decl,
6546 flags | GOVD_SEEN);
6547 else
6548 n->value |= flags | GOVD_SEEN;
6551 else
6552 omp_notice_variable (ctx->outer_context, decl, true);
6555 break;
6557 case OMP_CLAUSE_LASTPRIVATE:
6558 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6559 accurately reflect the presence of a FIRSTPRIVATE clause. */
6560 decl = OMP_CLAUSE_DECL (c);
6561 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6562 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6563 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6564 break;
6566 case OMP_CLAUSE_ALIGNED:
6567 decl = OMP_CLAUSE_DECL (c);
6568 if (!is_global_var (decl))
6570 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6571 remove = n == NULL || !(n->value & GOVD_SEEN);
6572 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6574 struct gimplify_omp_ctx *octx;
6575 if (n != NULL
6576 && (n->value & (GOVD_DATA_SHARE_CLASS
6577 & ~GOVD_FIRSTPRIVATE)))
6578 remove = true;
6579 else
6580 for (octx = ctx->outer_context; octx;
6581 octx = octx->outer_context)
6583 n = splay_tree_lookup (octx->variables,
6584 (splay_tree_key) decl);
6585 if (n == NULL)
6586 continue;
6587 if (n->value & GOVD_LOCAL)
6588 break;
6589 /* We have to avoid assigning a shared variable
6590 to itself when trying to add
6591 __builtin_assume_aligned. */
6592 if (n->value & GOVD_SHARED)
6594 remove = true;
6595 break;
6600 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6602 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6603 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6604 remove = true;
6606 break;
6608 case OMP_CLAUSE_MAP:
6609 decl = OMP_CLAUSE_DECL (c);
6610 if (!DECL_P (decl))
6611 break;
6612 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6613 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6614 remove = true;
6615 else if (DECL_SIZE (decl)
6616 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6617 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6619 tree decl2 = DECL_VALUE_EXPR (decl);
6620 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6621 decl2 = TREE_OPERAND (decl2, 0);
6622 gcc_assert (DECL_P (decl2));
6623 tree mem = build_simple_mem_ref (decl2);
6624 OMP_CLAUSE_DECL (c) = mem;
6625 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6626 if (ctx->outer_context)
6628 omp_notice_variable (ctx->outer_context, decl2, true);
6629 omp_notice_variable (ctx->outer_context,
6630 OMP_CLAUSE_SIZE (c), true);
6632 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6633 OMP_CLAUSE_MAP);
6634 OMP_CLAUSE_DECL (nc) = decl;
6635 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6636 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6637 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6638 OMP_CLAUSE_CHAIN (c) = nc;
6639 c = nc;
6641 break;
6643 case OMP_CLAUSE_TO:
6644 case OMP_CLAUSE_FROM:
6645 decl = OMP_CLAUSE_DECL (c);
6646 if (!DECL_P (decl))
6647 break;
6648 if (DECL_SIZE (decl)
6649 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6651 tree decl2 = DECL_VALUE_EXPR (decl);
6652 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6653 decl2 = TREE_OPERAND (decl2, 0);
6654 gcc_assert (DECL_P (decl2));
6655 tree mem = build_simple_mem_ref (decl2);
6656 OMP_CLAUSE_DECL (c) = mem;
6657 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6658 if (ctx->outer_context)
6660 omp_notice_variable (ctx->outer_context, decl2, true);
6661 omp_notice_variable (ctx->outer_context,
6662 OMP_CLAUSE_SIZE (c), true);
6665 break;
6667 case OMP_CLAUSE_REDUCTION:
6668 case OMP_CLAUSE_COPYIN:
6669 case OMP_CLAUSE_COPYPRIVATE:
6670 case OMP_CLAUSE_IF:
6671 case OMP_CLAUSE_NUM_THREADS:
6672 case OMP_CLAUSE_NUM_TEAMS:
6673 case OMP_CLAUSE_THREAD_LIMIT:
6674 case OMP_CLAUSE_DIST_SCHEDULE:
6675 case OMP_CLAUSE_DEVICE:
6676 case OMP_CLAUSE_SCHEDULE:
6677 case OMP_CLAUSE_NOWAIT:
6678 case OMP_CLAUSE_ORDERED:
6679 case OMP_CLAUSE_DEFAULT:
6680 case OMP_CLAUSE_UNTIED:
6681 case OMP_CLAUSE_COLLAPSE:
6682 case OMP_CLAUSE_FINAL:
6683 case OMP_CLAUSE_MERGEABLE:
6684 case OMP_CLAUSE_PROC_BIND:
6685 case OMP_CLAUSE_SAFELEN:
6686 case OMP_CLAUSE_DEPEND:
6687 break;
6689 default:
6690 gcc_unreachable ();
6693 if (remove)
6694 *list_p = OMP_CLAUSE_CHAIN (c);
6695 else
6696 list_p = &OMP_CLAUSE_CHAIN (c);
6699 /* Add in any implicit data sharing. */
6700 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6702 gimplify_omp_ctxp = ctx->outer_context;
6703 delete_omp_context (ctx);
6706 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6707 gimplification of the body, as well as scanning the body for used
6708 variables. We need to do this scan now, because variable-sized
6709 decls will be decomposed during gimplification. */
6711 static void
6712 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6714 tree expr = *expr_p;
6715 gimple g;
6716 gimple_seq body = NULL;
6717 struct gimplify_ctx gctx;
6719 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6720 OMP_PARALLEL_COMBINED (expr)
6721 ? ORT_COMBINED_PARALLEL
6722 : ORT_PARALLEL);
6724 push_gimplify_context (&gctx);
6726 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6727 if (gimple_code (g) == GIMPLE_BIND)
6728 pop_gimplify_context (g);
6729 else
6730 pop_gimplify_context (NULL);
6732 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6734 g = gimple_build_omp_parallel (body,
6735 OMP_PARALLEL_CLAUSES (expr),
6736 NULL_TREE, NULL_TREE);
6737 if (OMP_PARALLEL_COMBINED (expr))
6738 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6739 gimplify_seq_add_stmt (pre_p, g);
6740 *expr_p = NULL_TREE;
6743 /* Gimplify the contents of an OMP_TASK statement. This involves
6744 gimplification of the body, as well as scanning the body for used
6745 variables. We need to do this scan now, because variable-sized
6746 decls will be decomposed during gimplification. */
6748 static void
6749 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6751 tree expr = *expr_p;
6752 gimple g;
6753 gimple_seq body = NULL;
6754 struct gimplify_ctx gctx;
6756 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6757 find_omp_clause (OMP_TASK_CLAUSES (expr),
6758 OMP_CLAUSE_UNTIED)
6759 ? ORT_UNTIED_TASK : ORT_TASK);
6761 push_gimplify_context (&gctx);
6763 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6764 if (gimple_code (g) == GIMPLE_BIND)
6765 pop_gimplify_context (g);
6766 else
6767 pop_gimplify_context (NULL);
6769 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6771 g = gimple_build_omp_task (body,
6772 OMP_TASK_CLAUSES (expr),
6773 NULL_TREE, NULL_TREE,
6774 NULL_TREE, NULL_TREE, NULL_TREE);
6775 gimplify_seq_add_stmt (pre_p, g);
6776 *expr_p = NULL_TREE;
6779 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6780 with non-NULL OMP_FOR_INIT. */
6782 static tree
6783 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6785 *walk_subtrees = 0;
6786 switch (TREE_CODE (*tp))
6788 case OMP_FOR:
6789 *walk_subtrees = 1;
6790 /* FALLTHRU */
6791 case OMP_SIMD:
6792 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6793 return *tp;
6794 break;
6795 case BIND_EXPR:
6796 case STATEMENT_LIST:
6797 case OMP_PARALLEL:
6798 *walk_subtrees = 1;
6799 break;
6800 default:
6801 break;
6803 return NULL_TREE;
6806 /* Gimplify the gross structure of an OMP_FOR statement. */
6808 static enum gimplify_status
6809 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6811 tree for_stmt, orig_for_stmt, decl, var, t;
6812 enum gimplify_status ret = GS_ALL_DONE;
6813 enum gimplify_status tret;
6814 gimple gfor;
6815 gimple_seq for_body, for_pre_body;
6816 int i;
6817 bool simd;
6818 bitmap has_decl_expr = NULL;
6820 orig_for_stmt = for_stmt = *expr_p;
6822 simd = TREE_CODE (for_stmt) == OMP_SIMD;
6823 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6824 simd ? ORT_SIMD : ORT_WORKSHARE);
6826 /* Handle OMP_FOR_INIT. */
6827 for_pre_body = NULL;
6828 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6830 has_decl_expr = BITMAP_ALLOC (NULL);
6831 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6832 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6833 == VAR_DECL)
6835 t = OMP_FOR_PRE_BODY (for_stmt);
6836 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6838 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6840 tree_stmt_iterator si;
6841 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6842 tsi_next (&si))
6844 t = tsi_stmt (si);
6845 if (TREE_CODE (t) == DECL_EXPR
6846 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6847 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6851 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6852 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6854 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6856 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6857 NULL, NULL);
6858 gcc_assert (for_stmt != NULL_TREE);
6859 gimplify_omp_ctxp->combined_loop = true;
6862 for_body = NULL;
6863 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6864 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6865 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6866 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6867 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6869 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6870 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6871 decl = TREE_OPERAND (t, 0);
6872 gcc_assert (DECL_P (decl));
6873 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6874 || POINTER_TYPE_P (TREE_TYPE (decl)));
6876 /* Make sure the iteration variable is private. */
6877 tree c = NULL_TREE;
6878 if (orig_for_stmt != for_stmt)
6879 /* Do this only on innermost construct for combined ones. */;
6880 else if (simd)
6882 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6883 (splay_tree_key)decl);
6884 omp_is_private (gimplify_omp_ctxp, decl, simd);
6885 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6886 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6887 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6889 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6890 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6891 if (has_decl_expr
6892 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6893 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6894 OMP_CLAUSE_DECL (c) = decl;
6895 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6896 OMP_FOR_CLAUSES (for_stmt) = c;
6897 omp_add_variable (gimplify_omp_ctxp, decl,
6898 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6900 else
6902 bool lastprivate
6903 = (!has_decl_expr
6904 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6905 c = build_omp_clause (input_location,
6906 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6907 : OMP_CLAUSE_PRIVATE);
6908 OMP_CLAUSE_DECL (c) = decl;
6909 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6910 omp_add_variable (gimplify_omp_ctxp, decl,
6911 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6912 | GOVD_SEEN);
6913 c = NULL_TREE;
6916 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6917 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6918 else
6919 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6921 /* If DECL is not a gimple register, create a temporary variable to act
6922 as an iteration counter. This is valid, since DECL cannot be
6923 modified in the body of the loop. */
6924 if (orig_for_stmt != for_stmt)
6925 var = decl;
6926 else if (!is_gimple_reg (decl))
6928 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6929 TREE_OPERAND (t, 0) = var;
6931 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6933 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6935 else
6936 var = decl;
6938 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6939 is_gimple_val, fb_rvalue);
6940 ret = MIN (ret, tret);
6941 if (ret == GS_ERROR)
6942 return ret;
6944 /* Handle OMP_FOR_COND. */
6945 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6946 gcc_assert (COMPARISON_CLASS_P (t));
6947 gcc_assert (TREE_OPERAND (t, 0) == decl);
6949 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6950 is_gimple_val, fb_rvalue);
6951 ret = MIN (ret, tret);
6953 /* Handle OMP_FOR_INCR. */
6954 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6955 switch (TREE_CODE (t))
6957 case PREINCREMENT_EXPR:
6958 case POSTINCREMENT_EXPR:
6959 if (orig_for_stmt != for_stmt)
6960 break;
6961 t = build_int_cst (TREE_TYPE (decl), 1);
6962 if (c)
6963 OMP_CLAUSE_LINEAR_STEP (c) = t;
6964 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6965 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6966 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6967 break;
6969 case PREDECREMENT_EXPR:
6970 case POSTDECREMENT_EXPR:
6971 if (orig_for_stmt != for_stmt)
6972 break;
6973 t = build_int_cst (TREE_TYPE (decl), -1);
6974 if (c)
6975 OMP_CLAUSE_LINEAR_STEP (c) = t;
6976 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6977 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6978 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6979 break;
6981 case MODIFY_EXPR:
6982 gcc_assert (TREE_OPERAND (t, 0) == decl);
6983 TREE_OPERAND (t, 0) = var;
6985 t = TREE_OPERAND (t, 1);
6986 switch (TREE_CODE (t))
6988 case PLUS_EXPR:
6989 if (TREE_OPERAND (t, 1) == decl)
6991 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6992 TREE_OPERAND (t, 0) = var;
6993 break;
6996 /* Fallthru. */
6997 case MINUS_EXPR:
6998 case POINTER_PLUS_EXPR:
6999 gcc_assert (TREE_OPERAND (t, 0) == decl);
7000 TREE_OPERAND (t, 0) = var;
7001 break;
7002 default:
7003 gcc_unreachable ();
7006 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7007 is_gimple_val, fb_rvalue);
7008 ret = MIN (ret, tret);
7009 if (c)
7011 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
7012 if (TREE_CODE (t) == MINUS_EXPR)
7014 t = TREE_OPERAND (t, 1);
7015 OMP_CLAUSE_LINEAR_STEP (c)
7016 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
7017 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7018 &for_pre_body, NULL,
7019 is_gimple_val, fb_rvalue);
7020 ret = MIN (ret, tret);
7023 break;
7025 default:
7026 gcc_unreachable ();
7029 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7030 && orig_for_stmt == for_stmt)
7032 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7033 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7034 && OMP_CLAUSE_DECL (c) == decl
7035 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7037 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7038 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7039 gcc_assert (TREE_OPERAND (t, 0) == var);
7040 t = TREE_OPERAND (t, 1);
7041 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7042 || TREE_CODE (t) == MINUS_EXPR
7043 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7044 gcc_assert (TREE_OPERAND (t, 0) == var);
7045 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7046 TREE_OPERAND (t, 1));
7047 gimplify_assign (decl, t,
7048 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7053 BITMAP_FREE (has_decl_expr);
7055 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7057 if (orig_for_stmt != for_stmt)
7058 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7060 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7061 decl = TREE_OPERAND (t, 0);
7062 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7063 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7064 TREE_OPERAND (t, 0) = var;
7065 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7066 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7067 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7070 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
7072 int kind;
7073 switch (TREE_CODE (orig_for_stmt))
7075 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7076 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7077 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7078 default:
7079 gcc_unreachable ();
7081 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7082 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7083 for_pre_body);
7084 if (orig_for_stmt != for_stmt)
7085 gimple_omp_for_set_combined_p (gfor, true);
7086 if (gimplify_omp_ctxp
7087 && (gimplify_omp_ctxp->combined_loop
7088 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7089 && gimplify_omp_ctxp->outer_context
7090 && gimplify_omp_ctxp->outer_context->combined_loop)))
7092 gimple_omp_for_set_combined_into_p (gfor, true);
7093 if (gimplify_omp_ctxp->combined_loop)
7094 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7095 else
7096 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7099 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7101 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7102 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7103 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7104 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7105 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7106 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7107 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7108 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7111 gimplify_seq_add_stmt (pre_p, gfor);
7112 if (ret != GS_ALL_DONE)
7113 return GS_ERROR;
7114 *expr_p = NULL_TREE;
7115 return GS_ALL_DONE;
7118 /* Gimplify the gross structure of other OpenMP constructs.
7119 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7120 and OMP_TEAMS. */
7122 static void
7123 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7125 tree expr = *expr_p;
7126 gimple stmt;
7127 gimple_seq body = NULL;
7128 enum omp_region_type ort = ORT_WORKSHARE;
7130 switch (TREE_CODE (expr))
7132 case OMP_SECTIONS:
7133 case OMP_SINGLE:
7134 break;
7135 case OMP_TARGET:
7136 ort = ORT_TARGET;
7137 break;
7138 case OMP_TARGET_DATA:
7139 ort = ORT_TARGET_DATA;
7140 break;
7141 case OMP_TEAMS:
7142 ort = ORT_TEAMS;
7143 break;
7144 default:
7145 gcc_unreachable ();
7147 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7148 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7150 struct gimplify_ctx gctx;
7151 push_gimplify_context (&gctx);
7152 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7153 if (gimple_code (g) == GIMPLE_BIND)
7154 pop_gimplify_context (g);
7155 else
7156 pop_gimplify_context (NULL);
7157 if (ort == ORT_TARGET_DATA)
7159 gimple_seq cleanup = NULL;
7160 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7161 g = gimple_build_call (fn, 0);
7162 gimple_seq_add_stmt (&cleanup, g);
7163 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7164 body = NULL;
7165 gimple_seq_add_stmt (&body, g);
7168 else
7169 gimplify_and_add (OMP_BODY (expr), &body);
7170 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7172 switch (TREE_CODE (expr))
7174 case OMP_SECTIONS:
7175 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7176 break;
7177 case OMP_SINGLE:
7178 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7179 break;
7180 case OMP_TARGET:
7181 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7182 OMP_CLAUSES (expr));
7183 break;
7184 case OMP_TARGET_DATA:
7185 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7186 OMP_CLAUSES (expr));
7187 break;
7188 case OMP_TEAMS:
7189 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7190 break;
7191 default:
7192 gcc_unreachable ();
7195 gimplify_seq_add_stmt (pre_p, stmt);
7196 *expr_p = NULL_TREE;
7199 /* Gimplify the gross structure of OpenMP target update construct. */
7201 static void
7202 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7204 tree expr = *expr_p;
7205 gimple stmt;
7207 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7208 ORT_WORKSHARE);
7209 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7210 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7211 OMP_TARGET_UPDATE_CLAUSES (expr));
7213 gimplify_seq_add_stmt (pre_p, stmt);
7214 *expr_p = NULL_TREE;
7217 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7218 stabilized the lhs of the atomic operation as *ADDR. Return true if
7219 EXPR is this stabilized form. */
7221 static bool
7222 goa_lhs_expr_p (tree expr, tree addr)
7224 /* Also include casts to other type variants. The C front end is fond
7225 of adding these for e.g. volatile variables. This is like
7226 STRIP_TYPE_NOPS but includes the main variant lookup. */
7227 STRIP_USELESS_TYPE_CONVERSION (expr);
7229 if (TREE_CODE (expr) == INDIRECT_REF)
7231 expr = TREE_OPERAND (expr, 0);
7232 while (expr != addr
7233 && (CONVERT_EXPR_P (expr)
7234 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7235 && TREE_CODE (expr) == TREE_CODE (addr)
7236 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7238 expr = TREE_OPERAND (expr, 0);
7239 addr = TREE_OPERAND (addr, 0);
7241 if (expr == addr)
7242 return true;
7243 return (TREE_CODE (addr) == ADDR_EXPR
7244 && TREE_CODE (expr) == ADDR_EXPR
7245 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7247 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7248 return true;
7249 return false;
7252 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7253 expression does not involve the lhs, evaluate it into a temporary.
7254 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7255 or -1 if an error was encountered. */
7257 static int
7258 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7259 tree lhs_var)
7261 tree expr = *expr_p;
7262 int saw_lhs;
7264 if (goa_lhs_expr_p (expr, lhs_addr))
7266 *expr_p = lhs_var;
7267 return 1;
7269 if (is_gimple_val (expr))
7270 return 0;
7272 saw_lhs = 0;
7273 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7275 case tcc_binary:
7276 case tcc_comparison:
7277 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7278 lhs_var);
7279 case tcc_unary:
7280 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7281 lhs_var);
7282 break;
7283 case tcc_expression:
7284 switch (TREE_CODE (expr))
7286 case TRUTH_ANDIF_EXPR:
7287 case TRUTH_ORIF_EXPR:
7288 case TRUTH_AND_EXPR:
7289 case TRUTH_OR_EXPR:
7290 case TRUTH_XOR_EXPR:
7291 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7292 lhs_addr, lhs_var);
7293 case TRUTH_NOT_EXPR:
7294 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7295 lhs_addr, lhs_var);
7296 break;
7297 case COMPOUND_EXPR:
7298 /* Break out any preevaluations from cp_build_modify_expr. */
7299 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7300 expr = TREE_OPERAND (expr, 1))
7301 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7302 *expr_p = expr;
7303 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7304 default:
7305 break;
7307 break;
7308 default:
7309 break;
7312 if (saw_lhs == 0)
7314 enum gimplify_status gs;
7315 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7316 if (gs != GS_ALL_DONE)
7317 saw_lhs = -1;
7320 return saw_lhs;
7323 /* Gimplify an OMP_ATOMIC statement. */
7325 static enum gimplify_status
7326 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7328 tree addr = TREE_OPERAND (*expr_p, 0);
7329 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7330 ? NULL : TREE_OPERAND (*expr_p, 1);
7331 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7332 tree tmp_load;
7333 gimple loadstmt, storestmt;
7335 tmp_load = create_tmp_reg (type, NULL);
7336 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7337 return GS_ERROR;
7339 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7340 != GS_ALL_DONE)
7341 return GS_ERROR;
7343 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7344 gimplify_seq_add_stmt (pre_p, loadstmt);
7345 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7346 != GS_ALL_DONE)
7347 return GS_ERROR;
7349 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7350 rhs = tmp_load;
7351 storestmt = gimple_build_omp_atomic_store (rhs);
7352 gimplify_seq_add_stmt (pre_p, storestmt);
7353 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7355 gimple_omp_atomic_set_seq_cst (loadstmt);
7356 gimple_omp_atomic_set_seq_cst (storestmt);
7358 switch (TREE_CODE (*expr_p))
7360 case OMP_ATOMIC_READ:
7361 case OMP_ATOMIC_CAPTURE_OLD:
7362 *expr_p = tmp_load;
7363 gimple_omp_atomic_set_need_value (loadstmt);
7364 break;
7365 case OMP_ATOMIC_CAPTURE_NEW:
7366 *expr_p = rhs;
7367 gimple_omp_atomic_set_need_value (storestmt);
7368 break;
7369 default:
7370 *expr_p = NULL;
7371 break;
7374 return GS_ALL_DONE;
7377 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7378 body, and adding some EH bits. */
7380 static enum gimplify_status
7381 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7383 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7384 gimple g;
7385 gimple_seq body = NULL;
7386 struct gimplify_ctx gctx;
7387 int subcode = 0;
7389 /* Wrap the transaction body in a BIND_EXPR so we have a context
7390 where to put decls for OpenMP. */
7391 if (TREE_CODE (tbody) != BIND_EXPR)
7393 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7394 TREE_SIDE_EFFECTS (bind) = 1;
7395 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7396 TRANSACTION_EXPR_BODY (expr) = bind;
7399 push_gimplify_context (&gctx);
7400 temp = voidify_wrapper_expr (*expr_p, NULL);
7402 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7403 pop_gimplify_context (g);
7405 g = gimple_build_transaction (body, NULL);
7406 if (TRANSACTION_EXPR_OUTER (expr))
7407 subcode = GTMA_IS_OUTER;
7408 else if (TRANSACTION_EXPR_RELAXED (expr))
7409 subcode = GTMA_IS_RELAXED;
7410 gimple_transaction_set_subcode (g, subcode);
7412 gimplify_seq_add_stmt (pre_p, g);
7414 if (temp)
7416 *expr_p = temp;
7417 return GS_OK;
7420 *expr_p = NULL_TREE;
7421 return GS_ALL_DONE;
7424 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7425 expression produces a value to be used as an operand inside a GIMPLE
7426 statement, the value will be stored back in *EXPR_P. This value will
7427 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7428 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7429 emitted in PRE_P and POST_P.
7431 Additionally, this process may overwrite parts of the input
7432 expression during gimplification. Ideally, it should be
7433 possible to do non-destructive gimplification.
7435 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7436 the expression needs to evaluate to a value to be used as
7437 an operand in a GIMPLE statement, this value will be stored in
7438 *EXPR_P on exit. This happens when the caller specifies one
7439 of fb_lvalue or fb_rvalue fallback flags.
7441 PRE_P will contain the sequence of GIMPLE statements corresponding
7442 to the evaluation of EXPR and all the side-effects that must
7443 be executed before the main expression. On exit, the last
7444 statement of PRE_P is the core statement being gimplified. For
7445 instance, when gimplifying 'if (++a)' the last statement in
7446 PRE_P will be 'if (t.1)' where t.1 is the result of
7447 pre-incrementing 'a'.
7449 POST_P will contain the sequence of GIMPLE statements corresponding
7450 to the evaluation of all the side-effects that must be executed
7451 after the main expression. If this is NULL, the post
7452 side-effects are stored at the end of PRE_P.
7454 The reason why the output is split in two is to handle post
7455 side-effects explicitly. In some cases, an expression may have
7456 inner and outer post side-effects which need to be emitted in
7457 an order different from the one given by the recursive
7458 traversal. For instance, for the expression (*p--)++ the post
7459 side-effects of '--' must actually occur *after* the post
7460 side-effects of '++'. However, gimplification will first visit
7461 the inner expression, so if a separate POST sequence was not
7462 used, the resulting sequence would be:
7464 1 t.1 = *p
7465 2 p = p - 1
7466 3 t.2 = t.1 + 1
7467 4 *p = t.2
7469 However, the post-decrement operation in line #2 must not be
7470 evaluated until after the store to *p at line #4, so the
7471 correct sequence should be:
7473 1 t.1 = *p
7474 2 t.2 = t.1 + 1
7475 3 *p = t.2
7476 4 p = p - 1
7478 So, by specifying a separate post queue, it is possible
7479 to emit the post side-effects in the correct order.
7480 If POST_P is NULL, an internal queue will be used. Before
7481 returning to the caller, the sequence POST_P is appended to
7482 the main output sequence PRE_P.
7484 GIMPLE_TEST_F points to a function that takes a tree T and
7485 returns nonzero if T is in the GIMPLE form requested by the
7486 caller. The GIMPLE predicates are in gimple.c.
7488 FALLBACK tells the function what sort of a temporary we want if
7489 gimplification cannot produce an expression that complies with
7490 GIMPLE_TEST_F.
7492 fb_none means that no temporary should be generated
7493 fb_rvalue means that an rvalue is OK to generate
7494 fb_lvalue means that an lvalue is OK to generate
7495 fb_either means that either is OK, but an lvalue is preferable.
7496 fb_mayfail means that gimplification may fail (in which case
7497 GS_ERROR will be returned)
7499 The return value is either GS_ERROR or GS_ALL_DONE, since this
7500 function iterates until EXPR is completely gimplified or an error
7501 occurs. */
7503 enum gimplify_status
7504 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7505 bool (*gimple_test_f) (tree), fallback_t fallback)
7507 tree tmp;
7508 gimple_seq internal_pre = NULL;
7509 gimple_seq internal_post = NULL;
7510 tree save_expr;
7511 bool is_statement;
7512 location_t saved_location;
7513 enum gimplify_status ret;
7514 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7516 save_expr = *expr_p;
7517 if (save_expr == NULL_TREE)
7518 return GS_ALL_DONE;
7520 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7521 is_statement = gimple_test_f == is_gimple_stmt;
7522 if (is_statement)
7523 gcc_assert (pre_p);
7525 /* Consistency checks. */
7526 if (gimple_test_f == is_gimple_reg)
7527 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7528 else if (gimple_test_f == is_gimple_val
7529 || gimple_test_f == is_gimple_call_addr
7530 || gimple_test_f == is_gimple_condexpr
7531 || gimple_test_f == is_gimple_mem_rhs
7532 || gimple_test_f == is_gimple_mem_rhs_or_call
7533 || gimple_test_f == is_gimple_reg_rhs
7534 || gimple_test_f == is_gimple_reg_rhs_or_call
7535 || gimple_test_f == is_gimple_asm_val
7536 || gimple_test_f == is_gimple_mem_ref_addr)
7537 gcc_assert (fallback & fb_rvalue);
7538 else if (gimple_test_f == is_gimple_min_lval
7539 || gimple_test_f == is_gimple_lvalue)
7540 gcc_assert (fallback & fb_lvalue);
7541 else if (gimple_test_f == is_gimple_addressable)
7542 gcc_assert (fallback & fb_either);
7543 else if (gimple_test_f == is_gimple_stmt)
7544 gcc_assert (fallback == fb_none);
7545 else
7547 /* We should have recognized the GIMPLE_TEST_F predicate to
7548 know what kind of fallback to use in case a temporary is
7549 needed to hold the value or address of *EXPR_P. */
7550 gcc_unreachable ();
7553 /* We used to check the predicate here and return immediately if it
7554 succeeds. This is wrong; the design is for gimplification to be
7555 idempotent, and for the predicates to only test for valid forms, not
7556 whether they are fully simplified. */
7557 if (pre_p == NULL)
7558 pre_p = &internal_pre;
7560 if (post_p == NULL)
7561 post_p = &internal_post;
7563 /* Remember the last statements added to PRE_P and POST_P. Every
7564 new statement added by the gimplification helpers needs to be
7565 annotated with location information. To centralize the
7566 responsibility, we remember the last statement that had been
7567 added to both queues before gimplifying *EXPR_P. If
7568 gimplification produces new statements in PRE_P and POST_P, those
7569 statements will be annotated with the same location information
7570 as *EXPR_P. */
7571 pre_last_gsi = gsi_last (*pre_p);
7572 post_last_gsi = gsi_last (*post_p);
7574 saved_location = input_location;
7575 if (save_expr != error_mark_node
7576 && EXPR_HAS_LOCATION (*expr_p))
7577 input_location = EXPR_LOCATION (*expr_p);
7579 /* Loop over the specific gimplifiers until the toplevel node
7580 remains the same. */
7583 /* Strip away as many useless type conversions as possible
7584 at the toplevel. */
7585 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7587 /* Remember the expr. */
7588 save_expr = *expr_p;
7590 /* Die, die, die, my darling. */
7591 if (save_expr == error_mark_node
7592 || (TREE_TYPE (save_expr)
7593 && TREE_TYPE (save_expr) == error_mark_node))
7595 ret = GS_ERROR;
7596 break;
7599 /* Do any language-specific gimplification. */
7600 ret = ((enum gimplify_status)
7601 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7602 if (ret == GS_OK)
7604 if (*expr_p == NULL_TREE)
7605 break;
7606 if (*expr_p != save_expr)
7607 continue;
7609 else if (ret != GS_UNHANDLED)
7610 break;
7612 /* Make sure that all the cases set 'ret' appropriately. */
7613 ret = GS_UNHANDLED;
7614 switch (TREE_CODE (*expr_p))
7616 /* First deal with the special cases. */
7618 case POSTINCREMENT_EXPR:
7619 case POSTDECREMENT_EXPR:
7620 case PREINCREMENT_EXPR:
7621 case PREDECREMENT_EXPR:
7622 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7623 fallback != fb_none,
7624 TREE_TYPE (*expr_p));
7625 break;
7627 case ARRAY_REF:
7628 case ARRAY_RANGE_REF:
7629 case REALPART_EXPR:
7630 case IMAGPART_EXPR:
7631 case COMPONENT_REF:
7632 case VIEW_CONVERT_EXPR:
7633 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7634 fallback ? fallback : fb_rvalue);
7635 break;
7637 case COND_EXPR:
7638 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7640 /* C99 code may assign to an array in a structure value of a
7641 conditional expression, and this has undefined behavior
7642 only on execution, so create a temporary if an lvalue is
7643 required. */
7644 if (fallback == fb_lvalue)
7646 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7647 mark_addressable (*expr_p);
7648 ret = GS_OK;
7650 break;
7652 case CALL_EXPR:
7653 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7655 /* C99 code may assign to an array in a structure returned
7656 from a function, and this has undefined behavior only on
7657 execution, so create a temporary if an lvalue is
7658 required. */
7659 if (fallback == fb_lvalue)
7661 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7662 mark_addressable (*expr_p);
7663 ret = GS_OK;
7665 break;
7667 case TREE_LIST:
7668 gcc_unreachable ();
7670 case COMPOUND_EXPR:
7671 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7672 break;
7674 case COMPOUND_LITERAL_EXPR:
7675 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7676 gimple_test_f, fallback);
7677 break;
7679 case MODIFY_EXPR:
7680 case INIT_EXPR:
7681 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7682 fallback != fb_none);
7683 break;
7685 case TRUTH_ANDIF_EXPR:
7686 case TRUTH_ORIF_EXPR:
7688 /* Preserve the original type of the expression and the
7689 source location of the outer expression. */
7690 tree org_type = TREE_TYPE (*expr_p);
7691 *expr_p = gimple_boolify (*expr_p);
7692 *expr_p = build3_loc (input_location, COND_EXPR,
7693 org_type, *expr_p,
7694 fold_convert_loc
7695 (input_location,
7696 org_type, boolean_true_node),
7697 fold_convert_loc
7698 (input_location,
7699 org_type, boolean_false_node));
7700 ret = GS_OK;
7701 break;
7704 case TRUTH_NOT_EXPR:
7706 tree type = TREE_TYPE (*expr_p);
7707 /* The parsers are careful to generate TRUTH_NOT_EXPR
7708 only with operands that are always zero or one.
7709 We do not fold here but handle the only interesting case
7710 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7711 *expr_p = gimple_boolify (*expr_p);
7712 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7713 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7714 TREE_TYPE (*expr_p),
7715 TREE_OPERAND (*expr_p, 0));
7716 else
7717 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7718 TREE_TYPE (*expr_p),
7719 TREE_OPERAND (*expr_p, 0),
7720 build_int_cst (TREE_TYPE (*expr_p), 1));
7721 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7722 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7723 ret = GS_OK;
7724 break;
7727 case ADDR_EXPR:
7728 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7729 break;
7731 case VA_ARG_EXPR:
7732 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7733 break;
7735 CASE_CONVERT:
7736 if (IS_EMPTY_STMT (*expr_p))
7738 ret = GS_ALL_DONE;
7739 break;
7742 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7743 || fallback == fb_none)
7745 /* Just strip a conversion to void (or in void context) and
7746 try again. */
7747 *expr_p = TREE_OPERAND (*expr_p, 0);
7748 ret = GS_OK;
7749 break;
7752 ret = gimplify_conversion (expr_p);
7753 if (ret == GS_ERROR)
7754 break;
7755 if (*expr_p != save_expr)
7756 break;
7757 /* FALLTHRU */
7759 case FIX_TRUNC_EXPR:
7760 /* unary_expr: ... | '(' cast ')' val | ... */
7761 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7762 is_gimple_val, fb_rvalue);
7763 recalculate_side_effects (*expr_p);
7764 break;
7766 case INDIRECT_REF:
7768 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7769 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7770 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7772 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7773 if (*expr_p != save_expr)
7775 ret = GS_OK;
7776 break;
7779 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7780 is_gimple_reg, fb_rvalue);
7781 if (ret == GS_ERROR)
7782 break;
7784 recalculate_side_effects (*expr_p);
7785 *expr_p = fold_build2_loc (input_location, MEM_REF,
7786 TREE_TYPE (*expr_p),
7787 TREE_OPERAND (*expr_p, 0),
7788 build_int_cst (saved_ptr_type, 0));
7789 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7790 TREE_THIS_NOTRAP (*expr_p) = notrap;
7791 ret = GS_OK;
7792 break;
7795 /* We arrive here through the various re-gimplifcation paths. */
7796 case MEM_REF:
7797 /* First try re-folding the whole thing. */
7798 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7799 TREE_OPERAND (*expr_p, 0),
7800 TREE_OPERAND (*expr_p, 1));
7801 if (tmp)
7803 *expr_p = tmp;
7804 recalculate_side_effects (*expr_p);
7805 ret = GS_OK;
7806 break;
7808 /* Avoid re-gimplifying the address operand if it is already
7809 in suitable form. Re-gimplifying would mark the address
7810 operand addressable. Always gimplify when not in SSA form
7811 as we still may have to gimplify decls with value-exprs. */
7812 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7813 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7815 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7816 is_gimple_mem_ref_addr, fb_rvalue);
7817 if (ret == GS_ERROR)
7818 break;
7820 recalculate_side_effects (*expr_p);
7821 ret = GS_ALL_DONE;
7822 break;
7824 /* Constants need not be gimplified. */
7825 case INTEGER_CST:
7826 case REAL_CST:
7827 case FIXED_CST:
7828 case STRING_CST:
7829 case COMPLEX_CST:
7830 case VECTOR_CST:
7831 ret = GS_ALL_DONE;
7832 break;
7834 case CONST_DECL:
7835 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7836 CONST_DECL node. Otherwise the decl is replaceable by its
7837 value. */
7838 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7839 if (fallback & fb_lvalue)
7840 ret = GS_ALL_DONE;
7841 else
7843 *expr_p = DECL_INITIAL (*expr_p);
7844 ret = GS_OK;
7846 break;
7848 case DECL_EXPR:
7849 ret = gimplify_decl_expr (expr_p, pre_p);
7850 break;
7852 case BIND_EXPR:
7853 ret = gimplify_bind_expr (expr_p, pre_p);
7854 break;
7856 case LOOP_EXPR:
7857 ret = gimplify_loop_expr (expr_p, pre_p);
7858 break;
7860 case SWITCH_EXPR:
7861 ret = gimplify_switch_expr (expr_p, pre_p);
7862 break;
7864 case EXIT_EXPR:
7865 ret = gimplify_exit_expr (expr_p);
7866 break;
7868 case GOTO_EXPR:
7869 /* If the target is not LABEL, then it is a computed jump
7870 and the target needs to be gimplified. */
7871 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7873 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7874 NULL, is_gimple_val, fb_rvalue);
7875 if (ret == GS_ERROR)
7876 break;
7878 gimplify_seq_add_stmt (pre_p,
7879 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7880 ret = GS_ALL_DONE;
7881 break;
7883 case PREDICT_EXPR:
7884 gimplify_seq_add_stmt (pre_p,
7885 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7886 PREDICT_EXPR_OUTCOME (*expr_p)));
7887 ret = GS_ALL_DONE;
7888 break;
7890 case LABEL_EXPR:
7891 ret = GS_ALL_DONE;
7892 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7893 == current_function_decl);
7894 gimplify_seq_add_stmt (pre_p,
7895 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7896 break;
7898 case CASE_LABEL_EXPR:
7899 ret = gimplify_case_label_expr (expr_p, pre_p);
7900 break;
7902 case RETURN_EXPR:
7903 ret = gimplify_return_expr (*expr_p, pre_p);
7904 break;
7906 case CONSTRUCTOR:
7907 /* Don't reduce this in place; let gimplify_init_constructor work its
7908 magic. Buf if we're just elaborating this for side effects, just
7909 gimplify any element that has side-effects. */
7910 if (fallback == fb_none)
7912 unsigned HOST_WIDE_INT ix;
7913 tree val;
7914 tree temp = NULL_TREE;
7915 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7916 if (TREE_SIDE_EFFECTS (val))
7917 append_to_statement_list (val, &temp);
7919 *expr_p = temp;
7920 ret = temp ? GS_OK : GS_ALL_DONE;
7922 /* C99 code may assign to an array in a constructed
7923 structure or union, and this has undefined behavior only
7924 on execution, so create a temporary if an lvalue is
7925 required. */
7926 else if (fallback == fb_lvalue)
7928 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7929 mark_addressable (*expr_p);
7930 ret = GS_OK;
7932 else
7933 ret = GS_ALL_DONE;
7934 break;
7936 /* The following are special cases that are not handled by the
7937 original GIMPLE grammar. */
7939 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7940 eliminated. */
7941 case SAVE_EXPR:
7942 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7943 break;
7945 case BIT_FIELD_REF:
7946 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7947 post_p, is_gimple_lvalue, fb_either);
7948 recalculate_side_effects (*expr_p);
7949 break;
7951 case TARGET_MEM_REF:
7953 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7955 if (TMR_BASE (*expr_p))
7956 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7957 post_p, is_gimple_mem_ref_addr, fb_either);
7958 if (TMR_INDEX (*expr_p))
7959 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7960 post_p, is_gimple_val, fb_rvalue);
7961 if (TMR_INDEX2 (*expr_p))
7962 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7963 post_p, is_gimple_val, fb_rvalue);
7964 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7965 ret = MIN (r0, r1);
7967 break;
7969 case NON_LVALUE_EXPR:
7970 /* This should have been stripped above. */
7971 gcc_unreachable ();
7973 case ASM_EXPR:
7974 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7975 break;
7977 case TRY_FINALLY_EXPR:
7978 case TRY_CATCH_EXPR:
7980 gimple_seq eval, cleanup;
7981 gimple try_;
7983 /* Calls to destructors are generated automatically in FINALLY/CATCH
7984 block. They should have location as UNKNOWN_LOCATION. However,
7985 gimplify_call_expr will reset these call stmts to input_location
7986 if it finds stmt's location is unknown. To prevent resetting for
7987 destructors, we set the input_location to unknown.
7988 Note that this only affects the destructor calls in FINALLY/CATCH
7989 block, and will automatically reset to its original value by the
7990 end of gimplify_expr. */
7991 input_location = UNKNOWN_LOCATION;
7992 eval = cleanup = NULL;
7993 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7994 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7995 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7996 if (gimple_seq_empty_p (cleanup))
7998 gimple_seq_add_seq (pre_p, eval);
7999 ret = GS_ALL_DONE;
8000 break;
8002 try_ = gimple_build_try (eval, cleanup,
8003 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8004 ? GIMPLE_TRY_FINALLY
8005 : GIMPLE_TRY_CATCH);
8006 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8007 gimple_set_location (try_, saved_location);
8008 else
8009 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8010 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8011 gimple_try_set_catch_is_cleanup (try_,
8012 TRY_CATCH_IS_CLEANUP (*expr_p));
8013 gimplify_seq_add_stmt (pre_p, try_);
8014 ret = GS_ALL_DONE;
8015 break;
8018 case CLEANUP_POINT_EXPR:
8019 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8020 break;
8022 case TARGET_EXPR:
8023 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8024 break;
8026 case CATCH_EXPR:
8028 gimple c;
8029 gimple_seq handler = NULL;
8030 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8031 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8032 gimplify_seq_add_stmt (pre_p, c);
8033 ret = GS_ALL_DONE;
8034 break;
8037 case EH_FILTER_EXPR:
8039 gimple ehf;
8040 gimple_seq failure = NULL;
8042 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8043 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8044 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8045 gimplify_seq_add_stmt (pre_p, ehf);
8046 ret = GS_ALL_DONE;
8047 break;
8050 case OBJ_TYPE_REF:
8052 enum gimplify_status r0, r1;
8053 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8054 post_p, is_gimple_val, fb_rvalue);
8055 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8056 post_p, is_gimple_val, fb_rvalue);
8057 TREE_SIDE_EFFECTS (*expr_p) = 0;
8058 ret = MIN (r0, r1);
8060 break;
8062 case LABEL_DECL:
8063 /* We get here when taking the address of a label. We mark
8064 the label as "forced"; meaning it can never be removed and
8065 it is a potential target for any computed goto. */
8066 FORCED_LABEL (*expr_p) = 1;
8067 ret = GS_ALL_DONE;
8068 break;
8070 case STATEMENT_LIST:
8071 ret = gimplify_statement_list (expr_p, pre_p);
8072 break;
8074 case WITH_SIZE_EXPR:
8076 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8077 post_p == &internal_post ? NULL : post_p,
8078 gimple_test_f, fallback);
8079 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8080 is_gimple_val, fb_rvalue);
8081 ret = GS_ALL_DONE;
8083 break;
8085 case VAR_DECL:
8086 case PARM_DECL:
8087 ret = gimplify_var_or_parm_decl (expr_p);
8088 break;
8090 case RESULT_DECL:
8091 /* When within an OpenMP context, notice uses of variables. */
8092 if (gimplify_omp_ctxp)
8093 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8094 ret = GS_ALL_DONE;
8095 break;
8097 case SSA_NAME:
8098 /* Allow callbacks into the gimplifier during optimization. */
8099 ret = GS_ALL_DONE;
8100 break;
8102 case OMP_PARALLEL:
8103 gimplify_omp_parallel (expr_p, pre_p);
8104 ret = GS_ALL_DONE;
8105 break;
8107 case OMP_TASK:
8108 gimplify_omp_task (expr_p, pre_p);
8109 ret = GS_ALL_DONE;
8110 break;
8112 case OMP_FOR:
8113 case OMP_SIMD:
8114 case OMP_DISTRIBUTE:
8115 ret = gimplify_omp_for (expr_p, pre_p);
8116 break;
8118 case OMP_SECTIONS:
8119 case OMP_SINGLE:
8120 case OMP_TARGET:
8121 case OMP_TARGET_DATA:
8122 case OMP_TEAMS:
8123 gimplify_omp_workshare (expr_p, pre_p);
8124 ret = GS_ALL_DONE;
8125 break;
8127 case OMP_TARGET_UPDATE:
8128 gimplify_omp_target_update (expr_p, pre_p);
8129 ret = GS_ALL_DONE;
8130 break;
8132 case OMP_SECTION:
8133 case OMP_MASTER:
8134 case OMP_TASKGROUP:
8135 case OMP_ORDERED:
8136 case OMP_CRITICAL:
8138 gimple_seq body = NULL;
8139 gimple g;
8141 gimplify_and_add (OMP_BODY (*expr_p), &body);
8142 switch (TREE_CODE (*expr_p))
8144 case OMP_SECTION:
8145 g = gimple_build_omp_section (body);
8146 break;
8147 case OMP_MASTER:
8148 g = gimple_build_omp_master (body);
8149 break;
8150 case OMP_TASKGROUP:
8152 gimple_seq cleanup = NULL;
8153 tree fn
8154 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8155 g = gimple_build_call (fn, 0);
8156 gimple_seq_add_stmt (&cleanup, g);
8157 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8158 body = NULL;
8159 gimple_seq_add_stmt (&body, g);
8160 g = gimple_build_omp_taskgroup (body);
8162 break;
8163 case OMP_ORDERED:
8164 g = gimple_build_omp_ordered (body);
8165 break;
8166 case OMP_CRITICAL:
8167 g = gimple_build_omp_critical (body,
8168 OMP_CRITICAL_NAME (*expr_p));
8169 break;
8170 default:
8171 gcc_unreachable ();
8173 gimplify_seq_add_stmt (pre_p, g);
8174 ret = GS_ALL_DONE;
8175 break;
8178 case OMP_ATOMIC:
8179 case OMP_ATOMIC_READ:
8180 case OMP_ATOMIC_CAPTURE_OLD:
8181 case OMP_ATOMIC_CAPTURE_NEW:
8182 ret = gimplify_omp_atomic (expr_p, pre_p);
8183 break;
8185 case TRANSACTION_EXPR:
8186 ret = gimplify_transaction (expr_p, pre_p);
8187 break;
8189 case TRUTH_AND_EXPR:
8190 case TRUTH_OR_EXPR:
8191 case TRUTH_XOR_EXPR:
8193 tree orig_type = TREE_TYPE (*expr_p);
8194 tree new_type, xop0, xop1;
8195 *expr_p = gimple_boolify (*expr_p);
8196 new_type = TREE_TYPE (*expr_p);
8197 if (!useless_type_conversion_p (orig_type, new_type))
8199 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8200 ret = GS_OK;
8201 break;
8204 /* Boolified binary truth expressions are semantically equivalent
8205 to bitwise binary expressions. Canonicalize them to the
8206 bitwise variant. */
8207 switch (TREE_CODE (*expr_p))
8209 case TRUTH_AND_EXPR:
8210 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8211 break;
8212 case TRUTH_OR_EXPR:
8213 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8214 break;
8215 case TRUTH_XOR_EXPR:
8216 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8217 break;
8218 default:
8219 break;
8221 /* Now make sure that operands have compatible type to
8222 expression's new_type. */
8223 xop0 = TREE_OPERAND (*expr_p, 0);
8224 xop1 = TREE_OPERAND (*expr_p, 1);
8225 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8226 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8227 new_type,
8228 xop0);
8229 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8230 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8231 new_type,
8232 xop1);
8233 /* Continue classified as tcc_binary. */
8234 goto expr_2;
8237 case FMA_EXPR:
8238 case VEC_COND_EXPR:
8239 case VEC_PERM_EXPR:
8240 /* Classified as tcc_expression. */
8241 goto expr_3;
8243 case POINTER_PLUS_EXPR:
8245 enum gimplify_status r0, r1;
8246 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8247 post_p, is_gimple_val, fb_rvalue);
8248 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8249 post_p, is_gimple_val, fb_rvalue);
8250 recalculate_side_effects (*expr_p);
8251 ret = MIN (r0, r1);
8252 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8253 after gimplifying operands - this is similar to how
8254 it would be folding all gimplified stmts on creation
8255 to have them canonicalized, which is what we eventually
8256 should do anyway. */
8257 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8258 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8260 *expr_p = build_fold_addr_expr_with_type_loc
8261 (input_location,
8262 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8263 TREE_OPERAND (*expr_p, 0),
8264 fold_convert (ptr_type_node,
8265 TREE_OPERAND (*expr_p, 1))),
8266 TREE_TYPE (*expr_p));
8267 ret = MIN (ret, GS_OK);
8269 break;
8272 default:
8273 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8275 case tcc_comparison:
8276 /* Handle comparison of objects of non scalar mode aggregates
8277 with a call to memcmp. It would be nice to only have to do
8278 this for variable-sized objects, but then we'd have to allow
8279 the same nest of reference nodes we allow for MODIFY_EXPR and
8280 that's too complex.
8282 Compare scalar mode aggregates as scalar mode values. Using
8283 memcmp for them would be very inefficient at best, and is
8284 plain wrong if bitfields are involved. */
8286 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8288 /* Vector comparisons need no boolification. */
8289 if (TREE_CODE (type) == VECTOR_TYPE)
8290 goto expr_2;
8291 else if (!AGGREGATE_TYPE_P (type))
8293 tree org_type = TREE_TYPE (*expr_p);
8294 *expr_p = gimple_boolify (*expr_p);
8295 if (!useless_type_conversion_p (org_type,
8296 TREE_TYPE (*expr_p)))
8298 *expr_p = fold_convert_loc (input_location,
8299 org_type, *expr_p);
8300 ret = GS_OK;
8302 else
8303 goto expr_2;
8305 else if (TYPE_MODE (type) != BLKmode)
8306 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8307 else
8308 ret = gimplify_variable_sized_compare (expr_p);
8310 break;
8313 /* If *EXPR_P does not need to be special-cased, handle it
8314 according to its class. */
8315 case tcc_unary:
8316 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8317 post_p, is_gimple_val, fb_rvalue);
8318 break;
8320 case tcc_binary:
8321 expr_2:
8323 enum gimplify_status r0, r1;
8325 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8326 post_p, is_gimple_val, fb_rvalue);
8327 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8328 post_p, is_gimple_val, fb_rvalue);
8330 ret = MIN (r0, r1);
8331 break;
8334 expr_3:
8336 enum gimplify_status r0, r1, r2;
8338 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8339 post_p, is_gimple_val, fb_rvalue);
8340 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8341 post_p, is_gimple_val, fb_rvalue);
8342 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8343 post_p, is_gimple_val, fb_rvalue);
8345 ret = MIN (MIN (r0, r1), r2);
8346 break;
8349 case tcc_declaration:
8350 case tcc_constant:
8351 ret = GS_ALL_DONE;
8352 goto dont_recalculate;
8354 default:
8355 gcc_unreachable ();
8358 recalculate_side_effects (*expr_p);
8360 dont_recalculate:
8361 break;
8364 gcc_assert (*expr_p || ret != GS_OK);
8366 while (ret == GS_OK);
8368 /* If we encountered an error_mark somewhere nested inside, either
8369 stub out the statement or propagate the error back out. */
8370 if (ret == GS_ERROR)
8372 if (is_statement)
8373 *expr_p = NULL;
8374 goto out;
8377 /* This was only valid as a return value from the langhook, which
8378 we handled. Make sure it doesn't escape from any other context. */
8379 gcc_assert (ret != GS_UNHANDLED);
8381 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8383 /* We aren't looking for a value, and we don't have a valid
8384 statement. If it doesn't have side-effects, throw it away. */
8385 if (!TREE_SIDE_EFFECTS (*expr_p))
8386 *expr_p = NULL;
8387 else if (!TREE_THIS_VOLATILE (*expr_p))
8389 /* This is probably a _REF that contains something nested that
8390 has side effects. Recurse through the operands to find it. */
8391 enum tree_code code = TREE_CODE (*expr_p);
8393 switch (code)
8395 case COMPONENT_REF:
8396 case REALPART_EXPR:
8397 case IMAGPART_EXPR:
8398 case VIEW_CONVERT_EXPR:
8399 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8400 gimple_test_f, fallback);
8401 break;
8403 case ARRAY_REF:
8404 case ARRAY_RANGE_REF:
8405 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8406 gimple_test_f, fallback);
8407 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8408 gimple_test_f, fallback);
8409 break;
8411 default:
8412 /* Anything else with side-effects must be converted to
8413 a valid statement before we get here. */
8414 gcc_unreachable ();
8417 *expr_p = NULL;
8419 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8420 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8422 /* Historically, the compiler has treated a bare reference
8423 to a non-BLKmode volatile lvalue as forcing a load. */
8424 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8426 /* Normally, we do not want to create a temporary for a
8427 TREE_ADDRESSABLE type because such a type should not be
8428 copied by bitwise-assignment. However, we make an
8429 exception here, as all we are doing here is ensuring that
8430 we read the bytes that make up the type. We use
8431 create_tmp_var_raw because create_tmp_var will abort when
8432 given a TREE_ADDRESSABLE type. */
8433 tree tmp = create_tmp_var_raw (type, "vol");
8434 gimple_add_tmp_var (tmp);
8435 gimplify_assign (tmp, *expr_p, pre_p);
8436 *expr_p = NULL;
8438 else
8439 /* We can't do anything useful with a volatile reference to
8440 an incomplete type, so just throw it away. Likewise for
8441 a BLKmode type, since any implicit inner load should
8442 already have been turned into an explicit one by the
8443 gimplification process. */
8444 *expr_p = NULL;
8447 /* If we are gimplifying at the statement level, we're done. Tack
8448 everything together and return. */
8449 if (fallback == fb_none || is_statement)
8451 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8452 it out for GC to reclaim it. */
8453 *expr_p = NULL_TREE;
8455 if (!gimple_seq_empty_p (internal_pre)
8456 || !gimple_seq_empty_p (internal_post))
8458 gimplify_seq_add_seq (&internal_pre, internal_post);
8459 gimplify_seq_add_seq (pre_p, internal_pre);
8462 /* The result of gimplifying *EXPR_P is going to be the last few
8463 statements in *PRE_P and *POST_P. Add location information
8464 to all the statements that were added by the gimplification
8465 helpers. */
8466 if (!gimple_seq_empty_p (*pre_p))
8467 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8469 if (!gimple_seq_empty_p (*post_p))
8470 annotate_all_with_location_after (*post_p, post_last_gsi,
8471 input_location);
8473 goto out;
8476 #ifdef ENABLE_GIMPLE_CHECKING
8477 if (*expr_p)
8479 enum tree_code code = TREE_CODE (*expr_p);
8480 /* These expressions should already be in gimple IR form. */
8481 gcc_assert (code != MODIFY_EXPR
8482 && code != ASM_EXPR
8483 && code != BIND_EXPR
8484 && code != CATCH_EXPR
8485 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8486 && code != EH_FILTER_EXPR
8487 && code != GOTO_EXPR
8488 && code != LABEL_EXPR
8489 && code != LOOP_EXPR
8490 && code != SWITCH_EXPR
8491 && code != TRY_FINALLY_EXPR
8492 && code != OMP_CRITICAL
8493 && code != OMP_FOR
8494 && code != OMP_MASTER
8495 && code != OMP_TASKGROUP
8496 && code != OMP_ORDERED
8497 && code != OMP_PARALLEL
8498 && code != OMP_SECTIONS
8499 && code != OMP_SECTION
8500 && code != OMP_SINGLE);
8502 #endif
8504 /* Otherwise we're gimplifying a subexpression, so the resulting
8505 value is interesting. If it's a valid operand that matches
8506 GIMPLE_TEST_F, we're done. Unless we are handling some
8507 post-effects internally; if that's the case, we need to copy into
8508 a temporary before adding the post-effects to POST_P. */
8509 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8510 goto out;
8512 /* Otherwise, we need to create a new temporary for the gimplified
8513 expression. */
8515 /* We can't return an lvalue if we have an internal postqueue. The
8516 object the lvalue refers to would (probably) be modified by the
8517 postqueue; we need to copy the value out first, which means an
8518 rvalue. */
8519 if ((fallback & fb_lvalue)
8520 && gimple_seq_empty_p (internal_post)
8521 && is_gimple_addressable (*expr_p))
8523 /* An lvalue will do. Take the address of the expression, store it
8524 in a temporary, and replace the expression with an INDIRECT_REF of
8525 that temporary. */
8526 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8527 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8528 *expr_p = build_simple_mem_ref (tmp);
8530 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8532 /* An rvalue will do. Assign the gimplified expression into a
8533 new temporary TMP and replace the original expression with
8534 TMP. First, make sure that the expression has a type so that
8535 it can be assigned into a temporary. */
8536 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8537 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8539 else
8541 #ifdef ENABLE_GIMPLE_CHECKING
8542 if (!(fallback & fb_mayfail))
8544 fprintf (stderr, "gimplification failed:\n");
8545 print_generic_expr (stderr, *expr_p, 0);
8546 debug_tree (*expr_p);
8547 internal_error ("gimplification failed");
8549 #endif
8550 gcc_assert (fallback & fb_mayfail);
8552 /* If this is an asm statement, and the user asked for the
8553 impossible, don't die. Fail and let gimplify_asm_expr
8554 issue an error. */
8555 ret = GS_ERROR;
8556 goto out;
8559 /* Make sure the temporary matches our predicate. */
8560 gcc_assert ((*gimple_test_f) (*expr_p));
8562 if (!gimple_seq_empty_p (internal_post))
8564 annotate_all_with_location (internal_post, input_location);
8565 gimplify_seq_add_seq (pre_p, internal_post);
8568 out:
8569 input_location = saved_location;
8570 return ret;
8573 /* Look through TYPE for variable-sized objects and gimplify each such
8574 size that we find. Add to LIST_P any statements generated. */
8576 void
8577 gimplify_type_sizes (tree type, gimple_seq *list_p)
8579 tree field, t;
8581 if (type == NULL || type == error_mark_node)
8582 return;
8584 /* We first do the main variant, then copy into any other variants. */
8585 type = TYPE_MAIN_VARIANT (type);
8587 /* Avoid infinite recursion. */
8588 if (TYPE_SIZES_GIMPLIFIED (type))
8589 return;
8591 TYPE_SIZES_GIMPLIFIED (type) = 1;
8593 switch (TREE_CODE (type))
8595 case INTEGER_TYPE:
8596 case ENUMERAL_TYPE:
8597 case BOOLEAN_TYPE:
8598 case REAL_TYPE:
8599 case FIXED_POINT_TYPE:
8600 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8601 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8603 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8605 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8606 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8608 break;
8610 case ARRAY_TYPE:
8611 /* These types may not have declarations, so handle them here. */
8612 gimplify_type_sizes (TREE_TYPE (type), list_p);
8613 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8614 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8615 with assigned stack slots, for -O1+ -g they should be tracked
8616 by VTA. */
8617 if (!(TYPE_NAME (type)
8618 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8619 && DECL_IGNORED_P (TYPE_NAME (type)))
8620 && TYPE_DOMAIN (type)
8621 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8623 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8624 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8625 DECL_IGNORED_P (t) = 0;
8626 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8627 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8628 DECL_IGNORED_P (t) = 0;
8630 break;
8632 case RECORD_TYPE:
8633 case UNION_TYPE:
8634 case QUAL_UNION_TYPE:
8635 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8636 if (TREE_CODE (field) == FIELD_DECL)
8638 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8639 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8640 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8641 gimplify_type_sizes (TREE_TYPE (field), list_p);
8643 break;
8645 case POINTER_TYPE:
8646 case REFERENCE_TYPE:
8647 /* We used to recurse on the pointed-to type here, which turned out to
8648 be incorrect because its definition might refer to variables not
8649 yet initialized at this point if a forward declaration is involved.
8651 It was actually useful for anonymous pointed-to types to ensure
8652 that the sizes evaluation dominates every possible later use of the
8653 values. Restricting to such types here would be safe since there
8654 is no possible forward declaration around, but would introduce an
8655 undesirable middle-end semantic to anonymity. We then defer to
8656 front-ends the responsibility of ensuring that the sizes are
8657 evaluated both early and late enough, e.g. by attaching artificial
8658 type declarations to the tree. */
8659 break;
8661 default:
8662 break;
8665 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8666 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8668 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8670 TYPE_SIZE (t) = TYPE_SIZE (type);
8671 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8672 TYPE_SIZES_GIMPLIFIED (t) = 1;
8676 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8677 a size or position, has had all of its SAVE_EXPRs evaluated.
8678 We add any required statements to *STMT_P. */
8680 void
8681 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8683 tree expr = *expr_p;
8685 /* We don't do anything if the value isn't there, is constant, or contains
8686 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8687 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8688 will want to replace it with a new variable, but that will cause problems
8689 if this type is from outside the function. It's OK to have that here. */
8690 if (is_gimple_sizepos (expr))
8691 return;
8693 *expr_p = unshare_expr (expr);
8695 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8698 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8699 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8700 is true, also gimplify the parameters. */
8702 gimple
8703 gimplify_body (tree fndecl, bool do_parms)
8705 location_t saved_location = input_location;
8706 gimple_seq parm_stmts, seq;
8707 gimple outer_bind;
8708 struct gimplify_ctx gctx;
8709 struct cgraph_node *cgn;
8711 timevar_push (TV_TREE_GIMPLIFY);
8713 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8714 gimplification. */
8715 default_rtl_profile ();
8717 gcc_assert (gimplify_ctxp == NULL);
8718 push_gimplify_context (&gctx);
8720 if (flag_openmp)
8722 gcc_assert (gimplify_omp_ctxp == NULL);
8723 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8724 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8727 /* Unshare most shared trees in the body and in that of any nested functions.
8728 It would seem we don't have to do this for nested functions because
8729 they are supposed to be output and then the outer function gimplified
8730 first, but the g++ front end doesn't always do it that way. */
8731 unshare_body (fndecl);
8732 unvisit_body (fndecl);
8734 cgn = cgraph_get_node (fndecl);
8735 if (cgn && cgn->origin)
8736 nonlocal_vlas = pointer_set_create ();
8738 /* Make sure input_location isn't set to something weird. */
8739 input_location = DECL_SOURCE_LOCATION (fndecl);
8741 /* Resolve callee-copies. This has to be done before processing
8742 the body so that DECL_VALUE_EXPR gets processed correctly. */
8743 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8745 /* Gimplify the function's body. */
8746 seq = NULL;
8747 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8748 outer_bind = gimple_seq_first_stmt (seq);
8749 if (!outer_bind)
8751 outer_bind = gimple_build_nop ();
8752 gimplify_seq_add_stmt (&seq, outer_bind);
8755 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8756 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8757 if (gimple_code (outer_bind) == GIMPLE_BIND
8758 && gimple_seq_first (seq) == gimple_seq_last (seq))
8760 else
8761 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8763 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8765 /* If we had callee-copies statements, insert them at the beginning
8766 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8767 if (!gimple_seq_empty_p (parm_stmts))
8769 tree parm;
8771 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8772 gimple_bind_set_body (outer_bind, parm_stmts);
8774 for (parm = DECL_ARGUMENTS (current_function_decl);
8775 parm; parm = DECL_CHAIN (parm))
8776 if (DECL_HAS_VALUE_EXPR_P (parm))
8778 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8779 DECL_IGNORED_P (parm) = 0;
8783 if (nonlocal_vlas)
8785 pointer_set_destroy (nonlocal_vlas);
8786 nonlocal_vlas = NULL;
8789 if (flag_openmp && gimplify_omp_ctxp)
8791 delete_omp_context (gimplify_omp_ctxp);
8792 gimplify_omp_ctxp = NULL;
8795 pop_gimplify_context (outer_bind);
8796 gcc_assert (gimplify_ctxp == NULL);
8798 #ifdef ENABLE_CHECKING
8799 if (!seen_error ())
8800 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8801 #endif
8803 timevar_pop (TV_TREE_GIMPLIFY);
8804 input_location = saved_location;
8806 return outer_bind;
8809 typedef char *char_p; /* For DEF_VEC_P. */
8811 /* Return whether we should exclude FNDECL from instrumentation. */
8813 bool
8814 flag_instrument_functions_exclude_p (tree fndecl)
8816 vec<char_p> *v;
8818 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8819 if (v && v->length () > 0)
8821 const char *name;
8822 int i;
8823 char *s;
8825 name = lang_hooks.decl_printable_name (fndecl, 0);
8826 FOR_EACH_VEC_ELT (*v, i, s)
8827 if (strstr (name, s) != NULL)
8828 return true;
8831 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8832 if (v && v->length () > 0)
8834 const char *name;
8835 int i;
8836 char *s;
8838 name = DECL_SOURCE_FILE (fndecl);
8839 FOR_EACH_VEC_ELT (*v, i, s)
8840 if (strstr (name, s) != NULL)
8841 return true;
8844 return false;
8847 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8848 node for the function we want to gimplify.
8850 Return the sequence of GIMPLE statements corresponding to the body
8851 of FNDECL. */
8853 void
8854 gimplify_function_tree (tree fndecl)
8856 tree parm, ret;
8857 gimple_seq seq;
8858 gimple bind;
8860 gcc_assert (!gimple_body (fndecl));
8862 if (DECL_STRUCT_FUNCTION (fndecl))
8863 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8864 else
8865 push_struct_function (fndecl);
8867 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8869 /* Preliminarily mark non-addressed complex variables as eligible
8870 for promotion to gimple registers. We'll transform their uses
8871 as we find them. */
8872 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8873 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8874 && !TREE_THIS_VOLATILE (parm)
8875 && !needs_to_live_in_memory (parm))
8876 DECL_GIMPLE_REG_P (parm) = 1;
8879 ret = DECL_RESULT (fndecl);
8880 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8881 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8882 && !needs_to_live_in_memory (ret))
8883 DECL_GIMPLE_REG_P (ret) = 1;
8885 bind = gimplify_body (fndecl, true);
8887 /* The tree body of the function is no longer needed, replace it
8888 with the new GIMPLE body. */
8889 seq = NULL;
8890 gimple_seq_add_stmt (&seq, bind);
8891 gimple_set_body (fndecl, seq);
8893 /* If we're instrumenting function entry/exit, then prepend the call to
8894 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8895 catch the exit hook. */
8896 /* ??? Add some way to ignore exceptions for this TFE. */
8897 if (flag_instrument_function_entry_exit
8898 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8899 && !flag_instrument_functions_exclude_p (fndecl))
8901 tree x;
8902 gimple new_bind;
8903 gimple tf;
8904 gimple_seq cleanup = NULL, body = NULL;
8905 tree tmp_var;
8906 gimple call;
8908 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8909 call = gimple_build_call (x, 1, integer_zero_node);
8910 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8911 gimple_call_set_lhs (call, tmp_var);
8912 gimplify_seq_add_stmt (&cleanup, call);
8913 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8914 call = gimple_build_call (x, 2,
8915 build_fold_addr_expr (current_function_decl),
8916 tmp_var);
8917 gimplify_seq_add_stmt (&cleanup, call);
8918 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8920 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8921 call = gimple_build_call (x, 1, integer_zero_node);
8922 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8923 gimple_call_set_lhs (call, tmp_var);
8924 gimplify_seq_add_stmt (&body, call);
8925 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8926 call = gimple_build_call (x, 2,
8927 build_fold_addr_expr (current_function_decl),
8928 tmp_var);
8929 gimplify_seq_add_stmt (&body, call);
8930 gimplify_seq_add_stmt (&body, tf);
8931 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8932 /* Clear the block for BIND, since it is no longer directly inside
8933 the function, but within a try block. */
8934 gimple_bind_set_block (bind, NULL);
8936 /* Replace the current function body with the body
8937 wrapped in the try/finally TF. */
8938 seq = NULL;
8939 gimple_seq_add_stmt (&seq, new_bind);
8940 gimple_set_body (fndecl, seq);
8943 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8944 cfun->curr_properties = PROP_gimple_any;
8946 pop_cfun ();
8949 /* Some transformations like inlining may invalidate the GIMPLE form
8950 for operands. This function traverses all the operands in STMT and
8951 gimplifies anything that is not a valid gimple operand. Any new
8952 GIMPLE statements are inserted before *GSI_P. */
8954 void
8955 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8957 size_t i, num_ops;
8958 tree lhs;
8959 gimple_seq pre = NULL;
8960 gimple post_stmt = NULL;
8961 struct gimplify_ctx gctx;
8963 push_gimplify_context (&gctx);
8964 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8966 switch (gimple_code (stmt))
8968 case GIMPLE_COND:
8969 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8970 is_gimple_val, fb_rvalue);
8971 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8972 is_gimple_val, fb_rvalue);
8973 break;
8974 case GIMPLE_SWITCH:
8975 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8976 is_gimple_val, fb_rvalue);
8977 break;
8978 case GIMPLE_OMP_ATOMIC_LOAD:
8979 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8980 is_gimple_val, fb_rvalue);
8981 break;
8982 case GIMPLE_ASM:
8984 size_t i, noutputs = gimple_asm_noutputs (stmt);
8985 const char *constraint, **oconstraints;
8986 bool allows_mem, allows_reg, is_inout;
8988 oconstraints
8989 = (const char **) alloca ((noutputs) * sizeof (const char *));
8990 for (i = 0; i < noutputs; i++)
8992 tree op = gimple_asm_output_op (stmt, i);
8993 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8994 oconstraints[i] = constraint;
8995 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8996 &allows_reg, &is_inout);
8997 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8998 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8999 fb_lvalue | fb_mayfail);
9001 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
9003 tree op = gimple_asm_input_op (stmt, i);
9004 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
9005 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
9006 oconstraints, &allows_mem, &allows_reg);
9007 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
9008 allows_reg = 0;
9009 if (!allows_reg && allows_mem)
9010 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9011 is_gimple_lvalue, fb_lvalue | fb_mayfail);
9012 else
9013 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9014 is_gimple_asm_val, fb_rvalue);
9017 break;
9018 default:
9019 /* NOTE: We start gimplifying operands from last to first to
9020 make sure that side-effects on the RHS of calls, assignments
9021 and ASMs are executed before the LHS. The ordering is not
9022 important for other statements. */
9023 num_ops = gimple_num_ops (stmt);
9024 for (i = num_ops; i > 0; i--)
9026 tree op = gimple_op (stmt, i - 1);
9027 if (op == NULL_TREE)
9028 continue;
9029 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
9030 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
9031 else if (i == 2
9032 && is_gimple_assign (stmt)
9033 && num_ops == 2
9034 && get_gimple_rhs_class (gimple_expr_code (stmt))
9035 == GIMPLE_SINGLE_RHS)
9036 gimplify_expr (&op, &pre, NULL,
9037 rhs_predicate_for (gimple_assign_lhs (stmt)),
9038 fb_rvalue);
9039 else if (i == 2 && is_gimple_call (stmt))
9041 if (TREE_CODE (op) == FUNCTION_DECL)
9042 continue;
9043 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
9045 else
9046 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
9047 gimple_set_op (stmt, i - 1, op);
9050 lhs = gimple_get_lhs (stmt);
9051 /* If the LHS changed it in a way that requires a simple RHS,
9052 create temporary. */
9053 if (lhs && !is_gimple_reg (lhs))
9055 bool need_temp = false;
9057 if (is_gimple_assign (stmt)
9058 && num_ops == 2
9059 && get_gimple_rhs_class (gimple_expr_code (stmt))
9060 == GIMPLE_SINGLE_RHS)
9061 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
9062 rhs_predicate_for (gimple_assign_lhs (stmt)),
9063 fb_rvalue);
9064 else if (is_gimple_reg (lhs))
9066 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9068 if (is_gimple_call (stmt))
9070 i = gimple_call_flags (stmt);
9071 if ((i & ECF_LOOPING_CONST_OR_PURE)
9072 || !(i & (ECF_CONST | ECF_PURE)))
9073 need_temp = true;
9075 if (stmt_can_throw_internal (stmt))
9076 need_temp = true;
9079 else
9081 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9082 need_temp = true;
9083 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
9085 if (is_gimple_call (stmt))
9087 tree fndecl = gimple_call_fndecl (stmt);
9089 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
9090 && !(fndecl && DECL_RESULT (fndecl)
9091 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
9092 need_temp = true;
9094 else
9095 need_temp = true;
9098 if (need_temp)
9100 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
9101 if (gimple_in_ssa_p (cfun))
9102 temp = make_ssa_name (temp, NULL);
9103 gimple_set_lhs (stmt, temp);
9104 post_stmt = gimple_build_assign (lhs, temp);
9105 if (TREE_CODE (lhs) == SSA_NAME)
9106 SSA_NAME_DEF_STMT (lhs) = post_stmt;
9109 break;
9112 if (!gimple_seq_empty_p (pre))
9113 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
9114 if (post_stmt)
9115 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
9117 pop_gimplify_context (NULL);
9120 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
9121 the predicate that will hold for the result. If VAR is not NULL, make the
9122 base variable of the final destination be VAR if suitable. */
9124 tree
9125 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
9126 gimple_predicate gimple_test_f, tree var)
9128 enum gimplify_status ret;
9129 struct gimplify_ctx gctx;
9130 location_t saved_location;
9132 *stmts = NULL;
9134 /* gimple_test_f might be more strict than is_gimple_val, make
9135 sure we pass both. Just checking gimple_test_f doesn't work
9136 because most gimple predicates do not work recursively. */
9137 if (is_gimple_val (expr)
9138 && (*gimple_test_f) (expr))
9139 return expr;
9141 push_gimplify_context (&gctx);
9142 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
9143 gimplify_ctxp->allow_rhs_cond_expr = true;
9144 saved_location = input_location;
9145 input_location = UNKNOWN_LOCATION;
9147 if (var)
9149 if (gimplify_ctxp->into_ssa
9150 && is_gimple_reg (var))
9151 var = make_ssa_name (var, NULL);
9152 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
9155 if (TREE_CODE (expr) != MODIFY_EXPR
9156 && TREE_TYPE (expr) == void_type_node)
9158 gimplify_and_add (expr, stmts);
9159 expr = NULL_TREE;
9161 else
9163 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
9164 gcc_assert (ret != GS_ERROR);
9167 input_location = saved_location;
9168 pop_gimplify_context (NULL);
9170 return expr;
9173 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
9174 force the result to be either ssa_name or an invariant, otherwise
9175 just force it to be a rhs expression. If VAR is not NULL, make the
9176 base variable of the final destination be VAR if suitable. */
9178 tree
9179 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
9181 return force_gimple_operand_1 (expr, stmts,
9182 simple ? is_gimple_val : is_gimple_reg_rhs,
9183 var);
9186 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
9187 and VAR. If some statements are produced, emits them at GSI.
9188 If BEFORE is true. the statements are appended before GSI, otherwise
9189 they are appended after it. M specifies the way GSI moves after
9190 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
9192 tree
9193 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
9194 gimple_predicate gimple_test_f,
9195 tree var, bool before,
9196 enum gsi_iterator_update m)
9198 gimple_seq stmts;
9200 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
9202 if (!gimple_seq_empty_p (stmts))
9204 if (before)
9205 gsi_insert_seq_before (gsi, stmts, m);
9206 else
9207 gsi_insert_seq_after (gsi, stmts, m);
9210 return expr;
9213 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
9214 If SIMPLE is true, force the result to be either ssa_name or an invariant,
9215 otherwise just force it to be a rhs expression. If some statements are
9216 produced, emits them at GSI. If BEFORE is true, the statements are
9217 appended before GSI, otherwise they are appended after it. M specifies
9218 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
9219 are the usual values). */
9221 tree
9222 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
9223 bool simple_p, tree var, bool before,
9224 enum gsi_iterator_update m)
9226 return force_gimple_operand_gsi_1 (gsi, expr,
9227 simple_p
9228 ? is_gimple_val : is_gimple_reg_rhs,
9229 var, before, m);
9232 #ifndef PAD_VARARGS_DOWN
9233 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
9234 #endif
9236 /* Build an indirect-ref expression over the given TREE, which represents a
9237 piece of a va_arg() expansion. */
9238 tree
9239 build_va_arg_indirect_ref (tree addr)
9241 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
9243 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
9244 mf_mark (addr);
9246 return addr;
9249 /* The "standard" implementation of va_arg: read the value from the
9250 current (padded) address and increment by the (padded) size. */
9252 tree
9253 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
9254 gimple_seq *post_p)
9256 tree addr, t, type_size, rounded_size, valist_tmp;
9257 unsigned HOST_WIDE_INT align, boundary;
9258 bool indirect;
9260 #ifdef ARGS_GROW_DOWNWARD
9261 /* All of the alignment and movement below is for args-grow-up machines.
9262 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
9263 implement their own specialized gimplify_va_arg_expr routines. */
9264 gcc_unreachable ();
9265 #endif
9267 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
9268 if (indirect)
9269 type = build_pointer_type (type);
9271 align = PARM_BOUNDARY / BITS_PER_UNIT;
9272 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
9274 /* When we align parameter on stack for caller, if the parameter
9275 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
9276 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
9277 here with caller. */
9278 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9279 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9281 boundary /= BITS_PER_UNIT;
9283 /* Hoist the valist value into a temporary for the moment. */
9284 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9286 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
9287 requires greater alignment, we must perform dynamic alignment. */
9288 if (boundary > align
9289 && !integer_zerop (TYPE_SIZE (type)))
9291 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9292 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9293 gimplify_and_add (t, pre_p);
9295 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9296 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
9297 valist_tmp,
9298 build_int_cst (TREE_TYPE (valist), -boundary)));
9299 gimplify_and_add (t, pre_p);
9301 else
9302 boundary = align;
9304 /* If the actual alignment is less than the alignment of the type,
9305 adjust the type accordingly so that we don't assume strict alignment
9306 when dereferencing the pointer. */
9307 boundary *= BITS_PER_UNIT;
9308 if (boundary < TYPE_ALIGN (type))
9310 type = build_variant_type_copy (type);
9311 TYPE_ALIGN (type) = boundary;
9314 /* Compute the rounded size of the type. */
9315 type_size = size_in_bytes (type);
9316 rounded_size = round_up (type_size, align);
9318 /* Reduce rounded_size so it's sharable with the postqueue. */
9319 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
9321 /* Get AP. */
9322 addr = valist_tmp;
9323 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
9325 /* Small args are padded downward. */
9326 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
9327 rounded_size, size_int (align));
9328 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
9329 size_binop (MINUS_EXPR, rounded_size, type_size));
9330 addr = fold_build_pointer_plus (addr, t);
9333 /* Compute new value for AP. */
9334 t = fold_build_pointer_plus (valist_tmp, rounded_size);
9335 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9336 gimplify_and_add (t, pre_p);
9338 addr = fold_convert (build_pointer_type (type), addr);
9340 if (indirect)
9341 addr = build_va_arg_indirect_ref (addr);
9343 return build_va_arg_indirect_ref (addr);
9346 #include "gt-gimplify.h"