1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
5 Major work done by Sebastian Pop <s.pop@laposte.net>,
6 Diego Novillo <dnovillo@redhat.com> and Jason Merrill <jason@redhat.com>.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "tree-pretty-print.h"
33 #include "langhooks.h"
34 #include "tree-flow.h"
42 #include "diagnostic-core.h"
44 #include "pointer-set.h"
45 #include "splay-tree.h"
48 #include "tree-pass.h"
50 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name. */
51 #include "expr.h" /* FIXME: for can_move_by_pieces
52 and STACK_CHECK_MAX_VAR_SIZE. */
54 enum gimplify_omp_var_data
60 GOVD_FIRSTPRIVATE = 16,
61 GOVD_LASTPRIVATE = 32,
64 GOVD_DEBUG_PRIVATE = 256,
65 GOVD_PRIVATE_OUTER_REF = 512,
66 GOVD_DATA_SHARE_CLASS = (GOVD_SHARED | GOVD_PRIVATE | GOVD_FIRSTPRIVATE
67 | GOVD_LASTPRIVATE | GOVD_REDUCTION | GOVD_LOCAL)
75 ORT_COMBINED_PARALLEL = 3,
80 struct gimplify_omp_ctx
82 struct gimplify_omp_ctx *outer_context;
84 struct pointer_set_t *privatized_types;
86 enum omp_clause_default_kind default_kind;
87 enum omp_region_type region_type;
90 static struct gimplify_ctx *gimplify_ctxp;
91 static struct gimplify_omp_ctx *gimplify_omp_ctxp;
94 /* Formal (expression) temporary table handling: multiple occurrences of
95 the same scalar expression are evaluated into the same temporary. */
97 typedef struct gimple_temp_hash_elt
100 tree temp; /* Value */
103 /* Forward declaration. */
104 static enum gimplify_status gimplify_compound_expr (tree *, gimple_seq *, bool);
106 /* Mark X addressable. Unlike the langhook we expect X to be in gimple
107 form and we don't do any syntax checking. */
110 mark_addressable (tree x)
112 while (handled_component_p (x))
113 x = TREE_OPERAND (x, 0);
114 if (TREE_CODE (x) == MEM_REF
115 && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
116 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
117 if (TREE_CODE (x) != VAR_DECL
118 && TREE_CODE (x) != PARM_DECL
119 && TREE_CODE (x) != RESULT_DECL)
121 TREE_ADDRESSABLE (x) = 1;
124 /* Return a hash value for a formal temporary table entry. */
127 gimple_tree_hash (const void *p)
129 tree t = ((const elt_t *) p)->val;
130 return iterative_hash_expr (t, 0);
133 /* Compare two formal temporary table entries. */
136 gimple_tree_eq (const void *p1, const void *p2)
138 tree t1 = ((const elt_t *) p1)->val;
139 tree t2 = ((const elt_t *) p2)->val;
140 enum tree_code code = TREE_CODE (t1);
142 if (TREE_CODE (t2) != code
143 || TREE_TYPE (t1) != TREE_TYPE (t2))
146 if (!operand_equal_p (t1, t2, 0))
149 #ifdef ENABLE_CHECKING
150 /* Only allow them to compare equal if they also hash equal; otherwise
151 results are nondeterminate, and we fail bootstrap comparison. */
152 gcc_assert (gimple_tree_hash (p1) == gimple_tree_hash (p2));
158 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
159 *SEQ_P is NULL, a new sequence is allocated. This function is
160 similar to gimple_seq_add_stmt, but does not scan the operands.
161 During gimplification, we need to manipulate statement sequences
162 before the def/use vectors have been constructed. */
165 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
167 gimple_stmt_iterator si;
173 *seq_p = gimple_seq_alloc ();
175 si = gsi_last (*seq_p);
177 gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
180 /* Shorter alias name for the above function for use in gimplify.c
184 gimplify_seq_add_stmt (gimple_seq *seq_p, gimple gs)
186 gimple_seq_add_stmt_without_update (seq_p, gs);
189 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
190 NULL, a new sequence is allocated. This function is
191 similar to gimple_seq_add_seq, but does not scan the operands.
192 During gimplification, we need to manipulate statement sequences
193 before the def/use vectors have been constructed. */
196 gimplify_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
198 gimple_stmt_iterator si;
204 *dst_p = gimple_seq_alloc ();
206 si = gsi_last (*dst_p);
207 gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
210 /* Set up a context for the gimplifier. */
213 push_gimplify_context (struct gimplify_ctx *c)
215 memset (c, '\0', sizeof (*c));
216 c->prev_context = gimplify_ctxp;
220 /* Tear down a context for the gimplifier. If BODY is non-null, then
221 put the temporaries into the outer BIND_EXPR. Otherwise, put them
224 BODY is not a sequence, but the first tuple in a sequence. */
227 pop_gimplify_context (gimple body)
229 struct gimplify_ctx *c = gimplify_ctxp;
231 gcc_assert (c && (c->bind_expr_stack == NULL
232 || VEC_empty (gimple, c->bind_expr_stack)));
233 VEC_free (gimple, heap, c->bind_expr_stack);
234 gimplify_ctxp = c->prev_context;
237 declare_vars (c->temps, body, false);
239 record_vars (c->temps);
242 htab_delete (c->temp_htab);
245 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
248 gimple_push_bind_expr (gimple gimple_bind)
250 if (gimplify_ctxp->bind_expr_stack == NULL)
251 gimplify_ctxp->bind_expr_stack = VEC_alloc (gimple, heap, 8);
252 VEC_safe_push (gimple, heap, gimplify_ctxp->bind_expr_stack, gimple_bind);
255 /* Pop the first element off the stack of bindings. */
258 gimple_pop_bind_expr (void)
260 VEC_pop (gimple, gimplify_ctxp->bind_expr_stack);
263 /* Return the first element of the stack of bindings. */
266 gimple_current_bind_expr (void)
268 return VEC_last (gimple, gimplify_ctxp->bind_expr_stack);
271 /* Return the stack of bindings created during gimplification. */
274 gimple_bind_expr_stack (void)
276 return gimplify_ctxp->bind_expr_stack;
279 /* Return true iff there is a COND_EXPR between us and the innermost
280 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
283 gimple_conditional_context (void)
285 return gimplify_ctxp->conditions > 0;
288 /* Note that we've entered a COND_EXPR. */
291 gimple_push_condition (void)
293 #ifdef ENABLE_GIMPLE_CHECKING
294 if (gimplify_ctxp->conditions == 0)
295 gcc_assert (gimple_seq_empty_p (gimplify_ctxp->conditional_cleanups));
297 ++(gimplify_ctxp->conditions);
300 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
301 now, add any conditional cleanups we've seen to the prequeue. */
304 gimple_pop_condition (gimple_seq *pre_p)
306 int conds = --(gimplify_ctxp->conditions);
308 gcc_assert (conds >= 0);
311 gimplify_seq_add_seq (pre_p, gimplify_ctxp->conditional_cleanups);
312 gimplify_ctxp->conditional_cleanups = NULL;
316 /* A stable comparison routine for use with splay trees and DECLs. */
319 splay_tree_compare_decl_uid (splay_tree_key xa, splay_tree_key xb)
324 return DECL_UID (a) - DECL_UID (b);
327 /* Create a new omp construct that deals with variable remapping. */
329 static struct gimplify_omp_ctx *
330 new_omp_context (enum omp_region_type region_type)
332 struct gimplify_omp_ctx *c;
334 c = XCNEW (struct gimplify_omp_ctx);
335 c->outer_context = gimplify_omp_ctxp;
336 c->variables = splay_tree_new (splay_tree_compare_decl_uid, 0, 0);
337 c->privatized_types = pointer_set_create ();
338 c->location = input_location;
339 c->region_type = region_type;
340 if ((region_type & ORT_TASK) == 0)
341 c->default_kind = OMP_CLAUSE_DEFAULT_SHARED;
343 c->default_kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
348 /* Destroy an omp construct that deals with variable remapping. */
351 delete_omp_context (struct gimplify_omp_ctx *c)
353 splay_tree_delete (c->variables);
354 pointer_set_destroy (c->privatized_types);
358 static void omp_add_variable (struct gimplify_omp_ctx *, tree, unsigned int);
359 static bool omp_notice_variable (struct gimplify_omp_ctx *, tree, bool);
361 /* Both gimplify the statement T and append it to *SEQ_P. This function
362 behaves exactly as gimplify_stmt, but you don't have to pass T as a
366 gimplify_and_add (tree t, gimple_seq *seq_p)
368 gimplify_stmt (&t, seq_p);
371 /* Gimplify statement T into sequence *SEQ_P, and return the first
372 tuple in the sequence of generated tuples for this statement.
373 Return NULL if gimplifying T produced no tuples. */
376 gimplify_and_return_first (tree t, gimple_seq *seq_p)
378 gimple_stmt_iterator last = gsi_last (*seq_p);
380 gimplify_and_add (t, seq_p);
382 if (!gsi_end_p (last))
385 return gsi_stmt (last);
388 return gimple_seq_first_stmt (*seq_p);
391 /* Strip off a legitimate source ending from the input string NAME of
392 length LEN. Rather than having to know the names used by all of
393 our front ends, we strip off an ending of a period followed by
394 up to five characters. (Java uses ".class".) */
397 remove_suffix (char *name, int len)
401 for (i = 2; i < 8 && len > i; i++)
403 if (name[len - i] == '.')
405 name[len - i] = '\0';
411 /* Create a new temporary name with PREFIX. Return an identifier. */
413 static GTY(()) unsigned int tmp_var_id_num;
416 create_tmp_var_name (const char *prefix)
422 char *preftmp = ASTRDUP (prefix);
424 remove_suffix (preftmp, strlen (preftmp));
425 clean_symbol_name (preftmp);
430 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
431 return get_identifier (tmp_name);
434 /* Create a new temporary variable declaration of type TYPE.
435 Do NOT push it into the current binding. */
438 create_tmp_var_raw (tree type, const char *prefix)
442 tmp_var = build_decl (input_location,
443 VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
446 /* The variable was declared by the compiler. */
447 DECL_ARTIFICIAL (tmp_var) = 1;
448 /* And we don't want debug info for it. */
449 DECL_IGNORED_P (tmp_var) = 1;
451 /* Make the variable writable. */
452 TREE_READONLY (tmp_var) = 0;
454 DECL_EXTERNAL (tmp_var) = 0;
455 TREE_STATIC (tmp_var) = 0;
456 TREE_USED (tmp_var) = 1;
461 /* Create a new temporary variable declaration of type TYPE. DO push the
462 variable into the current binding. Further, assume that this is called
463 only from gimplification or optimization, at which point the creation of
464 certain types are bugs. */
467 create_tmp_var (tree type, const char *prefix)
471 /* We don't allow types that are addressable (meaning we can't make copies),
472 or incomplete. We also used to reject every variable size objects here,
473 but now support those for which a constant upper bound can be obtained.
474 The processing for variable sizes is performed in gimple_add_tmp_var,
475 point at which it really matters and possibly reached via paths not going
476 through this function, e.g. after direct calls to create_tmp_var_raw. */
477 gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
479 tmp_var = create_tmp_var_raw (type, prefix);
480 gimple_add_tmp_var (tmp_var);
484 /* Create a new temporary variable declaration of type TYPE by calling
485 create_tmp_var and if TYPE is a vector or a complex number, mark the new
486 temporary as gimple register. */
489 create_tmp_reg (tree type, const char *prefix)
493 tmp = create_tmp_var (type, prefix);
494 if (TREE_CODE (type) == COMPLEX_TYPE
495 || TREE_CODE (type) == VECTOR_TYPE)
496 DECL_GIMPLE_REG_P (tmp) = 1;
501 /* Create a temporary with a name derived from VAL. Subroutine of
502 lookup_tmp_var; nobody else should call this function. */
505 create_tmp_from_val (tree val)
507 return create_tmp_var (TREE_TYPE (val), get_name (val));
510 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
511 an existing expression temporary. */
514 lookup_tmp_var (tree val, bool is_formal)
518 /* If not optimizing, never really reuse a temporary. local-alloc
519 won't allocate any variable that is used in more than one basic
520 block, which means it will go into memory, causing much extra
521 work in reload and final and poorer code generation, outweighing
522 the extra memory allocation here. */
523 if (!optimize || !is_formal || TREE_SIDE_EFFECTS (val))
524 ret = create_tmp_from_val (val);
531 if (gimplify_ctxp->temp_htab == NULL)
532 gimplify_ctxp->temp_htab
533 = htab_create (1000, gimple_tree_hash, gimple_tree_eq, free);
534 slot = htab_find_slot (gimplify_ctxp->temp_htab, (void *)&elt, INSERT);
537 elt_p = XNEW (elt_t);
539 elt_p->temp = ret = create_tmp_from_val (val);
540 *slot = (void *) elt_p;
544 elt_p = (elt_t *) *slot;
552 /* Return true if T is a CALL_EXPR or an expression that can be
553 assigned to a temporary. Note that this predicate should only be
554 used during gimplification. See the rationale for this in
555 gimplify_modify_expr. */
558 is_gimple_reg_rhs_or_call (tree t)
560 return (get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS
561 || TREE_CODE (t) == CALL_EXPR);
564 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
565 this predicate should only be used during gimplification. See the
566 rationale for this in gimplify_modify_expr. */
569 is_gimple_mem_rhs_or_call (tree t)
571 /* If we're dealing with a renamable type, either source or dest must be
572 a renamed variable. */
573 if (is_gimple_reg_type (TREE_TYPE (t)))
574 return is_gimple_val (t);
576 return (is_gimple_val (t) || is_gimple_lvalue (t)
577 || TREE_CODE (t) == CALL_EXPR);
580 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
583 internal_get_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p,
588 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
589 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
590 gimplify_expr (&val, pre_p, post_p, is_gimple_reg_rhs_or_call,
593 t = lookup_tmp_var (val, is_formal);
596 && (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
597 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE))
598 DECL_GIMPLE_REG_P (t) = 1;
600 mod = build2 (INIT_EXPR, TREE_TYPE (t), t, unshare_expr (val));
602 SET_EXPR_LOCATION (mod, EXPR_LOC_OR_HERE (val));
604 /* gimplify_modify_expr might want to reduce this further. */
605 gimplify_and_add (mod, pre_p);
608 /* If we're gimplifying into ssa, gimplify_modify_expr will have
609 given our temporary an SSA name. Find and return it. */
610 if (gimplify_ctxp->into_ssa)
612 gimple last = gimple_seq_last_stmt (*pre_p);
613 t = gimple_get_lhs (last);
619 /* Return a formal temporary variable initialized with VAL. PRE_P is as
620 in gimplify_expr. Only use this function if:
622 1) The value of the unfactored expression represented by VAL will not
623 change between the initialization and use of the temporary, and
624 2) The temporary will not be otherwise modified.
626 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
627 and #2 means it is inappropriate for && temps.
629 For other cases, use get_initialized_tmp_var instead. */
632 get_formal_tmp_var (tree val, gimple_seq *pre_p)
634 return internal_get_tmp_var (val, pre_p, NULL, true);
637 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
638 are as in gimplify_expr. */
641 get_initialized_tmp_var (tree val, gimple_seq *pre_p, gimple_seq *post_p)
643 return internal_get_tmp_var (val, pre_p, post_p, false);
646 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
647 generate debug info for them; otherwise don't. */
650 declare_vars (tree vars, gimple scope, bool debug_info)
657 gcc_assert (gimple_code (scope) == GIMPLE_BIND);
659 temps = nreverse (last);
661 block = gimple_bind_block (scope);
662 gcc_assert (!block || TREE_CODE (block) == BLOCK);
663 if (!block || !debug_info)
665 DECL_CHAIN (last) = gimple_bind_vars (scope);
666 gimple_bind_set_vars (scope, temps);
670 /* We need to attach the nodes both to the BIND_EXPR and to its
671 associated BLOCK for debugging purposes. The key point here
672 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
673 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
674 if (BLOCK_VARS (block))
675 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), temps);
678 gimple_bind_set_vars (scope,
679 chainon (gimple_bind_vars (scope), temps));
680 BLOCK_VARS (block) = temps;
686 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
687 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
688 no such upper bound can be obtained. */
691 force_constant_size (tree var)
693 /* The only attempt we make is by querying the maximum size of objects
694 of the variable's type. */
696 HOST_WIDE_INT max_size;
698 gcc_assert (TREE_CODE (var) == VAR_DECL);
700 max_size = max_int_size_in_bytes (TREE_TYPE (var));
702 gcc_assert (max_size >= 0);
705 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var)), max_size);
707 = build_int_cst (TREE_TYPE (DECL_SIZE (var)), max_size * BITS_PER_UNIT);
710 /* Push the temporary variable TMP into the current binding. */
713 gimple_add_tmp_var (tree tmp)
715 gcc_assert (!DECL_CHAIN (tmp) && !DECL_SEEN_IN_BIND_EXPR_P (tmp));
717 /* Later processing assumes that the object size is constant, which might
718 not be true at this point. Force the use of a constant upper bound in
720 if (!host_integerp (DECL_SIZE_UNIT (tmp), 1))
721 force_constant_size (tmp);
723 DECL_CONTEXT (tmp) = current_function_decl;
724 DECL_SEEN_IN_BIND_EXPR_P (tmp) = 1;
728 DECL_CHAIN (tmp) = gimplify_ctxp->temps;
729 gimplify_ctxp->temps = tmp;
731 /* Mark temporaries local within the nearest enclosing parallel. */
732 if (gimplify_omp_ctxp)
734 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
735 while (ctx && ctx->region_type == ORT_WORKSHARE)
736 ctx = ctx->outer_context;
738 omp_add_variable (ctx, tmp, GOVD_LOCAL | GOVD_SEEN);
747 /* This case is for nested functions. We need to expose the locals
749 body_seq = gimple_body (current_function_decl);
750 declare_vars (tmp, gimple_seq_first_stmt (body_seq), false);
754 /* Determine whether to assign a location to the statement GS. */
757 should_carry_location_p (gimple gs)
759 /* Don't emit a line note for a label. We particularly don't want to
760 emit one for the break label, since it doesn't actually correspond
761 to the beginning of the loop/switch. */
762 if (gimple_code (gs) == GIMPLE_LABEL)
768 /* Return true if a location should not be emitted for this statement
769 by annotate_one_with_location. */
772 gimple_do_not_emit_location_p (gimple g)
774 return gimple_plf (g, GF_PLF_1);
777 /* Mark statement G so a location will not be emitted by
778 annotate_one_with_location. */
781 gimple_set_do_not_emit_location (gimple g)
783 /* The PLF flags are initialized to 0 when a new tuple is created,
784 so no need to initialize it anywhere. */
785 gimple_set_plf (g, GF_PLF_1, true);
788 /* Set the location for gimple statement GS to LOCATION. */
791 annotate_one_with_location (gimple gs, location_t location)
793 if (!gimple_has_location (gs)
794 && !gimple_do_not_emit_location_p (gs)
795 && should_carry_location_p (gs))
796 gimple_set_location (gs, location);
799 /* Set LOCATION for all the statements after iterator GSI in sequence
800 SEQ. If GSI is pointing to the end of the sequence, start with the
801 first statement in SEQ. */
804 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
808 gsi = gsi_start (seq);
812 for (; !gsi_end_p (gsi); gsi_next (&gsi))
813 annotate_one_with_location (gsi_stmt (gsi), location);
816 /* Set the location for all the statements in a sequence STMT_P to LOCATION. */
819 annotate_all_with_location (gimple_seq stmt_p, location_t location)
821 gimple_stmt_iterator i;
823 if (gimple_seq_empty_p (stmt_p))
826 for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
828 gimple gs = gsi_stmt (i);
829 annotate_one_with_location (gs, location);
833 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
834 nodes that are referenced more than once in GENERIC functions. This is
835 necessary because gimplification (translation into GIMPLE) is performed
836 by modifying tree nodes in-place, so gimplication of a shared node in a
837 first context could generate an invalid GIMPLE form in a second context.
839 This is achieved with a simple mark/copy/unmark algorithm that walks the
840 GENERIC representation top-down, marks nodes with TREE_VISITED the first
841 time it encounters them, duplicates them if they already have TREE_VISITED
842 set, and finally removes the TREE_VISITED marks it has set.
844 The algorithm works only at the function level, i.e. it generates a GENERIC
845 representation of a function with no nodes shared within the function when
846 passed a GENERIC function (except for nodes that are allowed to be shared).
848 At the global level, it is also necessary to unshare tree nodes that are
849 referenced in more than one function, for the same aforementioned reason.
850 This requires some cooperation from the front-end. There are 2 strategies:
852 1. Manual unsharing. The front-end needs to call unshare_expr on every
853 expression that might end up being shared across functions.
855 2. Deep unsharing. This is an extension of regular unsharing. Instead
856 of calling unshare_expr on expressions that might be shared across
857 functions, the front-end pre-marks them with TREE_VISITED. This will
858 ensure that they are unshared on the first reference within functions
859 when the regular unsharing algorithm runs. The counterpart is that
860 this algorithm must look deeper than for manual unsharing, which is
861 specified by LANG_HOOKS_DEEP_UNSHARING.
863 If there are only few specific cases of node sharing across functions, it is
864 probably easier for a front-end to unshare the expressions manually. On the
865 contrary, if the expressions generated at the global level are as widespread
866 as expressions generated within functions, deep unsharing is very likely the
869 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
870 These nodes model computations that must be done once. If we were to
871 unshare something like SAVE_EXPR(i++), the gimplification process would
872 create wrong code. However, if DATA is non-null, it must hold a pointer
873 set that is used to unshare the subtrees of these nodes. */
876 mostly_copy_tree_r (tree *tp, int *walk_subtrees, void *data)
879 enum tree_code code = TREE_CODE (t);
881 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
882 copy their subtrees if we can make sure to do it only once. */
883 if (code == SAVE_EXPR || code == TARGET_EXPR || code == BIND_EXPR)
885 if (data && !pointer_set_insert ((struct pointer_set_t *)data, t))
891 /* Stop at types, decls, constants like copy_tree_r. */
892 else if (TREE_CODE_CLASS (code) == tcc_type
893 || TREE_CODE_CLASS (code) == tcc_declaration
894 || TREE_CODE_CLASS (code) == tcc_constant
895 /* We can't do anything sensible with a BLOCK used as an
896 expression, but we also can't just die when we see it
897 because of non-expression uses. So we avert our eyes
898 and cross our fingers. Silly Java. */
902 /* Cope with the statement expression extension. */
903 else if (code == STATEMENT_LIST)
906 /* Leave the bulk of the work to copy_tree_r itself. */
908 copy_tree_r (tp, walk_subtrees, NULL);
913 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
914 If *TP has been visited already, then *TP is deeply copied by calling
915 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
918 copy_if_shared_r (tree *tp, int *walk_subtrees, void *data)
921 enum tree_code code = TREE_CODE (t);
923 /* Skip types, decls, and constants. But we do want to look at their
924 types and the bounds of types. Mark them as visited so we properly
925 unmark their subtrees on the unmark pass. If we've already seen them,
926 don't look down further. */
927 if (TREE_CODE_CLASS (code) == tcc_type
928 || TREE_CODE_CLASS (code) == tcc_declaration
929 || TREE_CODE_CLASS (code) == tcc_constant)
931 if (TREE_VISITED (t))
934 TREE_VISITED (t) = 1;
937 /* If this node has been visited already, unshare it and don't look
939 else if (TREE_VISITED (t))
941 walk_tree (tp, mostly_copy_tree_r, data, NULL);
945 /* Otherwise, mark the node as visited and keep looking. */
947 TREE_VISITED (t) = 1;
952 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
953 copy_if_shared_r callback unmodified. */
956 copy_if_shared (tree *tp, void *data)
958 walk_tree (tp, copy_if_shared_r, data, NULL);
961 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
962 any nested functions. */
965 unshare_body (tree fndecl)
967 struct cgraph_node *cgn = cgraph_get_node (fndecl);
968 /* If the language requires deep unsharing, we need a pointer set to make
969 sure we don't repeatedly unshare subtrees of unshareable nodes. */
970 struct pointer_set_t *visited
971 = lang_hooks.deep_unsharing ? pointer_set_create () : NULL;
973 copy_if_shared (&DECL_SAVED_TREE (fndecl), visited);
974 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl)), visited);
975 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)), visited);
978 pointer_set_destroy (visited);
981 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
982 unshare_body (cgn->decl);
985 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
986 Subtrees are walked until the first unvisited node is encountered. */
989 unmark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
993 /* If this node has been visited, unmark it and keep looking. */
994 if (TREE_VISITED (t))
995 TREE_VISITED (t) = 0;
997 /* Otherwise, don't look any deeper. */
1004 /* Unmark the visited trees rooted at *TP. */
1007 unmark_visited (tree *tp)
1009 walk_tree (tp, unmark_visited_r, NULL, NULL);
1012 /* Likewise, but mark all trees as not visited. */
1015 unvisit_body (tree fndecl)
1017 struct cgraph_node *cgn = cgraph_get_node (fndecl);
1019 unmark_visited (&DECL_SAVED_TREE (fndecl));
1020 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl)));
1021 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl)));
1024 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1025 unvisit_body (cgn->decl);
1028 /* Unconditionally make an unshared copy of EXPR. This is used when using
1029 stored expressions which span multiple functions, such as BINFO_VTABLE,
1030 as the normal unsharing process can't tell that they're shared. */
1033 unshare_expr (tree expr)
1035 walk_tree (&expr, mostly_copy_tree_r, NULL, NULL);
1039 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
1040 contain statements and have a value. Assign its value to a temporary
1041 and give it void_type_node. Return the temporary, or NULL_TREE if
1042 WRAPPER was already void. */
1045 voidify_wrapper_expr (tree wrapper, tree temp)
1047 tree type = TREE_TYPE (wrapper);
1048 if (type && !VOID_TYPE_P (type))
1052 /* Set p to point to the body of the wrapper. Loop until we find
1053 something that isn't a wrapper. */
1054 for (p = &wrapper; p && *p; )
1056 switch (TREE_CODE (*p))
1059 TREE_SIDE_EFFECTS (*p) = 1;
1060 TREE_TYPE (*p) = void_type_node;
1061 /* For a BIND_EXPR, the body is operand 1. */
1062 p = &BIND_EXPR_BODY (*p);
1065 case CLEANUP_POINT_EXPR:
1066 case TRY_FINALLY_EXPR:
1067 case TRY_CATCH_EXPR:
1068 TREE_SIDE_EFFECTS (*p) = 1;
1069 TREE_TYPE (*p) = void_type_node;
1070 p = &TREE_OPERAND (*p, 0);
1073 case STATEMENT_LIST:
1075 tree_stmt_iterator i = tsi_last (*p);
1076 TREE_SIDE_EFFECTS (*p) = 1;
1077 TREE_TYPE (*p) = void_type_node;
1078 p = tsi_end_p (i) ? NULL : tsi_stmt_ptr (i);
1083 /* Advance to the last statement. Set all container types to
1085 for (; TREE_CODE (*p) == COMPOUND_EXPR; p = &TREE_OPERAND (*p, 1))
1087 TREE_SIDE_EFFECTS (*p) = 1;
1088 TREE_TYPE (*p) = void_type_node;
1092 case TRANSACTION_EXPR:
1093 TREE_SIDE_EFFECTS (*p) = 1;
1094 TREE_TYPE (*p) = void_type_node;
1095 p = &TRANSACTION_EXPR_BODY (*p);
1099 /* Assume that any tree upon which voidify_wrapper_expr is
1100 directly called is a wrapper, and that its body is op0. */
1103 TREE_SIDE_EFFECTS (*p) = 1;
1104 TREE_TYPE (*p) = void_type_node;
1105 p = &TREE_OPERAND (*p, 0);
1113 if (p == NULL || IS_EMPTY_STMT (*p))
1117 /* The wrapper is on the RHS of an assignment that we're pushing
1119 gcc_assert (TREE_CODE (temp) == INIT_EXPR
1120 || TREE_CODE (temp) == MODIFY_EXPR);
1121 TREE_OPERAND (temp, 1) = *p;
1126 temp = create_tmp_var (type, "retval");
1127 *p = build2 (INIT_EXPR, type, temp, *p);
1136 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1137 a temporary through which they communicate. */
1140 build_stack_save_restore (gimple *save, gimple *restore)
1144 *save = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE), 0);
1145 tmp_var = create_tmp_var (ptr_type_node, "saved_stack");
1146 gimple_call_set_lhs (*save, tmp_var);
1149 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE),
1153 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1155 static enum gimplify_status
1156 gimplify_bind_expr (tree *expr_p, gimple_seq *pre_p)
1158 tree bind_expr = *expr_p;
1159 bool old_save_stack = gimplify_ctxp->save_stack;
1162 gimple_seq body, cleanup;
1165 tree temp = voidify_wrapper_expr (bind_expr, NULL);
1167 /* Mark variables seen in this bind expr. */
1168 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1170 if (TREE_CODE (t) == VAR_DECL)
1172 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1174 /* Mark variable as local. */
1175 if (ctx && !DECL_EXTERNAL (t)
1176 && (! DECL_SEEN_IN_BIND_EXPR_P (t)
1177 || splay_tree_lookup (ctx->variables,
1178 (splay_tree_key) t) == NULL))
1179 omp_add_variable (gimplify_omp_ctxp, t, GOVD_LOCAL | GOVD_SEEN);
1181 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
1183 if (DECL_HARD_REGISTER (t) && !is_global_var (t) && cfun)
1184 cfun->has_local_explicit_reg_vars = true;
1187 /* Preliminarily mark non-addressed complex variables as eligible
1188 for promotion to gimple registers. We'll transform their uses
1190 if ((TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
1191 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1192 && !TREE_THIS_VOLATILE (t)
1193 && (TREE_CODE (t) == VAR_DECL && !DECL_HARD_REGISTER (t))
1194 && !needs_to_live_in_memory (t))
1195 DECL_GIMPLE_REG_P (t) = 1;
1198 gimple_bind = gimple_build_bind (BIND_EXPR_VARS (bind_expr), NULL,
1199 BIND_EXPR_BLOCK (bind_expr));
1200 gimple_push_bind_expr (gimple_bind);
1202 gimplify_ctxp->save_stack = false;
1204 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1206 gimplify_stmt (&BIND_EXPR_BODY (bind_expr), &body);
1207 gimple_bind_set_body (gimple_bind, body);
1211 if (gimplify_ctxp->save_stack)
1213 gimple stack_restore;
1215 /* Save stack on entry and restore it on exit. Add a try_finally
1216 block to achieve this. Note that mudflap depends on the
1217 format of the emitted code: see mx_register_decls(). */
1218 build_stack_save_restore (&stack_save, &stack_restore);
1220 gimplify_seq_add_stmt (&cleanup, stack_restore);
1223 /* Add clobbers for all variables that go out of scope. */
1224 for (t = BIND_EXPR_VARS (bind_expr); t ; t = DECL_CHAIN (t))
1226 if (TREE_CODE (t) == VAR_DECL
1227 && !is_global_var (t)
1228 && DECL_CONTEXT (t) == current_function_decl
1229 && !DECL_HARD_REGISTER (t)
1230 && !TREE_THIS_VOLATILE (t)
1231 && !DECL_HAS_VALUE_EXPR_P (t)
1232 /* Only care for variables that have to be in memory. Others
1233 will be rewritten into SSA names, hence moved to the top-level. */
1234 && needs_to_live_in_memory (t))
1236 tree clobber = build_constructor (TREE_TYPE (t), NULL);
1237 TREE_THIS_VOLATILE (clobber) = 1;
1238 gimplify_seq_add_stmt (&cleanup, gimple_build_assign (t, clobber));
1245 gimple_seq new_body;
1248 gs = gimple_build_try (gimple_bind_body (gimple_bind), cleanup,
1249 GIMPLE_TRY_FINALLY);
1252 gimplify_seq_add_stmt (&new_body, stack_save);
1253 gimplify_seq_add_stmt (&new_body, gs);
1254 gimple_bind_set_body (gimple_bind, new_body);
1257 gimplify_ctxp->save_stack = old_save_stack;
1258 gimple_pop_bind_expr ();
1260 gimplify_seq_add_stmt (pre_p, gimple_bind);
1268 *expr_p = NULL_TREE;
1272 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1273 GIMPLE value, it is assigned to a new temporary and the statement is
1274 re-written to return the temporary.
1276 PRE_P points to the sequence where side effects that must happen before
1277 STMT should be stored. */
1279 static enum gimplify_status
1280 gimplify_return_expr (tree stmt, gimple_seq *pre_p)
1283 tree ret_expr = TREE_OPERAND (stmt, 0);
1284 tree result_decl, result;
1286 if (ret_expr == error_mark_node)
1290 || TREE_CODE (ret_expr) == RESULT_DECL
1291 || ret_expr == error_mark_node)
1293 gimple ret = gimple_build_return (ret_expr);
1294 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1295 gimplify_seq_add_stmt (pre_p, ret);
1299 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1300 result_decl = NULL_TREE;
1303 result_decl = TREE_OPERAND (ret_expr, 0);
1305 /* See through a return by reference. */
1306 if (TREE_CODE (result_decl) == INDIRECT_REF)
1307 result_decl = TREE_OPERAND (result_decl, 0);
1309 gcc_assert ((TREE_CODE (ret_expr) == MODIFY_EXPR
1310 || TREE_CODE (ret_expr) == INIT_EXPR)
1311 && TREE_CODE (result_decl) == RESULT_DECL);
1314 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1315 Recall that aggregate_value_p is FALSE for any aggregate type that is
1316 returned in registers. If we're returning values in registers, then
1317 we don't want to extend the lifetime of the RESULT_DECL, particularly
1318 across another call. In addition, for those aggregates for which
1319 hard_function_value generates a PARALLEL, we'll die during normal
1320 expansion of structure assignments; there's special code in expand_return
1321 to handle this case that does not exist in expand_expr. */
1324 else if (aggregate_value_p (result_decl, TREE_TYPE (current_function_decl)))
1326 if (TREE_CODE (DECL_SIZE (result_decl)) != INTEGER_CST)
1328 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl)))
1329 gimplify_type_sizes (TREE_TYPE (result_decl), pre_p);
1330 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1331 should be effectively allocated by the caller, i.e. all calls to
1332 this function must be subject to the Return Slot Optimization. */
1333 gimplify_one_sizepos (&DECL_SIZE (result_decl), pre_p);
1334 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl), pre_p);
1336 result = result_decl;
1338 else if (gimplify_ctxp->return_temp)
1339 result = gimplify_ctxp->return_temp;
1342 result = create_tmp_reg (TREE_TYPE (result_decl), NULL);
1344 /* ??? With complex control flow (usually involving abnormal edges),
1345 we can wind up warning about an uninitialized value for this. Due
1346 to how this variable is constructed and initialized, this is never
1347 true. Give up and never warn. */
1348 TREE_NO_WARNING (result) = 1;
1350 gimplify_ctxp->return_temp = result;
1353 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1354 Then gimplify the whole thing. */
1355 if (result != result_decl)
1356 TREE_OPERAND (ret_expr, 0) = result;
1358 gimplify_and_add (TREE_OPERAND (stmt, 0), pre_p);
1360 ret = gimple_build_return (result);
1361 gimple_set_no_warning (ret, TREE_NO_WARNING (stmt));
1362 gimplify_seq_add_stmt (pre_p, ret);
1367 /* Gimplify a variable-length array DECL. */
1370 gimplify_vla_decl (tree decl, gimple_seq *seq_p)
1372 /* This is a variable-sized decl. Simplify its size and mark it
1373 for deferred expansion. Note that mudflap depends on the format
1374 of the emitted code: see mx_register_decls(). */
1375 tree t, addr, ptr_type;
1377 gimplify_one_sizepos (&DECL_SIZE (decl), seq_p);
1378 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl), seq_p);
1380 /* All occurrences of this decl in final gimplified code will be
1381 replaced by indirection. Setting DECL_VALUE_EXPR does two
1382 things: First, it lets the rest of the gimplifier know what
1383 replacement to use. Second, it lets the debug info know
1384 where to find the value. */
1385 ptr_type = build_pointer_type (TREE_TYPE (decl));
1386 addr = create_tmp_var (ptr_type, get_name (decl));
1387 DECL_IGNORED_P (addr) = 0;
1388 t = build_fold_indirect_ref (addr);
1389 TREE_THIS_NOTRAP (t) = 1;
1390 SET_DECL_VALUE_EXPR (decl, t);
1391 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1393 t = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
1394 t = build_call_expr (t, 2, DECL_SIZE_UNIT (decl),
1395 size_int (DECL_ALIGN (decl)));
1396 /* The call has been built for a variable-sized object. */
1397 CALL_ALLOCA_FOR_VAR_P (t) = 1;
1398 t = fold_convert (ptr_type, t);
1399 t = build2 (MODIFY_EXPR, TREE_TYPE (addr), addr, t);
1401 gimplify_and_add (t, seq_p);
1403 /* Indicate that we need to restore the stack level when the
1404 enclosing BIND_EXPR is exited. */
1405 gimplify_ctxp->save_stack = true;
1408 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1409 and initialization explicit. */
1411 static enum gimplify_status
1412 gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
1414 tree stmt = *stmt_p;
1415 tree decl = DECL_EXPR_DECL (stmt);
1417 *stmt_p = NULL_TREE;
1419 if (TREE_TYPE (decl) == error_mark_node)
1422 if ((TREE_CODE (decl) == TYPE_DECL
1423 || TREE_CODE (decl) == VAR_DECL)
1424 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl)))
1425 gimplify_type_sizes (TREE_TYPE (decl), seq_p);
1427 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1429 tree init = DECL_INITIAL (decl);
1431 if (TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1432 || (!TREE_STATIC (decl)
1433 && flag_stack_check == GENERIC_STACK_CHECK
1434 && compare_tree_int (DECL_SIZE_UNIT (decl),
1435 STACK_CHECK_MAX_VAR_SIZE) > 0))
1436 gimplify_vla_decl (decl, seq_p);
1438 /* Some front ends do not explicitly declare all anonymous
1439 artificial variables. We compensate here by declaring the
1440 variables, though it would be better if the front ends would
1441 explicitly declare them. */
1442 if (!DECL_SEEN_IN_BIND_EXPR_P (decl)
1443 && DECL_ARTIFICIAL (decl) && DECL_NAME (decl) == NULL_TREE)
1444 gimple_add_tmp_var (decl);
1446 if (init && init != error_mark_node)
1448 if (!TREE_STATIC (decl))
1450 DECL_INITIAL (decl) = NULL_TREE;
1451 init = build2 (INIT_EXPR, void_type_node, decl, init);
1452 gimplify_and_add (init, seq_p);
1456 /* We must still examine initializers for static variables
1457 as they may contain a label address. */
1458 walk_tree (&init, force_labels_r, NULL, NULL);
1465 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1466 and replacing the LOOP_EXPR with goto, but if the loop contains an
1467 EXIT_EXPR, we need to append a label for it to jump to. */
1469 static enum gimplify_status
1470 gimplify_loop_expr (tree *expr_p, gimple_seq *pre_p)
1472 tree saved_label = gimplify_ctxp->exit_label;
1473 tree start_label = create_artificial_label (UNKNOWN_LOCATION);
1475 gimplify_seq_add_stmt (pre_p, gimple_build_label (start_label));
1477 gimplify_ctxp->exit_label = NULL_TREE;
1479 gimplify_and_add (LOOP_EXPR_BODY (*expr_p), pre_p);
1481 gimplify_seq_add_stmt (pre_p, gimple_build_goto (start_label));
1483 if (gimplify_ctxp->exit_label)
1484 gimplify_seq_add_stmt (pre_p,
1485 gimple_build_label (gimplify_ctxp->exit_label));
1487 gimplify_ctxp->exit_label = saved_label;
1493 /* Gimplify a statement list onto a sequence. These may be created either
1494 by an enlightened front-end, or by shortcut_cond_expr. */
1496 static enum gimplify_status
1497 gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
1499 tree temp = voidify_wrapper_expr (*expr_p, NULL);
1501 tree_stmt_iterator i = tsi_start (*expr_p);
1503 while (!tsi_end_p (i))
1505 gimplify_stmt (tsi_stmt_ptr (i), pre_p);
1518 /* Compare two case labels. Because the front end should already have
1519 made sure that case ranges do not overlap, it is enough to only compare
1520 the CASE_LOW values of each case label. */
1523 compare_case_labels (const void *p1, const void *p2)
1525 const_tree const case1 = *(const_tree const*)p1;
1526 const_tree const case2 = *(const_tree const*)p2;
1528 /* The 'default' case label always goes first. */
1529 if (!CASE_LOW (case1))
1531 else if (!CASE_LOW (case2))
1534 return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
1537 /* Sort the case labels in LABEL_VEC in place in ascending order. */
1540 sort_case_labels (VEC(tree,heap)* label_vec)
1542 VEC_qsort (tree, label_vec, compare_case_labels);
1545 /* Gimplify a SWITCH_EXPR, and collect a TREE_VEC of the labels it can
1548 static enum gimplify_status
1549 gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
1551 tree switch_expr = *expr_p;
1552 gimple_seq switch_body_seq = NULL;
1553 enum gimplify_status ret;
1555 ret = gimplify_expr (&SWITCH_COND (switch_expr), pre_p, NULL, is_gimple_val,
1557 if (ret == GS_ERROR || ret == GS_UNHANDLED)
1560 if (SWITCH_BODY (switch_expr))
1562 VEC (tree,heap) *labels;
1563 VEC (tree,heap) *saved_labels;
1564 tree default_case = NULL_TREE;
1566 gimple gimple_switch;
1568 /* If someone can be bothered to fill in the labels, they can
1569 be bothered to null out the body too. */
1570 gcc_assert (!SWITCH_LABELS (switch_expr));
1572 /* save old labels, get new ones from body, then restore the old
1573 labels. Save all the things from the switch body to append after. */
1574 saved_labels = gimplify_ctxp->case_labels;
1575 gimplify_ctxp->case_labels = VEC_alloc (tree, heap, 8);
1577 gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
1578 labels = gimplify_ctxp->case_labels;
1579 gimplify_ctxp->case_labels = saved_labels;
1582 while (i < VEC_length (tree, labels))
1584 tree elt = VEC_index (tree, labels, i);
1585 tree low = CASE_LOW (elt);
1586 bool remove_element = FALSE;
1590 /* Discard empty ranges. */
1591 tree high = CASE_HIGH (elt);
1592 if (high && tree_int_cst_lt (high, low))
1593 remove_element = TRUE;
1597 /* The default case must be the last label in the list. */
1598 gcc_assert (!default_case);
1600 remove_element = TRUE;
1604 VEC_ordered_remove (tree, labels, i);
1610 if (!VEC_empty (tree, labels))
1611 sort_case_labels (labels);
1615 tree type = TREE_TYPE (switch_expr);
1617 /* If the switch has no default label, add one, so that we jump
1618 around the switch body. If the labels already cover the whole
1619 range of type, add the default label pointing to one of the
1621 if (type == void_type_node)
1622 type = TREE_TYPE (SWITCH_COND (switch_expr));
1624 && INTEGRAL_TYPE_P (type)
1625 && TYPE_MIN_VALUE (type)
1626 && TYPE_MAX_VALUE (type)
1627 && tree_int_cst_equal (CASE_LOW (VEC_index (tree, labels, 0)),
1628 TYPE_MIN_VALUE (type)))
1630 tree low, high = CASE_HIGH (VEC_index (tree, labels, len - 1));
1632 high = CASE_LOW (VEC_index (tree, labels, len - 1));
1633 if (tree_int_cst_equal (high, TYPE_MAX_VALUE (type)))
1635 for (i = 1; i < len; i++)
1637 high = CASE_LOW (VEC_index (tree, labels, i));
1638 low = CASE_HIGH (VEC_index (tree, labels, i - 1));
1640 low = CASE_LOW (VEC_index (tree, labels, i - 1));
1641 if ((TREE_INT_CST_LOW (low) + 1
1642 != TREE_INT_CST_LOW (high))
1643 || (TREE_INT_CST_HIGH (low)
1644 + (TREE_INT_CST_LOW (high) == 0)
1645 != TREE_INT_CST_HIGH (high)))
1650 tree label = CASE_LABEL (VEC_index (tree, labels, 0));
1651 default_case = build_case_label (NULL_TREE, NULL_TREE,
1662 = build_case_label (NULL_TREE, NULL_TREE,
1663 create_artificial_label (UNKNOWN_LOCATION));
1664 new_default = gimple_build_label (CASE_LABEL (default_case));
1665 gimplify_seq_add_stmt (&switch_body_seq, new_default);
1669 gimple_switch = gimple_build_switch_vec (SWITCH_COND (switch_expr),
1670 default_case, labels);
1671 gimplify_seq_add_stmt (pre_p, gimple_switch);
1672 gimplify_seq_add_seq (pre_p, switch_body_seq);
1673 VEC_free(tree, heap, labels);
1676 gcc_assert (SWITCH_LABELS (switch_expr));
1681 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1683 static enum gimplify_status
1684 gimplify_case_label_expr (tree *expr_p, gimple_seq *pre_p)
1686 struct gimplify_ctx *ctxp;
1687 gimple gimple_label;
1689 /* Invalid OpenMP programs can play Duff's Device type games with
1690 #pragma omp parallel. At least in the C front end, we don't
1691 detect such invalid branches until after gimplification. */
1692 for (ctxp = gimplify_ctxp; ; ctxp = ctxp->prev_context)
1693 if (ctxp->case_labels)
1696 gimple_label = gimple_build_label (CASE_LABEL (*expr_p));
1697 VEC_safe_push (tree, heap, ctxp->case_labels, *expr_p);
1698 gimplify_seq_add_stmt (pre_p, gimple_label);
1703 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1707 build_and_jump (tree *label_p)
1709 if (label_p == NULL)
1710 /* If there's nowhere to jump, just fall through. */
1713 if (*label_p == NULL_TREE)
1715 tree label = create_artificial_label (UNKNOWN_LOCATION);
1719 return build1 (GOTO_EXPR, void_type_node, *label_p);
1722 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1723 This also involves building a label to jump to and communicating it to
1724 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1726 static enum gimplify_status
1727 gimplify_exit_expr (tree *expr_p)
1729 tree cond = TREE_OPERAND (*expr_p, 0);
1732 expr = build_and_jump (&gimplify_ctxp->exit_label);
1733 expr = build3 (COND_EXPR, void_type_node, cond, expr, NULL_TREE);
1739 /* A helper function to be called via walk_tree. Mark all labels under *TP
1740 as being forced. To be called for DECL_INITIAL of static variables. */
1743 force_labels_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
1747 if (TREE_CODE (*tp) == LABEL_DECL)
1748 FORCED_LABEL (*tp) = 1;
1753 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1754 different from its canonical type, wrap the whole thing inside a
1755 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1758 The canonical type of a COMPONENT_REF is the type of the field being
1759 referenced--unless the field is a bit-field which can be read directly
1760 in a smaller mode, in which case the canonical type is the
1761 sign-appropriate type corresponding to that mode. */
1764 canonicalize_component_ref (tree *expr_p)
1766 tree expr = *expr_p;
1769 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
1771 if (INTEGRAL_TYPE_P (TREE_TYPE (expr)))
1772 type = TREE_TYPE (get_unwidened (expr, NULL_TREE));
1774 type = TREE_TYPE (TREE_OPERAND (expr, 1));
1776 /* One could argue that all the stuff below is not necessary for
1777 the non-bitfield case and declare it a FE error if type
1778 adjustment would be needed. */
1779 if (TREE_TYPE (expr) != type)
1781 #ifdef ENABLE_TYPES_CHECKING
1782 tree old_type = TREE_TYPE (expr);
1786 /* We need to preserve qualifiers and propagate them from
1788 type_quals = TYPE_QUALS (type)
1789 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr, 0)));
1790 if (TYPE_QUALS (type) != type_quals)
1791 type = build_qualified_type (TYPE_MAIN_VARIANT (type), type_quals);
1793 /* Set the type of the COMPONENT_REF to the underlying type. */
1794 TREE_TYPE (expr) = type;
1796 #ifdef ENABLE_TYPES_CHECKING
1797 /* It is now a FE error, if the conversion from the canonical
1798 type to the original expression type is not useless. */
1799 gcc_assert (useless_type_conversion_p (old_type, type));
1804 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1805 to foo, embed that change in the ADDR_EXPR by converting
1810 where L is the lower bound. For simplicity, only do this for constant
1812 The constraint is that the type of &array[L] is trivially convertible
1816 canonicalize_addr_expr (tree *expr_p)
1818 tree expr = *expr_p;
1819 tree addr_expr = TREE_OPERAND (expr, 0);
1820 tree datype, ddatype, pddatype;
1822 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1823 if (!POINTER_TYPE_P (TREE_TYPE (expr))
1824 || TREE_CODE (addr_expr) != ADDR_EXPR)
1827 /* The addr_expr type should be a pointer to an array. */
1828 datype = TREE_TYPE (TREE_TYPE (addr_expr));
1829 if (TREE_CODE (datype) != ARRAY_TYPE)
1832 /* The pointer to element type shall be trivially convertible to
1833 the expression pointer type. */
1834 ddatype = TREE_TYPE (datype);
1835 pddatype = build_pointer_type (ddatype);
1836 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr)),
1840 /* The lower bound and element sizes must be constant. */
1841 if (!TYPE_SIZE_UNIT (ddatype)
1842 || TREE_CODE (TYPE_SIZE_UNIT (ddatype)) != INTEGER_CST
1843 || !TYPE_DOMAIN (datype) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype))
1844 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype))) != INTEGER_CST)
1847 /* All checks succeeded. Build a new node to merge the cast. */
1848 *expr_p = build4 (ARRAY_REF, ddatype, TREE_OPERAND (addr_expr, 0),
1849 TYPE_MIN_VALUE (TYPE_DOMAIN (datype)),
1850 NULL_TREE, NULL_TREE);
1851 *expr_p = build1 (ADDR_EXPR, pddatype, *expr_p);
1853 /* We can have stripped a required restrict qualifier above. */
1854 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
1855 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
1858 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1859 underneath as appropriate. */
1861 static enum gimplify_status
1862 gimplify_conversion (tree *expr_p)
1864 location_t loc = EXPR_LOCATION (*expr_p);
1865 gcc_assert (CONVERT_EXPR_P (*expr_p));
1867 /* Then strip away all but the outermost conversion. */
1868 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p, 0));
1870 /* And remove the outermost conversion if it's useless. */
1871 if (tree_ssa_useless_type_conversion (*expr_p))
1872 *expr_p = TREE_OPERAND (*expr_p, 0);
1874 /* If we still have a conversion at the toplevel,
1875 then canonicalize some constructs. */
1876 if (CONVERT_EXPR_P (*expr_p))
1878 tree sub = TREE_OPERAND (*expr_p, 0);
1880 /* If a NOP conversion is changing the type of a COMPONENT_REF
1881 expression, then canonicalize its type now in order to expose more
1882 redundant conversions. */
1883 if (TREE_CODE (sub) == COMPONENT_REF)
1884 canonicalize_component_ref (&TREE_OPERAND (*expr_p, 0));
1886 /* If a NOP conversion is changing a pointer to array of foo
1887 to a pointer to foo, embed that change in the ADDR_EXPR. */
1888 else if (TREE_CODE (sub) == ADDR_EXPR)
1889 canonicalize_addr_expr (expr_p);
1892 /* If we have a conversion to a non-register type force the
1893 use of a VIEW_CONVERT_EXPR instead. */
1894 if (CONVERT_EXPR_P (*expr_p) && !is_gimple_reg_type (TREE_TYPE (*expr_p)))
1895 *expr_p = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (*expr_p),
1896 TREE_OPERAND (*expr_p, 0));
1901 /* Nonlocal VLAs seen in the current function. */
1902 static struct pointer_set_t *nonlocal_vlas;
1904 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1905 DECL_VALUE_EXPR, and it's worth re-examining things. */
1907 static enum gimplify_status
1908 gimplify_var_or_parm_decl (tree *expr_p)
1910 tree decl = *expr_p;
1912 /* ??? If this is a local variable, and it has not been seen in any
1913 outer BIND_EXPR, then it's probably the result of a duplicate
1914 declaration, for which we've already issued an error. It would
1915 be really nice if the front end wouldn't leak these at all.
1916 Currently the only known culprit is C++ destructors, as seen
1917 in g++.old-deja/g++.jason/binding.C. */
1918 if (TREE_CODE (decl) == VAR_DECL
1919 && !DECL_SEEN_IN_BIND_EXPR_P (decl)
1920 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
1921 && decl_function_context (decl) == current_function_decl)
1923 gcc_assert (seen_error ());
1927 /* When within an OpenMP context, notice uses of variables. */
1928 if (gimplify_omp_ctxp && omp_notice_variable (gimplify_omp_ctxp, decl, true))
1931 /* If the decl is an alias for another expression, substitute it now. */
1932 if (DECL_HAS_VALUE_EXPR_P (decl))
1934 tree value_expr = DECL_VALUE_EXPR (decl);
1936 /* For referenced nonlocal VLAs add a decl for debugging purposes
1937 to the current function. */
1938 if (TREE_CODE (decl) == VAR_DECL
1939 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
1940 && nonlocal_vlas != NULL
1941 && TREE_CODE (value_expr) == INDIRECT_REF
1942 && TREE_CODE (TREE_OPERAND (value_expr, 0)) == VAR_DECL
1943 && decl_function_context (decl) != current_function_decl)
1945 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
1946 while (ctx && ctx->region_type == ORT_WORKSHARE)
1947 ctx = ctx->outer_context;
1948 if (!ctx && !pointer_set_insert (nonlocal_vlas, decl))
1950 tree copy = copy_node (decl), block;
1952 lang_hooks.dup_lang_specific_decl (copy);
1953 SET_DECL_RTL (copy, 0);
1954 TREE_USED (copy) = 1;
1955 block = DECL_INITIAL (current_function_decl);
1956 DECL_CHAIN (copy) = BLOCK_VARS (block);
1957 BLOCK_VARS (block) = copy;
1958 SET_DECL_VALUE_EXPR (copy, unshare_expr (value_expr));
1959 DECL_HAS_VALUE_EXPR_P (copy) = 1;
1963 *expr_p = unshare_expr (value_expr);
1970 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1974 : min_lval '[' val ']'
1976 | compound_lval '[' val ']'
1977 | compound_lval '.' ID
1979 This is not part of the original SIMPLE definition, which separates
1980 array and member references, but it seems reasonable to handle them
1981 together. Also, this way we don't run into problems with union
1982 aliasing; gcc requires that for accesses through a union to alias, the
1983 union reference must be explicit, which was not always the case when we
1984 were splitting up array and member refs.
1986 PRE_P points to the sequence where side effects that must happen before
1987 *EXPR_P should be stored.
1989 POST_P points to the sequence where side effects that must happen after
1990 *EXPR_P should be stored. */
1992 static enum gimplify_status
1993 gimplify_compound_lval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
1994 fallback_t fallback)
1997 VEC(tree,heap) *stack;
1998 enum gimplify_status ret = GS_ALL_DONE, tret;
2000 location_t loc = EXPR_LOCATION (*expr_p);
2001 tree expr = *expr_p;
2003 /* Create a stack of the subexpressions so later we can walk them in
2004 order from inner to outer. */
2005 stack = VEC_alloc (tree, heap, 10);
2007 /* We can handle anything that get_inner_reference can deal with. */
2008 for (p = expr_p; ; p = &TREE_OPERAND (*p, 0))
2011 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2012 if (TREE_CODE (*p) == INDIRECT_REF)
2013 *p = fold_indirect_ref_loc (loc, *p);
2015 if (handled_component_p (*p))
2017 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2018 additional COMPONENT_REFs. */
2019 else if ((TREE_CODE (*p) == VAR_DECL || TREE_CODE (*p) == PARM_DECL)
2020 && gimplify_var_or_parm_decl (p) == GS_OK)
2025 VEC_safe_push (tree, heap, stack, *p);
2028 gcc_assert (VEC_length (tree, stack));
2030 /* Now STACK is a stack of pointers to all the refs we've walked through
2031 and P points to the innermost expression.
2033 Java requires that we elaborated nodes in source order. That
2034 means we must gimplify the inner expression followed by each of
2035 the indices, in order. But we can't gimplify the inner
2036 expression until we deal with any variable bounds, sizes, or
2037 positions in order to deal with PLACEHOLDER_EXPRs.
2039 So we do this in three steps. First we deal with the annotations
2040 for any variables in the components, then we gimplify the base,
2041 then we gimplify any indices, from left to right. */
2042 for (i = VEC_length (tree, stack) - 1; i >= 0; i--)
2044 tree t = VEC_index (tree, stack, i);
2046 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2048 /* Gimplify the low bound and element type size and put them into
2049 the ARRAY_REF. If these values are set, they have already been
2051 if (TREE_OPERAND (t, 2) == NULL_TREE)
2053 tree low = unshare_expr (array_ref_low_bound (t));
2054 if (!is_gimple_min_invariant (low))
2056 TREE_OPERAND (t, 2) = low;
2057 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2058 post_p, is_gimple_reg,
2060 ret = MIN (ret, tret);
2065 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2066 is_gimple_reg, fb_rvalue);
2067 ret = MIN (ret, tret);
2070 if (TREE_OPERAND (t, 3) == NULL_TREE)
2072 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)));
2073 tree elmt_size = unshare_expr (array_ref_element_size (t));
2074 tree factor = size_int (TYPE_ALIGN_UNIT (elmt_type));
2076 /* Divide the element size by the alignment of the element
2079 = size_binop_loc (loc, EXACT_DIV_EXPR, elmt_size, factor);
2081 if (!is_gimple_min_invariant (elmt_size))
2083 TREE_OPERAND (t, 3) = elmt_size;
2084 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p,
2085 post_p, is_gimple_reg,
2087 ret = MIN (ret, tret);
2092 tret = gimplify_expr (&TREE_OPERAND (t, 3), pre_p, post_p,
2093 is_gimple_reg, fb_rvalue);
2094 ret = MIN (ret, tret);
2097 else if (TREE_CODE (t) == COMPONENT_REF)
2099 /* Set the field offset into T and gimplify it. */
2100 if (TREE_OPERAND (t, 2) == NULL_TREE)
2102 tree offset = unshare_expr (component_ref_field_offset (t));
2103 tree field = TREE_OPERAND (t, 1);
2105 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
2107 /* Divide the offset by its alignment. */
2108 offset = size_binop_loc (loc, EXACT_DIV_EXPR, offset, factor);
2110 if (!is_gimple_min_invariant (offset))
2112 TREE_OPERAND (t, 2) = offset;
2113 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p,
2114 post_p, is_gimple_reg,
2116 ret = MIN (ret, tret);
2121 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2122 is_gimple_reg, fb_rvalue);
2123 ret = MIN (ret, tret);
2128 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2129 so as to match the min_lval predicate. Failure to do so may result
2130 in the creation of large aggregate temporaries. */
2131 tret = gimplify_expr (p, pre_p, post_p, is_gimple_min_lval,
2132 fallback | fb_lvalue);
2133 ret = MIN (ret, tret);
2135 /* And finally, the indices and operands to BIT_FIELD_REF. During this
2136 loop we also remove any useless conversions. */
2137 for (; VEC_length (tree, stack) > 0; )
2139 tree t = VEC_pop (tree, stack);
2141 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
2143 /* Gimplify the dimension. */
2144 if (!is_gimple_min_invariant (TREE_OPERAND (t, 1)))
2146 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2147 is_gimple_val, fb_rvalue);
2148 ret = MIN (ret, tret);
2151 else if (TREE_CODE (t) == BIT_FIELD_REF)
2153 tret = gimplify_expr (&TREE_OPERAND (t, 1), pre_p, post_p,
2154 is_gimple_val, fb_rvalue);
2155 ret = MIN (ret, tret);
2156 tret = gimplify_expr (&TREE_OPERAND (t, 2), pre_p, post_p,
2157 is_gimple_val, fb_rvalue);
2158 ret = MIN (ret, tret);
2161 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t, 0));
2163 /* The innermost expression P may have originally had
2164 TREE_SIDE_EFFECTS set which would have caused all the outer
2165 expressions in *EXPR_P leading to P to also have had
2166 TREE_SIDE_EFFECTS set. */
2167 recalculate_side_effects (t);
2170 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2171 if ((fallback & fb_rvalue) && TREE_CODE (*expr_p) == COMPONENT_REF)
2173 canonicalize_component_ref (expr_p);
2176 VEC_free (tree, heap, stack);
2178 gcc_assert (*expr_p == expr || ret != GS_ALL_DONE);
2183 /* Gimplify the self modifying expression pointed to by EXPR_P
2186 PRE_P points to the list where side effects that must happen before
2187 *EXPR_P should be stored.
2189 POST_P points to the list where side effects that must happen after
2190 *EXPR_P should be stored.
2192 WANT_VALUE is nonzero iff we want to use the value of this expression
2193 in another expression. */
2195 static enum gimplify_status
2196 gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
2199 enum tree_code code;
2200 tree lhs, lvalue, rhs, t1;
2201 gimple_seq post = NULL, *orig_post_p = post_p;
2203 enum tree_code arith_code;
2204 enum gimplify_status ret;
2205 location_t loc = EXPR_LOCATION (*expr_p);
2207 code = TREE_CODE (*expr_p);
2209 gcc_assert (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR
2210 || code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR);
2212 /* Prefix or postfix? */
2213 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
2214 /* Faster to treat as prefix if result is not used. */
2215 postfix = want_value;
2219 /* For postfix, make sure the inner expression's post side effects
2220 are executed after side effects from this expression. */
2224 /* Add or subtract? */
2225 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2226 arith_code = PLUS_EXPR;
2228 arith_code = MINUS_EXPR;
2230 /* Gimplify the LHS into a GIMPLE lvalue. */
2231 lvalue = TREE_OPERAND (*expr_p, 0);
2232 ret = gimplify_expr (&lvalue, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
2233 if (ret == GS_ERROR)
2236 /* Extract the operands to the arithmetic operation. */
2238 rhs = TREE_OPERAND (*expr_p, 1);
2240 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2241 that as the result value and in the postqueue operation. We also
2242 make sure to make lvalue a minimal lval, see
2243 gcc.c-torture/execute/20040313-1.c for an example where this matters. */
2246 if (!is_gimple_min_lval (lvalue))
2248 mark_addressable (lvalue);
2249 lvalue = build_fold_addr_expr_loc (input_location, lvalue);
2250 gimplify_expr (&lvalue, pre_p, post_p, is_gimple_val, fb_rvalue);
2251 lvalue = build_fold_indirect_ref_loc (input_location, lvalue);
2253 ret = gimplify_expr (&lhs, pre_p, post_p, is_gimple_val, fb_rvalue);
2254 if (ret == GS_ERROR)
2258 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2259 if (POINTER_TYPE_P (TREE_TYPE (lhs)))
2261 rhs = convert_to_ptrofftype_loc (loc, rhs);
2262 if (arith_code == MINUS_EXPR)
2263 rhs = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (rhs), rhs);
2264 arith_code = POINTER_PLUS_EXPR;
2267 t1 = build2 (arith_code, TREE_TYPE (*expr_p), lhs, rhs);
2271 gimplify_assign (lvalue, t1, orig_post_p);
2272 gimplify_seq_add_seq (orig_post_p, post);
2278 *expr_p = build2 (MODIFY_EXPR, TREE_TYPE (lvalue), lvalue, t1);
2283 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2286 maybe_with_size_expr (tree *expr_p)
2288 tree expr = *expr_p;
2289 tree type = TREE_TYPE (expr);
2292 /* If we've already wrapped this or the type is error_mark_node, we can't do
2294 if (TREE_CODE (expr) == WITH_SIZE_EXPR
2295 || type == error_mark_node)
2298 /* If the size isn't known or is a constant, we have nothing to do. */
2299 size = TYPE_SIZE_UNIT (type);
2300 if (!size || TREE_CODE (size) == INTEGER_CST)
2303 /* Otherwise, make a WITH_SIZE_EXPR. */
2304 size = unshare_expr (size);
2305 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, expr);
2306 *expr_p = build2 (WITH_SIZE_EXPR, type, expr, size);
2309 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2310 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2313 static enum gimplify_status
2314 gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location)
2316 bool (*test) (tree);
2319 /* In general, we allow lvalues for function arguments to avoid
2320 extra overhead of copying large aggregates out of even larger
2321 aggregates into temporaries only to copy the temporaries to
2322 the argument list. Make optimizers happy by pulling out to
2323 temporaries those types that fit in registers. */
2324 if (is_gimple_reg_type (TREE_TYPE (*arg_p)))
2325 test = is_gimple_val, fb = fb_rvalue;
2328 test = is_gimple_lvalue, fb = fb_either;
2329 /* Also strip a TARGET_EXPR that would force an extra copy. */
2330 if (TREE_CODE (*arg_p) == TARGET_EXPR)
2332 tree init = TARGET_EXPR_INITIAL (*arg_p);
2334 && !VOID_TYPE_P (TREE_TYPE (init)))
2339 /* If this is a variable sized type, we must remember the size. */
2340 maybe_with_size_expr (arg_p);
2342 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2343 /* Make sure arguments have the same location as the function call
2345 protected_set_expr_location (*arg_p, call_location);
2347 /* There is a sequence point before a function call. Side effects in
2348 the argument list must occur before the actual call. So, when
2349 gimplifying arguments, force gimplify_expr to use an internal
2350 post queue which is then appended to the end of PRE_P. */
2351 return gimplify_expr (arg_p, pre_p, NULL, test, fb);
2354 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2355 WANT_VALUE is true if the result of the call is desired. */
2357 static enum gimplify_status
2358 gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
2360 tree fndecl, parms, p, fnptrtype;
2361 enum gimplify_status ret;
2364 bool builtin_va_start_p = FALSE;
2365 location_t loc = EXPR_LOCATION (*expr_p);
2367 gcc_assert (TREE_CODE (*expr_p) == CALL_EXPR);
2369 /* For reliable diagnostics during inlining, it is necessary that
2370 every call_expr be annotated with file and line. */
2371 if (! EXPR_HAS_LOCATION (*expr_p))
2372 SET_EXPR_LOCATION (*expr_p, input_location);
2374 /* This may be a call to a builtin function.
2376 Builtin function calls may be transformed into different
2377 (and more efficient) builtin function calls under certain
2378 circumstances. Unfortunately, gimplification can muck things
2379 up enough that the builtin expanders are not aware that certain
2380 transformations are still valid.
2382 So we attempt transformation/gimplification of the call before
2383 we gimplify the CALL_EXPR. At this time we do not manage to
2384 transform all calls in the same manner as the expanders do, but
2385 we do transform most of them. */
2386 fndecl = get_callee_fndecl (*expr_p);
2387 if (fndecl && DECL_BUILT_IN (fndecl))
2389 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2391 if (new_tree && new_tree != *expr_p)
2393 /* There was a transformation of this call which computes the
2394 same value, but in a more efficient way. Return and try
2400 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2401 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_VA_START)
2403 builtin_va_start_p = TRUE;
2404 if (call_expr_nargs (*expr_p) < 2)
2406 error ("too few arguments to function %<va_start%>");
2407 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2411 if (fold_builtin_next_arg (*expr_p, true))
2413 *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
2419 /* Remember the original function pointer type. */
2420 fnptrtype = TREE_TYPE (CALL_EXPR_FN (*expr_p));
2422 /* There is a sequence point before the call, so any side effects in
2423 the calling expression must occur before the actual call. Force
2424 gimplify_expr to use an internal post queue. */
2425 ret = gimplify_expr (&CALL_EXPR_FN (*expr_p), pre_p, NULL,
2426 is_gimple_call_addr, fb_rvalue);
2428 nargs = call_expr_nargs (*expr_p);
2430 /* Get argument types for verification. */
2431 fndecl = get_callee_fndecl (*expr_p);
2434 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2435 else if (POINTER_TYPE_P (TREE_TYPE (CALL_EXPR_FN (*expr_p))))
2436 parms = TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (*expr_p))));
2438 if (fndecl && DECL_ARGUMENTS (fndecl))
2439 p = DECL_ARGUMENTS (fndecl);
2444 for (i = 0; i < nargs && p; i++, p = TREE_CHAIN (p))
2447 /* If the last argument is __builtin_va_arg_pack () and it is not
2448 passed as a named argument, decrease the number of CALL_EXPR
2449 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2452 && TREE_CODE (CALL_EXPR_ARG (*expr_p, nargs - 1)) == CALL_EXPR)
2454 tree last_arg = CALL_EXPR_ARG (*expr_p, nargs - 1);
2455 tree last_arg_fndecl = get_callee_fndecl (last_arg);
2458 && TREE_CODE (last_arg_fndecl) == FUNCTION_DECL
2459 && DECL_BUILT_IN_CLASS (last_arg_fndecl) == BUILT_IN_NORMAL
2460 && DECL_FUNCTION_CODE (last_arg_fndecl) == BUILT_IN_VA_ARG_PACK)
2462 tree call = *expr_p;
2465 *expr_p = build_call_array_loc (loc, TREE_TYPE (call),
2466 CALL_EXPR_FN (call),
2467 nargs, CALL_EXPR_ARGP (call));
2469 /* Copy all CALL_EXPR flags, location and block, except
2470 CALL_EXPR_VA_ARG_PACK flag. */
2471 CALL_EXPR_STATIC_CHAIN (*expr_p) = CALL_EXPR_STATIC_CHAIN (call);
2472 CALL_EXPR_TAILCALL (*expr_p) = CALL_EXPR_TAILCALL (call);
2473 CALL_EXPR_RETURN_SLOT_OPT (*expr_p)
2474 = CALL_EXPR_RETURN_SLOT_OPT (call);
2475 CALL_FROM_THUNK_P (*expr_p) = CALL_FROM_THUNK_P (call);
2476 SET_EXPR_LOCATION (*expr_p, EXPR_LOCATION (call));
2477 TREE_BLOCK (*expr_p) = TREE_BLOCK (call);
2479 /* Set CALL_EXPR_VA_ARG_PACK. */
2480 CALL_EXPR_VA_ARG_PACK (*expr_p) = 1;
2484 /* Finally, gimplify the function arguments. */
2487 for (i = (PUSH_ARGS_REVERSED ? nargs - 1 : 0);
2488 PUSH_ARGS_REVERSED ? i >= 0 : i < nargs;
2489 PUSH_ARGS_REVERSED ? i-- : i++)
2491 enum gimplify_status t;
2493 /* Avoid gimplifying the second argument to va_start, which needs to
2494 be the plain PARM_DECL. */
2495 if ((i != 1) || !builtin_va_start_p)
2497 t = gimplify_arg (&CALL_EXPR_ARG (*expr_p, i), pre_p,
2498 EXPR_LOCATION (*expr_p));
2506 /* Verify the function result. */
2507 if (want_value && fndecl
2508 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype))))
2510 error_at (loc, "using result of function returning %<void%>");
2514 /* Try this again in case gimplification exposed something. */
2515 if (ret != GS_ERROR)
2517 tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
2519 if (new_tree && new_tree != *expr_p)
2521 /* There was a transformation of this call which computes the
2522 same value, but in a more efficient way. Return and try
2530 *expr_p = error_mark_node;
2534 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2535 decl. This allows us to eliminate redundant or useless
2536 calls to "const" functions. */
2537 if (TREE_CODE (*expr_p) == CALL_EXPR)
2539 int flags = call_expr_flags (*expr_p);
2540 if (flags & (ECF_CONST | ECF_PURE)
2541 /* An infinite loop is considered a side effect. */
2542 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
2543 TREE_SIDE_EFFECTS (*expr_p) = 0;
2546 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2547 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2548 form and delegate the creation of a GIMPLE_CALL to
2549 gimplify_modify_expr. This is always possible because when
2550 WANT_VALUE is true, the caller wants the result of this call into
2551 a temporary, which means that we will emit an INIT_EXPR in
2552 internal_get_tmp_var which will then be handled by
2553 gimplify_modify_expr. */
2556 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2557 have to do is replicate it as a GIMPLE_CALL tuple. */
2558 gimple_stmt_iterator gsi;
2559 call = gimple_build_call_from_tree (*expr_p);
2560 gimple_call_set_fntype (call, TREE_TYPE (fnptrtype));
2561 gimplify_seq_add_stmt (pre_p, call);
2562 gsi = gsi_last (*pre_p);
2564 *expr_p = NULL_TREE;
2567 /* Remember the original function type. */
2568 CALL_EXPR_FN (*expr_p) = build1 (NOP_EXPR, fnptrtype,
2569 CALL_EXPR_FN (*expr_p));
2574 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2575 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2577 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2578 condition is true or false, respectively. If null, we should generate
2579 our own to skip over the evaluation of this specific expression.
2581 LOCUS is the source location of the COND_EXPR.
2583 This function is the tree equivalent of do_jump.
2585 shortcut_cond_r should only be called by shortcut_cond_expr. */
2588 shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
2591 tree local_label = NULL_TREE;
2592 tree t, expr = NULL;
2594 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2595 retain the shortcut semantics. Just insert the gotos here;
2596 shortcut_cond_expr will append the real blocks later. */
2597 if (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2599 location_t new_locus;
2601 /* Turn if (a && b) into
2603 if (a); else goto no;
2604 if (b) goto yes; else goto no;
2607 if (false_label_p == NULL)
2608 false_label_p = &local_label;
2610 /* Keep the original source location on the first 'if'. */
2611 t = shortcut_cond_r (TREE_OPERAND (pred, 0), NULL, false_label_p, locus);
2612 append_to_statement_list (t, &expr);
2614 /* Set the source location of the && on the second 'if'. */
2615 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2616 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2618 append_to_statement_list (t, &expr);
2620 else if (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2622 location_t new_locus;
2624 /* Turn if (a || b) into
2627 if (b) goto yes; else goto no;
2630 if (true_label_p == NULL)
2631 true_label_p = &local_label;
2633 /* Keep the original source location on the first 'if'. */
2634 t = shortcut_cond_r (TREE_OPERAND (pred, 0), true_label_p, NULL, locus);
2635 append_to_statement_list (t, &expr);
2637 /* Set the source location of the || on the second 'if'. */
2638 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2639 t = shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p, false_label_p,
2641 append_to_statement_list (t, &expr);
2643 else if (TREE_CODE (pred) == COND_EXPR
2644 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
2645 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
2647 location_t new_locus;
2649 /* As long as we're messing with gotos, turn if (a ? b : c) into
2651 if (b) goto yes; else goto no;
2653 if (c) goto yes; else goto no;
2655 Don't do this if one of the arms has void type, which can happen
2656 in C++ when the arm is throw. */
2658 /* Keep the original source location on the first 'if'. Set the source
2659 location of the ? on the second 'if'. */
2660 new_locus = EXPR_HAS_LOCATION (pred) ? EXPR_LOCATION (pred) : locus;
2661 expr = build3 (COND_EXPR, void_type_node, TREE_OPERAND (pred, 0),
2662 shortcut_cond_r (TREE_OPERAND (pred, 1), true_label_p,
2663 false_label_p, locus),
2664 shortcut_cond_r (TREE_OPERAND (pred, 2), true_label_p,
2665 false_label_p, new_locus));
2669 expr = build3 (COND_EXPR, void_type_node, pred,
2670 build_and_jump (true_label_p),
2671 build_and_jump (false_label_p));
2672 SET_EXPR_LOCATION (expr, locus);
2677 t = build1 (LABEL_EXPR, void_type_node, local_label);
2678 append_to_statement_list (t, &expr);
2684 /* Given a conditional expression EXPR with short-circuit boolean
2685 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2686 predicate appart into the equivalent sequence of conditionals. */
2689 shortcut_cond_expr (tree expr)
2691 tree pred = TREE_OPERAND (expr, 0);
2692 tree then_ = TREE_OPERAND (expr, 1);
2693 tree else_ = TREE_OPERAND (expr, 2);
2694 tree true_label, false_label, end_label, t;
2696 tree *false_label_p;
2697 bool emit_end, emit_false, jump_over_else;
2698 bool then_se = then_ && TREE_SIDE_EFFECTS (then_);
2699 bool else_se = else_ && TREE_SIDE_EFFECTS (else_);
2701 /* First do simple transformations. */
2704 /* If there is no 'else', turn
2707 if (a) if (b) then c. */
2708 while (TREE_CODE (pred) == TRUTH_ANDIF_EXPR)
2710 /* Keep the original source location on the first 'if'. */
2711 location_t locus = EXPR_LOC_OR_HERE (expr);
2712 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2713 /* Set the source location of the && on the second 'if'. */
2714 if (EXPR_HAS_LOCATION (pred))
2715 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2716 then_ = shortcut_cond_expr (expr);
2717 then_se = then_ && TREE_SIDE_EFFECTS (then_);
2718 pred = TREE_OPERAND (pred, 0);
2719 expr = build3 (COND_EXPR, void_type_node, pred, then_, NULL_TREE);
2720 SET_EXPR_LOCATION (expr, locus);
2726 /* If there is no 'then', turn
2729 if (a); else if (b); else d. */
2730 while (TREE_CODE (pred) == TRUTH_ORIF_EXPR)
2732 /* Keep the original source location on the first 'if'. */
2733 location_t locus = EXPR_LOC_OR_HERE (expr);
2734 TREE_OPERAND (expr, 0) = TREE_OPERAND (pred, 1);
2735 /* Set the source location of the || on the second 'if'. */
2736 if (EXPR_HAS_LOCATION (pred))
2737 SET_EXPR_LOCATION (expr, EXPR_LOCATION (pred));
2738 else_ = shortcut_cond_expr (expr);
2739 else_se = else_ && TREE_SIDE_EFFECTS (else_);
2740 pred = TREE_OPERAND (pred, 0);
2741 expr = build3 (COND_EXPR, void_type_node, pred, NULL_TREE, else_);
2742 SET_EXPR_LOCATION (expr, locus);
2746 /* If we're done, great. */
2747 if (TREE_CODE (pred) != TRUTH_ANDIF_EXPR
2748 && TREE_CODE (pred) != TRUTH_ORIF_EXPR)
2751 /* Otherwise we need to mess with gotos. Change
2754 if (a); else goto no;
2757 and recursively gimplify the condition. */
2759 true_label = false_label = end_label = NULL_TREE;
2761 /* If our arms just jump somewhere, hijack those labels so we don't
2762 generate jumps to jumps. */
2765 && TREE_CODE (then_) == GOTO_EXPR
2766 && TREE_CODE (GOTO_DESTINATION (then_)) == LABEL_DECL)
2768 true_label = GOTO_DESTINATION (then_);
2774 && TREE_CODE (else_) == GOTO_EXPR
2775 && TREE_CODE (GOTO_DESTINATION (else_)) == LABEL_DECL)
2777 false_label = GOTO_DESTINATION (else_);
2782 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2784 true_label_p = &true_label;
2786 true_label_p = NULL;
2788 /* The 'else' branch also needs a label if it contains interesting code. */
2789 if (false_label || else_se)
2790 false_label_p = &false_label;
2792 false_label_p = NULL;
2794 /* If there was nothing else in our arms, just forward the label(s). */
2795 if (!then_se && !else_se)
2796 return shortcut_cond_r (pred, true_label_p, false_label_p,
2797 EXPR_LOC_OR_HERE (expr));
2799 /* If our last subexpression already has a terminal label, reuse it. */
2801 t = expr_last (else_);
2803 t = expr_last (then_);
2806 if (t && TREE_CODE (t) == LABEL_EXPR)
2807 end_label = LABEL_EXPR_LABEL (t);
2809 /* If we don't care about jumping to the 'else' branch, jump to the end
2810 if the condition is false. */
2812 false_label_p = &end_label;
2814 /* We only want to emit these labels if we aren't hijacking them. */
2815 emit_end = (end_label == NULL_TREE);
2816 emit_false = (false_label == NULL_TREE);
2818 /* We only emit the jump over the else clause if we have to--if the
2819 then clause may fall through. Otherwise we can wind up with a
2820 useless jump and a useless label at the end of gimplified code,
2821 which will cause us to think that this conditional as a whole
2822 falls through even if it doesn't. If we then inline a function
2823 which ends with such a condition, that can cause us to issue an
2824 inappropriate warning about control reaching the end of a
2825 non-void function. */
2826 jump_over_else = block_may_fallthru (then_);
2828 pred = shortcut_cond_r (pred, true_label_p, false_label_p,
2829 EXPR_LOC_OR_HERE (expr));
2832 append_to_statement_list (pred, &expr);
2834 append_to_statement_list (then_, &expr);
2839 tree last = expr_last (expr);
2840 t = build_and_jump (&end_label);
2841 if (EXPR_HAS_LOCATION (last))
2842 SET_EXPR_LOCATION (t, EXPR_LOCATION (last));
2843 append_to_statement_list (t, &expr);
2847 t = build1 (LABEL_EXPR, void_type_node, false_label);
2848 append_to_statement_list (t, &expr);
2850 append_to_statement_list (else_, &expr);
2852 if (emit_end && end_label)
2854 t = build1 (LABEL_EXPR, void_type_node, end_label);
2855 append_to_statement_list (t, &expr);
2861 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2864 gimple_boolify (tree expr)
2866 tree type = TREE_TYPE (expr);
2867 location_t loc = EXPR_LOCATION (expr);
2869 if (TREE_CODE (expr) == NE_EXPR
2870 && TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR
2871 && integer_zerop (TREE_OPERAND (expr, 1)))
2873 tree call = TREE_OPERAND (expr, 0);
2874 tree fn = get_callee_fndecl (call);
2876 /* For __builtin_expect ((long) (x), y) recurse into x as well
2877 if x is truth_value_p. */
2879 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2880 && DECL_FUNCTION_CODE (fn) == BUILT_IN_EXPECT
2881 && call_expr_nargs (call) == 2)
2883 tree arg = CALL_EXPR_ARG (call, 0);
2886 if (TREE_CODE (arg) == NOP_EXPR
2887 && TREE_TYPE (arg) == TREE_TYPE (call))
2888 arg = TREE_OPERAND (arg, 0);
2889 if (truth_value_p (TREE_CODE (arg)))
2891 arg = gimple_boolify (arg);
2892 CALL_EXPR_ARG (call, 0)
2893 = fold_convert_loc (loc, TREE_TYPE (call), arg);
2899 switch (TREE_CODE (expr))
2901 case TRUTH_AND_EXPR:
2903 case TRUTH_XOR_EXPR:
2904 case TRUTH_ANDIF_EXPR:
2905 case TRUTH_ORIF_EXPR:
2906 /* Also boolify the arguments of truth exprs. */
2907 TREE_OPERAND (expr, 1) = gimple_boolify (TREE_OPERAND (expr, 1));
2910 case TRUTH_NOT_EXPR:
2911 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
2913 /* These expressions always produce boolean results. */
2914 if (TREE_CODE (type) != BOOLEAN_TYPE)
2915 TREE_TYPE (expr) = boolean_type_node;
2919 if (COMPARISON_CLASS_P (expr))
2921 /* There expressions always prduce boolean results. */
2922 if (TREE_CODE (type) != BOOLEAN_TYPE)
2923 TREE_TYPE (expr) = boolean_type_node;
2926 /* Other expressions that get here must have boolean values, but
2927 might need to be converted to the appropriate mode. */
2928 if (TREE_CODE (type) == BOOLEAN_TYPE)
2930 return fold_convert_loc (loc, boolean_type_node, expr);
2934 /* Given a conditional expression *EXPR_P without side effects, gimplify
2935 its operands. New statements are inserted to PRE_P. */
2937 static enum gimplify_status
2938 gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p)
2940 tree expr = *expr_p, cond;
2941 enum gimplify_status ret, tret;
2942 enum tree_code code;
2944 cond = gimple_boolify (COND_EXPR_COND (expr));
2946 /* We need to handle && and || specially, as their gimplification
2947 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
2948 code = TREE_CODE (cond);
2949 if (code == TRUTH_ANDIF_EXPR)
2950 TREE_SET_CODE (cond, TRUTH_AND_EXPR);
2951 else if (code == TRUTH_ORIF_EXPR)
2952 TREE_SET_CODE (cond, TRUTH_OR_EXPR);
2953 ret = gimplify_expr (&cond, pre_p, NULL, is_gimple_condexpr, fb_rvalue);
2954 COND_EXPR_COND (*expr_p) = cond;
2956 tret = gimplify_expr (&COND_EXPR_THEN (expr), pre_p, NULL,
2957 is_gimple_val, fb_rvalue);
2958 ret = MIN (ret, tret);
2959 tret = gimplify_expr (&COND_EXPR_ELSE (expr), pre_p, NULL,
2960 is_gimple_val, fb_rvalue);
2962 return MIN (ret, tret);
2965 /* Return true if evaluating EXPR could trap.
2966 EXPR is GENERIC, while tree_could_trap_p can be called
2970 generic_expr_could_trap_p (tree expr)
2974 if (!expr || is_gimple_val (expr))
2977 if (!EXPR_P (expr) || tree_could_trap_p (expr))
2980 n = TREE_OPERAND_LENGTH (expr);
2981 for (i = 0; i < n; i++)
2982 if (generic_expr_could_trap_p (TREE_OPERAND (expr, i)))
2988 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
2997 The second form is used when *EXPR_P is of type void.
2999 PRE_P points to the list where side effects that must happen before
3000 *EXPR_P should be stored. */
3002 static enum gimplify_status
3003 gimplify_cond_expr (tree *expr_p, gimple_seq *pre_p, fallback_t fallback)
3005 tree expr = *expr_p;
3006 tree type = TREE_TYPE (expr);
3007 location_t loc = EXPR_LOCATION (expr);
3008 tree tmp, arm1, arm2;
3009 enum gimplify_status ret;
3010 tree label_true, label_false, label_cont;
3011 bool have_then_clause_p, have_else_clause_p;
3013 enum tree_code pred_code;
3014 gimple_seq seq = NULL;
3016 /* If this COND_EXPR has a value, copy the values into a temporary within
3018 if (!VOID_TYPE_P (type))
3020 tree then_ = TREE_OPERAND (expr, 1), else_ = TREE_OPERAND (expr, 2);
3023 /* If either an rvalue is ok or we do not require an lvalue, create the
3024 temporary. But we cannot do that if the type is addressable. */
3025 if (((fallback & fb_rvalue) || !(fallback & fb_lvalue))
3026 && !TREE_ADDRESSABLE (type))
3028 if (gimplify_ctxp->allow_rhs_cond_expr
3029 /* If either branch has side effects or could trap, it can't be
3030 evaluated unconditionally. */
3031 && !TREE_SIDE_EFFECTS (then_)
3032 && !generic_expr_could_trap_p (then_)
3033 && !TREE_SIDE_EFFECTS (else_)
3034 && !generic_expr_could_trap_p (else_))
3035 return gimplify_pure_cond_expr (expr_p, pre_p);
3037 tmp = create_tmp_var (type, "iftmp");
3041 /* Otherwise, only create and copy references to the values. */
3044 type = build_pointer_type (type);
3046 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3047 then_ = build_fold_addr_expr_loc (loc, then_);
3049 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3050 else_ = build_fold_addr_expr_loc (loc, else_);
3053 = build3 (COND_EXPR, type, TREE_OPERAND (expr, 0), then_, else_);
3055 tmp = create_tmp_var (type, "iftmp");
3056 result = build_simple_mem_ref_loc (loc, tmp);
3059 /* Build the new then clause, `tmp = then_;'. But don't build the
3060 assignment if the value is void; in C++ it can be if it's a throw. */
3061 if (!VOID_TYPE_P (TREE_TYPE (then_)))
3062 TREE_OPERAND (expr, 1) = build2 (MODIFY_EXPR, type, tmp, then_);
3064 /* Similarly, build the new else clause, `tmp = else_;'. */
3065 if (!VOID_TYPE_P (TREE_TYPE (else_)))
3066 TREE_OPERAND (expr, 2) = build2 (MODIFY_EXPR, type, tmp, else_);
3068 TREE_TYPE (expr) = void_type_node;
3069 recalculate_side_effects (expr);
3071 /* Move the COND_EXPR to the prequeue. */
3072 gimplify_stmt (&expr, pre_p);
3078 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3079 STRIP_TYPE_NOPS (TREE_OPERAND (expr, 0));
3080 if (TREE_CODE (TREE_OPERAND (expr, 0)) == COMPOUND_EXPR)
3081 gimplify_compound_expr (&TREE_OPERAND (expr, 0), pre_p, true);
3083 /* Make sure the condition has BOOLEAN_TYPE. */
3084 TREE_OPERAND (expr, 0) = gimple_boolify (TREE_OPERAND (expr, 0));
3086 /* Break apart && and || conditions. */
3087 if (TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ANDIF_EXPR
3088 || TREE_CODE (TREE_OPERAND (expr, 0)) == TRUTH_ORIF_EXPR)
3090 expr = shortcut_cond_expr (expr);
3092 if (expr != *expr_p)
3096 /* We can't rely on gimplify_expr to re-gimplify the expanded
3097 form properly, as cleanups might cause the target labels to be
3098 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3099 set up a conditional context. */
3100 gimple_push_condition ();
3101 gimplify_stmt (expr_p, &seq);
3102 gimple_pop_condition (pre_p);
3103 gimple_seq_add_seq (pre_p, seq);
3109 /* Now do the normal gimplification. */
3111 /* Gimplify condition. */
3112 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, NULL, is_gimple_condexpr,
3114 if (ret == GS_ERROR)
3116 gcc_assert (TREE_OPERAND (expr, 0) != NULL_TREE);
3118 gimple_push_condition ();
3120 have_then_clause_p = have_else_clause_p = false;
3121 if (TREE_OPERAND (expr, 1) != NULL
3122 && TREE_CODE (TREE_OPERAND (expr, 1)) == GOTO_EXPR
3123 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 1))) == LABEL_DECL
3124 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 1)))
3125 == current_function_decl)
3126 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3127 have different locations, otherwise we end up with incorrect
3128 location information on the branches. */
3130 || !EXPR_HAS_LOCATION (expr)
3131 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 1))
3132 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 1))))
3134 label_true = GOTO_DESTINATION (TREE_OPERAND (expr, 1));
3135 have_then_clause_p = true;
3138 label_true = create_artificial_label (UNKNOWN_LOCATION);
3139 if (TREE_OPERAND (expr, 2) != NULL
3140 && TREE_CODE (TREE_OPERAND (expr, 2)) == GOTO_EXPR
3141 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr, 2))) == LABEL_DECL
3142 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr, 2)))
3143 == current_function_decl)
3144 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3145 have different locations, otherwise we end up with incorrect
3146 location information on the branches. */
3148 || !EXPR_HAS_LOCATION (expr)
3149 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr, 2))
3150 || EXPR_LOCATION (expr) == EXPR_LOCATION (TREE_OPERAND (expr, 2))))
3152 label_false = GOTO_DESTINATION (TREE_OPERAND (expr, 2));
3153 have_else_clause_p = true;
3156 label_false = create_artificial_label (UNKNOWN_LOCATION);
3158 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr), &pred_code, &arm1,
3161 gimple_cond = gimple_build_cond (pred_code, arm1, arm2, label_true,
3164 gimplify_seq_add_stmt (&seq, gimple_cond);
3165 label_cont = NULL_TREE;
3166 if (!have_then_clause_p)
3168 /* For if (...) {} else { code; } put label_true after
3170 if (TREE_OPERAND (expr, 1) == NULL_TREE
3171 && !have_else_clause_p
3172 && TREE_OPERAND (expr, 2) != NULL_TREE)
3173 label_cont = label_true;
3176 gimplify_seq_add_stmt (&seq, gimple_build_label (label_true));
3177 have_then_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 1), &seq);
3178 /* For if (...) { code; } else {} or
3179 if (...) { code; } else goto label; or
3180 if (...) { code; return; } else { ... }
3181 label_cont isn't needed. */
3182 if (!have_else_clause_p
3183 && TREE_OPERAND (expr, 2) != NULL_TREE
3184 && gimple_seq_may_fallthru (seq))
3187 label_cont = create_artificial_label (UNKNOWN_LOCATION);
3189 g = gimple_build_goto (label_cont);
3191 /* GIMPLE_COND's are very low level; they have embedded
3192 gotos. This particular embedded goto should not be marked
3193 with the location of the original COND_EXPR, as it would
3194 correspond to the COND_EXPR's condition, not the ELSE or the
3195 THEN arms. To avoid marking it with the wrong location, flag
3196 it as "no location". */
3197 gimple_set_do_not_emit_location (g);
3199 gimplify_seq_add_stmt (&seq, g);
3203 if (!have_else_clause_p)
3205 gimplify_seq_add_stmt (&seq, gimple_build_label (label_false));
3206 have_else_clause_p = gimplify_stmt (&TREE_OPERAND (expr, 2), &seq);
3209 gimplify_seq_add_stmt (&seq, gimple_build_label (label_cont));
3211 gimple_pop_condition (pre_p);
3212 gimple_seq_add_seq (pre_p, seq);
3214 if (ret == GS_ERROR)
3216 else if (have_then_clause_p || have_else_clause_p)
3220 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3221 expr = TREE_OPERAND (expr, 0);
3222 gimplify_stmt (&expr, pre_p);
3229 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3230 to be marked addressable.
3232 We cannot rely on such an expression being directly markable if a temporary
3233 has been created by the gimplification. In this case, we create another
3234 temporary and initialize it with a copy, which will become a store after we
3235 mark it addressable. This can happen if the front-end passed us something
3236 that it could not mark addressable yet, like a Fortran pass-by-reference
3237 parameter (int) floatvar. */
3240 prepare_gimple_addressable (tree *expr_p, gimple_seq *seq_p)
3242 while (handled_component_p (*expr_p))
3243 expr_p = &TREE_OPERAND (*expr_p, 0);
3244 if (is_gimple_reg (*expr_p))
3245 *expr_p = get_initialized_tmp_var (*expr_p, seq_p, NULL);
3248 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3249 a call to __builtin_memcpy. */
3251 static enum gimplify_status
3252 gimplify_modify_expr_to_memcpy (tree *expr_p, tree size, bool want_value,
3255 tree t, to, to_ptr, from, from_ptr;
3257 location_t loc = EXPR_LOCATION (*expr_p);
3259 to = TREE_OPERAND (*expr_p, 0);
3260 from = TREE_OPERAND (*expr_p, 1);
3262 /* Mark the RHS addressable. Beware that it may not be possible to do so
3263 directly if a temporary has been created by the gimplification. */
3264 prepare_gimple_addressable (&from, seq_p);
3266 mark_addressable (from);
3267 from_ptr = build_fold_addr_expr_loc (loc, from);
3268 gimplify_arg (&from_ptr, seq_p, loc);
3270 mark_addressable (to);
3271 to_ptr = build_fold_addr_expr_loc (loc, to);
3272 gimplify_arg (&to_ptr, seq_p, loc);
3274 t = builtin_decl_implicit (BUILT_IN_MEMCPY);
3276 gs = gimple_build_call (t, 3, to_ptr, from_ptr, size);
3280 /* tmp = memcpy() */
3281 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3282 gimple_call_set_lhs (gs, t);
3283 gimplify_seq_add_stmt (seq_p, gs);
3285 *expr_p = build_simple_mem_ref (t);
3289 gimplify_seq_add_stmt (seq_p, gs);
3294 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3295 a call to __builtin_memset. In this case we know that the RHS is
3296 a CONSTRUCTOR with an empty element list. */
3298 static enum gimplify_status
3299 gimplify_modify_expr_to_memset (tree *expr_p, tree size, bool want_value,
3302 tree t, from, to, to_ptr;
3304 location_t loc = EXPR_LOCATION (*expr_p);
3306 /* Assert our assumptions, to abort instead of producing wrong code
3307 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3308 not be immediately exposed. */
3309 from = TREE_OPERAND (*expr_p, 1);
3310 if (TREE_CODE (from) == WITH_SIZE_EXPR)
3311 from = TREE_OPERAND (from, 0);
3313 gcc_assert (TREE_CODE (from) == CONSTRUCTOR
3314 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (from)));
3317 to = TREE_OPERAND (*expr_p, 0);
3319 to_ptr = build_fold_addr_expr_loc (loc, to);
3320 gimplify_arg (&to_ptr, seq_p, loc);
3321 t = builtin_decl_implicit (BUILT_IN_MEMSET);
3323 gs = gimple_build_call (t, 3, to_ptr, integer_zero_node, size);
3327 /* tmp = memset() */
3328 t = create_tmp_var (TREE_TYPE (to_ptr), NULL);
3329 gimple_call_set_lhs (gs, t);
3330 gimplify_seq_add_stmt (seq_p, gs);
3332 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (to), t);
3336 gimplify_seq_add_stmt (seq_p, gs);
3341 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3342 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3343 assignment. Return non-null if we detect a potential overlap. */
3345 struct gimplify_init_ctor_preeval_data
3347 /* The base decl of the lhs object. May be NULL, in which case we
3348 have to assume the lhs is indirect. */
3351 /* The alias set of the lhs object. */
3352 alias_set_type lhs_alias_set;
3356 gimplify_init_ctor_preeval_1 (tree *tp, int *walk_subtrees, void *xdata)
3358 struct gimplify_init_ctor_preeval_data *data
3359 = (struct gimplify_init_ctor_preeval_data *) xdata;
3362 /* If we find the base object, obviously we have overlap. */
3363 if (data->lhs_base_decl == t)
3366 /* If the constructor component is indirect, determine if we have a
3367 potential overlap with the lhs. The only bits of information we
3368 have to go on at this point are addressability and alias sets. */
3369 if ((INDIRECT_REF_P (t)
3370 || TREE_CODE (t) == MEM_REF)
3371 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3372 && alias_sets_conflict_p (data->lhs_alias_set, get_alias_set (t)))
3375 /* If the constructor component is a call, determine if it can hide a
3376 potential overlap with the lhs through an INDIRECT_REF like above.
3377 ??? Ugh - this is completely broken. In fact this whole analysis
3378 doesn't look conservative. */
3379 if (TREE_CODE (t) == CALL_EXPR)
3381 tree type, fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t)));
3383 for (type = TYPE_ARG_TYPES (fntype); type; type = TREE_CHAIN (type))
3384 if (POINTER_TYPE_P (TREE_VALUE (type))
3385 && (!data->lhs_base_decl || TREE_ADDRESSABLE (data->lhs_base_decl))
3386 && alias_sets_conflict_p (data->lhs_alias_set,
3388 (TREE_TYPE (TREE_VALUE (type)))))
3392 if (IS_TYPE_OR_DECL_P (t))
3397 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3398 force values that overlap with the lhs (as described by *DATA)
3399 into temporaries. */
3402 gimplify_init_ctor_preeval (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3403 struct gimplify_init_ctor_preeval_data *data)
3405 enum gimplify_status one;
3407 /* If the value is constant, then there's nothing to pre-evaluate. */
3408 if (TREE_CONSTANT (*expr_p))
3410 /* Ensure it does not have side effects, it might contain a reference to
3411 the object we're initializing. */
3412 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p));
3416 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3417 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p)))
3420 /* Recurse for nested constructors. */
3421 if (TREE_CODE (*expr_p) == CONSTRUCTOR)
3423 unsigned HOST_WIDE_INT ix;
3424 constructor_elt *ce;
3425 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (*expr_p);
3427 FOR_EACH_VEC_ELT (constructor_elt, v, ix, ce)
3428 gimplify_init_ctor_preeval (&ce->value, pre_p, post_p, data);
3433 /* If this is a variable sized type, we must remember the size. */
3434 maybe_with_size_expr (expr_p);
3436 /* Gimplify the constructor element to something appropriate for the rhs
3437 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3438 the gimplifier will consider this a store to memory. Doing this
3439 gimplification now means that we won't have to deal with complicated
3440 language-specific trees, nor trees like SAVE_EXPR that can induce
3441 exponential search behavior. */
3442 one = gimplify_expr (expr_p, pre_p, post_p, is_gimple_mem_rhs, fb_rvalue);
3443 if (one == GS_ERROR)
3449 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3450 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3451 always be true for all scalars, since is_gimple_mem_rhs insists on a
3452 temporary variable for them. */
3453 if (DECL_P (*expr_p))
3456 /* If this is of variable size, we have no choice but to assume it doesn't
3457 overlap since we can't make a temporary for it. */
3458 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p))) != INTEGER_CST)
3461 /* Otherwise, we must search for overlap ... */
3462 if (!walk_tree (expr_p, gimplify_init_ctor_preeval_1, data, NULL))
3465 /* ... and if found, force the value into a temporary. */
3466 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
3469 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3470 a RANGE_EXPR in a CONSTRUCTOR for an array.
3474 object[var] = value;
3481 We increment var _after_ the loop exit check because we might otherwise
3482 fail if upper == TYPE_MAX_VALUE (type for upper).
3484 Note that we never have to deal with SAVE_EXPRs here, because this has
3485 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3487 static void gimplify_init_ctor_eval (tree, VEC(constructor_elt,gc) *,
3488 gimple_seq *, bool);
3491 gimplify_init_ctor_eval_range (tree object, tree lower, tree upper,
3492 tree value, tree array_elt_type,
3493 gimple_seq *pre_p, bool cleared)
3495 tree loop_entry_label, loop_exit_label, fall_thru_label;
3496 tree var, var_type, cref, tmp;
3498 loop_entry_label = create_artificial_label (UNKNOWN_LOCATION);
3499 loop_exit_label = create_artificial_label (UNKNOWN_LOCATION);
3500 fall_thru_label = create_artificial_label (UNKNOWN_LOCATION);
3502 /* Create and initialize the index variable. */
3503 var_type = TREE_TYPE (upper);
3504 var = create_tmp_var (var_type, NULL);
3505 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, lower));
3507 /* Add the loop entry label. */
3508 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_entry_label));
3510 /* Build the reference. */
3511 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3512 var, NULL_TREE, NULL_TREE);
3514 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3515 the store. Otherwise just assign value to the reference. */
3517 if (TREE_CODE (value) == CONSTRUCTOR)
3518 /* NB we might have to call ourself recursively through
3519 gimplify_init_ctor_eval if the value is a constructor. */
3520 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3523 gimplify_seq_add_stmt (pre_p, gimple_build_assign (cref, value));
3525 /* We exit the loop when the index var is equal to the upper bound. */
3526 gimplify_seq_add_stmt (pre_p,
3527 gimple_build_cond (EQ_EXPR, var, upper,
3528 loop_exit_label, fall_thru_label));
3530 gimplify_seq_add_stmt (pre_p, gimple_build_label (fall_thru_label));
3532 /* Otherwise, increment the index var... */
3533 tmp = build2 (PLUS_EXPR, var_type, var,
3534 fold_convert (var_type, integer_one_node));
3535 gimplify_seq_add_stmt (pre_p, gimple_build_assign (var, tmp));
3537 /* ...and jump back to the loop entry. */
3538 gimplify_seq_add_stmt (pre_p, gimple_build_goto (loop_entry_label));
3540 /* Add the loop exit label. */
3541 gimplify_seq_add_stmt (pre_p, gimple_build_label (loop_exit_label));
3544 /* Return true if FDECL is accessing a field that is zero sized. */
3547 zero_sized_field_decl (const_tree fdecl)
3549 if (TREE_CODE (fdecl) == FIELD_DECL && DECL_SIZE (fdecl)
3550 && integer_zerop (DECL_SIZE (fdecl)))
3555 /* Return true if TYPE is zero sized. */
3558 zero_sized_type (const_tree type)
3560 if (AGGREGATE_TYPE_P (type) && TYPE_SIZE (type)
3561 && integer_zerop (TYPE_SIZE (type)))
3566 /* A subroutine of gimplify_init_constructor. Generate individual
3567 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3568 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3569 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3573 gimplify_init_ctor_eval (tree object, VEC(constructor_elt,gc) *elts,
3574 gimple_seq *pre_p, bool cleared)
3576 tree array_elt_type = NULL;
3577 unsigned HOST_WIDE_INT ix;
3578 tree purpose, value;
3580 if (TREE_CODE (TREE_TYPE (object)) == ARRAY_TYPE)
3581 array_elt_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object)));
3583 FOR_EACH_CONSTRUCTOR_ELT (elts, ix, purpose, value)
3587 /* NULL values are created above for gimplification errors. */
3591 if (cleared && initializer_zerop (value))
3594 /* ??? Here's to hoping the front end fills in all of the indices,
3595 so we don't have to figure out what's missing ourselves. */
3596 gcc_assert (purpose);
3598 /* Skip zero-sized fields, unless value has side-effects. This can
3599 happen with calls to functions returning a zero-sized type, which
3600 we shouldn't discard. As a number of downstream passes don't
3601 expect sets of zero-sized fields, we rely on the gimplification of
3602 the MODIFY_EXPR we make below to drop the assignment statement. */
3603 if (! TREE_SIDE_EFFECTS (value) && zero_sized_field_decl (purpose))
3606 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3608 if (TREE_CODE (purpose) == RANGE_EXPR)
3610 tree lower = TREE_OPERAND (purpose, 0);
3611 tree upper = TREE_OPERAND (purpose, 1);
3613 /* If the lower bound is equal to upper, just treat it as if
3614 upper was the index. */
3615 if (simple_cst_equal (lower, upper))
3619 gimplify_init_ctor_eval_range (object, lower, upper, value,
3620 array_elt_type, pre_p, cleared);
3627 /* Do not use bitsizetype for ARRAY_REF indices. */
3628 if (TYPE_DOMAIN (TREE_TYPE (object)))
3630 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object))),
3632 cref = build4 (ARRAY_REF, array_elt_type, unshare_expr (object),
3633 purpose, NULL_TREE, NULL_TREE);
3637 gcc_assert (TREE_CODE (purpose) == FIELD_DECL);
3638 cref = build3 (COMPONENT_REF, TREE_TYPE (purpose),
3639 unshare_expr (object), purpose, NULL_TREE);
3642 if (TREE_CODE (value) == CONSTRUCTOR
3643 && TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE)
3644 gimplify_init_ctor_eval (cref, CONSTRUCTOR_ELTS (value),
3648 tree init = build2 (INIT_EXPR, TREE_TYPE (cref), cref, value);
3649 gimplify_and_add (init, pre_p);
3655 /* Return the appropriate RHS predicate for this LHS. */
3658 rhs_predicate_for (tree lhs)
3660 if (is_gimple_reg (lhs))
3661 return is_gimple_reg_rhs_or_call;
3663 return is_gimple_mem_rhs_or_call;
3666 /* Gimplify a C99 compound literal expression. This just means adding
3667 the DECL_EXPR before the current statement and using its anonymous
3670 static enum gimplify_status
3671 gimplify_compound_literal_expr (tree *expr_p, gimple_seq *pre_p)
3673 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p);
3674 tree decl = DECL_EXPR_DECL (decl_s);
3675 /* Mark the decl as addressable if the compound literal
3676 expression is addressable now, otherwise it is marked too late
3677 after we gimplify the initialization expression. */
3678 if (TREE_ADDRESSABLE (*expr_p))
3679 TREE_ADDRESSABLE (decl) = 1;
3681 /* Preliminarily mark non-addressed complex variables as eligible
3682 for promotion to gimple registers. We'll transform their uses
3684 if ((TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE
3685 || TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE)
3686 && !TREE_THIS_VOLATILE (decl)
3687 && !needs_to_live_in_memory (decl))
3688 DECL_GIMPLE_REG_P (decl) = 1;
3690 /* This decl isn't mentioned in the enclosing block, so add it to the
3691 list of temps. FIXME it seems a bit of a kludge to say that
3692 anonymous artificial vars aren't pushed, but everything else is. */
3693 if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
3694 gimple_add_tmp_var (decl);
3696 gimplify_and_add (decl_s, pre_p);
3701 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3702 return a new CONSTRUCTOR if something changed. */
3705 optimize_compound_literals_in_ctor (tree orig_ctor)
3707 tree ctor = orig_ctor;
3708 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (ctor);
3709 unsigned int idx, num = VEC_length (constructor_elt, elts);
3711 for (idx = 0; idx < num; idx++)
3713 tree value = VEC_index (constructor_elt, elts, idx)->value;
3714 tree newval = value;
3715 if (TREE_CODE (value) == CONSTRUCTOR)
3716 newval = optimize_compound_literals_in_ctor (value);
3717 else if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
3719 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (value);
3720 tree decl = DECL_EXPR_DECL (decl_s);
3721 tree init = DECL_INITIAL (decl);
3723 if (!TREE_ADDRESSABLE (value)
3724 && !TREE_ADDRESSABLE (decl)
3726 newval = optimize_compound_literals_in_ctor (init);
3728 if (newval == value)
3731 if (ctor == orig_ctor)
3733 ctor = copy_node (orig_ctor);
3734 CONSTRUCTOR_ELTS (ctor) = VEC_copy (constructor_elt, gc, elts);
3735 elts = CONSTRUCTOR_ELTS (ctor);
3737 VEC_index (constructor_elt, elts, idx)->value = newval;
3742 /* A subroutine of gimplify_modify_expr. Break out elements of a
3743 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3745 Note that we still need to clear any elements that don't have explicit
3746 initializers, so if not all elements are initialized we keep the
3747 original MODIFY_EXPR, we just remove all of the constructor elements.
3749 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3750 GS_ERROR if we would have to create a temporary when gimplifying
3751 this constructor. Otherwise, return GS_OK.
3753 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3755 static enum gimplify_status
3756 gimplify_init_constructor (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
3757 bool want_value, bool notify_temp_creation)
3759 tree object, ctor, type;
3760 enum gimplify_status ret;
3761 VEC(constructor_elt,gc) *elts;
3763 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == CONSTRUCTOR);
3765 if (!notify_temp_creation)
3767 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
3768 is_gimple_lvalue, fb_lvalue);
3769 if (ret == GS_ERROR)
3773 object = TREE_OPERAND (*expr_p, 0);
3774 ctor = TREE_OPERAND (*expr_p, 1) =
3775 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p, 1));
3776 type = TREE_TYPE (ctor);
3777 elts = CONSTRUCTOR_ELTS (ctor);
3780 switch (TREE_CODE (type))
3784 case QUAL_UNION_TYPE:
3787 struct gimplify_init_ctor_preeval_data preeval_data;
3788 HOST_WIDE_INT num_ctor_elements, num_nonzero_elements;
3789 bool cleared, complete_p, valid_const_initializer;
3791 /* Aggregate types must lower constructors to initialization of
3792 individual elements. The exception is that a CONSTRUCTOR node
3793 with no elements indicates zero-initialization of the whole. */
3794 if (VEC_empty (constructor_elt, elts))
3796 if (notify_temp_creation)
3801 /* Fetch information about the constructor to direct later processing.
3802 We might want to make static versions of it in various cases, and
3803 can only do so if it known to be a valid constant initializer. */
3804 valid_const_initializer
3805 = categorize_ctor_elements (ctor, &num_nonzero_elements,
3806 &num_ctor_elements, &complete_p);
3808 /* If a const aggregate variable is being initialized, then it
3809 should never be a lose to promote the variable to be static. */
3810 if (valid_const_initializer
3811 && num_nonzero_elements > 1
3812 && TREE_READONLY (object)
3813 && TREE_CODE (object) == VAR_DECL
3814 && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object)))
3816 if (notify_temp_creation)
3818 DECL_INITIAL (object) = ctor;
3819 TREE_STATIC (object) = 1;
3820 if (!DECL_NAME (object))
3821 DECL_NAME (object) = create_tmp_var_name ("C");
3822 walk_tree (&DECL_INITIAL (object), force_labels_r, NULL, NULL);
3824 /* ??? C++ doesn't automatically append a .<number> to the
3825 assembler name, and even when it does, it looks a FE private
3826 data structures to figure out what that number should be,
3827 which are not set for this variable. I suppose this is
3828 important for local statics for inline functions, which aren't
3829 "local" in the object file sense. So in order to get a unique
3830 TU-local symbol, we must invoke the lhd version now. */
3831 lhd_set_decl_assembler_name (object);
3833 *expr_p = NULL_TREE;
3837 /* If there are "lots" of initialized elements, even discounting
3838 those that are not address constants (and thus *must* be
3839 computed at runtime), then partition the constructor into
3840 constant and non-constant parts. Block copy the constant
3841 parts in, then generate code for the non-constant parts. */
3842 /* TODO. There's code in cp/typeck.c to do this. */
3844 if (int_size_in_bytes (TREE_TYPE (ctor)) < 0)
3845 /* store_constructor will ignore the clearing of variable-sized
3846 objects. Initializers for such objects must explicitly set
3847 every field that needs to be set. */
3849 else if (!complete_p)
3850 /* If the constructor isn't complete, clear the whole object
3853 ??? This ought not to be needed. For any element not present
3854 in the initializer, we should simply set them to zero. Except
3855 we'd need to *find* the elements that are not present, and that
3856 requires trickery to avoid quadratic compile-time behavior in
3857 large cases or excessive memory use in small cases. */
3859 else if (num_ctor_elements - num_nonzero_elements
3860 > CLEAR_RATIO (optimize_function_for_speed_p (cfun))
3861 && num_nonzero_elements < num_ctor_elements / 4)
3862 /* If there are "lots" of zeros, it's more efficient to clear
3863 the memory and then set the nonzero elements. */
3868 /* If there are "lots" of initialized elements, and all of them
3869 are valid address constants, then the entire initializer can
3870 be dropped to memory, and then memcpy'd out. Don't do this
3871 for sparse arrays, though, as it's more efficient to follow
3872 the standard CONSTRUCTOR behavior of memset followed by
3873 individual element initialization. Also don't do this for small
3874 all-zero initializers (which aren't big enough to merit
3875 clearing), and don't try to make bitwise copies of
3876 TREE_ADDRESSABLE types. */
3877 if (valid_const_initializer
3878 && !(cleared || num_nonzero_elements == 0)
3879 && !TREE_ADDRESSABLE (type))
3881 HOST_WIDE_INT size = int_size_in_bytes (type);
3884 /* ??? We can still get unbounded array types, at least
3885 from the C++ front end. This seems wrong, but attempt
3886 to work around it for now. */
3889 size = int_size_in_bytes (TREE_TYPE (object));
3891 TREE_TYPE (ctor) = type = TREE_TYPE (object);
3894 /* Find the maximum alignment we can assume for the object. */
3895 /* ??? Make use of DECL_OFFSET_ALIGN. */
3896 if (DECL_P (object))
3897 align = DECL_ALIGN (object);
3899 align = TYPE_ALIGN (type);
3902 && num_nonzero_elements > 1
3903 && !can_move_by_pieces (size, align))
3905 if (notify_temp_creation)
3908 walk_tree (&ctor, force_labels_r, NULL, NULL);
3909 ctor = tree_output_constant_def (ctor);
3910 if (!useless_type_conversion_p (type, TREE_TYPE (ctor)))
3911 ctor = build1 (VIEW_CONVERT_EXPR, type, ctor);
3912 TREE_OPERAND (*expr_p, 1) = ctor;
3914 /* This is no longer an assignment of a CONSTRUCTOR, but
3915 we still may have processing to do on the LHS. So
3916 pretend we didn't do anything here to let that happen. */
3917 return GS_UNHANDLED;
3921 /* If the target is volatile, we have non-zero elements and more than
3922 one field to assign, initialize the target from a temporary. */
3923 if (TREE_THIS_VOLATILE (object)
3924 && !TREE_ADDRESSABLE (type)
3925 && num_nonzero_elements > 0
3926 && VEC_length (constructor_elt, elts) > 1)
3928 tree temp = create_tmp_var (TYPE_MAIN_VARIANT (type), NULL);
3929 TREE_OPERAND (*expr_p, 0) = temp;
3930 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
3932 build2 (MODIFY_EXPR, void_type_node,
3937 if (notify_temp_creation)
3940 /* If there are nonzero elements and if needed, pre-evaluate to capture
3941 elements overlapping with the lhs into temporaries. We must do this
3942 before clearing to fetch the values before they are zeroed-out. */
3943 if (num_nonzero_elements > 0 && TREE_CODE (*expr_p) != INIT_EXPR)
3945 preeval_data.lhs_base_decl = get_base_address (object);
3946 if (!DECL_P (preeval_data.lhs_base_decl))
3947 preeval_data.lhs_base_decl = NULL;
3948 preeval_data.lhs_alias_set = get_alias_set (object);
3950 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p, 1),
3951 pre_p, post_p, &preeval_data);
3956 /* Zap the CONSTRUCTOR element list, which simplifies this case.
3957 Note that we still have to gimplify, in order to handle the
3958 case of variable sized types. Avoid shared tree structures. */
3959 CONSTRUCTOR_ELTS (ctor) = NULL;
3960 TREE_SIDE_EFFECTS (ctor) = 0;
3961 object = unshare_expr (object);
3962 gimplify_stmt (expr_p, pre_p);
3965 /* If we have not block cleared the object, or if there are nonzero
3966 elements in the constructor, add assignments to the individual
3967 scalar fields of the object. */
3968 if (!cleared || num_nonzero_elements > 0)
3969 gimplify_init_ctor_eval (object, elts, pre_p, cleared);
3971 *expr_p = NULL_TREE;
3979 if (notify_temp_creation)
3982 /* Extract the real and imaginary parts out of the ctor. */
3983 gcc_assert (VEC_length (constructor_elt, elts) == 2);
3984 r = VEC_index (constructor_elt, elts, 0)->value;
3985 i = VEC_index (constructor_elt, elts, 1)->value;
3986 if (r == NULL || i == NULL)
3988 tree zero = build_zero_cst (TREE_TYPE (type));
3995 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
3996 represent creation of a complex value. */
3997 if (TREE_CONSTANT (r) && TREE_CONSTANT (i))
3999 ctor = build_complex (type, r, i);
4000 TREE_OPERAND (*expr_p, 1) = ctor;
4004 ctor = build2 (COMPLEX_EXPR, type, r, i);
4005 TREE_OPERAND (*expr_p, 1) = ctor;
4006 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 1),
4009 rhs_predicate_for (TREE_OPERAND (*expr_p, 0)),
4017 unsigned HOST_WIDE_INT ix;
4018 constructor_elt *ce;
4020 if (notify_temp_creation)
4023 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4024 if (TREE_CONSTANT (ctor))
4026 bool constant_p = true;
4029 /* Even when ctor is constant, it might contain non-*_CST
4030 elements, such as addresses or trapping values like
4031 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4032 in VECTOR_CST nodes. */
4033 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
4034 if (!CONSTANT_CLASS_P (value))
4042 TREE_OPERAND (*expr_p, 1) = build_vector_from_ctor (type, elts);
4046 /* Don't reduce an initializer constant even if we can't
4047 make a VECTOR_CST. It won't do anything for us, and it'll
4048 prevent us from representing it as a single constant. */
4049 if (initializer_constant_valid_p (ctor, type))
4052 TREE_CONSTANT (ctor) = 0;
4055 /* Vector types use CONSTRUCTOR all the way through gimple
4056 compilation as a general initializer. */
4057 FOR_EACH_VEC_ELT (constructor_elt, elts, ix, ce)
4059 enum gimplify_status tret;
4060 tret = gimplify_expr (&ce->value, pre_p, post_p, is_gimple_val,
4062 if (tret == GS_ERROR)
4065 if (!is_gimple_reg (TREE_OPERAND (*expr_p, 0)))
4066 TREE_OPERAND (*expr_p, 1) = get_formal_tmp_var (ctor, pre_p);
4071 /* So how did we get a CONSTRUCTOR for a scalar type? */
4075 if (ret == GS_ERROR)
4077 else if (want_value)
4084 /* If we have gimplified both sides of the initializer but have
4085 not emitted an assignment, do so now. */
4088 tree lhs = TREE_OPERAND (*expr_p, 0);
4089 tree rhs = TREE_OPERAND (*expr_p, 1);
4090 gimple init = gimple_build_assign (lhs, rhs);
4091 gimplify_seq_add_stmt (pre_p, init);
4099 /* Given a pointer value OP0, return a simplified version of an
4100 indirection through OP0, or NULL_TREE if no simplification is
4101 possible. Note that the resulting type may be different from
4102 the type pointed to in the sense that it is still compatible
4103 from the langhooks point of view. */
4106 gimple_fold_indirect_ref (tree t)
4108 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
4113 subtype = TREE_TYPE (sub);
4114 if (!POINTER_TYPE_P (subtype))
4117 if (TREE_CODE (sub) == ADDR_EXPR)
4119 tree op = TREE_OPERAND (sub, 0);
4120 tree optype = TREE_TYPE (op);
4122 if (useless_type_conversion_p (type, optype))
4125 /* *(foo *)&fooarray => fooarray[0] */
4126 if (TREE_CODE (optype) == ARRAY_TYPE
4127 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
4128 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4130 tree type_domain = TYPE_DOMAIN (optype);
4131 tree min_val = size_zero_node;
4132 if (type_domain && TYPE_MIN_VALUE (type_domain))
4133 min_val = TYPE_MIN_VALUE (type_domain);
4134 if (TREE_CODE (min_val) == INTEGER_CST)
4135 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
4137 /* *(foo *)&complexfoo => __real__ complexfoo */
4138 else if (TREE_CODE (optype) == COMPLEX_TYPE
4139 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4140 return fold_build1 (REALPART_EXPR, type, op);
4141 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
4142 else if (TREE_CODE (optype) == VECTOR_TYPE
4143 && useless_type_conversion_p (type, TREE_TYPE (optype)))
4145 tree part_width = TYPE_SIZE (type);
4146 tree index = bitsize_int (0);
4147 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
4151 /* *(p + CST) -> ... */
4152 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4153 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
4155 tree addr = TREE_OPERAND (sub, 0);
4156 tree off = TREE_OPERAND (sub, 1);
4160 addrtype = TREE_TYPE (addr);
4162 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
4163 if (TREE_CODE (addr) == ADDR_EXPR
4164 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
4165 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
4166 && host_integerp (off, 1))
4168 unsigned HOST_WIDE_INT offset = tree_low_cst (off, 1);
4169 tree part_width = TYPE_SIZE (type);
4170 unsigned HOST_WIDE_INT part_widthi
4171 = tree_low_cst (part_width, 0) / BITS_PER_UNIT;
4172 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
4173 tree index = bitsize_int (indexi);
4174 if (offset / part_widthi
4175 <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype)))
4176 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
4180 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
4181 if (TREE_CODE (addr) == ADDR_EXPR
4182 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
4183 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
4185 tree size = TYPE_SIZE_UNIT (type);
4186 if (tree_int_cst_equal (size, off))
4187 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
4190 /* *(p + CST) -> MEM_REF <p, CST>. */
4191 if (TREE_CODE (addr) != ADDR_EXPR
4192 || DECL_P (TREE_OPERAND (addr, 0)))
4193 return fold_build2 (MEM_REF, type,
4195 build_int_cst_wide (ptype,
4196 TREE_INT_CST_LOW (off),
4197 TREE_INT_CST_HIGH (off)));
4200 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4201 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4202 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
4203 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4206 tree min_val = size_zero_node;
4208 sub = gimple_fold_indirect_ref (sub);
4210 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
4211 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4212 if (type_domain && TYPE_MIN_VALUE (type_domain))
4213 min_val = TYPE_MIN_VALUE (type_domain);
4214 if (TREE_CODE (min_val) == INTEGER_CST)
4215 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
4221 /* Given a pointer value OP0, return a simplified version of an
4222 indirection through OP0, or NULL_TREE if no simplification is
4223 possible. This may only be applied to a rhs of an expression.
4224 Note that the resulting type may be different from the type pointed
4225 to in the sense that it is still compatible from the langhooks
4229 gimple_fold_indirect_ref_rhs (tree t)
4231 return gimple_fold_indirect_ref (t);
4234 /* Subroutine of gimplify_modify_expr to do simplifications of
4235 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4236 something changes. */
4238 static enum gimplify_status
4239 gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
4240 gimple_seq *pre_p, gimple_seq *post_p,
4243 enum gimplify_status ret = GS_UNHANDLED;
4249 switch (TREE_CODE (*from_p))
4252 /* If we're assigning from a read-only variable initialized with
4253 a constructor, do the direct assignment from the constructor,
4254 but only if neither source nor target are volatile since this
4255 latter assignment might end up being done on a per-field basis. */
4256 if (DECL_INITIAL (*from_p)
4257 && TREE_READONLY (*from_p)
4258 && !TREE_THIS_VOLATILE (*from_p)
4259 && !TREE_THIS_VOLATILE (*to_p)
4260 && TREE_CODE (DECL_INITIAL (*from_p)) == CONSTRUCTOR)
4262 tree old_from = *from_p;
4263 enum gimplify_status subret;
4265 /* Move the constructor into the RHS. */
4266 *from_p = unshare_expr (DECL_INITIAL (*from_p));
4268 /* Let's see if gimplify_init_constructor will need to put
4270 subret = gimplify_init_constructor (expr_p, NULL, NULL,
4272 if (subret == GS_ERROR)
4274 /* If so, revert the change. */
4286 /* If we have code like
4290 where the type of "x" is a (possibly cv-qualified variant
4291 of "A"), treat the entire expression as identical to "x".
4292 This kind of code arises in C++ when an object is bound
4293 to a const reference, and if "x" is a TARGET_EXPR we want
4294 to take advantage of the optimization below. */
4295 bool volatile_p = TREE_THIS_VOLATILE (*from_p);
4296 tree t = gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p, 0));
4299 if (TREE_THIS_VOLATILE (t) != volatile_p)
4301 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration)
4302 t = build_simple_mem_ref_loc (EXPR_LOCATION (*from_p),
4303 build_fold_addr_expr (t));
4304 if (REFERENCE_CLASS_P (t))
4305 TREE_THIS_VOLATILE (t) = volatile_p;
4316 /* If we are initializing something from a TARGET_EXPR, strip the
4317 TARGET_EXPR and initialize it directly, if possible. This can't
4318 be done if the initializer is void, since that implies that the
4319 temporary is set in some non-trivial way.
4321 ??? What about code that pulls out the temp and uses it
4322 elsewhere? I think that such code never uses the TARGET_EXPR as
4323 an initializer. If I'm wrong, we'll die because the temp won't
4324 have any RTL. In that case, I guess we'll need to replace
4325 references somehow. */
4326 tree init = TARGET_EXPR_INITIAL (*from_p);
4329 && !VOID_TYPE_P (TREE_TYPE (init)))
4339 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4341 gimplify_compound_expr (from_p, pre_p, true);
4347 /* If we already made some changes, let the front end have a
4348 crack at this before we break it down. */
4349 if (ret != GS_UNHANDLED)
4351 /* If we're initializing from a CONSTRUCTOR, break this into
4352 individual MODIFY_EXPRs. */
4353 return gimplify_init_constructor (expr_p, pre_p, post_p, want_value,
4357 /* If we're assigning to a non-register type, push the assignment
4358 down into the branches. This is mandatory for ADDRESSABLE types,
4359 since we cannot generate temporaries for such, but it saves a
4360 copy in other cases as well. */
4361 if (!is_gimple_reg_type (TREE_TYPE (*from_p)))
4363 /* This code should mirror the code in gimplify_cond_expr. */
4364 enum tree_code code = TREE_CODE (*expr_p);
4365 tree cond = *from_p;
4366 tree result = *to_p;
4368 ret = gimplify_expr (&result, pre_p, post_p,
4369 is_gimple_lvalue, fb_lvalue);
4370 if (ret != GS_ERROR)
4373 if (TREE_TYPE (TREE_OPERAND (cond, 1)) != void_type_node)
4374 TREE_OPERAND (cond, 1)
4375 = build2 (code, void_type_node, result,
4376 TREE_OPERAND (cond, 1));
4377 if (TREE_TYPE (TREE_OPERAND (cond, 2)) != void_type_node)
4378 TREE_OPERAND (cond, 2)
4379 = build2 (code, void_type_node, unshare_expr (result),
4380 TREE_OPERAND (cond, 2));
4382 TREE_TYPE (cond) = void_type_node;
4383 recalculate_side_effects (cond);
4387 gimplify_and_add (cond, pre_p);
4388 *expr_p = unshare_expr (result);
4397 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4398 return slot so that we don't generate a temporary. */
4399 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p)
4400 && aggregate_value_p (*from_p, *from_p))
4404 if (!(rhs_predicate_for (*to_p))(*from_p))
4405 /* If we need a temporary, *to_p isn't accurate. */
4407 /* It's OK to use the return slot directly unless it's an NRV. */
4408 else if (TREE_CODE (*to_p) == RESULT_DECL
4409 && DECL_NAME (*to_p) == NULL_TREE
4410 && needs_to_live_in_memory (*to_p))
4412 else if (is_gimple_reg_type (TREE_TYPE (*to_p))
4413 || (DECL_P (*to_p) && DECL_REGISTER (*to_p)))
4414 /* Don't force regs into memory. */
4416 else if (TREE_CODE (*expr_p) == INIT_EXPR)
4417 /* It's OK to use the target directly if it's being
4420 else if (TREE_CODE (*to_p) != SSA_NAME
4421 && (!is_gimple_variable (*to_p)
4422 || needs_to_live_in_memory (*to_p)))
4423 /* Don't use the original target if it's already addressable;
4424 if its address escapes, and the called function uses the
4425 NRV optimization, a conforming program could see *to_p
4426 change before the called function returns; see c++/19317.
4427 When optimizing, the return_slot pass marks more functions
4428 as safe after we have escape info. */
4435 CALL_EXPR_RETURN_SLOT_OPT (*from_p) = 1;
4436 mark_addressable (*to_p);
4441 case WITH_SIZE_EXPR:
4442 /* Likewise for calls that return an aggregate of non-constant size,
4443 since we would not be able to generate a temporary at all. */
4444 if (TREE_CODE (TREE_OPERAND (*from_p, 0)) == CALL_EXPR)
4446 *from_p = TREE_OPERAND (*from_p, 0);
4447 /* We don't change ret in this case because the
4448 WITH_SIZE_EXPR might have been added in
4449 gimplify_modify_expr, so returning GS_OK would lead to an
4455 /* If we're initializing from a container, push the initialization
4457 case CLEANUP_POINT_EXPR:
4459 case STATEMENT_LIST:
4461 tree wrap = *from_p;
4464 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_min_lval,
4466 if (ret != GS_ERROR)
4469 t = voidify_wrapper_expr (wrap, *expr_p);
4470 gcc_assert (t == *expr_p);
4474 gimplify_and_add (wrap, pre_p);
4475 *expr_p = unshare_expr (*to_p);
4482 case COMPOUND_LITERAL_EXPR:
4484 tree complit = TREE_OPERAND (*expr_p, 1);
4485 tree decl_s = COMPOUND_LITERAL_EXPR_DECL_EXPR (complit);
4486 tree decl = DECL_EXPR_DECL (decl_s);
4487 tree init = DECL_INITIAL (decl);
4489 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4490 into struct T x = { 0, 1, 2 } if the address of the
4491 compound literal has never been taken. */
4492 if (!TREE_ADDRESSABLE (complit)
4493 && !TREE_ADDRESSABLE (decl)
4496 *expr_p = copy_node (*expr_p);
4497 TREE_OPERAND (*expr_p, 1) = init;
4511 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4512 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4513 DECL_GIMPLE_REG_P set.
4515 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4516 other, unmodified part of the complex object just before the total store.
4517 As a consequence, if the object is still uninitialized, an undefined value
4518 will be loaded into a register, which may result in a spurious exception
4519 if the register is floating-point and the value happens to be a signaling
4520 NaN for example. Then the fully-fledged complex operations lowering pass
4521 followed by a DCE pass are necessary in order to fix things up. */
4523 static enum gimplify_status
4524 gimplify_modify_expr_complex_part (tree *expr_p, gimple_seq *pre_p,
4527 enum tree_code code, ocode;
4528 tree lhs, rhs, new_rhs, other, realpart, imagpart;
4530 lhs = TREE_OPERAND (*expr_p, 0);
4531 rhs = TREE_OPERAND (*expr_p, 1);
4532 code = TREE_CODE (lhs);
4533 lhs = TREE_OPERAND (lhs, 0);
4535 ocode = code == REALPART_EXPR ? IMAGPART_EXPR : REALPART_EXPR;
4536 other = build1 (ocode, TREE_TYPE (rhs), lhs);
4537 TREE_NO_WARNING (other) = 1;
4538 other = get_formal_tmp_var (other, pre_p);
4540 realpart = code == REALPART_EXPR ? rhs : other;
4541 imagpart = code == REALPART_EXPR ? other : rhs;
4543 if (TREE_CONSTANT (realpart) && TREE_CONSTANT (imagpart))
4544 new_rhs = build_complex (TREE_TYPE (lhs), realpart, imagpart);
4546 new_rhs = build2 (COMPLEX_EXPR, TREE_TYPE (lhs), realpart, imagpart);
4548 gimplify_seq_add_stmt (pre_p, gimple_build_assign (lhs, new_rhs));
4549 *expr_p = (want_value) ? rhs : NULL_TREE;
4554 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4560 PRE_P points to the list where side effects that must happen before
4561 *EXPR_P should be stored.
4563 POST_P points to the list where side effects that must happen after
4564 *EXPR_P should be stored.
4566 WANT_VALUE is nonzero iff we want to use the value of this expression
4567 in another expression. */
4569 static enum gimplify_status
4570 gimplify_modify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
4573 tree *from_p = &TREE_OPERAND (*expr_p, 1);
4574 tree *to_p = &TREE_OPERAND (*expr_p, 0);
4575 enum gimplify_status ret = GS_UNHANDLED;
4577 location_t loc = EXPR_LOCATION (*expr_p);
4579 gcc_assert (TREE_CODE (*expr_p) == MODIFY_EXPR
4580 || TREE_CODE (*expr_p) == INIT_EXPR);
4582 /* Trying to simplify a clobber using normal logic doesn't work,
4583 so handle it here. */
4584 if (TREE_CLOBBER_P (*from_p))
4586 gcc_assert (!want_value && TREE_CODE (*to_p) == VAR_DECL);
4587 gimplify_seq_add_stmt (pre_p, gimple_build_assign (*to_p, *from_p));
4592 /* Insert pointer conversions required by the middle-end that are not
4593 required by the frontend. This fixes middle-end type checking for
4594 for example gcc.dg/redecl-6.c. */
4595 if (POINTER_TYPE_P (TREE_TYPE (*to_p)))
4597 STRIP_USELESS_TYPE_CONVERSION (*from_p);
4598 if (!useless_type_conversion_p (TREE_TYPE (*to_p), TREE_TYPE (*from_p)))
4599 *from_p = fold_convert_loc (loc, TREE_TYPE (*to_p), *from_p);
4602 /* See if any simplifications can be done based on what the RHS is. */
4603 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4605 if (ret != GS_UNHANDLED)
4608 /* For zero sized types only gimplify the left hand side and right hand
4609 side as statements and throw away the assignment. Do this after
4610 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4612 if (zero_sized_type (TREE_TYPE (*from_p)) && !want_value)
4614 gimplify_stmt (from_p, pre_p);
4615 gimplify_stmt (to_p, pre_p);
4616 *expr_p = NULL_TREE;
4620 /* If the value being copied is of variable width, compute the length
4621 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4622 before gimplifying any of the operands so that we can resolve any
4623 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4624 the size of the expression to be copied, not of the destination, so
4625 that is what we must do here. */
4626 maybe_with_size_expr (from_p);
4628 ret = gimplify_expr (to_p, pre_p, post_p, is_gimple_lvalue, fb_lvalue);
4629 if (ret == GS_ERROR)
4632 /* As a special case, we have to temporarily allow for assignments
4633 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4634 a toplevel statement, when gimplifying the GENERIC expression
4635 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4636 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4638 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4639 prevent gimplify_expr from trying to create a new temporary for
4640 foo's LHS, we tell it that it should only gimplify until it
4641 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4642 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4643 and all we need to do here is set 'a' to be its LHS. */
4644 ret = gimplify_expr (from_p, pre_p, post_p, rhs_predicate_for (*to_p),
4646 if (ret == GS_ERROR)
4649 /* Now see if the above changed *from_p to something we handle specially. */
4650 ret = gimplify_modify_expr_rhs (expr_p, from_p, to_p, pre_p, post_p,
4652 if (ret != GS_UNHANDLED)
4655 /* If we've got a variable sized assignment between two lvalues (i.e. does
4656 not involve a call), then we can make things a bit more straightforward
4657 by converting the assignment to memcpy or memset. */
4658 if (TREE_CODE (*from_p) == WITH_SIZE_EXPR)
4660 tree from = TREE_OPERAND (*from_p, 0);
4661 tree size = TREE_OPERAND (*from_p, 1);
4663 if (TREE_CODE (from) == CONSTRUCTOR)
4664 return gimplify_modify_expr_to_memset (expr_p, size, want_value, pre_p);
4666 if (is_gimple_addressable (from))
4669 return gimplify_modify_expr_to_memcpy (expr_p, size, want_value,
4674 /* Transform partial stores to non-addressable complex variables into
4675 total stores. This allows us to use real instead of virtual operands
4676 for these variables, which improves optimization. */
4677 if ((TREE_CODE (*to_p) == REALPART_EXPR
4678 || TREE_CODE (*to_p) == IMAGPART_EXPR)
4679 && is_gimple_reg (TREE_OPERAND (*to_p, 0)))
4680 return gimplify_modify_expr_complex_part (expr_p, pre_p, want_value);
4682 /* Try to alleviate the effects of the gimplification creating artificial
4683 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4684 if (!gimplify_ctxp->into_ssa
4685 && TREE_CODE (*from_p) == VAR_DECL
4686 && DECL_IGNORED_P (*from_p)
4688 && !DECL_IGNORED_P (*to_p))
4690 if (!DECL_NAME (*from_p) && DECL_NAME (*to_p))
4692 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p)));
4693 DECL_DEBUG_EXPR_IS_FROM (*from_p) = 1;
4694 SET_DECL_DEBUG_EXPR (*from_p, *to_p);
4697 if (want_value && TREE_THIS_VOLATILE (*to_p))
4698 *from_p = get_initialized_tmp_var (*from_p, pre_p, post_p);
4700 if (TREE_CODE (*from_p) == CALL_EXPR)
4702 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4703 instead of a GIMPLE_ASSIGN. */
4704 tree fnptrtype = TREE_TYPE (CALL_EXPR_FN (*from_p));
4705 CALL_EXPR_FN (*from_p) = TREE_OPERAND (CALL_EXPR_FN (*from_p), 0);
4706 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p));
4707 assign = gimple_build_call_from_tree (*from_p);
4708 gimple_call_set_fntype (assign, TREE_TYPE (fnptrtype));
4709 if (!gimple_call_noreturn_p (assign))
4710 gimple_call_set_lhs (assign, *to_p);
4714 assign = gimple_build_assign (*to_p, *from_p);
4715 gimple_set_location (assign, EXPR_LOCATION (*expr_p));
4718 gimplify_seq_add_stmt (pre_p, assign);
4720 if (gimplify_ctxp->into_ssa && is_gimple_reg (*to_p))
4722 /* If we've somehow already got an SSA_NAME on the LHS, then
4723 we've probably modified it twice. Not good. */
4724 gcc_assert (TREE_CODE (*to_p) != SSA_NAME);
4725 *to_p = make_ssa_name (*to_p, assign);
4726 gimple_set_lhs (assign, *to_p);
4731 *expr_p = TREE_THIS_VOLATILE (*to_p) ? *from_p : unshare_expr (*to_p);
4740 /* Gimplify a comparison between two variable-sized objects. Do this
4741 with a call to BUILT_IN_MEMCMP. */
4743 static enum gimplify_status
4744 gimplify_variable_sized_compare (tree *expr_p)
4746 location_t loc = EXPR_LOCATION (*expr_p);
4747 tree op0 = TREE_OPERAND (*expr_p, 0);
4748 tree op1 = TREE_OPERAND (*expr_p, 1);
4749 tree t, arg, dest, src, expr;
4751 arg = TYPE_SIZE_UNIT (TREE_TYPE (op0));
4752 arg = unshare_expr (arg);
4753 arg = SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg, op0);
4754 src = build_fold_addr_expr_loc (loc, op1);
4755 dest = build_fold_addr_expr_loc (loc, op0);
4756 t = builtin_decl_implicit (BUILT_IN_MEMCMP);
4757 t = build_call_expr_loc (loc, t, 3, dest, src, arg);
4760 = build2 (TREE_CODE (*expr_p), TREE_TYPE (*expr_p), t, integer_zero_node);
4761 SET_EXPR_LOCATION (expr, loc);
4767 /* Gimplify a comparison between two aggregate objects of integral scalar
4768 mode as a comparison between the bitwise equivalent scalar values. */
4770 static enum gimplify_status
4771 gimplify_scalar_mode_aggregate_compare (tree *expr_p)
4773 location_t loc = EXPR_LOCATION (*expr_p);
4774 tree op0 = TREE_OPERAND (*expr_p, 0);
4775 tree op1 = TREE_OPERAND (*expr_p, 1);
4777 tree type = TREE_TYPE (op0);
4778 tree scalar_type = lang_hooks.types.type_for_mode (TYPE_MODE (type), 1);
4780 op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op0);
4781 op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, scalar_type, op1);
4784 = fold_build2_loc (loc, TREE_CODE (*expr_p), TREE_TYPE (*expr_p), op0, op1);
4789 /* Gimplify an expression sequence. This function gimplifies each
4790 expression and rewrites the original expression with the last
4791 expression of the sequence in GIMPLE form.
4793 PRE_P points to the list where the side effects for all the
4794 expressions in the sequence will be emitted.
4796 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4798 static enum gimplify_status
4799 gimplify_compound_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
4805 tree *sub_p = &TREE_OPERAND (t, 0);
4807 if (TREE_CODE (*sub_p) == COMPOUND_EXPR)
4808 gimplify_compound_expr (sub_p, pre_p, false);
4810 gimplify_stmt (sub_p, pre_p);
4812 t = TREE_OPERAND (t, 1);
4814 while (TREE_CODE (t) == COMPOUND_EXPR);
4821 gimplify_stmt (expr_p, pre_p);
4826 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4827 gimplify. After gimplification, EXPR_P will point to a new temporary
4828 that holds the original value of the SAVE_EXPR node.
4830 PRE_P points to the list where side effects that must happen before
4831 *EXPR_P should be stored. */
4833 static enum gimplify_status
4834 gimplify_save_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4836 enum gimplify_status ret = GS_ALL_DONE;
4839 gcc_assert (TREE_CODE (*expr_p) == SAVE_EXPR);
4840 val = TREE_OPERAND (*expr_p, 0);
4842 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4843 if (!SAVE_EXPR_RESOLVED_P (*expr_p))
4845 /* The operand may be a void-valued expression such as SAVE_EXPRs
4846 generated by the Java frontend for class initialization. It is
4847 being executed only for its side-effects. */
4848 if (TREE_TYPE (val) == void_type_node)
4850 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
4851 is_gimple_stmt, fb_none);
4855 val = get_initialized_tmp_var (val, pre_p, post_p);
4857 TREE_OPERAND (*expr_p, 0) = val;
4858 SAVE_EXPR_RESOLVED_P (*expr_p) = 1;
4866 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4873 PRE_P points to the list where side effects that must happen before
4874 *EXPR_P should be stored.
4876 POST_P points to the list where side effects that must happen after
4877 *EXPR_P should be stored. */
4879 static enum gimplify_status
4880 gimplify_addr_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4882 tree expr = *expr_p;
4883 tree op0 = TREE_OPERAND (expr, 0);
4884 enum gimplify_status ret;
4885 location_t loc = EXPR_LOCATION (*expr_p);
4887 switch (TREE_CODE (op0))
4891 /* Check if we are dealing with an expression of the form '&*ptr'.
4892 While the front end folds away '&*ptr' into 'ptr', these
4893 expressions may be generated internally by the compiler (e.g.,
4894 builtins like __builtin_va_end). */
4895 /* Caution: the silent array decomposition semantics we allow for
4896 ADDR_EXPR means we can't always discard the pair. */
4897 /* Gimplification of the ADDR_EXPR operand may drop
4898 cv-qualification conversions, so make sure we add them if
4901 tree op00 = TREE_OPERAND (op0, 0);
4902 tree t_expr = TREE_TYPE (expr);
4903 tree t_op00 = TREE_TYPE (op00);
4905 if (!useless_type_conversion_p (t_expr, t_op00))
4906 op00 = fold_convert_loc (loc, TREE_TYPE (expr), op00);
4912 case VIEW_CONVERT_EXPR:
4913 /* Take the address of our operand and then convert it to the type of
4916 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4917 all clear. The impact of this transformation is even less clear. */
4919 /* If the operand is a useless conversion, look through it. Doing so
4920 guarantees that the ADDR_EXPR and its operand will remain of the
4922 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0, 0)))
4923 op0 = TREE_OPERAND (op0, 0);
4925 *expr_p = fold_convert_loc (loc, TREE_TYPE (expr),
4926 build_fold_addr_expr_loc (loc,
4927 TREE_OPERAND (op0, 0)));
4932 /* We use fb_either here because the C frontend sometimes takes
4933 the address of a call that returns a struct; see
4934 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4935 the implied temporary explicit. */
4937 /* Make the operand addressable. */
4938 ret = gimplify_expr (&TREE_OPERAND (expr, 0), pre_p, post_p,
4939 is_gimple_addressable, fb_either);
4940 if (ret == GS_ERROR)
4943 /* Then mark it. Beware that it may not be possible to do so directly
4944 if a temporary has been created by the gimplification. */
4945 prepare_gimple_addressable (&TREE_OPERAND (expr, 0), pre_p);
4947 op0 = TREE_OPERAND (expr, 0);
4949 /* For various reasons, the gimplification of the expression
4950 may have made a new INDIRECT_REF. */
4951 if (TREE_CODE (op0) == INDIRECT_REF)
4952 goto do_indirect_ref;
4954 mark_addressable (TREE_OPERAND (expr, 0));
4956 /* The FEs may end up building ADDR_EXPRs early on a decl with
4957 an incomplete type. Re-build ADDR_EXPRs in canonical form
4959 if (!types_compatible_p (TREE_TYPE (op0), TREE_TYPE (TREE_TYPE (expr))))
4960 *expr_p = build_fold_addr_expr (op0);
4962 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4963 recompute_tree_invariant_for_addr_expr (*expr_p);
4965 /* If we re-built the ADDR_EXPR add a conversion to the original type
4967 if (!useless_type_conversion_p (TREE_TYPE (expr), TREE_TYPE (*expr_p)))
4968 *expr_p = fold_convert (TREE_TYPE (expr), *expr_p);
4976 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4977 value; output operands should be a gimple lvalue. */
4979 static enum gimplify_status
4980 gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4984 const char **oconstraints;
4987 const char *constraint;
4988 bool allows_mem, allows_reg, is_inout;
4989 enum gimplify_status ret, tret;
4991 VEC(tree, gc) *inputs;
4992 VEC(tree, gc) *outputs;
4993 VEC(tree, gc) *clobbers;
4994 VEC(tree, gc) *labels;
4998 noutputs = list_length (ASM_OUTPUTS (expr));
4999 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
5001 inputs = outputs = clobbers = labels = NULL;
5004 link_next = NULL_TREE;
5005 for (i = 0, link = ASM_OUTPUTS (expr); link; ++i, link = link_next)
5008 size_t constraint_len;
5010 link_next = TREE_CHAIN (link);
5014 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5015 constraint_len = strlen (constraint);
5016 if (constraint_len == 0)
5019 ok = parse_output_constraint (&constraint, i, 0, 0,
5020 &allows_mem, &allows_reg, &is_inout);
5027 if (!allows_reg && allows_mem)
5028 mark_addressable (TREE_VALUE (link));
5030 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5031 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
5032 fb_lvalue | fb_mayfail);
5033 if (tret == GS_ERROR)
5035 error ("invalid lvalue in asm output %d", i);
5039 VEC_safe_push (tree, gc, outputs, link);
5040 TREE_CHAIN (link) = NULL_TREE;
5044 /* An input/output operand. To give the optimizers more
5045 flexibility, split it into separate input and output
5050 /* Turn the in/out constraint into an output constraint. */
5051 char *p = xstrdup (constraint);
5053 TREE_VALUE (TREE_PURPOSE (link)) = build_string (constraint_len, p);
5055 /* And add a matching input constraint. */
5058 sprintf (buf, "%d", i);
5060 /* If there are multiple alternatives in the constraint,
5061 handle each of them individually. Those that allow register
5062 will be replaced with operand number, the others will stay
5064 if (strchr (p, ',') != NULL)
5066 size_t len = 0, buflen = strlen (buf);
5067 char *beg, *end, *str, *dst;
5071 end = strchr (beg, ',');
5073 end = strchr (beg, '\0');
5074 if ((size_t) (end - beg) < buflen)
5077 len += end - beg + 1;
5084 str = (char *) alloca (len);
5085 for (beg = p + 1, dst = str;;)
5088 bool mem_p, reg_p, inout_p;
5090 end = strchr (beg, ',');
5095 parse_output_constraint (&tem, i, 0, 0,
5096 &mem_p, ®_p, &inout_p);
5101 memcpy (dst, buf, buflen);
5110 memcpy (dst, beg, len);
5119 input = build_string (dst - str, str);
5122 input = build_string (strlen (buf), buf);
5125 input = build_string (constraint_len - 1, constraint + 1);
5129 input = build_tree_list (build_tree_list (NULL_TREE, input),
5130 unshare_expr (TREE_VALUE (link)));
5131 ASM_INPUTS (expr) = chainon (ASM_INPUTS (expr), input);
5135 link_next = NULL_TREE;
5136 for (link = ASM_INPUTS (expr); link; ++i, link = link_next)
5138 link_next = TREE_CHAIN (link);
5139 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5140 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
5141 oconstraints, &allows_mem, &allows_reg);
5143 /* If we can't make copies, we can only accept memory. */
5144 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link))))
5150 error ("impossible constraint in %<asm%>");
5151 error ("non-memory input %d must stay in memory", i);
5156 /* If the operand is a memory input, it should be an lvalue. */
5157 if (!allows_reg && allows_mem)
5159 tree inputv = TREE_VALUE (link);
5160 STRIP_NOPS (inputv);
5161 if (TREE_CODE (inputv) == PREDECREMENT_EXPR
5162 || TREE_CODE (inputv) == PREINCREMENT_EXPR
5163 || TREE_CODE (inputv) == POSTDECREMENT_EXPR
5164 || TREE_CODE (inputv) == POSTINCREMENT_EXPR)
5165 TREE_VALUE (link) = error_mark_node;
5166 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5167 is_gimple_lvalue, fb_lvalue | fb_mayfail);
5168 mark_addressable (TREE_VALUE (link));
5169 if (tret == GS_ERROR)
5171 if (EXPR_HAS_LOCATION (TREE_VALUE (link)))
5172 input_location = EXPR_LOCATION (TREE_VALUE (link));
5173 error ("memory input %d is not directly addressable", i);
5179 tret = gimplify_expr (&TREE_VALUE (link), pre_p, post_p,
5180 is_gimple_asm_val, fb_rvalue);
5181 if (tret == GS_ERROR)
5185 TREE_CHAIN (link) = NULL_TREE;
5186 VEC_safe_push (tree, gc, inputs, link);
5189 for (link = ASM_CLOBBERS (expr); link; ++i, link = TREE_CHAIN (link))
5190 VEC_safe_push (tree, gc, clobbers, link);
5192 for (link = ASM_LABELS (expr); link; ++i, link = TREE_CHAIN (link))
5193 VEC_safe_push (tree, gc, labels, link);
5195 /* Do not add ASMs with errors to the gimple IL stream. */
5196 if (ret != GS_ERROR)
5198 stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)),
5199 inputs, outputs, clobbers, labels);
5201 gimple_asm_set_volatile (stmt, ASM_VOLATILE_P (expr));
5202 gimple_asm_set_input (stmt, ASM_INPUT_P (expr));
5204 gimplify_seq_add_stmt (pre_p, stmt);
5210 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5211 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5212 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5213 return to this function.
5215 FIXME should we complexify the prequeue handling instead? Or use flags
5216 for all the cleanups and let the optimizer tighten them up? The current
5217 code seems pretty fragile; it will break on a cleanup within any
5218 non-conditional nesting. But any such nesting would be broken, anyway;
5219 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5220 and continues out of it. We can do that at the RTL level, though, so
5221 having an optimizer to tighten up try/finally regions would be a Good
5224 static enum gimplify_status
5225 gimplify_cleanup_point_expr (tree *expr_p, gimple_seq *pre_p)
5227 gimple_stmt_iterator iter;
5228 gimple_seq body_sequence = NULL;
5230 tree temp = voidify_wrapper_expr (*expr_p, NULL);
5232 /* We only care about the number of conditions between the innermost
5233 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5234 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5235 int old_conds = gimplify_ctxp->conditions;
5236 gimple_seq old_cleanups = gimplify_ctxp->conditional_cleanups;
5237 bool old_in_cleanup_point_expr = gimplify_ctxp->in_cleanup_point_expr;
5238 gimplify_ctxp->conditions = 0;
5239 gimplify_ctxp->conditional_cleanups = NULL;
5240 gimplify_ctxp->in_cleanup_point_expr = true;
5242 gimplify_stmt (&TREE_OPERAND (*expr_p, 0), &body_sequence);
5244 gimplify_ctxp->conditions = old_conds;
5245 gimplify_ctxp->conditional_cleanups = old_cleanups;
5246 gimplify_ctxp->in_cleanup_point_expr = old_in_cleanup_point_expr;
5248 for (iter = gsi_start (body_sequence); !gsi_end_p (iter); )
5250 gimple wce = gsi_stmt (iter);
5252 if (gimple_code (wce) == GIMPLE_WITH_CLEANUP_EXPR)
5254 if (gsi_one_before_end_p (iter))
5256 /* Note that gsi_insert_seq_before and gsi_remove do not
5257 scan operands, unlike some other sequence mutators. */
5258 if (!gimple_wce_cleanup_eh_only (wce))
5259 gsi_insert_seq_before_without_update (&iter,
5260 gimple_wce_cleanup (wce),
5262 gsi_remove (&iter, true);
5269 enum gimple_try_flags kind;
5271 if (gimple_wce_cleanup_eh_only (wce))
5272 kind = GIMPLE_TRY_CATCH;
5274 kind = GIMPLE_TRY_FINALLY;
5275 seq = gsi_split_seq_after (iter);
5277 gtry = gimple_build_try (seq, gimple_wce_cleanup (wce), kind);
5278 /* Do not use gsi_replace here, as it may scan operands.
5279 We want to do a simple structural modification only. */
5280 *gsi_stmt_ptr (&iter) = gtry;
5281 iter = gsi_start (seq);
5288 gimplify_seq_add_seq (pre_p, body_sequence);
5301 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5302 is the cleanup action required. EH_ONLY is true if the cleanup should
5303 only be executed if an exception is thrown, not on normal exit. */
5306 gimple_push_cleanup (tree var, tree cleanup, bool eh_only, gimple_seq *pre_p)
5309 gimple_seq cleanup_stmts = NULL;
5311 /* Errors can result in improperly nested cleanups. Which results in
5312 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5316 if (gimple_conditional_context ())
5318 /* If we're in a conditional context, this is more complex. We only
5319 want to run the cleanup if we actually ran the initialization that
5320 necessitates it, but we want to run it after the end of the
5321 conditional context. So we wrap the try/finally around the
5322 condition and use a flag to determine whether or not to actually
5323 run the destructor. Thus
5327 becomes (approximately)
5331 if (test) { A::A(temp); flag = 1; val = f(temp); }
5334 if (flag) A::~A(temp);
5338 tree flag = create_tmp_var (boolean_type_node, "cleanup");
5339 gimple ffalse = gimple_build_assign (flag, boolean_false_node);
5340 gimple ftrue = gimple_build_assign (flag, boolean_true_node);
5342 cleanup = build3 (COND_EXPR, void_type_node, flag, cleanup, NULL);
5343 gimplify_stmt (&cleanup, &cleanup_stmts);
5344 wce = gimple_build_wce (cleanup_stmts);
5346 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, ffalse);
5347 gimplify_seq_add_stmt (&gimplify_ctxp->conditional_cleanups, wce);
5348 gimplify_seq_add_stmt (pre_p, ftrue);
5350 /* Because of this manipulation, and the EH edges that jump
5351 threading cannot redirect, the temporary (VAR) will appear
5352 to be used uninitialized. Don't warn. */
5353 TREE_NO_WARNING (var) = 1;
5357 gimplify_stmt (&cleanup, &cleanup_stmts);
5358 wce = gimple_build_wce (cleanup_stmts);
5359 gimple_wce_set_cleanup_eh_only (wce, eh_only);
5360 gimplify_seq_add_stmt (pre_p, wce);
5364 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5366 static enum gimplify_status
5367 gimplify_target_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
5369 tree targ = *expr_p;
5370 tree temp = TARGET_EXPR_SLOT (targ);
5371 tree init = TARGET_EXPR_INITIAL (targ);
5372 enum gimplify_status ret;
5376 tree cleanup = NULL_TREE;
5378 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5379 to the temps list. Handle also variable length TARGET_EXPRs. */
5380 if (TREE_CODE (DECL_SIZE (temp)) != INTEGER_CST)
5382 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp)))
5383 gimplify_type_sizes (TREE_TYPE (temp), pre_p);
5384 gimplify_vla_decl (temp, pre_p);
5387 gimple_add_tmp_var (temp);
5389 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5390 expression is supposed to initialize the slot. */
5391 if (VOID_TYPE_P (TREE_TYPE (init)))
5392 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5395 tree init_expr = build2 (INIT_EXPR, void_type_node, temp, init);
5397 ret = gimplify_expr (&init, pre_p, post_p, is_gimple_stmt, fb_none);
5399 ggc_free (init_expr);
5401 if (ret == GS_ERROR)
5403 /* PR c++/28266 Make sure this is expanded only once. */
5404 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5408 gimplify_and_add (init, pre_p);
5410 /* If needed, push the cleanup for the temp. */
5411 if (TARGET_EXPR_CLEANUP (targ))
5413 if (CLEANUP_EH_ONLY (targ))
5414 gimple_push_cleanup (temp, TARGET_EXPR_CLEANUP (targ),
5415 CLEANUP_EH_ONLY (targ), pre_p);
5417 cleanup = TARGET_EXPR_CLEANUP (targ);
5420 /* Add a clobber for the temporary going out of scope, like
5421 gimplify_bind_expr. */
5422 if (gimplify_ctxp->in_cleanup_point_expr
5423 && needs_to_live_in_memory (temp))
5425 tree clobber = build_constructor (TREE_TYPE (temp), NULL);
5426 TREE_THIS_VOLATILE (clobber) = true;
5427 clobber = build2 (MODIFY_EXPR, TREE_TYPE (temp), temp, clobber);
5429 cleanup = build2 (COMPOUND_EXPR, void_type_node, cleanup,
5436 gimple_push_cleanup (temp, cleanup, false, pre_p);
5438 /* Only expand this once. */
5439 TREE_OPERAND (targ, 3) = init;
5440 TARGET_EXPR_INITIAL (targ) = NULL_TREE;
5443 /* We should have expanded this before. */
5444 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp));
5450 /* Gimplification of expression trees. */
5452 /* Gimplify an expression which appears at statement context. The
5453 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5454 NULL, a new sequence is allocated.
5456 Return true if we actually added a statement to the queue. */
5459 gimplify_stmt (tree *stmt_p, gimple_seq *seq_p)
5461 gimple_seq_node last;
5464 *seq_p = gimple_seq_alloc ();
5466 last = gimple_seq_last (*seq_p);
5467 gimplify_expr (stmt_p, seq_p, NULL, is_gimple_stmt, fb_none);
5468 return last != gimple_seq_last (*seq_p);
5471 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5472 to CTX. If entries already exist, force them to be some flavor of private.
5473 If there is no enclosing parallel, do nothing. */
5476 omp_firstprivatize_variable (struct gimplify_omp_ctx *ctx, tree decl)
5480 if (decl == NULL || !DECL_P (decl))
5485 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5488 if (n->value & GOVD_SHARED)
5489 n->value = GOVD_FIRSTPRIVATE | (n->value & GOVD_SEEN);
5493 else if (ctx->region_type != ORT_WORKSHARE)
5494 omp_add_variable (ctx, decl, GOVD_FIRSTPRIVATE);
5496 ctx = ctx->outer_context;
5501 /* Similarly for each of the type sizes of TYPE. */
5504 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *ctx, tree type)
5506 if (type == NULL || type == error_mark_node)
5508 type = TYPE_MAIN_VARIANT (type);
5510 if (pointer_set_insert (ctx->privatized_types, type))
5513 switch (TREE_CODE (type))
5519 case FIXED_POINT_TYPE:
5520 omp_firstprivatize_variable (ctx, TYPE_MIN_VALUE (type));
5521 omp_firstprivatize_variable (ctx, TYPE_MAX_VALUE (type));
5525 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5526 omp_firstprivatize_type_sizes (ctx, TYPE_DOMAIN (type));
5531 case QUAL_UNION_TYPE:
5534 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5535 if (TREE_CODE (field) == FIELD_DECL)
5537 omp_firstprivatize_variable (ctx, DECL_FIELD_OFFSET (field));
5538 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (field));
5544 case REFERENCE_TYPE:
5545 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (type));
5552 omp_firstprivatize_variable (ctx, TYPE_SIZE (type));
5553 omp_firstprivatize_variable (ctx, TYPE_SIZE_UNIT (type));
5554 lang_hooks.types.omp_firstprivatize_type_sizes (ctx, type);
5557 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5560 omp_add_variable (struct gimplify_omp_ctx *ctx, tree decl, unsigned int flags)
5563 unsigned int nflags;
5566 if (error_operand_p (decl))
5569 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5570 there are constructors involved somewhere. */
5571 if (TREE_ADDRESSABLE (TREE_TYPE (decl))
5572 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
5575 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5578 /* We shouldn't be re-adding the decl with the same data
5580 gcc_assert ((n->value & GOVD_DATA_SHARE_CLASS & flags) == 0);
5581 /* The only combination of data sharing classes we should see is
5582 FIRSTPRIVATE and LASTPRIVATE. */
5583 nflags = n->value | flags;
5584 gcc_assert ((nflags & GOVD_DATA_SHARE_CLASS)
5585 == (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE));
5590 /* When adding a variable-sized variable, we have to handle all sorts
5591 of additional bits of data: the pointer replacement variable, and
5592 the parameters of the type. */
5593 if (DECL_SIZE (decl) && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5595 /* Add the pointer replacement variable as PRIVATE if the variable
5596 replacement is private, else FIRSTPRIVATE since we'll need the
5597 address of the original variable either for SHARED, or for the
5598 copy into or out of the context. */
5599 if (!(flags & GOVD_LOCAL))
5601 nflags = flags & GOVD_PRIVATE ? GOVD_PRIVATE : GOVD_FIRSTPRIVATE;
5602 nflags |= flags & GOVD_SEEN;
5603 t = DECL_VALUE_EXPR (decl);
5604 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5605 t = TREE_OPERAND (t, 0);
5606 gcc_assert (DECL_P (t));
5607 omp_add_variable (ctx, t, nflags);
5610 /* Add all of the variable and type parameters (which should have
5611 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5612 omp_firstprivatize_variable (ctx, DECL_SIZE_UNIT (decl));
5613 omp_firstprivatize_variable (ctx, DECL_SIZE (decl));
5614 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5616 /* The variable-sized variable itself is never SHARED, only some form
5617 of PRIVATE. The sharing would take place via the pointer variable
5618 which we remapped above. */
5619 if (flags & GOVD_SHARED)
5620 flags = GOVD_PRIVATE | GOVD_DEBUG_PRIVATE
5621 | (flags & (GOVD_SEEN | GOVD_EXPLICIT));
5623 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5624 alloca statement we generate for the variable, so make sure it
5625 is available. This isn't automatically needed for the SHARED
5626 case, since we won't be allocating local storage then.
5627 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5628 in this case omp_notice_variable will be called later
5629 on when it is gimplified. */
5630 else if (! (flags & GOVD_LOCAL)
5631 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl))))
5632 omp_notice_variable (ctx, TYPE_SIZE_UNIT (TREE_TYPE (decl)), true);
5634 else if (lang_hooks.decls.omp_privatize_by_reference (decl))
5636 gcc_assert ((flags & GOVD_LOCAL) == 0);
5637 omp_firstprivatize_type_sizes (ctx, TREE_TYPE (decl));
5639 /* Similar to the direct variable sized case above, we'll need the
5640 size of references being privatized. */
5641 if ((flags & GOVD_SHARED) == 0)
5643 t = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
5644 if (TREE_CODE (t) != INTEGER_CST)
5645 omp_notice_variable (ctx, t, true);
5649 splay_tree_insert (ctx->variables, (splay_tree_key)decl, flags);
5652 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5653 This just prints out diagnostics about threadprivate variable uses
5654 in untied tasks. If DECL2 is non-NULL, prevent this warning
5655 on that variable. */
5658 omp_notice_threadprivate_variable (struct gimplify_omp_ctx *ctx, tree decl,
5663 if (ctx->region_type != ORT_UNTIED_TASK)
5665 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5668 error ("threadprivate variable %qE used in untied task",
5670 error_at (ctx->location, "enclosing task");
5671 splay_tree_insert (ctx->variables, (splay_tree_key)decl, 0);
5674 splay_tree_insert (ctx->variables, (splay_tree_key)decl2, 0);
5678 /* Record the fact that DECL was used within the OpenMP context CTX.
5679 IN_CODE is true when real code uses DECL, and false when we should
5680 merely emit default(none) errors. Return true if DECL is going to
5681 be remapped and thus DECL shouldn't be gimplified into its
5682 DECL_VALUE_EXPR (if any). */
5685 omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
5688 unsigned flags = in_code ? GOVD_SEEN : 0;
5689 bool ret = false, shared;
5691 if (error_operand_p (decl))
5694 /* Threadprivate variables are predetermined. */
5695 if (is_global_var (decl))
5697 if (DECL_THREAD_LOCAL_P (decl))
5698 return omp_notice_threadprivate_variable (ctx, decl, NULL_TREE);
5700 if (DECL_HAS_VALUE_EXPR_P (decl))
5702 tree value = get_base_address (DECL_VALUE_EXPR (decl));
5704 if (value && DECL_P (value) && DECL_THREAD_LOCAL_P (value))
5705 return omp_notice_threadprivate_variable (ctx, decl, value);
5709 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5712 enum omp_clause_default_kind default_kind, kind;
5713 struct gimplify_omp_ctx *octx;
5715 if (ctx->region_type == ORT_WORKSHARE)
5718 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5719 remapped firstprivate instead of shared. To some extent this is
5720 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5721 default_kind = ctx->default_kind;
5722 kind = lang_hooks.decls.omp_predetermined_sharing (decl);
5723 if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED)
5724 default_kind = kind;
5726 switch (default_kind)
5728 case OMP_CLAUSE_DEFAULT_NONE:
5729 error ("%qE not specified in enclosing parallel",
5730 DECL_NAME (lang_hooks.decls.omp_report_decl (decl)));
5731 if ((ctx->region_type & ORT_TASK) != 0)
5732 error_at (ctx->location, "enclosing task");
5734 error_at (ctx->location, "enclosing parallel");
5736 case OMP_CLAUSE_DEFAULT_SHARED:
5737 flags |= GOVD_SHARED;
5739 case OMP_CLAUSE_DEFAULT_PRIVATE:
5740 flags |= GOVD_PRIVATE;
5742 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE:
5743 flags |= GOVD_FIRSTPRIVATE;
5745 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5746 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5747 gcc_assert ((ctx->region_type & ORT_TASK) != 0);
5748 if (ctx->outer_context)
5749 omp_notice_variable (ctx->outer_context, decl, in_code);
5750 for (octx = ctx->outer_context; octx; octx = octx->outer_context)
5754 n2 = splay_tree_lookup (octx->variables, (splay_tree_key) decl);
5755 if (n2 && (n2->value & GOVD_DATA_SHARE_CLASS) != GOVD_SHARED)
5757 flags |= GOVD_FIRSTPRIVATE;
5760 if ((octx->region_type & ORT_PARALLEL) != 0)
5763 if (flags & GOVD_FIRSTPRIVATE)
5766 && (TREE_CODE (decl) == PARM_DECL
5767 || (!is_global_var (decl)
5768 && DECL_CONTEXT (decl) == current_function_decl)))
5770 flags |= GOVD_FIRSTPRIVATE;
5773 flags |= GOVD_SHARED;
5779 if ((flags & GOVD_PRIVATE)
5780 && lang_hooks.decls.omp_private_outer_ref (decl))
5781 flags |= GOVD_PRIVATE_OUTER_REF;
5783 omp_add_variable (ctx, decl, flags);
5785 shared = (flags & GOVD_SHARED) != 0;
5786 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5790 if ((n->value & (GOVD_SEEN | GOVD_LOCAL)) == 0
5791 && (flags & (GOVD_SEEN | GOVD_LOCAL)) == GOVD_SEEN
5793 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
5796 tree t = DECL_VALUE_EXPR (decl);
5797 gcc_assert (TREE_CODE (t) == INDIRECT_REF);
5798 t = TREE_OPERAND (t, 0);
5799 gcc_assert (DECL_P (t));
5800 n2 = splay_tree_lookup (ctx->variables, (splay_tree_key) t);
5801 n2->value |= GOVD_SEEN;
5804 shared = ((flags | n->value) & GOVD_SHARED) != 0;
5805 ret = lang_hooks.decls.omp_disregard_value_expr (decl, shared);
5807 /* If nothing changed, there's nothing left to do. */
5808 if ((n->value & flags) == flags)
5814 /* If the variable is private in the current context, then we don't
5815 need to propagate anything to an outer context. */
5816 if ((flags & GOVD_PRIVATE) && !(flags & GOVD_PRIVATE_OUTER_REF))
5818 if (ctx->outer_context
5819 && omp_notice_variable (ctx->outer_context, decl, in_code))
5824 /* Verify that DECL is private within CTX. If there's specific information
5825 to the contrary in the innermost scope, generate an error. */
5828 omp_is_private (struct gimplify_omp_ctx *ctx, tree decl)
5832 n = splay_tree_lookup (ctx->variables, (splay_tree_key)decl);
5835 if (n->value & GOVD_SHARED)
5837 if (ctx == gimplify_omp_ctxp)
5839 error ("iteration variable %qE should be private",
5841 n->value = GOVD_PRIVATE;
5847 else if ((n->value & GOVD_EXPLICIT) != 0
5848 && (ctx == gimplify_omp_ctxp
5849 || (ctx->region_type == ORT_COMBINED_PARALLEL
5850 && gimplify_omp_ctxp->outer_context == ctx)))
5852 if ((n->value & GOVD_FIRSTPRIVATE) != 0)
5853 error ("iteration variable %qE should not be firstprivate",
5855 else if ((n->value & GOVD_REDUCTION) != 0)
5856 error ("iteration variable %qE should not be reduction",
5859 return (ctx == gimplify_omp_ctxp
5860 || (ctx->region_type == ORT_COMBINED_PARALLEL
5861 && gimplify_omp_ctxp->outer_context == ctx));
5864 if (ctx->region_type != ORT_WORKSHARE)
5866 else if (ctx->outer_context)
5867 return omp_is_private (ctx->outer_context, decl);
5871 /* Return true if DECL is private within a parallel region
5872 that binds to the current construct's context or in parallel
5873 region's REDUCTION clause. */
5876 omp_check_private (struct gimplify_omp_ctx *ctx, tree decl)
5882 ctx = ctx->outer_context;
5884 return !(is_global_var (decl)
5885 /* References might be private, but might be shared too. */
5886 || lang_hooks.decls.omp_privatize_by_reference (decl));
5888 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
5890 return (n->value & GOVD_SHARED) == 0;
5892 while (ctx->region_type == ORT_WORKSHARE);
5896 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5897 and previous omp contexts. */
5900 gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
5901 enum omp_region_type region_type)
5903 struct gimplify_omp_ctx *ctx, *outer_ctx;
5904 struct gimplify_ctx gctx;
5907 ctx = new_omp_context (region_type);
5908 outer_ctx = ctx->outer_context;
5910 while ((c = *list_p) != NULL)
5912 bool remove = false;
5913 bool notice_outer = true;
5914 const char *check_non_private = NULL;
5918 switch (OMP_CLAUSE_CODE (c))
5920 case OMP_CLAUSE_PRIVATE:
5921 flags = GOVD_PRIVATE | GOVD_EXPLICIT;
5922 if (lang_hooks.decls.omp_private_outer_ref (OMP_CLAUSE_DECL (c)))
5924 flags |= GOVD_PRIVATE_OUTER_REF;
5925 OMP_CLAUSE_PRIVATE_OUTER_REF (c) = 1;
5928 notice_outer = false;
5930 case OMP_CLAUSE_SHARED:
5931 flags = GOVD_SHARED | GOVD_EXPLICIT;
5933 case OMP_CLAUSE_FIRSTPRIVATE:
5934 flags = GOVD_FIRSTPRIVATE | GOVD_EXPLICIT;
5935 check_non_private = "firstprivate";
5937 case OMP_CLAUSE_LASTPRIVATE:
5938 flags = GOVD_LASTPRIVATE | GOVD_SEEN | GOVD_EXPLICIT;
5939 check_non_private = "lastprivate";
5941 case OMP_CLAUSE_REDUCTION:
5942 flags = GOVD_REDUCTION | GOVD_SEEN | GOVD_EXPLICIT;
5943 check_non_private = "reduction";
5947 decl = OMP_CLAUSE_DECL (c);
5948 if (error_operand_p (decl))
5953 omp_add_variable (ctx, decl, flags);
5954 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5955 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5957 omp_add_variable (ctx, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c),
5958 GOVD_LOCAL | GOVD_SEEN);
5959 gimplify_omp_ctxp = ctx;
5960 push_gimplify_context (&gctx);
5962 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c) = gimple_seq_alloc ();
5963 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c) = gimple_seq_alloc ();
5965 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c),
5966 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c));
5967 pop_gimplify_context
5968 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c)));
5969 push_gimplify_context (&gctx);
5970 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c),
5971 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c));
5972 pop_gimplify_context
5973 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c)));
5974 OMP_CLAUSE_REDUCTION_INIT (c) = NULL_TREE;
5975 OMP_CLAUSE_REDUCTION_MERGE (c) = NULL_TREE;
5977 gimplify_omp_ctxp = outer_ctx;
5979 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
5980 && OMP_CLAUSE_LASTPRIVATE_STMT (c))
5982 gimplify_omp_ctxp = ctx;
5983 push_gimplify_context (&gctx);
5984 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c)) != BIND_EXPR)
5986 tree bind = build3 (BIND_EXPR, void_type_node, NULL,
5988 TREE_SIDE_EFFECTS (bind) = 1;
5989 BIND_EXPR_BODY (bind) = OMP_CLAUSE_LASTPRIVATE_STMT (c);
5990 OMP_CLAUSE_LASTPRIVATE_STMT (c) = bind;
5992 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c),
5993 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
5994 pop_gimplify_context
5995 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c)));
5996 OMP_CLAUSE_LASTPRIVATE_STMT (c) = NULL_TREE;
5998 gimplify_omp_ctxp = outer_ctx;
6004 case OMP_CLAUSE_COPYIN:
6005 case OMP_CLAUSE_COPYPRIVATE:
6006 decl = OMP_CLAUSE_DECL (c);
6007 if (error_operand_p (decl))
6014 omp_notice_variable (outer_ctx, decl, true);
6015 if (check_non_private
6016 && region_type == ORT_WORKSHARE
6017 && omp_check_private (ctx, decl))
6019 error ("%s variable %qE is private in outer context",
6020 check_non_private, DECL_NAME (decl));
6025 case OMP_CLAUSE_FINAL:
6027 OMP_CLAUSE_OPERAND (c, 0)
6028 = gimple_boolify (OMP_CLAUSE_OPERAND (c, 0));
6031 case OMP_CLAUSE_SCHEDULE:
6032 case OMP_CLAUSE_NUM_THREADS:
6033 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c, 0), pre_p, NULL,
6034 is_gimple_val, fb_rvalue) == GS_ERROR)
6038 case OMP_CLAUSE_NOWAIT:
6039 case OMP_CLAUSE_ORDERED:
6040 case OMP_CLAUSE_UNTIED:
6041 case OMP_CLAUSE_COLLAPSE:
6042 case OMP_CLAUSE_MERGEABLE:
6045 case OMP_CLAUSE_DEFAULT:
6046 ctx->default_kind = OMP_CLAUSE_DEFAULT_KIND (c);
6054 *list_p = OMP_CLAUSE_CHAIN (c);
6056 list_p = &OMP_CLAUSE_CHAIN (c);
6059 gimplify_omp_ctxp = ctx;
6062 /* For all variables that were not actually used within the context,
6063 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6066 gimplify_adjust_omp_clauses_1 (splay_tree_node n, void *data)
6068 tree *list_p = (tree *) data;
6069 tree decl = (tree) n->key;
6070 unsigned flags = n->value;
6071 enum omp_clause_code code;
6075 if (flags & (GOVD_EXPLICIT | GOVD_LOCAL))
6077 if ((flags & GOVD_SEEN) == 0)
6079 if (flags & GOVD_DEBUG_PRIVATE)
6081 gcc_assert ((flags & GOVD_DATA_SHARE_CLASS) == GOVD_PRIVATE);
6082 private_debug = true;
6086 = lang_hooks.decls.omp_private_debug_clause (decl,
6087 !!(flags & GOVD_SHARED));
6089 code = OMP_CLAUSE_PRIVATE;
6090 else if (flags & GOVD_SHARED)
6092 if (is_global_var (decl))
6094 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp->outer_context;
6098 = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6099 if (on && (on->value & (GOVD_FIRSTPRIVATE | GOVD_LASTPRIVATE
6100 | GOVD_PRIVATE | GOVD_REDUCTION)) != 0)
6102 ctx = ctx->outer_context;
6107 code = OMP_CLAUSE_SHARED;
6109 else if (flags & GOVD_PRIVATE)
6110 code = OMP_CLAUSE_PRIVATE;
6111 else if (flags & GOVD_FIRSTPRIVATE)
6112 code = OMP_CLAUSE_FIRSTPRIVATE;
6116 clause = build_omp_clause (input_location, code);
6117 OMP_CLAUSE_DECL (clause) = decl;
6118 OMP_CLAUSE_CHAIN (clause) = *list_p;
6120 OMP_CLAUSE_PRIVATE_DEBUG (clause) = 1;
6121 else if (code == OMP_CLAUSE_PRIVATE && (flags & GOVD_PRIVATE_OUTER_REF))
6122 OMP_CLAUSE_PRIVATE_OUTER_REF (clause) = 1;
6124 lang_hooks.decls.omp_finish_clause (clause);
6130 gimplify_adjust_omp_clauses (tree *list_p)
6132 struct gimplify_omp_ctx *ctx = gimplify_omp_ctxp;
6135 while ((c = *list_p) != NULL)
6138 bool remove = false;
6140 switch (OMP_CLAUSE_CODE (c))
6142 case OMP_CLAUSE_PRIVATE:
6143 case OMP_CLAUSE_SHARED:
6144 case OMP_CLAUSE_FIRSTPRIVATE:
6145 decl = OMP_CLAUSE_DECL (c);
6146 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6147 remove = !(n->value & GOVD_SEEN);
6150 bool shared = OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED;
6151 if ((n->value & GOVD_DEBUG_PRIVATE)
6152 || lang_hooks.decls.omp_private_debug_clause (decl, shared))
6154 gcc_assert ((n->value & GOVD_DEBUG_PRIVATE) == 0
6155 || ((n->value & GOVD_DATA_SHARE_CLASS)
6157 OMP_CLAUSE_SET_CODE (c, OMP_CLAUSE_PRIVATE);
6158 OMP_CLAUSE_PRIVATE_DEBUG (c) = 1;
6163 case OMP_CLAUSE_LASTPRIVATE:
6164 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6165 accurately reflect the presence of a FIRSTPRIVATE clause. */
6166 decl = OMP_CLAUSE_DECL (c);
6167 n = splay_tree_lookup (ctx->variables, (splay_tree_key) decl);
6168 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c)
6169 = (n->value & GOVD_FIRSTPRIVATE) != 0;
6172 case OMP_CLAUSE_REDUCTION:
6173 case OMP_CLAUSE_COPYIN:
6174 case OMP_CLAUSE_COPYPRIVATE:
6176 case OMP_CLAUSE_NUM_THREADS:
6177 case OMP_CLAUSE_SCHEDULE:
6178 case OMP_CLAUSE_NOWAIT:
6179 case OMP_CLAUSE_ORDERED:
6180 case OMP_CLAUSE_DEFAULT:
6181 case OMP_CLAUSE_UNTIED:
6182 case OMP_CLAUSE_COLLAPSE:
6183 case OMP_CLAUSE_FINAL:
6184 case OMP_CLAUSE_MERGEABLE:
6192 *list_p = OMP_CLAUSE_CHAIN (c);
6194 list_p = &OMP_CLAUSE_CHAIN (c);
6197 /* Add in any implicit data sharing. */
6198 splay_tree_foreach (ctx->variables, gimplify_adjust_omp_clauses_1, list_p);
6200 gimplify_omp_ctxp = ctx->outer_context;
6201 delete_omp_context (ctx);
6204 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6205 gimplification of the body, as well as scanning the body for used
6206 variables. We need to do this scan now, because variable-sized
6207 decls will be decomposed during gimplification. */
6210 gimplify_omp_parallel (tree *expr_p, gimple_seq *pre_p)
6212 tree expr = *expr_p;
6214 gimple_seq body = NULL;
6215 struct gimplify_ctx gctx;
6217 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr), pre_p,
6218 OMP_PARALLEL_COMBINED (expr)
6219 ? ORT_COMBINED_PARALLEL
6222 push_gimplify_context (&gctx);
6224 g = gimplify_and_return_first (OMP_PARALLEL_BODY (expr), &body);
6225 if (gimple_code (g) == GIMPLE_BIND)
6226 pop_gimplify_context (g);
6228 pop_gimplify_context (NULL);
6230 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr));
6232 g = gimple_build_omp_parallel (body,
6233 OMP_PARALLEL_CLAUSES (expr),
6234 NULL_TREE, NULL_TREE);
6235 if (OMP_PARALLEL_COMBINED (expr))
6236 gimple_omp_set_subcode (g, GF_OMP_PARALLEL_COMBINED);
6237 gimplify_seq_add_stmt (pre_p, g);
6238 *expr_p = NULL_TREE;
6241 /* Gimplify the contents of an OMP_TASK statement. This involves
6242 gimplification of the body, as well as scanning the body for used
6243 variables. We need to do this scan now, because variable-sized
6244 decls will be decomposed during gimplification. */
6247 gimplify_omp_task (tree *expr_p, gimple_seq *pre_p)
6249 tree expr = *expr_p;
6251 gimple_seq body = NULL;
6252 struct gimplify_ctx gctx;
6254 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr), pre_p,
6255 find_omp_clause (OMP_TASK_CLAUSES (expr),
6257 ? ORT_UNTIED_TASK : ORT_TASK);
6259 push_gimplify_context (&gctx);
6261 g = gimplify_and_return_first (OMP_TASK_BODY (expr), &body);
6262 if (gimple_code (g) == GIMPLE_BIND)
6263 pop_gimplify_context (g);
6265 pop_gimplify_context (NULL);
6267 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr));
6269 g = gimple_build_omp_task (body,
6270 OMP_TASK_CLAUSES (expr),
6271 NULL_TREE, NULL_TREE,
6272 NULL_TREE, NULL_TREE, NULL_TREE);
6273 gimplify_seq_add_stmt (pre_p, g);
6274 *expr_p = NULL_TREE;
6277 /* Gimplify the gross structure of an OMP_FOR statement. */
6279 static enum gimplify_status
6280 gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
6282 tree for_stmt, decl, var, t;
6283 enum gimplify_status ret = GS_ALL_DONE;
6284 enum gimplify_status tret;
6286 gimple_seq for_body, for_pre_body;
6291 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt), pre_p,
6294 /* Handle OMP_FOR_INIT. */
6295 for_pre_body = NULL;
6296 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt), &for_pre_body);
6297 OMP_FOR_PRE_BODY (for_stmt) = NULL_TREE;
6299 for_body = gimple_seq_alloc ();
6300 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6301 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt)));
6302 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt))
6303 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt)));
6304 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6306 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6307 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6308 decl = TREE_OPERAND (t, 0);
6309 gcc_assert (DECL_P (decl));
6310 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl))
6311 || POINTER_TYPE_P (TREE_TYPE (decl)));
6313 /* Make sure the iteration variable is private. */
6314 if (omp_is_private (gimplify_omp_ctxp, decl))
6315 omp_notice_variable (gimplify_omp_ctxp, decl, true);
6317 omp_add_variable (gimplify_omp_ctxp, decl, GOVD_PRIVATE | GOVD_SEEN);
6319 /* If DECL is not a gimple register, create a temporary variable to act
6320 as an iteration counter. This is valid, since DECL cannot be
6321 modified in the body of the loop. */
6322 if (!is_gimple_reg (decl))
6324 var = create_tmp_var (TREE_TYPE (decl), get_name (decl));
6325 TREE_OPERAND (t, 0) = var;
6327 gimplify_seq_add_stmt (&for_body, gimple_build_assign (decl, var));
6329 omp_add_variable (gimplify_omp_ctxp, var, GOVD_PRIVATE | GOVD_SEEN);
6334 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6335 is_gimple_val, fb_rvalue);
6336 ret = MIN (ret, tret);
6337 if (ret == GS_ERROR)
6340 /* Handle OMP_FOR_COND. */
6341 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6342 gcc_assert (COMPARISON_CLASS_P (t));
6343 gcc_assert (TREE_OPERAND (t, 0) == decl);
6345 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6346 is_gimple_val, fb_rvalue);
6347 ret = MIN (ret, tret);
6349 /* Handle OMP_FOR_INCR. */
6350 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6351 switch (TREE_CODE (t))
6353 case PREINCREMENT_EXPR:
6354 case POSTINCREMENT_EXPR:
6355 t = build_int_cst (TREE_TYPE (decl), 1);
6356 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6357 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6358 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6361 case PREDECREMENT_EXPR:
6362 case POSTDECREMENT_EXPR:
6363 t = build_int_cst (TREE_TYPE (decl), -1);
6364 t = build2 (PLUS_EXPR, TREE_TYPE (decl), var, t);
6365 t = build2 (MODIFY_EXPR, TREE_TYPE (var), var, t);
6366 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i) = t;
6370 gcc_assert (TREE_OPERAND (t, 0) == decl);
6371 TREE_OPERAND (t, 0) = var;
6373 t = TREE_OPERAND (t, 1);
6374 switch (TREE_CODE (t))
6377 if (TREE_OPERAND (t, 1) == decl)
6379 TREE_OPERAND (t, 1) = TREE_OPERAND (t, 0);
6380 TREE_OPERAND (t, 0) = var;
6386 case POINTER_PLUS_EXPR:
6387 gcc_assert (TREE_OPERAND (t, 0) == decl);
6388 TREE_OPERAND (t, 0) = var;
6394 tret = gimplify_expr (&TREE_OPERAND (t, 1), &for_pre_body, NULL,
6395 is_gimple_val, fb_rvalue);
6396 ret = MIN (ret, tret);
6403 if (var != decl || TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)) > 1)
6406 for (c = OMP_FOR_CLAUSES (for_stmt); c ; c = OMP_CLAUSE_CHAIN (c))
6407 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6408 && OMP_CLAUSE_DECL (c) == decl
6409 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c) == NULL)
6411 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6412 gcc_assert (TREE_CODE (t) == MODIFY_EXPR);
6413 gcc_assert (TREE_OPERAND (t, 0) == var);
6414 t = TREE_OPERAND (t, 1);
6415 gcc_assert (TREE_CODE (t) == PLUS_EXPR
6416 || TREE_CODE (t) == MINUS_EXPR
6417 || TREE_CODE (t) == POINTER_PLUS_EXPR);
6418 gcc_assert (TREE_OPERAND (t, 0) == var);
6419 t = build2 (TREE_CODE (t), TREE_TYPE (decl), decl,
6420 TREE_OPERAND (t, 1));
6421 gimplify_assign (decl, t,
6422 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
6427 gimplify_and_add (OMP_FOR_BODY (for_stmt), &for_body);
6429 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt));
6431 gfor = gimple_build_omp_for (for_body, OMP_FOR_CLAUSES (for_stmt),
6432 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)),
6435 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt)); i++)
6437 t = TREE_VEC_ELT (OMP_FOR_INIT (for_stmt), i);
6438 gimple_omp_for_set_index (gfor, i, TREE_OPERAND (t, 0));
6439 gimple_omp_for_set_initial (gfor, i, TREE_OPERAND (t, 1));
6440 t = TREE_VEC_ELT (OMP_FOR_COND (for_stmt), i);
6441 gimple_omp_for_set_cond (gfor, i, TREE_CODE (t));
6442 gimple_omp_for_set_final (gfor, i, TREE_OPERAND (t, 1));
6443 t = TREE_VEC_ELT (OMP_FOR_INCR (for_stmt), i);
6444 gimple_omp_for_set_incr (gfor, i, TREE_OPERAND (t, 1));
6447 gimplify_seq_add_stmt (pre_p, gfor);
6448 return ret == GS_ALL_DONE ? GS_ALL_DONE : GS_ERROR;
6451 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6452 In particular, OMP_SECTIONS and OMP_SINGLE. */
6455 gimplify_omp_workshare (tree *expr_p, gimple_seq *pre_p)
6457 tree expr = *expr_p;
6459 gimple_seq body = NULL;
6461 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr), pre_p, ORT_WORKSHARE);
6462 gimplify_and_add (OMP_BODY (expr), &body);
6463 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr));
6465 if (TREE_CODE (expr) == OMP_SECTIONS)
6466 stmt = gimple_build_omp_sections (body, OMP_CLAUSES (expr));
6467 else if (TREE_CODE (expr) == OMP_SINGLE)
6468 stmt = gimple_build_omp_single (body, OMP_CLAUSES (expr));
6472 gimplify_seq_add_stmt (pre_p, stmt);
6475 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6476 stabilized the lhs of the atomic operation as *ADDR. Return true if
6477 EXPR is this stabilized form. */
6480 goa_lhs_expr_p (tree expr, tree addr)
6482 /* Also include casts to other type variants. The C front end is fond
6483 of adding these for e.g. volatile variables. This is like
6484 STRIP_TYPE_NOPS but includes the main variant lookup. */
6485 STRIP_USELESS_TYPE_CONVERSION (expr);
6487 if (TREE_CODE (expr) == INDIRECT_REF)
6489 expr = TREE_OPERAND (expr, 0);
6491 && (CONVERT_EXPR_P (expr)
6492 || TREE_CODE (expr) == NON_LVALUE_EXPR)
6493 && TREE_CODE (expr) == TREE_CODE (addr)
6494 && types_compatible_p (TREE_TYPE (expr), TREE_TYPE (addr)))
6496 expr = TREE_OPERAND (expr, 0);
6497 addr = TREE_OPERAND (addr, 0);
6501 return (TREE_CODE (addr) == ADDR_EXPR
6502 && TREE_CODE (expr) == ADDR_EXPR
6503 && TREE_OPERAND (addr, 0) == TREE_OPERAND (expr, 0));
6505 if (TREE_CODE (addr) == ADDR_EXPR && expr == TREE_OPERAND (addr, 0))
6510 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6511 expression does not involve the lhs, evaluate it into a temporary.
6512 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6513 or -1 if an error was encountered. */
6516 goa_stabilize_expr (tree *expr_p, gimple_seq *pre_p, tree lhs_addr,
6519 tree expr = *expr_p;
6522 if (goa_lhs_expr_p (expr, lhs_addr))
6527 if (is_gimple_val (expr))
6531 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
6534 case tcc_comparison:
6535 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p, lhs_addr,
6538 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p, lhs_addr,
6541 case tcc_expression:
6542 switch (TREE_CODE (expr))
6544 case TRUTH_ANDIF_EXPR:
6545 case TRUTH_ORIF_EXPR:
6546 case TRUTH_AND_EXPR:
6548 case TRUTH_XOR_EXPR:
6549 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 1), pre_p,
6551 case TRUTH_NOT_EXPR:
6552 saw_lhs |= goa_stabilize_expr (&TREE_OPERAND (expr, 0), pre_p,
6556 /* Break out any preevaluations from cp_build_modify_expr. */
6557 for (; TREE_CODE (expr) == COMPOUND_EXPR;
6558 expr = TREE_OPERAND (expr, 1))
6559 gimplify_stmt (&TREE_OPERAND (expr, 0), pre_p);
6561 return goa_stabilize_expr (expr_p, pre_p, lhs_addr, lhs_var);
6572 enum gimplify_status gs;
6573 gs = gimplify_expr (expr_p, pre_p, NULL, is_gimple_val, fb_rvalue);
6574 if (gs != GS_ALL_DONE)
6581 /* Gimplify an OMP_ATOMIC statement. */
6583 static enum gimplify_status
6584 gimplify_omp_atomic (tree *expr_p, gimple_seq *pre_p)
6586 tree addr = TREE_OPERAND (*expr_p, 0);
6587 tree rhs = TREE_CODE (*expr_p) == OMP_ATOMIC_READ
6588 ? NULL : TREE_OPERAND (*expr_p, 1);
6589 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr)));
6591 gimple loadstmt, storestmt;
6593 tmp_load = create_tmp_reg (type, NULL);
6594 if (rhs && goa_stabilize_expr (&rhs, pre_p, addr, tmp_load) < 0)
6597 if (gimplify_expr (&addr, pre_p, NULL, is_gimple_val, fb_rvalue)
6601 loadstmt = gimple_build_omp_atomic_load (tmp_load, addr);
6602 gimplify_seq_add_stmt (pre_p, loadstmt);
6603 if (rhs && gimplify_expr (&rhs, pre_p, NULL, is_gimple_val, fb_rvalue)
6607 if (TREE_CODE (*expr_p) == OMP_ATOMIC_READ)
6609 storestmt = gimple_build_omp_atomic_store (rhs);
6610 gimplify_seq_add_stmt (pre_p, storestmt);
6611 switch (TREE_CODE (*expr_p))
6613 case OMP_ATOMIC_READ:
6614 case OMP_ATOMIC_CAPTURE_OLD:
6616 gimple_omp_atomic_set_need_value (loadstmt);
6618 case OMP_ATOMIC_CAPTURE_NEW:
6620 gimple_omp_atomic_set_need_value (storestmt);
6630 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6631 body, and adding some EH bits. */
6633 static enum gimplify_status
6634 gimplify_transaction (tree *expr_p, gimple_seq *pre_p)
6636 tree expr = *expr_p, temp, tbody = TRANSACTION_EXPR_BODY (expr);
6638 gimple_seq body = NULL;
6639 struct gimplify_ctx gctx;
6642 /* Wrap the transaction body in a BIND_EXPR so we have a context
6643 where to put decls for OpenMP. */
6644 if (TREE_CODE (tbody) != BIND_EXPR)
6646 tree bind = build3 (BIND_EXPR, void_type_node, NULL, tbody, NULL);
6647 TREE_SIDE_EFFECTS (bind) = 1;
6648 SET_EXPR_LOCATION (bind, EXPR_LOCATION (tbody));
6649 TRANSACTION_EXPR_BODY (expr) = bind;
6652 push_gimplify_context (&gctx);
6653 temp = voidify_wrapper_expr (*expr_p, NULL);
6655 g = gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr), &body);
6656 pop_gimplify_context (g);
6658 g = gimple_build_transaction (body, NULL);
6659 if (TRANSACTION_EXPR_OUTER (expr))
6660 subcode = GTMA_IS_OUTER;
6661 else if (TRANSACTION_EXPR_RELAXED (expr))
6662 subcode = GTMA_IS_RELAXED;
6663 gimple_transaction_set_subcode (g, subcode);
6665 gimplify_seq_add_stmt (pre_p, g);
6673 *expr_p = NULL_TREE;
6677 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6678 expression produces a value to be used as an operand inside a GIMPLE
6679 statement, the value will be stored back in *EXPR_P. This value will
6680 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6681 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6682 emitted in PRE_P and POST_P.
6684 Additionally, this process may overwrite parts of the input
6685 expression during gimplification. Ideally, it should be
6686 possible to do non-destructive gimplification.
6688 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6689 the expression needs to evaluate to a value to be used as
6690 an operand in a GIMPLE statement, this value will be stored in
6691 *EXPR_P on exit. This happens when the caller specifies one
6692 of fb_lvalue or fb_rvalue fallback flags.
6694 PRE_P will contain the sequence of GIMPLE statements corresponding
6695 to the evaluation of EXPR and all the side-effects that must
6696 be executed before the main expression. On exit, the last
6697 statement of PRE_P is the core statement being gimplified. For
6698 instance, when gimplifying 'if (++a)' the last statement in
6699 PRE_P will be 'if (t.1)' where t.1 is the result of
6700 pre-incrementing 'a'.
6702 POST_P will contain the sequence of GIMPLE statements corresponding
6703 to the evaluation of all the side-effects that must be executed
6704 after the main expression. If this is NULL, the post
6705 side-effects are stored at the end of PRE_P.
6707 The reason why the output is split in two is to handle post
6708 side-effects explicitly. In some cases, an expression may have
6709 inner and outer post side-effects which need to be emitted in
6710 an order different from the one given by the recursive
6711 traversal. For instance, for the expression (*p--)++ the post
6712 side-effects of '--' must actually occur *after* the post
6713 side-effects of '++'. However, gimplification will first visit
6714 the inner expression, so if a separate POST sequence was not
6715 used, the resulting sequence would be:
6722 However, the post-decrement operation in line #2 must not be
6723 evaluated until after the store to *p at line #4, so the
6724 correct sequence should be:
6731 So, by specifying a separate post queue, it is possible
6732 to emit the post side-effects in the correct order.
6733 If POST_P is NULL, an internal queue will be used. Before
6734 returning to the caller, the sequence POST_P is appended to
6735 the main output sequence PRE_P.
6737 GIMPLE_TEST_F points to a function that takes a tree T and
6738 returns nonzero if T is in the GIMPLE form requested by the
6739 caller. The GIMPLE predicates are in gimple.c.
6741 FALLBACK tells the function what sort of a temporary we want if
6742 gimplification cannot produce an expression that complies with
6745 fb_none means that no temporary should be generated
6746 fb_rvalue means that an rvalue is OK to generate
6747 fb_lvalue means that an lvalue is OK to generate
6748 fb_either means that either is OK, but an lvalue is preferable.
6749 fb_mayfail means that gimplification may fail (in which case
6750 GS_ERROR will be returned)
6752 The return value is either GS_ERROR or GS_ALL_DONE, since this
6753 function iterates until EXPR is completely gimplified or an error
6756 enum gimplify_status
6757 gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
6758 bool (*gimple_test_f) (tree), fallback_t fallback)
6761 gimple_seq internal_pre = NULL;
6762 gimple_seq internal_post = NULL;
6765 location_t saved_location;
6766 enum gimplify_status ret;
6767 gimple_stmt_iterator pre_last_gsi, post_last_gsi;
6769 save_expr = *expr_p;
6770 if (save_expr == NULL_TREE)
6773 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6774 is_statement = gimple_test_f == is_gimple_stmt;
6778 /* Consistency checks. */
6779 if (gimple_test_f == is_gimple_reg)
6780 gcc_assert (fallback & (fb_rvalue | fb_lvalue));
6781 else if (gimple_test_f == is_gimple_val
6782 || gimple_test_f == is_gimple_call_addr
6783 || gimple_test_f == is_gimple_condexpr
6784 || gimple_test_f == is_gimple_mem_rhs
6785 || gimple_test_f == is_gimple_mem_rhs_or_call
6786 || gimple_test_f == is_gimple_reg_rhs
6787 || gimple_test_f == is_gimple_reg_rhs_or_call
6788 || gimple_test_f == is_gimple_asm_val
6789 || gimple_test_f == is_gimple_mem_ref_addr)
6790 gcc_assert (fallback & fb_rvalue);
6791 else if (gimple_test_f == is_gimple_min_lval
6792 || gimple_test_f == is_gimple_lvalue)
6793 gcc_assert (fallback & fb_lvalue);
6794 else if (gimple_test_f == is_gimple_addressable)
6795 gcc_assert (fallback & fb_either);
6796 else if (gimple_test_f == is_gimple_stmt)
6797 gcc_assert (fallback == fb_none);
6800 /* We should have recognized the GIMPLE_TEST_F predicate to
6801 know what kind of fallback to use in case a temporary is
6802 needed to hold the value or address of *EXPR_P. */
6806 /* We used to check the predicate here and return immediately if it
6807 succeeds. This is wrong; the design is for gimplification to be
6808 idempotent, and for the predicates to only test for valid forms, not
6809 whether they are fully simplified. */
6811 pre_p = &internal_pre;
6814 post_p = &internal_post;
6816 /* Remember the last statements added to PRE_P and POST_P. Every
6817 new statement added by the gimplification helpers needs to be
6818 annotated with location information. To centralize the
6819 responsibility, we remember the last statement that had been
6820 added to both queues before gimplifying *EXPR_P. If
6821 gimplification produces new statements in PRE_P and POST_P, those
6822 statements will be annotated with the same location information
6824 pre_last_gsi = gsi_last (*pre_p);
6825 post_last_gsi = gsi_last (*post_p);
6827 saved_location = input_location;
6828 if (save_expr != error_mark_node
6829 && EXPR_HAS_LOCATION (*expr_p))
6830 input_location = EXPR_LOCATION (*expr_p);
6832 /* Loop over the specific gimplifiers until the toplevel node
6833 remains the same. */
6836 /* Strip away as many useless type conversions as possible
6838 STRIP_USELESS_TYPE_CONVERSION (*expr_p);
6840 /* Remember the expr. */
6841 save_expr = *expr_p;
6843 /* Die, die, die, my darling. */
6844 if (save_expr == error_mark_node
6845 || (TREE_TYPE (save_expr)
6846 && TREE_TYPE (save_expr) == error_mark_node))
6852 /* Do any language-specific gimplification. */
6853 ret = ((enum gimplify_status)
6854 lang_hooks.gimplify_expr (expr_p, pre_p, post_p));
6857 if (*expr_p == NULL_TREE)
6859 if (*expr_p != save_expr)
6862 else if (ret != GS_UNHANDLED)
6865 /* Make sure that all the cases set 'ret' appropriately. */
6867 switch (TREE_CODE (*expr_p))
6869 /* First deal with the special cases. */
6871 case POSTINCREMENT_EXPR:
6872 case POSTDECREMENT_EXPR:
6873 case PREINCREMENT_EXPR:
6874 case PREDECREMENT_EXPR:
6875 ret = gimplify_self_mod_expr (expr_p, pre_p, post_p,
6876 fallback != fb_none);
6880 case ARRAY_RANGE_REF:
6884 case VIEW_CONVERT_EXPR:
6885 ret = gimplify_compound_lval (expr_p, pre_p, post_p,
6886 fallback ? fallback : fb_rvalue);
6890 ret = gimplify_cond_expr (expr_p, pre_p, fallback);
6892 /* C99 code may assign to an array in a structure value of a
6893 conditional expression, and this has undefined behavior
6894 only on execution, so create a temporary if an lvalue is
6896 if (fallback == fb_lvalue)
6898 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6899 mark_addressable (*expr_p);
6905 ret = gimplify_call_expr (expr_p, pre_p, fallback != fb_none);
6907 /* C99 code may assign to an array in a structure returned
6908 from a function, and this has undefined behavior only on
6909 execution, so create a temporary if an lvalue is
6911 if (fallback == fb_lvalue)
6913 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
6914 mark_addressable (*expr_p);
6923 ret = gimplify_compound_expr (expr_p, pre_p, fallback != fb_none);
6926 case COMPOUND_LITERAL_EXPR:
6927 ret = gimplify_compound_literal_expr (expr_p, pre_p);
6932 ret = gimplify_modify_expr (expr_p, pre_p, post_p,
6933 fallback != fb_none);
6936 case TRUTH_ANDIF_EXPR:
6937 case TRUTH_ORIF_EXPR:
6939 /* Preserve the original type of the expression and the
6940 source location of the outer expression. */
6941 tree org_type = TREE_TYPE (*expr_p);
6942 *expr_p = gimple_boolify (*expr_p);
6943 *expr_p = build3_loc (input_location, COND_EXPR,
6947 org_type, boolean_true_node),
6950 org_type, boolean_false_node));
6955 case TRUTH_NOT_EXPR:
6957 tree type = TREE_TYPE (*expr_p);
6958 /* The parsers are careful to generate TRUTH_NOT_EXPR
6959 only with operands that are always zero or one.
6960 We do not fold here but handle the only interesting case
6961 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
6962 *expr_p = gimple_boolify (*expr_p);
6963 if (TYPE_PRECISION (TREE_TYPE (*expr_p)) == 1)
6964 *expr_p = build1_loc (input_location, BIT_NOT_EXPR,
6965 TREE_TYPE (*expr_p),
6966 TREE_OPERAND (*expr_p, 0));
6968 *expr_p = build2_loc (input_location, BIT_XOR_EXPR,
6969 TREE_TYPE (*expr_p),
6970 TREE_OPERAND (*expr_p, 0),
6971 build_int_cst (TREE_TYPE (*expr_p), 1));
6972 if (!useless_type_conversion_p (type, TREE_TYPE (*expr_p)))
6973 *expr_p = fold_convert_loc (input_location, type, *expr_p);
6979 ret = gimplify_addr_expr (expr_p, pre_p, post_p);
6983 ret = gimplify_va_arg_expr (expr_p, pre_p, post_p);
6987 if (IS_EMPTY_STMT (*expr_p))
6993 if (VOID_TYPE_P (TREE_TYPE (*expr_p))
6994 || fallback == fb_none)
6996 /* Just strip a conversion to void (or in void context) and
6998 *expr_p = TREE_OPERAND (*expr_p, 0);
7003 ret = gimplify_conversion (expr_p);
7004 if (ret == GS_ERROR)
7006 if (*expr_p != save_expr)
7010 case FIX_TRUNC_EXPR:
7011 /* unary_expr: ... | '(' cast ')' val | ... */
7012 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7013 is_gimple_val, fb_rvalue);
7014 recalculate_side_effects (*expr_p);
7019 bool volatilep = TREE_THIS_VOLATILE (*expr_p);
7020 bool notrap = TREE_THIS_NOTRAP (*expr_p);
7021 tree saved_ptr_type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
7023 *expr_p = fold_indirect_ref_loc (input_location, *expr_p);
7024 if (*expr_p != save_expr)
7030 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7031 is_gimple_reg, fb_rvalue);
7032 if (ret == GS_ERROR)
7035 recalculate_side_effects (*expr_p);
7036 *expr_p = fold_build2_loc (input_location, MEM_REF,
7037 TREE_TYPE (*expr_p),
7038 TREE_OPERAND (*expr_p, 0),
7039 build_int_cst (saved_ptr_type, 0));
7040 TREE_THIS_VOLATILE (*expr_p) = volatilep;
7041 TREE_THIS_NOTRAP (*expr_p) = notrap;
7046 /* We arrive here through the various re-gimplifcation paths. */
7048 /* First try re-folding the whole thing. */
7049 tmp = fold_binary (MEM_REF, TREE_TYPE (*expr_p),
7050 TREE_OPERAND (*expr_p, 0),
7051 TREE_OPERAND (*expr_p, 1));
7055 recalculate_side_effects (*expr_p);
7059 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7060 is_gimple_mem_ref_addr, fb_rvalue);
7061 if (ret == GS_ERROR)
7063 recalculate_side_effects (*expr_p);
7067 /* Constants need not be gimplified. */
7078 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7079 CONST_DECL node. Otherwise the decl is replaceable by its
7081 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7082 if (fallback & fb_lvalue)
7086 *expr_p = DECL_INITIAL (*expr_p);
7092 ret = gimplify_decl_expr (expr_p, pre_p);
7096 ret = gimplify_bind_expr (expr_p, pre_p);
7100 ret = gimplify_loop_expr (expr_p, pre_p);
7104 ret = gimplify_switch_expr (expr_p, pre_p);
7108 ret = gimplify_exit_expr (expr_p);
7112 /* If the target is not LABEL, then it is a computed jump
7113 and the target needs to be gimplified. */
7114 if (TREE_CODE (GOTO_DESTINATION (*expr_p)) != LABEL_DECL)
7116 ret = gimplify_expr (&GOTO_DESTINATION (*expr_p), pre_p,
7117 NULL, is_gimple_val, fb_rvalue);
7118 if (ret == GS_ERROR)
7121 gimplify_seq_add_stmt (pre_p,
7122 gimple_build_goto (GOTO_DESTINATION (*expr_p)));
7127 gimplify_seq_add_stmt (pre_p,
7128 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p),
7129 PREDICT_EXPR_OUTCOME (*expr_p)));
7135 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p))
7136 == current_function_decl);
7137 gimplify_seq_add_stmt (pre_p,
7138 gimple_build_label (LABEL_EXPR_LABEL (*expr_p)));
7141 case CASE_LABEL_EXPR:
7142 ret = gimplify_case_label_expr (expr_p, pre_p);
7146 ret = gimplify_return_expr (*expr_p, pre_p);
7150 /* Don't reduce this in place; let gimplify_init_constructor work its
7151 magic. Buf if we're just elaborating this for side effects, just
7152 gimplify any element that has side-effects. */
7153 if (fallback == fb_none)
7155 unsigned HOST_WIDE_INT ix;
7157 tree temp = NULL_TREE;
7158 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p), ix, val)
7159 if (TREE_SIDE_EFFECTS (val))
7160 append_to_statement_list (val, &temp);
7163 ret = temp ? GS_OK : GS_ALL_DONE;
7165 /* C99 code may assign to an array in a constructed
7166 structure or union, and this has undefined behavior only
7167 on execution, so create a temporary if an lvalue is
7169 else if (fallback == fb_lvalue)
7171 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7172 mark_addressable (*expr_p);
7179 /* The following are special cases that are not handled by the
7180 original GIMPLE grammar. */
7182 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7185 ret = gimplify_save_expr (expr_p, pre_p, post_p);
7190 enum gimplify_status r0, r1, r2;
7192 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7193 post_p, is_gimple_lvalue, fb_either);
7194 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7195 post_p, is_gimple_val, fb_rvalue);
7196 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7197 post_p, is_gimple_val, fb_rvalue);
7198 recalculate_side_effects (*expr_p);
7200 ret = MIN (r0, MIN (r1, r2));
7204 case TARGET_MEM_REF:
7206 enum gimplify_status r0 = GS_ALL_DONE, r1 = GS_ALL_DONE;
7208 if (TMR_BASE (*expr_p))
7209 r0 = gimplify_expr (&TMR_BASE (*expr_p), pre_p,
7210 post_p, is_gimple_mem_ref_addr, fb_either);
7211 if (TMR_INDEX (*expr_p))
7212 r1 = gimplify_expr (&TMR_INDEX (*expr_p), pre_p,
7213 post_p, is_gimple_val, fb_rvalue);
7214 if (TMR_INDEX2 (*expr_p))
7215 r1 = gimplify_expr (&TMR_INDEX2 (*expr_p), pre_p,
7216 post_p, is_gimple_val, fb_rvalue);
7217 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7222 case NON_LVALUE_EXPR:
7223 /* This should have been stripped above. */
7227 ret = gimplify_asm_expr (expr_p, pre_p, post_p);
7230 case TRY_FINALLY_EXPR:
7231 case TRY_CATCH_EXPR:
7233 gimple_seq eval, cleanup;
7236 eval = cleanup = NULL;
7237 gimplify_and_add (TREE_OPERAND (*expr_p, 0), &eval);
7238 gimplify_and_add (TREE_OPERAND (*expr_p, 1), &cleanup);
7239 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7240 if (gimple_seq_empty_p (cleanup))
7242 gimple_seq_add_seq (pre_p, eval);
7246 try_ = gimple_build_try (eval, cleanup,
7247 TREE_CODE (*expr_p) == TRY_FINALLY_EXPR
7248 ? GIMPLE_TRY_FINALLY
7249 : GIMPLE_TRY_CATCH);
7250 if (TREE_CODE (*expr_p) == TRY_CATCH_EXPR)
7251 gimple_try_set_catch_is_cleanup (try_,
7252 TRY_CATCH_IS_CLEANUP (*expr_p));
7253 gimplify_seq_add_stmt (pre_p, try_);
7258 case CLEANUP_POINT_EXPR:
7259 ret = gimplify_cleanup_point_expr (expr_p, pre_p);
7263 ret = gimplify_target_expr (expr_p, pre_p, post_p);
7269 gimple_seq handler = NULL;
7270 gimplify_and_add (CATCH_BODY (*expr_p), &handler);
7271 c = gimple_build_catch (CATCH_TYPES (*expr_p), handler);
7272 gimplify_seq_add_stmt (pre_p, c);
7277 case EH_FILTER_EXPR:
7280 gimple_seq failure = NULL;
7282 gimplify_and_add (EH_FILTER_FAILURE (*expr_p), &failure);
7283 ehf = gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p), failure);
7284 gimple_set_no_warning (ehf, TREE_NO_WARNING (*expr_p));
7285 gimplify_seq_add_stmt (pre_p, ehf);
7292 enum gimplify_status r0, r1;
7293 r0 = gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p), pre_p,
7294 post_p, is_gimple_val, fb_rvalue);
7295 r1 = gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p), pre_p,
7296 post_p, is_gimple_val, fb_rvalue);
7297 TREE_SIDE_EFFECTS (*expr_p) = 0;
7303 /* We get here when taking the address of a label. We mark
7304 the label as "forced"; meaning it can never be removed and
7305 it is a potential target for any computed goto. */
7306 FORCED_LABEL (*expr_p) = 1;
7310 case STATEMENT_LIST:
7311 ret = gimplify_statement_list (expr_p, pre_p);
7314 case WITH_SIZE_EXPR:
7316 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7317 post_p == &internal_post ? NULL : post_p,
7318 gimple_test_f, fallback);
7319 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7320 is_gimple_val, fb_rvalue);
7327 ret = gimplify_var_or_parm_decl (expr_p);
7331 /* When within an OpenMP context, notice uses of variables. */
7332 if (gimplify_omp_ctxp)
7333 omp_notice_variable (gimplify_omp_ctxp, *expr_p, true);
7338 /* Allow callbacks into the gimplifier during optimization. */
7343 gimplify_omp_parallel (expr_p, pre_p);
7348 gimplify_omp_task (expr_p, pre_p);
7353 ret = gimplify_omp_for (expr_p, pre_p);
7358 gimplify_omp_workshare (expr_p, pre_p);
7367 gimple_seq body = NULL;
7370 gimplify_and_add (OMP_BODY (*expr_p), &body);
7371 switch (TREE_CODE (*expr_p))
7374 g = gimple_build_omp_section (body);
7377 g = gimple_build_omp_master (body);
7380 g = gimple_build_omp_ordered (body);
7383 g = gimple_build_omp_critical (body,
7384 OMP_CRITICAL_NAME (*expr_p));
7389 gimplify_seq_add_stmt (pre_p, g);
7395 case OMP_ATOMIC_READ:
7396 case OMP_ATOMIC_CAPTURE_OLD:
7397 case OMP_ATOMIC_CAPTURE_NEW:
7398 ret = gimplify_omp_atomic (expr_p, pre_p);
7401 case TRANSACTION_EXPR:
7402 ret = gimplify_transaction (expr_p, pre_p);
7405 case TRUTH_AND_EXPR:
7407 case TRUTH_XOR_EXPR:
7409 tree orig_type = TREE_TYPE (*expr_p);
7410 tree new_type, xop0, xop1;
7411 *expr_p = gimple_boolify (*expr_p);
7412 new_type = TREE_TYPE (*expr_p);
7413 if (!useless_type_conversion_p (orig_type, new_type))
7415 *expr_p = fold_convert_loc (input_location, orig_type, *expr_p);
7420 /* Boolified binary truth expressions are semantically equivalent
7421 to bitwise binary expressions. Canonicalize them to the
7423 switch (TREE_CODE (*expr_p))
7425 case TRUTH_AND_EXPR:
7426 TREE_SET_CODE (*expr_p, BIT_AND_EXPR);
7429 TREE_SET_CODE (*expr_p, BIT_IOR_EXPR);
7431 case TRUTH_XOR_EXPR:
7432 TREE_SET_CODE (*expr_p, BIT_XOR_EXPR);
7437 /* Now make sure that operands have compatible type to
7438 expression's new_type. */
7439 xop0 = TREE_OPERAND (*expr_p, 0);
7440 xop1 = TREE_OPERAND (*expr_p, 1);
7441 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop0)))
7442 TREE_OPERAND (*expr_p, 0) = fold_convert_loc (input_location,
7445 if (!useless_type_conversion_p (new_type, TREE_TYPE (xop1)))
7446 TREE_OPERAND (*expr_p, 1) = fold_convert_loc (input_location,
7449 /* Continue classified as tcc_binary. */
7455 /* Classified as tcc_expression. */
7458 case POINTER_PLUS_EXPR:
7460 enum gimplify_status r0, r1;
7461 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7462 post_p, is_gimple_val, fb_rvalue);
7463 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7464 post_p, is_gimple_val, fb_rvalue);
7465 recalculate_side_effects (*expr_p);
7467 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7468 after gimplifying operands - this is similar to how
7469 it would be folding all gimplified stmts on creation
7470 to have them canonicalized, which is what we eventually
7471 should do anyway. */
7472 if (TREE_CODE (TREE_OPERAND (*expr_p, 1)) == INTEGER_CST
7473 && is_gimple_min_invariant (TREE_OPERAND (*expr_p, 0)))
7475 *expr_p = build_fold_addr_expr_with_type_loc
7477 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (*expr_p)),
7478 TREE_OPERAND (*expr_p, 0),
7479 fold_convert (ptr_type_node,
7480 TREE_OPERAND (*expr_p, 1))),
7481 TREE_TYPE (*expr_p));
7482 ret = MIN (ret, GS_OK);
7488 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p)))
7490 case tcc_comparison:
7491 /* Handle comparison of objects of non scalar mode aggregates
7492 with a call to memcmp. It would be nice to only have to do
7493 this for variable-sized objects, but then we'd have to allow
7494 the same nest of reference nodes we allow for MODIFY_EXPR and
7497 Compare scalar mode aggregates as scalar mode values. Using
7498 memcmp for them would be very inefficient at best, and is
7499 plain wrong if bitfields are involved. */
7501 tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 1));
7503 /* Vector comparisons need no boolification. */
7504 if (TREE_CODE (type) == VECTOR_TYPE)
7506 else if (!AGGREGATE_TYPE_P (type))
7508 tree org_type = TREE_TYPE (*expr_p);
7509 *expr_p = gimple_boolify (*expr_p);
7510 if (!useless_type_conversion_p (org_type,
7511 TREE_TYPE (*expr_p)))
7513 *expr_p = fold_convert_loc (input_location,
7520 else if (TYPE_MODE (type) != BLKmode)
7521 ret = gimplify_scalar_mode_aggregate_compare (expr_p);
7523 ret = gimplify_variable_sized_compare (expr_p);
7528 /* If *EXPR_P does not need to be special-cased, handle it
7529 according to its class. */
7531 ret = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7532 post_p, is_gimple_val, fb_rvalue);
7538 enum gimplify_status r0, r1;
7540 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7541 post_p, is_gimple_val, fb_rvalue);
7542 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7543 post_p, is_gimple_val, fb_rvalue);
7551 enum gimplify_status r0, r1, r2;
7553 r0 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p,
7554 post_p, is_gimple_val, fb_rvalue);
7555 r1 = gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p,
7556 post_p, is_gimple_val, fb_rvalue);
7557 r2 = gimplify_expr (&TREE_OPERAND (*expr_p, 2), pre_p,
7558 post_p, is_gimple_val, fb_rvalue);
7560 ret = MIN (MIN (r0, r1), r2);
7564 case tcc_declaration:
7567 goto dont_recalculate;
7573 recalculate_side_effects (*expr_p);
7579 gcc_assert (*expr_p || ret != GS_OK);
7581 while (ret == GS_OK);
7583 /* If we encountered an error_mark somewhere nested inside, either
7584 stub out the statement or propagate the error back out. */
7585 if (ret == GS_ERROR)
7592 /* This was only valid as a return value from the langhook, which
7593 we handled. Make sure it doesn't escape from any other context. */
7594 gcc_assert (ret != GS_UNHANDLED);
7596 if (fallback == fb_none && *expr_p && !is_gimple_stmt (*expr_p))
7598 /* We aren't looking for a value, and we don't have a valid
7599 statement. If it doesn't have side-effects, throw it away. */
7600 if (!TREE_SIDE_EFFECTS (*expr_p))
7602 else if (!TREE_THIS_VOLATILE (*expr_p))
7604 /* This is probably a _REF that contains something nested that
7605 has side effects. Recurse through the operands to find it. */
7606 enum tree_code code = TREE_CODE (*expr_p);
7613 case VIEW_CONVERT_EXPR:
7614 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7615 gimple_test_f, fallback);
7619 case ARRAY_RANGE_REF:
7620 gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
7621 gimple_test_f, fallback);
7622 gimplify_expr (&TREE_OPERAND (*expr_p, 1), pre_p, post_p,
7623 gimple_test_f, fallback);
7627 /* Anything else with side-effects must be converted to
7628 a valid statement before we get here. */
7634 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p))
7635 && TYPE_MODE (TREE_TYPE (*expr_p)) != BLKmode)
7637 /* Historically, the compiler has treated a bare reference
7638 to a non-BLKmode volatile lvalue as forcing a load. */
7639 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p));
7641 /* Normally, we do not want to create a temporary for a
7642 TREE_ADDRESSABLE type because such a type should not be
7643 copied by bitwise-assignment. However, we make an
7644 exception here, as all we are doing here is ensuring that
7645 we read the bytes that make up the type. We use
7646 create_tmp_var_raw because create_tmp_var will abort when
7647 given a TREE_ADDRESSABLE type. */
7648 tree tmp = create_tmp_var_raw (type, "vol");
7649 gimple_add_tmp_var (tmp);
7650 gimplify_assign (tmp, *expr_p, pre_p);
7654 /* We can't do anything useful with a volatile reference to
7655 an incomplete type, so just throw it away. Likewise for
7656 a BLKmode type, since any implicit inner load should
7657 already have been turned into an explicit one by the
7658 gimplification process. */
7662 /* If we are gimplifying at the statement level, we're done. Tack
7663 everything together and return. */
7664 if (fallback == fb_none || is_statement)
7666 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7667 it out for GC to reclaim it. */
7668 *expr_p = NULL_TREE;
7670 if (!gimple_seq_empty_p (internal_pre)
7671 || !gimple_seq_empty_p (internal_post))
7673 gimplify_seq_add_seq (&internal_pre, internal_post);
7674 gimplify_seq_add_seq (pre_p, internal_pre);
7677 /* The result of gimplifying *EXPR_P is going to be the last few
7678 statements in *PRE_P and *POST_P. Add location information
7679 to all the statements that were added by the gimplification
7681 if (!gimple_seq_empty_p (*pre_p))
7682 annotate_all_with_location_after (*pre_p, pre_last_gsi, input_location);
7684 if (!gimple_seq_empty_p (*post_p))
7685 annotate_all_with_location_after (*post_p, post_last_gsi,
7691 #ifdef ENABLE_GIMPLE_CHECKING
7694 enum tree_code code = TREE_CODE (*expr_p);
7695 /* These expressions should already be in gimple IR form. */
7696 gcc_assert (code != MODIFY_EXPR
7698 && code != BIND_EXPR
7699 && code != CATCH_EXPR
7700 && (code != COND_EXPR || gimplify_ctxp->allow_rhs_cond_expr)
7701 && code != EH_FILTER_EXPR
7702 && code != GOTO_EXPR
7703 && code != LABEL_EXPR
7704 && code != LOOP_EXPR
7705 && code != SWITCH_EXPR
7706 && code != TRY_FINALLY_EXPR
7707 && code != OMP_CRITICAL
7709 && code != OMP_MASTER
7710 && code != OMP_ORDERED
7711 && code != OMP_PARALLEL
7712 && code != OMP_SECTIONS
7713 && code != OMP_SECTION
7714 && code != OMP_SINGLE);
7718 /* Otherwise we're gimplifying a subexpression, so the resulting
7719 value is interesting. If it's a valid operand that matches
7720 GIMPLE_TEST_F, we're done. Unless we are handling some
7721 post-effects internally; if that's the case, we need to copy into
7722 a temporary before adding the post-effects to POST_P. */
7723 if (gimple_seq_empty_p (internal_post) && (*gimple_test_f) (*expr_p))
7726 /* Otherwise, we need to create a new temporary for the gimplified
7729 /* We can't return an lvalue if we have an internal postqueue. The
7730 object the lvalue refers to would (probably) be modified by the
7731 postqueue; we need to copy the value out first, which means an
7733 if ((fallback & fb_lvalue)
7734 && gimple_seq_empty_p (internal_post)
7735 && is_gimple_addressable (*expr_p))
7737 /* An lvalue will do. Take the address of the expression, store it
7738 in a temporary, and replace the expression with an INDIRECT_REF of
7740 tmp = build_fold_addr_expr_loc (input_location, *expr_p);
7741 gimplify_expr (&tmp, pre_p, post_p, is_gimple_reg, fb_rvalue);
7742 *expr_p = build_simple_mem_ref (tmp);
7744 else if ((fallback & fb_rvalue) && is_gimple_reg_rhs_or_call (*expr_p))
7746 /* An rvalue will do. Assign the gimplified expression into a
7747 new temporary TMP and replace the original expression with
7748 TMP. First, make sure that the expression has a type so that
7749 it can be assigned into a temporary. */
7750 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p)));
7752 if (!gimple_seq_empty_p (internal_post) || (fallback & fb_lvalue))
7753 /* The postqueue might change the value of the expression between
7754 the initialization and use of the temporary, so we can't use a
7755 formal temp. FIXME do we care? */
7757 *expr_p = get_initialized_tmp_var (*expr_p, pre_p, post_p);
7758 if (TREE_CODE (TREE_TYPE (*expr_p)) == COMPLEX_TYPE
7759 || TREE_CODE (TREE_TYPE (*expr_p)) == VECTOR_TYPE)
7760 DECL_GIMPLE_REG_P (*expr_p) = 1;
7763 *expr_p = get_formal_tmp_var (*expr_p, pre_p);
7767 #ifdef ENABLE_GIMPLE_CHECKING
7768 if (!(fallback & fb_mayfail))
7770 fprintf (stderr, "gimplification failed:\n");
7771 print_generic_expr (stderr, *expr_p, 0);
7772 debug_tree (*expr_p);
7773 internal_error ("gimplification failed");
7776 gcc_assert (fallback & fb_mayfail);
7778 /* If this is an asm statement, and the user asked for the
7779 impossible, don't die. Fail and let gimplify_asm_expr
7785 /* Make sure the temporary matches our predicate. */
7786 gcc_assert ((*gimple_test_f) (*expr_p));
7788 if (!gimple_seq_empty_p (internal_post))
7790 annotate_all_with_location (internal_post, input_location);
7791 gimplify_seq_add_seq (pre_p, internal_post);
7795 input_location = saved_location;
7799 /* Look through TYPE for variable-sized objects and gimplify each such
7800 size that we find. Add to LIST_P any statements generated. */
7803 gimplify_type_sizes (tree type, gimple_seq *list_p)
7807 if (type == NULL || type == error_mark_node)
7810 /* We first do the main variant, then copy into any other variants. */
7811 type = TYPE_MAIN_VARIANT (type);
7813 /* Avoid infinite recursion. */
7814 if (TYPE_SIZES_GIMPLIFIED (type))
7817 TYPE_SIZES_GIMPLIFIED (type) = 1;
7819 switch (TREE_CODE (type))
7825 case FIXED_POINT_TYPE:
7826 gimplify_one_sizepos (&TYPE_MIN_VALUE (type), list_p);
7827 gimplify_one_sizepos (&TYPE_MAX_VALUE (type), list_p);
7829 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7831 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (type);
7832 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (type);
7837 /* These types may not have declarations, so handle them here. */
7838 gimplify_type_sizes (TREE_TYPE (type), list_p);
7839 gimplify_type_sizes (TYPE_DOMAIN (type), list_p);
7840 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
7841 with assigned stack slots, for -O1+ -g they should be tracked
7843 if (!(TYPE_NAME (type)
7844 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
7845 && DECL_IGNORED_P (TYPE_NAME (type)))
7846 && TYPE_DOMAIN (type)
7847 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type)))
7849 t = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7850 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7851 DECL_IGNORED_P (t) = 0;
7852 t = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
7853 if (t && TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t))
7854 DECL_IGNORED_P (t) = 0;
7860 case QUAL_UNION_TYPE:
7861 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7862 if (TREE_CODE (field) == FIELD_DECL)
7864 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field), list_p);
7865 gimplify_one_sizepos (&DECL_SIZE (field), list_p);
7866 gimplify_one_sizepos (&DECL_SIZE_UNIT (field), list_p);
7867 gimplify_type_sizes (TREE_TYPE (field), list_p);
7872 case REFERENCE_TYPE:
7873 /* We used to recurse on the pointed-to type here, which turned out to
7874 be incorrect because its definition might refer to variables not
7875 yet initialized at this point if a forward declaration is involved.
7877 It was actually useful for anonymous pointed-to types to ensure
7878 that the sizes evaluation dominates every possible later use of the
7879 values. Restricting to such types here would be safe since there
7880 is no possible forward declaration around, but would introduce an
7881 undesirable middle-end semantic to anonymity. We then defer to
7882 front-ends the responsibility of ensuring that the sizes are
7883 evaluated both early and late enough, e.g. by attaching artificial
7884 type declarations to the tree. */
7891 gimplify_one_sizepos (&TYPE_SIZE (type), list_p);
7892 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type), list_p);
7894 for (t = TYPE_NEXT_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7896 TYPE_SIZE (t) = TYPE_SIZE (type);
7897 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
7898 TYPE_SIZES_GIMPLIFIED (t) = 1;
7902 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
7903 a size or position, has had all of its SAVE_EXPRs evaluated.
7904 We add any required statements to *STMT_P. */
7907 gimplify_one_sizepos (tree *expr_p, gimple_seq *stmt_p)
7909 tree type, expr = *expr_p;
7911 /* We don't do anything if the value isn't there, is constant, or contains
7912 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
7913 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
7914 will want to replace it with a new variable, but that will cause problems
7915 if this type is from outside the function. It's OK to have that here. */
7916 if (expr == NULL_TREE || TREE_CONSTANT (expr)
7917 || TREE_CODE (expr) == VAR_DECL
7918 || CONTAINS_PLACEHOLDER_P (expr))
7921 type = TREE_TYPE (expr);
7922 *expr_p = unshare_expr (expr);
7924 gimplify_expr (expr_p, stmt_p, NULL, is_gimple_val, fb_rvalue);
7927 /* Verify that we've an exact type match with the original expression.
7928 In particular, we do not wish to drop a "sizetype" in favour of a
7929 type of similar dimensions. We don't want to pollute the generic
7930 type-stripping code with this knowledge because it doesn't matter
7931 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
7932 and friends retain their "sizetype-ness". */
7933 if (TREE_TYPE (expr) != type
7934 && TREE_CODE (type) == INTEGER_TYPE
7935 && TYPE_IS_SIZETYPE (type))
7940 *expr_p = create_tmp_var (type, NULL);
7941 tmp = build1 (NOP_EXPR, type, expr);
7942 stmt = gimplify_assign (*expr_p, tmp, stmt_p);
7943 gimple_set_location (stmt, EXPR_LOC_OR_HERE (expr));
7947 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
7948 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
7949 is true, also gimplify the parameters. */
7952 gimplify_body (tree fndecl, bool do_parms)
7954 location_t saved_location = input_location;
7955 gimple_seq parm_stmts, seq;
7957 struct gimplify_ctx gctx;
7958 struct cgraph_node *cgn;
7960 timevar_push (TV_TREE_GIMPLIFY);
7962 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
7964 default_rtl_profile ();
7966 gcc_assert (gimplify_ctxp == NULL);
7967 push_gimplify_context (&gctx);
7969 /* Unshare most shared trees in the body and in that of any nested functions.
7970 It would seem we don't have to do this for nested functions because
7971 they are supposed to be output and then the outer function gimplified
7972 first, but the g++ front end doesn't always do it that way. */
7973 unshare_body (fndecl);
7974 unvisit_body (fndecl);
7976 cgn = cgraph_get_node (fndecl);
7977 if (cgn && cgn->origin)
7978 nonlocal_vlas = pointer_set_create ();
7980 /* Make sure input_location isn't set to something weird. */
7981 input_location = DECL_SOURCE_LOCATION (fndecl);
7983 /* Resolve callee-copies. This has to be done before processing
7984 the body so that DECL_VALUE_EXPR gets processed correctly. */
7985 parm_stmts = do_parms ? gimplify_parameters () : NULL;
7987 /* Gimplify the function's body. */
7989 gimplify_stmt (&DECL_SAVED_TREE (fndecl), &seq);
7990 outer_bind = gimple_seq_first_stmt (seq);
7993 outer_bind = gimple_build_nop ();
7994 gimplify_seq_add_stmt (&seq, outer_bind);
7997 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
7998 not the case, wrap everything in a GIMPLE_BIND to make it so. */
7999 if (gimple_code (outer_bind) == GIMPLE_BIND
8000 && gimple_seq_first (seq) == gimple_seq_last (seq))
8003 outer_bind = gimple_build_bind (NULL_TREE, seq, NULL);
8005 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8007 /* If we had callee-copies statements, insert them at the beginning
8008 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8009 if (!gimple_seq_empty_p (parm_stmts))
8013 gimplify_seq_add_seq (&parm_stmts, gimple_bind_body (outer_bind));
8014 gimple_bind_set_body (outer_bind, parm_stmts);
8016 for (parm = DECL_ARGUMENTS (current_function_decl);
8017 parm; parm = DECL_CHAIN (parm))
8018 if (DECL_HAS_VALUE_EXPR_P (parm))
8020 DECL_HAS_VALUE_EXPR_P (parm) = 0;
8021 DECL_IGNORED_P (parm) = 0;
8027 pointer_set_destroy (nonlocal_vlas);
8028 nonlocal_vlas = NULL;
8031 pop_gimplify_context (outer_bind);
8032 gcc_assert (gimplify_ctxp == NULL);
8035 verify_gimple_in_seq (gimple_bind_body (outer_bind));
8037 timevar_pop (TV_TREE_GIMPLIFY);
8038 input_location = saved_location;
8043 typedef char *char_p; /* For DEF_VEC_P. */
8045 DEF_VEC_ALLOC_P(char_p,heap);
8047 /* Return whether we should exclude FNDECL from instrumentation. */
8050 flag_instrument_functions_exclude_p (tree fndecl)
8052 VEC(char_p,heap) *vec;
8054 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_functions;
8055 if (VEC_length (char_p, vec) > 0)
8061 name = lang_hooks.decl_printable_name (fndecl, 0);
8062 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8063 if (strstr (name, s) != NULL)
8067 vec = (VEC(char_p,heap) *) flag_instrument_functions_exclude_files;
8068 if (VEC_length (char_p, vec) > 0)
8074 name = DECL_SOURCE_FILE (fndecl);
8075 FOR_EACH_VEC_ELT (char_p, vec, i, s)
8076 if (strstr (name, s) != NULL)
8083 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8084 node for the function we want to gimplify.
8086 Return the sequence of GIMPLE statements corresponding to the body
8090 gimplify_function_tree (tree fndecl)
8092 tree oldfn, parm, ret;
8096 gcc_assert (!gimple_body (fndecl));
8098 oldfn = current_function_decl;
8099 current_function_decl = fndecl;
8100 if (DECL_STRUCT_FUNCTION (fndecl))
8101 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
8103 push_struct_function (fndecl);
8105 for (parm = DECL_ARGUMENTS (fndecl); parm ; parm = DECL_CHAIN (parm))
8107 /* Preliminarily mark non-addressed complex variables as eligible
8108 for promotion to gimple registers. We'll transform their uses
8110 if ((TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE
8111 || TREE_CODE (TREE_TYPE (parm)) == VECTOR_TYPE)
8112 && !TREE_THIS_VOLATILE (parm)
8113 && !needs_to_live_in_memory (parm))
8114 DECL_GIMPLE_REG_P (parm) = 1;
8117 ret = DECL_RESULT (fndecl);
8118 if ((TREE_CODE (TREE_TYPE (ret)) == COMPLEX_TYPE
8119 || TREE_CODE (TREE_TYPE (ret)) == VECTOR_TYPE)
8120 && !needs_to_live_in_memory (ret))
8121 DECL_GIMPLE_REG_P (ret) = 1;
8123 bind = gimplify_body (fndecl, true);
8125 /* The tree body of the function is no longer needed, replace it
8126 with the new GIMPLE body. */
8127 seq = gimple_seq_alloc ();
8128 gimple_seq_add_stmt (&seq, bind);
8129 gimple_set_body (fndecl, seq);
8131 /* If we're instrumenting function entry/exit, then prepend the call to
8132 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8133 catch the exit hook. */
8134 /* ??? Add some way to ignore exceptions for this TFE. */
8135 if (flag_instrument_function_entry_exit
8136 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)
8137 && !flag_instrument_functions_exclude_p (fndecl))
8142 gimple_seq cleanup = NULL, body = NULL;
8146 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8147 call = gimple_build_call (x, 1, integer_zero_node);
8148 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8149 gimple_call_set_lhs (call, tmp_var);
8150 gimplify_seq_add_stmt (&cleanup, call);
8151 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT);
8152 call = gimple_build_call (x, 2,
8153 build_fold_addr_expr (current_function_decl),
8155 gimplify_seq_add_stmt (&cleanup, call);
8156 tf = gimple_build_try (seq, cleanup, GIMPLE_TRY_FINALLY);
8158 x = builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS);
8159 call = gimple_build_call (x, 1, integer_zero_node);
8160 tmp_var = create_tmp_var (ptr_type_node, "return_addr");
8161 gimple_call_set_lhs (call, tmp_var);
8162 gimplify_seq_add_stmt (&body, call);
8163 x = builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER);
8164 call = gimple_build_call (x, 2,
8165 build_fold_addr_expr (current_function_decl),
8167 gimplify_seq_add_stmt (&body, call);
8168 gimplify_seq_add_stmt (&body, tf);
8169 new_bind = gimple_build_bind (NULL, body, gimple_bind_block (bind));
8170 /* Clear the block for BIND, since it is no longer directly inside
8171 the function, but within a try block. */
8172 gimple_bind_set_block (bind, NULL);
8174 /* Replace the current function body with the body
8175 wrapped in the try/finally TF. */
8176 seq = gimple_seq_alloc ();
8177 gimple_seq_add_stmt (&seq, new_bind);
8178 gimple_set_body (fndecl, seq);
8181 DECL_SAVED_TREE (fndecl) = NULL_TREE;
8182 cfun->curr_properties = PROP_gimple_any;
8184 current_function_decl = oldfn;
8188 /* Some transformations like inlining may invalidate the GIMPLE form
8189 for operands. This function traverses all the operands in STMT and
8190 gimplifies anything that is not a valid gimple operand. Any new
8191 GIMPLE statements are inserted before *GSI_P. */
8194 gimple_regimplify_operands (gimple stmt, gimple_stmt_iterator *gsi_p)
8197 tree orig_lhs = NULL_TREE, lhs, t;
8198 gimple_seq pre = NULL;
8199 gimple post_stmt = NULL;
8200 struct gimplify_ctx gctx;
8202 push_gimplify_context (&gctx);
8203 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8205 switch (gimple_code (stmt))
8208 gimplify_expr (gimple_cond_lhs_ptr (stmt), &pre, NULL,
8209 is_gimple_val, fb_rvalue);
8210 gimplify_expr (gimple_cond_rhs_ptr (stmt), &pre, NULL,
8211 is_gimple_val, fb_rvalue);
8214 gimplify_expr (gimple_switch_index_ptr (stmt), &pre, NULL,
8215 is_gimple_val, fb_rvalue);
8217 case GIMPLE_OMP_ATOMIC_LOAD:
8218 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt), &pre, NULL,
8219 is_gimple_val, fb_rvalue);
8223 size_t i, noutputs = gimple_asm_noutputs (stmt);
8224 const char *constraint, **oconstraints;
8225 bool allows_mem, allows_reg, is_inout;
8228 = (const char **) alloca ((noutputs) * sizeof (const char *));
8229 for (i = 0; i < noutputs; i++)
8231 tree op = gimple_asm_output_op (stmt, i);
8232 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8233 oconstraints[i] = constraint;
8234 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
8235 &allows_reg, &is_inout);
8236 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8237 is_inout ? is_gimple_min_lval : is_gimple_lvalue,
8238 fb_lvalue | fb_mayfail);
8240 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
8242 tree op = gimple_asm_input_op (stmt, i);
8243 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
8244 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
8245 oconstraints, &allows_mem, &allows_reg);
8246 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op))) && allows_mem)
8248 if (!allows_reg && allows_mem)
8249 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8250 is_gimple_lvalue, fb_lvalue | fb_mayfail);
8252 gimplify_expr (&TREE_VALUE (op), &pre, NULL,
8253 is_gimple_asm_val, fb_rvalue);
8258 /* NOTE: We start gimplifying operands from last to first to
8259 make sure that side-effects on the RHS of calls, assignments
8260 and ASMs are executed before the LHS. The ordering is not
8261 important for other statements. */
8262 num_ops = gimple_num_ops (stmt);
8263 orig_lhs = gimple_get_lhs (stmt);
8264 for (i = num_ops; i > 0; i--)
8266 tree op = gimple_op (stmt, i - 1);
8267 if (op == NULL_TREE)
8269 if (i == 1 && (is_gimple_call (stmt) || is_gimple_assign (stmt)))
8270 gimplify_expr (&op, &pre, NULL, is_gimple_lvalue, fb_lvalue);
8272 && is_gimple_assign (stmt)
8274 && get_gimple_rhs_class (gimple_expr_code (stmt))
8275 == GIMPLE_SINGLE_RHS)
8276 gimplify_expr (&op, &pre, NULL,
8277 rhs_predicate_for (gimple_assign_lhs (stmt)),
8279 else if (i == 2 && is_gimple_call (stmt))
8281 if (TREE_CODE (op) == FUNCTION_DECL)
8283 gimplify_expr (&op, &pre, NULL, is_gimple_call_addr, fb_rvalue);
8286 gimplify_expr (&op, &pre, NULL, is_gimple_val, fb_rvalue);
8287 gimple_set_op (stmt, i - 1, op);
8290 lhs = gimple_get_lhs (stmt);
8291 /* If the LHS changed it in a way that requires a simple RHS,
8292 create temporary. */
8293 if (lhs && !is_gimple_reg (lhs))
8295 bool need_temp = false;
8297 if (is_gimple_assign (stmt)
8299 && get_gimple_rhs_class (gimple_expr_code (stmt))
8300 == GIMPLE_SINGLE_RHS)
8301 gimplify_expr (gimple_assign_rhs1_ptr (stmt), &pre, NULL,
8302 rhs_predicate_for (gimple_assign_lhs (stmt)),
8304 else if (is_gimple_reg (lhs))
8306 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8308 if (is_gimple_call (stmt))
8310 i = gimple_call_flags (stmt);
8311 if ((i & ECF_LOOPING_CONST_OR_PURE)
8312 || !(i & (ECF_CONST | ECF_PURE)))
8315 if (stmt_can_throw_internal (stmt))
8321 if (is_gimple_reg_type (TREE_TYPE (lhs)))
8323 else if (TYPE_MODE (TREE_TYPE (lhs)) != BLKmode)
8325 if (is_gimple_call (stmt))
8327 tree fndecl = gimple_call_fndecl (stmt);
8329 if (!aggregate_value_p (TREE_TYPE (lhs), fndecl)
8330 && !(fndecl && DECL_RESULT (fndecl)
8331 && DECL_BY_REFERENCE (DECL_RESULT (fndecl))))
8340 tree temp = create_tmp_reg (TREE_TYPE (lhs), NULL);
8342 if (TREE_CODE (orig_lhs) == SSA_NAME)
8343 orig_lhs = SSA_NAME_VAR (orig_lhs);
8345 if (gimple_in_ssa_p (cfun))
8346 temp = make_ssa_name (temp, NULL);
8347 gimple_set_lhs (stmt, temp);
8348 post_stmt = gimple_build_assign (lhs, temp);
8349 if (TREE_CODE (lhs) == SSA_NAME)
8350 SSA_NAME_DEF_STMT (lhs) = post_stmt;
8356 if (gimple_referenced_vars (cfun))
8357 for (t = gimplify_ctxp->temps; t ; t = TREE_CHAIN (t))
8358 add_referenced_var (t);
8360 if (!gimple_seq_empty_p (pre))
8362 if (gimple_in_ssa_p (cfun))
8364 gimple_stmt_iterator i;
8366 for (i = gsi_start (pre); !gsi_end_p (i); gsi_next (&i))
8367 mark_symbols_for_renaming (gsi_stmt (i));
8369 gsi_insert_seq_before (gsi_p, pre, GSI_SAME_STMT);
8372 gsi_insert_after (gsi_p, post_stmt, GSI_NEW_STMT);
8374 pop_gimplify_context (NULL);
8377 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8378 the predicate that will hold for the result. If VAR is not NULL, make the
8379 base variable of the final destination be VAR if suitable. */
8382 force_gimple_operand_1 (tree expr, gimple_seq *stmts,
8383 gimple_predicate gimple_test_f, tree var)
8386 enum gimplify_status ret;
8387 struct gimplify_ctx gctx;
8391 /* gimple_test_f might be more strict than is_gimple_val, make
8392 sure we pass both. Just checking gimple_test_f doesn't work
8393 because most gimple predicates do not work recursively. */
8394 if (is_gimple_val (expr)
8395 && (*gimple_test_f) (expr))
8398 push_gimplify_context (&gctx);
8399 gimplify_ctxp->into_ssa = gimple_in_ssa_p (cfun);
8400 gimplify_ctxp->allow_rhs_cond_expr = true;
8403 expr = build2 (MODIFY_EXPR, TREE_TYPE (var), var, expr);
8405 if (TREE_CODE (expr) != MODIFY_EXPR
8406 && TREE_TYPE (expr) == void_type_node)
8408 gimplify_and_add (expr, stmts);
8413 ret = gimplify_expr (&expr, stmts, NULL, gimple_test_f, fb_rvalue);
8414 gcc_assert (ret != GS_ERROR);
8417 if (gimple_referenced_vars (cfun))
8418 for (t = gimplify_ctxp->temps; t ; t = DECL_CHAIN (t))
8419 add_referenced_var (t);
8421 pop_gimplify_context (NULL);
8426 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8427 force the result to be either ssa_name or an invariant, otherwise
8428 just force it to be a rhs expression. If VAR is not NULL, make the
8429 base variable of the final destination be VAR if suitable. */
8432 force_gimple_operand (tree expr, gimple_seq *stmts, bool simple, tree var)
8434 return force_gimple_operand_1 (expr, stmts,
8435 simple ? is_gimple_val : is_gimple_reg_rhs,
8439 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8440 and VAR. If some statements are produced, emits them at GSI.
8441 If BEFORE is true. the statements are appended before GSI, otherwise
8442 they are appended after it. M specifies the way GSI moves after
8443 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8446 force_gimple_operand_gsi_1 (gimple_stmt_iterator *gsi, tree expr,
8447 gimple_predicate gimple_test_f,
8448 tree var, bool before,
8449 enum gsi_iterator_update m)
8453 expr = force_gimple_operand_1 (expr, &stmts, gimple_test_f, var);
8455 if (!gimple_seq_empty_p (stmts))
8457 if (gimple_in_ssa_p (cfun))
8459 gimple_stmt_iterator i;
8461 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
8462 mark_symbols_for_renaming (gsi_stmt (i));
8466 gsi_insert_seq_before (gsi, stmts, m);
8468 gsi_insert_seq_after (gsi, stmts, m);
8474 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8475 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8476 otherwise just force it to be a rhs expression. If some statements are
8477 produced, emits them at GSI. If BEFORE is true, the statements are
8478 appended before GSI, otherwise they are appended after it. M specifies
8479 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8480 are the usual values). */
8483 force_gimple_operand_gsi (gimple_stmt_iterator *gsi, tree expr,
8484 bool simple_p, tree var, bool before,
8485 enum gsi_iterator_update m)
8487 return force_gimple_operand_gsi_1 (gsi, expr,
8489 ? is_gimple_val : is_gimple_reg_rhs,
8494 #include "gt-gimplify.h"