2013-10-30 Balaji V. Iyer <balaji.v.iyer@intel.com>
[official-gcc.git] / gcc / gimplify.c
blob1f18466fa8786ced2ca8f7fbac737471551fb381
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 "bitmap.h"
34 #include "gimple-ssa.h"
35 #include "cgraph.h"
36 #include "tree-cfg.h"
37 #include "tree-ssanames.h"
38 #include "tree-ssa.h"
39 #include "timevar.h"
40 #include "hashtab.h"
41 #include "flags.h"
42 #include "function.h"
43 #include "ggc.h"
44 #include "diagnostic-core.h"
45 #include "target.h"
46 #include "pointer-set.h"
47 #include "splay-tree.h"
48 #include "vec.h"
49 #include "omp-low.h"
50 #include "gimple-low.h"
51 #include "cilk.h"
53 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
54 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
55 #include "expr.h"
56 #include "tm_p.h"
58 enum gimplify_omp_var_data
60 GOVD_SEEN = 1,
61 GOVD_EXPLICIT = 2,
62 GOVD_SHARED = 4,
63 GOVD_PRIVATE = 8,
64 GOVD_FIRSTPRIVATE = 16,
65 GOVD_LASTPRIVATE = 32,
66 GOVD_REDUCTION = 64,
67 GOVD_LOCAL = 128,
68 GOVD_MAP = 256,
69 GOVD_DEBUG_PRIVATE = 512,
70 GOVD_PRIVATE_OUTER_REF = 1024,
71 GOVD_LINEAR = 2048,
72 GOVD_ALIGNED = 4096,
73 GOVD_MAP_TO_ONLY = 8192,
74 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
75 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LINEAR
76 | GOVD_LOCAL)
80 enum omp_region_type
82 ORT_WORKSHARE = 0,
83 ORT_SIMD = 1,
84 ORT_PARALLEL = 2,
85 ORT_COMBINED_PARALLEL = 3,
86 ORT_TASK = 4,
87 ORT_UNTIED_TASK = 5,
88 ORT_TEAMS = 8,
89 ORT_TARGET_DATA = 16,
90 ORT_TARGET = 32
93 struct gimplify_omp_ctx
95 struct gimplify_omp_ctx *outer_context;
96 splay_tree variables;
97 struct pointer_set_t *privatized_types;
98 location_t location;
99 enum omp_clause_default_kind default_kind;
100 enum omp_region_type region_type;
101 bool combined_loop;
104 static struct gimplify_ctx *gimplify_ctxp;
105 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
108 /* Forward declaration. */
109 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
111 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
112 form and we don't do any syntax checking. */
114 void
115 mark_addressable (tree x)
117 while (handled_component_p (x))
118 x = TREE_OPERAND (x, 0);
119 if (TREE_CODE (x) == MEM_REF
120 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
121 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
122 if (TREE_CODE (x) != VAR_DECL
123 && TREE_CODE (x) != PARM_DECL
124 && TREE_CODE (x) != RESULT_DECL)
125 return;
126 TREE_ADDRESSABLE (x) = 1;
128 /* Also mark the artificial SSA_NAME that points to the partition of X. */
129 if (TREE_CODE (x) == VAR_DECL
130 && !DECL_EXTERNAL (x)
131 && !TREE_STATIC (x)
132 && cfun->gimple_df != NULL
133 && cfun->gimple_df->decls_to_pointers != NULL)
135 void *namep
136 = pointer_map_contains (cfun->gimple_df->decls_to_pointers, x);
137 if (namep)
138 TREE_ADDRESSABLE (*(tree *)namep) = 1;
142 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
143 *SEQ_P is NULL, a new sequence is allocated. This function is
144 similar to gimple_seq_add_stmt, but does not scan the operands.
145 During gimplification, we need to manipulate statement sequences
146 before the def/use vectors have been constructed. */
148 void
149 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
151 gimple_stmt_iterator si;
153 if (gs == NULL)
154 return;
156 si = gsi_last (*seq_p);
157 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
160 /* Shorter alias name for the above function for use in gimplify.c
161 only. */
163 static inline void
164 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
166 gimple_seq_add_stmt_without_update (seq_p, gs);
169 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
170 NULL, a new sequence is allocated. This function is
171 similar to gimple_seq_add_seq, but does not scan the operands.
172 During gimplification, we need to manipulate statement sequences
173 before the def/use vectors have been constructed. */
175 static void
176 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
178 gimple_stmt_iterator si;
180 if (src == NULL)
181 return;
183 si = gsi_last (*dst_p);
184 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
187 /* Set up a context for the gimplifier. */
189 void
190 push_gimplify_context (struct gimplify_ctx *c)
192 memset (c, '\0', sizeof (*c));
193 c->prev_context = gimplify_ctxp;
194 gimplify_ctxp = c;
197 /* Tear down a context for the gimplifier. If BODY is non-null, then
198 put the temporaries into the outer BIND_EXPR. Otherwise, put them
199 in the local_decls.
201 BODY is not a sequence, but the first tuple in a sequence. */
203 void
204 pop_gimplify_context (gimple body)
206 struct gimplify_ctx *c = gimplify_ctxp;
208 gcc_assert (c
209 && (!c->bind_expr_stack.exists ()
210 || c->bind_expr_stack.is_empty ()));
211 c->bind_expr_stack.release ();
212 gimplify_ctxp = c->prev_context;
214 if (body)
215 declare_vars (c->temps, body, false);
216 else
217 record_vars (c->temps);
219 if (c->temp_htab.is_created ())
220 c->temp_htab.dispose ();
223 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
225 static void
226 gimple_push_bind_expr (gimple gimple_bind)
228 gimplify_ctxp->bind_expr_stack.reserve (8);
229 gimplify_ctxp->bind_expr_stack.safe_push (gimple_bind);
232 /* Pop the first element off the stack of bindings. */
234 static void
235 gimple_pop_bind_expr (void)
237 gimplify_ctxp->bind_expr_stack.pop ();
240 /* Return the first element of the stack of bindings. */
242 gimple
243 gimple_current_bind_expr (void)
245 return gimplify_ctxp->bind_expr_stack.last ();
248 /* Return the stack of bindings created during gimplification. */
250 vec<gimple>
251 gimple_bind_expr_stack (void)
253 return gimplify_ctxp->bind_expr_stack;
256 /* Return true iff there is a COND_EXPR between us and the innermost
257 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
259 static bool
260 gimple_conditional_context (void)
262 return gimplify_ctxp->conditions > 0;
265 /* Note that we've entered a COND_EXPR. */
267 static void
268 gimple_push_condition (void)
270 #ifdef ENABLE_GIMPLE_CHECKING
271 if (gimplify_ctxp->conditions == 0)
272 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
273 #endif
274 ++(gimplify_ctxp->conditions);
277 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
278 now, add any conditional cleanups we've seen to the prequeue. */
280 static void
281 gimple_pop_condition (gimple_seq *pre_p)
283 int conds = --(gimplify_ctxp->conditions);
285 gcc_assert (conds >= 0);
286 if (conds == 0)
288 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
289 gimplify_ctxp->conditional_cleanups = NULL;
293 /* A stable comparison routine for use with splay trees and DECLs. */
295 static int
296 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
298 tree a = (tree) xa;
299 tree b = (tree) xb;
301 return DECL_UID (a) - DECL_UID (b);
304 /* Create a new omp construct that deals with variable remapping. */
306 static struct gimplify_omp_ctx *
307 new_omp_context (enum omp_region_type region_type)
309 struct gimplify_omp_ctx *c;
311 c = XCNEW (struct gimplify_omp_ctx);
312 c->outer_context = gimplify_omp_ctxp;
313 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
314 c->privatized_types = pointer_set_create ();
315 c->location = input_location;
316 c->region_type = region_type;
317 if ((region_type & ORT_TASK) == 0)
318 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
319 else
320 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
322 return c;
325 /* Destroy an omp construct that deals with variable remapping. */
327 static void
328 delete_omp_context (struct gimplify_omp_ctx *c)
330 splay_tree_delete (c->variables);
331 pointer_set_destroy (c->privatized_types);
332 XDELETE (c);
335 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
336 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
338 /* Both gimplify the statement T and append it to *SEQ_P. This function
339 behaves exactly as gimplify_stmt, but you don't have to pass T as a
340 reference. */
342 void
343 gimplify_and_add (tree t, gimple_seq *seq_p)
345 gimplify_stmt (&t, seq_p);
348 /* Gimplify statement T into sequence *SEQ_P, and return the first
349 tuple in the sequence of generated tuples for this statement.
350 Return NULL if gimplifying T produced no tuples. */
352 static gimple
353 gimplify_and_return_first (tree t, gimple_seq *seq_p)
355 gimple_stmt_iterator last = gsi_last (*seq_p);
357 gimplify_and_add (t, seq_p);
359 if (!gsi_end_p (last))
361 gsi_next (&last);
362 return gsi_stmt (last);
364 else
365 return gimple_seq_first_stmt (*seq_p);
368 /* Strip off a legitimate source ending from the input string NAME of
369 length LEN. Rather than having to know the names used by all of
370 our front ends, we strip off an ending of a period followed by
371 up to five characters. (Java uses ".class".) */
373 static inline void
374 remove_suffix (char *name, int len)
376 int i;
378 for (i = 2; i < 8 && len > i; i++)
380 if (name[len - i] == '.')
382 name[len - i] = '\0';
383 break;
388 /* Create a new temporary name with PREFIX. Return an identifier. */
390 static GTY(()) unsigned int tmp_var_id_num;
392 tree
393 create_tmp_var_name (const char *prefix)
395 char *tmp_name;
397 if (prefix)
399 char *preftmp = ASTRDUP (prefix);
401 remove_suffix (preftmp, strlen (preftmp));
402 clean_symbol_name (preftmp);
404 prefix = preftmp;
407 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
408 return get_identifier (tmp_name);
411 /* Create a new temporary variable declaration of type TYPE.
412 Do NOT push it into the current binding. */
414 tree
415 create_tmp_var_raw (tree type, const char *prefix)
417 tree tmp_var;
419 tmp_var = build_decl (input_location,
420 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
421 type);
423 /* The variable was declared by the compiler. */
424 DECL_ARTIFICIAL (tmp_var) = 1;
425 /* And we don't want debug info for it. */
426 DECL_IGNORED_P (tmp_var) = 1;
428 /* Make the variable writable. */
429 TREE_READONLY (tmp_var) = 0;
431 DECL_EXTERNAL (tmp_var) = 0;
432 TREE_STATIC (tmp_var) = 0;
433 TREE_USED (tmp_var) = 1;
435 return tmp_var;
438 /* Create a new temporary variable declaration of type TYPE. DO push the
439 variable into the current binding. Further, assume that this is called
440 only from gimplification or optimization, at which point the creation of
441 certain types are bugs. */
443 tree
444 create_tmp_var (tree type, const char *prefix)
446 tree tmp_var;
448 /* We don't allow types that are addressable (meaning we can't make copies),
449 or incomplete. We also used to reject every variable size objects here,
450 but now support those for which a constant upper bound can be obtained.
451 The processing for variable sizes is performed in gimple_add_tmp_var,
452 point at which it really matters and possibly reached via paths not going
453 through this function, e.g. after direct calls to create_tmp_var_raw. */
454 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
456 tmp_var = create_tmp_var_raw (type, prefix);
457 gimple_add_tmp_var (tmp_var);
458 return tmp_var;
461 /* Create a new temporary variable declaration of type TYPE by calling
462 create_tmp_var and if TYPE is a vector or a complex number, mark the new
463 temporary as gimple register. */
465 tree
466 create_tmp_reg (tree type, const char *prefix)
468 tree tmp;
470 tmp = create_tmp_var (type, prefix);
471 if (TREE_CODE (type) == COMPLEX_TYPE
472 || TREE_CODE (type) == VECTOR_TYPE)
473 DECL_GIMPLE_REG_P (tmp) = 1;
475 return tmp;
478 /* Returns true iff T is a valid RHS for an assignment to a renamed
479 user -- or front-end generated artificial -- variable. */
481 static bool
482 is_gimple_reg_rhs (tree t)
484 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
487 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
488 LHS, or for a call argument. */
490 static bool
491 is_gimple_mem_rhs (tree t)
493 /* If we're dealing with a renamable type, either source or dest must be
494 a renamed variable. */
495 if (is_gimple_reg_type (TREE_TYPE (t)))
496 return is_gimple_val (t);
497 else
498 return is_gimple_val (t) || is_gimple_lvalue (t);
501 /* Return true if T is a CALL_EXPR or an expression that can be
502 assigned to a temporary. Note that this predicate should only be
503 used during gimplification. See the rationale for this in
504 gimplify_modify_expr. */
506 static bool
507 is_gimple_reg_rhs_or_call (tree t)
509 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
510 || TREE_CODE (t) == CALL_EXPR);
513 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
514 this predicate should only be used during gimplification. See the
515 rationale for this in gimplify_modify_expr. */
517 static bool
518 is_gimple_mem_rhs_or_call (tree t)
520 /* If we're dealing with a renamable type, either source or dest must be
521 a renamed variable. */
522 if (is_gimple_reg_type (TREE_TYPE (t)))
523 return is_gimple_val (t);
524 else
525 return (is_gimple_val (t) || is_gimple_lvalue (t)
526 || TREE_CODE (t) == CALL_EXPR);
529 /* Create a temporary with a name derived from VAL. Subroutine of
530 lookup_tmp_var; nobody else should call this function. */
532 static inline tree
533 create_tmp_from_val (tree val, bool is_formal)
535 /* Drop all qualifiers and address-space information from the value type. */
536 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (val));
537 tree var = create_tmp_var (type, get_name (val));
538 if (is_formal
539 && (TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE
540 || TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE))
541 DECL_GIMPLE_REG_P (var) = 1;
542 return var;
545 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
546 an existing expression temporary. */
548 static tree
549 lookup_tmp_var (tree val, bool is_formal)
551 tree ret;
553 /* If not optimizing, never really reuse a temporary. local-alloc
554 won't allocate any variable that is used in more than one basic
555 block, which means it will go into memory, causing much extra
556 work in reload and final and poorer code generation, outweighing
557 the extra memory allocation here. */
558 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
559 ret = create_tmp_from_val (val, is_formal);
560 else
562 elt_t elt, *elt_p;
563 elt_t **slot;
565 elt.val = val;
566 if (!gimplify_ctxp->temp_htab.is_created ())
567 gimplify_ctxp->temp_htab.create (1000);
568 slot = gimplify_ctxp->temp_htab.find_slot (&elt, INSERT);
569 if (*slot == NULL)
571 elt_p = XNEW (elt_t);
572 elt_p->val = val;
573 elt_p->temp = ret = create_tmp_from_val (val, is_formal);
574 *slot = elt_p;
576 else
578 elt_p = *slot;
579 ret = elt_p->temp;
583 return ret;
586 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
588 static tree
589 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
590 bool is_formal)
592 tree t, mod;
594 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
595 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
596 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
597 fb_rvalue);
599 if (gimplify_ctxp->into_ssa
600 && is_gimple_reg_type (TREE_TYPE (val)))
601 t = make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val)), NULL);
602 else
603 t = lookup_tmp_var (val, is_formal);
605 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
607 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
609 /* gimplify_modify_expr might want to reduce this further. */
610 gimplify_and_add (mod, pre_p);
611 ggc_free (mod);
613 return t;
616 /* Return a formal temporary variable initialized with VAL. PRE_P is as
617 in gimplify_expr. Only use this function if:
619 1) The value of the unfactored expression represented by VAL will not
620 change between the initialization and use of the temporary, and
621 2) The temporary will not be otherwise modified.
623 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
624 and #2 means it is inappropriate for && temps.
626 For other cases, use get_initialized_tmp_var instead. */
628 tree
629 get_formal_tmp_var (tree val, gimple_seq *pre_p)
631 return internal_get_tmp_var (val, pre_p, NULL, true);
634 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
635 are as in gimplify_expr. */
637 tree
638 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
640 return internal_get_tmp_var (val, pre_p, post_p, false);
643 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
644 generate debug info for them; otherwise don't. */
646 void
647 declare_vars (tree vars, gimple scope, bool debug_info)
649 tree last = vars;
650 if (last)
652 tree temps, block;
654 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
656 temps = nreverse (last);
658 block = gimple_bind_block (scope);
659 gcc_assert (!block || TREE_CODE (block) == BLOCK);
660 if (!block || !debug_info)
662 DECL_CHAIN (last) = gimple_bind_vars (scope);
663 gimple_bind_set_vars (scope, temps);
665 else
667 /* We need to attach the nodes both to the BIND_EXPR and to its
668 associated BLOCK for debugging purposes. The key point here
669 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
670 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
671 if (BLOCK_VARS (block))
672 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
673 else
675 gimple_bind_set_vars (scope,
676 chainon (gimple_bind_vars (scope), temps));
677 BLOCK_VARS (block) = temps;
683 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
684 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
685 no such upper bound can be obtained. */
687 static void
688 force_constant_size (tree var)
690 /* The only attempt we make is by querying the maximum size of objects
691 of the variable's type. */
693 HOST_WIDE_INT max_size;
695 gcc_assert (TREE_CODE (var) == VAR_DECL);
697 max_size = max_int_size_in_bytes (TREE_TYPE (var));
699 gcc_assert (max_size >= 0);
701 DECL_SIZE_UNIT (var)
702 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
703 DECL_SIZE (var)
704 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
707 /* Push the temporary variable TMP into the current binding. */
709 void
710 gimple_add_tmp_var (tree tmp)
712 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
714 /* Later processing assumes that the object size is constant, which might
715 not be true at this point. Force the use of a constant upper bound in
716 this case. */
717 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
718 force_constant_size (tmp);
720 DECL_CONTEXT (tmp) = current_function_decl;
721 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
723 if (gimplify_ctxp)
725 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
726 gimplify_ctxp->temps = tmp;
728 /* Mark temporaries local within the nearest enclosing parallel. */
729 if (gimplify_omp_ctxp)
731 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
732 while (ctx
733 && (ctx->region_type == ORT_WORKSHARE
734 || ctx->region_type == ORT_SIMD))
735 ctx = ctx->outer_context;
736 if (ctx)
737 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
740 else if (cfun)
741 record_vars (tmp);
742 else
744 gimple_seq body_seq;
746 /* This case is for nested functions. We need to expose the locals
747 they create. */
748 body_seq = gimple_body (current_function_decl);
749 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
753 /* Determine whether to assign a location to the statement GS. */
755 static bool
756 should_carry_location_p (gimple gs)
758 /* Don't emit a line note for a label. We particularly don't want to
759 emit one for the break label, since it doesn't actually correspond
760 to the beginning of the loop/switch. */
761 if (gimple_code (gs) == GIMPLE_LABEL)
762 return false;
764 return true;
767 /* Return true if a location should not be emitted for this statement
768 by annotate_one_with_location. */
770 static inline bool
771 gimple_do_not_emit_location_p (gimple g)
773 return gimple_plf (g, GF_PLF_1);
776 /* Mark statement G so a location will not be emitted by
777 annotate_one_with_location. */
779 static inline void
780 gimple_set_do_not_emit_location (gimple g)
782 /* The PLF flags are initialized to 0 when a new tuple is created,
783 so no need to initialize it anywhere. */
784 gimple_set_plf (g, GF_PLF_1, true);
787 /* Set the location for gimple statement GS to LOCATION. */
789 static void
790 annotate_one_with_location (gimple gs, location_t location)
792 if (!gimple_has_location (gs)
793 && !gimple_do_not_emit_location_p (gs)
794 && should_carry_location_p (gs))
795 gimple_set_location (gs, location);
798 /* Set LOCATION for all the statements after iterator GSI in sequence
799 SEQ. If GSI is pointing to the end of the sequence, start with the
800 first statement in SEQ. */
802 static void
803 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
804 location_t location)
806 if (gsi_end_p (gsi))
807 gsi = gsi_start (seq);
808 else
809 gsi_next (&gsi);
811 for (; !gsi_end_p (gsi); gsi_next (&gsi))
812 annotate_one_with_location (gsi_stmt (gsi), location);
815 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
817 void
818 annotate_all_with_location (gimple_seq stmt_p, location_t location)
820 gimple_stmt_iterator i;
822 if (gimple_seq_empty_p (stmt_p))
823 return;
825 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
827 gimple gs = gsi_stmt (i);
828 annotate_one_with_location (gs, location);
832 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
833 nodes that are referenced more than once in GENERIC functions. This is
834 necessary because gimplification (translation into GIMPLE) is performed
835 by modifying tree nodes in-place, so gimplication of a shared node in a
836 first context could generate an invalid GIMPLE form in a second context.
838 This is achieved with a simple mark/copy/unmark algorithm that walks the
839 GENERIC representation top-down, marks nodes with TREE_VISITED the first
840 time it encounters them, duplicates them if they already have TREE_VISITED
841 set, and finally removes the TREE_VISITED marks it has set.
843 The algorithm works only at the function level, i.e. it generates a GENERIC
844 representation of a function with no nodes shared within the function when
845 passed a GENERIC function (except for nodes that are allowed to be shared).
847 At the global level, it is also necessary to unshare tree nodes that are
848 referenced in more than one function, for the same aforementioned reason.
849 This requires some cooperation from the front-end. There are 2 strategies:
851 1. Manual unsharing. The front-end needs to call unshare_expr on every
852 expression that might end up being shared across functions.
854 2. Deep unsharing. This is an extension of regular unsharing. Instead
855 of calling unshare_expr on expressions that might be shared across
856 functions, the front-end pre-marks them with TREE_VISITED. This will
857 ensure that they are unshared on the first reference within functions
858 when the regular unsharing algorithm runs. The counterpart is that
859 this algorithm must look deeper than for manual unsharing, which is
860 specified by LANG_HOOKS_DEEP_UNSHARING.
862 If there are only few specific cases of node sharing across functions, it is
863 probably easier for a front-end to unshare the expressions manually. On the
864 contrary, if the expressions generated at the global level are as widespread
865 as expressions generated within functions, deep unsharing is very likely the
866 way to go. */
868 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
869 These nodes model computations that must be done once. If we were to
870 unshare something like SAVE_EXPR(i++), the gimplification process would
871 create wrong code. However, if DATA is non-null, it must hold a pointer
872 set that is used to unshare the subtrees of these nodes. */
874 static tree
875 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
877 tree t = *tp;
878 enum tree_code code = TREE_CODE (t);
880 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
881 copy their subtrees if we can make sure to do it only once. */
882 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
884 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
886 else
887 *walk_subtrees = 0;
890 /* Stop at types, decls, constants like copy_tree_r. */
891 else if (TREE_CODE_CLASS (code) == tcc_type
892 || TREE_CODE_CLASS (code) == tcc_declaration
893 || TREE_CODE_CLASS (code) == tcc_constant
894 /* We can't do anything sensible with a BLOCK used as an
895 expression, but we also can't just die when we see it
896 because of non-expression uses. So we avert our eyes
897 and cross our fingers. Silly Java. */
898 || code == BLOCK)
899 *walk_subtrees = 0;
901 /* Cope with the statement expression extension. */
902 else if (code == STATEMENT_LIST)
905 /* Leave the bulk of the work to copy_tree_r itself. */
906 else
907 copy_tree_r (tp, walk_subtrees, NULL);
909 return NULL_TREE;
912 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
913 If *TP has been visited already, then *TP is deeply copied by calling
914 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
916 static tree
917 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
919 tree t = *tp;
920 enum tree_code code = TREE_CODE (t);
922 /* Skip types, decls, and constants. But we do want to look at their
923 types and the bounds of types. Mark them as visited so we properly
924 unmark their subtrees on the unmark pass. If we've already seen them,
925 don't look down further. */
926 if (TREE_CODE_CLASS (code) == tcc_type
927 || TREE_CODE_CLASS (code) == tcc_declaration
928 || TREE_CODE_CLASS (code) == tcc_constant)
930 if (TREE_VISITED (t))
931 *walk_subtrees = 0;
932 else
933 TREE_VISITED (t) = 1;
936 /* If this node has been visited already, unshare it and don't look
937 any deeper. */
938 else if (TREE_VISITED (t))
940 walk_tree (tp, mostly_copy_tree_r, data, NULL);
941 *walk_subtrees = 0;
944 /* Otherwise, mark the node as visited and keep looking. */
945 else
946 TREE_VISITED (t) = 1;
948 return NULL_TREE;
951 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
952 copy_if_shared_r callback unmodified. */
954 static inline void
955 copy_if_shared (tree *tp, void *data)
957 walk_tree (tp, copy_if_shared_r, data, NULL);
960 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
961 any nested functions. */
963 static void
964 unshare_body (tree fndecl)
966 struct cgraph_node *cgn = cgraph_get_node (fndecl);
967 /* If the language requires deep unsharing, we need a pointer set to make
968 sure we don't repeatedly unshare subtrees of unshareable nodes. */
969 struct pointer_set_t *visited
970 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
972 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
973 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
974 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
976 if (visited)
977 pointer_set_destroy (visited);
979 if (cgn)
980 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
981 unshare_body (cgn->decl);
984 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
985 Subtrees are walked until the first unvisited node is encountered. */
987 static tree
988 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
990 tree t = *tp;
992 /* If this node has been visited, unmark it and keep looking. */
993 if (TREE_VISITED (t))
994 TREE_VISITED (t) = 0;
996 /* Otherwise, don't look any deeper. */
997 else
998 *walk_subtrees = 0;
1000 return NULL_TREE;
1003 /* Unmark the visited trees rooted at *TP. */
1005 static inline void
1006 unmark_visited (tree *tp)
1008 walk_tree (tp, unmark_visited_r, NULL, NULL);
1011 /* Likewise, but mark all trees as not visited. */
1013 static void
1014 unvisit_body (tree fndecl)
1016 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1018 unmark_visited (&DECL_SAVED_TREE (fndecl));
1019 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1020 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1022 if (cgn)
1023 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1024 unvisit_body (cgn->decl);
1027 /* Unconditionally make an unshared copy of EXPR. This is used when using
1028 stored expressions which span multiple functions, such as BINFO_VTABLE,
1029 as the normal unsharing process can't tell that they're shared. */
1031 tree
1032 unshare_expr (tree expr)
1034 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1035 return expr;
1038 /* Worker for unshare_expr_without_location. */
1040 static tree
1041 prune_expr_location (tree *tp, int *walk_subtrees, void *)
1043 if (EXPR_P (*tp))
1044 SET_EXPR_LOCATION (*tp, UNKNOWN_LOCATION);
1045 else
1046 *walk_subtrees = 0;
1047 return NULL_TREE;
1050 /* Similar to unshare_expr but also prune all expression locations
1051 from EXPR. */
1053 tree
1054 unshare_expr_without_location (tree expr)
1056 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1057 if (EXPR_P (expr))
1058 walk_tree (&expr, prune_expr_location, NULL, NULL);
1059 return expr;
1062 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1063 contain statements and have a value. Assign its value to a temporary
1064 and give it void_type_node. Return the temporary, or NULL_TREE if
1065 WRAPPER was already void. */
1067 tree
1068 voidify_wrapper_expr (tree wrapper, tree temp)
1070 tree type = TREE_TYPE (wrapper);
1071 if (type && !VOID_TYPE_P (type))
1073 tree *p;
1075 /* Set p to point to the body of the wrapper. Loop until we find
1076 something that isn't a wrapper. */
1077 for (p = &wrapper; p && *p; )
1079 switch (TREE_CODE (*p))
1081 case BIND_EXPR:
1082 TREE_SIDE_EFFECTS (*p) = 1;
1083 TREE_TYPE (*p) = void_type_node;
1084 /* For a BIND_EXPR, the body is operand 1. */
1085 p = &BIND_EXPR_BODY (*p);
1086 break;
1088 case CLEANUP_POINT_EXPR:
1089 case TRY_FINALLY_EXPR:
1090 case TRY_CATCH_EXPR:
1091 TREE_SIDE_EFFECTS (*p) = 1;
1092 TREE_TYPE (*p) = void_type_node;
1093 p = &TREE_OPERAND (*p, 0);
1094 break;
1096 case STATEMENT_LIST:
1098 tree_stmt_iterator i = tsi_last (*p);
1099 TREE_SIDE_EFFECTS (*p) = 1;
1100 TREE_TYPE (*p) = void_type_node;
1101 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1103 break;
1105 case COMPOUND_EXPR:
1106 /* Advance to the last statement. Set all container types to
1107 void. */
1108 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1110 TREE_SIDE_EFFECTS (*p) = 1;
1111 TREE_TYPE (*p) = void_type_node;
1113 break;
1115 case TRANSACTION_EXPR:
1116 TREE_SIDE_EFFECTS (*p) = 1;
1117 TREE_TYPE (*p) = void_type_node;
1118 p = &TRANSACTION_EXPR_BODY (*p);
1119 break;
1121 default:
1122 /* Assume that any tree upon which voidify_wrapper_expr is
1123 directly called is a wrapper, and that its body is op0. */
1124 if (p == &wrapper)
1126 TREE_SIDE_EFFECTS (*p) = 1;
1127 TREE_TYPE (*p) = void_type_node;
1128 p = &TREE_OPERAND (*p, 0);
1129 break;
1131 goto out;
1135 out:
1136 if (p == NULL || IS_EMPTY_STMT (*p))
1137 temp = NULL_TREE;
1138 else if (temp)
1140 /* The wrapper is on the RHS of an assignment that we're pushing
1141 down. */
1142 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1143 || TREE_CODE (temp) == MODIFY_EXPR);
1144 TREE_OPERAND (temp, 1) = *p;
1145 *p = temp;
1147 else
1149 temp = create_tmp_var (type, "retval");
1150 *p = build2 (INIT_EXPR, type, temp, *p);
1153 return temp;
1156 return NULL_TREE;
1159 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1160 a temporary through which they communicate. */
1162 static void
1163 build_stack_save_restore (gimple *save, gimple *restore)
1165 tree tmp_var;
1167 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1168 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1169 gimple_call_set_lhs (*save, tmp_var);
1171 *restore
1172 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1173 1, tmp_var);
1176 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1178 static enum gimplify_status
1179 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1181 tree bind_expr = *expr_p;
1182 bool old_save_stack = gimplify_ctxp->save_stack;
1183 tree t;
1184 gimple gimple_bind;
1185 gimple_seq body, cleanup;
1186 gimple stack_save;
1188 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1190 /* Mark variables seen in this bind expr. */
1191 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1193 if (TREE_CODE (t) == VAR_DECL)
1195 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1197 /* Mark variable as local. */
1198 if (ctx && !DECL_EXTERNAL (t)
1199 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1200 || splay_tree_lookup (ctx->variables,
1201 (splay_tree_key) t) == NULL))
1202 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1204 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1206 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1207 cfun->has_local_explicit_reg_vars = true;
1210 /* Preliminarily mark non-addressed complex variables as eligible
1211 for promotion to gimple registers. We'll transform their uses
1212 as we find them. */
1213 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1214 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1215 && !TREE_THIS_VOLATILE (t)
1216 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1217 && !needs_to_live_in_memory (t))
1218 DECL_GIMPLE_REG_P (t) = 1;
1221 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1222 BIND_EXPR_BLOCK (bind_expr));
1223 gimple_push_bind_expr (gimple_bind);
1225 gimplify_ctxp->save_stack = false;
1227 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1228 body = NULL;
1229 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1230 gimple_bind_set_body (gimple_bind, body);
1232 cleanup = NULL;
1233 stack_save = NULL;
1234 if (gimplify_ctxp->save_stack)
1236 gimple stack_restore;
1238 /* Save stack on entry and restore it on exit. Add a try_finally
1239 block to achieve this. */
1240 build_stack_save_restore (&stack_save, &stack_restore);
1242 gimplify_seq_add_stmt (&cleanup, stack_restore);
1245 /* Add clobbers for all variables that go out of scope. */
1246 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1248 if (TREE_CODE (t) == VAR_DECL
1249 && !is_global_var (t)
1250 && DECL_CONTEXT (t) == current_function_decl
1251 && !DECL_HARD_REGISTER (t)
1252 && !TREE_THIS_VOLATILE (t)
1253 && !DECL_HAS_VALUE_EXPR_P (t)
1254 /* Only care for variables that have to be in memory. Others
1255 will be rewritten into SSA names, hence moved to the top-level. */
1256 && !is_gimple_reg (t)
1257 && flag_stack_reuse != SR_NONE)
1259 tree clobber = build_constructor (TREE_TYPE (t),
1260 NULL);
1261 TREE_THIS_VOLATILE (clobber) = 1;
1262 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1266 if (cleanup)
1268 gimple gs;
1269 gimple_seq new_body;
1271 new_body = NULL;
1272 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1273 GIMPLE_TRY_FINALLY);
1275 if (stack_save)
1276 gimplify_seq_add_stmt (&new_body, stack_save);
1277 gimplify_seq_add_stmt (&new_body, gs);
1278 gimple_bind_set_body (gimple_bind, new_body);
1281 gimplify_ctxp->save_stack = old_save_stack;
1282 gimple_pop_bind_expr ();
1284 gimplify_seq_add_stmt (pre_p, gimple_bind);
1286 if (temp)
1288 *expr_p = temp;
1289 return GS_OK;
1292 *expr_p = NULL_TREE;
1293 return GS_ALL_DONE;
1296 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1297 GIMPLE value, it is assigned to a new temporary and the statement is
1298 re-written to return the temporary.
1300 PRE_P points to the sequence where side effects that must happen before
1301 STMT should be stored. */
1303 static enum gimplify_status
1304 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1306 gimple ret;
1307 tree ret_expr = TREE_OPERAND (stmt, 0);
1308 tree result_decl, result;
1310 if (ret_expr == error_mark_node)
1311 return GS_ERROR;
1313 /* Implicit _Cilk_sync must be inserted right before any return statement
1314 if there is a _Cilk_spawn in the function. If the user has provided a
1315 _Cilk_sync, the optimizer should remove this duplicate one. */
1316 if (fn_contains_cilk_spawn_p (cfun))
1318 tree impl_sync = build0 (CILK_SYNC_STMT, void_type_node);
1319 gimplify_and_add (impl_sync, pre_p);
1322 if (!ret_expr
1323 || TREE_CODE (ret_expr) == RESULT_DECL
1324 || ret_expr == error_mark_node)
1326 gimple ret = gimple_build_return (ret_expr);
1327 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1328 gimplify_seq_add_stmt (pre_p, ret);
1329 return GS_ALL_DONE;
1332 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1333 result_decl = NULL_TREE;
1334 else
1336 result_decl = TREE_OPERAND (ret_expr, 0);
1338 /* See through a return by reference. */
1339 if (TREE_CODE (result_decl) == INDIRECT_REF)
1340 result_decl = TREE_OPERAND (result_decl, 0);
1342 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1343 || TREE_CODE (ret_expr) == INIT_EXPR)
1344 && TREE_CODE (result_decl) == RESULT_DECL);
1347 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1348 Recall that aggregate_value_p is FALSE for any aggregate type that is
1349 returned in registers. If we're returning values in registers, then
1350 we don't want to extend the lifetime of the RESULT_DECL, particularly
1351 across another call. In addition, for those aggregates for which
1352 hard_function_value generates a PARALLEL, we'll die during normal
1353 expansion of structure assignments; there's special code in expand_return
1354 to handle this case that does not exist in expand_expr. */
1355 if (!result_decl)
1356 result = NULL_TREE;
1357 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1359 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1361 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1362 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1363 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1364 should be effectively allocated by the caller, i.e. all calls to
1365 this function must be subject to the Return Slot Optimization. */
1366 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1367 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1369 result = result_decl;
1371 else if (gimplify_ctxp->return_temp)
1372 result = gimplify_ctxp->return_temp;
1373 else
1375 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1377 /* ??? With complex control flow (usually involving abnormal edges),
1378 we can wind up warning about an uninitialized value for this. Due
1379 to how this variable is constructed and initialized, this is never
1380 true. Give up and never warn. */
1381 TREE_NO_WARNING (result) = 1;
1383 gimplify_ctxp->return_temp = result;
1386 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1387 Then gimplify the whole thing. */
1388 if (result != result_decl)
1389 TREE_OPERAND (ret_expr, 0) = result;
1391 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1393 ret = gimple_build_return (result);
1394 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1395 gimplify_seq_add_stmt (pre_p, ret);
1397 return GS_ALL_DONE;
1400 /* Gimplify a variable-length array DECL. */
1402 static void
1403 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1405 /* This is a variable-sized decl. Simplify its size and mark it
1406 for deferred expansion. */
1407 tree t, addr, ptr_type;
1409 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1410 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1412 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1413 if (DECL_HAS_VALUE_EXPR_P (decl))
1414 return;
1416 /* All occurrences of this decl in final gimplified code will be
1417 replaced by indirection. Setting DECL_VALUE_EXPR does two
1418 things: First, it lets the rest of the gimplifier know what
1419 replacement to use. Second, it lets the debug info know
1420 where to find the value. */
1421 ptr_type = build_pointer_type (TREE_TYPE (decl));
1422 addr = create_tmp_var (ptr_type, get_name (decl));
1423 DECL_IGNORED_P (addr) = 0;
1424 t = build_fold_indirect_ref (addr);
1425 TREE_THIS_NOTRAP (t) = 1;
1426 SET_DECL_VALUE_EXPR (decl, t);
1427 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1429 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1430 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1431 size_int (DECL_ALIGN (decl)));
1432 /* The call has been built for a variable-sized object. */
1433 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1434 t = fold_convert (ptr_type, t);
1435 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1437 gimplify_and_add (t, seq_p);
1439 /* Indicate that we need to restore the stack level when the
1440 enclosing BIND_EXPR is exited. */
1441 gimplify_ctxp->save_stack = true;
1444 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1445 and initialization explicit. */
1447 static enum gimplify_status
1448 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1450 tree stmt = *stmt_p;
1451 tree decl = DECL_EXPR_DECL (stmt);
1453 *stmt_p = NULL_TREE;
1455 if (TREE_TYPE (decl) == error_mark_node)
1456 return GS_ERROR;
1458 if ((TREE_CODE (decl) == TYPE_DECL
1459 || TREE_CODE (decl) == VAR_DECL)
1460 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1461 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1463 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1464 in case its size expressions contain problematic nodes like CALL_EXPR. */
1465 if (TREE_CODE (decl) == TYPE_DECL
1466 && DECL_ORIGINAL_TYPE (decl)
1467 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl)))
1468 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl), seq_p);
1470 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1472 tree init = DECL_INITIAL (decl);
1474 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1475 || (!TREE_STATIC (decl)
1476 && flag_stack_check == GENERIC_STACK_CHECK
1477 && compare_tree_int (DECL_SIZE_UNIT (decl),
1478 STACK_CHECK_MAX_VAR_SIZE) > 0))
1479 gimplify_vla_decl (decl, seq_p);
1481 /* Some front ends do not explicitly declare all anonymous
1482 artificial variables. We compensate here by declaring the
1483 variables, though it would be better if the front ends would
1484 explicitly declare them. */
1485 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1486 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1487 gimple_add_tmp_var (decl);
1489 if (init && init != error_mark_node)
1491 if (!TREE_STATIC (decl))
1493 DECL_INITIAL (decl) = NULL_TREE;
1494 init = build2 (INIT_EXPR, void_type_node, decl, init);
1495 gimplify_and_add (init, seq_p);
1496 ggc_free (init);
1498 else
1499 /* We must still examine initializers for static variables
1500 as they may contain a label address. */
1501 walk_tree (&init, force_labels_r, NULL, NULL);
1505 return GS_ALL_DONE;
1508 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1509 and replacing the LOOP_EXPR with goto, but if the loop contains an
1510 EXIT_EXPR, we need to append a label for it to jump to. */
1512 static enum gimplify_status
1513 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1515 tree saved_label = gimplify_ctxp->exit_label;
1516 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1518 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1520 gimplify_ctxp->exit_label = NULL_TREE;
1522 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1524 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1526 if (gimplify_ctxp->exit_label)
1527 gimplify_seq_add_stmt (pre_p,
1528 gimple_build_label (gimplify_ctxp->exit_label));
1530 gimplify_ctxp->exit_label = saved_label;
1532 *expr_p = NULL;
1533 return GS_ALL_DONE;
1536 /* Gimplify a statement list onto a sequence. These may be created either
1537 by an enlightened front-end, or by shortcut_cond_expr. */
1539 static enum gimplify_status
1540 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1542 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1544 tree_stmt_iterator i = tsi_start (*expr_p);
1546 while (!tsi_end_p (i))
1548 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1549 tsi_delink (&i);
1552 if (temp)
1554 *expr_p = temp;
1555 return GS_OK;
1558 return GS_ALL_DONE;
1561 /* Compare two case labels. Because the front end should already have
1562 made sure that case ranges do not overlap, it is enough to only compare
1563 the CASE_LOW values of each case label. */
1565 static int
1566 compare_case_labels (const void *p1, const void *p2)
1568 const_tree const case1 = *(const_tree const*)p1;
1569 const_tree const case2 = *(const_tree const*)p2;
1571 /* The 'default' case label always goes first. */
1572 if (!CASE_LOW (case1))
1573 return -1;
1574 else if (!CASE_LOW (case2))
1575 return 1;
1576 else
1577 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1580 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1582 void
1583 sort_case_labels (vec<tree> label_vec)
1585 label_vec.qsort (compare_case_labels);
1588 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
1590 LABELS is a vector that contains all case labels to look at.
1592 INDEX_TYPE is the type of the switch index expression. Case labels
1593 in LABELS are discarded if their values are not in the value range
1594 covered by INDEX_TYPE. The remaining case label values are folded
1595 to INDEX_TYPE.
1597 If a default case exists in LABELS, it is removed from LABELS and
1598 returned in DEFAULT_CASEP. If no default case exists, but the
1599 case labels already cover the whole range of INDEX_TYPE, a default
1600 case is returned pointing to one of the existing case labels.
1601 Otherwise DEFAULT_CASEP is set to NULL_TREE.
1603 DEFAULT_CASEP may be NULL, in which case the above comment doesn't
1604 apply and no action is taken regardless of whether a default case is
1605 found or not. */
1607 void
1608 preprocess_case_label_vec_for_gimple (vec<tree> labels,
1609 tree index_type,
1610 tree *default_casep)
1612 tree min_value, max_value;
1613 tree default_case = NULL_TREE;
1614 size_t i, len;
1616 i = 0;
1617 min_value = TYPE_MIN_VALUE (index_type);
1618 max_value = TYPE_MAX_VALUE (index_type);
1619 while (i < labels.length ())
1621 tree elt = labels[i];
1622 tree low = CASE_LOW (elt);
1623 tree high = CASE_HIGH (elt);
1624 bool remove_element = FALSE;
1626 if (low)
1628 gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
1629 gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
1631 /* This is a non-default case label, i.e. it has a value.
1633 See if the case label is reachable within the range of
1634 the index type. Remove out-of-range case values. Turn
1635 case ranges into a canonical form (high > low strictly)
1636 and convert the case label values to the index type.
1638 NB: The type of gimple_switch_index() may be the promoted
1639 type, but the case labels retain the original type. */
1641 if (high)
1643 /* This is a case range. Discard empty ranges.
1644 If the bounds or the range are equal, turn this
1645 into a simple (one-value) case. */
1646 int cmp = tree_int_cst_compare (high, low);
1647 if (cmp < 0)
1648 remove_element = TRUE;
1649 else if (cmp == 0)
1650 high = NULL_TREE;
1653 if (! high)
1655 /* If the simple case value is unreachable, ignore it. */
1656 if ((TREE_CODE (min_value) == INTEGER_CST
1657 && tree_int_cst_compare (low, min_value) < 0)
1658 || (TREE_CODE (max_value) == INTEGER_CST
1659 && tree_int_cst_compare (low, max_value) > 0))
1660 remove_element = TRUE;
1661 else
1662 low = fold_convert (index_type, low);
1664 else
1666 /* If the entire case range is unreachable, ignore it. */
1667 if ((TREE_CODE (min_value) == INTEGER_CST
1668 && tree_int_cst_compare (high, min_value) < 0)
1669 || (TREE_CODE (max_value) == INTEGER_CST
1670 && tree_int_cst_compare (low, max_value) > 0))
1671 remove_element = TRUE;
1672 else
1674 /* If the lower bound is less than the index type's
1675 minimum value, truncate the range bounds. */
1676 if (TREE_CODE (min_value) == INTEGER_CST
1677 && tree_int_cst_compare (low, min_value) < 0)
1678 low = min_value;
1679 low = fold_convert (index_type, low);
1681 /* If the upper bound is greater than the index type's
1682 maximum value, truncate the range bounds. */
1683 if (TREE_CODE (max_value) == INTEGER_CST
1684 && tree_int_cst_compare (high, max_value) > 0)
1685 high = max_value;
1686 high = fold_convert (index_type, high);
1688 /* We may have folded a case range to a one-value case. */
1689 if (tree_int_cst_equal (low, high))
1690 high = NULL_TREE;
1694 CASE_LOW (elt) = low;
1695 CASE_HIGH (elt) = high;
1697 else
1699 gcc_assert (!default_case);
1700 default_case = elt;
1701 /* The default case must be passed separately to the
1702 gimple_build_switch routine. But if DEFAULT_CASEP
1703 is NULL, we do not remove the default case (it would
1704 be completely lost). */
1705 if (default_casep)
1706 remove_element = TRUE;
1709 if (remove_element)
1710 labels.ordered_remove (i);
1711 else
1712 i++;
1714 len = i;
1716 if (!labels.is_empty ())
1717 sort_case_labels (labels);
1719 if (default_casep && !default_case)
1721 /* If the switch has no default label, add one, so that we jump
1722 around the switch body. If the labels already cover the whole
1723 range of the switch index_type, add the default label pointing
1724 to one of the existing labels. */
1725 if (len
1726 && TYPE_MIN_VALUE (index_type)
1727 && TYPE_MAX_VALUE (index_type)
1728 && tree_int_cst_equal (CASE_LOW (labels[0]),
1729 TYPE_MIN_VALUE (index_type)))
1731 tree low, high = CASE_HIGH (labels[len - 1]);
1732 if (!high)
1733 high = CASE_LOW (labels[len - 1]);
1734 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
1736 for (i = 1; i < len; i++)
1738 high = CASE_LOW (labels[i]);
1739 low = CASE_HIGH (labels[i - 1]);
1740 if (!low)
1741 low = CASE_LOW (labels[i - 1]);
1742 if ((TREE_INT_CST_LOW (low) + 1
1743 != TREE_INT_CST_LOW (high))
1744 || (TREE_INT_CST_HIGH (low)
1745 + (TREE_INT_CST_LOW (high) == 0)
1746 != TREE_INT_CST_HIGH (high)))
1747 break;
1749 if (i == len)
1751 tree label = CASE_LABEL (labels[0]);
1752 default_case = build_case_label (NULL_TREE, NULL_TREE,
1753 label);
1759 if (default_casep)
1760 *default_casep = default_case;
1763 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1764 branch to. */
1766 static enum gimplify_status
1767 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1769 tree switch_expr = *expr_p;
1770 gimple_seq switch_body_seq = NULL;
1771 enum gimplify_status ret;
1772 tree index_type = TREE_TYPE (switch_expr);
1773 if (index_type == NULL_TREE)
1774 index_type = TREE_TYPE (SWITCH_COND (switch_expr));
1776 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1777 fb_rvalue);
1778 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1779 return ret;
1781 if (SWITCH_BODY (switch_expr))
1783 vec<tree> labels;
1784 vec<tree> saved_labels;
1785 tree default_case = NULL_TREE;
1786 gimple gimple_switch;
1788 /* If someone can be bothered to fill in the labels, they can
1789 be bothered to null out the body too. */
1790 gcc_assert (!SWITCH_LABELS (switch_expr));
1792 /* Save old labels, get new ones from body, then restore the old
1793 labels. Save all the things from the switch body to append after. */
1794 saved_labels = gimplify_ctxp->case_labels;
1795 gimplify_ctxp->case_labels.create (8);
1797 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1798 labels = gimplify_ctxp->case_labels;
1799 gimplify_ctxp->case_labels = saved_labels;
1801 preprocess_case_label_vec_for_gimple (labels, index_type,
1802 &default_case);
1804 if (!default_case)
1806 gimple new_default;
1808 default_case
1809 = build_case_label (NULL_TREE, NULL_TREE,
1810 create_artificial_label (UNKNOWN_LOCATION));
1811 new_default = gimple_build_label (CASE_LABEL (default_case));
1812 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1815 gimple_switch = gimple_build_switch (SWITCH_COND (switch_expr),
1816 default_case, labels);
1817 gimplify_seq_add_stmt (pre_p, gimple_switch);
1818 gimplify_seq_add_seq (pre_p, switch_body_seq);
1819 labels.release ();
1821 else
1822 gcc_assert (SWITCH_LABELS (switch_expr));
1824 return GS_ALL_DONE;
1827 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1829 static enum gimplify_status
1830 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1832 struct gimplify_ctx *ctxp;
1833 gimple gimple_label;
1835 /* Invalid OpenMP programs can play Duff's Device type games with
1836 #pragma omp parallel. At least in the C front end, we don't
1837 detect such invalid branches until after gimplification. */
1838 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1839 if (ctxp->case_labels.exists ())
1840 break;
1842 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1843 ctxp->case_labels.safe_push (*expr_p);
1844 gimplify_seq_add_stmt (pre_p, gimple_label);
1846 return GS_ALL_DONE;
1849 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1850 if necessary. */
1852 tree
1853 build_and_jump (tree *label_p)
1855 if (label_p == NULL)
1856 /* If there's nowhere to jump, just fall through. */
1857 return NULL_TREE;
1859 if (*label_p == NULL_TREE)
1861 tree label = create_artificial_label (UNKNOWN_LOCATION);
1862 *label_p = label;
1865 return build1 (GOTO_EXPR, void_type_node, *label_p);
1868 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1869 This also involves building a label to jump to and communicating it to
1870 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1872 static enum gimplify_status
1873 gimplify_exit_expr (tree *expr_p)
1875 tree cond = TREE_OPERAND (*expr_p, 0);
1876 tree expr;
1878 expr = build_and_jump (&gimplify_ctxp->exit_label);
1879 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1880 *expr_p = expr;
1882 return GS_OK;
1885 /* A helper function to be called via walk_tree. Mark all labels under *TP
1886 as being forced. To be called for DECL_INITIAL of static variables. */
1888 tree
1889 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1891 if (TYPE_P (*tp))
1892 *walk_subtrees = 0;
1893 if (TREE_CODE (*tp) == LABEL_DECL)
1894 FORCED_LABEL (*tp) = 1;
1896 return NULL_TREE;
1899 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1900 different from its canonical type, wrap the whole thing inside a
1901 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1902 type.
1904 The canonical type of a COMPONENT_REF is the type of the field being
1905 referenced--unless the field is a bit-field which can be read directly
1906 in a smaller mode, in which case the canonical type is the
1907 sign-appropriate type corresponding to that mode. */
1909 static void
1910 canonicalize_component_ref (tree *expr_p)
1912 tree expr = *expr_p;
1913 tree type;
1915 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1917 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1918 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1919 else
1920 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1922 /* One could argue that all the stuff below is not necessary for
1923 the non-bitfield case and declare it a FE error if type
1924 adjustment would be needed. */
1925 if (TREE_TYPE (expr) != type)
1927 #ifdef ENABLE_TYPES_CHECKING
1928 tree old_type = TREE_TYPE (expr);
1929 #endif
1930 int type_quals;
1932 /* We need to preserve qualifiers and propagate them from
1933 operand 0. */
1934 type_quals = TYPE_QUALS (type)
1935 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1936 if (TYPE_QUALS (type) != type_quals)
1937 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1939 /* Set the type of the COMPONENT_REF to the underlying type. */
1940 TREE_TYPE (expr) = type;
1942 #ifdef ENABLE_TYPES_CHECKING
1943 /* It is now a FE error, if the conversion from the canonical
1944 type to the original expression type is not useless. */
1945 gcc_assert (useless_type_conversion_p (old_type, type));
1946 #endif
1950 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1951 to foo, embed that change in the ADDR_EXPR by converting
1952 T array[U];
1953 (T *)&array
1955 &array[L]
1956 where L is the lower bound. For simplicity, only do this for constant
1957 lower bound.
1958 The constraint is that the type of &array[L] is trivially convertible
1959 to T *. */
1961 static void
1962 canonicalize_addr_expr (tree *expr_p)
1964 tree expr = *expr_p;
1965 tree addr_expr = TREE_OPERAND (expr, 0);
1966 tree datype, ddatype, pddatype;
1968 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1969 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1970 || TREE_CODE (addr_expr) != ADDR_EXPR)
1971 return;
1973 /* The addr_expr type should be a pointer to an array. */
1974 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1975 if (TREE_CODE (datype) != ARRAY_TYPE)
1976 return;
1978 /* The pointer to element type shall be trivially convertible to
1979 the expression pointer type. */
1980 ddatype = TREE_TYPE (datype);
1981 pddatype = build_pointer_type (ddatype);
1982 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1983 pddatype))
1984 return;
1986 /* The lower bound and element sizes must be constant. */
1987 if (!TYPE_SIZE_UNIT (ddatype)
1988 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1989 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1990 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1991 return;
1993 /* All checks succeeded. Build a new node to merge the cast. */
1994 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1995 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1996 NULL_TREE, NULL_TREE);
1997 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1999 /* We can have stripped a required restrict qualifier above. */
2000 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
2001 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
2004 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
2005 underneath as appropriate. */
2007 static enum gimplify_status
2008 gimplify_conversion (tree *expr_p)
2010 location_t loc = EXPR_LOCATION (*expr_p);
2011 gcc_assert (CONVERT_EXPR_P (*expr_p));
2013 /* Then strip away all but the outermost conversion. */
2014 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
2016 /* And remove the outermost conversion if it's useless. */
2017 if (tree_ssa_useless_type_conversion (*expr_p))
2018 *expr_p = TREE_OPERAND (*expr_p, 0);
2020 /* If we still have a conversion at the toplevel,
2021 then canonicalize some constructs. */
2022 if (CONVERT_EXPR_P (*expr_p))
2024 tree sub = TREE_OPERAND (*expr_p, 0);
2026 /* If a NOP conversion is changing the type of a COMPONENT_REF
2027 expression, then canonicalize its type now in order to expose more
2028 redundant conversions. */
2029 if (TREE_CODE (sub) == COMPONENT_REF)
2030 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
2032 /* If a NOP conversion is changing a pointer to array of foo
2033 to a pointer to foo, embed that change in the ADDR_EXPR. */
2034 else if (TREE_CODE (sub) == ADDR_EXPR)
2035 canonicalize_addr_expr (expr_p);
2038 /* If we have a conversion to a non-register type force the
2039 use of a VIEW_CONVERT_EXPR instead. */
2040 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
2041 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
2042 TREE_OPERAND (*expr_p, 0));
2044 return GS_OK;
2047 /* Nonlocal VLAs seen in the current function. */
2048 static struct pointer_set_t *nonlocal_vlas;
2050 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
2051 DECL_VALUE_EXPR, and it's worth re-examining things. */
2053 static enum gimplify_status
2054 gimplify_var_or_parm_decl (tree *expr_p)
2056 tree decl = *expr_p;
2058 /* ??? If this is a local variable, and it has not been seen in any
2059 outer BIND_EXPR, then it's probably the result of a duplicate
2060 declaration, for which we've already issued an error. It would
2061 be really nice if the front end wouldn't leak these at all.
2062 Currently the only known culprit is C++ destructors, as seen
2063 in g++.old-deja/g++.jason/binding.C. */
2064 if (TREE_CODE (decl) == VAR_DECL
2065 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
2066 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2067 && decl_function_context (decl) == current_function_decl)
2069 gcc_assert (seen_error ());
2070 return GS_ERROR;
2073 /* When within an OpenMP context, notice uses of variables. */
2074 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
2075 return GS_ALL_DONE;
2077 /* If the decl is an alias for another expression, substitute it now. */
2078 if (DECL_HAS_VALUE_EXPR_P (decl))
2080 tree value_expr = DECL_VALUE_EXPR (decl);
2082 /* For referenced nonlocal VLAs add a decl for debugging purposes
2083 to the current function. */
2084 if (TREE_CODE (decl) == VAR_DECL
2085 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
2086 && nonlocal_vlas != NULL
2087 && TREE_CODE (value_expr) == INDIRECT_REF
2088 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
2089 && decl_function_context (decl) != current_function_decl)
2091 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
2092 while (ctx
2093 && (ctx->region_type == ORT_WORKSHARE
2094 || ctx->region_type == ORT_SIMD))
2095 ctx = ctx->outer_context;
2096 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
2098 tree copy = copy_node (decl), block;
2100 lang_hooks.dup_lang_specific_decl (copy);
2101 SET_DECL_RTL (copy, 0);
2102 TREE_USED (copy) = 1;
2103 block = DECL_INITIAL (current_function_decl);
2104 DECL_CHAIN (copy) = BLOCK_VARS (block);
2105 BLOCK_VARS (block) = copy;
2106 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
2107 DECL_HAS_VALUE_EXPR_P (copy) = 1;
2111 *expr_p = unshare_expr (value_expr);
2112 return GS_OK;
2115 return GS_ALL_DONE;
2118 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
2119 node *EXPR_P.
2121 compound_lval
2122 : min_lval '[' val ']'
2123 | min_lval '.' ID
2124 | compound_lval '[' val ']'
2125 | compound_lval '.' ID
2127 This is not part of the original SIMPLE definition, which separates
2128 array and member references, but it seems reasonable to handle them
2129 together. Also, this way we don't run into problems with union
2130 aliasing; gcc requires that for accesses through a union to alias, the
2131 union reference must be explicit, which was not always the case when we
2132 were splitting up array and member refs.
2134 PRE_P points to the sequence where side effects that must happen before
2135 *EXPR_P should be stored.
2137 POST_P points to the sequence where side effects that must happen after
2138 *EXPR_P should be stored. */
2140 static enum gimplify_status
2141 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2142 fallback_t fallback)
2144 tree *p;
2145 vec<tree> expr_stack;
2146 enum gimplify_status ret = GS_ALL_DONE, tret;
2147 int i;
2148 location_t loc = EXPR_LOCATION (*expr_p);
2149 tree expr = *expr_p;
2151 /* Create a stack of the subexpressions so later we can walk them in
2152 order from inner to outer. */
2153 expr_stack.create (10);
2155 /* We can handle anything that get_inner_reference can deal with. */
2156 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2158 restart:
2159 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2160 if (TREE_CODE (*p) == INDIRECT_REF)
2161 *p = fold_indirect_ref_loc (loc, *p);
2163 if (handled_component_p (*p))
2165 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2166 additional COMPONENT_REFs. */
2167 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2168 && gimplify_var_or_parm_decl (p) == GS_OK)
2169 goto restart;
2170 else
2171 break;
2173 expr_stack.safe_push (*p);
2176 gcc_assert (expr_stack.length ());
2178 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2179 walked through and P points to the innermost expression.
2181 Java requires that we elaborated nodes in source order. That
2182 means we must gimplify the inner expression followed by each of
2183 the indices, in order. But we can't gimplify the inner
2184 expression until we deal with any variable bounds, sizes, or
2185 positions in order to deal with PLACEHOLDER_EXPRs.
2187 So we do this in three steps. First we deal with the annotations
2188 for any variables in the components, then we gimplify the base,
2189 then we gimplify any indices, from left to right. */
2190 for (i = expr_stack.length () - 1; i >= 0; i--)
2192 tree t = expr_stack[i];
2194 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2196 /* Gimplify the low bound and element type size and put them into
2197 the ARRAY_REF. If these values are set, they have already been
2198 gimplified. */
2199 if (TREE_OPERAND (t, 2) == NULL_TREE)
2201 tree low = unshare_expr (array_ref_low_bound (t));
2202 if (!is_gimple_min_invariant (low))
2204 TREE_OPERAND (t, 2) = low;
2205 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2206 post_p, is_gimple_reg,
2207 fb_rvalue);
2208 ret = MIN (ret, tret);
2211 else
2213 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2214 is_gimple_reg, fb_rvalue);
2215 ret = MIN (ret, tret);
2218 if (TREE_OPERAND (t, 3) == NULL_TREE)
2220 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2221 tree elmt_size = unshare_expr (array_ref_element_size (t));
2222 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2224 /* Divide the element size by the alignment of the element
2225 type (above). */
2226 elmt_size
2227 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2229 if (!is_gimple_min_invariant (elmt_size))
2231 TREE_OPERAND (t, 3) = elmt_size;
2232 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2233 post_p, is_gimple_reg,
2234 fb_rvalue);
2235 ret = MIN (ret, tret);
2238 else
2240 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2241 is_gimple_reg, fb_rvalue);
2242 ret = MIN (ret, tret);
2245 else if (TREE_CODE (t) == COMPONENT_REF)
2247 /* Set the field offset into T and gimplify it. */
2248 if (TREE_OPERAND (t, 2) == NULL_TREE)
2250 tree offset = unshare_expr (component_ref_field_offset (t));
2251 tree field = TREE_OPERAND (t, 1);
2252 tree factor
2253 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2255 /* Divide the offset by its alignment. */
2256 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2258 if (!is_gimple_min_invariant (offset))
2260 TREE_OPERAND (t, 2) = offset;
2261 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2262 post_p, is_gimple_reg,
2263 fb_rvalue);
2264 ret = MIN (ret, tret);
2267 else
2269 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2270 is_gimple_reg, fb_rvalue);
2271 ret = MIN (ret, tret);
2276 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2277 so as to match the min_lval predicate. Failure to do so may result
2278 in the creation of large aggregate temporaries. */
2279 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2280 fallback | fb_lvalue);
2281 ret = MIN (ret, tret);
2283 /* And finally, the indices and operands of ARRAY_REF. During this
2284 loop we also remove any useless conversions. */
2285 for (; expr_stack.length () > 0; )
2287 tree t = expr_stack.pop ();
2289 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2291 /* Gimplify the dimension. */
2292 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2294 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2295 is_gimple_val, fb_rvalue);
2296 ret = MIN (ret, tret);
2300 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2302 /* The innermost expression P may have originally had
2303 TREE_SIDE_EFFECTS set which would have caused all the outer
2304 expressions in *EXPR_P leading to P to also have had
2305 TREE_SIDE_EFFECTS set. */
2306 recalculate_side_effects (t);
2309 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2310 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2312 canonicalize_component_ref (expr_p);
2315 expr_stack.release ();
2317 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2319 return ret;
2322 /* Gimplify the self modifying expression pointed to by EXPR_P
2323 (++, --, +=, -=).
2325 PRE_P points to the list where side effects that must happen before
2326 *EXPR_P should be stored.
2328 POST_P points to the list where side effects that must happen after
2329 *EXPR_P should be stored.
2331 WANT_VALUE is nonzero iff we want to use the value of this expression
2332 in another expression.
2334 ARITH_TYPE is the type the computation should be performed in. */
2336 enum gimplify_status
2337 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2338 bool want_value, tree arith_type)
2340 enum tree_code code;
2341 tree lhs, lvalue, rhs, t1;
2342 gimple_seq post = NULL, *orig_post_p = post_p;
2343 bool postfix;
2344 enum tree_code arith_code;
2345 enum gimplify_status ret;
2346 location_t loc = EXPR_LOCATION (*expr_p);
2348 code = TREE_CODE (*expr_p);
2350 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2351 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2353 /* Prefix or postfix? */
2354 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2355 /* Faster to treat as prefix if result is not used. */
2356 postfix = want_value;
2357 else
2358 postfix = false;
2360 /* For postfix, make sure the inner expression's post side effects
2361 are executed after side effects from this expression. */
2362 if (postfix)
2363 post_p = &post;
2365 /* Add or subtract? */
2366 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2367 arith_code = PLUS_EXPR;
2368 else
2369 arith_code = MINUS_EXPR;
2371 /* Gimplify the LHS into a GIMPLE lvalue. */
2372 lvalue = TREE_OPERAND (*expr_p, 0);
2373 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2374 if (ret == GS_ERROR)
2375 return ret;
2377 /* Extract the operands to the arithmetic operation. */
2378 lhs = lvalue;
2379 rhs = TREE_OPERAND (*expr_p, 1);
2381 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2382 that as the result value and in the postqueue operation. */
2383 if (postfix)
2385 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2386 if (ret == GS_ERROR)
2387 return ret;
2389 lhs = get_initialized_tmp_var (lhs, pre_p, NULL);
2392 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2393 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2395 rhs = convert_to_ptrofftype_loc (loc, rhs);
2396 if (arith_code == MINUS_EXPR)
2397 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2398 t1 = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (*expr_p), lhs, rhs);
2400 else
2401 t1 = fold_convert (TREE_TYPE (*expr_p),
2402 fold_build2 (arith_code, arith_type,
2403 fold_convert (arith_type, lhs),
2404 fold_convert (arith_type, rhs)));
2406 if (postfix)
2408 gimplify_assign (lvalue, t1, pre_p);
2409 gimplify_seq_add_seq (orig_post_p, post);
2410 *expr_p = lhs;
2411 return GS_ALL_DONE;
2413 else
2415 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2416 return GS_OK;
2420 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2422 static void
2423 maybe_with_size_expr (tree *expr_p)
2425 tree expr = *expr_p;
2426 tree type = TREE_TYPE (expr);
2427 tree size;
2429 /* If we've already wrapped this or the type is error_mark_node, we can't do
2430 anything. */
2431 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2432 || type == error_mark_node)
2433 return;
2435 /* If the size isn't known or is a constant, we have nothing to do. */
2436 size = TYPE_SIZE_UNIT (type);
2437 if (!size || TREE_CODE (size) == INTEGER_CST)
2438 return;
2440 /* Otherwise, make a WITH_SIZE_EXPR. */
2441 size = unshare_expr (size);
2442 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2443 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2446 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2447 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2448 the CALL_EXPR. */
2450 static enum gimplify_status
2451 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2453 bool (*test) (tree);
2454 fallback_t fb;
2456 /* In general, we allow lvalues for function arguments to avoid
2457 extra overhead of copying large aggregates out of even larger
2458 aggregates into temporaries only to copy the temporaries to
2459 the argument list. Make optimizers happy by pulling out to
2460 temporaries those types that fit in registers. */
2461 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2462 test = is_gimple_val, fb = fb_rvalue;
2463 else
2465 test = is_gimple_lvalue, fb = fb_either;
2466 /* Also strip a TARGET_EXPR that would force an extra copy. */
2467 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2469 tree init = TARGET_EXPR_INITIAL (*arg_p);
2470 if (init
2471 && !VOID_TYPE_P (TREE_TYPE (init)))
2472 *arg_p = init;
2476 /* If this is a variable sized type, we must remember the size. */
2477 maybe_with_size_expr (arg_p);
2479 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2480 /* Make sure arguments have the same location as the function call
2481 itself. */
2482 protected_set_expr_location (*arg_p, call_location);
2484 /* There is a sequence point before a function call. Side effects in
2485 the argument list must occur before the actual call. So, when
2486 gimplifying arguments, force gimplify_expr to use an internal
2487 post queue which is then appended to the end of PRE_P. */
2488 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2491 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2492 WANT_VALUE is true if the result of the call is desired. */
2494 static enum gimplify_status
2495 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2497 tree fndecl, parms, p, fnptrtype;
2498 enum gimplify_status ret;
2499 int i, nargs;
2500 gimple call;
2501 bool builtin_va_start_p = FALSE;
2502 location_t loc = EXPR_LOCATION (*expr_p);
2504 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2506 /* For reliable diagnostics during inlining, it is necessary that
2507 every call_expr be annotated with file and line. */
2508 if (! EXPR_HAS_LOCATION (*expr_p))
2509 SET_EXPR_LOCATION (*expr_p, input_location);
2511 if (fn_contains_cilk_spawn_p (cfun)
2512 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
2513 && !seen_error ())
2514 return (enum gimplify_status)
2515 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, NULL);
2517 /* This may be a call to a builtin function.
2519 Builtin function calls may be transformed into different
2520 (and more efficient) builtin function calls under certain
2521 circumstances. Unfortunately, gimplification can muck things
2522 up enough that the builtin expanders are not aware that certain
2523 transformations are still valid.
2525 So we attempt transformation/gimplification of the call before
2526 we gimplify the CALL_EXPR. At this time we do not manage to
2527 transform all calls in the same manner as the expanders do, but
2528 we do transform most of them. */
2529 fndecl = get_callee_fndecl (*expr_p);
2530 if (fndecl
2531 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2532 switch (DECL_FUNCTION_CODE (fndecl))
2534 case BUILT_IN_VA_START:
2536 builtin_va_start_p = TRUE;
2537 if (call_expr_nargs (*expr_p) < 2)
2539 error ("too few arguments to function %<va_start%>");
2540 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2541 return GS_OK;
2544 if (fold_builtin_next_arg (*expr_p, true))
2546 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2547 return GS_OK;
2549 break;
2551 case BUILT_IN_LINE:
2553 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2554 *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
2555 return GS_OK;
2557 case BUILT_IN_FILE:
2559 expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
2560 *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
2561 return GS_OK;
2563 case BUILT_IN_FUNCTION:
2565 const char *function;
2566 function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
2567 *expr_p = build_string_literal (strlen (function) + 1, function);
2568 return GS_OK;
2570 default:
2573 if (fndecl && DECL_BUILT_IN (fndecl))
2575 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2576 if (new_tree && new_tree != *expr_p)
2578 /* There was a transformation of this call which computes the
2579 same value, but in a more efficient way. Return and try
2580 again. */
2581 *expr_p = new_tree;
2582 return GS_OK;
2586 /* Remember the original function pointer type. */
2587 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2589 /* There is a sequence point before the call, so any side effects in
2590 the calling expression must occur before the actual call. Force
2591 gimplify_expr to use an internal post queue. */
2592 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2593 is_gimple_call_addr, fb_rvalue);
2595 nargs = call_expr_nargs (*expr_p);
2597 /* Get argument types for verification. */
2598 fndecl = get_callee_fndecl (*expr_p);
2599 parms = NULL_TREE;
2600 if (fndecl)
2601 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2602 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2603 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2605 if (fndecl && DECL_ARGUMENTS (fndecl))
2606 p = DECL_ARGUMENTS (fndecl);
2607 else if (parms)
2608 p = parms;
2609 else
2610 p = NULL_TREE;
2611 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2614 /* If the last argument is __builtin_va_arg_pack () and it is not
2615 passed as a named argument, decrease the number of CALL_EXPR
2616 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2617 if (!p
2618 && i < nargs
2619 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2621 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2622 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2624 if (last_arg_fndecl
2625 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2626 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2627 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2629 tree call = *expr_p;
2631 --nargs;
2632 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2633 CALL_EXPR_FN (call),
2634 nargs, CALL_EXPR_ARGP (call));
2636 /* Copy all CALL_EXPR flags, location and block, except
2637 CALL_EXPR_VA_ARG_PACK flag. */
2638 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2639 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2640 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2641 = CALL_EXPR_RETURN_SLOT_OPT (call);
2642 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2643 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2645 /* Set CALL_EXPR_VA_ARG_PACK. */
2646 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2650 /* Finally, gimplify the function arguments. */
2651 if (nargs > 0)
2653 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2654 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2655 PUSH_ARGS_REVERSED ? i-- : i++)
2657 enum gimplify_status t;
2659 /* Avoid gimplifying the second argument to va_start, which needs to
2660 be the plain PARM_DECL. */
2661 if ((i != 1) || !builtin_va_start_p)
2663 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2664 EXPR_LOCATION (*expr_p));
2666 if (t == GS_ERROR)
2667 ret = GS_ERROR;
2672 /* Verify the function result. */
2673 if (want_value && fndecl
2674 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2676 error_at (loc, "using result of function returning %<void%>");
2677 ret = GS_ERROR;
2680 /* Try this again in case gimplification exposed something. */
2681 if (ret != GS_ERROR)
2683 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2685 if (new_tree && new_tree != *expr_p)
2687 /* There was a transformation of this call which computes the
2688 same value, but in a more efficient way. Return and try
2689 again. */
2690 *expr_p = new_tree;
2691 return GS_OK;
2694 else
2696 *expr_p = error_mark_node;
2697 return GS_ERROR;
2700 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2701 decl. This allows us to eliminate redundant or useless
2702 calls to "const" functions. */
2703 if (TREE_CODE (*expr_p) == CALL_EXPR)
2705 int flags = call_expr_flags (*expr_p);
2706 if (flags & (ECF_CONST | ECF_PURE)
2707 /* An infinite loop is considered a side effect. */
2708 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2709 TREE_SIDE_EFFECTS (*expr_p) = 0;
2712 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2713 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2714 form and delegate the creation of a GIMPLE_CALL to
2715 gimplify_modify_expr. This is always possible because when
2716 WANT_VALUE is true, the caller wants the result of this call into
2717 a temporary, which means that we will emit an INIT_EXPR in
2718 internal_get_tmp_var which will then be handled by
2719 gimplify_modify_expr. */
2720 if (!want_value)
2722 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2723 have to do is replicate it as a GIMPLE_CALL tuple. */
2724 gimple_stmt_iterator gsi;
2725 call = gimple_build_call_from_tree (*expr_p);
2726 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2727 notice_special_calls (call);
2728 gimplify_seq_add_stmt (pre_p, call);
2729 gsi = gsi_last (*pre_p);
2730 /* Don't fold stmts inside of target construct. We'll do it
2731 during omplower pass instead. */
2732 struct gimplify_omp_ctx *ctx;
2733 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
2734 if (ctx->region_type == ORT_TARGET)
2735 break;
2736 if (ctx == NULL)
2737 fold_stmt (&gsi);
2738 *expr_p = NULL_TREE;
2740 else
2741 /* Remember the original function type. */
2742 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2743 CALL_EXPR_FN (*expr_p));
2745 return ret;
2748 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2749 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2751 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2752 condition is true or false, respectively. If null, we should generate
2753 our own to skip over the evaluation of this specific expression.
2755 LOCUS is the source location of the COND_EXPR.
2757 This function is the tree equivalent of do_jump.
2759 shortcut_cond_r should only be called by shortcut_cond_expr. */
2761 static tree
2762 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2763 location_t locus)
2765 tree local_label = NULL_TREE;
2766 tree t, expr = NULL;
2768 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2769 retain the shortcut semantics. Just insert the gotos here;
2770 shortcut_cond_expr will append the real blocks later. */
2771 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2773 location_t new_locus;
2775 /* Turn if (a && b) into
2777 if (a); else goto no;
2778 if (b) goto yes; else goto no;
2779 (no:) */
2781 if (false_label_p == NULL)
2782 false_label_p = &local_label;
2784 /* Keep the original source location on the first 'if'. */
2785 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2786 append_to_statement_list (t, &expr);
2788 /* Set the source location of the && on the second 'if'. */
2789 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2790 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2791 new_locus);
2792 append_to_statement_list (t, &expr);
2794 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2796 location_t new_locus;
2798 /* Turn if (a || b) into
2800 if (a) goto yes;
2801 if (b) goto yes; else goto no;
2802 (yes:) */
2804 if (true_label_p == NULL)
2805 true_label_p = &local_label;
2807 /* Keep the original source location on the first 'if'. */
2808 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2809 append_to_statement_list (t, &expr);
2811 /* Set the source location of the || on the second 'if'. */
2812 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2813 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2814 new_locus);
2815 append_to_statement_list (t, &expr);
2817 else if (TREE_CODE (pred) == COND_EXPR
2818 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2819 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2821 location_t new_locus;
2823 /* As long as we're messing with gotos, turn if (a ? b : c) into
2824 if (a)
2825 if (b) goto yes; else goto no;
2826 else
2827 if (c) goto yes; else goto no;
2829 Don't do this if one of the arms has void type, which can happen
2830 in C++ when the arm is throw. */
2832 /* Keep the original source location on the first 'if'. Set the source
2833 location of the ? on the second 'if'. */
2834 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2835 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2836 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2837 false_label_p, locus),
2838 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2839 false_label_p, new_locus));
2841 else
2843 expr = build3 (COND_EXPR, void_type_node, pred,
2844 build_and_jump (true_label_p),
2845 build_and_jump (false_label_p));
2846 SET_EXPR_LOCATION (expr, locus);
2849 if (local_label)
2851 t = build1 (LABEL_EXPR, void_type_node, local_label);
2852 append_to_statement_list (t, &expr);
2855 return expr;
2858 /* Given a conditional expression EXPR with short-circuit boolean
2859 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2860 predicate apart into the equivalent sequence of conditionals. */
2862 static tree
2863 shortcut_cond_expr (tree expr)
2865 tree pred = TREE_OPERAND (expr, 0);
2866 tree then_ = TREE_OPERAND (expr, 1);
2867 tree else_ = TREE_OPERAND (expr, 2);
2868 tree true_label, false_label, end_label, t;
2869 tree *true_label_p;
2870 tree *false_label_p;
2871 bool emit_end, emit_false, jump_over_else;
2872 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2873 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2875 /* First do simple transformations. */
2876 if (!else_se)
2878 /* If there is no 'else', turn
2879 if (a && b) then c
2880 into
2881 if (a) if (b) then c. */
2882 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2884 /* Keep the original source location on the first 'if'. */
2885 location_t locus = EXPR_LOC_OR_HERE (expr);
2886 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2887 /* Set the source location of the && on the second 'if'. */
2888 if (EXPR_HAS_LOCATION (pred))
2889 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2890 then_ = shortcut_cond_expr (expr);
2891 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2892 pred = TREE_OPERAND (pred, 0);
2893 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2894 SET_EXPR_LOCATION (expr, locus);
2898 if (!then_se)
2900 /* If there is no 'then', turn
2901 if (a || b); else d
2902 into
2903 if (a); else if (b); else d. */
2904 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2906 /* Keep the original source location on the first 'if'. */
2907 location_t locus = EXPR_LOC_OR_HERE (expr);
2908 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2909 /* Set the source location of the || on the second 'if'. */
2910 if (EXPR_HAS_LOCATION (pred))
2911 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2912 else_ = shortcut_cond_expr (expr);
2913 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2914 pred = TREE_OPERAND (pred, 0);
2915 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2916 SET_EXPR_LOCATION (expr, locus);
2920 /* If we're done, great. */
2921 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2922 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2923 return expr;
2925 /* Otherwise we need to mess with gotos. Change
2926 if (a) c; else d;
2928 if (a); else goto no;
2929 c; goto end;
2930 no: d; end:
2931 and recursively gimplify the condition. */
2933 true_label = false_label = end_label = NULL_TREE;
2935 /* If our arms just jump somewhere, hijack those labels so we don't
2936 generate jumps to jumps. */
2938 if (then_
2939 && TREE_CODE (then_) == GOTO_EXPR
2940 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2942 true_label = GOTO_DESTINATION (then_);
2943 then_ = NULL;
2944 then_se = false;
2947 if (else_
2948 && TREE_CODE (else_) == GOTO_EXPR
2949 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2951 false_label = GOTO_DESTINATION (else_);
2952 else_ = NULL;
2953 else_se = false;
2956 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2957 if (true_label)
2958 true_label_p = &true_label;
2959 else
2960 true_label_p = NULL;
2962 /* The 'else' branch also needs a label if it contains interesting code. */
2963 if (false_label || else_se)
2964 false_label_p = &false_label;
2965 else
2966 false_label_p = NULL;
2968 /* If there was nothing else in our arms, just forward the label(s). */
2969 if (!then_se && !else_se)
2970 return shortcut_cond_r (pred, true_label_p, false_label_p,
2971 EXPR_LOC_OR_HERE (expr));
2973 /* If our last subexpression already has a terminal label, reuse it. */
2974 if (else_se)
2975 t = expr_last (else_);
2976 else if (then_se)
2977 t = expr_last (then_);
2978 else
2979 t = NULL;
2980 if (t && TREE_CODE (t) == LABEL_EXPR)
2981 end_label = LABEL_EXPR_LABEL (t);
2983 /* If we don't care about jumping to the 'else' branch, jump to the end
2984 if the condition is false. */
2985 if (!false_label_p)
2986 false_label_p = &end_label;
2988 /* We only want to emit these labels if we aren't hijacking them. */
2989 emit_end = (end_label == NULL_TREE);
2990 emit_false = (false_label == NULL_TREE);
2992 /* We only emit the jump over the else clause if we have to--if the
2993 then clause may fall through. Otherwise we can wind up with a
2994 useless jump and a useless label at the end of gimplified code,
2995 which will cause us to think that this conditional as a whole
2996 falls through even if it doesn't. If we then inline a function
2997 which ends with such a condition, that can cause us to issue an
2998 inappropriate warning about control reaching the end of a
2999 non-void function. */
3000 jump_over_else = block_may_fallthru (then_);
3002 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
3003 EXPR_LOC_OR_HERE (expr));
3005 expr = NULL;
3006 append_to_statement_list (pred, &expr);
3008 append_to_statement_list (then_, &expr);
3009 if (else_se)
3011 if (jump_over_else)
3013 tree last = expr_last (expr);
3014 t = build_and_jump (&end_label);
3015 if (EXPR_HAS_LOCATION (last))
3016 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
3017 append_to_statement_list (t, &expr);
3019 if (emit_false)
3021 t = build1 (LABEL_EXPR, void_type_node, false_label);
3022 append_to_statement_list (t, &expr);
3024 append_to_statement_list (else_, &expr);
3026 if (emit_end && end_label)
3028 t = build1 (LABEL_EXPR, void_type_node, end_label);
3029 append_to_statement_list (t, &expr);
3032 return expr;
3035 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
3037 tree
3038 gimple_boolify (tree expr)
3040 tree type = TREE_TYPE (expr);
3041 location_t loc = EXPR_LOCATION (expr);
3043 if (TREE_CODE (expr) == NE_EXPR
3044 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
3045 && integer_zerop (TREE_OPERAND (expr, 1)))
3047 tree call = TREE_OPERAND (expr, 0);
3048 tree fn = get_callee_fndecl (call);
3050 /* For __builtin_expect ((long) (x), y) recurse into x as well
3051 if x is truth_value_p. */
3052 if (fn
3053 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3054 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
3055 && call_expr_nargs (call) == 2)
3057 tree arg = CALL_EXPR_ARG (call, 0);
3058 if (arg)
3060 if (TREE_CODE (arg) == NOP_EXPR
3061 && TREE_TYPE (arg) == TREE_TYPE (call))
3062 arg = TREE_OPERAND (arg, 0);
3063 if (truth_value_p (TREE_CODE (arg)))
3065 arg = gimple_boolify (arg);
3066 CALL_EXPR_ARG (call, 0)
3067 = fold_convert_loc (loc, TREE_TYPE (call), arg);
3073 switch (TREE_CODE (expr))
3075 case TRUTH_AND_EXPR:
3076 case TRUTH_OR_EXPR:
3077 case TRUTH_XOR_EXPR:
3078 case TRUTH_ANDIF_EXPR:
3079 case TRUTH_ORIF_EXPR:
3080 /* Also boolify the arguments of truth exprs. */
3081 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
3082 /* FALLTHRU */
3084 case TRUTH_NOT_EXPR:
3085 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3087 /* These expressions always produce boolean results. */
3088 if (TREE_CODE (type) != BOOLEAN_TYPE)
3089 TREE_TYPE (expr) = boolean_type_node;
3090 return expr;
3092 case ANNOTATE_EXPR:
3093 if ((enum annot_expr_kind) TREE_INT_CST_LOW (TREE_OPERAND (expr, 1))
3094 == annot_expr_ivdep_kind)
3096 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3097 if (TREE_CODE (type) != BOOLEAN_TYPE)
3098 TREE_TYPE (expr) = boolean_type_node;
3099 return expr;
3101 /* FALLTHRU */
3103 default:
3104 if (COMPARISON_CLASS_P (expr))
3106 /* There expressions always prduce boolean results. */
3107 if (TREE_CODE (type) != BOOLEAN_TYPE)
3108 TREE_TYPE (expr) = boolean_type_node;
3109 return expr;
3111 /* Other expressions that get here must have boolean values, but
3112 might need to be converted to the appropriate mode. */
3113 if (TREE_CODE (type) == BOOLEAN_TYPE)
3114 return expr;
3115 return fold_convert_loc (loc, boolean_type_node, expr);
3119 /* Given a conditional expression *EXPR_P without side effects, gimplify
3120 its operands. New statements are inserted to PRE_P. */
3122 static enum gimplify_status
3123 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
3125 tree expr = *expr_p, cond;
3126 enum gimplify_status ret, tret;
3127 enum tree_code code;
3129 cond = gimple_boolify (COND_EXPR_COND (expr));
3131 /* We need to handle && and || specially, as their gimplification
3132 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3133 code = TREE_CODE (cond);
3134 if (code == TRUTH_ANDIF_EXPR)
3135 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
3136 else if (code == TRUTH_ORIF_EXPR)
3137 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
3138 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
3139 COND_EXPR_COND (*expr_p) = cond;
3141 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
3142 is_gimple_val, fb_rvalue);
3143 ret = MIN (ret, tret);
3144 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
3145 is_gimple_val, fb_rvalue);
3147 return MIN (ret, tret);
3150 /* Return true if evaluating EXPR could trap.
3151 EXPR is GENERIC, while tree_could_trap_p can be called
3152 only on GIMPLE. */
3154 static bool
3155 generic_expr_could_trap_p (tree expr)
3157 unsigned i, n;
3159 if (!expr || is_gimple_val (expr))
3160 return false;
3162 if (!EXPR_P (expr) || tree_could_trap_p (expr))
3163 return true;
3165 n = TREE_OPERAND_LENGTH (expr);
3166 for (i = 0; i < n; i++)
3167 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
3168 return true;
3170 return false;
3173 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3174 into
3176 if (p) if (p)
3177 t1 = a; a;
3178 else or else
3179 t1 = b; b;
3182 The second form is used when *EXPR_P is of type void.
3184 PRE_P points to the list where side effects that must happen before
3185 *EXPR_P should be stored. */
3187 static enum gimplify_status
3188 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3190 tree expr = *expr_p;
3191 tree type = TREE_TYPE (expr);
3192 location_t loc = EXPR_LOCATION (expr);
3193 tree tmp, arm1, arm2;
3194 enum gimplify_status ret;
3195 tree label_true, label_false, label_cont;
3196 bool have_then_clause_p, have_else_clause_p;
3197 gimple gimple_cond;
3198 enum tree_code pred_code;
3199 gimple_seq seq = NULL;
3201 /* If this COND_EXPR has a value, copy the values into a temporary within
3202 the arms. */
3203 if (!VOID_TYPE_P (type))
3205 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3206 tree result;
3208 /* If either an rvalue is ok or we do not require an lvalue, create the
3209 temporary. But we cannot do that if the type is addressable. */
3210 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3211 && !TREE_ADDRESSABLE (type))
3213 if (gimplify_ctxp->allow_rhs_cond_expr
3214 /* If either branch has side effects or could trap, it can't be
3215 evaluated unconditionally. */
3216 && !TREE_SIDE_EFFECTS (then_)
3217 && !generic_expr_could_trap_p (then_)
3218 && !TREE_SIDE_EFFECTS (else_)
3219 && !generic_expr_could_trap_p (else_))
3220 return gimplify_pure_cond_expr (expr_p, pre_p);
3222 tmp = create_tmp_var (type, "iftmp");
3223 result = tmp;
3226 /* Otherwise, only create and copy references to the values. */
3227 else
3229 type = build_pointer_type (type);
3231 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3232 then_ = build_fold_addr_expr_loc (loc, then_);
3234 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3235 else_ = build_fold_addr_expr_loc (loc, else_);
3237 expr
3238 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3240 tmp = create_tmp_var (type, "iftmp");
3241 result = build_simple_mem_ref_loc (loc, tmp);
3244 /* Build the new then clause, `tmp = then_;'. But don't build the
3245 assignment if the value is void; in C++ it can be if it's a throw. */
3246 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3247 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3249 /* Similarly, build the new else clause, `tmp = else_;'. */
3250 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3251 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3253 TREE_TYPE (expr) = void_type_node;
3254 recalculate_side_effects (expr);
3256 /* Move the COND_EXPR to the prequeue. */
3257 gimplify_stmt (&expr, pre_p);
3259 *expr_p = result;
3260 return GS_ALL_DONE;
3263 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3264 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3265 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3266 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3268 /* Make sure the condition has BOOLEAN_TYPE. */
3269 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3271 /* Break apart && and || conditions. */
3272 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3273 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3275 expr = shortcut_cond_expr (expr);
3277 if (expr != *expr_p)
3279 *expr_p = expr;
3281 /* We can't rely on gimplify_expr to re-gimplify the expanded
3282 form properly, as cleanups might cause the target labels to be
3283 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3284 set up a conditional context. */
3285 gimple_push_condition ();
3286 gimplify_stmt (expr_p, &seq);
3287 gimple_pop_condition (pre_p);
3288 gimple_seq_add_seq (pre_p, seq);
3290 return GS_ALL_DONE;
3294 /* Now do the normal gimplification. */
3296 /* Gimplify condition. */
3297 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3298 fb_rvalue);
3299 if (ret == GS_ERROR)
3300 return GS_ERROR;
3301 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3303 gimple_push_condition ();
3305 have_then_clause_p = have_else_clause_p = false;
3306 if (TREE_OPERAND (expr, 1) != NULL
3307 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3308 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3309 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3310 == current_function_decl)
3311 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3312 have different locations, otherwise we end up with incorrect
3313 location information on the branches. */
3314 && (optimize
3315 || !EXPR_HAS_LOCATION (expr)
3316 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3317 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3319 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3320 have_then_clause_p = true;
3322 else
3323 label_true = create_artificial_label (UNKNOWN_LOCATION);
3324 if (TREE_OPERAND (expr, 2) != NULL
3325 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3326 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3327 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3328 == current_function_decl)
3329 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3330 have different locations, otherwise we end up with incorrect
3331 location information on the branches. */
3332 && (optimize
3333 || !EXPR_HAS_LOCATION (expr)
3334 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3335 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3337 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3338 have_else_clause_p = true;
3340 else
3341 label_false = create_artificial_label (UNKNOWN_LOCATION);
3343 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3344 &arm2);
3346 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3347 label_false);
3349 gimplify_seq_add_stmt (&seq, gimple_cond);
3350 label_cont = NULL_TREE;
3351 if (!have_then_clause_p)
3353 /* For if (...) {} else { code; } put label_true after
3354 the else block. */
3355 if (TREE_OPERAND (expr, 1) == NULL_TREE
3356 && !have_else_clause_p
3357 && TREE_OPERAND (expr, 2) != NULL_TREE)
3358 label_cont = label_true;
3359 else
3361 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3362 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3363 /* For if (...) { code; } else {} or
3364 if (...) { code; } else goto label; or
3365 if (...) { code; return; } else { ... }
3366 label_cont isn't needed. */
3367 if (!have_else_clause_p
3368 && TREE_OPERAND (expr, 2) != NULL_TREE
3369 && gimple_seq_may_fallthru (seq))
3371 gimple g;
3372 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3374 g = gimple_build_goto (label_cont);
3376 /* GIMPLE_COND's are very low level; they have embedded
3377 gotos. This particular embedded goto should not be marked
3378 with the location of the original COND_EXPR, as it would
3379 correspond to the COND_EXPR's condition, not the ELSE or the
3380 THEN arms. To avoid marking it with the wrong location, flag
3381 it as "no location". */
3382 gimple_set_do_not_emit_location (g);
3384 gimplify_seq_add_stmt (&seq, g);
3388 if (!have_else_clause_p)
3390 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3391 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3393 if (label_cont)
3394 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3396 gimple_pop_condition (pre_p);
3397 gimple_seq_add_seq (pre_p, seq);
3399 if (ret == GS_ERROR)
3400 ; /* Do nothing. */
3401 else if (have_then_clause_p || have_else_clause_p)
3402 ret = GS_ALL_DONE;
3403 else
3405 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3406 expr = TREE_OPERAND (expr, 0);
3407 gimplify_stmt (&expr, pre_p);
3410 *expr_p = NULL;
3411 return ret;
3414 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3415 to be marked addressable.
3417 We cannot rely on such an expression being directly markable if a temporary
3418 has been created by the gimplification. In this case, we create another
3419 temporary and initialize it with a copy, which will become a store after we
3420 mark it addressable. This can happen if the front-end passed us something
3421 that it could not mark addressable yet, like a Fortran pass-by-reference
3422 parameter (int) floatvar. */
3424 static void
3425 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3427 while (handled_component_p (*expr_p))
3428 expr_p = &TREE_OPERAND (*expr_p, 0);
3429 if (is_gimple_reg (*expr_p))
3430 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3433 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3434 a call to __builtin_memcpy. */
3436 static enum gimplify_status
3437 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3438 gimple_seq *seq_p)
3440 tree t, to, to_ptr, from, from_ptr;
3441 gimple gs;
3442 location_t loc = EXPR_LOCATION (*expr_p);
3444 to = TREE_OPERAND (*expr_p, 0);
3445 from = TREE_OPERAND (*expr_p, 1);
3447 /* Mark the RHS addressable. Beware that it may not be possible to do so
3448 directly if a temporary has been created by the gimplification. */
3449 prepare_gimple_addressable (&from, seq_p);
3451 mark_addressable (from);
3452 from_ptr = build_fold_addr_expr_loc (loc, from);
3453 gimplify_arg (&from_ptr, seq_p, loc);
3455 mark_addressable (to);
3456 to_ptr = build_fold_addr_expr_loc (loc, to);
3457 gimplify_arg (&to_ptr, seq_p, loc);
3459 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3461 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3463 if (want_value)
3465 /* tmp = memcpy() */
3466 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3467 gimple_call_set_lhs (gs, t);
3468 gimplify_seq_add_stmt (seq_p, gs);
3470 *expr_p = build_simple_mem_ref (t);
3471 return GS_ALL_DONE;
3474 gimplify_seq_add_stmt (seq_p, gs);
3475 *expr_p = NULL;
3476 return GS_ALL_DONE;
3479 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3480 a call to __builtin_memset. In this case we know that the RHS is
3481 a CONSTRUCTOR with an empty element list. */
3483 static enum gimplify_status
3484 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3485 gimple_seq *seq_p)
3487 tree t, from, to, to_ptr;
3488 gimple gs;
3489 location_t loc = EXPR_LOCATION (*expr_p);
3491 /* Assert our assumptions, to abort instead of producing wrong code
3492 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3493 not be immediately exposed. */
3494 from = TREE_OPERAND (*expr_p, 1);
3495 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3496 from = TREE_OPERAND (from, 0);
3498 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3499 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from)));
3501 /* Now proceed. */
3502 to = TREE_OPERAND (*expr_p, 0);
3504 to_ptr = build_fold_addr_expr_loc (loc, to);
3505 gimplify_arg (&to_ptr, seq_p, loc);
3506 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3508 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3510 if (want_value)
3512 /* tmp = memset() */
3513 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3514 gimple_call_set_lhs (gs, t);
3515 gimplify_seq_add_stmt (seq_p, gs);
3517 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3518 return GS_ALL_DONE;
3521 gimplify_seq_add_stmt (seq_p, gs);
3522 *expr_p = NULL;
3523 return GS_ALL_DONE;
3526 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3527 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3528 assignment. Return non-null if we detect a potential overlap. */
3530 struct gimplify_init_ctor_preeval_data
3532 /* The base decl of the lhs object. May be NULL, in which case we
3533 have to assume the lhs is indirect. */
3534 tree lhs_base_decl;
3536 /* The alias set of the lhs object. */
3537 alias_set_type lhs_alias_set;
3540 static tree
3541 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3543 struct gimplify_init_ctor_preeval_data *data
3544 = (struct gimplify_init_ctor_preeval_data *) xdata;
3545 tree t = *tp;
3547 /* If we find the base object, obviously we have overlap. */
3548 if (data->lhs_base_decl == t)
3549 return t;
3551 /* If the constructor component is indirect, determine if we have a
3552 potential overlap with the lhs. The only bits of information we
3553 have to go on at this point are addressability and alias sets. */
3554 if ((INDIRECT_REF_P (t)
3555 || TREE_CODE (t) == MEM_REF)
3556 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3557 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3558 return t;
3560 /* If the constructor component is a call, determine if it can hide a
3561 potential overlap with the lhs through an INDIRECT_REF like above.
3562 ??? Ugh - this is completely broken. In fact this whole analysis
3563 doesn't look conservative. */
3564 if (TREE_CODE (t) == CALL_EXPR)
3566 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3568 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3569 if (POINTER_TYPE_P (TREE_VALUE (type))
3570 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3571 && alias_sets_conflict_p (data->lhs_alias_set,
3572 get_alias_set
3573 (TREE_TYPE (TREE_VALUE (type)))))
3574 return t;
3577 if (IS_TYPE_OR_DECL_P (t))
3578 *walk_subtrees = 0;
3579 return NULL;
3582 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3583 force values that overlap with the lhs (as described by *DATA)
3584 into temporaries. */
3586 static void
3587 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3588 struct gimplify_init_ctor_preeval_data *data)
3590 enum gimplify_status one;
3592 /* If the value is constant, then there's nothing to pre-evaluate. */
3593 if (TREE_CONSTANT (*expr_p))
3595 /* Ensure it does not have side effects, it might contain a reference to
3596 the object we're initializing. */
3597 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3598 return;
3601 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3602 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3603 return;
3605 /* Recurse for nested constructors. */
3606 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3608 unsigned HOST_WIDE_INT ix;
3609 constructor_elt *ce;
3610 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (*expr_p);
3612 FOR_EACH_VEC_SAFE_ELT (v, ix, ce)
3613 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3615 return;
3618 /* If this is a variable sized type, we must remember the size. */
3619 maybe_with_size_expr (expr_p);
3621 /* Gimplify the constructor element to something appropriate for the rhs
3622 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3623 the gimplifier will consider this a store to memory. Doing this
3624 gimplification now means that we won't have to deal with complicated
3625 language-specific trees, nor trees like SAVE_EXPR that can induce
3626 exponential search behavior. */
3627 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3628 if (one == GS_ERROR)
3630 *expr_p = NULL;
3631 return;
3634 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3635 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3636 always be true for all scalars, since is_gimple_mem_rhs insists on a
3637 temporary variable for them. */
3638 if (DECL_P (*expr_p))
3639 return;
3641 /* If this is of variable size, we have no choice but to assume it doesn't
3642 overlap since we can't make a temporary for it. */
3643 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3644 return;
3646 /* Otherwise, we must search for overlap ... */
3647 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3648 return;
3650 /* ... and if found, force the value into a temporary. */
3651 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3654 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3655 a RANGE_EXPR in a CONSTRUCTOR for an array.
3657 var = lower;
3658 loop_entry:
3659 object[var] = value;
3660 if (var == upper)
3661 goto loop_exit;
3662 var = var + 1;
3663 goto loop_entry;
3664 loop_exit:
3666 We increment var _after_ the loop exit check because we might otherwise
3667 fail if upper == TYPE_MAX_VALUE (type for upper).
3669 Note that we never have to deal with SAVE_EXPRs here, because this has
3670 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3672 static void gimplify_init_ctor_eval (tree, vec<constructor_elt, va_gc> *,
3673 gimple_seq *, bool);
3675 static void
3676 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3677 tree value, tree array_elt_type,
3678 gimple_seq *pre_p, bool cleared)
3680 tree loop_entry_label, loop_exit_label, fall_thru_label;
3681 tree var, var_type, cref, tmp;
3683 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3684 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3685 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3687 /* Create and initialize the index variable. */
3688 var_type = TREE_TYPE (upper);
3689 var = create_tmp_var (var_type, NULL);
3690 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3692 /* Add the loop entry label. */
3693 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3695 /* Build the reference. */
3696 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3697 var, NULL_TREE, NULL_TREE);
3699 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3700 the store. Otherwise just assign value to the reference. */
3702 if (TREE_CODE (value) == CONSTRUCTOR)
3703 /* NB we might have to call ourself recursively through
3704 gimplify_init_ctor_eval if the value is a constructor. */
3705 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3706 pre_p, cleared);
3707 else
3708 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3710 /* We exit the loop when the index var is equal to the upper bound. */
3711 gimplify_seq_add_stmt (pre_p,
3712 gimple_build_cond (EQ_EXPR, var, upper,
3713 loop_exit_label, fall_thru_label));
3715 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3717 /* Otherwise, increment the index var... */
3718 tmp = build2 (PLUS_EXPR, var_type, var,
3719 fold_convert (var_type, integer_one_node));
3720 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3722 /* ...and jump back to the loop entry. */
3723 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3725 /* Add the loop exit label. */
3726 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3729 /* Return true if FDECL is accessing a field that is zero sized. */
3731 static bool
3732 zero_sized_field_decl (const_tree fdecl)
3734 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3735 && integer_zerop (DECL_SIZE (fdecl)))
3736 return true;
3737 return false;
3740 /* Return true if TYPE is zero sized. */
3742 static bool
3743 zero_sized_type (const_tree type)
3745 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3746 && integer_zerop (TYPE_SIZE (type)))
3747 return true;
3748 return false;
3751 /* A subroutine of gimplify_init_constructor. Generate individual
3752 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3753 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3754 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3755 zeroed first. */
3757 static void
3758 gimplify_init_ctor_eval (tree object, vec<constructor_elt, va_gc> *elts,
3759 gimple_seq *pre_p, bool cleared)
3761 tree array_elt_type = NULL;
3762 unsigned HOST_WIDE_INT ix;
3763 tree purpose, value;
3765 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3766 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3768 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3770 tree cref;
3772 /* NULL values are created above for gimplification errors. */
3773 if (value == NULL)
3774 continue;
3776 if (cleared && initializer_zerop (value))
3777 continue;
3779 /* ??? Here's to hoping the front end fills in all of the indices,
3780 so we don't have to figure out what's missing ourselves. */
3781 gcc_assert (purpose);
3783 /* Skip zero-sized fields, unless value has side-effects. This can
3784 happen with calls to functions returning a zero-sized type, which
3785 we shouldn't discard. As a number of downstream passes don't
3786 expect sets of zero-sized fields, we rely on the gimplification of
3787 the MODIFY_EXPR we make below to drop the assignment statement. */
3788 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3789 continue;
3791 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3792 whole range. */
3793 if (TREE_CODE (purpose) == RANGE_EXPR)
3795 tree lower = TREE_OPERAND (purpose, 0);
3796 tree upper = TREE_OPERAND (purpose, 1);
3798 /* If the lower bound is equal to upper, just treat it as if
3799 upper was the index. */
3800 if (simple_cst_equal (lower, upper))
3801 purpose = upper;
3802 else
3804 gimplify_init_ctor_eval_range (object, lower, upper, value,
3805 array_elt_type, pre_p, cleared);
3806 continue;
3810 if (array_elt_type)
3812 /* Do not use bitsizetype for ARRAY_REF indices. */
3813 if (TYPE_DOMAIN (TREE_TYPE (object)))
3814 purpose
3815 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3816 purpose);
3817 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3818 purpose, NULL_TREE, NULL_TREE);
3820 else
3822 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3823 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3824 unshare_expr (object), purpose, NULL_TREE);
3827 if (TREE_CODE (value) == CONSTRUCTOR
3828 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3829 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3830 pre_p, cleared);
3831 else
3833 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3834 gimplify_and_add (init, pre_p);
3835 ggc_free (init);
3840 /* Return the appropriate RHS predicate for this LHS. */
3842 gimple_predicate
3843 rhs_predicate_for (tree lhs)
3845 if (is_gimple_reg (lhs))
3846 return is_gimple_reg_rhs_or_call;
3847 else
3848 return is_gimple_mem_rhs_or_call;
3851 /* Gimplify a C99 compound literal expression. This just means adding
3852 the DECL_EXPR before the current statement and using its anonymous
3853 decl instead. */
3855 static enum gimplify_status
3856 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p,
3857 bool (*gimple_test_f) (tree),
3858 fallback_t fallback)
3860 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3861 tree decl = DECL_EXPR_DECL (decl_s);
3862 tree init = DECL_INITIAL (decl);
3863 /* Mark the decl as addressable if the compound literal
3864 expression is addressable now, otherwise it is marked too late
3865 after we gimplify the initialization expression. */
3866 if (TREE_ADDRESSABLE (*expr_p))
3867 TREE_ADDRESSABLE (decl) = 1;
3868 /* Otherwise, if we don't need an lvalue and have a literal directly
3869 substitute it. Check if it matches the gimple predicate, as
3870 otherwise we'd generate a new temporary, and we can as well just
3871 use the decl we already have. */
3872 else if (!TREE_ADDRESSABLE (decl)
3873 && init
3874 && (fallback & fb_lvalue) == 0
3875 && gimple_test_f (init))
3877 *expr_p = init;
3878 return GS_OK;
3881 /* Preliminarily mark non-addressed complex variables as eligible
3882 for promotion to gimple registers. We'll transform their uses
3883 as we find them. */
3884 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3885 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3886 && !TREE_THIS_VOLATILE (decl)
3887 && !needs_to_live_in_memory (decl))
3888 DECL_GIMPLE_REG_P (decl) = 1;
3890 /* If the decl is not addressable, then it is being used in some
3891 expression or on the right hand side of a statement, and it can
3892 be put into a readonly data section. */
3893 if (!TREE_ADDRESSABLE (decl) && (fallback & fb_lvalue) == 0)
3894 TREE_READONLY (decl) = 1;
3896 /* This decl isn't mentioned in the enclosing block, so add it to the
3897 list of temps. FIXME it seems a bit of a kludge to say that
3898 anonymous artificial vars aren't pushed, but everything else is. */
3899 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3900 gimple_add_tmp_var (decl);
3902 gimplify_and_add (decl_s, pre_p);
3903 *expr_p = decl;
3904 return GS_OK;
3907 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3908 return a new CONSTRUCTOR if something changed. */
3910 static tree
3911 optimize_compound_literals_in_ctor (tree orig_ctor)
3913 tree ctor = orig_ctor;
3914 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3915 unsigned int idx, num = vec_safe_length (elts);
3917 for (idx = 0; idx < num; idx++)
3919 tree value = (*elts)[idx].value;
3920 tree newval = value;
3921 if (TREE_CODE (value) == CONSTRUCTOR)
3922 newval = optimize_compound_literals_in_ctor (value);
3923 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3925 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3926 tree decl = DECL_EXPR_DECL (decl_s);
3927 tree init = DECL_INITIAL (decl);
3929 if (!TREE_ADDRESSABLE (value)
3930 && !TREE_ADDRESSABLE (decl)
3931 && init
3932 && TREE_CODE (init) == CONSTRUCTOR)
3933 newval = optimize_compound_literals_in_ctor (init);
3935 if (newval == value)
3936 continue;
3938 if (ctor == orig_ctor)
3940 ctor = copy_node (orig_ctor);
3941 CONSTRUCTOR_ELTS (ctor) = vec_safe_copy (elts);
3942 elts = CONSTRUCTOR_ELTS (ctor);
3944 (*elts)[idx].value = newval;
3946 return ctor;
3949 /* A subroutine of gimplify_modify_expr. Break out elements of a
3950 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3952 Note that we still need to clear any elements that don't have explicit
3953 initializers, so if not all elements are initialized we keep the
3954 original MODIFY_EXPR, we just remove all of the constructor elements.
3956 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3957 GS_ERROR if we would have to create a temporary when gimplifying
3958 this constructor. Otherwise, return GS_OK.
3960 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3962 static enum gimplify_status
3963 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3964 bool want_value, bool notify_temp_creation)
3966 tree object, ctor, type;
3967 enum gimplify_status ret;
3968 vec<constructor_elt, va_gc> *elts;
3970 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3972 if (!notify_temp_creation)
3974 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3975 is_gimple_lvalue, fb_lvalue);
3976 if (ret == GS_ERROR)
3977 return ret;
3980 object = TREE_OPERAND (*expr_p, 0);
3981 ctor = TREE_OPERAND (*expr_p, 1) =
3982 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3983 type = TREE_TYPE (ctor);
3984 elts = CONSTRUCTOR_ELTS (ctor);
3985 ret = GS_ALL_DONE;
3987 switch (TREE_CODE (type))
3989 case RECORD_TYPE:
3990 case UNION_TYPE:
3991 case QUAL_UNION_TYPE:
3992 case ARRAY_TYPE:
3994 struct gimplify_init_ctor_preeval_data preeval_data;
3995 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3996 bool cleared, complete_p, valid_const_initializer;
3998 /* Aggregate types must lower constructors to initialization of
3999 individual elements. The exception is that a CONSTRUCTOR node
4000 with no elements indicates zero-initialization of the whole. */
4001 if (vec_safe_is_empty (elts))
4003 if (notify_temp_creation)
4004 return GS_OK;
4005 break;
4008 /* Fetch information about the constructor to direct later processing.
4009 We might want to make static versions of it in various cases, and
4010 can only do so if it known to be a valid constant initializer. */
4011 valid_const_initializer
4012 = categorize_ctor_elements (ctor, &num_nonzero_elements,
4013 &num_ctor_elements, &complete_p);
4015 /* If a const aggregate variable is being initialized, then it
4016 should never be a lose to promote the variable to be static. */
4017 if (valid_const_initializer
4018 && num_nonzero_elements > 1
4019 && TREE_READONLY (object)
4020 && TREE_CODE (object) == VAR_DECL
4021 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
4023 if (notify_temp_creation)
4024 return GS_ERROR;
4025 DECL_INITIAL (object) = ctor;
4026 TREE_STATIC (object) = 1;
4027 if (!DECL_NAME (object))
4028 DECL_NAME (object) = create_tmp_var_name ("C");
4029 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
4031 /* ??? C++ doesn't automatically append a .<number> to the
4032 assembler name, and even when it does, it looks at FE private
4033 data structures to figure out what that number should be,
4034 which are not set for this variable. I suppose this is
4035 important for local statics for inline functions, which aren't
4036 "local" in the object file sense. So in order to get a unique
4037 TU-local symbol, we must invoke the lhd version now. */
4038 lhd_set_decl_assembler_name (object);
4040 *expr_p = NULL_TREE;
4041 break;
4044 /* If there are "lots" of initialized elements, even discounting
4045 those that are not address constants (and thus *must* be
4046 computed at runtime), then partition the constructor into
4047 constant and non-constant parts. Block copy the constant
4048 parts in, then generate code for the non-constant parts. */
4049 /* TODO. There's code in cp/typeck.c to do this. */
4051 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
4052 /* store_constructor will ignore the clearing of variable-sized
4053 objects. Initializers for such objects must explicitly set
4054 every field that needs to be set. */
4055 cleared = false;
4056 else if (!complete_p)
4057 /* If the constructor isn't complete, clear the whole object
4058 beforehand.
4060 ??? This ought not to be needed. For any element not present
4061 in the initializer, we should simply set them to zero. Except
4062 we'd need to *find* the elements that are not present, and that
4063 requires trickery to avoid quadratic compile-time behavior in
4064 large cases or excessive memory use in small cases. */
4065 cleared = true;
4066 else if (num_ctor_elements - num_nonzero_elements
4067 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
4068 && num_nonzero_elements < num_ctor_elements / 4)
4069 /* If there are "lots" of zeros, it's more efficient to clear
4070 the memory and then set the nonzero elements. */
4071 cleared = true;
4072 else
4073 cleared = false;
4075 /* If there are "lots" of initialized elements, and all of them
4076 are valid address constants, then the entire initializer can
4077 be dropped to memory, and then memcpy'd out. Don't do this
4078 for sparse arrays, though, as it's more efficient to follow
4079 the standard CONSTRUCTOR behavior of memset followed by
4080 individual element initialization. Also don't do this for small
4081 all-zero initializers (which aren't big enough to merit
4082 clearing), and don't try to make bitwise copies of
4083 TREE_ADDRESSABLE types.
4085 We cannot apply such transformation when compiling chkp static
4086 initializer because creation of initializer image in the memory
4087 will require static initialization of bounds for it. It should
4088 result in another gimplification of similar initializer and we
4089 may fall into infinite loop. */
4090 if (valid_const_initializer
4091 && !(cleared || num_nonzero_elements == 0)
4092 && !TREE_ADDRESSABLE (type)
4093 && (!current_function_decl
4094 || !lookup_attribute ("chkp ctor",
4095 DECL_ATTRIBUTES (current_function_decl))))
4097 HOST_WIDE_INT size = int_size_in_bytes (type);
4098 unsigned int align;
4100 /* ??? We can still get unbounded array types, at least
4101 from the C++ front end. This seems wrong, but attempt
4102 to work around it for now. */
4103 if (size < 0)
4105 size = int_size_in_bytes (TREE_TYPE (object));
4106 if (size >= 0)
4107 TREE_TYPE (ctor) = type = TREE_TYPE (object);
4110 /* Find the maximum alignment we can assume for the object. */
4111 /* ??? Make use of DECL_OFFSET_ALIGN. */
4112 if (DECL_P (object))
4113 align = DECL_ALIGN (object);
4114 else
4115 align = TYPE_ALIGN (type);
4117 /* Do a block move either if the size is so small as to make
4118 each individual move a sub-unit move on average, or if it
4119 is so large as to make individual moves inefficient. */
4120 if (size > 0
4121 && num_nonzero_elements > 1
4122 && (size < num_nonzero_elements
4123 || !can_move_by_pieces (size, align)))
4125 if (notify_temp_creation)
4126 return GS_ERROR;
4128 walk_tree (&ctor, force_labels_r, NULL, NULL);
4129 ctor = tree_output_constant_def (ctor);
4130 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
4131 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
4132 TREE_OPERAND (*expr_p, 1) = ctor;
4134 /* This is no longer an assignment of a CONSTRUCTOR, but
4135 we still may have processing to do on the LHS. So
4136 pretend we didn't do anything here to let that happen. */
4137 return GS_UNHANDLED;
4141 /* If the target is volatile, we have non-zero elements and more than
4142 one field to assign, initialize the target from a temporary. */
4143 if (TREE_THIS_VOLATILE (object)
4144 && !TREE_ADDRESSABLE (type)
4145 && num_nonzero_elements > 0
4146 && vec_safe_length (elts) > 1)
4148 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
4149 TREE_OPERAND (*expr_p, 0) = temp;
4150 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
4151 *expr_p,
4152 build2 (MODIFY_EXPR, void_type_node,
4153 object, temp));
4154 return GS_OK;
4157 if (notify_temp_creation)
4158 return GS_OK;
4160 /* If there are nonzero elements and if needed, pre-evaluate to capture
4161 elements overlapping with the lhs into temporaries. We must do this
4162 before clearing to fetch the values before they are zeroed-out. */
4163 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
4165 preeval_data.lhs_base_decl = get_base_address (object);
4166 if (!DECL_P (preeval_data.lhs_base_decl))
4167 preeval_data.lhs_base_decl = NULL;
4168 preeval_data.lhs_alias_set = get_alias_set (object);
4170 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
4171 pre_p, post_p, &preeval_data);
4174 if (cleared)
4176 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4177 Note that we still have to gimplify, in order to handle the
4178 case of variable sized types. Avoid shared tree structures. */
4179 CONSTRUCTOR_ELTS (ctor) = NULL;
4180 TREE_SIDE_EFFECTS (ctor) = 0;
4181 object = unshare_expr (object);
4182 gimplify_stmt (expr_p, pre_p);
4185 /* If we have not block cleared the object, or if there are nonzero
4186 elements in the constructor, add assignments to the individual
4187 scalar fields of the object. */
4188 if (!cleared || num_nonzero_elements > 0)
4189 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
4191 *expr_p = NULL_TREE;
4193 break;
4195 case COMPLEX_TYPE:
4197 tree r, i;
4199 if (notify_temp_creation)
4200 return GS_OK;
4202 /* Extract the real and imaginary parts out of the ctor. */
4203 gcc_assert (elts->length () == 2);
4204 r = (*elts)[0].value;
4205 i = (*elts)[1].value;
4206 if (r == NULL || i == NULL)
4208 tree zero = build_zero_cst (TREE_TYPE (type));
4209 if (r == NULL)
4210 r = zero;
4211 if (i == NULL)
4212 i = zero;
4215 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4216 represent creation of a complex value. */
4217 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
4219 ctor = build_complex (type, r, i);
4220 TREE_OPERAND (*expr_p, 1) = ctor;
4222 else
4224 ctor = build2 (COMPLEX_EXPR, type, r, i);
4225 TREE_OPERAND (*expr_p, 1) = ctor;
4226 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4227 pre_p,
4228 post_p,
4229 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4230 fb_rvalue);
4233 break;
4235 case VECTOR_TYPE:
4237 unsigned HOST_WIDE_INT ix;
4238 constructor_elt *ce;
4240 if (notify_temp_creation)
4241 return GS_OK;
4243 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4244 if (TREE_CONSTANT (ctor))
4246 bool constant_p = true;
4247 tree value;
4249 /* Even when ctor is constant, it might contain non-*_CST
4250 elements, such as addresses or trapping values like
4251 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4252 in VECTOR_CST nodes. */
4253 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4254 if (!CONSTANT_CLASS_P (value))
4256 constant_p = false;
4257 break;
4260 if (constant_p)
4262 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4263 break;
4266 /* Don't reduce an initializer constant even if we can't
4267 make a VECTOR_CST. It won't do anything for us, and it'll
4268 prevent us from representing it as a single constant. */
4269 if (initializer_constant_valid_p (ctor, type))
4270 break;
4272 TREE_CONSTANT (ctor) = 0;
4275 /* Vector types use CONSTRUCTOR all the way through gimple
4276 compilation as a general initializer. */
4277 FOR_EACH_VEC_SAFE_ELT (elts, ix, ce)
4279 enum gimplify_status tret;
4280 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4281 fb_rvalue);
4282 if (tret == GS_ERROR)
4283 ret = GS_ERROR;
4285 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4286 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4288 break;
4290 default:
4291 /* So how did we get a CONSTRUCTOR for a scalar type? */
4292 gcc_unreachable ();
4295 if (ret == GS_ERROR)
4296 return GS_ERROR;
4297 else if (want_value)
4299 *expr_p = object;
4300 return GS_OK;
4302 else
4304 /* If we have gimplified both sides of the initializer but have
4305 not emitted an assignment, do so now. */
4306 if (*expr_p)
4308 tree lhs = TREE_OPERAND (*expr_p, 0);
4309 tree rhs = TREE_OPERAND (*expr_p, 1);
4310 gimple init = gimple_build_assign (lhs, rhs);
4311 gimplify_seq_add_stmt (pre_p, init);
4312 *expr_p = NULL;
4315 return GS_ALL_DONE;
4319 /* Given a pointer value OP0, return a simplified version of an
4320 indirection through OP0, or NULL_TREE if no simplification is
4321 possible. This may only be applied to a rhs of an expression.
4322 Note that the resulting type may be different from the type pointed
4323 to in the sense that it is still compatible from the langhooks
4324 point of view. */
4326 static tree
4327 gimple_fold_indirect_ref_rhs (tree t)
4329 return gimple_fold_indirect_ref (t);
4332 /* Subroutine of gimplify_modify_expr to do simplifications of
4333 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4334 something changes. */
4336 static enum gimplify_status
4337 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4338 gimple_seq *pre_p, gimple_seq *post_p,
4339 bool want_value)
4341 enum gimplify_status ret = GS_UNHANDLED;
4342 bool changed;
4346 changed = false;
4347 switch (TREE_CODE (*from_p))
4349 case VAR_DECL:
4350 /* If we're assigning from a read-only variable initialized with
4351 a constructor, do the direct assignment from the constructor,
4352 but only if neither source nor target are volatile since this
4353 latter assignment might end up being done on a per-field basis. */
4354 if (DECL_INITIAL (*from_p)
4355 && TREE_READONLY (*from_p)
4356 && !TREE_THIS_VOLATILE (*from_p)
4357 && !TREE_THIS_VOLATILE (*to_p)
4358 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4360 tree old_from = *from_p;
4361 enum gimplify_status subret;
4363 /* Move the constructor into the RHS. */
4364 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4366 /* Let's see if gimplify_init_constructor will need to put
4367 it in memory. */
4368 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4369 false, true);
4370 if (subret == GS_ERROR)
4372 /* If so, revert the change. */
4373 *from_p = old_from;
4375 else
4377 ret = GS_OK;
4378 changed = true;
4381 break;
4382 case INDIRECT_REF:
4384 /* If we have code like
4386 *(const A*)(A*)&x
4388 where the type of "x" is a (possibly cv-qualified variant
4389 of "A"), treat the entire expression as identical to "x".
4390 This kind of code arises in C++ when an object is bound
4391 to a const reference, and if "x" is a TARGET_EXPR we want
4392 to take advantage of the optimization below. */
4393 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4394 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4395 if (t)
4397 if (TREE_THIS_VOLATILE (t) != volatile_p)
4399 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4400 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4401 build_fold_addr_expr (t));
4402 if (REFERENCE_CLASS_P (t))
4403 TREE_THIS_VOLATILE (t) = volatile_p;
4405 *from_p = t;
4406 ret = GS_OK;
4407 changed = true;
4409 break;
4412 case TARGET_EXPR:
4414 /* If we are initializing something from a TARGET_EXPR, strip the
4415 TARGET_EXPR and initialize it directly, if possible. This can't
4416 be done if the initializer is void, since that implies that the
4417 temporary is set in some non-trivial way.
4419 ??? What about code that pulls out the temp and uses it
4420 elsewhere? I think that such code never uses the TARGET_EXPR as
4421 an initializer. If I'm wrong, we'll die because the temp won't
4422 have any RTL. In that case, I guess we'll need to replace
4423 references somehow. */
4424 tree init = TARGET_EXPR_INITIAL (*from_p);
4426 if (init
4427 && !VOID_TYPE_P (TREE_TYPE (init)))
4429 *from_p = init;
4430 ret = GS_OK;
4431 changed = true;
4434 break;
4436 case COMPOUND_EXPR:
4437 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4438 caught. */
4439 gimplify_compound_expr (from_p, pre_p, true);
4440 ret = GS_OK;
4441 changed = true;
4442 break;
4444 case CONSTRUCTOR:
4445 /* If we already made some changes, let the front end have a
4446 crack at this before we break it down. */
4447 if (ret != GS_UNHANDLED)
4448 break;
4449 /* If we're initializing from a CONSTRUCTOR, break this into
4450 individual MODIFY_EXPRs. */
4451 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4452 false);
4454 case COND_EXPR:
4455 /* If we're assigning to a non-register type, push the assignment
4456 down into the branches. This is mandatory for ADDRESSABLE types,
4457 since we cannot generate temporaries for such, but it saves a
4458 copy in other cases as well. */
4459 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4461 /* This code should mirror the code in gimplify_cond_expr. */
4462 enum tree_code code = TREE_CODE (*expr_p);
4463 tree cond = *from_p;
4464 tree result = *to_p;
4466 ret = gimplify_expr (&result, pre_p, post_p,
4467 is_gimple_lvalue, fb_lvalue);
4468 if (ret != GS_ERROR)
4469 ret = GS_OK;
4471 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4472 TREE_OPERAND (cond, 1)
4473 = build2 (code, void_type_node, result,
4474 TREE_OPERAND (cond, 1));
4475 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4476 TREE_OPERAND (cond, 2)
4477 = build2 (code, void_type_node, unshare_expr (result),
4478 TREE_OPERAND (cond, 2));
4480 TREE_TYPE (cond) = void_type_node;
4481 recalculate_side_effects (cond);
4483 if (want_value)
4485 gimplify_and_add (cond, pre_p);
4486 *expr_p = unshare_expr (result);
4488 else
4489 *expr_p = cond;
4490 return ret;
4492 break;
4494 case CALL_EXPR:
4495 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4496 return slot so that we don't generate a temporary. */
4497 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4498 && aggregate_value_p (*from_p, *from_p))
4500 bool use_target;
4502 if (!(rhs_predicate_for (*to_p))(*from_p))
4503 /* If we need a temporary, *to_p isn't accurate. */
4504 use_target = false;
4505 /* It's OK to use the return slot directly unless it's an NRV. */
4506 else if (TREE_CODE (*to_p) == RESULT_DECL
4507 && DECL_NAME (*to_p) == NULL_TREE
4508 && needs_to_live_in_memory (*to_p))
4509 use_target = true;
4510 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4511 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4512 /* Don't force regs into memory. */
4513 use_target = false;
4514 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4515 /* It's OK to use the target directly if it's being
4516 initialized. */
4517 use_target = true;
4518 else if (variably_modified_type_p (TREE_TYPE (*to_p), NULL_TREE))
4519 /* Always use the target and thus RSO for variable-sized types.
4520 GIMPLE cannot deal with a variable-sized assignment
4521 embedded in a call statement. */
4522 use_target = true;
4523 else if (TREE_CODE (*to_p) != SSA_NAME
4524 && (!is_gimple_variable (*to_p)
4525 || needs_to_live_in_memory (*to_p)))
4526 /* Don't use the original target if it's already addressable;
4527 if its address escapes, and the called function uses the
4528 NRV optimization, a conforming program could see *to_p
4529 change before the called function returns; see c++/19317.
4530 When optimizing, the return_slot pass marks more functions
4531 as safe after we have escape info. */
4532 use_target = false;
4533 else
4534 use_target = true;
4536 if (use_target)
4538 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4539 mark_addressable (*to_p);
4542 break;
4544 case WITH_SIZE_EXPR:
4545 /* Likewise for calls that return an aggregate of non-constant size,
4546 since we would not be able to generate a temporary at all. */
4547 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4549 *from_p = TREE_OPERAND (*from_p, 0);
4550 /* We don't change ret in this case because the
4551 WITH_SIZE_EXPR might have been added in
4552 gimplify_modify_expr, so returning GS_OK would lead to an
4553 infinite loop. */
4554 changed = true;
4556 break;
4558 /* If we're initializing from a container, push the initialization
4559 inside it. */
4560 case CLEANUP_POINT_EXPR:
4561 case BIND_EXPR:
4562 case STATEMENT_LIST:
4564 tree wrap = *from_p;
4565 tree t;
4567 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4568 fb_lvalue);
4569 if (ret != GS_ERROR)
4570 ret = GS_OK;
4572 t = voidify_wrapper_expr (wrap, *expr_p);
4573 gcc_assert (t == *expr_p);
4575 if (want_value)
4577 gimplify_and_add (wrap, pre_p);
4578 *expr_p = unshare_expr (*to_p);
4580 else
4581 *expr_p = wrap;
4582 return GS_OK;
4585 case COMPOUND_LITERAL_EXPR:
4587 tree complit = TREE_OPERAND (*expr_p, 1);
4588 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4589 tree decl = DECL_EXPR_DECL (decl_s);
4590 tree init = DECL_INITIAL (decl);
4592 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4593 into struct T x = { 0, 1, 2 } if the address of the
4594 compound literal has never been taken. */
4595 if (!TREE_ADDRESSABLE (complit)
4596 && !TREE_ADDRESSABLE (decl)
4597 && init)
4599 *expr_p = copy_node (*expr_p);
4600 TREE_OPERAND (*expr_p, 1) = init;
4601 return GS_OK;
4605 default:
4606 break;
4609 while (changed);
4611 return ret;
4615 /* Return true if T looks like a valid GIMPLE statement. */
4617 static bool
4618 is_gimple_stmt (tree t)
4620 const enum tree_code code = TREE_CODE (t);
4622 switch (code)
4624 case NOP_EXPR:
4625 /* The only valid NOP_EXPR is the empty statement. */
4626 return IS_EMPTY_STMT (t);
4628 case BIND_EXPR:
4629 case COND_EXPR:
4630 /* These are only valid if they're void. */
4631 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
4633 case SWITCH_EXPR:
4634 case GOTO_EXPR:
4635 case RETURN_EXPR:
4636 case LABEL_EXPR:
4637 case CASE_LABEL_EXPR:
4638 case TRY_CATCH_EXPR:
4639 case TRY_FINALLY_EXPR:
4640 case EH_FILTER_EXPR:
4641 case CATCH_EXPR:
4642 case ASM_EXPR:
4643 case STATEMENT_LIST:
4644 case OMP_PARALLEL:
4645 case OMP_FOR:
4646 case OMP_SIMD:
4647 case OMP_DISTRIBUTE:
4648 case OMP_SECTIONS:
4649 case OMP_SECTION:
4650 case OMP_SINGLE:
4651 case OMP_MASTER:
4652 case OMP_TASKGROUP:
4653 case OMP_ORDERED:
4654 case OMP_CRITICAL:
4655 case OMP_TASK:
4656 /* These are always void. */
4657 return true;
4659 case CALL_EXPR:
4660 case MODIFY_EXPR:
4661 case PREDICT_EXPR:
4662 /* These are valid regardless of their type. */
4663 return true;
4665 default:
4666 return false;
4671 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4672 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4673 DECL_GIMPLE_REG_P set.
4675 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4676 other, unmodified part of the complex object just before the total store.
4677 As a consequence, if the object is still uninitialized, an undefined value
4678 will be loaded into a register, which may result in a spurious exception
4679 if the register is floating-point and the value happens to be a signaling
4680 NaN for example. Then the fully-fledged complex operations lowering pass
4681 followed by a DCE pass are necessary in order to fix things up. */
4683 static enum gimplify_status
4684 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4685 bool want_value)
4687 enum tree_code code, ocode;
4688 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4690 lhs = TREE_OPERAND (*expr_p, 0);
4691 rhs = TREE_OPERAND (*expr_p, 1);
4692 code = TREE_CODE (lhs);
4693 lhs = TREE_OPERAND (lhs, 0);
4695 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4696 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4697 TREE_NO_WARNING (other) = 1;
4698 other = get_formal_tmp_var (other, pre_p);
4700 realpart = code == REALPART_EXPR ? rhs : other;
4701 imagpart = code == REALPART_EXPR ? other : rhs;
4703 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4704 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4705 else
4706 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4708 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4709 *expr_p = (want_value) ? rhs : NULL_TREE;
4711 return GS_ALL_DONE;
4714 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4716 modify_expr
4717 : varname '=' rhs
4718 | '*' ID '=' rhs
4720 PRE_P points to the list where side effects that must happen before
4721 *EXPR_P should be stored.
4723 POST_P points to the list where side effects that must happen after
4724 *EXPR_P should be stored.
4726 WANT_VALUE is nonzero iff we want to use the value of this expression
4727 in another expression. */
4729 static enum gimplify_status
4730 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4731 bool want_value)
4733 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4734 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4735 enum gimplify_status ret = GS_UNHANDLED;
4736 gimple assign;
4737 location_t loc = EXPR_LOCATION (*expr_p);
4738 gimple_stmt_iterator gsi;
4740 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4741 || TREE_CODE (*expr_p) == INIT_EXPR);
4743 if (fn_contains_cilk_spawn_p (cfun)
4744 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p)
4745 && !seen_error ())
4746 return (enum gimplify_status)
4747 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p, post_p);
4749 /* Trying to simplify a clobber using normal logic doesn't work,
4750 so handle it here. */
4751 if (TREE_CLOBBER_P (*from_p))
4753 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4754 if (ret == GS_ERROR)
4755 return ret;
4756 gcc_assert (!want_value
4757 && (TREE_CODE (*to_p) == VAR_DECL
4758 || TREE_CODE (*to_p) == MEM_REF));
4759 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4760 *expr_p = NULL;
4761 return GS_ALL_DONE;
4764 /* Insert pointer conversions required by the middle-end that are not
4765 required by the frontend. This fixes middle-end type checking for
4766 for example gcc.dg/redecl-6.c. */
4767 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4769 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4770 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4771 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4774 /* See if any simplifications can be done based on what the RHS is. */
4775 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4776 want_value);
4777 if (ret != GS_UNHANDLED)
4778 return ret;
4780 /* For zero sized types only gimplify the left hand side and right hand
4781 side as statements and throw away the assignment. Do this after
4782 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4783 types properly. */
4784 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4786 gimplify_stmt (from_p, pre_p);
4787 gimplify_stmt (to_p, pre_p);
4788 *expr_p = NULL_TREE;
4789 return GS_ALL_DONE;
4792 /* If the value being copied is of variable width, compute the length
4793 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4794 before gimplifying any of the operands so that we can resolve any
4795 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4796 the size of the expression to be copied, not of the destination, so
4797 that is what we must do here. */
4798 maybe_with_size_expr (from_p);
4800 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4801 if (ret == GS_ERROR)
4802 return ret;
4804 /* As a special case, we have to temporarily allow for assignments
4805 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4806 a toplevel statement, when gimplifying the GENERIC expression
4807 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4808 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4810 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4811 prevent gimplify_expr from trying to create a new temporary for
4812 foo's LHS, we tell it that it should only gimplify until it
4813 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4814 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4815 and all we need to do here is set 'a' to be its LHS. */
4816 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4817 fb_rvalue);
4818 if (ret == GS_ERROR)
4819 return ret;
4821 /* Now see if the above changed *from_p to something we handle specially. */
4822 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4823 want_value);
4824 if (ret != GS_UNHANDLED)
4825 return ret;
4827 /* If we've got a variable sized assignment between two lvalues (i.e. does
4828 not involve a call), then we can make things a bit more straightforward
4829 by converting the assignment to memcpy or memset. */
4830 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4832 tree from = TREE_OPERAND (*from_p, 0);
4833 tree size = TREE_OPERAND (*from_p, 1);
4835 if (TREE_CODE (from) == CONSTRUCTOR)
4836 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4838 if (is_gimple_addressable (from))
4840 *from_p = from;
4841 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4842 pre_p);
4846 /* Transform partial stores to non-addressable complex variables into
4847 total stores. This allows us to use real instead of virtual operands
4848 for these variables, which improves optimization. */
4849 if ((TREE_CODE (*to_p) == REALPART_EXPR
4850 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4851 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4852 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4854 /* Try to alleviate the effects of the gimplification creating artificial
4855 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4856 if (!gimplify_ctxp->into_ssa
4857 && TREE_CODE (*from_p) == VAR_DECL
4858 && DECL_IGNORED_P (*from_p)
4859 && DECL_P (*to_p)
4860 && !DECL_IGNORED_P (*to_p))
4862 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4863 DECL_NAME (*from_p)
4864 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4865 DECL_HAS_DEBUG_EXPR_P (*from_p) = 1;
4866 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4869 if (want_value && TREE_THIS_VOLATILE (*to_p))
4870 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4872 if (TREE_CODE (*from_p) == CALL_EXPR)
4874 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4875 instead of a GIMPLE_ASSIGN. */
4876 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4877 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4878 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4879 assign = gimple_build_call_from_tree (*from_p);
4880 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4881 notice_special_calls (assign);
4882 if (!gimple_call_noreturn_p (assign))
4883 gimple_call_set_lhs (assign, *to_p);
4885 else
4887 assign = gimple_build_assign (*to_p, *from_p);
4888 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4891 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4893 /* We should have got an SSA name from the start. */
4894 gcc_assert (TREE_CODE (*to_p) == SSA_NAME);
4897 gimplify_seq_add_stmt (pre_p, assign);
4898 gsi = gsi_last (*pre_p);
4899 /* Don't fold stmts inside of target construct. We'll do it
4900 during omplower pass instead. */
4901 struct gimplify_omp_ctx *ctx;
4902 for (ctx = gimplify_omp_ctxp; ctx; ctx = ctx->outer_context)
4903 if (ctx->region_type == ORT_TARGET)
4904 break;
4905 if (ctx == NULL)
4906 fold_stmt (&gsi);
4908 if (want_value)
4910 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4911 return GS_OK;
4913 else
4914 *expr_p = NULL;
4916 return GS_ALL_DONE;
4919 /* Gimplify a comparison between two variable-sized objects. Do this
4920 with a call to BUILT_IN_MEMCMP. */
4922 static enum gimplify_status
4923 gimplify_variable_sized_compare (tree *expr_p)
4925 location_t loc = EXPR_LOCATION (*expr_p);
4926 tree op0 = TREE_OPERAND (*expr_p, 0);
4927 tree op1 = TREE_OPERAND (*expr_p, 1);
4928 tree t, arg, dest, src, expr;
4930 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4931 arg = unshare_expr (arg);
4932 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4933 src = build_fold_addr_expr_loc (loc, op1);
4934 dest = build_fold_addr_expr_loc (loc, op0);
4935 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4936 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4938 expr
4939 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4940 SET_EXPR_LOCATION (expr, loc);
4941 *expr_p = expr;
4943 return GS_OK;
4946 /* Gimplify a comparison between two aggregate objects of integral scalar
4947 mode as a comparison between the bitwise equivalent scalar values. */
4949 static enum gimplify_status
4950 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4952 location_t loc = EXPR_LOCATION (*expr_p);
4953 tree op0 = TREE_OPERAND (*expr_p, 0);
4954 tree op1 = TREE_OPERAND (*expr_p, 1);
4956 tree type = TREE_TYPE (op0);
4957 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4959 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4960 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4962 *expr_p
4963 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4965 return GS_OK;
4968 /* Gimplify an expression sequence. This function gimplifies each
4969 expression and rewrites the original expression with the last
4970 expression of the sequence in GIMPLE form.
4972 PRE_P points to the list where the side effects for all the
4973 expressions in the sequence will be emitted.
4975 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4977 static enum gimplify_status
4978 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4980 tree t = *expr_p;
4984 tree *sub_p = &TREE_OPERAND (t, 0);
4986 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4987 gimplify_compound_expr (sub_p, pre_p, false);
4988 else
4989 gimplify_stmt (sub_p, pre_p);
4991 t = TREE_OPERAND (t, 1);
4993 while (TREE_CODE (t) == COMPOUND_EXPR);
4995 *expr_p = t;
4996 if (want_value)
4997 return GS_OK;
4998 else
5000 gimplify_stmt (expr_p, pre_p);
5001 return GS_ALL_DONE;
5005 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
5006 gimplify. After gimplification, EXPR_P will point to a new temporary
5007 that holds the original value of the SAVE_EXPR node.
5009 PRE_P points to the list where side effects that must happen before
5010 *EXPR_P should be stored. */
5012 static enum gimplify_status
5013 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5015 enum gimplify_status ret = GS_ALL_DONE;
5016 tree val;
5018 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
5019 val = TREE_OPERAND (*expr_p, 0);
5021 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
5022 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
5024 /* The operand may be a void-valued expression such as SAVE_EXPRs
5025 generated by the Java frontend for class initialization. It is
5026 being executed only for its side-effects. */
5027 if (TREE_TYPE (val) == void_type_node)
5029 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
5030 is_gimple_stmt, fb_none);
5031 val = NULL;
5033 else
5034 val = get_initialized_tmp_var (val, pre_p, post_p);
5036 TREE_OPERAND (*expr_p, 0) = val;
5037 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
5040 *expr_p = val;
5042 return ret;
5045 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5047 unary_expr
5048 : ...
5049 | '&' varname
5052 PRE_P points to the list where side effects that must happen before
5053 *EXPR_P should be stored.
5055 POST_P points to the list where side effects that must happen after
5056 *EXPR_P should be stored. */
5058 static enum gimplify_status
5059 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5061 tree expr = *expr_p;
5062 tree op0 = TREE_OPERAND (expr, 0);
5063 enum gimplify_status ret;
5064 location_t loc = EXPR_LOCATION (*expr_p);
5066 switch (TREE_CODE (op0))
5068 case INDIRECT_REF:
5069 do_indirect_ref:
5070 /* Check if we are dealing with an expression of the form '&*ptr'.
5071 While the front end folds away '&*ptr' into 'ptr', these
5072 expressions may be generated internally by the compiler (e.g.,
5073 builtins like __builtin_va_end). */
5074 /* Caution: the silent array decomposition semantics we allow for
5075 ADDR_EXPR means we can't always discard the pair. */
5076 /* Gimplification of the ADDR_EXPR operand may drop
5077 cv-qualification conversions, so make sure we add them if
5078 needed. */
5080 tree op00 = TREE_OPERAND (op0, 0);
5081 tree t_expr = TREE_TYPE (expr);
5082 tree t_op00 = TREE_TYPE (op00);
5084 if (!useless_type_conversion_p (t_expr, t_op00))
5085 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
5086 *expr_p = op00;
5087 ret = GS_OK;
5089 break;
5091 case VIEW_CONVERT_EXPR:
5092 /* Take the address of our operand and then convert it to the type of
5093 this ADDR_EXPR.
5095 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5096 all clear. The impact of this transformation is even less clear. */
5098 /* If the operand is a useless conversion, look through it. Doing so
5099 guarantees that the ADDR_EXPR and its operand will remain of the
5100 same type. */
5101 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
5102 op0 = TREE_OPERAND (op0, 0);
5104 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
5105 build_fold_addr_expr_loc (loc,
5106 TREE_OPERAND (op0, 0)));
5107 ret = GS_OK;
5108 break;
5110 default:
5111 /* We use fb_either here because the C frontend sometimes takes
5112 the address of a call that returns a struct; see
5113 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5114 the implied temporary explicit. */
5116 /* Make the operand addressable. */
5117 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
5118 is_gimple_addressable, fb_either);
5119 if (ret == GS_ERROR)
5120 break;
5122 /* Then mark it. Beware that it may not be possible to do so directly
5123 if a temporary has been created by the gimplification. */
5124 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
5126 op0 = TREE_OPERAND (expr, 0);
5128 /* For various reasons, the gimplification of the expression
5129 may have made a new INDIRECT_REF. */
5130 if (TREE_CODE (op0) == INDIRECT_REF)
5131 goto do_indirect_ref;
5133 mark_addressable (TREE_OPERAND (expr, 0));
5135 /* The FEs may end up building ADDR_EXPRs early on a decl with
5136 an incomplete type. Re-build ADDR_EXPRs in canonical form
5137 here. */
5138 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
5139 *expr_p = build_fold_addr_expr (op0);
5141 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5142 recompute_tree_invariant_for_addr_expr (*expr_p);
5144 /* If we re-built the ADDR_EXPR add a conversion to the original type
5145 if required. */
5146 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
5147 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
5149 break;
5152 return ret;
5155 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5156 value; output operands should be a gimple lvalue. */
5158 static enum gimplify_status
5159 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5161 tree expr;
5162 int noutputs;
5163 const char **oconstraints;
5164 int i;
5165 tree link;
5166 const char *constraint;
5167 bool allows_mem, allows_reg, is_inout;
5168 enum gimplify_status ret, tret;
5169 gimple stmt;
5170 vec<tree, va_gc> *inputs;
5171 vec<tree, va_gc> *outputs;
5172 vec<tree, va_gc> *clobbers;
5173 vec<tree, va_gc> *labels;
5174 tree link_next;
5176 expr = *expr_p;
5177 noutputs = list_length (ASM_OUTPUTS (expr));
5178 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5180 inputs = NULL;
5181 outputs = NULL;
5182 clobbers = NULL;
5183 labels = NULL;
5185 ret = GS_ALL_DONE;
5186 link_next = NULL_TREE;
5187 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5189 bool ok;
5190 size_t constraint_len;
5192 link_next = TREE_CHAIN (link);
5194 oconstraints[i]
5195 = constraint
5196 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5197 constraint_len = strlen (constraint);
5198 if (constraint_len == 0)
5199 continue;
5201 ok = parse_output_constraint (&constraint, i, 0, 0,
5202 &allows_mem, &allows_reg, &is_inout);
5203 if (!ok)
5205 ret = GS_ERROR;
5206 is_inout = false;
5209 if (!allows_reg && allows_mem)
5210 mark_addressable (TREE_VALUE (link));
5212 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5213 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5214 fb_lvalue | fb_mayfail);
5215 if (tret == GS_ERROR)
5217 error ("invalid lvalue in asm output %d", i);
5218 ret = tret;
5221 vec_safe_push (outputs, link);
5222 TREE_CHAIN (link) = NULL_TREE;
5224 if (is_inout)
5226 /* An input/output operand. To give the optimizers more
5227 flexibility, split it into separate input and output
5228 operands. */
5229 tree input;
5230 char buf[10];
5232 /* Turn the in/out constraint into an output constraint. */
5233 char *p = xstrdup (constraint);
5234 p[0] = '=';
5235 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5237 /* And add a matching input constraint. */
5238 if (allows_reg)
5240 sprintf (buf, "%d", i);
5242 /* If there are multiple alternatives in the constraint,
5243 handle each of them individually. Those that allow register
5244 will be replaced with operand number, the others will stay
5245 unchanged. */
5246 if (strchr (p, ',') != NULL)
5248 size_t len = 0, buflen = strlen (buf);
5249 char *beg, *end, *str, *dst;
5251 for (beg = p + 1;;)
5253 end = strchr (beg, ',');
5254 if (end == NULL)
5255 end = strchr (beg, '\0');
5256 if ((size_t) (end - beg) < buflen)
5257 len += buflen + 1;
5258 else
5259 len += end - beg + 1;
5260 if (*end)
5261 beg = end + 1;
5262 else
5263 break;
5266 str = (char *) alloca (len);
5267 for (beg = p + 1, dst = str;;)
5269 const char *tem;
5270 bool mem_p, reg_p, inout_p;
5272 end = strchr (beg, ',');
5273 if (end)
5274 *end = '\0';
5275 beg[-1] = '=';
5276 tem = beg - 1;
5277 parse_output_constraint (&tem, i, 0, 0,
5278 &mem_p, &reg_p, &inout_p);
5279 if (dst != str)
5280 *dst++ = ',';
5281 if (reg_p)
5283 memcpy (dst, buf, buflen);
5284 dst += buflen;
5286 else
5288 if (end)
5289 len = end - beg;
5290 else
5291 len = strlen (beg);
5292 memcpy (dst, beg, len);
5293 dst += len;
5295 if (end)
5296 beg = end + 1;
5297 else
5298 break;
5300 *dst = '\0';
5301 input = build_string (dst - str, str);
5303 else
5304 input = build_string (strlen (buf), buf);
5306 else
5307 input = build_string (constraint_len - 1, constraint + 1);
5309 free (p);
5311 input = build_tree_list (build_tree_list (NULL_TREE, input),
5312 unshare_expr (TREE_VALUE (link)));
5313 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5317 link_next = NULL_TREE;
5318 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5320 link_next = TREE_CHAIN (link);
5321 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5322 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5323 oconstraints, &allows_mem, &allows_reg);
5325 /* If we can't make copies, we can only accept memory. */
5326 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5328 if (allows_mem)
5329 allows_reg = 0;
5330 else
5332 error ("impossible constraint in %<asm%>");
5333 error ("non-memory input %d must stay in memory", i);
5334 return GS_ERROR;
5338 /* If the operand is a memory input, it should be an lvalue. */
5339 if (!allows_reg && allows_mem)
5341 tree inputv = TREE_VALUE (link);
5342 STRIP_NOPS (inputv);
5343 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5344 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5345 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5346 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5347 TREE_VALUE (link) = error_mark_node;
5348 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5349 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5350 mark_addressable (TREE_VALUE (link));
5351 if (tret == GS_ERROR)
5353 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5354 input_location = EXPR_LOCATION (TREE_VALUE (link));
5355 error ("memory input %d is not directly addressable", i);
5356 ret = tret;
5359 else
5361 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5362 is_gimple_asm_val, fb_rvalue);
5363 if (tret == GS_ERROR)
5364 ret = tret;
5367 TREE_CHAIN (link) = NULL_TREE;
5368 vec_safe_push (inputs, link);
5371 link_next = NULL_TREE;
5372 for (link = ASM_CLOBBERS (expr); link; ++i, link = link_next)
5374 link_next = TREE_CHAIN (link);
5375 TREE_CHAIN (link) = NULL_TREE;
5376 vec_safe_push (clobbers, link);
5379 link_next = NULL_TREE;
5380 for (link = ASM_LABELS (expr); link; ++i, link = link_next)
5382 link_next = TREE_CHAIN (link);
5383 TREE_CHAIN (link) = NULL_TREE;
5384 vec_safe_push (labels, link);
5387 /* Do not add ASMs with errors to the gimple IL stream. */
5388 if (ret != GS_ERROR)
5390 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5391 inputs, outputs, clobbers, labels);
5393 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5394 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5396 gimplify_seq_add_stmt (pre_p, stmt);
5399 return ret;
5402 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5403 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5404 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5405 return to this function.
5407 FIXME should we complexify the prequeue handling instead? Or use flags
5408 for all the cleanups and let the optimizer tighten them up? The current
5409 code seems pretty fragile; it will break on a cleanup within any
5410 non-conditional nesting. But any such nesting would be broken, anyway;
5411 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5412 and continues out of it. We can do that at the RTL level, though, so
5413 having an optimizer to tighten up try/finally regions would be a Good
5414 Thing. */
5416 static enum gimplify_status
5417 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5419 gimple_stmt_iterator iter;
5420 gimple_seq body_sequence = NULL;
5422 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5424 /* We only care about the number of conditions between the innermost
5425 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5426 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5427 int old_conds = gimplify_ctxp->conditions;
5428 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5429 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5430 gimplify_ctxp->conditions = 0;
5431 gimplify_ctxp->conditional_cleanups = NULL;
5432 gimplify_ctxp->in_cleanup_point_expr = true;
5434 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5436 gimplify_ctxp->conditions = old_conds;
5437 gimplify_ctxp->conditional_cleanups = old_cleanups;
5438 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5440 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5442 gimple wce = gsi_stmt (iter);
5444 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5446 if (gsi_one_before_end_p (iter))
5448 /* Note that gsi_insert_seq_before and gsi_remove do not
5449 scan operands, unlike some other sequence mutators. */
5450 if (!gimple_wce_cleanup_eh_only (wce))
5451 gsi_insert_seq_before_without_update (&iter,
5452 gimple_wce_cleanup (wce),
5453 GSI_SAME_STMT);
5454 gsi_remove (&iter, true);
5455 break;
5457 else
5459 gimple gtry;
5460 gimple_seq seq;
5461 enum gimple_try_flags kind;
5463 if (gimple_wce_cleanup_eh_only (wce))
5464 kind = GIMPLE_TRY_CATCH;
5465 else
5466 kind = GIMPLE_TRY_FINALLY;
5467 seq = gsi_split_seq_after (iter);
5469 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5470 /* Do not use gsi_replace here, as it may scan operands.
5471 We want to do a simple structural modification only. */
5472 gsi_set_stmt (&iter, gtry);
5473 iter = gsi_start (gtry->gimple_try.eval);
5476 else
5477 gsi_next (&iter);
5480 gimplify_seq_add_seq (pre_p, body_sequence);
5481 if (temp)
5483 *expr_p = temp;
5484 return GS_OK;
5486 else
5488 *expr_p = NULL;
5489 return GS_ALL_DONE;
5493 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5494 is the cleanup action required. EH_ONLY is true if the cleanup should
5495 only be executed if an exception is thrown, not on normal exit. */
5497 static void
5498 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5500 gimple wce;
5501 gimple_seq cleanup_stmts = NULL;
5503 /* Errors can result in improperly nested cleanups. Which results in
5504 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5505 if (seen_error ())
5506 return;
5508 if (gimple_conditional_context ())
5510 /* If we're in a conditional context, this is more complex. We only
5511 want to run the cleanup if we actually ran the initialization that
5512 necessitates it, but we want to run it after the end of the
5513 conditional context. So we wrap the try/finally around the
5514 condition and use a flag to determine whether or not to actually
5515 run the destructor. Thus
5517 test ? f(A()) : 0
5519 becomes (approximately)
5521 flag = 0;
5522 try {
5523 if (test) { A::A(temp); flag = 1; val = f(temp); }
5524 else { val = 0; }
5525 } finally {
5526 if (flag) A::~A(temp);
5530 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5531 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5532 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5534 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5535 gimplify_stmt (&cleanup, &cleanup_stmts);
5536 wce = gimple_build_wce (cleanup_stmts);
5538 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5539 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5540 gimplify_seq_add_stmt (pre_p, ftrue);
5542 /* Because of this manipulation, and the EH edges that jump
5543 threading cannot redirect, the temporary (VAR) will appear
5544 to be used uninitialized. Don't warn. */
5545 TREE_NO_WARNING (var) = 1;
5547 else
5549 gimplify_stmt (&cleanup, &cleanup_stmts);
5550 wce = gimple_build_wce (cleanup_stmts);
5551 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5552 gimplify_seq_add_stmt (pre_p, wce);
5556 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5558 static enum gimplify_status
5559 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5561 tree targ = *expr_p;
5562 tree temp = TARGET_EXPR_SLOT (targ);
5563 tree init = TARGET_EXPR_INITIAL (targ);
5564 enum gimplify_status ret;
5566 if (init)
5568 tree cleanup = NULL_TREE;
5570 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5571 to the temps list. Handle also variable length TARGET_EXPRs. */
5572 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5574 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5575 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5576 gimplify_vla_decl (temp, pre_p);
5578 else
5579 gimple_add_tmp_var (temp);
5581 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5582 expression is supposed to initialize the slot. */
5583 if (VOID_TYPE_P (TREE_TYPE (init)))
5584 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5585 else
5587 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5588 init = init_expr;
5589 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5590 init = NULL;
5591 ggc_free (init_expr);
5593 if (ret == GS_ERROR)
5595 /* PR c++/28266 Make sure this is expanded only once. */
5596 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5597 return GS_ERROR;
5599 if (init)
5600 gimplify_and_add (init, pre_p);
5602 /* If needed, push the cleanup for the temp. */
5603 if (TARGET_EXPR_CLEANUP (targ))
5605 if (CLEANUP_EH_ONLY (targ))
5606 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5607 CLEANUP_EH_ONLY (targ), pre_p);
5608 else
5609 cleanup = TARGET_EXPR_CLEANUP (targ);
5612 /* Add a clobber for the temporary going out of scope, like
5613 gimplify_bind_expr. */
5614 if (gimplify_ctxp->in_cleanup_point_expr
5615 && needs_to_live_in_memory (temp)
5616 && flag_stack_reuse == SR_ALL)
5618 tree clobber = build_constructor (TREE_TYPE (temp),
5619 NULL);
5620 TREE_THIS_VOLATILE (clobber) = true;
5621 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5622 if (cleanup)
5623 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5624 clobber);
5625 else
5626 cleanup = clobber;
5629 if (cleanup)
5630 gimple_push_cleanup (temp, cleanup, false, pre_p);
5632 /* Only expand this once. */
5633 TREE_OPERAND (targ, 3) = init;
5634 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5636 else
5637 /* We should have expanded this before. */
5638 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5640 *expr_p = temp;
5641 return GS_OK;
5644 /* Gimplification of expression trees. */
5646 /* Gimplify an expression which appears at statement context. The
5647 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5648 NULL, a new sequence is allocated.
5650 Return true if we actually added a statement to the queue. */
5652 bool
5653 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5655 gimple_seq_node last;
5657 last = gimple_seq_last (*seq_p);
5658 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5659 return last != gimple_seq_last (*seq_p);
5662 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5663 to CTX. If entries already exist, force them to be some flavor of private.
5664 If there is no enclosing parallel, do nothing. */
5666 void
5667 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5669 splay_tree_node n;
5671 if (decl == NULL || !DECL_P (decl))
5672 return;
5676 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5677 if (n != NULL)
5679 if (n->value & GOVD_SHARED)
5680 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5681 else if (n->value & GOVD_MAP)
5682 n->value |= GOVD_MAP_TO_ONLY;
5683 else
5684 return;
5686 else if (ctx->region_type == ORT_TARGET)
5687 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_MAP_TO_ONLY);
5688 else if (ctx->region_type != ORT_WORKSHARE
5689 && ctx->region_type != ORT_SIMD
5690 && ctx->region_type != ORT_TARGET_DATA)
5691 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5693 ctx = ctx->outer_context;
5695 while (ctx);
5698 /* Similarly for each of the type sizes of TYPE. */
5700 static void
5701 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5703 if (type == NULL || type == error_mark_node)
5704 return;
5705 type = TYPE_MAIN_VARIANT (type);
5707 if (pointer_set_insert (ctx->privatized_types, type))
5708 return;
5710 switch (TREE_CODE (type))
5712 case INTEGER_TYPE:
5713 case ENUMERAL_TYPE:
5714 case BOOLEAN_TYPE:
5715 case REAL_TYPE:
5716 case FIXED_POINT_TYPE:
5717 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5718 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5719 break;
5721 case ARRAY_TYPE:
5722 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5723 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5724 break;
5726 case RECORD_TYPE:
5727 case UNION_TYPE:
5728 case QUAL_UNION_TYPE:
5730 tree field;
5731 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5732 if (TREE_CODE (field) == FIELD_DECL)
5734 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5735 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5738 break;
5740 case POINTER_TYPE:
5741 case REFERENCE_TYPE:
5742 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5743 break;
5745 default:
5746 break;
5749 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5750 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5751 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5754 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5756 static void
5757 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5759 splay_tree_node n;
5760 unsigned int nflags;
5761 tree t;
5763 if (error_operand_p (decl))
5764 return;
5766 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5767 there are constructors involved somewhere. */
5768 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5769 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5770 flags |= GOVD_SEEN;
5772 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5773 if (n != NULL && n->value != GOVD_ALIGNED)
5775 /* We shouldn't be re-adding the decl with the same data
5776 sharing class. */
5777 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5778 /* The only combination of data sharing classes we should see is
5779 FIRSTPRIVATE and LASTPRIVATE. */
5780 nflags = n->value | flags;
5781 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5782 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE)
5783 || (flags & GOVD_DATA_SHARE_CLASS) == 0);
5784 n->value = nflags;
5785 return;
5788 /* When adding a variable-sized variable, we have to handle all sorts
5789 of additional bits of data: the pointer replacement variable, and
5790 the parameters of the type. */
5791 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5793 /* Add the pointer replacement variable as PRIVATE if the variable
5794 replacement is private, else FIRSTPRIVATE since we'll need the
5795 address of the original variable either for SHARED, or for the
5796 copy into or out of the context. */
5797 if (!(flags & GOVD_LOCAL))
5799 nflags = flags & GOVD_MAP
5800 ? GOVD_MAP | GOVD_MAP_TO_ONLY | GOVD_EXPLICIT
5801 : flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5802 nflags |= flags & GOVD_SEEN;
5803 t = DECL_VALUE_EXPR (decl);
5804 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5805 t = TREE_OPERAND (t, 0);
5806 gcc_assert (DECL_P (t));
5807 omp_add_variable (ctx, t, nflags);
5810 /* Add all of the variable and type parameters (which should have
5811 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5812 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5813 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5814 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5816 /* The variable-sized variable itself is never SHARED, only some form
5817 of PRIVATE. The sharing would take place via the pointer variable
5818 which we remapped above. */
5819 if (flags & GOVD_SHARED)
5820 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5821 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5823 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5824 alloca statement we generate for the variable, so make sure it
5825 is available. This isn't automatically needed for the SHARED
5826 case, since we won't be allocating local storage then.
5827 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5828 in this case omp_notice_variable will be called later
5829 on when it is gimplified. */
5830 else if (! (flags & (GOVD_LOCAL | GOVD_MAP))
5831 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5832 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5834 else if ((flags & (GOVD_MAP | GOVD_LOCAL)) == 0
5835 && lang_hooks.decls.omp_privatize_by_reference (decl))
5837 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5839 /* Similar to the direct variable sized case above, we'll need the
5840 size of references being privatized. */
5841 if ((flags & GOVD_SHARED) == 0)
5843 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5844 if (TREE_CODE (t) != INTEGER_CST)
5845 omp_notice_variable (ctx, t, true);
5849 if (n != NULL)
5850 n->value |= flags;
5851 else
5852 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5855 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5856 This just prints out diagnostics about threadprivate variable uses
5857 in untied tasks. If DECL2 is non-NULL, prevent this warning
5858 on that variable. */
5860 static bool
5861 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5862 tree decl2)
5864 splay_tree_node n;
5865 struct gimplify_omp_ctx *octx;
5867 for (octx = ctx; octx; octx = octx->outer_context)
5868 if (octx->region_type == ORT_TARGET)
5870 n = splay_tree_lookup (octx->variables, (splay_tree_key)decl);
5871 if (n == NULL)
5873 error ("threadprivate variable %qE used in target region",
5874 DECL_NAME (decl));
5875 error_at (octx->location, "enclosing target region");
5876 splay_tree_insert (octx->variables, (splay_tree_key)decl, 0);
5878 if (decl2)
5879 splay_tree_insert (octx->variables, (splay_tree_key)decl2, 0);
5882 if (ctx->region_type != ORT_UNTIED_TASK)
5883 return false;
5884 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5885 if (n == NULL)
5887 error ("threadprivate variable %qE used in untied task",
5888 DECL_NAME (decl));
5889 error_at (ctx->location, "enclosing task");
5890 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5892 if (decl2)
5893 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5894 return false;
5897 /* Record the fact that DECL was used within the OpenMP context CTX.
5898 IN_CODE is true when real code uses DECL, and false when we should
5899 merely emit default(none) errors. Return true if DECL is going to
5900 be remapped and thus DECL shouldn't be gimplified into its
5901 DECL_VALUE_EXPR (if any). */
5903 static bool
5904 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5906 splay_tree_node n;
5907 unsigned flags = in_code ? GOVD_SEEN : 0;
5908 bool ret = false, shared;
5910 if (error_operand_p (decl))
5911 return false;
5913 /* Threadprivate variables are predetermined. */
5914 if (is_global_var (decl))
5916 if (DECL_THREAD_LOCAL_P (decl))
5917 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5919 if (DECL_HAS_VALUE_EXPR_P (decl))
5921 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5923 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5924 return omp_notice_threadprivate_variable (ctx, decl, value);
5928 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5929 if (ctx->region_type == ORT_TARGET)
5931 if (n == NULL)
5933 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5935 error ("%qD referenced in target region does not have "
5936 "a mappable type", decl);
5937 omp_add_variable (ctx, decl, GOVD_MAP | GOVD_EXPLICIT | flags);
5939 else
5940 omp_add_variable (ctx, decl, GOVD_MAP | flags);
5942 else
5943 n->value |= flags;
5944 ret = lang_hooks.decls.omp_disregard_value_expr (decl, true);
5945 goto do_outer;
5948 if (n == NULL)
5950 enum omp_clause_default_kind default_kind, kind;
5951 struct gimplify_omp_ctx *octx;
5953 if (ctx->region_type == ORT_WORKSHARE
5954 || ctx->region_type == ORT_SIMD
5955 || ctx->region_type == ORT_TARGET_DATA)
5956 goto do_outer;
5958 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5959 remapped firstprivate instead of shared. To some extent this is
5960 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5961 default_kind = ctx->default_kind;
5962 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5963 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5964 default_kind = kind;
5966 switch (default_kind)
5968 case OMP_CLAUSE_DEFAULT_NONE:
5969 if ((ctx->region_type & ORT_TASK) != 0)
5971 error ("%qE not specified in enclosing task",
5972 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5973 error_at (ctx->location, "enclosing task");
5975 else if (ctx->region_type == ORT_TEAMS)
5977 error ("%qE not specified in enclosing teams construct",
5978 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5979 error_at (ctx->location, "enclosing teams construct");
5981 else
5983 error ("%qE not specified in enclosing parallel",
5984 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5985 error_at (ctx->location, "enclosing parallel");
5987 /* FALLTHRU */
5988 case OMP_CLAUSE_DEFAULT_SHARED:
5989 flags |= GOVD_SHARED;
5990 break;
5991 case OMP_CLAUSE_DEFAULT_PRIVATE:
5992 flags |= GOVD_PRIVATE;
5993 break;
5994 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5995 flags |= GOVD_FIRSTPRIVATE;
5996 break;
5997 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5998 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5999 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
6000 if (ctx->outer_context)
6001 omp_notice_variable (ctx->outer_context, decl, in_code);
6002 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
6004 splay_tree_node n2;
6006 if ((octx->region_type & (ORT_TARGET_DATA | ORT_TARGET)) != 0)
6007 continue;
6008 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
6009 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
6011 flags |= GOVD_FIRSTPRIVATE;
6012 break;
6014 if ((octx->region_type & (ORT_PARALLEL | ORT_TEAMS)) != 0)
6015 break;
6017 if (flags & GOVD_FIRSTPRIVATE)
6018 break;
6019 if (octx == NULL
6020 && (TREE_CODE (decl) == PARM_DECL
6021 || (!is_global_var (decl)
6022 && DECL_CONTEXT (decl) == current_function_decl)))
6024 flags |= GOVD_FIRSTPRIVATE;
6025 break;
6027 flags |= GOVD_SHARED;
6028 break;
6029 default:
6030 gcc_unreachable ();
6033 if ((flags & GOVD_PRIVATE)
6034 && lang_hooks.decls.omp_private_outer_ref (decl))
6035 flags |= GOVD_PRIVATE_OUTER_REF;
6037 omp_add_variable (ctx, decl, flags);
6039 shared = (flags & GOVD_SHARED) != 0;
6040 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6041 goto do_outer;
6044 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
6045 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
6046 && DECL_SIZE (decl)
6047 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6049 splay_tree_node n2;
6050 tree t = DECL_VALUE_EXPR (decl);
6051 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
6052 t = TREE_OPERAND (t, 0);
6053 gcc_assert (DECL_P (t));
6054 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
6055 n2->value |= GOVD_SEEN;
6058 shared = ((flags | n->value) & GOVD_SHARED) != 0;
6059 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
6061 /* If nothing changed, there's nothing left to do. */
6062 if ((n->value & flags) == flags)
6063 return ret;
6064 flags |= n->value;
6065 n->value = flags;
6067 do_outer:
6068 /* If the variable is private in the current context, then we don't
6069 need to propagate anything to an outer context. */
6070 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
6071 return ret;
6072 if (ctx->outer_context
6073 && omp_notice_variable (ctx->outer_context, decl, in_code))
6074 return true;
6075 return ret;
6078 /* Verify that DECL is private within CTX. If there's specific information
6079 to the contrary in the innermost scope, generate an error. */
6081 static bool
6082 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl, bool simd)
6084 splay_tree_node n;
6086 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
6087 if (n != NULL)
6089 if (n->value & GOVD_SHARED)
6091 if (ctx == gimplify_omp_ctxp)
6093 if (simd)
6094 error ("iteration variable %qE is predetermined linear",
6095 DECL_NAME (decl));
6096 else
6097 error ("iteration variable %qE should be private",
6098 DECL_NAME (decl));
6099 n->value = GOVD_PRIVATE;
6100 return true;
6102 else
6103 return false;
6105 else if ((n->value & GOVD_EXPLICIT) != 0
6106 && (ctx == gimplify_omp_ctxp
6107 || (ctx->region_type == ORT_COMBINED_PARALLEL
6108 && gimplify_omp_ctxp->outer_context == ctx)))
6110 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
6111 error ("iteration variable %qE should not be firstprivate",
6112 DECL_NAME (decl));
6113 else if ((n->value & GOVD_REDUCTION) != 0)
6114 error ("iteration variable %qE should not be reduction",
6115 DECL_NAME (decl));
6116 else if (simd && (n->value & GOVD_LASTPRIVATE) != 0)
6117 error ("iteration variable %qE should not be lastprivate",
6118 DECL_NAME (decl));
6119 else if (simd && (n->value & GOVD_PRIVATE) != 0)
6120 error ("iteration variable %qE should not be private",
6121 DECL_NAME (decl));
6122 else if (simd && (n->value & GOVD_LINEAR) != 0)
6123 error ("iteration variable %qE is predetermined linear",
6124 DECL_NAME (decl));
6126 return (ctx == gimplify_omp_ctxp
6127 || (ctx->region_type == ORT_COMBINED_PARALLEL
6128 && gimplify_omp_ctxp->outer_context == ctx));
6131 if (ctx->region_type != ORT_WORKSHARE
6132 && ctx->region_type != ORT_SIMD)
6133 return false;
6134 else if (ctx->outer_context)
6135 return omp_is_private (ctx->outer_context, decl, simd);
6136 return false;
6139 /* Return true if DECL is private within a parallel region
6140 that binds to the current construct's context or in parallel
6141 region's REDUCTION clause. */
6143 static bool
6144 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
6146 splay_tree_node n;
6150 ctx = ctx->outer_context;
6151 if (ctx == NULL)
6152 return !(is_global_var (decl)
6153 /* References might be private, but might be shared too. */
6154 || lang_hooks.decls.omp_privatize_by_reference (decl));
6156 if ((ctx->region_type & (ORT_TARGET | ORT_TARGET_DATA)) != 0)
6157 continue;
6159 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6160 if (n != NULL)
6161 return (n->value & GOVD_SHARED) == 0;
6163 while (ctx->region_type == ORT_WORKSHARE
6164 || ctx->region_type == ORT_SIMD);
6165 return false;
6168 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
6169 and previous omp contexts. */
6171 static void
6172 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
6173 enum omp_region_type region_type)
6175 struct gimplify_omp_ctx *ctx, *outer_ctx;
6176 struct gimplify_ctx gctx;
6177 tree c;
6179 ctx = new_omp_context (region_type);
6180 outer_ctx = ctx->outer_context;
6182 while ((c = *list_p) != NULL)
6184 bool remove = false;
6185 bool notice_outer = true;
6186 const char *check_non_private = NULL;
6187 unsigned int flags;
6188 tree decl;
6190 switch (OMP_CLAUSE_CODE (c))
6192 case OMP_CLAUSE_PRIVATE:
6193 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
6194 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
6196 flags |= GOVD_PRIVATE_OUTER_REF;
6197 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
6199 else
6200 notice_outer = false;
6201 goto do_add;
6202 case OMP_CLAUSE_SHARED:
6203 flags = GOVD_SHARED | GOVD_EXPLICIT;
6204 goto do_add;
6205 case OMP_CLAUSE_FIRSTPRIVATE:
6206 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
6207 check_non_private = "firstprivate";
6208 goto do_add;
6209 case OMP_CLAUSE_LASTPRIVATE:
6210 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
6211 check_non_private = "lastprivate";
6212 goto do_add;
6213 case OMP_CLAUSE_REDUCTION:
6214 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
6215 check_non_private = "reduction";
6216 goto do_add;
6217 case OMP_CLAUSE_LINEAR:
6218 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c), pre_p, NULL,
6219 is_gimple_val, fb_rvalue) == GS_ERROR)
6221 remove = true;
6222 break;
6224 flags = GOVD_LINEAR | GOVD_EXPLICIT;
6225 goto do_add;
6227 case OMP_CLAUSE_MAP:
6228 if (OMP_CLAUSE_SIZE (c)
6229 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6230 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6232 remove = true;
6233 break;
6235 decl = OMP_CLAUSE_DECL (c);
6236 if (!DECL_P (decl))
6238 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6239 NULL, is_gimple_lvalue, fb_lvalue)
6240 == GS_ERROR)
6242 remove = true;
6243 break;
6245 break;
6247 flags = GOVD_MAP | GOVD_EXPLICIT;
6248 goto do_add;
6250 case OMP_CLAUSE_DEPEND:
6251 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPOUND_EXPR)
6253 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c), 0), pre_p,
6254 NULL, is_gimple_val, fb_rvalue);
6255 OMP_CLAUSE_DECL (c) = TREE_OPERAND (OMP_CLAUSE_DECL (c), 1);
6257 if (error_operand_p (OMP_CLAUSE_DECL (c)))
6259 remove = true;
6260 break;
6262 OMP_CLAUSE_DECL (c) = build_fold_addr_expr (OMP_CLAUSE_DECL (c));
6263 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p, NULL,
6264 is_gimple_val, fb_rvalue) == GS_ERROR)
6266 remove = true;
6267 break;
6269 break;
6271 case OMP_CLAUSE_TO:
6272 case OMP_CLAUSE_FROM:
6273 if (OMP_CLAUSE_SIZE (c)
6274 && gimplify_expr (&OMP_CLAUSE_SIZE (c), pre_p,
6275 NULL, is_gimple_val, fb_rvalue) == GS_ERROR)
6277 remove = true;
6278 break;
6280 decl = OMP_CLAUSE_DECL (c);
6281 if (error_operand_p (decl))
6283 remove = true;
6284 break;
6286 if (!DECL_P (decl))
6288 if (gimplify_expr (&OMP_CLAUSE_DECL (c), pre_p,
6289 NULL, is_gimple_lvalue, fb_lvalue)
6290 == GS_ERROR)
6292 remove = true;
6293 break;
6295 break;
6297 goto do_notice;
6299 do_add:
6300 decl = OMP_CLAUSE_DECL (c);
6301 if (error_operand_p (decl))
6303 remove = true;
6304 break;
6306 omp_add_variable (ctx, decl, flags);
6307 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6308 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6310 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
6311 GOVD_LOCAL | GOVD_SEEN);
6312 gimplify_omp_ctxp = ctx;
6313 push_gimplify_context (&gctx);
6315 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = NULL;
6316 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = NULL;
6318 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
6319 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
6320 pop_gimplify_context
6321 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
6322 push_gimplify_context (&gctx);
6323 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
6324 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
6325 pop_gimplify_context
6326 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
6327 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
6328 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
6330 gimplify_omp_ctxp = outer_ctx;
6332 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6333 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
6335 gimplify_omp_ctxp = ctx;
6336 push_gimplify_context (&gctx);
6337 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
6339 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
6340 NULL, NULL);
6341 TREE_SIDE_EFFECTS (bind) = 1;
6342 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
6343 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
6345 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
6346 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6347 pop_gimplify_context
6348 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
6349 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
6351 gimplify_omp_ctxp = outer_ctx;
6353 if (notice_outer)
6354 goto do_notice;
6355 break;
6357 case OMP_CLAUSE_COPYIN:
6358 case OMP_CLAUSE_COPYPRIVATE:
6359 decl = OMP_CLAUSE_DECL (c);
6360 if (error_operand_p (decl))
6362 remove = true;
6363 break;
6365 do_notice:
6366 if (outer_ctx)
6367 omp_notice_variable (outer_ctx, decl, true);
6368 if (check_non_private
6369 && region_type == ORT_WORKSHARE
6370 && omp_check_private (ctx, decl))
6372 error ("%s variable %qE is private in outer context",
6373 check_non_private, DECL_NAME (decl));
6374 remove = true;
6376 break;
6378 case OMP_CLAUSE_FINAL:
6379 case OMP_CLAUSE_IF:
6380 OMP_CLAUSE_OPERAND (c, 0)
6381 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6382 /* Fall through. */
6384 case OMP_CLAUSE_SCHEDULE:
6385 case OMP_CLAUSE_NUM_THREADS:
6386 case OMP_CLAUSE_NUM_TEAMS:
6387 case OMP_CLAUSE_THREAD_LIMIT:
6388 case OMP_CLAUSE_DIST_SCHEDULE:
6389 case OMP_CLAUSE_DEVICE:
6390 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6391 is_gimple_val, fb_rvalue) == GS_ERROR)
6392 remove = true;
6393 break;
6395 case OMP_CLAUSE_NOWAIT:
6396 case OMP_CLAUSE_ORDERED:
6397 case OMP_CLAUSE_UNTIED:
6398 case OMP_CLAUSE_COLLAPSE:
6399 case OMP_CLAUSE_MERGEABLE:
6400 case OMP_CLAUSE_PROC_BIND:
6401 case OMP_CLAUSE_SAFELEN:
6402 break;
6404 case OMP_CLAUSE_ALIGNED:
6405 decl = OMP_CLAUSE_DECL (c);
6406 if (error_operand_p (decl))
6408 remove = true;
6409 break;
6411 if (!is_global_var (decl)
6412 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6413 omp_add_variable (ctx, decl, GOVD_ALIGNED);
6414 break;
6416 case OMP_CLAUSE_DEFAULT:
6417 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6418 break;
6420 default:
6421 gcc_unreachable ();
6424 if (remove)
6425 *list_p = OMP_CLAUSE_CHAIN (c);
6426 else
6427 list_p = &OMP_CLAUSE_CHAIN (c);
6430 gimplify_omp_ctxp = ctx;
6433 /* For all variables that were not actually used within the context,
6434 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6436 static int
6437 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6439 tree *list_p = (tree *) data;
6440 tree decl = (tree) n->key;
6441 unsigned flags = n->value;
6442 enum omp_clause_code code;
6443 tree clause;
6444 bool private_debug;
6446 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6447 return 0;
6448 if ((flags & GOVD_SEEN) == 0)
6449 return 0;
6450 if (flags & GOVD_DEBUG_PRIVATE)
6452 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6453 private_debug = true;
6455 else if (flags & GOVD_MAP)
6456 private_debug = false;
6457 else
6458 private_debug
6459 = lang_hooks.decls.omp_private_debug_clause (decl,
6460 !!(flags & GOVD_SHARED));
6461 if (private_debug)
6462 code = OMP_CLAUSE_PRIVATE;
6463 else if (flags & GOVD_MAP)
6464 code = OMP_CLAUSE_MAP;
6465 else if (flags & GOVD_SHARED)
6467 if (is_global_var (decl))
6469 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6470 while (ctx != NULL)
6472 splay_tree_node on
6473 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6474 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6475 | GOVD_PRIVATE | GOVD_REDUCTION
6476 | GOVD_LINEAR)) != 0)
6477 break;
6478 ctx = ctx->outer_context;
6480 if (ctx == NULL)
6481 return 0;
6483 code = OMP_CLAUSE_SHARED;
6485 else if (flags & GOVD_PRIVATE)
6486 code = OMP_CLAUSE_PRIVATE;
6487 else if (flags & GOVD_FIRSTPRIVATE)
6488 code = OMP_CLAUSE_FIRSTPRIVATE;
6489 else if (flags & GOVD_LASTPRIVATE)
6490 code = OMP_CLAUSE_LASTPRIVATE;
6491 else if (flags & GOVD_ALIGNED)
6492 return 0;
6493 else
6494 gcc_unreachable ();
6496 clause = build_omp_clause (input_location, code);
6497 OMP_CLAUSE_DECL (clause) = decl;
6498 OMP_CLAUSE_CHAIN (clause) = *list_p;
6499 if (private_debug)
6500 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6501 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6502 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6503 else if (code == OMP_CLAUSE_MAP)
6505 OMP_CLAUSE_MAP_KIND (clause) = flags & GOVD_MAP_TO_ONLY
6506 ? OMP_CLAUSE_MAP_TO
6507 : OMP_CLAUSE_MAP_TOFROM;
6508 if (DECL_SIZE (decl)
6509 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6511 tree decl2 = DECL_VALUE_EXPR (decl);
6512 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6513 decl2 = TREE_OPERAND (decl2, 0);
6514 gcc_assert (DECL_P (decl2));
6515 tree mem = build_simple_mem_ref (decl2);
6516 OMP_CLAUSE_DECL (clause) = mem;
6517 OMP_CLAUSE_SIZE (clause) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6518 if (gimplify_omp_ctxp->outer_context)
6520 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6521 omp_notice_variable (ctx, decl2, true);
6522 omp_notice_variable (ctx, OMP_CLAUSE_SIZE (clause), true);
6524 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (clause),
6525 OMP_CLAUSE_MAP);
6526 OMP_CLAUSE_DECL (nc) = decl;
6527 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6528 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6529 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (clause);
6530 OMP_CLAUSE_CHAIN (clause) = nc;
6533 *list_p = clause;
6534 lang_hooks.decls.omp_finish_clause (clause);
6536 return 0;
6539 static void
6540 gimplify_adjust_omp_clauses (tree *list_p)
6542 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6543 tree c, decl;
6545 while ((c = *list_p) != NULL)
6547 splay_tree_node n;
6548 bool remove = false;
6550 switch (OMP_CLAUSE_CODE (c))
6552 case OMP_CLAUSE_PRIVATE:
6553 case OMP_CLAUSE_SHARED:
6554 case OMP_CLAUSE_FIRSTPRIVATE:
6555 case OMP_CLAUSE_LINEAR:
6556 decl = OMP_CLAUSE_DECL (c);
6557 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6558 remove = !(n->value & GOVD_SEEN);
6559 if (! remove)
6561 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6562 if ((n->value & GOVD_DEBUG_PRIVATE)
6563 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6565 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6566 || ((n->value & GOVD_DATA_SHARE_CLASS)
6567 == GOVD_PRIVATE));
6568 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6569 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6571 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6572 && ctx->outer_context
6573 && !(OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6574 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c))
6575 && !is_global_var (decl))
6577 if (ctx->outer_context->region_type == ORT_COMBINED_PARALLEL)
6579 n = splay_tree_lookup (ctx->outer_context->variables,
6580 (splay_tree_key) decl);
6581 if (n == NULL
6582 || (n->value & GOVD_DATA_SHARE_CLASS) == 0)
6584 int flags = OMP_CLAUSE_LINEAR_NO_COPYIN (c)
6585 ? GOVD_LASTPRIVATE : GOVD_SHARED;
6586 if (n == NULL)
6587 omp_add_variable (ctx->outer_context, decl,
6588 flags | GOVD_SEEN);
6589 else
6590 n->value |= flags | GOVD_SEEN;
6593 else
6594 omp_notice_variable (ctx->outer_context, decl, true);
6597 break;
6599 case OMP_CLAUSE_LASTPRIVATE:
6600 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6601 accurately reflect the presence of a FIRSTPRIVATE clause. */
6602 decl = OMP_CLAUSE_DECL (c);
6603 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6604 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6605 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6606 break;
6608 case OMP_CLAUSE_ALIGNED:
6609 decl = OMP_CLAUSE_DECL (c);
6610 if (!is_global_var (decl))
6612 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6613 remove = n == NULL || !(n->value & GOVD_SEEN);
6614 if (!remove && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
6616 struct gimplify_omp_ctx *octx;
6617 if (n != NULL
6618 && (n->value & (GOVD_DATA_SHARE_CLASS
6619 & ~GOVD_FIRSTPRIVATE)))
6620 remove = true;
6621 else
6622 for (octx = ctx->outer_context; octx;
6623 octx = octx->outer_context)
6625 n = splay_tree_lookup (octx->variables,
6626 (splay_tree_key) decl);
6627 if (n == NULL)
6628 continue;
6629 if (n->value & GOVD_LOCAL)
6630 break;
6631 /* We have to avoid assigning a shared variable
6632 to itself when trying to add
6633 __builtin_assume_aligned. */
6634 if (n->value & GOVD_SHARED)
6636 remove = true;
6637 break;
6642 else if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6644 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6645 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6646 remove = true;
6648 break;
6650 case OMP_CLAUSE_MAP:
6651 decl = OMP_CLAUSE_DECL (c);
6652 if (!DECL_P (decl))
6653 break;
6654 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6655 if (ctx->region_type == ORT_TARGET && !(n->value & GOVD_SEEN))
6656 remove = true;
6657 else if (DECL_SIZE (decl)
6658 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
6659 && OMP_CLAUSE_MAP_KIND (c) != OMP_CLAUSE_MAP_POINTER)
6661 tree decl2 = DECL_VALUE_EXPR (decl);
6662 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6663 decl2 = TREE_OPERAND (decl2, 0);
6664 gcc_assert (DECL_P (decl2));
6665 tree mem = build_simple_mem_ref (decl2);
6666 OMP_CLAUSE_DECL (c) = mem;
6667 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6668 if (ctx->outer_context)
6670 omp_notice_variable (ctx->outer_context, decl2, true);
6671 omp_notice_variable (ctx->outer_context,
6672 OMP_CLAUSE_SIZE (c), true);
6674 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6675 OMP_CLAUSE_MAP);
6676 OMP_CLAUSE_DECL (nc) = decl;
6677 OMP_CLAUSE_SIZE (nc) = size_zero_node;
6678 OMP_CLAUSE_MAP_KIND (nc) = OMP_CLAUSE_MAP_POINTER;
6679 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
6680 OMP_CLAUSE_CHAIN (c) = nc;
6681 c = nc;
6683 break;
6685 case OMP_CLAUSE_TO:
6686 case OMP_CLAUSE_FROM:
6687 decl = OMP_CLAUSE_DECL (c);
6688 if (!DECL_P (decl))
6689 break;
6690 if (DECL_SIZE (decl)
6691 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
6693 tree decl2 = DECL_VALUE_EXPR (decl);
6694 gcc_assert (TREE_CODE (decl2) == INDIRECT_REF);
6695 decl2 = TREE_OPERAND (decl2, 0);
6696 gcc_assert (DECL_P (decl2));
6697 tree mem = build_simple_mem_ref (decl2);
6698 OMP_CLAUSE_DECL (c) = mem;
6699 OMP_CLAUSE_SIZE (c) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
6700 if (ctx->outer_context)
6702 omp_notice_variable (ctx->outer_context, decl2, true);
6703 omp_notice_variable (ctx->outer_context,
6704 OMP_CLAUSE_SIZE (c), true);
6707 break;
6709 case OMP_CLAUSE_REDUCTION:
6710 case OMP_CLAUSE_COPYIN:
6711 case OMP_CLAUSE_COPYPRIVATE:
6712 case OMP_CLAUSE_IF:
6713 case OMP_CLAUSE_NUM_THREADS:
6714 case OMP_CLAUSE_NUM_TEAMS:
6715 case OMP_CLAUSE_THREAD_LIMIT:
6716 case OMP_CLAUSE_DIST_SCHEDULE:
6717 case OMP_CLAUSE_DEVICE:
6718 case OMP_CLAUSE_SCHEDULE:
6719 case OMP_CLAUSE_NOWAIT:
6720 case OMP_CLAUSE_ORDERED:
6721 case OMP_CLAUSE_DEFAULT:
6722 case OMP_CLAUSE_UNTIED:
6723 case OMP_CLAUSE_COLLAPSE:
6724 case OMP_CLAUSE_FINAL:
6725 case OMP_CLAUSE_MERGEABLE:
6726 case OMP_CLAUSE_PROC_BIND:
6727 case OMP_CLAUSE_SAFELEN:
6728 case OMP_CLAUSE_DEPEND:
6729 break;
6731 default:
6732 gcc_unreachable ();
6735 if (remove)
6736 *list_p = OMP_CLAUSE_CHAIN (c);
6737 else
6738 list_p = &OMP_CLAUSE_CHAIN (c);
6741 /* Add in any implicit data sharing. */
6742 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6744 gimplify_omp_ctxp = ctx->outer_context;
6745 delete_omp_context (ctx);
6748 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6749 gimplification of the body, as well as scanning the body for used
6750 variables. We need to do this scan now, because variable-sized
6751 decls will be decomposed during gimplification. */
6753 static void
6754 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6756 tree expr = *expr_p;
6757 gimple g;
6758 gimple_seq body = NULL;
6759 struct gimplify_ctx gctx;
6761 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6762 OMP_PARALLEL_COMBINED (expr)
6763 ? ORT_COMBINED_PARALLEL
6764 : ORT_PARALLEL);
6766 push_gimplify_context (&gctx);
6768 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6769 if (gimple_code (g) == GIMPLE_BIND)
6770 pop_gimplify_context (g);
6771 else
6772 pop_gimplify_context (NULL);
6774 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6776 g = gimple_build_omp_parallel (body,
6777 OMP_PARALLEL_CLAUSES (expr),
6778 NULL_TREE, NULL_TREE);
6779 if (OMP_PARALLEL_COMBINED (expr))
6780 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6781 gimplify_seq_add_stmt (pre_p, g);
6782 *expr_p = NULL_TREE;
6785 /* Gimplify the contents of an OMP_TASK statement. This involves
6786 gimplification of the body, as well as scanning the body for used
6787 variables. We need to do this scan now, because variable-sized
6788 decls will be decomposed during gimplification. */
6790 static void
6791 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6793 tree expr = *expr_p;
6794 gimple g;
6795 gimple_seq body = NULL;
6796 struct gimplify_ctx gctx;
6798 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6799 find_omp_clause (OMP_TASK_CLAUSES (expr),
6800 OMP_CLAUSE_UNTIED)
6801 ? ORT_UNTIED_TASK : ORT_TASK);
6803 push_gimplify_context (&gctx);
6805 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6806 if (gimple_code (g) == GIMPLE_BIND)
6807 pop_gimplify_context (g);
6808 else
6809 pop_gimplify_context (NULL);
6811 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6813 g = gimple_build_omp_task (body,
6814 OMP_TASK_CLAUSES (expr),
6815 NULL_TREE, NULL_TREE,
6816 NULL_TREE, NULL_TREE, NULL_TREE);
6817 gimplify_seq_add_stmt (pre_p, g);
6818 *expr_p = NULL_TREE;
6821 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
6822 with non-NULL OMP_FOR_INIT. */
6824 static tree
6825 find_combined_omp_for (tree *tp, int *walk_subtrees, void *)
6827 *walk_subtrees = 0;
6828 switch (TREE_CODE (*tp))
6830 case OMP_FOR:
6831 *walk_subtrees = 1;
6832 /* FALLTHRU */
6833 case OMP_SIMD:
6834 if (OMP_FOR_INIT (*tp) != NULL_TREE)
6835 return *tp;
6836 break;
6837 case BIND_EXPR:
6838 case STATEMENT_LIST:
6839 case OMP_PARALLEL:
6840 *walk_subtrees = 1;
6841 break;
6842 default:
6843 break;
6845 return NULL_TREE;
6848 /* Gimplify the gross structure of an OMP_FOR statement. */
6850 static enum gimplify_status
6851 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6853 tree for_stmt, orig_for_stmt, decl, var, t;
6854 enum gimplify_status ret = GS_ALL_DONE;
6855 enum gimplify_status tret;
6856 gimple gfor;
6857 gimple_seq for_body, for_pre_body;
6858 int i;
6859 bool simd;
6860 bitmap has_decl_expr = NULL;
6862 orig_for_stmt = for_stmt = *expr_p;
6864 simd = TREE_CODE (for_stmt) == OMP_SIMD;
6865 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6866 simd ? ORT_SIMD : ORT_WORKSHARE);
6868 /* Handle OMP_FOR_INIT. */
6869 for_pre_body = NULL;
6870 if (simd && OMP_FOR_PRE_BODY (for_stmt))
6872 has_decl_expr = BITMAP_ALLOC (NULL);
6873 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == DECL_EXPR
6874 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt)))
6875 == VAR_DECL)
6877 t = OMP_FOR_PRE_BODY (for_stmt);
6878 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6880 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt)) == STATEMENT_LIST)
6882 tree_stmt_iterator si;
6883 for (si = tsi_start (OMP_FOR_PRE_BODY (for_stmt)); !tsi_end_p (si);
6884 tsi_next (&si))
6886 t = tsi_stmt (si);
6887 if (TREE_CODE (t) == DECL_EXPR
6888 && TREE_CODE (DECL_EXPR_DECL (t)) == VAR_DECL)
6889 bitmap_set_bit (has_decl_expr, DECL_UID (DECL_EXPR_DECL (t)));
6893 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6894 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6896 if (OMP_FOR_INIT (for_stmt) == NULL_TREE)
6898 for_stmt = walk_tree (&OMP_FOR_BODY (for_stmt), find_combined_omp_for,
6899 NULL, NULL);
6900 gcc_assert (for_stmt != NULL_TREE);
6901 gimplify_omp_ctxp->combined_loop = true;
6904 for_body = NULL;
6905 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6906 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6907 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6908 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6909 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6911 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6912 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6913 decl = TREE_OPERAND (t, 0);
6914 gcc_assert (DECL_P (decl));
6915 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6916 || POINTER_TYPE_P (TREE_TYPE (decl)));
6918 /* Make sure the iteration variable is private. */
6919 tree c = NULL_TREE;
6920 if (orig_for_stmt != for_stmt)
6921 /* Do this only on innermost construct for combined ones. */;
6922 else if (simd)
6924 splay_tree_node n = splay_tree_lookup (gimplify_omp_ctxp->variables,
6925 (splay_tree_key)decl);
6926 omp_is_private (gimplify_omp_ctxp, decl, simd);
6927 if (n != NULL && (n->value & GOVD_DATA_SHARE_CLASS) != 0)
6928 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6929 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) == 1)
6931 c = build_omp_clause (input_location, OMP_CLAUSE_LINEAR);
6932 OMP_CLAUSE_LINEAR_NO_COPYIN (c) = 1;
6933 if (has_decl_expr
6934 && bitmap_bit_p (has_decl_expr, DECL_UID (decl)))
6935 OMP_CLAUSE_LINEAR_NO_COPYOUT (c) = 1;
6936 OMP_CLAUSE_DECL (c) = decl;
6937 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6938 OMP_FOR_CLAUSES (for_stmt) = c;
6939 omp_add_variable (gimplify_omp_ctxp, decl,
6940 GOVD_LINEAR | GOVD_EXPLICIT | GOVD_SEEN);
6942 else
6944 bool lastprivate
6945 = (!has_decl_expr
6946 || !bitmap_bit_p (has_decl_expr, DECL_UID (decl)));
6947 c = build_omp_clause (input_location,
6948 lastprivate ? OMP_CLAUSE_LASTPRIVATE
6949 : OMP_CLAUSE_PRIVATE);
6950 OMP_CLAUSE_DECL (c) = decl;
6951 OMP_CLAUSE_CHAIN (c) = OMP_FOR_CLAUSES (for_stmt);
6952 omp_add_variable (gimplify_omp_ctxp, decl,
6953 (lastprivate ? GOVD_LASTPRIVATE : GOVD_PRIVATE)
6954 | GOVD_SEEN);
6955 c = NULL_TREE;
6958 else if (omp_is_private (gimplify_omp_ctxp, decl, simd))
6959 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6960 else
6961 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6963 /* If DECL is not a gimple register, create a temporary variable to act
6964 as an iteration counter. This is valid, since DECL cannot be
6965 modified in the body of the loop. */
6966 if (orig_for_stmt != for_stmt)
6967 var = decl;
6968 else if (!is_gimple_reg (decl))
6970 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6971 TREE_OPERAND (t, 0) = var;
6973 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6975 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6977 else
6978 var = decl;
6980 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6981 is_gimple_val, fb_rvalue);
6982 ret = MIN (ret, tret);
6983 if (ret == GS_ERROR)
6984 return ret;
6986 /* Handle OMP_FOR_COND. */
6987 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6988 gcc_assert (COMPARISON_CLASS_P (t));
6989 gcc_assert (TREE_OPERAND (t, 0) == decl);
6991 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6992 is_gimple_val, fb_rvalue);
6993 ret = MIN (ret, tret);
6995 /* Handle OMP_FOR_INCR. */
6996 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6997 switch (TREE_CODE (t))
6999 case PREINCREMENT_EXPR:
7000 case POSTINCREMENT_EXPR:
7001 if (orig_for_stmt != for_stmt)
7002 break;
7003 t = build_int_cst (TREE_TYPE (decl), 1);
7004 if (c)
7005 OMP_CLAUSE_LINEAR_STEP (c) = t;
7006 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7007 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7008 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7009 break;
7011 case PREDECREMENT_EXPR:
7012 case POSTDECREMENT_EXPR:
7013 if (orig_for_stmt != for_stmt)
7014 break;
7015 t = build_int_cst (TREE_TYPE (decl), -1);
7016 if (c)
7017 OMP_CLAUSE_LINEAR_STEP (c) = t;
7018 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
7019 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
7020 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
7021 break;
7023 case MODIFY_EXPR:
7024 gcc_assert (TREE_OPERAND (t, 0) == decl);
7025 TREE_OPERAND (t, 0) = var;
7027 t = TREE_OPERAND (t, 1);
7028 switch (TREE_CODE (t))
7030 case PLUS_EXPR:
7031 if (TREE_OPERAND (t, 1) == decl)
7033 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
7034 TREE_OPERAND (t, 0) = var;
7035 break;
7038 /* Fallthru. */
7039 case MINUS_EXPR:
7040 case POINTER_PLUS_EXPR:
7041 gcc_assert (TREE_OPERAND (t, 0) == decl);
7042 TREE_OPERAND (t, 0) = var;
7043 break;
7044 default:
7045 gcc_unreachable ();
7048 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
7049 is_gimple_val, fb_rvalue);
7050 ret = MIN (ret, tret);
7051 if (c)
7053 OMP_CLAUSE_LINEAR_STEP (c) = TREE_OPERAND (t, 1);
7054 if (TREE_CODE (t) == MINUS_EXPR)
7056 t = TREE_OPERAND (t, 1);
7057 OMP_CLAUSE_LINEAR_STEP (c)
7058 = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
7059 tret = gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c),
7060 &for_pre_body, NULL,
7061 is_gimple_val, fb_rvalue);
7062 ret = MIN (ret, tret);
7065 break;
7067 default:
7068 gcc_unreachable ();
7071 if ((var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
7072 && orig_for_stmt == for_stmt)
7074 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
7075 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7076 && OMP_CLAUSE_DECL (c) == decl
7077 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
7079 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7080 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
7081 gcc_assert (TREE_OPERAND (t, 0) == var);
7082 t = TREE_OPERAND (t, 1);
7083 gcc_assert (TREE_CODE (t) == PLUS_EXPR
7084 || TREE_CODE (t) == MINUS_EXPR
7085 || TREE_CODE (t) == POINTER_PLUS_EXPR);
7086 gcc_assert (TREE_OPERAND (t, 0) == var);
7087 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
7088 TREE_OPERAND (t, 1));
7089 gimplify_assign (decl, t,
7090 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
7095 BITMAP_FREE (has_decl_expr);
7097 gimplify_and_add (OMP_FOR_BODY (orig_for_stmt), &for_body);
7099 if (orig_for_stmt != for_stmt)
7100 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7102 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7103 decl = TREE_OPERAND (t, 0);
7104 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
7105 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
7106 TREE_OPERAND (t, 0) = var;
7107 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7108 TREE_OPERAND (t, 1) = copy_node (TREE_OPERAND (t, 1));
7109 TREE_OPERAND (TREE_OPERAND (t, 1), 0) = var;
7112 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt));
7114 int kind;
7115 switch (TREE_CODE (orig_for_stmt))
7117 case OMP_FOR: kind = GF_OMP_FOR_KIND_FOR; break;
7118 case OMP_SIMD: kind = GF_OMP_FOR_KIND_SIMD; break;
7119 case OMP_DISTRIBUTE: kind = GF_OMP_FOR_KIND_DISTRIBUTE; break;
7120 default:
7121 gcc_unreachable ();
7123 gfor = gimple_build_omp_for (for_body, kind, OMP_FOR_CLAUSES (orig_for_stmt),
7124 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
7125 for_pre_body);
7126 if (orig_for_stmt != for_stmt)
7127 gimple_omp_for_set_combined_p (gfor, true);
7128 if (gimplify_omp_ctxp
7129 && (gimplify_omp_ctxp->combined_loop
7130 || (gimplify_omp_ctxp->region_type == ORT_COMBINED_PARALLEL
7131 && gimplify_omp_ctxp->outer_context
7132 && gimplify_omp_ctxp->outer_context->combined_loop)))
7134 gimple_omp_for_set_combined_into_p (gfor, true);
7135 if (gimplify_omp_ctxp->combined_loop)
7136 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_SIMD);
7137 else
7138 gcc_assert (TREE_CODE (orig_for_stmt) == OMP_FOR);
7141 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
7143 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
7144 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
7145 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
7146 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
7147 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
7148 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
7149 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
7150 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
7153 gimplify_seq_add_stmt (pre_p, gfor);
7154 if (ret != GS_ALL_DONE)
7155 return GS_ERROR;
7156 *expr_p = NULL_TREE;
7157 return GS_ALL_DONE;
7160 /* Gimplify the gross structure of other OpenMP constructs.
7161 In particular, OMP_SECTIONS, OMP_SINGLE, OMP_TARGET, OMP_TARGET_DATA
7162 and OMP_TEAMS. */
7164 static void
7165 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
7167 tree expr = *expr_p;
7168 gimple stmt;
7169 gimple_seq body = NULL;
7170 enum omp_region_type ort = ORT_WORKSHARE;
7172 switch (TREE_CODE (expr))
7174 case OMP_SECTIONS:
7175 case OMP_SINGLE:
7176 break;
7177 case OMP_TARGET:
7178 ort = ORT_TARGET;
7179 break;
7180 case OMP_TARGET_DATA:
7181 ort = ORT_TARGET_DATA;
7182 break;
7183 case OMP_TEAMS:
7184 ort = ORT_TEAMS;
7185 break;
7186 default:
7187 gcc_unreachable ();
7189 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ort);
7190 if (ort == ORT_TARGET || ort == ORT_TARGET_DATA)
7192 struct gimplify_ctx gctx;
7193 push_gimplify_context (&gctx);
7194 gimple g = gimplify_and_return_first (OMP_BODY (expr), &body);
7195 if (gimple_code (g) == GIMPLE_BIND)
7196 pop_gimplify_context (g);
7197 else
7198 pop_gimplify_context (NULL);
7199 if (ort == ORT_TARGET_DATA)
7201 gimple_seq cleanup = NULL;
7202 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TARGET_END_DATA);
7203 g = gimple_build_call (fn, 0);
7204 gimple_seq_add_stmt (&cleanup, g);
7205 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
7206 body = NULL;
7207 gimple_seq_add_stmt (&body, g);
7210 else
7211 gimplify_and_add (OMP_BODY (expr), &body);
7212 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
7214 switch (TREE_CODE (expr))
7216 case OMP_SECTIONS:
7217 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
7218 break;
7219 case OMP_SINGLE:
7220 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
7221 break;
7222 case OMP_TARGET:
7223 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_REGION,
7224 OMP_CLAUSES (expr));
7225 break;
7226 case OMP_TARGET_DATA:
7227 stmt = gimple_build_omp_target (body, GF_OMP_TARGET_KIND_DATA,
7228 OMP_CLAUSES (expr));
7229 break;
7230 case OMP_TEAMS:
7231 stmt = gimple_build_omp_teams (body, OMP_CLAUSES (expr));
7232 break;
7233 default:
7234 gcc_unreachable ();
7237 gimplify_seq_add_stmt (pre_p, stmt);
7238 *expr_p = NULL_TREE;
7241 /* Gimplify the gross structure of OpenMP target update construct. */
7243 static void
7244 gimplify_omp_target_update (tree *expr_p, gimple_seq *pre_p)
7246 tree expr = *expr_p;
7247 gimple stmt;
7249 gimplify_scan_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr), pre_p,
7250 ORT_WORKSHARE);
7251 gimplify_adjust_omp_clauses (&OMP_TARGET_UPDATE_CLAUSES (expr));
7252 stmt = gimple_build_omp_target (NULL, GF_OMP_TARGET_KIND_UPDATE,
7253 OMP_TARGET_UPDATE_CLAUSES (expr));
7255 gimplify_seq_add_stmt (pre_p, stmt);
7256 *expr_p = NULL_TREE;
7259 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
7260 stabilized the lhs of the atomic operation as *ADDR. Return true if
7261 EXPR is this stabilized form. */
7263 static bool
7264 goa_lhs_expr_p (tree expr, tree addr)
7266 /* Also include casts to other type variants. The C front end is fond
7267 of adding these for e.g. volatile variables. This is like
7268 STRIP_TYPE_NOPS but includes the main variant lookup. */
7269 STRIP_USELESS_TYPE_CONVERSION (expr);
7271 if (TREE_CODE (expr) == INDIRECT_REF)
7273 expr = TREE_OPERAND (expr, 0);
7274 while (expr != addr
7275 && (CONVERT_EXPR_P (expr)
7276 || TREE_CODE (expr) == NON_LVALUE_EXPR)
7277 && TREE_CODE (expr) == TREE_CODE (addr)
7278 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
7280 expr = TREE_OPERAND (expr, 0);
7281 addr = TREE_OPERAND (addr, 0);
7283 if (expr == addr)
7284 return true;
7285 return (TREE_CODE (addr) == ADDR_EXPR
7286 && TREE_CODE (expr) == ADDR_EXPR
7287 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
7289 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
7290 return true;
7291 return false;
7294 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
7295 expression does not involve the lhs, evaluate it into a temporary.
7296 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
7297 or -1 if an error was encountered. */
7299 static int
7300 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
7301 tree lhs_var)
7303 tree expr = *expr_p;
7304 int saw_lhs;
7306 if (goa_lhs_expr_p (expr, lhs_addr))
7308 *expr_p = lhs_var;
7309 return 1;
7311 if (is_gimple_val (expr))
7312 return 0;
7314 saw_lhs = 0;
7315 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
7317 case tcc_binary:
7318 case tcc_comparison:
7319 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
7320 lhs_var);
7321 case tcc_unary:
7322 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
7323 lhs_var);
7324 break;
7325 case tcc_expression:
7326 switch (TREE_CODE (expr))
7328 case TRUTH_ANDIF_EXPR:
7329 case TRUTH_ORIF_EXPR:
7330 case TRUTH_AND_EXPR:
7331 case TRUTH_OR_EXPR:
7332 case TRUTH_XOR_EXPR:
7333 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
7334 lhs_addr, lhs_var);
7335 case TRUTH_NOT_EXPR:
7336 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
7337 lhs_addr, lhs_var);
7338 break;
7339 case COMPOUND_EXPR:
7340 /* Break out any preevaluations from cp_build_modify_expr. */
7341 for (; TREE_CODE (expr) == COMPOUND_EXPR;
7342 expr = TREE_OPERAND (expr, 1))
7343 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
7344 *expr_p = expr;
7345 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
7346 default:
7347 break;
7349 break;
7350 default:
7351 break;
7354 if (saw_lhs == 0)
7356 enum gimplify_status gs;
7357 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
7358 if (gs != GS_ALL_DONE)
7359 saw_lhs = -1;
7362 return saw_lhs;
7365 /* Gimplify an OMP_ATOMIC statement. */
7367 static enum gimplify_status
7368 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
7370 tree addr = TREE_OPERAND (*expr_p, 0);
7371 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
7372 ? NULL : TREE_OPERAND (*expr_p, 1);
7373 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
7374 tree tmp_load;
7375 gimple loadstmt, storestmt;
7377 tmp_load = create_tmp_reg (type, NULL);
7378 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
7379 return GS_ERROR;
7381 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
7382 != GS_ALL_DONE)
7383 return GS_ERROR;
7385 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
7386 gimplify_seq_add_stmt (pre_p, loadstmt);
7387 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
7388 != GS_ALL_DONE)
7389 return GS_ERROR;
7391 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
7392 rhs = tmp_load;
7393 storestmt = gimple_build_omp_atomic_store (rhs);
7394 gimplify_seq_add_stmt (pre_p, storestmt);
7395 if (OMP_ATOMIC_SEQ_CST (*expr_p))
7397 gimple_omp_atomic_set_seq_cst (loadstmt);
7398 gimple_omp_atomic_set_seq_cst (storestmt);
7400 switch (TREE_CODE (*expr_p))
7402 case OMP_ATOMIC_READ:
7403 case OMP_ATOMIC_CAPTURE_OLD:
7404 *expr_p = tmp_load;
7405 gimple_omp_atomic_set_need_value (loadstmt);
7406 break;
7407 case OMP_ATOMIC_CAPTURE_NEW:
7408 *expr_p = rhs;
7409 gimple_omp_atomic_set_need_value (storestmt);
7410 break;
7411 default:
7412 *expr_p = NULL;
7413 break;
7416 return GS_ALL_DONE;
7419 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
7420 body, and adding some EH bits. */
7422 static enum gimplify_status
7423 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
7425 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
7426 gimple g;
7427 gimple_seq body = NULL;
7428 struct gimplify_ctx gctx;
7429 int subcode = 0;
7431 /* Wrap the transaction body in a BIND_EXPR so we have a context
7432 where to put decls for OpenMP. */
7433 if (TREE_CODE (tbody) != BIND_EXPR)
7435 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
7436 TREE_SIDE_EFFECTS (bind) = 1;
7437 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
7438 TRANSACTION_EXPR_BODY (expr) = bind;
7441 push_gimplify_context (&gctx);
7442 temp = voidify_wrapper_expr (*expr_p, NULL);
7444 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
7445 pop_gimplify_context (g);
7447 g = gimple_build_transaction (body, NULL);
7448 if (TRANSACTION_EXPR_OUTER (expr))
7449 subcode = GTMA_IS_OUTER;
7450 else if (TRANSACTION_EXPR_RELAXED (expr))
7451 subcode = GTMA_IS_RELAXED;
7452 gimple_transaction_set_subcode (g, subcode);
7454 gimplify_seq_add_stmt (pre_p, g);
7456 if (temp)
7458 *expr_p = temp;
7459 return GS_OK;
7462 *expr_p = NULL_TREE;
7463 return GS_ALL_DONE;
7466 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
7467 expression produces a value to be used as an operand inside a GIMPLE
7468 statement, the value will be stored back in *EXPR_P. This value will
7469 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
7470 an SSA_NAME. The corresponding sequence of GIMPLE statements is
7471 emitted in PRE_P and POST_P.
7473 Additionally, this process may overwrite parts of the input
7474 expression during gimplification. Ideally, it should be
7475 possible to do non-destructive gimplification.
7477 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
7478 the expression needs to evaluate to a value to be used as
7479 an operand in a GIMPLE statement, this value will be stored in
7480 *EXPR_P on exit. This happens when the caller specifies one
7481 of fb_lvalue or fb_rvalue fallback flags.
7483 PRE_P will contain the sequence of GIMPLE statements corresponding
7484 to the evaluation of EXPR and all the side-effects that must
7485 be executed before the main expression. On exit, the last
7486 statement of PRE_P is the core statement being gimplified. For
7487 instance, when gimplifying 'if (++a)' the last statement in
7488 PRE_P will be 'if (t.1)' where t.1 is the result of
7489 pre-incrementing 'a'.
7491 POST_P will contain the sequence of GIMPLE statements corresponding
7492 to the evaluation of all the side-effects that must be executed
7493 after the main expression. If this is NULL, the post
7494 side-effects are stored at the end of PRE_P.
7496 The reason why the output is split in two is to handle post
7497 side-effects explicitly. In some cases, an expression may have
7498 inner and outer post side-effects which need to be emitted in
7499 an order different from the one given by the recursive
7500 traversal. For instance, for the expression (*p--)++ the post
7501 side-effects of '--' must actually occur *after* the post
7502 side-effects of '++'. However, gimplification will first visit
7503 the inner expression, so if a separate POST sequence was not
7504 used, the resulting sequence would be:
7506 1 t.1 = *p
7507 2 p = p - 1
7508 3 t.2 = t.1 + 1
7509 4 *p = t.2
7511 However, the post-decrement operation in line #2 must not be
7512 evaluated until after the store to *p at line #4, so the
7513 correct sequence should be:
7515 1 t.1 = *p
7516 2 t.2 = t.1 + 1
7517 3 *p = t.2
7518 4 p = p - 1
7520 So, by specifying a separate post queue, it is possible
7521 to emit the post side-effects in the correct order.
7522 If POST_P is NULL, an internal queue will be used. Before
7523 returning to the caller, the sequence POST_P is appended to
7524 the main output sequence PRE_P.
7526 GIMPLE_TEST_F points to a function that takes a tree T and
7527 returns nonzero if T is in the GIMPLE form requested by the
7528 caller. The GIMPLE predicates are in gimple.c.
7530 FALLBACK tells the function what sort of a temporary we want if
7531 gimplification cannot produce an expression that complies with
7532 GIMPLE_TEST_F.
7534 fb_none means that no temporary should be generated
7535 fb_rvalue means that an rvalue is OK to generate
7536 fb_lvalue means that an lvalue is OK to generate
7537 fb_either means that either is OK, but an lvalue is preferable.
7538 fb_mayfail means that gimplification may fail (in which case
7539 GS_ERROR will be returned)
7541 The return value is either GS_ERROR or GS_ALL_DONE, since this
7542 function iterates until EXPR is completely gimplified or an error
7543 occurs. */
7545 enum gimplify_status
7546 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
7547 bool (*gimple_test_f) (tree), fallback_t fallback)
7549 tree tmp;
7550 gimple_seq internal_pre = NULL;
7551 gimple_seq internal_post = NULL;
7552 tree save_expr;
7553 bool is_statement;
7554 location_t saved_location;
7555 enum gimplify_status ret;
7556 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
7558 save_expr = *expr_p;
7559 if (save_expr == NULL_TREE)
7560 return GS_ALL_DONE;
7562 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
7563 is_statement = gimple_test_f == is_gimple_stmt;
7564 if (is_statement)
7565 gcc_assert (pre_p);
7567 /* Consistency checks. */
7568 if (gimple_test_f == is_gimple_reg)
7569 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
7570 else if (gimple_test_f == is_gimple_val
7571 || gimple_test_f == is_gimple_call_addr
7572 || gimple_test_f == is_gimple_condexpr
7573 || gimple_test_f == is_gimple_mem_rhs
7574 || gimple_test_f == is_gimple_mem_rhs_or_call
7575 || gimple_test_f == is_gimple_reg_rhs
7576 || gimple_test_f == is_gimple_reg_rhs_or_call
7577 || gimple_test_f == is_gimple_asm_val
7578 || gimple_test_f == is_gimple_mem_ref_addr)
7579 gcc_assert (fallback & fb_rvalue);
7580 else if (gimple_test_f == is_gimple_min_lval
7581 || gimple_test_f == is_gimple_lvalue)
7582 gcc_assert (fallback & fb_lvalue);
7583 else if (gimple_test_f == is_gimple_addressable)
7584 gcc_assert (fallback & fb_either);
7585 else if (gimple_test_f == is_gimple_stmt)
7586 gcc_assert (fallback == fb_none);
7587 else
7589 /* We should have recognized the GIMPLE_TEST_F predicate to
7590 know what kind of fallback to use in case a temporary is
7591 needed to hold the value or address of *EXPR_P. */
7592 gcc_unreachable ();
7595 /* We used to check the predicate here and return immediately if it
7596 succeeds. This is wrong; the design is for gimplification to be
7597 idempotent, and for the predicates to only test for valid forms, not
7598 whether they are fully simplified. */
7599 if (pre_p == NULL)
7600 pre_p = &internal_pre;
7602 if (post_p == NULL)
7603 post_p = &internal_post;
7605 /* Remember the last statements added to PRE_P and POST_P. Every
7606 new statement added by the gimplification helpers needs to be
7607 annotated with location information. To centralize the
7608 responsibility, we remember the last statement that had been
7609 added to both queues before gimplifying *EXPR_P. If
7610 gimplification produces new statements in PRE_P and POST_P, those
7611 statements will be annotated with the same location information
7612 as *EXPR_P. */
7613 pre_last_gsi = gsi_last (*pre_p);
7614 post_last_gsi = gsi_last (*post_p);
7616 saved_location = input_location;
7617 if (save_expr != error_mark_node
7618 && EXPR_HAS_LOCATION (*expr_p))
7619 input_location = EXPR_LOCATION (*expr_p);
7621 /* Loop over the specific gimplifiers until the toplevel node
7622 remains the same. */
7625 /* Strip away as many useless type conversions as possible
7626 at the toplevel. */
7627 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
7629 /* Remember the expr. */
7630 save_expr = *expr_p;
7632 /* Die, die, die, my darling. */
7633 if (save_expr == error_mark_node
7634 || (TREE_TYPE (save_expr)
7635 && TREE_TYPE (save_expr) == error_mark_node))
7637 ret = GS_ERROR;
7638 break;
7641 /* Do any language-specific gimplification. */
7642 ret = ((enum gimplify_status)
7643 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
7644 if (ret == GS_OK)
7646 if (*expr_p == NULL_TREE)
7647 break;
7648 if (*expr_p != save_expr)
7649 continue;
7651 else if (ret != GS_UNHANDLED)
7652 break;
7654 /* Make sure that all the cases set 'ret' appropriately. */
7655 ret = GS_UNHANDLED;
7656 switch (TREE_CODE (*expr_p))
7658 /* First deal with the special cases. */
7660 case POSTINCREMENT_EXPR:
7661 case POSTDECREMENT_EXPR:
7662 case PREINCREMENT_EXPR:
7663 case PREDECREMENT_EXPR:
7664 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
7665 fallback != fb_none,
7666 TREE_TYPE (*expr_p));
7667 break;
7669 case ARRAY_REF:
7670 case ARRAY_RANGE_REF:
7671 case REALPART_EXPR:
7672 case IMAGPART_EXPR:
7673 case COMPONENT_REF:
7674 case VIEW_CONVERT_EXPR:
7675 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
7676 fallback ? fallback : fb_rvalue);
7677 break;
7679 case COND_EXPR:
7680 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
7682 /* C99 code may assign to an array in a structure value of a
7683 conditional expression, and this has undefined behavior
7684 only on execution, so create a temporary if an lvalue is
7685 required. */
7686 if (fallback == fb_lvalue)
7688 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7689 mark_addressable (*expr_p);
7690 ret = GS_OK;
7692 break;
7694 case CILK_SPAWN_STMT:
7695 gcc_assert
7696 (fn_contains_cilk_spawn_p (cfun)
7697 && lang_hooks.cilkplus.cilk_detect_spawn_and_unwrap (expr_p));
7698 if (!seen_error ())
7700 ret = (enum gimplify_status)
7701 lang_hooks.cilkplus.gimplify_cilk_spawn (expr_p, pre_p,
7702 post_p);
7703 break;
7705 /* If errors are seen, then just process it as a CALL_EXPR. */
7707 case CALL_EXPR:
7708 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
7710 /* C99 code may assign to an array in a structure returned
7711 from a function, and this has undefined behavior only on
7712 execution, so create a temporary if an lvalue is
7713 required. */
7714 if (fallback == fb_lvalue)
7716 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7717 mark_addressable (*expr_p);
7718 ret = GS_OK;
7720 break;
7722 case TREE_LIST:
7723 gcc_unreachable ();
7725 case COMPOUND_EXPR:
7726 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
7727 break;
7729 case COMPOUND_LITERAL_EXPR:
7730 ret = gimplify_compound_literal_expr (expr_p, pre_p,
7731 gimple_test_f, fallback);
7732 break;
7734 case MODIFY_EXPR:
7735 case INIT_EXPR:
7736 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
7737 fallback != fb_none);
7738 break;
7740 case TRUTH_ANDIF_EXPR:
7741 case TRUTH_ORIF_EXPR:
7743 /* Preserve the original type of the expression and the
7744 source location of the outer expression. */
7745 tree org_type = TREE_TYPE (*expr_p);
7746 *expr_p = gimple_boolify (*expr_p);
7747 *expr_p = build3_loc (input_location, COND_EXPR,
7748 org_type, *expr_p,
7749 fold_convert_loc
7750 (input_location,
7751 org_type, boolean_true_node),
7752 fold_convert_loc
7753 (input_location,
7754 org_type, boolean_false_node));
7755 ret = GS_OK;
7756 break;
7759 case TRUTH_NOT_EXPR:
7761 tree type = TREE_TYPE (*expr_p);
7762 /* The parsers are careful to generate TRUTH_NOT_EXPR
7763 only with operands that are always zero or one.
7764 We do not fold here but handle the only interesting case
7765 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
7766 *expr_p = gimple_boolify (*expr_p);
7767 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
7768 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
7769 TREE_TYPE (*expr_p),
7770 TREE_OPERAND (*expr_p, 0));
7771 else
7772 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
7773 TREE_TYPE (*expr_p),
7774 TREE_OPERAND (*expr_p, 0),
7775 build_int_cst (TREE_TYPE (*expr_p), 1));
7776 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
7777 *expr_p = fold_convert_loc (input_location, type, *expr_p);
7778 ret = GS_OK;
7779 break;
7782 case ADDR_EXPR:
7783 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
7784 break;
7786 case ANNOTATE_EXPR:
7788 tree cond = TREE_OPERAND (*expr_p, 0);
7789 tree id = TREE_OPERAND (*expr_p, 1);
7790 tree tmp = create_tmp_var_raw (TREE_TYPE(cond), NULL);
7791 gimplify_arg (&cond, pre_p, EXPR_LOCATION (*expr_p));
7792 gimple call = gimple_build_call_internal (IFN_ANNOTATE, 2,
7793 cond, id);
7794 gimple_call_set_lhs (call, tmp);
7795 gimplify_seq_add_stmt (pre_p, call);
7796 *expr_p = tmp;
7797 ret = GS_ALL_DONE;
7798 break;
7801 case VA_ARG_EXPR:
7802 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
7803 break;
7805 CASE_CONVERT:
7806 if (IS_EMPTY_STMT (*expr_p))
7808 ret = GS_ALL_DONE;
7809 break;
7812 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
7813 || fallback == fb_none)
7815 /* Just strip a conversion to void (or in void context) and
7816 try again. */
7817 *expr_p = TREE_OPERAND (*expr_p, 0);
7818 ret = GS_OK;
7819 break;
7822 ret = gimplify_conversion (expr_p);
7823 if (ret == GS_ERROR)
7824 break;
7825 if (*expr_p != save_expr)
7826 break;
7827 /* FALLTHRU */
7829 case FIX_TRUNC_EXPR:
7830 /* unary_expr: ... | '(' cast ')' val | ... */
7831 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7832 is_gimple_val, fb_rvalue);
7833 recalculate_side_effects (*expr_p);
7834 break;
7836 case INDIRECT_REF:
7838 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7839 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7840 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7842 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7843 if (*expr_p != save_expr)
7845 ret = GS_OK;
7846 break;
7849 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7850 is_gimple_reg, fb_rvalue);
7851 if (ret == GS_ERROR)
7852 break;
7854 recalculate_side_effects (*expr_p);
7855 *expr_p = fold_build2_loc (input_location, MEM_REF,
7856 TREE_TYPE (*expr_p),
7857 TREE_OPERAND (*expr_p, 0),
7858 build_int_cst (saved_ptr_type, 0));
7859 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7860 TREE_THIS_NOTRAP (*expr_p) = notrap;
7861 ret = GS_OK;
7862 break;
7865 /* We arrive here through the various re-gimplifcation paths. */
7866 case MEM_REF:
7867 /* First try re-folding the whole thing. */
7868 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7869 TREE_OPERAND (*expr_p, 0),
7870 TREE_OPERAND (*expr_p, 1));
7871 if (tmp)
7873 *expr_p = tmp;
7874 recalculate_side_effects (*expr_p);
7875 ret = GS_OK;
7876 break;
7878 /* Avoid re-gimplifying the address operand if it is already
7879 in suitable form. Re-gimplifying would mark the address
7880 operand addressable. Always gimplify when not in SSA form
7881 as we still may have to gimplify decls with value-exprs. */
7882 if (!gimplify_ctxp || !gimplify_ctxp->into_ssa
7883 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p, 0)))
7885 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7886 is_gimple_mem_ref_addr, fb_rvalue);
7887 if (ret == GS_ERROR)
7888 break;
7890 recalculate_side_effects (*expr_p);
7891 ret = GS_ALL_DONE;
7892 break;
7894 /* Constants need not be gimplified. */
7895 case INTEGER_CST:
7896 case REAL_CST:
7897 case FIXED_CST:
7898 case STRING_CST:
7899 case COMPLEX_CST:
7900 case VECTOR_CST:
7901 ret = GS_ALL_DONE;
7902 break;
7904 case CONST_DECL:
7905 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7906 CONST_DECL node. Otherwise the decl is replaceable by its
7907 value. */
7908 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7909 if (fallback & fb_lvalue)
7910 ret = GS_ALL_DONE;
7911 else
7913 *expr_p = DECL_INITIAL (*expr_p);
7914 ret = GS_OK;
7916 break;
7918 case DECL_EXPR:
7919 ret = gimplify_decl_expr (expr_p, pre_p);
7920 break;
7922 case BIND_EXPR:
7923 ret = gimplify_bind_expr (expr_p, pre_p);
7924 break;
7926 case LOOP_EXPR:
7927 ret = gimplify_loop_expr (expr_p, pre_p);
7928 break;
7930 case SWITCH_EXPR:
7931 ret = gimplify_switch_expr (expr_p, pre_p);
7932 break;
7934 case EXIT_EXPR:
7935 ret = gimplify_exit_expr (expr_p);
7936 break;
7938 case GOTO_EXPR:
7939 /* If the target is not LABEL, then it is a computed jump
7940 and the target needs to be gimplified. */
7941 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7943 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7944 NULL, is_gimple_val, fb_rvalue);
7945 if (ret == GS_ERROR)
7946 break;
7948 gimplify_seq_add_stmt (pre_p,
7949 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7950 ret = GS_ALL_DONE;
7951 break;
7953 case PREDICT_EXPR:
7954 gimplify_seq_add_stmt (pre_p,
7955 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7956 PREDICT_EXPR_OUTCOME (*expr_p)));
7957 ret = GS_ALL_DONE;
7958 break;
7960 case LABEL_EXPR:
7961 ret = GS_ALL_DONE;
7962 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7963 == current_function_decl);
7964 gimplify_seq_add_stmt (pre_p,
7965 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7966 break;
7968 case CASE_LABEL_EXPR:
7969 ret = gimplify_case_label_expr (expr_p, pre_p);
7970 break;
7972 case RETURN_EXPR:
7973 ret = gimplify_return_expr (*expr_p, pre_p);
7974 break;
7976 case CONSTRUCTOR:
7977 /* Don't reduce this in place; let gimplify_init_constructor work its
7978 magic. Buf if we're just elaborating this for side effects, just
7979 gimplify any element that has side-effects. */
7980 if (fallback == fb_none)
7982 unsigned HOST_WIDE_INT ix;
7983 tree val;
7984 tree temp = NULL_TREE;
7985 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7986 if (TREE_SIDE_EFFECTS (val))
7987 append_to_statement_list (val, &temp);
7989 *expr_p = temp;
7990 ret = temp ? GS_OK : GS_ALL_DONE;
7992 /* C99 code may assign to an array in a constructed
7993 structure or union, and this has undefined behavior only
7994 on execution, so create a temporary if an lvalue is
7995 required. */
7996 else if (fallback == fb_lvalue)
7998 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7999 mark_addressable (*expr_p);
8000 ret = GS_OK;
8002 else
8003 ret = GS_ALL_DONE;
8004 break;
8006 /* The following are special cases that are not handled by the
8007 original GIMPLE grammar. */
8009 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
8010 eliminated. */
8011 case SAVE_EXPR:
8012 ret = gimplify_save_expr (expr_p, pre_p, post_p);
8013 break;
8015 case BIT_FIELD_REF:
8016 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8017 post_p, is_gimple_lvalue, fb_either);
8018 recalculate_side_effects (*expr_p);
8019 break;
8021 case TARGET_MEM_REF:
8023 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
8025 if (TMR_BASE (*expr_p))
8026 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
8027 post_p, is_gimple_mem_ref_addr, fb_either);
8028 if (TMR_INDEX (*expr_p))
8029 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
8030 post_p, is_gimple_val, fb_rvalue);
8031 if (TMR_INDEX2 (*expr_p))
8032 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
8033 post_p, is_gimple_val, fb_rvalue);
8034 /* TMR_STEP and TMR_OFFSET are always integer constants. */
8035 ret = MIN (r0, r1);
8037 break;
8039 case NON_LVALUE_EXPR:
8040 /* This should have been stripped above. */
8041 gcc_unreachable ();
8043 case ASM_EXPR:
8044 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
8045 break;
8047 case TRY_FINALLY_EXPR:
8048 case TRY_CATCH_EXPR:
8050 gimple_seq eval, cleanup;
8051 gimple try_;
8053 /* Calls to destructors are generated automatically in FINALLY/CATCH
8054 block. They should have location as UNKNOWN_LOCATION. However,
8055 gimplify_call_expr will reset these call stmts to input_location
8056 if it finds stmt's location is unknown. To prevent resetting for
8057 destructors, we set the input_location to unknown.
8058 Note that this only affects the destructor calls in FINALLY/CATCH
8059 block, and will automatically reset to its original value by the
8060 end of gimplify_expr. */
8061 input_location = UNKNOWN_LOCATION;
8062 eval = cleanup = NULL;
8063 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
8064 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
8065 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
8066 if (gimple_seq_empty_p (cleanup))
8068 gimple_seq_add_seq (pre_p, eval);
8069 ret = GS_ALL_DONE;
8070 break;
8072 try_ = gimple_build_try (eval, cleanup,
8073 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
8074 ? GIMPLE_TRY_FINALLY
8075 : GIMPLE_TRY_CATCH);
8076 if (LOCATION_LOCUS (saved_location) != UNKNOWN_LOCATION)
8077 gimple_set_location (try_, saved_location);
8078 else
8079 gimple_set_location (try_, EXPR_LOCATION (save_expr));
8080 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
8081 gimple_try_set_catch_is_cleanup (try_,
8082 TRY_CATCH_IS_CLEANUP (*expr_p));
8083 gimplify_seq_add_stmt (pre_p, try_);
8084 ret = GS_ALL_DONE;
8085 break;
8088 case CLEANUP_POINT_EXPR:
8089 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
8090 break;
8092 case TARGET_EXPR:
8093 ret = gimplify_target_expr (expr_p, pre_p, post_p);
8094 break;
8096 case CATCH_EXPR:
8098 gimple c;
8099 gimple_seq handler = NULL;
8100 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
8101 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
8102 gimplify_seq_add_stmt (pre_p, c);
8103 ret = GS_ALL_DONE;
8104 break;
8107 case EH_FILTER_EXPR:
8109 gimple ehf;
8110 gimple_seq failure = NULL;
8112 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
8113 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
8114 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
8115 gimplify_seq_add_stmt (pre_p, ehf);
8116 ret = GS_ALL_DONE;
8117 break;
8120 case OBJ_TYPE_REF:
8122 enum gimplify_status r0, r1;
8123 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
8124 post_p, is_gimple_val, fb_rvalue);
8125 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
8126 post_p, is_gimple_val, fb_rvalue);
8127 TREE_SIDE_EFFECTS (*expr_p) = 0;
8128 ret = MIN (r0, r1);
8130 break;
8132 case LABEL_DECL:
8133 /* We get here when taking the address of a label. We mark
8134 the label as "forced"; meaning it can never be removed and
8135 it is a potential target for any computed goto. */
8136 FORCED_LABEL (*expr_p) = 1;
8137 ret = GS_ALL_DONE;
8138 break;
8140 case STATEMENT_LIST:
8141 ret = gimplify_statement_list (expr_p, pre_p);
8142 break;
8144 case WITH_SIZE_EXPR:
8146 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8147 post_p == &internal_post ? NULL : post_p,
8148 gimple_test_f, fallback);
8149 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8150 is_gimple_val, fb_rvalue);
8151 ret = GS_ALL_DONE;
8153 break;
8155 case VAR_DECL:
8156 case PARM_DECL:
8157 ret = gimplify_var_or_parm_decl (expr_p);
8158 break;
8160 case RESULT_DECL:
8161 /* When within an OpenMP context, notice uses of variables. */
8162 if (gimplify_omp_ctxp)
8163 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
8164 ret = GS_ALL_DONE;
8165 break;
8167 case SSA_NAME:
8168 /* Allow callbacks into the gimplifier during optimization. */
8169 ret = GS_ALL_DONE;
8170 break;
8172 case OMP_PARALLEL:
8173 gimplify_omp_parallel (expr_p, pre_p);
8174 ret = GS_ALL_DONE;
8175 break;
8177 case OMP_TASK:
8178 gimplify_omp_task (expr_p, pre_p);
8179 ret = GS_ALL_DONE;
8180 break;
8182 case OMP_FOR:
8183 case OMP_SIMD:
8184 case OMP_DISTRIBUTE:
8185 ret = gimplify_omp_for (expr_p, pre_p);
8186 break;
8188 case OMP_SECTIONS:
8189 case OMP_SINGLE:
8190 case OMP_TARGET:
8191 case OMP_TARGET_DATA:
8192 case OMP_TEAMS:
8193 gimplify_omp_workshare (expr_p, pre_p);
8194 ret = GS_ALL_DONE;
8195 break;
8197 case OMP_TARGET_UPDATE:
8198 gimplify_omp_target_update (expr_p, pre_p);
8199 ret = GS_ALL_DONE;
8200 break;
8202 case OMP_SECTION:
8203 case OMP_MASTER:
8204 case OMP_TASKGROUP:
8205 case OMP_ORDERED:
8206 case OMP_CRITICAL:
8208 gimple_seq body = NULL;
8209 gimple g;
8211 gimplify_and_add (OMP_BODY (*expr_p), &body);
8212 switch (TREE_CODE (*expr_p))
8214 case OMP_SECTION:
8215 g = gimple_build_omp_section (body);
8216 break;
8217 case OMP_MASTER:
8218 g = gimple_build_omp_master (body);
8219 break;
8220 case OMP_TASKGROUP:
8222 gimple_seq cleanup = NULL;
8223 tree fn
8224 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END);
8225 g = gimple_build_call (fn, 0);
8226 gimple_seq_add_stmt (&cleanup, g);
8227 g = gimple_build_try (body, cleanup, GIMPLE_TRY_FINALLY);
8228 body = NULL;
8229 gimple_seq_add_stmt (&body, g);
8230 g = gimple_build_omp_taskgroup (body);
8232 break;
8233 case OMP_ORDERED:
8234 g = gimple_build_omp_ordered (body);
8235 break;
8236 case OMP_CRITICAL:
8237 g = gimple_build_omp_critical (body,
8238 OMP_CRITICAL_NAME (*expr_p));
8239 break;
8240 default:
8241 gcc_unreachable ();
8243 gimplify_seq_add_stmt (pre_p, g);
8244 ret = GS_ALL_DONE;
8245 break;
8248 case OMP_ATOMIC:
8249 case OMP_ATOMIC_READ:
8250 case OMP_ATOMIC_CAPTURE_OLD:
8251 case OMP_ATOMIC_CAPTURE_NEW:
8252 ret = gimplify_omp_atomic (expr_p, pre_p);
8253 break;
8255 case TRANSACTION_EXPR:
8256 ret = gimplify_transaction (expr_p, pre_p);
8257 break;
8259 case TRUTH_AND_EXPR:
8260 case TRUTH_OR_EXPR:
8261 case TRUTH_XOR_EXPR:
8263 tree orig_type = TREE_TYPE (*expr_p);
8264 tree new_type, xop0, xop1;
8265 *expr_p = gimple_boolify (*expr_p);
8266 new_type = TREE_TYPE (*expr_p);
8267 if (!useless_type_conversion_p (orig_type, new_type))
8269 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
8270 ret = GS_OK;
8271 break;
8274 /* Boolified binary truth expressions are semantically equivalent
8275 to bitwise binary expressions. Canonicalize them to the
8276 bitwise variant. */
8277 switch (TREE_CODE (*expr_p))
8279 case TRUTH_AND_EXPR:
8280 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
8281 break;
8282 case TRUTH_OR_EXPR:
8283 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
8284 break;
8285 case TRUTH_XOR_EXPR:
8286 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
8287 break;
8288 default:
8289 break;
8291 /* Now make sure that operands have compatible type to
8292 expression's new_type. */
8293 xop0 = TREE_OPERAND (*expr_p, 0);
8294 xop1 = TREE_OPERAND (*expr_p, 1);
8295 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
8296 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
8297 new_type,
8298 xop0);
8299 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
8300 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
8301 new_type,
8302 xop1);
8303 /* Continue classified as tcc_binary. */
8304 goto expr_2;
8307 case FMA_EXPR:
8308 case VEC_COND_EXPR:
8309 case VEC_PERM_EXPR:
8310 /* Classified as tcc_expression. */
8311 goto expr_3;
8313 case POINTER_PLUS_EXPR:
8315 enum gimplify_status r0, r1;
8316 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8317 post_p, is_gimple_val, fb_rvalue);
8318 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8319 post_p, is_gimple_val, fb_rvalue);
8320 recalculate_side_effects (*expr_p);
8321 ret = MIN (r0, r1);
8322 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
8323 after gimplifying operands - this is similar to how
8324 it would be folding all gimplified stmts on creation
8325 to have them canonicalized, which is what we eventually
8326 should do anyway. */
8327 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
8328 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
8330 *expr_p = build_fold_addr_expr_with_type_loc
8331 (input_location,
8332 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
8333 TREE_OPERAND (*expr_p, 0),
8334 fold_convert (ptr_type_node,
8335 TREE_OPERAND (*expr_p, 1))),
8336 TREE_TYPE (*expr_p));
8337 ret = MIN (ret, GS_OK);
8339 break;
8342 case CILK_SYNC_STMT:
8344 if (!fn_contains_cilk_spawn_p (cfun))
8346 error_at (EXPR_LOCATION (*expr_p),
8347 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
8348 ret = GS_ERROR;
8350 else
8352 gimplify_cilk_sync (expr_p, pre_p);
8353 ret = GS_ALL_DONE;
8355 break;
8358 default:
8359 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
8361 case tcc_comparison:
8362 /* Handle comparison of objects of non scalar mode aggregates
8363 with a call to memcmp. It would be nice to only have to do
8364 this for variable-sized objects, but then we'd have to allow
8365 the same nest of reference nodes we allow for MODIFY_EXPR and
8366 that's too complex.
8368 Compare scalar mode aggregates as scalar mode values. Using
8369 memcmp for them would be very inefficient at best, and is
8370 plain wrong if bitfields are involved. */
8372 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
8374 /* Vector comparisons need no boolification. */
8375 if (TREE_CODE (type) == VECTOR_TYPE)
8376 goto expr_2;
8377 else if (!AGGREGATE_TYPE_P (type))
8379 tree org_type = TREE_TYPE (*expr_p);
8380 *expr_p = gimple_boolify (*expr_p);
8381 if (!useless_type_conversion_p (org_type,
8382 TREE_TYPE (*expr_p)))
8384 *expr_p = fold_convert_loc (input_location,
8385 org_type, *expr_p);
8386 ret = GS_OK;
8388 else
8389 goto expr_2;
8391 else if (TYPE_MODE (type) != BLKmode)
8392 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
8393 else
8394 ret = gimplify_variable_sized_compare (expr_p);
8396 break;
8399 /* If *EXPR_P does not need to be special-cased, handle it
8400 according to its class. */
8401 case tcc_unary:
8402 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8403 post_p, is_gimple_val, fb_rvalue);
8404 break;
8406 case tcc_binary:
8407 expr_2:
8409 enum gimplify_status r0, r1;
8411 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8412 post_p, is_gimple_val, fb_rvalue);
8413 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8414 post_p, is_gimple_val, fb_rvalue);
8416 ret = MIN (r0, r1);
8417 break;
8420 expr_3:
8422 enum gimplify_status r0, r1, r2;
8424 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
8425 post_p, is_gimple_val, fb_rvalue);
8426 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
8427 post_p, is_gimple_val, fb_rvalue);
8428 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
8429 post_p, is_gimple_val, fb_rvalue);
8431 ret = MIN (MIN (r0, r1), r2);
8432 break;
8435 case tcc_declaration:
8436 case tcc_constant:
8437 ret = GS_ALL_DONE;
8438 goto dont_recalculate;
8440 default:
8441 gcc_unreachable ();
8444 recalculate_side_effects (*expr_p);
8446 dont_recalculate:
8447 break;
8450 gcc_assert (*expr_p || ret != GS_OK);
8452 while (ret == GS_OK);
8454 /* If we encountered an error_mark somewhere nested inside, either
8455 stub out the statement or propagate the error back out. */
8456 if (ret == GS_ERROR)
8458 if (is_statement)
8459 *expr_p = NULL;
8460 goto out;
8463 /* This was only valid as a return value from the langhook, which
8464 we handled. Make sure it doesn't escape from any other context. */
8465 gcc_assert (ret != GS_UNHANDLED);
8467 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
8469 /* We aren't looking for a value, and we don't have a valid
8470 statement. If it doesn't have side-effects, throw it away. */
8471 if (!TREE_SIDE_EFFECTS (*expr_p))
8472 *expr_p = NULL;
8473 else if (!TREE_THIS_VOLATILE (*expr_p))
8475 /* This is probably a _REF that contains something nested that
8476 has side effects. Recurse through the operands to find it. */
8477 enum tree_code code = TREE_CODE (*expr_p);
8479 switch (code)
8481 case COMPONENT_REF:
8482 case REALPART_EXPR:
8483 case IMAGPART_EXPR:
8484 case VIEW_CONVERT_EXPR:
8485 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8486 gimple_test_f, fallback);
8487 break;
8489 case ARRAY_REF:
8490 case ARRAY_RANGE_REF:
8491 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
8492 gimple_test_f, fallback);
8493 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
8494 gimple_test_f, fallback);
8495 break;
8497 default:
8498 /* Anything else with side-effects must be converted to
8499 a valid statement before we get here. */
8500 gcc_unreachable ();
8503 *expr_p = NULL;
8505 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
8506 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
8508 /* Historically, the compiler has treated a bare reference
8509 to a non-BLKmode volatile lvalue as forcing a load. */
8510 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
8512 /* Normally, we do not want to create a temporary for a
8513 TREE_ADDRESSABLE type because such a type should not be
8514 copied by bitwise-assignment. However, we make an
8515 exception here, as all we are doing here is ensuring that
8516 we read the bytes that make up the type. We use
8517 create_tmp_var_raw because create_tmp_var will abort when
8518 given a TREE_ADDRESSABLE type. */
8519 tree tmp = create_tmp_var_raw (type, "vol");
8520 gimple_add_tmp_var (tmp);
8521 gimplify_assign (tmp, *expr_p, pre_p);
8522 *expr_p = NULL;
8524 else
8525 /* We can't do anything useful with a volatile reference to
8526 an incomplete type, so just throw it away. Likewise for
8527 a BLKmode type, since any implicit inner load should
8528 already have been turned into an explicit one by the
8529 gimplification process. */
8530 *expr_p = NULL;
8533 /* If we are gimplifying at the statement level, we're done. Tack
8534 everything together and return. */
8535 if (fallback == fb_none || is_statement)
8537 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
8538 it out for GC to reclaim it. */
8539 *expr_p = NULL_TREE;
8541 if (!gimple_seq_empty_p (internal_pre)
8542 || !gimple_seq_empty_p (internal_post))
8544 gimplify_seq_add_seq (&internal_pre, internal_post);
8545 gimplify_seq_add_seq (pre_p, internal_pre);
8548 /* The result of gimplifying *EXPR_P is going to be the last few
8549 statements in *PRE_P and *POST_P. Add location information
8550 to all the statements that were added by the gimplification
8551 helpers. */
8552 if (!gimple_seq_empty_p (*pre_p))
8553 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
8555 if (!gimple_seq_empty_p (*post_p))
8556 annotate_all_with_location_after (*post_p, post_last_gsi,
8557 input_location);
8559 goto out;
8562 #ifdef ENABLE_GIMPLE_CHECKING
8563 if (*expr_p)
8565 enum tree_code code = TREE_CODE (*expr_p);
8566 /* These expressions should already be in gimple IR form. */
8567 gcc_assert (code != MODIFY_EXPR
8568 && code != ASM_EXPR
8569 && code != BIND_EXPR
8570 && code != CATCH_EXPR
8571 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
8572 && code != EH_FILTER_EXPR
8573 && code != GOTO_EXPR
8574 && code != LABEL_EXPR
8575 && code != LOOP_EXPR
8576 && code != SWITCH_EXPR
8577 && code != TRY_FINALLY_EXPR
8578 && code != OMP_CRITICAL
8579 && code != OMP_FOR
8580 && code != OMP_MASTER
8581 && code != OMP_TASKGROUP
8582 && code != OMP_ORDERED
8583 && code != OMP_PARALLEL
8584 && code != OMP_SECTIONS
8585 && code != OMP_SECTION
8586 && code != OMP_SINGLE);
8588 #endif
8590 /* Otherwise we're gimplifying a subexpression, so the resulting
8591 value is interesting. If it's a valid operand that matches
8592 GIMPLE_TEST_F, we're done. Unless we are handling some
8593 post-effects internally; if that's the case, we need to copy into
8594 a temporary before adding the post-effects to POST_P. */
8595 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
8596 goto out;
8598 /* Otherwise, we need to create a new temporary for the gimplified
8599 expression. */
8601 /* We can't return an lvalue if we have an internal postqueue. The
8602 object the lvalue refers to would (probably) be modified by the
8603 postqueue; we need to copy the value out first, which means an
8604 rvalue. */
8605 if ((fallback & fb_lvalue)
8606 && gimple_seq_empty_p (internal_post)
8607 && is_gimple_addressable (*expr_p))
8609 /* An lvalue will do. Take the address of the expression, store it
8610 in a temporary, and replace the expression with an INDIRECT_REF of
8611 that temporary. */
8612 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
8613 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
8614 *expr_p = build_simple_mem_ref (tmp);
8616 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
8618 /* An rvalue will do. Assign the gimplified expression into a
8619 new temporary TMP and replace the original expression with
8620 TMP. First, make sure that the expression has a type so that
8621 it can be assigned into a temporary. */
8622 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
8623 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
8625 else
8627 #ifdef ENABLE_GIMPLE_CHECKING
8628 if (!(fallback & fb_mayfail))
8630 fprintf (stderr, "gimplification failed:\n");
8631 print_generic_expr (stderr, *expr_p, 0);
8632 debug_tree (*expr_p);
8633 internal_error ("gimplification failed");
8635 #endif
8636 gcc_assert (fallback & fb_mayfail);
8638 /* If this is an asm statement, and the user asked for the
8639 impossible, don't die. Fail and let gimplify_asm_expr
8640 issue an error. */
8641 ret = GS_ERROR;
8642 goto out;
8645 /* Make sure the temporary matches our predicate. */
8646 gcc_assert ((*gimple_test_f) (*expr_p));
8648 if (!gimple_seq_empty_p (internal_post))
8650 annotate_all_with_location (internal_post, input_location);
8651 gimplify_seq_add_seq (pre_p, internal_post);
8654 out:
8655 input_location = saved_location;
8656 return ret;
8659 /* Look through TYPE for variable-sized objects and gimplify each such
8660 size that we find. Add to LIST_P any statements generated. */
8662 void
8663 gimplify_type_sizes (tree type, gimple_seq *list_p)
8665 tree field, t;
8667 if (type == NULL || type == error_mark_node)
8668 return;
8670 /* We first do the main variant, then copy into any other variants. */
8671 type = TYPE_MAIN_VARIANT (type);
8673 /* Avoid infinite recursion. */
8674 if (TYPE_SIZES_GIMPLIFIED (type))
8675 return;
8677 TYPE_SIZES_GIMPLIFIED (type) = 1;
8679 switch (TREE_CODE (type))
8681 case INTEGER_TYPE:
8682 case ENUMERAL_TYPE:
8683 case BOOLEAN_TYPE:
8684 case REAL_TYPE:
8685 case FIXED_POINT_TYPE:
8686 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
8687 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
8689 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8691 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
8692 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
8694 break;
8696 case ARRAY_TYPE:
8697 /* These types may not have declarations, so handle them here. */
8698 gimplify_type_sizes (TREE_TYPE (type), list_p);
8699 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
8700 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
8701 with assigned stack slots, for -O1+ -g they should be tracked
8702 by VTA. */
8703 if (!(TYPE_NAME (type)
8704 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8705 && DECL_IGNORED_P (TYPE_NAME (type)))
8706 && TYPE_DOMAIN (type)
8707 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
8709 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8710 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8711 DECL_IGNORED_P (t) = 0;
8712 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
8713 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
8714 DECL_IGNORED_P (t) = 0;
8716 break;
8718 case RECORD_TYPE:
8719 case UNION_TYPE:
8720 case QUAL_UNION_TYPE:
8721 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8722 if (TREE_CODE (field) == FIELD_DECL)
8724 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
8725 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
8726 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
8727 gimplify_type_sizes (TREE_TYPE (field), list_p);
8729 break;
8731 case POINTER_TYPE:
8732 case REFERENCE_TYPE:
8733 /* We used to recurse on the pointed-to type here, which turned out to
8734 be incorrect because its definition might refer to variables not
8735 yet initialized at this point if a forward declaration is involved.
8737 It was actually useful for anonymous pointed-to types to ensure
8738 that the sizes evaluation dominates every possible later use of the
8739 values. Restricting to such types here would be safe since there
8740 is no possible forward declaration around, but would introduce an
8741 undesirable middle-end semantic to anonymity. We then defer to
8742 front-ends the responsibility of ensuring that the sizes are
8743 evaluated both early and late enough, e.g. by attaching artificial
8744 type declarations to the tree. */
8745 break;
8747 default:
8748 break;
8751 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
8752 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
8754 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8756 TYPE_SIZE (t) = TYPE_SIZE (type);
8757 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
8758 TYPE_SIZES_GIMPLIFIED (t) = 1;
8762 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
8763 a size or position, has had all of its SAVE_EXPRs evaluated.
8764 We add any required statements to *STMT_P. */
8766 void
8767 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
8769 tree expr = *expr_p;
8771 /* We don't do anything if the value isn't there, is constant, or contains
8772 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
8773 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
8774 will want to replace it with a new variable, but that will cause problems
8775 if this type is from outside the function. It's OK to have that here. */
8776 if (is_gimple_sizepos (expr))
8777 return;
8779 *expr_p = unshare_expr (expr);
8781 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
8784 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
8785 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
8786 is true, also gimplify the parameters. */
8788 gimple
8789 gimplify_body (tree fndecl, bool do_parms)
8791 location_t saved_location = input_location;
8792 gimple_seq parm_stmts, seq;
8793 gimple outer_bind;
8794 struct gimplify_ctx gctx;
8795 struct cgraph_node *cgn;
8797 timevar_push (TV_TREE_GIMPLIFY);
8799 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
8800 gimplification. */
8801 default_rtl_profile ();
8803 gcc_assert (gimplify_ctxp == NULL);
8804 push_gimplify_context (&gctx);
8806 if (flag_openmp)
8808 gcc_assert (gimplify_omp_ctxp == NULL);
8809 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl)))
8810 gimplify_omp_ctxp = new_omp_context (ORT_TARGET);
8813 /* Unshare most shared trees in the body and in that of any nested functions.
8814 It would seem we don't have to do this for nested functions because
8815 they are supposed to be output and then the outer function gimplified
8816 first, but the g++ front end doesn't always do it that way. */
8817 unshare_body (fndecl);
8818 unvisit_body (fndecl);
8820 cgn = cgraph_get_node (fndecl);
8821 if (cgn && cgn->origin)
8822 nonlocal_vlas = pointer_set_create ();
8824 /* Make sure input_location isn't set to something weird. */
8825 input_location = DECL_SOURCE_LOCATION (fndecl);
8827 /* Resolve callee-copies. This has to be done before processing
8828 the body so that DECL_VALUE_EXPR gets processed correctly. */
8829 parm_stmts = do_parms ? gimplify_parameters () : NULL;
8831 /* Gimplify the function's body. */
8832 seq = NULL;
8833 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
8834 outer_bind = gimple_seq_first_stmt (seq);
8835 if (!outer_bind)
8837 outer_bind = gimple_build_nop ();
8838 gimplify_seq_add_stmt (&seq, outer_bind);
8841 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8842 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8843 if (gimple_code (outer_bind) == GIMPLE_BIND
8844 && gimple_seq_first (seq) == gimple_seq_last (seq))
8846 else
8847 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8849 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8851 /* If we had callee-copies statements, insert them at the beginning
8852 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8853 if (!gimple_seq_empty_p (parm_stmts))
8855 tree parm;
8857 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8858 gimple_bind_set_body (outer_bind, parm_stmts);
8860 for (parm = DECL_ARGUMENTS (current_function_decl);
8861 parm; parm = DECL_CHAIN (parm))
8862 if (DECL_HAS_VALUE_EXPR_P (parm))
8864 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8865 DECL_IGNORED_P (parm) = 0;
8869 if (nonlocal_vlas)
8871 pointer_set_destroy (nonlocal_vlas);
8872 nonlocal_vlas = NULL;
8875 if (flag_openmp && gimplify_omp_ctxp)
8877 delete_omp_context (gimplify_omp_ctxp);
8878 gimplify_omp_ctxp = NULL;
8881 pop_gimplify_context (outer_bind);
8882 gcc_assert (gimplify_ctxp == NULL);
8884 #ifdef ENABLE_CHECKING
8885 if (!seen_error ())
8886 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8887 #endif
8889 timevar_pop (TV_TREE_GIMPLIFY);
8890 input_location = saved_location;
8892 return outer_bind;
8895 typedef char *char_p; /* For DEF_VEC_P. */
8897 /* Return whether we should exclude FNDECL from instrumentation. */
8899 static bool
8900 flag_instrument_functions_exclude_p (tree fndecl)
8902 vec<char_p> *v;
8904 v = (vec<char_p> *) flag_instrument_functions_exclude_functions;
8905 if (v && v->length () > 0)
8907 const char *name;
8908 int i;
8909 char *s;
8911 name = lang_hooks.decl_printable_name (fndecl, 0);
8912 FOR_EACH_VEC_ELT (*v, i, s)
8913 if (strstr (name, s) != NULL)
8914 return true;
8917 v = (vec<char_p> *) flag_instrument_functions_exclude_files;
8918 if (v && v->length () > 0)
8920 const char *name;
8921 int i;
8922 char *s;
8924 name = DECL_SOURCE_FILE (fndecl);
8925 FOR_EACH_VEC_ELT (*v, i, s)
8926 if (strstr (name, s) != NULL)
8927 return true;
8930 return false;
8933 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8934 node for the function we want to gimplify.
8936 Return the sequence of GIMPLE statements corresponding to the body
8937 of FNDECL. */
8939 void
8940 gimplify_function_tree (tree fndecl)
8942 tree parm, ret;
8943 gimple_seq seq;
8944 gimple bind;
8946 gcc_assert (!gimple_body (fndecl));
8948 if (DECL_STRUCT_FUNCTION (fndecl))
8949 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8950 else
8951 push_struct_function (fndecl);
8953 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8955 /* Preliminarily mark non-addressed complex variables as eligible
8956 for promotion to gimple registers. We'll transform their uses
8957 as we find them. */
8958 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8959 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8960 && !TREE_THIS_VOLATILE (parm)
8961 && !needs_to_live_in_memory (parm))
8962 DECL_GIMPLE_REG_P (parm) = 1;
8965 ret = DECL_RESULT (fndecl);
8966 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8967 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8968 && !needs_to_live_in_memory (ret))
8969 DECL_GIMPLE_REG_P (ret) = 1;
8971 bind = gimplify_body (fndecl, true);
8973 /* The tree body of the function is no longer needed, replace it
8974 with the new GIMPLE body. */
8975 seq = NULL;
8976 gimple_seq_add_stmt (&seq, bind);
8977 gimple_set_body (fndecl, seq);
8979 /* If we're instrumenting function entry/exit, then prepend the call to
8980 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8981 catch the exit hook. */
8982 /* ??? Add some way to ignore exceptions for this TFE. */
8983 if (flag_instrument_function_entry_exit
8984 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8985 && !flag_instrument_functions_exclude_p (fndecl))
8987 tree x;
8988 gimple new_bind;
8989 gimple tf;
8990 gimple_seq cleanup = NULL, body = NULL;
8991 tree tmp_var;
8992 gimple call;
8994 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8995 call = gimple_build_call (x, 1, integer_zero_node);
8996 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8997 gimple_call_set_lhs (call, tmp_var);
8998 gimplify_seq_add_stmt (&cleanup, call);
8999 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
9000 call = gimple_build_call (x, 2,
9001 build_fold_addr_expr (current_function_decl),
9002 tmp_var);
9003 gimplify_seq_add_stmt (&cleanup, call);
9004 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
9006 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
9007 call = gimple_build_call (x, 1, integer_zero_node);
9008 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
9009 gimple_call_set_lhs (call, tmp_var);
9010 gimplify_seq_add_stmt (&body, call);
9011 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
9012 call = gimple_build_call (x, 2,
9013 build_fold_addr_expr (current_function_decl),
9014 tmp_var);
9015 gimplify_seq_add_stmt (&body, call);
9016 gimplify_seq_add_stmt (&body, tf);
9017 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
9018 /* Clear the block for BIND, since it is no longer directly inside
9019 the function, but within a try block. */
9020 gimple_bind_set_block (bind, NULL);
9022 /* Replace the current function body with the body
9023 wrapped in the try/finally TF. */
9024 seq = NULL;
9025 gimple_seq_add_stmt (&seq, new_bind);
9026 gimple_set_body (fndecl, seq);
9029 DECL_SAVED_TREE (fndecl) = NULL_TREE;
9030 cfun->curr_properties = PROP_gimple_any;
9032 pop_cfun ();
9035 /* Some transformations like inlining may invalidate the GIMPLE form
9036 for operands. This function traverses all the operands in STMT and
9037 gimplifies anything that is not a valid gimple operand. Any new
9038 GIMPLE statements are inserted before *GSI_P. */
9040 void
9041 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
9043 size_t i, num_ops;
9044 tree lhs;
9045 gimple_seq pre = NULL;
9046 gimple post_stmt = NULL;
9047 struct gimplify_ctx gctx;
9049 push_gimplify_context (&gctx);
9050 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
9052 switch (gimple_code (stmt))
9054 case GIMPLE_COND:
9055 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
9056 is_gimple_val, fb_rvalue);
9057 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
9058 is_gimple_val, fb_rvalue);
9059 break;
9060 case GIMPLE_SWITCH:
9061 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
9062 is_gimple_val, fb_rvalue);
9063 break;
9064 case GIMPLE_OMP_ATOMIC_LOAD:
9065 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
9066 is_gimple_val, fb_rvalue);
9067 break;
9068 case GIMPLE_ASM:
9070 size_t i, noutputs = gimple_asm_noutputs (stmt);
9071 const char *constraint, **oconstraints;
9072 bool allows_mem, allows_reg, is_inout;
9074 oconstraints
9075 = (const char **) alloca ((noutputs) * sizeof (const char *));
9076 for (i = 0; i < noutputs; i++)
9078 tree op = gimple_asm_output_op (stmt, i);
9079 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
9080 oconstraints[i] = constraint;
9081 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
9082 &allows_reg, &is_inout);
9083 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9084 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
9085 fb_lvalue | fb_mayfail);
9087 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
9089 tree op = gimple_asm_input_op (stmt, i);
9090 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
9091 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
9092 oconstraints, &allows_mem, &allows_reg);
9093 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
9094 allows_reg = 0;
9095 if (!allows_reg && allows_mem)
9096 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9097 is_gimple_lvalue, fb_lvalue | fb_mayfail);
9098 else
9099 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
9100 is_gimple_asm_val, fb_rvalue);
9103 break;
9104 default:
9105 /* NOTE: We start gimplifying operands from last to first to
9106 make sure that side-effects on the RHS of calls, assignments
9107 and ASMs are executed before the LHS. The ordering is not
9108 important for other statements. */
9109 num_ops = gimple_num_ops (stmt);
9110 for (i = num_ops; i > 0; i--)
9112 tree op = gimple_op (stmt, i - 1);
9113 if (op == NULL_TREE)
9114 continue;
9115 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
9116 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
9117 else if (i == 2
9118 && is_gimple_assign (stmt)
9119 && num_ops == 2
9120 && get_gimple_rhs_class (gimple_expr_code (stmt))
9121 == GIMPLE_SINGLE_RHS)
9122 gimplify_expr (&op, &pre, NULL,
9123 rhs_predicate_for (gimple_assign_lhs (stmt)),
9124 fb_rvalue);
9125 else if (i == 2 && is_gimple_call (stmt))
9127 if (TREE_CODE (op) == FUNCTION_DECL)
9128 continue;
9129 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
9131 else
9132 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
9133 gimple_set_op (stmt, i - 1, op);
9136 lhs = gimple_get_lhs (stmt);
9137 /* If the LHS changed it in a way that requires a simple RHS,
9138 create temporary. */
9139 if (lhs && !is_gimple_reg (lhs))
9141 bool need_temp = false;
9143 if (is_gimple_assign (stmt)
9144 && num_ops == 2
9145 && get_gimple_rhs_class (gimple_expr_code (stmt))
9146 == GIMPLE_SINGLE_RHS)
9147 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
9148 rhs_predicate_for (gimple_assign_lhs (stmt)),
9149 fb_rvalue);
9150 else if (is_gimple_reg (lhs))
9152 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9154 if (is_gimple_call (stmt))
9156 i = gimple_call_flags (stmt);
9157 if ((i & ECF_LOOPING_CONST_OR_PURE)
9158 || !(i & (ECF_CONST | ECF_PURE)))
9159 need_temp = true;
9161 if (stmt_can_throw_internal (stmt))
9162 need_temp = true;
9165 else
9167 if (is_gimple_reg_type (TREE_TYPE (lhs)))
9168 need_temp = true;
9169 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
9171 if (is_gimple_call (stmt))
9173 tree fndecl = gimple_call_fndecl (stmt);
9175 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
9176 && !(fndecl && DECL_RESULT (fndecl)
9177 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
9178 need_temp = true;
9180 else
9181 need_temp = true;
9184 if (need_temp)
9186 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
9187 if (gimple_in_ssa_p (cfun))
9188 temp = make_ssa_name (temp, NULL);
9189 gimple_set_lhs (stmt, temp);
9190 post_stmt = gimple_build_assign (lhs, temp);
9191 if (TREE_CODE (lhs) == SSA_NAME)
9192 SSA_NAME_DEF_STMT (lhs) = post_stmt;
9195 break;
9198 if (!gimple_seq_empty_p (pre))
9199 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
9200 if (post_stmt)
9201 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
9203 pop_gimplify_context (NULL);
9206 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
9207 the predicate that will hold for the result. If VAR is not NULL, make the
9208 base variable of the final destination be VAR if suitable. */
9210 tree
9211 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
9212 gimple_predicate gimple_test_f, tree var)
9214 enum gimplify_status ret;
9215 struct gimplify_ctx gctx;
9216 location_t saved_location;
9218 *stmts = NULL;
9220 /* gimple_test_f might be more strict than is_gimple_val, make
9221 sure we pass both. Just checking gimple_test_f doesn't work
9222 because most gimple predicates do not work recursively. */
9223 if (is_gimple_val (expr)
9224 && (*gimple_test_f) (expr))
9225 return expr;
9227 push_gimplify_context (&gctx);
9228 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
9229 gimplify_ctxp->allow_rhs_cond_expr = true;
9230 saved_location = input_location;
9231 input_location = UNKNOWN_LOCATION;
9233 if (var)
9235 if (gimplify_ctxp->into_ssa
9236 && is_gimple_reg (var))
9237 var = make_ssa_name (var, NULL);
9238 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
9241 if (TREE_CODE (expr) != MODIFY_EXPR
9242 && TREE_TYPE (expr) == void_type_node)
9244 gimplify_and_add (expr, stmts);
9245 expr = NULL_TREE;
9247 else
9249 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
9250 gcc_assert (ret != GS_ERROR);
9253 input_location = saved_location;
9254 pop_gimplify_context (NULL);
9256 return expr;
9259 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
9260 force the result to be either ssa_name or an invariant, otherwise
9261 just force it to be a rhs expression. If VAR is not NULL, make the
9262 base variable of the final destination be VAR if suitable. */
9264 tree
9265 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
9267 return force_gimple_operand_1 (expr, stmts,
9268 simple ? is_gimple_val : is_gimple_reg_rhs,
9269 var);
9272 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
9273 and VAR. If some statements are produced, emits them at GSI.
9274 If BEFORE is true. the statements are appended before GSI, otherwise
9275 they are appended after it. M specifies the way GSI moves after
9276 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
9278 tree
9279 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
9280 gimple_predicate gimple_test_f,
9281 tree var, bool before,
9282 enum gsi_iterator_update m)
9284 gimple_seq stmts;
9286 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
9288 if (!gimple_seq_empty_p (stmts))
9290 if (before)
9291 gsi_insert_seq_before (gsi, stmts, m);
9292 else
9293 gsi_insert_seq_after (gsi, stmts, m);
9296 return expr;
9299 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
9300 If SIMPLE is true, force the result to be either ssa_name or an invariant,
9301 otherwise just force it to be a rhs expression. If some statements are
9302 produced, emits them at GSI. If BEFORE is true, the statements are
9303 appended before GSI, otherwise they are appended after it. M specifies
9304 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
9305 are the usual values). */
9307 tree
9308 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
9309 bool simple_p, tree var, bool before,
9310 enum gsi_iterator_update m)
9312 return force_gimple_operand_gsi_1 (gsi, expr,
9313 simple_p
9314 ? is_gimple_val : is_gimple_reg_rhs,
9315 var, before, m);
9318 /* Return a dummy expression of type TYPE in order to keep going after an
9319 error. */
9321 static tree
9322 dummy_object (tree type)
9324 tree t = build_int_cst (build_pointer_type (type), 0);
9325 return build2 (MEM_REF, type, t, t);
9328 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
9329 builtin function, but a very special sort of operator. */
9331 enum gimplify_status
9332 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
9334 tree promoted_type, have_va_type;
9335 tree valist = TREE_OPERAND (*expr_p, 0);
9336 tree type = TREE_TYPE (*expr_p);
9337 tree t;
9338 location_t loc = EXPR_LOCATION (*expr_p);
9340 /* Verify that valist is of the proper type. */
9341 have_va_type = TREE_TYPE (valist);
9342 if (have_va_type == error_mark_node)
9343 return GS_ERROR;
9344 have_va_type = targetm.canonical_va_list_type (have_va_type);
9346 if (have_va_type == NULL_TREE)
9348 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
9349 return GS_ERROR;
9352 /* Generate a diagnostic for requesting data of a type that cannot
9353 be passed through `...' due to type promotion at the call site. */
9354 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
9355 != type)
9357 static bool gave_help;
9358 bool warned;
9360 /* Unfortunately, this is merely undefined, rather than a constraint
9361 violation, so we cannot make this an error. If this call is never
9362 executed, the program is still strictly conforming. */
9363 warned = warning_at (loc, 0,
9364 "%qT is promoted to %qT when passed through %<...%>",
9365 type, promoted_type);
9366 if (!gave_help && warned)
9368 gave_help = true;
9369 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
9370 promoted_type, type);
9373 /* We can, however, treat "undefined" any way we please.
9374 Call abort to encourage the user to fix the program. */
9375 if (warned)
9376 inform (loc, "if this code is reached, the program will abort");
9377 /* Before the abort, allow the evaluation of the va_list
9378 expression to exit or longjmp. */
9379 gimplify_and_add (valist, pre_p);
9380 t = build_call_expr_loc (loc,
9381 builtin_decl_implicit (BUILT_IN_TRAP), 0);
9382 gimplify_and_add (t, pre_p);
9384 /* This is dead code, but go ahead and finish so that the
9385 mode of the result comes out right. */
9386 *expr_p = dummy_object (type);
9387 return GS_ALL_DONE;
9389 else
9391 /* Make it easier for the backends by protecting the valist argument
9392 from multiple evaluations. */
9393 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
9395 /* For this case, the backends will be expecting a pointer to
9396 TREE_TYPE (abi), but it's possible we've
9397 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
9398 So fix it. */
9399 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
9401 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
9402 valist = fold_convert_loc (loc, p1,
9403 build_fold_addr_expr_loc (loc, valist));
9406 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
9408 else
9409 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
9411 if (!targetm.gimplify_va_arg_expr)
9412 /* FIXME: Once most targets are converted we should merely
9413 assert this is non-null. */
9414 return GS_ALL_DONE;
9416 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
9417 return GS_OK;
9421 #include "gt-gimplify.h"