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