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 && !is_gimple_reg (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 (variably_modified_type_p (TREE_TYPE (*to_p
), NULL_TREE
))
4421 /* Always use the target and thus RSO for variable-sized types.
4422 GIMPLE cannot deal with a variable-sized assignment
4423 embedded in a call statement. */
4425 else if (TREE_CODE (*to_p
) != SSA_NAME
4426 && (!is_gimple_variable (*to_p
)
4427 || needs_to_live_in_memory (*to_p
)))
4428 /* Don't use the original target if it's already addressable;
4429 if its address escapes, and the called function uses the
4430 NRV optimization, a conforming program could see *to_p
4431 change before the called function returns; see c++/19317.
4432 When optimizing, the return_slot pass marks more functions
4433 as safe after we have escape info. */
4440 CALL_EXPR_RETURN_SLOT_OPT (*from_p
) = 1;
4441 mark_addressable (*to_p
);
4446 case WITH_SIZE_EXPR
:
4447 /* Likewise for calls that return an aggregate of non-constant size,
4448 since we would not be able to generate a temporary at all. */
4449 if (TREE_CODE (TREE_OPERAND (*from_p
, 0)) == CALL_EXPR
)
4451 *from_p
= TREE_OPERAND (*from_p
, 0);
4452 /* We don't change ret in this case because the
4453 WITH_SIZE_EXPR might have been added in
4454 gimplify_modify_expr, so returning GS_OK would lead to an
4460 /* If we're initializing from a container, push the initialization
4462 case CLEANUP_POINT_EXPR
:
4464 case STATEMENT_LIST
:
4466 tree wrap
= *from_p
;
4469 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_min_lval
,
4471 if (ret
!= GS_ERROR
)
4474 t
= voidify_wrapper_expr (wrap
, *expr_p
);
4475 gcc_assert (t
== *expr_p
);
4479 gimplify_and_add (wrap
, pre_p
);
4480 *expr_p
= unshare_expr (*to_p
);
4487 case COMPOUND_LITERAL_EXPR
:
4489 tree complit
= TREE_OPERAND (*expr_p
, 1);
4490 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (complit
);
4491 tree decl
= DECL_EXPR_DECL (decl_s
);
4492 tree init
= DECL_INITIAL (decl
);
4494 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4495 into struct T x = { 0, 1, 2 } if the address of the
4496 compound literal has never been taken. */
4497 if (!TREE_ADDRESSABLE (complit
)
4498 && !TREE_ADDRESSABLE (decl
)
4501 *expr_p
= copy_node (*expr_p
);
4502 TREE_OPERAND (*expr_p
, 1) = init
;
4516 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4517 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4518 DECL_GIMPLE_REG_P set.
4520 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4521 other, unmodified part of the complex object just before the total store.
4522 As a consequence, if the object is still uninitialized, an undefined value
4523 will be loaded into a register, which may result in a spurious exception
4524 if the register is floating-point and the value happens to be a signaling
4525 NaN for example. Then the fully-fledged complex operations lowering pass
4526 followed by a DCE pass are necessary in order to fix things up. */
4528 static enum gimplify_status
4529 gimplify_modify_expr_complex_part (tree
*expr_p
, gimple_seq
*pre_p
,
4532 enum tree_code code
, ocode
;
4533 tree lhs
, rhs
, new_rhs
, other
, realpart
, imagpart
;
4535 lhs
= TREE_OPERAND (*expr_p
, 0);
4536 rhs
= TREE_OPERAND (*expr_p
, 1);
4537 code
= TREE_CODE (lhs
);
4538 lhs
= TREE_OPERAND (lhs
, 0);
4540 ocode
= code
== REALPART_EXPR
? IMAGPART_EXPR
: REALPART_EXPR
;
4541 other
= build1 (ocode
, TREE_TYPE (rhs
), lhs
);
4542 TREE_NO_WARNING (other
) = 1;
4543 other
= get_formal_tmp_var (other
, pre_p
);
4545 realpart
= code
== REALPART_EXPR
? rhs
: other
;
4546 imagpart
= code
== REALPART_EXPR
? other
: rhs
;
4548 if (TREE_CONSTANT (realpart
) && TREE_CONSTANT (imagpart
))
4549 new_rhs
= build_complex (TREE_TYPE (lhs
), realpart
, imagpart
);
4551 new_rhs
= build2 (COMPLEX_EXPR
, TREE_TYPE (lhs
), realpart
, imagpart
);
4553 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (lhs
, new_rhs
));
4554 *expr_p
= (want_value
) ? rhs
: NULL_TREE
;
4559 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4565 PRE_P points to the list where side effects that must happen before
4566 *EXPR_P should be stored.
4568 POST_P points to the list where side effects that must happen after
4569 *EXPR_P should be stored.
4571 WANT_VALUE is nonzero iff we want to use the value of this expression
4572 in another expression. */
4574 static enum gimplify_status
4575 gimplify_modify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
4578 tree
*from_p
= &TREE_OPERAND (*expr_p
, 1);
4579 tree
*to_p
= &TREE_OPERAND (*expr_p
, 0);
4580 enum gimplify_status ret
= GS_UNHANDLED
;
4582 location_t loc
= EXPR_LOCATION (*expr_p
);
4584 gcc_assert (TREE_CODE (*expr_p
) == MODIFY_EXPR
4585 || TREE_CODE (*expr_p
) == INIT_EXPR
);
4587 /* Trying to simplify a clobber using normal logic doesn't work,
4588 so handle it here. */
4589 if (TREE_CLOBBER_P (*from_p
))
4591 gcc_assert (!want_value
&& TREE_CODE (*to_p
) == VAR_DECL
);
4592 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (*to_p
, *from_p
));
4597 /* Insert pointer conversions required by the middle-end that are not
4598 required by the frontend. This fixes middle-end type checking for
4599 for example gcc.dg/redecl-6.c. */
4600 if (POINTER_TYPE_P (TREE_TYPE (*to_p
)))
4602 STRIP_USELESS_TYPE_CONVERSION (*from_p
);
4603 if (!useless_type_conversion_p (TREE_TYPE (*to_p
), TREE_TYPE (*from_p
)))
4604 *from_p
= fold_convert_loc (loc
, TREE_TYPE (*to_p
), *from_p
);
4607 /* See if any simplifications can be done based on what the RHS is. */
4608 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
4610 if (ret
!= GS_UNHANDLED
)
4613 /* For zero sized types only gimplify the left hand side and right hand
4614 side as statements and throw away the assignment. Do this after
4615 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4617 if (zero_sized_type (TREE_TYPE (*from_p
)) && !want_value
)
4619 gimplify_stmt (from_p
, pre_p
);
4620 gimplify_stmt (to_p
, pre_p
);
4621 *expr_p
= NULL_TREE
;
4625 /* If the value being copied is of variable width, compute the length
4626 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4627 before gimplifying any of the operands so that we can resolve any
4628 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4629 the size of the expression to be copied, not of the destination, so
4630 that is what we must do here. */
4631 maybe_with_size_expr (from_p
);
4633 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
4634 if (ret
== GS_ERROR
)
4637 /* As a special case, we have to temporarily allow for assignments
4638 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4639 a toplevel statement, when gimplifying the GENERIC expression
4640 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4641 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4643 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4644 prevent gimplify_expr from trying to create a new temporary for
4645 foo's LHS, we tell it that it should only gimplify until it
4646 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4647 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4648 and all we need to do here is set 'a' to be its LHS. */
4649 ret
= gimplify_expr (from_p
, pre_p
, post_p
, rhs_predicate_for (*to_p
),
4651 if (ret
== GS_ERROR
)
4654 /* Now see if the above changed *from_p to something we handle specially. */
4655 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
4657 if (ret
!= GS_UNHANDLED
)
4660 /* If we've got a variable sized assignment between two lvalues (i.e. does
4661 not involve a call), then we can make things a bit more straightforward
4662 by converting the assignment to memcpy or memset. */
4663 if (TREE_CODE (*from_p
) == WITH_SIZE_EXPR
)
4665 tree from
= TREE_OPERAND (*from_p
, 0);
4666 tree size
= TREE_OPERAND (*from_p
, 1);
4668 if (TREE_CODE (from
) == CONSTRUCTOR
)
4669 return gimplify_modify_expr_to_memset (expr_p
, size
, want_value
, pre_p
);
4671 if (is_gimple_addressable (from
))
4674 return gimplify_modify_expr_to_memcpy (expr_p
, size
, want_value
,
4679 /* Transform partial stores to non-addressable complex variables into
4680 total stores. This allows us to use real instead of virtual operands
4681 for these variables, which improves optimization. */
4682 if ((TREE_CODE (*to_p
) == REALPART_EXPR
4683 || TREE_CODE (*to_p
) == IMAGPART_EXPR
)
4684 && is_gimple_reg (TREE_OPERAND (*to_p
, 0)))
4685 return gimplify_modify_expr_complex_part (expr_p
, pre_p
, want_value
);
4687 /* Try to alleviate the effects of the gimplification creating artificial
4688 temporaries (see for example is_gimple_reg_rhs) on the debug info. */
4689 if (!gimplify_ctxp
->into_ssa
4690 && TREE_CODE (*from_p
) == VAR_DECL
4691 && DECL_IGNORED_P (*from_p
)
4693 && !DECL_IGNORED_P (*to_p
))
4695 if (!DECL_NAME (*from_p
) && DECL_NAME (*to_p
))
4697 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p
)));
4698 DECL_DEBUG_EXPR_IS_FROM (*from_p
) = 1;
4699 SET_DECL_DEBUG_EXPR (*from_p
, *to_p
);
4702 if (want_value
&& TREE_THIS_VOLATILE (*to_p
))
4703 *from_p
= get_initialized_tmp_var (*from_p
, pre_p
, post_p
);
4705 if (TREE_CODE (*from_p
) == CALL_EXPR
)
4707 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4708 instead of a GIMPLE_ASSIGN. */
4709 tree fnptrtype
= TREE_TYPE (CALL_EXPR_FN (*from_p
));
4710 CALL_EXPR_FN (*from_p
) = TREE_OPERAND (CALL_EXPR_FN (*from_p
), 0);
4711 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p
));
4712 assign
= gimple_build_call_from_tree (*from_p
);
4713 gimple_call_set_fntype (assign
, TREE_TYPE (fnptrtype
));
4714 if (!gimple_call_noreturn_p (assign
))
4715 gimple_call_set_lhs (assign
, *to_p
);
4719 assign
= gimple_build_assign (*to_p
, *from_p
);
4720 gimple_set_location (assign
, EXPR_LOCATION (*expr_p
));
4723 gimplify_seq_add_stmt (pre_p
, assign
);
4725 if (gimplify_ctxp
->into_ssa
&& is_gimple_reg (*to_p
))
4727 /* If we've somehow already got an SSA_NAME on the LHS, then
4728 we've probably modified it twice. Not good. */
4729 gcc_assert (TREE_CODE (*to_p
) != SSA_NAME
);
4730 *to_p
= make_ssa_name (*to_p
, assign
);
4731 gimple_set_lhs (assign
, *to_p
);
4736 *expr_p
= TREE_THIS_VOLATILE (*to_p
) ? *from_p
: unshare_expr (*to_p
);
4745 /* Gimplify a comparison between two variable-sized objects. Do this
4746 with a call to BUILT_IN_MEMCMP. */
4748 static enum gimplify_status
4749 gimplify_variable_sized_compare (tree
*expr_p
)
4751 location_t loc
= EXPR_LOCATION (*expr_p
);
4752 tree op0
= TREE_OPERAND (*expr_p
, 0);
4753 tree op1
= TREE_OPERAND (*expr_p
, 1);
4754 tree t
, arg
, dest
, src
, expr
;
4756 arg
= TYPE_SIZE_UNIT (TREE_TYPE (op0
));
4757 arg
= unshare_expr (arg
);
4758 arg
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg
, op0
);
4759 src
= build_fold_addr_expr_loc (loc
, op1
);
4760 dest
= build_fold_addr_expr_loc (loc
, op0
);
4761 t
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
4762 t
= build_call_expr_loc (loc
, t
, 3, dest
, src
, arg
);
4765 = build2 (TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), t
, integer_zero_node
);
4766 SET_EXPR_LOCATION (expr
, loc
);
4772 /* Gimplify a comparison between two aggregate objects of integral scalar
4773 mode as a comparison between the bitwise equivalent scalar values. */
4775 static enum gimplify_status
4776 gimplify_scalar_mode_aggregate_compare (tree
*expr_p
)
4778 location_t loc
= EXPR_LOCATION (*expr_p
);
4779 tree op0
= TREE_OPERAND (*expr_p
, 0);
4780 tree op1
= TREE_OPERAND (*expr_p
, 1);
4782 tree type
= TREE_TYPE (op0
);
4783 tree scalar_type
= lang_hooks
.types
.type_for_mode (TYPE_MODE (type
), 1);
4785 op0
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op0
);
4786 op1
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op1
);
4789 = fold_build2_loc (loc
, TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), op0
, op1
);
4794 /* Gimplify an expression sequence. This function gimplifies each
4795 expression and rewrites the original expression with the last
4796 expression of the sequence in GIMPLE form.
4798 PRE_P points to the list where the side effects for all the
4799 expressions in the sequence will be emitted.
4801 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4803 static enum gimplify_status
4804 gimplify_compound_expr (tree
*expr_p
, gimple_seq
*pre_p
, bool want_value
)
4810 tree
*sub_p
= &TREE_OPERAND (t
, 0);
4812 if (TREE_CODE (*sub_p
) == COMPOUND_EXPR
)
4813 gimplify_compound_expr (sub_p
, pre_p
, false);
4815 gimplify_stmt (sub_p
, pre_p
);
4817 t
= TREE_OPERAND (t
, 1);
4819 while (TREE_CODE (t
) == COMPOUND_EXPR
);
4826 gimplify_stmt (expr_p
, pre_p
);
4831 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4832 gimplify. After gimplification, EXPR_P will point to a new temporary
4833 that holds the original value of the SAVE_EXPR node.
4835 PRE_P points to the list where side effects that must happen before
4836 *EXPR_P should be stored. */
4838 static enum gimplify_status
4839 gimplify_save_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
4841 enum gimplify_status ret
= GS_ALL_DONE
;
4844 gcc_assert (TREE_CODE (*expr_p
) == SAVE_EXPR
);
4845 val
= TREE_OPERAND (*expr_p
, 0);
4847 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4848 if (!SAVE_EXPR_RESOLVED_P (*expr_p
))
4850 /* The operand may be a void-valued expression such as SAVE_EXPRs
4851 generated by the Java frontend for class initialization. It is
4852 being executed only for its side-effects. */
4853 if (TREE_TYPE (val
) == void_type_node
)
4855 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
4856 is_gimple_stmt
, fb_none
);
4860 val
= get_initialized_tmp_var (val
, pre_p
, post_p
);
4862 TREE_OPERAND (*expr_p
, 0) = val
;
4863 SAVE_EXPR_RESOLVED_P (*expr_p
) = 1;
4871 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
4878 PRE_P points to the list where side effects that must happen before
4879 *EXPR_P should be stored.
4881 POST_P points to the list where side effects that must happen after
4882 *EXPR_P should be stored. */
4884 static enum gimplify_status
4885 gimplify_addr_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
4887 tree expr
= *expr_p
;
4888 tree op0
= TREE_OPERAND (expr
, 0);
4889 enum gimplify_status ret
;
4890 location_t loc
= EXPR_LOCATION (*expr_p
);
4892 switch (TREE_CODE (op0
))
4896 /* Check if we are dealing with an expression of the form '&*ptr'.
4897 While the front end folds away '&*ptr' into 'ptr', these
4898 expressions may be generated internally by the compiler (e.g.,
4899 builtins like __builtin_va_end). */
4900 /* Caution: the silent array decomposition semantics we allow for
4901 ADDR_EXPR means we can't always discard the pair. */
4902 /* Gimplification of the ADDR_EXPR operand may drop
4903 cv-qualification conversions, so make sure we add them if
4906 tree op00
= TREE_OPERAND (op0
, 0);
4907 tree t_expr
= TREE_TYPE (expr
);
4908 tree t_op00
= TREE_TYPE (op00
);
4910 if (!useless_type_conversion_p (t_expr
, t_op00
))
4911 op00
= fold_convert_loc (loc
, TREE_TYPE (expr
), op00
);
4917 case VIEW_CONVERT_EXPR
:
4918 /* Take the address of our operand and then convert it to the type of
4921 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
4922 all clear. The impact of this transformation is even less clear. */
4924 /* If the operand is a useless conversion, look through it. Doing so
4925 guarantees that the ADDR_EXPR and its operand will remain of the
4927 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0
, 0)))
4928 op0
= TREE_OPERAND (op0
, 0);
4930 *expr_p
= fold_convert_loc (loc
, TREE_TYPE (expr
),
4931 build_fold_addr_expr_loc (loc
,
4932 TREE_OPERAND (op0
, 0)));
4937 /* We use fb_either here because the C frontend sometimes takes
4938 the address of a call that returns a struct; see
4939 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
4940 the implied temporary explicit. */
4942 /* Make the operand addressable. */
4943 ret
= gimplify_expr (&TREE_OPERAND (expr
, 0), pre_p
, post_p
,
4944 is_gimple_addressable
, fb_either
);
4945 if (ret
== GS_ERROR
)
4948 /* Then mark it. Beware that it may not be possible to do so directly
4949 if a temporary has been created by the gimplification. */
4950 prepare_gimple_addressable (&TREE_OPERAND (expr
, 0), pre_p
);
4952 op0
= TREE_OPERAND (expr
, 0);
4954 /* For various reasons, the gimplification of the expression
4955 may have made a new INDIRECT_REF. */
4956 if (TREE_CODE (op0
) == INDIRECT_REF
)
4957 goto do_indirect_ref
;
4959 mark_addressable (TREE_OPERAND (expr
, 0));
4961 /* The FEs may end up building ADDR_EXPRs early on a decl with
4962 an incomplete type. Re-build ADDR_EXPRs in canonical form
4964 if (!types_compatible_p (TREE_TYPE (op0
), TREE_TYPE (TREE_TYPE (expr
))))
4965 *expr_p
= build_fold_addr_expr (op0
);
4967 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
4968 recompute_tree_invariant_for_addr_expr (*expr_p
);
4970 /* If we re-built the ADDR_EXPR add a conversion to the original type
4972 if (!useless_type_conversion_p (TREE_TYPE (expr
), TREE_TYPE (*expr_p
)))
4973 *expr_p
= fold_convert (TREE_TYPE (expr
), *expr_p
);
4981 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
4982 value; output operands should be a gimple lvalue. */
4984 static enum gimplify_status
4985 gimplify_asm_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
4989 const char **oconstraints
;
4992 const char *constraint
;
4993 bool allows_mem
, allows_reg
, is_inout
;
4994 enum gimplify_status ret
, tret
;
4996 VEC(tree
, gc
) *inputs
;
4997 VEC(tree
, gc
) *outputs
;
4998 VEC(tree
, gc
) *clobbers
;
4999 VEC(tree
, gc
) *labels
;
5003 noutputs
= list_length (ASM_OUTPUTS (expr
));
5004 oconstraints
= (const char **) alloca ((noutputs
) * sizeof (const char *));
5006 inputs
= outputs
= clobbers
= labels
= NULL
;
5009 link_next
= NULL_TREE
;
5010 for (i
= 0, link
= ASM_OUTPUTS (expr
); link
; ++i
, link
= link_next
)
5013 size_t constraint_len
;
5015 link_next
= TREE_CHAIN (link
);
5019 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5020 constraint_len
= strlen (constraint
);
5021 if (constraint_len
== 0)
5024 ok
= parse_output_constraint (&constraint
, i
, 0, 0,
5025 &allows_mem
, &allows_reg
, &is_inout
);
5032 if (!allows_reg
&& allows_mem
)
5033 mark_addressable (TREE_VALUE (link
));
5035 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5036 is_inout
? is_gimple_min_lval
: is_gimple_lvalue
,
5037 fb_lvalue
| fb_mayfail
);
5038 if (tret
== GS_ERROR
)
5040 error ("invalid lvalue in asm output %d", i
);
5044 VEC_safe_push (tree
, gc
, outputs
, link
);
5045 TREE_CHAIN (link
) = NULL_TREE
;
5049 /* An input/output operand. To give the optimizers more
5050 flexibility, split it into separate input and output
5055 /* Turn the in/out constraint into an output constraint. */
5056 char *p
= xstrdup (constraint
);
5058 TREE_VALUE (TREE_PURPOSE (link
)) = build_string (constraint_len
, p
);
5060 /* And add a matching input constraint. */
5063 sprintf (buf
, "%d", i
);
5065 /* If there are multiple alternatives in the constraint,
5066 handle each of them individually. Those that allow register
5067 will be replaced with operand number, the others will stay
5069 if (strchr (p
, ',') != NULL
)
5071 size_t len
= 0, buflen
= strlen (buf
);
5072 char *beg
, *end
, *str
, *dst
;
5076 end
= strchr (beg
, ',');
5078 end
= strchr (beg
, '\0');
5079 if ((size_t) (end
- beg
) < buflen
)
5082 len
+= end
- beg
+ 1;
5089 str
= (char *) alloca (len
);
5090 for (beg
= p
+ 1, dst
= str
;;)
5093 bool mem_p
, reg_p
, inout_p
;
5095 end
= strchr (beg
, ',');
5100 parse_output_constraint (&tem
, i
, 0, 0,
5101 &mem_p
, ®_p
, &inout_p
);
5106 memcpy (dst
, buf
, buflen
);
5115 memcpy (dst
, beg
, len
);
5124 input
= build_string (dst
- str
, str
);
5127 input
= build_string (strlen (buf
), buf
);
5130 input
= build_string (constraint_len
- 1, constraint
+ 1);
5134 input
= build_tree_list (build_tree_list (NULL_TREE
, input
),
5135 unshare_expr (TREE_VALUE (link
)));
5136 ASM_INPUTS (expr
) = chainon (ASM_INPUTS (expr
), input
);
5140 link_next
= NULL_TREE
;
5141 for (link
= ASM_INPUTS (expr
); link
; ++i
, link
= link_next
)
5143 link_next
= TREE_CHAIN (link
);
5144 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5145 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
5146 oconstraints
, &allows_mem
, &allows_reg
);
5148 /* If we can't make copies, we can only accept memory. */
5149 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link
))))
5155 error ("impossible constraint in %<asm%>");
5156 error ("non-memory input %d must stay in memory", i
);
5161 /* If the operand is a memory input, it should be an lvalue. */
5162 if (!allows_reg
&& allows_mem
)
5164 tree inputv
= TREE_VALUE (link
);
5165 STRIP_NOPS (inputv
);
5166 if (TREE_CODE (inputv
) == PREDECREMENT_EXPR
5167 || TREE_CODE (inputv
) == PREINCREMENT_EXPR
5168 || TREE_CODE (inputv
) == POSTDECREMENT_EXPR
5169 || TREE_CODE (inputv
) == POSTINCREMENT_EXPR
)
5170 TREE_VALUE (link
) = error_mark_node
;
5171 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5172 is_gimple_lvalue
, fb_lvalue
| fb_mayfail
);
5173 mark_addressable (TREE_VALUE (link
));
5174 if (tret
== GS_ERROR
)
5176 if (EXPR_HAS_LOCATION (TREE_VALUE (link
)))
5177 input_location
= EXPR_LOCATION (TREE_VALUE (link
));
5178 error ("memory input %d is not directly addressable", i
);
5184 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5185 is_gimple_asm_val
, fb_rvalue
);
5186 if (tret
== GS_ERROR
)
5190 TREE_CHAIN (link
) = NULL_TREE
;
5191 VEC_safe_push (tree
, gc
, inputs
, link
);
5194 for (link
= ASM_CLOBBERS (expr
); link
; ++i
, link
= TREE_CHAIN (link
))
5195 VEC_safe_push (tree
, gc
, clobbers
, link
);
5197 for (link
= ASM_LABELS (expr
); link
; ++i
, link
= TREE_CHAIN (link
))
5198 VEC_safe_push (tree
, gc
, labels
, link
);
5200 /* Do not add ASMs with errors to the gimple IL stream. */
5201 if (ret
!= GS_ERROR
)
5203 stmt
= gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr
)),
5204 inputs
, outputs
, clobbers
, labels
);
5206 gimple_asm_set_volatile (stmt
, ASM_VOLATILE_P (expr
));
5207 gimple_asm_set_input (stmt
, ASM_INPUT_P (expr
));
5209 gimplify_seq_add_stmt (pre_p
, stmt
);
5215 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5216 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5217 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5218 return to this function.
5220 FIXME should we complexify the prequeue handling instead? Or use flags
5221 for all the cleanups and let the optimizer tighten them up? The current
5222 code seems pretty fragile; it will break on a cleanup within any
5223 non-conditional nesting. But any such nesting would be broken, anyway;
5224 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5225 and continues out of it. We can do that at the RTL level, though, so
5226 having an optimizer to tighten up try/finally regions would be a Good
5229 static enum gimplify_status
5230 gimplify_cleanup_point_expr (tree
*expr_p
, gimple_seq
*pre_p
)
5232 gimple_stmt_iterator iter
;
5233 gimple_seq body_sequence
= NULL
;
5235 tree temp
= voidify_wrapper_expr (*expr_p
, NULL
);
5237 /* We only care about the number of conditions between the innermost
5238 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5239 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5240 int old_conds
= gimplify_ctxp
->conditions
;
5241 gimple_seq old_cleanups
= gimplify_ctxp
->conditional_cleanups
;
5242 bool old_in_cleanup_point_expr
= gimplify_ctxp
->in_cleanup_point_expr
;
5243 gimplify_ctxp
->conditions
= 0;
5244 gimplify_ctxp
->conditional_cleanups
= NULL
;
5245 gimplify_ctxp
->in_cleanup_point_expr
= true;
5247 gimplify_stmt (&TREE_OPERAND (*expr_p
, 0), &body_sequence
);
5249 gimplify_ctxp
->conditions
= old_conds
;
5250 gimplify_ctxp
->conditional_cleanups
= old_cleanups
;
5251 gimplify_ctxp
->in_cleanup_point_expr
= old_in_cleanup_point_expr
;
5253 for (iter
= gsi_start (body_sequence
); !gsi_end_p (iter
); )
5255 gimple wce
= gsi_stmt (iter
);
5257 if (gimple_code (wce
) == GIMPLE_WITH_CLEANUP_EXPR
)
5259 if (gsi_one_before_end_p (iter
))
5261 /* Note that gsi_insert_seq_before and gsi_remove do not
5262 scan operands, unlike some other sequence mutators. */
5263 if (!gimple_wce_cleanup_eh_only (wce
))
5264 gsi_insert_seq_before_without_update (&iter
,
5265 gimple_wce_cleanup (wce
),
5267 gsi_remove (&iter
, true);
5274 enum gimple_try_flags kind
;
5276 if (gimple_wce_cleanup_eh_only (wce
))
5277 kind
= GIMPLE_TRY_CATCH
;
5279 kind
= GIMPLE_TRY_FINALLY
;
5280 seq
= gsi_split_seq_after (iter
);
5282 gtry
= gimple_build_try (seq
, gimple_wce_cleanup (wce
), kind
);
5283 /* Do not use gsi_replace here, as it may scan operands.
5284 We want to do a simple structural modification only. */
5285 *gsi_stmt_ptr (&iter
) = gtry
;
5286 iter
= gsi_start (seq
);
5293 gimplify_seq_add_seq (pre_p
, body_sequence
);
5306 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5307 is the cleanup action required. EH_ONLY is true if the cleanup should
5308 only be executed if an exception is thrown, not on normal exit. */
5311 gimple_push_cleanup (tree var
, tree cleanup
, bool eh_only
, gimple_seq
*pre_p
)
5314 gimple_seq cleanup_stmts
= NULL
;
5316 /* Errors can result in improperly nested cleanups. Which results in
5317 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5321 if (gimple_conditional_context ())
5323 /* If we're in a conditional context, this is more complex. We only
5324 want to run the cleanup if we actually ran the initialization that
5325 necessitates it, but we want to run it after the end of the
5326 conditional context. So we wrap the try/finally around the
5327 condition and use a flag to determine whether or not to actually
5328 run the destructor. Thus
5332 becomes (approximately)
5336 if (test) { A::A(temp); flag = 1; val = f(temp); }
5339 if (flag) A::~A(temp);
5343 tree flag
= create_tmp_var (boolean_type_node
, "cleanup");
5344 gimple ffalse
= gimple_build_assign (flag
, boolean_false_node
);
5345 gimple ftrue
= gimple_build_assign (flag
, boolean_true_node
);
5347 cleanup
= build3 (COND_EXPR
, void_type_node
, flag
, cleanup
, NULL
);
5348 gimplify_stmt (&cleanup
, &cleanup_stmts
);
5349 wce
= gimple_build_wce (cleanup_stmts
);
5351 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, ffalse
);
5352 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, wce
);
5353 gimplify_seq_add_stmt (pre_p
, ftrue
);
5355 /* Because of this manipulation, and the EH edges that jump
5356 threading cannot redirect, the temporary (VAR) will appear
5357 to be used uninitialized. Don't warn. */
5358 TREE_NO_WARNING (var
) = 1;
5362 gimplify_stmt (&cleanup
, &cleanup_stmts
);
5363 wce
= gimple_build_wce (cleanup_stmts
);
5364 gimple_wce_set_cleanup_eh_only (wce
, eh_only
);
5365 gimplify_seq_add_stmt (pre_p
, wce
);
5369 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5371 static enum gimplify_status
5372 gimplify_target_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5374 tree targ
= *expr_p
;
5375 tree temp
= TARGET_EXPR_SLOT (targ
);
5376 tree init
= TARGET_EXPR_INITIAL (targ
);
5377 enum gimplify_status ret
;
5381 tree cleanup
= NULL_TREE
;
5383 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5384 to the temps list. Handle also variable length TARGET_EXPRs. */
5385 if (TREE_CODE (DECL_SIZE (temp
)) != INTEGER_CST
)
5387 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp
)))
5388 gimplify_type_sizes (TREE_TYPE (temp
), pre_p
);
5389 gimplify_vla_decl (temp
, pre_p
);
5392 gimple_add_tmp_var (temp
);
5394 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5395 expression is supposed to initialize the slot. */
5396 if (VOID_TYPE_P (TREE_TYPE (init
)))
5397 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
5400 tree init_expr
= build2 (INIT_EXPR
, void_type_node
, temp
, init
);
5402 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
5404 ggc_free (init_expr
);
5406 if (ret
== GS_ERROR
)
5408 /* PR c++/28266 Make sure this is expanded only once. */
5409 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
5413 gimplify_and_add (init
, pre_p
);
5415 /* If needed, push the cleanup for the temp. */
5416 if (TARGET_EXPR_CLEANUP (targ
))
5418 if (CLEANUP_EH_ONLY (targ
))
5419 gimple_push_cleanup (temp
, TARGET_EXPR_CLEANUP (targ
),
5420 CLEANUP_EH_ONLY (targ
), pre_p
);
5422 cleanup
= TARGET_EXPR_CLEANUP (targ
);
5425 /* Add a clobber for the temporary going out of scope, like
5426 gimplify_bind_expr. */
5427 if (gimplify_ctxp
->in_cleanup_point_expr
5428 && needs_to_live_in_memory (temp
))
5430 tree clobber
= build_constructor (TREE_TYPE (temp
), NULL
);
5431 TREE_THIS_VOLATILE (clobber
) = true;
5432 clobber
= build2 (MODIFY_EXPR
, TREE_TYPE (temp
), temp
, clobber
);
5434 cleanup
= build2 (COMPOUND_EXPR
, void_type_node
, cleanup
,
5441 gimple_push_cleanup (temp
, cleanup
, false, pre_p
);
5443 /* Only expand this once. */
5444 TREE_OPERAND (targ
, 3) = init
;
5445 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
5448 /* We should have expanded this before. */
5449 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp
));
5455 /* Gimplification of expression trees. */
5457 /* Gimplify an expression which appears at statement context. The
5458 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5459 NULL, a new sequence is allocated.
5461 Return true if we actually added a statement to the queue. */
5464 gimplify_stmt (tree
*stmt_p
, gimple_seq
*seq_p
)
5466 gimple_seq_node last
;
5469 *seq_p
= gimple_seq_alloc ();
5471 last
= gimple_seq_last (*seq_p
);
5472 gimplify_expr (stmt_p
, seq_p
, NULL
, is_gimple_stmt
, fb_none
);
5473 return last
!= gimple_seq_last (*seq_p
);
5476 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5477 to CTX. If entries already exist, force them to be some flavor of private.
5478 If there is no enclosing parallel, do nothing. */
5481 omp_firstprivatize_variable (struct gimplify_omp_ctx
*ctx
, tree decl
)
5485 if (decl
== NULL
|| !DECL_P (decl
))
5490 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5493 if (n
->value
& GOVD_SHARED
)
5494 n
->value
= GOVD_FIRSTPRIVATE
| (n
->value
& GOVD_SEEN
);
5498 else if (ctx
->region_type
!= ORT_WORKSHARE
)
5499 omp_add_variable (ctx
, decl
, GOVD_FIRSTPRIVATE
);
5501 ctx
= ctx
->outer_context
;
5506 /* Similarly for each of the type sizes of TYPE. */
5509 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx
*ctx
, tree type
)
5511 if (type
== NULL
|| type
== error_mark_node
)
5513 type
= TYPE_MAIN_VARIANT (type
);
5515 if (pointer_set_insert (ctx
->privatized_types
, type
))
5518 switch (TREE_CODE (type
))
5524 case FIXED_POINT_TYPE
:
5525 omp_firstprivatize_variable (ctx
, TYPE_MIN_VALUE (type
));
5526 omp_firstprivatize_variable (ctx
, TYPE_MAX_VALUE (type
));
5530 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
5531 omp_firstprivatize_type_sizes (ctx
, TYPE_DOMAIN (type
));
5536 case QUAL_UNION_TYPE
:
5539 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5540 if (TREE_CODE (field
) == FIELD_DECL
)
5542 omp_firstprivatize_variable (ctx
, DECL_FIELD_OFFSET (field
));
5543 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (field
));
5549 case REFERENCE_TYPE
:
5550 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
5557 omp_firstprivatize_variable (ctx
, TYPE_SIZE (type
));
5558 omp_firstprivatize_variable (ctx
, TYPE_SIZE_UNIT (type
));
5559 lang_hooks
.types
.omp_firstprivatize_type_sizes (ctx
, type
);
5562 /* Add an entry for DECL in the OpenMP context CTX with FLAGS. */
5565 omp_add_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, unsigned int flags
)
5568 unsigned int nflags
;
5571 if (error_operand_p (decl
))
5574 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5575 there are constructors involved somewhere. */
5576 if (TREE_ADDRESSABLE (TREE_TYPE (decl
))
5577 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
5580 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5583 /* We shouldn't be re-adding the decl with the same data
5585 gcc_assert ((n
->value
& GOVD_DATA_SHARE_CLASS
& flags
) == 0);
5586 /* The only combination of data sharing classes we should see is
5587 FIRSTPRIVATE and LASTPRIVATE. */
5588 nflags
= n
->value
| flags
;
5589 gcc_assert ((nflags
& GOVD_DATA_SHARE_CLASS
)
5590 == (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
));
5595 /* When adding a variable-sized variable, we have to handle all sorts
5596 of additional bits of data: the pointer replacement variable, and
5597 the parameters of the type. */
5598 if (DECL_SIZE (decl
) && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
5600 /* Add the pointer replacement variable as PRIVATE if the variable
5601 replacement is private, else FIRSTPRIVATE since we'll need the
5602 address of the original variable either for SHARED, or for the
5603 copy into or out of the context. */
5604 if (!(flags
& GOVD_LOCAL
))
5606 nflags
= flags
& GOVD_PRIVATE
? GOVD_PRIVATE
: GOVD_FIRSTPRIVATE
;
5607 nflags
|= flags
& GOVD_SEEN
;
5608 t
= DECL_VALUE_EXPR (decl
);
5609 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
5610 t
= TREE_OPERAND (t
, 0);
5611 gcc_assert (DECL_P (t
));
5612 omp_add_variable (ctx
, t
, nflags
);
5615 /* Add all of the variable and type parameters (which should have
5616 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5617 omp_firstprivatize_variable (ctx
, DECL_SIZE_UNIT (decl
));
5618 omp_firstprivatize_variable (ctx
, DECL_SIZE (decl
));
5619 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
5621 /* The variable-sized variable itself is never SHARED, only some form
5622 of PRIVATE. The sharing would take place via the pointer variable
5623 which we remapped above. */
5624 if (flags
& GOVD_SHARED
)
5625 flags
= GOVD_PRIVATE
| GOVD_DEBUG_PRIVATE
5626 | (flags
& (GOVD_SEEN
| GOVD_EXPLICIT
));
5628 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5629 alloca statement we generate for the variable, so make sure it
5630 is available. This isn't automatically needed for the SHARED
5631 case, since we won't be allocating local storage then.
5632 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5633 in this case omp_notice_variable will be called later
5634 on when it is gimplified. */
5635 else if (! (flags
& GOVD_LOCAL
)
5636 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl
))))
5637 omp_notice_variable (ctx
, TYPE_SIZE_UNIT (TREE_TYPE (decl
)), true);
5639 else if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
5641 gcc_assert ((flags
& GOVD_LOCAL
) == 0);
5642 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
5644 /* Similar to the direct variable sized case above, we'll need the
5645 size of references being privatized. */
5646 if ((flags
& GOVD_SHARED
) == 0)
5648 t
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)));
5649 if (TREE_CODE (t
) != INTEGER_CST
)
5650 omp_notice_variable (ctx
, t
, true);
5654 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, flags
);
5657 /* Notice a threadprivate variable DECL used in OpenMP context CTX.
5658 This just prints out diagnostics about threadprivate variable uses
5659 in untied tasks. If DECL2 is non-NULL, prevent this warning
5660 on that variable. */
5663 omp_notice_threadprivate_variable (struct gimplify_omp_ctx
*ctx
, tree decl
,
5668 if (ctx
->region_type
!= ORT_UNTIED_TASK
)
5670 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5673 error ("threadprivate variable %qE used in untied task",
5675 error_at (ctx
->location
, "enclosing task");
5676 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, 0);
5679 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl2
, 0);
5683 /* Record the fact that DECL was used within the OpenMP context CTX.
5684 IN_CODE is true when real code uses DECL, and false when we should
5685 merely emit default(none) errors. Return true if DECL is going to
5686 be remapped and thus DECL shouldn't be gimplified into its
5687 DECL_VALUE_EXPR (if any). */
5690 omp_notice_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, bool in_code
)
5693 unsigned flags
= in_code
? GOVD_SEEN
: 0;
5694 bool ret
= false, shared
;
5696 if (error_operand_p (decl
))
5699 /* Threadprivate variables are predetermined. */
5700 if (is_global_var (decl
))
5702 if (DECL_THREAD_LOCAL_P (decl
))
5703 return omp_notice_threadprivate_variable (ctx
, decl
, NULL_TREE
);
5705 if (DECL_HAS_VALUE_EXPR_P (decl
))
5707 tree value
= get_base_address (DECL_VALUE_EXPR (decl
));
5709 if (value
&& DECL_P (value
) && DECL_THREAD_LOCAL_P (value
))
5710 return omp_notice_threadprivate_variable (ctx
, decl
, value
);
5714 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5717 enum omp_clause_default_kind default_kind
, kind
;
5718 struct gimplify_omp_ctx
*octx
;
5720 if (ctx
->region_type
== ORT_WORKSHARE
)
5723 /* ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5724 remapped firstprivate instead of shared. To some extent this is
5725 addressed in omp_firstprivatize_type_sizes, but not effectively. */
5726 default_kind
= ctx
->default_kind
;
5727 kind
= lang_hooks
.decls
.omp_predetermined_sharing (decl
);
5728 if (kind
!= OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
5729 default_kind
= kind
;
5731 switch (default_kind
)
5733 case OMP_CLAUSE_DEFAULT_NONE
:
5734 error ("%qE not specified in enclosing parallel",
5735 DECL_NAME (lang_hooks
.decls
.omp_report_decl (decl
)));
5736 if ((ctx
->region_type
& ORT_TASK
) != 0)
5737 error_at (ctx
->location
, "enclosing task");
5739 error_at (ctx
->location
, "enclosing parallel");
5741 case OMP_CLAUSE_DEFAULT_SHARED
:
5742 flags
|= GOVD_SHARED
;
5744 case OMP_CLAUSE_DEFAULT_PRIVATE
:
5745 flags
|= GOVD_PRIVATE
;
5747 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE
:
5748 flags
|= GOVD_FIRSTPRIVATE
;
5750 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
5751 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
5752 gcc_assert ((ctx
->region_type
& ORT_TASK
) != 0);
5753 if (ctx
->outer_context
)
5754 omp_notice_variable (ctx
->outer_context
, decl
, in_code
);
5755 for (octx
= ctx
->outer_context
; octx
; octx
= octx
->outer_context
)
5759 n2
= splay_tree_lookup (octx
->variables
, (splay_tree_key
) decl
);
5760 if (n2
&& (n2
->value
& GOVD_DATA_SHARE_CLASS
) != GOVD_SHARED
)
5762 flags
|= GOVD_FIRSTPRIVATE
;
5765 if ((octx
->region_type
& ORT_PARALLEL
) != 0)
5768 if (flags
& GOVD_FIRSTPRIVATE
)
5771 && (TREE_CODE (decl
) == PARM_DECL
5772 || (!is_global_var (decl
)
5773 && DECL_CONTEXT (decl
) == current_function_decl
)))
5775 flags
|= GOVD_FIRSTPRIVATE
;
5778 flags
|= GOVD_SHARED
;
5784 if ((flags
& GOVD_PRIVATE
)
5785 && lang_hooks
.decls
.omp_private_outer_ref (decl
))
5786 flags
|= GOVD_PRIVATE_OUTER_REF
;
5788 omp_add_variable (ctx
, decl
, flags
);
5790 shared
= (flags
& GOVD_SHARED
) != 0;
5791 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
5795 if ((n
->value
& (GOVD_SEEN
| GOVD_LOCAL
)) == 0
5796 && (flags
& (GOVD_SEEN
| GOVD_LOCAL
)) == GOVD_SEEN
5798 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
5801 tree t
= DECL_VALUE_EXPR (decl
);
5802 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
5803 t
= TREE_OPERAND (t
, 0);
5804 gcc_assert (DECL_P (t
));
5805 n2
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
5806 n2
->value
|= GOVD_SEEN
;
5809 shared
= ((flags
| n
->value
) & GOVD_SHARED
) != 0;
5810 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
5812 /* If nothing changed, there's nothing left to do. */
5813 if ((n
->value
& flags
) == flags
)
5819 /* If the variable is private in the current context, then we don't
5820 need to propagate anything to an outer context. */
5821 if ((flags
& GOVD_PRIVATE
) && !(flags
& GOVD_PRIVATE_OUTER_REF
))
5823 if (ctx
->outer_context
5824 && omp_notice_variable (ctx
->outer_context
, decl
, in_code
))
5829 /* Verify that DECL is private within CTX. If there's specific information
5830 to the contrary in the innermost scope, generate an error. */
5833 omp_is_private (struct gimplify_omp_ctx
*ctx
, tree decl
)
5837 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5840 if (n
->value
& GOVD_SHARED
)
5842 if (ctx
== gimplify_omp_ctxp
)
5844 error ("iteration variable %qE should be private",
5846 n
->value
= GOVD_PRIVATE
;
5852 else if ((n
->value
& GOVD_EXPLICIT
) != 0
5853 && (ctx
== gimplify_omp_ctxp
5854 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
5855 && gimplify_omp_ctxp
->outer_context
== ctx
)))
5857 if ((n
->value
& GOVD_FIRSTPRIVATE
) != 0)
5858 error ("iteration variable %qE should not be firstprivate",
5860 else if ((n
->value
& GOVD_REDUCTION
) != 0)
5861 error ("iteration variable %qE should not be reduction",
5864 return (ctx
== gimplify_omp_ctxp
5865 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
5866 && gimplify_omp_ctxp
->outer_context
== ctx
));
5869 if (ctx
->region_type
!= ORT_WORKSHARE
)
5871 else if (ctx
->outer_context
)
5872 return omp_is_private (ctx
->outer_context
, decl
);
5876 /* Return true if DECL is private within a parallel region
5877 that binds to the current construct's context or in parallel
5878 region's REDUCTION clause. */
5881 omp_check_private (struct gimplify_omp_ctx
*ctx
, tree decl
)
5887 ctx
= ctx
->outer_context
;
5889 return !(is_global_var (decl
)
5890 /* References might be private, but might be shared too. */
5891 || lang_hooks
.decls
.omp_privatize_by_reference (decl
));
5893 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
5895 return (n
->value
& GOVD_SHARED
) == 0;
5897 while (ctx
->region_type
== ORT_WORKSHARE
);
5901 /* Scan the OpenMP clauses in *LIST_P, installing mappings into a new
5902 and previous omp contexts. */
5905 gimplify_scan_omp_clauses (tree
*list_p
, gimple_seq
*pre_p
,
5906 enum omp_region_type region_type
)
5908 struct gimplify_omp_ctx
*ctx
, *outer_ctx
;
5909 struct gimplify_ctx gctx
;
5912 ctx
= new_omp_context (region_type
);
5913 outer_ctx
= ctx
->outer_context
;
5915 while ((c
= *list_p
) != NULL
)
5917 bool remove
= false;
5918 bool notice_outer
= true;
5919 const char *check_non_private
= NULL
;
5923 switch (OMP_CLAUSE_CODE (c
))
5925 case OMP_CLAUSE_PRIVATE
:
5926 flags
= GOVD_PRIVATE
| GOVD_EXPLICIT
;
5927 if (lang_hooks
.decls
.omp_private_outer_ref (OMP_CLAUSE_DECL (c
)))
5929 flags
|= GOVD_PRIVATE_OUTER_REF
;
5930 OMP_CLAUSE_PRIVATE_OUTER_REF (c
) = 1;
5933 notice_outer
= false;
5935 case OMP_CLAUSE_SHARED
:
5936 flags
= GOVD_SHARED
| GOVD_EXPLICIT
;
5938 case OMP_CLAUSE_FIRSTPRIVATE
:
5939 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
5940 check_non_private
= "firstprivate";
5942 case OMP_CLAUSE_LASTPRIVATE
:
5943 flags
= GOVD_LASTPRIVATE
| GOVD_SEEN
| GOVD_EXPLICIT
;
5944 check_non_private
= "lastprivate";
5946 case OMP_CLAUSE_REDUCTION
:
5947 flags
= GOVD_REDUCTION
| GOVD_SEEN
| GOVD_EXPLICIT
;
5948 check_non_private
= "reduction";
5952 decl
= OMP_CLAUSE_DECL (c
);
5953 if (error_operand_p (decl
))
5958 omp_add_variable (ctx
, decl
, flags
);
5959 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
5960 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
5962 omp_add_variable (ctx
, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
),
5963 GOVD_LOCAL
| GOVD_SEEN
);
5964 gimplify_omp_ctxp
= ctx
;
5965 push_gimplify_context (&gctx
);
5967 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
) = gimple_seq_alloc ();
5968 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
) = gimple_seq_alloc ();
5970 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c
),
5971 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
));
5972 pop_gimplify_context
5973 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
)));
5974 push_gimplify_context (&gctx
);
5975 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c
),
5976 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
));
5977 pop_gimplify_context
5978 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
)));
5979 OMP_CLAUSE_REDUCTION_INIT (c
) = NULL_TREE
;
5980 OMP_CLAUSE_REDUCTION_MERGE (c
) = NULL_TREE
;
5982 gimplify_omp_ctxp
= outer_ctx
;
5984 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
5985 && OMP_CLAUSE_LASTPRIVATE_STMT (c
))
5987 gimplify_omp_ctxp
= ctx
;
5988 push_gimplify_context (&gctx
);
5989 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c
)) != BIND_EXPR
)
5991 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
,
5993 TREE_SIDE_EFFECTS (bind
) = 1;
5994 BIND_EXPR_BODY (bind
) = OMP_CLAUSE_LASTPRIVATE_STMT (c
);
5995 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = bind
;
5997 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c
),
5998 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
));
5999 pop_gimplify_context
6000 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
)));
6001 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = NULL_TREE
;
6003 gimplify_omp_ctxp
= outer_ctx
;
6009 case OMP_CLAUSE_COPYIN
:
6010 case OMP_CLAUSE_COPYPRIVATE
:
6011 decl
= OMP_CLAUSE_DECL (c
);
6012 if (error_operand_p (decl
))
6019 omp_notice_variable (outer_ctx
, decl
, true);
6020 if (check_non_private
6021 && region_type
== ORT_WORKSHARE
6022 && omp_check_private (ctx
, decl
))
6024 error ("%s variable %qE is private in outer context",
6025 check_non_private
, DECL_NAME (decl
));
6030 case OMP_CLAUSE_FINAL
:
6032 OMP_CLAUSE_OPERAND (c
, 0)
6033 = gimple_boolify (OMP_CLAUSE_OPERAND (c
, 0));
6036 case OMP_CLAUSE_SCHEDULE
:
6037 case OMP_CLAUSE_NUM_THREADS
:
6038 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 0), pre_p
, NULL
,
6039 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
6043 case OMP_CLAUSE_NOWAIT
:
6044 case OMP_CLAUSE_ORDERED
:
6045 case OMP_CLAUSE_UNTIED
:
6046 case OMP_CLAUSE_COLLAPSE
:
6047 case OMP_CLAUSE_MERGEABLE
:
6050 case OMP_CLAUSE_DEFAULT
:
6051 ctx
->default_kind
= OMP_CLAUSE_DEFAULT_KIND (c
);
6059 *list_p
= OMP_CLAUSE_CHAIN (c
);
6061 list_p
= &OMP_CLAUSE_CHAIN (c
);
6064 gimplify_omp_ctxp
= ctx
;
6067 /* For all variables that were not actually used within the context,
6068 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
6071 gimplify_adjust_omp_clauses_1 (splay_tree_node n
, void *data
)
6073 tree
*list_p
= (tree
*) data
;
6074 tree decl
= (tree
) n
->key
;
6075 unsigned flags
= n
->value
;
6076 enum omp_clause_code code
;
6080 if (flags
& (GOVD_EXPLICIT
| GOVD_LOCAL
))
6082 if ((flags
& GOVD_SEEN
) == 0)
6084 if (flags
& GOVD_DEBUG_PRIVATE
)
6086 gcc_assert ((flags
& GOVD_DATA_SHARE_CLASS
) == GOVD_PRIVATE
);
6087 private_debug
= true;
6091 = lang_hooks
.decls
.omp_private_debug_clause (decl
,
6092 !!(flags
& GOVD_SHARED
));
6094 code
= OMP_CLAUSE_PRIVATE
;
6095 else if (flags
& GOVD_SHARED
)
6097 if (is_global_var (decl
))
6099 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
->outer_context
;
6103 = splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
6104 if (on
&& (on
->value
& (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
6105 | GOVD_PRIVATE
| GOVD_REDUCTION
)) != 0)
6107 ctx
= ctx
->outer_context
;
6112 code
= OMP_CLAUSE_SHARED
;
6114 else if (flags
& GOVD_PRIVATE
)
6115 code
= OMP_CLAUSE_PRIVATE
;
6116 else if (flags
& GOVD_FIRSTPRIVATE
)
6117 code
= OMP_CLAUSE_FIRSTPRIVATE
;
6121 clause
= build_omp_clause (input_location
, code
);
6122 OMP_CLAUSE_DECL (clause
) = decl
;
6123 OMP_CLAUSE_CHAIN (clause
) = *list_p
;
6125 OMP_CLAUSE_PRIVATE_DEBUG (clause
) = 1;
6126 else if (code
== OMP_CLAUSE_PRIVATE
&& (flags
& GOVD_PRIVATE_OUTER_REF
))
6127 OMP_CLAUSE_PRIVATE_OUTER_REF (clause
) = 1;
6129 lang_hooks
.decls
.omp_finish_clause (clause
);
6135 gimplify_adjust_omp_clauses (tree
*list_p
)
6137 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
6140 while ((c
= *list_p
) != NULL
)
6143 bool remove
= false;
6145 switch (OMP_CLAUSE_CODE (c
))
6147 case OMP_CLAUSE_PRIVATE
:
6148 case OMP_CLAUSE_SHARED
:
6149 case OMP_CLAUSE_FIRSTPRIVATE
:
6150 decl
= OMP_CLAUSE_DECL (c
);
6151 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
6152 remove
= !(n
->value
& GOVD_SEEN
);
6155 bool shared
= OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
;
6156 if ((n
->value
& GOVD_DEBUG_PRIVATE
)
6157 || lang_hooks
.decls
.omp_private_debug_clause (decl
, shared
))
6159 gcc_assert ((n
->value
& GOVD_DEBUG_PRIVATE
) == 0
6160 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
6162 OMP_CLAUSE_SET_CODE (c
, OMP_CLAUSE_PRIVATE
);
6163 OMP_CLAUSE_PRIVATE_DEBUG (c
) = 1;
6168 case OMP_CLAUSE_LASTPRIVATE
:
6169 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
6170 accurately reflect the presence of a FIRSTPRIVATE clause. */
6171 decl
= OMP_CLAUSE_DECL (c
);
6172 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
6173 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
)
6174 = (n
->value
& GOVD_FIRSTPRIVATE
) != 0;
6177 case OMP_CLAUSE_REDUCTION
:
6178 case OMP_CLAUSE_COPYIN
:
6179 case OMP_CLAUSE_COPYPRIVATE
:
6181 case OMP_CLAUSE_NUM_THREADS
:
6182 case OMP_CLAUSE_SCHEDULE
:
6183 case OMP_CLAUSE_NOWAIT
:
6184 case OMP_CLAUSE_ORDERED
:
6185 case OMP_CLAUSE_DEFAULT
:
6186 case OMP_CLAUSE_UNTIED
:
6187 case OMP_CLAUSE_COLLAPSE
:
6188 case OMP_CLAUSE_FINAL
:
6189 case OMP_CLAUSE_MERGEABLE
:
6197 *list_p
= OMP_CLAUSE_CHAIN (c
);
6199 list_p
= &OMP_CLAUSE_CHAIN (c
);
6202 /* Add in any implicit data sharing. */
6203 splay_tree_foreach (ctx
->variables
, gimplify_adjust_omp_clauses_1
, list_p
);
6205 gimplify_omp_ctxp
= ctx
->outer_context
;
6206 delete_omp_context (ctx
);
6209 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
6210 gimplification of the body, as well as scanning the body for used
6211 variables. We need to do this scan now, because variable-sized
6212 decls will be decomposed during gimplification. */
6215 gimplify_omp_parallel (tree
*expr_p
, gimple_seq
*pre_p
)
6217 tree expr
= *expr_p
;
6219 gimple_seq body
= NULL
;
6220 struct gimplify_ctx gctx
;
6222 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr
), pre_p
,
6223 OMP_PARALLEL_COMBINED (expr
)
6224 ? ORT_COMBINED_PARALLEL
6227 push_gimplify_context (&gctx
);
6229 g
= gimplify_and_return_first (OMP_PARALLEL_BODY (expr
), &body
);
6230 if (gimple_code (g
) == GIMPLE_BIND
)
6231 pop_gimplify_context (g
);
6233 pop_gimplify_context (NULL
);
6235 gimplify_adjust_omp_clauses (&OMP_PARALLEL_CLAUSES (expr
));
6237 g
= gimple_build_omp_parallel (body
,
6238 OMP_PARALLEL_CLAUSES (expr
),
6239 NULL_TREE
, NULL_TREE
);
6240 if (OMP_PARALLEL_COMBINED (expr
))
6241 gimple_omp_set_subcode (g
, GF_OMP_PARALLEL_COMBINED
);
6242 gimplify_seq_add_stmt (pre_p
, g
);
6243 *expr_p
= NULL_TREE
;
6246 /* Gimplify the contents of an OMP_TASK statement. This involves
6247 gimplification of the body, as well as scanning the body for used
6248 variables. We need to do this scan now, because variable-sized
6249 decls will be decomposed during gimplification. */
6252 gimplify_omp_task (tree
*expr_p
, gimple_seq
*pre_p
)
6254 tree expr
= *expr_p
;
6256 gimple_seq body
= NULL
;
6257 struct gimplify_ctx gctx
;
6259 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr
), pre_p
,
6260 find_omp_clause (OMP_TASK_CLAUSES (expr
),
6262 ? ORT_UNTIED_TASK
: ORT_TASK
);
6264 push_gimplify_context (&gctx
);
6266 g
= gimplify_and_return_first (OMP_TASK_BODY (expr
), &body
);
6267 if (gimple_code (g
) == GIMPLE_BIND
)
6268 pop_gimplify_context (g
);
6270 pop_gimplify_context (NULL
);
6272 gimplify_adjust_omp_clauses (&OMP_TASK_CLAUSES (expr
));
6274 g
= gimple_build_omp_task (body
,
6275 OMP_TASK_CLAUSES (expr
),
6276 NULL_TREE
, NULL_TREE
,
6277 NULL_TREE
, NULL_TREE
, NULL_TREE
);
6278 gimplify_seq_add_stmt (pre_p
, g
);
6279 *expr_p
= NULL_TREE
;
6282 /* Gimplify the gross structure of an OMP_FOR statement. */
6284 static enum gimplify_status
6285 gimplify_omp_for (tree
*expr_p
, gimple_seq
*pre_p
)
6287 tree for_stmt
, decl
, var
, t
;
6288 enum gimplify_status ret
= GS_ALL_DONE
;
6289 enum gimplify_status tret
;
6291 gimple_seq for_body
, for_pre_body
;
6296 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt
), pre_p
,
6299 /* Handle OMP_FOR_INIT. */
6300 for_pre_body
= NULL
;
6301 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt
), &for_pre_body
);
6302 OMP_FOR_PRE_BODY (for_stmt
) = NULL_TREE
;
6304 for_body
= gimple_seq_alloc ();
6305 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
6306 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt
)));
6307 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
6308 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt
)));
6309 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
6311 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
6312 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
6313 decl
= TREE_OPERAND (t
, 0);
6314 gcc_assert (DECL_P (decl
));
6315 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl
))
6316 || POINTER_TYPE_P (TREE_TYPE (decl
)));
6318 /* Make sure the iteration variable is private. */
6319 if (omp_is_private (gimplify_omp_ctxp
, decl
))
6320 omp_notice_variable (gimplify_omp_ctxp
, decl
, true);
6322 omp_add_variable (gimplify_omp_ctxp
, decl
, GOVD_PRIVATE
| GOVD_SEEN
);
6324 /* If DECL is not a gimple register, create a temporary variable to act
6325 as an iteration counter. This is valid, since DECL cannot be
6326 modified in the body of the loop. */
6327 if (!is_gimple_reg (decl
))
6329 var
= create_tmp_var (TREE_TYPE (decl
), get_name (decl
));
6330 TREE_OPERAND (t
, 0) = var
;
6332 gimplify_seq_add_stmt (&for_body
, gimple_build_assign (decl
, var
));
6334 omp_add_variable (gimplify_omp_ctxp
, var
, GOVD_PRIVATE
| GOVD_SEEN
);
6339 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
6340 is_gimple_val
, fb_rvalue
);
6341 ret
= MIN (ret
, tret
);
6342 if (ret
== GS_ERROR
)
6345 /* Handle OMP_FOR_COND. */
6346 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
6347 gcc_assert (COMPARISON_CLASS_P (t
));
6348 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
6350 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
6351 is_gimple_val
, fb_rvalue
);
6352 ret
= MIN (ret
, tret
);
6354 /* Handle OMP_FOR_INCR. */
6355 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
6356 switch (TREE_CODE (t
))
6358 case PREINCREMENT_EXPR
:
6359 case POSTINCREMENT_EXPR
:
6360 t
= build_int_cst (TREE_TYPE (decl
), 1);
6361 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
6362 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
6363 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
6366 case PREDECREMENT_EXPR
:
6367 case POSTDECREMENT_EXPR
:
6368 t
= build_int_cst (TREE_TYPE (decl
), -1);
6369 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
6370 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
6371 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
6375 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
6376 TREE_OPERAND (t
, 0) = var
;
6378 t
= TREE_OPERAND (t
, 1);
6379 switch (TREE_CODE (t
))
6382 if (TREE_OPERAND (t
, 1) == decl
)
6384 TREE_OPERAND (t
, 1) = TREE_OPERAND (t
, 0);
6385 TREE_OPERAND (t
, 0) = var
;
6391 case POINTER_PLUS_EXPR
:
6392 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
6393 TREE_OPERAND (t
, 0) = var
;
6399 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
6400 is_gimple_val
, fb_rvalue
);
6401 ret
= MIN (ret
, tret
);
6408 if (var
!= decl
|| TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) > 1)
6411 for (c
= OMP_FOR_CLAUSES (for_stmt
); c
; c
= OMP_CLAUSE_CHAIN (c
))
6412 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
6413 && OMP_CLAUSE_DECL (c
) == decl
6414 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
) == NULL
)
6416 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
6417 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
6418 gcc_assert (TREE_OPERAND (t
, 0) == var
);
6419 t
= TREE_OPERAND (t
, 1);
6420 gcc_assert (TREE_CODE (t
) == PLUS_EXPR
6421 || TREE_CODE (t
) == MINUS_EXPR
6422 || TREE_CODE (t
) == POINTER_PLUS_EXPR
);
6423 gcc_assert (TREE_OPERAND (t
, 0) == var
);
6424 t
= build2 (TREE_CODE (t
), TREE_TYPE (decl
), decl
,
6425 TREE_OPERAND (t
, 1));
6426 gimplify_assign (decl
, t
,
6427 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
));
6432 gimplify_and_add (OMP_FOR_BODY (for_stmt
), &for_body
);
6434 gimplify_adjust_omp_clauses (&OMP_FOR_CLAUSES (for_stmt
));
6436 gfor
= gimple_build_omp_for (for_body
, OMP_FOR_CLAUSES (for_stmt
),
6437 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)),
6440 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
6442 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
6443 gimple_omp_for_set_index (gfor
, i
, TREE_OPERAND (t
, 0));
6444 gimple_omp_for_set_initial (gfor
, i
, TREE_OPERAND (t
, 1));
6445 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
6446 gimple_omp_for_set_cond (gfor
, i
, TREE_CODE (t
));
6447 gimple_omp_for_set_final (gfor
, i
, TREE_OPERAND (t
, 1));
6448 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
6449 gimple_omp_for_set_incr (gfor
, i
, TREE_OPERAND (t
, 1));
6452 gimplify_seq_add_stmt (pre_p
, gfor
);
6453 return ret
== GS_ALL_DONE
? GS_ALL_DONE
: GS_ERROR
;
6456 /* Gimplify the gross structure of other OpenMP worksharing constructs.
6457 In particular, OMP_SECTIONS and OMP_SINGLE. */
6460 gimplify_omp_workshare (tree
*expr_p
, gimple_seq
*pre_p
)
6462 tree expr
= *expr_p
;
6464 gimple_seq body
= NULL
;
6466 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr
), pre_p
, ORT_WORKSHARE
);
6467 gimplify_and_add (OMP_BODY (expr
), &body
);
6468 gimplify_adjust_omp_clauses (&OMP_CLAUSES (expr
));
6470 if (TREE_CODE (expr
) == OMP_SECTIONS
)
6471 stmt
= gimple_build_omp_sections (body
, OMP_CLAUSES (expr
));
6472 else if (TREE_CODE (expr
) == OMP_SINGLE
)
6473 stmt
= gimple_build_omp_single (body
, OMP_CLAUSES (expr
));
6477 gimplify_seq_add_stmt (pre_p
, stmt
);
6480 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
6481 stabilized the lhs of the atomic operation as *ADDR. Return true if
6482 EXPR is this stabilized form. */
6485 goa_lhs_expr_p (tree expr
, tree addr
)
6487 /* Also include casts to other type variants. The C front end is fond
6488 of adding these for e.g. volatile variables. This is like
6489 STRIP_TYPE_NOPS but includes the main variant lookup. */
6490 STRIP_USELESS_TYPE_CONVERSION (expr
);
6492 if (TREE_CODE (expr
) == INDIRECT_REF
)
6494 expr
= TREE_OPERAND (expr
, 0);
6496 && (CONVERT_EXPR_P (expr
)
6497 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
6498 && TREE_CODE (expr
) == TREE_CODE (addr
)
6499 && types_compatible_p (TREE_TYPE (expr
), TREE_TYPE (addr
)))
6501 expr
= TREE_OPERAND (expr
, 0);
6502 addr
= TREE_OPERAND (addr
, 0);
6506 return (TREE_CODE (addr
) == ADDR_EXPR
6507 && TREE_CODE (expr
) == ADDR_EXPR
6508 && TREE_OPERAND (addr
, 0) == TREE_OPERAND (expr
, 0));
6510 if (TREE_CODE (addr
) == ADDR_EXPR
&& expr
== TREE_OPERAND (addr
, 0))
6515 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
6516 expression does not involve the lhs, evaluate it into a temporary.
6517 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
6518 or -1 if an error was encountered. */
6521 goa_stabilize_expr (tree
*expr_p
, gimple_seq
*pre_p
, tree lhs_addr
,
6524 tree expr
= *expr_p
;
6527 if (goa_lhs_expr_p (expr
, lhs_addr
))
6532 if (is_gimple_val (expr
))
6536 switch (TREE_CODE_CLASS (TREE_CODE (expr
)))
6539 case tcc_comparison
:
6540 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
, lhs_addr
,
6543 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
, lhs_addr
,
6546 case tcc_expression
:
6547 switch (TREE_CODE (expr
))
6549 case TRUTH_ANDIF_EXPR
:
6550 case TRUTH_ORIF_EXPR
:
6551 case TRUTH_AND_EXPR
:
6553 case TRUTH_XOR_EXPR
:
6554 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
,
6556 case TRUTH_NOT_EXPR
:
6557 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
,
6561 /* Break out any preevaluations from cp_build_modify_expr. */
6562 for (; TREE_CODE (expr
) == COMPOUND_EXPR
;
6563 expr
= TREE_OPERAND (expr
, 1))
6564 gimplify_stmt (&TREE_OPERAND (expr
, 0), pre_p
);
6566 return goa_stabilize_expr (expr_p
, pre_p
, lhs_addr
, lhs_var
);
6577 enum gimplify_status gs
;
6578 gs
= gimplify_expr (expr_p
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
6579 if (gs
!= GS_ALL_DONE
)
6586 /* Gimplify an OMP_ATOMIC statement. */
6588 static enum gimplify_status
6589 gimplify_omp_atomic (tree
*expr_p
, gimple_seq
*pre_p
)
6591 tree addr
= TREE_OPERAND (*expr_p
, 0);
6592 tree rhs
= TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
6593 ? NULL
: TREE_OPERAND (*expr_p
, 1);
6594 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr
)));
6596 gimple loadstmt
, storestmt
;
6598 tmp_load
= create_tmp_reg (type
, NULL
);
6599 if (rhs
&& goa_stabilize_expr (&rhs
, pre_p
, addr
, tmp_load
) < 0)
6602 if (gimplify_expr (&addr
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
6606 loadstmt
= gimple_build_omp_atomic_load (tmp_load
, addr
);
6607 gimplify_seq_add_stmt (pre_p
, loadstmt
);
6608 if (rhs
&& gimplify_expr (&rhs
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
6612 if (TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
)
6614 storestmt
= gimple_build_omp_atomic_store (rhs
);
6615 gimplify_seq_add_stmt (pre_p
, storestmt
);
6616 switch (TREE_CODE (*expr_p
))
6618 case OMP_ATOMIC_READ
:
6619 case OMP_ATOMIC_CAPTURE_OLD
:
6621 gimple_omp_atomic_set_need_value (loadstmt
);
6623 case OMP_ATOMIC_CAPTURE_NEW
:
6625 gimple_omp_atomic_set_need_value (storestmt
);
6635 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
6636 body, and adding some EH bits. */
6638 static enum gimplify_status
6639 gimplify_transaction (tree
*expr_p
, gimple_seq
*pre_p
)
6641 tree expr
= *expr_p
, temp
, tbody
= TRANSACTION_EXPR_BODY (expr
);
6643 gimple_seq body
= NULL
;
6644 struct gimplify_ctx gctx
;
6647 /* Wrap the transaction body in a BIND_EXPR so we have a context
6648 where to put decls for OpenMP. */
6649 if (TREE_CODE (tbody
) != BIND_EXPR
)
6651 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, tbody
, NULL
);
6652 TREE_SIDE_EFFECTS (bind
) = 1;
6653 SET_EXPR_LOCATION (bind
, EXPR_LOCATION (tbody
));
6654 TRANSACTION_EXPR_BODY (expr
) = bind
;
6657 push_gimplify_context (&gctx
);
6658 temp
= voidify_wrapper_expr (*expr_p
, NULL
);
6660 g
= gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr
), &body
);
6661 pop_gimplify_context (g
);
6663 g
= gimple_build_transaction (body
, NULL
);
6664 if (TRANSACTION_EXPR_OUTER (expr
))
6665 subcode
= GTMA_IS_OUTER
;
6666 else if (TRANSACTION_EXPR_RELAXED (expr
))
6667 subcode
= GTMA_IS_RELAXED
;
6668 gimple_transaction_set_subcode (g
, subcode
);
6670 gimplify_seq_add_stmt (pre_p
, g
);
6678 *expr_p
= NULL_TREE
;
6682 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
6683 expression produces a value to be used as an operand inside a GIMPLE
6684 statement, the value will be stored back in *EXPR_P. This value will
6685 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
6686 an SSA_NAME. The corresponding sequence of GIMPLE statements is
6687 emitted in PRE_P and POST_P.
6689 Additionally, this process may overwrite parts of the input
6690 expression during gimplification. Ideally, it should be
6691 possible to do non-destructive gimplification.
6693 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
6694 the expression needs to evaluate to a value to be used as
6695 an operand in a GIMPLE statement, this value will be stored in
6696 *EXPR_P on exit. This happens when the caller specifies one
6697 of fb_lvalue or fb_rvalue fallback flags.
6699 PRE_P will contain the sequence of GIMPLE statements corresponding
6700 to the evaluation of EXPR and all the side-effects that must
6701 be executed before the main expression. On exit, the last
6702 statement of PRE_P is the core statement being gimplified. For
6703 instance, when gimplifying 'if (++a)' the last statement in
6704 PRE_P will be 'if (t.1)' where t.1 is the result of
6705 pre-incrementing 'a'.
6707 POST_P will contain the sequence of GIMPLE statements corresponding
6708 to the evaluation of all the side-effects that must be executed
6709 after the main expression. If this is NULL, the post
6710 side-effects are stored at the end of PRE_P.
6712 The reason why the output is split in two is to handle post
6713 side-effects explicitly. In some cases, an expression may have
6714 inner and outer post side-effects which need to be emitted in
6715 an order different from the one given by the recursive
6716 traversal. For instance, for the expression (*p--)++ the post
6717 side-effects of '--' must actually occur *after* the post
6718 side-effects of '++'. However, gimplification will first visit
6719 the inner expression, so if a separate POST sequence was not
6720 used, the resulting sequence would be:
6727 However, the post-decrement operation in line #2 must not be
6728 evaluated until after the store to *p at line #4, so the
6729 correct sequence should be:
6736 So, by specifying a separate post queue, it is possible
6737 to emit the post side-effects in the correct order.
6738 If POST_P is NULL, an internal queue will be used. Before
6739 returning to the caller, the sequence POST_P is appended to
6740 the main output sequence PRE_P.
6742 GIMPLE_TEST_F points to a function that takes a tree T and
6743 returns nonzero if T is in the GIMPLE form requested by the
6744 caller. The GIMPLE predicates are in gimple.c.
6746 FALLBACK tells the function what sort of a temporary we want if
6747 gimplification cannot produce an expression that complies with
6750 fb_none means that no temporary should be generated
6751 fb_rvalue means that an rvalue is OK to generate
6752 fb_lvalue means that an lvalue is OK to generate
6753 fb_either means that either is OK, but an lvalue is preferable.
6754 fb_mayfail means that gimplification may fail (in which case
6755 GS_ERROR will be returned)
6757 The return value is either GS_ERROR or GS_ALL_DONE, since this
6758 function iterates until EXPR is completely gimplified or an error
6761 enum gimplify_status
6762 gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
6763 bool (*gimple_test_f
) (tree
), fallback_t fallback
)
6766 gimple_seq internal_pre
= NULL
;
6767 gimple_seq internal_post
= NULL
;
6770 location_t saved_location
;
6771 enum gimplify_status ret
;
6772 gimple_stmt_iterator pre_last_gsi
, post_last_gsi
;
6774 save_expr
= *expr_p
;
6775 if (save_expr
== NULL_TREE
)
6778 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
6779 is_statement
= gimple_test_f
== is_gimple_stmt
;
6783 /* Consistency checks. */
6784 if (gimple_test_f
== is_gimple_reg
)
6785 gcc_assert (fallback
& (fb_rvalue
| fb_lvalue
));
6786 else if (gimple_test_f
== is_gimple_val
6787 || gimple_test_f
== is_gimple_call_addr
6788 || gimple_test_f
== is_gimple_condexpr
6789 || gimple_test_f
== is_gimple_mem_rhs
6790 || gimple_test_f
== is_gimple_mem_rhs_or_call
6791 || gimple_test_f
== is_gimple_reg_rhs
6792 || gimple_test_f
== is_gimple_reg_rhs_or_call
6793 || gimple_test_f
== is_gimple_asm_val
6794 || gimple_test_f
== is_gimple_mem_ref_addr
)
6795 gcc_assert (fallback
& fb_rvalue
);
6796 else if (gimple_test_f
== is_gimple_min_lval
6797 || gimple_test_f
== is_gimple_lvalue
)
6798 gcc_assert (fallback
& fb_lvalue
);
6799 else if (gimple_test_f
== is_gimple_addressable
)
6800 gcc_assert (fallback
& fb_either
);
6801 else if (gimple_test_f
== is_gimple_stmt
)
6802 gcc_assert (fallback
== fb_none
);
6805 /* We should have recognized the GIMPLE_TEST_F predicate to
6806 know what kind of fallback to use in case a temporary is
6807 needed to hold the value or address of *EXPR_P. */
6811 /* We used to check the predicate here and return immediately if it
6812 succeeds. This is wrong; the design is for gimplification to be
6813 idempotent, and for the predicates to only test for valid forms, not
6814 whether they are fully simplified. */
6816 pre_p
= &internal_pre
;
6819 post_p
= &internal_post
;
6821 /* Remember the last statements added to PRE_P and POST_P. Every
6822 new statement added by the gimplification helpers needs to be
6823 annotated with location information. To centralize the
6824 responsibility, we remember the last statement that had been
6825 added to both queues before gimplifying *EXPR_P. If
6826 gimplification produces new statements in PRE_P and POST_P, those
6827 statements will be annotated with the same location information
6829 pre_last_gsi
= gsi_last (*pre_p
);
6830 post_last_gsi
= gsi_last (*post_p
);
6832 saved_location
= input_location
;
6833 if (save_expr
!= error_mark_node
6834 && EXPR_HAS_LOCATION (*expr_p
))
6835 input_location
= EXPR_LOCATION (*expr_p
);
6837 /* Loop over the specific gimplifiers until the toplevel node
6838 remains the same. */
6841 /* Strip away as many useless type conversions as possible
6843 STRIP_USELESS_TYPE_CONVERSION (*expr_p
);
6845 /* Remember the expr. */
6846 save_expr
= *expr_p
;
6848 /* Die, die, die, my darling. */
6849 if (save_expr
== error_mark_node
6850 || (TREE_TYPE (save_expr
)
6851 && TREE_TYPE (save_expr
) == error_mark_node
))
6857 /* Do any language-specific gimplification. */
6858 ret
= ((enum gimplify_status
)
6859 lang_hooks
.gimplify_expr (expr_p
, pre_p
, post_p
));
6862 if (*expr_p
== NULL_TREE
)
6864 if (*expr_p
!= save_expr
)
6867 else if (ret
!= GS_UNHANDLED
)
6870 /* Make sure that all the cases set 'ret' appropriately. */
6872 switch (TREE_CODE (*expr_p
))
6874 /* First deal with the special cases. */
6876 case POSTINCREMENT_EXPR
:
6877 case POSTDECREMENT_EXPR
:
6878 case PREINCREMENT_EXPR
:
6879 case PREDECREMENT_EXPR
:
6880 ret
= gimplify_self_mod_expr (expr_p
, pre_p
, post_p
,
6881 fallback
!= fb_none
);
6885 case ARRAY_RANGE_REF
:
6889 case VIEW_CONVERT_EXPR
:
6890 ret
= gimplify_compound_lval (expr_p
, pre_p
, post_p
,
6891 fallback
? fallback
: fb_rvalue
);
6895 ret
= gimplify_cond_expr (expr_p
, pre_p
, fallback
);
6897 /* C99 code may assign to an array in a structure value of a
6898 conditional expression, and this has undefined behavior
6899 only on execution, so create a temporary if an lvalue is
6901 if (fallback
== fb_lvalue
)
6903 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
);
6904 mark_addressable (*expr_p
);
6910 ret
= gimplify_call_expr (expr_p
, pre_p
, fallback
!= fb_none
);
6912 /* C99 code may assign to an array in a structure returned
6913 from a function, and this has undefined behavior only on
6914 execution, so create a temporary if an lvalue is
6916 if (fallback
== fb_lvalue
)
6918 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
);
6919 mark_addressable (*expr_p
);
6928 ret
= gimplify_compound_expr (expr_p
, pre_p
, fallback
!= fb_none
);
6931 case COMPOUND_LITERAL_EXPR
:
6932 ret
= gimplify_compound_literal_expr (expr_p
, pre_p
);
6937 ret
= gimplify_modify_expr (expr_p
, pre_p
, post_p
,
6938 fallback
!= fb_none
);
6941 case TRUTH_ANDIF_EXPR
:
6942 case TRUTH_ORIF_EXPR
:
6944 /* Preserve the original type of the expression and the
6945 source location of the outer expression. */
6946 tree org_type
= TREE_TYPE (*expr_p
);
6947 *expr_p
= gimple_boolify (*expr_p
);
6948 *expr_p
= build3_loc (input_location
, COND_EXPR
,
6952 org_type
, boolean_true_node
),
6955 org_type
, boolean_false_node
));
6960 case TRUTH_NOT_EXPR
:
6962 tree type
= TREE_TYPE (*expr_p
);
6963 /* The parsers are careful to generate TRUTH_NOT_EXPR
6964 only with operands that are always zero or one.
6965 We do not fold here but handle the only interesting case
6966 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
6967 *expr_p
= gimple_boolify (*expr_p
);
6968 if (TYPE_PRECISION (TREE_TYPE (*expr_p
)) == 1)
6969 *expr_p
= build1_loc (input_location
, BIT_NOT_EXPR
,
6970 TREE_TYPE (*expr_p
),
6971 TREE_OPERAND (*expr_p
, 0));
6973 *expr_p
= build2_loc (input_location
, BIT_XOR_EXPR
,
6974 TREE_TYPE (*expr_p
),
6975 TREE_OPERAND (*expr_p
, 0),
6976 build_int_cst (TREE_TYPE (*expr_p
), 1));
6977 if (!useless_type_conversion_p (type
, TREE_TYPE (*expr_p
)))
6978 *expr_p
= fold_convert_loc (input_location
, type
, *expr_p
);
6984 ret
= gimplify_addr_expr (expr_p
, pre_p
, post_p
);
6988 ret
= gimplify_va_arg_expr (expr_p
, pre_p
, post_p
);
6992 if (IS_EMPTY_STMT (*expr_p
))
6998 if (VOID_TYPE_P (TREE_TYPE (*expr_p
))
6999 || fallback
== fb_none
)
7001 /* Just strip a conversion to void (or in void context) and
7003 *expr_p
= TREE_OPERAND (*expr_p
, 0);
7008 ret
= gimplify_conversion (expr_p
);
7009 if (ret
== GS_ERROR
)
7011 if (*expr_p
!= save_expr
)
7015 case FIX_TRUNC_EXPR
:
7016 /* unary_expr: ... | '(' cast ')' val | ... */
7017 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
7018 is_gimple_val
, fb_rvalue
);
7019 recalculate_side_effects (*expr_p
);
7024 bool volatilep
= TREE_THIS_VOLATILE (*expr_p
);
7025 bool notrap
= TREE_THIS_NOTRAP (*expr_p
);
7026 tree saved_ptr_type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 0));
7028 *expr_p
= fold_indirect_ref_loc (input_location
, *expr_p
);
7029 if (*expr_p
!= save_expr
)
7035 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
7036 is_gimple_reg
, fb_rvalue
);
7037 if (ret
== GS_ERROR
)
7040 recalculate_side_effects (*expr_p
);
7041 *expr_p
= fold_build2_loc (input_location
, MEM_REF
,
7042 TREE_TYPE (*expr_p
),
7043 TREE_OPERAND (*expr_p
, 0),
7044 build_int_cst (saved_ptr_type
, 0));
7045 TREE_THIS_VOLATILE (*expr_p
) = volatilep
;
7046 TREE_THIS_NOTRAP (*expr_p
) = notrap
;
7051 /* We arrive here through the various re-gimplifcation paths. */
7053 /* First try re-folding the whole thing. */
7054 tmp
= fold_binary (MEM_REF
, TREE_TYPE (*expr_p
),
7055 TREE_OPERAND (*expr_p
, 0),
7056 TREE_OPERAND (*expr_p
, 1));
7060 recalculate_side_effects (*expr_p
);
7064 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
7065 is_gimple_mem_ref_addr
, fb_rvalue
);
7066 if (ret
== GS_ERROR
)
7068 recalculate_side_effects (*expr_p
);
7072 /* Constants need not be gimplified. */
7083 /* If we require an lvalue, such as for ADDR_EXPR, retain the
7084 CONST_DECL node. Otherwise the decl is replaceable by its
7086 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
7087 if (fallback
& fb_lvalue
)
7091 *expr_p
= DECL_INITIAL (*expr_p
);
7097 ret
= gimplify_decl_expr (expr_p
, pre_p
);
7101 ret
= gimplify_bind_expr (expr_p
, pre_p
);
7105 ret
= gimplify_loop_expr (expr_p
, pre_p
);
7109 ret
= gimplify_switch_expr (expr_p
, pre_p
);
7113 ret
= gimplify_exit_expr (expr_p
);
7117 /* If the target is not LABEL, then it is a computed jump
7118 and the target needs to be gimplified. */
7119 if (TREE_CODE (GOTO_DESTINATION (*expr_p
)) != LABEL_DECL
)
7121 ret
= gimplify_expr (&GOTO_DESTINATION (*expr_p
), pre_p
,
7122 NULL
, is_gimple_val
, fb_rvalue
);
7123 if (ret
== GS_ERROR
)
7126 gimplify_seq_add_stmt (pre_p
,
7127 gimple_build_goto (GOTO_DESTINATION (*expr_p
)));
7132 gimplify_seq_add_stmt (pre_p
,
7133 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p
),
7134 PREDICT_EXPR_OUTCOME (*expr_p
)));
7140 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p
))
7141 == current_function_decl
);
7142 gimplify_seq_add_stmt (pre_p
,
7143 gimple_build_label (LABEL_EXPR_LABEL (*expr_p
)));
7146 case CASE_LABEL_EXPR
:
7147 ret
= gimplify_case_label_expr (expr_p
, pre_p
);
7151 ret
= gimplify_return_expr (*expr_p
, pre_p
);
7155 /* Don't reduce this in place; let gimplify_init_constructor work its
7156 magic. Buf if we're just elaborating this for side effects, just
7157 gimplify any element that has side-effects. */
7158 if (fallback
== fb_none
)
7160 unsigned HOST_WIDE_INT ix
;
7162 tree temp
= NULL_TREE
;
7163 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p
), ix
, val
)
7164 if (TREE_SIDE_EFFECTS (val
))
7165 append_to_statement_list (val
, &temp
);
7168 ret
= temp
? GS_OK
: GS_ALL_DONE
;
7170 /* C99 code may assign to an array in a constructed
7171 structure or union, and this has undefined behavior only
7172 on execution, so create a temporary if an lvalue is
7174 else if (fallback
== fb_lvalue
)
7176 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
);
7177 mark_addressable (*expr_p
);
7184 /* The following are special cases that are not handled by the
7185 original GIMPLE grammar. */
7187 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
7190 ret
= gimplify_save_expr (expr_p
, pre_p
, post_p
);
7195 enum gimplify_status r0
, r1
, r2
;
7197 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7198 post_p
, is_gimple_lvalue
, fb_either
);
7199 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
7200 post_p
, is_gimple_val
, fb_rvalue
);
7201 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
7202 post_p
, is_gimple_val
, fb_rvalue
);
7203 recalculate_side_effects (*expr_p
);
7205 ret
= MIN (r0
, MIN (r1
, r2
));
7209 case TARGET_MEM_REF
:
7211 enum gimplify_status r0
= GS_ALL_DONE
, r1
= GS_ALL_DONE
;
7213 if (TMR_BASE (*expr_p
))
7214 r0
= gimplify_expr (&TMR_BASE (*expr_p
), pre_p
,
7215 post_p
, is_gimple_mem_ref_addr
, fb_either
);
7216 if (TMR_INDEX (*expr_p
))
7217 r1
= gimplify_expr (&TMR_INDEX (*expr_p
), pre_p
,
7218 post_p
, is_gimple_val
, fb_rvalue
);
7219 if (TMR_INDEX2 (*expr_p
))
7220 r1
= gimplify_expr (&TMR_INDEX2 (*expr_p
), pre_p
,
7221 post_p
, is_gimple_val
, fb_rvalue
);
7222 /* TMR_STEP and TMR_OFFSET are always integer constants. */
7227 case NON_LVALUE_EXPR
:
7228 /* This should have been stripped above. */
7232 ret
= gimplify_asm_expr (expr_p
, pre_p
, post_p
);
7235 case TRY_FINALLY_EXPR
:
7236 case TRY_CATCH_EXPR
:
7238 gimple_seq eval
, cleanup
;
7241 eval
= cleanup
= NULL
;
7242 gimplify_and_add (TREE_OPERAND (*expr_p
, 0), &eval
);
7243 gimplify_and_add (TREE_OPERAND (*expr_p
, 1), &cleanup
);
7244 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
7245 if (gimple_seq_empty_p (cleanup
))
7247 gimple_seq_add_seq (pre_p
, eval
);
7251 try_
= gimple_build_try (eval
, cleanup
,
7252 TREE_CODE (*expr_p
) == TRY_FINALLY_EXPR
7253 ? GIMPLE_TRY_FINALLY
7254 : GIMPLE_TRY_CATCH
);
7255 if (TREE_CODE (*expr_p
) == TRY_CATCH_EXPR
)
7256 gimple_try_set_catch_is_cleanup (try_
,
7257 TRY_CATCH_IS_CLEANUP (*expr_p
));
7258 gimplify_seq_add_stmt (pre_p
, try_
);
7263 case CLEANUP_POINT_EXPR
:
7264 ret
= gimplify_cleanup_point_expr (expr_p
, pre_p
);
7268 ret
= gimplify_target_expr (expr_p
, pre_p
, post_p
);
7274 gimple_seq handler
= NULL
;
7275 gimplify_and_add (CATCH_BODY (*expr_p
), &handler
);
7276 c
= gimple_build_catch (CATCH_TYPES (*expr_p
), handler
);
7277 gimplify_seq_add_stmt (pre_p
, c
);
7282 case EH_FILTER_EXPR
:
7285 gimple_seq failure
= NULL
;
7287 gimplify_and_add (EH_FILTER_FAILURE (*expr_p
), &failure
);
7288 ehf
= gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p
), failure
);
7289 gimple_set_no_warning (ehf
, TREE_NO_WARNING (*expr_p
));
7290 gimplify_seq_add_stmt (pre_p
, ehf
);
7297 enum gimplify_status r0
, r1
;
7298 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
,
7299 post_p
, is_gimple_val
, fb_rvalue
);
7300 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
,
7301 post_p
, is_gimple_val
, fb_rvalue
);
7302 TREE_SIDE_EFFECTS (*expr_p
) = 0;
7308 /* We get here when taking the address of a label. We mark
7309 the label as "forced"; meaning it can never be removed and
7310 it is a potential target for any computed goto. */
7311 FORCED_LABEL (*expr_p
) = 1;
7315 case STATEMENT_LIST
:
7316 ret
= gimplify_statement_list (expr_p
, pre_p
);
7319 case WITH_SIZE_EXPR
:
7321 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7322 post_p
== &internal_post
? NULL
: post_p
,
7323 gimple_test_f
, fallback
);
7324 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
7325 is_gimple_val
, fb_rvalue
);
7332 ret
= gimplify_var_or_parm_decl (expr_p
);
7336 /* When within an OpenMP context, notice uses of variables. */
7337 if (gimplify_omp_ctxp
)
7338 omp_notice_variable (gimplify_omp_ctxp
, *expr_p
, true);
7343 /* Allow callbacks into the gimplifier during optimization. */
7348 gimplify_omp_parallel (expr_p
, pre_p
);
7353 gimplify_omp_task (expr_p
, pre_p
);
7358 ret
= gimplify_omp_for (expr_p
, pre_p
);
7363 gimplify_omp_workshare (expr_p
, pre_p
);
7372 gimple_seq body
= NULL
;
7375 gimplify_and_add (OMP_BODY (*expr_p
), &body
);
7376 switch (TREE_CODE (*expr_p
))
7379 g
= gimple_build_omp_section (body
);
7382 g
= gimple_build_omp_master (body
);
7385 g
= gimple_build_omp_ordered (body
);
7388 g
= gimple_build_omp_critical (body
,
7389 OMP_CRITICAL_NAME (*expr_p
));
7394 gimplify_seq_add_stmt (pre_p
, g
);
7400 case OMP_ATOMIC_READ
:
7401 case OMP_ATOMIC_CAPTURE_OLD
:
7402 case OMP_ATOMIC_CAPTURE_NEW
:
7403 ret
= gimplify_omp_atomic (expr_p
, pre_p
);
7406 case TRANSACTION_EXPR
:
7407 ret
= gimplify_transaction (expr_p
, pre_p
);
7410 case TRUTH_AND_EXPR
:
7412 case TRUTH_XOR_EXPR
:
7414 tree orig_type
= TREE_TYPE (*expr_p
);
7415 tree new_type
, xop0
, xop1
;
7416 *expr_p
= gimple_boolify (*expr_p
);
7417 new_type
= TREE_TYPE (*expr_p
);
7418 if (!useless_type_conversion_p (orig_type
, new_type
))
7420 *expr_p
= fold_convert_loc (input_location
, orig_type
, *expr_p
);
7425 /* Boolified binary truth expressions are semantically equivalent
7426 to bitwise binary expressions. Canonicalize them to the
7428 switch (TREE_CODE (*expr_p
))
7430 case TRUTH_AND_EXPR
:
7431 TREE_SET_CODE (*expr_p
, BIT_AND_EXPR
);
7434 TREE_SET_CODE (*expr_p
, BIT_IOR_EXPR
);
7436 case TRUTH_XOR_EXPR
:
7437 TREE_SET_CODE (*expr_p
, BIT_XOR_EXPR
);
7442 /* Now make sure that operands have compatible type to
7443 expression's new_type. */
7444 xop0
= TREE_OPERAND (*expr_p
, 0);
7445 xop1
= TREE_OPERAND (*expr_p
, 1);
7446 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop0
)))
7447 TREE_OPERAND (*expr_p
, 0) = fold_convert_loc (input_location
,
7450 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop1
)))
7451 TREE_OPERAND (*expr_p
, 1) = fold_convert_loc (input_location
,
7454 /* Continue classified as tcc_binary. */
7460 /* Classified as tcc_expression. */
7463 case POINTER_PLUS_EXPR
:
7465 enum gimplify_status r0
, r1
;
7466 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7467 post_p
, is_gimple_val
, fb_rvalue
);
7468 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
7469 post_p
, is_gimple_val
, fb_rvalue
);
7470 recalculate_side_effects (*expr_p
);
7472 /* Convert &X + CST to invariant &MEM[&X, CST]. Do this
7473 after gimplifying operands - this is similar to how
7474 it would be folding all gimplified stmts on creation
7475 to have them canonicalized, which is what we eventually
7476 should do anyway. */
7477 if (TREE_CODE (TREE_OPERAND (*expr_p
, 1)) == INTEGER_CST
7478 && is_gimple_min_invariant (TREE_OPERAND (*expr_p
, 0)))
7480 *expr_p
= build_fold_addr_expr_with_type_loc
7482 fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (*expr_p
)),
7483 TREE_OPERAND (*expr_p
, 0),
7484 fold_convert (ptr_type_node
,
7485 TREE_OPERAND (*expr_p
, 1))),
7486 TREE_TYPE (*expr_p
));
7487 ret
= MIN (ret
, GS_OK
);
7493 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p
)))
7495 case tcc_comparison
:
7496 /* Handle comparison of objects of non scalar mode aggregates
7497 with a call to memcmp. It would be nice to only have to do
7498 this for variable-sized objects, but then we'd have to allow
7499 the same nest of reference nodes we allow for MODIFY_EXPR and
7502 Compare scalar mode aggregates as scalar mode values. Using
7503 memcmp for them would be very inefficient at best, and is
7504 plain wrong if bitfields are involved. */
7506 tree type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 1));
7508 /* Vector comparisons need no boolification. */
7509 if (TREE_CODE (type
) == VECTOR_TYPE
)
7511 else if (!AGGREGATE_TYPE_P (type
))
7513 tree org_type
= TREE_TYPE (*expr_p
);
7514 *expr_p
= gimple_boolify (*expr_p
);
7515 if (!useless_type_conversion_p (org_type
,
7516 TREE_TYPE (*expr_p
)))
7518 *expr_p
= fold_convert_loc (input_location
,
7525 else if (TYPE_MODE (type
) != BLKmode
)
7526 ret
= gimplify_scalar_mode_aggregate_compare (expr_p
);
7528 ret
= gimplify_variable_sized_compare (expr_p
);
7533 /* If *EXPR_P does not need to be special-cased, handle it
7534 according to its class. */
7536 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7537 post_p
, is_gimple_val
, fb_rvalue
);
7543 enum gimplify_status r0
, r1
;
7545 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7546 post_p
, is_gimple_val
, fb_rvalue
);
7547 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
7548 post_p
, is_gimple_val
, fb_rvalue
);
7556 enum gimplify_status r0
, r1
, r2
;
7558 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
7559 post_p
, is_gimple_val
, fb_rvalue
);
7560 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
7561 post_p
, is_gimple_val
, fb_rvalue
);
7562 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
7563 post_p
, is_gimple_val
, fb_rvalue
);
7565 ret
= MIN (MIN (r0
, r1
), r2
);
7569 case tcc_declaration
:
7572 goto dont_recalculate
;
7578 recalculate_side_effects (*expr_p
);
7584 gcc_assert (*expr_p
|| ret
!= GS_OK
);
7586 while (ret
== GS_OK
);
7588 /* If we encountered an error_mark somewhere nested inside, either
7589 stub out the statement or propagate the error back out. */
7590 if (ret
== GS_ERROR
)
7597 /* This was only valid as a return value from the langhook, which
7598 we handled. Make sure it doesn't escape from any other context. */
7599 gcc_assert (ret
!= GS_UNHANDLED
);
7601 if (fallback
== fb_none
&& *expr_p
&& !is_gimple_stmt (*expr_p
))
7603 /* We aren't looking for a value, and we don't have a valid
7604 statement. If it doesn't have side-effects, throw it away. */
7605 if (!TREE_SIDE_EFFECTS (*expr_p
))
7607 else if (!TREE_THIS_VOLATILE (*expr_p
))
7609 /* This is probably a _REF that contains something nested that
7610 has side effects. Recurse through the operands to find it. */
7611 enum tree_code code
= TREE_CODE (*expr_p
);
7618 case VIEW_CONVERT_EXPR
:
7619 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
7620 gimple_test_f
, fallback
);
7624 case ARRAY_RANGE_REF
:
7625 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
7626 gimple_test_f
, fallback
);
7627 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
7628 gimple_test_f
, fallback
);
7632 /* Anything else with side-effects must be converted to
7633 a valid statement before we get here. */
7639 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p
))
7640 && TYPE_MODE (TREE_TYPE (*expr_p
)) != BLKmode
)
7642 /* Historically, the compiler has treated a bare reference
7643 to a non-BLKmode volatile lvalue as forcing a load. */
7644 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p
));
7646 /* Normally, we do not want to create a temporary for a
7647 TREE_ADDRESSABLE type because such a type should not be
7648 copied by bitwise-assignment. However, we make an
7649 exception here, as all we are doing here is ensuring that
7650 we read the bytes that make up the type. We use
7651 create_tmp_var_raw because create_tmp_var will abort when
7652 given a TREE_ADDRESSABLE type. */
7653 tree tmp
= create_tmp_var_raw (type
, "vol");
7654 gimple_add_tmp_var (tmp
);
7655 gimplify_assign (tmp
, *expr_p
, pre_p
);
7659 /* We can't do anything useful with a volatile reference to
7660 an incomplete type, so just throw it away. Likewise for
7661 a BLKmode type, since any implicit inner load should
7662 already have been turned into an explicit one by the
7663 gimplification process. */
7667 /* If we are gimplifying at the statement level, we're done. Tack
7668 everything together and return. */
7669 if (fallback
== fb_none
|| is_statement
)
7671 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
7672 it out for GC to reclaim it. */
7673 *expr_p
= NULL_TREE
;
7675 if (!gimple_seq_empty_p (internal_pre
)
7676 || !gimple_seq_empty_p (internal_post
))
7678 gimplify_seq_add_seq (&internal_pre
, internal_post
);
7679 gimplify_seq_add_seq (pre_p
, internal_pre
);
7682 /* The result of gimplifying *EXPR_P is going to be the last few
7683 statements in *PRE_P and *POST_P. Add location information
7684 to all the statements that were added by the gimplification
7686 if (!gimple_seq_empty_p (*pre_p
))
7687 annotate_all_with_location_after (*pre_p
, pre_last_gsi
, input_location
);
7689 if (!gimple_seq_empty_p (*post_p
))
7690 annotate_all_with_location_after (*post_p
, post_last_gsi
,
7696 #ifdef ENABLE_GIMPLE_CHECKING
7699 enum tree_code code
= TREE_CODE (*expr_p
);
7700 /* These expressions should already be in gimple IR form. */
7701 gcc_assert (code
!= MODIFY_EXPR
7703 && code
!= BIND_EXPR
7704 && code
!= CATCH_EXPR
7705 && (code
!= COND_EXPR
|| gimplify_ctxp
->allow_rhs_cond_expr
)
7706 && code
!= EH_FILTER_EXPR
7707 && code
!= GOTO_EXPR
7708 && code
!= LABEL_EXPR
7709 && code
!= LOOP_EXPR
7710 && code
!= SWITCH_EXPR
7711 && code
!= TRY_FINALLY_EXPR
7712 && code
!= OMP_CRITICAL
7714 && code
!= OMP_MASTER
7715 && code
!= OMP_ORDERED
7716 && code
!= OMP_PARALLEL
7717 && code
!= OMP_SECTIONS
7718 && code
!= OMP_SECTION
7719 && code
!= OMP_SINGLE
);
7723 /* Otherwise we're gimplifying a subexpression, so the resulting
7724 value is interesting. If it's a valid operand that matches
7725 GIMPLE_TEST_F, we're done. Unless we are handling some
7726 post-effects internally; if that's the case, we need to copy into
7727 a temporary before adding the post-effects to POST_P. */
7728 if (gimple_seq_empty_p (internal_post
) && (*gimple_test_f
) (*expr_p
))
7731 /* Otherwise, we need to create a new temporary for the gimplified
7734 /* We can't return an lvalue if we have an internal postqueue. The
7735 object the lvalue refers to would (probably) be modified by the
7736 postqueue; we need to copy the value out first, which means an
7738 if ((fallback
& fb_lvalue
)
7739 && gimple_seq_empty_p (internal_post
)
7740 && is_gimple_addressable (*expr_p
))
7742 /* An lvalue will do. Take the address of the expression, store it
7743 in a temporary, and replace the expression with an INDIRECT_REF of
7745 tmp
= build_fold_addr_expr_loc (input_location
, *expr_p
);
7746 gimplify_expr (&tmp
, pre_p
, post_p
, is_gimple_reg
, fb_rvalue
);
7747 *expr_p
= build_simple_mem_ref (tmp
);
7749 else if ((fallback
& fb_rvalue
) && is_gimple_reg_rhs_or_call (*expr_p
))
7751 /* An rvalue will do. Assign the gimplified expression into a
7752 new temporary TMP and replace the original expression with
7753 TMP. First, make sure that the expression has a type so that
7754 it can be assigned into a temporary. */
7755 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p
)));
7757 if (!gimple_seq_empty_p (internal_post
) || (fallback
& fb_lvalue
))
7758 /* The postqueue might change the value of the expression between
7759 the initialization and use of the temporary, so we can't use a
7760 formal temp. FIXME do we care? */
7762 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
);
7763 if (TREE_CODE (TREE_TYPE (*expr_p
)) == COMPLEX_TYPE
7764 || TREE_CODE (TREE_TYPE (*expr_p
)) == VECTOR_TYPE
)
7765 DECL_GIMPLE_REG_P (*expr_p
) = 1;
7768 *expr_p
= get_formal_tmp_var (*expr_p
, pre_p
);
7772 #ifdef ENABLE_GIMPLE_CHECKING
7773 if (!(fallback
& fb_mayfail
))
7775 fprintf (stderr
, "gimplification failed:\n");
7776 print_generic_expr (stderr
, *expr_p
, 0);
7777 debug_tree (*expr_p
);
7778 internal_error ("gimplification failed");
7781 gcc_assert (fallback
& fb_mayfail
);
7783 /* If this is an asm statement, and the user asked for the
7784 impossible, don't die. Fail and let gimplify_asm_expr
7790 /* Make sure the temporary matches our predicate. */
7791 gcc_assert ((*gimple_test_f
) (*expr_p
));
7793 if (!gimple_seq_empty_p (internal_post
))
7795 annotate_all_with_location (internal_post
, input_location
);
7796 gimplify_seq_add_seq (pre_p
, internal_post
);
7800 input_location
= saved_location
;
7804 /* Look through TYPE for variable-sized objects and gimplify each such
7805 size that we find. Add to LIST_P any statements generated. */
7808 gimplify_type_sizes (tree type
, gimple_seq
*list_p
)
7812 if (type
== NULL
|| type
== error_mark_node
)
7815 /* We first do the main variant, then copy into any other variants. */
7816 type
= TYPE_MAIN_VARIANT (type
);
7818 /* Avoid infinite recursion. */
7819 if (TYPE_SIZES_GIMPLIFIED (type
))
7822 TYPE_SIZES_GIMPLIFIED (type
) = 1;
7824 switch (TREE_CODE (type
))
7830 case FIXED_POINT_TYPE
:
7831 gimplify_one_sizepos (&TYPE_MIN_VALUE (type
), list_p
);
7832 gimplify_one_sizepos (&TYPE_MAX_VALUE (type
), list_p
);
7834 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
7836 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
7837 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
7842 /* These types may not have declarations, so handle them here. */
7843 gimplify_type_sizes (TREE_TYPE (type
), list_p
);
7844 gimplify_type_sizes (TYPE_DOMAIN (type
), list_p
);
7845 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
7846 with assigned stack slots, for -O1+ -g they should be tracked
7848 if (!(TYPE_NAME (type
)
7849 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
7850 && DECL_IGNORED_P (TYPE_NAME (type
)))
7851 && TYPE_DOMAIN (type
)
7852 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type
)))
7854 t
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
7855 if (t
&& TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
))
7856 DECL_IGNORED_P (t
) = 0;
7857 t
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
7858 if (t
&& TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
))
7859 DECL_IGNORED_P (t
) = 0;
7865 case QUAL_UNION_TYPE
:
7866 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
7867 if (TREE_CODE (field
) == FIELD_DECL
)
7869 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field
), list_p
);
7870 gimplify_one_sizepos (&DECL_SIZE (field
), list_p
);
7871 gimplify_one_sizepos (&DECL_SIZE_UNIT (field
), list_p
);
7872 gimplify_type_sizes (TREE_TYPE (field
), list_p
);
7877 case REFERENCE_TYPE
:
7878 /* We used to recurse on the pointed-to type here, which turned out to
7879 be incorrect because its definition might refer to variables not
7880 yet initialized at this point if a forward declaration is involved.
7882 It was actually useful for anonymous pointed-to types to ensure
7883 that the sizes evaluation dominates every possible later use of the
7884 values. Restricting to such types here would be safe since there
7885 is no possible forward declaration around, but would introduce an
7886 undesirable middle-end semantic to anonymity. We then defer to
7887 front-ends the responsibility of ensuring that the sizes are
7888 evaluated both early and late enough, e.g. by attaching artificial
7889 type declarations to the tree. */
7896 gimplify_one_sizepos (&TYPE_SIZE (type
), list_p
);
7897 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type
), list_p
);
7899 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
7901 TYPE_SIZE (t
) = TYPE_SIZE (type
);
7902 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
7903 TYPE_SIZES_GIMPLIFIED (t
) = 1;
7907 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
7908 a size or position, has had all of its SAVE_EXPRs evaluated.
7909 We add any required statements to *STMT_P. */
7912 gimplify_one_sizepos (tree
*expr_p
, gimple_seq
*stmt_p
)
7914 tree type
, expr
= *expr_p
;
7916 /* We don't do anything if the value isn't there, is constant, or contains
7917 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
7918 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
7919 will want to replace it with a new variable, but that will cause problems
7920 if this type is from outside the function. It's OK to have that here. */
7921 if (expr
== NULL_TREE
|| TREE_CONSTANT (expr
)
7922 || TREE_CODE (expr
) == VAR_DECL
7923 || CONTAINS_PLACEHOLDER_P (expr
))
7926 type
= TREE_TYPE (expr
);
7927 *expr_p
= unshare_expr (expr
);
7929 gimplify_expr (expr_p
, stmt_p
, NULL
, is_gimple_val
, fb_rvalue
);
7932 /* Verify that we've an exact type match with the original expression.
7933 In particular, we do not wish to drop a "sizetype" in favour of a
7934 type of similar dimensions. We don't want to pollute the generic
7935 type-stripping code with this knowledge because it doesn't matter
7936 for the bulk of GENERIC/GIMPLE. It only matters that TYPE_SIZE_UNIT
7937 and friends retain their "sizetype-ness". */
7938 if (TREE_TYPE (expr
) != type
7939 && TREE_CODE (type
) == INTEGER_TYPE
7940 && TYPE_IS_SIZETYPE (type
))
7945 *expr_p
= create_tmp_var (type
, NULL
);
7946 tmp
= build1 (NOP_EXPR
, type
, expr
);
7947 stmt
= gimplify_assign (*expr_p
, tmp
, stmt_p
);
7948 gimple_set_location (stmt
, EXPR_LOC_OR_HERE (expr
));
7952 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
7953 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
7954 is true, also gimplify the parameters. */
7957 gimplify_body (tree fndecl
, bool do_parms
)
7959 location_t saved_location
= input_location
;
7960 gimple_seq parm_stmts
, seq
;
7962 struct gimplify_ctx gctx
;
7963 struct cgraph_node
*cgn
;
7965 timevar_push (TV_TREE_GIMPLIFY
);
7967 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
7969 default_rtl_profile ();
7971 gcc_assert (gimplify_ctxp
== NULL
);
7972 push_gimplify_context (&gctx
);
7974 /* Unshare most shared trees in the body and in that of any nested functions.
7975 It would seem we don't have to do this for nested functions because
7976 they are supposed to be output and then the outer function gimplified
7977 first, but the g++ front end doesn't always do it that way. */
7978 unshare_body (fndecl
);
7979 unvisit_body (fndecl
);
7981 cgn
= cgraph_get_node (fndecl
);
7982 if (cgn
&& cgn
->origin
)
7983 nonlocal_vlas
= pointer_set_create ();
7985 /* Make sure input_location isn't set to something weird. */
7986 input_location
= DECL_SOURCE_LOCATION (fndecl
);
7988 /* Resolve callee-copies. This has to be done before processing
7989 the body so that DECL_VALUE_EXPR gets processed correctly. */
7990 parm_stmts
= do_parms
? gimplify_parameters () : NULL
;
7992 /* Gimplify the function's body. */
7994 gimplify_stmt (&DECL_SAVED_TREE (fndecl
), &seq
);
7995 outer_bind
= gimple_seq_first_stmt (seq
);
7998 outer_bind
= gimple_build_nop ();
7999 gimplify_seq_add_stmt (&seq
, outer_bind
);
8002 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
8003 not the case, wrap everything in a GIMPLE_BIND to make it so. */
8004 if (gimple_code (outer_bind
) == GIMPLE_BIND
8005 && gimple_seq_first (seq
) == gimple_seq_last (seq
))
8008 outer_bind
= gimple_build_bind (NULL_TREE
, seq
, NULL
);
8010 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
8012 /* If we had callee-copies statements, insert them at the beginning
8013 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
8014 if (!gimple_seq_empty_p (parm_stmts
))
8018 gimplify_seq_add_seq (&parm_stmts
, gimple_bind_body (outer_bind
));
8019 gimple_bind_set_body (outer_bind
, parm_stmts
);
8021 for (parm
= DECL_ARGUMENTS (current_function_decl
);
8022 parm
; parm
= DECL_CHAIN (parm
))
8023 if (DECL_HAS_VALUE_EXPR_P (parm
))
8025 DECL_HAS_VALUE_EXPR_P (parm
) = 0;
8026 DECL_IGNORED_P (parm
) = 0;
8032 pointer_set_destroy (nonlocal_vlas
);
8033 nonlocal_vlas
= NULL
;
8036 pop_gimplify_context (outer_bind
);
8037 gcc_assert (gimplify_ctxp
== NULL
);
8040 verify_gimple_in_seq (gimple_bind_body (outer_bind
));
8042 timevar_pop (TV_TREE_GIMPLIFY
);
8043 input_location
= saved_location
;
8048 typedef char *char_p
; /* For DEF_VEC_P. */
8050 DEF_VEC_ALLOC_P(char_p
,heap
);
8052 /* Return whether we should exclude FNDECL from instrumentation. */
8055 flag_instrument_functions_exclude_p (tree fndecl
)
8057 VEC(char_p
,heap
) *vec
;
8059 vec
= (VEC(char_p
,heap
) *) flag_instrument_functions_exclude_functions
;
8060 if (VEC_length (char_p
, vec
) > 0)
8066 name
= lang_hooks
.decl_printable_name (fndecl
, 0);
8067 FOR_EACH_VEC_ELT (char_p
, vec
, i
, s
)
8068 if (strstr (name
, s
) != NULL
)
8072 vec
= (VEC(char_p
,heap
) *) flag_instrument_functions_exclude_files
;
8073 if (VEC_length (char_p
, vec
) > 0)
8079 name
= DECL_SOURCE_FILE (fndecl
);
8080 FOR_EACH_VEC_ELT (char_p
, vec
, i
, s
)
8081 if (strstr (name
, s
) != NULL
)
8088 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
8089 node for the function we want to gimplify.
8091 Return the sequence of GIMPLE statements corresponding to the body
8095 gimplify_function_tree (tree fndecl
)
8097 tree oldfn
, parm
, ret
;
8101 gcc_assert (!gimple_body (fndecl
));
8103 oldfn
= current_function_decl
;
8104 current_function_decl
= fndecl
;
8105 if (DECL_STRUCT_FUNCTION (fndecl
))
8106 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
8108 push_struct_function (fndecl
);
8110 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
8112 /* Preliminarily mark non-addressed complex variables as eligible
8113 for promotion to gimple registers. We'll transform their uses
8115 if ((TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
8116 || TREE_CODE (TREE_TYPE (parm
)) == VECTOR_TYPE
)
8117 && !TREE_THIS_VOLATILE (parm
)
8118 && !needs_to_live_in_memory (parm
))
8119 DECL_GIMPLE_REG_P (parm
) = 1;
8122 ret
= DECL_RESULT (fndecl
);
8123 if ((TREE_CODE (TREE_TYPE (ret
)) == COMPLEX_TYPE
8124 || TREE_CODE (TREE_TYPE (ret
)) == VECTOR_TYPE
)
8125 && !needs_to_live_in_memory (ret
))
8126 DECL_GIMPLE_REG_P (ret
) = 1;
8128 bind
= gimplify_body (fndecl
, true);
8130 /* The tree body of the function is no longer needed, replace it
8131 with the new GIMPLE body. */
8132 seq
= gimple_seq_alloc ();
8133 gimple_seq_add_stmt (&seq
, bind
);
8134 gimple_set_body (fndecl
, seq
);
8136 /* If we're instrumenting function entry/exit, then prepend the call to
8137 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
8138 catch the exit hook. */
8139 /* ??? Add some way to ignore exceptions for this TFE. */
8140 if (flag_instrument_function_entry_exit
8141 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl
)
8142 && !flag_instrument_functions_exclude_p (fndecl
))
8147 gimple_seq cleanup
= NULL
, body
= NULL
;
8151 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
8152 call
= gimple_build_call (x
, 1, integer_zero_node
);
8153 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
8154 gimple_call_set_lhs (call
, tmp_var
);
8155 gimplify_seq_add_stmt (&cleanup
, call
);
8156 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT
);
8157 call
= gimple_build_call (x
, 2,
8158 build_fold_addr_expr (current_function_decl
),
8160 gimplify_seq_add_stmt (&cleanup
, call
);
8161 tf
= gimple_build_try (seq
, cleanup
, GIMPLE_TRY_FINALLY
);
8163 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
8164 call
= gimple_build_call (x
, 1, integer_zero_node
);
8165 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
8166 gimple_call_set_lhs (call
, tmp_var
);
8167 gimplify_seq_add_stmt (&body
, call
);
8168 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER
);
8169 call
= gimple_build_call (x
, 2,
8170 build_fold_addr_expr (current_function_decl
),
8172 gimplify_seq_add_stmt (&body
, call
);
8173 gimplify_seq_add_stmt (&body
, tf
);
8174 new_bind
= gimple_build_bind (NULL
, body
, gimple_bind_block (bind
));
8175 /* Clear the block for BIND, since it is no longer directly inside
8176 the function, but within a try block. */
8177 gimple_bind_set_block (bind
, NULL
);
8179 /* Replace the current function body with the body
8180 wrapped in the try/finally TF. */
8181 seq
= gimple_seq_alloc ();
8182 gimple_seq_add_stmt (&seq
, new_bind
);
8183 gimple_set_body (fndecl
, seq
);
8186 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
8187 cfun
->curr_properties
= PROP_gimple_any
;
8189 current_function_decl
= oldfn
;
8193 /* Some transformations like inlining may invalidate the GIMPLE form
8194 for operands. This function traverses all the operands in STMT and
8195 gimplifies anything that is not a valid gimple operand. Any new
8196 GIMPLE statements are inserted before *GSI_P. */
8199 gimple_regimplify_operands (gimple stmt
, gimple_stmt_iterator
*gsi_p
)
8202 tree orig_lhs
= NULL_TREE
, lhs
, t
;
8203 gimple_seq pre
= NULL
;
8204 gimple post_stmt
= NULL
;
8205 struct gimplify_ctx gctx
;
8207 push_gimplify_context (&gctx
);
8208 gimplify_ctxp
->into_ssa
= gimple_in_ssa_p (cfun
);
8210 switch (gimple_code (stmt
))
8213 gimplify_expr (gimple_cond_lhs_ptr (stmt
), &pre
, NULL
,
8214 is_gimple_val
, fb_rvalue
);
8215 gimplify_expr (gimple_cond_rhs_ptr (stmt
), &pre
, NULL
,
8216 is_gimple_val
, fb_rvalue
);
8219 gimplify_expr (gimple_switch_index_ptr (stmt
), &pre
, NULL
,
8220 is_gimple_val
, fb_rvalue
);
8222 case GIMPLE_OMP_ATOMIC_LOAD
:
8223 gimplify_expr (gimple_omp_atomic_load_rhs_ptr (stmt
), &pre
, NULL
,
8224 is_gimple_val
, fb_rvalue
);
8228 size_t i
, noutputs
= gimple_asm_noutputs (stmt
);
8229 const char *constraint
, **oconstraints
;
8230 bool allows_mem
, allows_reg
, is_inout
;
8233 = (const char **) alloca ((noutputs
) * sizeof (const char *));
8234 for (i
= 0; i
< noutputs
; i
++)
8236 tree op
= gimple_asm_output_op (stmt
, i
);
8237 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op
)));
8238 oconstraints
[i
] = constraint
;
8239 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
8240 &allows_reg
, &is_inout
);
8241 gimplify_expr (&TREE_VALUE (op
), &pre
, NULL
,
8242 is_inout
? is_gimple_min_lval
: is_gimple_lvalue
,
8243 fb_lvalue
| fb_mayfail
);
8245 for (i
= 0; i
< gimple_asm_ninputs (stmt
); i
++)
8247 tree op
= gimple_asm_input_op (stmt
, i
);
8248 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op
)));
8249 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
8250 oconstraints
, &allows_mem
, &allows_reg
);
8251 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (op
))) && allows_mem
)
8253 if (!allows_reg
&& allows_mem
)
8254 gimplify_expr (&TREE_VALUE (op
), &pre
, NULL
,
8255 is_gimple_lvalue
, fb_lvalue
| fb_mayfail
);
8257 gimplify_expr (&TREE_VALUE (op
), &pre
, NULL
,
8258 is_gimple_asm_val
, fb_rvalue
);
8263 /* NOTE: We start gimplifying operands from last to first to
8264 make sure that side-effects on the RHS of calls, assignments
8265 and ASMs are executed before the LHS. The ordering is not
8266 important for other statements. */
8267 num_ops
= gimple_num_ops (stmt
);
8268 orig_lhs
= gimple_get_lhs (stmt
);
8269 for (i
= num_ops
; i
> 0; i
--)
8271 tree op
= gimple_op (stmt
, i
- 1);
8272 if (op
== NULL_TREE
)
8274 if (i
== 1 && (is_gimple_call (stmt
) || is_gimple_assign (stmt
)))
8275 gimplify_expr (&op
, &pre
, NULL
, is_gimple_lvalue
, fb_lvalue
);
8277 && is_gimple_assign (stmt
)
8279 && get_gimple_rhs_class (gimple_expr_code (stmt
))
8280 == GIMPLE_SINGLE_RHS
)
8281 gimplify_expr (&op
, &pre
, NULL
,
8282 rhs_predicate_for (gimple_assign_lhs (stmt
)),
8284 else if (i
== 2 && is_gimple_call (stmt
))
8286 if (TREE_CODE (op
) == FUNCTION_DECL
)
8288 gimplify_expr (&op
, &pre
, NULL
, is_gimple_call_addr
, fb_rvalue
);
8291 gimplify_expr (&op
, &pre
, NULL
, is_gimple_val
, fb_rvalue
);
8292 gimple_set_op (stmt
, i
- 1, op
);
8295 lhs
= gimple_get_lhs (stmt
);
8296 /* If the LHS changed it in a way that requires a simple RHS,
8297 create temporary. */
8298 if (lhs
&& !is_gimple_reg (lhs
))
8300 bool need_temp
= false;
8302 if (is_gimple_assign (stmt
)
8304 && get_gimple_rhs_class (gimple_expr_code (stmt
))
8305 == GIMPLE_SINGLE_RHS
)
8306 gimplify_expr (gimple_assign_rhs1_ptr (stmt
), &pre
, NULL
,
8307 rhs_predicate_for (gimple_assign_lhs (stmt
)),
8309 else if (is_gimple_reg (lhs
))
8311 if (is_gimple_reg_type (TREE_TYPE (lhs
)))
8313 if (is_gimple_call (stmt
))
8315 i
= gimple_call_flags (stmt
);
8316 if ((i
& ECF_LOOPING_CONST_OR_PURE
)
8317 || !(i
& (ECF_CONST
| ECF_PURE
)))
8320 if (stmt_can_throw_internal (stmt
))
8326 if (is_gimple_reg_type (TREE_TYPE (lhs
)))
8328 else if (TYPE_MODE (TREE_TYPE (lhs
)) != BLKmode
)
8330 if (is_gimple_call (stmt
))
8332 tree fndecl
= gimple_call_fndecl (stmt
);
8334 if (!aggregate_value_p (TREE_TYPE (lhs
), fndecl
)
8335 && !(fndecl
&& DECL_RESULT (fndecl
)
8336 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
))))
8345 tree temp
= create_tmp_reg (TREE_TYPE (lhs
), NULL
);
8347 if (TREE_CODE (orig_lhs
) == SSA_NAME
)
8348 orig_lhs
= SSA_NAME_VAR (orig_lhs
);
8350 if (gimple_in_ssa_p (cfun
))
8351 temp
= make_ssa_name (temp
, NULL
);
8352 gimple_set_lhs (stmt
, temp
);
8353 post_stmt
= gimple_build_assign (lhs
, temp
);
8354 if (TREE_CODE (lhs
) == SSA_NAME
)
8355 SSA_NAME_DEF_STMT (lhs
) = post_stmt
;
8361 if (gimple_referenced_vars (cfun
))
8362 for (t
= gimplify_ctxp
->temps
; t
; t
= TREE_CHAIN (t
))
8363 add_referenced_var (t
);
8365 if (!gimple_seq_empty_p (pre
))
8367 if (gimple_in_ssa_p (cfun
))
8369 gimple_stmt_iterator i
;
8371 for (i
= gsi_start (pre
); !gsi_end_p (i
); gsi_next (&i
))
8372 mark_symbols_for_renaming (gsi_stmt (i
));
8374 gsi_insert_seq_before (gsi_p
, pre
, GSI_SAME_STMT
);
8377 gsi_insert_after (gsi_p
, post_stmt
, GSI_NEW_STMT
);
8379 pop_gimplify_context (NULL
);
8382 /* Expand EXPR to list of gimple statements STMTS. GIMPLE_TEST_F specifies
8383 the predicate that will hold for the result. If VAR is not NULL, make the
8384 base variable of the final destination be VAR if suitable. */
8387 force_gimple_operand_1 (tree expr
, gimple_seq
*stmts
,
8388 gimple_predicate gimple_test_f
, tree var
)
8391 enum gimplify_status ret
;
8392 struct gimplify_ctx gctx
;
8396 /* gimple_test_f might be more strict than is_gimple_val, make
8397 sure we pass both. Just checking gimple_test_f doesn't work
8398 because most gimple predicates do not work recursively. */
8399 if (is_gimple_val (expr
)
8400 && (*gimple_test_f
) (expr
))
8403 push_gimplify_context (&gctx
);
8404 gimplify_ctxp
->into_ssa
= gimple_in_ssa_p (cfun
);
8405 gimplify_ctxp
->allow_rhs_cond_expr
= true;
8408 expr
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, expr
);
8410 if (TREE_CODE (expr
) != MODIFY_EXPR
8411 && TREE_TYPE (expr
) == void_type_node
)
8413 gimplify_and_add (expr
, stmts
);
8418 ret
= gimplify_expr (&expr
, stmts
, NULL
, gimple_test_f
, fb_rvalue
);
8419 gcc_assert (ret
!= GS_ERROR
);
8422 if (gimple_referenced_vars (cfun
))
8423 for (t
= gimplify_ctxp
->temps
; t
; t
= DECL_CHAIN (t
))
8424 add_referenced_var (t
);
8426 pop_gimplify_context (NULL
);
8431 /* Expand EXPR to list of gimple statements STMTS. If SIMPLE is true,
8432 force the result to be either ssa_name or an invariant, otherwise
8433 just force it to be a rhs expression. If VAR is not NULL, make the
8434 base variable of the final destination be VAR if suitable. */
8437 force_gimple_operand (tree expr
, gimple_seq
*stmts
, bool simple
, tree var
)
8439 return force_gimple_operand_1 (expr
, stmts
,
8440 simple
? is_gimple_val
: is_gimple_reg_rhs
,
8444 /* Invoke force_gimple_operand_1 for EXPR with parameters GIMPLE_TEST_F
8445 and VAR. If some statements are produced, emits them at GSI.
8446 If BEFORE is true. the statements are appended before GSI, otherwise
8447 they are appended after it. M specifies the way GSI moves after
8448 insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING are the usual values). */
8451 force_gimple_operand_gsi_1 (gimple_stmt_iterator
*gsi
, tree expr
,
8452 gimple_predicate gimple_test_f
,
8453 tree var
, bool before
,
8454 enum gsi_iterator_update m
)
8458 expr
= force_gimple_operand_1 (expr
, &stmts
, gimple_test_f
, var
);
8460 if (!gimple_seq_empty_p (stmts
))
8462 if (gimple_in_ssa_p (cfun
))
8464 gimple_stmt_iterator i
;
8466 for (i
= gsi_start (stmts
); !gsi_end_p (i
); gsi_next (&i
))
8467 mark_symbols_for_renaming (gsi_stmt (i
));
8471 gsi_insert_seq_before (gsi
, stmts
, m
);
8473 gsi_insert_seq_after (gsi
, stmts
, m
);
8479 /* Invoke force_gimple_operand_1 for EXPR with parameter VAR.
8480 If SIMPLE is true, force the result to be either ssa_name or an invariant,
8481 otherwise just force it to be a rhs expression. If some statements are
8482 produced, emits them at GSI. If BEFORE is true, the statements are
8483 appended before GSI, otherwise they are appended after it. M specifies
8484 the way GSI moves after insertion (GSI_SAME_STMT or GSI_CONTINUE_LINKING
8485 are the usual values). */
8488 force_gimple_operand_gsi (gimple_stmt_iterator
*gsi
, tree expr
,
8489 bool simple_p
, tree var
, bool before
,
8490 enum gsi_iterator_update m
)
8492 return force_gimple_operand_gsi_1 (gsi
, expr
,
8494 ? is_gimple_val
: is_gimple_reg_rhs
,
8499 #include "gt-gimplify.h"