1 /* Tree lowering pass. This pass converts the GENERIC functions-as-trees
2 tree representation into the GIMPLE form.
3 Copyright (C) 2002-2016 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"
31 #include "gimple-predict.h"
32 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any */
35 #include "tree-pretty-print.h"
36 #include "diagnostic-core.h"
38 #include "fold-const.h"
43 #include "gimple-fold.h"
46 #include "gimple-iterator.h"
47 #include "stor-layout.h"
48 #include "print-tree.h"
49 #include "tree-iterator.h"
50 #include "tree-inline.h"
51 #include "langhooks.h"
55 #include "gimple-low.h"
57 #include "gomp-constants.h"
58 #include "tree-dump.h"
59 #include "gimple-walk.h"
60 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name */
63 enum gimplify_omp_var_data
69 GOVD_FIRSTPRIVATE
= 16,
70 GOVD_LASTPRIVATE
= 32,
74 GOVD_DEBUG_PRIVATE
= 512,
75 GOVD_PRIVATE_OUTER_REF
= 1024,
79 /* Flag for GOVD_MAP: don't copy back. */
80 GOVD_MAP_TO_ONLY
= 8192,
82 /* Flag for GOVD_LINEAR or GOVD_LASTPRIVATE: no outer reference. */
83 GOVD_LINEAR_LASTPRIVATE_NO_OUTER
= 16384,
85 GOVD_MAP_0LEN_ARRAY
= 32768,
87 /* Flag for GOVD_MAP, if it is always, to or always, tofrom mapping. */
88 GOVD_MAP_ALWAYS_TO
= 65536,
90 /* Flag for shared vars that are or might be stored to in the region. */
91 GOVD_WRITTEN
= 131072,
93 /* Flag for GOVD_MAP, if it is a forced mapping. */
94 GOVD_MAP_FORCE
= 262144,
96 GOVD_DATA_SHARE_CLASS
= (GOVD_SHARED
| GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
97 | GOVD_LASTPRIVATE
| GOVD_REDUCTION
| GOVD_LINEAR
104 ORT_WORKSHARE
= 0x00,
108 ORT_COMBINED_PARALLEL
= 0x03,
111 ORT_UNTIED_TASK
= 0x05,
114 ORT_COMBINED_TEAMS
= 0x09,
117 ORT_TARGET_DATA
= 0x10,
119 /* Data region with offloading. */
121 ORT_COMBINED_TARGET
= 0x21,
123 /* OpenACC variants. */
124 ORT_ACC
= 0x40, /* A generic OpenACC region. */
125 ORT_ACC_DATA
= ORT_ACC
| ORT_TARGET_DATA
, /* Data construct. */
126 ORT_ACC_PARALLEL
= ORT_ACC
| ORT_TARGET
, /* Parallel construct */
127 ORT_ACC_KERNELS
= ORT_ACC
| ORT_TARGET
| 0x80, /* Kernels construct. */
128 ORT_ACC_HOST_DATA
= ORT_ACC
| ORT_TARGET_DATA
| 0x80, /* Host data. */
130 /* Dummy OpenMP region, used to disable expansion of
131 DECL_VALUE_EXPRs in taskloop pre body. */
135 /* Gimplify hashtable helper. */
137 struct gimplify_hasher
: free_ptr_hash
<elt_t
>
139 static inline hashval_t
hash (const elt_t
*);
140 static inline bool equal (const elt_t
*, const elt_t
*);
145 struct gimplify_ctx
*prev_context
;
147 vec
<gbind
*> bind_expr_stack
;
149 gimple_seq conditional_cleanups
;
153 vec
<tree
> case_labels
;
154 /* The formal temporary table. Should this be persistent? */
155 hash_table
<gimplify_hasher
> *temp_htab
;
158 unsigned into_ssa
: 1;
159 unsigned allow_rhs_cond_expr
: 1;
160 unsigned in_cleanup_point_expr
: 1;
161 unsigned keep_stack
: 1;
162 unsigned save_stack
: 1;
165 struct gimplify_omp_ctx
167 struct gimplify_omp_ctx
*outer_context
;
168 splay_tree variables
;
169 hash_set
<tree
> *privatized_types
;
170 /* Iteration variables in an OMP_FOR. */
171 vec
<tree
> loop_iter_var
;
173 enum omp_clause_default_kind default_kind
;
174 enum omp_region_type region_type
;
177 bool target_map_scalars_firstprivate
;
178 bool target_map_pointers_as_0len_arrays
;
179 bool target_firstprivatize_array_bases
;
182 static struct gimplify_ctx
*gimplify_ctxp
;
183 static struct gimplify_omp_ctx
*gimplify_omp_ctxp
;
185 /* Forward declaration. */
186 static enum gimplify_status
gimplify_compound_expr (tree
*, gimple_seq
*, bool);
187 static hash_map
<tree
, tree
> *oacc_declare_returns
;
188 static enum gimplify_status
gimplify_expr (tree
*, gimple_seq
*, gimple_seq
*,
189 bool (*) (tree
), fallback_t
, bool);
191 /* Shorter alias name for the above function for use in gimplify.c
195 gimplify_seq_add_stmt (gimple_seq
*seq_p
, gimple
*gs
)
197 gimple_seq_add_stmt_without_update (seq_p
, gs
);
200 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
201 NULL, a new sequence is allocated. This function is
202 similar to gimple_seq_add_seq, but does not scan the operands.
203 During gimplification, we need to manipulate statement sequences
204 before the def/use vectors have been constructed. */
207 gimplify_seq_add_seq (gimple_seq
*dst_p
, gimple_seq src
)
209 gimple_stmt_iterator si
;
214 si
= gsi_last (*dst_p
);
215 gsi_insert_seq_after_without_update (&si
, src
, GSI_NEW_STMT
);
219 /* Pointer to a list of allocated gimplify_ctx structs to be used for pushing
220 and popping gimplify contexts. */
222 static struct gimplify_ctx
*ctx_pool
= NULL
;
224 /* Return a gimplify context struct from the pool. */
226 static inline struct gimplify_ctx
*
229 struct gimplify_ctx
* c
= ctx_pool
;
232 ctx_pool
= c
->prev_context
;
234 c
= XNEW (struct gimplify_ctx
);
236 memset (c
, '\0', sizeof (*c
));
240 /* Put gimplify context C back into the pool. */
243 ctx_free (struct gimplify_ctx
*c
)
245 c
->prev_context
= ctx_pool
;
249 /* Free allocated ctx stack memory. */
252 free_gimplify_stack (void)
254 struct gimplify_ctx
*c
;
256 while ((c
= ctx_pool
))
258 ctx_pool
= c
->prev_context
;
264 /* Set up a context for the gimplifier. */
267 push_gimplify_context (bool in_ssa
, bool rhs_cond_ok
)
269 struct gimplify_ctx
*c
= ctx_alloc ();
271 c
->prev_context
= gimplify_ctxp
;
273 gimplify_ctxp
->into_ssa
= in_ssa
;
274 gimplify_ctxp
->allow_rhs_cond_expr
= rhs_cond_ok
;
277 /* Tear down a context for the gimplifier. If BODY is non-null, then
278 put the temporaries into the outer BIND_EXPR. Otherwise, put them
281 BODY is not a sequence, but the first tuple in a sequence. */
284 pop_gimplify_context (gimple
*body
)
286 struct gimplify_ctx
*c
= gimplify_ctxp
;
289 && (!c
->bind_expr_stack
.exists ()
290 || c
->bind_expr_stack
.is_empty ()));
291 c
->bind_expr_stack
.release ();
292 gimplify_ctxp
= c
->prev_context
;
295 declare_vars (c
->temps
, body
, false);
297 record_vars (c
->temps
);
304 /* Push a GIMPLE_BIND tuple onto the stack of bindings. */
307 gimple_push_bind_expr (gbind
*bind_stmt
)
309 gimplify_ctxp
->bind_expr_stack
.reserve (8);
310 gimplify_ctxp
->bind_expr_stack
.safe_push (bind_stmt
);
313 /* Pop the first element off the stack of bindings. */
316 gimple_pop_bind_expr (void)
318 gimplify_ctxp
->bind_expr_stack
.pop ();
321 /* Return the first element of the stack of bindings. */
324 gimple_current_bind_expr (void)
326 return gimplify_ctxp
->bind_expr_stack
.last ();
329 /* Return the stack of bindings created during gimplification. */
332 gimple_bind_expr_stack (void)
334 return gimplify_ctxp
->bind_expr_stack
;
337 /* Return true iff there is a COND_EXPR between us and the innermost
338 CLEANUP_POINT_EXPR. This info is used by gimple_push_cleanup. */
341 gimple_conditional_context (void)
343 return gimplify_ctxp
->conditions
> 0;
346 /* Note that we've entered a COND_EXPR. */
349 gimple_push_condition (void)
351 #ifdef ENABLE_GIMPLE_CHECKING
352 if (gimplify_ctxp
->conditions
== 0)
353 gcc_assert (gimple_seq_empty_p (gimplify_ctxp
->conditional_cleanups
));
355 ++(gimplify_ctxp
->conditions
);
358 /* Note that we've left a COND_EXPR. If we're back at unconditional scope
359 now, add any conditional cleanups we've seen to the prequeue. */
362 gimple_pop_condition (gimple_seq
*pre_p
)
364 int conds
= --(gimplify_ctxp
->conditions
);
366 gcc_assert (conds
>= 0);
369 gimplify_seq_add_seq (pre_p
, gimplify_ctxp
->conditional_cleanups
);
370 gimplify_ctxp
->conditional_cleanups
= NULL
;
374 /* A stable comparison routine for use with splay trees and DECLs. */
377 splay_tree_compare_decl_uid (splay_tree_key xa
, splay_tree_key xb
)
382 return DECL_UID (a
) - DECL_UID (b
);
385 /* Create a new omp construct that deals with variable remapping. */
387 static struct gimplify_omp_ctx
*
388 new_omp_context (enum omp_region_type region_type
)
390 struct gimplify_omp_ctx
*c
;
392 c
= XCNEW (struct gimplify_omp_ctx
);
393 c
->outer_context
= gimplify_omp_ctxp
;
394 c
->variables
= splay_tree_new (splay_tree_compare_decl_uid
, 0, 0);
395 c
->privatized_types
= new hash_set
<tree
>;
396 c
->location
= input_location
;
397 c
->region_type
= region_type
;
398 if ((region_type
& ORT_TASK
) == 0)
399 c
->default_kind
= OMP_CLAUSE_DEFAULT_SHARED
;
401 c
->default_kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
406 /* Destroy an omp construct that deals with variable remapping. */
409 delete_omp_context (struct gimplify_omp_ctx
*c
)
411 splay_tree_delete (c
->variables
);
412 delete c
->privatized_types
;
413 c
->loop_iter_var
.release ();
417 static void omp_add_variable (struct gimplify_omp_ctx
*, tree
, unsigned int);
418 static bool omp_notice_variable (struct gimplify_omp_ctx
*, tree
, bool);
420 /* Both gimplify the statement T and append it to *SEQ_P. This function
421 behaves exactly as gimplify_stmt, but you don't have to pass T as a
425 gimplify_and_add (tree t
, gimple_seq
*seq_p
)
427 gimplify_stmt (&t
, seq_p
);
430 /* Gimplify statement T into sequence *SEQ_P, and return the first
431 tuple in the sequence of generated tuples for this statement.
432 Return NULL if gimplifying T produced no tuples. */
435 gimplify_and_return_first (tree t
, gimple_seq
*seq_p
)
437 gimple_stmt_iterator last
= gsi_last (*seq_p
);
439 gimplify_and_add (t
, seq_p
);
441 if (!gsi_end_p (last
))
444 return gsi_stmt (last
);
447 return gimple_seq_first_stmt (*seq_p
);
450 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
451 LHS, or for a call argument. */
454 is_gimple_mem_rhs (tree t
)
456 /* If we're dealing with a renamable type, either source or dest must be
457 a renamed variable. */
458 if (is_gimple_reg_type (TREE_TYPE (t
)))
459 return is_gimple_val (t
);
461 return is_gimple_val (t
) || is_gimple_lvalue (t
);
464 /* Return true if T is a CALL_EXPR or an expression that can be
465 assigned to a temporary. Note that this predicate should only be
466 used during gimplification. See the rationale for this in
467 gimplify_modify_expr. */
470 is_gimple_reg_rhs_or_call (tree t
)
472 return (get_gimple_rhs_class (TREE_CODE (t
)) != GIMPLE_INVALID_RHS
473 || TREE_CODE (t
) == CALL_EXPR
);
476 /* Return true if T is a valid memory RHS or a CALL_EXPR. Note that
477 this predicate should only be used during gimplification. See the
478 rationale for this in gimplify_modify_expr. */
481 is_gimple_mem_rhs_or_call (tree t
)
483 /* If we're dealing with a renamable type, either source or dest must be
484 a renamed variable. */
485 if (is_gimple_reg_type (TREE_TYPE (t
)))
486 return is_gimple_val (t
);
488 return (is_gimple_val (t
) || is_gimple_lvalue (t
)
489 || TREE_CODE (t
) == CALL_EXPR
);
492 /* Create a temporary with a name derived from VAL. Subroutine of
493 lookup_tmp_var; nobody else should call this function. */
496 create_tmp_from_val (tree val
)
498 /* Drop all qualifiers and address-space information from the value type. */
499 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (val
));
500 tree var
= create_tmp_var (type
, get_name (val
));
501 if (TREE_CODE (TREE_TYPE (var
)) == COMPLEX_TYPE
502 || TREE_CODE (TREE_TYPE (var
)) == VECTOR_TYPE
)
503 DECL_GIMPLE_REG_P (var
) = 1;
507 /* Create a temporary to hold the value of VAL. If IS_FORMAL, try to reuse
508 an existing expression temporary. */
511 lookup_tmp_var (tree val
, bool is_formal
)
515 /* If not optimizing, never really reuse a temporary. local-alloc
516 won't allocate any variable that is used in more than one basic
517 block, which means it will go into memory, causing much extra
518 work in reload and final and poorer code generation, outweighing
519 the extra memory allocation here. */
520 if (!optimize
|| !is_formal
|| TREE_SIDE_EFFECTS (val
))
521 ret
= create_tmp_from_val (val
);
528 if (!gimplify_ctxp
->temp_htab
)
529 gimplify_ctxp
->temp_htab
= new hash_table
<gimplify_hasher
> (1000);
530 slot
= gimplify_ctxp
->temp_htab
->find_slot (&elt
, INSERT
);
533 elt_p
= XNEW (elt_t
);
535 elt_p
->temp
= ret
= create_tmp_from_val (val
);
548 /* Helper for get_formal_tmp_var and get_initialized_tmp_var. */
551 internal_get_tmp_var (tree val
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
552 bool is_formal
, bool allow_ssa
)
556 /* Notice that we explicitly allow VAL to be a CALL_EXPR so that we
557 can create an INIT_EXPR and convert it into a GIMPLE_CALL below. */
558 gimplify_expr (&val
, pre_p
, post_p
, is_gimple_reg_rhs_or_call
,
562 && gimplify_ctxp
->into_ssa
563 && is_gimple_reg_type (TREE_TYPE (val
)))
565 t
= make_ssa_name (TYPE_MAIN_VARIANT (TREE_TYPE (val
)));
566 if (! gimple_in_ssa_p (cfun
))
568 const char *name
= get_name (val
);
570 SET_SSA_NAME_VAR_OR_IDENTIFIER (t
, create_tmp_var_name (name
));
574 t
= lookup_tmp_var (val
, is_formal
);
576 mod
= build2 (INIT_EXPR
, TREE_TYPE (t
), t
, unshare_expr (val
));
578 SET_EXPR_LOCATION (mod
, EXPR_LOC_OR_LOC (val
, input_location
));
580 /* gimplify_modify_expr might want to reduce this further. */
581 gimplify_and_add (mod
, pre_p
);
587 /* Return a formal temporary variable initialized with VAL. PRE_P is as
588 in gimplify_expr. Only use this function if:
590 1) The value of the unfactored expression represented by VAL will not
591 change between the initialization and use of the temporary, and
592 2) The temporary will not be otherwise modified.
594 For instance, #1 means that this is inappropriate for SAVE_EXPR temps,
595 and #2 means it is inappropriate for && temps.
597 For other cases, use get_initialized_tmp_var instead. */
600 get_formal_tmp_var (tree val
, gimple_seq
*pre_p
)
602 return internal_get_tmp_var (val
, pre_p
, NULL
, true, true);
605 /* Return a temporary variable initialized with VAL. PRE_P and POST_P
606 are as in gimplify_expr. */
609 get_initialized_tmp_var (tree val
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
612 return internal_get_tmp_var (val
, pre_p
, post_p
, false, allow_ssa
);
615 /* Declare all the variables in VARS in SCOPE. If DEBUG_INFO is true,
616 generate debug info for them; otherwise don't. */
619 declare_vars (tree vars
, gimple
*gs
, bool debug_info
)
626 gbind
*scope
= as_a
<gbind
*> (gs
);
628 temps
= nreverse (last
);
630 block
= gimple_bind_block (scope
);
631 gcc_assert (!block
|| TREE_CODE (block
) == BLOCK
);
632 if (!block
|| !debug_info
)
634 DECL_CHAIN (last
) = gimple_bind_vars (scope
);
635 gimple_bind_set_vars (scope
, temps
);
639 /* We need to attach the nodes both to the BIND_EXPR and to its
640 associated BLOCK for debugging purposes. The key point here
641 is that the BLOCK_VARS of the BIND_EXPR_BLOCK of a BIND_EXPR
642 is a subchain of the BIND_EXPR_VARS of the BIND_EXPR. */
643 if (BLOCK_VARS (block
))
644 BLOCK_VARS (block
) = chainon (BLOCK_VARS (block
), temps
);
647 gimple_bind_set_vars (scope
,
648 chainon (gimple_bind_vars (scope
), temps
));
649 BLOCK_VARS (block
) = temps
;
655 /* For VAR a VAR_DECL of variable size, try to find a constant upper bound
656 for the size and adjust DECL_SIZE/DECL_SIZE_UNIT accordingly. Abort if
657 no such upper bound can be obtained. */
660 force_constant_size (tree var
)
662 /* The only attempt we make is by querying the maximum size of objects
663 of the variable's type. */
665 HOST_WIDE_INT max_size
;
667 gcc_assert (TREE_CODE (var
) == VAR_DECL
);
669 max_size
= max_int_size_in_bytes (TREE_TYPE (var
));
671 gcc_assert (max_size
>= 0);
674 = build_int_cst (TREE_TYPE (DECL_SIZE_UNIT (var
)), max_size
);
676 = build_int_cst (TREE_TYPE (DECL_SIZE (var
)), max_size
* BITS_PER_UNIT
);
679 /* Push the temporary variable TMP into the current binding. */
682 gimple_add_tmp_var_fn (struct function
*fn
, tree tmp
)
684 gcc_assert (!DECL_CHAIN (tmp
) && !DECL_SEEN_IN_BIND_EXPR_P (tmp
));
686 /* Later processing assumes that the object size is constant, which might
687 not be true at this point. Force the use of a constant upper bound in
689 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp
)))
690 force_constant_size (tmp
);
692 DECL_CONTEXT (tmp
) = fn
->decl
;
693 DECL_SEEN_IN_BIND_EXPR_P (tmp
) = 1;
695 record_vars_into (tmp
, fn
->decl
);
698 /* Push the temporary variable TMP into the current binding. */
701 gimple_add_tmp_var (tree tmp
)
703 gcc_assert (!DECL_CHAIN (tmp
) && !DECL_SEEN_IN_BIND_EXPR_P (tmp
));
705 /* Later processing assumes that the object size is constant, which might
706 not be true at this point. Force the use of a constant upper bound in
708 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (tmp
)))
709 force_constant_size (tmp
);
711 DECL_CONTEXT (tmp
) = current_function_decl
;
712 DECL_SEEN_IN_BIND_EXPR_P (tmp
) = 1;
716 DECL_CHAIN (tmp
) = gimplify_ctxp
->temps
;
717 gimplify_ctxp
->temps
= tmp
;
719 /* Mark temporaries local within the nearest enclosing parallel. */
720 if (gimplify_omp_ctxp
)
722 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
724 && (ctx
->region_type
== ORT_WORKSHARE
725 || ctx
->region_type
== ORT_SIMD
726 || ctx
->region_type
== ORT_ACC
))
727 ctx
= ctx
->outer_context
;
729 omp_add_variable (ctx
, tmp
, GOVD_LOCAL
| GOVD_SEEN
);
738 /* This case is for nested functions. We need to expose the locals
740 body_seq
= gimple_body (current_function_decl
);
741 declare_vars (tmp
, gimple_seq_first_stmt (body_seq
), false);
747 /* This page contains routines to unshare tree nodes, i.e. to duplicate tree
748 nodes that are referenced more than once in GENERIC functions. This is
749 necessary because gimplification (translation into GIMPLE) is performed
750 by modifying tree nodes in-place, so gimplication of a shared node in a
751 first context could generate an invalid GIMPLE form in a second context.
753 This is achieved with a simple mark/copy/unmark algorithm that walks the
754 GENERIC representation top-down, marks nodes with TREE_VISITED the first
755 time it encounters them, duplicates them if they already have TREE_VISITED
756 set, and finally removes the TREE_VISITED marks it has set.
758 The algorithm works only at the function level, i.e. it generates a GENERIC
759 representation of a function with no nodes shared within the function when
760 passed a GENERIC function (except for nodes that are allowed to be shared).
762 At the global level, it is also necessary to unshare tree nodes that are
763 referenced in more than one function, for the same aforementioned reason.
764 This requires some cooperation from the front-end. There are 2 strategies:
766 1. Manual unsharing. The front-end needs to call unshare_expr on every
767 expression that might end up being shared across functions.
769 2. Deep unsharing. This is an extension of regular unsharing. Instead
770 of calling unshare_expr on expressions that might be shared across
771 functions, the front-end pre-marks them with TREE_VISITED. This will
772 ensure that they are unshared on the first reference within functions
773 when the regular unsharing algorithm runs. The counterpart is that
774 this algorithm must look deeper than for manual unsharing, which is
775 specified by LANG_HOOKS_DEEP_UNSHARING.
777 If there are only few specific cases of node sharing across functions, it is
778 probably easier for a front-end to unshare the expressions manually. On the
779 contrary, if the expressions generated at the global level are as widespread
780 as expressions generated within functions, deep unsharing is very likely the
783 /* Similar to copy_tree_r but do not copy SAVE_EXPR or TARGET_EXPR nodes.
784 These nodes model computations that must be done once. If we were to
785 unshare something like SAVE_EXPR(i++), the gimplification process would
786 create wrong code. However, if DATA is non-null, it must hold a pointer
787 set that is used to unshare the subtrees of these nodes. */
790 mostly_copy_tree_r (tree
*tp
, int *walk_subtrees
, void *data
)
793 enum tree_code code
= TREE_CODE (t
);
795 /* Do not copy SAVE_EXPR, TARGET_EXPR or BIND_EXPR nodes themselves, but
796 copy their subtrees if we can make sure to do it only once. */
797 if (code
== SAVE_EXPR
|| code
== TARGET_EXPR
|| code
== BIND_EXPR
)
799 if (data
&& !((hash_set
<tree
> *)data
)->add (t
))
805 /* Stop at types, decls, constants like copy_tree_r. */
806 else if (TREE_CODE_CLASS (code
) == tcc_type
807 || TREE_CODE_CLASS (code
) == tcc_declaration
808 || TREE_CODE_CLASS (code
) == tcc_constant
809 /* We can't do anything sensible with a BLOCK used as an
810 expression, but we also can't just die when we see it
811 because of non-expression uses. So we avert our eyes
812 and cross our fingers. Silly Java. */
816 /* Cope with the statement expression extension. */
817 else if (code
== STATEMENT_LIST
)
820 /* Leave the bulk of the work to copy_tree_r itself. */
822 copy_tree_r (tp
, walk_subtrees
, NULL
);
827 /* Callback for walk_tree to unshare most of the shared trees rooted at *TP.
828 If *TP has been visited already, then *TP is deeply copied by calling
829 mostly_copy_tree_r. DATA is passed to mostly_copy_tree_r unmodified. */
832 copy_if_shared_r (tree
*tp
, int *walk_subtrees
, void *data
)
835 enum tree_code code
= TREE_CODE (t
);
837 /* Skip types, decls, and constants. But we do want to look at their
838 types and the bounds of types. Mark them as visited so we properly
839 unmark their subtrees on the unmark pass. If we've already seen them,
840 don't look down further. */
841 if (TREE_CODE_CLASS (code
) == tcc_type
842 || TREE_CODE_CLASS (code
) == tcc_declaration
843 || TREE_CODE_CLASS (code
) == tcc_constant
)
845 if (TREE_VISITED (t
))
848 TREE_VISITED (t
) = 1;
851 /* If this node has been visited already, unshare it and don't look
853 else if (TREE_VISITED (t
))
855 walk_tree (tp
, mostly_copy_tree_r
, data
, NULL
);
859 /* Otherwise, mark the node as visited and keep looking. */
861 TREE_VISITED (t
) = 1;
866 /* Unshare most of the shared trees rooted at *TP. DATA is passed to the
867 copy_if_shared_r callback unmodified. */
870 copy_if_shared (tree
*tp
, void *data
)
872 walk_tree (tp
, copy_if_shared_r
, data
, NULL
);
875 /* Unshare all the trees in the body of FNDECL, as well as in the bodies of
876 any nested functions. */
879 unshare_body (tree fndecl
)
881 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
882 /* If the language requires deep unsharing, we need a pointer set to make
883 sure we don't repeatedly unshare subtrees of unshareable nodes. */
884 hash_set
<tree
> *visited
885 = lang_hooks
.deep_unsharing
? new hash_set
<tree
> : NULL
;
887 copy_if_shared (&DECL_SAVED_TREE (fndecl
), visited
);
888 copy_if_shared (&DECL_SIZE (DECL_RESULT (fndecl
)), visited
);
889 copy_if_shared (&DECL_SIZE_UNIT (DECL_RESULT (fndecl
)), visited
);
894 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
895 unshare_body (cgn
->decl
);
898 /* Callback for walk_tree to unmark the visited trees rooted at *TP.
899 Subtrees are walked until the first unvisited node is encountered. */
902 unmark_visited_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
906 /* If this node has been visited, unmark it and keep looking. */
907 if (TREE_VISITED (t
))
908 TREE_VISITED (t
) = 0;
910 /* Otherwise, don't look any deeper. */
917 /* Unmark the visited trees rooted at *TP. */
920 unmark_visited (tree
*tp
)
922 walk_tree (tp
, unmark_visited_r
, NULL
, NULL
);
925 /* Likewise, but mark all trees as not visited. */
928 unvisit_body (tree fndecl
)
930 struct cgraph_node
*cgn
= cgraph_node::get (fndecl
);
932 unmark_visited (&DECL_SAVED_TREE (fndecl
));
933 unmark_visited (&DECL_SIZE (DECL_RESULT (fndecl
)));
934 unmark_visited (&DECL_SIZE_UNIT (DECL_RESULT (fndecl
)));
937 for (cgn
= cgn
->nested
; cgn
; cgn
= cgn
->next_nested
)
938 unvisit_body (cgn
->decl
);
941 /* Unconditionally make an unshared copy of EXPR. This is used when using
942 stored expressions which span multiple functions, such as BINFO_VTABLE,
943 as the normal unsharing process can't tell that they're shared. */
946 unshare_expr (tree expr
)
948 walk_tree (&expr
, mostly_copy_tree_r
, NULL
, NULL
);
952 /* Worker for unshare_expr_without_location. */
955 prune_expr_location (tree
*tp
, int *walk_subtrees
, void *)
958 SET_EXPR_LOCATION (*tp
, UNKNOWN_LOCATION
);
964 /* Similar to unshare_expr but also prune all expression locations
968 unshare_expr_without_location (tree expr
)
970 walk_tree (&expr
, mostly_copy_tree_r
, NULL
, NULL
);
972 walk_tree (&expr
, prune_expr_location
, NULL
, NULL
);
976 /* WRAPPER is a code such as BIND_EXPR or CLEANUP_POINT_EXPR which can both
977 contain statements and have a value. Assign its value to a temporary
978 and give it void_type_node. Return the temporary, or NULL_TREE if
979 WRAPPER was already void. */
982 voidify_wrapper_expr (tree wrapper
, tree temp
)
984 tree type
= TREE_TYPE (wrapper
);
985 if (type
&& !VOID_TYPE_P (type
))
989 /* Set p to point to the body of the wrapper. Loop until we find
990 something that isn't a wrapper. */
991 for (p
= &wrapper
; p
&& *p
; )
993 switch (TREE_CODE (*p
))
996 TREE_SIDE_EFFECTS (*p
) = 1;
997 TREE_TYPE (*p
) = void_type_node
;
998 /* For a BIND_EXPR, the body is operand 1. */
999 p
= &BIND_EXPR_BODY (*p
);
1002 case CLEANUP_POINT_EXPR
:
1003 case TRY_FINALLY_EXPR
:
1004 case TRY_CATCH_EXPR
:
1005 TREE_SIDE_EFFECTS (*p
) = 1;
1006 TREE_TYPE (*p
) = void_type_node
;
1007 p
= &TREE_OPERAND (*p
, 0);
1010 case STATEMENT_LIST
:
1012 tree_stmt_iterator i
= tsi_last (*p
);
1013 TREE_SIDE_EFFECTS (*p
) = 1;
1014 TREE_TYPE (*p
) = void_type_node
;
1015 p
= tsi_end_p (i
) ? NULL
: tsi_stmt_ptr (i
);
1020 /* Advance to the last statement. Set all container types to
1022 for (; TREE_CODE (*p
) == COMPOUND_EXPR
; p
= &TREE_OPERAND (*p
, 1))
1024 TREE_SIDE_EFFECTS (*p
) = 1;
1025 TREE_TYPE (*p
) = void_type_node
;
1029 case TRANSACTION_EXPR
:
1030 TREE_SIDE_EFFECTS (*p
) = 1;
1031 TREE_TYPE (*p
) = void_type_node
;
1032 p
= &TRANSACTION_EXPR_BODY (*p
);
1036 /* Assume that any tree upon which voidify_wrapper_expr is
1037 directly called is a wrapper, and that its body is op0. */
1040 TREE_SIDE_EFFECTS (*p
) = 1;
1041 TREE_TYPE (*p
) = void_type_node
;
1042 p
= &TREE_OPERAND (*p
, 0);
1050 if (p
== NULL
|| IS_EMPTY_STMT (*p
))
1054 /* The wrapper is on the RHS of an assignment that we're pushing
1056 gcc_assert (TREE_CODE (temp
) == INIT_EXPR
1057 || TREE_CODE (temp
) == MODIFY_EXPR
);
1058 TREE_OPERAND (temp
, 1) = *p
;
1063 temp
= create_tmp_var (type
, "retval");
1064 *p
= build2 (INIT_EXPR
, type
, temp
, *p
);
1073 /* Prepare calls to builtins to SAVE and RESTORE the stack as well as
1074 a temporary through which they communicate. */
1077 build_stack_save_restore (gcall
**save
, gcall
**restore
)
1081 *save
= gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_SAVE
), 0);
1082 tmp_var
= create_tmp_var (ptr_type_node
, "saved_stack");
1083 gimple_call_set_lhs (*save
, tmp_var
);
1086 = gimple_build_call (builtin_decl_implicit (BUILT_IN_STACK_RESTORE
),
1090 /* Gimplify a BIND_EXPR. Just voidify and recurse. */
1092 static enum gimplify_status
1093 gimplify_bind_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1095 tree bind_expr
= *expr_p
;
1096 bool old_keep_stack
= gimplify_ctxp
->keep_stack
;
1097 bool old_save_stack
= gimplify_ctxp
->save_stack
;
1100 gimple_seq body
, cleanup
;
1102 location_t start_locus
= 0, end_locus
= 0;
1103 tree ret_clauses
= NULL
;
1105 tree temp
= voidify_wrapper_expr (bind_expr
, NULL
);
1107 /* Mark variables seen in this bind expr. */
1108 for (t
= BIND_EXPR_VARS (bind_expr
); t
; t
= DECL_CHAIN (t
))
1110 if (TREE_CODE (t
) == VAR_DECL
)
1112 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
1114 /* Mark variable as local. */
1115 if (ctx
&& ctx
->region_type
!= ORT_NONE
&& !DECL_EXTERNAL (t
)
1116 && (! DECL_SEEN_IN_BIND_EXPR_P (t
)
1117 || splay_tree_lookup (ctx
->variables
,
1118 (splay_tree_key
) t
) == NULL
))
1120 if (ctx
->region_type
== ORT_SIMD
1121 && TREE_ADDRESSABLE (t
)
1122 && !TREE_STATIC (t
))
1123 omp_add_variable (ctx
, t
, GOVD_PRIVATE
| GOVD_SEEN
);
1125 omp_add_variable (ctx
, t
, GOVD_LOCAL
| GOVD_SEEN
);
1128 DECL_SEEN_IN_BIND_EXPR_P (t
) = 1;
1130 if (DECL_HARD_REGISTER (t
) && !is_global_var (t
) && cfun
)
1131 cfun
->has_local_explicit_reg_vars
= true;
1134 /* Preliminarily mark non-addressed complex variables as eligible
1135 for promotion to gimple registers. We'll transform their uses
1137 if ((TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
1138 || TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1139 && !TREE_THIS_VOLATILE (t
)
1140 && (TREE_CODE (t
) == VAR_DECL
&& !DECL_HARD_REGISTER (t
))
1141 && !needs_to_live_in_memory (t
))
1142 DECL_GIMPLE_REG_P (t
) = 1;
1145 bind_stmt
= gimple_build_bind (BIND_EXPR_VARS (bind_expr
), NULL
,
1146 BIND_EXPR_BLOCK (bind_expr
));
1147 gimple_push_bind_expr (bind_stmt
);
1149 gimplify_ctxp
->keep_stack
= false;
1150 gimplify_ctxp
->save_stack
= false;
1152 /* Gimplify the body into the GIMPLE_BIND tuple's body. */
1154 gimplify_stmt (&BIND_EXPR_BODY (bind_expr
), &body
);
1155 gimple_bind_set_body (bind_stmt
, body
);
1157 /* Source location wise, the cleanup code (stack_restore and clobbers)
1158 belongs to the end of the block, so propagate what we have. The
1159 stack_save operation belongs to the beginning of block, which we can
1160 infer from the bind_expr directly if the block has no explicit
1162 if (BIND_EXPR_BLOCK (bind_expr
))
1164 end_locus
= BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr
));
1165 start_locus
= BLOCK_SOURCE_LOCATION (BIND_EXPR_BLOCK (bind_expr
));
1167 if (start_locus
== 0)
1168 start_locus
= EXPR_LOCATION (bind_expr
);
1173 /* If the code both contains VLAs and calls alloca, then we cannot reclaim
1174 the stack space allocated to the VLAs. */
1175 if (gimplify_ctxp
->save_stack
&& !gimplify_ctxp
->keep_stack
)
1177 gcall
*stack_restore
;
1179 /* Save stack on entry and restore it on exit. Add a try_finally
1180 block to achieve this. */
1181 build_stack_save_restore (&stack_save
, &stack_restore
);
1183 gimple_set_location (stack_save
, start_locus
);
1184 gimple_set_location (stack_restore
, end_locus
);
1186 gimplify_seq_add_stmt (&cleanup
, stack_restore
);
1189 /* Add clobbers for all variables that go out of scope. */
1190 for (t
= BIND_EXPR_VARS (bind_expr
); t
; t
= DECL_CHAIN (t
))
1192 if (TREE_CODE (t
) == VAR_DECL
1193 && !is_global_var (t
)
1194 && DECL_CONTEXT (t
) == current_function_decl
1195 && !DECL_HARD_REGISTER (t
)
1196 && !TREE_THIS_VOLATILE (t
)
1197 && !DECL_HAS_VALUE_EXPR_P (t
)
1198 /* Only care for variables that have to be in memory. Others
1199 will be rewritten into SSA names, hence moved to the top-level. */
1200 && !is_gimple_reg (t
)
1201 && flag_stack_reuse
!= SR_NONE
)
1203 tree clobber
= build_constructor (TREE_TYPE (t
), NULL
);
1204 gimple
*clobber_stmt
;
1205 TREE_THIS_VOLATILE (clobber
) = 1;
1206 clobber_stmt
= gimple_build_assign (t
, clobber
);
1207 gimple_set_location (clobber_stmt
, end_locus
);
1208 gimplify_seq_add_stmt (&cleanup
, clobber_stmt
);
1210 if (flag_openacc
&& oacc_declare_returns
!= NULL
)
1212 tree
*c
= oacc_declare_returns
->get (t
);
1216 OMP_CLAUSE_CHAIN (*c
) = ret_clauses
;
1220 oacc_declare_returns
->remove (t
);
1222 if (oacc_declare_returns
->elements () == 0)
1224 delete oacc_declare_returns
;
1225 oacc_declare_returns
= NULL
;
1235 gimple_stmt_iterator si
= gsi_start (cleanup
);
1237 stmt
= gimple_build_omp_target (NULL
, GF_OMP_TARGET_KIND_OACC_DECLARE
,
1239 gsi_insert_seq_before_without_update (&si
, stmt
, GSI_NEW_STMT
);
1245 gimple_seq new_body
;
1248 gs
= gimple_build_try (gimple_bind_body (bind_stmt
), cleanup
,
1249 GIMPLE_TRY_FINALLY
);
1252 gimplify_seq_add_stmt (&new_body
, stack_save
);
1253 gimplify_seq_add_stmt (&new_body
, gs
);
1254 gimple_bind_set_body (bind_stmt
, new_body
);
1257 /* keep_stack propagates all the way up to the outermost BIND_EXPR. */
1258 if (!gimplify_ctxp
->keep_stack
)
1259 gimplify_ctxp
->keep_stack
= old_keep_stack
;
1260 gimplify_ctxp
->save_stack
= old_save_stack
;
1262 gimple_pop_bind_expr ();
1264 gimplify_seq_add_stmt (pre_p
, bind_stmt
);
1272 *expr_p
= NULL_TREE
;
1276 /* Gimplify a RETURN_EXPR. If the expression to be returned is not a
1277 GIMPLE value, it is assigned to a new temporary and the statement is
1278 re-written to return the temporary.
1280 PRE_P points to the sequence where side effects that must happen before
1281 STMT should be stored. */
1283 static enum gimplify_status
1284 gimplify_return_expr (tree stmt
, gimple_seq
*pre_p
)
1287 tree ret_expr
= TREE_OPERAND (stmt
, 0);
1288 tree result_decl
, result
;
1290 if (ret_expr
== error_mark_node
)
1293 /* Implicit _Cilk_sync must be inserted right before any return statement
1294 if there is a _Cilk_spawn in the function. If the user has provided a
1295 _Cilk_sync, the optimizer should remove this duplicate one. */
1296 if (fn_contains_cilk_spawn_p (cfun
))
1298 tree impl_sync
= build0 (CILK_SYNC_STMT
, void_type_node
);
1299 gimplify_and_add (impl_sync
, pre_p
);
1303 || TREE_CODE (ret_expr
) == RESULT_DECL
1304 || ret_expr
== error_mark_node
)
1306 greturn
*ret
= gimple_build_return (ret_expr
);
1307 gimple_set_no_warning (ret
, TREE_NO_WARNING (stmt
));
1308 gimplify_seq_add_stmt (pre_p
, ret
);
1312 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
1313 result_decl
= NULL_TREE
;
1316 result_decl
= TREE_OPERAND (ret_expr
, 0);
1318 /* See through a return by reference. */
1319 if (TREE_CODE (result_decl
) == INDIRECT_REF
)
1320 result_decl
= TREE_OPERAND (result_decl
, 0);
1322 gcc_assert ((TREE_CODE (ret_expr
) == MODIFY_EXPR
1323 || TREE_CODE (ret_expr
) == INIT_EXPR
)
1324 && TREE_CODE (result_decl
) == RESULT_DECL
);
1327 /* If aggregate_value_p is true, then we can return the bare RESULT_DECL.
1328 Recall that aggregate_value_p is FALSE for any aggregate type that is
1329 returned in registers. If we're returning values in registers, then
1330 we don't want to extend the lifetime of the RESULT_DECL, particularly
1331 across another call. In addition, for those aggregates for which
1332 hard_function_value generates a PARALLEL, we'll die during normal
1333 expansion of structure assignments; there's special code in expand_return
1334 to handle this case that does not exist in expand_expr. */
1337 else if (aggregate_value_p (result_decl
, TREE_TYPE (current_function_decl
)))
1339 if (TREE_CODE (DECL_SIZE (result_decl
)) != INTEGER_CST
)
1341 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (result_decl
)))
1342 gimplify_type_sizes (TREE_TYPE (result_decl
), pre_p
);
1343 /* Note that we don't use gimplify_vla_decl because the RESULT_DECL
1344 should be effectively allocated by the caller, i.e. all calls to
1345 this function must be subject to the Return Slot Optimization. */
1346 gimplify_one_sizepos (&DECL_SIZE (result_decl
), pre_p
);
1347 gimplify_one_sizepos (&DECL_SIZE_UNIT (result_decl
), pre_p
);
1349 result
= result_decl
;
1351 else if (gimplify_ctxp
->return_temp
)
1352 result
= gimplify_ctxp
->return_temp
;
1355 result
= create_tmp_reg (TREE_TYPE (result_decl
));
1357 /* ??? With complex control flow (usually involving abnormal edges),
1358 we can wind up warning about an uninitialized value for this. Due
1359 to how this variable is constructed and initialized, this is never
1360 true. Give up and never warn. */
1361 TREE_NO_WARNING (result
) = 1;
1363 gimplify_ctxp
->return_temp
= result
;
1366 /* Smash the lhs of the MODIFY_EXPR to the temporary we plan to use.
1367 Then gimplify the whole thing. */
1368 if (result
!= result_decl
)
1369 TREE_OPERAND (ret_expr
, 0) = result
;
1371 gimplify_and_add (TREE_OPERAND (stmt
, 0), pre_p
);
1373 ret
= gimple_build_return (result
);
1374 gimple_set_no_warning (ret
, TREE_NO_WARNING (stmt
));
1375 gimplify_seq_add_stmt (pre_p
, ret
);
1380 /* Gimplify a variable-length array DECL. */
1383 gimplify_vla_decl (tree decl
, gimple_seq
*seq_p
)
1385 /* This is a variable-sized decl. Simplify its size and mark it
1386 for deferred expansion. */
1387 tree t
, addr
, ptr_type
;
1389 gimplify_one_sizepos (&DECL_SIZE (decl
), seq_p
);
1390 gimplify_one_sizepos (&DECL_SIZE_UNIT (decl
), seq_p
);
1392 /* Don't mess with a DECL_VALUE_EXPR set by the front-end. */
1393 if (DECL_HAS_VALUE_EXPR_P (decl
))
1396 /* All occurrences of this decl in final gimplified code will be
1397 replaced by indirection. Setting DECL_VALUE_EXPR does two
1398 things: First, it lets the rest of the gimplifier know what
1399 replacement to use. Second, it lets the debug info know
1400 where to find the value. */
1401 ptr_type
= build_pointer_type (TREE_TYPE (decl
));
1402 addr
= create_tmp_var (ptr_type
, get_name (decl
));
1403 DECL_IGNORED_P (addr
) = 0;
1404 t
= build_fold_indirect_ref (addr
);
1405 TREE_THIS_NOTRAP (t
) = 1;
1406 SET_DECL_VALUE_EXPR (decl
, t
);
1407 DECL_HAS_VALUE_EXPR_P (decl
) = 1;
1409 t
= builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN
);
1410 t
= build_call_expr (t
, 2, DECL_SIZE_UNIT (decl
),
1411 size_int (DECL_ALIGN (decl
)));
1412 /* The call has been built for a variable-sized object. */
1413 CALL_ALLOCA_FOR_VAR_P (t
) = 1;
1414 t
= fold_convert (ptr_type
, t
);
1415 t
= build2 (MODIFY_EXPR
, TREE_TYPE (addr
), addr
, t
);
1417 gimplify_and_add (t
, seq_p
);
1420 /* A helper function to be called via walk_tree. Mark all labels under *TP
1421 as being forced. To be called for DECL_INITIAL of static variables. */
1424 force_labels_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
1428 if (TREE_CODE (*tp
) == LABEL_DECL
)
1430 FORCED_LABEL (*tp
) = 1;
1431 cfun
->has_forced_label_in_static
= 1;
1437 /* Gimplify a DECL_EXPR node *STMT_P by making any necessary allocation
1438 and initialization explicit. */
1440 static enum gimplify_status
1441 gimplify_decl_expr (tree
*stmt_p
, gimple_seq
*seq_p
)
1443 tree stmt
= *stmt_p
;
1444 tree decl
= DECL_EXPR_DECL (stmt
);
1446 *stmt_p
= NULL_TREE
;
1448 if (TREE_TYPE (decl
) == error_mark_node
)
1451 if ((TREE_CODE (decl
) == TYPE_DECL
1452 || TREE_CODE (decl
) == VAR_DECL
)
1453 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (decl
)))
1455 gimplify_type_sizes (TREE_TYPE (decl
), seq_p
);
1456 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
1457 gimplify_type_sizes (TREE_TYPE (TREE_TYPE (decl
)), seq_p
);
1460 /* ??? DECL_ORIGINAL_TYPE is streamed for LTO so it needs to be gimplified
1461 in case its size expressions contain problematic nodes like CALL_EXPR. */
1462 if (TREE_CODE (decl
) == TYPE_DECL
1463 && DECL_ORIGINAL_TYPE (decl
)
1464 && !TYPE_SIZES_GIMPLIFIED (DECL_ORIGINAL_TYPE (decl
)))
1466 gimplify_type_sizes (DECL_ORIGINAL_TYPE (decl
), seq_p
);
1467 if (TREE_CODE (DECL_ORIGINAL_TYPE (decl
)) == REFERENCE_TYPE
)
1468 gimplify_type_sizes (TREE_TYPE (DECL_ORIGINAL_TYPE (decl
)), seq_p
);
1471 if (TREE_CODE (decl
) == VAR_DECL
&& !DECL_EXTERNAL (decl
))
1473 tree init
= DECL_INITIAL (decl
);
1475 if (TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
1476 || (!TREE_STATIC (decl
)
1477 && flag_stack_check
== GENERIC_STACK_CHECK
1478 && compare_tree_int (DECL_SIZE_UNIT (decl
),
1479 STACK_CHECK_MAX_VAR_SIZE
) > 0))
1480 gimplify_vla_decl (decl
, seq_p
);
1482 /* Some front ends do not explicitly declare all anonymous
1483 artificial variables. We compensate here by declaring the
1484 variables, though it would be better if the front ends would
1485 explicitly declare them. */
1486 if (!DECL_SEEN_IN_BIND_EXPR_P (decl
)
1487 && DECL_ARTIFICIAL (decl
) && DECL_NAME (decl
) == NULL_TREE
)
1488 gimple_add_tmp_var (decl
);
1490 if (init
&& init
!= error_mark_node
)
1492 if (!TREE_STATIC (decl
))
1494 DECL_INITIAL (decl
) = NULL_TREE
;
1495 init
= build2 (INIT_EXPR
, void_type_node
, decl
, init
);
1496 gimplify_and_add (init
, seq_p
);
1500 /* We must still examine initializers for static variables
1501 as they may contain a label address. */
1502 walk_tree (&init
, force_labels_r
, NULL
, NULL
);
1509 /* Gimplify a LOOP_EXPR. Normally this just involves gimplifying the body
1510 and replacing the LOOP_EXPR with goto, but if the loop contains an
1511 EXIT_EXPR, we need to append a label for it to jump to. */
1513 static enum gimplify_status
1514 gimplify_loop_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1516 tree saved_label
= gimplify_ctxp
->exit_label
;
1517 tree start_label
= create_artificial_label (UNKNOWN_LOCATION
);
1519 gimplify_seq_add_stmt (pre_p
, gimple_build_label (start_label
));
1521 gimplify_ctxp
->exit_label
= NULL_TREE
;
1523 gimplify_and_add (LOOP_EXPR_BODY (*expr_p
), pre_p
);
1525 gimplify_seq_add_stmt (pre_p
, gimple_build_goto (start_label
));
1527 if (gimplify_ctxp
->exit_label
)
1528 gimplify_seq_add_stmt (pre_p
,
1529 gimple_build_label (gimplify_ctxp
->exit_label
));
1531 gimplify_ctxp
->exit_label
= saved_label
;
1537 /* Gimplify a statement list onto a sequence. These may be created either
1538 by an enlightened front-end, or by shortcut_cond_expr. */
1540 static enum gimplify_status
1541 gimplify_statement_list (tree
*expr_p
, gimple_seq
*pre_p
)
1543 tree temp
= voidify_wrapper_expr (*expr_p
, NULL
);
1545 tree_stmt_iterator i
= tsi_start (*expr_p
);
1547 while (!tsi_end_p (i
))
1549 gimplify_stmt (tsi_stmt_ptr (i
), pre_p
);
1563 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
1566 static enum gimplify_status
1567 gimplify_switch_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1569 tree switch_expr
= *expr_p
;
1570 gimple_seq switch_body_seq
= NULL
;
1571 enum gimplify_status ret
;
1572 tree index_type
= TREE_TYPE (switch_expr
);
1573 if (index_type
== NULL_TREE
)
1574 index_type
= TREE_TYPE (SWITCH_COND (switch_expr
));
1576 ret
= gimplify_expr (&SWITCH_COND (switch_expr
), pre_p
, NULL
, is_gimple_val
,
1578 if (ret
== GS_ERROR
|| ret
== GS_UNHANDLED
)
1581 if (SWITCH_BODY (switch_expr
))
1584 vec
<tree
> saved_labels
;
1585 tree default_case
= NULL_TREE
;
1586 gswitch
*switch_stmt
;
1588 /* If someone can be bothered to fill in the labels, they can
1589 be bothered to null out the body too. */
1590 gcc_assert (!SWITCH_LABELS (switch_expr
));
1592 /* Save old labels, get new ones from body, then restore the old
1593 labels. Save all the things from the switch body to append after. */
1594 saved_labels
= gimplify_ctxp
->case_labels
;
1595 gimplify_ctxp
->case_labels
.create (8);
1597 gimplify_stmt (&SWITCH_BODY (switch_expr
), &switch_body_seq
);
1598 labels
= gimplify_ctxp
->case_labels
;
1599 gimplify_ctxp
->case_labels
= saved_labels
;
1601 preprocess_case_label_vec_for_gimple (labels
, index_type
,
1606 glabel
*new_default
;
1609 = build_case_label (NULL_TREE
, NULL_TREE
,
1610 create_artificial_label (UNKNOWN_LOCATION
));
1611 new_default
= gimple_build_label (CASE_LABEL (default_case
));
1612 gimplify_seq_add_stmt (&switch_body_seq
, new_default
);
1615 switch_stmt
= gimple_build_switch (SWITCH_COND (switch_expr
),
1616 default_case
, labels
);
1617 gimplify_seq_add_stmt (pre_p
, switch_stmt
);
1618 gimplify_seq_add_seq (pre_p
, switch_body_seq
);
1622 gcc_assert (SWITCH_LABELS (switch_expr
));
1627 /* Gimplify the CASE_LABEL_EXPR pointed to by EXPR_P. */
1629 static enum gimplify_status
1630 gimplify_case_label_expr (tree
*expr_p
, gimple_seq
*pre_p
)
1632 struct gimplify_ctx
*ctxp
;
1635 /* Invalid programs can play Duff's Device type games with, for example,
1636 #pragma omp parallel. At least in the C front end, we don't
1637 detect such invalid branches until after gimplification, in the
1638 diagnose_omp_blocks pass. */
1639 for (ctxp
= gimplify_ctxp
; ; ctxp
= ctxp
->prev_context
)
1640 if (ctxp
->case_labels
.exists ())
1643 label_stmt
= gimple_build_label (CASE_LABEL (*expr_p
));
1644 ctxp
->case_labels
.safe_push (*expr_p
);
1645 gimplify_seq_add_stmt (pre_p
, label_stmt
);
1650 /* Build a GOTO to the LABEL_DECL pointed to by LABEL_P, building it first
1654 build_and_jump (tree
*label_p
)
1656 if (label_p
== NULL
)
1657 /* If there's nowhere to jump, just fall through. */
1660 if (*label_p
== NULL_TREE
)
1662 tree label
= create_artificial_label (UNKNOWN_LOCATION
);
1666 return build1 (GOTO_EXPR
, void_type_node
, *label_p
);
1669 /* Gimplify an EXIT_EXPR by converting to a GOTO_EXPR inside a COND_EXPR.
1670 This also involves building a label to jump to and communicating it to
1671 gimplify_loop_expr through gimplify_ctxp->exit_label. */
1673 static enum gimplify_status
1674 gimplify_exit_expr (tree
*expr_p
)
1676 tree cond
= TREE_OPERAND (*expr_p
, 0);
1679 expr
= build_and_jump (&gimplify_ctxp
->exit_label
);
1680 expr
= build3 (COND_EXPR
, void_type_node
, cond
, expr
, NULL_TREE
);
1686 /* *EXPR_P is a COMPONENT_REF being used as an rvalue. If its type is
1687 different from its canonical type, wrap the whole thing inside a
1688 NOP_EXPR and force the type of the COMPONENT_REF to be the canonical
1691 The canonical type of a COMPONENT_REF is the type of the field being
1692 referenced--unless the field is a bit-field which can be read directly
1693 in a smaller mode, in which case the canonical type is the
1694 sign-appropriate type corresponding to that mode. */
1697 canonicalize_component_ref (tree
*expr_p
)
1699 tree expr
= *expr_p
;
1702 gcc_assert (TREE_CODE (expr
) == COMPONENT_REF
);
1704 if (INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
1705 type
= TREE_TYPE (get_unwidened (expr
, NULL_TREE
));
1707 type
= TREE_TYPE (TREE_OPERAND (expr
, 1));
1709 /* One could argue that all the stuff below is not necessary for
1710 the non-bitfield case and declare it a FE error if type
1711 adjustment would be needed. */
1712 if (TREE_TYPE (expr
) != type
)
1714 #ifdef ENABLE_TYPES_CHECKING
1715 tree old_type
= TREE_TYPE (expr
);
1719 /* We need to preserve qualifiers and propagate them from
1721 type_quals
= TYPE_QUALS (type
)
1722 | TYPE_QUALS (TREE_TYPE (TREE_OPERAND (expr
, 0)));
1723 if (TYPE_QUALS (type
) != type_quals
)
1724 type
= build_qualified_type (TYPE_MAIN_VARIANT (type
), type_quals
);
1726 /* Set the type of the COMPONENT_REF to the underlying type. */
1727 TREE_TYPE (expr
) = type
;
1729 #ifdef ENABLE_TYPES_CHECKING
1730 /* It is now a FE error, if the conversion from the canonical
1731 type to the original expression type is not useless. */
1732 gcc_assert (useless_type_conversion_p (old_type
, type
));
1737 /* If a NOP conversion is changing a pointer to array of foo to a pointer
1738 to foo, embed that change in the ADDR_EXPR by converting
1743 where L is the lower bound. For simplicity, only do this for constant
1745 The constraint is that the type of &array[L] is trivially convertible
1749 canonicalize_addr_expr (tree
*expr_p
)
1751 tree expr
= *expr_p
;
1752 tree addr_expr
= TREE_OPERAND (expr
, 0);
1753 tree datype
, ddatype
, pddatype
;
1755 /* We simplify only conversions from an ADDR_EXPR to a pointer type. */
1756 if (!POINTER_TYPE_P (TREE_TYPE (expr
))
1757 || TREE_CODE (addr_expr
) != ADDR_EXPR
)
1760 /* The addr_expr type should be a pointer to an array. */
1761 datype
= TREE_TYPE (TREE_TYPE (addr_expr
));
1762 if (TREE_CODE (datype
) != ARRAY_TYPE
)
1765 /* The pointer to element type shall be trivially convertible to
1766 the expression pointer type. */
1767 ddatype
= TREE_TYPE (datype
);
1768 pddatype
= build_pointer_type (ddatype
);
1769 if (!useless_type_conversion_p (TYPE_MAIN_VARIANT (TREE_TYPE (expr
)),
1773 /* The lower bound and element sizes must be constant. */
1774 if (!TYPE_SIZE_UNIT (ddatype
)
1775 || TREE_CODE (TYPE_SIZE_UNIT (ddatype
)) != INTEGER_CST
1776 || !TYPE_DOMAIN (datype
) || !TYPE_MIN_VALUE (TYPE_DOMAIN (datype
))
1777 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (datype
))) != INTEGER_CST
)
1780 /* All checks succeeded. Build a new node to merge the cast. */
1781 *expr_p
= build4 (ARRAY_REF
, ddatype
, TREE_OPERAND (addr_expr
, 0),
1782 TYPE_MIN_VALUE (TYPE_DOMAIN (datype
)),
1783 NULL_TREE
, NULL_TREE
);
1784 *expr_p
= build1 (ADDR_EXPR
, pddatype
, *expr_p
);
1786 /* We can have stripped a required restrict qualifier above. */
1787 if (!useless_type_conversion_p (TREE_TYPE (expr
), TREE_TYPE (*expr_p
)))
1788 *expr_p
= fold_convert (TREE_TYPE (expr
), *expr_p
);
1791 /* *EXPR_P is a NOP_EXPR or CONVERT_EXPR. Remove it and/or other conversions
1792 underneath as appropriate. */
1794 static enum gimplify_status
1795 gimplify_conversion (tree
*expr_p
)
1797 location_t loc
= EXPR_LOCATION (*expr_p
);
1798 gcc_assert (CONVERT_EXPR_P (*expr_p
));
1800 /* Then strip away all but the outermost conversion. */
1801 STRIP_SIGN_NOPS (TREE_OPERAND (*expr_p
, 0));
1803 /* And remove the outermost conversion if it's useless. */
1804 if (tree_ssa_useless_type_conversion (*expr_p
))
1805 *expr_p
= TREE_OPERAND (*expr_p
, 0);
1807 /* If we still have a conversion at the toplevel,
1808 then canonicalize some constructs. */
1809 if (CONVERT_EXPR_P (*expr_p
))
1811 tree sub
= TREE_OPERAND (*expr_p
, 0);
1813 /* If a NOP conversion is changing the type of a COMPONENT_REF
1814 expression, then canonicalize its type now in order to expose more
1815 redundant conversions. */
1816 if (TREE_CODE (sub
) == COMPONENT_REF
)
1817 canonicalize_component_ref (&TREE_OPERAND (*expr_p
, 0));
1819 /* If a NOP conversion is changing a pointer to array of foo
1820 to a pointer to foo, embed that change in the ADDR_EXPR. */
1821 else if (TREE_CODE (sub
) == ADDR_EXPR
)
1822 canonicalize_addr_expr (expr_p
);
1825 /* If we have a conversion to a non-register type force the
1826 use of a VIEW_CONVERT_EXPR instead. */
1827 if (CONVERT_EXPR_P (*expr_p
) && !is_gimple_reg_type (TREE_TYPE (*expr_p
)))
1828 *expr_p
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, TREE_TYPE (*expr_p
),
1829 TREE_OPERAND (*expr_p
, 0));
1831 /* Canonicalize CONVERT_EXPR to NOP_EXPR. */
1832 if (TREE_CODE (*expr_p
) == CONVERT_EXPR
)
1833 TREE_SET_CODE (*expr_p
, NOP_EXPR
);
1838 /* Nonlocal VLAs seen in the current function. */
1839 static hash_set
<tree
> *nonlocal_vlas
;
1841 /* The VAR_DECLs created for nonlocal VLAs for debug info purposes. */
1842 static tree nonlocal_vla_vars
;
1844 /* Gimplify a VAR_DECL or PARM_DECL. Return GS_OK if we expanded a
1845 DECL_VALUE_EXPR, and it's worth re-examining things. */
1847 static enum gimplify_status
1848 gimplify_var_or_parm_decl (tree
*expr_p
)
1850 tree decl
= *expr_p
;
1852 /* ??? If this is a local variable, and it has not been seen in any
1853 outer BIND_EXPR, then it's probably the result of a duplicate
1854 declaration, for which we've already issued an error. It would
1855 be really nice if the front end wouldn't leak these at all.
1856 Currently the only known culprit is C++ destructors, as seen
1857 in g++.old-deja/g++.jason/binding.C. */
1858 if (TREE_CODE (decl
) == VAR_DECL
1859 && !DECL_SEEN_IN_BIND_EXPR_P (decl
)
1860 && !TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
)
1861 && decl_function_context (decl
) == current_function_decl
)
1863 gcc_assert (seen_error ());
1867 /* When within an OMP context, notice uses of variables. */
1868 if (gimplify_omp_ctxp
&& omp_notice_variable (gimplify_omp_ctxp
, decl
, true))
1871 /* If the decl is an alias for another expression, substitute it now. */
1872 if (DECL_HAS_VALUE_EXPR_P (decl
))
1874 tree value_expr
= DECL_VALUE_EXPR (decl
);
1876 /* For referenced nonlocal VLAs add a decl for debugging purposes
1877 to the current function. */
1878 if (TREE_CODE (decl
) == VAR_DECL
1879 && TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
1880 && nonlocal_vlas
!= NULL
1881 && TREE_CODE (value_expr
) == INDIRECT_REF
1882 && TREE_CODE (TREE_OPERAND (value_expr
, 0)) == VAR_DECL
1883 && decl_function_context (decl
) != current_function_decl
)
1885 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
1887 && (ctx
->region_type
== ORT_WORKSHARE
1888 || ctx
->region_type
== ORT_SIMD
1889 || ctx
->region_type
== ORT_ACC
))
1890 ctx
= ctx
->outer_context
;
1891 if (!ctx
&& !nonlocal_vlas
->add (decl
))
1893 tree copy
= copy_node (decl
);
1895 lang_hooks
.dup_lang_specific_decl (copy
);
1896 SET_DECL_RTL (copy
, 0);
1897 TREE_USED (copy
) = 1;
1898 DECL_CHAIN (copy
) = nonlocal_vla_vars
;
1899 nonlocal_vla_vars
= copy
;
1900 SET_DECL_VALUE_EXPR (copy
, unshare_expr (value_expr
));
1901 DECL_HAS_VALUE_EXPR_P (copy
) = 1;
1905 *expr_p
= unshare_expr (value_expr
);
1912 /* Recalculate the value of the TREE_SIDE_EFFECTS flag for T. */
1915 recalculate_side_effects (tree t
)
1917 enum tree_code code
= TREE_CODE (t
);
1918 int len
= TREE_OPERAND_LENGTH (t
);
1921 switch (TREE_CODE_CLASS (code
))
1923 case tcc_expression
:
1929 case PREDECREMENT_EXPR
:
1930 case PREINCREMENT_EXPR
:
1931 case POSTDECREMENT_EXPR
:
1932 case POSTINCREMENT_EXPR
:
1933 /* All of these have side-effects, no matter what their
1942 case tcc_comparison
: /* a comparison expression */
1943 case tcc_unary
: /* a unary arithmetic expression */
1944 case tcc_binary
: /* a binary arithmetic expression */
1945 case tcc_reference
: /* a reference */
1946 case tcc_vl_exp
: /* a function call */
1947 TREE_SIDE_EFFECTS (t
) = TREE_THIS_VOLATILE (t
);
1948 for (i
= 0; i
< len
; ++i
)
1950 tree op
= TREE_OPERAND (t
, i
);
1951 if (op
&& TREE_SIDE_EFFECTS (op
))
1952 TREE_SIDE_EFFECTS (t
) = 1;
1957 /* No side-effects. */
1965 /* Gimplify the COMPONENT_REF, ARRAY_REF, REALPART_EXPR or IMAGPART_EXPR
1969 : min_lval '[' val ']'
1971 | compound_lval '[' val ']'
1972 | compound_lval '.' ID
1974 This is not part of the original SIMPLE definition, which separates
1975 array and member references, but it seems reasonable to handle them
1976 together. Also, this way we don't run into problems with union
1977 aliasing; gcc requires that for accesses through a union to alias, the
1978 union reference must be explicit, which was not always the case when we
1979 were splitting up array and member refs.
1981 PRE_P points to the sequence where side effects that must happen before
1982 *EXPR_P should be stored.
1984 POST_P points to the sequence where side effects that must happen after
1985 *EXPR_P should be stored. */
1987 static enum gimplify_status
1988 gimplify_compound_lval (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
1989 fallback_t fallback
)
1992 enum gimplify_status ret
= GS_ALL_DONE
, tret
;
1994 location_t loc
= EXPR_LOCATION (*expr_p
);
1995 tree expr
= *expr_p
;
1997 /* Create a stack of the subexpressions so later we can walk them in
1998 order from inner to outer. */
1999 auto_vec
<tree
, 10> expr_stack
;
2001 /* We can handle anything that get_inner_reference can deal with. */
2002 for (p
= expr_p
; ; p
= &TREE_OPERAND (*p
, 0))
2005 /* Fold INDIRECT_REFs now to turn them into ARRAY_REFs. */
2006 if (TREE_CODE (*p
) == INDIRECT_REF
)
2007 *p
= fold_indirect_ref_loc (loc
, *p
);
2009 if (handled_component_p (*p
))
2011 /* Expand DECL_VALUE_EXPR now. In some cases that may expose
2012 additional COMPONENT_REFs. */
2013 else if ((TREE_CODE (*p
) == VAR_DECL
|| TREE_CODE (*p
) == PARM_DECL
)
2014 && gimplify_var_or_parm_decl (p
) == GS_OK
)
2019 expr_stack
.safe_push (*p
);
2022 gcc_assert (expr_stack
.length ());
2024 /* Now EXPR_STACK is a stack of pointers to all the refs we've
2025 walked through and P points to the innermost expression.
2027 Java requires that we elaborated nodes in source order. That
2028 means we must gimplify the inner expression followed by each of
2029 the indices, in order. But we can't gimplify the inner
2030 expression until we deal with any variable bounds, sizes, or
2031 positions in order to deal with PLACEHOLDER_EXPRs.
2033 So we do this in three steps. First we deal with the annotations
2034 for any variables in the components, then we gimplify the base,
2035 then we gimplify any indices, from left to right. */
2036 for (i
= expr_stack
.length () - 1; i
>= 0; i
--)
2038 tree t
= expr_stack
[i
];
2040 if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
2042 /* Gimplify the low bound and element type size and put them into
2043 the ARRAY_REF. If these values are set, they have already been
2045 if (TREE_OPERAND (t
, 2) == NULL_TREE
)
2047 tree low
= unshare_expr (array_ref_low_bound (t
));
2048 if (!is_gimple_min_invariant (low
))
2050 TREE_OPERAND (t
, 2) = low
;
2051 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
,
2052 post_p
, is_gimple_reg
,
2054 ret
= MIN (ret
, tret
);
2059 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
, post_p
,
2060 is_gimple_reg
, fb_rvalue
);
2061 ret
= MIN (ret
, tret
);
2064 if (TREE_OPERAND (t
, 3) == NULL_TREE
)
2066 tree elmt_type
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0)));
2067 tree elmt_size
= unshare_expr (array_ref_element_size (t
));
2068 tree factor
= size_int (TYPE_ALIGN_UNIT (elmt_type
));
2070 /* Divide the element size by the alignment of the element
2073 = size_binop_loc (loc
, EXACT_DIV_EXPR
, elmt_size
, factor
);
2075 if (!is_gimple_min_invariant (elmt_size
))
2077 TREE_OPERAND (t
, 3) = elmt_size
;
2078 tret
= gimplify_expr (&TREE_OPERAND (t
, 3), pre_p
,
2079 post_p
, is_gimple_reg
,
2081 ret
= MIN (ret
, tret
);
2086 tret
= gimplify_expr (&TREE_OPERAND (t
, 3), pre_p
, post_p
,
2087 is_gimple_reg
, fb_rvalue
);
2088 ret
= MIN (ret
, tret
);
2091 else if (TREE_CODE (t
) == COMPONENT_REF
)
2093 /* Set the field offset into T and gimplify it. */
2094 if (TREE_OPERAND (t
, 2) == NULL_TREE
)
2096 tree offset
= unshare_expr (component_ref_field_offset (t
));
2097 tree field
= TREE_OPERAND (t
, 1);
2099 = size_int (DECL_OFFSET_ALIGN (field
) / BITS_PER_UNIT
);
2101 /* Divide the offset by its alignment. */
2102 offset
= size_binop_loc (loc
, EXACT_DIV_EXPR
, offset
, factor
);
2104 if (!is_gimple_min_invariant (offset
))
2106 TREE_OPERAND (t
, 2) = offset
;
2107 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
,
2108 post_p
, is_gimple_reg
,
2110 ret
= MIN (ret
, tret
);
2115 tret
= gimplify_expr (&TREE_OPERAND (t
, 2), pre_p
, post_p
,
2116 is_gimple_reg
, fb_rvalue
);
2117 ret
= MIN (ret
, tret
);
2122 /* Step 2 is to gimplify the base expression. Make sure lvalue is set
2123 so as to match the min_lval predicate. Failure to do so may result
2124 in the creation of large aggregate temporaries. */
2125 tret
= gimplify_expr (p
, pre_p
, post_p
, is_gimple_min_lval
,
2126 fallback
| fb_lvalue
);
2127 ret
= MIN (ret
, tret
);
2129 /* And finally, the indices and operands of ARRAY_REF. During this
2130 loop we also remove any useless conversions. */
2131 for (; expr_stack
.length () > 0; )
2133 tree t
= expr_stack
.pop ();
2135 if (TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
2137 /* Gimplify the dimension. */
2138 if (!is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
2140 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), pre_p
, post_p
,
2141 is_gimple_val
, fb_rvalue
);
2142 ret
= MIN (ret
, tret
);
2146 STRIP_USELESS_TYPE_CONVERSION (TREE_OPERAND (t
, 0));
2148 /* The innermost expression P may have originally had
2149 TREE_SIDE_EFFECTS set which would have caused all the outer
2150 expressions in *EXPR_P leading to P to also have had
2151 TREE_SIDE_EFFECTS set. */
2152 recalculate_side_effects (t
);
2155 /* If the outermost expression is a COMPONENT_REF, canonicalize its type. */
2156 if ((fallback
& fb_rvalue
) && TREE_CODE (*expr_p
) == COMPONENT_REF
)
2158 canonicalize_component_ref (expr_p
);
2161 expr_stack
.release ();
2163 gcc_assert (*expr_p
== expr
|| ret
!= GS_ALL_DONE
);
2168 /* Gimplify the self modifying expression pointed to by EXPR_P
2171 PRE_P points to the list where side effects that must happen before
2172 *EXPR_P should be stored.
2174 POST_P points to the list where side effects that must happen after
2175 *EXPR_P should be stored.
2177 WANT_VALUE is nonzero iff we want to use the value of this expression
2178 in another expression.
2180 ARITH_TYPE is the type the computation should be performed in. */
2182 enum gimplify_status
2183 gimplify_self_mod_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
2184 bool want_value
, tree arith_type
)
2186 enum tree_code code
;
2187 tree lhs
, lvalue
, rhs
, t1
;
2188 gimple_seq post
= NULL
, *orig_post_p
= post_p
;
2190 enum tree_code arith_code
;
2191 enum gimplify_status ret
;
2192 location_t loc
= EXPR_LOCATION (*expr_p
);
2194 code
= TREE_CODE (*expr_p
);
2196 gcc_assert (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
2197 || code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
);
2199 /* Prefix or postfix? */
2200 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
2201 /* Faster to treat as prefix if result is not used. */
2202 postfix
= want_value
;
2206 /* For postfix, make sure the inner expression's post side effects
2207 are executed after side effects from this expression. */
2211 /* Add or subtract? */
2212 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2213 arith_code
= PLUS_EXPR
;
2215 arith_code
= MINUS_EXPR
;
2217 /* Gimplify the LHS into a GIMPLE lvalue. */
2218 lvalue
= TREE_OPERAND (*expr_p
, 0);
2219 ret
= gimplify_expr (&lvalue
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
2220 if (ret
== GS_ERROR
)
2223 /* Extract the operands to the arithmetic operation. */
2225 rhs
= TREE_OPERAND (*expr_p
, 1);
2227 /* For postfix operator, we evaluate the LHS to an rvalue and then use
2228 that as the result value and in the postqueue operation. */
2231 ret
= gimplify_expr (&lhs
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
2232 if (ret
== GS_ERROR
)
2235 lhs
= get_initialized_tmp_var (lhs
, pre_p
, NULL
);
2238 /* For POINTERs increment, use POINTER_PLUS_EXPR. */
2239 if (POINTER_TYPE_P (TREE_TYPE (lhs
)))
2241 rhs
= convert_to_ptrofftype_loc (loc
, rhs
);
2242 if (arith_code
== MINUS_EXPR
)
2243 rhs
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
2244 t1
= fold_build2 (POINTER_PLUS_EXPR
, TREE_TYPE (*expr_p
), lhs
, rhs
);
2247 t1
= fold_convert (TREE_TYPE (*expr_p
),
2248 fold_build2 (arith_code
, arith_type
,
2249 fold_convert (arith_type
, lhs
),
2250 fold_convert (arith_type
, rhs
)));
2254 gimplify_assign (lvalue
, t1
, pre_p
);
2255 gimplify_seq_add_seq (orig_post_p
, post
);
2261 *expr_p
= build2 (MODIFY_EXPR
, TREE_TYPE (lvalue
), lvalue
, t1
);
2266 /* If *EXPR_P has a variable sized type, wrap it in a WITH_SIZE_EXPR. */
2269 maybe_with_size_expr (tree
*expr_p
)
2271 tree expr
= *expr_p
;
2272 tree type
= TREE_TYPE (expr
);
2275 /* If we've already wrapped this or the type is error_mark_node, we can't do
2277 if (TREE_CODE (expr
) == WITH_SIZE_EXPR
2278 || type
== error_mark_node
)
2281 /* If the size isn't known or is a constant, we have nothing to do. */
2282 size
= TYPE_SIZE_UNIT (type
);
2283 if (!size
|| TREE_CODE (size
) == INTEGER_CST
)
2286 /* Otherwise, make a WITH_SIZE_EXPR. */
2287 size
= unshare_expr (size
);
2288 size
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, expr
);
2289 *expr_p
= build2 (WITH_SIZE_EXPR
, type
, expr
, size
);
2292 /* Helper for gimplify_call_expr. Gimplify a single argument *ARG_P
2293 Store any side-effects in PRE_P. CALL_LOCATION is the location of
2294 the CALL_EXPR. If ALLOW_SSA is set the actual parameter may be
2295 gimplified to an SSA name. */
2297 enum gimplify_status
2298 gimplify_arg (tree
*arg_p
, gimple_seq
*pre_p
, location_t call_location
,
2301 bool (*test
) (tree
);
2304 /* In general, we allow lvalues for function arguments to avoid
2305 extra overhead of copying large aggregates out of even larger
2306 aggregates into temporaries only to copy the temporaries to
2307 the argument list. Make optimizers happy by pulling out to
2308 temporaries those types that fit in registers. */
2309 if (is_gimple_reg_type (TREE_TYPE (*arg_p
)))
2310 test
= is_gimple_val
, fb
= fb_rvalue
;
2313 test
= is_gimple_lvalue
, fb
= fb_either
;
2314 /* Also strip a TARGET_EXPR that would force an extra copy. */
2315 if (TREE_CODE (*arg_p
) == TARGET_EXPR
)
2317 tree init
= TARGET_EXPR_INITIAL (*arg_p
);
2319 && !VOID_TYPE_P (TREE_TYPE (init
)))
2324 /* If this is a variable sized type, we must remember the size. */
2325 maybe_with_size_expr (arg_p
);
2327 /* FIXME diagnostics: This will mess up gcc.dg/Warray-bounds.c. */
2328 /* Make sure arguments have the same location as the function call
2330 protected_set_expr_location (*arg_p
, call_location
);
2332 /* There is a sequence point before a function call. Side effects in
2333 the argument list must occur before the actual call. So, when
2334 gimplifying arguments, force gimplify_expr to use an internal
2335 post queue which is then appended to the end of PRE_P. */
2336 return gimplify_expr (arg_p
, pre_p
, NULL
, test
, fb
, allow_ssa
);
2339 /* Don't fold inside offloading or taskreg regions: it can break code by
2340 adding decl references that weren't in the source. We'll do it during
2341 omplower pass instead. */
2344 maybe_fold_stmt (gimple_stmt_iterator
*gsi
)
2346 struct gimplify_omp_ctx
*ctx
;
2347 for (ctx
= gimplify_omp_ctxp
; ctx
; ctx
= ctx
->outer_context
)
2348 if ((ctx
->region_type
& (ORT_TARGET
| ORT_PARALLEL
| ORT_TASK
)) != 0)
2350 return fold_stmt (gsi
);
2353 /* Gimplify the CALL_EXPR node *EXPR_P into the GIMPLE sequence PRE_P.
2354 WANT_VALUE is true if the result of the call is desired. */
2356 static enum gimplify_status
2357 gimplify_call_expr (tree
*expr_p
, gimple_seq
*pre_p
, bool want_value
)
2359 tree fndecl
, parms
, p
, fnptrtype
;
2360 enum gimplify_status ret
;
2363 bool builtin_va_start_p
= false;
2364 location_t loc
= EXPR_LOCATION (*expr_p
);
2366 gcc_assert (TREE_CODE (*expr_p
) == CALL_EXPR
);
2368 /* For reliable diagnostics during inlining, it is necessary that
2369 every call_expr be annotated with file and line. */
2370 if (! EXPR_HAS_LOCATION (*expr_p
))
2371 SET_EXPR_LOCATION (*expr_p
, input_location
);
2373 /* Gimplify internal functions created in the FEs. */
2374 if (CALL_EXPR_FN (*expr_p
) == NULL_TREE
)
2379 nargs
= call_expr_nargs (*expr_p
);
2380 enum internal_fn ifn
= CALL_EXPR_IFN (*expr_p
);
2381 auto_vec
<tree
> vargs (nargs
);
2383 for (i
= 0; i
< nargs
; i
++)
2385 gimplify_arg (&CALL_EXPR_ARG (*expr_p
, i
), pre_p
,
2386 EXPR_LOCATION (*expr_p
));
2387 vargs
.quick_push (CALL_EXPR_ARG (*expr_p
, i
));
2389 gimple
*call
= gimple_build_call_internal_vec (ifn
, vargs
);
2390 gimplify_seq_add_stmt (pre_p
, call
);
2394 /* This may be a call to a builtin function.
2396 Builtin function calls may be transformed into different
2397 (and more efficient) builtin function calls under certain
2398 circumstances. Unfortunately, gimplification can muck things
2399 up enough that the builtin expanders are not aware that certain
2400 transformations are still valid.
2402 So we attempt transformation/gimplification of the call before
2403 we gimplify the CALL_EXPR. At this time we do not manage to
2404 transform all calls in the same manner as the expanders do, but
2405 we do transform most of them. */
2406 fndecl
= get_callee_fndecl (*expr_p
);
2408 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
2409 switch (DECL_FUNCTION_CODE (fndecl
))
2411 case BUILT_IN_ALLOCA
:
2412 case BUILT_IN_ALLOCA_WITH_ALIGN
:
2413 /* If the call has been built for a variable-sized object, then we
2414 want to restore the stack level when the enclosing BIND_EXPR is
2415 exited to reclaim the allocated space; otherwise, we precisely
2416 need to do the opposite and preserve the latest stack level. */
2417 if (CALL_ALLOCA_FOR_VAR_P (*expr_p
))
2418 gimplify_ctxp
->save_stack
= true;
2420 gimplify_ctxp
->keep_stack
= true;
2423 case BUILT_IN_VA_START
:
2425 builtin_va_start_p
= TRUE
;
2426 if (call_expr_nargs (*expr_p
) < 2)
2428 error ("too few arguments to function %<va_start%>");
2429 *expr_p
= build_empty_stmt (EXPR_LOCATION (*expr_p
));
2433 if (fold_builtin_next_arg (*expr_p
, true))
2435 *expr_p
= build_empty_stmt (EXPR_LOCATION (*expr_p
));
2444 if (fndecl
&& DECL_BUILT_IN (fndecl
))
2446 tree new_tree
= fold_call_expr (input_location
, *expr_p
, !want_value
);
2447 if (new_tree
&& new_tree
!= *expr_p
)
2449 /* There was a transformation of this call which computes the
2450 same value, but in a more efficient way. Return and try
2457 /* Remember the original function pointer type. */
2458 fnptrtype
= TREE_TYPE (CALL_EXPR_FN (*expr_p
));
2460 /* There is a sequence point before the call, so any side effects in
2461 the calling expression must occur before the actual call. Force
2462 gimplify_expr to use an internal post queue. */
2463 ret
= gimplify_expr (&CALL_EXPR_FN (*expr_p
), pre_p
, NULL
,
2464 is_gimple_call_addr
, fb_rvalue
);
2466 nargs
= call_expr_nargs (*expr_p
);
2468 /* Get argument types for verification. */
2469 fndecl
= get_callee_fndecl (*expr_p
);
2472 parms
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2474 parms
= TYPE_ARG_TYPES (TREE_TYPE (fnptrtype
));
2476 if (fndecl
&& DECL_ARGUMENTS (fndecl
))
2477 p
= DECL_ARGUMENTS (fndecl
);
2482 for (i
= 0; i
< nargs
&& p
; i
++, p
= TREE_CHAIN (p
))
2485 /* If the last argument is __builtin_va_arg_pack () and it is not
2486 passed as a named argument, decrease the number of CALL_EXPR
2487 arguments and set instead the CALL_EXPR_VA_ARG_PACK flag. */
2490 && TREE_CODE (CALL_EXPR_ARG (*expr_p
, nargs
- 1)) == CALL_EXPR
)
2492 tree last_arg
= CALL_EXPR_ARG (*expr_p
, nargs
- 1);
2493 tree last_arg_fndecl
= get_callee_fndecl (last_arg
);
2496 && TREE_CODE (last_arg_fndecl
) == FUNCTION_DECL
2497 && DECL_BUILT_IN_CLASS (last_arg_fndecl
) == BUILT_IN_NORMAL
2498 && DECL_FUNCTION_CODE (last_arg_fndecl
) == BUILT_IN_VA_ARG_PACK
)
2500 tree call
= *expr_p
;
2503 *expr_p
= build_call_array_loc (loc
, TREE_TYPE (call
),
2504 CALL_EXPR_FN (call
),
2505 nargs
, CALL_EXPR_ARGP (call
));
2507 /* Copy all CALL_EXPR flags, location and block, except
2508 CALL_EXPR_VA_ARG_PACK flag. */
2509 CALL_EXPR_STATIC_CHAIN (*expr_p
) = CALL_EXPR_STATIC_CHAIN (call
);
2510 CALL_EXPR_TAILCALL (*expr_p
) = CALL_EXPR_TAILCALL (call
);
2511 CALL_EXPR_RETURN_SLOT_OPT (*expr_p
)
2512 = CALL_EXPR_RETURN_SLOT_OPT (call
);
2513 CALL_FROM_THUNK_P (*expr_p
) = CALL_FROM_THUNK_P (call
);
2514 SET_EXPR_LOCATION (*expr_p
, EXPR_LOCATION (call
));
2516 /* Set CALL_EXPR_VA_ARG_PACK. */
2517 CALL_EXPR_VA_ARG_PACK (*expr_p
) = 1;
2521 /* If the call returns twice then after building the CFG the call
2522 argument computations will no longer dominate the call because
2523 we add an abnormal incoming edge to the call. So do not use SSA
2525 bool returns_twice
= call_expr_flags (*expr_p
) & ECF_RETURNS_TWICE
;
2527 /* Gimplify the function arguments. */
2530 for (i
= (PUSH_ARGS_REVERSED
? nargs
- 1 : 0);
2531 PUSH_ARGS_REVERSED
? i
>= 0 : i
< nargs
;
2532 PUSH_ARGS_REVERSED
? i
-- : i
++)
2534 enum gimplify_status t
;
2536 /* Avoid gimplifying the second argument to va_start, which needs to
2537 be the plain PARM_DECL. */
2538 if ((i
!= 1) || !builtin_va_start_p
)
2540 t
= gimplify_arg (&CALL_EXPR_ARG (*expr_p
, i
), pre_p
,
2541 EXPR_LOCATION (*expr_p
), ! returns_twice
);
2549 /* Gimplify the static chain. */
2550 if (CALL_EXPR_STATIC_CHAIN (*expr_p
))
2552 if (fndecl
&& !DECL_STATIC_CHAIN (fndecl
))
2553 CALL_EXPR_STATIC_CHAIN (*expr_p
) = NULL
;
2556 enum gimplify_status t
;
2557 t
= gimplify_arg (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
,
2558 EXPR_LOCATION (*expr_p
), ! returns_twice
);
2564 /* Verify the function result. */
2565 if (want_value
&& fndecl
2566 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fnptrtype
))))
2568 error_at (loc
, "using result of function returning %<void%>");
2572 /* Try this again in case gimplification exposed something. */
2573 if (ret
!= GS_ERROR
)
2575 tree new_tree
= fold_call_expr (input_location
, *expr_p
, !want_value
);
2577 if (new_tree
&& new_tree
!= *expr_p
)
2579 /* There was a transformation of this call which computes the
2580 same value, but in a more efficient way. Return and try
2588 *expr_p
= error_mark_node
;
2592 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
2593 decl. This allows us to eliminate redundant or useless
2594 calls to "const" functions. */
2595 if (TREE_CODE (*expr_p
) == CALL_EXPR
)
2597 int flags
= call_expr_flags (*expr_p
);
2598 if (flags
& (ECF_CONST
| ECF_PURE
)
2599 /* An infinite loop is considered a side effect. */
2600 && !(flags
& (ECF_LOOPING_CONST_OR_PURE
)))
2601 TREE_SIDE_EFFECTS (*expr_p
) = 0;
2604 /* If the value is not needed by the caller, emit a new GIMPLE_CALL
2605 and clear *EXPR_P. Otherwise, leave *EXPR_P in its gimplified
2606 form and delegate the creation of a GIMPLE_CALL to
2607 gimplify_modify_expr. This is always possible because when
2608 WANT_VALUE is true, the caller wants the result of this call into
2609 a temporary, which means that we will emit an INIT_EXPR in
2610 internal_get_tmp_var which will then be handled by
2611 gimplify_modify_expr. */
2614 /* The CALL_EXPR in *EXPR_P is already in GIMPLE form, so all we
2615 have to do is replicate it as a GIMPLE_CALL tuple. */
2616 gimple_stmt_iterator gsi
;
2617 call
= gimple_build_call_from_tree (*expr_p
);
2618 gimple_call_set_fntype (call
, TREE_TYPE (fnptrtype
));
2619 notice_special_calls (call
);
2620 gimplify_seq_add_stmt (pre_p
, call
);
2621 gsi
= gsi_last (*pre_p
);
2622 maybe_fold_stmt (&gsi
);
2623 *expr_p
= NULL_TREE
;
2626 /* Remember the original function type. */
2627 CALL_EXPR_FN (*expr_p
) = build1 (NOP_EXPR
, fnptrtype
,
2628 CALL_EXPR_FN (*expr_p
));
2633 /* Handle shortcut semantics in the predicate operand of a COND_EXPR by
2634 rewriting it into multiple COND_EXPRs, and possibly GOTO_EXPRs.
2636 TRUE_LABEL_P and FALSE_LABEL_P point to the labels to jump to if the
2637 condition is true or false, respectively. If null, we should generate
2638 our own to skip over the evaluation of this specific expression.
2640 LOCUS is the source location of the COND_EXPR.
2642 This function is the tree equivalent of do_jump.
2644 shortcut_cond_r should only be called by shortcut_cond_expr. */
2647 shortcut_cond_r (tree pred
, tree
*true_label_p
, tree
*false_label_p
,
2650 tree local_label
= NULL_TREE
;
2651 tree t
, expr
= NULL
;
2653 /* OK, it's not a simple case; we need to pull apart the COND_EXPR to
2654 retain the shortcut semantics. Just insert the gotos here;
2655 shortcut_cond_expr will append the real blocks later. */
2656 if (TREE_CODE (pred
) == TRUTH_ANDIF_EXPR
)
2658 location_t new_locus
;
2660 /* Turn if (a && b) into
2662 if (a); else goto no;
2663 if (b) goto yes; else goto no;
2666 if (false_label_p
== NULL
)
2667 false_label_p
= &local_label
;
2669 /* Keep the original source location on the first 'if'. */
2670 t
= shortcut_cond_r (TREE_OPERAND (pred
, 0), NULL
, false_label_p
, locus
);
2671 append_to_statement_list (t
, &expr
);
2673 /* Set the source location of the && on the second 'if'. */
2674 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
2675 t
= shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
, false_label_p
,
2677 append_to_statement_list (t
, &expr
);
2679 else if (TREE_CODE (pred
) == TRUTH_ORIF_EXPR
)
2681 location_t new_locus
;
2683 /* Turn if (a || b) into
2686 if (b) goto yes; else goto no;
2689 if (true_label_p
== NULL
)
2690 true_label_p
= &local_label
;
2692 /* Keep the original source location on the first 'if'. */
2693 t
= shortcut_cond_r (TREE_OPERAND (pred
, 0), true_label_p
, NULL
, locus
);
2694 append_to_statement_list (t
, &expr
);
2696 /* Set the source location of the || on the second 'if'. */
2697 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
2698 t
= shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
, false_label_p
,
2700 append_to_statement_list (t
, &expr
);
2702 else if (TREE_CODE (pred
) == COND_EXPR
2703 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred
, 1)))
2704 && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred
, 2))))
2706 location_t new_locus
;
2708 /* As long as we're messing with gotos, turn if (a ? b : c) into
2710 if (b) goto yes; else goto no;
2712 if (c) goto yes; else goto no;
2714 Don't do this if one of the arms has void type, which can happen
2715 in C++ when the arm is throw. */
2717 /* Keep the original source location on the first 'if'. Set the source
2718 location of the ? on the second 'if'. */
2719 new_locus
= EXPR_HAS_LOCATION (pred
) ? EXPR_LOCATION (pred
) : locus
;
2720 expr
= build3 (COND_EXPR
, void_type_node
, TREE_OPERAND (pred
, 0),
2721 shortcut_cond_r (TREE_OPERAND (pred
, 1), true_label_p
,
2722 false_label_p
, locus
),
2723 shortcut_cond_r (TREE_OPERAND (pred
, 2), true_label_p
,
2724 false_label_p
, new_locus
));
2728 expr
= build3 (COND_EXPR
, void_type_node
, pred
,
2729 build_and_jump (true_label_p
),
2730 build_and_jump (false_label_p
));
2731 SET_EXPR_LOCATION (expr
, locus
);
2736 t
= build1 (LABEL_EXPR
, void_type_node
, local_label
);
2737 append_to_statement_list (t
, &expr
);
2743 /* Given a conditional expression EXPR with short-circuit boolean
2744 predicates using TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR, break the
2745 predicate apart into the equivalent sequence of conditionals. */
2748 shortcut_cond_expr (tree expr
)
2750 tree pred
= TREE_OPERAND (expr
, 0);
2751 tree then_
= TREE_OPERAND (expr
, 1);
2752 tree else_
= TREE_OPERAND (expr
, 2);
2753 tree true_label
, false_label
, end_label
, t
;
2755 tree
*false_label_p
;
2756 bool emit_end
, emit_false
, jump_over_else
;
2757 bool then_se
= then_
&& TREE_SIDE_EFFECTS (then_
);
2758 bool else_se
= else_
&& TREE_SIDE_EFFECTS (else_
);
2760 /* First do simple transformations. */
2763 /* If there is no 'else', turn
2766 if (a) if (b) then c. */
2767 while (TREE_CODE (pred
) == TRUTH_ANDIF_EXPR
)
2769 /* Keep the original source location on the first 'if'. */
2770 location_t locus
= EXPR_LOC_OR_LOC (expr
, input_location
);
2771 TREE_OPERAND (expr
, 0) = TREE_OPERAND (pred
, 1);
2772 /* Set the source location of the && on the second 'if'. */
2773 if (EXPR_HAS_LOCATION (pred
))
2774 SET_EXPR_LOCATION (expr
, EXPR_LOCATION (pred
));
2775 then_
= shortcut_cond_expr (expr
);
2776 then_se
= then_
&& TREE_SIDE_EFFECTS (then_
);
2777 pred
= TREE_OPERAND (pred
, 0);
2778 expr
= build3 (COND_EXPR
, void_type_node
, pred
, then_
, NULL_TREE
);
2779 SET_EXPR_LOCATION (expr
, locus
);
2785 /* If there is no 'then', turn
2788 if (a); else if (b); else d. */
2789 while (TREE_CODE (pred
) == TRUTH_ORIF_EXPR
)
2791 /* Keep the original source location on the first 'if'. */
2792 location_t locus
= EXPR_LOC_OR_LOC (expr
, input_location
);
2793 TREE_OPERAND (expr
, 0) = TREE_OPERAND (pred
, 1);
2794 /* Set the source location of the || on the second 'if'. */
2795 if (EXPR_HAS_LOCATION (pred
))
2796 SET_EXPR_LOCATION (expr
, EXPR_LOCATION (pred
));
2797 else_
= shortcut_cond_expr (expr
);
2798 else_se
= else_
&& TREE_SIDE_EFFECTS (else_
);
2799 pred
= TREE_OPERAND (pred
, 0);
2800 expr
= build3 (COND_EXPR
, void_type_node
, pred
, NULL_TREE
, else_
);
2801 SET_EXPR_LOCATION (expr
, locus
);
2805 /* If we're done, great. */
2806 if (TREE_CODE (pred
) != TRUTH_ANDIF_EXPR
2807 && TREE_CODE (pred
) != TRUTH_ORIF_EXPR
)
2810 /* Otherwise we need to mess with gotos. Change
2813 if (a); else goto no;
2816 and recursively gimplify the condition. */
2818 true_label
= false_label
= end_label
= NULL_TREE
;
2820 /* If our arms just jump somewhere, hijack those labels so we don't
2821 generate jumps to jumps. */
2824 && TREE_CODE (then_
) == GOTO_EXPR
2825 && TREE_CODE (GOTO_DESTINATION (then_
)) == LABEL_DECL
)
2827 true_label
= GOTO_DESTINATION (then_
);
2833 && TREE_CODE (else_
) == GOTO_EXPR
2834 && TREE_CODE (GOTO_DESTINATION (else_
)) == LABEL_DECL
)
2836 false_label
= GOTO_DESTINATION (else_
);
2841 /* If we aren't hijacking a label for the 'then' branch, it falls through. */
2843 true_label_p
= &true_label
;
2845 true_label_p
= NULL
;
2847 /* The 'else' branch also needs a label if it contains interesting code. */
2848 if (false_label
|| else_se
)
2849 false_label_p
= &false_label
;
2851 false_label_p
= NULL
;
2853 /* If there was nothing else in our arms, just forward the label(s). */
2854 if (!then_se
&& !else_se
)
2855 return shortcut_cond_r (pred
, true_label_p
, false_label_p
,
2856 EXPR_LOC_OR_LOC (expr
, input_location
));
2858 /* If our last subexpression already has a terminal label, reuse it. */
2860 t
= expr_last (else_
);
2862 t
= expr_last (then_
);
2865 if (t
&& TREE_CODE (t
) == LABEL_EXPR
)
2866 end_label
= LABEL_EXPR_LABEL (t
);
2868 /* If we don't care about jumping to the 'else' branch, jump to the end
2869 if the condition is false. */
2871 false_label_p
= &end_label
;
2873 /* We only want to emit these labels if we aren't hijacking them. */
2874 emit_end
= (end_label
== NULL_TREE
);
2875 emit_false
= (false_label
== NULL_TREE
);
2877 /* We only emit the jump over the else clause if we have to--if the
2878 then clause may fall through. Otherwise we can wind up with a
2879 useless jump and a useless label at the end of gimplified code,
2880 which will cause us to think that this conditional as a whole
2881 falls through even if it doesn't. If we then inline a function
2882 which ends with such a condition, that can cause us to issue an
2883 inappropriate warning about control reaching the end of a
2884 non-void function. */
2885 jump_over_else
= block_may_fallthru (then_
);
2887 pred
= shortcut_cond_r (pred
, true_label_p
, false_label_p
,
2888 EXPR_LOC_OR_LOC (expr
, input_location
));
2891 append_to_statement_list (pred
, &expr
);
2893 append_to_statement_list (then_
, &expr
);
2898 tree last
= expr_last (expr
);
2899 t
= build_and_jump (&end_label
);
2900 if (EXPR_HAS_LOCATION (last
))
2901 SET_EXPR_LOCATION (t
, EXPR_LOCATION (last
));
2902 append_to_statement_list (t
, &expr
);
2906 t
= build1 (LABEL_EXPR
, void_type_node
, false_label
);
2907 append_to_statement_list (t
, &expr
);
2909 append_to_statement_list (else_
, &expr
);
2911 if (emit_end
&& end_label
)
2913 t
= build1 (LABEL_EXPR
, void_type_node
, end_label
);
2914 append_to_statement_list (t
, &expr
);
2920 /* EXPR is used in a boolean context; make sure it has BOOLEAN_TYPE. */
2923 gimple_boolify (tree expr
)
2925 tree type
= TREE_TYPE (expr
);
2926 location_t loc
= EXPR_LOCATION (expr
);
2928 if (TREE_CODE (expr
) == NE_EXPR
2929 && TREE_CODE (TREE_OPERAND (expr
, 0)) == CALL_EXPR
2930 && integer_zerop (TREE_OPERAND (expr
, 1)))
2932 tree call
= TREE_OPERAND (expr
, 0);
2933 tree fn
= get_callee_fndecl (call
);
2935 /* For __builtin_expect ((long) (x), y) recurse into x as well
2936 if x is truth_value_p. */
2938 && DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
2939 && DECL_FUNCTION_CODE (fn
) == BUILT_IN_EXPECT
2940 && call_expr_nargs (call
) == 2)
2942 tree arg
= CALL_EXPR_ARG (call
, 0);
2945 if (TREE_CODE (arg
) == NOP_EXPR
2946 && TREE_TYPE (arg
) == TREE_TYPE (call
))
2947 arg
= TREE_OPERAND (arg
, 0);
2948 if (truth_value_p (TREE_CODE (arg
)))
2950 arg
= gimple_boolify (arg
);
2951 CALL_EXPR_ARG (call
, 0)
2952 = fold_convert_loc (loc
, TREE_TYPE (call
), arg
);
2958 switch (TREE_CODE (expr
))
2960 case TRUTH_AND_EXPR
:
2962 case TRUTH_XOR_EXPR
:
2963 case TRUTH_ANDIF_EXPR
:
2964 case TRUTH_ORIF_EXPR
:
2965 /* Also boolify the arguments of truth exprs. */
2966 TREE_OPERAND (expr
, 1) = gimple_boolify (TREE_OPERAND (expr
, 1));
2969 case TRUTH_NOT_EXPR
:
2970 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
2972 /* These expressions always produce boolean results. */
2973 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
2974 TREE_TYPE (expr
) = boolean_type_node
;
2978 switch ((enum annot_expr_kind
) TREE_INT_CST_LOW (TREE_OPERAND (expr
, 1)))
2980 case annot_expr_ivdep_kind
:
2981 case annot_expr_no_vector_kind
:
2982 case annot_expr_vector_kind
:
2983 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
2984 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
2985 TREE_TYPE (expr
) = boolean_type_node
;
2992 if (COMPARISON_CLASS_P (expr
))
2994 /* There expressions always prduce boolean results. */
2995 if (TREE_CODE (type
) != BOOLEAN_TYPE
)
2996 TREE_TYPE (expr
) = boolean_type_node
;
2999 /* Other expressions that get here must have boolean values, but
3000 might need to be converted to the appropriate mode. */
3001 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
3003 return fold_convert_loc (loc
, boolean_type_node
, expr
);
3007 /* Given a conditional expression *EXPR_P without side effects, gimplify
3008 its operands. New statements are inserted to PRE_P. */
3010 static enum gimplify_status
3011 gimplify_pure_cond_expr (tree
*expr_p
, gimple_seq
*pre_p
)
3013 tree expr
= *expr_p
, cond
;
3014 enum gimplify_status ret
, tret
;
3015 enum tree_code code
;
3017 cond
= gimple_boolify (COND_EXPR_COND (expr
));
3019 /* We need to handle && and || specially, as their gimplification
3020 creates pure cond_expr, thus leading to an infinite cycle otherwise. */
3021 code
= TREE_CODE (cond
);
3022 if (code
== TRUTH_ANDIF_EXPR
)
3023 TREE_SET_CODE (cond
, TRUTH_AND_EXPR
);
3024 else if (code
== TRUTH_ORIF_EXPR
)
3025 TREE_SET_CODE (cond
, TRUTH_OR_EXPR
);
3026 ret
= gimplify_expr (&cond
, pre_p
, NULL
, is_gimple_condexpr
, fb_rvalue
);
3027 COND_EXPR_COND (*expr_p
) = cond
;
3029 tret
= gimplify_expr (&COND_EXPR_THEN (expr
), pre_p
, NULL
,
3030 is_gimple_val
, fb_rvalue
);
3031 ret
= MIN (ret
, tret
);
3032 tret
= gimplify_expr (&COND_EXPR_ELSE (expr
), pre_p
, NULL
,
3033 is_gimple_val
, fb_rvalue
);
3035 return MIN (ret
, tret
);
3038 /* Return true if evaluating EXPR could trap.
3039 EXPR is GENERIC, while tree_could_trap_p can be called
3043 generic_expr_could_trap_p (tree expr
)
3047 if (!expr
|| is_gimple_val (expr
))
3050 if (!EXPR_P (expr
) || tree_could_trap_p (expr
))
3053 n
= TREE_OPERAND_LENGTH (expr
);
3054 for (i
= 0; i
< n
; i
++)
3055 if (generic_expr_could_trap_p (TREE_OPERAND (expr
, i
)))
3061 /* Convert the conditional expression pointed to by EXPR_P '(p) ? a : b;'
3070 The second form is used when *EXPR_P is of type void.
3072 PRE_P points to the list where side effects that must happen before
3073 *EXPR_P should be stored. */
3075 static enum gimplify_status
3076 gimplify_cond_expr (tree
*expr_p
, gimple_seq
*pre_p
, fallback_t fallback
)
3078 tree expr
= *expr_p
;
3079 tree type
= TREE_TYPE (expr
);
3080 location_t loc
= EXPR_LOCATION (expr
);
3081 tree tmp
, arm1
, arm2
;
3082 enum gimplify_status ret
;
3083 tree label_true
, label_false
, label_cont
;
3084 bool have_then_clause_p
, have_else_clause_p
;
3086 enum tree_code pred_code
;
3087 gimple_seq seq
= NULL
;
3089 /* If this COND_EXPR has a value, copy the values into a temporary within
3091 if (!VOID_TYPE_P (type
))
3093 tree then_
= TREE_OPERAND (expr
, 1), else_
= TREE_OPERAND (expr
, 2);
3096 /* If either an rvalue is ok or we do not require an lvalue, create the
3097 temporary. But we cannot do that if the type is addressable. */
3098 if (((fallback
& fb_rvalue
) || !(fallback
& fb_lvalue
))
3099 && !TREE_ADDRESSABLE (type
))
3101 if (gimplify_ctxp
->allow_rhs_cond_expr
3102 /* If either branch has side effects or could trap, it can't be
3103 evaluated unconditionally. */
3104 && !TREE_SIDE_EFFECTS (then_
)
3105 && !generic_expr_could_trap_p (then_
)
3106 && !TREE_SIDE_EFFECTS (else_
)
3107 && !generic_expr_could_trap_p (else_
))
3108 return gimplify_pure_cond_expr (expr_p
, pre_p
);
3110 tmp
= create_tmp_var (type
, "iftmp");
3114 /* Otherwise, only create and copy references to the values. */
3117 type
= build_pointer_type (type
);
3119 if (!VOID_TYPE_P (TREE_TYPE (then_
)))
3120 then_
= build_fold_addr_expr_loc (loc
, then_
);
3122 if (!VOID_TYPE_P (TREE_TYPE (else_
)))
3123 else_
= build_fold_addr_expr_loc (loc
, else_
);
3126 = build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0), then_
, else_
);
3128 tmp
= create_tmp_var (type
, "iftmp");
3129 result
= build_simple_mem_ref_loc (loc
, tmp
);
3132 /* Build the new then clause, `tmp = then_;'. But don't build the
3133 assignment if the value is void; in C++ it can be if it's a throw. */
3134 if (!VOID_TYPE_P (TREE_TYPE (then_
)))
3135 TREE_OPERAND (expr
, 1) = build2 (MODIFY_EXPR
, type
, tmp
, then_
);
3137 /* Similarly, build the new else clause, `tmp = else_;'. */
3138 if (!VOID_TYPE_P (TREE_TYPE (else_
)))
3139 TREE_OPERAND (expr
, 2) = build2 (MODIFY_EXPR
, type
, tmp
, else_
);
3141 TREE_TYPE (expr
) = void_type_node
;
3142 recalculate_side_effects (expr
);
3144 /* Move the COND_EXPR to the prequeue. */
3145 gimplify_stmt (&expr
, pre_p
);
3151 /* Remove any COMPOUND_EXPR so the following cases will be caught. */
3152 STRIP_TYPE_NOPS (TREE_OPERAND (expr
, 0));
3153 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == COMPOUND_EXPR
)
3154 gimplify_compound_expr (&TREE_OPERAND (expr
, 0), pre_p
, true);
3156 /* Make sure the condition has BOOLEAN_TYPE. */
3157 TREE_OPERAND (expr
, 0) = gimple_boolify (TREE_OPERAND (expr
, 0));
3159 /* Break apart && and || conditions. */
3160 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == TRUTH_ANDIF_EXPR
3161 || TREE_CODE (TREE_OPERAND (expr
, 0)) == TRUTH_ORIF_EXPR
)
3163 expr
= shortcut_cond_expr (expr
);
3165 if (expr
!= *expr_p
)
3169 /* We can't rely on gimplify_expr to re-gimplify the expanded
3170 form properly, as cleanups might cause the target labels to be
3171 wrapped in a TRY_FINALLY_EXPR. To prevent that, we need to
3172 set up a conditional context. */
3173 gimple_push_condition ();
3174 gimplify_stmt (expr_p
, &seq
);
3175 gimple_pop_condition (pre_p
);
3176 gimple_seq_add_seq (pre_p
, seq
);
3182 /* Now do the normal gimplification. */
3184 /* Gimplify condition. */
3185 ret
= gimplify_expr (&TREE_OPERAND (expr
, 0), pre_p
, NULL
, is_gimple_condexpr
,
3187 if (ret
== GS_ERROR
)
3189 gcc_assert (TREE_OPERAND (expr
, 0) != NULL_TREE
);
3191 gimple_push_condition ();
3193 have_then_clause_p
= have_else_clause_p
= false;
3194 if (TREE_OPERAND (expr
, 1) != NULL
3195 && TREE_CODE (TREE_OPERAND (expr
, 1)) == GOTO_EXPR
3196 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr
, 1))) == LABEL_DECL
3197 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr
, 1)))
3198 == current_function_decl
)
3199 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3200 have different locations, otherwise we end up with incorrect
3201 location information on the branches. */
3203 || !EXPR_HAS_LOCATION (expr
)
3204 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr
, 1))
3205 || EXPR_LOCATION (expr
) == EXPR_LOCATION (TREE_OPERAND (expr
, 1))))
3207 label_true
= GOTO_DESTINATION (TREE_OPERAND (expr
, 1));
3208 have_then_clause_p
= true;
3211 label_true
= create_artificial_label (UNKNOWN_LOCATION
);
3212 if (TREE_OPERAND (expr
, 2) != NULL
3213 && TREE_CODE (TREE_OPERAND (expr
, 2)) == GOTO_EXPR
3214 && TREE_CODE (GOTO_DESTINATION (TREE_OPERAND (expr
, 2))) == LABEL_DECL
3215 && (DECL_CONTEXT (GOTO_DESTINATION (TREE_OPERAND (expr
, 2)))
3216 == current_function_decl
)
3217 /* For -O0 avoid this optimization if the COND_EXPR and GOTO_EXPR
3218 have different locations, otherwise we end up with incorrect
3219 location information on the branches. */
3221 || !EXPR_HAS_LOCATION (expr
)
3222 || !EXPR_HAS_LOCATION (TREE_OPERAND (expr
, 2))
3223 || EXPR_LOCATION (expr
) == EXPR_LOCATION (TREE_OPERAND (expr
, 2))))
3225 label_false
= GOTO_DESTINATION (TREE_OPERAND (expr
, 2));
3226 have_else_clause_p
= true;
3229 label_false
= create_artificial_label (UNKNOWN_LOCATION
);
3231 gimple_cond_get_ops_from_tree (COND_EXPR_COND (expr
), &pred_code
, &arm1
,
3233 cond_stmt
= gimple_build_cond (pred_code
, arm1
, arm2
, label_true
,
3235 gimple_set_no_warning (cond_stmt
, TREE_NO_WARNING (COND_EXPR_COND (expr
)));
3236 gimplify_seq_add_stmt (&seq
, cond_stmt
);
3237 gimple_stmt_iterator gsi
= gsi_last (seq
);
3238 maybe_fold_stmt (&gsi
);
3240 label_cont
= NULL_TREE
;
3241 if (!have_then_clause_p
)
3243 /* For if (...) {} else { code; } put label_true after
3245 if (TREE_OPERAND (expr
, 1) == NULL_TREE
3246 && !have_else_clause_p
3247 && TREE_OPERAND (expr
, 2) != NULL_TREE
)
3248 label_cont
= label_true
;
3251 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_true
));
3252 have_then_clause_p
= gimplify_stmt (&TREE_OPERAND (expr
, 1), &seq
);
3253 /* For if (...) { code; } else {} or
3254 if (...) { code; } else goto label; or
3255 if (...) { code; return; } else { ... }
3256 label_cont isn't needed. */
3257 if (!have_else_clause_p
3258 && TREE_OPERAND (expr
, 2) != NULL_TREE
3259 && gimple_seq_may_fallthru (seq
))
3262 label_cont
= create_artificial_label (UNKNOWN_LOCATION
);
3264 g
= gimple_build_goto (label_cont
);
3266 /* GIMPLE_COND's are very low level; they have embedded
3267 gotos. This particular embedded goto should not be marked
3268 with the location of the original COND_EXPR, as it would
3269 correspond to the COND_EXPR's condition, not the ELSE or the
3270 THEN arms. To avoid marking it with the wrong location, flag
3271 it as "no location". */
3272 gimple_set_do_not_emit_location (g
);
3274 gimplify_seq_add_stmt (&seq
, g
);
3278 if (!have_else_clause_p
)
3280 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_false
));
3281 have_else_clause_p
= gimplify_stmt (&TREE_OPERAND (expr
, 2), &seq
);
3284 gimplify_seq_add_stmt (&seq
, gimple_build_label (label_cont
));
3286 gimple_pop_condition (pre_p
);
3287 gimple_seq_add_seq (pre_p
, seq
);
3289 if (ret
== GS_ERROR
)
3291 else if (have_then_clause_p
|| have_else_clause_p
)
3295 /* Both arms are empty; replace the COND_EXPR with its predicate. */
3296 expr
= TREE_OPERAND (expr
, 0);
3297 gimplify_stmt (&expr
, pre_p
);
3304 /* Prepare the node pointed to by EXPR_P, an is_gimple_addressable expression,
3305 to be marked addressable.
3307 We cannot rely on such an expression being directly markable if a temporary
3308 has been created by the gimplification. In this case, we create another
3309 temporary and initialize it with a copy, which will become a store after we
3310 mark it addressable. This can happen if the front-end passed us something
3311 that it could not mark addressable yet, like a Fortran pass-by-reference
3312 parameter (int) floatvar. */
3315 prepare_gimple_addressable (tree
*expr_p
, gimple_seq
*seq_p
)
3317 while (handled_component_p (*expr_p
))
3318 expr_p
= &TREE_OPERAND (*expr_p
, 0);
3319 if (is_gimple_reg (*expr_p
))
3321 /* Do not allow an SSA name as the temporary. */
3322 tree var
= get_initialized_tmp_var (*expr_p
, seq_p
, NULL
, false);
3323 DECL_GIMPLE_REG_P (var
) = 0;
3328 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3329 a call to __builtin_memcpy. */
3331 static enum gimplify_status
3332 gimplify_modify_expr_to_memcpy (tree
*expr_p
, tree size
, bool want_value
,
3335 tree t
, to
, to_ptr
, from
, from_ptr
;
3337 location_t loc
= EXPR_LOCATION (*expr_p
);
3339 to
= TREE_OPERAND (*expr_p
, 0);
3340 from
= TREE_OPERAND (*expr_p
, 1);
3342 /* Mark the RHS addressable. Beware that it may not be possible to do so
3343 directly if a temporary has been created by the gimplification. */
3344 prepare_gimple_addressable (&from
, seq_p
);
3346 mark_addressable (from
);
3347 from_ptr
= build_fold_addr_expr_loc (loc
, from
);
3348 gimplify_arg (&from_ptr
, seq_p
, loc
);
3350 mark_addressable (to
);
3351 to_ptr
= build_fold_addr_expr_loc (loc
, to
);
3352 gimplify_arg (&to_ptr
, seq_p
, loc
);
3354 t
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3356 gs
= gimple_build_call (t
, 3, to_ptr
, from_ptr
, size
);
3360 /* tmp = memcpy() */
3361 t
= create_tmp_var (TREE_TYPE (to_ptr
));
3362 gimple_call_set_lhs (gs
, t
);
3363 gimplify_seq_add_stmt (seq_p
, gs
);
3365 *expr_p
= build_simple_mem_ref (t
);
3369 gimplify_seq_add_stmt (seq_p
, gs
);
3374 /* A subroutine of gimplify_modify_expr. Replace a MODIFY_EXPR with
3375 a call to __builtin_memset. In this case we know that the RHS is
3376 a CONSTRUCTOR with an empty element list. */
3378 static enum gimplify_status
3379 gimplify_modify_expr_to_memset (tree
*expr_p
, tree size
, bool want_value
,
3382 tree t
, from
, to
, to_ptr
;
3384 location_t loc
= EXPR_LOCATION (*expr_p
);
3386 /* Assert our assumptions, to abort instead of producing wrong code
3387 silently if they are not met. Beware that the RHS CONSTRUCTOR might
3388 not be immediately exposed. */
3389 from
= TREE_OPERAND (*expr_p
, 1);
3390 if (TREE_CODE (from
) == WITH_SIZE_EXPR
)
3391 from
= TREE_OPERAND (from
, 0);
3393 gcc_assert (TREE_CODE (from
) == CONSTRUCTOR
3394 && vec_safe_is_empty (CONSTRUCTOR_ELTS (from
)));
3397 to
= TREE_OPERAND (*expr_p
, 0);
3399 to_ptr
= build_fold_addr_expr_loc (loc
, to
);
3400 gimplify_arg (&to_ptr
, seq_p
, loc
);
3401 t
= builtin_decl_implicit (BUILT_IN_MEMSET
);
3403 gs
= gimple_build_call (t
, 3, to_ptr
, integer_zero_node
, size
);
3407 /* tmp = memset() */
3408 t
= create_tmp_var (TREE_TYPE (to_ptr
));
3409 gimple_call_set_lhs (gs
, t
);
3410 gimplify_seq_add_stmt (seq_p
, gs
);
3412 *expr_p
= build1 (INDIRECT_REF
, TREE_TYPE (to
), t
);
3416 gimplify_seq_add_stmt (seq_p
, gs
);
3421 /* A subroutine of gimplify_init_ctor_preeval. Called via walk_tree,
3422 determine, cautiously, if a CONSTRUCTOR overlaps the lhs of an
3423 assignment. Return non-null if we detect a potential overlap. */
3425 struct gimplify_init_ctor_preeval_data
3427 /* The base decl of the lhs object. May be NULL, in which case we
3428 have to assume the lhs is indirect. */
3431 /* The alias set of the lhs object. */
3432 alias_set_type lhs_alias_set
;
3436 gimplify_init_ctor_preeval_1 (tree
*tp
, int *walk_subtrees
, void *xdata
)
3438 struct gimplify_init_ctor_preeval_data
*data
3439 = (struct gimplify_init_ctor_preeval_data
*) xdata
;
3442 /* If we find the base object, obviously we have overlap. */
3443 if (data
->lhs_base_decl
== t
)
3446 /* If the constructor component is indirect, determine if we have a
3447 potential overlap with the lhs. The only bits of information we
3448 have to go on at this point are addressability and alias sets. */
3449 if ((INDIRECT_REF_P (t
)
3450 || TREE_CODE (t
) == MEM_REF
)
3451 && (!data
->lhs_base_decl
|| TREE_ADDRESSABLE (data
->lhs_base_decl
))
3452 && alias_sets_conflict_p (data
->lhs_alias_set
, get_alias_set (t
)))
3455 /* If the constructor component is a call, determine if it can hide a
3456 potential overlap with the lhs through an INDIRECT_REF like above.
3457 ??? Ugh - this is completely broken. In fact this whole analysis
3458 doesn't look conservative. */
3459 if (TREE_CODE (t
) == CALL_EXPR
)
3461 tree type
, fntype
= TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (t
)));
3463 for (type
= TYPE_ARG_TYPES (fntype
); type
; type
= TREE_CHAIN (type
))
3464 if (POINTER_TYPE_P (TREE_VALUE (type
))
3465 && (!data
->lhs_base_decl
|| TREE_ADDRESSABLE (data
->lhs_base_decl
))
3466 && alias_sets_conflict_p (data
->lhs_alias_set
,
3468 (TREE_TYPE (TREE_VALUE (type
)))))
3472 if (IS_TYPE_OR_DECL_P (t
))
3477 /* A subroutine of gimplify_init_constructor. Pre-evaluate EXPR,
3478 force values that overlap with the lhs (as described by *DATA)
3479 into temporaries. */
3482 gimplify_init_ctor_preeval (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
3483 struct gimplify_init_ctor_preeval_data
*data
)
3485 enum gimplify_status one
;
3487 /* If the value is constant, then there's nothing to pre-evaluate. */
3488 if (TREE_CONSTANT (*expr_p
))
3490 /* Ensure it does not have side effects, it might contain a reference to
3491 the object we're initializing. */
3492 gcc_assert (!TREE_SIDE_EFFECTS (*expr_p
));
3496 /* If the type has non-trivial constructors, we can't pre-evaluate. */
3497 if (TREE_ADDRESSABLE (TREE_TYPE (*expr_p
)))
3500 /* Recurse for nested constructors. */
3501 if (TREE_CODE (*expr_p
) == CONSTRUCTOR
)
3503 unsigned HOST_WIDE_INT ix
;
3504 constructor_elt
*ce
;
3505 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (*expr_p
);
3507 FOR_EACH_VEC_SAFE_ELT (v
, ix
, ce
)
3508 gimplify_init_ctor_preeval (&ce
->value
, pre_p
, post_p
, data
);
3513 /* If this is a variable sized type, we must remember the size. */
3514 maybe_with_size_expr (expr_p
);
3516 /* Gimplify the constructor element to something appropriate for the rhs
3517 of a MODIFY_EXPR. Given that we know the LHS is an aggregate, we know
3518 the gimplifier will consider this a store to memory. Doing this
3519 gimplification now means that we won't have to deal with complicated
3520 language-specific trees, nor trees like SAVE_EXPR that can induce
3521 exponential search behavior. */
3522 one
= gimplify_expr (expr_p
, pre_p
, post_p
, is_gimple_mem_rhs
, fb_rvalue
);
3523 if (one
== GS_ERROR
)
3529 /* If we gimplified to a bare decl, we can be sure that it doesn't overlap
3530 with the lhs, since "a = { .x=a }" doesn't make sense. This will
3531 always be true for all scalars, since is_gimple_mem_rhs insists on a
3532 temporary variable for them. */
3533 if (DECL_P (*expr_p
))
3536 /* If this is of variable size, we have no choice but to assume it doesn't
3537 overlap since we can't make a temporary for it. */
3538 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (*expr_p
))) != INTEGER_CST
)
3541 /* Otherwise, we must search for overlap ... */
3542 if (!walk_tree (expr_p
, gimplify_init_ctor_preeval_1
, data
, NULL
))
3545 /* ... and if found, force the value into a temporary. */
3546 *expr_p
= get_formal_tmp_var (*expr_p
, pre_p
);
3549 /* A subroutine of gimplify_init_ctor_eval. Create a loop for
3550 a RANGE_EXPR in a CONSTRUCTOR for an array.
3554 object[var] = value;
3561 We increment var _after_ the loop exit check because we might otherwise
3562 fail if upper == TYPE_MAX_VALUE (type for upper).
3564 Note that we never have to deal with SAVE_EXPRs here, because this has
3565 already been taken care of for us, in gimplify_init_ctor_preeval(). */
3567 static void gimplify_init_ctor_eval (tree
, vec
<constructor_elt
, va_gc
> *,
3568 gimple_seq
*, bool);
3571 gimplify_init_ctor_eval_range (tree object
, tree lower
, tree upper
,
3572 tree value
, tree array_elt_type
,
3573 gimple_seq
*pre_p
, bool cleared
)
3575 tree loop_entry_label
, loop_exit_label
, fall_thru_label
;
3576 tree var
, var_type
, cref
, tmp
;
3578 loop_entry_label
= create_artificial_label (UNKNOWN_LOCATION
);
3579 loop_exit_label
= create_artificial_label (UNKNOWN_LOCATION
);
3580 fall_thru_label
= create_artificial_label (UNKNOWN_LOCATION
);
3582 /* Create and initialize the index variable. */
3583 var_type
= TREE_TYPE (upper
);
3584 var
= create_tmp_var (var_type
);
3585 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (var
, lower
));
3587 /* Add the loop entry label. */
3588 gimplify_seq_add_stmt (pre_p
, gimple_build_label (loop_entry_label
));
3590 /* Build the reference. */
3591 cref
= build4 (ARRAY_REF
, array_elt_type
, unshare_expr (object
),
3592 var
, NULL_TREE
, NULL_TREE
);
3594 /* If we are a constructor, just call gimplify_init_ctor_eval to do
3595 the store. Otherwise just assign value to the reference. */
3597 if (TREE_CODE (value
) == CONSTRUCTOR
)
3598 /* NB we might have to call ourself recursively through
3599 gimplify_init_ctor_eval if the value is a constructor. */
3600 gimplify_init_ctor_eval (cref
, CONSTRUCTOR_ELTS (value
),
3603 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (cref
, value
));
3605 /* We exit the loop when the index var is equal to the upper bound. */
3606 gimplify_seq_add_stmt (pre_p
,
3607 gimple_build_cond (EQ_EXPR
, var
, upper
,
3608 loop_exit_label
, fall_thru_label
));
3610 gimplify_seq_add_stmt (pre_p
, gimple_build_label (fall_thru_label
));
3612 /* Otherwise, increment the index var... */
3613 tmp
= build2 (PLUS_EXPR
, var_type
, var
,
3614 fold_convert (var_type
, integer_one_node
));
3615 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (var
, tmp
));
3617 /* ...and jump back to the loop entry. */
3618 gimplify_seq_add_stmt (pre_p
, gimple_build_goto (loop_entry_label
));
3620 /* Add the loop exit label. */
3621 gimplify_seq_add_stmt (pre_p
, gimple_build_label (loop_exit_label
));
3624 /* Return true if FDECL is accessing a field that is zero sized. */
3627 zero_sized_field_decl (const_tree fdecl
)
3629 if (TREE_CODE (fdecl
) == FIELD_DECL
&& DECL_SIZE (fdecl
)
3630 && integer_zerop (DECL_SIZE (fdecl
)))
3635 /* Return true if TYPE is zero sized. */
3638 zero_sized_type (const_tree type
)
3640 if (AGGREGATE_TYPE_P (type
) && TYPE_SIZE (type
)
3641 && integer_zerop (TYPE_SIZE (type
)))
3646 /* A subroutine of gimplify_init_constructor. Generate individual
3647 MODIFY_EXPRs for a CONSTRUCTOR. OBJECT is the LHS against which the
3648 assignments should happen. ELTS is the CONSTRUCTOR_ELTS of the
3649 CONSTRUCTOR. CLEARED is true if the entire LHS object has been
3653 gimplify_init_ctor_eval (tree object
, vec
<constructor_elt
, va_gc
> *elts
,
3654 gimple_seq
*pre_p
, bool cleared
)
3656 tree array_elt_type
= NULL
;
3657 unsigned HOST_WIDE_INT ix
;
3658 tree purpose
, value
;
3660 if (TREE_CODE (TREE_TYPE (object
)) == ARRAY_TYPE
)
3661 array_elt_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (object
)));
3663 FOR_EACH_CONSTRUCTOR_ELT (elts
, ix
, purpose
, value
)
3667 /* NULL values are created above for gimplification errors. */
3671 if (cleared
&& initializer_zerop (value
))
3674 /* ??? Here's to hoping the front end fills in all of the indices,
3675 so we don't have to figure out what's missing ourselves. */
3676 gcc_assert (purpose
);
3678 /* Skip zero-sized fields, unless value has side-effects. This can
3679 happen with calls to functions returning a zero-sized type, which
3680 we shouldn't discard. As a number of downstream passes don't
3681 expect sets of zero-sized fields, we rely on the gimplification of
3682 the MODIFY_EXPR we make below to drop the assignment statement. */
3683 if (! TREE_SIDE_EFFECTS (value
) && zero_sized_field_decl (purpose
))
3686 /* If we have a RANGE_EXPR, we have to build a loop to assign the
3688 if (TREE_CODE (purpose
) == RANGE_EXPR
)
3690 tree lower
= TREE_OPERAND (purpose
, 0);
3691 tree upper
= TREE_OPERAND (purpose
, 1);
3693 /* If the lower bound is equal to upper, just treat it as if
3694 upper was the index. */
3695 if (simple_cst_equal (lower
, upper
))
3699 gimplify_init_ctor_eval_range (object
, lower
, upper
, value
,
3700 array_elt_type
, pre_p
, cleared
);
3707 /* Do not use bitsizetype for ARRAY_REF indices. */
3708 if (TYPE_DOMAIN (TREE_TYPE (object
)))
3710 = fold_convert (TREE_TYPE (TYPE_DOMAIN (TREE_TYPE (object
))),
3712 cref
= build4 (ARRAY_REF
, array_elt_type
, unshare_expr (object
),
3713 purpose
, NULL_TREE
, NULL_TREE
);
3717 gcc_assert (TREE_CODE (purpose
) == FIELD_DECL
);
3718 cref
= build3 (COMPONENT_REF
, TREE_TYPE (purpose
),
3719 unshare_expr (object
), purpose
, NULL_TREE
);
3722 if (TREE_CODE (value
) == CONSTRUCTOR
3723 && TREE_CODE (TREE_TYPE (value
)) != VECTOR_TYPE
)
3724 gimplify_init_ctor_eval (cref
, CONSTRUCTOR_ELTS (value
),
3728 tree init
= build2 (INIT_EXPR
, TREE_TYPE (cref
), cref
, value
);
3729 gimplify_and_add (init
, pre_p
);
3735 /* Return the appropriate RHS predicate for this LHS. */
3738 rhs_predicate_for (tree lhs
)
3740 if (is_gimple_reg (lhs
))
3741 return is_gimple_reg_rhs_or_call
;
3743 return is_gimple_mem_rhs_or_call
;
3746 /* Gimplify a C99 compound literal expression. This just means adding
3747 the DECL_EXPR before the current statement and using its anonymous
3750 static enum gimplify_status
3751 gimplify_compound_literal_expr (tree
*expr_p
, gimple_seq
*pre_p
,
3752 bool (*gimple_test_f
) (tree
),
3753 fallback_t fallback
)
3755 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (*expr_p
);
3756 tree decl
= DECL_EXPR_DECL (decl_s
);
3757 tree init
= DECL_INITIAL (decl
);
3758 /* Mark the decl as addressable if the compound literal
3759 expression is addressable now, otherwise it is marked too late
3760 after we gimplify the initialization expression. */
3761 if (TREE_ADDRESSABLE (*expr_p
))
3762 TREE_ADDRESSABLE (decl
) = 1;
3763 /* Otherwise, if we don't need an lvalue and have a literal directly
3764 substitute it. Check if it matches the gimple predicate, as
3765 otherwise we'd generate a new temporary, and we can as well just
3766 use the decl we already have. */
3767 else if (!TREE_ADDRESSABLE (decl
)
3769 && (fallback
& fb_lvalue
) == 0
3770 && gimple_test_f (init
))
3776 /* Preliminarily mark non-addressed complex variables as eligible
3777 for promotion to gimple registers. We'll transform their uses
3779 if ((TREE_CODE (TREE_TYPE (decl
)) == COMPLEX_TYPE
3780 || TREE_CODE (TREE_TYPE (decl
)) == VECTOR_TYPE
)
3781 && !TREE_THIS_VOLATILE (decl
)
3782 && !needs_to_live_in_memory (decl
))
3783 DECL_GIMPLE_REG_P (decl
) = 1;
3785 /* If the decl is not addressable, then it is being used in some
3786 expression or on the right hand side of a statement, and it can
3787 be put into a readonly data section. */
3788 if (!TREE_ADDRESSABLE (decl
) && (fallback
& fb_lvalue
) == 0)
3789 TREE_READONLY (decl
) = 1;
3791 /* This decl isn't mentioned in the enclosing block, so add it to the
3792 list of temps. FIXME it seems a bit of a kludge to say that
3793 anonymous artificial vars aren't pushed, but everything else is. */
3794 if (DECL_NAME (decl
) == NULL_TREE
&& !DECL_SEEN_IN_BIND_EXPR_P (decl
))
3795 gimple_add_tmp_var (decl
);
3797 gimplify_and_add (decl_s
, pre_p
);
3802 /* Optimize embedded COMPOUND_LITERAL_EXPRs within a CONSTRUCTOR,
3803 return a new CONSTRUCTOR if something changed. */
3806 optimize_compound_literals_in_ctor (tree orig_ctor
)
3808 tree ctor
= orig_ctor
;
3809 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3810 unsigned int idx
, num
= vec_safe_length (elts
);
3812 for (idx
= 0; idx
< num
; idx
++)
3814 tree value
= (*elts
)[idx
].value
;
3815 tree newval
= value
;
3816 if (TREE_CODE (value
) == CONSTRUCTOR
)
3817 newval
= optimize_compound_literals_in_ctor (value
);
3818 else if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
3820 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (value
);
3821 tree decl
= DECL_EXPR_DECL (decl_s
);
3822 tree init
= DECL_INITIAL (decl
);
3824 if (!TREE_ADDRESSABLE (value
)
3825 && !TREE_ADDRESSABLE (decl
)
3827 && TREE_CODE (init
) == CONSTRUCTOR
)
3828 newval
= optimize_compound_literals_in_ctor (init
);
3830 if (newval
== value
)
3833 if (ctor
== orig_ctor
)
3835 ctor
= copy_node (orig_ctor
);
3836 CONSTRUCTOR_ELTS (ctor
) = vec_safe_copy (elts
);
3837 elts
= CONSTRUCTOR_ELTS (ctor
);
3839 (*elts
)[idx
].value
= newval
;
3844 /* A subroutine of gimplify_modify_expr. Break out elements of a
3845 CONSTRUCTOR used as an initializer into separate MODIFY_EXPRs.
3847 Note that we still need to clear any elements that don't have explicit
3848 initializers, so if not all elements are initialized we keep the
3849 original MODIFY_EXPR, we just remove all of the constructor elements.
3851 If NOTIFY_TEMP_CREATION is true, do not gimplify, just return
3852 GS_ERROR if we would have to create a temporary when gimplifying
3853 this constructor. Otherwise, return GS_OK.
3855 If NOTIFY_TEMP_CREATION is false, just do the gimplification. */
3857 static enum gimplify_status
3858 gimplify_init_constructor (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
3859 bool want_value
, bool notify_temp_creation
)
3861 tree object
, ctor
, type
;
3862 enum gimplify_status ret
;
3863 vec
<constructor_elt
, va_gc
> *elts
;
3865 gcc_assert (TREE_CODE (TREE_OPERAND (*expr_p
, 1)) == CONSTRUCTOR
);
3867 if (!notify_temp_creation
)
3869 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
3870 is_gimple_lvalue
, fb_lvalue
);
3871 if (ret
== GS_ERROR
)
3875 object
= TREE_OPERAND (*expr_p
, 0);
3876 ctor
= TREE_OPERAND (*expr_p
, 1) =
3877 optimize_compound_literals_in_ctor (TREE_OPERAND (*expr_p
, 1));
3878 type
= TREE_TYPE (ctor
);
3879 elts
= CONSTRUCTOR_ELTS (ctor
);
3882 switch (TREE_CODE (type
))
3886 case QUAL_UNION_TYPE
:
3889 struct gimplify_init_ctor_preeval_data preeval_data
;
3890 HOST_WIDE_INT num_ctor_elements
, num_nonzero_elements
;
3891 bool cleared
, complete_p
, valid_const_initializer
;
3893 /* Aggregate types must lower constructors to initialization of
3894 individual elements. The exception is that a CONSTRUCTOR node
3895 with no elements indicates zero-initialization of the whole. */
3896 if (vec_safe_is_empty (elts
))
3898 if (notify_temp_creation
)
3903 /* Fetch information about the constructor to direct later processing.
3904 We might want to make static versions of it in various cases, and
3905 can only do so if it known to be a valid constant initializer. */
3906 valid_const_initializer
3907 = categorize_ctor_elements (ctor
, &num_nonzero_elements
,
3908 &num_ctor_elements
, &complete_p
);
3910 /* If a const aggregate variable is being initialized, then it
3911 should never be a lose to promote the variable to be static. */
3912 if (valid_const_initializer
3913 && num_nonzero_elements
> 1
3914 && TREE_READONLY (object
)
3915 && TREE_CODE (object
) == VAR_DECL
3916 && (flag_merge_constants
>= 2 || !TREE_ADDRESSABLE (object
)))
3918 if (notify_temp_creation
)
3920 DECL_INITIAL (object
) = ctor
;
3921 TREE_STATIC (object
) = 1;
3922 if (!DECL_NAME (object
))
3923 DECL_NAME (object
) = create_tmp_var_name ("C");
3924 walk_tree (&DECL_INITIAL (object
), force_labels_r
, NULL
, NULL
);
3926 /* ??? C++ doesn't automatically append a .<number> to the
3927 assembler name, and even when it does, it looks at FE private
3928 data structures to figure out what that number should be,
3929 which are not set for this variable. I suppose this is
3930 important for local statics for inline functions, which aren't
3931 "local" in the object file sense. So in order to get a unique
3932 TU-local symbol, we must invoke the lhd version now. */
3933 lhd_set_decl_assembler_name (object
);
3935 *expr_p
= NULL_TREE
;
3939 /* If there are "lots" of initialized elements, even discounting
3940 those that are not address constants (and thus *must* be
3941 computed at runtime), then partition the constructor into
3942 constant and non-constant parts. Block copy the constant
3943 parts in, then generate code for the non-constant parts. */
3944 /* TODO. There's code in cp/typeck.c to do this. */
3946 if (int_size_in_bytes (TREE_TYPE (ctor
)) < 0)
3947 /* store_constructor will ignore the clearing of variable-sized
3948 objects. Initializers for such objects must explicitly set
3949 every field that needs to be set. */
3951 else if (!complete_p
&& !CONSTRUCTOR_NO_CLEARING (ctor
))
3952 /* If the constructor isn't complete, clear the whole object
3953 beforehand, unless CONSTRUCTOR_NO_CLEARING is set on it.
3955 ??? This ought not to be needed. For any element not present
3956 in the initializer, we should simply set them to zero. Except
3957 we'd need to *find* the elements that are not present, and that
3958 requires trickery to avoid quadratic compile-time behavior in
3959 large cases or excessive memory use in small cases. */
3961 else if (num_ctor_elements
- num_nonzero_elements
3962 > CLEAR_RATIO (optimize_function_for_speed_p (cfun
))
3963 && num_nonzero_elements
< num_ctor_elements
/ 4)
3964 /* If there are "lots" of zeros, it's more efficient to clear
3965 the memory and then set the nonzero elements. */
3970 /* If there are "lots" of initialized elements, and all of them
3971 are valid address constants, then the entire initializer can
3972 be dropped to memory, and then memcpy'd out. Don't do this
3973 for sparse arrays, though, as it's more efficient to follow
3974 the standard CONSTRUCTOR behavior of memset followed by
3975 individual element initialization. Also don't do this for small
3976 all-zero initializers (which aren't big enough to merit
3977 clearing), and don't try to make bitwise copies of
3978 TREE_ADDRESSABLE types.
3980 We cannot apply such transformation when compiling chkp static
3981 initializer because creation of initializer image in the memory
3982 will require static initialization of bounds for it. It should
3983 result in another gimplification of similar initializer and we
3984 may fall into infinite loop. */
3985 if (valid_const_initializer
3986 && !(cleared
|| num_nonzero_elements
== 0)
3987 && !TREE_ADDRESSABLE (type
)
3988 && (!current_function_decl
3989 || !lookup_attribute ("chkp ctor",
3990 DECL_ATTRIBUTES (current_function_decl
))))
3992 HOST_WIDE_INT size
= int_size_in_bytes (type
);
3995 /* ??? We can still get unbounded array types, at least
3996 from the C++ front end. This seems wrong, but attempt
3997 to work around it for now. */
4000 size
= int_size_in_bytes (TREE_TYPE (object
));
4002 TREE_TYPE (ctor
) = type
= TREE_TYPE (object
);
4005 /* Find the maximum alignment we can assume for the object. */
4006 /* ??? Make use of DECL_OFFSET_ALIGN. */
4007 if (DECL_P (object
))
4008 align
= DECL_ALIGN (object
);
4010 align
= TYPE_ALIGN (type
);
4012 /* Do a block move either if the size is so small as to make
4013 each individual move a sub-unit move on average, or if it
4014 is so large as to make individual moves inefficient. */
4016 && num_nonzero_elements
> 1
4017 && (size
< num_nonzero_elements
4018 || !can_move_by_pieces (size
, align
)))
4020 if (notify_temp_creation
)
4023 walk_tree (&ctor
, force_labels_r
, NULL
, NULL
);
4024 ctor
= tree_output_constant_def (ctor
);
4025 if (!useless_type_conversion_p (type
, TREE_TYPE (ctor
)))
4026 ctor
= build1 (VIEW_CONVERT_EXPR
, type
, ctor
);
4027 TREE_OPERAND (*expr_p
, 1) = ctor
;
4029 /* This is no longer an assignment of a CONSTRUCTOR, but
4030 we still may have processing to do on the LHS. So
4031 pretend we didn't do anything here to let that happen. */
4032 return GS_UNHANDLED
;
4036 /* If the target is volatile, we have non-zero elements and more than
4037 one field to assign, initialize the target from a temporary. */
4038 if (TREE_THIS_VOLATILE (object
)
4039 && !TREE_ADDRESSABLE (type
)
4040 && num_nonzero_elements
> 0
4041 && vec_safe_length (elts
) > 1)
4043 tree temp
= create_tmp_var (TYPE_MAIN_VARIANT (type
));
4044 TREE_OPERAND (*expr_p
, 0) = temp
;
4045 *expr_p
= build2 (COMPOUND_EXPR
, TREE_TYPE (*expr_p
),
4047 build2 (MODIFY_EXPR
, void_type_node
,
4052 if (notify_temp_creation
)
4055 /* If there are nonzero elements and if needed, pre-evaluate to capture
4056 elements overlapping with the lhs into temporaries. We must do this
4057 before clearing to fetch the values before they are zeroed-out. */
4058 if (num_nonzero_elements
> 0 && TREE_CODE (*expr_p
) != INIT_EXPR
)
4060 preeval_data
.lhs_base_decl
= get_base_address (object
);
4061 if (!DECL_P (preeval_data
.lhs_base_decl
))
4062 preeval_data
.lhs_base_decl
= NULL
;
4063 preeval_data
.lhs_alias_set
= get_alias_set (object
);
4065 gimplify_init_ctor_preeval (&TREE_OPERAND (*expr_p
, 1),
4066 pre_p
, post_p
, &preeval_data
);
4069 bool ctor_has_side_effects_p
4070 = TREE_SIDE_EFFECTS (TREE_OPERAND (*expr_p
, 1));
4074 /* Zap the CONSTRUCTOR element list, which simplifies this case.
4075 Note that we still have to gimplify, in order to handle the
4076 case of variable sized types. Avoid shared tree structures. */
4077 CONSTRUCTOR_ELTS (ctor
) = NULL
;
4078 TREE_SIDE_EFFECTS (ctor
) = 0;
4079 object
= unshare_expr (object
);
4080 gimplify_stmt (expr_p
, pre_p
);
4083 /* If we have not block cleared the object, or if there are nonzero
4084 elements in the constructor, or if the constructor has side effects,
4085 add assignments to the individual scalar fields of the object. */
4087 || num_nonzero_elements
> 0
4088 || ctor_has_side_effects_p
)
4089 gimplify_init_ctor_eval (object
, elts
, pre_p
, cleared
);
4091 *expr_p
= NULL_TREE
;
4099 if (notify_temp_creation
)
4102 /* Extract the real and imaginary parts out of the ctor. */
4103 gcc_assert (elts
->length () == 2);
4104 r
= (*elts
)[0].value
;
4105 i
= (*elts
)[1].value
;
4106 if (r
== NULL
|| i
== NULL
)
4108 tree zero
= build_zero_cst (TREE_TYPE (type
));
4115 /* Complex types have either COMPLEX_CST or COMPLEX_EXPR to
4116 represent creation of a complex value. */
4117 if (TREE_CONSTANT (r
) && TREE_CONSTANT (i
))
4119 ctor
= build_complex (type
, r
, i
);
4120 TREE_OPERAND (*expr_p
, 1) = ctor
;
4124 ctor
= build2 (COMPLEX_EXPR
, type
, r
, i
);
4125 TREE_OPERAND (*expr_p
, 1) = ctor
;
4126 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1),
4129 rhs_predicate_for (TREE_OPERAND (*expr_p
, 0)),
4137 unsigned HOST_WIDE_INT ix
;
4138 constructor_elt
*ce
;
4140 if (notify_temp_creation
)
4143 /* Go ahead and simplify constant constructors to VECTOR_CST. */
4144 if (TREE_CONSTANT (ctor
))
4146 bool constant_p
= true;
4149 /* Even when ctor is constant, it might contain non-*_CST
4150 elements, such as addresses or trapping values like
4151 1.0/0.0 - 1.0/0.0. Such expressions don't belong
4152 in VECTOR_CST nodes. */
4153 FOR_EACH_CONSTRUCTOR_VALUE (elts
, ix
, value
)
4154 if (!CONSTANT_CLASS_P (value
))
4162 TREE_OPERAND (*expr_p
, 1) = build_vector_from_ctor (type
, elts
);
4166 TREE_CONSTANT (ctor
) = 0;
4169 /* Vector types use CONSTRUCTOR all the way through gimple
4170 compilation as a general initializer. */
4171 FOR_EACH_VEC_SAFE_ELT (elts
, ix
, ce
)
4173 enum gimplify_status tret
;
4174 tret
= gimplify_expr (&ce
->value
, pre_p
, post_p
, is_gimple_val
,
4176 if (tret
== GS_ERROR
)
4178 else if (TREE_STATIC (ctor
)
4179 && !initializer_constant_valid_p (ce
->value
,
4180 TREE_TYPE (ce
->value
)))
4181 TREE_STATIC (ctor
) = 0;
4183 if (!is_gimple_reg (TREE_OPERAND (*expr_p
, 0)))
4184 TREE_OPERAND (*expr_p
, 1) = get_formal_tmp_var (ctor
, pre_p
);
4189 /* So how did we get a CONSTRUCTOR for a scalar type? */
4193 if (ret
== GS_ERROR
)
4195 else if (want_value
)
4202 /* If we have gimplified both sides of the initializer but have
4203 not emitted an assignment, do so now. */
4206 tree lhs
= TREE_OPERAND (*expr_p
, 0);
4207 tree rhs
= TREE_OPERAND (*expr_p
, 1);
4208 gassign
*init
= gimple_build_assign (lhs
, rhs
);
4209 gimplify_seq_add_stmt (pre_p
, init
);
4217 /* Given a pointer value OP0, return a simplified version of an
4218 indirection through OP0, or NULL_TREE if no simplification is
4219 possible. This may only be applied to a rhs of an expression.
4220 Note that the resulting type may be different from the type pointed
4221 to in the sense that it is still compatible from the langhooks
4225 gimple_fold_indirect_ref_rhs (tree t
)
4227 return gimple_fold_indirect_ref (t
);
4230 /* Subroutine of gimplify_modify_expr to do simplifications of
4231 MODIFY_EXPRs based on the code of the RHS. We loop for as long as
4232 something changes. */
4234 static enum gimplify_status
4235 gimplify_modify_expr_rhs (tree
*expr_p
, tree
*from_p
, tree
*to_p
,
4236 gimple_seq
*pre_p
, gimple_seq
*post_p
,
4239 enum gimplify_status ret
= GS_UNHANDLED
;
4245 switch (TREE_CODE (*from_p
))
4248 /* If we're assigning from a read-only variable initialized with
4249 a constructor, do the direct assignment from the constructor,
4250 but only if neither source nor target are volatile since this
4251 latter assignment might end up being done on a per-field basis. */
4252 if (DECL_INITIAL (*from_p
)
4253 && TREE_READONLY (*from_p
)
4254 && !TREE_THIS_VOLATILE (*from_p
)
4255 && !TREE_THIS_VOLATILE (*to_p
)
4256 && TREE_CODE (DECL_INITIAL (*from_p
)) == CONSTRUCTOR
)
4258 tree old_from
= *from_p
;
4259 enum gimplify_status subret
;
4261 /* Move the constructor into the RHS. */
4262 *from_p
= unshare_expr (DECL_INITIAL (*from_p
));
4264 /* Let's see if gimplify_init_constructor will need to put
4266 subret
= gimplify_init_constructor (expr_p
, NULL
, NULL
,
4268 if (subret
== GS_ERROR
)
4270 /* If so, revert the change. */
4282 /* If we have code like
4286 where the type of "x" is a (possibly cv-qualified variant
4287 of "A"), treat the entire expression as identical to "x".
4288 This kind of code arises in C++ when an object is bound
4289 to a const reference, and if "x" is a TARGET_EXPR we want
4290 to take advantage of the optimization below. */
4291 bool volatile_p
= TREE_THIS_VOLATILE (*from_p
);
4292 tree t
= gimple_fold_indirect_ref_rhs (TREE_OPERAND (*from_p
, 0));
4295 if (TREE_THIS_VOLATILE (t
) != volatile_p
)
4298 t
= build_simple_mem_ref_loc (EXPR_LOCATION (*from_p
),
4299 build_fold_addr_expr (t
));
4300 if (REFERENCE_CLASS_P (t
))
4301 TREE_THIS_VOLATILE (t
) = volatile_p
;
4312 /* If we are initializing something from a TARGET_EXPR, strip the
4313 TARGET_EXPR and initialize it directly, if possible. This can't
4314 be done if the initializer is void, since that implies that the
4315 temporary is set in some non-trivial way.
4317 ??? What about code that pulls out the temp and uses it
4318 elsewhere? I think that such code never uses the TARGET_EXPR as
4319 an initializer. If I'm wrong, we'll die because the temp won't
4320 have any RTL. In that case, I guess we'll need to replace
4321 references somehow. */
4322 tree init
= TARGET_EXPR_INITIAL (*from_p
);
4325 && !VOID_TYPE_P (TREE_TYPE (init
)))
4335 /* Remove any COMPOUND_EXPR in the RHS so the following cases will be
4337 gimplify_compound_expr (from_p
, pre_p
, true);
4343 /* If we already made some changes, let the front end have a
4344 crack at this before we break it down. */
4345 if (ret
!= GS_UNHANDLED
)
4347 /* If we're initializing from a CONSTRUCTOR, break this into
4348 individual MODIFY_EXPRs. */
4349 return gimplify_init_constructor (expr_p
, pre_p
, post_p
, want_value
,
4353 /* If we're assigning to a non-register type, push the assignment
4354 down into the branches. This is mandatory for ADDRESSABLE types,
4355 since we cannot generate temporaries for such, but it saves a
4356 copy in other cases as well. */
4357 if (!is_gimple_reg_type (TREE_TYPE (*from_p
)))
4359 /* This code should mirror the code in gimplify_cond_expr. */
4360 enum tree_code code
= TREE_CODE (*expr_p
);
4361 tree cond
= *from_p
;
4362 tree result
= *to_p
;
4364 ret
= gimplify_expr (&result
, pre_p
, post_p
,
4365 is_gimple_lvalue
, fb_lvalue
);
4366 if (ret
!= GS_ERROR
)
4369 if (TREE_TYPE (TREE_OPERAND (cond
, 1)) != void_type_node
)
4370 TREE_OPERAND (cond
, 1)
4371 = build2 (code
, void_type_node
, result
,
4372 TREE_OPERAND (cond
, 1));
4373 if (TREE_TYPE (TREE_OPERAND (cond
, 2)) != void_type_node
)
4374 TREE_OPERAND (cond
, 2)
4375 = build2 (code
, void_type_node
, unshare_expr (result
),
4376 TREE_OPERAND (cond
, 2));
4378 TREE_TYPE (cond
) = void_type_node
;
4379 recalculate_side_effects (cond
);
4383 gimplify_and_add (cond
, pre_p
);
4384 *expr_p
= unshare_expr (result
);
4393 /* For calls that return in memory, give *to_p as the CALL_EXPR's
4394 return slot so that we don't generate a temporary. */
4395 if (!CALL_EXPR_RETURN_SLOT_OPT (*from_p
)
4396 && aggregate_value_p (*from_p
, *from_p
))
4400 if (!(rhs_predicate_for (*to_p
))(*from_p
))
4401 /* If we need a temporary, *to_p isn't accurate. */
4403 /* It's OK to use the return slot directly unless it's an NRV. */
4404 else if (TREE_CODE (*to_p
) == RESULT_DECL
4405 && DECL_NAME (*to_p
) == NULL_TREE
4406 && needs_to_live_in_memory (*to_p
))
4408 else if (is_gimple_reg_type (TREE_TYPE (*to_p
))
4409 || (DECL_P (*to_p
) && DECL_REGISTER (*to_p
)))
4410 /* Don't force regs into memory. */
4412 else if (TREE_CODE (*expr_p
) == INIT_EXPR
)
4413 /* It's OK to use the target directly if it's being
4416 else if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p
)))
4418 /* Always use the target and thus RSO for variable-sized types.
4419 GIMPLE cannot deal with a variable-sized assignment
4420 embedded in a call statement. */
4422 else if (TREE_CODE (*to_p
) != SSA_NAME
4423 && (!is_gimple_variable (*to_p
)
4424 || needs_to_live_in_memory (*to_p
)))
4425 /* Don't use the original target if it's already addressable;
4426 if its address escapes, and the called function uses the
4427 NRV optimization, a conforming program could see *to_p
4428 change before the called function returns; see c++/19317.
4429 When optimizing, the return_slot pass marks more functions
4430 as safe after we have escape info. */
4437 CALL_EXPR_RETURN_SLOT_OPT (*from_p
) = 1;
4438 mark_addressable (*to_p
);
4443 case WITH_SIZE_EXPR
:
4444 /* Likewise for calls that return an aggregate of non-constant size,
4445 since we would not be able to generate a temporary at all. */
4446 if (TREE_CODE (TREE_OPERAND (*from_p
, 0)) == CALL_EXPR
)
4448 *from_p
= TREE_OPERAND (*from_p
, 0);
4449 /* We don't change ret in this case because the
4450 WITH_SIZE_EXPR might have been added in
4451 gimplify_modify_expr, so returning GS_OK would lead to an
4457 /* If we're initializing from a container, push the initialization
4459 case CLEANUP_POINT_EXPR
:
4461 case STATEMENT_LIST
:
4463 tree wrap
= *from_p
;
4466 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_min_lval
,
4468 if (ret
!= GS_ERROR
)
4471 t
= voidify_wrapper_expr (wrap
, *expr_p
);
4472 gcc_assert (t
== *expr_p
);
4476 gimplify_and_add (wrap
, pre_p
);
4477 *expr_p
= unshare_expr (*to_p
);
4484 case COMPOUND_LITERAL_EXPR
:
4486 tree complit
= TREE_OPERAND (*expr_p
, 1);
4487 tree decl_s
= COMPOUND_LITERAL_EXPR_DECL_EXPR (complit
);
4488 tree decl
= DECL_EXPR_DECL (decl_s
);
4489 tree init
= DECL_INITIAL (decl
);
4491 /* struct T x = (struct T) { 0, 1, 2 } can be optimized
4492 into struct T x = { 0, 1, 2 } if the address of the
4493 compound literal has never been taken. */
4494 if (!TREE_ADDRESSABLE (complit
)
4495 && !TREE_ADDRESSABLE (decl
)
4498 *expr_p
= copy_node (*expr_p
);
4499 TREE_OPERAND (*expr_p
, 1) = init
;
4514 /* Return true if T looks like a valid GIMPLE statement. */
4517 is_gimple_stmt (tree t
)
4519 const enum tree_code code
= TREE_CODE (t
);
4524 /* The only valid NOP_EXPR is the empty statement. */
4525 return IS_EMPTY_STMT (t
);
4529 /* These are only valid if they're void. */
4530 return TREE_TYPE (t
) == NULL
|| VOID_TYPE_P (TREE_TYPE (t
));
4536 case CASE_LABEL_EXPR
:
4537 case TRY_CATCH_EXPR
:
4538 case TRY_FINALLY_EXPR
:
4539 case EH_FILTER_EXPR
:
4542 case STATEMENT_LIST
:
4546 case OACC_HOST_DATA
:
4549 case OACC_ENTER_DATA
:
4550 case OACC_EXIT_DATA
:
4556 case OMP_DISTRIBUTE
:
4567 case OMP_TARGET_DATA
:
4568 case OMP_TARGET_UPDATE
:
4569 case OMP_TARGET_ENTER_DATA
:
4570 case OMP_TARGET_EXIT_DATA
:
4573 /* These are always void. */
4579 /* These are valid regardless of their type. */
4588 /* Promote partial stores to COMPLEX variables to total stores. *EXPR_P is
4589 a MODIFY_EXPR with a lhs of a REAL/IMAGPART_EXPR of a variable with
4590 DECL_GIMPLE_REG_P set.
4592 IMPORTANT NOTE: This promotion is performed by introducing a load of the
4593 other, unmodified part of the complex object just before the total store.
4594 As a consequence, if the object is still uninitialized, an undefined value
4595 will be loaded into a register, which may result in a spurious exception
4596 if the register is floating-point and the value happens to be a signaling
4597 NaN for example. Then the fully-fledged complex operations lowering pass
4598 followed by a DCE pass are necessary in order to fix things up. */
4600 static enum gimplify_status
4601 gimplify_modify_expr_complex_part (tree
*expr_p
, gimple_seq
*pre_p
,
4604 enum tree_code code
, ocode
;
4605 tree lhs
, rhs
, new_rhs
, other
, realpart
, imagpart
;
4607 lhs
= TREE_OPERAND (*expr_p
, 0);
4608 rhs
= TREE_OPERAND (*expr_p
, 1);
4609 code
= TREE_CODE (lhs
);
4610 lhs
= TREE_OPERAND (lhs
, 0);
4612 ocode
= code
== REALPART_EXPR
? IMAGPART_EXPR
: REALPART_EXPR
;
4613 other
= build1 (ocode
, TREE_TYPE (rhs
), lhs
);
4614 TREE_NO_WARNING (other
) = 1;
4615 other
= get_formal_tmp_var (other
, pre_p
);
4617 realpart
= code
== REALPART_EXPR
? rhs
: other
;
4618 imagpart
= code
== REALPART_EXPR
? other
: rhs
;
4620 if (TREE_CONSTANT (realpart
) && TREE_CONSTANT (imagpart
))
4621 new_rhs
= build_complex (TREE_TYPE (lhs
), realpart
, imagpart
);
4623 new_rhs
= build2 (COMPLEX_EXPR
, TREE_TYPE (lhs
), realpart
, imagpart
);
4625 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (lhs
, new_rhs
));
4626 *expr_p
= (want_value
) ? rhs
: NULL_TREE
;
4631 /* Gimplify the MODIFY_EXPR node pointed to by EXPR_P.
4637 PRE_P points to the list where side effects that must happen before
4638 *EXPR_P should be stored.
4640 POST_P points to the list where side effects that must happen after
4641 *EXPR_P should be stored.
4643 WANT_VALUE is nonzero iff we want to use the value of this expression
4644 in another expression. */
4646 static enum gimplify_status
4647 gimplify_modify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
4650 tree
*from_p
= &TREE_OPERAND (*expr_p
, 1);
4651 tree
*to_p
= &TREE_OPERAND (*expr_p
, 0);
4652 enum gimplify_status ret
= GS_UNHANDLED
;
4654 location_t loc
= EXPR_LOCATION (*expr_p
);
4655 gimple_stmt_iterator gsi
;
4657 gcc_assert (TREE_CODE (*expr_p
) == MODIFY_EXPR
4658 || TREE_CODE (*expr_p
) == INIT_EXPR
);
4660 /* Trying to simplify a clobber using normal logic doesn't work,
4661 so handle it here. */
4662 if (TREE_CLOBBER_P (*from_p
))
4664 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
4665 if (ret
== GS_ERROR
)
4667 gcc_assert (!want_value
4668 && (TREE_CODE (*to_p
) == VAR_DECL
4669 || TREE_CODE (*to_p
) == MEM_REF
));
4670 gimplify_seq_add_stmt (pre_p
, gimple_build_assign (*to_p
, *from_p
));
4675 /* Insert pointer conversions required by the middle-end that are not
4676 required by the frontend. This fixes middle-end type checking for
4677 for example gcc.dg/redecl-6.c. */
4678 if (POINTER_TYPE_P (TREE_TYPE (*to_p
)))
4680 STRIP_USELESS_TYPE_CONVERSION (*from_p
);
4681 if (!useless_type_conversion_p (TREE_TYPE (*to_p
), TREE_TYPE (*from_p
)))
4682 *from_p
= fold_convert_loc (loc
, TREE_TYPE (*to_p
), *from_p
);
4685 /* See if any simplifications can be done based on what the RHS is. */
4686 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
4688 if (ret
!= GS_UNHANDLED
)
4691 /* For zero sized types only gimplify the left hand side and right hand
4692 side as statements and throw away the assignment. Do this after
4693 gimplify_modify_expr_rhs so we handle TARGET_EXPRs of addressable
4695 if (zero_sized_type (TREE_TYPE (*from_p
)) && !want_value
)
4697 gimplify_stmt (from_p
, pre_p
);
4698 gimplify_stmt (to_p
, pre_p
);
4699 *expr_p
= NULL_TREE
;
4703 /* If the value being copied is of variable width, compute the length
4704 of the copy into a WITH_SIZE_EXPR. Note that we need to do this
4705 before gimplifying any of the operands so that we can resolve any
4706 PLACEHOLDER_EXPRs in the size. Also note that the RTL expander uses
4707 the size of the expression to be copied, not of the destination, so
4708 that is what we must do here. */
4709 maybe_with_size_expr (from_p
);
4711 ret
= gimplify_expr (to_p
, pre_p
, post_p
, is_gimple_lvalue
, fb_lvalue
);
4712 if (ret
== GS_ERROR
)
4715 /* As a special case, we have to temporarily allow for assignments
4716 with a CALL_EXPR on the RHS. Since in GIMPLE a function call is
4717 a toplevel statement, when gimplifying the GENERIC expression
4718 MODIFY_EXPR <a, CALL_EXPR <foo>>, we cannot create the tuple
4719 GIMPLE_ASSIGN <a, GIMPLE_CALL <foo>>.
4721 Instead, we need to create the tuple GIMPLE_CALL <a, foo>. To
4722 prevent gimplify_expr from trying to create a new temporary for
4723 foo's LHS, we tell it that it should only gimplify until it
4724 reaches the CALL_EXPR. On return from gimplify_expr, the newly
4725 created GIMPLE_CALL <foo> will be the last statement in *PRE_P
4726 and all we need to do here is set 'a' to be its LHS. */
4727 ret
= gimplify_expr (from_p
, pre_p
, post_p
, rhs_predicate_for (*to_p
),
4729 if (ret
== GS_ERROR
)
4732 /* In case of va_arg internal fn wrappped in a WITH_SIZE_EXPR, add the type
4733 size as argument to the call. */
4734 if (TREE_CODE (*from_p
) == WITH_SIZE_EXPR
)
4736 tree call
= TREE_OPERAND (*from_p
, 0);
4737 tree vlasize
= TREE_OPERAND (*from_p
, 1);
4739 if (TREE_CODE (call
) == CALL_EXPR
4740 && CALL_EXPR_IFN (call
) == IFN_VA_ARG
)
4742 int nargs
= call_expr_nargs (call
);
4743 tree type
= TREE_TYPE (call
);
4744 tree ap
= CALL_EXPR_ARG (call
, 0);
4745 tree tag
= CALL_EXPR_ARG (call
, 1);
4746 tree aptag
= CALL_EXPR_ARG (call
, 2);
4747 tree newcall
= build_call_expr_internal_loc (EXPR_LOCATION (call
),
4751 TREE_OPERAND (*from_p
, 0) = newcall
;
4755 /* Now see if the above changed *from_p to something we handle specially. */
4756 ret
= gimplify_modify_expr_rhs (expr_p
, from_p
, to_p
, pre_p
, post_p
,
4758 if (ret
!= GS_UNHANDLED
)
4761 /* If we've got a variable sized assignment between two lvalues (i.e. does
4762 not involve a call), then we can make things a bit more straightforward
4763 by converting the assignment to memcpy or memset. */
4764 if (TREE_CODE (*from_p
) == WITH_SIZE_EXPR
)
4766 tree from
= TREE_OPERAND (*from_p
, 0);
4767 tree size
= TREE_OPERAND (*from_p
, 1);
4769 if (TREE_CODE (from
) == CONSTRUCTOR
)
4770 return gimplify_modify_expr_to_memset (expr_p
, size
, want_value
, pre_p
);
4772 if (is_gimple_addressable (from
))
4775 return gimplify_modify_expr_to_memcpy (expr_p
, size
, want_value
,
4780 /* Transform partial stores to non-addressable complex variables into
4781 total stores. This allows us to use real instead of virtual operands
4782 for these variables, which improves optimization. */
4783 if ((TREE_CODE (*to_p
) == REALPART_EXPR
4784 || TREE_CODE (*to_p
) == IMAGPART_EXPR
)
4785 && is_gimple_reg (TREE_OPERAND (*to_p
, 0)))
4786 return gimplify_modify_expr_complex_part (expr_p
, pre_p
, want_value
);
4788 /* Try to alleviate the effects of the gimplification creating artificial
4789 temporaries (see for example is_gimple_reg_rhs) on the debug info, but
4790 make sure not to create DECL_DEBUG_EXPR links across functions. */
4791 if (!gimplify_ctxp
->into_ssa
4792 && TREE_CODE (*from_p
) == VAR_DECL
4793 && DECL_IGNORED_P (*from_p
)
4795 && !DECL_IGNORED_P (*to_p
)
4796 && decl_function_context (*to_p
) == current_function_decl
)
4798 if (!DECL_NAME (*from_p
) && DECL_NAME (*to_p
))
4800 = create_tmp_var_name (IDENTIFIER_POINTER (DECL_NAME (*to_p
)));
4801 DECL_HAS_DEBUG_EXPR_P (*from_p
) = 1;
4802 SET_DECL_DEBUG_EXPR (*from_p
, *to_p
);
4805 if (want_value
&& TREE_THIS_VOLATILE (*to_p
))
4806 *from_p
= get_initialized_tmp_var (*from_p
, pre_p
, post_p
);
4808 if (TREE_CODE (*from_p
) == CALL_EXPR
)
4810 /* Since the RHS is a CALL_EXPR, we need to create a GIMPLE_CALL
4811 instead of a GIMPLE_ASSIGN. */
4813 if (CALL_EXPR_FN (*from_p
) == NULL_TREE
)
4815 /* Gimplify internal functions created in the FEs. */
4816 int nargs
= call_expr_nargs (*from_p
), i
;
4817 enum internal_fn ifn
= CALL_EXPR_IFN (*from_p
);
4818 auto_vec
<tree
> vargs (nargs
);
4820 for (i
= 0; i
< nargs
; i
++)
4822 gimplify_arg (&CALL_EXPR_ARG (*from_p
, i
), pre_p
,
4823 EXPR_LOCATION (*from_p
));
4824 vargs
.quick_push (CALL_EXPR_ARG (*from_p
, i
));
4826 call_stmt
= gimple_build_call_internal_vec (ifn
, vargs
);
4827 gimple_set_location (call_stmt
, EXPR_LOCATION (*expr_p
));
4831 tree fnptrtype
= TREE_TYPE (CALL_EXPR_FN (*from_p
));
4832 CALL_EXPR_FN (*from_p
) = TREE_OPERAND (CALL_EXPR_FN (*from_p
), 0);
4833 STRIP_USELESS_TYPE_CONVERSION (CALL_EXPR_FN (*from_p
));
4834 tree fndecl
= get_callee_fndecl (*from_p
);
4836 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
4837 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
4838 && call_expr_nargs (*from_p
) == 3)
4839 call_stmt
= gimple_build_call_internal (IFN_BUILTIN_EXPECT
, 3,
4840 CALL_EXPR_ARG (*from_p
, 0),
4841 CALL_EXPR_ARG (*from_p
, 1),
4842 CALL_EXPR_ARG (*from_p
, 2));
4845 call_stmt
= gimple_build_call_from_tree (*from_p
);
4846 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fnptrtype
));
4849 notice_special_calls (call_stmt
);
4850 if (!gimple_call_noreturn_p (call_stmt
)
4851 || TREE_ADDRESSABLE (TREE_TYPE (*to_p
))
4852 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (*to_p
))) != INTEGER_CST
)
4853 gimple_call_set_lhs (call_stmt
, *to_p
);
4854 else if (TREE_CODE (*to_p
) == SSA_NAME
)
4855 /* The above is somewhat premature, avoid ICEing later for a
4856 SSA name w/o a definition. We may have uses in the GIMPLE IL.
4857 ??? This doesn't make it a default-def. */
4858 SSA_NAME_DEF_STMT (*to_p
) = gimple_build_nop ();
4863 assign
= gimple_build_assign (*to_p
, *from_p
);
4864 gimple_set_location (assign
, EXPR_LOCATION (*expr_p
));
4865 if (COMPARISON_CLASS_P (*from_p
))
4866 gimple_set_no_warning (assign
, TREE_NO_WARNING (*from_p
));
4869 if (gimplify_ctxp
->into_ssa
&& is_gimple_reg (*to_p
))
4871 /* We should have got an SSA name from the start. */
4872 gcc_assert (TREE_CODE (*to_p
) == SSA_NAME
4873 || ! gimple_in_ssa_p (cfun
));
4876 gimplify_seq_add_stmt (pre_p
, assign
);
4877 gsi
= gsi_last (*pre_p
);
4878 maybe_fold_stmt (&gsi
);
4882 *expr_p
= TREE_THIS_VOLATILE (*to_p
) ? *from_p
: unshare_expr (*to_p
);
4891 /* Gimplify a comparison between two variable-sized objects. Do this
4892 with a call to BUILT_IN_MEMCMP. */
4894 static enum gimplify_status
4895 gimplify_variable_sized_compare (tree
*expr_p
)
4897 location_t loc
= EXPR_LOCATION (*expr_p
);
4898 tree op0
= TREE_OPERAND (*expr_p
, 0);
4899 tree op1
= TREE_OPERAND (*expr_p
, 1);
4900 tree t
, arg
, dest
, src
, expr
;
4902 arg
= TYPE_SIZE_UNIT (TREE_TYPE (op0
));
4903 arg
= unshare_expr (arg
);
4904 arg
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (arg
, op0
);
4905 src
= build_fold_addr_expr_loc (loc
, op1
);
4906 dest
= build_fold_addr_expr_loc (loc
, op0
);
4907 t
= builtin_decl_implicit (BUILT_IN_MEMCMP
);
4908 t
= build_call_expr_loc (loc
, t
, 3, dest
, src
, arg
);
4911 = build2 (TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), t
, integer_zero_node
);
4912 SET_EXPR_LOCATION (expr
, loc
);
4918 /* Gimplify a comparison between two aggregate objects of integral scalar
4919 mode as a comparison between the bitwise equivalent scalar values. */
4921 static enum gimplify_status
4922 gimplify_scalar_mode_aggregate_compare (tree
*expr_p
)
4924 location_t loc
= EXPR_LOCATION (*expr_p
);
4925 tree op0
= TREE_OPERAND (*expr_p
, 0);
4926 tree op1
= TREE_OPERAND (*expr_p
, 1);
4928 tree type
= TREE_TYPE (op0
);
4929 tree scalar_type
= lang_hooks
.types
.type_for_mode (TYPE_MODE (type
), 1);
4931 op0
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op0
);
4932 op1
= fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, scalar_type
, op1
);
4935 = fold_build2_loc (loc
, TREE_CODE (*expr_p
), TREE_TYPE (*expr_p
), op0
, op1
);
4940 /* Gimplify an expression sequence. This function gimplifies each
4941 expression and rewrites the original expression with the last
4942 expression of the sequence in GIMPLE form.
4944 PRE_P points to the list where the side effects for all the
4945 expressions in the sequence will be emitted.
4947 WANT_VALUE is true when the result of the last COMPOUND_EXPR is used. */
4949 static enum gimplify_status
4950 gimplify_compound_expr (tree
*expr_p
, gimple_seq
*pre_p
, bool want_value
)
4956 tree
*sub_p
= &TREE_OPERAND (t
, 0);
4958 if (TREE_CODE (*sub_p
) == COMPOUND_EXPR
)
4959 gimplify_compound_expr (sub_p
, pre_p
, false);
4961 gimplify_stmt (sub_p
, pre_p
);
4963 t
= TREE_OPERAND (t
, 1);
4965 while (TREE_CODE (t
) == COMPOUND_EXPR
);
4972 gimplify_stmt (expr_p
, pre_p
);
4977 /* Gimplify a SAVE_EXPR node. EXPR_P points to the expression to
4978 gimplify. After gimplification, EXPR_P will point to a new temporary
4979 that holds the original value of the SAVE_EXPR node.
4981 PRE_P points to the list where side effects that must happen before
4982 *EXPR_P should be stored. */
4984 static enum gimplify_status
4985 gimplify_save_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
4987 enum gimplify_status ret
= GS_ALL_DONE
;
4990 gcc_assert (TREE_CODE (*expr_p
) == SAVE_EXPR
);
4991 val
= TREE_OPERAND (*expr_p
, 0);
4993 /* If the SAVE_EXPR has not been resolved, then evaluate it once. */
4994 if (!SAVE_EXPR_RESOLVED_P (*expr_p
))
4996 /* The operand may be a void-valued expression such as SAVE_EXPRs
4997 generated by the Java frontend for class initialization. It is
4998 being executed only for its side-effects. */
4999 if (TREE_TYPE (val
) == void_type_node
)
5001 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
5002 is_gimple_stmt
, fb_none
);
5006 /* The temporary may not be an SSA name as later abnormal and EH
5007 control flow may invalidate use/def domination. */
5008 val
= get_initialized_tmp_var (val
, pre_p
, post_p
, false);
5010 TREE_OPERAND (*expr_p
, 0) = val
;
5011 SAVE_EXPR_RESOLVED_P (*expr_p
) = 1;
5019 /* Rewrite the ADDR_EXPR node pointed to by EXPR_P
5026 PRE_P points to the list where side effects that must happen before
5027 *EXPR_P should be stored.
5029 POST_P points to the list where side effects that must happen after
5030 *EXPR_P should be stored. */
5032 static enum gimplify_status
5033 gimplify_addr_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5035 tree expr
= *expr_p
;
5036 tree op0
= TREE_OPERAND (expr
, 0);
5037 enum gimplify_status ret
;
5038 location_t loc
= EXPR_LOCATION (*expr_p
);
5040 switch (TREE_CODE (op0
))
5044 /* Check if we are dealing with an expression of the form '&*ptr'.
5045 While the front end folds away '&*ptr' into 'ptr', these
5046 expressions may be generated internally by the compiler (e.g.,
5047 builtins like __builtin_va_end). */
5048 /* Caution: the silent array decomposition semantics we allow for
5049 ADDR_EXPR means we can't always discard the pair. */
5050 /* Gimplification of the ADDR_EXPR operand may drop
5051 cv-qualification conversions, so make sure we add them if
5054 tree op00
= TREE_OPERAND (op0
, 0);
5055 tree t_expr
= TREE_TYPE (expr
);
5056 tree t_op00
= TREE_TYPE (op00
);
5058 if (!useless_type_conversion_p (t_expr
, t_op00
))
5059 op00
= fold_convert_loc (loc
, TREE_TYPE (expr
), op00
);
5065 case VIEW_CONVERT_EXPR
:
5066 /* Take the address of our operand and then convert it to the type of
5069 ??? The interactions of VIEW_CONVERT_EXPR and aliasing is not at
5070 all clear. The impact of this transformation is even less clear. */
5072 /* If the operand is a useless conversion, look through it. Doing so
5073 guarantees that the ADDR_EXPR and its operand will remain of the
5075 if (tree_ssa_useless_type_conversion (TREE_OPERAND (op0
, 0)))
5076 op0
= TREE_OPERAND (op0
, 0);
5078 *expr_p
= fold_convert_loc (loc
, TREE_TYPE (expr
),
5079 build_fold_addr_expr_loc (loc
,
5080 TREE_OPERAND (op0
, 0)));
5085 if (integer_zerop (TREE_OPERAND (op0
, 1)))
5086 goto do_indirect_ref
;
5088 /* ... fall through ... */
5091 /* If we see a call to a declared builtin or see its address
5092 being taken (we can unify those cases here) then we can mark
5093 the builtin for implicit generation by GCC. */
5094 if (TREE_CODE (op0
) == FUNCTION_DECL
5095 && DECL_BUILT_IN_CLASS (op0
) == BUILT_IN_NORMAL
5096 && builtin_decl_declared_p (DECL_FUNCTION_CODE (op0
)))
5097 set_builtin_decl_implicit_p (DECL_FUNCTION_CODE (op0
), true);
5099 /* We use fb_either here because the C frontend sometimes takes
5100 the address of a call that returns a struct; see
5101 gcc.dg/c99-array-lval-1.c. The gimplifier will correctly make
5102 the implied temporary explicit. */
5104 /* Make the operand addressable. */
5105 ret
= gimplify_expr (&TREE_OPERAND (expr
, 0), pre_p
, post_p
,
5106 is_gimple_addressable
, fb_either
);
5107 if (ret
== GS_ERROR
)
5110 /* Then mark it. Beware that it may not be possible to do so directly
5111 if a temporary has been created by the gimplification. */
5112 prepare_gimple_addressable (&TREE_OPERAND (expr
, 0), pre_p
);
5114 op0
= TREE_OPERAND (expr
, 0);
5116 /* For various reasons, the gimplification of the expression
5117 may have made a new INDIRECT_REF. */
5118 if (TREE_CODE (op0
) == INDIRECT_REF
)
5119 goto do_indirect_ref
;
5121 mark_addressable (TREE_OPERAND (expr
, 0));
5123 /* The FEs may end up building ADDR_EXPRs early on a decl with
5124 an incomplete type. Re-build ADDR_EXPRs in canonical form
5126 if (!types_compatible_p (TREE_TYPE (op0
), TREE_TYPE (TREE_TYPE (expr
))))
5127 *expr_p
= build_fold_addr_expr (op0
);
5129 /* Make sure TREE_CONSTANT and TREE_SIDE_EFFECTS are set properly. */
5130 recompute_tree_invariant_for_addr_expr (*expr_p
);
5132 /* If we re-built the ADDR_EXPR add a conversion to the original type
5134 if (!useless_type_conversion_p (TREE_TYPE (expr
), TREE_TYPE (*expr_p
)))
5135 *expr_p
= fold_convert (TREE_TYPE (expr
), *expr_p
);
5143 /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple
5144 value; output operands should be a gimple lvalue. */
5146 static enum gimplify_status
5147 gimplify_asm_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5151 const char **oconstraints
;
5154 const char *constraint
;
5155 bool allows_mem
, allows_reg
, is_inout
;
5156 enum gimplify_status ret
, tret
;
5158 vec
<tree
, va_gc
> *inputs
;
5159 vec
<tree
, va_gc
> *outputs
;
5160 vec
<tree
, va_gc
> *clobbers
;
5161 vec
<tree
, va_gc
> *labels
;
5165 noutputs
= list_length (ASM_OUTPUTS (expr
));
5166 oconstraints
= (const char **) alloca ((noutputs
) * sizeof (const char *));
5174 link_next
= NULL_TREE
;
5175 for (i
= 0, link
= ASM_OUTPUTS (expr
); link
; ++i
, link
= link_next
)
5178 size_t constraint_len
;
5180 link_next
= TREE_CHAIN (link
);
5184 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5185 constraint_len
= strlen (constraint
);
5186 if (constraint_len
== 0)
5189 ok
= parse_output_constraint (&constraint
, i
, 0, 0,
5190 &allows_mem
, &allows_reg
, &is_inout
);
5197 if (!allows_reg
&& allows_mem
)
5198 mark_addressable (TREE_VALUE (link
));
5200 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5201 is_inout
? is_gimple_min_lval
: is_gimple_lvalue
,
5202 fb_lvalue
| fb_mayfail
);
5203 if (tret
== GS_ERROR
)
5205 error ("invalid lvalue in asm output %d", i
);
5209 /* If the constraint does not allow memory make sure we gimplify
5210 it to a register if it is not already but its base is. This
5211 happens for complex and vector components. */
5214 tree op
= TREE_VALUE (link
);
5215 if (! is_gimple_val (op
)
5216 && is_gimple_reg_type (TREE_TYPE (op
))
5217 && is_gimple_reg (get_base_address (op
)))
5219 tree tem
= create_tmp_reg (TREE_TYPE (op
));
5223 ass
= build2 (MODIFY_EXPR
, TREE_TYPE (tem
),
5224 tem
, unshare_expr (op
));
5225 gimplify_and_add (ass
, pre_p
);
5227 ass
= build2 (MODIFY_EXPR
, TREE_TYPE (tem
), op
, tem
);
5228 gimplify_and_add (ass
, post_p
);
5230 TREE_VALUE (link
) = tem
;
5235 vec_safe_push (outputs
, link
);
5236 TREE_CHAIN (link
) = NULL_TREE
;
5240 /* An input/output operand. To give the optimizers more
5241 flexibility, split it into separate input and output
5246 /* Turn the in/out constraint into an output constraint. */
5247 char *p
= xstrdup (constraint
);
5249 TREE_VALUE (TREE_PURPOSE (link
)) = build_string (constraint_len
, p
);
5251 /* And add a matching input constraint. */
5254 sprintf (buf
, "%d", i
);
5256 /* If there are multiple alternatives in the constraint,
5257 handle each of them individually. Those that allow register
5258 will be replaced with operand number, the others will stay
5260 if (strchr (p
, ',') != NULL
)
5262 size_t len
= 0, buflen
= strlen (buf
);
5263 char *beg
, *end
, *str
, *dst
;
5267 end
= strchr (beg
, ',');
5269 end
= strchr (beg
, '\0');
5270 if ((size_t) (end
- beg
) < buflen
)
5273 len
+= end
- beg
+ 1;
5280 str
= (char *) alloca (len
);
5281 for (beg
= p
+ 1, dst
= str
;;)
5284 bool mem_p
, reg_p
, inout_p
;
5286 end
= strchr (beg
, ',');
5291 parse_output_constraint (&tem
, i
, 0, 0,
5292 &mem_p
, ®_p
, &inout_p
);
5297 memcpy (dst
, buf
, buflen
);
5306 memcpy (dst
, beg
, len
);
5315 input
= build_string (dst
- str
, str
);
5318 input
= build_string (strlen (buf
), buf
);
5321 input
= build_string (constraint_len
- 1, constraint
+ 1);
5325 input
= build_tree_list (build_tree_list (NULL_TREE
, input
),
5326 unshare_expr (TREE_VALUE (link
)));
5327 ASM_INPUTS (expr
) = chainon (ASM_INPUTS (expr
), input
);
5331 link_next
= NULL_TREE
;
5332 for (link
= ASM_INPUTS (expr
); link
; ++i
, link
= link_next
)
5334 link_next
= TREE_CHAIN (link
);
5335 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5336 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0,
5337 oconstraints
, &allows_mem
, &allows_reg
);
5339 /* If we can't make copies, we can only accept memory. */
5340 if (TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (link
))))
5346 error ("impossible constraint in %<asm%>");
5347 error ("non-memory input %d must stay in memory", i
);
5352 /* If the operand is a memory input, it should be an lvalue. */
5353 if (!allows_reg
&& allows_mem
)
5355 tree inputv
= TREE_VALUE (link
);
5356 STRIP_NOPS (inputv
);
5357 if (TREE_CODE (inputv
) == PREDECREMENT_EXPR
5358 || TREE_CODE (inputv
) == PREINCREMENT_EXPR
5359 || TREE_CODE (inputv
) == POSTDECREMENT_EXPR
5360 || TREE_CODE (inputv
) == POSTINCREMENT_EXPR
5361 || TREE_CODE (inputv
) == MODIFY_EXPR
)
5362 TREE_VALUE (link
) = error_mark_node
;
5363 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5364 is_gimple_lvalue
, fb_lvalue
| fb_mayfail
);
5365 if (tret
!= GS_ERROR
)
5367 /* Unlike output operands, memory inputs are not guaranteed
5368 to be lvalues by the FE, and while the expressions are
5369 marked addressable there, if it is e.g. a statement
5370 expression, temporaries in it might not end up being
5371 addressable. They might be already used in the IL and thus
5372 it is too late to make them addressable now though. */
5373 tree x
= TREE_VALUE (link
);
5374 while (handled_component_p (x
))
5375 x
= TREE_OPERAND (x
, 0);
5376 if (TREE_CODE (x
) == MEM_REF
5377 && TREE_CODE (TREE_OPERAND (x
, 0)) == ADDR_EXPR
)
5378 x
= TREE_OPERAND (TREE_OPERAND (x
, 0), 0);
5379 if ((TREE_CODE (x
) == VAR_DECL
5380 || TREE_CODE (x
) == PARM_DECL
5381 || TREE_CODE (x
) == RESULT_DECL
)
5382 && !TREE_ADDRESSABLE (x
)
5383 && is_gimple_reg (x
))
5385 warning_at (EXPR_LOC_OR_LOC (TREE_VALUE (link
),
5387 "memory input %d is not directly addressable",
5389 prepare_gimple_addressable (&TREE_VALUE (link
), pre_p
);
5392 mark_addressable (TREE_VALUE (link
));
5393 if (tret
== GS_ERROR
)
5395 error_at (EXPR_LOC_OR_LOC (TREE_VALUE (link
), input_location
),
5396 "memory input %d is not directly addressable", i
);
5402 tret
= gimplify_expr (&TREE_VALUE (link
), pre_p
, post_p
,
5403 is_gimple_asm_val
, fb_rvalue
);
5404 if (tret
== GS_ERROR
)
5408 TREE_CHAIN (link
) = NULL_TREE
;
5409 vec_safe_push (inputs
, link
);
5412 link_next
= NULL_TREE
;
5413 for (link
= ASM_CLOBBERS (expr
); link
; ++i
, link
= link_next
)
5415 link_next
= TREE_CHAIN (link
);
5416 TREE_CHAIN (link
) = NULL_TREE
;
5417 vec_safe_push (clobbers
, link
);
5420 link_next
= NULL_TREE
;
5421 for (link
= ASM_LABELS (expr
); link
; ++i
, link
= link_next
)
5423 link_next
= TREE_CHAIN (link
);
5424 TREE_CHAIN (link
) = NULL_TREE
;
5425 vec_safe_push (labels
, link
);
5428 /* Do not add ASMs with errors to the gimple IL stream. */
5429 if (ret
!= GS_ERROR
)
5431 stmt
= gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr
)),
5432 inputs
, outputs
, clobbers
, labels
);
5434 gimple_asm_set_volatile (stmt
, ASM_VOLATILE_P (expr
) || noutputs
== 0);
5435 gimple_asm_set_input (stmt
, ASM_INPUT_P (expr
));
5437 gimplify_seq_add_stmt (pre_p
, stmt
);
5443 /* Gimplify a CLEANUP_POINT_EXPR. Currently this works by adding
5444 GIMPLE_WITH_CLEANUP_EXPRs to the prequeue as we encounter cleanups while
5445 gimplifying the body, and converting them to TRY_FINALLY_EXPRs when we
5446 return to this function.
5448 FIXME should we complexify the prequeue handling instead? Or use flags
5449 for all the cleanups and let the optimizer tighten them up? The current
5450 code seems pretty fragile; it will break on a cleanup within any
5451 non-conditional nesting. But any such nesting would be broken, anyway;
5452 we can't write a TRY_FINALLY_EXPR that starts inside a nesting construct
5453 and continues out of it. We can do that at the RTL level, though, so
5454 having an optimizer to tighten up try/finally regions would be a Good
5457 static enum gimplify_status
5458 gimplify_cleanup_point_expr (tree
*expr_p
, gimple_seq
*pre_p
)
5460 gimple_stmt_iterator iter
;
5461 gimple_seq body_sequence
= NULL
;
5463 tree temp
= voidify_wrapper_expr (*expr_p
, NULL
);
5465 /* We only care about the number of conditions between the innermost
5466 CLEANUP_POINT_EXPR and the cleanup. So save and reset the count and
5467 any cleanups collected outside the CLEANUP_POINT_EXPR. */
5468 int old_conds
= gimplify_ctxp
->conditions
;
5469 gimple_seq old_cleanups
= gimplify_ctxp
->conditional_cleanups
;
5470 bool old_in_cleanup_point_expr
= gimplify_ctxp
->in_cleanup_point_expr
;
5471 gimplify_ctxp
->conditions
= 0;
5472 gimplify_ctxp
->conditional_cleanups
= NULL
;
5473 gimplify_ctxp
->in_cleanup_point_expr
= true;
5475 gimplify_stmt (&TREE_OPERAND (*expr_p
, 0), &body_sequence
);
5477 gimplify_ctxp
->conditions
= old_conds
;
5478 gimplify_ctxp
->conditional_cleanups
= old_cleanups
;
5479 gimplify_ctxp
->in_cleanup_point_expr
= old_in_cleanup_point_expr
;
5481 for (iter
= gsi_start (body_sequence
); !gsi_end_p (iter
); )
5483 gimple
*wce
= gsi_stmt (iter
);
5485 if (gimple_code (wce
) == GIMPLE_WITH_CLEANUP_EXPR
)
5487 if (gsi_one_before_end_p (iter
))
5489 /* Note that gsi_insert_seq_before and gsi_remove do not
5490 scan operands, unlike some other sequence mutators. */
5491 if (!gimple_wce_cleanup_eh_only (wce
))
5492 gsi_insert_seq_before_without_update (&iter
,
5493 gimple_wce_cleanup (wce
),
5495 gsi_remove (&iter
, true);
5502 enum gimple_try_flags kind
;
5504 if (gimple_wce_cleanup_eh_only (wce
))
5505 kind
= GIMPLE_TRY_CATCH
;
5507 kind
= GIMPLE_TRY_FINALLY
;
5508 seq
= gsi_split_seq_after (iter
);
5510 gtry
= gimple_build_try (seq
, gimple_wce_cleanup (wce
), kind
);
5511 /* Do not use gsi_replace here, as it may scan operands.
5512 We want to do a simple structural modification only. */
5513 gsi_set_stmt (&iter
, gtry
);
5514 iter
= gsi_start (gtry
->eval
);
5521 gimplify_seq_add_seq (pre_p
, body_sequence
);
5534 /* Insert a cleanup marker for gimplify_cleanup_point_expr. CLEANUP
5535 is the cleanup action required. EH_ONLY is true if the cleanup should
5536 only be executed if an exception is thrown, not on normal exit. */
5539 gimple_push_cleanup (tree var
, tree cleanup
, bool eh_only
, gimple_seq
*pre_p
)
5542 gimple_seq cleanup_stmts
= NULL
;
5544 /* Errors can result in improperly nested cleanups. Which results in
5545 confusion when trying to resolve the GIMPLE_WITH_CLEANUP_EXPR. */
5549 if (gimple_conditional_context ())
5551 /* If we're in a conditional context, this is more complex. We only
5552 want to run the cleanup if we actually ran the initialization that
5553 necessitates it, but we want to run it after the end of the
5554 conditional context. So we wrap the try/finally around the
5555 condition and use a flag to determine whether or not to actually
5556 run the destructor. Thus
5560 becomes (approximately)
5564 if (test) { A::A(temp); flag = 1; val = f(temp); }
5567 if (flag) A::~A(temp);
5571 tree flag
= create_tmp_var (boolean_type_node
, "cleanup");
5572 gassign
*ffalse
= gimple_build_assign (flag
, boolean_false_node
);
5573 gassign
*ftrue
= gimple_build_assign (flag
, boolean_true_node
);
5575 cleanup
= build3 (COND_EXPR
, void_type_node
, flag
, cleanup
, NULL
);
5576 gimplify_stmt (&cleanup
, &cleanup_stmts
);
5577 wce
= gimple_build_wce (cleanup_stmts
);
5579 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, ffalse
);
5580 gimplify_seq_add_stmt (&gimplify_ctxp
->conditional_cleanups
, wce
);
5581 gimplify_seq_add_stmt (pre_p
, ftrue
);
5583 /* Because of this manipulation, and the EH edges that jump
5584 threading cannot redirect, the temporary (VAR) will appear
5585 to be used uninitialized. Don't warn. */
5586 TREE_NO_WARNING (var
) = 1;
5590 gimplify_stmt (&cleanup
, &cleanup_stmts
);
5591 wce
= gimple_build_wce (cleanup_stmts
);
5592 gimple_wce_set_cleanup_eh_only (wce
, eh_only
);
5593 gimplify_seq_add_stmt (pre_p
, wce
);
5597 /* Gimplify a TARGET_EXPR which doesn't appear on the rhs of an INIT_EXPR. */
5599 static enum gimplify_status
5600 gimplify_target_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
5602 tree targ
= *expr_p
;
5603 tree temp
= TARGET_EXPR_SLOT (targ
);
5604 tree init
= TARGET_EXPR_INITIAL (targ
);
5605 enum gimplify_status ret
;
5609 tree cleanup
= NULL_TREE
;
5611 /* TARGET_EXPR temps aren't part of the enclosing block, so add it
5612 to the temps list. Handle also variable length TARGET_EXPRs. */
5613 if (TREE_CODE (DECL_SIZE (temp
)) != INTEGER_CST
)
5615 if (!TYPE_SIZES_GIMPLIFIED (TREE_TYPE (temp
)))
5616 gimplify_type_sizes (TREE_TYPE (temp
), pre_p
);
5617 gimplify_vla_decl (temp
, pre_p
);
5620 gimple_add_tmp_var (temp
);
5622 /* If TARGET_EXPR_INITIAL is void, then the mere evaluation of the
5623 expression is supposed to initialize the slot. */
5624 if (VOID_TYPE_P (TREE_TYPE (init
)))
5625 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
5628 tree init_expr
= build2 (INIT_EXPR
, void_type_node
, temp
, init
);
5630 ret
= gimplify_expr (&init
, pre_p
, post_p
, is_gimple_stmt
, fb_none
);
5632 ggc_free (init_expr
);
5634 if (ret
== GS_ERROR
)
5636 /* PR c++/28266 Make sure this is expanded only once. */
5637 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
5641 gimplify_and_add (init
, pre_p
);
5643 /* If needed, push the cleanup for the temp. */
5644 if (TARGET_EXPR_CLEANUP (targ
))
5646 if (CLEANUP_EH_ONLY (targ
))
5647 gimple_push_cleanup (temp
, TARGET_EXPR_CLEANUP (targ
),
5648 CLEANUP_EH_ONLY (targ
), pre_p
);
5650 cleanup
= TARGET_EXPR_CLEANUP (targ
);
5653 /* Add a clobber for the temporary going out of scope, like
5654 gimplify_bind_expr. */
5655 if (gimplify_ctxp
->in_cleanup_point_expr
5656 && needs_to_live_in_memory (temp
)
5657 && flag_stack_reuse
== SR_ALL
)
5659 tree clobber
= build_constructor (TREE_TYPE (temp
),
5661 TREE_THIS_VOLATILE (clobber
) = true;
5662 clobber
= build2 (MODIFY_EXPR
, TREE_TYPE (temp
), temp
, clobber
);
5664 cleanup
= build2 (COMPOUND_EXPR
, void_type_node
, cleanup
,
5671 gimple_push_cleanup (temp
, cleanup
, false, pre_p
);
5673 /* Only expand this once. */
5674 TREE_OPERAND (targ
, 3) = init
;
5675 TARGET_EXPR_INITIAL (targ
) = NULL_TREE
;
5678 /* We should have expanded this before. */
5679 gcc_assert (DECL_SEEN_IN_BIND_EXPR_P (temp
));
5685 /* Gimplification of expression trees. */
5687 /* Gimplify an expression which appears at statement context. The
5688 corresponding GIMPLE statements are added to *SEQ_P. If *SEQ_P is
5689 NULL, a new sequence is allocated.
5691 Return true if we actually added a statement to the queue. */
5694 gimplify_stmt (tree
*stmt_p
, gimple_seq
*seq_p
)
5696 gimple_seq_node last
;
5698 last
= gimple_seq_last (*seq_p
);
5699 gimplify_expr (stmt_p
, seq_p
, NULL
, is_gimple_stmt
, fb_none
);
5700 return last
!= gimple_seq_last (*seq_p
);
5703 /* Add FIRSTPRIVATE entries for DECL in the OpenMP the surrounding parallels
5704 to CTX. If entries already exist, force them to be some flavor of private.
5705 If there is no enclosing parallel, do nothing. */
5708 omp_firstprivatize_variable (struct gimplify_omp_ctx
*ctx
, tree decl
)
5712 if (decl
== NULL
|| !DECL_P (decl
) || ctx
->region_type
== ORT_NONE
)
5717 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5720 if (n
->value
& GOVD_SHARED
)
5721 n
->value
= GOVD_FIRSTPRIVATE
| (n
->value
& GOVD_SEEN
);
5722 else if (n
->value
& GOVD_MAP
)
5723 n
->value
|= GOVD_MAP_TO_ONLY
;
5727 else if ((ctx
->region_type
& ORT_TARGET
) != 0)
5729 if (ctx
->target_map_scalars_firstprivate
)
5730 omp_add_variable (ctx
, decl
, GOVD_FIRSTPRIVATE
);
5732 omp_add_variable (ctx
, decl
, GOVD_MAP
| GOVD_MAP_TO_ONLY
);
5734 else if (ctx
->region_type
!= ORT_WORKSHARE
5735 && ctx
->region_type
!= ORT_SIMD
5736 && ctx
->region_type
!= ORT_ACC
5737 && !(ctx
->region_type
& ORT_TARGET_DATA
))
5738 omp_add_variable (ctx
, decl
, GOVD_FIRSTPRIVATE
);
5740 ctx
= ctx
->outer_context
;
5745 /* Similarly for each of the type sizes of TYPE. */
5748 omp_firstprivatize_type_sizes (struct gimplify_omp_ctx
*ctx
, tree type
)
5750 if (type
== NULL
|| type
== error_mark_node
)
5752 type
= TYPE_MAIN_VARIANT (type
);
5754 if (ctx
->privatized_types
->add (type
))
5757 switch (TREE_CODE (type
))
5763 case FIXED_POINT_TYPE
:
5764 omp_firstprivatize_variable (ctx
, TYPE_MIN_VALUE (type
));
5765 omp_firstprivatize_variable (ctx
, TYPE_MAX_VALUE (type
));
5769 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
5770 omp_firstprivatize_type_sizes (ctx
, TYPE_DOMAIN (type
));
5775 case QUAL_UNION_TYPE
:
5778 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5779 if (TREE_CODE (field
) == FIELD_DECL
)
5781 omp_firstprivatize_variable (ctx
, DECL_FIELD_OFFSET (field
));
5782 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (field
));
5788 case REFERENCE_TYPE
:
5789 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (type
));
5796 omp_firstprivatize_variable (ctx
, TYPE_SIZE (type
));
5797 omp_firstprivatize_variable (ctx
, TYPE_SIZE_UNIT (type
));
5798 lang_hooks
.types
.omp_firstprivatize_type_sizes (ctx
, type
);
5801 /* Add an entry for DECL in the OMP context CTX with FLAGS. */
5804 omp_add_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, unsigned int flags
)
5807 unsigned int nflags
;
5810 if (error_operand_p (decl
) || ctx
->region_type
== ORT_NONE
)
5813 /* Never elide decls whose type has TREE_ADDRESSABLE set. This means
5814 there are constructors involved somewhere. */
5815 if (TREE_ADDRESSABLE (TREE_TYPE (decl
))
5816 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
5819 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5820 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
5822 /* We shouldn't be re-adding the decl with the same data
5824 gcc_assert ((n
->value
& GOVD_DATA_SHARE_CLASS
& flags
) == 0);
5825 nflags
= n
->value
| flags
;
5826 /* The only combination of data sharing classes we should see is
5827 FIRSTPRIVATE and LASTPRIVATE. However, OpenACC permits
5828 reduction variables to be used in data sharing clauses. */
5829 gcc_assert ((ctx
->region_type
& ORT_ACC
) != 0
5830 || ((nflags
& GOVD_DATA_SHARE_CLASS
)
5831 == (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
))
5832 || (flags
& GOVD_DATA_SHARE_CLASS
) == 0);
5837 /* When adding a variable-sized variable, we have to handle all sorts
5838 of additional bits of data: the pointer replacement variable, and
5839 the parameters of the type. */
5840 if (DECL_SIZE (decl
) && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
5842 /* Add the pointer replacement variable as PRIVATE if the variable
5843 replacement is private, else FIRSTPRIVATE since we'll need the
5844 address of the original variable either for SHARED, or for the
5845 copy into or out of the context. */
5846 if (!(flags
& GOVD_LOCAL
))
5848 if (flags
& GOVD_MAP
)
5849 nflags
= GOVD_MAP
| GOVD_MAP_TO_ONLY
| GOVD_EXPLICIT
;
5850 else if (flags
& GOVD_PRIVATE
)
5851 nflags
= GOVD_PRIVATE
;
5852 else if ((ctx
->region_type
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0
5853 && (flags
& GOVD_FIRSTPRIVATE
))
5854 nflags
= GOVD_PRIVATE
| GOVD_EXPLICIT
;
5856 nflags
= GOVD_FIRSTPRIVATE
;
5857 nflags
|= flags
& GOVD_SEEN
;
5858 t
= DECL_VALUE_EXPR (decl
);
5859 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
5860 t
= TREE_OPERAND (t
, 0);
5861 gcc_assert (DECL_P (t
));
5862 omp_add_variable (ctx
, t
, nflags
);
5865 /* Add all of the variable and type parameters (which should have
5866 been gimplified to a formal temporary) as FIRSTPRIVATE. */
5867 omp_firstprivatize_variable (ctx
, DECL_SIZE_UNIT (decl
));
5868 omp_firstprivatize_variable (ctx
, DECL_SIZE (decl
));
5869 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
5871 /* The variable-sized variable itself is never SHARED, only some form
5872 of PRIVATE. The sharing would take place via the pointer variable
5873 which we remapped above. */
5874 if (flags
& GOVD_SHARED
)
5875 flags
= GOVD_PRIVATE
| GOVD_DEBUG_PRIVATE
5876 | (flags
& (GOVD_SEEN
| GOVD_EXPLICIT
));
5878 /* We're going to make use of the TYPE_SIZE_UNIT at least in the
5879 alloca statement we generate for the variable, so make sure it
5880 is available. This isn't automatically needed for the SHARED
5881 case, since we won't be allocating local storage then.
5882 For local variables TYPE_SIZE_UNIT might not be gimplified yet,
5883 in this case omp_notice_variable will be called later
5884 on when it is gimplified. */
5885 else if (! (flags
& (GOVD_LOCAL
| GOVD_MAP
))
5886 && DECL_P (TYPE_SIZE_UNIT (TREE_TYPE (decl
))))
5887 omp_notice_variable (ctx
, TYPE_SIZE_UNIT (TREE_TYPE (decl
)), true);
5889 else if ((flags
& (GOVD_MAP
| GOVD_LOCAL
)) == 0
5890 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
5892 omp_firstprivatize_type_sizes (ctx
, TREE_TYPE (decl
));
5894 /* Similar to the direct variable sized case above, we'll need the
5895 size of references being privatized. */
5896 if ((flags
& GOVD_SHARED
) == 0)
5898 t
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)));
5900 omp_notice_variable (ctx
, t
, true);
5907 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, flags
);
5910 /* Notice a threadprivate variable DECL used in OMP context CTX.
5911 This just prints out diagnostics about threadprivate variable uses
5912 in untied tasks. If DECL2 is non-NULL, prevent this warning
5913 on that variable. */
5916 omp_notice_threadprivate_variable (struct gimplify_omp_ctx
*ctx
, tree decl
,
5920 struct gimplify_omp_ctx
*octx
;
5922 for (octx
= ctx
; octx
; octx
= octx
->outer_context
)
5923 if ((octx
->region_type
& ORT_TARGET
) != 0)
5925 n
= splay_tree_lookup (octx
->variables
, (splay_tree_key
)decl
);
5928 error ("threadprivate variable %qE used in target region",
5930 error_at (octx
->location
, "enclosing target region");
5931 splay_tree_insert (octx
->variables
, (splay_tree_key
)decl
, 0);
5934 splay_tree_insert (octx
->variables
, (splay_tree_key
)decl2
, 0);
5937 if (ctx
->region_type
!= ORT_UNTIED_TASK
)
5939 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
5942 error ("threadprivate variable %qE used in untied task",
5944 error_at (ctx
->location
, "enclosing task");
5945 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl
, 0);
5948 splay_tree_insert (ctx
->variables
, (splay_tree_key
)decl2
, 0);
5952 /* Return true if global var DECL is device resident. */
5955 device_resident_p (tree decl
)
5957 tree attr
= lookup_attribute ("oacc declare target", DECL_ATTRIBUTES (decl
));
5962 for (tree t
= TREE_VALUE (attr
); t
; t
= TREE_PURPOSE (t
))
5964 tree c
= TREE_VALUE (t
);
5965 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DEVICE_RESIDENT
)
5972 /* Determine outer default flags for DECL mentioned in an OMP region
5973 but not declared in an enclosing clause.
5975 ??? Some compiler-generated variables (like SAVE_EXPRs) could be
5976 remapped firstprivate instead of shared. To some extent this is
5977 addressed in omp_firstprivatize_type_sizes, but not
5981 omp_default_clause (struct gimplify_omp_ctx
*ctx
, tree decl
,
5982 bool in_code
, unsigned flags
)
5984 enum omp_clause_default_kind default_kind
= ctx
->default_kind
;
5985 enum omp_clause_default_kind kind
;
5987 kind
= lang_hooks
.decls
.omp_predetermined_sharing (decl
);
5988 if (kind
!= OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
5989 default_kind
= kind
;
5991 switch (default_kind
)
5993 case OMP_CLAUSE_DEFAULT_NONE
:
5997 if (ctx
->region_type
& ORT_PARALLEL
)
5999 else if (ctx
->region_type
& ORT_TASK
)
6001 else if (ctx
->region_type
& ORT_TEAMS
)
6006 error ("%qE not specified in enclosing %s",
6007 DECL_NAME (lang_hooks
.decls
.omp_report_decl (decl
)), rtype
);
6008 error_at (ctx
->location
, "enclosing %s", rtype
);
6011 case OMP_CLAUSE_DEFAULT_SHARED
:
6012 flags
|= GOVD_SHARED
;
6014 case OMP_CLAUSE_DEFAULT_PRIVATE
:
6015 flags
|= GOVD_PRIVATE
;
6017 case OMP_CLAUSE_DEFAULT_FIRSTPRIVATE
:
6018 flags
|= GOVD_FIRSTPRIVATE
;
6020 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
6021 /* decl will be either GOVD_FIRSTPRIVATE or GOVD_SHARED. */
6022 gcc_assert ((ctx
->region_type
& ORT_TASK
) != 0);
6023 if (struct gimplify_omp_ctx
*octx
= ctx
->outer_context
)
6025 omp_notice_variable (octx
, decl
, in_code
);
6026 for (; octx
; octx
= octx
->outer_context
)
6030 n2
= splay_tree_lookup (octx
->variables
, (splay_tree_key
) decl
);
6031 if ((octx
->region_type
& (ORT_TARGET_DATA
| ORT_TARGET
)) != 0
6032 && (n2
== NULL
|| (n2
->value
& GOVD_DATA_SHARE_CLASS
) == 0))
6034 if (n2
&& (n2
->value
& GOVD_DATA_SHARE_CLASS
) != GOVD_SHARED
)
6036 flags
|= GOVD_FIRSTPRIVATE
;
6039 if ((octx
->region_type
& (ORT_PARALLEL
| ORT_TEAMS
)) != 0)
6041 flags
|= GOVD_SHARED
;
6047 if (TREE_CODE (decl
) == PARM_DECL
6048 || (!is_global_var (decl
)
6049 && DECL_CONTEXT (decl
) == current_function_decl
))
6050 flags
|= GOVD_FIRSTPRIVATE
;
6052 flags
|= GOVD_SHARED
;
6064 /* Determine outer default flags for DECL mentioned in an OACC region
6065 but not declared in an enclosing clause. */
6068 oacc_default_clause (struct gimplify_omp_ctx
*ctx
, tree decl
, unsigned flags
)
6071 bool on_device
= false;
6072 tree type
= TREE_TYPE (decl
);
6074 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
6075 type
= TREE_TYPE (type
);
6077 if ((ctx
->region_type
& (ORT_ACC_PARALLEL
| ORT_ACC_KERNELS
)) != 0
6078 && is_global_var (decl
)
6079 && device_resident_p (decl
))
6082 flags
|= GOVD_MAP_TO_ONLY
;
6085 switch (ctx
->region_type
)
6090 case ORT_ACC_KERNELS
:
6091 /* Scalars are default 'copy' under kernels, non-scalars are default
6092 'present_or_copy'. */
6094 if (!AGGREGATE_TYPE_P (type
))
6095 flags
|= GOVD_MAP_FORCE
;
6100 case ORT_ACC_PARALLEL
:
6102 if (on_device
|| AGGREGATE_TYPE_P (type
))
6103 /* Aggregates default to 'present_or_copy'. */
6106 /* Scalars default to 'firstprivate'. */
6107 flags
|= GOVD_FIRSTPRIVATE
;
6113 if (DECL_ARTIFICIAL (decl
))
6114 ; /* We can get compiler-generated decls, and should not complain
6116 else if (ctx
->default_kind
== OMP_CLAUSE_DEFAULT_NONE
)
6118 error ("%qE not specified in enclosing OpenACC %qs construct",
6119 DECL_NAME (lang_hooks
.decls
.omp_report_decl (decl
)), rkind
);
6120 inform (ctx
->location
, "enclosing OpenACC %qs construct", rkind
);
6123 gcc_checking_assert (ctx
->default_kind
== OMP_CLAUSE_DEFAULT_SHARED
);
6128 /* Record the fact that DECL was used within the OMP context CTX.
6129 IN_CODE is true when real code uses DECL, and false when we should
6130 merely emit default(none) errors. Return true if DECL is going to
6131 be remapped and thus DECL shouldn't be gimplified into its
6132 DECL_VALUE_EXPR (if any). */
6135 omp_notice_variable (struct gimplify_omp_ctx
*ctx
, tree decl
, bool in_code
)
6138 unsigned flags
= in_code
? GOVD_SEEN
: 0;
6139 bool ret
= false, shared
;
6141 if (error_operand_p (decl
))
6144 if (ctx
->region_type
== ORT_NONE
)
6145 return lang_hooks
.decls
.omp_disregard_value_expr (decl
, false);
6147 if (is_global_var (decl
))
6149 /* Threadprivate variables are predetermined. */
6150 if (DECL_THREAD_LOCAL_P (decl
))
6151 return omp_notice_threadprivate_variable (ctx
, decl
, NULL_TREE
);
6153 if (DECL_HAS_VALUE_EXPR_P (decl
))
6155 tree value
= get_base_address (DECL_VALUE_EXPR (decl
));
6157 if (value
&& DECL_P (value
) && DECL_THREAD_LOCAL_P (value
))
6158 return omp_notice_threadprivate_variable (ctx
, decl
, value
);
6161 if (gimplify_omp_ctxp
->outer_context
== NULL
6163 && get_oacc_fn_attrib (current_function_decl
))
6165 location_t loc
= DECL_SOURCE_LOCATION (decl
);
6167 if (lookup_attribute ("omp declare target link",
6168 DECL_ATTRIBUTES (decl
)))
6171 "%qE with %<link%> clause used in %<routine%> function",
6175 else if (!lookup_attribute ("omp declare target",
6176 DECL_ATTRIBUTES (decl
)))
6179 "%qE requires a %<declare%> directive for use "
6180 "in a %<routine%> function", DECL_NAME (decl
));
6186 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6187 if ((ctx
->region_type
& ORT_TARGET
) != 0)
6189 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, true);
6192 unsigned nflags
= flags
;
6193 if (ctx
->target_map_pointers_as_0len_arrays
6194 || ctx
->target_map_scalars_firstprivate
)
6196 bool is_declare_target
= false;
6197 bool is_scalar
= false;
6198 if (is_global_var (decl
)
6199 && varpool_node::get_create (decl
)->offloadable
)
6201 struct gimplify_omp_ctx
*octx
;
6202 for (octx
= ctx
->outer_context
;
6203 octx
; octx
= octx
->outer_context
)
6205 n
= splay_tree_lookup (octx
->variables
,
6206 (splay_tree_key
)decl
);
6208 && (n
->value
& GOVD_DATA_SHARE_CLASS
) != GOVD_SHARED
6209 && (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
6212 is_declare_target
= octx
== NULL
;
6214 if (!is_declare_target
&& ctx
->target_map_scalars_firstprivate
)
6216 tree type
= TREE_TYPE (decl
);
6217 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6218 type
= TREE_TYPE (type
);
6219 if (TREE_CODE (type
) == COMPLEX_TYPE
)
6220 type
= TREE_TYPE (type
);
6221 if (INTEGRAL_TYPE_P (type
)
6222 || SCALAR_FLOAT_TYPE_P (type
)
6223 || TREE_CODE (type
) == POINTER_TYPE
)
6226 if (is_declare_target
)
6228 else if (ctx
->target_map_pointers_as_0len_arrays
6229 && (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
6230 || (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
6231 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl
)))
6233 nflags
|= GOVD_MAP
| GOVD_MAP_0LEN_ARRAY
;
6235 nflags
|= GOVD_FIRSTPRIVATE
;
6238 struct gimplify_omp_ctx
*octx
= ctx
->outer_context
;
6239 if ((ctx
->region_type
& ORT_ACC
) && octx
)
6241 /* Look in outer OpenACC contexts, to see if there's a
6242 data attribute for this variable. */
6243 omp_notice_variable (octx
, decl
, in_code
);
6245 for (; octx
; octx
= octx
->outer_context
)
6247 if (!(octx
->region_type
& (ORT_TARGET_DATA
| ORT_TARGET
)))
6250 = splay_tree_lookup (octx
->variables
,
6251 (splay_tree_key
) decl
);
6254 if (octx
->region_type
== ORT_ACC_HOST_DATA
)
6255 error ("variable %qE declared in enclosing "
6256 "%<host_data%> region", DECL_NAME (decl
));
6264 tree type
= TREE_TYPE (decl
);
6267 && gimplify_omp_ctxp
->target_firstprivatize_array_bases
6268 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
6269 type
= TREE_TYPE (type
);
6271 && !lang_hooks
.types
.omp_mappable_type (type
))
6273 error ("%qD referenced in target region does not have "
6274 "a mappable type", decl
);
6275 nflags
|= GOVD_MAP
| GOVD_EXPLICIT
;
6277 else if (nflags
== flags
)
6279 if ((ctx
->region_type
& ORT_ACC
) != 0)
6280 nflags
= oacc_default_clause (ctx
, decl
, flags
);
6286 omp_add_variable (ctx
, decl
, nflags
);
6290 /* If nothing changed, there's nothing left to do. */
6291 if ((n
->value
& flags
) == flags
)
6301 if (ctx
->region_type
== ORT_WORKSHARE
6302 || ctx
->region_type
== ORT_SIMD
6303 || ctx
->region_type
== ORT_ACC
6304 || (ctx
->region_type
& ORT_TARGET_DATA
) != 0)
6307 flags
= omp_default_clause (ctx
, decl
, in_code
, flags
);
6309 if ((flags
& GOVD_PRIVATE
)
6310 && lang_hooks
.decls
.omp_private_outer_ref (decl
))
6311 flags
|= GOVD_PRIVATE_OUTER_REF
;
6313 omp_add_variable (ctx
, decl
, flags
);
6315 shared
= (flags
& GOVD_SHARED
) != 0;
6316 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
6320 if ((n
->value
& (GOVD_SEEN
| GOVD_LOCAL
)) == 0
6321 && (flags
& (GOVD_SEEN
| GOVD_LOCAL
)) == GOVD_SEEN
6322 && DECL_SIZE (decl
))
6324 if (TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
6327 tree t
= DECL_VALUE_EXPR (decl
);
6328 gcc_assert (TREE_CODE (t
) == INDIRECT_REF
);
6329 t
= TREE_OPERAND (t
, 0);
6330 gcc_assert (DECL_P (t
));
6331 n2
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
6332 n2
->value
|= GOVD_SEEN
;
6334 else if (lang_hooks
.decls
.omp_privatize_by_reference (decl
)
6335 && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)))
6336 && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
))))
6340 tree t
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
)));
6341 gcc_assert (DECL_P (t
));
6342 n2
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
6344 n2
->value
|= GOVD_SEEN
;
6348 shared
= ((flags
| n
->value
) & GOVD_SHARED
) != 0;
6349 ret
= lang_hooks
.decls
.omp_disregard_value_expr (decl
, shared
);
6351 /* If nothing changed, there's nothing left to do. */
6352 if ((n
->value
& flags
) == flags
)
6358 /* If the variable is private in the current context, then we don't
6359 need to propagate anything to an outer context. */
6360 if ((flags
& GOVD_PRIVATE
) && !(flags
& GOVD_PRIVATE_OUTER_REF
))
6362 if ((flags
& (GOVD_LINEAR
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
6363 == (GOVD_LINEAR
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
6365 if ((flags
& (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
6366 | GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
6367 == (GOVD_LASTPRIVATE
| GOVD_LINEAR_LASTPRIVATE_NO_OUTER
))
6369 if (ctx
->outer_context
6370 && omp_notice_variable (ctx
->outer_context
, decl
, in_code
))
6375 /* Verify that DECL is private within CTX. If there's specific information
6376 to the contrary in the innermost scope, generate an error. */
6379 omp_is_private (struct gimplify_omp_ctx
*ctx
, tree decl
, int simd
)
6383 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6386 if (n
->value
& GOVD_SHARED
)
6388 if (ctx
== gimplify_omp_ctxp
)
6391 error ("iteration variable %qE is predetermined linear",
6394 error ("iteration variable %qE should be private",
6396 n
->value
= GOVD_PRIVATE
;
6402 else if ((n
->value
& GOVD_EXPLICIT
) != 0
6403 && (ctx
== gimplify_omp_ctxp
6404 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
6405 && gimplify_omp_ctxp
->outer_context
== ctx
)))
6407 if ((n
->value
& GOVD_FIRSTPRIVATE
) != 0)
6408 error ("iteration variable %qE should not be firstprivate",
6410 else if ((n
->value
& GOVD_REDUCTION
) != 0)
6411 error ("iteration variable %qE should not be reduction",
6413 else if (simd
== 0 && (n
->value
& GOVD_LINEAR
) != 0)
6414 error ("iteration variable %qE should not be linear",
6416 else if (simd
== 1 && (n
->value
& GOVD_LASTPRIVATE
) != 0)
6417 error ("iteration variable %qE should not be lastprivate",
6419 else if (simd
&& (n
->value
& GOVD_PRIVATE
) != 0)
6420 error ("iteration variable %qE should not be private",
6422 else if (simd
== 2 && (n
->value
& GOVD_LINEAR
) != 0)
6423 error ("iteration variable %qE is predetermined linear",
6426 return (ctx
== gimplify_omp_ctxp
6427 || (ctx
->region_type
== ORT_COMBINED_PARALLEL
6428 && gimplify_omp_ctxp
->outer_context
== ctx
));
6431 if (ctx
->region_type
!= ORT_WORKSHARE
6432 && ctx
->region_type
!= ORT_SIMD
6433 && ctx
->region_type
!= ORT_ACC
)
6435 else if (ctx
->outer_context
)
6436 return omp_is_private (ctx
->outer_context
, decl
, simd
);
6440 /* Return true if DECL is private within a parallel region
6441 that binds to the current construct's context or in parallel
6442 region's REDUCTION clause. */
6445 omp_check_private (struct gimplify_omp_ctx
*ctx
, tree decl
, bool copyprivate
)
6451 ctx
= ctx
->outer_context
;
6454 if (is_global_var (decl
))
6457 /* References might be private, but might be shared too,
6458 when checking for copyprivate, assume they might be
6459 private, otherwise assume they might be shared. */
6463 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
6466 /* Treat C++ privatized non-static data members outside
6467 of the privatization the same. */
6468 if (omp_member_access_dummy_var (decl
))
6474 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
6476 if ((ctx
->region_type
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0
6477 && (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0))
6482 if ((n
->value
& GOVD_LOCAL
) != 0
6483 && omp_member_access_dummy_var (decl
))
6485 return (n
->value
& GOVD_SHARED
) == 0;
6488 while (ctx
->region_type
== ORT_WORKSHARE
6489 || ctx
->region_type
== ORT_SIMD
6490 || ctx
->region_type
== ORT_ACC
);
6494 /* Return true if the CTX is combined with distribute and thus
6495 lastprivate can't be supported. */
6498 omp_no_lastprivate (struct gimplify_omp_ctx
*ctx
)
6502 if (ctx
->outer_context
== NULL
)
6504 ctx
= ctx
->outer_context
;
6505 switch (ctx
->region_type
)
6508 if (!ctx
->combined_loop
)
6510 if (ctx
->distribute
)
6511 return lang_GNU_Fortran ();
6513 case ORT_COMBINED_PARALLEL
:
6515 case ORT_COMBINED_TEAMS
:
6516 return lang_GNU_Fortran ();
6524 /* Callback for walk_tree to find a DECL_EXPR for the given DECL. */
6527 find_decl_expr (tree
*tp
, int *walk_subtrees
, void *data
)
6531 /* If this node has been visited, unmark it and keep looking. */
6532 if (TREE_CODE (t
) == DECL_EXPR
&& DECL_EXPR_DECL (t
) == (tree
) data
)
6535 if (IS_TYPE_OR_DECL_P (t
))
6540 /* Scan the OMP clauses in *LIST_P, installing mappings into a new
6541 and previous omp contexts. */
6544 gimplify_scan_omp_clauses (tree
*list_p
, gimple_seq
*pre_p
,
6545 enum omp_region_type region_type
,
6546 enum tree_code code
)
6548 struct gimplify_omp_ctx
*ctx
, *outer_ctx
;
6550 hash_map
<tree
, tree
> *struct_map_to_clause
= NULL
;
6551 tree
*prev_list_p
= NULL
;
6553 ctx
= new_omp_context (region_type
);
6554 outer_ctx
= ctx
->outer_context
;
6555 if (code
== OMP_TARGET
&& !lang_GNU_Fortran ())
6557 ctx
->target_map_pointers_as_0len_arrays
= true;
6558 /* FIXME: For Fortran we want to set this too, when
6559 the Fortran FE is updated to OpenMP 4.5. */
6560 ctx
->target_map_scalars_firstprivate
= true;
6562 if (!lang_GNU_Fortran ())
6566 case OMP_TARGET_DATA
:
6567 case OMP_TARGET_ENTER_DATA
:
6568 case OMP_TARGET_EXIT_DATA
:
6569 case OACC_HOST_DATA
:
6570 ctx
->target_firstprivatize_array_bases
= true;
6575 while ((c
= *list_p
) != NULL
)
6577 bool remove
= false;
6578 bool notice_outer
= true;
6579 const char *check_non_private
= NULL
;
6583 switch (OMP_CLAUSE_CODE (c
))
6585 case OMP_CLAUSE_PRIVATE
:
6586 flags
= GOVD_PRIVATE
| GOVD_EXPLICIT
;
6587 if (lang_hooks
.decls
.omp_private_outer_ref (OMP_CLAUSE_DECL (c
)))
6589 flags
|= GOVD_PRIVATE_OUTER_REF
;
6590 OMP_CLAUSE_PRIVATE_OUTER_REF (c
) = 1;
6593 notice_outer
= false;
6595 case OMP_CLAUSE_SHARED
:
6596 flags
= GOVD_SHARED
| GOVD_EXPLICIT
;
6598 case OMP_CLAUSE_FIRSTPRIVATE
:
6599 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
6600 check_non_private
= "firstprivate";
6602 case OMP_CLAUSE_LASTPRIVATE
:
6603 flags
= GOVD_LASTPRIVATE
| GOVD_SEEN
| GOVD_EXPLICIT
;
6604 check_non_private
= "lastprivate";
6605 decl
= OMP_CLAUSE_DECL (c
);
6606 if (omp_no_lastprivate (ctx
))
6608 notice_outer
= false;
6609 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
6611 else if (error_operand_p (decl
))
6614 && (outer_ctx
->region_type
== ORT_COMBINED_PARALLEL
6615 || outer_ctx
->region_type
== ORT_COMBINED_TEAMS
)
6616 && splay_tree_lookup (outer_ctx
->variables
,
6617 (splay_tree_key
) decl
) == NULL
)
6619 omp_add_variable (outer_ctx
, decl
, GOVD_SHARED
| GOVD_SEEN
);
6620 if (outer_ctx
->outer_context
)
6621 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
6624 && (outer_ctx
->region_type
& ORT_TASK
) != 0
6625 && outer_ctx
->combined_loop
6626 && splay_tree_lookup (outer_ctx
->variables
,
6627 (splay_tree_key
) decl
) == NULL
)
6629 omp_add_variable (outer_ctx
, decl
, GOVD_LASTPRIVATE
| GOVD_SEEN
);
6630 if (outer_ctx
->outer_context
)
6631 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
6634 && (outer_ctx
->region_type
== ORT_WORKSHARE
6635 || outer_ctx
->region_type
== ORT_ACC
)
6636 && outer_ctx
->combined_loop
6637 && splay_tree_lookup (outer_ctx
->variables
,
6638 (splay_tree_key
) decl
) == NULL
6639 && !omp_check_private (outer_ctx
, decl
, false))
6641 omp_add_variable (outer_ctx
, decl
, GOVD_LASTPRIVATE
| GOVD_SEEN
);
6642 if (outer_ctx
->outer_context
6643 && (outer_ctx
->outer_context
->region_type
6644 == ORT_COMBINED_PARALLEL
)
6645 && splay_tree_lookup (outer_ctx
->outer_context
->variables
,
6646 (splay_tree_key
) decl
) == NULL
)
6648 struct gimplify_omp_ctx
*octx
= outer_ctx
->outer_context
;
6649 omp_add_variable (octx
, decl
, GOVD_SHARED
| GOVD_SEEN
);
6650 if (octx
->outer_context
)
6651 omp_notice_variable (octx
->outer_context
, decl
, true);
6653 else if (outer_ctx
->outer_context
)
6654 omp_notice_variable (outer_ctx
->outer_context
, decl
, true);
6657 case OMP_CLAUSE_REDUCTION
:
6658 flags
= GOVD_REDUCTION
| GOVD_SEEN
| GOVD_EXPLICIT
;
6659 /* OpenACC permits reductions on private variables. */
6660 if (!(region_type
& ORT_ACC
))
6661 check_non_private
= "reduction";
6662 decl
= OMP_CLAUSE_DECL (c
);
6663 if (TREE_CODE (decl
) == MEM_REF
)
6665 tree type
= TREE_TYPE (decl
);
6666 if (gimplify_expr (&TYPE_MAX_VALUE (TYPE_DOMAIN (type
)), pre_p
,
6667 NULL
, is_gimple_val
, fb_rvalue
, false)
6673 tree v
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
6676 omp_firstprivatize_variable (ctx
, v
);
6677 omp_notice_variable (ctx
, v
, true);
6679 decl
= TREE_OPERAND (decl
, 0);
6680 if (TREE_CODE (decl
) == POINTER_PLUS_EXPR
)
6682 if (gimplify_expr (&TREE_OPERAND (decl
, 1), pre_p
,
6683 NULL
, is_gimple_val
, fb_rvalue
, false)
6689 v
= TREE_OPERAND (decl
, 1);
6692 omp_firstprivatize_variable (ctx
, v
);
6693 omp_notice_variable (ctx
, v
, true);
6695 decl
= TREE_OPERAND (decl
, 0);
6697 if (TREE_CODE (decl
) == ADDR_EXPR
6698 || TREE_CODE (decl
) == INDIRECT_REF
)
6699 decl
= TREE_OPERAND (decl
, 0);
6702 case OMP_CLAUSE_LINEAR
:
6703 if (gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c
), pre_p
, NULL
,
6704 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
6711 if (code
== OMP_SIMD
6712 && !OMP_CLAUSE_LINEAR_NO_COPYIN (c
))
6714 struct gimplify_omp_ctx
*octx
= outer_ctx
;
6716 && octx
->region_type
== ORT_WORKSHARE
6717 && octx
->combined_loop
6718 && !octx
->distribute
)
6720 if (octx
->outer_context
6721 && (octx
->outer_context
->region_type
6722 == ORT_COMBINED_PARALLEL
))
6723 octx
= octx
->outer_context
->outer_context
;
6725 octx
= octx
->outer_context
;
6728 && octx
->region_type
== ORT_WORKSHARE
6729 && octx
->combined_loop
6731 && !lang_GNU_Fortran ())
6733 error_at (OMP_CLAUSE_LOCATION (c
),
6734 "%<linear%> clause for variable other than "
6735 "loop iterator specified on construct "
6736 "combined with %<distribute%>");
6741 /* For combined #pragma omp parallel for simd, need to put
6742 lastprivate and perhaps firstprivate too on the
6743 parallel. Similarly for #pragma omp for simd. */
6744 struct gimplify_omp_ctx
*octx
= outer_ctx
;
6746 if (omp_no_lastprivate (ctx
))
6747 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
6750 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
6751 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
6753 decl
= OMP_CLAUSE_DECL (c
);
6754 if (error_operand_p (decl
))
6760 if (!OMP_CLAUSE_LINEAR_NO_COPYIN (c
))
6761 flags
|= GOVD_FIRSTPRIVATE
;
6762 if (!OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
6763 flags
|= GOVD_LASTPRIVATE
;
6765 && octx
->region_type
== ORT_WORKSHARE
6766 && octx
->combined_loop
)
6768 if (octx
->outer_context
6769 && (octx
->outer_context
->region_type
6770 == ORT_COMBINED_PARALLEL
))
6771 octx
= octx
->outer_context
;
6772 else if (omp_check_private (octx
, decl
, false))
6776 && (octx
->region_type
& ORT_TASK
) != 0
6777 && octx
->combined_loop
)
6780 && octx
->region_type
== ORT_COMBINED_PARALLEL
6781 && ctx
->region_type
== ORT_WORKSHARE
6782 && octx
== outer_ctx
)
6783 flags
= GOVD_SEEN
| GOVD_SHARED
;
6785 && octx
->region_type
== ORT_COMBINED_TEAMS
)
6786 flags
= GOVD_SEEN
| GOVD_SHARED
;
6788 && octx
->region_type
== ORT_COMBINED_TARGET
)
6790 flags
&= ~GOVD_LASTPRIVATE
;
6791 if (flags
== GOVD_SEEN
)
6797 = splay_tree_lookup (octx
->variables
,
6798 (splay_tree_key
) decl
);
6799 if (on
&& (on
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
6804 omp_add_variable (octx
, decl
, flags
);
6805 if (octx
->outer_context
== NULL
)
6807 octx
= octx
->outer_context
;
6812 && (!OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
6813 || !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)))
6814 omp_notice_variable (octx
, decl
, true);
6816 flags
= GOVD_LINEAR
| GOVD_EXPLICIT
;
6817 if (OMP_CLAUSE_LINEAR_NO_COPYIN (c
)
6818 && OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
6820 notice_outer
= false;
6821 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
6825 case OMP_CLAUSE_MAP
:
6826 decl
= OMP_CLAUSE_DECL (c
);
6827 if (error_operand_p (decl
))
6833 case OMP_TARGET_DATA
:
6834 case OMP_TARGET_ENTER_DATA
:
6835 case OMP_TARGET_EXIT_DATA
:
6836 case OACC_HOST_DATA
:
6837 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
6838 || (OMP_CLAUSE_MAP_KIND (c
)
6839 == GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
6840 /* For target {,enter ,exit }data only the array slice is
6841 mapped, but not the pointer to it. */
6849 if (DECL_P (decl
) && outer_ctx
&& (region_type
& ORT_ACC
))
6851 struct gimplify_omp_ctx
*octx
;
6852 for (octx
= outer_ctx
; octx
; octx
= octx
->outer_context
)
6854 if (octx
->region_type
!= ORT_ACC_HOST_DATA
)
6857 = splay_tree_lookup (octx
->variables
,
6858 (splay_tree_key
) decl
);
6860 error_at (OMP_CLAUSE_LOCATION (c
), "variable %qE "
6861 "declared in enclosing %<host_data%> region",
6865 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
6866 OMP_CLAUSE_SIZE (c
) = DECL_P (decl
) ? DECL_SIZE_UNIT (decl
)
6867 : TYPE_SIZE_UNIT (TREE_TYPE (decl
));
6868 if (gimplify_expr (&OMP_CLAUSE_SIZE (c
), pre_p
,
6869 NULL
, is_gimple_val
, fb_rvalue
) == GS_ERROR
)
6874 else if ((OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
6875 || (OMP_CLAUSE_MAP_KIND (c
)
6876 == GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
6877 && TREE_CODE (OMP_CLAUSE_SIZE (c
)) != INTEGER_CST
)
6880 = get_initialized_tmp_var (OMP_CLAUSE_SIZE (c
), pre_p
, NULL
,
6882 omp_add_variable (ctx
, OMP_CLAUSE_SIZE (c
),
6883 GOVD_FIRSTPRIVATE
| GOVD_SEEN
);
6888 if (TREE_CODE (d
) == ARRAY_REF
)
6890 while (TREE_CODE (d
) == ARRAY_REF
)
6891 d
= TREE_OPERAND (d
, 0);
6892 if (TREE_CODE (d
) == COMPONENT_REF
6893 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
)
6896 pd
= &OMP_CLAUSE_DECL (c
);
6898 && TREE_CODE (decl
) == INDIRECT_REF
6899 && TREE_CODE (TREE_OPERAND (decl
, 0)) == COMPONENT_REF
6900 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl
, 0)))
6903 pd
= &TREE_OPERAND (decl
, 0);
6904 decl
= TREE_OPERAND (decl
, 0);
6906 if (TREE_CODE (decl
) == COMPONENT_REF
)
6908 while (TREE_CODE (decl
) == COMPONENT_REF
)
6909 decl
= TREE_OPERAND (decl
, 0);
6911 if (gimplify_expr (pd
, pre_p
, NULL
, is_gimple_lvalue
, fb_lvalue
)
6919 if (error_operand_p (decl
))
6925 if (TYPE_SIZE_UNIT (TREE_TYPE (decl
)) == NULL
6926 || (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (decl
)))
6929 error_at (OMP_CLAUSE_LOCATION (c
),
6930 "mapping field %qE of variable length "
6931 "structure", OMP_CLAUSE_DECL (c
));
6936 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_POINTER
)
6938 /* Error recovery. */
6939 if (prev_list_p
== NULL
)
6944 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
6946 tree ch
= OMP_CLAUSE_CHAIN (*prev_list_p
);
6947 if (ch
== NULL_TREE
|| OMP_CLAUSE_CHAIN (ch
) != c
)
6956 HOST_WIDE_INT bitsize
, bitpos
;
6958 int unsignedp
, reversep
, volatilep
= 0;
6959 tree base
= OMP_CLAUSE_DECL (c
);
6960 while (TREE_CODE (base
) == ARRAY_REF
)
6961 base
= TREE_OPERAND (base
, 0);
6962 if (TREE_CODE (base
) == INDIRECT_REF
)
6963 base
= TREE_OPERAND (base
, 0);
6964 base
= get_inner_reference (base
, &bitsize
, &bitpos
, &offset
,
6965 &mode
, &unsignedp
, &reversep
,
6967 gcc_assert (base
== decl
6968 && (offset
== NULL_TREE
6969 || TREE_CODE (offset
) == INTEGER_CST
));
6972 = splay_tree_lookup (ctx
->variables
, (splay_tree_key
)decl
);
6973 bool ptr
= (OMP_CLAUSE_MAP_KIND (c
)
6974 == GOMP_MAP_ALWAYS_POINTER
);
6975 if (n
== NULL
|| (n
->value
& GOVD_MAP
) == 0)
6977 tree l
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
6979 OMP_CLAUSE_SET_MAP_KIND (l
, GOMP_MAP_STRUCT
);
6980 OMP_CLAUSE_DECL (l
) = decl
;
6981 OMP_CLAUSE_SIZE (l
) = size_int (1);
6982 if (struct_map_to_clause
== NULL
)
6983 struct_map_to_clause
= new hash_map
<tree
, tree
>;
6984 struct_map_to_clause
->put (decl
, l
);
6987 enum gomp_map_kind mkind
6988 = code
== OMP_TARGET_EXIT_DATA
6989 ? GOMP_MAP_RELEASE
: GOMP_MAP_ALLOC
;
6990 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
6992 OMP_CLAUSE_SET_MAP_KIND (c2
, mkind
);
6993 OMP_CLAUSE_DECL (c2
)
6994 = unshare_expr (OMP_CLAUSE_DECL (c
));
6995 OMP_CLAUSE_CHAIN (c2
) = *prev_list_p
;
6996 OMP_CLAUSE_SIZE (c2
)
6997 = TYPE_SIZE_UNIT (ptr_type_node
);
6998 OMP_CLAUSE_CHAIN (l
) = c2
;
6999 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
7001 tree c4
= OMP_CLAUSE_CHAIN (*prev_list_p
);
7003 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7005 OMP_CLAUSE_SET_MAP_KIND (c3
, mkind
);
7006 OMP_CLAUSE_DECL (c3
)
7007 = unshare_expr (OMP_CLAUSE_DECL (c4
));
7008 OMP_CLAUSE_SIZE (c3
)
7009 = TYPE_SIZE_UNIT (ptr_type_node
);
7010 OMP_CLAUSE_CHAIN (c3
) = *prev_list_p
;
7011 OMP_CLAUSE_CHAIN (c2
) = c3
;
7018 OMP_CLAUSE_CHAIN (l
) = c
;
7020 list_p
= &OMP_CLAUSE_CHAIN (l
);
7022 flags
= GOVD_MAP
| GOVD_EXPLICIT
;
7023 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) || ptr
)
7029 tree
*osc
= struct_map_to_clause
->get (decl
);
7030 tree
*sc
= NULL
, *scp
= NULL
;
7031 if (GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) || ptr
)
7032 n
->value
|= GOVD_SEEN
;
7035 o1
= wi::to_offset (offset
);
7039 o1
= o1
+ bitpos
/ BITS_PER_UNIT
;
7040 for (sc
= &OMP_CLAUSE_CHAIN (*osc
);
7041 *sc
!= c
; sc
= &OMP_CLAUSE_CHAIN (*sc
))
7042 if (ptr
&& sc
== prev_list_p
)
7044 else if (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7046 && (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7048 && (TREE_CODE (OMP_CLAUSE_DECL (*sc
))
7054 HOST_WIDE_INT bitsize2
, bitpos2
;
7055 base
= OMP_CLAUSE_DECL (*sc
);
7056 if (TREE_CODE (base
) == ARRAY_REF
)
7058 while (TREE_CODE (base
) == ARRAY_REF
)
7059 base
= TREE_OPERAND (base
, 0);
7060 if (TREE_CODE (base
) != COMPONENT_REF
7061 || (TREE_CODE (TREE_TYPE (base
))
7065 else if (TREE_CODE (base
) == INDIRECT_REF
7066 && (TREE_CODE (TREE_OPERAND (base
, 0))
7068 && (TREE_CODE (TREE_TYPE
7069 (TREE_OPERAND (base
, 0)))
7071 base
= TREE_OPERAND (base
, 0);
7072 base
= get_inner_reference (base
, &bitsize2
,
7075 &reversep
, &volatilep
,
7081 gcc_assert (offset
== NULL_TREE
7082 || TREE_CODE (offset
) == INTEGER_CST
);
7083 tree d1
= OMP_CLAUSE_DECL (*sc
);
7084 tree d2
= OMP_CLAUSE_DECL (c
);
7085 while (TREE_CODE (d1
) == ARRAY_REF
)
7086 d1
= TREE_OPERAND (d1
, 0);
7087 while (TREE_CODE (d2
) == ARRAY_REF
)
7088 d2
= TREE_OPERAND (d2
, 0);
7089 if (TREE_CODE (d1
) == INDIRECT_REF
)
7090 d1
= TREE_OPERAND (d1
, 0);
7091 if (TREE_CODE (d2
) == INDIRECT_REF
)
7092 d2
= TREE_OPERAND (d2
, 0);
7093 while (TREE_CODE (d1
) == COMPONENT_REF
)
7094 if (TREE_CODE (d2
) == COMPONENT_REF
7095 && TREE_OPERAND (d1
, 1)
7096 == TREE_OPERAND (d2
, 1))
7098 d1
= TREE_OPERAND (d1
, 0);
7099 d2
= TREE_OPERAND (d2
, 0);
7105 error_at (OMP_CLAUSE_LOCATION (c
),
7106 "%qE appears more than once in map "
7107 "clauses", OMP_CLAUSE_DECL (c
));
7112 o2
= wi::to_offset (offset2
);
7116 o2
= o2
+ bitpos2
/ BITS_PER_UNIT
;
7117 if (wi::ltu_p (o1
, o2
)
7118 || (wi::eq_p (o1
, o2
) && bitpos
< bitpos2
))
7128 OMP_CLAUSE_SIZE (*osc
)
7129 = size_binop (PLUS_EXPR
, OMP_CLAUSE_SIZE (*osc
),
7133 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7135 tree cl
= NULL_TREE
;
7136 enum gomp_map_kind mkind
7137 = code
== OMP_TARGET_EXIT_DATA
7138 ? GOMP_MAP_RELEASE
: GOMP_MAP_ALLOC
;
7139 OMP_CLAUSE_SET_MAP_KIND (c2
, mkind
);
7140 OMP_CLAUSE_DECL (c2
)
7141 = unshare_expr (OMP_CLAUSE_DECL (c
));
7142 OMP_CLAUSE_CHAIN (c2
) = scp
? *scp
: *prev_list_p
;
7143 OMP_CLAUSE_SIZE (c2
)
7144 = TYPE_SIZE_UNIT (ptr_type_node
);
7145 cl
= scp
? *prev_list_p
: c2
;
7146 if (OMP_CLAUSE_CHAIN (*prev_list_p
) != c
)
7148 tree c4
= OMP_CLAUSE_CHAIN (*prev_list_p
);
7150 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
7152 OMP_CLAUSE_SET_MAP_KIND (c3
, mkind
);
7153 OMP_CLAUSE_DECL (c3
)
7154 = unshare_expr (OMP_CLAUSE_DECL (c4
));
7155 OMP_CLAUSE_SIZE (c3
)
7156 = TYPE_SIZE_UNIT (ptr_type_node
);
7157 OMP_CLAUSE_CHAIN (c3
) = *prev_list_p
;
7159 OMP_CLAUSE_CHAIN (c2
) = c3
;
7165 if (sc
== prev_list_p
)
7172 *prev_list_p
= OMP_CLAUSE_CHAIN (c
);
7173 list_p
= prev_list_p
;
7175 OMP_CLAUSE_CHAIN (c
) = *sc
;
7182 *list_p
= OMP_CLAUSE_CHAIN (c
);
7183 OMP_CLAUSE_CHAIN (c
) = *sc
;
7190 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ALWAYS_POINTER
7191 && OMP_CLAUSE_CHAIN (c
)
7192 && OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (c
)) == OMP_CLAUSE_MAP
7193 && (OMP_CLAUSE_MAP_KIND (OMP_CLAUSE_CHAIN (c
))
7194 == GOMP_MAP_ALWAYS_POINTER
))
7195 prev_list_p
= list_p
;
7198 flags
= GOVD_MAP
| GOVD_EXPLICIT
;
7199 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_TO
7200 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_TOFROM
)
7201 flags
|= GOVD_MAP_ALWAYS_TO
;
7204 case OMP_CLAUSE_DEPEND
:
7205 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
7206 || OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
)
7208 /* Nothing to do. OMP_CLAUSE_DECL will be lowered in
7212 if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPOUND_EXPR
)
7214 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0), pre_p
,
7215 NULL
, is_gimple_val
, fb_rvalue
);
7216 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (OMP_CLAUSE_DECL (c
), 1);
7218 if (error_operand_p (OMP_CLAUSE_DECL (c
)))
7223 OMP_CLAUSE_DECL (c
) = build_fold_addr_expr (OMP_CLAUSE_DECL (c
));
7224 if (gimplify_expr (&OMP_CLAUSE_DECL (c
), pre_p
, NULL
,
7225 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7233 case OMP_CLAUSE_FROM
:
7234 case OMP_CLAUSE__CACHE_
:
7235 decl
= OMP_CLAUSE_DECL (c
);
7236 if (error_operand_p (decl
))
7241 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
7242 OMP_CLAUSE_SIZE (c
) = DECL_P (decl
) ? DECL_SIZE_UNIT (decl
)
7243 : TYPE_SIZE_UNIT (TREE_TYPE (decl
));
7244 if (gimplify_expr (&OMP_CLAUSE_SIZE (c
), pre_p
,
7245 NULL
, is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7252 if (gimplify_expr (&OMP_CLAUSE_DECL (c
), pre_p
,
7253 NULL
, is_gimple_lvalue
, fb_lvalue
)
7263 case OMP_CLAUSE_USE_DEVICE_PTR
:
7264 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
7266 case OMP_CLAUSE_IS_DEVICE_PTR
:
7267 flags
= GOVD_FIRSTPRIVATE
| GOVD_EXPLICIT
;
7271 decl
= OMP_CLAUSE_DECL (c
);
7273 if (error_operand_p (decl
))
7278 if (DECL_NAME (decl
) == NULL_TREE
&& (flags
& GOVD_SHARED
) == 0)
7280 tree t
= omp_member_access_dummy_var (decl
);
7283 tree v
= DECL_VALUE_EXPR (decl
);
7284 DECL_NAME (decl
) = DECL_NAME (TREE_OPERAND (v
, 1));
7286 omp_notice_variable (outer_ctx
, t
, true);
7289 omp_add_variable (ctx
, decl
, flags
);
7290 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
7291 && OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
7293 omp_add_variable (ctx
, OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
),
7294 GOVD_LOCAL
| GOVD_SEEN
);
7295 if (OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
)
7296 && walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
7298 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
),
7300 omp_add_variable (ctx
,
7301 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
),
7302 GOVD_LOCAL
| GOVD_SEEN
);
7303 gimplify_omp_ctxp
= ctx
;
7304 push_gimplify_context ();
7306 OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
) = NULL
;
7307 OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
) = NULL
;
7309 gimplify_and_add (OMP_CLAUSE_REDUCTION_INIT (c
),
7310 &OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
));
7311 pop_gimplify_context
7312 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_INIT (c
)));
7313 push_gimplify_context ();
7314 gimplify_and_add (OMP_CLAUSE_REDUCTION_MERGE (c
),
7315 &OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
));
7316 pop_gimplify_context
7317 (gimple_seq_first_stmt (OMP_CLAUSE_REDUCTION_GIMPLE_MERGE (c
)));
7318 OMP_CLAUSE_REDUCTION_INIT (c
) = NULL_TREE
;
7319 OMP_CLAUSE_REDUCTION_MERGE (c
) = NULL_TREE
;
7321 gimplify_omp_ctxp
= outer_ctx
;
7323 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
7324 && OMP_CLAUSE_LASTPRIVATE_STMT (c
))
7326 gimplify_omp_ctxp
= ctx
;
7327 push_gimplify_context ();
7328 if (TREE_CODE (OMP_CLAUSE_LASTPRIVATE_STMT (c
)) != BIND_EXPR
)
7330 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
,
7332 TREE_SIDE_EFFECTS (bind
) = 1;
7333 BIND_EXPR_BODY (bind
) = OMP_CLAUSE_LASTPRIVATE_STMT (c
);
7334 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = bind
;
7336 gimplify_and_add (OMP_CLAUSE_LASTPRIVATE_STMT (c
),
7337 &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
));
7338 pop_gimplify_context
7339 (gimple_seq_first_stmt (OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
)));
7340 OMP_CLAUSE_LASTPRIVATE_STMT (c
) = NULL_TREE
;
7342 gimplify_omp_ctxp
= outer_ctx
;
7344 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
7345 && OMP_CLAUSE_LINEAR_STMT (c
))
7347 gimplify_omp_ctxp
= ctx
;
7348 push_gimplify_context ();
7349 if (TREE_CODE (OMP_CLAUSE_LINEAR_STMT (c
)) != BIND_EXPR
)
7351 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
,
7353 TREE_SIDE_EFFECTS (bind
) = 1;
7354 BIND_EXPR_BODY (bind
) = OMP_CLAUSE_LINEAR_STMT (c
);
7355 OMP_CLAUSE_LINEAR_STMT (c
) = bind
;
7357 gimplify_and_add (OMP_CLAUSE_LINEAR_STMT (c
),
7358 &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
));
7359 pop_gimplify_context
7360 (gimple_seq_first_stmt (OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
)));
7361 OMP_CLAUSE_LINEAR_STMT (c
) = NULL_TREE
;
7363 gimplify_omp_ctxp
= outer_ctx
;
7369 case OMP_CLAUSE_COPYIN
:
7370 case OMP_CLAUSE_COPYPRIVATE
:
7371 decl
= OMP_CLAUSE_DECL (c
);
7372 if (error_operand_p (decl
))
7377 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_COPYPRIVATE
7379 && !omp_check_private (ctx
, decl
, true))
7382 if (is_global_var (decl
))
7384 if (DECL_THREAD_LOCAL_P (decl
))
7386 else if (DECL_HAS_VALUE_EXPR_P (decl
))
7388 tree value
= get_base_address (DECL_VALUE_EXPR (decl
));
7392 && DECL_THREAD_LOCAL_P (value
))
7397 error_at (OMP_CLAUSE_LOCATION (c
),
7398 "copyprivate variable %qE is not threadprivate"
7399 " or private in outer context", DECL_NAME (decl
));
7403 omp_notice_variable (outer_ctx
, decl
, true);
7404 if (check_non_private
7405 && region_type
== ORT_WORKSHARE
7406 && (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
7407 || decl
== OMP_CLAUSE_DECL (c
)
7408 || (TREE_CODE (OMP_CLAUSE_DECL (c
)) == MEM_REF
7409 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0))
7411 || (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0))
7412 == POINTER_PLUS_EXPR
7413 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND
7414 (OMP_CLAUSE_DECL (c
), 0), 0))
7416 && omp_check_private (ctx
, decl
, false))
7418 error ("%s variable %qE is private in outer context",
7419 check_non_private
, DECL_NAME (decl
));
7425 if (OMP_CLAUSE_IF_MODIFIER (c
) != ERROR_MARK
7426 && OMP_CLAUSE_IF_MODIFIER (c
) != code
)
7429 for (int i
= 0; i
< 2; i
++)
7430 switch (i
? OMP_CLAUSE_IF_MODIFIER (c
) : code
)
7432 case OMP_PARALLEL
: p
[i
] = "parallel"; break;
7433 case OMP_TASK
: p
[i
] = "task"; break;
7434 case OMP_TASKLOOP
: p
[i
] = "taskloop"; break;
7435 case OMP_TARGET_DATA
: p
[i
] = "target data"; break;
7436 case OMP_TARGET
: p
[i
] = "target"; break;
7437 case OMP_TARGET_UPDATE
: p
[i
] = "target update"; break;
7438 case OMP_TARGET_ENTER_DATA
:
7439 p
[i
] = "target enter data"; break;
7440 case OMP_TARGET_EXIT_DATA
: p
[i
] = "target exit data"; break;
7441 default: gcc_unreachable ();
7443 error_at (OMP_CLAUSE_LOCATION (c
),
7444 "expected %qs %<if%> clause modifier rather than %qs",
7450 case OMP_CLAUSE_FINAL
:
7451 OMP_CLAUSE_OPERAND (c
, 0)
7452 = gimple_boolify (OMP_CLAUSE_OPERAND (c
, 0));
7455 case OMP_CLAUSE_SCHEDULE
:
7456 case OMP_CLAUSE_NUM_THREADS
:
7457 case OMP_CLAUSE_NUM_TEAMS
:
7458 case OMP_CLAUSE_THREAD_LIMIT
:
7459 case OMP_CLAUSE_DIST_SCHEDULE
:
7460 case OMP_CLAUSE_DEVICE
:
7461 case OMP_CLAUSE_PRIORITY
:
7462 case OMP_CLAUSE_GRAINSIZE
:
7463 case OMP_CLAUSE_NUM_TASKS
:
7464 case OMP_CLAUSE_HINT
:
7465 case OMP_CLAUSE__CILK_FOR_COUNT_
:
7466 case OMP_CLAUSE_ASYNC
:
7467 case OMP_CLAUSE_WAIT
:
7468 case OMP_CLAUSE_NUM_GANGS
:
7469 case OMP_CLAUSE_NUM_WORKERS
:
7470 case OMP_CLAUSE_VECTOR_LENGTH
:
7471 case OMP_CLAUSE_WORKER
:
7472 case OMP_CLAUSE_VECTOR
:
7473 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 0), pre_p
, NULL
,
7474 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7478 case OMP_CLAUSE_GANG
:
7479 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 0), pre_p
, NULL
,
7480 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7482 if (gimplify_expr (&OMP_CLAUSE_OPERAND (c
, 1), pre_p
, NULL
,
7483 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7487 case OMP_CLAUSE_TILE
:
7488 for (tree list
= OMP_CLAUSE_TILE_LIST (c
); !remove
&& list
;
7489 list
= TREE_CHAIN (list
))
7491 if (gimplify_expr (&TREE_VALUE (list
), pre_p
, NULL
,
7492 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7497 case OMP_CLAUSE_DEVICE_RESIDENT
:
7501 case OMP_CLAUSE_NOWAIT
:
7502 case OMP_CLAUSE_ORDERED
:
7503 case OMP_CLAUSE_UNTIED
:
7504 case OMP_CLAUSE_COLLAPSE
:
7505 case OMP_CLAUSE_AUTO
:
7506 case OMP_CLAUSE_SEQ
:
7507 case OMP_CLAUSE_INDEPENDENT
:
7508 case OMP_CLAUSE_MERGEABLE
:
7509 case OMP_CLAUSE_PROC_BIND
:
7510 case OMP_CLAUSE_SAFELEN
:
7511 case OMP_CLAUSE_SIMDLEN
:
7512 case OMP_CLAUSE_NOGROUP
:
7513 case OMP_CLAUSE_THREADS
:
7514 case OMP_CLAUSE_SIMD
:
7517 case OMP_CLAUSE_DEFAULTMAP
:
7518 ctx
->target_map_scalars_firstprivate
= false;
7521 case OMP_CLAUSE_ALIGNED
:
7522 decl
= OMP_CLAUSE_DECL (c
);
7523 if (error_operand_p (decl
))
7528 if (gimplify_expr (&OMP_CLAUSE_ALIGNED_ALIGNMENT (c
), pre_p
, NULL
,
7529 is_gimple_val
, fb_rvalue
) == GS_ERROR
)
7534 if (!is_global_var (decl
)
7535 && TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
7536 omp_add_variable (ctx
, decl
, GOVD_ALIGNED
);
7539 case OMP_CLAUSE_DEFAULT
:
7540 ctx
->default_kind
= OMP_CLAUSE_DEFAULT_KIND (c
);
7548 *list_p
= OMP_CLAUSE_CHAIN (c
);
7550 list_p
= &OMP_CLAUSE_CHAIN (c
);
7553 gimplify_omp_ctxp
= ctx
;
7554 if (struct_map_to_clause
)
7555 delete struct_map_to_clause
;
7558 /* Return true if DECL is a candidate for shared to firstprivate
7559 optimization. We only consider non-addressable scalars, not
7560 too big, and not references. */
7563 omp_shared_to_firstprivate_optimizable_decl_p (tree decl
)
7565 if (TREE_ADDRESSABLE (decl
))
7567 tree type
= TREE_TYPE (decl
);
7568 if (!is_gimple_reg_type (type
)
7569 || TREE_CODE (type
) == REFERENCE_TYPE
7570 || TREE_ADDRESSABLE (type
))
7572 /* Don't optimize too large decls, as each thread/task will have
7574 HOST_WIDE_INT len
= int_size_in_bytes (type
);
7575 if (len
== -1 || len
> 4 * POINTER_SIZE
/ BITS_PER_UNIT
)
7577 if (lang_hooks
.decls
.omp_privatize_by_reference (decl
))
7582 /* Helper function of omp_find_stores_op and gimplify_adjust_omp_clauses*.
7583 For omp_shared_to_firstprivate_optimizable_decl_p decl mark it as
7584 GOVD_WRITTEN in outer contexts. */
7587 omp_mark_stores (struct gimplify_omp_ctx
*ctx
, tree decl
)
7589 for (; ctx
; ctx
= ctx
->outer_context
)
7591 splay_tree_node n
= splay_tree_lookup (ctx
->variables
,
7592 (splay_tree_key
) decl
);
7595 else if (n
->value
& GOVD_SHARED
)
7597 n
->value
|= GOVD_WRITTEN
;
7600 else if (n
->value
& GOVD_DATA_SHARE_CLASS
)
7605 /* Helper callback for walk_gimple_seq to discover possible stores
7606 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
7607 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
7611 omp_find_stores_op (tree
*tp
, int *walk_subtrees
, void *data
)
7613 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
7622 if (handled_component_p (op
))
7623 op
= TREE_OPERAND (op
, 0);
7624 else if ((TREE_CODE (op
) == MEM_REF
|| TREE_CODE (op
) == TARGET_MEM_REF
)
7625 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
)
7626 op
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
7631 if (!DECL_P (op
) || !omp_shared_to_firstprivate_optimizable_decl_p (op
))
7634 omp_mark_stores (gimplify_omp_ctxp
, op
);
7638 /* Helper callback for walk_gimple_seq to discover possible stores
7639 to omp_shared_to_firstprivate_optimizable_decl_p decls and set
7640 GOVD_WRITTEN if they are GOVD_SHARED in some outer context
7644 omp_find_stores_stmt (gimple_stmt_iterator
*gsi_p
,
7645 bool *handled_ops_p
,
7646 struct walk_stmt_info
*wi
)
7648 gimple
*stmt
= gsi_stmt (*gsi_p
);
7649 switch (gimple_code (stmt
))
7651 /* Don't recurse on OpenMP constructs for which
7652 gimplify_adjust_omp_clauses already handled the bodies,
7653 except handle gimple_omp_for_pre_body. */
7654 case GIMPLE_OMP_FOR
:
7655 *handled_ops_p
= true;
7656 if (gimple_omp_for_pre_body (stmt
))
7657 walk_gimple_seq (gimple_omp_for_pre_body (stmt
),
7658 omp_find_stores_stmt
, omp_find_stores_op
, wi
);
7660 case GIMPLE_OMP_PARALLEL
:
7661 case GIMPLE_OMP_TASK
:
7662 case GIMPLE_OMP_SECTIONS
:
7663 case GIMPLE_OMP_SINGLE
:
7664 case GIMPLE_OMP_TARGET
:
7665 case GIMPLE_OMP_TEAMS
:
7666 case GIMPLE_OMP_CRITICAL
:
7667 *handled_ops_p
= true;
7675 struct gimplify_adjust_omp_clauses_data
7681 /* For all variables that were not actually used within the context,
7682 remove PRIVATE, SHARED, and FIRSTPRIVATE clauses. */
7685 gimplify_adjust_omp_clauses_1 (splay_tree_node n
, void *data
)
7687 tree
*list_p
= ((struct gimplify_adjust_omp_clauses_data
*) data
)->list_p
;
7689 = ((struct gimplify_adjust_omp_clauses_data
*) data
)->pre_p
;
7690 tree decl
= (tree
) n
->key
;
7691 unsigned flags
= n
->value
;
7692 enum omp_clause_code code
;
7696 if (flags
& (GOVD_EXPLICIT
| GOVD_LOCAL
))
7698 if ((flags
& GOVD_SEEN
) == 0)
7700 if (flags
& GOVD_DEBUG_PRIVATE
)
7702 gcc_assert ((flags
& GOVD_DATA_SHARE_CLASS
) == GOVD_PRIVATE
);
7703 private_debug
= true;
7705 else if (flags
& GOVD_MAP
)
7706 private_debug
= false;
7709 = lang_hooks
.decls
.omp_private_debug_clause (decl
,
7710 !!(flags
& GOVD_SHARED
));
7712 code
= OMP_CLAUSE_PRIVATE
;
7713 else if (flags
& GOVD_MAP
)
7714 code
= OMP_CLAUSE_MAP
;
7715 else if (flags
& GOVD_SHARED
)
7717 if (is_global_var (decl
))
7719 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
->outer_context
;
7723 = splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7724 if (on
&& (on
->value
& (GOVD_FIRSTPRIVATE
| GOVD_LASTPRIVATE
7725 | GOVD_PRIVATE
| GOVD_REDUCTION
7726 | GOVD_LINEAR
| GOVD_MAP
)) != 0)
7728 ctx
= ctx
->outer_context
;
7733 code
= OMP_CLAUSE_SHARED
;
7735 else if (flags
& GOVD_PRIVATE
)
7736 code
= OMP_CLAUSE_PRIVATE
;
7737 else if (flags
& GOVD_FIRSTPRIVATE
)
7738 code
= OMP_CLAUSE_FIRSTPRIVATE
;
7739 else if (flags
& GOVD_LASTPRIVATE
)
7740 code
= OMP_CLAUSE_LASTPRIVATE
;
7741 else if (flags
& GOVD_ALIGNED
)
7746 if (((flags
& GOVD_LASTPRIVATE
)
7747 || (code
== OMP_CLAUSE_SHARED
&& (flags
& GOVD_WRITTEN
)))
7748 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
7749 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
7751 clause
= build_omp_clause (input_location
, code
);
7752 OMP_CLAUSE_DECL (clause
) = decl
;
7753 OMP_CLAUSE_CHAIN (clause
) = *list_p
;
7755 OMP_CLAUSE_PRIVATE_DEBUG (clause
) = 1;
7756 else if (code
== OMP_CLAUSE_PRIVATE
&& (flags
& GOVD_PRIVATE_OUTER_REF
))
7757 OMP_CLAUSE_PRIVATE_OUTER_REF (clause
) = 1;
7758 else if (code
== OMP_CLAUSE_SHARED
7759 && (flags
& GOVD_WRITTEN
) == 0
7760 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
7761 OMP_CLAUSE_SHARED_READONLY (clause
) = 1;
7762 else if (code
== OMP_CLAUSE_FIRSTPRIVATE
&& (flags
& GOVD_EXPLICIT
) == 0)
7763 OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clause
) = 1;
7764 else if (code
== OMP_CLAUSE_MAP
&& (flags
& GOVD_MAP_0LEN_ARRAY
) != 0)
7766 tree nc
= build_omp_clause (input_location
, OMP_CLAUSE_MAP
);
7767 OMP_CLAUSE_DECL (nc
) = decl
;
7768 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
7769 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) == POINTER_TYPE
)
7770 OMP_CLAUSE_DECL (clause
)
7771 = build_simple_mem_ref_loc (input_location
, decl
);
7772 OMP_CLAUSE_DECL (clause
)
7773 = build2 (MEM_REF
, char_type_node
, OMP_CLAUSE_DECL (clause
),
7774 build_int_cst (build_pointer_type (char_type_node
), 0));
7775 OMP_CLAUSE_SIZE (clause
) = size_zero_node
;
7776 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
7777 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_ALLOC
);
7778 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (clause
) = 1;
7779 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
7780 OMP_CLAUSE_CHAIN (nc
) = *list_p
;
7781 OMP_CLAUSE_CHAIN (clause
) = nc
;
7782 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
7783 gimplify_omp_ctxp
= ctx
->outer_context
;
7784 gimplify_expr (&TREE_OPERAND (OMP_CLAUSE_DECL (clause
), 0),
7785 pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
7786 gimplify_omp_ctxp
= ctx
;
7788 else if (code
== OMP_CLAUSE_MAP
)
7790 int kind
= (flags
& GOVD_MAP_TO_ONLY
7793 if (flags
& GOVD_MAP_FORCE
)
7794 kind
|= GOMP_MAP_FLAG_FORCE
;
7795 OMP_CLAUSE_SET_MAP_KIND (clause
, kind
);
7796 if (DECL_SIZE (decl
)
7797 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
7799 tree decl2
= DECL_VALUE_EXPR (decl
);
7800 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
7801 decl2
= TREE_OPERAND (decl2
, 0);
7802 gcc_assert (DECL_P (decl2
));
7803 tree mem
= build_simple_mem_ref (decl2
);
7804 OMP_CLAUSE_DECL (clause
) = mem
;
7805 OMP_CLAUSE_SIZE (clause
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
7806 if (gimplify_omp_ctxp
->outer_context
)
7808 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
->outer_context
;
7809 omp_notice_variable (ctx
, decl2
, true);
7810 omp_notice_variable (ctx
, OMP_CLAUSE_SIZE (clause
), true);
7812 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
),
7814 OMP_CLAUSE_DECL (nc
) = decl
;
7815 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
7816 if (gimplify_omp_ctxp
->target_firstprivatize_array_bases
)
7817 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
7819 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_POINTER
);
7820 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (clause
);
7821 OMP_CLAUSE_CHAIN (clause
) = nc
;
7823 else if (gimplify_omp_ctxp
->target_firstprivatize_array_bases
7824 && lang_hooks
.decls
.omp_privatize_by_reference (decl
))
7826 OMP_CLAUSE_DECL (clause
) = build_simple_mem_ref (decl
);
7827 OMP_CLAUSE_SIZE (clause
)
7828 = unshare_expr (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl
))));
7829 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
7830 gimplify_omp_ctxp
= ctx
->outer_context
;
7831 gimplify_expr (&OMP_CLAUSE_SIZE (clause
),
7832 pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
7833 gimplify_omp_ctxp
= ctx
;
7834 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
),
7836 OMP_CLAUSE_DECL (nc
) = decl
;
7837 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
7838 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_FIRSTPRIVATE_REFERENCE
);
7839 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (clause
);
7840 OMP_CLAUSE_CHAIN (clause
) = nc
;
7843 OMP_CLAUSE_SIZE (clause
) = DECL_SIZE_UNIT (decl
);
7845 if (code
== OMP_CLAUSE_FIRSTPRIVATE
&& (flags
& GOVD_LASTPRIVATE
) != 0)
7847 tree nc
= build_omp_clause (input_location
, OMP_CLAUSE_LASTPRIVATE
);
7848 OMP_CLAUSE_DECL (nc
) = decl
;
7849 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (nc
) = 1;
7850 OMP_CLAUSE_CHAIN (nc
) = *list_p
;
7851 OMP_CLAUSE_CHAIN (clause
) = nc
;
7852 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
7853 gimplify_omp_ctxp
= ctx
->outer_context
;
7854 lang_hooks
.decls
.omp_finish_clause (nc
, pre_p
);
7855 gimplify_omp_ctxp
= ctx
;
7858 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
7859 gimplify_omp_ctxp
= ctx
->outer_context
;
7860 lang_hooks
.decls
.omp_finish_clause (clause
, pre_p
);
7861 gimplify_omp_ctxp
= ctx
;
7866 gimplify_adjust_omp_clauses (gimple_seq
*pre_p
, gimple_seq body
, tree
*list_p
,
7867 enum tree_code code
)
7869 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
7874 struct gimplify_omp_ctx
*octx
;
7875 for (octx
= ctx
; octx
; octx
= octx
->outer_context
)
7876 if ((octx
->region_type
& (ORT_PARALLEL
| ORT_TASK
| ORT_TEAMS
)) != 0)
7880 struct walk_stmt_info wi
;
7881 memset (&wi
, 0, sizeof (wi
));
7882 walk_gimple_seq (body
, omp_find_stores_stmt
,
7883 omp_find_stores_op
, &wi
);
7886 while ((c
= *list_p
) != NULL
)
7889 bool remove
= false;
7891 switch (OMP_CLAUSE_CODE (c
))
7893 case OMP_CLAUSE_PRIVATE
:
7894 case OMP_CLAUSE_SHARED
:
7895 case OMP_CLAUSE_FIRSTPRIVATE
:
7896 case OMP_CLAUSE_LINEAR
:
7897 decl
= OMP_CLAUSE_DECL (c
);
7898 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7899 remove
= !(n
->value
& GOVD_SEEN
);
7902 bool shared
= OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
;
7903 if ((n
->value
& GOVD_DEBUG_PRIVATE
)
7904 || lang_hooks
.decls
.omp_private_debug_clause (decl
, shared
))
7906 gcc_assert ((n
->value
& GOVD_DEBUG_PRIVATE
) == 0
7907 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
7909 OMP_CLAUSE_SET_CODE (c
, OMP_CLAUSE_PRIVATE
);
7910 OMP_CLAUSE_PRIVATE_DEBUG (c
) = 1;
7912 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
7913 && (n
->value
& GOVD_WRITTEN
) == 0
7915 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
7916 OMP_CLAUSE_SHARED_READONLY (c
) = 1;
7917 else if (DECL_P (decl
)
7918 && ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
7919 && (n
->value
& GOVD_WRITTEN
) != 1)
7920 || (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
7921 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)))
7922 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
7923 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
7927 case OMP_CLAUSE_LASTPRIVATE
:
7928 /* Make sure OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE is set to
7929 accurately reflect the presence of a FIRSTPRIVATE clause. */
7930 decl
= OMP_CLAUSE_DECL (c
);
7931 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7932 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
)
7933 = (n
->value
& GOVD_FIRSTPRIVATE
) != 0;
7934 if (omp_no_lastprivate (ctx
))
7936 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
7939 OMP_CLAUSE_CODE (c
) = OMP_CLAUSE_PRIVATE
;
7941 else if (code
== OMP_DISTRIBUTE
7942 && OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
7945 error_at (OMP_CLAUSE_LOCATION (c
),
7946 "same variable used in %<firstprivate%> and "
7947 "%<lastprivate%> clauses on %<distribute%> "
7951 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
7953 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
7954 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
7957 case OMP_CLAUSE_ALIGNED
:
7958 decl
= OMP_CLAUSE_DECL (c
);
7959 if (!is_global_var (decl
))
7961 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7962 remove
= n
== NULL
|| !(n
->value
& GOVD_SEEN
);
7963 if (!remove
&& TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
7965 struct gimplify_omp_ctx
*octx
;
7967 && (n
->value
& (GOVD_DATA_SHARE_CLASS
7968 & ~GOVD_FIRSTPRIVATE
)))
7971 for (octx
= ctx
->outer_context
; octx
;
7972 octx
= octx
->outer_context
)
7974 n
= splay_tree_lookup (octx
->variables
,
7975 (splay_tree_key
) decl
);
7978 if (n
->value
& GOVD_LOCAL
)
7980 /* We have to avoid assigning a shared variable
7981 to itself when trying to add
7982 __builtin_assume_aligned. */
7983 if (n
->value
& GOVD_SHARED
)
7991 else if (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
7993 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
7994 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
7999 case OMP_CLAUSE_MAP
:
8000 if (code
== OMP_TARGET_EXIT_DATA
8001 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ALWAYS_POINTER
)
8006 decl
= OMP_CLAUSE_DECL (c
);
8007 /* Data clasues associated with acc parallel reductions must be
8008 compatible with present_or_copy. Warn and adjust the clause
8009 if that is not the case. */
8010 if (ctx
->region_type
== ORT_ACC_PARALLEL
)
8012 tree t
= DECL_P (decl
) ? decl
: TREE_OPERAND (decl
, 0);
8016 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) t
);
8018 if (n
&& (n
->value
& GOVD_REDUCTION
))
8020 enum gomp_map_kind kind
= OMP_CLAUSE_MAP_KIND (c
);
8022 OMP_CLAUSE_MAP_IN_REDUCTION (c
) = 1;
8023 if ((kind
& GOMP_MAP_TOFROM
) != GOMP_MAP_TOFROM
8024 && kind
!= GOMP_MAP_FORCE_PRESENT
8025 && kind
!= GOMP_MAP_POINTER
)
8027 warning_at (OMP_CLAUSE_LOCATION (c
), 0,
8028 "incompatible data clause with reduction "
8029 "on %qE; promoting to present_or_copy",
8031 OMP_CLAUSE_SET_MAP_KIND (c
, GOMP_MAP_TOFROM
);
8037 if ((ctx
->region_type
& ORT_TARGET
) != 0
8038 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
8040 if (TREE_CODE (decl
) == INDIRECT_REF
8041 && TREE_CODE (TREE_OPERAND (decl
, 0)) == COMPONENT_REF
8042 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (decl
, 0)))
8044 decl
= TREE_OPERAND (decl
, 0);
8045 if (TREE_CODE (decl
) == COMPONENT_REF
)
8047 while (TREE_CODE (decl
) == COMPONENT_REF
)
8048 decl
= TREE_OPERAND (decl
, 0);
8051 n
= splay_tree_lookup (ctx
->variables
,
8052 (splay_tree_key
) decl
);
8053 if (!(n
->value
& GOVD_SEEN
))
8060 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8061 if ((ctx
->region_type
& ORT_TARGET
) != 0
8062 && !(n
->value
& GOVD_SEEN
)
8063 && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c
)) == 0
8064 && !lookup_attribute ("omp declare target link",
8065 DECL_ATTRIBUTES (decl
)))
8068 /* For struct element mapping, if struct is never referenced
8069 in target block and none of the mapping has always modifier,
8070 remove all the struct element mappings, which immediately
8071 follow the GOMP_MAP_STRUCT map clause. */
8072 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_STRUCT
)
8074 HOST_WIDE_INT cnt
= tree_to_shwi (OMP_CLAUSE_SIZE (c
));
8076 OMP_CLAUSE_CHAIN (c
)
8077 = OMP_CLAUSE_CHAIN (OMP_CLAUSE_CHAIN (c
));
8080 else if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_STRUCT
8081 && code
== OMP_TARGET_EXIT_DATA
)
8083 else if (DECL_SIZE (decl
)
8084 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
8085 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_POINTER
8086 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FIRSTPRIVATE_POINTER
8087 && (OMP_CLAUSE_MAP_KIND (c
)
8088 != GOMP_MAP_FIRSTPRIVATE_REFERENCE
))
8090 /* For GOMP_MAP_FORCE_DEVICEPTR, we'll never enter here, because
8091 for these, TREE_CODE (DECL_SIZE (decl)) will always be
8093 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
8095 tree decl2
= DECL_VALUE_EXPR (decl
);
8096 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
8097 decl2
= TREE_OPERAND (decl2
, 0);
8098 gcc_assert (DECL_P (decl2
));
8099 tree mem
= build_simple_mem_ref (decl2
);
8100 OMP_CLAUSE_DECL (c
) = mem
;
8101 OMP_CLAUSE_SIZE (c
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
8102 if (ctx
->outer_context
)
8104 omp_notice_variable (ctx
->outer_context
, decl2
, true);
8105 omp_notice_variable (ctx
->outer_context
,
8106 OMP_CLAUSE_SIZE (c
), true);
8108 if (((ctx
->region_type
& ORT_TARGET
) != 0
8109 || !ctx
->target_firstprivatize_array_bases
)
8110 && ((n
->value
& GOVD_SEEN
) == 0
8111 || (n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
)) == 0))
8113 tree nc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
8115 OMP_CLAUSE_DECL (nc
) = decl
;
8116 OMP_CLAUSE_SIZE (nc
) = size_zero_node
;
8117 if (ctx
->target_firstprivatize_array_bases
)
8118 OMP_CLAUSE_SET_MAP_KIND (nc
,
8119 GOMP_MAP_FIRSTPRIVATE_POINTER
);
8121 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_POINTER
);
8122 OMP_CLAUSE_CHAIN (nc
) = OMP_CLAUSE_CHAIN (c
);
8123 OMP_CLAUSE_CHAIN (c
) = nc
;
8129 if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
8130 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
8131 gcc_assert ((n
->value
& GOVD_SEEN
) == 0
8132 || ((n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
))
8138 case OMP_CLAUSE_FROM
:
8139 case OMP_CLAUSE__CACHE_
:
8140 decl
= OMP_CLAUSE_DECL (c
);
8143 if (DECL_SIZE (decl
)
8144 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
8146 tree decl2
= DECL_VALUE_EXPR (decl
);
8147 gcc_assert (TREE_CODE (decl2
) == INDIRECT_REF
);
8148 decl2
= TREE_OPERAND (decl2
, 0);
8149 gcc_assert (DECL_P (decl2
));
8150 tree mem
= build_simple_mem_ref (decl2
);
8151 OMP_CLAUSE_DECL (c
) = mem
;
8152 OMP_CLAUSE_SIZE (c
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
8153 if (ctx
->outer_context
)
8155 omp_notice_variable (ctx
->outer_context
, decl2
, true);
8156 omp_notice_variable (ctx
->outer_context
,
8157 OMP_CLAUSE_SIZE (c
), true);
8160 else if (OMP_CLAUSE_SIZE (c
) == NULL_TREE
)
8161 OMP_CLAUSE_SIZE (c
) = DECL_SIZE_UNIT (decl
);
8164 case OMP_CLAUSE_REDUCTION
:
8165 decl
= OMP_CLAUSE_DECL (c
);
8166 /* OpenACC reductions need a present_or_copy data clause.
8167 Add one if necessary. Error is the reduction is private. */
8168 if (ctx
->region_type
== ORT_ACC_PARALLEL
)
8170 n
= splay_tree_lookup (ctx
->variables
, (splay_tree_key
) decl
);
8171 if (n
->value
& (GOVD_PRIVATE
| GOVD_FIRSTPRIVATE
))
8172 error_at (OMP_CLAUSE_LOCATION (c
), "invalid private "
8173 "reduction on %qE", DECL_NAME (decl
));
8174 else if ((n
->value
& GOVD_MAP
) == 0)
8176 tree next
= OMP_CLAUSE_CHAIN (c
);
8177 tree nc
= build_omp_clause (UNKNOWN_LOCATION
, OMP_CLAUSE_MAP
);
8178 OMP_CLAUSE_SET_MAP_KIND (nc
, GOMP_MAP_TOFROM
);
8179 OMP_CLAUSE_DECL (nc
) = decl
;
8180 OMP_CLAUSE_CHAIN (c
) = nc
;
8181 lang_hooks
.decls
.omp_finish_clause (nc
, pre_p
);
8184 OMP_CLAUSE_MAP_IN_REDUCTION (nc
) = 1;
8185 if (OMP_CLAUSE_CHAIN (nc
) == NULL
)
8187 nc
= OMP_CLAUSE_CHAIN (nc
);
8189 OMP_CLAUSE_CHAIN (nc
) = next
;
8190 n
->value
|= GOVD_MAP
;
8194 && omp_shared_to_firstprivate_optimizable_decl_p (decl
))
8195 omp_mark_stores (gimplify_omp_ctxp
->outer_context
, decl
);
8197 case OMP_CLAUSE_COPYIN
:
8198 case OMP_CLAUSE_COPYPRIVATE
:
8200 case OMP_CLAUSE_NUM_THREADS
:
8201 case OMP_CLAUSE_NUM_TEAMS
:
8202 case OMP_CLAUSE_THREAD_LIMIT
:
8203 case OMP_CLAUSE_DIST_SCHEDULE
:
8204 case OMP_CLAUSE_DEVICE
:
8205 case OMP_CLAUSE_SCHEDULE
:
8206 case OMP_CLAUSE_NOWAIT
:
8207 case OMP_CLAUSE_ORDERED
:
8208 case OMP_CLAUSE_DEFAULT
:
8209 case OMP_CLAUSE_UNTIED
:
8210 case OMP_CLAUSE_COLLAPSE
:
8211 case OMP_CLAUSE_FINAL
:
8212 case OMP_CLAUSE_MERGEABLE
:
8213 case OMP_CLAUSE_PROC_BIND
:
8214 case OMP_CLAUSE_SAFELEN
:
8215 case OMP_CLAUSE_SIMDLEN
:
8216 case OMP_CLAUSE_DEPEND
:
8217 case OMP_CLAUSE_PRIORITY
:
8218 case OMP_CLAUSE_GRAINSIZE
:
8219 case OMP_CLAUSE_NUM_TASKS
:
8220 case OMP_CLAUSE_NOGROUP
:
8221 case OMP_CLAUSE_THREADS
:
8222 case OMP_CLAUSE_SIMD
:
8223 case OMP_CLAUSE_HINT
:
8224 case OMP_CLAUSE_DEFAULTMAP
:
8225 case OMP_CLAUSE_USE_DEVICE_PTR
:
8226 case OMP_CLAUSE_IS_DEVICE_PTR
:
8227 case OMP_CLAUSE__CILK_FOR_COUNT_
:
8228 case OMP_CLAUSE_ASYNC
:
8229 case OMP_CLAUSE_WAIT
:
8230 case OMP_CLAUSE_DEVICE_RESIDENT
:
8231 case OMP_CLAUSE_INDEPENDENT
:
8232 case OMP_CLAUSE_NUM_GANGS
:
8233 case OMP_CLAUSE_NUM_WORKERS
:
8234 case OMP_CLAUSE_VECTOR_LENGTH
:
8235 case OMP_CLAUSE_GANG
:
8236 case OMP_CLAUSE_WORKER
:
8237 case OMP_CLAUSE_VECTOR
:
8238 case OMP_CLAUSE_AUTO
:
8239 case OMP_CLAUSE_SEQ
:
8240 case OMP_CLAUSE_TILE
:
8248 *list_p
= OMP_CLAUSE_CHAIN (c
);
8250 list_p
= &OMP_CLAUSE_CHAIN (c
);
8253 /* Add in any implicit data sharing. */
8254 struct gimplify_adjust_omp_clauses_data data
;
8255 data
.list_p
= list_p
;
8257 splay_tree_foreach (ctx
->variables
, gimplify_adjust_omp_clauses_1
, &data
);
8259 gimplify_omp_ctxp
= ctx
->outer_context
;
8260 delete_omp_context (ctx
);
8263 /* Gimplify OACC_CACHE. */
8266 gimplify_oacc_cache (tree
*expr_p
, gimple_seq
*pre_p
)
8268 tree expr
= *expr_p
;
8270 gimplify_scan_omp_clauses (&OACC_CACHE_CLAUSES (expr
), pre_p
, ORT_ACC
,
8272 gimplify_adjust_omp_clauses (pre_p
, NULL
, &OACC_CACHE_CLAUSES (expr
),
8275 /* TODO: Do something sensible with this information. */
8277 *expr_p
= NULL_TREE
;
8280 /* Helper function of gimplify_oacc_declare. The helper's purpose is to,
8281 if required, translate 'kind' in CLAUSE into an 'entry' kind and 'exit'
8282 kind. The entry kind will replace the one in CLAUSE, while the exit
8283 kind will be used in a new omp_clause and returned to the caller. */
8286 gimplify_oacc_declare_1 (tree clause
)
8288 HOST_WIDE_INT kind
, new_op
;
8292 kind
= OMP_CLAUSE_MAP_KIND (clause
);
8296 case GOMP_MAP_ALLOC
:
8297 case GOMP_MAP_FORCE_ALLOC
:
8298 case GOMP_MAP_FORCE_TO
:
8299 new_op
= GOMP_MAP_DELETE
;
8303 case GOMP_MAP_FORCE_FROM
:
8304 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_ALLOC
);
8305 new_op
= GOMP_MAP_FORCE_FROM
;
8309 case GOMP_MAP_FORCE_TOFROM
:
8310 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_TO
);
8311 new_op
= GOMP_MAP_FORCE_FROM
;
8316 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_FORCE_ALLOC
);
8317 new_op
= GOMP_MAP_FROM
;
8321 case GOMP_MAP_TOFROM
:
8322 OMP_CLAUSE_SET_MAP_KIND (clause
, GOMP_MAP_TO
);
8323 new_op
= GOMP_MAP_FROM
;
8327 case GOMP_MAP_DEVICE_RESIDENT
:
8328 case GOMP_MAP_FORCE_DEVICEPTR
:
8329 case GOMP_MAP_FORCE_PRESENT
:
8331 case GOMP_MAP_POINTER
:
8342 c
= build_omp_clause (OMP_CLAUSE_LOCATION (clause
), OMP_CLAUSE_MAP
);
8343 OMP_CLAUSE_SET_MAP_KIND (c
, new_op
);
8344 OMP_CLAUSE_DECL (c
) = OMP_CLAUSE_DECL (clause
);
8350 /* Gimplify OACC_DECLARE. */
8353 gimplify_oacc_declare (tree
*expr_p
, gimple_seq
*pre_p
)
8355 tree expr
= *expr_p
;
8359 clauses
= OACC_DECLARE_CLAUSES (expr
);
8361 gimplify_scan_omp_clauses (&clauses
, pre_p
, ORT_TARGET_DATA
, OACC_DECLARE
);
8363 for (t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
8365 tree decl
= OMP_CLAUSE_DECL (t
);
8367 if (TREE_CODE (decl
) == MEM_REF
)
8370 if (TREE_CODE (decl
) == VAR_DECL
8371 && !is_global_var (decl
)
8372 && DECL_CONTEXT (decl
) == current_function_decl
)
8374 tree c
= gimplify_oacc_declare_1 (t
);
8377 if (oacc_declare_returns
== NULL
)
8378 oacc_declare_returns
= new hash_map
<tree
, tree
>;
8380 oacc_declare_returns
->put (decl
, c
);
8384 omp_add_variable (gimplify_omp_ctxp
, decl
, GOVD_SEEN
);
8387 stmt
= gimple_build_omp_target (NULL
, GF_OMP_TARGET_KIND_OACC_DECLARE
,
8390 gimplify_seq_add_stmt (pre_p
, stmt
);
8392 *expr_p
= NULL_TREE
;
8395 /* Gimplify the contents of an OMP_PARALLEL statement. This involves
8396 gimplification of the body, as well as scanning the body for used
8397 variables. We need to do this scan now, because variable-sized
8398 decls will be decomposed during gimplification. */
8401 gimplify_omp_parallel (tree
*expr_p
, gimple_seq
*pre_p
)
8403 tree expr
= *expr_p
;
8405 gimple_seq body
= NULL
;
8407 gimplify_scan_omp_clauses (&OMP_PARALLEL_CLAUSES (expr
), pre_p
,
8408 OMP_PARALLEL_COMBINED (expr
)
8409 ? ORT_COMBINED_PARALLEL
8410 : ORT_PARALLEL
, OMP_PARALLEL
);
8412 push_gimplify_context ();
8414 g
= gimplify_and_return_first (OMP_PARALLEL_BODY (expr
), &body
);
8415 if (gimple_code (g
) == GIMPLE_BIND
)
8416 pop_gimplify_context (g
);
8418 pop_gimplify_context (NULL
);
8420 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_PARALLEL_CLAUSES (expr
),
8423 g
= gimple_build_omp_parallel (body
,
8424 OMP_PARALLEL_CLAUSES (expr
),
8425 NULL_TREE
, NULL_TREE
);
8426 if (OMP_PARALLEL_COMBINED (expr
))
8427 gimple_omp_set_subcode (g
, GF_OMP_PARALLEL_COMBINED
);
8428 gimplify_seq_add_stmt (pre_p
, g
);
8429 *expr_p
= NULL_TREE
;
8432 /* Gimplify the contents of an OMP_TASK statement. This involves
8433 gimplification of the body, as well as scanning the body for used
8434 variables. We need to do this scan now, because variable-sized
8435 decls will be decomposed during gimplification. */
8438 gimplify_omp_task (tree
*expr_p
, gimple_seq
*pre_p
)
8440 tree expr
= *expr_p
;
8442 gimple_seq body
= NULL
;
8444 gimplify_scan_omp_clauses (&OMP_TASK_CLAUSES (expr
), pre_p
,
8445 find_omp_clause (OMP_TASK_CLAUSES (expr
),
8447 ? ORT_UNTIED_TASK
: ORT_TASK
, OMP_TASK
);
8449 push_gimplify_context ();
8451 g
= gimplify_and_return_first (OMP_TASK_BODY (expr
), &body
);
8452 if (gimple_code (g
) == GIMPLE_BIND
)
8453 pop_gimplify_context (g
);
8455 pop_gimplify_context (NULL
);
8457 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_TASK_CLAUSES (expr
),
8460 g
= gimple_build_omp_task (body
,
8461 OMP_TASK_CLAUSES (expr
),
8462 NULL_TREE
, NULL_TREE
,
8463 NULL_TREE
, NULL_TREE
, NULL_TREE
);
8464 gimplify_seq_add_stmt (pre_p
, g
);
8465 *expr_p
= NULL_TREE
;
8468 /* Helper function of gimplify_omp_for, find OMP_FOR resp. OMP_SIMD
8469 with non-NULL OMP_FOR_INIT. */
8472 find_combined_omp_for (tree
*tp
, int *walk_subtrees
, void *)
8475 switch (TREE_CODE (*tp
))
8481 if (OMP_FOR_INIT (*tp
) != NULL_TREE
)
8485 case STATEMENT_LIST
:
8495 /* Gimplify the gross structure of an OMP_FOR statement. */
8497 static enum gimplify_status
8498 gimplify_omp_for (tree
*expr_p
, gimple_seq
*pre_p
)
8500 tree for_stmt
, orig_for_stmt
, inner_for_stmt
= NULL_TREE
, decl
, var
, t
;
8501 enum gimplify_status ret
= GS_ALL_DONE
;
8502 enum gimplify_status tret
;
8504 gimple_seq for_body
, for_pre_body
;
8506 bitmap has_decl_expr
= NULL
;
8507 enum omp_region_type ort
= ORT_WORKSHARE
;
8509 orig_for_stmt
= for_stmt
= *expr_p
;
8511 switch (TREE_CODE (for_stmt
))
8515 case OMP_DISTRIBUTE
:
8521 if (find_omp_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_UNTIED
))
8522 ort
= ORT_UNTIED_TASK
;
8534 /* Set OMP_CLAUSE_LINEAR_NO_COPYIN flag on explicit linear
8535 clause for the IV. */
8536 if (ort
== ORT_SIMD
&& TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
8538 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), 0);
8539 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
8540 decl
= TREE_OPERAND (t
, 0);
8541 for (tree c
= OMP_FOR_CLAUSES (for_stmt
); c
; c
= OMP_CLAUSE_CHAIN (c
))
8542 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
8543 && OMP_CLAUSE_DECL (c
) == decl
)
8545 OMP_CLAUSE_LINEAR_NO_COPYIN (c
) = 1;
8550 if (OMP_FOR_INIT (for_stmt
) == NULL_TREE
)
8552 gcc_assert (TREE_CODE (for_stmt
) != OACC_LOOP
);
8553 inner_for_stmt
= walk_tree (&OMP_FOR_BODY (for_stmt
),
8554 find_combined_omp_for
, NULL
, NULL
);
8555 if (inner_for_stmt
== NULL_TREE
)
8557 gcc_assert (seen_error ());
8558 *expr_p
= NULL_TREE
;
8563 if (TREE_CODE (for_stmt
) != OMP_TASKLOOP
)
8564 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (for_stmt
), pre_p
, ort
,
8565 TREE_CODE (for_stmt
));
8567 if (TREE_CODE (for_stmt
) == OMP_DISTRIBUTE
)
8568 gimplify_omp_ctxp
->distribute
= true;
8570 /* Handle OMP_FOR_INIT. */
8571 for_pre_body
= NULL
;
8572 if (ort
== ORT_SIMD
&& OMP_FOR_PRE_BODY (for_stmt
))
8574 has_decl_expr
= BITMAP_ALLOC (NULL
);
8575 if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt
)) == DECL_EXPR
8576 && TREE_CODE (DECL_EXPR_DECL (OMP_FOR_PRE_BODY (for_stmt
)))
8579 t
= OMP_FOR_PRE_BODY (for_stmt
);
8580 bitmap_set_bit (has_decl_expr
, DECL_UID (DECL_EXPR_DECL (t
)));
8582 else if (TREE_CODE (OMP_FOR_PRE_BODY (for_stmt
)) == STATEMENT_LIST
)
8584 tree_stmt_iterator si
;
8585 for (si
= tsi_start (OMP_FOR_PRE_BODY (for_stmt
)); !tsi_end_p (si
);
8589 if (TREE_CODE (t
) == DECL_EXPR
8590 && TREE_CODE (DECL_EXPR_DECL (t
)) == VAR_DECL
)
8591 bitmap_set_bit (has_decl_expr
, DECL_UID (DECL_EXPR_DECL (t
)));
8595 if (OMP_FOR_PRE_BODY (for_stmt
))
8597 if (TREE_CODE (for_stmt
) != OMP_TASKLOOP
|| gimplify_omp_ctxp
)
8598 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt
), &for_pre_body
);
8601 struct gimplify_omp_ctx ctx
;
8602 memset (&ctx
, 0, sizeof (ctx
));
8603 ctx
.region_type
= ORT_NONE
;
8604 gimplify_omp_ctxp
= &ctx
;
8605 gimplify_and_add (OMP_FOR_PRE_BODY (for_stmt
), &for_pre_body
);
8606 gimplify_omp_ctxp
= NULL
;
8609 OMP_FOR_PRE_BODY (for_stmt
) = NULL_TREE
;
8611 if (OMP_FOR_INIT (for_stmt
) == NULL_TREE
)
8612 for_stmt
= inner_for_stmt
;
8614 /* For taskloop, need to gimplify the start, end and step before the
8615 taskloop, outside of the taskloop omp context. */
8616 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
8618 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
8620 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
8621 if (!is_gimple_constant (TREE_OPERAND (t
, 1)))
8624 = get_initialized_tmp_var (TREE_OPERAND (t
, 1),
8625 pre_p
, NULL
, false);
8626 tree c
= build_omp_clause (input_location
,
8627 OMP_CLAUSE_FIRSTPRIVATE
);
8628 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (t
, 1);
8629 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
8630 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
8633 /* Handle OMP_FOR_COND. */
8634 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
8635 if (!is_gimple_constant (TREE_OPERAND (t
, 1)))
8638 = get_initialized_tmp_var (TREE_OPERAND (t
, 1),
8639 gimple_seq_empty_p (for_pre_body
)
8640 ? pre_p
: &for_pre_body
, NULL
,
8642 tree c
= build_omp_clause (input_location
,
8643 OMP_CLAUSE_FIRSTPRIVATE
);
8644 OMP_CLAUSE_DECL (c
) = TREE_OPERAND (t
, 1);
8645 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
8646 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
8649 /* Handle OMP_FOR_INCR. */
8650 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
8651 if (TREE_CODE (t
) == MODIFY_EXPR
)
8653 decl
= TREE_OPERAND (t
, 0);
8654 t
= TREE_OPERAND (t
, 1);
8655 tree
*tp
= &TREE_OPERAND (t
, 1);
8656 if (TREE_CODE (t
) == PLUS_EXPR
&& *tp
== decl
)
8657 tp
= &TREE_OPERAND (t
, 0);
8659 if (!is_gimple_constant (*tp
))
8661 gimple_seq
*seq
= gimple_seq_empty_p (for_pre_body
)
8662 ? pre_p
: &for_pre_body
;
8663 *tp
= get_initialized_tmp_var (*tp
, seq
, NULL
, false);
8664 tree c
= build_omp_clause (input_location
,
8665 OMP_CLAUSE_FIRSTPRIVATE
);
8666 OMP_CLAUSE_DECL (c
) = *tp
;
8667 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (orig_for_stmt
);
8668 OMP_FOR_CLAUSES (orig_for_stmt
) = c
;
8673 gimplify_scan_omp_clauses (&OMP_FOR_CLAUSES (orig_for_stmt
), pre_p
, ort
,
8677 if (orig_for_stmt
!= for_stmt
)
8678 gimplify_omp_ctxp
->combined_loop
= true;
8681 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
8682 == TREE_VEC_LENGTH (OMP_FOR_COND (for_stmt
)));
8683 gcc_assert (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
8684 == TREE_VEC_LENGTH (OMP_FOR_INCR (for_stmt
)));
8686 tree c
= find_omp_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_ORDERED
);
8687 bool is_doacross
= false;
8688 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
8691 gimplify_omp_ctxp
->loop_iter_var
.create (TREE_VEC_LENGTH
8692 (OMP_FOR_INIT (for_stmt
))
8696 c
= find_omp_clause (OMP_FOR_CLAUSES (for_stmt
), OMP_CLAUSE_COLLAPSE
);
8698 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c
));
8699 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
8701 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
8702 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
8703 decl
= TREE_OPERAND (t
, 0);
8704 gcc_assert (DECL_P (decl
));
8705 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (decl
))
8706 || POINTER_TYPE_P (TREE_TYPE (decl
)));
8709 if (TREE_CODE (for_stmt
) == OMP_FOR
&& OMP_FOR_ORIG_DECLS (for_stmt
))
8710 gimplify_omp_ctxp
->loop_iter_var
.quick_push
8711 (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (for_stmt
), i
));
8713 gimplify_omp_ctxp
->loop_iter_var
.quick_push (decl
);
8714 gimplify_omp_ctxp
->loop_iter_var
.quick_push (decl
);
8717 /* Make sure the iteration variable is private. */
8719 tree c2
= NULL_TREE
;
8720 if (orig_for_stmt
!= for_stmt
)
8721 /* Do this only on innermost construct for combined ones. */;
8722 else if (ort
== ORT_SIMD
)
8724 splay_tree_node n
= splay_tree_lookup (gimplify_omp_ctxp
->variables
,
8725 (splay_tree_key
) decl
);
8726 omp_is_private (gimplify_omp_ctxp
, decl
,
8727 1 + (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
))
8729 if (n
!= NULL
&& (n
->value
& GOVD_DATA_SHARE_CLASS
) != 0)
8730 omp_notice_variable (gimplify_omp_ctxp
, decl
, true);
8731 else if (TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
8733 c
= build_omp_clause (input_location
, OMP_CLAUSE_LINEAR
);
8734 OMP_CLAUSE_LINEAR_NO_COPYIN (c
) = 1;
8735 unsigned int flags
= GOVD_LINEAR
| GOVD_EXPLICIT
| GOVD_SEEN
;
8737 && bitmap_bit_p (has_decl_expr
, DECL_UID (decl
)))
8738 || omp_no_lastprivate (gimplify_omp_ctxp
))
8740 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
8741 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
8743 struct gimplify_omp_ctx
*outer
8744 = gimplify_omp_ctxp
->outer_context
;
8745 if (outer
&& !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
8747 if (outer
->region_type
== ORT_WORKSHARE
8748 && outer
->combined_loop
)
8750 n
= splay_tree_lookup (outer
->variables
,
8751 (splay_tree_key
)decl
);
8752 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
8754 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
8755 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
8759 struct gimplify_omp_ctx
*octx
= outer
->outer_context
;
8761 && octx
->region_type
== ORT_COMBINED_PARALLEL
8762 && octx
->outer_context
8763 && (octx
->outer_context
->region_type
8765 && octx
->outer_context
->combined_loop
)
8767 octx
= octx
->outer_context
;
8768 n
= splay_tree_lookup (octx
->variables
,
8769 (splay_tree_key
)decl
);
8770 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
8772 OMP_CLAUSE_LINEAR_NO_COPYOUT (c
) = 1;
8773 flags
|= GOVD_LINEAR_LASTPRIVATE_NO_OUTER
;
8780 OMP_CLAUSE_DECL (c
) = decl
;
8781 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (for_stmt
);
8782 OMP_FOR_CLAUSES (for_stmt
) = c
;
8783 omp_add_variable (gimplify_omp_ctxp
, decl
, flags
);
8784 if (outer
&& !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
))
8786 if (outer
->region_type
== ORT_WORKSHARE
8787 && outer
->combined_loop
)
8789 if (outer
->outer_context
8790 && (outer
->outer_context
->region_type
8791 == ORT_COMBINED_PARALLEL
))
8792 outer
= outer
->outer_context
;
8793 else if (omp_check_private (outer
, decl
, false))
8796 else if (((outer
->region_type
& ORT_TASK
) != 0)
8797 && outer
->combined_loop
8798 && !omp_check_private (gimplify_omp_ctxp
,
8801 else if (outer
->region_type
!= ORT_COMBINED_PARALLEL
)
8803 omp_notice_variable (outer
, decl
, true);
8808 n
= splay_tree_lookup (outer
->variables
,
8809 (splay_tree_key
)decl
);
8810 if (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
8812 omp_add_variable (outer
, decl
,
8813 GOVD_LASTPRIVATE
| GOVD_SEEN
);
8814 if (outer
->region_type
== ORT_COMBINED_PARALLEL
8815 && outer
->outer_context
8816 && (outer
->outer_context
->region_type
8818 && outer
->outer_context
->combined_loop
)
8820 outer
= outer
->outer_context
;
8821 n
= splay_tree_lookup (outer
->variables
,
8822 (splay_tree_key
)decl
);
8823 if (omp_check_private (outer
, decl
, false))
8826 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
8828 omp_add_variable (outer
, decl
,
8834 if (outer
&& outer
->outer_context
8835 && (outer
->outer_context
->region_type
8836 == ORT_COMBINED_TEAMS
))
8838 outer
= outer
->outer_context
;
8839 n
= splay_tree_lookup (outer
->variables
,
8840 (splay_tree_key
)decl
);
8842 || (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
8843 omp_add_variable (outer
, decl
,
8844 GOVD_SHARED
| GOVD_SEEN
);
8848 if (outer
&& outer
->outer_context
)
8849 omp_notice_variable (outer
->outer_context
, decl
,
8859 || !bitmap_bit_p (has_decl_expr
, DECL_UID (decl
)))
8860 && !omp_no_lastprivate (gimplify_omp_ctxp
);
8861 struct gimplify_omp_ctx
*outer
8862 = gimplify_omp_ctxp
->outer_context
;
8863 if (outer
&& lastprivate
)
8865 if (outer
->region_type
== ORT_WORKSHARE
8866 && outer
->combined_loop
)
8868 n
= splay_tree_lookup (outer
->variables
,
8869 (splay_tree_key
)decl
);
8870 if (n
!= NULL
&& (n
->value
& GOVD_LOCAL
) != 0)
8872 lastprivate
= false;
8875 else if (outer
->outer_context
8876 && (outer
->outer_context
->region_type
8877 == ORT_COMBINED_PARALLEL
))
8878 outer
= outer
->outer_context
;
8879 else if (omp_check_private (outer
, decl
, false))
8882 else if (((outer
->region_type
& ORT_TASK
) != 0)
8883 && outer
->combined_loop
8884 && !omp_check_private (gimplify_omp_ctxp
,
8887 else if (outer
->region_type
!= ORT_COMBINED_PARALLEL
)
8889 omp_notice_variable (outer
, decl
, true);
8894 n
= splay_tree_lookup (outer
->variables
,
8895 (splay_tree_key
)decl
);
8896 if (n
== NULL
|| (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
8898 omp_add_variable (outer
, decl
,
8899 GOVD_LASTPRIVATE
| GOVD_SEEN
);
8900 if (outer
->region_type
== ORT_COMBINED_PARALLEL
8901 && outer
->outer_context
8902 && (outer
->outer_context
->region_type
8904 && outer
->outer_context
->combined_loop
)
8906 outer
= outer
->outer_context
;
8907 n
= splay_tree_lookup (outer
->variables
,
8908 (splay_tree_key
)decl
);
8909 if (omp_check_private (outer
, decl
, false))
8912 || ((n
->value
& GOVD_DATA_SHARE_CLASS
)
8914 omp_add_variable (outer
, decl
,
8920 if (outer
&& outer
->outer_context
8921 && (outer
->outer_context
->region_type
8922 == ORT_COMBINED_TEAMS
))
8924 outer
= outer
->outer_context
;
8925 n
= splay_tree_lookup (outer
->variables
,
8926 (splay_tree_key
)decl
);
8928 || (n
->value
& GOVD_DATA_SHARE_CLASS
) == 0)
8929 omp_add_variable (outer
, decl
,
8930 GOVD_SHARED
| GOVD_SEEN
);
8934 if (outer
&& outer
->outer_context
)
8935 omp_notice_variable (outer
->outer_context
, decl
,
8941 c
= build_omp_clause (input_location
,
8942 lastprivate
? OMP_CLAUSE_LASTPRIVATE
8943 : OMP_CLAUSE_PRIVATE
);
8944 OMP_CLAUSE_DECL (c
) = decl
;
8945 OMP_CLAUSE_CHAIN (c
) = OMP_FOR_CLAUSES (for_stmt
);
8946 OMP_FOR_CLAUSES (for_stmt
) = c
;
8947 omp_add_variable (gimplify_omp_ctxp
, decl
,
8948 (lastprivate
? GOVD_LASTPRIVATE
: GOVD_PRIVATE
)
8949 | GOVD_EXPLICIT
| GOVD_SEEN
);
8953 else if (omp_is_private (gimplify_omp_ctxp
, decl
, 0))
8954 omp_notice_variable (gimplify_omp_ctxp
, decl
, true);
8956 omp_add_variable (gimplify_omp_ctxp
, decl
, GOVD_PRIVATE
| GOVD_SEEN
);
8958 /* If DECL is not a gimple register, create a temporary variable to act
8959 as an iteration counter. This is valid, since DECL cannot be
8960 modified in the body of the loop. Similarly for any iteration vars
8961 in simd with collapse > 1 where the iterator vars must be
8963 if (orig_for_stmt
!= for_stmt
)
8965 else if (!is_gimple_reg (decl
)
8967 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) > 1))
8969 var
= create_tmp_var (TREE_TYPE (decl
), get_name (decl
));
8970 TREE_OPERAND (t
, 0) = var
;
8972 gimplify_seq_add_stmt (&for_body
, gimple_build_assign (decl
, var
));
8975 && TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)) == 1)
8977 c2
= build_omp_clause (input_location
, OMP_CLAUSE_LINEAR
);
8978 OMP_CLAUSE_LINEAR_NO_COPYIN (c2
) = 1;
8979 OMP_CLAUSE_LINEAR_NO_COPYOUT (c2
) = 1;
8980 OMP_CLAUSE_DECL (c2
) = var
;
8981 OMP_CLAUSE_CHAIN (c2
) = OMP_FOR_CLAUSES (for_stmt
);
8982 OMP_FOR_CLAUSES (for_stmt
) = c2
;
8983 omp_add_variable (gimplify_omp_ctxp
, var
,
8984 GOVD_LINEAR
| GOVD_EXPLICIT
| GOVD_SEEN
);
8992 omp_add_variable (gimplify_omp_ctxp
, var
,
8993 GOVD_PRIVATE
| GOVD_SEEN
);
8998 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
8999 is_gimple_val
, fb_rvalue
, false);
9000 ret
= MIN (ret
, tret
);
9001 if (ret
== GS_ERROR
)
9004 /* Handle OMP_FOR_COND. */
9005 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
9006 gcc_assert (COMPARISON_CLASS_P (t
));
9007 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
9009 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
9010 is_gimple_val
, fb_rvalue
, false);
9011 ret
= MIN (ret
, tret
);
9013 /* Handle OMP_FOR_INCR. */
9014 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
9015 switch (TREE_CODE (t
))
9017 case PREINCREMENT_EXPR
:
9018 case POSTINCREMENT_EXPR
:
9020 tree decl
= TREE_OPERAND (t
, 0);
9021 /* c_omp_for_incr_canonicalize_ptr() should have been
9022 called to massage things appropriately. */
9023 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl
)));
9025 if (orig_for_stmt
!= for_stmt
)
9027 t
= build_int_cst (TREE_TYPE (decl
), 1);
9029 OMP_CLAUSE_LINEAR_STEP (c
) = t
;
9030 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
9031 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
9032 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
9036 case PREDECREMENT_EXPR
:
9037 case POSTDECREMENT_EXPR
:
9038 /* c_omp_for_incr_canonicalize_ptr() should have been
9039 called to massage things appropriately. */
9040 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl
)));
9041 if (orig_for_stmt
!= for_stmt
)
9043 t
= build_int_cst (TREE_TYPE (decl
), -1);
9045 OMP_CLAUSE_LINEAR_STEP (c
) = t
;
9046 t
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), var
, t
);
9047 t
= build2 (MODIFY_EXPR
, TREE_TYPE (var
), var
, t
);
9048 TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
) = t
;
9052 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
9053 TREE_OPERAND (t
, 0) = var
;
9055 t
= TREE_OPERAND (t
, 1);
9056 switch (TREE_CODE (t
))
9059 if (TREE_OPERAND (t
, 1) == decl
)
9061 TREE_OPERAND (t
, 1) = TREE_OPERAND (t
, 0);
9062 TREE_OPERAND (t
, 0) = var
;
9068 case POINTER_PLUS_EXPR
:
9069 gcc_assert (TREE_OPERAND (t
, 0) == decl
);
9070 TREE_OPERAND (t
, 0) = var
;
9076 tret
= gimplify_expr (&TREE_OPERAND (t
, 1), &for_pre_body
, NULL
,
9077 is_gimple_val
, fb_rvalue
, false);
9078 ret
= MIN (ret
, tret
);
9081 tree step
= TREE_OPERAND (t
, 1);
9082 tree stept
= TREE_TYPE (decl
);
9083 if (POINTER_TYPE_P (stept
))
9085 step
= fold_convert (stept
, step
);
9086 if (TREE_CODE (t
) == MINUS_EXPR
)
9087 step
= fold_build1 (NEGATE_EXPR
, stept
, step
);
9088 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
9089 if (step
!= TREE_OPERAND (t
, 1))
9091 tret
= gimplify_expr (&OMP_CLAUSE_LINEAR_STEP (c
),
9092 &for_pre_body
, NULL
,
9093 is_gimple_val
, fb_rvalue
, false);
9094 ret
= MIN (ret
, tret
);
9106 OMP_CLAUSE_LINEAR_STEP (c2
) = OMP_CLAUSE_LINEAR_STEP (c
);
9109 if ((var
!= decl
|| collapse
> 1) && orig_for_stmt
== for_stmt
)
9111 for (c
= OMP_FOR_CLAUSES (for_stmt
); c
; c
= OMP_CLAUSE_CHAIN (c
))
9112 if (((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
9113 && OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
) == NULL
)
9114 || (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
9115 && !OMP_CLAUSE_LINEAR_NO_COPYOUT (c
)
9116 && OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
) == NULL
))
9117 && OMP_CLAUSE_DECL (c
) == decl
)
9119 if (is_doacross
&& (collapse
== 1 || i
>= collapse
))
9123 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
9124 gcc_assert (TREE_CODE (t
) == MODIFY_EXPR
);
9125 gcc_assert (TREE_OPERAND (t
, 0) == var
);
9126 t
= TREE_OPERAND (t
, 1);
9127 gcc_assert (TREE_CODE (t
) == PLUS_EXPR
9128 || TREE_CODE (t
) == MINUS_EXPR
9129 || TREE_CODE (t
) == POINTER_PLUS_EXPR
);
9130 gcc_assert (TREE_OPERAND (t
, 0) == var
);
9131 t
= build2 (TREE_CODE (t
), TREE_TYPE (decl
),
9132 is_doacross
? var
: decl
,
9133 TREE_OPERAND (t
, 1));
9136 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
9137 seq
= &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c
);
9139 seq
= &OMP_CLAUSE_LINEAR_GIMPLE_SEQ (c
);
9140 gimplify_assign (decl
, t
, seq
);
9145 BITMAP_FREE (has_decl_expr
);
9147 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
9149 push_gimplify_context ();
9150 if (TREE_CODE (OMP_FOR_BODY (orig_for_stmt
)) != BIND_EXPR
)
9152 OMP_FOR_BODY (orig_for_stmt
)
9153 = build3 (BIND_EXPR
, void_type_node
, NULL
,
9154 OMP_FOR_BODY (orig_for_stmt
), NULL
);
9155 TREE_SIDE_EFFECTS (OMP_FOR_BODY (orig_for_stmt
)) = 1;
9159 gimple
*g
= gimplify_and_return_first (OMP_FOR_BODY (orig_for_stmt
),
9162 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
9164 if (gimple_code (g
) == GIMPLE_BIND
)
9165 pop_gimplify_context (g
);
9167 pop_gimplify_context (NULL
);
9170 if (orig_for_stmt
!= for_stmt
)
9171 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
9173 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
9174 decl
= TREE_OPERAND (t
, 0);
9175 struct gimplify_omp_ctx
*ctx
= gimplify_omp_ctxp
;
9176 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
9177 gimplify_omp_ctxp
= ctx
->outer_context
;
9178 var
= create_tmp_var (TREE_TYPE (decl
), get_name (decl
));
9179 gimplify_omp_ctxp
= ctx
;
9180 omp_add_variable (gimplify_omp_ctxp
, var
, GOVD_PRIVATE
| GOVD_SEEN
);
9181 TREE_OPERAND (t
, 0) = var
;
9182 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
9183 TREE_OPERAND (t
, 1) = copy_node (TREE_OPERAND (t
, 1));
9184 TREE_OPERAND (TREE_OPERAND (t
, 1), 0) = var
;
9187 gimplify_adjust_omp_clauses (pre_p
, for_body
,
9188 &OMP_FOR_CLAUSES (orig_for_stmt
),
9189 TREE_CODE (orig_for_stmt
));
9192 switch (TREE_CODE (orig_for_stmt
))
9194 case OMP_FOR
: kind
= GF_OMP_FOR_KIND_FOR
; break;
9195 case OMP_SIMD
: kind
= GF_OMP_FOR_KIND_SIMD
; break;
9196 case CILK_SIMD
: kind
= GF_OMP_FOR_KIND_CILKSIMD
; break;
9197 case CILK_FOR
: kind
= GF_OMP_FOR_KIND_CILKFOR
; break;
9198 case OMP_DISTRIBUTE
: kind
= GF_OMP_FOR_KIND_DISTRIBUTE
; break;
9199 case OMP_TASKLOOP
: kind
= GF_OMP_FOR_KIND_TASKLOOP
; break;
9200 case OACC_LOOP
: kind
= GF_OMP_FOR_KIND_OACC_LOOP
; break;
9204 gfor
= gimple_build_omp_for (for_body
, kind
, OMP_FOR_CLAUSES (orig_for_stmt
),
9205 TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)),
9207 if (orig_for_stmt
!= for_stmt
)
9208 gimple_omp_for_set_combined_p (gfor
, true);
9209 if (gimplify_omp_ctxp
9210 && (gimplify_omp_ctxp
->combined_loop
9211 || (gimplify_omp_ctxp
->region_type
== ORT_COMBINED_PARALLEL
9212 && gimplify_omp_ctxp
->outer_context
9213 && gimplify_omp_ctxp
->outer_context
->combined_loop
)))
9215 gimple_omp_for_set_combined_into_p (gfor
, true);
9216 if (gimplify_omp_ctxp
->combined_loop
)
9217 gcc_assert (TREE_CODE (orig_for_stmt
) == OMP_SIMD
);
9219 gcc_assert (TREE_CODE (orig_for_stmt
) == OMP_FOR
);
9222 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (for_stmt
)); i
++)
9224 t
= TREE_VEC_ELT (OMP_FOR_INIT (for_stmt
), i
);
9225 gimple_omp_for_set_index (gfor
, i
, TREE_OPERAND (t
, 0));
9226 gimple_omp_for_set_initial (gfor
, i
, TREE_OPERAND (t
, 1));
9227 t
= TREE_VEC_ELT (OMP_FOR_COND (for_stmt
), i
);
9228 gimple_omp_for_set_cond (gfor
, i
, TREE_CODE (t
));
9229 gimple_omp_for_set_final (gfor
, i
, TREE_OPERAND (t
, 1));
9230 t
= TREE_VEC_ELT (OMP_FOR_INCR (for_stmt
), i
);
9231 gimple_omp_for_set_incr (gfor
, i
, TREE_OPERAND (t
, 1));
9234 /* OMP_TASKLOOP is gimplified as two GIMPLE_OMP_FOR taskloop
9235 constructs with GIMPLE_OMP_TASK sandwiched in between them.
9236 The outer taskloop stands for computing the number of iterations,
9237 counts for collapsed loops and holding taskloop specific clauses.
9238 The task construct stands for the effect of data sharing on the
9239 explicit task it creates and the inner taskloop stands for expansion
9240 of the static loop inside of the explicit task construct. */
9241 if (TREE_CODE (orig_for_stmt
) == OMP_TASKLOOP
)
9243 tree
*gfor_clauses_ptr
= gimple_omp_for_clauses_ptr (gfor
);
9244 tree task_clauses
= NULL_TREE
;
9245 tree c
= *gfor_clauses_ptr
;
9246 tree
*gtask_clauses_ptr
= &task_clauses
;
9247 tree outer_for_clauses
= NULL_TREE
;
9248 tree
*gforo_clauses_ptr
= &outer_for_clauses
;
9249 for (; c
; c
= OMP_CLAUSE_CHAIN (c
))
9250 switch (OMP_CLAUSE_CODE (c
))
9252 /* These clauses are allowed on task, move them there. */
9253 case OMP_CLAUSE_SHARED
:
9254 case OMP_CLAUSE_FIRSTPRIVATE
:
9255 case OMP_CLAUSE_DEFAULT
:
9257 case OMP_CLAUSE_UNTIED
:
9258 case OMP_CLAUSE_FINAL
:
9259 case OMP_CLAUSE_MERGEABLE
:
9260 case OMP_CLAUSE_PRIORITY
:
9261 *gtask_clauses_ptr
= c
;
9262 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9264 case OMP_CLAUSE_PRIVATE
:
9265 if (OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c
))
9267 /* We want private on outer for and firstprivate
9270 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
9271 OMP_CLAUSE_FIRSTPRIVATE
);
9272 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
9273 lang_hooks
.decls
.omp_finish_clause (*gtask_clauses_ptr
, NULL
);
9274 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
9275 *gforo_clauses_ptr
= c
;
9276 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9280 *gtask_clauses_ptr
= c
;
9281 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9284 /* These clauses go into outer taskloop clauses. */
9285 case OMP_CLAUSE_GRAINSIZE
:
9286 case OMP_CLAUSE_NUM_TASKS
:
9287 case OMP_CLAUSE_NOGROUP
:
9288 *gforo_clauses_ptr
= c
;
9289 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9291 /* Taskloop clause we duplicate on both taskloops. */
9292 case OMP_CLAUSE_COLLAPSE
:
9293 *gfor_clauses_ptr
= c
;
9294 gfor_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9295 *gforo_clauses_ptr
= copy_node (c
);
9296 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr
);
9298 /* For lastprivate, keep the clause on inner taskloop, and add
9299 a shared clause on task. If the same decl is also firstprivate,
9300 add also firstprivate clause on the inner taskloop. */
9301 case OMP_CLAUSE_LASTPRIVATE
:
9302 if (OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c
))
9304 /* For taskloop C++ lastprivate IVs, we want:
9305 1) private on outer taskloop
9306 2) firstprivate and shared on task
9307 3) lastprivate on inner taskloop */
9309 = build_omp_clause (OMP_CLAUSE_LOCATION (c
),
9310 OMP_CLAUSE_FIRSTPRIVATE
);
9311 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
9312 lang_hooks
.decls
.omp_finish_clause (*gtask_clauses_ptr
, NULL
);
9313 gtask_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
9314 OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
) = 1;
9315 *gforo_clauses_ptr
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
9316 OMP_CLAUSE_PRIVATE
);
9317 OMP_CLAUSE_DECL (*gforo_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
9318 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (*gforo_clauses_ptr
) = 1;
9319 TREE_TYPE (*gforo_clauses_ptr
) = TREE_TYPE (c
);
9320 gforo_clauses_ptr
= &OMP_CLAUSE_CHAIN (*gforo_clauses_ptr
);
9322 *gfor_clauses_ptr
= c
;
9323 gfor_clauses_ptr
= &OMP_CLAUSE_CHAIN (c
);
9325 = build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_SHARED
);
9326 OMP_CLAUSE_DECL (*gtask_clauses_ptr
) = OMP_CLAUSE_DECL (c
);
9327 if (OMP_CLAUSE_LASTPRIVATE_FIRSTPRIVATE (c
))
9328 OMP_CLAUSE_SHARED_FIRSTPRIVATE (*gtask_clauses_ptr
) = 1;
9330 = &OMP_CLAUSE_CHAIN (*gtask_clauses_ptr
);
9335 *gfor_clauses_ptr
= NULL_TREE
;
9336 *gtask_clauses_ptr
= NULL_TREE
;
9337 *gforo_clauses_ptr
= NULL_TREE
;
9338 g
= gimple_build_bind (NULL_TREE
, gfor
, NULL_TREE
);
9339 g
= gimple_build_omp_task (g
, task_clauses
, NULL_TREE
, NULL_TREE
,
9340 NULL_TREE
, NULL_TREE
, NULL_TREE
);
9341 gimple_omp_task_set_taskloop_p (g
, true);
9342 g
= gimple_build_bind (NULL_TREE
, g
, NULL_TREE
);
9344 = gimple_build_omp_for (g
, GF_OMP_FOR_KIND_TASKLOOP
, outer_for_clauses
,
9345 gimple_omp_for_collapse (gfor
),
9346 gimple_omp_for_pre_body (gfor
));
9347 gimple_omp_for_set_pre_body (gfor
, NULL
);
9348 gimple_omp_for_set_combined_p (gforo
, true);
9349 gimple_omp_for_set_combined_into_p (gfor
, true);
9350 for (i
= 0; i
< (int) gimple_omp_for_collapse (gfor
); i
++)
9352 t
= unshare_expr (gimple_omp_for_index (gfor
, i
));
9353 gimple_omp_for_set_index (gforo
, i
, t
);
9354 t
= unshare_expr (gimple_omp_for_initial (gfor
, i
));
9355 gimple_omp_for_set_initial (gforo
, i
, t
);
9356 gimple_omp_for_set_cond (gforo
, i
,
9357 gimple_omp_for_cond (gfor
, i
));
9358 t
= unshare_expr (gimple_omp_for_final (gfor
, i
));
9359 gimple_omp_for_set_final (gforo
, i
, t
);
9360 t
= unshare_expr (gimple_omp_for_incr (gfor
, i
));
9361 gimple_omp_for_set_incr (gforo
, i
, t
);
9363 gimplify_seq_add_stmt (pre_p
, gforo
);
9366 gimplify_seq_add_stmt (pre_p
, gfor
);
9367 if (ret
!= GS_ALL_DONE
)
9369 *expr_p
= NULL_TREE
;
9373 /* Helper function of optimize_target_teams, find OMP_TEAMS inside
9374 of OMP_TARGET's body. */
9377 find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
9380 switch (TREE_CODE (*tp
))
9385 case STATEMENT_LIST
:
9394 /* Helper function of optimize_target_teams, determine if the expression
9395 can be computed safely before the target construct on the host. */
9398 computable_teams_clause (tree
*tp
, int *walk_subtrees
, void *)
9407 switch (TREE_CODE (*tp
))
9413 if (error_operand_p (*tp
)
9414 || !INTEGRAL_TYPE_P (TREE_TYPE (*tp
))
9415 || DECL_HAS_VALUE_EXPR_P (*tp
)
9416 || DECL_THREAD_LOCAL_P (*tp
)
9417 || TREE_SIDE_EFFECTS (*tp
)
9418 || TREE_THIS_VOLATILE (*tp
))
9420 if (is_global_var (*tp
)
9421 && (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (*tp
))
9422 || lookup_attribute ("omp declare target link",
9423 DECL_ATTRIBUTES (*tp
))))
9425 n
= splay_tree_lookup (gimplify_omp_ctxp
->variables
,
9426 (splay_tree_key
) *tp
);
9429 if (gimplify_omp_ctxp
->target_map_scalars_firstprivate
)
9433 else if (n
->value
& GOVD_LOCAL
)
9435 else if (n
->value
& GOVD_FIRSTPRIVATE
)
9437 else if ((n
->value
& (GOVD_MAP
| GOVD_MAP_ALWAYS_TO
))
9438 == (GOVD_MAP
| GOVD_MAP_ALWAYS_TO
))
9442 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp
)))
9446 if (TARGET_EXPR_INITIAL (*tp
)
9447 || TREE_CODE (TARGET_EXPR_SLOT (*tp
)) != VAR_DECL
)
9449 return computable_teams_clause (&TARGET_EXPR_SLOT (*tp
),
9450 walk_subtrees
, NULL
);
9451 /* Allow some reasonable subset of integral arithmetics. */
9455 case TRUNC_DIV_EXPR
:
9457 case FLOOR_DIV_EXPR
:
9458 case ROUND_DIV_EXPR
:
9459 case TRUNC_MOD_EXPR
:
9461 case FLOOR_MOD_EXPR
:
9462 case ROUND_MOD_EXPR
:
9464 case EXACT_DIV_EXPR
:
9475 case NON_LVALUE_EXPR
:
9477 if (!INTEGRAL_TYPE_P (TREE_TYPE (*tp
)))
9480 /* And disallow anything else, except for comparisons. */
9482 if (COMPARISON_CLASS_P (*tp
))
9488 /* Try to determine if the num_teams and/or thread_limit expressions
9489 can have their values determined already before entering the
9491 INTEGER_CSTs trivially are,
9492 integral decls that are firstprivate (explicitly or implicitly)
9493 or explicitly map(always, to:) or map(always, tofrom:) on the target
9494 region too, and expressions involving simple arithmetics on those
9495 too, function calls are not ok, dereferencing something neither etc.
9496 Add NUM_TEAMS and THREAD_LIMIT clauses to the OMP_CLAUSES of
9497 EXPR based on what we find:
9498 0 stands for clause not specified at all, use implementation default
9499 -1 stands for value that can't be determined easily before entering
9500 the target construct.
9501 If teams construct is not present at all, use 1 for num_teams
9502 and 0 for thread_limit (only one team is involved, and the thread
9503 limit is implementation defined. */
9506 optimize_target_teams (tree target
, gimple_seq
*pre_p
)
9508 tree body
= OMP_BODY (target
);
9509 tree teams
= walk_tree (&body
, find_omp_teams
, NULL
, NULL
);
9510 tree num_teams
= integer_zero_node
;
9511 tree thread_limit
= integer_zero_node
;
9512 location_t num_teams_loc
= EXPR_LOCATION (target
);
9513 location_t thread_limit_loc
= EXPR_LOCATION (target
);
9515 struct gimplify_omp_ctx
*target_ctx
= gimplify_omp_ctxp
;
9517 if (teams
== NULL_TREE
)
9518 num_teams
= integer_one_node
;
9520 for (c
= OMP_TEAMS_CLAUSES (teams
); c
; c
= OMP_CLAUSE_CHAIN (c
))
9522 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
)
9525 num_teams_loc
= OMP_CLAUSE_LOCATION (c
);
9527 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
9530 thread_limit_loc
= OMP_CLAUSE_LOCATION (c
);
9534 expr
= OMP_CLAUSE_OPERAND (c
, 0);
9535 if (TREE_CODE (expr
) == INTEGER_CST
)
9540 if (walk_tree (&expr
, computable_teams_clause
, NULL
, NULL
))
9542 *p
= integer_minus_one_node
;
9546 gimplify_omp_ctxp
= gimplify_omp_ctxp
->outer_context
;
9547 if (gimplify_expr (p
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
, false)
9550 gimplify_omp_ctxp
= target_ctx
;
9551 *p
= integer_minus_one_node
;
9554 gimplify_omp_ctxp
= target_ctx
;
9555 if (!DECL_P (expr
) && TREE_CODE (expr
) != TARGET_EXPR
)
9556 OMP_CLAUSE_OPERAND (c
, 0) = *p
;
9558 c
= build_omp_clause (thread_limit_loc
, OMP_CLAUSE_THREAD_LIMIT
);
9559 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = thread_limit
;
9560 OMP_CLAUSE_CHAIN (c
) = OMP_TARGET_CLAUSES (target
);
9561 OMP_TARGET_CLAUSES (target
) = c
;
9562 c
= build_omp_clause (num_teams_loc
, OMP_CLAUSE_NUM_TEAMS
);
9563 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = num_teams
;
9564 OMP_CLAUSE_CHAIN (c
) = OMP_TARGET_CLAUSES (target
);
9565 OMP_TARGET_CLAUSES (target
) = c
;
9568 /* Gimplify the gross structure of several OMP constructs. */
9571 gimplify_omp_workshare (tree
*expr_p
, gimple_seq
*pre_p
)
9573 tree expr
= *expr_p
;
9575 gimple_seq body
= NULL
;
9576 enum omp_region_type ort
;
9578 switch (TREE_CODE (expr
))
9582 ort
= ORT_WORKSHARE
;
9585 ort
= OMP_TARGET_COMBINED (expr
) ? ORT_COMBINED_TARGET
: ORT_TARGET
;
9588 ort
= ORT_ACC_KERNELS
;
9591 ort
= ORT_ACC_PARALLEL
;
9596 case OMP_TARGET_DATA
:
9597 ort
= ORT_TARGET_DATA
;
9600 ort
= OMP_TEAMS_COMBINED (expr
) ? ORT_COMBINED_TEAMS
: ORT_TEAMS
;
9602 case OACC_HOST_DATA
:
9603 ort
= ORT_ACC_HOST_DATA
;
9608 gimplify_scan_omp_clauses (&OMP_CLAUSES (expr
), pre_p
, ort
,
9610 if (TREE_CODE (expr
) == OMP_TARGET
)
9611 optimize_target_teams (expr
, pre_p
);
9612 if ((ort
& (ORT_TARGET
| ORT_TARGET_DATA
)) != 0)
9614 push_gimplify_context ();
9615 gimple
*g
= gimplify_and_return_first (OMP_BODY (expr
), &body
);
9616 if (gimple_code (g
) == GIMPLE_BIND
)
9617 pop_gimplify_context (g
);
9619 pop_gimplify_context (NULL
);
9620 if ((ort
& ORT_TARGET_DATA
) != 0)
9622 enum built_in_function end_ix
;
9623 switch (TREE_CODE (expr
))
9626 case OACC_HOST_DATA
:
9627 end_ix
= BUILT_IN_GOACC_DATA_END
;
9629 case OMP_TARGET_DATA
:
9630 end_ix
= BUILT_IN_GOMP_TARGET_END_DATA
;
9635 tree fn
= builtin_decl_explicit (end_ix
);
9636 g
= gimple_build_call (fn
, 0);
9637 gimple_seq cleanup
= NULL
;
9638 gimple_seq_add_stmt (&cleanup
, g
);
9639 g
= gimple_build_try (body
, cleanup
, GIMPLE_TRY_FINALLY
);
9641 gimple_seq_add_stmt (&body
, g
);
9645 gimplify_and_add (OMP_BODY (expr
), &body
);
9646 gimplify_adjust_omp_clauses (pre_p
, body
, &OMP_CLAUSES (expr
),
9649 switch (TREE_CODE (expr
))
9652 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_DATA
,
9653 OMP_CLAUSES (expr
));
9656 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_KERNELS
,
9657 OMP_CLAUSES (expr
));
9659 case OACC_HOST_DATA
:
9660 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_HOST_DATA
,
9661 OMP_CLAUSES (expr
));
9664 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_OACC_PARALLEL
,
9665 OMP_CLAUSES (expr
));
9668 stmt
= gimple_build_omp_sections (body
, OMP_CLAUSES (expr
));
9671 stmt
= gimple_build_omp_single (body
, OMP_CLAUSES (expr
));
9674 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_REGION
,
9675 OMP_CLAUSES (expr
));
9677 case OMP_TARGET_DATA
:
9678 stmt
= gimple_build_omp_target (body
, GF_OMP_TARGET_KIND_DATA
,
9679 OMP_CLAUSES (expr
));
9682 stmt
= gimple_build_omp_teams (body
, OMP_CLAUSES (expr
));
9688 gimplify_seq_add_stmt (pre_p
, stmt
);
9689 *expr_p
= NULL_TREE
;
9692 /* Gimplify the gross structure of OpenACC enter/exit data, update, and OpenMP
9693 target update constructs. */
9696 gimplify_omp_target_update (tree
*expr_p
, gimple_seq
*pre_p
)
9698 tree expr
= *expr_p
;
9701 enum omp_region_type ort
= ORT_WORKSHARE
;
9703 switch (TREE_CODE (expr
))
9705 case OACC_ENTER_DATA
:
9706 case OACC_EXIT_DATA
:
9707 kind
= GF_OMP_TARGET_KIND_OACC_ENTER_EXIT_DATA
;
9711 kind
= GF_OMP_TARGET_KIND_OACC_UPDATE
;
9714 case OMP_TARGET_UPDATE
:
9715 kind
= GF_OMP_TARGET_KIND_UPDATE
;
9717 case OMP_TARGET_ENTER_DATA
:
9718 kind
= GF_OMP_TARGET_KIND_ENTER_DATA
;
9720 case OMP_TARGET_EXIT_DATA
:
9721 kind
= GF_OMP_TARGET_KIND_EXIT_DATA
;
9726 gimplify_scan_omp_clauses (&OMP_STANDALONE_CLAUSES (expr
), pre_p
,
9727 ort
, TREE_CODE (expr
));
9728 gimplify_adjust_omp_clauses (pre_p
, NULL
, &OMP_STANDALONE_CLAUSES (expr
),
9730 stmt
= gimple_build_omp_target (NULL
, kind
, OMP_STANDALONE_CLAUSES (expr
));
9732 gimplify_seq_add_stmt (pre_p
, stmt
);
9733 *expr_p
= NULL_TREE
;
9736 /* A subroutine of gimplify_omp_atomic. The front end is supposed to have
9737 stabilized the lhs of the atomic operation as *ADDR. Return true if
9738 EXPR is this stabilized form. */
9741 goa_lhs_expr_p (tree expr
, tree addr
)
9743 /* Also include casts to other type variants. The C front end is fond
9744 of adding these for e.g. volatile variables. This is like
9745 STRIP_TYPE_NOPS but includes the main variant lookup. */
9746 STRIP_USELESS_TYPE_CONVERSION (expr
);
9748 if (TREE_CODE (expr
) == INDIRECT_REF
)
9750 expr
= TREE_OPERAND (expr
, 0);
9752 && (CONVERT_EXPR_P (expr
)
9753 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
9754 && TREE_CODE (expr
) == TREE_CODE (addr
)
9755 && types_compatible_p (TREE_TYPE (expr
), TREE_TYPE (addr
)))
9757 expr
= TREE_OPERAND (expr
, 0);
9758 addr
= TREE_OPERAND (addr
, 0);
9762 return (TREE_CODE (addr
) == ADDR_EXPR
9763 && TREE_CODE (expr
) == ADDR_EXPR
9764 && TREE_OPERAND (addr
, 0) == TREE_OPERAND (expr
, 0));
9766 if (TREE_CODE (addr
) == ADDR_EXPR
&& expr
== TREE_OPERAND (addr
, 0))
9771 /* Walk *EXPR_P and replace appearances of *LHS_ADDR with LHS_VAR. If an
9772 expression does not involve the lhs, evaluate it into a temporary.
9773 Return 1 if the lhs appeared as a subexpression, 0 if it did not,
9774 or -1 if an error was encountered. */
9777 goa_stabilize_expr (tree
*expr_p
, gimple_seq
*pre_p
, tree lhs_addr
,
9780 tree expr
= *expr_p
;
9783 if (goa_lhs_expr_p (expr
, lhs_addr
))
9788 if (is_gimple_val (expr
))
9792 switch (TREE_CODE_CLASS (TREE_CODE (expr
)))
9795 case tcc_comparison
:
9796 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
, lhs_addr
,
9799 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
, lhs_addr
,
9802 case tcc_expression
:
9803 switch (TREE_CODE (expr
))
9805 case TRUTH_ANDIF_EXPR
:
9806 case TRUTH_ORIF_EXPR
:
9807 case TRUTH_AND_EXPR
:
9809 case TRUTH_XOR_EXPR
:
9810 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 1), pre_p
,
9812 case TRUTH_NOT_EXPR
:
9813 saw_lhs
|= goa_stabilize_expr (&TREE_OPERAND (expr
, 0), pre_p
,
9817 /* Break out any preevaluations from cp_build_modify_expr. */
9818 for (; TREE_CODE (expr
) == COMPOUND_EXPR
;
9819 expr
= TREE_OPERAND (expr
, 1))
9820 gimplify_stmt (&TREE_OPERAND (expr
, 0), pre_p
);
9822 return goa_stabilize_expr (expr_p
, pre_p
, lhs_addr
, lhs_var
);
9833 enum gimplify_status gs
;
9834 gs
= gimplify_expr (expr_p
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
);
9835 if (gs
!= GS_ALL_DONE
)
9842 /* Gimplify an OMP_ATOMIC statement. */
9844 static enum gimplify_status
9845 gimplify_omp_atomic (tree
*expr_p
, gimple_seq
*pre_p
)
9847 tree addr
= TREE_OPERAND (*expr_p
, 0);
9848 tree rhs
= TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
9849 ? NULL
: TREE_OPERAND (*expr_p
, 1);
9850 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (addr
)));
9852 gomp_atomic_load
*loadstmt
;
9853 gomp_atomic_store
*storestmt
;
9855 tmp_load
= create_tmp_reg (type
);
9856 if (rhs
&& goa_stabilize_expr (&rhs
, pre_p
, addr
, tmp_load
) < 0)
9859 if (gimplify_expr (&addr
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
9863 loadstmt
= gimple_build_omp_atomic_load (tmp_load
, addr
);
9864 gimplify_seq_add_stmt (pre_p
, loadstmt
);
9865 if (rhs
&& gimplify_expr (&rhs
, pre_p
, NULL
, is_gimple_val
, fb_rvalue
)
9869 if (TREE_CODE (*expr_p
) == OMP_ATOMIC_READ
)
9871 storestmt
= gimple_build_omp_atomic_store (rhs
);
9872 gimplify_seq_add_stmt (pre_p
, storestmt
);
9873 if (OMP_ATOMIC_SEQ_CST (*expr_p
))
9875 gimple_omp_atomic_set_seq_cst (loadstmt
);
9876 gimple_omp_atomic_set_seq_cst (storestmt
);
9878 switch (TREE_CODE (*expr_p
))
9880 case OMP_ATOMIC_READ
:
9881 case OMP_ATOMIC_CAPTURE_OLD
:
9883 gimple_omp_atomic_set_need_value (loadstmt
);
9885 case OMP_ATOMIC_CAPTURE_NEW
:
9887 gimple_omp_atomic_set_need_value (storestmt
);
9897 /* Gimplify a TRANSACTION_EXPR. This involves gimplification of the
9898 body, and adding some EH bits. */
9900 static enum gimplify_status
9901 gimplify_transaction (tree
*expr_p
, gimple_seq
*pre_p
)
9903 tree expr
= *expr_p
, temp
, tbody
= TRANSACTION_EXPR_BODY (expr
);
9905 gtransaction
*trans_stmt
;
9906 gimple_seq body
= NULL
;
9909 /* Wrap the transaction body in a BIND_EXPR so we have a context
9910 where to put decls for OMP. */
9911 if (TREE_CODE (tbody
) != BIND_EXPR
)
9913 tree bind
= build3 (BIND_EXPR
, void_type_node
, NULL
, tbody
, NULL
);
9914 TREE_SIDE_EFFECTS (bind
) = 1;
9915 SET_EXPR_LOCATION (bind
, EXPR_LOCATION (tbody
));
9916 TRANSACTION_EXPR_BODY (expr
) = bind
;
9919 push_gimplify_context ();
9920 temp
= voidify_wrapper_expr (*expr_p
, NULL
);
9922 body_stmt
= gimplify_and_return_first (TRANSACTION_EXPR_BODY (expr
), &body
);
9923 pop_gimplify_context (body_stmt
);
9925 trans_stmt
= gimple_build_transaction (body
);
9926 if (TRANSACTION_EXPR_OUTER (expr
))
9927 subcode
= GTMA_IS_OUTER
;
9928 else if (TRANSACTION_EXPR_RELAXED (expr
))
9929 subcode
= GTMA_IS_RELAXED
;
9930 gimple_transaction_set_subcode (trans_stmt
, subcode
);
9932 gimplify_seq_add_stmt (pre_p
, trans_stmt
);
9940 *expr_p
= NULL_TREE
;
9944 /* Gimplify an OMP_ORDERED construct. EXPR is the tree version. BODY
9945 is the OMP_BODY of the original EXPR (which has already been
9946 gimplified so it's not present in the EXPR).
9948 Return the gimplified GIMPLE_OMP_ORDERED tuple. */
9951 gimplify_omp_ordered (tree expr
, gimple_seq body
)
9956 tree source_c
= NULL_TREE
;
9957 tree sink_c
= NULL_TREE
;
9959 if (gimplify_omp_ctxp
)
9961 for (c
= OMP_ORDERED_CLAUSES (expr
); c
; c
= OMP_CLAUSE_CHAIN (c
))
9962 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
9963 && gimplify_omp_ctxp
->loop_iter_var
.is_empty ()
9964 && (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
9965 || OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
))
9967 error_at (OMP_CLAUSE_LOCATION (c
),
9968 "%<ordered%> construct with %<depend%> clause must be "
9969 "closely nested inside a loop with %<ordered%> clause "
9970 "with a parameter");
9973 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
9974 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
9977 for (decls
= OMP_CLAUSE_DECL (c
), i
= 0;
9978 decls
&& TREE_CODE (decls
) == TREE_LIST
;
9979 decls
= TREE_CHAIN (decls
), ++i
)
9980 if (i
>= gimplify_omp_ctxp
->loop_iter_var
.length () / 2)
9982 else if (TREE_VALUE (decls
)
9983 != gimplify_omp_ctxp
->loop_iter_var
[2 * i
])
9985 error_at (OMP_CLAUSE_LOCATION (c
),
9986 "variable %qE is not an iteration "
9987 "of outermost loop %d, expected %qE",
9988 TREE_VALUE (decls
), i
+ 1,
9989 gimplify_omp_ctxp
->loop_iter_var
[2 * i
]);
9995 = gimplify_omp_ctxp
->loop_iter_var
[2 * i
+ 1];
9996 if (!fail
&& i
!= gimplify_omp_ctxp
->loop_iter_var
.length () / 2)
9998 error_at (OMP_CLAUSE_LOCATION (c
),
9999 "number of variables in %<depend(sink)%> "
10000 "clause does not match number of "
10001 "iteration variables");
10006 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
10007 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SOURCE
)
10011 error_at (OMP_CLAUSE_LOCATION (c
),
10012 "more than one %<depend(source)%> clause on an "
10013 "%<ordered%> construct");
10020 if (source_c
&& sink_c
)
10022 error_at (OMP_CLAUSE_LOCATION (source_c
),
10023 "%<depend(source)%> clause specified together with "
10024 "%<depend(sink:)%> clauses on the same construct");
10029 return gimple_build_nop ();
10030 return gimple_build_omp_ordered (body
, OMP_ORDERED_CLAUSES (expr
));
10033 /* Convert the GENERIC expression tree *EXPR_P to GIMPLE. If the
10034 expression produces a value to be used as an operand inside a GIMPLE
10035 statement, the value will be stored back in *EXPR_P. This value will
10036 be a tree of class tcc_declaration, tcc_constant, tcc_reference or
10037 an SSA_NAME. The corresponding sequence of GIMPLE statements is
10038 emitted in PRE_P and POST_P.
10040 Additionally, this process may overwrite parts of the input
10041 expression during gimplification. Ideally, it should be
10042 possible to do non-destructive gimplification.
10044 EXPR_P points to the GENERIC expression to convert to GIMPLE. If
10045 the expression needs to evaluate to a value to be used as
10046 an operand in a GIMPLE statement, this value will be stored in
10047 *EXPR_P on exit. This happens when the caller specifies one
10048 of fb_lvalue or fb_rvalue fallback flags.
10050 PRE_P will contain the sequence of GIMPLE statements corresponding
10051 to the evaluation of EXPR and all the side-effects that must
10052 be executed before the main expression. On exit, the last
10053 statement of PRE_P is the core statement being gimplified. For
10054 instance, when gimplifying 'if (++a)' the last statement in
10055 PRE_P will be 'if (t.1)' where t.1 is the result of
10056 pre-incrementing 'a'.
10058 POST_P will contain the sequence of GIMPLE statements corresponding
10059 to the evaluation of all the side-effects that must be executed
10060 after the main expression. If this is NULL, the post
10061 side-effects are stored at the end of PRE_P.
10063 The reason why the output is split in two is to handle post
10064 side-effects explicitly. In some cases, an expression may have
10065 inner and outer post side-effects which need to be emitted in
10066 an order different from the one given by the recursive
10067 traversal. For instance, for the expression (*p--)++ the post
10068 side-effects of '--' must actually occur *after* the post
10069 side-effects of '++'. However, gimplification will first visit
10070 the inner expression, so if a separate POST sequence was not
10071 used, the resulting sequence would be:
10078 However, the post-decrement operation in line #2 must not be
10079 evaluated until after the store to *p at line #4, so the
10080 correct sequence should be:
10087 So, by specifying a separate post queue, it is possible
10088 to emit the post side-effects in the correct order.
10089 If POST_P is NULL, an internal queue will be used. Before
10090 returning to the caller, the sequence POST_P is appended to
10091 the main output sequence PRE_P.
10093 GIMPLE_TEST_F points to a function that takes a tree T and
10094 returns nonzero if T is in the GIMPLE form requested by the
10095 caller. The GIMPLE predicates are in gimple.c.
10097 FALLBACK tells the function what sort of a temporary we want if
10098 gimplification cannot produce an expression that complies with
10101 fb_none means that no temporary should be generated
10102 fb_rvalue means that an rvalue is OK to generate
10103 fb_lvalue means that an lvalue is OK to generate
10104 fb_either means that either is OK, but an lvalue is preferable.
10105 fb_mayfail means that gimplification may fail (in which case
10106 GS_ERROR will be returned)
10108 The return value is either GS_ERROR or GS_ALL_DONE, since this
10109 function iterates until EXPR is completely gimplified or an error
10112 enum gimplify_status
10113 gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
10114 bool (*gimple_test_f
) (tree
), fallback_t fallback
)
10117 gimple_seq internal_pre
= NULL
;
10118 gimple_seq internal_post
= NULL
;
10121 location_t saved_location
;
10122 enum gimplify_status ret
;
10123 gimple_stmt_iterator pre_last_gsi
, post_last_gsi
;
10125 save_expr
= *expr_p
;
10126 if (save_expr
== NULL_TREE
)
10127 return GS_ALL_DONE
;
10129 /* If we are gimplifying a top-level statement, PRE_P must be valid. */
10130 is_statement
= gimple_test_f
== is_gimple_stmt
;
10132 gcc_assert (pre_p
);
10134 /* Consistency checks. */
10135 if (gimple_test_f
== is_gimple_reg
)
10136 gcc_assert (fallback
& (fb_rvalue
| fb_lvalue
));
10137 else if (gimple_test_f
== is_gimple_val
10138 || gimple_test_f
== is_gimple_call_addr
10139 || gimple_test_f
== is_gimple_condexpr
10140 || gimple_test_f
== is_gimple_mem_rhs
10141 || gimple_test_f
== is_gimple_mem_rhs_or_call
10142 || gimple_test_f
== is_gimple_reg_rhs
10143 || gimple_test_f
== is_gimple_reg_rhs_or_call
10144 || gimple_test_f
== is_gimple_asm_val
10145 || gimple_test_f
== is_gimple_mem_ref_addr
)
10146 gcc_assert (fallback
& fb_rvalue
);
10147 else if (gimple_test_f
== is_gimple_min_lval
10148 || gimple_test_f
== is_gimple_lvalue
)
10149 gcc_assert (fallback
& fb_lvalue
);
10150 else if (gimple_test_f
== is_gimple_addressable
)
10151 gcc_assert (fallback
& fb_either
);
10152 else if (gimple_test_f
== is_gimple_stmt
)
10153 gcc_assert (fallback
== fb_none
);
10156 /* We should have recognized the GIMPLE_TEST_F predicate to
10157 know what kind of fallback to use in case a temporary is
10158 needed to hold the value or address of *EXPR_P. */
10159 gcc_unreachable ();
10162 /* We used to check the predicate here and return immediately if it
10163 succeeds. This is wrong; the design is for gimplification to be
10164 idempotent, and for the predicates to only test for valid forms, not
10165 whether they are fully simplified. */
10167 pre_p
= &internal_pre
;
10169 if (post_p
== NULL
)
10170 post_p
= &internal_post
;
10172 /* Remember the last statements added to PRE_P and POST_P. Every
10173 new statement added by the gimplification helpers needs to be
10174 annotated with location information. To centralize the
10175 responsibility, we remember the last statement that had been
10176 added to both queues before gimplifying *EXPR_P. If
10177 gimplification produces new statements in PRE_P and POST_P, those
10178 statements will be annotated with the same location information
10180 pre_last_gsi
= gsi_last (*pre_p
);
10181 post_last_gsi
= gsi_last (*post_p
);
10183 saved_location
= input_location
;
10184 if (save_expr
!= error_mark_node
10185 && EXPR_HAS_LOCATION (*expr_p
))
10186 input_location
= EXPR_LOCATION (*expr_p
);
10188 /* Loop over the specific gimplifiers until the toplevel node
10189 remains the same. */
10192 /* Strip away as many useless type conversions as possible
10193 at the toplevel. */
10194 STRIP_USELESS_TYPE_CONVERSION (*expr_p
);
10196 /* Remember the expr. */
10197 save_expr
= *expr_p
;
10199 /* Die, die, die, my darling. */
10200 if (save_expr
== error_mark_node
10201 || (TREE_TYPE (save_expr
)
10202 && TREE_TYPE (save_expr
) == error_mark_node
))
10208 /* Do any language-specific gimplification. */
10209 ret
= ((enum gimplify_status
)
10210 lang_hooks
.gimplify_expr (expr_p
, pre_p
, post_p
));
10213 if (*expr_p
== NULL_TREE
)
10215 if (*expr_p
!= save_expr
)
10218 else if (ret
!= GS_UNHANDLED
)
10221 /* Make sure that all the cases set 'ret' appropriately. */
10222 ret
= GS_UNHANDLED
;
10223 switch (TREE_CODE (*expr_p
))
10225 /* First deal with the special cases. */
10227 case POSTINCREMENT_EXPR
:
10228 case POSTDECREMENT_EXPR
:
10229 case PREINCREMENT_EXPR
:
10230 case PREDECREMENT_EXPR
:
10231 ret
= gimplify_self_mod_expr (expr_p
, pre_p
, post_p
,
10232 fallback
!= fb_none
,
10233 TREE_TYPE (*expr_p
));
10236 case VIEW_CONVERT_EXPR
:
10237 if (is_gimple_reg_type (TREE_TYPE (*expr_p
))
10238 && is_gimple_reg_type (TREE_TYPE (TREE_OPERAND (*expr_p
, 0))))
10240 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
10241 post_p
, is_gimple_val
, fb_rvalue
);
10242 recalculate_side_effects (*expr_p
);
10248 case ARRAY_RANGE_REF
:
10249 case REALPART_EXPR
:
10250 case IMAGPART_EXPR
:
10251 case COMPONENT_REF
:
10252 ret
= gimplify_compound_lval (expr_p
, pre_p
, post_p
,
10253 fallback
? fallback
: fb_rvalue
);
10257 ret
= gimplify_cond_expr (expr_p
, pre_p
, fallback
);
10259 /* C99 code may assign to an array in a structure value of a
10260 conditional expression, and this has undefined behavior
10261 only on execution, so create a temporary if an lvalue is
10263 if (fallback
== fb_lvalue
)
10265 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
10266 mark_addressable (*expr_p
);
10272 ret
= gimplify_call_expr (expr_p
, pre_p
, fallback
!= fb_none
);
10274 /* C99 code may assign to an array in a structure returned
10275 from a function, and this has undefined behavior only on
10276 execution, so create a temporary if an lvalue is
10278 if (fallback
== fb_lvalue
)
10280 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
10281 mark_addressable (*expr_p
);
10287 gcc_unreachable ();
10289 case COMPOUND_EXPR
:
10290 ret
= gimplify_compound_expr (expr_p
, pre_p
, fallback
!= fb_none
);
10293 case COMPOUND_LITERAL_EXPR
:
10294 ret
= gimplify_compound_literal_expr (expr_p
, pre_p
,
10295 gimple_test_f
, fallback
);
10300 ret
= gimplify_modify_expr (expr_p
, pre_p
, post_p
,
10301 fallback
!= fb_none
);
10304 case TRUTH_ANDIF_EXPR
:
10305 case TRUTH_ORIF_EXPR
:
10307 /* Preserve the original type of the expression and the
10308 source location of the outer expression. */
10309 tree org_type
= TREE_TYPE (*expr_p
);
10310 *expr_p
= gimple_boolify (*expr_p
);
10311 *expr_p
= build3_loc (input_location
, COND_EXPR
,
10315 org_type
, boolean_true_node
),
10318 org_type
, boolean_false_node
));
10323 case TRUTH_NOT_EXPR
:
10325 tree type
= TREE_TYPE (*expr_p
);
10326 /* The parsers are careful to generate TRUTH_NOT_EXPR
10327 only with operands that are always zero or one.
10328 We do not fold here but handle the only interesting case
10329 manually, as fold may re-introduce the TRUTH_NOT_EXPR. */
10330 *expr_p
= gimple_boolify (*expr_p
);
10331 if (TYPE_PRECISION (TREE_TYPE (*expr_p
)) == 1)
10332 *expr_p
= build1_loc (input_location
, BIT_NOT_EXPR
,
10333 TREE_TYPE (*expr_p
),
10334 TREE_OPERAND (*expr_p
, 0));
10336 *expr_p
= build2_loc (input_location
, BIT_XOR_EXPR
,
10337 TREE_TYPE (*expr_p
),
10338 TREE_OPERAND (*expr_p
, 0),
10339 build_int_cst (TREE_TYPE (*expr_p
), 1));
10340 if (!useless_type_conversion_p (type
, TREE_TYPE (*expr_p
)))
10341 *expr_p
= fold_convert_loc (input_location
, type
, *expr_p
);
10347 ret
= gimplify_addr_expr (expr_p
, pre_p
, post_p
);
10350 case ANNOTATE_EXPR
:
10352 tree cond
= TREE_OPERAND (*expr_p
, 0);
10353 tree kind
= TREE_OPERAND (*expr_p
, 1);
10354 tree type
= TREE_TYPE (cond
);
10355 if (!INTEGRAL_TYPE_P (type
))
10361 tree tmp
= create_tmp_var (type
);
10362 gimplify_arg (&cond
, pre_p
, EXPR_LOCATION (*expr_p
));
10364 = gimple_build_call_internal (IFN_ANNOTATE
, 2, cond
, kind
);
10365 gimple_call_set_lhs (call
, tmp
);
10366 gimplify_seq_add_stmt (pre_p
, call
);
10373 ret
= gimplify_va_arg_expr (expr_p
, pre_p
, post_p
);
10377 if (IS_EMPTY_STMT (*expr_p
))
10383 if (VOID_TYPE_P (TREE_TYPE (*expr_p
))
10384 || fallback
== fb_none
)
10386 /* Just strip a conversion to void (or in void context) and
10388 *expr_p
= TREE_OPERAND (*expr_p
, 0);
10393 ret
= gimplify_conversion (expr_p
);
10394 if (ret
== GS_ERROR
)
10396 if (*expr_p
!= save_expr
)
10400 case FIX_TRUNC_EXPR
:
10401 /* unary_expr: ... | '(' cast ')' val | ... */
10402 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
10403 is_gimple_val
, fb_rvalue
);
10404 recalculate_side_effects (*expr_p
);
10409 bool volatilep
= TREE_THIS_VOLATILE (*expr_p
);
10410 bool notrap
= TREE_THIS_NOTRAP (*expr_p
);
10411 tree saved_ptr_type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 0));
10413 *expr_p
= fold_indirect_ref_loc (input_location
, *expr_p
);
10414 if (*expr_p
!= save_expr
)
10420 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
10421 is_gimple_reg
, fb_rvalue
);
10422 if (ret
== GS_ERROR
)
10425 recalculate_side_effects (*expr_p
);
10426 *expr_p
= fold_build2_loc (input_location
, MEM_REF
,
10427 TREE_TYPE (*expr_p
),
10428 TREE_OPERAND (*expr_p
, 0),
10429 build_int_cst (saved_ptr_type
, 0));
10430 TREE_THIS_VOLATILE (*expr_p
) = volatilep
;
10431 TREE_THIS_NOTRAP (*expr_p
) = notrap
;
10436 /* We arrive here through the various re-gimplifcation paths. */
10438 /* First try re-folding the whole thing. */
10439 tmp
= fold_binary (MEM_REF
, TREE_TYPE (*expr_p
),
10440 TREE_OPERAND (*expr_p
, 0),
10441 TREE_OPERAND (*expr_p
, 1));
10444 REF_REVERSE_STORAGE_ORDER (tmp
)
10445 = REF_REVERSE_STORAGE_ORDER (*expr_p
);
10447 recalculate_side_effects (*expr_p
);
10451 /* Avoid re-gimplifying the address operand if it is already
10452 in suitable form. Re-gimplifying would mark the address
10453 operand addressable. Always gimplify when not in SSA form
10454 as we still may have to gimplify decls with value-exprs. */
10455 if (!gimplify_ctxp
|| !gimplify_ctxp
->into_ssa
10456 || !is_gimple_mem_ref_addr (TREE_OPERAND (*expr_p
, 0)))
10458 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
10459 is_gimple_mem_ref_addr
, fb_rvalue
);
10460 if (ret
== GS_ERROR
)
10463 recalculate_side_effects (*expr_p
);
10467 /* Constants need not be gimplified. */
10474 /* Drop the overflow flag on constants, we do not want
10475 that in the GIMPLE IL. */
10476 if (TREE_OVERFLOW_P (*expr_p
))
10477 *expr_p
= drop_tree_overflow (*expr_p
);
10482 /* If we require an lvalue, such as for ADDR_EXPR, retain the
10483 CONST_DECL node. Otherwise the decl is replaceable by its
10485 /* ??? Should be == fb_lvalue, but ADDR_EXPR passes fb_either. */
10486 if (fallback
& fb_lvalue
)
10490 *expr_p
= DECL_INITIAL (*expr_p
);
10496 ret
= gimplify_decl_expr (expr_p
, pre_p
);
10500 ret
= gimplify_bind_expr (expr_p
, pre_p
);
10504 ret
= gimplify_loop_expr (expr_p
, pre_p
);
10508 ret
= gimplify_switch_expr (expr_p
, pre_p
);
10512 ret
= gimplify_exit_expr (expr_p
);
10516 /* If the target is not LABEL, then it is a computed jump
10517 and the target needs to be gimplified. */
10518 if (TREE_CODE (GOTO_DESTINATION (*expr_p
)) != LABEL_DECL
)
10520 ret
= gimplify_expr (&GOTO_DESTINATION (*expr_p
), pre_p
,
10521 NULL
, is_gimple_val
, fb_rvalue
);
10522 if (ret
== GS_ERROR
)
10525 gimplify_seq_add_stmt (pre_p
,
10526 gimple_build_goto (GOTO_DESTINATION (*expr_p
)));
10531 gimplify_seq_add_stmt (pre_p
,
10532 gimple_build_predict (PREDICT_EXPR_PREDICTOR (*expr_p
),
10533 PREDICT_EXPR_OUTCOME (*expr_p
)));
10539 gcc_assert (decl_function_context (LABEL_EXPR_LABEL (*expr_p
))
10540 == current_function_decl
);
10541 gimplify_seq_add_stmt (pre_p
,
10542 gimple_build_label (LABEL_EXPR_LABEL (*expr_p
)));
10545 case CASE_LABEL_EXPR
:
10546 ret
= gimplify_case_label_expr (expr_p
, pre_p
);
10550 ret
= gimplify_return_expr (*expr_p
, pre_p
);
10554 /* Don't reduce this in place; let gimplify_init_constructor work its
10555 magic. Buf if we're just elaborating this for side effects, just
10556 gimplify any element that has side-effects. */
10557 if (fallback
== fb_none
)
10559 unsigned HOST_WIDE_INT ix
;
10561 tree temp
= NULL_TREE
;
10562 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (*expr_p
), ix
, val
)
10563 if (TREE_SIDE_EFFECTS (val
))
10564 append_to_statement_list (val
, &temp
);
10567 ret
= temp
? GS_OK
: GS_ALL_DONE
;
10569 /* C99 code may assign to an array in a constructed
10570 structure or union, and this has undefined behavior only
10571 on execution, so create a temporary if an lvalue is
10573 else if (fallback
== fb_lvalue
)
10575 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, post_p
, false);
10576 mark_addressable (*expr_p
);
10583 /* The following are special cases that are not handled by the
10584 original GIMPLE grammar. */
10586 /* SAVE_EXPR nodes are converted into a GIMPLE identifier and
10589 ret
= gimplify_save_expr (expr_p
, pre_p
, post_p
);
10592 case BIT_FIELD_REF
:
10593 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
10594 post_p
, is_gimple_lvalue
, fb_either
);
10595 recalculate_side_effects (*expr_p
);
10598 case TARGET_MEM_REF
:
10600 enum gimplify_status r0
= GS_ALL_DONE
, r1
= GS_ALL_DONE
;
10602 if (TMR_BASE (*expr_p
))
10603 r0
= gimplify_expr (&TMR_BASE (*expr_p
), pre_p
,
10604 post_p
, is_gimple_mem_ref_addr
, fb_either
);
10605 if (TMR_INDEX (*expr_p
))
10606 r1
= gimplify_expr (&TMR_INDEX (*expr_p
), pre_p
,
10607 post_p
, is_gimple_val
, fb_rvalue
);
10608 if (TMR_INDEX2 (*expr_p
))
10609 r1
= gimplify_expr (&TMR_INDEX2 (*expr_p
), pre_p
,
10610 post_p
, is_gimple_val
, fb_rvalue
);
10611 /* TMR_STEP and TMR_OFFSET are always integer constants. */
10612 ret
= MIN (r0
, r1
);
10616 case NON_LVALUE_EXPR
:
10617 /* This should have been stripped above. */
10618 gcc_unreachable ();
10621 ret
= gimplify_asm_expr (expr_p
, pre_p
, post_p
);
10624 case TRY_FINALLY_EXPR
:
10625 case TRY_CATCH_EXPR
:
10627 gimple_seq eval
, cleanup
;
10630 /* Calls to destructors are generated automatically in FINALLY/CATCH
10631 block. They should have location as UNKNOWN_LOCATION. However,
10632 gimplify_call_expr will reset these call stmts to input_location
10633 if it finds stmt's location is unknown. To prevent resetting for
10634 destructors, we set the input_location to unknown.
10635 Note that this only affects the destructor calls in FINALLY/CATCH
10636 block, and will automatically reset to its original value by the
10637 end of gimplify_expr. */
10638 input_location
= UNKNOWN_LOCATION
;
10639 eval
= cleanup
= NULL
;
10640 gimplify_and_add (TREE_OPERAND (*expr_p
, 0), &eval
);
10641 gimplify_and_add (TREE_OPERAND (*expr_p
, 1), &cleanup
);
10642 /* Don't create bogus GIMPLE_TRY with empty cleanup. */
10643 if (gimple_seq_empty_p (cleanup
))
10645 gimple_seq_add_seq (pre_p
, eval
);
10649 try_
= gimple_build_try (eval
, cleanup
,
10650 TREE_CODE (*expr_p
) == TRY_FINALLY_EXPR
10651 ? GIMPLE_TRY_FINALLY
10652 : GIMPLE_TRY_CATCH
);
10653 if (EXPR_HAS_LOCATION (save_expr
))
10654 gimple_set_location (try_
, EXPR_LOCATION (save_expr
));
10655 else if (LOCATION_LOCUS (saved_location
) != UNKNOWN_LOCATION
)
10656 gimple_set_location (try_
, saved_location
);
10657 if (TREE_CODE (*expr_p
) == TRY_CATCH_EXPR
)
10658 gimple_try_set_catch_is_cleanup (try_
,
10659 TRY_CATCH_IS_CLEANUP (*expr_p
));
10660 gimplify_seq_add_stmt (pre_p
, try_
);
10665 case CLEANUP_POINT_EXPR
:
10666 ret
= gimplify_cleanup_point_expr (expr_p
, pre_p
);
10670 ret
= gimplify_target_expr (expr_p
, pre_p
, post_p
);
10676 gimple_seq handler
= NULL
;
10677 gimplify_and_add (CATCH_BODY (*expr_p
), &handler
);
10678 c
= gimple_build_catch (CATCH_TYPES (*expr_p
), handler
);
10679 gimplify_seq_add_stmt (pre_p
, c
);
10684 case EH_FILTER_EXPR
:
10687 gimple_seq failure
= NULL
;
10689 gimplify_and_add (EH_FILTER_FAILURE (*expr_p
), &failure
);
10690 ehf
= gimple_build_eh_filter (EH_FILTER_TYPES (*expr_p
), failure
);
10691 gimple_set_no_warning (ehf
, TREE_NO_WARNING (*expr_p
));
10692 gimplify_seq_add_stmt (pre_p
, ehf
);
10699 enum gimplify_status r0
, r1
;
10700 r0
= gimplify_expr (&OBJ_TYPE_REF_OBJECT (*expr_p
), pre_p
,
10701 post_p
, is_gimple_val
, fb_rvalue
);
10702 r1
= gimplify_expr (&OBJ_TYPE_REF_EXPR (*expr_p
), pre_p
,
10703 post_p
, is_gimple_val
, fb_rvalue
);
10704 TREE_SIDE_EFFECTS (*expr_p
) = 0;
10705 ret
= MIN (r0
, r1
);
10710 /* We get here when taking the address of a label. We mark
10711 the label as "forced"; meaning it can never be removed and
10712 it is a potential target for any computed goto. */
10713 FORCED_LABEL (*expr_p
) = 1;
10717 case STATEMENT_LIST
:
10718 ret
= gimplify_statement_list (expr_p
, pre_p
);
10721 case WITH_SIZE_EXPR
:
10723 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
10724 post_p
== &internal_post
? NULL
: post_p
,
10725 gimple_test_f
, fallback
);
10726 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
10727 is_gimple_val
, fb_rvalue
);
10734 ret
= gimplify_var_or_parm_decl (expr_p
);
10738 /* When within an OMP context, notice uses of variables. */
10739 if (gimplify_omp_ctxp
)
10740 omp_notice_variable (gimplify_omp_ctxp
, *expr_p
, true);
10745 /* Allow callbacks into the gimplifier during optimization. */
10750 gimplify_omp_parallel (expr_p
, pre_p
);
10755 gimplify_omp_task (expr_p
, pre_p
);
10763 case OMP_DISTRIBUTE
:
10766 ret
= gimplify_omp_for (expr_p
, pre_p
);
10770 gimplify_oacc_cache (expr_p
, pre_p
);
10775 gimplify_oacc_declare (expr_p
, pre_p
);
10779 case OACC_HOST_DATA
:
10782 case OACC_PARALLEL
:
10786 case OMP_TARGET_DATA
:
10788 gimplify_omp_workshare (expr_p
, pre_p
);
10792 case OACC_ENTER_DATA
:
10793 case OACC_EXIT_DATA
:
10795 case OMP_TARGET_UPDATE
:
10796 case OMP_TARGET_ENTER_DATA
:
10797 case OMP_TARGET_EXIT_DATA
:
10798 gimplify_omp_target_update (expr_p
, pre_p
);
10804 case OMP_TASKGROUP
:
10808 gimple_seq body
= NULL
;
10811 gimplify_and_add (OMP_BODY (*expr_p
), &body
);
10812 switch (TREE_CODE (*expr_p
))
10815 g
= gimple_build_omp_section (body
);
10818 g
= gimple_build_omp_master (body
);
10820 case OMP_TASKGROUP
:
10822 gimple_seq cleanup
= NULL
;
10824 = builtin_decl_explicit (BUILT_IN_GOMP_TASKGROUP_END
);
10825 g
= gimple_build_call (fn
, 0);
10826 gimple_seq_add_stmt (&cleanup
, g
);
10827 g
= gimple_build_try (body
, cleanup
, GIMPLE_TRY_FINALLY
);
10829 gimple_seq_add_stmt (&body
, g
);
10830 g
= gimple_build_omp_taskgroup (body
);
10834 g
= gimplify_omp_ordered (*expr_p
, body
);
10837 gimplify_scan_omp_clauses (&OMP_CRITICAL_CLAUSES (*expr_p
),
10838 pre_p
, ORT_WORKSHARE
, OMP_CRITICAL
);
10839 gimplify_adjust_omp_clauses (pre_p
, body
,
10840 &OMP_CRITICAL_CLAUSES (*expr_p
),
10842 g
= gimple_build_omp_critical (body
,
10843 OMP_CRITICAL_NAME (*expr_p
),
10844 OMP_CRITICAL_CLAUSES (*expr_p
));
10847 gcc_unreachable ();
10849 gimplify_seq_add_stmt (pre_p
, g
);
10855 case OMP_ATOMIC_READ
:
10856 case OMP_ATOMIC_CAPTURE_OLD
:
10857 case OMP_ATOMIC_CAPTURE_NEW
:
10858 ret
= gimplify_omp_atomic (expr_p
, pre_p
);
10861 case TRANSACTION_EXPR
:
10862 ret
= gimplify_transaction (expr_p
, pre_p
);
10865 case TRUTH_AND_EXPR
:
10866 case TRUTH_OR_EXPR
:
10867 case TRUTH_XOR_EXPR
:
10869 tree orig_type
= TREE_TYPE (*expr_p
);
10870 tree new_type
, xop0
, xop1
;
10871 *expr_p
= gimple_boolify (*expr_p
);
10872 new_type
= TREE_TYPE (*expr_p
);
10873 if (!useless_type_conversion_p (orig_type
, new_type
))
10875 *expr_p
= fold_convert_loc (input_location
, orig_type
, *expr_p
);
10880 /* Boolified binary truth expressions are semantically equivalent
10881 to bitwise binary expressions. Canonicalize them to the
10882 bitwise variant. */
10883 switch (TREE_CODE (*expr_p
))
10885 case TRUTH_AND_EXPR
:
10886 TREE_SET_CODE (*expr_p
, BIT_AND_EXPR
);
10888 case TRUTH_OR_EXPR
:
10889 TREE_SET_CODE (*expr_p
, BIT_IOR_EXPR
);
10891 case TRUTH_XOR_EXPR
:
10892 TREE_SET_CODE (*expr_p
, BIT_XOR_EXPR
);
10897 /* Now make sure that operands have compatible type to
10898 expression's new_type. */
10899 xop0
= TREE_OPERAND (*expr_p
, 0);
10900 xop1
= TREE_OPERAND (*expr_p
, 1);
10901 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop0
)))
10902 TREE_OPERAND (*expr_p
, 0) = fold_convert_loc (input_location
,
10905 if (!useless_type_conversion_p (new_type
, TREE_TYPE (xop1
)))
10906 TREE_OPERAND (*expr_p
, 1) = fold_convert_loc (input_location
,
10909 /* Continue classified as tcc_binary. */
10913 case VEC_COND_EXPR
:
10915 enum gimplify_status r0
, r1
, r2
;
10917 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
10918 post_p
, is_gimple_condexpr
, fb_rvalue
);
10919 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
10920 post_p
, is_gimple_val
, fb_rvalue
);
10921 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
10922 post_p
, is_gimple_val
, fb_rvalue
);
10924 ret
= MIN (MIN (r0
, r1
), r2
);
10925 recalculate_side_effects (*expr_p
);
10930 case VEC_PERM_EXPR
:
10931 /* Classified as tcc_expression. */
10934 case POINTER_PLUS_EXPR
:
10936 enum gimplify_status r0
, r1
;
10937 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
10938 post_p
, is_gimple_val
, fb_rvalue
);
10939 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
10940 post_p
, is_gimple_val
, fb_rvalue
);
10941 recalculate_side_effects (*expr_p
);
10942 ret
= MIN (r0
, r1
);
10946 case CILK_SYNC_STMT
:
10948 if (!fn_contains_cilk_spawn_p (cfun
))
10950 error_at (EXPR_LOCATION (*expr_p
),
10951 "expected %<_Cilk_spawn%> before %<_Cilk_sync%>");
10956 gimplify_cilk_sync (expr_p
, pre_p
);
10963 switch (TREE_CODE_CLASS (TREE_CODE (*expr_p
)))
10965 case tcc_comparison
:
10966 /* Handle comparison of objects of non scalar mode aggregates
10967 with a call to memcmp. It would be nice to only have to do
10968 this for variable-sized objects, but then we'd have to allow
10969 the same nest of reference nodes we allow for MODIFY_EXPR and
10970 that's too complex.
10972 Compare scalar mode aggregates as scalar mode values. Using
10973 memcmp for them would be very inefficient at best, and is
10974 plain wrong if bitfields are involved. */
10976 tree type
= TREE_TYPE (TREE_OPERAND (*expr_p
, 1));
10978 /* Vector comparisons need no boolification. */
10979 if (TREE_CODE (type
) == VECTOR_TYPE
)
10981 else if (!AGGREGATE_TYPE_P (type
))
10983 tree org_type
= TREE_TYPE (*expr_p
);
10984 *expr_p
= gimple_boolify (*expr_p
);
10985 if (!useless_type_conversion_p (org_type
,
10986 TREE_TYPE (*expr_p
)))
10988 *expr_p
= fold_convert_loc (input_location
,
10989 org_type
, *expr_p
);
10995 else if (TYPE_MODE (type
) != BLKmode
)
10996 ret
= gimplify_scalar_mode_aggregate_compare (expr_p
);
10998 ret
= gimplify_variable_sized_compare (expr_p
);
11003 /* If *EXPR_P does not need to be special-cased, handle it
11004 according to its class. */
11006 ret
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11007 post_p
, is_gimple_val
, fb_rvalue
);
11013 enum gimplify_status r0
, r1
;
11015 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11016 post_p
, is_gimple_val
, fb_rvalue
);
11017 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
11018 post_p
, is_gimple_val
, fb_rvalue
);
11020 ret
= MIN (r0
, r1
);
11026 enum gimplify_status r0
, r1
, r2
;
11028 r0
= gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
,
11029 post_p
, is_gimple_val
, fb_rvalue
);
11030 r1
= gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
,
11031 post_p
, is_gimple_val
, fb_rvalue
);
11032 r2
= gimplify_expr (&TREE_OPERAND (*expr_p
, 2), pre_p
,
11033 post_p
, is_gimple_val
, fb_rvalue
);
11035 ret
= MIN (MIN (r0
, r1
), r2
);
11039 case tcc_declaration
:
11042 goto dont_recalculate
;
11045 gcc_unreachable ();
11048 recalculate_side_effects (*expr_p
);
11054 gcc_assert (*expr_p
|| ret
!= GS_OK
);
11056 while (ret
== GS_OK
);
11058 /* If we encountered an error_mark somewhere nested inside, either
11059 stub out the statement or propagate the error back out. */
11060 if (ret
== GS_ERROR
)
11067 /* This was only valid as a return value from the langhook, which
11068 we handled. Make sure it doesn't escape from any other context. */
11069 gcc_assert (ret
!= GS_UNHANDLED
);
11071 if (fallback
== fb_none
&& *expr_p
&& !is_gimple_stmt (*expr_p
))
11073 /* We aren't looking for a value, and we don't have a valid
11074 statement. If it doesn't have side-effects, throw it away. */
11075 if (!TREE_SIDE_EFFECTS (*expr_p
))
11077 else if (!TREE_THIS_VOLATILE (*expr_p
))
11079 /* This is probably a _REF that contains something nested that
11080 has side effects. Recurse through the operands to find it. */
11081 enum tree_code code
= TREE_CODE (*expr_p
);
11085 case COMPONENT_REF
:
11086 case REALPART_EXPR
:
11087 case IMAGPART_EXPR
:
11088 case VIEW_CONVERT_EXPR
:
11089 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
11090 gimple_test_f
, fallback
);
11094 case ARRAY_RANGE_REF
:
11095 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
11096 gimple_test_f
, fallback
);
11097 gimplify_expr (&TREE_OPERAND (*expr_p
, 1), pre_p
, post_p
,
11098 gimple_test_f
, fallback
);
11102 /* Anything else with side-effects must be converted to
11103 a valid statement before we get here. */
11104 gcc_unreachable ();
11109 else if (COMPLETE_TYPE_P (TREE_TYPE (*expr_p
))
11110 && TYPE_MODE (TREE_TYPE (*expr_p
)) != BLKmode
)
11112 /* Historically, the compiler has treated a bare reference
11113 to a non-BLKmode volatile lvalue as forcing a load. */
11114 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (*expr_p
));
11116 /* Normally, we do not want to create a temporary for a
11117 TREE_ADDRESSABLE type because such a type should not be
11118 copied by bitwise-assignment. However, we make an
11119 exception here, as all we are doing here is ensuring that
11120 we read the bytes that make up the type. We use
11121 create_tmp_var_raw because create_tmp_var will abort when
11122 given a TREE_ADDRESSABLE type. */
11123 tree tmp
= create_tmp_var_raw (type
, "vol");
11124 gimple_add_tmp_var (tmp
);
11125 gimplify_assign (tmp
, *expr_p
, pre_p
);
11129 /* We can't do anything useful with a volatile reference to
11130 an incomplete type, so just throw it away. Likewise for
11131 a BLKmode type, since any implicit inner load should
11132 already have been turned into an explicit one by the
11133 gimplification process. */
11137 /* If we are gimplifying at the statement level, we're done. Tack
11138 everything together and return. */
11139 if (fallback
== fb_none
|| is_statement
)
11141 /* Since *EXPR_P has been converted into a GIMPLE tuple, clear
11142 it out for GC to reclaim it. */
11143 *expr_p
= NULL_TREE
;
11145 if (!gimple_seq_empty_p (internal_pre
)
11146 || !gimple_seq_empty_p (internal_post
))
11148 gimplify_seq_add_seq (&internal_pre
, internal_post
);
11149 gimplify_seq_add_seq (pre_p
, internal_pre
);
11152 /* The result of gimplifying *EXPR_P is going to be the last few
11153 statements in *PRE_P and *POST_P. Add location information
11154 to all the statements that were added by the gimplification
11156 if (!gimple_seq_empty_p (*pre_p
))
11157 annotate_all_with_location_after (*pre_p
, pre_last_gsi
, input_location
);
11159 if (!gimple_seq_empty_p (*post_p
))
11160 annotate_all_with_location_after (*post_p
, post_last_gsi
,
11166 #ifdef ENABLE_GIMPLE_CHECKING
11169 enum tree_code code
= TREE_CODE (*expr_p
);
11170 /* These expressions should already be in gimple IR form. */
11171 gcc_assert (code
!= MODIFY_EXPR
11172 && code
!= ASM_EXPR
11173 && code
!= BIND_EXPR
11174 && code
!= CATCH_EXPR
11175 && (code
!= COND_EXPR
|| gimplify_ctxp
->allow_rhs_cond_expr
)
11176 && code
!= EH_FILTER_EXPR
11177 && code
!= GOTO_EXPR
11178 && code
!= LABEL_EXPR
11179 && code
!= LOOP_EXPR
11180 && code
!= SWITCH_EXPR
11181 && code
!= TRY_FINALLY_EXPR
11182 && code
!= OACC_PARALLEL
11183 && code
!= OACC_KERNELS
11184 && code
!= OACC_DATA
11185 && code
!= OACC_HOST_DATA
11186 && code
!= OACC_DECLARE
11187 && code
!= OACC_UPDATE
11188 && code
!= OACC_ENTER_DATA
11189 && code
!= OACC_EXIT_DATA
11190 && code
!= OACC_CACHE
11191 && code
!= OMP_CRITICAL
11193 && code
!= OACC_LOOP
11194 && code
!= OMP_MASTER
11195 && code
!= OMP_TASKGROUP
11196 && code
!= OMP_ORDERED
11197 && code
!= OMP_PARALLEL
11198 && code
!= OMP_SECTIONS
11199 && code
!= OMP_SECTION
11200 && code
!= OMP_SINGLE
);
11204 /* Otherwise we're gimplifying a subexpression, so the resulting
11205 value is interesting. If it's a valid operand that matches
11206 GIMPLE_TEST_F, we're done. Unless we are handling some
11207 post-effects internally; if that's the case, we need to copy into
11208 a temporary before adding the post-effects to POST_P. */
11209 if (gimple_seq_empty_p (internal_post
) && (*gimple_test_f
) (*expr_p
))
11212 /* Otherwise, we need to create a new temporary for the gimplified
11215 /* We can't return an lvalue if we have an internal postqueue. The
11216 object the lvalue refers to would (probably) be modified by the
11217 postqueue; we need to copy the value out first, which means an
11219 if ((fallback
& fb_lvalue
)
11220 && gimple_seq_empty_p (internal_post
)
11221 && is_gimple_addressable (*expr_p
))
11223 /* An lvalue will do. Take the address of the expression, store it
11224 in a temporary, and replace the expression with an INDIRECT_REF of
11226 tmp
= build_fold_addr_expr_loc (input_location
, *expr_p
);
11227 gimplify_expr (&tmp
, pre_p
, post_p
, is_gimple_reg
, fb_rvalue
);
11228 *expr_p
= build_simple_mem_ref (tmp
);
11230 else if ((fallback
& fb_rvalue
) && is_gimple_reg_rhs_or_call (*expr_p
))
11232 /* An rvalue will do. Assign the gimplified expression into a
11233 new temporary TMP and replace the original expression with
11234 TMP. First, make sure that the expression has a type so that
11235 it can be assigned into a temporary. */
11236 gcc_assert (!VOID_TYPE_P (TREE_TYPE (*expr_p
)));
11237 *expr_p
= get_formal_tmp_var (*expr_p
, pre_p
);
11241 #ifdef ENABLE_GIMPLE_CHECKING
11242 if (!(fallback
& fb_mayfail
))
11244 fprintf (stderr
, "gimplification failed:\n");
11245 print_generic_expr (stderr
, *expr_p
, 0);
11246 debug_tree (*expr_p
);
11247 internal_error ("gimplification failed");
11250 gcc_assert (fallback
& fb_mayfail
);
11252 /* If this is an asm statement, and the user asked for the
11253 impossible, don't die. Fail and let gimplify_asm_expr
11259 /* Make sure the temporary matches our predicate. */
11260 gcc_assert ((*gimple_test_f
) (*expr_p
));
11262 if (!gimple_seq_empty_p (internal_post
))
11264 annotate_all_with_location (internal_post
, input_location
);
11265 gimplify_seq_add_seq (pre_p
, internal_post
);
11269 input_location
= saved_location
;
11273 /* Like gimplify_expr but make sure the gimplified result is not itself
11274 a SSA name (but a decl if it were). Temporaries required by
11275 evaluating *EXPR_P may be still SSA names. */
11277 static enum gimplify_status
11278 gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
,
11279 bool (*gimple_test_f
) (tree
), fallback_t fallback
,
11282 bool was_ssa_name_p
= TREE_CODE (*expr_p
) == SSA_NAME
;
11283 enum gimplify_status ret
= gimplify_expr (expr_p
, pre_p
, post_p
,
11284 gimple_test_f
, fallback
);
11286 && TREE_CODE (*expr_p
) == SSA_NAME
)
11288 tree name
= *expr_p
;
11289 if (was_ssa_name_p
)
11290 *expr_p
= get_initialized_tmp_var (*expr_p
, pre_p
, NULL
, false);
11293 /* Avoid the extra copy if possible. */
11294 *expr_p
= create_tmp_reg (TREE_TYPE (name
));
11295 gimple_set_lhs (SSA_NAME_DEF_STMT (name
), *expr_p
);
11296 release_ssa_name (name
);
11302 /* Look through TYPE for variable-sized objects and gimplify each such
11303 size that we find. Add to LIST_P any statements generated. */
11306 gimplify_type_sizes (tree type
, gimple_seq
*list_p
)
11310 if (type
== NULL
|| type
== error_mark_node
)
11313 /* We first do the main variant, then copy into any other variants. */
11314 type
= TYPE_MAIN_VARIANT (type
);
11316 /* Avoid infinite recursion. */
11317 if (TYPE_SIZES_GIMPLIFIED (type
))
11320 TYPE_SIZES_GIMPLIFIED (type
) = 1;
11322 switch (TREE_CODE (type
))
11325 case ENUMERAL_TYPE
:
11328 case FIXED_POINT_TYPE
:
11329 gimplify_one_sizepos (&TYPE_MIN_VALUE (type
), list_p
);
11330 gimplify_one_sizepos (&TYPE_MAX_VALUE (type
), list_p
);
11332 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
11334 TYPE_MIN_VALUE (t
) = TYPE_MIN_VALUE (type
);
11335 TYPE_MAX_VALUE (t
) = TYPE_MAX_VALUE (type
);
11340 /* These types may not have declarations, so handle them here. */
11341 gimplify_type_sizes (TREE_TYPE (type
), list_p
);
11342 gimplify_type_sizes (TYPE_DOMAIN (type
), list_p
);
11343 /* Ensure VLA bounds aren't removed, for -O0 they should be variables
11344 with assigned stack slots, for -O1+ -g they should be tracked
11346 if (!(TYPE_NAME (type
)
11347 && TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
11348 && DECL_IGNORED_P (TYPE_NAME (type
)))
11349 && TYPE_DOMAIN (type
)
11350 && INTEGRAL_TYPE_P (TYPE_DOMAIN (type
)))
11352 t
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
11353 if (t
&& TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
))
11354 DECL_IGNORED_P (t
) = 0;
11355 t
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
11356 if (t
&& TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
))
11357 DECL_IGNORED_P (t
) = 0;
11363 case QUAL_UNION_TYPE
:
11364 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
11365 if (TREE_CODE (field
) == FIELD_DECL
)
11367 gimplify_one_sizepos (&DECL_FIELD_OFFSET (field
), list_p
);
11368 gimplify_one_sizepos (&DECL_SIZE (field
), list_p
);
11369 gimplify_one_sizepos (&DECL_SIZE_UNIT (field
), list_p
);
11370 gimplify_type_sizes (TREE_TYPE (field
), list_p
);
11375 case REFERENCE_TYPE
:
11376 /* We used to recurse on the pointed-to type here, which turned out to
11377 be incorrect because its definition might refer to variables not
11378 yet initialized at this point if a forward declaration is involved.
11380 It was actually useful for anonymous pointed-to types to ensure
11381 that the sizes evaluation dominates every possible later use of the
11382 values. Restricting to such types here would be safe since there
11383 is no possible forward declaration around, but would introduce an
11384 undesirable middle-end semantic to anonymity. We then defer to
11385 front-ends the responsibility of ensuring that the sizes are
11386 evaluated both early and late enough, e.g. by attaching artificial
11387 type declarations to the tree. */
11394 gimplify_one_sizepos (&TYPE_SIZE (type
), list_p
);
11395 gimplify_one_sizepos (&TYPE_SIZE_UNIT (type
), list_p
);
11397 for (t
= TYPE_NEXT_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
11399 TYPE_SIZE (t
) = TYPE_SIZE (type
);
11400 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
11401 TYPE_SIZES_GIMPLIFIED (t
) = 1;
11405 /* A subroutine of gimplify_type_sizes to make sure that *EXPR_P,
11406 a size or position, has had all of its SAVE_EXPRs evaluated.
11407 We add any required statements to *STMT_P. */
11410 gimplify_one_sizepos (tree
*expr_p
, gimple_seq
*stmt_p
)
11412 tree expr
= *expr_p
;
11414 /* We don't do anything if the value isn't there, is constant, or contains
11415 A PLACEHOLDER_EXPR. We also don't want to do anything if it's already
11416 a VAR_DECL. If it's a VAR_DECL from another function, the gimplifier
11417 will want to replace it with a new variable, but that will cause problems
11418 if this type is from outside the function. It's OK to have that here. */
11419 if (is_gimple_sizepos (expr
))
11422 *expr_p
= unshare_expr (expr
);
11424 /* SSA names in decl/type fields are a bad idea - they'll get reclaimed
11425 if the def vanishes. */
11426 gimplify_expr (expr_p
, stmt_p
, NULL
, is_gimple_val
, fb_rvalue
, false);
11429 /* Gimplify the body of statements of FNDECL and return a GIMPLE_BIND node
11430 containing the sequence of corresponding GIMPLE statements. If DO_PARMS
11431 is true, also gimplify the parameters. */
11434 gimplify_body (tree fndecl
, bool do_parms
)
11436 location_t saved_location
= input_location
;
11437 gimple_seq parm_stmts
, seq
;
11438 gimple
*outer_stmt
;
11440 struct cgraph_node
*cgn
;
11442 timevar_push (TV_TREE_GIMPLIFY
);
11444 init_tree_ssa (cfun
);
11446 /* Initialize for optimize_insn_for_s{ize,peed}_p possibly called during
11448 default_rtl_profile ();
11450 gcc_assert (gimplify_ctxp
== NULL
);
11451 push_gimplify_context (true);
11453 if (flag_openacc
|| flag_openmp
)
11455 gcc_assert (gimplify_omp_ctxp
== NULL
);
11456 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (fndecl
)))
11457 gimplify_omp_ctxp
= new_omp_context (ORT_TARGET
);
11460 /* Unshare most shared trees in the body and in that of any nested functions.
11461 It would seem we don't have to do this for nested functions because
11462 they are supposed to be output and then the outer function gimplified
11463 first, but the g++ front end doesn't always do it that way. */
11464 unshare_body (fndecl
);
11465 unvisit_body (fndecl
);
11467 cgn
= cgraph_node::get (fndecl
);
11468 if (cgn
&& cgn
->origin
)
11469 nonlocal_vlas
= new hash_set
<tree
>;
11471 /* Make sure input_location isn't set to something weird. */
11472 input_location
= DECL_SOURCE_LOCATION (fndecl
);
11474 /* Resolve callee-copies. This has to be done before processing
11475 the body so that DECL_VALUE_EXPR gets processed correctly. */
11476 parm_stmts
= do_parms
? gimplify_parameters () : NULL
;
11478 /* Gimplify the function's body. */
11480 gimplify_stmt (&DECL_SAVED_TREE (fndecl
), &seq
);
11481 outer_stmt
= gimple_seq_first_stmt (seq
);
11484 outer_stmt
= gimple_build_nop ();
11485 gimplify_seq_add_stmt (&seq
, outer_stmt
);
11488 /* The body must contain exactly one statement, a GIMPLE_BIND. If this is
11489 not the case, wrap everything in a GIMPLE_BIND to make it so. */
11490 if (gimple_code (outer_stmt
) == GIMPLE_BIND
11491 && gimple_seq_first (seq
) == gimple_seq_last (seq
))
11492 outer_bind
= as_a
<gbind
*> (outer_stmt
);
11494 outer_bind
= gimple_build_bind (NULL_TREE
, seq
, NULL
);
11496 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
11498 /* If we had callee-copies statements, insert them at the beginning
11499 of the function and clear DECL_VALUE_EXPR_P on the parameters. */
11500 if (!gimple_seq_empty_p (parm_stmts
))
11504 gimplify_seq_add_seq (&parm_stmts
, gimple_bind_body (outer_bind
));
11505 gimple_bind_set_body (outer_bind
, parm_stmts
);
11507 for (parm
= DECL_ARGUMENTS (current_function_decl
);
11508 parm
; parm
= DECL_CHAIN (parm
))
11509 if (DECL_HAS_VALUE_EXPR_P (parm
))
11511 DECL_HAS_VALUE_EXPR_P (parm
) = 0;
11512 DECL_IGNORED_P (parm
) = 0;
11518 if (nonlocal_vla_vars
)
11520 /* tree-nested.c may later on call declare_vars (..., true);
11521 which relies on BLOCK_VARS chain to be the tail of the
11522 gimple_bind_vars chain. Ensure we don't violate that
11524 if (gimple_bind_block (outer_bind
)
11525 == DECL_INITIAL (current_function_decl
))
11526 declare_vars (nonlocal_vla_vars
, outer_bind
, true);
11528 BLOCK_VARS (DECL_INITIAL (current_function_decl
))
11529 = chainon (BLOCK_VARS (DECL_INITIAL (current_function_decl
)),
11530 nonlocal_vla_vars
);
11531 nonlocal_vla_vars
= NULL_TREE
;
11533 delete nonlocal_vlas
;
11534 nonlocal_vlas
= NULL
;
11537 if ((flag_openacc
|| flag_openmp
|| flag_openmp_simd
)
11538 && gimplify_omp_ctxp
)
11540 delete_omp_context (gimplify_omp_ctxp
);
11541 gimplify_omp_ctxp
= NULL
;
11544 pop_gimplify_context (outer_bind
);
11545 gcc_assert (gimplify_ctxp
== NULL
);
11547 if (flag_checking
&& !seen_error ())
11548 verify_gimple_in_seq (gimple_bind_body (outer_bind
));
11550 timevar_pop (TV_TREE_GIMPLIFY
);
11551 input_location
= saved_location
;
11556 typedef char *char_p
; /* For DEF_VEC_P. */
11558 /* Return whether we should exclude FNDECL from instrumentation. */
11561 flag_instrument_functions_exclude_p (tree fndecl
)
11565 v
= (vec
<char_p
> *) flag_instrument_functions_exclude_functions
;
11566 if (v
&& v
->length () > 0)
11572 name
= lang_hooks
.decl_printable_name (fndecl
, 0);
11573 FOR_EACH_VEC_ELT (*v
, i
, s
)
11574 if (strstr (name
, s
) != NULL
)
11578 v
= (vec
<char_p
> *) flag_instrument_functions_exclude_files
;
11579 if (v
&& v
->length () > 0)
11585 name
= DECL_SOURCE_FILE (fndecl
);
11586 FOR_EACH_VEC_ELT (*v
, i
, s
)
11587 if (strstr (name
, s
) != NULL
)
11594 /* Entry point to the gimplification pass. FNDECL is the FUNCTION_DECL
11595 node for the function we want to gimplify.
11597 Return the sequence of GIMPLE statements corresponding to the body
11601 gimplify_function_tree (tree fndecl
)
11607 gcc_assert (!gimple_body (fndecl
));
11609 if (DECL_STRUCT_FUNCTION (fndecl
))
11610 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
11612 push_struct_function (fndecl
);
11614 /* Tentatively set PROP_gimple_lva here, and reset it in gimplify_va_arg_expr
11616 cfun
->curr_properties
|= PROP_gimple_lva
;
11618 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
11620 /* Preliminarily mark non-addressed complex variables as eligible
11621 for promotion to gimple registers. We'll transform their uses
11622 as we find them. */
11623 if ((TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
11624 || TREE_CODE (TREE_TYPE (parm
)) == VECTOR_TYPE
)
11625 && !TREE_THIS_VOLATILE (parm
)
11626 && !needs_to_live_in_memory (parm
))
11627 DECL_GIMPLE_REG_P (parm
) = 1;
11630 ret
= DECL_RESULT (fndecl
);
11631 if ((TREE_CODE (TREE_TYPE (ret
)) == COMPLEX_TYPE
11632 || TREE_CODE (TREE_TYPE (ret
)) == VECTOR_TYPE
)
11633 && !needs_to_live_in_memory (ret
))
11634 DECL_GIMPLE_REG_P (ret
) = 1;
11636 bind
= gimplify_body (fndecl
, true);
11638 /* The tree body of the function is no longer needed, replace it
11639 with the new GIMPLE body. */
11641 gimple_seq_add_stmt (&seq
, bind
);
11642 gimple_set_body (fndecl
, seq
);
11644 /* If we're instrumenting function entry/exit, then prepend the call to
11645 the entry hook and wrap the whole function in a TRY_FINALLY_EXPR to
11646 catch the exit hook. */
11647 /* ??? Add some way to ignore exceptions for this TFE. */
11648 if (flag_instrument_function_entry_exit
11649 && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl
)
11650 && !flag_instrument_functions_exclude_p (fndecl
))
11655 gimple_seq cleanup
= NULL
, body
= NULL
;
11659 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
11660 call
= gimple_build_call (x
, 1, integer_zero_node
);
11661 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
11662 gimple_call_set_lhs (call
, tmp_var
);
11663 gimplify_seq_add_stmt (&cleanup
, call
);
11664 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_EXIT
);
11665 call
= gimple_build_call (x
, 2,
11666 build_fold_addr_expr (current_function_decl
),
11668 gimplify_seq_add_stmt (&cleanup
, call
);
11669 tf
= gimple_build_try (seq
, cleanup
, GIMPLE_TRY_FINALLY
);
11671 x
= builtin_decl_implicit (BUILT_IN_RETURN_ADDRESS
);
11672 call
= gimple_build_call (x
, 1, integer_zero_node
);
11673 tmp_var
= create_tmp_var (ptr_type_node
, "return_addr");
11674 gimple_call_set_lhs (call
, tmp_var
);
11675 gimplify_seq_add_stmt (&body
, call
);
11676 x
= builtin_decl_implicit (BUILT_IN_PROFILE_FUNC_ENTER
);
11677 call
= gimple_build_call (x
, 2,
11678 build_fold_addr_expr (current_function_decl
),
11680 gimplify_seq_add_stmt (&body
, call
);
11681 gimplify_seq_add_stmt (&body
, tf
);
11682 new_bind
= gimple_build_bind (NULL
, body
, gimple_bind_block (bind
));
11683 /* Clear the block for BIND, since it is no longer directly inside
11684 the function, but within a try block. */
11685 gimple_bind_set_block (bind
, NULL
);
11687 /* Replace the current function body with the body
11688 wrapped in the try/finally TF. */
11690 gimple_seq_add_stmt (&seq
, new_bind
);
11691 gimple_set_body (fndecl
, seq
);
11695 if ((flag_sanitize
& SANITIZE_THREAD
) != 0
11696 && !lookup_attribute ("no_sanitize_thread", DECL_ATTRIBUTES (fndecl
)))
11698 gcall
*call
= gimple_build_call_internal (IFN_TSAN_FUNC_EXIT
, 0);
11699 gimple
*tf
= gimple_build_try (seq
, call
, GIMPLE_TRY_FINALLY
);
11700 gbind
*new_bind
= gimple_build_bind (NULL
, tf
, gimple_bind_block (bind
));
11701 /* Clear the block for BIND, since it is no longer directly inside
11702 the function, but within a try block. */
11703 gimple_bind_set_block (bind
, NULL
);
11704 /* Replace the current function body with the body
11705 wrapped in the try/finally TF. */
11707 gimple_seq_add_stmt (&seq
, new_bind
);
11708 gimple_set_body (fndecl
, seq
);
11711 DECL_SAVED_TREE (fndecl
) = NULL_TREE
;
11712 cfun
->curr_properties
|= PROP_gimple_any
;
11716 dump_function (TDI_generic
, fndecl
);
11719 /* Return a dummy expression of type TYPE in order to keep going after an
11723 dummy_object (tree type
)
11725 tree t
= build_int_cst (build_pointer_type (type
), 0);
11726 return build2 (MEM_REF
, type
, t
, t
);
11729 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
11730 builtin function, but a very special sort of operator. */
11732 enum gimplify_status
11733 gimplify_va_arg_expr (tree
*expr_p
, gimple_seq
*pre_p
,
11734 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
11736 tree promoted_type
, have_va_type
;
11737 tree valist
= TREE_OPERAND (*expr_p
, 0);
11738 tree type
= TREE_TYPE (*expr_p
);
11739 tree t
, tag
, aptag
;
11740 location_t loc
= EXPR_LOCATION (*expr_p
);
11742 /* Verify that valist is of the proper type. */
11743 have_va_type
= TREE_TYPE (valist
);
11744 if (have_va_type
== error_mark_node
)
11746 have_va_type
= targetm
.canonical_va_list_type (have_va_type
);
11748 if (have_va_type
== NULL_TREE
)
11750 error_at (loc
, "first argument to %<va_arg%> not of type %<va_list%>");
11754 /* Generate a diagnostic for requesting data of a type that cannot
11755 be passed through `...' due to type promotion at the call site. */
11756 if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
11759 static bool gave_help
;
11761 /* Use the expansion point to handle cases such as passing bool (defined
11762 in a system header) through `...'. */
11763 source_location xloc
11764 = expansion_point_location_if_in_system_header (loc
);
11766 /* Unfortunately, this is merely undefined, rather than a constraint
11767 violation, so we cannot make this an error. If this call is never
11768 executed, the program is still strictly conforming. */
11769 warned
= warning_at (xloc
, 0,
11770 "%qT is promoted to %qT when passed through %<...%>",
11771 type
, promoted_type
);
11772 if (!gave_help
&& warned
)
11775 inform (xloc
, "(so you should pass %qT not %qT to %<va_arg%>)",
11776 promoted_type
, type
);
11779 /* We can, however, treat "undefined" any way we please.
11780 Call abort to encourage the user to fix the program. */
11782 inform (xloc
, "if this code is reached, the program will abort");
11783 /* Before the abort, allow the evaluation of the va_list
11784 expression to exit or longjmp. */
11785 gimplify_and_add (valist
, pre_p
);
11786 t
= build_call_expr_loc (loc
,
11787 builtin_decl_implicit (BUILT_IN_TRAP
), 0);
11788 gimplify_and_add (t
, pre_p
);
11790 /* This is dead code, but go ahead and finish so that the
11791 mode of the result comes out right. */
11792 *expr_p
= dummy_object (type
);
11793 return GS_ALL_DONE
;
11796 tag
= build_int_cst (build_pointer_type (type
), 0);
11797 aptag
= build_int_cst (TREE_TYPE (valist
), 0);
11799 *expr_p
= build_call_expr_internal_loc (loc
, IFN_VA_ARG
, type
, 3,
11800 valist
, tag
, aptag
);
11802 /* Clear the tentatively set PROP_gimple_lva, to indicate that IFN_VA_ARG
11803 needs to be expanded. */
11804 cfun
->curr_properties
&= ~PROP_gimple_lva
;
11809 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
11811 DST/SRC are the destination and source respectively. You can pass
11812 ungimplified trees in DST or SRC, in which case they will be
11813 converted to a gimple operand if necessary.
11815 This function returns the newly created GIMPLE_ASSIGN tuple. */
11818 gimplify_assign (tree dst
, tree src
, gimple_seq
*seq_p
)
11820 tree t
= build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
11821 gimplify_and_add (t
, seq_p
);
11823 return gimple_seq_last_stmt (*seq_p
);
11827 gimplify_hasher::hash (const elt_t
*p
)
11830 return iterative_hash_expr (t
, 0);
11834 gimplify_hasher::equal (const elt_t
*p1
, const elt_t
*p2
)
11838 enum tree_code code
= TREE_CODE (t1
);
11840 if (TREE_CODE (t2
) != code
11841 || TREE_TYPE (t1
) != TREE_TYPE (t2
))
11844 if (!operand_equal_p (t1
, t2
, 0))
11847 /* Only allow them to compare equal if they also hash equal; otherwise
11848 results are nondeterminate, and we fail bootstrap comparison. */
11849 gcc_checking_assert (hash (p1
) == hash (p2
));